FD.io VPP  v17.01-9-ge7dcee4
Vector Packet Processing
jvpp_snat.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 <snat/snat_msg_enum.h>
19 #define vl_typedefs /* define message structures */
20 #include <snat/snat_all_api_h.h>
21 #undef vl_typedefs
22 
23 #define vl_endianfun
24 #include <snat/snat_all_api_h.h>
25 #undef vl_endianfun
26 
27 #define vl_print(handle, ...)
28 #define vl_printfun
29 #include <snat/snat_all_api_h.h>
30 #undef vl_printfun
31 
32 /* Get the API version number */
33 #define vl_api_version(n,v) static u32 api_version=(v);
34 #include <snat/snat_all_api_h.h>
35 #undef vl_api_version
36 
37 #include <vnet/api_errno.h>
38 #include <vlibapi/api.h>
39 #include <vlibmemory/api.h>
40 
41 #if VPPJNI_DEBUG == 1
42  #define DEBUG_LOG(...) clib_warning(__VA_ARGS__)
43 #else
44  #define DEBUG_LOG(...)
45 #endif
46 
48 
49 #include "snat/jvpp/io_fd_vpp_jvpp_snat_JVppSnatImpl.h"
50 #include "jvpp_snat.h"
51 #include "snat/jvpp/jvpp_snat_gen.h"
52 
53 /*
54  * Class: io_fd_vpp_jvpp_snat_JVppsnatImpl
55  * Method: init0
56  * Signature: (JI)V
57  */
59  (JNIEnv *env, jclass clazz, jobject callback, jlong queue_address, jint my_client_index) {
60  snat_main_t * plugin_main = &snat_main;
61  u8 * name;
62  clib_warning ("Java_io_fd_vpp_jvpp_snat_JVppSnatImpl_init0");
63 
64  plugin_main->my_client_index = my_client_index;
65  plugin_main->vl_input_queue = (unix_shared_memory_queue_t *)queue_address;
66 
67  name = format (0, "snat_%08x%c", api_version, 0);
68  plugin_main->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
69 
70  if (plugin_main->msg_id_base == (u16) ~0) {
71  jclass exClass = (*env)->FindClass(env, "java/lang/IllegalStateException");
72  (*env)->ThrowNew(env, exClass, "snat plugin is not loaded in VPP");
73  } else {
74  plugin_main->callbackObject = (*env)->NewGlobalRef(env, callback);
75  plugin_main->callbackClass = (jclass)(*env)->NewGlobalRef(env, (*env)->GetObjectClass(env, callback));
76 
77  #define _(N,n) \
78  vl_msg_api_set_handlers(VL_API_##N + plugin_main->msg_id_base, #n, \
79  vl_api_##n##_t_handler, \
80  vl_noop_handler, \
81  vl_api_##n##_t_endian, \
82  vl_api_##n##_t_print, \
83  sizeof(vl_api_##n##_t), 1);
84  foreach_api_reply_handler;
85  #undef _
86  }
87 }
88 
90 (JNIEnv *env, jclass clazz) {
91  snat_main_t * plugin_main = &snat_main;
92 
93  // cleanup:
94  (*env)->DeleteGlobalRef(env, plugin_main->callbackClass);
95  (*env)->DeleteGlobalRef(env, plugin_main->callbackObject);
96 
97  plugin_main->callbackClass = NULL;
98  plugin_main->callbackObject = NULL;
99 }
100 
101 /* Attach thread to JVM and cache class references when initiating JVPP SNAT */
102 jint JNI_OnLoad(JavaVM *vm, void *reserved) {
103  JNIEnv* env;
104 
105  if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
106  return JNI_EVERSION;
107  }
108 
109  if (cache_class_references(env) != 0) {
110  clib_warning ("Failed to cache class references\n");
111  return JNI_ERR;
112  }
113 
114  return JNI_VERSION_1_8;
115 }
116 
117 /* Clean up cached references when disposing JVPP SNAT */
118 void JNI_OnUnload(JavaVM *vm, void *reserved) {
119  JNIEnv* env;
120  if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
121  return;
122  }
123  delete_class_references(env);
124 }
#define NULL
Definition: clib.h:55
jobject callbackObject
Definition: jvpp_snat.h:37
void JNI_OnUnload(JavaVM *vm, void *reserved)
Definition: jvpp_snat.c:118
JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_snat_JVppSnatImpl_init0(JNIEnv *env, jclass clazz, jobject callback, jlong queue_address, jint my_client_index)
Definition: jvpp_snat.c:59
#define clib_warning(format, args...)
Definition: error.h:59
jclass callbackClass
Definition: jvpp_snat.h:38
jint JNI_OnLoad(JavaVM *vm, void *reserved)
Definition: jvpp_snat.c:102
snat_main_t snat_main
Definition: jvpp_snat.h:42
u16 vl_client_get_first_plugin_msg_id(char *plugin_name)
u16 msg_id_base
Definition: jvpp_snat.h:28
JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_snat_JVppSnatImpl_close0(JNIEnv *env, jclass clazz)
Definition: jvpp_snat.c:90
unsigned short u16
Definition: types.h:57
u32 my_client_index
Definition: jvpp_snat.h:34
unsigned char u8
Definition: types.h:56
unix_shared_memory_queue_t * vl_input_queue
Definition: jvpp_snat.h:31
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
struct _unix_shared_memory_queue unix_shared_memory_queue_t