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 <api/vpe.api.h>
23 #include <jvpp/jvpp.h>
24 #include <jvpp/org_openvpp_jvpp_VppJNIConnection.h>
25 #include <jvpp/org_openvpp_jvpp_JVppImpl.h>
27 #include <api/vpe_msg_enum.h>
28 #define vl_typedefs /* define message structures */
29 #include <api/vpe_all_api_h.h>
33 #include <api/vpe_all_api_h.h>
36 /* instantiate all the print functions we know about */
37 #define vl_print(handle, ...)
39 #include <api/vpe_all_api_h.h>
43 #define VPPJNI_DEBUG 0
47 #define DEBUG_LOG(...) clib_warning(__VA_ARGS__)
49 #define DEBUG_LOG(...)
52 #include "gen/target/jvpp_gen.h"
54 static int connect_to_vpe(char *name);
57 * The Java runtime isn't compile w/ -fstack-protector,
58 * so we have to supply missing external references for the
59 * regular vpp libraries. Weak reference in case folks get religion
62 void __stack_chk_guard (void) __attribute__((weak));
63 void __stack_chk_guard (void) { }
65 void vl_client_add_api_signatures (vl_api_memclnt_create_t *mp)
68 * Send the main API signature in slot 0. This bit of code must
69 * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
71 mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
74 /* cleanup handler for RX thread */
75 static void cleanup_rx_thread(void *arg)
77 vppjni_main_t * jm = &vppjni_main;
81 int getEnvStat = (*jm->jvm)->GetEnv(jm->jvm, (void **)&(jm->jenv), JNI_VERSION_1_8);
82 if (getEnvStat == JNI_EVERSION) {
83 clib_warning ("Unsupported JNI version\n");
84 jm->retval = VNET_API_ERROR_UNSUPPORTED_JNI_VERSION;
86 } else if (getEnvStat != JNI_EDETACHED) {
87 (*jm->jvm)->DetachCurrentThread(jm->jvm);
93 JNIEXPORT jint JNICALL Java_org_openvpp_jvpp_VppJNIConnection_clientConnect
94 (JNIEnv *env, jclass obj, jstring clientName, jobject callback)
97 const char *client_name;
98 void vl_msg_reply_handler_hookup(void);
99 vppjni_main_t * jm = &vppjni_main;
102 * Bail out now if we're not running as root
105 return VNET_API_ERROR_NOT_RUNNING_AS_ROOT;
107 if (jm->is_connected)
108 return VNET_API_ERROR_ALREADY_CONNECTED;
110 client_name = (*env)->GetStringUTFChars(env, clientName, 0);
112 return VNET_API_ERROR_INVALID_VALUE;
114 rv = connect_to_vpe ((char *) client_name);
117 clib_warning ("connection failed, rv %d", rv);
119 (*env)->ReleaseStringUTFChars (env, clientName, client_name);
123 clib_time_t clib_time;
124 clib_time_init (&clib_time);
126 /* vl_msg_reply_handler_hookup (); */
127 jm->is_connected = 1;
129 jm->callback = (*env)->NewGlobalRef(env, callback);
130 jm->callbackClass = (jclass)(*env)->NewGlobalRef(env, (*env)->GetObjectClass(env, callback));
133 // call control ping first to attach rx thread to java thread
134 vl_api_control_ping_t * mp;
135 M(CONTROL_PING, control_ping);
138 // wait for results: Current time + 10 seconds is the timeout
139 timeout = clib_time_now (&clib_time) + 10.0;
140 rv = VNET_API_ERROR_RESPONSE_NOT_READY;
141 while (clib_time_now (&clib_time) < timeout) {
142 if (jm->result_ready == 1) {
149 clib_warning ("first control ping failed: %d", rv);
153 DEBUG_LOG ("clientConnect result: %d", rv);
157 JNIEXPORT void JNICALL Java_org_openvpp_jvpp_VppJNIConnection_clientDisconnect
158 (JNIEnv *env, jclass clazz)
160 vppjni_main_t * jm = &vppjni_main;
161 jm->is_connected = 0; // TODO make thread safe
162 vl_client_disconnect_from_vlib();
166 * Send error reply to the requestor
167 * const char* call pointer to the request name
168 * int context call context identifier
169 * int retval result of the operation
171 void CallOnError(const char* call, int context, int retval)
173 DEBUG_LOG("\nCallOnError : callback=%s,retval=%d,context=%d\n",call,clib_net_to_host_u32(retval), clib_net_to_host_u32(context));
174 vppjni_main_t * jm = &vppjni_main;
175 JNIEnv *env = jm->jenv;
176 if (!env) printf( "CallOnError : env is null!\n");
177 if (!jm->callbackClass) {
178 DEBUG_LOG( "CallOnError : jm->callbackClass is null!\n");
182 jmethodID excConstructor = (*env)->GetMethodID(env, callbackExceptionClass, "<init>", "(Ljava/lang/String;II)V");
183 if (!excConstructor) {
184 DEBUG_LOG( "CallOnError : excConstructor is null!\n");
187 jmethodID callbackExcMethod = (*env)->GetMethodID(env, jm->callbackClass, "onError", "(Lorg/openvpp/jvpp/VppCallbackException;)V");
188 if (!callbackExcMethod) {
189 DEBUG_LOG( "CallOnError : callbackExcMethod is null!\n");
193 jobject excObject = (*env)->NewObject(env, callbackExceptionClass, excConstructor,(*env)->NewStringUTF(env, call), clib_net_to_host_u32(context), clib_net_to_host_u32(retval));
195 DEBUG_LOG( "CallOnError : excObject is null!\n");
199 (*env)->CallVoidMethod(env, jm->callback, callbackExcMethod, excObject);
200 DEBUG_LOG( "CallOnError : Response sent\n");
203 // control ping needs to be very first thing called
204 // to attach rx thread to java thread
205 static void vl_api_control_ping_reply_t_handler
206 (vl_api_control_ping_reply_t * mp)
208 vppjni_main_t * jm = &vppjni_main;
210 char was_thread_connected = 0;
212 // attach to java thread if not attached
213 int getEnvStat = (*jm->jvm)->GetEnv(jm->jvm, (void **)&(jm->jenv), JNI_VERSION_1_8);
214 if (getEnvStat == JNI_EDETACHED) {
215 if ((*jm->jvm)->AttachCurrentThread(jm->jvm, (void **)&(jm->jenv), NULL) != 0) {
216 clib_warning("Failed to attach thread\n");
217 jm->retval = VNET_API_ERROR_FAILED_TO_ATTACH_TO_JAVA_THREAD;
221 // workaround as we can't use pthread_cleanup_push
222 pthread_key_create(&jm->cleanup_rx_thread_key, cleanup_rx_thread);
223 // destructor is only called if the value of key is non null
224 pthread_setspecific(jm->cleanup_rx_thread_key, (void *)1);
225 was_thread_connected = 1;
226 } else if (getEnvStat == JNI_EVERSION) {
227 clib_warning ("Unsupported JNI version\n");
228 jm->retval = VNET_API_ERROR_UNSUPPORTED_JNI_VERSION;
232 if (was_thread_connected == 0) {
233 JNIEnv *env = jm->jenv;
236 CallOnError("controlPing", mp->context, mp->retval);
238 jmethodID constructor = (*env)->GetMethodID(env, controlPingReplyClass, "<init>", "()V");
239 jmethodID callbackMethod = (*env)->GetMethodID(env, jm->callbackClass, "onControlPingReply", "(Lorg/openvpp/jvpp/dto/ControlPingReply;)V");
241 jobject dto = (*env)->NewObject(env, controlPingReplyClass, constructor);
243 jfieldID contextFieldId = (*env)->GetFieldID(env, controlPingReplyClass, "context", "I");
244 (*env)->SetIntField(env, dto, contextFieldId, clib_net_to_host_u32(mp->context));
246 jfieldID clientIndexFieldId = (*env)->GetFieldID(env, controlPingReplyClass, "clientIndex", "I");
247 (*env)->SetIntField(env, dto, clientIndexFieldId, clib_net_to_host_u32(mp->client_index));
249 jfieldID vpePidFieldId = (*env)->GetFieldID(env, controlPingReplyClass, "vpePid", "I");
250 (*env)->SetIntField(env, dto, vpePidFieldId, clib_net_to_host_u32(mp->vpe_pid));
252 (*env)->CallVoidMethod(env, jm->callback, callbackMethod, dto);
257 jm->result_ready = 1;
260 jint JNI_OnLoad(JavaVM *vm, void *reserved) {
261 vppjni_main_t * jm = &vppjni_main;
263 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
267 if (cache_class_references(env) != 0) {
272 return JNI_VERSION_1_8;
275 void JNI_OnUnload(JavaVM *vm, void *reserved) {
276 vppjni_main_t * jm = &vppjni_main;
278 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
283 (*env)->DeleteGlobalRef(env, jm->callbackClass);
284 (*env)->DeleteGlobalRef(env, jm->callback);
286 jm->callbackClass = NULL;
292 static int connect_to_vpe(char *name)
294 vppjni_main_t * jm = &vppjni_main;
295 api_main_t * am = &api_main;
297 if (vl_client_connect_to_vlib("/vpe-api", name, 32) < 0)
300 jm->my_client_index = am->my_client_index;
301 jm->vl_input_queue = am->shmem_hdr->vl_input_queue;
304 vl_msg_api_set_handlers(VL_API_##N, #n, \
305 vl_api_##n##_t_handler, \
307 vl_api_##n##_t_endian, \
308 vl_api_##n##_t_print, \
309 sizeof(vl_api_##n##_t), 1);