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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #define _GNU_SOURCE /* for strcasestr(3) */
16 #include <vnet/vnet.h>
18 #define vl_api_version(n,v) static u32 vpe_api_version = (v);
19 #include <vpp/api/vpe.api.h>
24 #include <jvpp-common/jvpp_common.h>
25 #include "io_fd_vpp_jvpp_VppJNIConnection.h"
26 #include "io_fd_vpp_jvpp_JVppRegistryImpl.h"
28 #include <vpp/api/vpe_msg_enum.h>
29 #define vl_typedefs /* define message structures */
30 #include <vpp/api/vpe_all_api_h.h>
34 #include <vpp/api/vpe_all_api_h.h>
37 /* instantiate all the print functions we know about */
38 #define vl_print(handle, ...)
40 #include <vpp/api/vpe_all_api_h.h>
43 vlib_main_t vlib_global_main;
44 vlib_main_t **vlib_mains;
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.
51 void __stack_chk_guard(void) __attribute__((weak));
52 void __stack_chk_guard(void) {
55 #define CONTROL_PING_MESSAGE "control_ping"
56 #define CONTROL_PING_REPLY_MESSAGE "control_ping_reply"
59 /* UThread attachment */
60 volatile u32 control_ping_result_ready;
61 volatile i32 control_ping_retval;
63 /* Control ping callback */
64 jobject registryObject;
66 jclass controlPingReplyClass;
67 jclass callbackExceptionClass;
68 int control_ping_msg_id;
69 int control_ping_reply_msg_id;
72 pthread_key_t cleanup_rx_thread_key;
74 /* Connected indication */
75 volatile u8 is_connected;
77 } jvpp_registry_main_t;
79 jvpp_registry_main_t jvpp_registry_main __attribute__((aligned (64)));
81 void vl_client_add_api_signatures(vl_api_memclnt_create_t *mp) {
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().
86 mp->api_versions[0] = clib_host_to_net_u32(vpe_api_version);
89 /* cleanup handler for RX thread */
90 static_always_inline void cleanup_rx_thread(void *arg) {
91 jvpp_main_t * jm = &jvpp_main;
92 jvpp_registry_main_t * rm = &jvpp_registry_main;
96 int getEnvStat = (*jm->jvm)->GetEnv(jm->jvm, (void **) &(jm->jenv),
98 if (getEnvStat == JNI_EVERSION) {
99 clib_warning("Unsupported JNI version\n");
100 rm->control_ping_retval = VNET_API_ERROR_UNSUPPORTED_JNI_VERSION;
102 } else if (getEnvStat != JNI_EDETACHED) {
103 (*jm->jvm)->DetachCurrentThread(jm->jvm);
105 out: vppjni_unlock(jm);
108 static void vl_api_control_ping_reply_t_handler(
109 vl_api_control_ping_reply_t * mp) {
110 jvpp_main_t * jm = &jvpp_main;
111 jvpp_registry_main_t * rm = &jvpp_registry_main;
112 char was_thread_connected = 0;
114 // attach to java thread if not attached
115 int getEnvStat = (*jm->jvm)->GetEnv(jm->jvm, (void **) &(jm->jenv),
117 if (getEnvStat == JNI_EDETACHED) {
118 if ((*jm->jvm)->AttachCurrentThread(jm->jvm, (void **) &(jm->jenv),
120 clib_warning("Failed to attach thread\n");
121 rm->control_ping_retval =
122 VNET_API_ERROR_FAILED_TO_ATTACH_TO_JAVA_THREAD;
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;
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,
142 rm->callbackExceptionClass);
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");
150 jobject dto = (*env)->NewObject(env, rm->controlPingReplyClass,
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));
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));
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));
168 (*env)->CallVoidMethod(env, rm->registryObject, callbackMethod,
170 (*env)->DeleteLocalRef(env, dto);
174 out: rm->vpe_pid = clib_net_to_host_u32(mp->vpe_pid);
175 rm->control_ping_result_ready = 1;
178 static int find_ping_id() {
180 jvpp_main_t * jm = &jvpp_main;
181 jvpp_registry_main_t * rm = &jvpp_registry_main;
182 api_main_t *am = &api_main;
184 jm->messages_hash = am->msg_index_by_name_and_crc;
186 rm->control_ping_msg_id = -1;
187 rm->control_ping_reply_msg_id = -1;
189 hash_foreach_pair (hp, jm->messages_hash,
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];
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];
202 if (rm->control_ping_msg_id == -1) {
203 clib_warning("failed to find id for %s", CONTROL_PING_MESSAGE);
206 if (rm->control_ping_reply_msg_id == -1) {
207 clib_warning("failed to find id for %s", CONTROL_PING_REPLY_MESSAGE);
213 static int send_initial_control_ping() {
215 clib_time_t clib_time;
216 vl_api_control_ping_t * mp;
217 jvpp_main_t * jm = &jvpp_main;
218 jvpp_registry_main_t * rm = &jvpp_registry_main;
220 clib_time_init(&clib_time);
222 rm->control_ping_result_ready = 0;
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;
229 vl_msg_api_send_shmem(jm->vl_input_queue, (u8 *) &mp);
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;
242 clib_warning("first control ping failed: %d", rv);
247 static int connect_to_vpe(char *shm_prefix, char *name) {
248 jvpp_main_t * jm = &jvpp_main;
249 api_main_t * am = &api_main;
250 jvpp_registry_main_t * rm = &jvpp_registry_main;
252 if (vl_client_connect_to_vlib(shm_prefix, name, 32) < 0)
254 jm->my_client_index = am->my_client_index;
256 jm->vl_input_queue = am->shmem_hdr->vl_input_queue;
258 if (find_ping_id() < 0)
261 vl_msg_api_set_handlers(rm->control_ping_reply_msg_id, CONTROL_PING_REPLY_MESSAGE,
262 vl_api_control_ping_reply_t_handler, vl_noop_handler,
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);
267 return send_initial_control_ping();
270 JNIEXPORT jobject JNICALL Java_io_fd_vpp_jvpp_VppJNIConnection_clientConnect(
271 JNIEnv *env, jclass obj, jstring shmPrefix, jstring clientName) {
273 * TODO introducing memory prefix as variable can be used in hc2vpp
274 * to be able to run without root privileges
275 * https://jira.fd.io/browse/HC2VPP-176
278 const char *client_name;
279 const char *shm_prefix;
280 void vl_msg_reply_handler_hookup(void);
281 jvpp_main_t * jm = &jvpp_main;
282 jvpp_registry_main_t * rm = &jvpp_registry_main;
284 jclass connectionInfoClass = (*env)->FindClass(env,
285 "io/fd/vpp/jvpp/VppJNIConnection$ConnectionInfo");
286 jmethodID connectionInfoConstructor = (*env)->GetMethodID(env,
287 connectionInfoClass, "<init>", "(JIII)V");
289 if (rm->is_connected) {
290 return (*env)->NewObject(env, connectionInfoClass,
291 connectionInfoConstructor, 0, 0,
292 VNET_API_ERROR_ALREADY_CONNECTED, 0);
295 client_name = (*env)->GetStringUTFChars(env, clientName, 0);
296 shm_prefix = (*env)->GetStringUTFChars(env, shmPrefix, 0);
299 return (*env)->NewObject(env, connectionInfoClass,
300 connectionInfoConstructor, 0, 0, VNET_API_ERROR_INVALID_VALUE, 0, shmPrefix);
304 return (*env)->NewObject(env, connectionInfoClass,
305 connectionInfoConstructor, 0, 0, VNET_API_ERROR_INVALID_VALUE, 0, shmPrefix);
308 rv = connect_to_vpe((char *) shm_prefix, (char *) client_name);
311 clib_warning("connection failed, rv %d", rv);
313 (*env)->ReleaseStringUTFChars(env, clientName, client_name);
314 (*env)->ReleaseStringUTFChars(env, shmPrefix, shm_prefix);
316 return (*env)->NewObject(env, connectionInfoClass,
317 connectionInfoConstructor, (jlong) pointer_to_uword (jm->vl_input_queue),
318 (jint) jm->my_client_index, (jint) rv, (jint) rm->vpe_pid);
321 JNIEXPORT jint JNICALL Java_io_fd_vpp_jvpp_JVppRegistryImpl_controlPing0(
322 JNIEnv *env, jobject regstryObject) {
323 jvpp_main_t * jm = &jvpp_main;
324 vl_api_control_ping_t * mp;
325 u32 my_context_id = vppjni_get_context_id(&jvpp_main);
326 jvpp_registry_main_t * rm = &jvpp_registry_main;
328 if (rm->registryObject == 0) {
329 rm->registryObject = (*env)->NewGlobalRef(env, regstryObject);
331 if (rm->registryClass == 0) {
332 rm->registryClass = (jclass) (*env)->NewGlobalRef(env,
333 (*env)->GetObjectClass(env, regstryObject));
336 mp = vl_msg_api_alloc(sizeof(*mp));
337 memset(mp, 0, sizeof(*mp));
338 mp->_vl_msg_id = ntohs(rm->control_ping_msg_id);
339 mp->client_index = jm->my_client_index;
340 mp->context = clib_host_to_net_u32(my_context_id);
343 vl_msg_api_send_shmem(jm->vl_input_queue, (u8 *) &mp);
344 return my_context_id;
347 JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_VppJNIConnection_clientDisconnect(
348 JNIEnv *env, jclass clazz) {
349 jvpp_registry_main_t * rm = &jvpp_registry_main;
350 rm->is_connected = 0; // TODO make thread safe
351 vl_client_disconnect_from_vlib();
354 if (rm->registryObject) {
355 (*env)->DeleteGlobalRef(env, rm->registryObject);
356 rm->registryObject = 0;
358 if (rm->registryClass) {
359 (*env)->DeleteGlobalRef(env, rm->registryClass);
360 rm->registryClass = 0;
364 jint JNI_OnLoad(JavaVM *vm, void *reserved) {
365 jvpp_main_t * jm = &jvpp_main;
366 jvpp_registry_main_t * rm = &jvpp_registry_main;
369 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
373 rm->controlPingReplyClass = (jclass) (*env)->NewGlobalRef(env,
374 (*env)->FindClass(env, "io/fd/vpp/jvpp/dto/ControlPingReply"));
375 if ((*env)->ExceptionCheck(env)) {
376 (*env)->ExceptionDescribe(env);
377 clib_warning("Failed to cache class references\n");
381 rm->callbackExceptionClass = (jclass) (*env)->NewGlobalRef(env,
382 (*env)->FindClass(env, "io/fd/vpp/jvpp/VppCallbackException"));
383 if ((*env)->ExceptionCheck(env)) {
384 (*env)->ExceptionDescribe(env);
389 return JNI_VERSION_1_8;
392 void JNI_OnUnload(JavaVM *vm, void *reserved) {
393 jvpp_main_t * jm = &jvpp_main;
395 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {