blob: bbe9719c30ffcea69848e74af836b818635d815f [file] [log] [blame]
Marek Gradzki66ea26b2016-07-26 15:28:22 +02001/*
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);
Damjan Marion7cd468a2016-12-19 23:05:39 +010019#include <vpp/api/vpe.api.h>
Marek Gradzki66ea26b2016-07-26 15:28:22 +020020#undef vl_api_version
21
Marek Gradzki1b5ea882016-12-21 13:38:39 +010022
Marek Gradzki66ea26b2016-07-26 15:28:22 +020023#include <jni.h>
24#include <jvpp-common/jvpp_common.h>
Marek Gradzkie85581c2016-09-28 10:12:04 +020025#include "io_fd_vpp_jvpp_VppJNIConnection.h"
26#include "io_fd_vpp_jvpp_JVppRegistryImpl.h"
Marek Gradzki66ea26b2016-07-26 15:28:22 +020027
Damjan Marion7cd468a2016-12-19 23:05:39 +010028#include <vpp/api/vpe_msg_enum.h>
Marek Gradzki66ea26b2016-07-26 15:28:22 +020029#define vl_typedefs /* define message structures */
Damjan Marion7cd468a2016-12-19 23:05:39 +010030#include <vpp/api/vpe_all_api_h.h>
Marek Gradzki66ea26b2016-07-26 15:28:22 +020031#undef vl_typedefs
32
33#define vl_endianfun
Damjan Marion7cd468a2016-12-19 23:05:39 +010034#include <vpp/api/vpe_all_api_h.h>
Marek Gradzki66ea26b2016-07-26 15:28:22 +020035#undef vl_endianfun
36
37/* instantiate all the print functions we know about */
38#define vl_print(handle, ...)
39#define vl_printfun
Damjan Marion7cd468a2016-12-19 23:05:39 +010040#include <vpp/api/vpe_all_api_h.h>
Marek Gradzki66ea26b2016-07-26 15:28:22 +020041#undef vl_printfun
42
Marek Gradzki1b5ea882016-12-21 13:38:39 +010043vlib_main_t vlib_global_main;
44vlib_main_t **vlib_mains;
45
Marek Gradzki66ea26b2016-07-26 15:28:22 +020046/*
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 */
51void __stack_chk_guard(void) __attribute__((weak));
52void __stack_chk_guard(void) {
53}
54
Marek Gradzki4746a5d2017-01-27 08:57:40 +010055#define CONTROL_PING_MESSAGE "control_ping"
56#define CONTROL_PING_REPLY_MESSAGE "control_ping_reply"
57
Marek Gradzki66ea26b2016-07-26 15:28:22 +020058typedef struct {
59 /* UThread attachment */
60 volatile u32 control_ping_result_ready;
61 volatile i32 control_ping_retval;
62
Marek Gradzki4746a5d2017-01-27 08:57:40 +010063 /* Control ping callback */
Marek Gradzki66ea26b2016-07-26 15:28:22 +020064 jobject registryObject;
65 jclass registryClass;
66 jclass controlPingReplyClass;
67 jclass callbackExceptionClass;
Marek Gradzki4746a5d2017-01-27 08:57:40 +010068 int control_ping_msg_id;
69 int control_ping_reply_msg_id;
Marek Gradzki66ea26b2016-07-26 15:28:22 +020070
71 /* Thread cleanup */
72 pthread_key_t cleanup_rx_thread_key;
73
74 /* Connected indication */
75 volatile u8 is_connected;
Matej Perina3e233672017-09-14 16:14:14 +020076 u32 vpe_pid;
Marek Gradzki66ea26b2016-07-26 15:28:22 +020077} jvpp_registry_main_t;
78
79jvpp_registry_main_t jvpp_registry_main __attribute__((aligned (64)));
80
81void vl_client_add_api_signatures(vl_api_memclnt_create_t *mp) {
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 */
90static_always_inline void cleanup_rx_thread(void *arg) {
91 jvpp_main_t * jm = &jvpp_main;
92 jvpp_registry_main_t * rm = &jvpp_registry_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
108static 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;
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,
142 rm->callbackExceptionClass);
143 } else {
144 jmethodID constructor = (*env)->GetMethodID(env,
145 rm->controlPingReplyClass, "<init>", "()V");
146 jmethodID callbackMethod = (*env)->GetMethodID(env,
147 rm->registryClass, "onControlPingReply",
Marek Gradzkie85581c2016-09-28 10:12:04 +0200148 "(Lio/fd/vpp/jvpp/dto/ControlPingReply;)V");
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200149
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);
Maros Marsalekc0f6cf32016-11-16 11:29:33 +0100170 (*env)->DeleteLocalRef(env, dto);
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200171 }
172 }
173
Matej Perina3e233672017-09-14 16:14:14 +0200174 out: rm->vpe_pid = clib_net_to_host_u32(mp->vpe_pid);
175 rm->control_ping_result_ready = 1;
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200176}
177
Marek Gradzki4746a5d2017-01-27 08:57:40 +0100178static int find_ping_id() {
179 int rv = 0;
180 jvpp_main_t * jm = &jvpp_main;
181 jvpp_registry_main_t * rm = &jvpp_registry_main;
182 api_main_t *am = &api_main;
183 hash_pair_t *hp;
184 jm->messages_hash = am->msg_index_by_name_and_crc;
185
186 rm->control_ping_msg_id = -1;
187 rm->control_ping_reply_msg_id = -1;
188
189 hash_foreach_pair (hp, jm->messages_hash,
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
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200213static int send_initial_control_ping() {
214 f64 timeout;
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;
219
220 clib_time_init(&clib_time);
221
222 rm->control_ping_result_ready = 0;
223 mp = vl_msg_api_alloc(sizeof(*mp));
224 memset(mp, 0, sizeof(*mp));
Marek Gradzki4746a5d2017-01-27 08:57:40 +0100225 mp->_vl_msg_id = ntohs(rm->control_ping_msg_id);
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200226 mp->client_index = jm->my_client_index;
227
228 // send message:
229 vl_msg_api_send_shmem(jm->vl_input_queue, (u8 *) &mp);
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) {
Matej Perina75a17ec2017-09-21 17:03:27 +0200242 vl_msg_api_clean_handlers(rm->control_ping_reply_msg_id);
Marek Gradzki4746a5d2017-01-27 08:57:40 +0100243 clib_warning("first control ping failed: %d", rv);
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200244 }
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200245 return rv;
246}
247
Dave Barach6a5adc32018-07-04 10:56:23 -0400248#if USE_DLMALLOC == 1
249void * __jvpp_heap;
250#endif
251
Jan Srnicek5beec812017-03-24 10:18:11 +0100252static int connect_to_vpe(char *shm_prefix, char *name) {
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200253 jvpp_main_t * jm = &jvpp_main;
254 api_main_t * am = &api_main;
Marek Gradzki4746a5d2017-01-27 08:57:40 +0100255 jvpp_registry_main_t * rm = &jvpp_registry_main;
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200256
Dave Barach6a5adc32018-07-04 10:56:23 -0400257#if USE_DLMALLOC == 1
258 __jvpp_heap = clib_mem_init (0, 1<<20);
259#endif
260
Jan Srnicek5beec812017-03-24 10:18:11 +0100261 if (vl_client_connect_to_vlib(shm_prefix, name, 32) < 0)
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200262 return -1;
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200263 jm->my_client_index = am->my_client_index;
264
265 jm->vl_input_queue = am->shmem_hdr->vl_input_queue;
266
Marek Gradzki4746a5d2017-01-27 08:57:40 +0100267 if (find_ping_id() < 0)
268 return -1;
269
270 vl_msg_api_set_handlers(rm->control_ping_reply_msg_id, CONTROL_PING_REPLY_MESSAGE,
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200271 vl_api_control_ping_reply_t_handler, vl_noop_handler,
272 vl_api_control_ping_reply_t_endian,
273 vl_api_control_ping_reply_t_print,
274 sizeof(vl_api_control_ping_reply_t), 1);
275
Marek Gradzki38bcb562016-12-07 08:37:25 +0100276 return send_initial_control_ping();
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200277}
278
Marek Gradzkie85581c2016-09-28 10:12:04 +0200279JNIEXPORT jobject JNICALL Java_io_fd_vpp_jvpp_VppJNIConnection_clientConnect(
Jan Srnicek5beec812017-03-24 10:18:11 +0100280 JNIEnv *env, jclass obj, jstring shmPrefix, jstring clientName) {
281 /*
282 * TODO introducing memory prefix as variable can be used in hc2vpp
283 * to be able to run without root privileges
284 * https://jira.fd.io/browse/HC2VPP-176
285 */
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200286 int rv;
287 const char *client_name;
Jan Srnicek5beec812017-03-24 10:18:11 +0100288 const char *shm_prefix;
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200289 void vl_msg_reply_handler_hookup(void);
290 jvpp_main_t * jm = &jvpp_main;
291 jvpp_registry_main_t * rm = &jvpp_registry_main;
292
293 jclass connectionInfoClass = (*env)->FindClass(env,
Marek Gradzkie85581c2016-09-28 10:12:04 +0200294 "io/fd/vpp/jvpp/VppJNIConnection$ConnectionInfo");
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200295 jmethodID connectionInfoConstructor = (*env)->GetMethodID(env,
Matej Perina3e233672017-09-14 16:14:14 +0200296 connectionInfoClass, "<init>", "(JIII)V");
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200297
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200298 if (rm->is_connected) {
299 return (*env)->NewObject(env, connectionInfoClass,
300 connectionInfoConstructor, 0, 0,
Matej Perina3e233672017-09-14 16:14:14 +0200301 VNET_API_ERROR_ALREADY_CONNECTED, 0);
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200302 }
303
304 client_name = (*env)->GetStringUTFChars(env, clientName, 0);
Jan Srnicek5beec812017-03-24 10:18:11 +0100305 shm_prefix = (*env)->GetStringUTFChars(env, shmPrefix, 0);
306
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200307 if (!client_name) {
308 return (*env)->NewObject(env, connectionInfoClass,
Matej Perina3e233672017-09-14 16:14:14 +0200309 connectionInfoConstructor, 0, 0, VNET_API_ERROR_INVALID_VALUE, 0, shmPrefix);
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200310 }
311
Jan Srnicek5beec812017-03-24 10:18:11 +0100312 if (!shm_prefix) {
313 return (*env)->NewObject(env, connectionInfoClass,
Matej Perina3e233672017-09-14 16:14:14 +0200314 connectionInfoConstructor, 0, 0, VNET_API_ERROR_INVALID_VALUE, 0, shmPrefix);
Jan Srnicek5beec812017-03-24 10:18:11 +0100315 }
316
317 rv = connect_to_vpe((char *) shm_prefix, (char *) client_name);
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200318
319 if (rv < 0)
320 clib_warning("connection failed, rv %d", rv);
321
322 (*env)->ReleaseStringUTFChars(env, clientName, client_name);
Jan Srnicek5beec812017-03-24 10:18:11 +0100323 (*env)->ReleaseStringUTFChars(env, shmPrefix, shm_prefix);
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200324
325 return (*env)->NewObject(env, connectionInfoClass,
Damjan Marion31d43482017-04-26 14:25:56 +0200326 connectionInfoConstructor, (jlong) pointer_to_uword (jm->vl_input_queue),
Matej Perina3e233672017-09-14 16:14:14 +0200327 (jint) jm->my_client_index, (jint) rv, (jint) rm->vpe_pid);
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200328}
329
Marek Gradzkie85581c2016-09-28 10:12:04 +0200330JNIEXPORT jint JNICALL Java_io_fd_vpp_jvpp_JVppRegistryImpl_controlPing0(
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200331 JNIEnv *env, jobject regstryObject) {
332 jvpp_main_t * jm = &jvpp_main;
333 vl_api_control_ping_t * mp;
334 u32 my_context_id = vppjni_get_context_id(&jvpp_main);
335 jvpp_registry_main_t * rm = &jvpp_registry_main;
336
337 if (rm->registryObject == 0) {
338 rm->registryObject = (*env)->NewGlobalRef(env, regstryObject);
339 }
340 if (rm->registryClass == 0) {
341 rm->registryClass = (jclass) (*env)->NewGlobalRef(env,
342 (*env)->GetObjectClass(env, regstryObject));
343 }
344
345 mp = vl_msg_api_alloc(sizeof(*mp));
346 memset(mp, 0, sizeof(*mp));
Marek Gradzki4746a5d2017-01-27 08:57:40 +0100347 mp->_vl_msg_id = ntohs(rm->control_ping_msg_id);
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200348 mp->client_index = jm->my_client_index;
349 mp->context = clib_host_to_net_u32(my_context_id);
350
351 // send message:
352 vl_msg_api_send_shmem(jm->vl_input_queue, (u8 *) &mp);
353 return my_context_id;
354}
355
Marek Gradzkie85581c2016-09-28 10:12:04 +0200356JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_VppJNIConnection_clientDisconnect(
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200357 JNIEnv *env, jclass clazz) {
358 jvpp_registry_main_t * rm = &jvpp_registry_main;
359 rm->is_connected = 0; // TODO make thread safe
360 vl_client_disconnect_from_vlib();
361
362 // cleanup:
363 if (rm->registryObject) {
364 (*env)->DeleteGlobalRef(env, rm->registryObject);
365 rm->registryObject = 0;
366 }
367 if (rm->registryClass) {
368 (*env)->DeleteGlobalRef(env, rm->registryClass);
369 rm->registryClass = 0;
370 }
371}
372
373jint JNI_OnLoad(JavaVM *vm, void *reserved) {
374 jvpp_main_t * jm = &jvpp_main;
375 jvpp_registry_main_t * rm = &jvpp_registry_main;
376 JNIEnv* env;
377
378 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
379 return JNI_EVERSION;
380 }
381
382 rm->controlPingReplyClass = (jclass) (*env)->NewGlobalRef(env,
Marek Gradzkie85581c2016-09-28 10:12:04 +0200383 (*env)->FindClass(env, "io/fd/vpp/jvpp/dto/ControlPingReply"));
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200384 if ((*env)->ExceptionCheck(env)) {
385 (*env)->ExceptionDescribe(env);
386 clib_warning("Failed to cache class references\n");
387 return JNI_ERR;
388 }
389
390 rm->callbackExceptionClass = (jclass) (*env)->NewGlobalRef(env,
Marek Gradzkie85581c2016-09-28 10:12:04 +0200391 (*env)->FindClass(env, "io/fd/vpp/jvpp/VppCallbackException"));
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200392 if ((*env)->ExceptionCheck(env)) {
393 (*env)->ExceptionDescribe(env);
394 return JNI_ERR;
395 }
396
397 jm->jvm = vm;
398 return JNI_VERSION_1_8;
399}
400
401void JNI_OnUnload(JavaVM *vm, void *reserved) {
402 jvpp_main_t * jm = &jvpp_main;
403 JNIEnv* env;
404 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
405 return;
406 }
407
408 jm->jenv = NULL;
409 jm->jvm = NULL;
410}