FD.io VPP  v19.01.3-6-g70449b9b9
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>
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 #include <vnet/api_errno.h>
24 #include <vlibapi/api.h>
25 #include <vlibmemory/api.h>
26 #include <jni.h>
27 #include <jvpp_core.h>
28 
29 
30 // TODO: generate jvpp_plugin_name.c files (or at least reuse plugin's main structure)
31 typedef struct {
32  /* Pointer to shared memory queue */
34 
35  /* VPP api client index */
37 
38  /* Callback object and class references enabling asynchronous Java calls */
39  jobject callbackObject;
40  jclass callbackClass;
41 
42 } core_main_t;
43 
44 core_main_t core_main __attribute__((aligned (64)));
45 
46 #include "io_fd_vpp_jvpp_core_JVppCoreImpl.h"
47 #include "jvpp_core_gen.h"
48 
50 (JNIEnv * env, jclass clazz, jobject callback, jlong queue_address, jint my_client_index) {
51  core_main_t * plugin_main = &core_main;
52  plugin_main->my_client_index = my_client_index;
53  plugin_main->vl_input_queue = uword_to_pointer (queue_address, svm_queue_t *);
54 
55  plugin_main->callbackObject = (*env)->NewGlobalRef(env, callback);
56  plugin_main->callbackClass = (jclass)(*env)->NewGlobalRef(env, (*env)->GetObjectClass(env, callback));
57 
58  // verify API has not changed since jar generation (exit on mismatch)
59  #define _(N) \
60  if (get_message_id(env, #N) == 0) return;
61  foreach_supported_api_message;
62  #undef _
63 
64  #define _(N,n) \
65  vl_msg_api_set_handlers(get_message_id(env, #N), #n, \
66  vl_api_##n##_t_handler, \
67  vl_noop_handler, \
68  vl_noop_handler, \
69  vl_noop_handler, \
70  sizeof(vl_api_##n##_t), 1);
71  foreach_api_reply_handler;
72  #undef _
73 }
74 
76 (JNIEnv *env, jclass clazz) {
77  core_main_t * plugin_main = &core_main;
78 
79  // cleanup:
80  (*env)->DeleteGlobalRef(env, plugin_main->callbackClass);
81  (*env)->DeleteGlobalRef(env, plugin_main->callbackObject);
82 
83  plugin_main->callbackClass = NULL;
84  plugin_main->callbackObject = NULL;
85 }
86 
87 jint JNI_OnLoad(JavaVM *vm, void *reserved) {
88  JNIEnv* env;
89 
90  if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
91  return JNI_EVERSION;
92  }
93 
94  if (cache_class_references(env) != 0) {
95  clib_warning ("Failed to cache class references\n");
96  return JNI_ERR;
97  }
98 
99  return JNI_VERSION_1_8;
100 }
101 
102 void JNI_OnUnload(JavaVM *vm, void *reserved) {
103  JNIEnv* env;
104  if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
105  return;
106  }
107  delete_class_references(env);
108 }
109 
110 
111 static void _host_to_net_string(JNIEnv * env, jstring javaString, vl_api_string_t * vl_api_string)
112 {
113  const char *nativeString;
114  // prevent null, which causes jni to crash
115  if (NULL != javaString) {
116  nativeString = (*env)->GetStringUTFChars(env, javaString, 0);
117  } else{
118  nativeString = "";
119  }
120 
121  vl_api_to_api_string(jstr_length(env, javaString), nativeString, vl_api_string);
122 
123  (*env)->ReleaseStringUTFChars(env, javaString, nativeString);
124 }
125 
126 
127 static jstring _net_to_host_string(JNIEnv * env, const vl_api_string_t * _net)
128 {
129  return (*env)->NewStringUTF(env, (char *)_net->buf);
130 }
131 
132 
133 static size_t jstr_length(JNIEnv *env, jstring string)
134 {
135  return ((int) (*env)->GetStringUTFLength(env, string));
136 }
svm_queue_t * vl_input_queue
Definition: jvpp_core.c:33
u32 my_client_index
Definition: jvpp_core.c:36
static size_t jstr_length(JNIEnv *env, jstring string)
Definition: jvpp_core.c:133
#define NULL
Definition: clib.h:58
static int vl_api_to_api_string(u32 len, const char *buf, vl_api_string_t *str)
Definition: api_types.h:35
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:50
jint JNI_OnLoad(JavaVM *vm, void *reserved)
Definition: jvpp_core.c:87
unsigned int u32
Definition: types.h:88
void JNI_OnUnload(JavaVM *vm, void *reserved)
Definition: jvpp_core.c:102
vlib_main_t * vm
Definition: buffer.c:301
#define clib_warning(format, args...)
Definition: error.h:59
jclass callbackClass
Definition: jvpp_core.c:40
#define uword_to_pointer(u, type)
Definition: types.h:136
core_main_t core_main
Definition: jvpp_core.c:44
jobject callbackObject
Definition: jvpp_core.c:39
JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_core_JVppCoreImpl_close0(JNIEnv *env, jclass clazz)
Definition: jvpp_core.c:76
struct _svm_queue svm_queue_t