jvpp_nsh.c revision d90f85ca
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/* Get the API version number */
33#define vl_api_version(n,v) static u32 api_version=(v);
34#include <vpp-api/nsh_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#include <jvpp-common/jvpp_common.h>
42
43#include "jvpp/io_fd_vpp_jvpp_nsh_JVppNshImpl.h"
44
45#include "jvpp_nsh.h"
46#include "jvpp/jvpp_nsh_gen.h"
47
48/*
49 * Class:     io_fd_vpp_jvpp_nsh_JVppNshImpl
50 * Method:    init0
51 * Signature: (JI)V
52 */
53JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_nsh_JVppNshImpl_init0
54  (JNIEnv *env, jclass clazz, jobject callback, jlong queue_address, jint my_client_index) {
55    nsh_main_t * plugin_main = &nsh_main;
56    u8 * name;
57    clib_warning ("Java_io_fd_vpp_jvpp_nsh_JVppNshImpl_init0");
58
59    plugin_main->my_client_index = my_client_index;
60    plugin_main->vl_input_queue = (unix_shared_memory_queue_t *)queue_address;
61
62    name = format (0, "nsh_%08x%c", api_version, 0);
63    plugin_main->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
64
65    if (plugin_main->msg_id_base == (u16) ~0) {
66        jclass exClass = (*env)->FindClass(env, "java/lang/IllegalStateException");
67        (*env)->ThrowNew(env, exClass, "nsh plugin is not loaded in VPP");
68    } else {
69        plugin_main->callbackObject = (*env)->NewGlobalRef(env, callback);
70        plugin_main->callbackClass = (jclass)(*env)->NewGlobalRef(env, (*env)->GetObjectClass(env, callback));
71
72        // verify API has not changed since jar generation
73        #define _(N)             \
74            get_message_id(env, #N);
75            foreach_supported_api_message;
76        #undef _
77
78        #define _(N,n)                                  \
79            vl_msg_api_set_handlers(get_message_id(env, #N), #n,     \
80    		    vl_api_##n##_t_handler,             \
81    		    vl_noop_handler,                    \
82    		    vl_api_##n##_t_endian,              \
83    		    vl_api_##n##_t_print,               \
84	    	    sizeof(vl_api_##n##_t), 1);
85            foreach_api_reply_handler;
86        #undef _
87    }
88}
89
90JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_nsh_JVppNshImpl_close0
91(JNIEnv *env, jclass clazz) {
92	nsh_main_t * plugin_main = &nsh_main;
93
94    // cleanup:
95    (*env)->DeleteGlobalRef(env, plugin_main->callbackClass);
96    (*env)->DeleteGlobalRef(env, plugin_main->callbackObject);
97
98    plugin_main->callbackClass = NULL;
99    plugin_main->callbackObject = NULL;
100}
101
102jint 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
117void JNI_OnUnload(JavaVM *vm, void *reserved) {
118    JNIEnv* env;
119    if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
120        return;
121    }
122    delete_class_references(env);
123}
124
125
126