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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/vnet.h>
18 #include <vpp/api/vpe_msg_enum.h>
19 #define vl_typedefs /* define message structures */
20 #include <vpp/api/vpe_all_api_h.h>
23 #include <vnet/api_errno.h>
24 #include <vlibapi/api.h>
25 #include <vlibmemory/api.h>
28 #include <jvpp-common/jvpp_common.h>
30 // TODO: generate jvpp_plugin_name.c files (or at least reuse plugin's main structure)
32 /* Pointer to shared memory queue */
33 unix_shared_memory_queue_t * vl_input_queue;
35 /* VPP api client index */
38 /* Callback object and class references enabling asynchronous Java calls */
39 jobject callbackObject;
44 core_main_t core_main __attribute__((aligned (64)));
46 #include "io_fd_vpp_jvpp_core_JVppCoreImpl.h"
47 #include "jvpp_core_gen.h"
49 JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_core_JVppCoreImpl_init0
50 (JNIEnv * env, jclass clazz, jobject callback, jlong queue_address, jint my_client_index) {
51 core_main_t * plugin_main = &core_main;
52 plugin_main->my_client_index = my_client_index;
53 plugin_main->vl_input_queue = (unix_shared_memory_queue_t *)queue_address;
55 plugin_main->callbackObject = (*env)->NewGlobalRef(env, callback);
56 plugin_main->callbackClass = (jclass)(*env)->NewGlobalRef(env, (*env)->GetObjectClass(env, callback));
58 // verify API has not changed since jar generation
60 get_message_id(env, #N); \
61 foreach_supported_api_message;
65 vl_msg_api_set_handlers(get_message_id(env, #N), #n, \
66 vl_api_##n##_t_handler, \
70 sizeof(vl_api_##n##_t), 1);
71 foreach_api_reply_handler;
75 JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_core_JVppCoreImpl_close0
76 (JNIEnv *env, jclass clazz) {
77 core_main_t * plugin_main = &core_main;
80 (*env)->DeleteGlobalRef(env, plugin_main->callbackClass);
81 (*env)->DeleteGlobalRef(env, plugin_main->callbackObject);
83 plugin_main->callbackClass = NULL;
84 plugin_main->callbackObject = NULL;
87 jint JNI_OnLoad(JavaVM *vm, void *reserved) {
90 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
94 if (cache_class_references(env) != 0) {
95 clib_warning ("Failed to cache class references\n");
99 return JNI_VERSION_1_8;
102 void JNI_OnUnload(JavaVM *vm, void *reserved) {
104 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
107 delete_class_references(env);