FD.io VPP  v17.04-9-g99c0734
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;
77 
78 jvpp_registry_main_t jvpp_registry_main __attribute__((aligned (64)));
79 
81  /*
82  * Send the main API signature in slot 0. This bit of code must
83  * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
84  */
85  mp->api_versions[0] = clib_host_to_net_u32(vpe_api_version);
86 }
87 
88 /* cleanup handler for RX thread */
90  jvpp_main_t * jm = &jvpp_main;
92 
93  vppjni_lock(jm, 99);
94 
95  int getEnvStat = (*jm->jvm)->GetEnv(jm->jvm, (void **) &(jm->jenv),
96  JNI_VERSION_1_8);
97  if (getEnvStat == JNI_EVERSION) {
98  clib_warning("Unsupported JNI version\n");
99  rm->control_ping_retval = VNET_API_ERROR_UNSUPPORTED_JNI_VERSION;
100  goto out;
101  } else if (getEnvStat != JNI_EDETACHED) {
102  (*jm->jvm)->DetachCurrentThread(jm->jvm);
103  }
104  out: vppjni_unlock(jm);
105 }
106 
109  jvpp_main_t * jm = &jvpp_main;
111  char was_thread_connected = 0;
112 
113  // attach to java thread if not attached
114  int getEnvStat = (*jm->jvm)->GetEnv(jm->jvm, (void **) &(jm->jenv),
115  JNI_VERSION_1_8);
116  if (getEnvStat == JNI_EDETACHED) {
117  if ((*jm->jvm)->AttachCurrentThread(jm->jvm, (void **) &(jm->jenv),
118  NULL) != 0) {
119  clib_warning("Failed to attach thread\n");
120  rm->control_ping_retval =
121  VNET_API_ERROR_FAILED_TO_ATTACH_TO_JAVA_THREAD;
122  goto out;
123  }
124 
125  // workaround as we can't use pthread_cleanup_push
126  pthread_key_create(&rm->cleanup_rx_thread_key, cleanup_rx_thread);
127  // destructor is only called if the value of key is non null
128  pthread_setspecific(rm->cleanup_rx_thread_key, (void *) 1);
129  was_thread_connected = 1;
130  } else if (getEnvStat == JNI_EVERSION) {
131  clib_warning("Unsupported JNI version\n");
132  rm->control_ping_retval = VNET_API_ERROR_UNSUPPORTED_JNI_VERSION;
133  goto out;
134  }
135 
136  if (was_thread_connected == 0) {
137  JNIEnv *env = jm->jenv;
138  if (mp->retval < 0) {
139  call_on_error("controlPing", mp->context, mp->retval,
140  rm->registryClass, rm->registryObject,
142  } else {
143  jmethodID constructor = (*env)->GetMethodID(env,
144  rm->controlPingReplyClass, "<init>", "()V");
145  jmethodID callbackMethod = (*env)->GetMethodID(env,
146  rm->registryClass, "onControlPingReply",
147  "(Lio/fd/vpp/jvpp/dto/ControlPingReply;)V");
148 
149  jobject dto = (*env)->NewObject(env, rm->controlPingReplyClass,
150  constructor);
151 
152  jfieldID contextFieldId = (*env)->GetFieldID(env,
153  rm->controlPingReplyClass, "context", "I");
154  (*env)->SetIntField(env, dto, contextFieldId,
155  clib_net_to_host_u32(mp->context));
156 
157  jfieldID clientIndexFieldId = (*env)->GetFieldID(env,
158  rm->controlPingReplyClass, "clientIndex", "I");
159  (*env)->SetIntField(env, dto, clientIndexFieldId,
160  clib_net_to_host_u32(mp->client_index));
161 
162  jfieldID vpePidFieldId = (*env)->GetFieldID(env,
163  rm->controlPingReplyClass, "vpePid", "I");
164  (*env)->SetIntField(env, dto, vpePidFieldId,
165  clib_net_to_host_u32(mp->vpe_pid));
166 
167  (*env)->CallVoidMethod(env, rm->registryObject, callbackMethod,
168  dto);
169  (*env)->DeleteLocalRef(env, dto);
170  }
171  }
172 
173  out: rm->control_ping_result_ready = 1;
174 }
175 
176 static int find_ping_id() {
177  int rv = 0;
178  jvpp_main_t * jm = &jvpp_main;
180  api_main_t *am = &api_main;
181  hash_pair_t *hp;
183 
184  rm->control_ping_msg_id = -1;
185  rm->control_ping_reply_msg_id = -1;
186 
188  ({
189  char *key = (char *)hp->key; // key format: name_crc
190  int msg_name_len = strlen(key) - 9; // ignore crc
191  if (strlen(CONTROL_PING_MESSAGE) == msg_name_len &&
192  strncmp(CONTROL_PING_MESSAGE, (char *)hp->key, msg_name_len) == 0) {
193  rm->control_ping_msg_id = (u32)hp->value[0];
194  }
195  if (strlen(CONTROL_PING_REPLY_MESSAGE) == msg_name_len &&
196  strncmp(CONTROL_PING_REPLY_MESSAGE, (char *)hp->key, msg_name_len) == 0) {
197  rm->control_ping_reply_msg_id = (u32)hp->value[0];
198  }
199  }));
200  if (rm->control_ping_msg_id == -1) {
201  clib_warning("failed to find id for %s", CONTROL_PING_MESSAGE);
202  rv = -1;
203  }
204  if (rm->control_ping_reply_msg_id == -1) {
205  clib_warning("failed to find id for %s", CONTROL_PING_REPLY_MESSAGE);
206  rv = -1;
207  }
208  return rv;
209 }
210 
212  f64 timeout;
213  clib_time_t clib_time;
215  jvpp_main_t * jm = &jvpp_main;
217 
218  clib_time_init(&clib_time);
219 
221  mp = vl_msg_api_alloc(sizeof(*mp));
222  memset(mp, 0, sizeof(*mp));
223  mp->_vl_msg_id = ntohs(rm->control_ping_msg_id);
224  mp->client_index = jm->my_client_index;
225 
226  // send message:
228 
229  // wait for results: Current time + 10 seconds is the timeout
230  timeout = clib_time_now(&clib_time) + 10.0;
231  int rv = VNET_API_ERROR_RESPONSE_NOT_READY;
232  while (clib_time_now(&clib_time) < timeout) {
233  if (rm->control_ping_result_ready == 1) {
234  rv = rm->control_ping_retval;
235  break;
236  }
237  }
238 
239  if (rv != 0) {
240  clib_warning("first control ping failed: %d", rv);
241  }
242 
243  return rv;
244 }
245 
246 static int connect_to_vpe(char *name) {
247  jvpp_main_t * jm = &jvpp_main;
248  api_main_t * am = &api_main;
250 
251  if (vl_client_connect_to_vlib("/vpe-api", name, 32) < 0)
252  return -1;
253 
255 
257 
258  if (find_ping_id() < 0)
259  return -1;
260 
263  vl_api_control_ping_reply_t_endian,
264  vl_api_control_ping_reply_t_print,
265  sizeof(vl_api_control_ping_reply_t), 1);
266 
267  return send_initial_control_ping();
268 }
269 
271  JNIEnv *env, jclass obj, jstring clientName) {
272  int rv;
273  const char *client_name;
274  void vl_msg_reply_handler_hookup(void);
275  jvpp_main_t * jm = &jvpp_main;
277 
278  jclass connectionInfoClass = (*env)->FindClass(env,
279  "io/fd/vpp/jvpp/VppJNIConnection$ConnectionInfo");
280  jmethodID connectionInfoConstructor = (*env)->GetMethodID(env,
281  connectionInfoClass, "<init>", "(JII)V");
282 
283  /*
284  * Bail out now if we're not running as root
285  */
286  if (geteuid() != 0) {
287  return (*env)->NewObject(env, connectionInfoClass,
288  connectionInfoConstructor, 0, 0,
289  VNET_API_ERROR_NOT_RUNNING_AS_ROOT);
290  }
291 
292  if (rm->is_connected) {
293  return (*env)->NewObject(env, connectionInfoClass,
294  connectionInfoConstructor, 0, 0,
295  VNET_API_ERROR_ALREADY_CONNECTED);
296  }
297 
298  client_name = (*env)->GetStringUTFChars(env, clientName, 0);
299  if (!client_name) {
300  return (*env)->NewObject(env, connectionInfoClass,
301  connectionInfoConstructor, 0, 0, VNET_API_ERROR_INVALID_VALUE);
302  }
303 
304  rv = connect_to_vpe((char *) client_name);
305 
306  if (rv < 0)
307  clib_warning("connection failed, rv %d", rv);
308 
309  (*env)->ReleaseStringUTFChars(env, clientName, client_name);
310 
311  return (*env)->NewObject(env, connectionInfoClass,
312  connectionInfoConstructor, (jlong) jm->vl_input_queue,
313  (jint) jm->my_client_index, (jint) rv);
314 }
315 
317  JNIEnv *env, jobject regstryObject) {
318  jvpp_main_t * jm = &jvpp_main;
320  u32 my_context_id = vppjni_get_context_id(&jvpp_main);
322 
323  if (rm->registryObject == 0) {
324  rm->registryObject = (*env)->NewGlobalRef(env, regstryObject);
325  }
326  if (rm->registryClass == 0) {
327  rm->registryClass = (jclass) (*env)->NewGlobalRef(env,
328  (*env)->GetObjectClass(env, regstryObject));
329  }
330 
331  mp = vl_msg_api_alloc(sizeof(*mp));
332  memset(mp, 0, sizeof(*mp));
333  mp->_vl_msg_id = ntohs(rm->control_ping_msg_id);
334  mp->client_index = jm->my_client_index;
335  mp->context = clib_host_to_net_u32(my_context_id);
336 
337  // send message:
339  return my_context_id;
340 }
341 
343  JNIEnv *env, jclass clazz) {
345  rm->is_connected = 0; // TODO make thread safe
347 
348  // cleanup:
349  if (rm->registryObject) {
350  (*env)->DeleteGlobalRef(env, rm->registryObject);
351  rm->registryObject = 0;
352  }
353  if (rm->registryClass) {
354  (*env)->DeleteGlobalRef(env, rm->registryClass);
355  rm->registryClass = 0;
356  }
357 }
358 
359 jint JNI_OnLoad(JavaVM *vm, void *reserved) {
360  jvpp_main_t * jm = &jvpp_main;
362  JNIEnv* env;
363 
364  if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
365  return JNI_EVERSION;
366  }
367 
368  rm->controlPingReplyClass = (jclass) (*env)->NewGlobalRef(env,
369  (*env)->FindClass(env, "io/fd/vpp/jvpp/dto/ControlPingReply"));
370  if ((*env)->ExceptionCheck(env)) {
371  (*env)->ExceptionDescribe(env);
372  clib_warning("Failed to cache class references\n");
373  return JNI_ERR;
374  }
375 
376  rm->callbackExceptionClass = (jclass) (*env)->NewGlobalRef(env,
377  (*env)->FindClass(env, "io/fd/vpp/jvpp/VppCallbackException"));
378  if ((*env)->ExceptionCheck(env)) {
379  (*env)->ExceptionDescribe(env);
380  return JNI_ERR;
381  }
382 
383  jm->jvm = vm;
384  return JNI_VERSION_1_8;
385 }
386 
387 void JNI_OnUnload(JavaVM *vm, void *reserved) {
388  jvpp_main_t * jm = &jvpp_main;
389  JNIEnv* env;
390  if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
391  return;
392  }
393 
394  jm->jenv = NULL;
395  jm->jvm = NULL;
396 }
volatile u32 control_ping_result_ready
Definition: jvpp_registry.c:60
int vl_client_connect_to_vlib(char *svm_name, char *client_name, int rx_queue_size)
static int connect_to_vpe(char *name)
static void vl_api_control_ping_reply_t_handler(vl_api_control_ping_reply_t *mp)
void vl_msg_api_send_shmem(unix_shared_memory_queue_t *q, u8 *elem)
Control ping from client to api server request.
Definition: vpe.api:482
void vl_client_disconnect_from_vlib(void)
unix_shared_memory_queue_t * vl_input_queue
Definition: api.h:73
int my_client_index
Definition: api.h:185
#define NULL
Definition: clib.h:55
void vl_client_add_api_signatures(vl_api_memclnt_create_t *mp)
Definition: jvpp_registry.c:80
static f64 clib_time_now(clib_time_t *c)
Definition: time.h:202
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
jvpp_registry_main_t jvpp_registry_main
Definition: jvpp_registry.c:78
#define CONTROL_PING_MESSAGE
Definition: jvpp_registry.c:55
api_main_t api_main
Definition: api_shared.c:35
#define static_always_inline
Definition: clib.h:85
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:684
int i32
Definition: types.h:81
vlib_main_t ** vlib_mains
Definition: jvpp_registry.c:44
jint JNI_OnLoad(JavaVM *vm, void *reserved)
struct vl_shmem_hdr_ * shmem_hdr
Definition: api.h:144
static int find_ping_id()
void clib_time_init(clib_time_t *c)
Definition: time.c:175
vlib_main_t vlib_global_main
Definition: jvpp_registry.c:43
JavaVM * jvm
Definition: jvpp_common.h:35
jvpp_main_t jvpp_main
Definition: jvpp_common.c:30
void * vl_msg_api_alloc(int nbytes)
vlib_main_t * vm
Definition: buffer.c:276
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
unsigned int u32
Definition: types.h:88
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:494
volatile i32 control_ping_retval
Definition: jvpp_registry.c:61
JNIEXPORT jobject JNICALL Java_io_fd_vpp_jvpp_VppJNIConnection_clientConnect(JNIEnv *env, jclass obj, jstring clientName)
#define CONTROL_PING_REPLY_MESSAGE
Definition: jvpp_registry.c:56
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
#define hash_foreach_pair(p, v, body)
Iterate over hash pairs.
Definition: hash.h:349
static_always_inline void cleanup_rx_thread(void *arg)
Definition: jvpp_registry.c:89
u32 my_client_index
Definition: jvpp_common.h:39
unix_shared_memory_queue_t * vl_input_queue
Definition: jvpp_common.h:38
void vl_noop_handler(void *mp)
Definition: api_shared.c:739
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:32
pthread_key_t cleanup_rx_thread_key
Definition: jvpp_registry.c:72
uword * msg_index_by_name_and_crc
Definition: api.h:196
uword key
Definition: hash.h:161