blob: 2b58b36001523264bc1e78a3d5a0bf731ad395db [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 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 */
Robert Varga40223da2016-02-10 15:57:23 +010015#define _GNU_SOURCE /* for strcasestr(3) */
Ed Warnickecb9cada2015-12-08 15:45:58 -070016#include <vnet/vnet.h>
17
18#define vl_api_version(n,v) static u32 vpe_api_version = (v);
Dave Barachaa6920e2016-06-27 09:25:13 -040019#include <vpp-api/vpe.api.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070020#undef vl_api_version
21
22#include <jni.h>
23#include <japi/vppjni.h>
24#include <japi/vppjni_bridge_domain.h>
Robert Varga81d99ac2016-01-30 18:30:36 +010025#include <japi/vppjni_env.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070026#include <japi/org_openvpp_vppjapi_vppConn.h>
27#include <japi/org_openvpp_vppjapi_vppApi.h>
28
Dave Barachaa6920e2016-06-27 09:25:13 -040029#include <vpp-api/vpe_msg_enum.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070030#define vl_typedefs /* define message structures */
Dave Barachaa6920e2016-06-27 09:25:13 -040031#include <vpp-api/vpe_all_api_h.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070032#undef vl_typedefs
33
Robert Vargad0f92092016-02-10 14:39:57 +010034#define vl_endianfun
Dave Barachaa6920e2016-06-27 09:25:13 -040035#include <vpp-api/vpe_all_api_h.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070036#undef vl_endianfun
37
38/* instantiate all the print functions we know about */
39#define vl_print(handle, ...)
40#define vl_printfun
Dave Barachaa6920e2016-06-27 09:25:13 -040041#include <vpp-api/vpe_all_api_h.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070042#undef vl_printfun
43
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +020044#ifndef VPPJNI_DEBUG
Dave Wallacebf8c15e2015-12-17 20:54:54 -050045#define VPPJNI_DEBUG 0
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +020046#endif
Dave Wallacebf8c15e2015-12-17 20:54:54 -050047
48#if VPPJNI_DEBUG == 1
49 #define DEBUG_LOG(...) clib_warning(__VA_ARGS__)
50#else
51 #define DEBUG_LOG(...)
52#endif
53
Ed Warnickecb9cada2015-12-08 15:45:58 -070054static int connect_to_vpe(char *name);
55
Robert Vargad0f92092016-02-10 14:39:57 +010056/*
Ed Warnickecb9cada2015-12-08 15:45:58 -070057 * The Java runtime isn't compile w/ -fstack-protector,
58 * so we have to supply missing external references for the
59 * regular vpp libraries. Weak reference in case folks get religion
60 * at a later date...
61 */
62void __stack_chk_guard (void) __attribute__((weak));
63void __stack_chk_guard (void) { }
64
Robert Varga81d99ac2016-01-30 18:30:36 +010065BIND_JAPI_CLASS(vppBridgeDomainDetails, "()V");
66BIND_JAPI_BOOL_FIELD(vppBridgeDomainDetails, arpTerm);
67BIND_JAPI_BOOL_FIELD(vppBridgeDomainDetails, flood);
68BIND_JAPI_BOOL_FIELD(vppBridgeDomainDetails, forward);
69BIND_JAPI_BOOL_FIELD(vppBridgeDomainDetails, learn);
70BIND_JAPI_BOOL_FIELD(vppBridgeDomainDetails, uuFlood);
71BIND_JAPI_INT_FIELD(vppBridgeDomainDetails, bdId);
72BIND_JAPI_STRING_FIELD(vppBridgeDomainDetails, name);
73BIND_JAPI_STRING_FIELD(vppBridgeDomainDetails, bviInterfaceName);
74BIND_JAPI_OBJ_FIELD(vppBridgeDomainDetails, interfaces, "[Lorg/openvpp/vppjapi/vppBridgeDomainInterfaceDetails;");
75
76BIND_JAPI_CLASS(vppBridgeDomainInterfaceDetails, "()V");
77BIND_JAPI_BYTE_FIELD(vppBridgeDomainInterfaceDetails, splitHorizonGroup);
78BIND_JAPI_STRING_FIELD(vppBridgeDomainInterfaceDetails, interfaceName);
79
80BIND_JAPI_CLASS(vppInterfaceCounters, "(JJJJJJJJJJJJJJJJJJJJJJ)V");
Pavel84e4ffe2016-02-17 15:10:04 +010081BIND_JAPI_CLASS(vppInterfaceDetails, "(ILjava/lang/String;I[BBBBBIBBIIBBBBIIIII)V");
Robert Varga81d99ac2016-01-30 18:30:36 +010082BIND_JAPI_CLASS(vppIPv4Address, "(IB)V");
83BIND_JAPI_CLASS(vppIPv6Address, "([BB)V");
84BIND_JAPI_CLASS(vppL2Fib, "([BZLjava/lang/String;ZZ)V");
85BIND_JAPI_CLASS(vppVersion, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
86BIND_JAPI_CLASS(vppVxlanTunnelDetails, "(IIIII)V");
87
Robert Vargad0f92092016-02-10 14:39:57 +010088void vl_client_add_api_signatures (vl_api_memclnt_create_t *mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -070089{
Robert Vargad0f92092016-02-10 14:39:57 +010090 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -070091 * Send the main API signature in slot 0. This bit of code must
92 * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
93 */
94 mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
95}
96
97/* Note: non-static, called once to set up the initial intfc table */
98static int sw_interface_dump (vppjni_main_t * jm)
99{
100 vl_api_sw_interface_dump_t *mp;
101 f64 timeout;
102 hash_pair_t * p;
103 name_sort_t * nses = 0, * ns;
104 sw_interface_subif_t * sub = NULL;
105
106 /* Toss the old name table */
Robert Vargad0f92092016-02-10 14:39:57 +0100107 hash_foreach_pair (p, jm->sw_if_index_by_interface_name,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108 ({
109 vec_add2 (nses, ns, 1);
110 ns->name = (u8 *)(p->key);
111 ns->value = (u32) p->value[0];
112 }));
113
114 hash_free (jm->sw_if_index_by_interface_name);
115
116 vec_foreach (ns, nses)
117 vec_free (ns->name);
118
119 vec_free (nses);
120
121 vec_foreach (sub, jm->sw_if_subif_table) {
122 vec_free (sub->interface_name);
123 }
124 vec_free (jm->sw_if_subif_table);
125
126 /* recreate the interface name hash table */
Robert Vargad0f92092016-02-10 14:39:57 +0100127 jm->sw_if_index_by_interface_name
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128 = hash_create_string (0, sizeof(uword));
129
130 /* Get list of ethernets */
131 M(SW_INTERFACE_DUMP, sw_interface_dump);
132 mp->name_filter_valid = 1;
Damjan Marionfa693552016-04-26 19:30:36 +0200133 strncpy ((char *) mp->name_filter, "Ether", sizeof(mp->name_filter)-1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134 S;
135
136 /* and local / loopback interfaces */
137 M(SW_INTERFACE_DUMP, sw_interface_dump);
138 mp->name_filter_valid = 1;
Damjan Marionfa693552016-04-26 19:30:36 +0200139 strncpy ((char *) mp->name_filter, "lo", sizeof(mp->name_filter)-1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140 S;
141
142 /* and vxlan tunnel interfaces */
143 M(SW_INTERFACE_DUMP, sw_interface_dump);
144 mp->name_filter_valid = 1;
Damjan Marionfa693552016-04-26 19:30:36 +0200145 strncpy ((char *) mp->name_filter, "vxlan", sizeof(mp->name_filter)-1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146 S;
147
148 /* and tap tunnel interfaces */
149 M(SW_INTERFACE_DUMP, sw_interface_dump);
150 mp->name_filter_valid = 1;
Damjan Marionfa693552016-04-26 19:30:36 +0200151 strncpy ((char *) mp->name_filter, "tap", sizeof(mp->name_filter)-1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152 S;
153
Damjan Marionb02e49c2016-03-31 17:44:25 +0200154 /* and host (af_packet) interfaces */
155 M(SW_INTERFACE_DUMP, sw_interface_dump);
156 mp->name_filter_valid = 1;
Damjan Marionfa693552016-04-26 19:30:36 +0200157 strncpy ((char *) mp->name_filter, "host", sizeof(mp->name_filter)-1);
Damjan Marionb02e49c2016-03-31 17:44:25 +0200158 S;
159
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160 /* and l2tpv3 tunnel interfaces */
161 M(SW_INTERFACE_DUMP, sw_interface_dump);
162 mp->name_filter_valid = 1;
Robert Vargad0f92092016-02-10 14:39:57 +0100163 strncpy ((char *) mp->name_filter, "l2tpv3_tunnel",
Damjan Marionfa693552016-04-26 19:30:36 +0200164 sizeof(mp->name_filter)-1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165 S;
166
167 /* Use a control ping for synchronization */
168 {
169 vl_api_control_ping_t * mp;
170 M(CONTROL_PING, control_ping);
171 S;
172 }
173 W;
174}
175
Dave Wallaceba474a22016-02-09 23:09:41 -0500176JNIEXPORT jobject JNICALL Java_org_openvpp_vppjapi_vppConn_getVppVersion0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 (JNIEnv *env, jobject obj)
178{
Robert Vargad0f92092016-02-10 14:39:57 +0100179 vppjni_main_t * jm = &vppjni_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180
Robert Vargad0f92092016-02-10 14:39:57 +0100181 vppjni_lock (jm, 11);
182 jstring progName = (*env)->NewStringUTF(env, (char *)jm->program_name);
183 jstring buildDir = (*env)->NewStringUTF(env, (char *)jm->build_directory);
184 jstring version = (*env)->NewStringUTF(env, (char *)jm->version);
185 jstring buildDate = (*env)->NewStringUTF(env, (char *)jm->build_date);
186 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187
Robert Vargad0f92092016-02-10 14:39:57 +0100188 return vppVersionObject(env, progName, buildDir, version, buildDate);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189}
190
191static int jm_show_version (vppjni_main_t *jm)
192{
Robert Vargad0f92092016-02-10 14:39:57 +0100193 int rv;
194 vl_api_show_version_t *mp;
195 f64 timeout;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196
Robert Vargad0f92092016-02-10 14:39:57 +0100197 vppjni_lock (jm, 10);
198 M(SHOW_VERSION, show_version);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199
Robert Vargad0f92092016-02-10 14:39:57 +0100200 S;
201 vppjni_unlock (jm);
202 WNR;
203 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204}
205
206static int jm_stats_enable_disable (vppjni_main_t *jm, u8 enable)
207{
Robert Vargad0f92092016-02-10 14:39:57 +0100208 vl_api_want_stats_t * mp;
209 f64 timeout;
210 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211
Robert Vargad0f92092016-02-10 14:39:57 +0100212 vppjni_lock (jm, 13);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213
Robert Vargad0f92092016-02-10 14:39:57 +0100214 M(WANT_STATS, want_stats);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215
Robert Vargad0f92092016-02-10 14:39:57 +0100216 mp->enable_disable = enable;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217
Robert Vargad0f92092016-02-10 14:39:57 +0100218 S;
219 vppjni_unlock (jm);
220 WNR;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221
Robert Vargad0f92092016-02-10 14:39:57 +0100222 // already subscribed / already disabled (it's ok)
223 if (rv == -2 || rv == -3)
224 rv = 0;
225 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226}
227
Dave Wallaceba474a22016-02-09 23:09:41 -0500228JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_setInterfaceDescription0
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500229 (JNIEnv *env, jobject obj, jstring ifName, jstring ifDesc)
230{
231 int rv = 0;
232 vppjni_main_t * jm = &vppjni_main;
233 uword * p;
234 u32 sw_if_index = ~0;
235 sw_if_config_t *cfg;
236
237 const char *if_name_str = (*env)->GetStringUTFChars (env, ifName, 0);
238 const char *if_desc_str = (*env)->GetStringUTFChars (env, ifDesc, 0);
239
240 vppjni_lock (jm, 23);
241
242 p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name_str);
243 if (p == 0) {
244 rv = -1;
245 goto out;
246 }
247 sw_if_index = (jint) p[0];
248
249 u8 *if_desc = 0;
250 vec_validate_init_c_string (if_desc, if_desc_str, strlen(if_desc_str));
251 (*env)->ReleaseStringUTFChars (env, ifDesc, if_desc_str);
252
253 p = hash_get (jm->sw_if_config_by_sw_if_index, sw_if_index);
254 if (p != 0) {
255 cfg = (sw_if_config_t *) (p[0]);
256 if (cfg->desc)
257 vec_free(cfg->desc);
Robert Vargad0f92092016-02-10 14:39:57 +0100258 } else {
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500259 cfg = (sw_if_config_t *) clib_mem_alloc(sizeof(sw_if_config_t));
260 hash_set (jm->sw_if_config_by_sw_if_index, sw_if_index, cfg);
261 }
262
263 cfg->desc = if_desc;
264
265out:
266 (*env)->ReleaseStringUTFChars (env, ifName, if_name_str);
267 vppjni_unlock (jm);
268 return rv;
269}
270
Dave Wallaceba474a22016-02-09 23:09:41 -0500271JNIEXPORT jstring JNICALL Java_org_openvpp_vppjapi_vppConn_getInterfaceDescription0
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500272(JNIEnv * env, jobject obj, jstring ifName)
273{
274 vppjni_main_t * jm = &vppjni_main;
275 u32 sw_if_index = ~0;
276 uword * p;
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500277 jstring ifDesc = NULL;
Robert Vargaf0f54d82016-02-10 16:01:58 +0100278 const char *if_name_str = (*env)->GetStringUTFChars (env, ifName, 0);
279 if (!if_name_str)
280 return NULL;
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500281
282 vppjni_lock (jm, 24);
283 p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name_str);
284 if (p == 0)
285 goto out;
286
287 sw_if_index = (jint) p[0];
288
289 p = hash_get (jm->sw_if_config_by_sw_if_index, sw_if_index);
290 if (p == 0)
291 goto out;
292
293 sw_if_config_t *cfg = (sw_if_config_t *) (p[0]);
294 u8 * s = format (0, "%s%c", cfg->desc, 0);
295 ifDesc = (*env)->NewStringUTF(env, (char *)s);
296
297out:
298 vppjni_unlock (jm);
299
300 return ifDesc;
301}
302
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_clientConnect
304 (JNIEnv *env, jobject obj, jstring clientName)
305{
Robert Vargad0f92092016-02-10 14:39:57 +0100306 int rv;
307 const char *client_name;
308 void vl_msg_reply_handler_hookup(void);
309 vppjni_main_t * jm = &vppjni_main;
310 api_main_t * am = &api_main;
311 u8 * heap;
312 mheap_t * h;
313 f64 timeout;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314
Robert Vargad0f92092016-02-10 14:39:57 +0100315 /*
316 * Bail out now if we're not running as root
317 */
318 if (geteuid() != 0)
319 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320
Robert Vargad0f92092016-02-10 14:39:57 +0100321 if (jm->is_connected)
322 return -2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323
Robert Vargaf0f54d82016-02-10 16:01:58 +0100324 client_name = (*env)->GetStringUTFChars(env, clientName, 0);
325 if (!client_name)
326 return -3;
327
Robert Vargad0f92092016-02-10 14:39:57 +0100328 if (jm->heap == 0)
329 clib_mem_init (0, 128<<20);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330
Robert Vargad0f92092016-02-10 14:39:57 +0100331 heap = clib_mem_get_per_cpu_heap();
332 h = mheap_header (heap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333
Robert Vargad0f92092016-02-10 14:39:57 +0100334 clib_time_init (&jm->clib_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335
Robert Vargad0f92092016-02-10 14:39:57 +0100336 rv = connect_to_vpe ((char *) client_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337
Robert Vargad0f92092016-02-10 14:39:57 +0100338 if (rv < 0)
339 clib_warning ("connection failed, rv %d", rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340
Robert Vargad0f92092016-02-10 14:39:57 +0100341 (*env)->ReleaseStringUTFChars (env, clientName, client_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342
Robert Vargad0f92092016-02-10 14:39:57 +0100343 if (rv == 0) {
344 vl_msg_reply_handler_hookup ();
345 jm->is_connected = 1;
346 /* make the main heap thread-safe */
347 h->flags |= MHEAP_FLAG_THREAD_SAFE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348
Robert Vargad0f92092016-02-10 14:39:57 +0100349 jm->reply_hash = hash_create (0, sizeof (uword));
350 //jm->callback_hash = hash_create (0, sizeof (uword));
351 //jm->ping_hash = hash_create (0, sizeof (uword));
352 jm->api_main = am;
353 vjbd_main_init(&jm->vjbd_main);
354 jm->sw_if_index_by_interface_name =
355 hash_create_string (0, sizeof (uword));
356 jm->sw_if_config_by_sw_if_index =
357 hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358
Robert Vargad0f92092016-02-10 14:39:57 +0100359 {
360 // call control ping first to attach rx thread to java thread
361 vl_api_control_ping_t * mp;
362 M(CONTROL_PING, control_ping);
363 S;
364 WNR;
365
366 if (rv != 0) {
367 clib_warning ("first control ping failed: %d", rv);
368 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369 }
Robert Vargad0f92092016-02-10 14:39:57 +0100370 rv = jm_show_version(jm);
371 if (rv != 0)
372 clib_warning ("unable to retrieve vpp version (rv: %d)", rv);
373 rv = sw_interface_dump(jm);
374 if (rv != 0)
375 clib_warning ("unable to retrieve interface list (rv: %d)", rv);
376 rv = jm_stats_enable_disable(jm, 1);
377 if (rv != 0)
378 clib_warning ("unable to subscribe to stats (rv: %d)", rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379 }
Robert Vargad0f92092016-02-10 14:39:57 +0100380 DEBUG_LOG ("clientConnect result: %d", rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381
Robert Vargad0f92092016-02-10 14:39:57 +0100382 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383}
384
385JNIEXPORT void JNICALL Java_org_openvpp_vppjapi_vppConn_clientDisconnect
386 (JNIEnv *env, jobject obj)
387{
Robert Vargad0f92092016-02-10 14:39:57 +0100388 u8 *save_heap;
389 vppjni_main_t * jm = &vppjni_main;
390 vl_client_disconnect_from_vlib();
391
392 save_heap = jm->heap;
393 memset (jm, 0, sizeof (*jm));
394 jm->heap = save_heap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395}
396
397void vl_api_generic_reply_handler (vl_api_generic_reply_t *mp)
398{
Robert Vargad0f92092016-02-10 14:39:57 +0100399 api_main_t * am = &api_main;
400 u16 msg_id = clib_net_to_host_u16 (mp->_vl_msg_id);
401 trace_cfg_t *cfgp;
402 i32 retval = clib_net_to_host_u32 (mp->retval);
403 int total_bytes = sizeof(mp);
404 vppjni_main_t * jm = &vppjni_main;
405 u8 * saved_reply = 0;
406 u32 context = clib_host_to_net_u32 (mp->context);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407
Robert Vargad0f92092016-02-10 14:39:57 +0100408 cfgp = am->api_trace_cfg + msg_id;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409
Robert Vargad0f92092016-02-10 14:39:57 +0100410 if (!cfgp)
411 clib_warning ("msg id %d: no trace configuration\n", msg_id);
412 else
413 total_bytes = cfgp->size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414
Robert Vargad0f92092016-02-10 14:39:57 +0100415 jm->context_id_received = context;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416
Robert Vargad0f92092016-02-10 14:39:57 +0100417 DEBUG_LOG ("Received generic reply for msg id %d", msg_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418
Robert Vargad0f92092016-02-10 14:39:57 +0100419 /* A generic reply, successful, we're done */
420 if (retval >= 0 && total_bytes == sizeof(*mp))
421 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422
Robert Vargad0f92092016-02-10 14:39:57 +0100423 /* Save the reply */
424 vec_validate (saved_reply, total_bytes - 1);
Damjan Marionf1213b82016-03-13 02:22:06 +0100425 clib_memcpy (saved_reply, mp, total_bytes);
Robert Vargad0f92092016-02-10 14:39:57 +0100426
427 vppjni_lock (jm, 2);
428 hash_set (jm->reply_hash, context, saved_reply);
429 jm->saved_reply_count ++;
430 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431}
432
Dave Wallaceba474a22016-02-09 23:09:41 -0500433JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_getRetval0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434(JNIEnv * env, jobject obj, jint context, jint release)
435{
Robert Vargad0f92092016-02-10 14:39:57 +0100436 vppjni_main_t * jm = &vppjni_main;
437 vl_api_generic_reply_t * mp;
438 uword * p;
439 int rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700440
Robert Vargad0f92092016-02-10 14:39:57 +0100441 /* Dunno yet? */
442 if (context > jm->context_id_received)
443 return (VNET_API_ERROR_RESPONSE_NOT_READY);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700444
Robert Vargad0f92092016-02-10 14:39:57 +0100445 vppjni_lock (jm, 1);
446 p = hash_get (jm->reply_hash, context);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447
Robert Vargad0f92092016-02-10 14:39:57 +0100448 /*
449 * Two cases: a generic "yes" reply - won't be in the hash table
450 * or "no", or "more data" which will be in the table.
451 */
452 if (p == 0)
453 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700454
Robert Vargad0f92092016-02-10 14:39:57 +0100455 mp = (vl_api_generic_reply_t *) (p[0]);
456 rv = clib_net_to_host_u32 (mp->retval);
457
458 if (release) {
459 u8 * free_me = (u8 *) mp;
460 vec_free (free_me);
461 hash_unset (jm->reply_hash, context);
462 jm->saved_reply_count --;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463 }
464
465out:
Robert Vargad0f92092016-02-10 14:39:57 +0100466 vppjni_unlock (jm);
467 return (rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468}
469
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500470static int
471name_sort_cmp (void * a1, void * a2)
472{
Robert Vargad0f92092016-02-10 14:39:57 +0100473 name_sort_t * n1 = a1;
474 name_sort_t * n2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500475
Robert Vargad0f92092016-02-10 14:39:57 +0100476 return strcmp ((char *)n1->name, (char *)n2->name);
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500477}
478
Dave Wallaceba474a22016-02-09 23:09:41 -0500479JNIEXPORT jstring JNICALL Java_org_openvpp_vppjapi_vppConn_getInterfaceList0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480 (JNIEnv * env, jobject obj, jstring name_filter)
481{
Robert Vargad0f92092016-02-10 14:39:57 +0100482 vppjni_main_t * jm = &vppjni_main;
483 jstring rv;
484 hash_pair_t * p;
485 name_sort_t * nses = 0, * ns;
486 const char *this_name;
Robert Vargad0f92092016-02-10 14:39:57 +0100487 u8 * s = 0;
Robert Vargaf0f54d82016-02-10 16:01:58 +0100488 const char * nf = (*env)->GetStringUTFChars (env, name_filter, NULL);
489 if (!nf)
490 return NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700491
Robert Vargad0f92092016-02-10 14:39:57 +0100492 vppjni_lock (jm, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493
Robert Vargad0f92092016-02-10 14:39:57 +0100494 hash_foreach_pair (p, jm->sw_if_index_by_interface_name,
495 ({
496 this_name = (const char *)(p->key);
497 if (strlen (nf) == 0 || strcasestr (this_name, nf)) {
498 vec_add2 (nses, ns, 1);
499 ns->name = (u8 *)(p->key);
500 ns->value = (u32) p->value[0];
501 }
502 }));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503
Robert Vargad0f92092016-02-10 14:39:57 +0100504 vec_sort_with_function (nses, name_sort_cmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505
Robert Vargad0f92092016-02-10 14:39:57 +0100506 vec_foreach (ns, nses)
507 s = format (s, "%s: %d, ", ns->name, ns->value);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508
Robert Vargad0f92092016-02-10 14:39:57 +0100509 _vec_len (s) = vec_len (s) - 2;
510 vec_terminate_c_string (s);
511 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700512
Robert Vargad0f92092016-02-10 14:39:57 +0100513 vec_free (nses);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514
Robert Vargad0f92092016-02-10 14:39:57 +0100515 (*env)->ReleaseStringUTFChars (env, name_filter, nf);
516
517 rv = (*env)->NewStringUTF (env, (char *) s);
518 vec_free (s);
519
520 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521}
522
Dave Wallaceba474a22016-02-09 23:09:41 -0500523JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_swIfIndexFromName0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524 (JNIEnv * env, jobject obj, jstring interfaceName)
525{
Robert Vargad0f92092016-02-10 14:39:57 +0100526 vppjni_main_t * jm = &vppjni_main;
527 jint rv = -1;
528 const char * if_name = (*env)->GetStringUTFChars (env, interfaceName, NULL);
Robert Vargaf0f54d82016-02-10 16:01:58 +0100529 if (if_name) {
530 uword * p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531
Robert Vargaf0f54d82016-02-10 16:01:58 +0100532 vppjni_lock (jm, 5);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533
Robert Vargaf0f54d82016-02-10 16:01:58 +0100534 p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535
Robert Vargaf0f54d82016-02-10 16:01:58 +0100536 if (p != 0)
537 rv = (jint) p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538
Robert Vargaf0f54d82016-02-10 16:01:58 +0100539 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540
Robert Vargaf0f54d82016-02-10 16:01:58 +0100541 (*env)->ReleaseStringUTFChars (env, interfaceName, if_name);
542 }
Robert Vargad0f92092016-02-10 14:39:57 +0100543
544 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545}
546
Dave Wallaceba474a22016-02-09 23:09:41 -0500547JNIEXPORT jobject JNICALL Java_org_openvpp_vppjapi_vppConn_getInterfaceCounters0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548(JNIEnv * env, jobject obj, jint swIfIndex)
549{
550 vppjni_main_t * jm = &vppjni_main;
551 sw_interface_stats_t *s;
552 u32 sw_if_index = swIfIndex;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553 jobject result = NULL;
554
Ed Warnickecb9cada2015-12-08 15:45:58 -0700555 vppjni_lock (jm, 16);
556
557 if (sw_if_index >= vec_len(jm->sw_if_stats_by_sw_if_index)) {
558 goto out;
559 }
560 s = &jm->sw_if_stats_by_sw_if_index[sw_if_index];
561 if (!s->valid) {
562 goto out;
563 }
564
Robert Varga81d99ac2016-01-30 18:30:36 +0100565 result = vppInterfaceCountersObject(env,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700566 s->rx.octets, s->rx.pkts.ip4, s->rx.pkts.ip6, s->rx.pkts.unicast,
567 s->rx.pkts.multicast, s->rx.pkts.broadcast, s->rx.pkts.discard,
568 s->rx.pkts.fifo_full, s->rx.pkts.error, s->rx.pkts.unknown_proto,
569 s->rx.pkts.miss,
570 s->tx.octets, s->tx.pkts.ip4, s->tx.pkts.ip6, s->tx.pkts.unicast,
571 s->tx.pkts.multicast, s->tx.pkts.broadcast, s->tx.pkts.discard,
572 s->tx.pkts.fifo_full, s->tx.pkts.error, s->tx.pkts.unknown_proto,
573 s->tx.pkts.miss);
574
575out:
576 vppjni_unlock (jm);
577 return result;
578}
579
Dave Wallaceba474a22016-02-09 23:09:41 -0500580JNIEXPORT jstring JNICALL Java_org_openvpp_vppjapi_vppConn_interfaceNameFromSwIfIndex0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581(JNIEnv * env, jobject obj, jint swIfIndex)
582{
583 vppjni_main_t * jm = &vppjni_main;
584 sw_interface_details_t *sw_if_details;
585 u32 sw_if_index;
586 jstring ifname = NULL;
587
588 vppjni_lock (jm, 8);
589
590 sw_if_index = swIfIndex;
591
592 if (sw_if_index >= vec_len(jm->sw_if_table)) {
593 goto out;
594 }
595 sw_if_details = &jm->sw_if_table[sw_if_index];
596 if (!sw_if_details->valid) {
597 goto out;
598 }
599
600 u8 * s = format (0, "%s%c", sw_if_details->interface_name, 0);
601 ifname = (*env)->NewStringUTF(env, (char *)s);
602
603out:
604 vppjni_unlock (jm);
605
606 return ifname;
607}
608
Dave Wallaceba474a22016-02-09 23:09:41 -0500609JNIEXPORT void JNICALL Java_org_openvpp_vppjapi_vppConn_clearInterfaceTable0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700610(JNIEnv * env, jobject obj)
611{
612 vppjni_main_t * jm = &vppjni_main;
613
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500614 vppjni_lock (jm, 21);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700615
616 vec_reset_length(jm->sw_if_table);
617
618 vppjni_unlock (jm);
619}
620
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500621static jobjectArray sw_if_dump_get_interfaces ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700622
Dave Wallaceba474a22016-02-09 23:09:41 -0500623JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_swInterfaceDump0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700624(JNIEnv * env, jobject obj, jbyte name_filter_valid, jbyteArray name_filter)
625{
626 vppjni_main_t *jm = &vppjni_main;
627 f64 timeout;
628 vl_api_sw_interface_dump_t * mp;
629 u32 my_context_id;
630 int rv;
631 rv = vppjni_sanity_check (jm);
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500632 if (rv) {
633 clib_warning("swInterfaceDump sanity_check rv = %d", rv);
634 return NULL;
635 }
636
Ed Warnickecb9cada2015-12-08 15:45:58 -0700637 vppjni_lock (jm, 7);
638 my_context_id = vppjni_get_context_id (jm);
Robert Varga427ce222016-02-01 18:12:18 +0100639 jsize cnt = (*env)->GetArrayLength (env, name_filter);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700640
641 M(SW_INTERFACE_DUMP, sw_interface_dump);
642 mp->context = clib_host_to_net_u32 (my_context_id);
643 mp->name_filter_valid = name_filter_valid;
644
645 if (cnt > sizeof(mp->name_filter))
646 cnt = sizeof(mp->name_filter);
647
Robert Varga427ce222016-02-01 18:12:18 +0100648 (*env)->GetByteArrayRegion(env, name_filter, 0, cnt, (jbyte *)mp->name_filter);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500650 DEBUG_LOG ("interface filter (%d, %s, len: %d)", mp->name_filter_valid, (char *)mp->name_filter, cnt);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651
Ed Warnickecb9cada2015-12-08 15:45:58 -0700652 jm->collect_indices = 1;
653
654 S;
655 {
656 // now send control ping so we know when it ends
657 vl_api_control_ping_t * mp;
658 M(CONTROL_PING, control_ping);
659 mp->context = clib_host_to_net_u32 (my_context_id);
660
Ed Warnickecb9cada2015-12-08 15:45:58 -0700661 S;
662 }
663 vppjni_unlock (jm);
664 WNR;
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500665
666 vppjni_lock (jm, 7);
667 jobjectArray result = sw_if_dump_get_interfaces(env);
668 vppjni_unlock (jm);
669 return result;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700670}
671
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500672static jobjectArray sw_if_dump_get_interfaces (JNIEnv * env)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673{
674 vppjni_main_t * jm = &vppjni_main;
675 sw_interface_details_t *sw_if_details;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676 u32 i;
677
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500678 int len = vec_len(jm->sw_if_dump_if_indices);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700679
Robert Varga81d99ac2016-01-30 18:30:36 +0100680 jobjectArray ifArray = vppInterfaceDetailsArray(env, len);
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500681
682 for (i = 0; i < len; i++) {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683 u32 sw_if_index = jm->sw_if_dump_if_indices[i];
684 ASSERT(sw_if_index < vec_len(jm->sw_if_table));
685 sw_if_details = &jm->sw_if_table[sw_if_index];
686 ASSERT(sw_if_details->valid);
687
688 u8 * s = format (0, "%s%c", sw_if_details->interface_name, 0);
689
690 jstring ifname = (*env)->NewStringUTF(env, (char *)s);
691 jint ifIndex = sw_if_details->sw_if_index;
692 jint supIfIndex = sw_if_details->sup_sw_if_index;
693 jbyteArray physAddr = (*env)->NewByteArray(env,
694 sw_if_details->l2_address_length);
695 (*env)->SetByteArrayRegion(env, physAddr, 0,
696 sw_if_details->l2_address_length,
697 (signed char*)sw_if_details->l2_address);
698 jint subId = sw_if_details->sub_id;
699 jint subOuterVlanId = sw_if_details->sub_outer_vlan_id;
700 jint subInnerVlanId = sw_if_details->sub_inner_vlan_id;
701 jint vtrOp = sw_if_details->vtr_op;
702 jint vtrPushDot1q = sw_if_details->vtr_push_dot1q;
703 jint vtrTag1 = sw_if_details->vtr_tag1;
704 jint vtrTag2 = sw_if_details->vtr_tag2;
Pavel84e4ffe2016-02-17 15:10:04 +0100705 jint linkMtu = sw_if_details->link_mtu;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700706
Ed Warnickecb9cada2015-12-08 15:45:58 -0700707 jbyte adminUpDown = sw_if_details->admin_up_down;
708 jbyte linkUpDown = sw_if_details->link_up_down;
709 jbyte linkDuplex = sw_if_details->link_duplex;
710 jbyte linkSpeed = sw_if_details->link_speed;
711 jbyte subDot1ad = sw_if_details->sub_dot1ad;
712 jbyte subNumberOfTags = sw_if_details->sub_number_of_tags;
713 jbyte subExactMatch = sw_if_details->sub_exact_match;
714 jbyte subDefault = sw_if_details->sub_default;
715 jbyte subOuterVlanIdAny = sw_if_details->sub_outer_vlan_id_any;
716 jbyte subInnerVlanIdAny = sw_if_details->sub_inner_vlan_id_any;
717
Robert Varga81d99ac2016-01-30 18:30:36 +0100718 jobject ifObj = vppInterfaceDetailsObject(env,
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500719 ifIndex, ifname,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720 supIfIndex, physAddr, adminUpDown, linkUpDown,
721 linkDuplex, linkSpeed, subId, subDot1ad,
722 subNumberOfTags, subOuterVlanId, subInnerVlanId,
723 subExactMatch, subDefault, subOuterVlanIdAny,
Pavel84e4ffe2016-02-17 15:10:04 +0100724 subInnerVlanIdAny, vtrOp, vtrPushDot1q, vtrTag1, vtrTag2, linkMtu);
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500725 (*env)->SetObjectArrayElement(env, ifArray, i, ifObj);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726 }
727
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728 jm->collect_indices = 0;
729 vec_reset_length(jm->sw_if_dump_if_indices);
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500730 return ifArray;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700731}
732
Dave Wallaceba474a22016-02-09 23:09:41 -0500733JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_findOrAddBridgeDomainId0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700734 (JNIEnv * env, jobject obj, jstring bridgeDomain)
735{
Robert Vargad0f92092016-02-10 14:39:57 +0100736 vppjni_main_t * jm = &vppjni_main;
Robert Vargad0f92092016-02-10 14:39:57 +0100737 jint rv = -1;
738 const char * bdName = (*env)->GetStringUTFChars (env, bridgeDomain, NULL);
Robert Vargaf0f54d82016-02-10 16:01:58 +0100739 if (bdName) {
740 static u8 * bd_name = 0;
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500741
Robert Vargaf0f54d82016-02-10 16:01:58 +0100742 vec_validate_init_c_string (bd_name, bdName, strlen(bdName));
743 (*env)->ReleaseStringUTFChars (env, bridgeDomain, bdName);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700744
Robert Vargaf0f54d82016-02-10 16:01:58 +0100745 vppjni_lock (jm, 6);
746 rv = (jint)vjbd_find_or_add_bd (&jm->vjbd_main, bd_name);
747 vppjni_unlock (jm);
Robert Vargad0f92092016-02-10 14:39:57 +0100748
Robert Vargaf0f54d82016-02-10 16:01:58 +0100749 _vec_len(bd_name) = 0;
750 }
Robert Vargad0f92092016-02-10 14:39:57 +0100751 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700752}
753
Dave Wallaceba474a22016-02-09 23:09:41 -0500754JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_bridgeDomainIdFromName0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755 (JNIEnv * env, jobject obj, jstring bridgeDomain)
756{
Robert Vargad0f92092016-02-10 14:39:57 +0100757 vppjni_main_t * jm = &vppjni_main;
Robert Vargad0f92092016-02-10 14:39:57 +0100758 jint rv = -1;
759 const char * bdName = (*env)->GetStringUTFChars (env, bridgeDomain, NULL);
Robert Vargaf0f54d82016-02-10 16:01:58 +0100760 if (bdName) {
761 static u8 * bd_name = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700762
Robert Vargaf0f54d82016-02-10 16:01:58 +0100763 vec_validate_init_c_string (bd_name, bdName, strlen(bdName));
764 (*env)->ReleaseStringUTFChars (env, bridgeDomain, bdName);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700765
Robert Vargaf0f54d82016-02-10 16:01:58 +0100766 vppjni_lock (jm, 20);
767 rv = (jint)vjbd_id_from_name(&jm->vjbd_main, (u8 *)bd_name);
768 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769
Robert Vargaf0f54d82016-02-10 16:01:58 +0100770 _vec_len(bd_name) = 0;
771 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772
Robert Vargad0f92092016-02-10 14:39:57 +0100773 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700774}
775
Dave Wallaceba474a22016-02-09 23:09:41 -0500776JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_bridgeDomainIdFromInterfaceName0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777 (JNIEnv * env, jobject obj, jstring interfaceName)
778{
Robert Vargad0f92092016-02-10 14:39:57 +0100779 vppjni_main_t * jm = &vppjni_main;
780 vjbd_main_t * bdm = &jm->vjbd_main;
781 u32 sw_if_index;
782 jint rv = -1;
783 const char * if_name;
784 uword * p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700785
Robert Vargad0f92092016-02-10 14:39:57 +0100786 if_name = (*env)->GetStringUTFChars (env, interfaceName, NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787
Robert Vargad0f92092016-02-10 14:39:57 +0100788 vppjni_lock (jm, 14);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700789
Robert Vargad0f92092016-02-10 14:39:57 +0100790 p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791
Robert Vargad0f92092016-02-10 14:39:57 +0100792 if (p != 0) {
793 sw_if_index = (jint) p[0];
794 p = hash_get (bdm->bd_id_by_sw_if_index, sw_if_index);
795 if (p != 0) {
796 rv = (jint) p[0];
797 }
798 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799
Robert Vargad0f92092016-02-10 14:39:57 +0100800 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700801
Robert Vargad0f92092016-02-10 14:39:57 +0100802 (*env)->ReleaseStringUTFChars (env, interfaceName, if_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700803
Robert Vargad0f92092016-02-10 14:39:57 +0100804 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700805}
806
Robert Vargad0f92092016-02-10 14:39:57 +0100807/*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700808 * Special-case: build the interface table, maintain
809 * the next loopback sw_if_index vbl.
810 */
811static void vl_api_sw_interface_details_t_handler
812(vl_api_sw_interface_details_t * mp)
813{
Robert Vargad0f92092016-02-10 14:39:57 +0100814 vppjni_main_t * jm = &vppjni_main;
815 static sw_interface_details_t empty_sw_if_details = {0,};
816 sw_interface_details_t *sw_if_details;
817 u32 sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700818
Robert Vargad0f92092016-02-10 14:39:57 +0100819 vppjni_lock (jm, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700820
Robert Vargad0f92092016-02-10 14:39:57 +0100821 sw_if_index = ntohl (mp->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700822
Robert Vargad0f92092016-02-10 14:39:57 +0100823 u8 * s = format (0, "%s%c", mp->interface_name, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700824
Robert Vargad0f92092016-02-10 14:39:57 +0100825 if (jm->collect_indices) {
826 u32 pos = vec_len(jm->sw_if_dump_if_indices);
827 vec_validate(jm->sw_if_dump_if_indices, pos);
828 jm->sw_if_dump_if_indices[pos] = sw_if_index;
829 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700830
Robert Vargad0f92092016-02-10 14:39:57 +0100831 vec_validate_init_empty(jm->sw_if_table, sw_if_index, empty_sw_if_details);
832 sw_if_details = &jm->sw_if_table[sw_if_index];
833 sw_if_details->valid = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700834
Robert Vargad0f92092016-02-10 14:39:57 +0100835 snprintf((char *)sw_if_details->interface_name,
836 sizeof(sw_if_details->interface_name), "%s", (char *)s);
837 sw_if_details->sw_if_index = sw_if_index;
838 sw_if_details->sup_sw_if_index = ntohl(mp->sup_sw_if_index);
839 sw_if_details->l2_address_length = ntohl (mp->l2_address_length);
840 ASSERT(sw_if_details->l2_address_length <= sizeof(sw_if_details->l2_address));
Damjan Marionf1213b82016-03-13 02:22:06 +0100841 clib_memcpy(sw_if_details->l2_address, mp->l2_address,
Robert Vargad0f92092016-02-10 14:39:57 +0100842 sw_if_details->l2_address_length);
843 sw_if_details->sub_id = ntohl (mp->sub_id);
844 sw_if_details->sub_outer_vlan_id = ntohl (mp->sub_outer_vlan_id);
845 sw_if_details->sub_inner_vlan_id = ntohl (mp->sub_inner_vlan_id);
846 sw_if_details->vtr_op = ntohl (mp->vtr_op);
847 sw_if_details->vtr_push_dot1q = ntohl (mp->vtr_push_dot1q);
848 sw_if_details->vtr_tag1 = ntohl (mp->vtr_tag1);
849 sw_if_details->vtr_tag2 = ntohl (mp->vtr_tag2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700850
Robert Vargad0f92092016-02-10 14:39:57 +0100851 sw_if_details->admin_up_down = mp->admin_up_down;
852 sw_if_details->link_up_down = mp->link_up_down;
853 sw_if_details->link_duplex = mp->link_duplex;
854 sw_if_details->link_speed = mp->link_speed;
855 sw_if_details->sub_dot1ad = mp->sub_dot1ad;
856 sw_if_details->sub_number_of_tags = mp->sub_number_of_tags;
857 sw_if_details->sub_exact_match = mp->sub_exact_match;
858 sw_if_details->sub_default = mp->sub_default;
859 sw_if_details->sub_outer_vlan_id_any = mp->sub_outer_vlan_id_any;
860 sw_if_details->sub_inner_vlan_id_any = mp->sub_inner_vlan_id_any;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700861
Robert Vargad0f92092016-02-10 14:39:57 +0100862 hash_set_mem (jm->sw_if_index_by_interface_name, s, sw_if_index);
863 DEBUG_LOG ("Got interface %s", (char *)s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700864
Robert Vargad0f92092016-02-10 14:39:57 +0100865 /* In sub interface case, fill the sub interface table entry */
866 if (mp->sw_if_index != mp->sup_sw_if_index) {
867 sw_interface_subif_t * sub = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700868
Robert Vargad0f92092016-02-10 14:39:57 +0100869 vec_add2(jm->sw_if_subif_table, sub, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700870
Robert Vargad0f92092016-02-10 14:39:57 +0100871 vec_validate(sub->interface_name, strlen((char *)s) + 1);
872 strncpy((char *)sub->interface_name, (char *)s,
873 vec_len(sub->interface_name));
874 sub->sw_if_index = ntohl(mp->sw_if_index);
875 sub->sub_id = ntohl(mp->sub_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700876
Robert Vargad0f92092016-02-10 14:39:57 +0100877 sub->sub_dot1ad = mp->sub_dot1ad;
878 sub->sub_number_of_tags = mp->sub_number_of_tags;
879 sub->sub_outer_vlan_id = ntohs(mp->sub_outer_vlan_id);
880 sub->sub_inner_vlan_id = ntohs(mp->sub_inner_vlan_id);
881 sub->sub_exact_match = mp->sub_exact_match;
882 sub->sub_default = mp->sub_default;
883 sub->sub_outer_vlan_id_any = mp->sub_outer_vlan_id_any;
884 sub->sub_inner_vlan_id_any = mp->sub_inner_vlan_id_any;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700885
Robert Vargad0f92092016-02-10 14:39:57 +0100886 /* vlan tag rewrite */
887 sub->vtr_op = ntohl(mp->vtr_op);
888 sub->vtr_push_dot1q = ntohl(mp->vtr_push_dot1q);
889 sub->vtr_tag1 = ntohl(mp->vtr_tag1);
890 sub->vtr_tag2 = ntohl(mp->vtr_tag2);
891 }
892 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700893}
894
895static void vl_api_sw_interface_set_flags_t_handler
896(vl_api_sw_interface_set_flags_t * mp)
897{
898 /* $$$ nothing for the moment */
899}
900
901static jintArray create_array_of_bd_ids(JNIEnv * env, jint bd_id)
902{
903 vppjni_main_t *jm = &vppjni_main;
904 vjbd_main_t * bdm = &jm->vjbd_main;
905 u32 *buf = NULL;
906 u32 i;
907
908 if (bd_id != ~0) {
909 vec_add1(buf, bd_id);
910 } else {
911 for (i = 0; i < vec_len(bdm->bd_oper); i++) {
912 u32 bd_id = bdm->bd_oper[i].bd_id;
913 vec_add1(buf, bd_id);
914 }
915 }
916
917 jintArray bdidArray = (*env)->NewIntArray(env, vec_len(buf));
Robert Vargaec3034c2016-02-10 16:00:16 +0100918 if (!bdidArray) {
919 goto out;
920 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700921
922 (*env)->SetIntArrayRegion(env, bdidArray, 0, vec_len(buf), (int*)buf);
923
Robert Vargaec3034c2016-02-10 16:00:16 +0100924out:
Robert Varga7a224a02016-02-01 18:33:38 +0100925 vec_free(buf);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700926 return bdidArray;
927}
928
929static void bridge_domain_oper_free(void)
930{
931 vppjni_main_t *jm = &vppjni_main;
932 vjbd_main_t *bdm = &jm->vjbd_main;
933 u32 i;
934
935 for (i = 0; i < vec_len(bdm->bd_oper); i++) {
936 vec_free(bdm->bd_oper->l2fib_oper);
937 }
938 vec_reset_length(bdm->bd_oper);
939 hash_free(bdm->bd_id_by_sw_if_index);
940 hash_free(bdm->oper_bd_index_by_bd_id);
941}
942
Dave Wallaceba474a22016-02-09 23:09:41 -0500943JNIEXPORT jintArray JNICALL Java_org_openvpp_vppjapi_vppConn_bridgeDomainDump0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700944(JNIEnv * env, jobject obj, jint bd_id)
945{
946 vppjni_main_t *jm = &vppjni_main;
947 vl_api_bridge_domain_dump_t * mp;
948 u32 my_context_id;
949 f64 timeout;
950 int rv;
951 rv = vppjni_sanity_check (jm);
952 if (rv) return NULL;
953
954 vppjni_lock (jm, 15);
955
956 if (~0 == bd_id) {
957 bridge_domain_oper_free();
958 }
959
960 my_context_id = vppjni_get_context_id (jm);
961 M(BRIDGE_DOMAIN_DUMP, bridge_domain_dump);
962 mp->context = clib_host_to_net_u32 (my_context_id);
963 mp->bd_id = clib_host_to_net_u32(bd_id);
964 S;
965
966 /* Use a control ping for synchronization */
967 {
968 vl_api_control_ping_t * mp;
969 M(CONTROL_PING, control_ping);
970 S;
971 }
972
973 WNR;
974 if (0 != rv) {
975 return NULL;
976 }
977
978 jintArray ret = create_array_of_bd_ids(env, bd_id);
979
980 vppjni_unlock (jm);
981
982 return ret;
983}
984
985static void
986vl_api_bridge_domain_details_t_handler (vl_api_bridge_domain_details_t * mp)
987{
Robert Vargad0f92092016-02-10 14:39:57 +0100988 vppjni_main_t *jm = &vppjni_main;
989 vjbd_main_t * bdm = &jm->vjbd_main;
990 vjbd_oper_t * bd_oper;
991 u32 bd_id, bd_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700992
Robert Vargad0f92092016-02-10 14:39:57 +0100993 bd_id = ntohl (mp->bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700994
Robert Vargad0f92092016-02-10 14:39:57 +0100995 bd_index = vec_len(bdm->bd_oper);
996 vec_validate (bdm->bd_oper, bd_index);
997 bd_oper = vec_elt_at_index(bdm->bd_oper, bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700998
Robert Vargad0f92092016-02-10 14:39:57 +0100999 hash_set(bdm->oper_bd_index_by_bd_id, bd_id, bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001000
Robert Vargad0f92092016-02-10 14:39:57 +01001001 bd_oper->bd_id = bd_id;
1002 bd_oper->flood = mp->flood != 0;
1003 bd_oper->forward = mp->forward != 0;
1004 bd_oper->learn = mp->learn != 0;
1005 bd_oper->uu_flood = mp->uu_flood != 0;
1006 bd_oper->arp_term = mp->arp_term != 0;
1007 bd_oper->bvi_sw_if_index = ntohl (mp->bvi_sw_if_index);
1008 bd_oper->n_sw_ifs = ntohl (mp->n_sw_ifs);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001009
Robert Vargad0f92092016-02-10 14:39:57 +01001010 bd_oper->bd_sw_if_oper = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001011}
1012
1013static void
1014vl_api_bridge_domain_sw_if_details_t_handler
1015(vl_api_bridge_domain_sw_if_details_t * mp)
1016{
Robert Vargad0f92092016-02-10 14:39:57 +01001017 vppjni_main_t *jm = &vppjni_main;
1018 vjbd_main_t * bdm = &jm->vjbd_main;
1019 bd_sw_if_oper_t * bd_sw_if_oper;
1020 u32 bd_id, sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001021
Robert Vargad0f92092016-02-10 14:39:57 +01001022 bd_id = ntohl (mp->bd_id);
1023 sw_if_index = ntohl (mp->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001024
Robert Vargad0f92092016-02-10 14:39:57 +01001025 uword *p;
1026 p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
1027 if (p == 0) {
1028 clib_warning("Invalid bd_id %d in bridge_domain_sw_if_details_t_handler", bd_id);
1029 return;
1030 }
1031 u32 oper_bd_index = (jint) p[0];
1032 vjbd_oper_t *bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001033
Robert Vargad0f92092016-02-10 14:39:57 +01001034 u32 len = vec_len(bd_oper->bd_sw_if_oper);
1035 vec_validate(bd_oper->bd_sw_if_oper, len);
1036 bd_sw_if_oper = &bd_oper->bd_sw_if_oper[len];
1037 bd_sw_if_oper->bd_id = bd_id;
1038 bd_sw_if_oper->sw_if_index = sw_if_index;
1039 bd_sw_if_oper->shg = mp->shg;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001040
Robert Vargad0f92092016-02-10 14:39:57 +01001041 hash_set(bdm->bd_id_by_sw_if_index, sw_if_index, bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001042}
1043
1044static const char* interface_name_from_sw_if_index(u32 sw_if_index)
1045{
1046 vppjni_main_t *jm = &vppjni_main;
1047
1048 if (sw_if_index >= vec_len(jm->sw_if_table)) {
1049 return NULL;
1050 }
1051 if (!jm->sw_if_table[sw_if_index].valid) {
1052 return NULL;
1053 }
1054 return (const char*)jm->sw_if_table[sw_if_index].interface_name;
1055}
1056
Dave Wallaceba474a22016-02-09 23:09:41 -05001057JNIEXPORT jobject JNICALL Java_org_openvpp_vppjapi_vppConn_getBridgeDomainDetails0
Ed Warnickecb9cada2015-12-08 15:45:58 -07001058(JNIEnv * env, jobject obj, jint bdId)
1059{
1060 vppjni_main_t *jm = &vppjni_main;
1061 vjbd_main_t * bdm = &jm->vjbd_main;
1062 u32 oper_bd_index;
1063 u32 bd_id = bdId;
1064 jobject rv = NULL;
1065 uword *p;
1066
1067 vppjni_lock (jm, 16);
1068
1069 p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
1070 if (p == 0) {
1071 rv = NULL;
1072 goto out;
1073 }
1074 oper_bd_index = (jint) p[0];
1075
1076 vjbd_oper_t *bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
1077
1078
1079 /* setting BridgeDomainDetails */
1080
Robert Varga81d99ac2016-01-30 18:30:36 +01001081 jobject bddObj = vppBridgeDomainDetailsObject(env);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001082
1083 u8 *vec_bd_name = vjbd_oper_name_from_id(bdm, bd_id);
1084 if (NULL == vec_bd_name) {
1085 rv = NULL;
1086 goto out;
1087 }
1088 char *str_bd_name = (char*)format (0, "%s%c", vec_bd_name, 0);
1089 vec_free(vec_bd_name);
1090 jstring bdName = (*env)->NewStringUTF(env, str_bd_name);
1091 vec_free(str_bd_name);
1092 if (NULL == bdName) {
1093 rv = NULL;
1094 goto out;
1095 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001096
Robert Varga81d99ac2016-01-30 18:30:36 +01001097 set_vppBridgeDomainDetails_name(env, bddObj, bdName);
1098 set_vppBridgeDomainDetails_bdId(env, bddObj, bdId);
1099 set_vppBridgeDomainDetails_flood(env, bddObj, (jboolean)bd_oper->flood);
1100 set_vppBridgeDomainDetails_uuFlood(env, bddObj, (jboolean)bd_oper->uu_flood);
1101 set_vppBridgeDomainDetails_forward(env, bddObj, (jboolean)bd_oper->forward);
1102 set_vppBridgeDomainDetails_learn(env, bddObj, (jboolean)bd_oper->learn);
1103 set_vppBridgeDomainDetails_arpTerm(env, bddObj, (jboolean)bd_oper->arp_term);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001104
1105 jstring bviInterfaceName = NULL;
1106 if (~0 != bd_oper->bvi_sw_if_index) {
1107 const char *str_if_name = interface_name_from_sw_if_index(bd_oper->bvi_sw_if_index);
1108 if (NULL == str_if_name) {
1109 clib_warning("Could not get interface name for sw_if_index %d", bd_oper->bvi_sw_if_index);
1110 rv = NULL;
1111 goto out;
1112 }
1113 bviInterfaceName = (*env)->NewStringUTF(env, str_if_name);
1114 if (NULL == bviInterfaceName) {
1115 rv = NULL;
1116 goto out;
1117 }
1118 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001119
Robert Varga81d99ac2016-01-30 18:30:36 +01001120 set_vppBridgeDomainDetails_bviInterfaceName(env, bddObj, bviInterfaceName);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001121
1122 /* setting BridgeDomainInterfaceDetails */
1123
Ed Warnickecb9cada2015-12-08 15:45:58 -07001124 u32 len = vec_len(bd_oper->bd_sw_if_oper);
1125 ASSERT(len == bd_oper->n_sw_ifs);
1126
Robert Varga81d99ac2016-01-30 18:30:36 +01001127 jobjectArray bdidArray = vppBridgeDomainInterfaceDetailsArray(env, len);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001128
1129 u32 i;
1130 for (i = 0; i < len; i++) {
1131 bd_sw_if_oper_t *sw_if_oper = &bd_oper->bd_sw_if_oper[i];
1132
Robert Varga81d99ac2016-01-30 18:30:36 +01001133 jobject bdidObj = vppBridgeDomainInterfaceDetailsObject(env);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001134 (*env)->SetObjectArrayElement(env, bdidArray, i, bdidObj);
1135
1136 u32 sw_if_index = sw_if_oper->sw_if_index;
1137 const char *str_if_name = interface_name_from_sw_if_index(sw_if_index);
1138 if (NULL == str_if_name) {
1139 rv = NULL;
1140 goto out;
1141 }
1142 jstring interfaceName = (*env)->NewStringUTF(env, str_if_name);
1143 if (NULL == interfaceName) {
1144 rv = NULL;
1145 goto out;
1146 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001147
Robert Varga81d99ac2016-01-30 18:30:36 +01001148 set_vppBridgeDomainInterfaceDetails_interfaceName(env, bdidObj, interfaceName);
1149 set_vppBridgeDomainInterfaceDetails_splitHorizonGroup(env, bdidObj, (jbyte)sw_if_oper->shg);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001150 }
1151
Robert Varga81d99ac2016-01-30 18:30:36 +01001152 set_vppBridgeDomainDetails_interfaces(env, bddObj, bdidArray);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001153
1154 rv = bddObj;
1155
1156out:
1157
1158 vppjni_unlock (jm);
1159
1160 return rv;
1161}
1162
1163static jobject l2_fib_create_object(JNIEnv *env, bd_l2fib_oper_t *l2_fib)
1164{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001165 u32 sw_if_index = l2_fib->sw_if_index;
1166 const char *str_if_name = interface_name_from_sw_if_index(sw_if_index);
1167 if (NULL == str_if_name) {
1168 return NULL;
1169 }
1170 jstring interfaceName = (*env)->NewStringUTF(env, str_if_name);
1171 if (NULL == interfaceName) {
1172 return NULL;
1173 }
1174
1175 jbyteArray physAddr = (*env)->NewByteArray(env, 6);
1176 (*env)->SetByteArrayRegion(env, physAddr, 0, 6,
1177 (signed char*)l2_fib->mac_addr.fields.mac);
1178 jboolean staticConfig = !l2_fib->learned;
1179 jstring outgoingInterface = interfaceName;
1180 jboolean filter = l2_fib->filter;
1181 jboolean bridgedVirtualInterface = l2_fib->bvi;
1182
Robert Varga81d99ac2016-01-30 18:30:36 +01001183 return vppL2FibObject(env, physAddr, staticConfig, outgoingInterface, filter, bridgedVirtualInterface);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001184}
1185
Dave Wallaceba474a22016-02-09 23:09:41 -05001186JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_l2FibTableDump0
Ed Warnickecb9cada2015-12-08 15:45:58 -07001187(JNIEnv * env, jobject obj, jint bd_id)
1188{
Robert Vargad0f92092016-02-10 14:39:57 +01001189 vppjni_main_t *jm = &vppjni_main;
1190 vjbd_main_t * bdm = &jm->vjbd_main;
1191 vl_api_l2_fib_table_dump_t *mp;
1192 jobjectArray l2FibArray = NULL;
1193 vjbd_oper_t *bd_oper;
1194 u32 oper_bd_index;
1195 uword *p;
1196 f64 timeout;
1197 int rv;
1198 u32 i;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001199
Robert Vargad0f92092016-02-10 14:39:57 +01001200 vppjni_lock (jm, 17);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001201
Robert Vargad0f92092016-02-10 14:39:57 +01001202 //vjbd_l2fib_oper_reset (bdm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001203
Robert Vargad0f92092016-02-10 14:39:57 +01001204 p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
1205 if (p == 0) {
1206 goto done;
1207 }
1208 oper_bd_index = p[0];
1209 bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
1210 vec_reset_length (bd_oper->l2fib_oper);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001211
Robert Vargad0f92092016-02-10 14:39:57 +01001212 /* Get list of l2 fib table entries */
1213 M(L2_FIB_TABLE_DUMP, l2_fib_table_dump);
1214 mp->bd_id = ntohl(bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001215 S;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001216
Robert Vargad0f92092016-02-10 14:39:57 +01001217 /* Use a control ping for synchronization */
1218 {
1219 vl_api_control_ping_t * mp;
1220 M(CONTROL_PING, control_ping);
1221 S;
1222 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001223
Robert Vargad0f92092016-02-10 14:39:57 +01001224 WNR;
1225 if (0 != rv) {
1226 goto done;
1227 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001228
Robert Vargad0f92092016-02-10 14:39:57 +01001229 u32 count = vec_len(bd_oper->l2fib_oper);
1230 bd_l2fib_oper_t *l2fib_oper = bd_oper->l2fib_oper;
1231
1232 l2FibArray = vppL2FibArray(env, count);
1233 for (i = 0; i < count; i++) {
1234 bd_l2fib_oper_t *l2_fib = &l2fib_oper[i];
1235 jobject l2FibObj = l2_fib_create_object(env, l2_fib);
1236 (*env)->SetObjectArrayElement(env, l2FibArray, i, l2FibObj);
1237 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001238
1239done:
Robert Vargad0f92092016-02-10 14:39:57 +01001240 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001241
Robert Vargad0f92092016-02-10 14:39:57 +01001242 return l2FibArray;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001243}
1244
1245static void
1246vl_api_l2_fib_table_entry_t_handler (vl_api_l2_fib_table_entry_t * mp)
1247{
Robert Vargad0f92092016-02-10 14:39:57 +01001248 //static u8 * mac_addr;
1249 vppjni_main_t *jm = &vppjni_main;
1250 vjbd_main_t * bdm = &jm->vjbd_main;
1251 vjbd_oper_t * bd_oper;
1252 u32 bd_id, oper_bd_index;
1253 //uword mhash_val_l2fi;
1254 bd_l2fib_oper_t * l2fib_oper;
1255 l2fib_u64_mac_t * l2fe_u64_mac = (l2fib_u64_mac_t *)&mp->mac;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001256
Robert Vargad0f92092016-02-10 14:39:57 +01001257 bd_id = ntohl (mp->bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001258
Robert Vargad0f92092016-02-10 14:39:57 +01001259 uword *p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
1260 if (p == 0) {
1261 return;
1262 }
1263 oper_bd_index = (jint) p[0];
1264 bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001265
1266#if 0
Robert Vargad0f92092016-02-10 14:39:57 +01001267 vec_validate (mac_addr, MAC_ADDRESS_SIZE);
Damjan Marionf1213b82016-03-13 02:22:06 +01001268 clib_memcpy (mac_addr, l2fe_u64_mac->fields.mac, MAC_ADDRESS_SIZE);
Robert Vargad0f92092016-02-10 14:39:57 +01001269 mhash_val_l2fi = vec_len (bd_oper->l2fib_oper);
1270 if (mhash_elts (&bd_oper->l2fib_index_by_mac) == 0)
1271 mhash_init (&bd_oper->l2fib_index_by_mac, sizeof (u32), MAC_ADDRESS_SIZE);
1272 mhash_set_mem (&bd_oper->l2fib_index_by_mac, mac_addr, &mhash_val_l2fi, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001273#endif
1274
Robert Vargad0f92092016-02-10 14:39:57 +01001275 vec_add2 (bd_oper->l2fib_oper, l2fib_oper, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001276
Robert Vargad0f92092016-02-10 14:39:57 +01001277 l2fib_oper->bd_id = bd_id;
1278 l2fib_oper->mac_addr.raw = l2fib_mac_to_u64 (l2fe_u64_mac->fields.mac);
1279 l2fib_oper->sw_if_index = ntohl (mp->sw_if_index);
1280 l2fib_oper->learned = !mp->static_mac;
1281 l2fib_oper->filter = mp->filter_mac;
1282 l2fib_oper->bvi = mp->bvi_mac;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001283}
1284
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001285static int ipAddressDump
1286(JNIEnv * env, jobject obj, jstring interfaceName, jboolean isIPv6)
1287{
1288 vppjni_main_t *jm = &vppjni_main;
1289 vl_api_ip_address_dump_t * mp;
1290 const char *if_name;
1291 u32 my_context_id;
1292 u32 sw_if_index;
1293 f64 timeout;
1294 uword *p;
1295 int rv = 0;
1296
1297 if (NULL == interfaceName) {
1298 return -1;
1299 }
1300
1301 if_name = (*env)->GetStringUTFChars (env, interfaceName, NULL);
Robert Vargaf0f54d82016-02-10 16:01:58 +01001302 if (!if_name) {
1303 return -1;
1304 }
1305
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001306 p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name);
1307 (*env)->ReleaseStringUTFChars (env, interfaceName, if_name);
1308 if (p == 0) {
1309 return -1;
1310 }
1311 sw_if_index = (u32) p[0];
1312
1313 rv = vppjni_sanity_check (jm);
1314 if (0 != rv) {
1315 return rv;
1316 }
1317
1318 my_context_id = vppjni_get_context_id (jm);
1319 M(IP_ADDRESS_DUMP, ip_address_dump);
1320 mp->context = clib_host_to_net_u32 (my_context_id);
1321 mp->sw_if_index = clib_host_to_net_u32(sw_if_index);
1322 mp->is_ipv6 = isIPv6;
1323 jm->is_ipv6 = isIPv6;
1324 S;
1325
1326 /* Use a control ping for synchronization */
1327 {
1328 vl_api_control_ping_t * mp;
1329 M(CONTROL_PING, control_ping);
1330 S;
1331 }
1332
1333 WNR;
1334
1335 return rv;
1336}
1337
Dave Wallaceba474a22016-02-09 23:09:41 -05001338JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_ipv4AddressDump0
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001339(JNIEnv * env, jobject obj, jstring interfaceName)
1340{
1341 vppjni_main_t *jm = &vppjni_main;
1342 jobject returnArray = NULL;
1343 int i;
1344
1345 vppjni_lock (jm, 18);
1346
1347 vec_reset_length(jm->ipv4_addresses);
1348
1349 if (0 != ipAddressDump(env, obj, interfaceName, 0)) {
1350 goto done;
1351 }
1352
1353 u32 count = vec_len(jm->ipv4_addresses);
1354 ipv4_address_t *ipv4_address = jm->ipv4_addresses;
1355
Robert Varga81d99ac2016-01-30 18:30:36 +01001356 jobjectArray ipv4AddressArray = vppIPv4AddressArray(env, count);
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001357
1358 for (i = 0; i < count; i++) {
1359 ipv4_address_t *address = &ipv4_address[i];
1360
1361 jint ip = address->ip;
1362 jbyte prefixLength = address->prefix_length;
1363
Robert Varga81d99ac2016-01-30 18:30:36 +01001364 jobject ipv4AddressObj = vppIPv4AddressObject(env, ip, prefixLength);
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001365
1366 (*env)->SetObjectArrayElement(env, ipv4AddressArray, i, ipv4AddressObj);
1367 }
1368
1369 returnArray = ipv4AddressArray;
1370
1371done:
1372 vppjni_unlock (jm);
1373 return returnArray;
1374}
1375
Dave Wallaceba474a22016-02-09 23:09:41 -05001376JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_ipv6AddressDump0
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001377(JNIEnv * env, jobject obj, jstring interfaceName)
1378{
1379 vppjni_main_t *jm = &vppjni_main;
1380 jobject returnArray = NULL;
1381 int i;
1382
1383 vppjni_lock (jm, 19);
1384
1385 vec_reset_length(jm->ipv6_addresses);
1386
1387 if (0 != ipAddressDump(env, obj, interfaceName, 1)) {
1388 goto done;
1389 }
1390
1391 u32 count = vec_len(jm->ipv6_addresses);
1392 ipv6_address_t *ipv6_address = jm->ipv6_addresses;
1393
Robert Varga81d99ac2016-01-30 18:30:36 +01001394 jobjectArray ipv6AddressArray = vppIPv6AddressArray(env, count);
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001395
1396 for (i = 0; i < count; i++) {
1397 ipv6_address_t *address = &ipv6_address[i];
1398
1399 jbyteArray ip = (*env)->NewByteArray(env, 16);
1400 (*env)->SetByteArrayRegion(env, ip, 0, 16,
1401 (signed char*)address->ip);
1402
1403 jbyte prefixLength = address->prefix_length;
1404
Robert Varga81d99ac2016-01-30 18:30:36 +01001405 jobject ipv6AddressObj = vppIPv6AddressObject(env, ip, prefixLength);
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001406
1407 (*env)->SetObjectArrayElement(env, ipv6AddressArray, i, ipv6AddressObj);
1408 }
1409
1410 returnArray = ipv6AddressArray;
1411
1412done:
1413 vppjni_unlock (jm);
1414 return returnArray;
1415}
1416
1417static void vl_api_ip_address_details_t_handler (vl_api_ip_address_details_t * mp)
1418{
1419 vppjni_main_t * jm = &vppjni_main;
1420
1421 if (!jm->is_ipv6) {
1422 ipv4_address_t *address = 0;
1423 vec_add2(jm->ipv4_addresses, address, 1);
Damjan Marionf1213b82016-03-13 02:22:06 +01001424 clib_memcpy(&address->ip, mp->ip, 4);
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001425 address->prefix_length = mp->prefix_length;
1426 } else {
1427 ipv6_address_t *address = 0;
1428 vec_add2(jm->ipv6_addresses, address, 1);
Damjan Marionf1213b82016-03-13 02:22:06 +01001429 clib_memcpy(address->ip, mp->ip, 16);
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001430 address->prefix_length = mp->prefix_length;
1431 }
1432}
1433
1434#define VXLAN_TUNNEL_INTERFACE_NAME_PREFIX "vxlan_tunnel"
1435
Dave Wallaceba474a22016-02-09 23:09:41 -05001436JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_vxlanTunnelDump0
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001437(JNIEnv * env, jobject obj, jint swIfIndex)
1438{
1439 vppjni_main_t *jm = &vppjni_main;
1440 vl_api_vxlan_tunnel_dump_t * mp;
1441 jobjectArray returnArray = NULL;
1442 u32 my_context_id;
1443 f64 timeout;
1444 int rv = 0;
1445 int i;
1446
1447 vppjni_lock (jm, 22);
1448
1449 vec_reset_length(jm->vxlan_tunnel_details);
1450
1451 my_context_id = vppjni_get_context_id (jm);
1452 M(VXLAN_TUNNEL_DUMP, vxlan_tunnel_dump);
1453 mp->context = clib_host_to_net_u32 (my_context_id);
1454 mp->sw_if_index = clib_host_to_net_u32 (swIfIndex);
1455 S;
1456
1457 /* Use a control ping for synchronization */
1458 {
1459 vl_api_control_ping_t * mp;
1460 M(CONTROL_PING, control_ping);
1461 S;
1462 }
1463
1464 WNR;
1465 if (0 != rv) {
1466 goto done;
1467 }
1468
1469 u32 count = vec_len(jm->vxlan_tunnel_details);
1470
Robert Varga81d99ac2016-01-30 18:30:36 +01001471 jobjectArray vxlanTunnelDetailsArray = vppVxlanTunnelDetailsArray(env, count);
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001472
1473 for (i = 0; i < count; i++) {
1474 vxlan_tunnel_details_t *details = &jm->vxlan_tunnel_details[i];
1475
Chris Luke99cb3352016-04-26 10:49:53 -04001476
1477 /* This interface to support both v4 and v6 addresses is nasty */
1478 jbyteArray src_address = (*env)->NewByteArray(env, 16);
1479 (*env)->SetByteArrayRegion(env, src_address, 0, 16,
1480 (signed char*)details->src_address);
1481
1482 jbyteArray dst_address = (*env)->NewByteArray(env, 16);
1483 (*env)->SetByteArrayRegion(env, dst_address, 0, 16,
1484 (signed char*)details->dst_address);
1485
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001486 jint encap_vrf_id = details->encap_vrf_id;
1487 jint vni = details->vni;
1488 jint decap_next_index = details->decap_next_index;
Chris Luke99cb3352016-04-26 10:49:53 -04001489 jboolean is_ipv6 = details->is_ipv6 ? 1 : 0;
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001490
Robert Varga81d99ac2016-01-30 18:30:36 +01001491 jobject vxlanTunnelDetailsObj = vppVxlanTunnelDetailsObject(env,
Chris Luke99cb3352016-04-26 10:49:53 -04001492 src_address, dst_address, encap_vrf_id, vni,
1493 decap_next_index, is_ipv6);
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001494
1495 (*env)->SetObjectArrayElement(env, vxlanTunnelDetailsArray, i,
1496 vxlanTunnelDetailsObj);
1497 }
1498
1499 returnArray = vxlanTunnelDetailsArray;
1500
1501done:
1502 vppjni_unlock (jm);
1503 return returnArray;
1504}
1505
1506static void vl_api_vxlan_tunnel_details_t_handler
1507(vl_api_vxlan_tunnel_details_t * mp)
1508{
1509 vppjni_main_t * jm = &vppjni_main;
1510 vxlan_tunnel_details_t *tunnel_details;
1511
1512 vec_add2(jm->vxlan_tunnel_details, tunnel_details, 1);
Chris Luke99cb3352016-04-26 10:49:53 -04001513 if (mp->is_ipv6){
1514 u64 *s, *d;
1515
1516 /* this is ugly - why is this in host order at all? -cluke */
1517 /* Per notes from Ole, Maros and Keith, this should all be
1518 * superceded with a new api builder soon, so this interface will
1519 * be short lived -cluke */
1520 s = (void *)mp->src_address;
1521 d = (void *)tunnel_details->src_address;
1522#if CLIB_ARCH_IS_LITTLE_ENDIAN
1523 d[0] = clib_net_to_host_u64(s[1]);
1524 d[1] = clib_net_to_host_u64(s[0]);
1525#else /* big endian */
1526 d[0] = s[0];
1527 d[1] = s[1];
1528#endif
1529
1530 s = (void *)mp->dst_address;
1531 d = (void *)tunnel_details->dst_address;
1532#if CLIB_ARCH_IS_LITTLE_ENDIAN
1533 d[0] = clib_net_to_host_u64(s[1]);
1534 d[1] = clib_net_to_host_u64(s[0]);
1535#else /* big endian */
1536 d[0] = s[0];
1537 d[1] = s[1];
1538#endif
1539 } else {
1540 u32 *s, *d;
1541
1542 /* the type changed from a u32 to u8[16] - this does
1543 * the same as dst = ntohl(src) with the u32 of a v4
1544 * address in the first 32 bits */
1545
1546 s = (void *)mp->src_address;
1547 d = (void *)tunnel_details->src_address;
1548 d[0] = ntohl(s[0]);
1549
1550 s = (void *)mp->dst_address;
1551 d = (void *)tunnel_details->dst_address;
1552 d[0] = ntohl(s[0]);
1553 }
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001554 tunnel_details->encap_vrf_id = ntohl(mp->encap_vrf_id);
1555 tunnel_details->vni = ntohl(mp->vni);
1556 tunnel_details->decap_next_index = ntohl(mp->decap_next_index);
Chris Luke99cb3352016-04-26 10:49:53 -04001557 tunnel_details->is_ipv6 = mp->is_ipv6;
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001558}
1559
Ed Warnickecb9cada2015-12-08 15:45:58 -07001560/* cleanup handler for RX thread */
Robert Varga190efbc2016-02-09 17:16:36 +01001561static void cleanup_rx_thread(void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001562{
Robert Vargad0f92092016-02-10 14:39:57 +01001563 vppjni_main_t * jm = &vppjni_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001564
Robert Vargad0f92092016-02-10 14:39:57 +01001565 vppjni_lock (jm, 99);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001566
Robert Vargad0f92092016-02-10 14:39:57 +01001567 int getEnvStat = (*jm->jvm)->GetEnv(jm->jvm, (void **)&(jm->jenv), JNI_VERSION_1_6);
1568 if (getEnvStat == JNI_EVERSION) {
1569 clib_warning ("Unsupported JNI version\n");
1570 jm->retval = -999;
1571 goto out;
1572 } else if (getEnvStat != JNI_EDETACHED) {
1573 (*jm->jvm)->DetachCurrentThread(jm->jvm);
1574 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001575out:
Robert Vargad0f92092016-02-10 14:39:57 +01001576 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001577}
1578
1579static void
1580vl_api_show_version_reply_t_handler (vl_api_show_version_reply_t * mp)
1581{
Robert Vargad0f92092016-02-10 14:39:57 +01001582 vppjni_main_t * jm = &vppjni_main;
1583 i32 retval = ntohl(mp->retval);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001584
Robert Vargad0f92092016-02-10 14:39:57 +01001585 if (retval >= 0) {
1586 DEBUG_LOG ("show version request succeeded(%d)");
1587 strncpy((char*)jm->program_name, (const char*)mp->program,
1588 sizeof(jm->program_name)-1);
1589 jm->program_name[sizeof(jm->program_name)-1] = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001590
Robert Vargad0f92092016-02-10 14:39:57 +01001591 strncpy((char*)jm->build_directory, (const char*)mp->build_directory,
1592 sizeof(jm->build_directory)-1);
1593 jm->build_directory[sizeof(jm->build_directory)-1] = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001594
Robert Vargad0f92092016-02-10 14:39:57 +01001595 strncpy((char*)jm->version, (const char*)mp->version,
1596 sizeof(jm->version)-1);
1597 jm->version[sizeof(jm->version)-1] = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001598
Robert Vargad0f92092016-02-10 14:39:57 +01001599 strncpy((char*)jm->build_date, (const char*)mp->build_date,
1600 sizeof(jm->build_date)-1);
1601 jm->build_date[sizeof(jm->build_date)-1] = 0;
1602 } else {
1603 clib_error ("show version request failed(%d)", retval);
1604 }
1605 jm->retval = retval;
1606 jm->result_ready = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001607}
1608
1609static void vl_api_want_stats_reply_t_handler (vl_api_want_stats_reply_t * mp)
1610{
Robert Vargad0f92092016-02-10 14:39:57 +01001611 vppjni_main_t * jm = &vppjni_main;
1612 jm->retval = mp->retval; // FIXME: vpp api does not do ntohl on this retval
1613 jm->result_ready = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001614}
1615
1616// control ping needs to be very first thing called
1617// to attach rx thread to java thread
1618static void vl_api_control_ping_reply_t_handler
1619(vl_api_control_ping_reply_t * mp)
1620{
Robert Vargad0f92092016-02-10 14:39:57 +01001621 vppjni_main_t * jm = &vppjni_main;
1622 i32 retval = ntohl(mp->retval);
1623 jm->retval = retval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001624
Robert Vargad0f92092016-02-10 14:39:57 +01001625 // attach to java thread if not attached
1626 int getEnvStat = (*jm->jvm)->GetEnv(jm->jvm, (void **)&(jm->jenv), JNI_VERSION_1_6);
1627 if (getEnvStat == JNI_EDETACHED) {
1628 if ((*jm->jvm)->AttachCurrentThread(jm->jvm, (void **)&(jm->jenv), NULL) != 0) {
1629 clib_warning("Failed to attach thread\n");
1630 jm->retval = -999;
1631 goto out;
1632 }
1633
1634 // workaround as we can't use pthread_cleanup_push
1635 pthread_key_create(&jm->cleanup_rx_thread_key, cleanup_rx_thread);
1636 // destructor is only called if the value of key is non null
1637 pthread_setspecific(jm->cleanup_rx_thread_key, (void *)1);
1638 } else if (getEnvStat == JNI_EVERSION) {
1639 clib_warning ("Unsupported JNI version\n");
1640 jm->retval = -999;
1641 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001642 }
Robert Vargad0f92092016-02-10 14:39:57 +01001643 // jm->jenv is now stable global reference that can be reused (only within RX thread)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001644
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001645#if 0
Robert Vargad0f92092016-02-10 14:39:57 +01001646 // ! callback system removed for now
1647 //
1648 // get issuer msg-id
1649 p = hash_get (jm->ping_hash, context);
1650 if (p != 0) { // ping marks end of some dump call
1651 JNIEnv *env = jm->jenv;
1652 u16 msg_id = (u16)p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001653
Robert Vargad0f92092016-02-10 14:39:57 +01001654 // we will no longer need this
1655 hash_unset (jm->ping_hash, context);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001656
Robert Vargad0f92092016-02-10 14:39:57 +01001657 // get original caller obj
1658 p = hash_get (jm->callback_hash, context);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001659
Robert Vargad0f92092016-02-10 14:39:57 +01001660 if (p == 0) // don't have callback stored
1661 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001662
Robert Vargad0f92092016-02-10 14:39:57 +01001663 jobject obj = (jobject)p[0]; // object that called original call
Ed Warnickecb9cada2015-12-08 15:45:58 -07001664
Robert Vargad0f92092016-02-10 14:39:57 +01001665 switch (msg_id) {
1666 case VL_API_SW_INTERFACE_DUMP:
1667 if (0 != sw_if_dump_call_all_callbacks(obj)) {
1668 goto out2;
1669 }
1670 break;
1671 default:
1672 clib_warning("Unhandled control ping issuer msg-id: %d", msg_id);
1673 goto out2;
1674 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001675 }
Robert Vargad0f92092016-02-10 14:39:57 +01001676out2:
1677 // free the saved obj
1678 hash_unset (jm->callback_hash, context);
1679 // delete global reference
1680 (*env)->DeleteGlobalRef(env, obj);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001681 }
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001682#endif
1683
Ed Warnickecb9cada2015-12-08 15:45:58 -07001684out:
Robert Vargad0f92092016-02-10 14:39:57 +01001685 jm->result_ready = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001686}
1687
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +02001688#ifndef VPPJNI_DEBUG_COUNTERS
Ed Warnickecb9cada2015-12-08 15:45:58 -07001689#define VPPJNI_DEBUG_COUNTERS 0
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +02001690#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -07001691
1692static void vl_api_vnet_interface_counters_t_handler
1693(vl_api_vnet_interface_counters_t *mp)
1694{
Robert Vargad0f92092016-02-10 14:39:57 +01001695 vppjni_main_t *jm = &vppjni_main;
1696 CLIB_UNUSED(char *counter_name);
1697 u32 count, sw_if_index;
1698 int i;
1699 static sw_interface_stats_t empty_stats = {0, };
Ed Warnickecb9cada2015-12-08 15:45:58 -07001700
Robert Vargad0f92092016-02-10 14:39:57 +01001701 vppjni_lock (jm, 12);
1702 count = ntohl (mp->count);
1703 sw_if_index = ntohl (mp->first_sw_if_index);
1704 if (mp->is_combined == 0) {
1705 u64 * vp, v;
1706 vp = (u64 *) mp->data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001707
Robert Vargad0f92092016-02-10 14:39:57 +01001708 for (i = 0; i < count; i++) {
1709 sw_interface_details_t *sw_if = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001710
Robert Vargad0f92092016-02-10 14:39:57 +01001711 v = clib_mem_unaligned (vp, u64);
1712 v = clib_net_to_host_u64 (v);
1713 vp++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001714
Robert Vargad0f92092016-02-10 14:39:57 +01001715 if (sw_if_index < vec_len(jm->sw_if_table))
1716 sw_if = vec_elt_at_index(jm->sw_if_table, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001717
Robert Vargad0f92092016-02-10 14:39:57 +01001718 if (sw_if /* && (sw_if->admin_up_down == 1)*/ && sw_if->interface_name[0] != 0) {
1719 vec_validate_init_empty(jm->sw_if_stats_by_sw_if_index, sw_if_index, empty_stats);
1720 sw_interface_stats_t * s = vec_elt_at_index(jm->sw_if_stats_by_sw_if_index, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001721
Robert Vargad0f92092016-02-10 14:39:57 +01001722 s->sw_if_index = sw_if_index;
1723 s->valid = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001724
Robert Vargad0f92092016-02-10 14:39:57 +01001725 switch (mp->vnet_counter_type) {
1726 case VNET_INTERFACE_COUNTER_DROP:
1727 counter_name = "drop";
1728 s->rx.pkts.discard = v;
1729 break;
1730 case VNET_INTERFACE_COUNTER_PUNT:
1731 counter_name = "punt";
1732 s->rx.pkts.unknown_proto = v;
1733 break;
1734 case VNET_INTERFACE_COUNTER_IP4:
1735 counter_name = "ip4";
1736 s->rx.pkts.ip4 = v;
1737 break;
1738 case VNET_INTERFACE_COUNTER_IP6:
1739 counter_name = "ip6";
1740 s->rx.pkts.ip6 = v;
1741 break;
1742 case VNET_INTERFACE_COUNTER_RX_NO_BUF:
1743 counter_name = "rx-no-buf";
1744 s->rx.pkts.fifo_full = v;
1745 break;
1746 case VNET_INTERFACE_COUNTER_RX_MISS:
1747 counter_name = "rx-miss";
1748 s->rx.pkts.miss = v;
1749 break;
1750 case VNET_INTERFACE_COUNTER_RX_ERROR:
1751 counter_name = "rx-error";
1752 s->rx.pkts.error = v;
1753 break;
1754 case VNET_INTERFACE_COUNTER_TX_ERROR:
1755 counter_name = "tx-error (fifo-full)";
1756 s->tx.pkts.fifo_full = v;
1757 break;
1758 default:
1759 counter_name = "bogus";
1760 break;
1761 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001762
1763#if VPPJNI_DEBUG_COUNTERS == 1
Robert Vargad0f92092016-02-10 14:39:57 +01001764 clib_warning ("%s (%d): %s (%lld)\n", sw_if->interface_name, s->sw_if_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001765 counter_name, v);
1766#endif
Robert Vargad0f92092016-02-10 14:39:57 +01001767 }
1768 sw_if_index++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001769 }
Robert Vargad0f92092016-02-10 14:39:57 +01001770 } else {
1771 vlib_counter_t *vp;
1772 u64 packets, bytes;
1773 vp = (vlib_counter_t *) mp->data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001774
Robert Vargad0f92092016-02-10 14:39:57 +01001775 for (i = 0; i < count; i++) {
1776 sw_interface_details_t *sw_if = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001777
Robert Vargad0f92092016-02-10 14:39:57 +01001778 packets = clib_mem_unaligned (&vp->packets, u64);
1779 packets = clib_net_to_host_u64 (packets);
1780 bytes = clib_mem_unaligned (&vp->bytes, u64);
1781 bytes = clib_net_to_host_u64 (bytes);
1782 vp++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001783
Robert Vargad0f92092016-02-10 14:39:57 +01001784 if (sw_if_index < vec_len(jm->sw_if_table))
1785 sw_if = vec_elt_at_index(jm->sw_if_table, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001786
Robert Vargad0f92092016-02-10 14:39:57 +01001787 if (sw_if /* && (sw_if->admin_up_down == 1) */ && sw_if->interface_name[0] != 0) {
1788 vec_validate_init_empty(jm->sw_if_stats_by_sw_if_index, sw_if_index, empty_stats);
1789 sw_interface_stats_t * s = vec_elt_at_index(jm->sw_if_stats_by_sw_if_index, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001790
Robert Vargad0f92092016-02-10 14:39:57 +01001791 s->valid = 1;
1792 s->sw_if_index = sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001793
Robert Vargad0f92092016-02-10 14:39:57 +01001794 switch (mp->vnet_counter_type) {
1795 case VNET_INTERFACE_COUNTER_RX:
1796 s->rx.pkts.unicast = packets;
1797 s->rx.octets = bytes;
1798 counter_name = "rx";
1799 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001800
Robert Vargad0f92092016-02-10 14:39:57 +01001801 case VNET_INTERFACE_COUNTER_TX:
1802 s->tx.pkts.unicast = packets;
1803 s->tx.octets = bytes;
1804 counter_name = "tx";
1805 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001806
Robert Vargad0f92092016-02-10 14:39:57 +01001807 default:
1808 counter_name = "bogus";
1809 break;
1810 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001811
1812#if VPPJNI_DEBUG_COUNTERS == 1
Robert Vargad0f92092016-02-10 14:39:57 +01001813 clib_warning ("%s (%d): %s.packets %lld\n",
1814 sw_if->interface_name,
1815 sw_if_index, counter_name, packets);
1816 clib_warning ("%s (%d): %s.bytes %lld\n",
1817 sw_if->interface_name,
1818 sw_if_index, counter_name, bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001819#endif
Robert Vargad0f92092016-02-10 14:39:57 +01001820 }
1821 sw_if_index++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001822 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001823 }
Robert Vargad0f92092016-02-10 14:39:57 +01001824 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001825}
1826
1827jint JNI_OnLoad(JavaVM *vm, void *reserved) {
Robert Vargad0f92092016-02-10 14:39:57 +01001828 vppjni_main_t * jm = &vppjni_main;
1829 JNIEnv* env;
1830 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_6) != JNI_OK) {
1831 return JNI_ERR;
1832 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001833
Robert Vargad0f92092016-02-10 14:39:57 +01001834 if (vppjni_init(env) != 0) {
1835 return JNI_ERR;
1836 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001837
Robert Vargad0f92092016-02-10 14:39:57 +01001838 jm->jvm = vm;
1839 return JNI_VERSION_1_6;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001840}
1841
1842void JNI_OnUnload(JavaVM *vm, void *reserved) {
Robert Vargad0f92092016-02-10 14:39:57 +01001843 vppjni_main_t * jm = &vppjni_main;
1844 JNIEnv* env;
1845 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_6) != JNI_OK) {
1846 return;
1847 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001848
Robert Vargad0f92092016-02-10 14:39:57 +01001849 vppjni_uninit(env);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001850
Robert Vargad0f92092016-02-10 14:39:57 +01001851 jm->jenv = NULL;
1852 jm->jvm = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001853}
1854
1855#define foreach_vpe_api_msg \
1856_(CONTROL_PING_REPLY, control_ping_reply) \
1857_(SW_INTERFACE_DETAILS, sw_interface_details) \
1858_(SHOW_VERSION_REPLY, show_version_reply) \
1859_(WANT_STATS_REPLY, want_stats_reply) \
1860_(VNET_INTERFACE_COUNTERS, vnet_interface_counters) \
1861_(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags) \
1862_(BRIDGE_DOMAIN_DETAILS, bridge_domain_details) \
1863_(BRIDGE_DOMAIN_SW_IF_DETAILS, bridge_domain_sw_if_details) \
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001864_(L2_FIB_TABLE_ENTRY, l2_fib_table_entry) \
1865_(IP_ADDRESS_DETAILS, ip_address_details) \
1866_(VXLAN_TUNNEL_DETAILS, vxlan_tunnel_details)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001867
1868static int connect_to_vpe(char *name)
1869{
Robert Vargad0f92092016-02-10 14:39:57 +01001870 vppjni_main_t * jm = &vppjni_main;
1871 api_main_t * am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001872
Robert Vargad0f92092016-02-10 14:39:57 +01001873 if (vl_client_connect_to_vlib("/vpe-api", name, 32) < 0)
1874 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001875
Robert Vargad0f92092016-02-10 14:39:57 +01001876 jm->my_client_index = am->my_client_index;
1877 jm->vl_input_queue = am->shmem_hdr->vl_input_queue;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001878
Robert Vargad0f92092016-02-10 14:39:57 +01001879#define _(N,n) \
1880 vl_msg_api_set_handlers(VL_API_##N, #n, \
1881 vl_api_##n##_t_handler, \
1882 vl_noop_handler, \
1883 vl_api_##n##_t_endian, \
1884 vl_api_##n##_t_print, \
1885 sizeof(vl_api_##n##_t), 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001886 foreach_vpe_api_msg;
1887#undef _
Ed Warnickecb9cada2015-12-08 15:45:58 -07001888
Robert Vargad0f92092016-02-10 14:39:57 +01001889 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001890}
1891
1892/* Format an IP6 address. */
1893u8 * format_ip6_address (u8 * s, va_list * args)
1894{
Robert Vargad0f92092016-02-10 14:39:57 +01001895 ip6_address_t * a = va_arg (*args, ip6_address_t *);
1896 u32 max_zero_run = 0, this_zero_run = 0;
1897 int max_zero_run_index = -1, this_zero_run_index=0;
1898 int in_zero_run = 0, i;
1899 int last_double_colon = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001900
Robert Vargad0f92092016-02-10 14:39:57 +01001901 /* Ugh, this is a pain. Scan forward looking for runs of 0's */
1902 for (i = 0; i < ARRAY_LEN (a->as_u16); i++) {
1903 if (a->as_u16[i] == 0) {
1904 if (in_zero_run) {
1905 this_zero_run++;
1906 } else {
1907 in_zero_run = 1;
1908 this_zero_run =1;
1909 this_zero_run_index = i;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001910 }
Robert Vargad0f92092016-02-10 14:39:57 +01001911 } else {
1912 if (in_zero_run) {
1913 /* offer to compress the biggest run of > 1 zero */
1914 if (this_zero_run > max_zero_run && this_zero_run > 1) {
1915 max_zero_run_index = this_zero_run_index;
1916 max_zero_run = this_zero_run;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001917 }
1918 }
Robert Vargad0f92092016-02-10 14:39:57 +01001919 in_zero_run = 0;
1920 this_zero_run = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001921 }
1922 }
1923
Robert Vargad0f92092016-02-10 14:39:57 +01001924 if (in_zero_run) {
1925 if (this_zero_run > max_zero_run && this_zero_run > 1) {
1926 max_zero_run_index = this_zero_run_index;
1927 max_zero_run = this_zero_run;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001928 }
1929 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001930
Robert Vargad0f92092016-02-10 14:39:57 +01001931 for (i = 0; i < ARRAY_LEN (a->as_u16); i++) {
1932 if (i == max_zero_run_index) {
1933 s = format (s, "::");
1934 i += max_zero_run - 1;
1935 last_double_colon = 1;
1936 } else {
1937 s = format (s, "%s%x",
1938 (last_double_colon || i == 0) ? "" : ":",
1939 clib_net_to_host_u16 (a->as_u16[i]));
1940 last_double_colon = 0;
1941 }
1942 }
1943
1944 return s;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001945}
1946
1947/* Format an IP4 address. */
1948u8 * format_ip4_address (u8 * s, va_list * args)
1949{
Robert Vargad0f92092016-02-10 14:39:57 +01001950 u8 * a = va_arg (*args, u8 *);
1951 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001952}
1953
1954