FD.io VPP  v16.12-rc0-308-g931be3a
Vector Packet Processing
jvpp_core.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 
16 #include <vnet/vnet.h>
17 
18 #include <vpp-api/vpe_msg_enum.h>
19 #define vl_typedefs /* define message structures */
20 #include <vpp-api/vpe_all_api_h.h>
21 #undef vl_typedefs
22 
23 #define vl_endianfun
24 #include <vpp-api/vpe_all_api_h.h>
25 #undef vl_endianfun
26 
27 #define vl_print(handle, ...)
28 #define vl_printfun
29 #include <vpp-api/vpe_all_api_h.h>
30 #undef vl_printfun
31 
32 #include <vnet/api_errno.h>
33 #include <vlibapi/api.h>
34 #include <vlibmemory/api.h>
35 #include <jni.h>
36 
38 
39 // TODO: generate jvpp_plugin_name.c files (or at least reuse plugin's main structure)
40 typedef struct {
41  /* Base message index for the nsh plugin */
43 
44  /* Pointer to shared memory queue */
46 
47  /* VPP api client index */
49 
50  /* Callback object and class references enabling asynchronous Java calls */
51  jobject callbackObject;
52  jclass callbackClass;
53 
54 } core_main_t;
55 
56 core_main_t core_main __attribute__((aligned (64)));
57 
58 #include "io_fd_vpp_jvpp_core_JVppCoreImpl.h"
59 #include "jvpp_core_gen.h"
60 
62 (JNIEnv * env, jclass clazz, jobject callback, jlong queue_address, jint my_client_index) {
63  core_main_t * plugin_main = &core_main;
64  plugin_main->my_client_index = my_client_index;
65  plugin_main->vl_input_queue = (unix_shared_memory_queue_t *)queue_address;
66 
67  plugin_main->callbackObject = (*env)->NewGlobalRef(env, callback);
68  plugin_main->callbackClass = (jclass)(*env)->NewGlobalRef(env, (*env)->GetObjectClass(env, callback));
69 
70  #define _(N,n) \
71  vl_msg_api_set_handlers(VL_API_##N, #n, \
72  vl_api_##n##_t_handler, \
73  vl_noop_handler, \
74  vl_noop_handler, \
75  vl_noop_handler, \
76  sizeof(vl_api_##n##_t), 1);
77  foreach_api_reply_handler;
78  #undef _
79 }
80 
82 (JNIEnv *env, jclass clazz) {
83  core_main_t * plugin_main = &core_main;
84 
85  // cleanup:
86  (*env)->DeleteGlobalRef(env, plugin_main->callbackClass);
87  (*env)->DeleteGlobalRef(env, plugin_main->callbackObject);
88 
89  plugin_main->callbackClass = NULL;
90  plugin_main->callbackObject = NULL;
91 }
92 
93 jint JNI_OnLoad(JavaVM *vm, void *reserved) {
94  JNIEnv* env;
95 
96  if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
97  return JNI_EVERSION;
98  }
99 
100  if (cache_class_references(env) != 0) {
101  clib_warning ("Failed to cache class references\n");
102  return JNI_ERR;
103  }
104 
105  return JNI_VERSION_1_8;
106 }
107 
108 void JNI_OnUnload(JavaVM *vm, void *reserved) {
109  JNIEnv* env;
110  if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
111  return;
112  }
113  delete_class_references(env);
114 }
115 
116 
117 
unix_shared_memory_queue_t * vl_input_queue
Definition: jvpp_core.c:45
u32 my_client_index
Definition: jvpp_core.c:48
#define NULL
Definition: clib.h:55
JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_core_JVppCoreImpl_init0(JNIEnv *env, jclass clazz, jobject callback, jlong queue_address, jint my_client_index)
Definition: jvpp_core.c:62
jint JNI_OnLoad(JavaVM *vm, void *reserved)
Definition: jvpp_core.c:93
#define clib_warning(format, args...)
Definition: error.h:59
void JNI_OnUnload(JavaVM *vm, void *reserved)
Definition: jvpp_core.c:108
jclass callbackClass
Definition: jvpp_core.c:52
unsigned int u32
Definition: types.h:88
core_main_t core_main
Definition: jvpp_core.c:56
u16 msg_id_base
Definition: jvpp_core.c:42
unsigned short u16
Definition: types.h:57
jobject callbackObject
Definition: jvpp_core.c:51
JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_core_JVppCoreImpl_close0(JNIEnv *env, jclass clazz)
Definition: jvpp_core.c:82
struct _unix_shared_memory_queue unix_shared_memory_queue_t