3 # Copyright (c) 2018 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 from string import Template
18 from jni_common_gen import generate_j2c_identifiers, generate_j2c_swap
19 from jvpp_model import is_dump, is_request, is_control_ping, is_control_ping_reply
22 def generate_jni_impl(model):
24 Generates JNI bindings for sending dump and request messages.
25 :param model: meta-model of VPP API used for jVPP generation.
28 for msg in model.messages:
29 if is_control_ping(msg) or is_control_ping_reply(msg):
30 # Skip control ping managed by jvpp registry.
32 if not (is_dump(msg) or is_request(msg)):
37 msg_initialization = ""
40 arguments = ", jobject request"
41 request_class = _REQUEST_CLASS_TEMPLATE.substitute(
42 plugin_name=model.plugin_name,
43 java_dto_name=msg.java_name_upper
45 jni_identifiers = generate_j2c_identifiers(msg, class_ref_name="requestClass", object_ref_name="request")
46 msg_initialization = generate_j2c_swap(msg, struct_ref_name="mp")
48 jni_impl.append(_JNI_IMPL_TEMPLATE.substitute(
50 json_filename=model.json_api_files,
51 json_definition=msg.doc,
52 plugin_name=model.plugin_name,
53 plugin_java_name=model.plugin_java_name,
54 java_method_name=msg.java_name_lower,
56 request_class=request_class,
57 jni_identifiers=jni_identifiers,
58 msg_size=_generate_msg_size(msg),
60 msg_initialization=msg_initialization
62 return "".join(jni_impl)
65 _JNI_IMPL_TEMPLATE = Template("""
67 * JNI binding for sending ${c_name} message.
68 * Generated based on $json_filename:
71 JNIEXPORT jint JNICALL Java_io_fd_vpp_jvpp_${plugin_name}_JVpp${plugin_java_name}Impl_${java_method_name}0
72 (JNIEnv * env, jclass clazz${arguments}) {
73 ${plugin_name}_main_t *plugin_main = &${plugin_name}_main;
74 vl_api_${c_name}_t * mp;
75 u32 my_context_id = vppjni_get_context_id (&jvpp_main);
80 const size_t _size = ${msg_size};
81 mp = vl_msg_api_alloc(_size);
82 memset (mp, 0, _size);
83 mp->_vl_msg_id = ntohs (get_message_id(env, "${c_name}_${crc}"));
84 mp->client_index = plugin_main->my_client_index;
85 mp->context = clib_host_to_net_u32 (my_context_id);
91 clib_warning ("Sending ${c_name} message");
92 vl_msg_api_send_shmem (plugin_main->vl_input_queue, (u8 *)&mp);
93 if ((*env)->ExceptionCheck(env)) {
99 # TODO: cache method and field identifiers to achieve better performance
100 # https://jira.fd.io/browse/HONEYCOMB-42
101 _REQUEST_CLASS_TEMPLATE = Template(""" jclass requestClass = (*env)->FindClass(env, "io/fd/vpp/jvpp/${plugin_name}/dto/${java_dto_name}");
105 def _generate_msg_size(msg):
106 msg_size = "sizeof(*mp)"
107 _size_components = []
108 for field in msg.fields:
109 # Ignore ZLAs for simplicity (to support them we need to call JNI functions to check actual size)
110 if field.array_len_field:
111 _size_components += " + %s*sizeof(%s)" % (field.array_len_field.java_name, field.type.base_type.vpp_name)
112 # FIXME(VPP-586): for proper nested structures support, we need generate functions computing type sizes
113 # and use it instead of sizeof
115 return msg_size + "".join(_size_components)