8c579811af9af44dc325cc63613821b0d5be5433
[vpp.git] / src / vpp-api / java / jvpp-core / jvpp_core.c
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/vpe_msg_enum.h>
19 #define vl_typedefs             /* define message structures */
20 #include <vpp/api/vpe_all_api_h.h>
21 #undef vl_typedefs
22
23 #define vl_endianfun
24 #include <vpp/api/vpe_all_api_h.h>
25 #undef vl_endianfun
26
27 #define vl_print(handle, ...)
28 #define vl_printfun
29 #include <vpp/api/vpe_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 #include <jni.h>
36
37 #include <jvpp-common/jvpp_common.h>
38
39 // TODO: generate jvpp_plugin_name.c files (or at least reuse plugin's main structure)
40 typedef struct {
41     /* Pointer to shared memory queue */
42     unix_shared_memory_queue_t * vl_input_queue;
43
44     /* VPP api client index */
45     u32 my_client_index;
46
47     /* Callback object and class references enabling asynchronous Java calls */
48     jobject callbackObject;
49     jclass callbackClass;
50
51 } core_main_t;
52
53 core_main_t core_main __attribute__((aligned (64)));
54
55 #include "io_fd_vpp_jvpp_core_JVppCoreImpl.h"
56 #include "jvpp_core_gen.h"
57
58 JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_core_JVppCoreImpl_init0
59 (JNIEnv * env, jclass clazz, jobject callback, jlong queue_address, jint my_client_index) {
60     core_main_t * plugin_main = &core_main;
61     plugin_main->my_client_index = my_client_index;
62     plugin_main->vl_input_queue = (unix_shared_memory_queue_t *)queue_address;
63
64     plugin_main->callbackObject = (*env)->NewGlobalRef(env, callback);
65     plugin_main->callbackClass = (jclass)(*env)->NewGlobalRef(env, (*env)->GetObjectClass(env, callback));
66
67     // verify API has not changed since jar generation
68     #define _(N)             \
69         get_message_id(env, #N);  \
70         foreach_supported_api_message;
71     #undef _
72
73     #define _(N,n)                                  \
74         vl_msg_api_set_handlers(get_message_id(env, #N), #n,     \
75                 vl_api_##n##_t_handler,             \
76                 vl_noop_handler,                    \
77                 vl_noop_handler,              \
78                 vl_noop_handler,               \
79                 sizeof(vl_api_##n##_t), 1);
80         foreach_api_reply_handler;
81     #undef _
82 }
83
84 JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_core_JVppCoreImpl_close0
85 (JNIEnv *env, jclass clazz) {
86     core_main_t * plugin_main = &core_main;
87
88     // cleanup:
89     (*env)->DeleteGlobalRef(env, plugin_main->callbackClass);
90     (*env)->DeleteGlobalRef(env, plugin_main->callbackObject);
91
92     plugin_main->callbackClass = NULL;
93     plugin_main->callbackObject = NULL;
94 }
95
96 jint JNI_OnLoad(JavaVM *vm, void *reserved) {
97     JNIEnv* env;
98
99     if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
100         return JNI_EVERSION;
101     }
102
103     if (cache_class_references(env) != 0) {
104         clib_warning ("Failed to cache class references\n");
105         return JNI_ERR;
106     }
107
108     return JNI_VERSION_1_8;
109 }
110
111 void JNI_OnUnload(JavaVM *vm, void *reserved) {
112     JNIEnv* env;
113     if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
114         return;
115     }
116     delete_class_references(env);
117 }
118
119
120