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>
42 #define VPPJNI_DEBUG 0
45 #define DEBUG_LOG(...) clib_warning(__VA_ARGS__)
47 #define DEBUG_LOG(...)
50 #include "gen/target/jvpp_gen.h"
52 static int connect_to_vpe(char *name);
55 * The Java runtime isn't compile w/ -fstack-protector,
56 * so we have to supply missing external references for the
57 * regular vpp libraries. Weak reference in case folks get religion
60 void __stack_chk_guard (void) __attribute__((weak));
61 void __stack_chk_guard (void) { }
63 void vl_client_add_api_signatures (vl_api_memclnt_create_t *mp)
66 * Send the main API signature in slot 0. This bit of code must
67 * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
69 mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
72 /* cleanup handler for RX thread */
73 static void cleanup_rx_thread(void *arg)
75 vppjni_main_t * jm = &vppjni_main;
79 int getEnvStat = (*jm->jvm)->GetEnv(jm->jvm, (void **)&(jm->jenv), JNI_VERSION_1_6);
80 if (getEnvStat == JNI_EVERSION) {
81 clib_warning ("Unsupported JNI version\n");
82 jm->retval = VNET_API_ERROR_UNSUPPORTED_JNI_VERSION;
84 } else if (getEnvStat != JNI_EDETACHED) {
85 (*jm->jvm)->DetachCurrentThread(jm->jvm);
91 JNIEXPORT jint JNICALL Java_org_openvpp_jvpp_VppJNIConnection_clientConnect
92 (JNIEnv *env, jclass obj, jstring clientName, jobject callback)
95 const char *client_name;
96 void vl_msg_reply_handler_hookup(void);
97 vppjni_main_t * jm = &vppjni_main;
100 * Bail out now if we're not running as root
103 return VNET_API_ERROR_NOT_RUNNING_AS_ROOT;
105 if (jm->is_connected)
106 return VNET_API_ERROR_ALREADY_CONNECTED;
108 client_name = (*env)->GetStringUTFChars(env, clientName, 0);
110 return VNET_API_ERROR_INVALID_VALUE;
112 rv = connect_to_vpe ((char *) client_name);
115 clib_warning ("connection failed, rv %d", rv);
117 (*env)->ReleaseStringUTFChars (env, clientName, client_name);
121 clib_time_t clib_time;
122 clib_time_init (&clib_time);
124 /* vl_msg_reply_handler_hookup (); */
125 jm->is_connected = 1;
127 jm->callback = (*env)->NewGlobalRef(env, callback);
128 jm->callbackClass = (jclass)(*env)->NewGlobalRef(env, (*env)->GetObjectClass(env, callback));
131 // call control ping first to attach rx thread to java thread
132 vl_api_control_ping_t * mp;
133 M(CONTROL_PING, control_ping);
137 timeout = clib_time_now (&clib_time) + 1.0;
138 rv = VNET_API_ERROR_RESPONSE_NOT_READY;
139 while (clib_time_now (&clib_time) < timeout) {
140 if (jm->result_ready == 1) {
147 clib_warning ("first control ping failed: %d", rv);
151 DEBUG_LOG ("clientConnect result: %d", rv);
155 JNIEXPORT void JNICALL Java_org_openvpp_jvpp_VppJNIConnection_clientDisconnect
156 (JNIEnv *env, jclass clazz)
158 vppjni_main_t * jm = &vppjni_main;
159 jm->is_connected = 0; // TODO make thread safe
160 vl_client_disconnect_from_vlib();
163 // control ping needs to be very first thing called
164 // to attach rx thread to java thread
165 static void vl_api_control_ping_reply_t_handler
166 (vl_api_control_ping_reply_t * mp)
168 vppjni_main_t * jm = &vppjni_main;
170 char was_thread_connected = 0;
172 // attach to java thread if not attached
173 int getEnvStat = (*jm->jvm)->GetEnv(jm->jvm, (void **)&(jm->jenv), JNI_VERSION_1_6);
174 if (getEnvStat == JNI_EDETACHED) {
175 if ((*jm->jvm)->AttachCurrentThread(jm->jvm, (void **)&(jm->jenv), NULL) != 0) {
176 clib_warning("Failed to attach thread\n");
177 jm->retval = VNET_API_ERROR_FAILED_TO_ATTACH_TO_JAVA_THREAD;
181 // workaround as we can't use pthread_cleanup_push
182 pthread_key_create(&jm->cleanup_rx_thread_key, cleanup_rx_thread);
183 // destructor is only called if the value of key is non null
184 pthread_setspecific(jm->cleanup_rx_thread_key, (void *)1);
185 was_thread_connected = 1;
186 } else if (getEnvStat == JNI_EVERSION) {
187 clib_warning ("Unsupported JNI version\n");
188 jm->retval = VNET_API_ERROR_UNSUPPORTED_JNI_VERSION;
192 if (was_thread_connected == 0) {
193 JNIEnv *env = jm->jenv;
195 jclass dtoClass = (*env)->FindClass(env, "org/openvpp/jvpp/dto/ControlPingReply");
197 jmethodID constructor = (*env)->GetMethodID(env, dtoClass, "<init>", "()V");
198 jmethodID callbackMethod = (*env)->GetMethodID(env, jm->callbackClass, "onControlPingReply", "(Lorg/openvpp/jvpp/dto/ControlPingReply;)V");
200 jobject dto = (*env)->NewObject(env, dtoClass, constructor);
203 printf("vl_api_control_ping_reply_t_handler ctx=%d\n", clib_net_to_host_u32(mp->context));
205 jfieldID contextFieldId = (*env)->GetFieldID(env, dtoClass, "context", "I");
206 (*env)->SetIntField(env, dto, contextFieldId, clib_net_to_host_u32(mp->context));
208 jfieldID retvalFieldId = (*env)->GetFieldID(env, dtoClass, "retval", "I");
209 (*env)->SetIntField(env, dto, retvalFieldId, clib_net_to_host_u32(mp->retval));
211 jfieldID clientIndexFieldId = (*env)->GetFieldID(env, dtoClass, "clientIndex", "I");
212 (*env)->SetIntField(env, dto, clientIndexFieldId, clib_net_to_host_u32(mp->client_index));
214 jfieldID vpePidFieldId = (*env)->GetFieldID(env, dtoClass, "vpePid", "I");
215 (*env)->SetIntField(env, dto, vpePidFieldId, clib_net_to_host_u32(mp->vpe_pid));
217 (*env)->CallVoidMethod(env, jm->callback, callbackMethod, dto);
221 jm->result_ready = 1;
225 jint JNI_OnLoad(JavaVM *vm, void *reserved) {
226 vppjni_main_t * jm = &vppjni_main;
228 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_6) != JNI_OK) {
233 return JNI_VERSION_1_6;
236 void JNI_OnUnload(JavaVM *vm, void *reserved) {
237 vppjni_main_t * jm = &vppjni_main;
239 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_6) != JNI_OK) {
244 (*env)->DeleteGlobalRef(env, jm->callbackClass);
245 (*env)->DeleteGlobalRef(env, jm->callback);
247 jm->callbackClass = NULL;
253 static int connect_to_vpe(char *name)
255 vppjni_main_t * jm = &vppjni_main;
256 api_main_t * am = &api_main;
258 if (vl_client_connect_to_vlib("/vpe-api", name, 32) < 0)
261 jm->my_client_index = am->my_client_index;
262 jm->vl_input_queue = am->shmem_hdr->vl_input_queue;
265 vl_msg_api_set_handlers(VL_API_##N, #n, \
266 vl_api_##n##_t_handler, \
268 vl_api_##n##_t_endian, \
269 vl_api_##n##_t_print, \
270 sizeof(vl_api_##n##_t), 1);