blob: f7e2b901cdf9dd6cc342d54b9f63f72c297ca98b [file] [log] [blame]
Sagar Srivastav5c3bfab2016-11-20 20:31:49 -08001/*
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 <ioam/lib-pot/pot_msg_enum.h>
19#define vl_typedefs /* define message structures */
20#include <ioam/lib-pot/pot_all_api_h.h>
21#undef vl_typedefs
22
23#define vl_endianfun
24#include <ioam/lib-pot/pot_all_api_h.h>
25#undef vl_endianfun
26
27#define vl_print(handle, ...)
28#define vl_printfun
29#include <ioam/lib-pot/pot_all_api_h.h>
30#undef vl_printfun
31
Sagar Srivastav5c3bfab2016-11-20 20:31:49 -080032#include <vnet/api_errno.h>
33#include <vlibapi/api.h>
34#include <vlibmemory/api.h>
35
36#if VPPJNI_DEBUG == 1
37 #define DEBUG_LOG(...) clib_warning(__VA_ARGS__)
38#else
39 #define DEBUG_LOG(...)
40#endif
41
42#include <jvpp-common/jvpp_common.h>
43
Damjan Marioncb034b92016-12-28 18:38:59 +010044#include "jvpp-ioampot/io_fd_vpp_jvpp_ioampot_JVppIoampotImpl.h"
Sagar Srivastav5c3bfab2016-11-20 20:31:49 -080045#include "jvpp_ioam_pot.h"
Damjan Marioncb034b92016-12-28 18:38:59 +010046#include "jvpp-ioampot/jvpp_ioampot_gen.h"
Sagar Srivastav5c3bfab2016-11-20 20:31:49 -080047
48/*
49 * Class: io_fd_vpp_jvpp_ioampot_JVppIoampotImpl
50 * Method: init0
51 * Signature: (JI)V
52 */
53JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_ioampot_JVppIoampotImpl_init0
54 (JNIEnv *env, jclass clazz, jobject callback, jlong queue_address, jint my_client_index) {
55 ioampot_main_t * plugin_main = &ioampot_main;
Sagar Srivastav5c3bfab2016-11-20 20:31:49 -080056 clib_warning ("Java_io_fd_vpp_jvpp_ioampot_JVppIoampotImpl_init0");
57
58 plugin_main->my_client_index = my_client_index;
59 plugin_main->vl_input_queue = (unix_shared_memory_queue_t *)queue_address;
60
Marek Gradzki2291a362017-02-20 09:14:13 +010061 plugin_main->callbackObject = (*env)->NewGlobalRef(env, callback);
62 plugin_main->callbackClass = (jclass)(*env)->NewGlobalRef(env, (*env)->GetObjectClass(env, callback));
Sagar Srivastav5c3bfab2016-11-20 20:31:49 -080063
Marek Gradzki2291a362017-02-20 09:14:13 +010064 // verify API has not changed since jar generation
65 #define _(N) \
66 get_message_id(env, #N);
67 foreach_supported_api_message;
68 #undef _
Sagar Srivastav5c3bfab2016-11-20 20:31:49 -080069
Marek Gradzki2291a362017-02-20 09:14:13 +010070 #define _(N,n) \
71 vl_msg_api_set_handlers(get_message_id(env, #N), #n, \
72 vl_api_##n##_t_handler, \
73 vl_noop_handler, \
74 vl_api_##n##_t_endian, \
75 vl_api_##n##_t_print, \
76 sizeof(vl_api_##n##_t), 1);
77 foreach_api_reply_handler;
78 #undef _
Sagar Srivastav5c3bfab2016-11-20 20:31:49 -080079}
80
81JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_ioampot_JVppIoampotImpl_close0
82(JNIEnv *env, jclass clazz) {
83 ioampot_main_t * plugin_main = &ioampot_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/* Attach thread to JVM and cache class references when initiating JVPP iOAM POT */
94jint JNI_OnLoad(JavaVM *vm, void *reserved) {
95 JNIEnv* env;
96
97 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
98 return JNI_EVERSION;
99 }
100
101 if (cache_class_references(env) != 0) {
102 clib_warning ("Failed to cache class references\n");
103 return JNI_ERR;
104 }
105
106 return JNI_VERSION_1_8;
107}
108
109/* Clean up cached references when disposing JVPP iOAM POT */
110void JNI_OnUnload(JavaVM *vm, void *reserved) {
111 JNIEnv* env;
112 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
113 return;
114 }
115 delete_class_references(env);
116}