FD.io VPP  v18.07.1-13-g909ba93
Vector Packet Processing
jvpp_registry.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #define _GNU_SOURCE /* for strcasestr(3) */
16 #include <vnet/vnet.h>
17 
18 #define vl_api_version(n,v) static u32 vpe_api_version = (v);
19 #include <vpp/api/vpe.api.h>
20 #undef vl_api_version
21 
22 
23 #include <jni.h>
25 #include "io_fd_vpp_jvpp_VppJNIConnection.h"
26 #include "io_fd_vpp_jvpp_JVppRegistryImpl.h"
27 
28 #include <vpp/api/vpe_msg_enum.h>
29 #define vl_typedefs /* define message structures */
30 #include <vpp/api/vpe_all_api_h.h>
31 #undef vl_typedefs
32 
33 #define vl_endianfun
34 #include <vpp/api/vpe_all_api_h.h>
35 #undef vl_endianfun
36 
37 /* instantiate all the print functions we know about */
38 #define vl_print(handle, ...)
39 #define vl_printfun
40 #include <vpp/api/vpe_all_api_h.h>
41 #undef vl_printfun
42 
45 
46 /*
47  * The Java runtime isn't compile w/ -fstack-protector,
48  * so we have to supply missing external references for the
49  * regular vpp libraries.
50  */
51 void __stack_chk_guard(void) __attribute__((weak));
52 void __stack_chk_guard(void) {
53 }
54 
55 #define CONTROL_PING_MESSAGE "control_ping"
56 #define CONTROL_PING_REPLY_MESSAGE "control_ping_reply"
57 
58 typedef struct {
59  /* UThread attachment */
62 
63  /* Control ping callback */
64  jobject registryObject;
65  jclass registryClass;
70 
71  /* Thread cleanup */
72  pthread_key_t cleanup_rx_thread_key;
73 
74  /* Connected indication */
75  volatile u8 is_connected;
78 
79 jvpp_registry_main_t jvpp_registry_main __attribute__((aligned (64)));
80 
82  /*
83  * Send the main API signature in slot 0. This bit of code must
84  * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
85  */
86  mp->api_versions[0] = clib_host_to_net_u32(vpe_api_version);
87 }
88 
89 /* cleanup handler for RX thread */
91  jvpp_main_t * jm = &jvpp_main;
93 
94  vppjni_lock(jm, 99);
95 
96  int getEnvStat = (*jm->jvm)->GetEnv(jm->jvm, (void **) &(jm->jenv),
97  JNI_VERSION_1_8);
98  if (getEnvStat == JNI_EVERSION) {
99  clib_warning("Unsupported JNI version\n");
100  rm->control_ping_retval = VNET_API_ERROR_UNSUPPORTED_JNI_VERSION;
101  goto out;
102  } else if (getEnvStat != JNI_EDETACHED) {
103  (*jm->jvm)->DetachCurrentThread(jm->jvm);
104  }
105  out: vppjni_unlock(jm);
106 }
107 
110  jvpp_main_t * jm = &jvpp_main;
112  char was_thread_connected = 0;
113 
114  // attach to java thread if not attached
115  int getEnvStat = (*jm->jvm)->GetEnv(jm->jvm, (void **) &(jm->jenv),
116  JNI_VERSION_1_8);
117  if (getEnvStat == JNI_EDETACHED) {
118  if ((*jm->jvm)->AttachCurrentThread(jm->jvm, (void **) &(jm->jenv),
119  NULL) != 0) {
120  clib_warning("Failed to attach thread\n");
121  rm->control_ping_retval =
122  VNET_API_ERROR_FAILED_TO_ATTACH_TO_JAVA_THREAD;
123  goto out;
124  }
125 
126  // workaround as we can't use pthread_cleanup_push
127  pthread_key_create(&rm->cleanup_rx_thread_key, cleanup_rx_thread);
128  // destructor is only called if the value of key is non null
129  pthread_setspecific(rm->cleanup_rx_thread_key, (void *) 1);
130  was_thread_connected = 1;
131  } else if (getEnvStat == JNI_EVERSION) {
132  clib_warning("Unsupported JNI version\n");
133  rm->control_ping_retval = VNET_API_ERROR_UNSUPPORTED_JNI_VERSION;
134  goto out;
135  }
136 
137  if (was_thread_connected == 0) {
138  JNIEnv *env = jm->jenv;
139  if (mp->retval < 0) {
140  call_on_error("controlPing", mp->context, mp->retval,
141  rm->registryClass, rm->registryObject,
143  } else {
144  jmethodID constructor = (*env)->GetMethodID(env,
145  rm->controlPingReplyClass, "<init>", "()V");
146  jmethodID callbackMethod = (*env)->GetMethodID(env,
147  rm->registryClass, "onControlPingReply",
148  "(Lio/fd/vpp/jvpp/dto/ControlPingReply;)V");
149 
150  jobject dto = (*env)->NewObject(env, rm->controlPingReplyClass,
151  constructor);
152 
153  jfieldID contextFieldId = (*env)->GetFieldID(env,
154  rm->controlPingReplyClass, "context", "I");
155  (*env)->SetIntField(env, dto, contextFieldId,
156  clib_net_to_host_u32(mp->context));
157 
158  jfieldID clientIndexFieldId = (*env)->GetFieldID(env,
159  rm->controlPingReplyClass, "clientIndex", "I");
160  (*env)->SetIntField(env, dto, clientIndexFieldId,
161  clib_net_to_host_u32(mp->client_index));
162 
163  jfieldID vpePidFieldId = (*env)->GetFieldID(env,
164  rm->controlPingReplyClass, "vpePid", "I");
165  (*env)->SetIntField(env, dto, vpePidFieldId,
166  clib_net_to_host_u32(mp->vpe_pid));
167 
168  (*env)->CallVoidMethod(env, rm->registryObject, callbackMethod,
169  dto);
170  (*env)->DeleteLocalRef(env, dto);
171  }
172  }
173 
174  out: rm->vpe_pid = clib_net_to_host_u32(mp->vpe_pid);
176 }
177 
178 static int find_ping_id() {
179  int rv = 0;
180  jvpp_main_t * jm = &jvpp_main;
182  api_main_t *am = &api_main;
183  hash_pair_t *hp;
185 
186  rm->control_ping_msg_id = -1;
187  rm->control_ping_reply_msg_id = -1;
188 
190  ({
191  char *key = (char *)hp->key; // key format: name_crc
192  int msg_name_len = strlen(key) - 9; // ignore crc
193  if (strlen(CONTROL_PING_MESSAGE) == msg_name_len &&
194  strncmp(CONTROL_PING_MESSAGE, (char *)hp->key, msg_name_len) == 0) {
195  rm->control_ping_msg_id = (u32)hp->value[0];
196  }
197  if (strlen(CONTROL_PING_REPLY_MESSAGE) == msg_name_len &&
198  strncmp(CONTROL_PING_REPLY_MESSAGE, (char *)hp->key, msg_name_len) == 0) {
199  rm->control_ping_reply_msg_id = (u32)hp->value[0];
200  }
201  }));
202  if (rm->control_ping_msg_id == -1) {
203  clib_warning("failed to find id for %s", CONTROL_PING_MESSAGE);
204  rv = -1;
205  }
206  if (rm->control_ping_reply_msg_id == -1) {
207  clib_warning("failed to find id for %s", CONTROL_PING_REPLY_MESSAGE);
208  rv = -1;
209  }
210  return rv;
211 }
212 
214  f64 timeout;
215  clib_time_t clib_time;
217  jvpp_main_t * jm = &jvpp_main;
219 
220  clib_time_init(&clib_time);
221 
223  mp = vl_msg_api_alloc(sizeof(*mp));
224  memset(mp, 0, sizeof(*mp));
225  mp->_vl_msg_id = ntohs(rm->control_ping_msg_id);
226  mp->client_index = jm->my_client_index;
227 
228  // send message:
230 
231  // wait for results: Current time + 10 seconds is the timeout
232  timeout = clib_time_now(&clib_time) + 10.0;
233  int rv = VNET_API_ERROR_RESPONSE_NOT_READY;
234  while (clib_time_now(&clib_time) < timeout) {
235  if (rm->control_ping_result_ready == 1) {
236  rv = rm->control_ping_retval;
237  break;
238  }
239  }
240 
241  if (rv != 0) {
243  clib_warning("first control ping failed: %d", rv);
244  }
245  return rv;
246 }
247 
248 static int connect_to_vpe(char *shm_prefix, char *name) {
249  jvpp_main_t * jm = &jvpp_main;
250  api_main_t * am = &api_main;
252 
253  if (vl_client_connect_to_vlib(shm_prefix, name, 32) < 0)
254  return -1;
256 
258 
259  if (find_ping_id() < 0)
260  return -1;
261 
264  vl_api_control_ping_reply_t_endian,
265  vl_api_control_ping_reply_t_print,
266  sizeof(vl_api_control_ping_reply_t), 1);
267 
268  return send_initial_control_ping();
269 }
270 
272  JNIEnv *env, jclass obj, jstring shmPrefix, jstring clientName) {
273  /*
274  * TODO introducing memory prefix as variable can be used in hc2vpp
275  * to be able to run without root privileges
276  * https://jira.fd.io/browse/HC2VPP-176
277  */
278  int rv;
279  const char *client_name;
280  const char *shm_prefix;
281  void vl_msg_reply_handler_hookup(void);
282  jvpp_main_t * jm = &jvpp_main;
284 
285  jclass connectionInfoClass = (*env)->FindClass(env,
286  "io/fd/vpp/jvpp/VppJNIConnection$ConnectionInfo");
287  jmethodID connectionInfoConstructor = (*env)->GetMethodID(env,
288  connectionInfoClass, "<init>", "(JIII)V");
289 
290  if (rm->is_connected) {
291  return (*env)->NewObject(env, connectionInfoClass,
292  connectionInfoConstructor, 0, 0,
293  VNET_API_ERROR_ALREADY_CONNECTED, 0);
294  }
295 
296  client_name = (*env)->GetStringUTFChars(env, clientName, 0);
297  shm_prefix = (*env)->GetStringUTFChars(env, shmPrefix, 0);
298 
299  if (!client_name) {
300  return (*env)->NewObject(env, connectionInfoClass,
301  connectionInfoConstructor, 0, 0, VNET_API_ERROR_INVALID_VALUE, 0, shmPrefix);
302  }
303 
304  if (!shm_prefix) {
305  return (*env)->NewObject(env, connectionInfoClass,
306  connectionInfoConstructor, 0, 0, VNET_API_ERROR_INVALID_VALUE, 0, shmPrefix);
307  }
308 
309  rv = connect_to_vpe((char *) shm_prefix, (char *) client_name);
310 
311  if (rv < 0)
312  clib_warning("connection failed, rv %d", rv);
313 
314  (*env)->ReleaseStringUTFChars(env, clientName, client_name);
315  (*env)->ReleaseStringUTFChars(env, shmPrefix, shm_prefix);
316 
317  return (*env)->NewObject(env, connectionInfoClass,
318  connectionInfoConstructor, (jlong) pointer_to_uword (jm->vl_input_queue),
319  (jint) jm->my_client_index, (jint) rv, (jint) rm->vpe_pid);
320 }
321 
323  JNIEnv *env, jobject regstryObject) {
324  jvpp_main_t * jm = &jvpp_main;
326  u32 my_context_id = vppjni_get_context_id(&jvpp_main);
328 
329  if (rm->registryObject == 0) {
330  rm->registryObject = (*env)->NewGlobalRef(env, regstryObject);
331  }
332  if (rm->registryClass == 0) {
333  rm->registryClass = (jclass) (*env)->NewGlobalRef(env,
334  (*env)->GetObjectClass(env, regstryObject));
335  }
336 
337  mp = vl_msg_api_alloc(sizeof(*mp));
338  memset(mp, 0, sizeof(*mp));
339  mp->_vl_msg_id = ntohs(rm->control_ping_msg_id);
340  mp->client_index = jm->my_client_index;
341  mp->context = clib_host_to_net_u32(my_context_id);
342 
343  // send message:
345  return my_context_id;
346 }
347 
349  JNIEnv *env, jclass clazz) {
351  rm->is_connected = 0; // TODO make thread safe
353 
354  // cleanup:
355  if (rm->registryObject) {
356  (*env)->DeleteGlobalRef(env, rm->registryObject);
357  rm->registryObject = 0;
358  }
359  if (rm->registryClass) {
360  (*env)->DeleteGlobalRef(env, rm->registryClass);
361  rm->registryClass = 0;
362  }
363 }
364 
365 jint JNI_OnLoad(JavaVM *vm, void *reserved) {
366  jvpp_main_t * jm = &jvpp_main;
368  JNIEnv* env;
369 
370  if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
371  return JNI_EVERSION;
372  }
373 
374  rm->controlPingReplyClass = (jclass) (*env)->NewGlobalRef(env,
375  (*env)->FindClass(env, "io/fd/vpp/jvpp/dto/ControlPingReply"));
376  if ((*env)->ExceptionCheck(env)) {
377  (*env)->ExceptionDescribe(env);
378  clib_warning("Failed to cache class references\n");
379  return JNI_ERR;
380  }
381 
382  rm->callbackExceptionClass = (jclass) (*env)->NewGlobalRef(env,
383  (*env)->FindClass(env, "io/fd/vpp/jvpp/VppCallbackException"));
384  if ((*env)->ExceptionCheck(env)) {
385  (*env)->ExceptionDescribe(env);
386  return JNI_ERR;
387  }
388 
389  jm->jvm = vm;
390  return JNI_VERSION_1_8;
391 }
392 
393 void JNI_OnUnload(JavaVM *vm, void *reserved) {
394  jvpp_main_t * jm = &jvpp_main;
395  JNIEnv* env;
396  if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
397  return;
398  }
399 
400  jm->jenv = NULL;
401  jm->jvm = NULL;
402 }
volatile u32 control_ping_result_ready
Definition: jvpp_registry.c:60
static int connect_to_vpe(char *shm_prefix, char *name)
static void vl_api_control_ping_reply_t_handler(vl_api_control_ping_reply_t *mp)
int vl_client_connect_to_vlib(const char *svm_name, const char *client_name, int rx_queue_size)
Control ping from client to api server request.
Definition: vpe.api:61
int my_client_index
All VLIB-side message handlers use my_client_index to identify the queue / client.
Definition: api_common.h:309
#define NULL
Definition: clib.h:55
void vl_client_add_api_signatures(vl_api_memclnt_create_t *mp)
Definition: jvpp_registry.c:81
void vl_noop_handler(void *mp)
Definition: api_shared.c:780
static f64 clib_time_now(clib_time_t *c)
Definition: time.h:203
JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_VppJNIConnection_clientDisconnect(JNIEnv *env, jclass clazz)
static int send_initial_control_ping()
static_always_inline u32 vppjni_get_context_id(jvpp_main_t *jm)
Definition: jvpp_common.h:45
void * vl_msg_api_alloc(int nbytes)
unsigned char u8
Definition: types.h:56
jvpp_registry_main_t jvpp_registry_main
Definition: jvpp_registry.c:79
double f64
Definition: types.h:142
#define CONTROL_PING_MESSAGE
Definition: jvpp_registry.c:55
#define static_always_inline
Definition: clib.h:93
vlib_main_t ** vlib_mains
Definition: jvpp_registry.c:44
jint JNI_OnLoad(JavaVM *vm, void *reserved)
struct vl_shmem_hdr_ * shmem_hdr
Binary API shared-memory segment header pointer.
Definition: api_common.h:264
unsigned int u32
Definition: types.h:88
static int find_ping_id()
void vl_msg_api_clean_handlers(int msg_id)
Definition: api_shared.c:735
void vl_msg_api_set_handlers(int msg_id, char *msg_name, void *handler, void *cleanup, void *endian, void *print, int msg_size, int traced)
Definition: api_shared.c:713
void clib_time_init(clib_time_t *c)
Definition: time.c:178
vlib_main_t vlib_global_main
Definition: jvpp_registry.c:43
JavaVM * jvm
Definition: jvpp_common.h:35
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:201
void vl_msg_api_send_shmem(svm_queue_t *q, u8 *elem)
jvpp_main_t jvpp_main
Definition: jvpp_common.c:41
vlib_main_t * vm
Definition: buffer.c:294
JNIEXPORT jint JNICALL Java_io_fd_vpp_jvpp_JVppRegistryImpl_controlPing0(JNIEnv *env, jobject regstryObject)
#define clib_warning(format, args...)
Definition: error.h:59
JNIEnv * jenv
Definition: jvpp_common.h:32
static_always_inline void vppjni_lock(jvpp_main_t *jm, u32 tag)
Definition: jvpp_common.h:49
svm_queue_t * vl_input_queue
Definition: memory_shared.h:84
JNIEXPORT jobject JNICALL Java_io_fd_vpp_jvpp_VppJNIConnection_clientConnect(JNIEnv *env, jclass obj, jstring shmPrefix, jstring clientName)
signed int i32
Definition: types.h:81
void JNI_OnUnload(JavaVM *vm, void *reserved)
static_always_inline void vppjni_unlock(jvpp_main_t *jm)
Definition: jvpp_common.h:55
Control ping from the client to the server response.
Definition: vpe.api:73
volatile i32 control_ping_retval
Definition: jvpp_registry.c:61
static uword pointer_to_uword(const void *p)
Definition: types.h:131
#define CONTROL_PING_REPLY_MESSAGE
Definition: jvpp_registry.c:56
#define hash_foreach_pair(p, v, body)
Iterate over hash pairs.
Definition: hash.h:373
static_always_inline void cleanup_rx_thread(void *arg)
Definition: jvpp_registry.c:90
u32 my_client_index
Definition: jvpp_common.h:39
void vl_client_disconnect_from_vlib(void)
volatile u8 is_connected
Definition: jvpp_registry.c:75
uword * messages_hash
Definition: jvpp_common.h:40
void call_on_error(const char *callName, int contextId, int retval, jclass callbackClass, jobject callbackObject, jclass callbackExceptionClass)
Calls onError callback on callbackObject reference.
Definition: jvpp_common.c:43
pthread_key_t cleanup_rx_thread_key
Definition: jvpp_registry.c:72
uword * msg_index_by_name_and_crc
client message index hash table
Definition: api_common.h:324
api_main_t api_main
Definition: api_shared.c:35
uword key
Definition: hash.h:162
svm_queue_t * vl_input_queue
Definition: jvpp_common.h:38