blob: 54846bcbd2d92feb0f0aaf3b230cfcf97ab786e0 [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);
19#include <api/vpe.api.h>
20#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
29#include <api/vpe_msg_enum.h>
30#define vl_typedefs /* define message structures */
Robert Vargad0f92092016-02-10 14:39:57 +010031#include <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
35#include <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
41#include <api/vpe_all_api_h.h>
42#undef vl_printfun
43
Dave Wallacebf8c15e2015-12-17 20:54:54 -050044#define VPPJNI_DEBUG 0
45
46#if VPPJNI_DEBUG == 1
47 #define DEBUG_LOG(...) clib_warning(__VA_ARGS__)
48#else
49 #define DEBUG_LOG(...)
50#endif
51
Ed Warnickecb9cada2015-12-08 15:45:58 -070052static int connect_to_vpe(char *name);
53
Robert Vargad0f92092016-02-10 14:39:57 +010054/*
Ed Warnickecb9cada2015-12-08 15:45:58 -070055 * The Java runtime isn't compile w/ -fstack-protector,
56 * so we have to supply missing external references for the
57 * regular vpp libraries. Weak reference in case folks get religion
58 * at a later date...
59 */
60void __stack_chk_guard (void) __attribute__((weak));
61void __stack_chk_guard (void) { }
62
Robert Varga81d99ac2016-01-30 18:30:36 +010063BIND_JAPI_CLASS(vppBridgeDomainDetails, "()V");
64BIND_JAPI_BOOL_FIELD(vppBridgeDomainDetails, arpTerm);
65BIND_JAPI_BOOL_FIELD(vppBridgeDomainDetails, flood);
66BIND_JAPI_BOOL_FIELD(vppBridgeDomainDetails, forward);
67BIND_JAPI_BOOL_FIELD(vppBridgeDomainDetails, learn);
68BIND_JAPI_BOOL_FIELD(vppBridgeDomainDetails, uuFlood);
69BIND_JAPI_INT_FIELD(vppBridgeDomainDetails, bdId);
70BIND_JAPI_STRING_FIELD(vppBridgeDomainDetails, name);
71BIND_JAPI_STRING_FIELD(vppBridgeDomainDetails, bviInterfaceName);
72BIND_JAPI_OBJ_FIELD(vppBridgeDomainDetails, interfaces, "[Lorg/openvpp/vppjapi/vppBridgeDomainInterfaceDetails;");
73
74BIND_JAPI_CLASS(vppBridgeDomainInterfaceDetails, "()V");
75BIND_JAPI_BYTE_FIELD(vppBridgeDomainInterfaceDetails, splitHorizonGroup);
76BIND_JAPI_STRING_FIELD(vppBridgeDomainInterfaceDetails, interfaceName);
77
78BIND_JAPI_CLASS(vppInterfaceCounters, "(JJJJJJJJJJJJJJJJJJJJJJ)V");
Pavel84e4ffe2016-02-17 15:10:04 +010079BIND_JAPI_CLASS(vppInterfaceDetails, "(ILjava/lang/String;I[BBBBBIBBIIBBBBIIIII)V");
Robert Varga81d99ac2016-01-30 18:30:36 +010080BIND_JAPI_CLASS(vppIPv4Address, "(IB)V");
81BIND_JAPI_CLASS(vppIPv6Address, "([BB)V");
82BIND_JAPI_CLASS(vppL2Fib, "([BZLjava/lang/String;ZZ)V");
83BIND_JAPI_CLASS(vppVersion, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
84BIND_JAPI_CLASS(vppVxlanTunnelDetails, "(IIIII)V");
85
Robert Vargad0f92092016-02-10 14:39:57 +010086void vl_client_add_api_signatures (vl_api_memclnt_create_t *mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -070087{
Robert Vargad0f92092016-02-10 14:39:57 +010088 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -070089 * Send the main API signature in slot 0. This bit of code must
90 * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
91 */
92 mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
93}
94
95/* Note: non-static, called once to set up the initial intfc table */
96static int sw_interface_dump (vppjni_main_t * jm)
97{
98 vl_api_sw_interface_dump_t *mp;
99 f64 timeout;
100 hash_pair_t * p;
101 name_sort_t * nses = 0, * ns;
102 sw_interface_subif_t * sub = NULL;
103
104 /* Toss the old name table */
Robert Vargad0f92092016-02-10 14:39:57 +0100105 hash_foreach_pair (p, jm->sw_if_index_by_interface_name,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 ({
107 vec_add2 (nses, ns, 1);
108 ns->name = (u8 *)(p->key);
109 ns->value = (u32) p->value[0];
110 }));
111
112 hash_free (jm->sw_if_index_by_interface_name);
113
114 vec_foreach (ns, nses)
115 vec_free (ns->name);
116
117 vec_free (nses);
118
119 vec_foreach (sub, jm->sw_if_subif_table) {
120 vec_free (sub->interface_name);
121 }
122 vec_free (jm->sw_if_subif_table);
123
124 /* recreate the interface name hash table */
Robert Vargad0f92092016-02-10 14:39:57 +0100125 jm->sw_if_index_by_interface_name
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126 = hash_create_string (0, sizeof(uword));
127
128 /* Get list of ethernets */
129 M(SW_INTERFACE_DUMP, sw_interface_dump);
130 mp->name_filter_valid = 1;
Robert Vargad0f92092016-02-10 14:39:57 +0100131 strncpy ((char *) mp->name_filter, "Ether", sizeof(mp->name_filter-1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132 S;
133
134 /* and local / loopback interfaces */
135 M(SW_INTERFACE_DUMP, sw_interface_dump);
136 mp->name_filter_valid = 1;
Robert Vargad0f92092016-02-10 14:39:57 +0100137 strncpy ((char *) mp->name_filter, "lo", sizeof(mp->name_filter-1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138 S;
139
140 /* and vxlan tunnel interfaces */
141 M(SW_INTERFACE_DUMP, sw_interface_dump);
142 mp->name_filter_valid = 1;
Robert Vargad0f92092016-02-10 14:39:57 +0100143 strncpy ((char *) mp->name_filter, "vxlan", sizeof(mp->name_filter-1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144 S;
145
146 /* and tap tunnel interfaces */
147 M(SW_INTERFACE_DUMP, sw_interface_dump);
148 mp->name_filter_valid = 1;
Robert Vargad0f92092016-02-10 14:39:57 +0100149 strncpy ((char *) mp->name_filter, "tap", sizeof(mp->name_filter-1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150 S;
151
152 /* and l2tpv3 tunnel interfaces */
153 M(SW_INTERFACE_DUMP, sw_interface_dump);
154 mp->name_filter_valid = 1;
Robert Vargad0f92092016-02-10 14:39:57 +0100155 strncpy ((char *) mp->name_filter, "l2tpv3_tunnel",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156 sizeof(mp->name_filter-1));
157 S;
158
159 /* Use a control ping for synchronization */
160 {
161 vl_api_control_ping_t * mp;
162 M(CONTROL_PING, control_ping);
163 S;
164 }
165 W;
166}
167
Dave Wallaceba474a22016-02-09 23:09:41 -0500168JNIEXPORT jobject JNICALL Java_org_openvpp_vppjapi_vppConn_getVppVersion0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 (JNIEnv *env, jobject obj)
170{
Robert Vargad0f92092016-02-10 14:39:57 +0100171 vppjni_main_t * jm = &vppjni_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172
Robert Vargad0f92092016-02-10 14:39:57 +0100173 vppjni_lock (jm, 11);
174 jstring progName = (*env)->NewStringUTF(env, (char *)jm->program_name);
175 jstring buildDir = (*env)->NewStringUTF(env, (char *)jm->build_directory);
176 jstring version = (*env)->NewStringUTF(env, (char *)jm->version);
177 jstring buildDate = (*env)->NewStringUTF(env, (char *)jm->build_date);
178 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179
Robert Vargad0f92092016-02-10 14:39:57 +0100180 return vppVersionObject(env, progName, buildDir, version, buildDate);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181}
182
183static int jm_show_version (vppjni_main_t *jm)
184{
Robert Vargad0f92092016-02-10 14:39:57 +0100185 int rv;
186 vl_api_show_version_t *mp;
187 f64 timeout;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188
Robert Vargad0f92092016-02-10 14:39:57 +0100189 vppjni_lock (jm, 10);
190 M(SHOW_VERSION, show_version);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191
Robert Vargad0f92092016-02-10 14:39:57 +0100192 S;
193 vppjni_unlock (jm);
194 WNR;
195 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196}
197
198static int jm_stats_enable_disable (vppjni_main_t *jm, u8 enable)
199{
Robert Vargad0f92092016-02-10 14:39:57 +0100200 vl_api_want_stats_t * mp;
201 f64 timeout;
202 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203
Robert Vargad0f92092016-02-10 14:39:57 +0100204 vppjni_lock (jm, 13);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205
Robert Vargad0f92092016-02-10 14:39:57 +0100206 M(WANT_STATS, want_stats);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207
Robert Vargad0f92092016-02-10 14:39:57 +0100208 mp->enable_disable = enable;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209
Robert Vargad0f92092016-02-10 14:39:57 +0100210 S;
211 vppjni_unlock (jm);
212 WNR;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213
Robert Vargad0f92092016-02-10 14:39:57 +0100214 // already subscribed / already disabled (it's ok)
215 if (rv == -2 || rv == -3)
216 rv = 0;
217 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218}
219
Dave Wallaceba474a22016-02-09 23:09:41 -0500220JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_setInterfaceDescription0
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500221 (JNIEnv *env, jobject obj, jstring ifName, jstring ifDesc)
222{
223 int rv = 0;
224 vppjni_main_t * jm = &vppjni_main;
225 uword * p;
226 u32 sw_if_index = ~0;
227 sw_if_config_t *cfg;
228
229 const char *if_name_str = (*env)->GetStringUTFChars (env, ifName, 0);
230 const char *if_desc_str = (*env)->GetStringUTFChars (env, ifDesc, 0);
231
232 vppjni_lock (jm, 23);
233
234 p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name_str);
235 if (p == 0) {
236 rv = -1;
237 goto out;
238 }
239 sw_if_index = (jint) p[0];
240
241 u8 *if_desc = 0;
242 vec_validate_init_c_string (if_desc, if_desc_str, strlen(if_desc_str));
243 (*env)->ReleaseStringUTFChars (env, ifDesc, if_desc_str);
244
245 p = hash_get (jm->sw_if_config_by_sw_if_index, sw_if_index);
246 if (p != 0) {
247 cfg = (sw_if_config_t *) (p[0]);
248 if (cfg->desc)
249 vec_free(cfg->desc);
Robert Vargad0f92092016-02-10 14:39:57 +0100250 } else {
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500251 cfg = (sw_if_config_t *) clib_mem_alloc(sizeof(sw_if_config_t));
252 hash_set (jm->sw_if_config_by_sw_if_index, sw_if_index, cfg);
253 }
254
255 cfg->desc = if_desc;
256
257out:
258 (*env)->ReleaseStringUTFChars (env, ifName, if_name_str);
259 vppjni_unlock (jm);
260 return rv;
261}
262
Dave Wallaceba474a22016-02-09 23:09:41 -0500263JNIEXPORT jstring JNICALL Java_org_openvpp_vppjapi_vppConn_getInterfaceDescription0
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500264(JNIEnv * env, jobject obj, jstring ifName)
265{
266 vppjni_main_t * jm = &vppjni_main;
267 u32 sw_if_index = ~0;
268 uword * p;
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500269 jstring ifDesc = NULL;
Robert Vargaf0f54d82016-02-10 16:01:58 +0100270 const char *if_name_str = (*env)->GetStringUTFChars (env, ifName, 0);
271 if (!if_name_str)
272 return NULL;
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500273
274 vppjni_lock (jm, 24);
275 p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name_str);
276 if (p == 0)
277 goto out;
278
279 sw_if_index = (jint) p[0];
280
281 p = hash_get (jm->sw_if_config_by_sw_if_index, sw_if_index);
282 if (p == 0)
283 goto out;
284
285 sw_if_config_t *cfg = (sw_if_config_t *) (p[0]);
286 u8 * s = format (0, "%s%c", cfg->desc, 0);
287 ifDesc = (*env)->NewStringUTF(env, (char *)s);
288
289out:
290 vppjni_unlock (jm);
291
292 return ifDesc;
293}
294
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_clientConnect
296 (JNIEnv *env, jobject obj, jstring clientName)
297{
Robert Vargad0f92092016-02-10 14:39:57 +0100298 int rv;
299 const char *client_name;
300 void vl_msg_reply_handler_hookup(void);
301 vppjni_main_t * jm = &vppjni_main;
302 api_main_t * am = &api_main;
303 u8 * heap;
304 mheap_t * h;
305 f64 timeout;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306
Robert Vargad0f92092016-02-10 14:39:57 +0100307 /*
308 * Bail out now if we're not running as root
309 */
310 if (geteuid() != 0)
311 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312
Robert Vargad0f92092016-02-10 14:39:57 +0100313 if (jm->is_connected)
314 return -2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315
Robert Vargaf0f54d82016-02-10 16:01:58 +0100316 client_name = (*env)->GetStringUTFChars(env, clientName, 0);
317 if (!client_name)
318 return -3;
319
Robert Vargad0f92092016-02-10 14:39:57 +0100320 if (jm->heap == 0)
321 clib_mem_init (0, 128<<20);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322
Robert Vargad0f92092016-02-10 14:39:57 +0100323 heap = clib_mem_get_per_cpu_heap();
324 h = mheap_header (heap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325
Robert Vargad0f92092016-02-10 14:39:57 +0100326 clib_time_init (&jm->clib_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327
Robert Vargad0f92092016-02-10 14:39:57 +0100328 rv = connect_to_vpe ((char *) client_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329
Robert Vargad0f92092016-02-10 14:39:57 +0100330 if (rv < 0)
331 clib_warning ("connection failed, rv %d", rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332
Robert Vargad0f92092016-02-10 14:39:57 +0100333 (*env)->ReleaseStringUTFChars (env, clientName, client_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334
Robert Vargad0f92092016-02-10 14:39:57 +0100335 if (rv == 0) {
336 vl_msg_reply_handler_hookup ();
337 jm->is_connected = 1;
338 /* make the main heap thread-safe */
339 h->flags |= MHEAP_FLAG_THREAD_SAFE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340
Robert Vargad0f92092016-02-10 14:39:57 +0100341 jm->reply_hash = hash_create (0, sizeof (uword));
342 //jm->callback_hash = hash_create (0, sizeof (uword));
343 //jm->ping_hash = hash_create (0, sizeof (uword));
344 jm->api_main = am;
345 vjbd_main_init(&jm->vjbd_main);
346 jm->sw_if_index_by_interface_name =
347 hash_create_string (0, sizeof (uword));
348 jm->sw_if_config_by_sw_if_index =
349 hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350
Robert Vargad0f92092016-02-10 14:39:57 +0100351 {
352 // call control ping first to attach rx thread to java thread
353 vl_api_control_ping_t * mp;
354 M(CONTROL_PING, control_ping);
355 S;
356 WNR;
357
358 if (rv != 0) {
359 clib_warning ("first control ping failed: %d", rv);
360 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361 }
Robert Vargad0f92092016-02-10 14:39:57 +0100362 rv = jm_show_version(jm);
363 if (rv != 0)
364 clib_warning ("unable to retrieve vpp version (rv: %d)", rv);
365 rv = sw_interface_dump(jm);
366 if (rv != 0)
367 clib_warning ("unable to retrieve interface list (rv: %d)", rv);
368 rv = jm_stats_enable_disable(jm, 1);
369 if (rv != 0)
370 clib_warning ("unable to subscribe to stats (rv: %d)", rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700371 }
Robert Vargad0f92092016-02-10 14:39:57 +0100372 DEBUG_LOG ("clientConnect result: %d", rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373
Robert Vargad0f92092016-02-10 14:39:57 +0100374 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375}
376
377JNIEXPORT void JNICALL Java_org_openvpp_vppjapi_vppConn_clientDisconnect
378 (JNIEnv *env, jobject obj)
379{
Robert Vargad0f92092016-02-10 14:39:57 +0100380 u8 *save_heap;
381 vppjni_main_t * jm = &vppjni_main;
382 vl_client_disconnect_from_vlib();
383
384 save_heap = jm->heap;
385 memset (jm, 0, sizeof (*jm));
386 jm->heap = save_heap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387}
388
389void vl_api_generic_reply_handler (vl_api_generic_reply_t *mp)
390{
Robert Vargad0f92092016-02-10 14:39:57 +0100391 api_main_t * am = &api_main;
392 u16 msg_id = clib_net_to_host_u16 (mp->_vl_msg_id);
393 trace_cfg_t *cfgp;
394 i32 retval = clib_net_to_host_u32 (mp->retval);
395 int total_bytes = sizeof(mp);
396 vppjni_main_t * jm = &vppjni_main;
397 u8 * saved_reply = 0;
398 u32 context = clib_host_to_net_u32 (mp->context);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399
Robert Vargad0f92092016-02-10 14:39:57 +0100400 cfgp = am->api_trace_cfg + msg_id;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700401
Robert Vargad0f92092016-02-10 14:39:57 +0100402 if (!cfgp)
403 clib_warning ("msg id %d: no trace configuration\n", msg_id);
404 else
405 total_bytes = cfgp->size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406
Robert Vargad0f92092016-02-10 14:39:57 +0100407 jm->context_id_received = context;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408
Robert Vargad0f92092016-02-10 14:39:57 +0100409 DEBUG_LOG ("Received generic reply for msg id %d", msg_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410
Robert Vargad0f92092016-02-10 14:39:57 +0100411 /* A generic reply, successful, we're done */
412 if (retval >= 0 && total_bytes == sizeof(*mp))
413 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414
Robert Vargad0f92092016-02-10 14:39:57 +0100415 /* Save the reply */
416 vec_validate (saved_reply, total_bytes - 1);
417 memcpy (saved_reply, mp, total_bytes);
418
419 vppjni_lock (jm, 2);
420 hash_set (jm->reply_hash, context, saved_reply);
421 jm->saved_reply_count ++;
422 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423}
424
Dave Wallaceba474a22016-02-09 23:09:41 -0500425JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_getRetval0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426(JNIEnv * env, jobject obj, jint context, jint release)
427{
Robert Vargad0f92092016-02-10 14:39:57 +0100428 vppjni_main_t * jm = &vppjni_main;
429 vl_api_generic_reply_t * mp;
430 uword * p;
431 int rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432
Robert Vargad0f92092016-02-10 14:39:57 +0100433 /* Dunno yet? */
434 if (context > jm->context_id_received)
435 return (VNET_API_ERROR_RESPONSE_NOT_READY);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436
Robert Vargad0f92092016-02-10 14:39:57 +0100437 vppjni_lock (jm, 1);
438 p = hash_get (jm->reply_hash, context);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439
Robert Vargad0f92092016-02-10 14:39:57 +0100440 /*
441 * Two cases: a generic "yes" reply - won't be in the hash table
442 * or "no", or "more data" which will be in the table.
443 */
444 if (p == 0)
445 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700446
Robert Vargad0f92092016-02-10 14:39:57 +0100447 mp = (vl_api_generic_reply_t *) (p[0]);
448 rv = clib_net_to_host_u32 (mp->retval);
449
450 if (release) {
451 u8 * free_me = (u8 *) mp;
452 vec_free (free_me);
453 hash_unset (jm->reply_hash, context);
454 jm->saved_reply_count --;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455 }
456
457out:
Robert Vargad0f92092016-02-10 14:39:57 +0100458 vppjni_unlock (jm);
459 return (rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460}
461
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500462static int
463name_sort_cmp (void * a1, void * a2)
464{
Robert Vargad0f92092016-02-10 14:39:57 +0100465 name_sort_t * n1 = a1;
466 name_sort_t * n2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500467
Robert Vargad0f92092016-02-10 14:39:57 +0100468 return strcmp ((char *)n1->name, (char *)n2->name);
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500469}
470
Dave Wallaceba474a22016-02-09 23:09:41 -0500471JNIEXPORT jstring JNICALL Java_org_openvpp_vppjapi_vppConn_getInterfaceList0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472 (JNIEnv * env, jobject obj, jstring name_filter)
473{
Robert Vargad0f92092016-02-10 14:39:57 +0100474 vppjni_main_t * jm = &vppjni_main;
475 jstring rv;
476 hash_pair_t * p;
477 name_sort_t * nses = 0, * ns;
478 const char *this_name;
Robert Vargad0f92092016-02-10 14:39:57 +0100479 u8 * s = 0;
Robert Vargaf0f54d82016-02-10 16:01:58 +0100480 const char * nf = (*env)->GetStringUTFChars (env, name_filter, NULL);
481 if (!nf)
482 return NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700483
Robert Vargad0f92092016-02-10 14:39:57 +0100484 vppjni_lock (jm, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485
Robert Vargad0f92092016-02-10 14:39:57 +0100486 hash_foreach_pair (p, jm->sw_if_index_by_interface_name,
487 ({
488 this_name = (const char *)(p->key);
489 if (strlen (nf) == 0 || strcasestr (this_name, nf)) {
490 vec_add2 (nses, ns, 1);
491 ns->name = (u8 *)(p->key);
492 ns->value = (u32) p->value[0];
493 }
494 }));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700495
Robert Vargad0f92092016-02-10 14:39:57 +0100496 vec_sort_with_function (nses, name_sort_cmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497
Robert Vargad0f92092016-02-10 14:39:57 +0100498 vec_foreach (ns, nses)
499 s = format (s, "%s: %d, ", ns->name, ns->value);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500
Robert Vargad0f92092016-02-10 14:39:57 +0100501 _vec_len (s) = vec_len (s) - 2;
502 vec_terminate_c_string (s);
503 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504
Robert Vargad0f92092016-02-10 14:39:57 +0100505 vec_free (nses);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506
Robert Vargad0f92092016-02-10 14:39:57 +0100507 (*env)->ReleaseStringUTFChars (env, name_filter, nf);
508
509 rv = (*env)->NewStringUTF (env, (char *) s);
510 vec_free (s);
511
512 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700513}
514
Dave Wallaceba474a22016-02-09 23:09:41 -0500515JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_swIfIndexFromName0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700516 (JNIEnv * env, jobject obj, jstring interfaceName)
517{
Robert Vargad0f92092016-02-10 14:39:57 +0100518 vppjni_main_t * jm = &vppjni_main;
519 jint rv = -1;
520 const char * if_name = (*env)->GetStringUTFChars (env, interfaceName, NULL);
Robert Vargaf0f54d82016-02-10 16:01:58 +0100521 if (if_name) {
522 uword * p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523
Robert Vargaf0f54d82016-02-10 16:01:58 +0100524 vppjni_lock (jm, 5);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525
Robert Vargaf0f54d82016-02-10 16:01:58 +0100526 p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527
Robert Vargaf0f54d82016-02-10 16:01:58 +0100528 if (p != 0)
529 rv = (jint) p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530
Robert Vargaf0f54d82016-02-10 16:01:58 +0100531 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532
Robert Vargaf0f54d82016-02-10 16:01:58 +0100533 (*env)->ReleaseStringUTFChars (env, interfaceName, if_name);
534 }
Robert Vargad0f92092016-02-10 14:39:57 +0100535
536 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537}
538
Dave Wallaceba474a22016-02-09 23:09:41 -0500539JNIEXPORT jobject JNICALL Java_org_openvpp_vppjapi_vppConn_getInterfaceCounters0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540(JNIEnv * env, jobject obj, jint swIfIndex)
541{
542 vppjni_main_t * jm = &vppjni_main;
543 sw_interface_stats_t *s;
544 u32 sw_if_index = swIfIndex;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545 jobject result = NULL;
546
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547 vppjni_lock (jm, 16);
548
549 if (sw_if_index >= vec_len(jm->sw_if_stats_by_sw_if_index)) {
550 goto out;
551 }
552 s = &jm->sw_if_stats_by_sw_if_index[sw_if_index];
553 if (!s->valid) {
554 goto out;
555 }
556
Robert Varga81d99ac2016-01-30 18:30:36 +0100557 result = vppInterfaceCountersObject(env,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558 s->rx.octets, s->rx.pkts.ip4, s->rx.pkts.ip6, s->rx.pkts.unicast,
559 s->rx.pkts.multicast, s->rx.pkts.broadcast, s->rx.pkts.discard,
560 s->rx.pkts.fifo_full, s->rx.pkts.error, s->rx.pkts.unknown_proto,
561 s->rx.pkts.miss,
562 s->tx.octets, s->tx.pkts.ip4, s->tx.pkts.ip6, s->tx.pkts.unicast,
563 s->tx.pkts.multicast, s->tx.pkts.broadcast, s->tx.pkts.discard,
564 s->tx.pkts.fifo_full, s->tx.pkts.error, s->tx.pkts.unknown_proto,
565 s->tx.pkts.miss);
566
567out:
568 vppjni_unlock (jm);
569 return result;
570}
571
Dave Wallaceba474a22016-02-09 23:09:41 -0500572JNIEXPORT jstring JNICALL Java_org_openvpp_vppjapi_vppConn_interfaceNameFromSwIfIndex0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573(JNIEnv * env, jobject obj, jint swIfIndex)
574{
575 vppjni_main_t * jm = &vppjni_main;
576 sw_interface_details_t *sw_if_details;
577 u32 sw_if_index;
578 jstring ifname = NULL;
579
580 vppjni_lock (jm, 8);
581
582 sw_if_index = swIfIndex;
583
584 if (sw_if_index >= vec_len(jm->sw_if_table)) {
585 goto out;
586 }
587 sw_if_details = &jm->sw_if_table[sw_if_index];
588 if (!sw_if_details->valid) {
589 goto out;
590 }
591
592 u8 * s = format (0, "%s%c", sw_if_details->interface_name, 0);
593 ifname = (*env)->NewStringUTF(env, (char *)s);
594
595out:
596 vppjni_unlock (jm);
597
598 return ifname;
599}
600
Dave Wallaceba474a22016-02-09 23:09:41 -0500601JNIEXPORT void JNICALL Java_org_openvpp_vppjapi_vppConn_clearInterfaceTable0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700602(JNIEnv * env, jobject obj)
603{
604 vppjni_main_t * jm = &vppjni_main;
605
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500606 vppjni_lock (jm, 21);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700607
608 vec_reset_length(jm->sw_if_table);
609
610 vppjni_unlock (jm);
611}
612
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500613static jobjectArray sw_if_dump_get_interfaces ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700614
Dave Wallaceba474a22016-02-09 23:09:41 -0500615JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_swInterfaceDump0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700616(JNIEnv * env, jobject obj, jbyte name_filter_valid, jbyteArray name_filter)
617{
618 vppjni_main_t *jm = &vppjni_main;
619 f64 timeout;
620 vl_api_sw_interface_dump_t * mp;
621 u32 my_context_id;
622 int rv;
623 rv = vppjni_sanity_check (jm);
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500624 if (rv) {
625 clib_warning("swInterfaceDump sanity_check rv = %d", rv);
626 return NULL;
627 }
628
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629 vppjni_lock (jm, 7);
630 my_context_id = vppjni_get_context_id (jm);
Robert Varga427ce222016-02-01 18:12:18 +0100631 jsize cnt = (*env)->GetArrayLength (env, name_filter);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700632
633 M(SW_INTERFACE_DUMP, sw_interface_dump);
634 mp->context = clib_host_to_net_u32 (my_context_id);
635 mp->name_filter_valid = name_filter_valid;
636
637 if (cnt > sizeof(mp->name_filter))
638 cnt = sizeof(mp->name_filter);
639
Robert Varga427ce222016-02-01 18:12:18 +0100640 (*env)->GetByteArrayRegion(env, name_filter, 0, cnt, (jbyte *)mp->name_filter);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700641
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500642 DEBUG_LOG ("interface filter (%d, %s, len: %d)", mp->name_filter_valid, (char *)mp->name_filter, cnt);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643
Ed Warnickecb9cada2015-12-08 15:45:58 -0700644 jm->collect_indices = 1;
645
646 S;
647 {
648 // now send control ping so we know when it ends
649 vl_api_control_ping_t * mp;
650 M(CONTROL_PING, control_ping);
651 mp->context = clib_host_to_net_u32 (my_context_id);
652
Ed Warnickecb9cada2015-12-08 15:45:58 -0700653 S;
654 }
655 vppjni_unlock (jm);
656 WNR;
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500657
658 vppjni_lock (jm, 7);
659 jobjectArray result = sw_if_dump_get_interfaces(env);
660 vppjni_unlock (jm);
661 return result;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700662}
663
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500664static jobjectArray sw_if_dump_get_interfaces (JNIEnv * env)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665{
666 vppjni_main_t * jm = &vppjni_main;
667 sw_interface_details_t *sw_if_details;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668 u32 i;
669
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500670 int len = vec_len(jm->sw_if_dump_if_indices);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671
Robert Varga81d99ac2016-01-30 18:30:36 +0100672 jobjectArray ifArray = vppInterfaceDetailsArray(env, len);
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500673
674 for (i = 0; i < len; i++) {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700675 u32 sw_if_index = jm->sw_if_dump_if_indices[i];
676 ASSERT(sw_if_index < vec_len(jm->sw_if_table));
677 sw_if_details = &jm->sw_if_table[sw_if_index];
678 ASSERT(sw_if_details->valid);
679
680 u8 * s = format (0, "%s%c", sw_if_details->interface_name, 0);
681
682 jstring ifname = (*env)->NewStringUTF(env, (char *)s);
683 jint ifIndex = sw_if_details->sw_if_index;
684 jint supIfIndex = sw_if_details->sup_sw_if_index;
685 jbyteArray physAddr = (*env)->NewByteArray(env,
686 sw_if_details->l2_address_length);
687 (*env)->SetByteArrayRegion(env, physAddr, 0,
688 sw_if_details->l2_address_length,
689 (signed char*)sw_if_details->l2_address);
690 jint subId = sw_if_details->sub_id;
691 jint subOuterVlanId = sw_if_details->sub_outer_vlan_id;
692 jint subInnerVlanId = sw_if_details->sub_inner_vlan_id;
693 jint vtrOp = sw_if_details->vtr_op;
694 jint vtrPushDot1q = sw_if_details->vtr_push_dot1q;
695 jint vtrTag1 = sw_if_details->vtr_tag1;
696 jint vtrTag2 = sw_if_details->vtr_tag2;
Pavel84e4ffe2016-02-17 15:10:04 +0100697 jint linkMtu = sw_if_details->link_mtu;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700698
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699 jbyte adminUpDown = sw_if_details->admin_up_down;
700 jbyte linkUpDown = sw_if_details->link_up_down;
701 jbyte linkDuplex = sw_if_details->link_duplex;
702 jbyte linkSpeed = sw_if_details->link_speed;
703 jbyte subDot1ad = sw_if_details->sub_dot1ad;
704 jbyte subNumberOfTags = sw_if_details->sub_number_of_tags;
705 jbyte subExactMatch = sw_if_details->sub_exact_match;
706 jbyte subDefault = sw_if_details->sub_default;
707 jbyte subOuterVlanIdAny = sw_if_details->sub_outer_vlan_id_any;
708 jbyte subInnerVlanIdAny = sw_if_details->sub_inner_vlan_id_any;
709
Robert Varga81d99ac2016-01-30 18:30:36 +0100710 jobject ifObj = vppInterfaceDetailsObject(env,
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500711 ifIndex, ifname,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700712 supIfIndex, physAddr, adminUpDown, linkUpDown,
713 linkDuplex, linkSpeed, subId, subDot1ad,
714 subNumberOfTags, subOuterVlanId, subInnerVlanId,
715 subExactMatch, subDefault, subOuterVlanIdAny,
Pavel84e4ffe2016-02-17 15:10:04 +0100716 subInnerVlanIdAny, vtrOp, vtrPushDot1q, vtrTag1, vtrTag2, linkMtu);
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500717 (*env)->SetObjectArrayElement(env, ifArray, i, ifObj);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700718 }
719
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720 jm->collect_indices = 0;
721 vec_reset_length(jm->sw_if_dump_if_indices);
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500722 return ifArray;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723}
724
Dave Wallaceba474a22016-02-09 23:09:41 -0500725JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_findOrAddBridgeDomainId0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726 (JNIEnv * env, jobject obj, jstring bridgeDomain)
727{
Robert Vargad0f92092016-02-10 14:39:57 +0100728 vppjni_main_t * jm = &vppjni_main;
Robert Vargad0f92092016-02-10 14:39:57 +0100729 jint rv = -1;
730 const char * bdName = (*env)->GetStringUTFChars (env, bridgeDomain, NULL);
Robert Vargaf0f54d82016-02-10 16:01:58 +0100731 if (bdName) {
732 static u8 * bd_name = 0;
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500733
Robert Vargaf0f54d82016-02-10 16:01:58 +0100734 vec_validate_init_c_string (bd_name, bdName, strlen(bdName));
735 (*env)->ReleaseStringUTFChars (env, bridgeDomain, bdName);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700736
Robert Vargaf0f54d82016-02-10 16:01:58 +0100737 vppjni_lock (jm, 6);
738 rv = (jint)vjbd_find_or_add_bd (&jm->vjbd_main, bd_name);
739 vppjni_unlock (jm);
Robert Vargad0f92092016-02-10 14:39:57 +0100740
Robert Vargaf0f54d82016-02-10 16:01:58 +0100741 _vec_len(bd_name) = 0;
742 }
Robert Vargad0f92092016-02-10 14:39:57 +0100743 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700744}
745
Dave Wallaceba474a22016-02-09 23:09:41 -0500746JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_bridgeDomainIdFromName0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700747 (JNIEnv * env, jobject obj, jstring bridgeDomain)
748{
Robert Vargad0f92092016-02-10 14:39:57 +0100749 vppjni_main_t * jm = &vppjni_main;
Robert Vargad0f92092016-02-10 14:39:57 +0100750 jint rv = -1;
751 const char * bdName = (*env)->GetStringUTFChars (env, bridgeDomain, NULL);
Robert Vargaf0f54d82016-02-10 16:01:58 +0100752 if (bdName) {
753 static u8 * bd_name = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700754
Robert Vargaf0f54d82016-02-10 16:01:58 +0100755 vec_validate_init_c_string (bd_name, bdName, strlen(bdName));
756 (*env)->ReleaseStringUTFChars (env, bridgeDomain, bdName);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700757
Robert Vargaf0f54d82016-02-10 16:01:58 +0100758 vppjni_lock (jm, 20);
759 rv = (jint)vjbd_id_from_name(&jm->vjbd_main, (u8 *)bd_name);
760 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700761
Robert Vargaf0f54d82016-02-10 16:01:58 +0100762 _vec_len(bd_name) = 0;
763 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700764
Robert Vargad0f92092016-02-10 14:39:57 +0100765 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766}
767
Dave Wallaceba474a22016-02-09 23:09:41 -0500768JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_bridgeDomainIdFromInterfaceName0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769 (JNIEnv * env, jobject obj, jstring interfaceName)
770{
Robert Vargad0f92092016-02-10 14:39:57 +0100771 vppjni_main_t * jm = &vppjni_main;
772 vjbd_main_t * bdm = &jm->vjbd_main;
773 u32 sw_if_index;
774 jint rv = -1;
775 const char * if_name;
776 uword * p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777
Robert Vargad0f92092016-02-10 14:39:57 +0100778 if_name = (*env)->GetStringUTFChars (env, interfaceName, NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700779
Robert Vargad0f92092016-02-10 14:39:57 +0100780 vppjni_lock (jm, 14);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700781
Robert Vargad0f92092016-02-10 14:39:57 +0100782 p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700783
Robert Vargad0f92092016-02-10 14:39:57 +0100784 if (p != 0) {
785 sw_if_index = (jint) p[0];
786 p = hash_get (bdm->bd_id_by_sw_if_index, sw_if_index);
787 if (p != 0) {
788 rv = (jint) p[0];
789 }
790 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791
Robert Vargad0f92092016-02-10 14:39:57 +0100792 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700793
Robert Vargad0f92092016-02-10 14:39:57 +0100794 (*env)->ReleaseStringUTFChars (env, interfaceName, if_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700795
Robert Vargad0f92092016-02-10 14:39:57 +0100796 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797}
798
Robert Vargad0f92092016-02-10 14:39:57 +0100799/*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700800 * Special-case: build the interface table, maintain
801 * the next loopback sw_if_index vbl.
802 */
803static void vl_api_sw_interface_details_t_handler
804(vl_api_sw_interface_details_t * mp)
805{
Robert Vargad0f92092016-02-10 14:39:57 +0100806 vppjni_main_t * jm = &vppjni_main;
807 static sw_interface_details_t empty_sw_if_details = {0,};
808 sw_interface_details_t *sw_if_details;
809 u32 sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700810
Robert Vargad0f92092016-02-10 14:39:57 +0100811 vppjni_lock (jm, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700812
Robert Vargad0f92092016-02-10 14:39:57 +0100813 sw_if_index = ntohl (mp->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700814
Robert Vargad0f92092016-02-10 14:39:57 +0100815 u8 * s = format (0, "%s%c", mp->interface_name, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700816
Robert Vargad0f92092016-02-10 14:39:57 +0100817 if (jm->collect_indices) {
818 u32 pos = vec_len(jm->sw_if_dump_if_indices);
819 vec_validate(jm->sw_if_dump_if_indices, pos);
820 jm->sw_if_dump_if_indices[pos] = sw_if_index;
821 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700822
Robert Vargad0f92092016-02-10 14:39:57 +0100823 vec_validate_init_empty(jm->sw_if_table, sw_if_index, empty_sw_if_details);
824 sw_if_details = &jm->sw_if_table[sw_if_index];
825 sw_if_details->valid = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700826
Robert Vargad0f92092016-02-10 14:39:57 +0100827 snprintf((char *)sw_if_details->interface_name,
828 sizeof(sw_if_details->interface_name), "%s", (char *)s);
829 sw_if_details->sw_if_index = sw_if_index;
830 sw_if_details->sup_sw_if_index = ntohl(mp->sup_sw_if_index);
831 sw_if_details->l2_address_length = ntohl (mp->l2_address_length);
832 ASSERT(sw_if_details->l2_address_length <= sizeof(sw_if_details->l2_address));
833 memcpy(sw_if_details->l2_address, mp->l2_address,
834 sw_if_details->l2_address_length);
835 sw_if_details->sub_id = ntohl (mp->sub_id);
836 sw_if_details->sub_outer_vlan_id = ntohl (mp->sub_outer_vlan_id);
837 sw_if_details->sub_inner_vlan_id = ntohl (mp->sub_inner_vlan_id);
838 sw_if_details->vtr_op = ntohl (mp->vtr_op);
839 sw_if_details->vtr_push_dot1q = ntohl (mp->vtr_push_dot1q);
840 sw_if_details->vtr_tag1 = ntohl (mp->vtr_tag1);
841 sw_if_details->vtr_tag2 = ntohl (mp->vtr_tag2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700842
Robert Vargad0f92092016-02-10 14:39:57 +0100843 sw_if_details->admin_up_down = mp->admin_up_down;
844 sw_if_details->link_up_down = mp->link_up_down;
845 sw_if_details->link_duplex = mp->link_duplex;
846 sw_if_details->link_speed = mp->link_speed;
847 sw_if_details->sub_dot1ad = mp->sub_dot1ad;
848 sw_if_details->sub_number_of_tags = mp->sub_number_of_tags;
849 sw_if_details->sub_exact_match = mp->sub_exact_match;
850 sw_if_details->sub_default = mp->sub_default;
851 sw_if_details->sub_outer_vlan_id_any = mp->sub_outer_vlan_id_any;
852 sw_if_details->sub_inner_vlan_id_any = mp->sub_inner_vlan_id_any;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700853
Robert Vargad0f92092016-02-10 14:39:57 +0100854 hash_set_mem (jm->sw_if_index_by_interface_name, s, sw_if_index);
855 DEBUG_LOG ("Got interface %s", (char *)s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700856
Robert Vargad0f92092016-02-10 14:39:57 +0100857 /* In sub interface case, fill the sub interface table entry */
858 if (mp->sw_if_index != mp->sup_sw_if_index) {
859 sw_interface_subif_t * sub = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700860
Robert Vargad0f92092016-02-10 14:39:57 +0100861 vec_add2(jm->sw_if_subif_table, sub, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700862
Robert Vargad0f92092016-02-10 14:39:57 +0100863 vec_validate(sub->interface_name, strlen((char *)s) + 1);
864 strncpy((char *)sub->interface_name, (char *)s,
865 vec_len(sub->interface_name));
866 sub->sw_if_index = ntohl(mp->sw_if_index);
867 sub->sub_id = ntohl(mp->sub_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700868
Robert Vargad0f92092016-02-10 14:39:57 +0100869 sub->sub_dot1ad = mp->sub_dot1ad;
870 sub->sub_number_of_tags = mp->sub_number_of_tags;
871 sub->sub_outer_vlan_id = ntohs(mp->sub_outer_vlan_id);
872 sub->sub_inner_vlan_id = ntohs(mp->sub_inner_vlan_id);
873 sub->sub_exact_match = mp->sub_exact_match;
874 sub->sub_default = mp->sub_default;
875 sub->sub_outer_vlan_id_any = mp->sub_outer_vlan_id_any;
876 sub->sub_inner_vlan_id_any = mp->sub_inner_vlan_id_any;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700877
Robert Vargad0f92092016-02-10 14:39:57 +0100878 /* vlan tag rewrite */
879 sub->vtr_op = ntohl(mp->vtr_op);
880 sub->vtr_push_dot1q = ntohl(mp->vtr_push_dot1q);
881 sub->vtr_tag1 = ntohl(mp->vtr_tag1);
882 sub->vtr_tag2 = ntohl(mp->vtr_tag2);
883 }
884 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700885}
886
887static void vl_api_sw_interface_set_flags_t_handler
888(vl_api_sw_interface_set_flags_t * mp)
889{
890 /* $$$ nothing for the moment */
891}
892
893static jintArray create_array_of_bd_ids(JNIEnv * env, jint bd_id)
894{
895 vppjni_main_t *jm = &vppjni_main;
896 vjbd_main_t * bdm = &jm->vjbd_main;
897 u32 *buf = NULL;
898 u32 i;
899
900 if (bd_id != ~0) {
901 vec_add1(buf, bd_id);
902 } else {
903 for (i = 0; i < vec_len(bdm->bd_oper); i++) {
904 u32 bd_id = bdm->bd_oper[i].bd_id;
905 vec_add1(buf, bd_id);
906 }
907 }
908
909 jintArray bdidArray = (*env)->NewIntArray(env, vec_len(buf));
Robert Vargaec3034c2016-02-10 16:00:16 +0100910 if (!bdidArray) {
911 goto out;
912 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700913
914 (*env)->SetIntArrayRegion(env, bdidArray, 0, vec_len(buf), (int*)buf);
915
Robert Vargaec3034c2016-02-10 16:00:16 +0100916out:
Robert Varga7a224a02016-02-01 18:33:38 +0100917 vec_free(buf);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700918 return bdidArray;
919}
920
921static void bridge_domain_oper_free(void)
922{
923 vppjni_main_t *jm = &vppjni_main;
924 vjbd_main_t *bdm = &jm->vjbd_main;
925 u32 i;
926
927 for (i = 0; i < vec_len(bdm->bd_oper); i++) {
928 vec_free(bdm->bd_oper->l2fib_oper);
929 }
930 vec_reset_length(bdm->bd_oper);
931 hash_free(bdm->bd_id_by_sw_if_index);
932 hash_free(bdm->oper_bd_index_by_bd_id);
933}
934
Dave Wallaceba474a22016-02-09 23:09:41 -0500935JNIEXPORT jintArray JNICALL Java_org_openvpp_vppjapi_vppConn_bridgeDomainDump0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700936(JNIEnv * env, jobject obj, jint bd_id)
937{
938 vppjni_main_t *jm = &vppjni_main;
939 vl_api_bridge_domain_dump_t * mp;
940 u32 my_context_id;
941 f64 timeout;
942 int rv;
943 rv = vppjni_sanity_check (jm);
944 if (rv) return NULL;
945
946 vppjni_lock (jm, 15);
947
948 if (~0 == bd_id) {
949 bridge_domain_oper_free();
950 }
951
952 my_context_id = vppjni_get_context_id (jm);
953 M(BRIDGE_DOMAIN_DUMP, bridge_domain_dump);
954 mp->context = clib_host_to_net_u32 (my_context_id);
955 mp->bd_id = clib_host_to_net_u32(bd_id);
956 S;
957
958 /* Use a control ping for synchronization */
959 {
960 vl_api_control_ping_t * mp;
961 M(CONTROL_PING, control_ping);
962 S;
963 }
964
965 WNR;
966 if (0 != rv) {
967 return NULL;
968 }
969
970 jintArray ret = create_array_of_bd_ids(env, bd_id);
971
972 vppjni_unlock (jm);
973
974 return ret;
975}
976
977static void
978vl_api_bridge_domain_details_t_handler (vl_api_bridge_domain_details_t * mp)
979{
Robert Vargad0f92092016-02-10 14:39:57 +0100980 vppjni_main_t *jm = &vppjni_main;
981 vjbd_main_t * bdm = &jm->vjbd_main;
982 vjbd_oper_t * bd_oper;
983 u32 bd_id, bd_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700984
Robert Vargad0f92092016-02-10 14:39:57 +0100985 bd_id = ntohl (mp->bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700986
Robert Vargad0f92092016-02-10 14:39:57 +0100987 bd_index = vec_len(bdm->bd_oper);
988 vec_validate (bdm->bd_oper, bd_index);
989 bd_oper = vec_elt_at_index(bdm->bd_oper, bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700990
Robert Vargad0f92092016-02-10 14:39:57 +0100991 hash_set(bdm->oper_bd_index_by_bd_id, bd_id, bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700992
Robert Vargad0f92092016-02-10 14:39:57 +0100993 bd_oper->bd_id = bd_id;
994 bd_oper->flood = mp->flood != 0;
995 bd_oper->forward = mp->forward != 0;
996 bd_oper->learn = mp->learn != 0;
997 bd_oper->uu_flood = mp->uu_flood != 0;
998 bd_oper->arp_term = mp->arp_term != 0;
999 bd_oper->bvi_sw_if_index = ntohl (mp->bvi_sw_if_index);
1000 bd_oper->n_sw_ifs = ntohl (mp->n_sw_ifs);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001001
Robert Vargad0f92092016-02-10 14:39:57 +01001002 bd_oper->bd_sw_if_oper = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001003}
1004
1005static void
1006vl_api_bridge_domain_sw_if_details_t_handler
1007(vl_api_bridge_domain_sw_if_details_t * mp)
1008{
Robert Vargad0f92092016-02-10 14:39:57 +01001009 vppjni_main_t *jm = &vppjni_main;
1010 vjbd_main_t * bdm = &jm->vjbd_main;
1011 bd_sw_if_oper_t * bd_sw_if_oper;
1012 u32 bd_id, sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001013
Robert Vargad0f92092016-02-10 14:39:57 +01001014 bd_id = ntohl (mp->bd_id);
1015 sw_if_index = ntohl (mp->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001016
Robert Vargad0f92092016-02-10 14:39:57 +01001017 uword *p;
1018 p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
1019 if (p == 0) {
1020 clib_warning("Invalid bd_id %d in bridge_domain_sw_if_details_t_handler", bd_id);
1021 return;
1022 }
1023 u32 oper_bd_index = (jint) p[0];
1024 vjbd_oper_t *bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001025
Robert Vargad0f92092016-02-10 14:39:57 +01001026 u32 len = vec_len(bd_oper->bd_sw_if_oper);
1027 vec_validate(bd_oper->bd_sw_if_oper, len);
1028 bd_sw_if_oper = &bd_oper->bd_sw_if_oper[len];
1029 bd_sw_if_oper->bd_id = bd_id;
1030 bd_sw_if_oper->sw_if_index = sw_if_index;
1031 bd_sw_if_oper->shg = mp->shg;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001032
Robert Vargad0f92092016-02-10 14:39:57 +01001033 hash_set(bdm->bd_id_by_sw_if_index, sw_if_index, bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001034}
1035
1036static const char* interface_name_from_sw_if_index(u32 sw_if_index)
1037{
1038 vppjni_main_t *jm = &vppjni_main;
1039
1040 if (sw_if_index >= vec_len(jm->sw_if_table)) {
1041 return NULL;
1042 }
1043 if (!jm->sw_if_table[sw_if_index].valid) {
1044 return NULL;
1045 }
1046 return (const char*)jm->sw_if_table[sw_if_index].interface_name;
1047}
1048
Dave Wallaceba474a22016-02-09 23:09:41 -05001049JNIEXPORT jobject JNICALL Java_org_openvpp_vppjapi_vppConn_getBridgeDomainDetails0
Ed Warnickecb9cada2015-12-08 15:45:58 -07001050(JNIEnv * env, jobject obj, jint bdId)
1051{
1052 vppjni_main_t *jm = &vppjni_main;
1053 vjbd_main_t * bdm = &jm->vjbd_main;
1054 u32 oper_bd_index;
1055 u32 bd_id = bdId;
1056 jobject rv = NULL;
1057 uword *p;
1058
1059 vppjni_lock (jm, 16);
1060
1061 p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
1062 if (p == 0) {
1063 rv = NULL;
1064 goto out;
1065 }
1066 oper_bd_index = (jint) p[0];
1067
1068 vjbd_oper_t *bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
1069
1070
1071 /* setting BridgeDomainDetails */
1072
Robert Varga81d99ac2016-01-30 18:30:36 +01001073 jobject bddObj = vppBridgeDomainDetailsObject(env);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001074
1075 u8 *vec_bd_name = vjbd_oper_name_from_id(bdm, bd_id);
1076 if (NULL == vec_bd_name) {
1077 rv = NULL;
1078 goto out;
1079 }
1080 char *str_bd_name = (char*)format (0, "%s%c", vec_bd_name, 0);
1081 vec_free(vec_bd_name);
1082 jstring bdName = (*env)->NewStringUTF(env, str_bd_name);
1083 vec_free(str_bd_name);
1084 if (NULL == bdName) {
1085 rv = NULL;
1086 goto out;
1087 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001088
Robert Varga81d99ac2016-01-30 18:30:36 +01001089 set_vppBridgeDomainDetails_name(env, bddObj, bdName);
1090 set_vppBridgeDomainDetails_bdId(env, bddObj, bdId);
1091 set_vppBridgeDomainDetails_flood(env, bddObj, (jboolean)bd_oper->flood);
1092 set_vppBridgeDomainDetails_uuFlood(env, bddObj, (jboolean)bd_oper->uu_flood);
1093 set_vppBridgeDomainDetails_forward(env, bddObj, (jboolean)bd_oper->forward);
1094 set_vppBridgeDomainDetails_learn(env, bddObj, (jboolean)bd_oper->learn);
1095 set_vppBridgeDomainDetails_arpTerm(env, bddObj, (jboolean)bd_oper->arp_term);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001096
1097 jstring bviInterfaceName = NULL;
1098 if (~0 != bd_oper->bvi_sw_if_index) {
1099 const char *str_if_name = interface_name_from_sw_if_index(bd_oper->bvi_sw_if_index);
1100 if (NULL == str_if_name) {
1101 clib_warning("Could not get interface name for sw_if_index %d", bd_oper->bvi_sw_if_index);
1102 rv = NULL;
1103 goto out;
1104 }
1105 bviInterfaceName = (*env)->NewStringUTF(env, str_if_name);
1106 if (NULL == bviInterfaceName) {
1107 rv = NULL;
1108 goto out;
1109 }
1110 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001111
Robert Varga81d99ac2016-01-30 18:30:36 +01001112 set_vppBridgeDomainDetails_bviInterfaceName(env, bddObj, bviInterfaceName);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001113
1114 /* setting BridgeDomainInterfaceDetails */
1115
Ed Warnickecb9cada2015-12-08 15:45:58 -07001116 u32 len = vec_len(bd_oper->bd_sw_if_oper);
1117 ASSERT(len == bd_oper->n_sw_ifs);
1118
Robert Varga81d99ac2016-01-30 18:30:36 +01001119 jobjectArray bdidArray = vppBridgeDomainInterfaceDetailsArray(env, len);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001120
1121 u32 i;
1122 for (i = 0; i < len; i++) {
1123 bd_sw_if_oper_t *sw_if_oper = &bd_oper->bd_sw_if_oper[i];
1124
Robert Varga81d99ac2016-01-30 18:30:36 +01001125 jobject bdidObj = vppBridgeDomainInterfaceDetailsObject(env);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001126 (*env)->SetObjectArrayElement(env, bdidArray, i, bdidObj);
1127
1128 u32 sw_if_index = sw_if_oper->sw_if_index;
1129 const char *str_if_name = interface_name_from_sw_if_index(sw_if_index);
1130 if (NULL == str_if_name) {
1131 rv = NULL;
1132 goto out;
1133 }
1134 jstring interfaceName = (*env)->NewStringUTF(env, str_if_name);
1135 if (NULL == interfaceName) {
1136 rv = NULL;
1137 goto out;
1138 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001139
Robert Varga81d99ac2016-01-30 18:30:36 +01001140 set_vppBridgeDomainInterfaceDetails_interfaceName(env, bdidObj, interfaceName);
1141 set_vppBridgeDomainInterfaceDetails_splitHorizonGroup(env, bdidObj, (jbyte)sw_if_oper->shg);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001142 }
1143
Robert Varga81d99ac2016-01-30 18:30:36 +01001144 set_vppBridgeDomainDetails_interfaces(env, bddObj, bdidArray);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001145
1146 rv = bddObj;
1147
1148out:
1149
1150 vppjni_unlock (jm);
1151
1152 return rv;
1153}
1154
1155static jobject l2_fib_create_object(JNIEnv *env, bd_l2fib_oper_t *l2_fib)
1156{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001157 u32 sw_if_index = l2_fib->sw_if_index;
1158 const char *str_if_name = interface_name_from_sw_if_index(sw_if_index);
1159 if (NULL == str_if_name) {
1160 return NULL;
1161 }
1162 jstring interfaceName = (*env)->NewStringUTF(env, str_if_name);
1163 if (NULL == interfaceName) {
1164 return NULL;
1165 }
1166
1167 jbyteArray physAddr = (*env)->NewByteArray(env, 6);
1168 (*env)->SetByteArrayRegion(env, physAddr, 0, 6,
1169 (signed char*)l2_fib->mac_addr.fields.mac);
1170 jboolean staticConfig = !l2_fib->learned;
1171 jstring outgoingInterface = interfaceName;
1172 jboolean filter = l2_fib->filter;
1173 jboolean bridgedVirtualInterface = l2_fib->bvi;
1174
Robert Varga81d99ac2016-01-30 18:30:36 +01001175 return vppL2FibObject(env, physAddr, staticConfig, outgoingInterface, filter, bridgedVirtualInterface);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001176}
1177
Dave Wallaceba474a22016-02-09 23:09:41 -05001178JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_l2FibTableDump0
Ed Warnickecb9cada2015-12-08 15:45:58 -07001179(JNIEnv * env, jobject obj, jint bd_id)
1180{
Robert Vargad0f92092016-02-10 14:39:57 +01001181 vppjni_main_t *jm = &vppjni_main;
1182 vjbd_main_t * bdm = &jm->vjbd_main;
1183 vl_api_l2_fib_table_dump_t *mp;
1184 jobjectArray l2FibArray = NULL;
1185 vjbd_oper_t *bd_oper;
1186 u32 oper_bd_index;
1187 uword *p;
1188 f64 timeout;
1189 int rv;
1190 u32 i;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001191
Robert Vargad0f92092016-02-10 14:39:57 +01001192 vppjni_lock (jm, 17);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001193
Robert Vargad0f92092016-02-10 14:39:57 +01001194 //vjbd_l2fib_oper_reset (bdm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001195
Robert Vargad0f92092016-02-10 14:39:57 +01001196 p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
1197 if (p == 0) {
1198 goto done;
1199 }
1200 oper_bd_index = p[0];
1201 bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
1202 vec_reset_length (bd_oper->l2fib_oper);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001203
Robert Vargad0f92092016-02-10 14:39:57 +01001204 /* Get list of l2 fib table entries */
1205 M(L2_FIB_TABLE_DUMP, l2_fib_table_dump);
1206 mp->bd_id = ntohl(bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001207 S;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001208
Robert Vargad0f92092016-02-10 14:39:57 +01001209 /* Use a control ping for synchronization */
1210 {
1211 vl_api_control_ping_t * mp;
1212 M(CONTROL_PING, control_ping);
1213 S;
1214 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001215
Robert Vargad0f92092016-02-10 14:39:57 +01001216 WNR;
1217 if (0 != rv) {
1218 goto done;
1219 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001220
Robert Vargad0f92092016-02-10 14:39:57 +01001221 u32 count = vec_len(bd_oper->l2fib_oper);
1222 bd_l2fib_oper_t *l2fib_oper = bd_oper->l2fib_oper;
1223
1224 l2FibArray = vppL2FibArray(env, count);
1225 for (i = 0; i < count; i++) {
1226 bd_l2fib_oper_t *l2_fib = &l2fib_oper[i];
1227 jobject l2FibObj = l2_fib_create_object(env, l2_fib);
1228 (*env)->SetObjectArrayElement(env, l2FibArray, i, l2FibObj);
1229 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001230
1231done:
Robert Vargad0f92092016-02-10 14:39:57 +01001232 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001233
Robert Vargad0f92092016-02-10 14:39:57 +01001234 return l2FibArray;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001235}
1236
1237static void
1238vl_api_l2_fib_table_entry_t_handler (vl_api_l2_fib_table_entry_t * mp)
1239{
Robert Vargad0f92092016-02-10 14:39:57 +01001240 //static u8 * mac_addr;
1241 vppjni_main_t *jm = &vppjni_main;
1242 vjbd_main_t * bdm = &jm->vjbd_main;
1243 vjbd_oper_t * bd_oper;
1244 u32 bd_id, oper_bd_index;
1245 //uword mhash_val_l2fi;
1246 bd_l2fib_oper_t * l2fib_oper;
1247 l2fib_u64_mac_t * l2fe_u64_mac = (l2fib_u64_mac_t *)&mp->mac;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001248
Robert Vargad0f92092016-02-10 14:39:57 +01001249 bd_id = ntohl (mp->bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001250
Robert Vargad0f92092016-02-10 14:39:57 +01001251 uword *p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
1252 if (p == 0) {
1253 return;
1254 }
1255 oper_bd_index = (jint) p[0];
1256 bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001257
1258#if 0
Robert Vargad0f92092016-02-10 14:39:57 +01001259 vec_validate (mac_addr, MAC_ADDRESS_SIZE);
1260 memcpy (mac_addr, l2fe_u64_mac->fields.mac, MAC_ADDRESS_SIZE);
1261 mhash_val_l2fi = vec_len (bd_oper->l2fib_oper);
1262 if (mhash_elts (&bd_oper->l2fib_index_by_mac) == 0)
1263 mhash_init (&bd_oper->l2fib_index_by_mac, sizeof (u32), MAC_ADDRESS_SIZE);
1264 mhash_set_mem (&bd_oper->l2fib_index_by_mac, mac_addr, &mhash_val_l2fi, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001265#endif
1266
Robert Vargad0f92092016-02-10 14:39:57 +01001267 vec_add2 (bd_oper->l2fib_oper, l2fib_oper, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001268
Robert Vargad0f92092016-02-10 14:39:57 +01001269 l2fib_oper->bd_id = bd_id;
1270 l2fib_oper->mac_addr.raw = l2fib_mac_to_u64 (l2fe_u64_mac->fields.mac);
1271 l2fib_oper->sw_if_index = ntohl (mp->sw_if_index);
1272 l2fib_oper->learned = !mp->static_mac;
1273 l2fib_oper->filter = mp->filter_mac;
1274 l2fib_oper->bvi = mp->bvi_mac;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001275}
1276
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001277static int ipAddressDump
1278(JNIEnv * env, jobject obj, jstring interfaceName, jboolean isIPv6)
1279{
1280 vppjni_main_t *jm = &vppjni_main;
1281 vl_api_ip_address_dump_t * mp;
1282 const char *if_name;
1283 u32 my_context_id;
1284 u32 sw_if_index;
1285 f64 timeout;
1286 uword *p;
1287 int rv = 0;
1288
1289 if (NULL == interfaceName) {
1290 return -1;
1291 }
1292
1293 if_name = (*env)->GetStringUTFChars (env, interfaceName, NULL);
Robert Vargaf0f54d82016-02-10 16:01:58 +01001294 if (!if_name) {
1295 return -1;
1296 }
1297
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001298 p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name);
1299 (*env)->ReleaseStringUTFChars (env, interfaceName, if_name);
1300 if (p == 0) {
1301 return -1;
1302 }
1303 sw_if_index = (u32) p[0];
1304
1305 rv = vppjni_sanity_check (jm);
1306 if (0 != rv) {
1307 return rv;
1308 }
1309
1310 my_context_id = vppjni_get_context_id (jm);
1311 M(IP_ADDRESS_DUMP, ip_address_dump);
1312 mp->context = clib_host_to_net_u32 (my_context_id);
1313 mp->sw_if_index = clib_host_to_net_u32(sw_if_index);
1314 mp->is_ipv6 = isIPv6;
1315 jm->is_ipv6 = isIPv6;
1316 S;
1317
1318 /* Use a control ping for synchronization */
1319 {
1320 vl_api_control_ping_t * mp;
1321 M(CONTROL_PING, control_ping);
1322 S;
1323 }
1324
1325 WNR;
1326
1327 return rv;
1328}
1329
Dave Wallaceba474a22016-02-09 23:09:41 -05001330JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_ipv4AddressDump0
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001331(JNIEnv * env, jobject obj, jstring interfaceName)
1332{
1333 vppjni_main_t *jm = &vppjni_main;
1334 jobject returnArray = NULL;
1335 int i;
1336
1337 vppjni_lock (jm, 18);
1338
1339 vec_reset_length(jm->ipv4_addresses);
1340
1341 if (0 != ipAddressDump(env, obj, interfaceName, 0)) {
1342 goto done;
1343 }
1344
1345 u32 count = vec_len(jm->ipv4_addresses);
1346 ipv4_address_t *ipv4_address = jm->ipv4_addresses;
1347
Robert Varga81d99ac2016-01-30 18:30:36 +01001348 jobjectArray ipv4AddressArray = vppIPv4AddressArray(env, count);
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001349
1350 for (i = 0; i < count; i++) {
1351 ipv4_address_t *address = &ipv4_address[i];
1352
1353 jint ip = address->ip;
1354 jbyte prefixLength = address->prefix_length;
1355
Robert Varga81d99ac2016-01-30 18:30:36 +01001356 jobject ipv4AddressObj = vppIPv4AddressObject(env, ip, prefixLength);
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001357
1358 (*env)->SetObjectArrayElement(env, ipv4AddressArray, i, ipv4AddressObj);
1359 }
1360
1361 returnArray = ipv4AddressArray;
1362
1363done:
1364 vppjni_unlock (jm);
1365 return returnArray;
1366}
1367
Dave Wallaceba474a22016-02-09 23:09:41 -05001368JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_ipv6AddressDump0
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001369(JNIEnv * env, jobject obj, jstring interfaceName)
1370{
1371 vppjni_main_t *jm = &vppjni_main;
1372 jobject returnArray = NULL;
1373 int i;
1374
1375 vppjni_lock (jm, 19);
1376
1377 vec_reset_length(jm->ipv6_addresses);
1378
1379 if (0 != ipAddressDump(env, obj, interfaceName, 1)) {
1380 goto done;
1381 }
1382
1383 u32 count = vec_len(jm->ipv6_addresses);
1384 ipv6_address_t *ipv6_address = jm->ipv6_addresses;
1385
Robert Varga81d99ac2016-01-30 18:30:36 +01001386 jobjectArray ipv6AddressArray = vppIPv6AddressArray(env, count);
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001387
1388 for (i = 0; i < count; i++) {
1389 ipv6_address_t *address = &ipv6_address[i];
1390
1391 jbyteArray ip = (*env)->NewByteArray(env, 16);
1392 (*env)->SetByteArrayRegion(env, ip, 0, 16,
1393 (signed char*)address->ip);
1394
1395 jbyte prefixLength = address->prefix_length;
1396
Robert Varga81d99ac2016-01-30 18:30:36 +01001397 jobject ipv6AddressObj = vppIPv6AddressObject(env, ip, prefixLength);
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001398
1399 (*env)->SetObjectArrayElement(env, ipv6AddressArray, i, ipv6AddressObj);
1400 }
1401
1402 returnArray = ipv6AddressArray;
1403
1404done:
1405 vppjni_unlock (jm);
1406 return returnArray;
1407}
1408
1409static void vl_api_ip_address_details_t_handler (vl_api_ip_address_details_t * mp)
1410{
1411 vppjni_main_t * jm = &vppjni_main;
1412
1413 if (!jm->is_ipv6) {
1414 ipv4_address_t *address = 0;
1415 vec_add2(jm->ipv4_addresses, address, 1);
Dave Wallace4afb2812016-01-04 22:14:40 -05001416 memcpy(&address->ip, mp->ip, 4);
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001417 address->prefix_length = mp->prefix_length;
1418 } else {
1419 ipv6_address_t *address = 0;
1420 vec_add2(jm->ipv6_addresses, address, 1);
1421 memcpy(address->ip, mp->ip, 16);
1422 address->prefix_length = mp->prefix_length;
1423 }
1424}
1425
1426#define VXLAN_TUNNEL_INTERFACE_NAME_PREFIX "vxlan_tunnel"
1427
Dave Wallaceba474a22016-02-09 23:09:41 -05001428JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_vxlanTunnelDump0
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001429(JNIEnv * env, jobject obj, jint swIfIndex)
1430{
1431 vppjni_main_t *jm = &vppjni_main;
1432 vl_api_vxlan_tunnel_dump_t * mp;
1433 jobjectArray returnArray = NULL;
1434 u32 my_context_id;
1435 f64 timeout;
1436 int rv = 0;
1437 int i;
1438
1439 vppjni_lock (jm, 22);
1440
1441 vec_reset_length(jm->vxlan_tunnel_details);
1442
1443 my_context_id = vppjni_get_context_id (jm);
1444 M(VXLAN_TUNNEL_DUMP, vxlan_tunnel_dump);
1445 mp->context = clib_host_to_net_u32 (my_context_id);
1446 mp->sw_if_index = clib_host_to_net_u32 (swIfIndex);
1447 S;
1448
1449 /* Use a control ping for synchronization */
1450 {
1451 vl_api_control_ping_t * mp;
1452 M(CONTROL_PING, control_ping);
1453 S;
1454 }
1455
1456 WNR;
1457 if (0 != rv) {
1458 goto done;
1459 }
1460
1461 u32 count = vec_len(jm->vxlan_tunnel_details);
1462
Robert Varga81d99ac2016-01-30 18:30:36 +01001463 jobjectArray vxlanTunnelDetailsArray = vppVxlanTunnelDetailsArray(env, count);
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001464
1465 for (i = 0; i < count; i++) {
1466 vxlan_tunnel_details_t *details = &jm->vxlan_tunnel_details[i];
1467
1468 jint src_address = details->src_address;
1469 jint dst_address = details->dst_address;
1470 jint encap_vrf_id = details->encap_vrf_id;
1471 jint vni = details->vni;
1472 jint decap_next_index = details->decap_next_index;
1473
Robert Varga81d99ac2016-01-30 18:30:36 +01001474 jobject vxlanTunnelDetailsObj = vppVxlanTunnelDetailsObject(env,
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001475 src_address, dst_address, encap_vrf_id, vni, decap_next_index);
1476
1477 (*env)->SetObjectArrayElement(env, vxlanTunnelDetailsArray, i,
1478 vxlanTunnelDetailsObj);
1479 }
1480
1481 returnArray = vxlanTunnelDetailsArray;
1482
1483done:
1484 vppjni_unlock (jm);
1485 return returnArray;
1486}
1487
1488static void vl_api_vxlan_tunnel_details_t_handler
1489(vl_api_vxlan_tunnel_details_t * mp)
1490{
1491 vppjni_main_t * jm = &vppjni_main;
1492 vxlan_tunnel_details_t *tunnel_details;
1493
1494 vec_add2(jm->vxlan_tunnel_details, tunnel_details, 1);
1495 tunnel_details->src_address = ntohl(mp->src_address);
1496 tunnel_details->dst_address = ntohl(mp->dst_address);
1497 tunnel_details->encap_vrf_id = ntohl(mp->encap_vrf_id);
1498 tunnel_details->vni = ntohl(mp->vni);
1499 tunnel_details->decap_next_index = ntohl(mp->decap_next_index);
1500}
1501
Ed Warnickecb9cada2015-12-08 15:45:58 -07001502/* cleanup handler for RX thread */
Robert Varga190efbc2016-02-09 17:16:36 +01001503static void cleanup_rx_thread(void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001504{
Robert Vargad0f92092016-02-10 14:39:57 +01001505 vppjni_main_t * jm = &vppjni_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001506
Robert Vargad0f92092016-02-10 14:39:57 +01001507 vppjni_lock (jm, 99);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001508
Robert Vargad0f92092016-02-10 14:39:57 +01001509 int getEnvStat = (*jm->jvm)->GetEnv(jm->jvm, (void **)&(jm->jenv), JNI_VERSION_1_6);
1510 if (getEnvStat == JNI_EVERSION) {
1511 clib_warning ("Unsupported JNI version\n");
1512 jm->retval = -999;
1513 goto out;
1514 } else if (getEnvStat != JNI_EDETACHED) {
1515 (*jm->jvm)->DetachCurrentThread(jm->jvm);
1516 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001517out:
Robert Vargad0f92092016-02-10 14:39:57 +01001518 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001519}
1520
1521static void
1522vl_api_show_version_reply_t_handler (vl_api_show_version_reply_t * mp)
1523{
Robert Vargad0f92092016-02-10 14:39:57 +01001524 vppjni_main_t * jm = &vppjni_main;
1525 i32 retval = ntohl(mp->retval);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001526
Robert Vargad0f92092016-02-10 14:39:57 +01001527 if (retval >= 0) {
1528 DEBUG_LOG ("show version request succeeded(%d)");
1529 strncpy((char*)jm->program_name, (const char*)mp->program,
1530 sizeof(jm->program_name)-1);
1531 jm->program_name[sizeof(jm->program_name)-1] = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001532
Robert Vargad0f92092016-02-10 14:39:57 +01001533 strncpy((char*)jm->build_directory, (const char*)mp->build_directory,
1534 sizeof(jm->build_directory)-1);
1535 jm->build_directory[sizeof(jm->build_directory)-1] = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001536
Robert Vargad0f92092016-02-10 14:39:57 +01001537 strncpy((char*)jm->version, (const char*)mp->version,
1538 sizeof(jm->version)-1);
1539 jm->version[sizeof(jm->version)-1] = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001540
Robert Vargad0f92092016-02-10 14:39:57 +01001541 strncpy((char*)jm->build_date, (const char*)mp->build_date,
1542 sizeof(jm->build_date)-1);
1543 jm->build_date[sizeof(jm->build_date)-1] = 0;
1544 } else {
1545 clib_error ("show version request failed(%d)", retval);
1546 }
1547 jm->retval = retval;
1548 jm->result_ready = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001549}
1550
1551static void vl_api_want_stats_reply_t_handler (vl_api_want_stats_reply_t * mp)
1552{
Robert Vargad0f92092016-02-10 14:39:57 +01001553 vppjni_main_t * jm = &vppjni_main;
1554 jm->retval = mp->retval; // FIXME: vpp api does not do ntohl on this retval
1555 jm->result_ready = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001556}
1557
1558// control ping needs to be very first thing called
1559// to attach rx thread to java thread
1560static void vl_api_control_ping_reply_t_handler
1561(vl_api_control_ping_reply_t * mp)
1562{
Robert Vargad0f92092016-02-10 14:39:57 +01001563 vppjni_main_t * jm = &vppjni_main;
1564 i32 retval = ntohl(mp->retval);
1565 jm->retval = retval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001566
Robert Vargad0f92092016-02-10 14:39:57 +01001567 // attach to java thread if not attached
1568 int getEnvStat = (*jm->jvm)->GetEnv(jm->jvm, (void **)&(jm->jenv), JNI_VERSION_1_6);
1569 if (getEnvStat == JNI_EDETACHED) {
1570 if ((*jm->jvm)->AttachCurrentThread(jm->jvm, (void **)&(jm->jenv), NULL) != 0) {
1571 clib_warning("Failed to attach thread\n");
1572 jm->retval = -999;
1573 goto out;
1574 }
1575
1576 // workaround as we can't use pthread_cleanup_push
1577 pthread_key_create(&jm->cleanup_rx_thread_key, cleanup_rx_thread);
1578 // destructor is only called if the value of key is non null
1579 pthread_setspecific(jm->cleanup_rx_thread_key, (void *)1);
1580 } else if (getEnvStat == JNI_EVERSION) {
1581 clib_warning ("Unsupported JNI version\n");
1582 jm->retval = -999;
1583 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001584 }
Robert Vargad0f92092016-02-10 14:39:57 +01001585 // jm->jenv is now stable global reference that can be reused (only within RX thread)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001586
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001587#if 0
Robert Vargad0f92092016-02-10 14:39:57 +01001588 // ! callback system removed for now
1589 //
1590 // get issuer msg-id
1591 p = hash_get (jm->ping_hash, context);
1592 if (p != 0) { // ping marks end of some dump call
1593 JNIEnv *env = jm->jenv;
1594 u16 msg_id = (u16)p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001595
Robert Vargad0f92092016-02-10 14:39:57 +01001596 // we will no longer need this
1597 hash_unset (jm->ping_hash, context);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001598
Robert Vargad0f92092016-02-10 14:39:57 +01001599 // get original caller obj
1600 p = hash_get (jm->callback_hash, context);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001601
Robert Vargad0f92092016-02-10 14:39:57 +01001602 if (p == 0) // don't have callback stored
1603 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001604
Robert Vargad0f92092016-02-10 14:39:57 +01001605 jobject obj = (jobject)p[0]; // object that called original call
Ed Warnickecb9cada2015-12-08 15:45:58 -07001606
Robert Vargad0f92092016-02-10 14:39:57 +01001607 switch (msg_id) {
1608 case VL_API_SW_INTERFACE_DUMP:
1609 if (0 != sw_if_dump_call_all_callbacks(obj)) {
1610 goto out2;
1611 }
1612 break;
1613 default:
1614 clib_warning("Unhandled control ping issuer msg-id: %d", msg_id);
1615 goto out2;
1616 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001617 }
Robert Vargad0f92092016-02-10 14:39:57 +01001618out2:
1619 // free the saved obj
1620 hash_unset (jm->callback_hash, context);
1621 // delete global reference
1622 (*env)->DeleteGlobalRef(env, obj);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001623 }
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001624#endif
1625
Ed Warnickecb9cada2015-12-08 15:45:58 -07001626out:
Robert Vargad0f92092016-02-10 14:39:57 +01001627 jm->result_ready = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001628}
1629
1630#define VPPJNI_DEBUG_COUNTERS 0
1631
1632static void vl_api_vnet_interface_counters_t_handler
1633(vl_api_vnet_interface_counters_t *mp)
1634{
Robert Vargad0f92092016-02-10 14:39:57 +01001635 vppjni_main_t *jm = &vppjni_main;
1636 CLIB_UNUSED(char *counter_name);
1637 u32 count, sw_if_index;
1638 int i;
1639 static sw_interface_stats_t empty_stats = {0, };
Ed Warnickecb9cada2015-12-08 15:45:58 -07001640
Robert Vargad0f92092016-02-10 14:39:57 +01001641 vppjni_lock (jm, 12);
1642 count = ntohl (mp->count);
1643 sw_if_index = ntohl (mp->first_sw_if_index);
1644 if (mp->is_combined == 0) {
1645 u64 * vp, v;
1646 vp = (u64 *) mp->data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001647
Robert Vargad0f92092016-02-10 14:39:57 +01001648 for (i = 0; i < count; i++) {
1649 sw_interface_details_t *sw_if = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001650
Robert Vargad0f92092016-02-10 14:39:57 +01001651 v = clib_mem_unaligned (vp, u64);
1652 v = clib_net_to_host_u64 (v);
1653 vp++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001654
Robert Vargad0f92092016-02-10 14:39:57 +01001655 if (sw_if_index < vec_len(jm->sw_if_table))
1656 sw_if = vec_elt_at_index(jm->sw_if_table, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001657
Robert Vargad0f92092016-02-10 14:39:57 +01001658 if (sw_if /* && (sw_if->admin_up_down == 1)*/ && sw_if->interface_name[0] != 0) {
1659 vec_validate_init_empty(jm->sw_if_stats_by_sw_if_index, sw_if_index, empty_stats);
1660 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 -07001661
Robert Vargad0f92092016-02-10 14:39:57 +01001662 s->sw_if_index = sw_if_index;
1663 s->valid = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001664
Robert Vargad0f92092016-02-10 14:39:57 +01001665 switch (mp->vnet_counter_type) {
1666 case VNET_INTERFACE_COUNTER_DROP:
1667 counter_name = "drop";
1668 s->rx.pkts.discard = v;
1669 break;
1670 case VNET_INTERFACE_COUNTER_PUNT:
1671 counter_name = "punt";
1672 s->rx.pkts.unknown_proto = v;
1673 break;
1674 case VNET_INTERFACE_COUNTER_IP4:
1675 counter_name = "ip4";
1676 s->rx.pkts.ip4 = v;
1677 break;
1678 case VNET_INTERFACE_COUNTER_IP6:
1679 counter_name = "ip6";
1680 s->rx.pkts.ip6 = v;
1681 break;
1682 case VNET_INTERFACE_COUNTER_RX_NO_BUF:
1683 counter_name = "rx-no-buf";
1684 s->rx.pkts.fifo_full = v;
1685 break;
1686 case VNET_INTERFACE_COUNTER_RX_MISS:
1687 counter_name = "rx-miss";
1688 s->rx.pkts.miss = v;
1689 break;
1690 case VNET_INTERFACE_COUNTER_RX_ERROR:
1691 counter_name = "rx-error";
1692 s->rx.pkts.error = v;
1693 break;
1694 case VNET_INTERFACE_COUNTER_TX_ERROR:
1695 counter_name = "tx-error (fifo-full)";
1696 s->tx.pkts.fifo_full = v;
1697 break;
1698 default:
1699 counter_name = "bogus";
1700 break;
1701 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001702
1703#if VPPJNI_DEBUG_COUNTERS == 1
Robert Vargad0f92092016-02-10 14:39:57 +01001704 clib_warning ("%s (%d): %s (%lld)\n", sw_if->interface_name, s->sw_if_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001705 counter_name, v);
1706#endif
Robert Vargad0f92092016-02-10 14:39:57 +01001707 }
1708 sw_if_index++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001709 }
Robert Vargad0f92092016-02-10 14:39:57 +01001710 } else {
1711 vlib_counter_t *vp;
1712 u64 packets, bytes;
1713 vp = (vlib_counter_t *) mp->data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001714
Robert Vargad0f92092016-02-10 14:39:57 +01001715 for (i = 0; i < count; i++) {
1716 sw_interface_details_t *sw_if = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001717
Robert Vargad0f92092016-02-10 14:39:57 +01001718 packets = clib_mem_unaligned (&vp->packets, u64);
1719 packets = clib_net_to_host_u64 (packets);
1720 bytes = clib_mem_unaligned (&vp->bytes, u64);
1721 bytes = clib_net_to_host_u64 (bytes);
1722 vp++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001723
Robert Vargad0f92092016-02-10 14:39:57 +01001724 if (sw_if_index < vec_len(jm->sw_if_table))
1725 sw_if = vec_elt_at_index(jm->sw_if_table, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001726
Robert Vargad0f92092016-02-10 14:39:57 +01001727 if (sw_if /* && (sw_if->admin_up_down == 1) */ && sw_if->interface_name[0] != 0) {
1728 vec_validate_init_empty(jm->sw_if_stats_by_sw_if_index, sw_if_index, empty_stats);
1729 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 -07001730
Robert Vargad0f92092016-02-10 14:39:57 +01001731 s->valid = 1;
1732 s->sw_if_index = sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001733
Robert Vargad0f92092016-02-10 14:39:57 +01001734 switch (mp->vnet_counter_type) {
1735 case VNET_INTERFACE_COUNTER_RX:
1736 s->rx.pkts.unicast = packets;
1737 s->rx.octets = bytes;
1738 counter_name = "rx";
1739 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001740
Robert Vargad0f92092016-02-10 14:39:57 +01001741 case VNET_INTERFACE_COUNTER_TX:
1742 s->tx.pkts.unicast = packets;
1743 s->tx.octets = bytes;
1744 counter_name = "tx";
1745 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001746
Robert Vargad0f92092016-02-10 14:39:57 +01001747 default:
1748 counter_name = "bogus";
1749 break;
1750 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001751
1752#if VPPJNI_DEBUG_COUNTERS == 1
Robert Vargad0f92092016-02-10 14:39:57 +01001753 clib_warning ("%s (%d): %s.packets %lld\n",
1754 sw_if->interface_name,
1755 sw_if_index, counter_name, packets);
1756 clib_warning ("%s (%d): %s.bytes %lld\n",
1757 sw_if->interface_name,
1758 sw_if_index, counter_name, bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001759#endif
Robert Vargad0f92092016-02-10 14:39:57 +01001760 }
1761 sw_if_index++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001762 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001763 }
Robert Vargad0f92092016-02-10 14:39:57 +01001764 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001765}
1766
1767jint JNI_OnLoad(JavaVM *vm, void *reserved) {
Robert Vargad0f92092016-02-10 14:39:57 +01001768 vppjni_main_t * jm = &vppjni_main;
1769 JNIEnv* env;
1770 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_6) != JNI_OK) {
1771 return JNI_ERR;
1772 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001773
Robert Vargad0f92092016-02-10 14:39:57 +01001774 if (vppjni_init(env) != 0) {
1775 return JNI_ERR;
1776 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001777
Robert Vargad0f92092016-02-10 14:39:57 +01001778 jm->jvm = vm;
1779 return JNI_VERSION_1_6;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001780}
1781
1782void JNI_OnUnload(JavaVM *vm, void *reserved) {
Robert Vargad0f92092016-02-10 14:39:57 +01001783 vppjni_main_t * jm = &vppjni_main;
1784 JNIEnv* env;
1785 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_6) != JNI_OK) {
1786 return;
1787 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001788
Robert Vargad0f92092016-02-10 14:39:57 +01001789 vppjni_uninit(env);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001790
Robert Vargad0f92092016-02-10 14:39:57 +01001791 jm->jenv = NULL;
1792 jm->jvm = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001793}
1794
1795#define foreach_vpe_api_msg \
1796_(CONTROL_PING_REPLY, control_ping_reply) \
1797_(SW_INTERFACE_DETAILS, sw_interface_details) \
1798_(SHOW_VERSION_REPLY, show_version_reply) \
1799_(WANT_STATS_REPLY, want_stats_reply) \
1800_(VNET_INTERFACE_COUNTERS, vnet_interface_counters) \
1801_(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags) \
1802_(BRIDGE_DOMAIN_DETAILS, bridge_domain_details) \
1803_(BRIDGE_DOMAIN_SW_IF_DETAILS, bridge_domain_sw_if_details) \
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001804_(L2_FIB_TABLE_ENTRY, l2_fib_table_entry) \
1805_(IP_ADDRESS_DETAILS, ip_address_details) \
1806_(VXLAN_TUNNEL_DETAILS, vxlan_tunnel_details)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001807
1808static int connect_to_vpe(char *name)
1809{
Robert Vargad0f92092016-02-10 14:39:57 +01001810 vppjni_main_t * jm = &vppjni_main;
1811 api_main_t * am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001812
Robert Vargad0f92092016-02-10 14:39:57 +01001813 if (vl_client_connect_to_vlib("/vpe-api", name, 32) < 0)
1814 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001815
Robert Vargad0f92092016-02-10 14:39:57 +01001816 jm->my_client_index = am->my_client_index;
1817 jm->vl_input_queue = am->shmem_hdr->vl_input_queue;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001818
Robert Vargad0f92092016-02-10 14:39:57 +01001819#define _(N,n) \
1820 vl_msg_api_set_handlers(VL_API_##N, #n, \
1821 vl_api_##n##_t_handler, \
1822 vl_noop_handler, \
1823 vl_api_##n##_t_endian, \
1824 vl_api_##n##_t_print, \
1825 sizeof(vl_api_##n##_t), 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001826 foreach_vpe_api_msg;
1827#undef _
Ed Warnickecb9cada2015-12-08 15:45:58 -07001828
Robert Vargad0f92092016-02-10 14:39:57 +01001829 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001830}
1831
1832/* Format an IP6 address. */
1833u8 * format_ip6_address (u8 * s, va_list * args)
1834{
Robert Vargad0f92092016-02-10 14:39:57 +01001835 ip6_address_t * a = va_arg (*args, ip6_address_t *);
1836 u32 max_zero_run = 0, this_zero_run = 0;
1837 int max_zero_run_index = -1, this_zero_run_index=0;
1838 int in_zero_run = 0, i;
1839 int last_double_colon = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001840
Robert Vargad0f92092016-02-10 14:39:57 +01001841 /* Ugh, this is a pain. Scan forward looking for runs of 0's */
1842 for (i = 0; i < ARRAY_LEN (a->as_u16); i++) {
1843 if (a->as_u16[i] == 0) {
1844 if (in_zero_run) {
1845 this_zero_run++;
1846 } else {
1847 in_zero_run = 1;
1848 this_zero_run =1;
1849 this_zero_run_index = i;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001850 }
Robert Vargad0f92092016-02-10 14:39:57 +01001851 } else {
1852 if (in_zero_run) {
1853 /* offer to compress the biggest run of > 1 zero */
1854 if (this_zero_run > max_zero_run && this_zero_run > 1) {
1855 max_zero_run_index = this_zero_run_index;
1856 max_zero_run = this_zero_run;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001857 }
1858 }
Robert Vargad0f92092016-02-10 14:39:57 +01001859 in_zero_run = 0;
1860 this_zero_run = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001861 }
1862 }
1863
Robert Vargad0f92092016-02-10 14:39:57 +01001864 if (in_zero_run) {
1865 if (this_zero_run > max_zero_run && this_zero_run > 1) {
1866 max_zero_run_index = this_zero_run_index;
1867 max_zero_run = this_zero_run;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001868 }
1869 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001870
Robert Vargad0f92092016-02-10 14:39:57 +01001871 for (i = 0; i < ARRAY_LEN (a->as_u16); i++) {
1872 if (i == max_zero_run_index) {
1873 s = format (s, "::");
1874 i += max_zero_run - 1;
1875 last_double_colon = 1;
1876 } else {
1877 s = format (s, "%s%x",
1878 (last_double_colon || i == 0) ? "" : ":",
1879 clib_net_to_host_u16 (a->as_u16[i]));
1880 last_double_colon = 0;
1881 }
1882 }
1883
1884 return s;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001885}
1886
1887/* Format an IP4 address. */
1888u8 * format_ip4_address (u8 * s, va_list * args)
1889{
Robert Vargad0f92092016-02-10 14:39:57 +01001890 u8 * a = va_arg (*args, u8 *);
1891 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001892}
1893
1894