jvpp_nsh.c revision aa609dfe
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/nsh_msg_enum.h>
19#define vl_typedefs             /* define message structures */
20#include <vpp-api/nsh_all_api_h.h>
21#undef vl_typedefs
22
23#define vl_endianfun
24#include <vpp-api/nsh_all_api_h.h>
25#undef vl_endianfun
26
27#define vl_print(handle, ...)
28#define vl_printfun
29#include <vpp-api/nsh_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
36#include <jvpp-common/jvpp_common.h>
37
38#include "jvpp/io_fd_vpp_jvpp_nsh_JVppNshImpl.h"
39
40#include "jvpp_nsh.h"
41#include "jvpp/jvpp_nsh_gen.h"
42
43/*
44 * Class:     io_fd_vpp_jvpp_nsh_JVppNshImpl
45 * Method:    init0
46 * Signature: (JI)V
47 */
48JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_nsh_JVppNshImpl_init0
49  (JNIEnv *env, jclass clazz, jobject callback, jlong queue_address, jint my_client_index) {
50    nsh_main_t * plugin_main = &nsh_main;
51    clib_warning ("Java_io_fd_vpp_jvpp_nsh_JVppNshImpl_init0");
52
53    plugin_main->my_client_index = my_client_index;
54    plugin_main->vl_input_queue = (unix_shared_memory_queue_t *)queue_address;
55
56    plugin_main->callbackObject = (*env)->NewGlobalRef(env, callback);
57    plugin_main->callbackClass = (jclass)(*env)->NewGlobalRef(env, (*env)->GetObjectClass(env, callback));
58
59    // verify API has not changed since jar generation
60    #define _(N)             \
61        get_message_id(env, #N);
62        foreach_supported_api_message;
63    #undef _
64
65    #define _(N,n)                                  \
66        vl_msg_api_set_handlers(get_message_id(env, #N), #n,     \
67    		vl_api_##n##_t_handler,             \
68    		vl_noop_handler,                    \
69    		vl_api_##n##_t_endian,              \
70    		vl_api_##n##_t_print,               \
71	    	sizeof(vl_api_##n##_t), 1);
72        foreach_api_reply_handler;
73    #undef _
74}
75
76JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_nsh_JVppNshImpl_close0
77(JNIEnv *env, jclass clazz) {
78	nsh_main_t * plugin_main = &nsh_main;
79
80    // cleanup:
81    (*env)->DeleteGlobalRef(env, plugin_main->callbackClass);
82    (*env)->DeleteGlobalRef(env, plugin_main->callbackObject);
83
84    plugin_main->callbackClass = NULL;
85    plugin_main->callbackObject = NULL;
86}
87
88jint JNI_OnLoad(JavaVM *vm, void *reserved) {
89    JNIEnv* env;
90
91    if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
92        return JNI_EVERSION;
93    }
94
95    if (cache_class_references(env) != 0) {
96        clib_warning ("Failed to cache class references\n");
97        return JNI_ERR;
98    }
99
100    return JNI_VERSION_1_8;
101}
102
103void JNI_OnUnload(JavaVM *vm, void *reserved) {
104    JNIEnv* env;
105    if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
106        return;
107    }
108    delete_class_references(env);
109}
110
111
112