blob: 2f74b5ad077a7a062920ac6cb66c80657bf273a4 [file] [log] [blame]
Shwetha04714482016-10-13 01:46:19 +01001/*
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-trace/trace_msg_enum.h>
19#define vl_typedefs /* define message structures */
20#include <ioam/lib-trace/trace_all_api_h.h>
21#undef vl_typedefs
22
23#define vl_endianfun
24#include <ioam/lib-trace/trace_all_api_h.h>
25#undef vl_endianfun
26
27#define vl_print(handle, ...)
28#define vl_printfun
29#include <ioam/lib-trace/trace_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 <ioam/lib-trace/trace_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
47#include <jvpp-common/jvpp_common.h>
48
Damjan Marioncb034b92016-12-28 18:38:59 +010049#include "jvpp-ioamtrace/io_fd_vpp_jvpp_ioamtrace_JVppIoamtraceImpl.h"
Shwetha04714482016-10-13 01:46:19 +010050#include "jvpp_ioam_trace.h"
Damjan Marioncb034b92016-12-28 18:38:59 +010051#include "jvpp-ioamtrace/jvpp_ioamtrace_gen.h"
Shwetha04714482016-10-13 01:46:19 +010052
53/*
54 * Class: io_fd_vpp_jvpp_ioamtrace_JVppIoamtraceImpl
55 * Method: init0
56 * Signature: (JI)V
57 */
58JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_ioamtrace_JVppIoamtraceImpl_init0
59 (JNIEnv *env, jclass clazz, jobject callback, jlong queue_address, jint my_client_index) {
60 ioamtrace_main_t * plugin_main = &ioamtrace_main;
61 u8 * name;
62 clib_warning ("Java_io_fd_vpp_jvpp_ioamtrace_JVppIoamtraceImpl_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
Marek Gradzkibdb1f812016-11-03 10:52:43 +010067 name = format (0, "ioam_trace_%08x%c", api_version, 0);
68 plugin_main->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
Shwetha04714482016-10-13 01:46:19 +010069
Marek Gradzkibdb1f812016-11-03 10:52:43 +010070 if (plugin_main->msg_id_base == (u16) ~0) {
71 jclass exClass = (*env)->FindClass(env, "java/lang/IllegalStateException");
72 (*env)->ThrowNew(env, exClass, "ioam_trace 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));
Shwetha04714482016-10-13 01:46:19 +010076
Marek Gradzki4746a5d2017-01-27 08:57:40 +010077 // verify API has not changed since jar generation
78 #define _(N) \
79 get_message_id(env, #N); \
80 foreach_supported_api_message;
81 #undef _
82
Shwetha04714482016-10-13 01:46:19 +010083 #define _(N,n) \
Marek Gradzki4746a5d2017-01-27 08:57:40 +010084 vl_msg_api_set_handlers(get_message_id(env, #N), #n, \
Shwetha04714482016-10-13 01:46:19 +010085 vl_api_##n##_t_handler, \
86 vl_noop_handler, \
87 vl_api_##n##_t_endian, \
88 vl_api_##n##_t_print, \
89 sizeof(vl_api_##n##_t), 1);
90 foreach_api_reply_handler;
91 #undef _
Marek Gradzkibdb1f812016-11-03 10:52:43 +010092 }
Shwetha04714482016-10-13 01:46:19 +010093}
94
95JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_ioamtrace_JVppIoamtraceImpl_close0
96(JNIEnv *env, jclass clazz) {
97 ioamtrace_main_t * plugin_main = &ioamtrace_main;
98
99 // cleanup:
100 (*env)->DeleteGlobalRef(env, plugin_main->callbackClass);
101 (*env)->DeleteGlobalRef(env, plugin_main->callbackObject);
102
103 plugin_main->callbackClass = NULL;
104 plugin_main->callbackObject = NULL;
105}
106
107/* Attach thread to JVM and cache class references when initiating JVPP iOAM Trace */
108jint JNI_OnLoad(JavaVM *vm, void *reserved) {
109 JNIEnv* env;
110
111 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
112 return JNI_EVERSION;
113 }
114
115 if (cache_class_references(env) != 0) {
116 clib_warning ("Failed to cache class references\n");
117 return JNI_ERR;
118 }
119
120 return JNI_VERSION_1_8;
121}
122
123/* Clean up cached references when disposing JVPP iOAM Trace */
124void JNI_OnUnload(JavaVM *vm, void *reserved) {
125 JNIEnv* env;
126 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
127 return;
128 }
129 delete_class_references(env);
130}