Warning be gone.
[vpp.git] / vpp-japi / japi / vppjni.c
1 /*
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  */
15 #include <vnet/vnet.h>
16
17 #define vl_api_version(n,v) static u32 vpe_api_version = (v);
18 #include <api/vpe.api.h>
19 #undef vl_api_version
20
21 #include <jni.h>
22 #include <japi/vppjni.h>
23 #include <japi/vppjni_bridge_domain.h>
24 #include <japi/org_openvpp_vppjapi_vppConn.h>
25 #include <japi/org_openvpp_vppjapi_vppApi.h>
26
27 #include <api/vpe_msg_enum.h>
28 #define vl_typedefs             /* define message structures */
29 #include <api/vpe_all_api_h.h> 
30 #undef vl_typedefs
31
32 #define vl_endianfun 
33 #include <api/vpe_all_api_h.h> 
34 #undef vl_endianfun
35
36 /* instantiate all the print functions we know about */
37 #define vl_print(handle, ...)
38 #define vl_printfun
39 #include <api/vpe_all_api_h.h>
40 #undef vl_printfun
41
42 #define VPPJNI_DEBUG 0
43
44 #if VPPJNI_DEBUG == 1
45   #define DEBUG_LOG(...) clib_warning(__VA_ARGS__)
46 #else
47   #define DEBUG_LOG(...)
48 #endif
49
50 static int connect_to_vpe(char *name);
51
52 /* 
53  * The Java runtime isn't compile w/ -fstack-protector,
54  * so we have to supply missing external references for the
55  * regular vpp libraries. Weak reference in case folks get religion
56  * at a later date...
57  */
58 void __stack_chk_guard (void) __attribute__((weak));
59 void __stack_chk_guard (void) {  }
60
61 void vl_client_add_api_signatures (vl_api_memclnt_create_t *mp) 
62 {
63     /* 
64      * Send the main API signature in slot 0. This bit of code must
65      * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
66      */
67     mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
68 }
69
70 /* Note: non-static, called once to set up the initial intfc table */
71 static int sw_interface_dump (vppjni_main_t * jm)
72 {
73     vl_api_sw_interface_dump_t *mp;
74     f64 timeout;
75     hash_pair_t * p;
76     name_sort_t * nses = 0, * ns;
77     sw_interface_subif_t * sub = NULL;
78
79     /* Toss the old name table */
80     hash_foreach_pair (p, jm->sw_if_index_by_interface_name, 
81     ({
82         vec_add2 (nses, ns, 1);
83         ns->name = (u8 *)(p->key);
84         ns->value = (u32) p->value[0];
85     }));
86
87     hash_free (jm->sw_if_index_by_interface_name);
88
89     vec_foreach (ns, nses)
90         vec_free (ns->name);
91
92     vec_free (nses);
93
94     vec_foreach (sub, jm->sw_if_subif_table) {
95         vec_free (sub->interface_name);
96     }
97     vec_free (jm->sw_if_subif_table);
98
99     /* recreate the interface name hash table */
100     jm->sw_if_index_by_interface_name 
101       = hash_create_string (0, sizeof(uword));
102
103     /* Get list of ethernets */
104     M(SW_INTERFACE_DUMP, sw_interface_dump);
105     mp->name_filter_valid = 1;
106     strncpy ((char *) mp->name_filter, "Ether", sizeof(mp->name_filter-1)); 
107     S;
108
109     /* and local / loopback interfaces */
110     M(SW_INTERFACE_DUMP, sw_interface_dump);
111     mp->name_filter_valid = 1;
112     strncpy ((char *) mp->name_filter, "lo", sizeof(mp->name_filter-1)); 
113     S;
114
115     /* and vxlan tunnel interfaces */
116     M(SW_INTERFACE_DUMP, sw_interface_dump);
117     mp->name_filter_valid = 1;
118     strncpy ((char *) mp->name_filter, "vxlan", sizeof(mp->name_filter-1)); 
119     S;
120
121     /* and tap tunnel interfaces */
122     M(SW_INTERFACE_DUMP, sw_interface_dump);
123     mp->name_filter_valid = 1;
124     strncpy ((char *) mp->name_filter, "tap", sizeof(mp->name_filter-1)); 
125     S;
126
127     /* and l2tpv3 tunnel interfaces */
128     M(SW_INTERFACE_DUMP, sw_interface_dump);
129     mp->name_filter_valid = 1;
130     strncpy ((char *) mp->name_filter, "l2tpv3_tunnel", 
131              sizeof(mp->name_filter-1));
132     S;
133
134     /* Use a control ping for synchronization */
135     {
136         vl_api_control_ping_t * mp;
137         M(CONTROL_PING, control_ping);
138         S;
139     }
140     W;
141 }
142
143 JNIEXPORT jobject JNICALL Java_org_openvpp_vppjapi_vppConn_getVppVersion
144   (JNIEnv *env, jobject obj)
145 {
146   vppjni_main_t * jm = &vppjni_main;
147   jmethodID constr;
148   // TODO: cache this
149   jclass cls = (*env)->FindClass(env, "org/openvpp/vppjapi/vppVersion");
150   if ((*env)->ExceptionCheck(env)) {
151       (*env)->ExceptionDescribe(env);
152       return NULL;
153   }
154
155   constr = (*env)->GetMethodID(env, cls, "<init>", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
156   if ((*env)->ExceptionCheck(env)) {
157       (*env)->ExceptionDescribe(env);
158       return NULL;
159   }
160
161   vppjni_lock (jm, 11);
162   jstring progName = (*env)->NewStringUTF(env, (char *)jm->program_name);
163   jstring buildDir = (*env)->NewStringUTF(env, (char *)jm->build_directory);
164   jstring version = (*env)->NewStringUTF(env, (char *)jm->version);
165   jstring buildDate = (*env)->NewStringUTF(env, (char *)jm->build_date);
166   vppjni_unlock (jm);
167
168   return (*env)->NewObject(env, cls, constr, progName, buildDir, version, buildDate);
169 }
170
171 static int jm_show_version (vppjni_main_t *jm)
172 {
173   int rv;
174   vl_api_show_version_t *mp;
175   f64 timeout;
176
177   vppjni_lock (jm, 10);
178   M(SHOW_VERSION, show_version);
179
180   S;
181   vppjni_unlock (jm);
182   WNR;
183   return rv;
184 }
185
186 static int jm_stats_enable_disable (vppjni_main_t *jm, u8 enable)
187 {
188   vl_api_want_stats_t * mp;
189   f64 timeout;
190   int rv;
191
192   vppjni_lock (jm, 13);
193
194   M(WANT_STATS, want_stats);
195
196   mp->enable_disable = enable;
197
198   S;
199   vppjni_unlock (jm);
200   WNR;
201
202   // already subscribed / already disabled (it's ok)
203   if (rv == -2 || rv == -3)
204       rv = 0;
205   return rv;
206 }
207
208 JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_setInterfaceDescription
209   (JNIEnv *env, jobject obj, jstring ifName, jstring ifDesc)
210 {
211     int rv = 0;
212     vppjni_main_t * jm = &vppjni_main;
213     uword * p;
214     u32 sw_if_index = ~0;
215     sw_if_config_t *cfg;
216
217     const char *if_name_str = (*env)->GetStringUTFChars (env, ifName, 0);
218     const char *if_desc_str = (*env)->GetStringUTFChars (env, ifDesc, 0);
219
220     vppjni_lock (jm, 23);
221
222     p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name_str);
223     if (p == 0) {
224         rv = -1;
225         goto out;
226     }
227     sw_if_index = (jint) p[0];
228
229     u8 *if_desc = 0;
230     vec_validate_init_c_string (if_desc, if_desc_str, strlen(if_desc_str));
231     (*env)->ReleaseStringUTFChars (env, ifDesc, if_desc_str);
232
233     p = hash_get (jm->sw_if_config_by_sw_if_index, sw_if_index);
234     if (p != 0) {
235         cfg = (sw_if_config_t *) (p[0]);
236         if (cfg->desc)
237             vec_free(cfg->desc);
238     }
239     else {
240         cfg = (sw_if_config_t *) clib_mem_alloc(sizeof(sw_if_config_t));
241         hash_set (jm->sw_if_config_by_sw_if_index, sw_if_index, cfg);
242     }
243
244     cfg->desc = if_desc;
245
246 out:
247     (*env)->ReleaseStringUTFChars (env, ifName, if_name_str);
248     vppjni_unlock (jm);
249     return rv;
250 }
251
252 JNIEXPORT jstring JNICALL Java_org_openvpp_vppjapi_vppConn_getInterfaceDescription
253 (JNIEnv * env, jobject obj, jstring ifName)
254 {
255     vppjni_main_t * jm = &vppjni_main;
256     u32 sw_if_index = ~0;
257     uword * p;
258     const char *if_name_str = (*env)->GetStringUTFChars (env, ifName, 0);
259     jstring ifDesc = NULL;
260
261     vppjni_lock (jm, 24);
262     p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name_str);
263     if (p == 0)
264         goto out;
265
266     sw_if_index = (jint) p[0];
267
268     p = hash_get (jm->sw_if_config_by_sw_if_index, sw_if_index);
269     if (p == 0)
270         goto out;
271
272     sw_if_config_t *cfg = (sw_if_config_t *) (p[0]);
273     u8 * s = format (0, "%s%c", cfg->desc, 0);
274     ifDesc = (*env)->NewStringUTF(env, (char *)s);
275
276 out:
277     vppjni_unlock (jm);
278
279     return ifDesc;
280 }
281
282 JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_clientConnect
283   (JNIEnv *env, jobject obj, jstring clientName)
284 {
285   int rv;
286   const char *client_name;
287   void vl_msg_reply_handler_hookup(void);
288   vppjni_main_t * jm = &vppjni_main;
289   api_main_t * am = &api_main;
290   u8 * heap;
291   mheap_t * h; 
292   f64 timeout;
293       
294   /* 
295    * Bail out now if we're not running as root
296    */
297   if (geteuid() != 0)
298     return -1;
299
300   if (jm->is_connected)
301       return -2;
302
303   if (jm->heap == 0)
304     clib_mem_init (0, 128<<20);
305
306   heap = clib_mem_get_per_cpu_heap();
307   h = mheap_header (heap);
308
309   client_name = (*env)->GetStringUTFChars (env, clientName, 0);
310
311   clib_time_init (&jm->clib_time);
312
313   rv = connect_to_vpe ((char *) client_name);
314
315   if (rv < 0)
316     clib_warning ("connection failed, rv %d", rv);
317
318   (*env)->ReleaseStringUTFChars (env, clientName, client_name);
319
320   if (rv == 0)
321     {
322       vl_msg_reply_handler_hookup ();
323       jm->is_connected = 1;
324       /* make the main heap thread-safe */
325       h->flags |= MHEAP_FLAG_THREAD_SAFE;
326
327       jm->reply_hash = hash_create (0, sizeof (uword));
328       //jm->callback_hash = hash_create (0, sizeof (uword));
329       //jm->ping_hash = hash_create (0, sizeof (uword));
330       jm->api_main = am;
331       vjbd_main_init(&jm->vjbd_main);
332       jm->sw_if_index_by_interface_name = 
333         hash_create_string (0, sizeof (uword));
334       jm->sw_if_config_by_sw_if_index =
335         hash_create (0, sizeof (uword));
336
337       {
338         // call control ping first to attach rx thread to java thread
339         vl_api_control_ping_t * mp;
340         M(CONTROL_PING, control_ping);
341         S;
342         WNR;
343
344         if (rv != 0) {
345             clib_warning ("first control ping failed: %d", rv);
346         }
347       }
348       rv = jm_show_version(jm);
349       if (rv != 0)
350           clib_warning ("unable to retrieve vpp version (rv: %d)", rv);
351       rv = sw_interface_dump(jm);
352       if (rv != 0)
353           clib_warning ("unable to retrieve interface list (rv: %d)", rv);
354       rv = jm_stats_enable_disable(jm, 1);
355       if (rv != 0)
356           clib_warning ("unable to subscribe to stats (rv: %d)", rv);
357     }
358   DEBUG_LOG ("clientConnect result: %d", rv);
359
360   return rv;
361 }
362
363 JNIEXPORT void JNICALL Java_org_openvpp_vppjapi_vppConn_clientDisconnect
364   (JNIEnv *env, jobject obj)
365 {
366   u8 *save_heap;
367   vppjni_main_t * jm = &vppjni_main;
368   vl_client_disconnect_from_vlib();
369   
370   save_heap = jm->heap;
371   memset (jm, 0, sizeof (*jm));
372   jm->heap = save_heap;
373 }
374
375 void vl_api_generic_reply_handler (vl_api_generic_reply_t *mp)
376 {
377   api_main_t * am = &api_main;
378   u16 msg_id = clib_net_to_host_u16 (mp->_vl_msg_id);
379   trace_cfg_t *cfgp;
380   i32 retval = clib_net_to_host_u32 (mp->retval);
381   int total_bytes = sizeof(mp);
382   vppjni_main_t * jm = &vppjni_main;
383   u8 * saved_reply = 0;
384   u32 context = clib_host_to_net_u32 (mp->context);
385     
386   cfgp = am->api_trace_cfg + msg_id;
387
388   if (!cfgp) 
389     clib_warning ("msg id %d: no trace configuration\n", msg_id);
390   else
391     total_bytes = cfgp->size;
392
393   jm->context_id_received = context;
394
395   DEBUG_LOG ("Received generic reply for msg id %d", msg_id);
396
397   /* A generic reply, successful, we're done */
398   if (retval >= 0 && total_bytes == sizeof(*mp))
399     return;
400
401   /* Save the reply */
402   vec_validate (saved_reply, total_bytes - 1);
403   memcpy (saved_reply, mp, total_bytes);
404
405   vppjni_lock (jm, 2);
406   hash_set (jm->reply_hash, context, saved_reply);
407   jm->saved_reply_count ++;
408   vppjni_unlock (jm);
409 }
410
411 JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_getRetval
412 (JNIEnv * env, jobject obj, jint context, jint release)
413 {
414   vppjni_main_t * jm = &vppjni_main;
415   vl_api_generic_reply_t * mp;
416   uword * p;
417   int rv = 0;
418
419   /* Dunno yet? */
420   if (context > jm->context_id_received)
421     return (VNET_API_ERROR_RESPONSE_NOT_READY);
422
423   vppjni_lock (jm, 1);
424   p = hash_get (jm->reply_hash, context);
425
426   /* 
427    * Two cases: a generic "yes" reply - won't be in the hash table
428    * or "no", or "more data" which will be in the table.
429    */
430   if (p == 0)
431     goto out;
432       
433   mp = (vl_api_generic_reply_t *) (p[0]);
434   rv = clib_net_to_host_u32 (mp->retval);
435
436   if (release)
437     {
438       u8 * free_me = (u8 *) mp;
439       vec_free (free_me);
440       hash_unset (jm->reply_hash, context);
441       jm->saved_reply_count --;
442     }
443
444 out:
445   vppjni_unlock (jm);
446   return (rv);
447 }
448
449 static int
450 name_sort_cmp (void * a1, void * a2)
451 {
452   name_sort_t * n1 = a1;
453   name_sort_t * n2 = a2;
454
455   return strcmp ((char *)n1->name, (char *)n2->name);
456 }
457
458 JNIEXPORT jstring JNICALL Java_org_openvpp_vppjapi_vppConn_getInterfaceList
459   (JNIEnv * env, jobject obj, jstring name_filter)
460 {
461   vppjni_main_t * jm = &vppjni_main;
462   jstring rv;
463   hash_pair_t * p;
464   name_sort_t * nses = 0, * ns;
465   const char *this_name;
466   const char * nf = (*env)->GetStringUTFChars (env, name_filter, NULL);
467   u8 * s = 0;
468   char *strcasestr (const char *, const char *);
469
470   vppjni_lock (jm, 4);
471   
472   hash_foreach_pair (p, jm->sw_if_index_by_interface_name, 
473     ({
474       this_name = (const char *)(p->key);
475       if (strlen (nf) == 0 || strcasestr (this_name, nf))
476         {
477           vec_add2 (nses, ns, 1);
478           ns->name = (u8 *)(p->key);
479           ns->value = (u32) p->value[0];
480         }
481     }));
482
483   vec_sort_with_function (nses, name_sort_cmp);
484
485   vec_foreach (ns, nses)
486     s = format (s, "%s: %d, ", ns->name, ns->value);
487
488   _vec_len (s) = vec_len (s) - 2;
489   vec_terminate_c_string (s);
490   vppjni_unlock (jm);
491
492   vec_free (nses);
493
494   (*env)->ReleaseStringUTFChars (env, name_filter, nf);
495
496   rv = (*env)->NewStringUTF (env, (char *) s);
497   vec_free (s);
498   
499   return rv;
500 }
501
502 JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_swIfIndexFromName
503   (JNIEnv * env, jobject obj, jstring interfaceName)
504 {
505   vppjni_main_t * jm = &vppjni_main;
506   jint rv = -1;
507   const char * if_name = (*env)->GetStringUTFChars (env, interfaceName, NULL);
508   uword * p;
509
510   vppjni_lock (jm, 5);
511
512   p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name);
513
514   if (p != 0)
515       rv = (jint) p[0];
516
517   vppjni_unlock (jm);
518   
519   (*env)->ReleaseStringUTFChars (env, interfaceName, if_name);
520
521   return rv;
522 }
523
524 JNIEXPORT jobject JNICALL Java_org_openvpp_vppjapi_vppConn_getInterfaceCounters
525 (JNIEnv * env, jobject obj, jint swIfIndex)
526 {
527     vppjni_main_t * jm = &vppjni_main;
528     sw_interface_stats_t *s;
529     u32 sw_if_index = swIfIndex;
530     jmethodID constr;
531     jobject result = NULL;
532
533     // TODO: cache this
534     jclass cls = (*env)->FindClass(env, "org/openvpp/vppjapi/vppInterfaceCounters");
535     if ((*env)->ExceptionCheck(env)) {
536         (*env)->ExceptionDescribe(env);
537         return NULL;
538     }
539
540     constr = (*env)->GetMethodID(env, cls, "<init>", "(JJJJJJJJJJJJJJJJJJJJJJ)V");
541     if ((*env)->ExceptionCheck(env)) {
542         (*env)->ExceptionDescribe(env);
543         return NULL;
544     }
545
546     vppjni_lock (jm, 16);
547
548     if (sw_if_index >= vec_len(jm->sw_if_stats_by_sw_if_index)) {
549         goto out;
550     }
551     s = &jm->sw_if_stats_by_sw_if_index[sw_if_index];
552     if (!s->valid) {
553         goto out;
554     }
555
556     result = (*env)->NewObject(env, cls, constr,
557             s->rx.octets, s->rx.pkts.ip4, s->rx.pkts.ip6, s->rx.pkts.unicast,
558             s->rx.pkts.multicast, s->rx.pkts.broadcast, s->rx.pkts.discard,
559             s->rx.pkts.fifo_full, s->rx.pkts.error, s->rx.pkts.unknown_proto,
560             s->rx.pkts.miss,
561             s->tx.octets, s->tx.pkts.ip4, s->tx.pkts.ip6, s->tx.pkts.unicast,
562             s->tx.pkts.multicast, s->tx.pkts.broadcast, s->tx.pkts.discard,
563             s->tx.pkts.fifo_full, s->tx.pkts.error, s->tx.pkts.unknown_proto,
564             s->tx.pkts.miss);
565
566 out:
567     vppjni_unlock (jm);
568     return result;
569 }
570
571 JNIEXPORT jstring JNICALL Java_org_openvpp_vppjapi_vppConn_interfaceNameFromSwIfIndex
572 (JNIEnv * env, jobject obj, jint swIfIndex)
573 {
574     vppjni_main_t * jm = &vppjni_main;
575     sw_interface_details_t *sw_if_details;
576     u32 sw_if_index;
577     jstring ifname = NULL;
578
579     vppjni_lock (jm, 8);
580
581     sw_if_index = swIfIndex;
582
583     if (sw_if_index >= vec_len(jm->sw_if_table)) {
584         goto out;
585     }
586     sw_if_details = &jm->sw_if_table[sw_if_index];
587     if (!sw_if_details->valid) {
588         goto out;
589     }
590
591     u8 * s = format (0, "%s%c", sw_if_details->interface_name, 0);
592     ifname = (*env)->NewStringUTF(env, (char *)s);
593
594 out:
595     vppjni_unlock (jm);
596
597     return ifname;
598 }
599
600 JNIEXPORT void JNICALL Java_org_openvpp_vppjapi_vppConn_clearInterfaceTable
601 (JNIEnv * env, jobject obj)
602 {
603     vppjni_main_t * jm = &vppjni_main;
604
605     vppjni_lock (jm, 21);
606
607     vec_reset_length(jm->sw_if_table);
608
609     vppjni_unlock (jm);
610 }
611
612 static jobjectArray sw_if_dump_get_interfaces ();
613
614 JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_swInterfaceDump
615 (JNIEnv * env, jobject obj, jbyte name_filter_valid, jbyteArray name_filter)
616 {
617     vppjni_main_t *jm = &vppjni_main;
618     f64 timeout;
619     vl_api_sw_interface_dump_t * mp;
620     u32 my_context_id;
621     int rv;
622     rv = vppjni_sanity_check (jm);
623     if (rv) {
624         clib_warning("swInterfaceDump sanity_check rv = %d", rv);
625         return NULL;
626     }
627
628     vppjni_lock (jm, 7);
629     my_context_id = vppjni_get_context_id (jm);
630     jbyte * name_filterP = (*env)->GetByteArrayElements (env, name_filter, NULL);
631     int cnt = (*env)->GetArrayLength (env, name_filter);
632
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
640     memcpy ((char *) mp->name_filter, name_filterP, cnt);
641     (*env)->ReleaseByteArrayElements (env, name_filter, name_filterP, 0);
642
643     DEBUG_LOG ("interface filter (%d, %s, len: %d)", mp->name_filter_valid, (char *)mp->name_filter, cnt);
644
645     jm->collect_indices = 1;
646
647     S;
648     {
649         // now send control ping so we know when it ends
650         vl_api_control_ping_t * mp;
651         M(CONTROL_PING, control_ping);
652         mp->context = clib_host_to_net_u32 (my_context_id);
653
654         S;
655     }
656     vppjni_unlock (jm);
657     WNR;
658
659     vppjni_lock (jm, 7);
660     jobjectArray result = sw_if_dump_get_interfaces(env);
661     vppjni_unlock (jm);
662     return result;
663 }
664
665 static jobjectArray sw_if_dump_get_interfaces (JNIEnv * env)
666 {
667     vppjni_main_t * jm = &vppjni_main;
668     sw_interface_details_t *sw_if_details;
669     u32 i;
670
671     int len = vec_len(jm->sw_if_dump_if_indices);
672     jmethodID jmtdIfDetails = jm->jmtdIfDetails;
673
674     jobjectArray ifArray = (*env)->NewObjectArray(env, len, jm->jcls, NULL);
675
676     for (i = 0; i < len; i++) {
677         u32 sw_if_index = jm->sw_if_dump_if_indices[i];
678         ASSERT(sw_if_index < vec_len(jm->sw_if_table));
679         sw_if_details = &jm->sw_if_table[sw_if_index];
680         ASSERT(sw_if_details->valid);
681
682         u8 * s = format (0, "%s%c", sw_if_details->interface_name, 0);
683
684         jstring ifname = (*env)->NewStringUTF(env, (char *)s);
685         jint ifIndex = sw_if_details->sw_if_index;
686         jint supIfIndex = sw_if_details->sup_sw_if_index;
687         jbyteArray physAddr = (*env)->NewByteArray(env,
688                 sw_if_details->l2_address_length);
689         (*env)->SetByteArrayRegion(env, physAddr, 0,
690                 sw_if_details->l2_address_length,
691                 (signed char*)sw_if_details->l2_address);
692         jint subId = sw_if_details->sub_id;
693         jint subOuterVlanId = sw_if_details->sub_outer_vlan_id;
694         jint subInnerVlanId = sw_if_details->sub_inner_vlan_id;
695         jint vtrOp = sw_if_details->vtr_op;
696         jint vtrPushDot1q = sw_if_details->vtr_push_dot1q;
697         jint vtrTag1 = sw_if_details->vtr_tag1;
698         jint vtrTag2 = sw_if_details->vtr_tag2;
699
700         jbyte adminUpDown = sw_if_details->admin_up_down;
701         jbyte linkUpDown = sw_if_details->link_up_down;
702         jbyte linkDuplex = sw_if_details->link_duplex;
703         jbyte linkSpeed = sw_if_details->link_speed;
704         jbyte subDot1ad = sw_if_details->sub_dot1ad;
705         jbyte subNumberOfTags = sw_if_details->sub_number_of_tags;
706         jbyte subExactMatch = sw_if_details->sub_exact_match;
707         jbyte subDefault = sw_if_details->sub_default;
708         jbyte subOuterVlanIdAny = sw_if_details->sub_outer_vlan_id_any;
709         jbyte subInnerVlanIdAny = sw_if_details->sub_inner_vlan_id_any;
710
711         jobject ifObj = (*env)->NewObject(env, jm->jcls, jmtdIfDetails,
712                 ifIndex, ifname,
713                 supIfIndex, physAddr, adminUpDown, linkUpDown,
714                 linkDuplex, linkSpeed, subId, subDot1ad,
715                 subNumberOfTags, subOuterVlanId, subInnerVlanId,
716                 subExactMatch, subDefault, subOuterVlanIdAny,
717                 subInnerVlanIdAny, vtrOp, vtrPushDot1q, vtrTag1, vtrTag2);
718         (*env)->SetObjectArrayElement(env, ifArray, i, ifObj);
719     }
720
721     jm->collect_indices = 0;
722     vec_reset_length(jm->sw_if_dump_if_indices);
723     return ifArray;
724 }
725
726 JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_findOrAddBridgeDomainId
727   (JNIEnv * env, jobject obj, jstring bridgeDomain)
728 {
729   vppjni_main_t * jm = &vppjni_main;
730   static u8 * bd_name = 0;
731   jint rv = -1;
732   const char * bdName = (*env)->GetStringUTFChars (env, bridgeDomain, NULL);
733
734   vec_validate_init_c_string (bd_name, bdName, strlen(bdName));
735   (*env)->ReleaseStringUTFChars (env, bridgeDomain, bdName);
736
737   vppjni_lock (jm, 6);
738   rv = (jint)vjbd_find_or_add_bd (&jm->vjbd_main, bd_name);
739   vppjni_unlock (jm);
740   
741   _vec_len(bd_name) = 0;
742   return rv;
743 }
744
745 JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_bridgeDomainIdFromName
746   (JNIEnv * env, jobject obj, jstring bridgeDomain)
747 {
748   vppjni_main_t * jm = &vppjni_main;
749   static u8 * bd_name = 0;
750   jint rv = -1;
751   const char * bdName = (*env)->GetStringUTFChars (env, bridgeDomain, NULL);
752
753   vec_validate_init_c_string (bd_name, bdName, strlen(bdName));
754   (*env)->ReleaseStringUTFChars (env, bridgeDomain, bdName);
755
756   vppjni_lock (jm, 20);
757   rv = (jint)vjbd_id_from_name(&jm->vjbd_main, (u8 *)bd_name);
758   vppjni_unlock (jm);
759
760   _vec_len(bd_name) = 0;
761
762   return rv;
763 }
764
765 JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_bridgeDomainIdFromInterfaceName
766   (JNIEnv * env, jobject obj, jstring interfaceName)
767 {
768   vppjni_main_t * jm = &vppjni_main;
769   vjbd_main_t * bdm = &jm->vjbd_main;
770   u32 sw_if_index;
771   jint rv = -1;
772   const char * if_name;
773   uword * p;
774
775   if_name = (*env)->GetStringUTFChars (env, interfaceName, NULL);
776
777   vppjni_lock (jm, 14);
778
779   p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name);
780
781   if (p != 0) {
782       sw_if_index = (jint) p[0];
783       p = hash_get (bdm->bd_id_by_sw_if_index, sw_if_index);
784       if (p != 0) {
785           rv = (jint) p[0];
786       }
787   }
788
789   vppjni_unlock (jm);
790
791   (*env)->ReleaseStringUTFChars (env, interfaceName, if_name);
792
793   return rv;
794 }
795
796 /* 
797  * Special-case: build the interface table, maintain
798  * the next loopback sw_if_index vbl.
799  */
800 static void vl_api_sw_interface_details_t_handler
801 (vl_api_sw_interface_details_t * mp)
802 {
803   vppjni_main_t * jm = &vppjni_main;
804   static sw_interface_details_t empty_sw_if_details = {0,};
805   sw_interface_details_t *sw_if_details;
806   u32 sw_if_index;
807
808   vppjni_lock (jm, 1);
809
810   sw_if_index = ntohl (mp->sw_if_index);
811
812   u8 * s = format (0, "%s%c", mp->interface_name, 0);
813
814   if (jm->collect_indices) {
815       u32 pos = vec_len(jm->sw_if_dump_if_indices);
816       vec_validate(jm->sw_if_dump_if_indices, pos);
817       jm->sw_if_dump_if_indices[pos] = sw_if_index;
818   }
819
820   vec_validate_init_empty(jm->sw_if_table, sw_if_index, empty_sw_if_details);
821   sw_if_details = &jm->sw_if_table[sw_if_index];
822   sw_if_details->valid = 1;
823
824   snprintf((char *)sw_if_details->interface_name,
825           sizeof(sw_if_details->interface_name), "%s", (char *)s);
826   sw_if_details->sw_if_index = sw_if_index;
827   sw_if_details->sup_sw_if_index = ntohl(mp->sup_sw_if_index);
828   sw_if_details->l2_address_length = ntohl (mp->l2_address_length);
829   ASSERT(sw_if_details->l2_address_length <= sizeof(sw_if_details->l2_address));
830   memcpy(sw_if_details->l2_address, mp->l2_address,
831           sw_if_details->l2_address_length);
832   sw_if_details->sub_id = ntohl (mp->sub_id);
833   sw_if_details->sub_outer_vlan_id = ntohl (mp->sub_outer_vlan_id);
834   sw_if_details->sub_inner_vlan_id = ntohl (mp->sub_inner_vlan_id);
835   sw_if_details->vtr_op = ntohl (mp->vtr_op);
836   sw_if_details->vtr_push_dot1q = ntohl (mp->vtr_push_dot1q);
837   sw_if_details->vtr_tag1 = ntohl (mp->vtr_tag1);
838   sw_if_details->vtr_tag2 = ntohl (mp->vtr_tag2);
839
840   sw_if_details->admin_up_down = mp->admin_up_down;
841   sw_if_details->link_up_down = mp->link_up_down;
842   sw_if_details->link_duplex = mp->link_duplex;
843   sw_if_details->link_speed = mp->link_speed;
844   sw_if_details->sub_dot1ad = mp->sub_dot1ad;
845   sw_if_details->sub_number_of_tags = mp->sub_number_of_tags;
846   sw_if_details->sub_exact_match = mp->sub_exact_match;
847   sw_if_details->sub_default = mp->sub_default;
848   sw_if_details->sub_outer_vlan_id_any = mp->sub_outer_vlan_id_any;
849   sw_if_details->sub_inner_vlan_id_any = mp->sub_inner_vlan_id_any;
850
851   hash_set_mem (jm->sw_if_index_by_interface_name, s, sw_if_index);
852   DEBUG_LOG ("Got interface %s", (char *)s);
853
854   /* In sub interface case, fill the sub interface table entry */
855   if (mp->sw_if_index != mp->sup_sw_if_index) {
856     sw_interface_subif_t * sub = NULL;
857
858     vec_add2(jm->sw_if_subif_table, sub, 1);
859
860     vec_validate(sub->interface_name, strlen((char *)s) + 1);
861     strncpy((char *)sub->interface_name, (char *)s,
862             vec_len(sub->interface_name));
863     sub->sw_if_index = ntohl(mp->sw_if_index);
864     sub->sub_id = ntohl(mp->sub_id);
865
866     sub->sub_dot1ad = mp->sub_dot1ad;
867     sub->sub_number_of_tags = mp->sub_number_of_tags;
868     sub->sub_outer_vlan_id = ntohs(mp->sub_outer_vlan_id);
869     sub->sub_inner_vlan_id = ntohs(mp->sub_inner_vlan_id);
870     sub->sub_exact_match = mp->sub_exact_match;
871     sub->sub_default = mp->sub_default;
872     sub->sub_outer_vlan_id_any = mp->sub_outer_vlan_id_any;
873     sub->sub_inner_vlan_id_any = mp->sub_inner_vlan_id_any;
874
875     /* vlan tag rewrite */
876     sub->vtr_op = ntohl(mp->vtr_op);
877     sub->vtr_push_dot1q = ntohl(mp->vtr_push_dot1q);
878     sub->vtr_tag1 = ntohl(mp->vtr_tag1);
879     sub->vtr_tag2 = ntohl(mp->vtr_tag2);
880   }
881   vppjni_unlock (jm);
882 }
883
884 static void vl_api_sw_interface_set_flags_t_handler
885 (vl_api_sw_interface_set_flags_t * mp)
886 {
887   /* $$$ nothing for the moment */
888 }
889
890 static jintArray create_array_of_bd_ids(JNIEnv * env, jint bd_id)
891 {
892     vppjni_main_t *jm = &vppjni_main;
893     vjbd_main_t * bdm = &jm->vjbd_main;
894     u32 *buf = NULL;
895     u32 i;
896
897     if (bd_id != ~0) {
898         vec_add1(buf, bd_id);
899     } else {
900         for (i = 0; i < vec_len(bdm->bd_oper); i++) {
901             u32 bd_id = bdm->bd_oper[i].bd_id;
902             vec_add1(buf, bd_id);
903         }
904     }
905
906     jintArray bdidArray = (*env)->NewIntArray(env, vec_len(buf));
907
908     (*env)->SetIntArrayRegion(env, bdidArray, 0, vec_len(buf), (int*)buf);
909
910     return bdidArray;
911 }
912
913 static void bridge_domain_oper_free(void)
914 {
915     vppjni_main_t *jm = &vppjni_main;
916     vjbd_main_t *bdm = &jm->vjbd_main;
917     u32 i;
918
919     for (i = 0; i < vec_len(bdm->bd_oper); i++) {
920         vec_free(bdm->bd_oper->l2fib_oper);
921     }
922     vec_reset_length(bdm->bd_oper);
923     hash_free(bdm->bd_id_by_sw_if_index);
924     hash_free(bdm->oper_bd_index_by_bd_id);
925 }
926
927 JNIEXPORT jintArray JNICALL Java_org_openvpp_vppjapi_vppConn_bridgeDomainDump
928 (JNIEnv * env, jobject obj, jint bd_id)
929 {
930     vppjni_main_t *jm = &vppjni_main;
931     vl_api_bridge_domain_dump_t * mp;
932     u32 my_context_id;
933     f64 timeout;
934     int rv;
935     rv = vppjni_sanity_check (jm);
936     if (rv) return NULL;
937
938     vppjni_lock (jm, 15);
939
940     if (~0 == bd_id) {
941         bridge_domain_oper_free();
942     }
943
944     my_context_id = vppjni_get_context_id (jm);
945     M(BRIDGE_DOMAIN_DUMP, bridge_domain_dump);
946     mp->context = clib_host_to_net_u32 (my_context_id);
947     mp->bd_id = clib_host_to_net_u32(bd_id);
948     S;
949
950     /* Use a control ping for synchronization */
951     {
952         vl_api_control_ping_t * mp;
953         M(CONTROL_PING, control_ping);
954         S;
955     }
956
957     WNR;
958     if (0 != rv) {
959         return NULL;
960     }
961
962     jintArray ret = create_array_of_bd_ids(env, bd_id);
963
964     vppjni_unlock (jm);
965
966     return ret;
967 }
968
969 static void
970 vl_api_bridge_domain_details_t_handler (vl_api_bridge_domain_details_t * mp)
971 {
972   vppjni_main_t *jm = &vppjni_main;
973   vjbd_main_t * bdm = &jm->vjbd_main;
974   vjbd_oper_t * bd_oper;
975   u32 bd_id, bd_index;
976
977   bd_id = ntohl (mp->bd_id);
978
979   bd_index = vec_len(bdm->bd_oper);
980   vec_validate (bdm->bd_oper, bd_index);
981   bd_oper = vec_elt_at_index(bdm->bd_oper, bd_index);
982
983   hash_set(bdm->oper_bd_index_by_bd_id, bd_id, bd_index);
984
985   bd_oper->bd_id = bd_id;
986   bd_oper->flood = mp->flood != 0;
987   bd_oper->forward = mp->forward != 0;
988   bd_oper->learn =  mp->learn != 0;
989   bd_oper->uu_flood = mp->uu_flood != 0;
990   bd_oper->arp_term = mp->arp_term != 0;
991   bd_oper->bvi_sw_if_index = ntohl (mp->bvi_sw_if_index);
992   bd_oper->n_sw_ifs = ntohl (mp->n_sw_ifs);
993
994   bd_oper->bd_sw_if_oper = 0;
995 }
996
997 static void
998 vl_api_bridge_domain_sw_if_details_t_handler
999 (vl_api_bridge_domain_sw_if_details_t * mp)
1000 {
1001   vppjni_main_t *jm = &vppjni_main;
1002   vjbd_main_t * bdm = &jm->vjbd_main;
1003   bd_sw_if_oper_t * bd_sw_if_oper;
1004   u32 bd_id, sw_if_index;
1005
1006   bd_id = ntohl (mp->bd_id);
1007   sw_if_index = ntohl (mp->sw_if_index);
1008
1009   uword *p;
1010   p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
1011   if (p == 0) {
1012       clib_warning("Invalid bd_id %d in bridge_domain_sw_if_details_t_handler", bd_id);
1013       return;
1014   }
1015   u32 oper_bd_index = (jint) p[0];
1016   vjbd_oper_t *bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
1017
1018   u32 len = vec_len(bd_oper->bd_sw_if_oper);
1019   vec_validate(bd_oper->bd_sw_if_oper, len);
1020   bd_sw_if_oper = &bd_oper->bd_sw_if_oper[len];
1021   bd_sw_if_oper->bd_id = bd_id;
1022   bd_sw_if_oper->sw_if_index = sw_if_index;
1023   bd_sw_if_oper->shg = mp->shg;
1024
1025   hash_set(bdm->bd_id_by_sw_if_index, sw_if_index, bd_id);
1026 }
1027
1028 static const char* interface_name_from_sw_if_index(u32 sw_if_index)
1029 {
1030     vppjni_main_t *jm = &vppjni_main;
1031
1032     if (sw_if_index >= vec_len(jm->sw_if_table)) {
1033         return NULL;
1034     }
1035     if (!jm->sw_if_table[sw_if_index].valid) {
1036         return NULL;
1037     }
1038     return (const char*)jm->sw_if_table[sw_if_index].interface_name;
1039 }
1040
1041 JNIEXPORT jobject JNICALL Java_org_openvpp_vppjapi_vppConn_getBridgeDomainDetails
1042 (JNIEnv * env, jobject obj, jint bdId)
1043 {
1044     vppjni_main_t *jm = &vppjni_main;
1045     vjbd_main_t * bdm = &jm->vjbd_main;
1046     u32 oper_bd_index;
1047     u32 bd_id = bdId;
1048     jobject rv = NULL;
1049     uword *p;
1050
1051     vppjni_lock (jm, 16);
1052
1053     p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
1054     if (p == 0) {
1055         rv = NULL;
1056         goto out;
1057     }
1058     oper_bd_index = (jint) p[0];
1059
1060     vjbd_oper_t *bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
1061
1062
1063     /* setting BridgeDomainDetails */
1064
1065     jclass bddClass = (*env)->FindClass(env, "org/openvpp/vppjapi/vppBridgeDomainDetails");
1066     if ((*env)->ExceptionCheck(env)) {
1067         (*env)->ExceptionDescribe(env);
1068         rv = NULL;
1069         goto out;
1070     }
1071
1072     jmethodID midInit = (*env)->GetMethodID(env, bddClass, "<init>", "()V");
1073     if ((*env)->ExceptionCheck(env)) {
1074         (*env)->ExceptionDescribe(env);
1075         rv = NULL;
1076         goto out;
1077     }
1078     jobject bddObj = (*env)->NewObject(env, bddClass, midInit);
1079
1080     u8 *vec_bd_name = vjbd_oper_name_from_id(bdm, bd_id);
1081     if (NULL == vec_bd_name) {
1082         rv = NULL;
1083         goto out;
1084     }
1085     char *str_bd_name = (char*)format (0, "%s%c", vec_bd_name, 0);
1086     vec_free(vec_bd_name);
1087     jstring bdName = (*env)->NewStringUTF(env, str_bd_name);
1088     vec_free(str_bd_name);
1089     if (NULL == bdName) {
1090         rv = NULL;
1091         goto out;
1092     }
1093     jfieldID fidName = (*env)->GetFieldID(env, bddClass, "name", "Ljava/lang/String;");
1094     if ((*env)->ExceptionCheck(env)) {
1095         (*env)->ExceptionDescribe(env);
1096         rv = NULL;
1097         goto out;
1098     }
1099     (*env)->SetObjectField(env, bddObj, fidName, bdName);
1100
1101     jfieldID fidBdId = (*env)->GetFieldID(env, bddClass, "bdId", "I");
1102     if ((*env)->ExceptionCheck(env)) {
1103         (*env)->ExceptionDescribe(env);
1104         rv = NULL;
1105         goto out;
1106     }
1107     (*env)->SetIntField(env, bddObj, fidBdId, bdId);
1108
1109     jboolean flood = bd_oper->flood;
1110     jfieldID fidFlood = (*env)->GetFieldID(env, bddClass, "flood", "Z");
1111     if ((*env)->ExceptionCheck(env)) {
1112         (*env)->ExceptionDescribe(env);
1113         rv = NULL;
1114         goto out;
1115     }
1116     (*env)->SetBooleanField(env, bddObj, fidFlood, flood);
1117
1118     jboolean uuFlood = bd_oper->uu_flood;
1119     jfieldID fidUuFlood = (*env)->GetFieldID(env, bddClass, "uuFlood", "Z");
1120     if ((*env)->ExceptionCheck(env)) {
1121         (*env)->ExceptionDescribe(env);
1122         rv = NULL;
1123         goto out;
1124     }
1125     (*env)->SetBooleanField(env, bddObj, fidUuFlood, uuFlood);
1126
1127     jboolean forward = bd_oper->forward;
1128     jfieldID fidForward = (*env)->GetFieldID(env, bddClass, "forward", "Z");
1129     if ((*env)->ExceptionCheck(env)) {
1130         (*env)->ExceptionDescribe(env);
1131         rv = NULL;
1132         goto out;
1133     }
1134     (*env)->SetBooleanField(env, bddObj, fidForward, forward);
1135
1136     jboolean learn = bd_oper->learn;
1137     jfieldID fidLearn = (*env)->GetFieldID(env, bddClass, "learn", "Z");
1138     if ((*env)->ExceptionCheck(env)) {
1139         (*env)->ExceptionDescribe(env);
1140         rv = NULL;
1141         goto out;
1142     }
1143     (*env)->SetBooleanField(env, bddObj, fidLearn, learn);
1144
1145     jboolean arpTerm = bd_oper->arp_term;
1146     jfieldID fidArpTerm = (*env)->GetFieldID(env, bddClass, "arpTerm", "Z");
1147     if ((*env)->ExceptionCheck(env)) {
1148         (*env)->ExceptionDescribe(env);
1149         rv = NULL;
1150         goto out;
1151     }
1152     (*env)->SetBooleanField(env, bddObj, fidArpTerm, arpTerm);
1153
1154     jstring bviInterfaceName = NULL;
1155     if (~0 != bd_oper->bvi_sw_if_index) {
1156         const char *str_if_name = interface_name_from_sw_if_index(bd_oper->bvi_sw_if_index);
1157         if (NULL == str_if_name) {
1158             clib_warning("Could not get interface name for sw_if_index %d", bd_oper->bvi_sw_if_index);
1159             rv = NULL;
1160             goto out;
1161         }
1162         bviInterfaceName = (*env)->NewStringUTF(env, str_if_name);
1163         if (NULL == bviInterfaceName) {
1164             rv = NULL;
1165             goto out;
1166         }
1167     }
1168     jfieldID fidBviInterfaceName = (*env)->GetFieldID(env, bddClass, "bviInterfaceName", "Ljava/lang/String;");
1169     if ((*env)->ExceptionCheck(env)) {
1170         (*env)->ExceptionDescribe(env);
1171         rv = NULL;
1172         goto out;
1173     }
1174     (*env)->SetObjectField(env, bddObj, fidBviInterfaceName, bviInterfaceName);
1175
1176
1177     /* setting BridgeDomainInterfaceDetails */
1178
1179     jclass bdidClass = (*env)->FindClass(env, "org/openvpp/vppjapi/vppBridgeDomainInterfaceDetails");
1180     if ((*env)->ExceptionCheck(env)) {
1181         (*env)->ExceptionDescribe(env);
1182         rv = NULL;
1183         goto out;
1184     }
1185
1186     jfieldID fidInterfaceName = (*env)->GetFieldID(env, bdidClass, "interfaceName", "Ljava/lang/String;");
1187     if ((*env)->ExceptionCheck(env)) {
1188         (*env)->ExceptionDescribe(env);
1189         rv = NULL;
1190         goto out;
1191     }
1192
1193     jfieldID fidSHG = (*env)->GetFieldID(env, bdidClass, "splitHorizonGroup", "B");
1194     if ((*env)->ExceptionCheck(env)) {
1195         (*env)->ExceptionDescribe(env);
1196         rv = NULL;
1197         goto out;
1198     }
1199
1200     u32 len = vec_len(bd_oper->bd_sw_if_oper);
1201     ASSERT(len == bd_oper->n_sw_ifs);
1202
1203     jobjectArray bdidArray = (*env)->NewObjectArray(env, len, bdidClass, NULL);
1204
1205     u32 i;
1206     for (i = 0; i < len; i++) {
1207         bd_sw_if_oper_t *sw_if_oper = &bd_oper->bd_sw_if_oper[i];
1208
1209         jmethodID midBdidInit = (*env)->GetMethodID(env, bdidClass, "<init>", "()V");
1210         if ((*env)->ExceptionCheck(env)) {
1211             (*env)->ExceptionDescribe(env);
1212             rv = NULL;
1213             goto out;
1214         }
1215         jobject bdidObj = (*env)->NewObject(env, bdidClass, midBdidInit);
1216         (*env)->SetObjectArrayElement(env, bdidArray, i, bdidObj);
1217
1218         u32 sw_if_index = sw_if_oper->sw_if_index;
1219         const char *str_if_name = interface_name_from_sw_if_index(sw_if_index);
1220         if (NULL == str_if_name) {
1221             rv = NULL;
1222             goto out;
1223         }
1224         jstring interfaceName = (*env)->NewStringUTF(env, str_if_name);
1225         if (NULL == interfaceName) {
1226             rv = NULL;
1227             goto out;
1228         }
1229         (*env)->SetObjectField(env, bdidObj, fidInterfaceName, interfaceName);
1230
1231         jbyte shg = sw_if_oper->shg;
1232         (*env)->SetByteField(env, bdidObj, fidSHG, shg);
1233     }
1234
1235     jfieldID fidInterfaces = (*env)->GetFieldID(env, bddClass, "interfaces",
1236             "[Lorg/openvpp/vppjapi/vppBridgeDomainInterfaceDetails;");
1237     if ((*env)->ExceptionCheck(env)) {
1238         (*env)->ExceptionDescribe(env);
1239         rv = NULL;
1240         goto out;
1241     }
1242     (*env)->SetObjectField(env, bddObj, fidInterfaces, bdidArray);
1243
1244     rv = bddObj;
1245
1246 out:
1247
1248     vppjni_unlock (jm);
1249
1250     return rv;
1251 }
1252
1253 static jobject l2_fib_create_object(JNIEnv *env, bd_l2fib_oper_t *l2_fib)
1254 {
1255     jclass l2FibClass = (*env)->FindClass(env, "org/openvpp/vppjapi/vppL2Fib");
1256     if ((*env)->ExceptionCheck(env)) {
1257         (*env)->ExceptionDescribe(env);
1258         return NULL;
1259     }
1260
1261     jmethodID midL2FIbInit = (*env)->GetMethodID(env, l2FibClass, "<init>", "([BZLjava/lang/String;ZZ)V");
1262     if ((*env)->ExceptionCheck(env)) {
1263         (*env)->ExceptionDescribe(env);
1264         return NULL;
1265     }
1266
1267     u32 sw_if_index = l2_fib->sw_if_index;
1268     const char *str_if_name = interface_name_from_sw_if_index(sw_if_index);
1269     if (NULL == str_if_name) {
1270         return NULL;
1271     }
1272     jstring interfaceName = (*env)->NewStringUTF(env, str_if_name);
1273     if (NULL == interfaceName) {
1274         return NULL;
1275     }
1276
1277     jbyteArray physAddr = (*env)->NewByteArray(env, 6);
1278     (*env)->SetByteArrayRegion(env, physAddr, 0, 6,
1279             (signed char*)l2_fib->mac_addr.fields.mac);
1280     jboolean staticConfig = !l2_fib->learned;
1281     jstring outgoingInterface = interfaceName;
1282     jboolean filter = l2_fib->filter;
1283     jboolean bridgedVirtualInterface = l2_fib->bvi;
1284
1285     jobject l2FibObj = (*env)->NewObject(env, l2FibClass, midL2FIbInit,
1286             physAddr, staticConfig, outgoingInterface, filter,
1287             bridgedVirtualInterface);
1288
1289     return l2FibObj;
1290 }
1291
1292 JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_l2FibTableDump
1293 (JNIEnv * env, jobject obj, jint bd_id)
1294 {
1295   vppjni_main_t *jm = &vppjni_main;
1296   vjbd_main_t * bdm = &jm->vjbd_main;
1297   vl_api_l2_fib_table_dump_t *mp;
1298   jobjectArray l2FibArray = NULL;
1299   vjbd_oper_t *bd_oper;
1300   u32 oper_bd_index;
1301   uword *p;
1302   f64 timeout;
1303   int rv;
1304   u32 i;
1305
1306   vppjni_lock (jm, 17);
1307
1308   //vjbd_l2fib_oper_reset (bdm);
1309
1310   p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
1311   if (p == 0) {
1312       goto done;
1313   }
1314   oper_bd_index = p[0];
1315   bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
1316   vec_reset_length (bd_oper->l2fib_oper);
1317
1318   /* Get list of l2 fib table entries */
1319   M(L2_FIB_TABLE_DUMP, l2_fib_table_dump);
1320   mp->bd_id = ntohl(bd_id);
1321   S;
1322
1323   /* Use a control ping for synchronization */
1324   {
1325     vl_api_control_ping_t * mp;
1326     M(CONTROL_PING, control_ping);
1327     S;
1328   }
1329
1330   WNR;
1331   if (0 != rv) {
1332       goto done;
1333   }
1334
1335   u32 count = vec_len(bd_oper->l2fib_oper);
1336   bd_l2fib_oper_t *l2fib_oper = bd_oper->l2fib_oper;
1337
1338   jclass l2FibClass = (*env)->FindClass(env, "org/openvpp/vppjapi/vppL2Fib");
1339   if ((*env)->ExceptionCheck(env)) {
1340       (*env)->ExceptionDescribe(env);
1341       goto done;
1342   }
1343
1344   l2FibArray = (*env)->NewObjectArray(env, count, l2FibClass, NULL);
1345
1346   for (i = 0; i < count; i++) {
1347       bd_l2fib_oper_t *l2_fib = &l2fib_oper[i];
1348       jobject l2FibObj = l2_fib_create_object(env, l2_fib);
1349       (*env)->SetObjectArrayElement(env, l2FibArray, i, l2FibObj);
1350   }
1351
1352 done:
1353   vppjni_unlock (jm);
1354
1355   return l2FibArray;
1356 }
1357
1358 static void
1359 vl_api_l2_fib_table_entry_t_handler (vl_api_l2_fib_table_entry_t * mp)
1360 {
1361   //static u8 * mac_addr;
1362   vppjni_main_t *jm = &vppjni_main;
1363   vjbd_main_t * bdm = &jm->vjbd_main;
1364   vjbd_oper_t * bd_oper;
1365   u32 bd_id, oper_bd_index;
1366   //uword mhash_val_l2fi;
1367   bd_l2fib_oper_t * l2fib_oper;
1368   l2fib_u64_mac_t * l2fe_u64_mac = (l2fib_u64_mac_t *)&mp->mac;
1369
1370   bd_id = ntohl (mp->bd_id);
1371
1372   uword *p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
1373   if (p == 0) {
1374       return;
1375   }
1376   oper_bd_index = (jint) p[0];
1377   bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
1378
1379 #if 0
1380   vec_validate (mac_addr, MAC_ADDRESS_SIZE);
1381   memcpy (mac_addr, l2fe_u64_mac->fields.mac, MAC_ADDRESS_SIZE);
1382   mhash_val_l2fi = vec_len (bd_oper->l2fib_oper);
1383   if (mhash_elts (&bd_oper->l2fib_index_by_mac) == 0)
1384     mhash_init (&bd_oper->l2fib_index_by_mac, sizeof (u32), MAC_ADDRESS_SIZE);
1385   mhash_set_mem (&bd_oper->l2fib_index_by_mac, mac_addr, &mhash_val_l2fi, 0);
1386 #endif
1387
1388   vec_add2 (bd_oper->l2fib_oper, l2fib_oper, 1);
1389
1390   l2fib_oper->bd_id = bd_id;
1391   l2fib_oper->mac_addr.raw = l2fib_mac_to_u64 (l2fe_u64_mac->fields.mac);
1392   l2fib_oper->sw_if_index = ntohl (mp->sw_if_index);
1393   l2fib_oper->learned = !mp->static_mac;
1394   l2fib_oper->filter = mp->filter_mac;
1395   l2fib_oper->bvi = mp->bvi_mac;
1396 }
1397
1398 static int ipAddressDump
1399 (JNIEnv * env, jobject obj, jstring interfaceName, jboolean isIPv6)
1400 {
1401     vppjni_main_t *jm = &vppjni_main;
1402     vl_api_ip_address_dump_t * mp;
1403     const char *if_name;
1404     u32 my_context_id;
1405     u32 sw_if_index;
1406     f64 timeout;
1407     uword *p;
1408     int rv = 0;
1409
1410     if (NULL == interfaceName) {
1411         return -1;
1412     }
1413
1414     if_name = (*env)->GetStringUTFChars (env, interfaceName, NULL);
1415     p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name);
1416     (*env)->ReleaseStringUTFChars (env, interfaceName, if_name);
1417     if (p == 0) {
1418         return -1;
1419     }
1420     sw_if_index = (u32) p[0];
1421
1422     rv = vppjni_sanity_check (jm);
1423     if (0 != rv) {
1424         return rv;
1425     }
1426
1427     my_context_id = vppjni_get_context_id (jm);
1428     M(IP_ADDRESS_DUMP, ip_address_dump);
1429     mp->context = clib_host_to_net_u32 (my_context_id);
1430     mp->sw_if_index = clib_host_to_net_u32(sw_if_index);
1431     mp->is_ipv6 = isIPv6;
1432     jm->is_ipv6 = isIPv6;
1433     S;
1434
1435     /* Use a control ping for synchronization */
1436     {
1437       vl_api_control_ping_t * mp;
1438       M(CONTROL_PING, control_ping);
1439       S;
1440     }
1441
1442     WNR;
1443
1444     return rv;
1445 }
1446
1447 JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_ipv4AddressDump
1448 (JNIEnv * env, jobject obj, jstring interfaceName)
1449 {
1450     vppjni_main_t *jm = &vppjni_main;
1451     jobject returnArray = NULL;
1452     int i;
1453
1454     vppjni_lock (jm, 18);
1455
1456     vec_reset_length(jm->ipv4_addresses);
1457
1458     if (0 != ipAddressDump(env, obj, interfaceName, 0)) {
1459         goto done;
1460     }
1461
1462     u32 count = vec_len(jm->ipv4_addresses);
1463     ipv4_address_t *ipv4_address = jm->ipv4_addresses;
1464
1465     jclass IPv4AddressClass = (*env)->FindClass(env, "org/openvpp/vppjapi/vppIPv4Address");
1466     if ((*env)->ExceptionCheck(env)) {
1467         (*env)->ExceptionDescribe(env);
1468         goto done;
1469     }
1470
1471     jmethodID midIPv4AddressInit = (*env)->GetMethodID(env, IPv4AddressClass, "<init>", "(IB)V");
1472     if ((*env)->ExceptionCheck(env)) {
1473         (*env)->ExceptionDescribe(env);
1474         goto done;
1475     }
1476
1477     jobjectArray ipv4AddressArray = (*env)->NewObjectArray(env, count, IPv4AddressClass, NULL);
1478
1479     for (i = 0; i < count; i++) {
1480         ipv4_address_t *address = &ipv4_address[i];
1481
1482         jint ip = address->ip;
1483         jbyte prefixLength = address->prefix_length;
1484
1485         jobject ipv4AddressObj = (*env)->NewObject(env, IPv4AddressClass, midIPv4AddressInit,
1486                 ip, prefixLength);
1487
1488         (*env)->SetObjectArrayElement(env, ipv4AddressArray, i, ipv4AddressObj);
1489     }
1490
1491     returnArray = ipv4AddressArray;
1492
1493 done:
1494     vppjni_unlock (jm);
1495     return returnArray;
1496 }
1497
1498 JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_ipv6AddressDump
1499 (JNIEnv * env, jobject obj, jstring interfaceName)
1500 {
1501     vppjni_main_t *jm = &vppjni_main;
1502     jobject returnArray = NULL;
1503     int i;
1504
1505     vppjni_lock (jm, 19);
1506
1507     vec_reset_length(jm->ipv6_addresses);
1508
1509     if (0 != ipAddressDump(env, obj, interfaceName, 1)) {
1510         goto done;
1511     }
1512
1513     u32 count = vec_len(jm->ipv6_addresses);
1514     ipv6_address_t *ipv6_address = jm->ipv6_addresses;
1515
1516     jclass IPv6AddressClass = (*env)->FindClass(env, "org/openvpp/vppjapi/vppIPv6Address");
1517     if ((*env)->ExceptionCheck(env)) {
1518         (*env)->ExceptionDescribe(env);
1519         goto done;
1520     }
1521
1522     jmethodID midIPv6AddressInit = (*env)->GetMethodID(env, IPv6AddressClass, "<init>", "([BB)V");
1523     if ((*env)->ExceptionCheck(env)) {
1524         (*env)->ExceptionDescribe(env);
1525         goto done;
1526     }
1527
1528     jobjectArray ipv6AddressArray = (*env)->NewObjectArray(env, count, IPv6AddressClass, NULL);
1529
1530     for (i = 0; i < count; i++) {
1531         ipv6_address_t *address = &ipv6_address[i];
1532
1533         jbyteArray ip = (*env)->NewByteArray(env, 16);
1534         (*env)->SetByteArrayRegion(env, ip, 0, 16,
1535                 (signed char*)address->ip);
1536
1537         jbyte prefixLength = address->prefix_length;
1538
1539         jobject ipv6AddressObj = (*env)->NewObject(env, IPv6AddressClass, midIPv6AddressInit,
1540                 ip, prefixLength);
1541
1542         (*env)->SetObjectArrayElement(env, ipv6AddressArray, i, ipv6AddressObj);
1543     }
1544
1545     returnArray = ipv6AddressArray;
1546
1547 done:
1548     vppjni_unlock (jm);
1549     return returnArray;
1550 }
1551
1552 static void vl_api_ip_address_details_t_handler (vl_api_ip_address_details_t * mp)
1553 {
1554     vppjni_main_t * jm = &vppjni_main;
1555
1556     if (!jm->is_ipv6) {
1557         ipv4_address_t *address = 0;
1558         vec_add2(jm->ipv4_addresses, address, 1);
1559         memcpy(&address->ip, mp->ip, 4);
1560         address->prefix_length = mp->prefix_length;
1561     } else {
1562         ipv6_address_t *address = 0;
1563         vec_add2(jm->ipv6_addresses, address, 1);
1564         memcpy(address->ip, mp->ip, 16);
1565         address->prefix_length = mp->prefix_length;
1566     }
1567 }
1568
1569 #define VXLAN_TUNNEL_INTERFACE_NAME_PREFIX "vxlan_tunnel"
1570
1571 JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_vxlanTunnelDump
1572 (JNIEnv * env, jobject obj, jint swIfIndex)
1573 {
1574     vppjni_main_t *jm = &vppjni_main;
1575     vl_api_vxlan_tunnel_dump_t * mp;
1576     jobjectArray returnArray = NULL;
1577     u32 my_context_id;
1578     f64 timeout;
1579     int rv = 0;
1580     int i;
1581
1582     vppjni_lock (jm, 22);
1583
1584     vec_reset_length(jm->vxlan_tunnel_details);
1585
1586     my_context_id = vppjni_get_context_id (jm);
1587     M(VXLAN_TUNNEL_DUMP, vxlan_tunnel_dump);
1588     mp->context = clib_host_to_net_u32 (my_context_id);
1589     mp->sw_if_index = clib_host_to_net_u32 (swIfIndex);
1590     S;
1591
1592     /* Use a control ping for synchronization */
1593     {
1594       vl_api_control_ping_t * mp;
1595       M(CONTROL_PING, control_ping);
1596       S;
1597     }
1598
1599     WNR;
1600     if (0 != rv) {
1601         goto done;
1602     }
1603
1604     u32 count = vec_len(jm->vxlan_tunnel_details);
1605
1606     jclass VxlanTunnelDetailsClass = (*env)->FindClass(env,
1607             "org/openvpp/vppjapi/vppVxlanTunnelDetails");
1608     if ((*env)->ExceptionCheck(env)) {
1609         (*env)->ExceptionDescribe(env);
1610         goto done;
1611     }
1612
1613     jmethodID midVxlanTunnelDetailsInit = (*env)->GetMethodID(env,
1614             VxlanTunnelDetailsClass, "<init>", "(IIIII)V");
1615     if ((*env)->ExceptionCheck(env)) {
1616         (*env)->ExceptionDescribe(env);
1617         goto done;
1618     }
1619
1620     jobjectArray vxlanTunnelDetailsArray = (*env)->NewObjectArray(env, count,
1621             VxlanTunnelDetailsClass, NULL);
1622
1623     for (i = 0; i < count; i++) {
1624         vxlan_tunnel_details_t *details = &jm->vxlan_tunnel_details[i];
1625
1626         jint src_address = details->src_address;
1627         jint dst_address = details->dst_address;
1628         jint encap_vrf_id = details->encap_vrf_id;
1629         jint vni = details->vni;
1630         jint decap_next_index = details->decap_next_index;
1631
1632         jobject vxlanTunnelDetailsObj = (*env)->NewObject(env,
1633                 VxlanTunnelDetailsClass, midVxlanTunnelDetailsInit,
1634                 src_address, dst_address, encap_vrf_id, vni, decap_next_index);
1635
1636         (*env)->SetObjectArrayElement(env, vxlanTunnelDetailsArray, i,
1637                 vxlanTunnelDetailsObj);
1638     }
1639
1640     returnArray = vxlanTunnelDetailsArray;
1641
1642 done:
1643     vppjni_unlock (jm);
1644     return returnArray;
1645 }
1646
1647 static void vl_api_vxlan_tunnel_details_t_handler
1648 (vl_api_vxlan_tunnel_details_t * mp)
1649 {
1650     vppjni_main_t * jm = &vppjni_main;
1651     vxlan_tunnel_details_t *tunnel_details;
1652
1653     vec_add2(jm->vxlan_tunnel_details, tunnel_details, 1);
1654     tunnel_details->src_address = ntohl(mp->src_address);
1655     tunnel_details->dst_address = ntohl(mp->dst_address);
1656     tunnel_details->encap_vrf_id = ntohl(mp->encap_vrf_id);
1657     tunnel_details->vni = ntohl(mp->vni);
1658     tunnel_details->decap_next_index = ntohl(mp->decap_next_index);
1659 }
1660
1661 /* cleanup handler for RX thread */
1662 void cleanup_rx_thread(void *arg)
1663 {
1664   vppjni_main_t * jm = &vppjni_main;
1665
1666   vppjni_lock (jm, 99);
1667
1668   int getEnvStat = (*jm->jvm)->GetEnv(jm->jvm, (void **)&(jm->jenv), JNI_VERSION_1_6);
1669   if (getEnvStat == JNI_EVERSION) {
1670     clib_warning ("Unsupported JNI version\n");
1671     jm->retval = -999;
1672     goto out;
1673   } else if (getEnvStat != JNI_EDETACHED) {
1674     (*jm->jvm)->DetachCurrentThread(jm->jvm);
1675   }
1676 out:
1677   vppjni_unlock (jm);
1678 }
1679
1680 static void
1681 vl_api_show_version_reply_t_handler (vl_api_show_version_reply_t * mp)
1682 {
1683   vppjni_main_t * jm = &vppjni_main;
1684   i32 retval = ntohl(mp->retval);
1685
1686   if (retval >= 0) {
1687     DEBUG_LOG ("show version request succeeded(%d)");
1688     strncpy((char*)jm->program_name, (const char*)mp->program,
1689             sizeof(jm->program_name)-1);
1690     jm->program_name[sizeof(jm->program_name)-1] = 0;
1691
1692     strncpy((char*)jm->build_directory, (const char*)mp->build_directory,
1693             sizeof(jm->build_directory)-1);
1694     jm->build_directory[sizeof(jm->build_directory)-1] = 0;
1695
1696     strncpy((char*)jm->version, (const char*)mp->version,
1697             sizeof(jm->version)-1);
1698     jm->version[sizeof(jm->version)-1] = 0;
1699
1700     strncpy((char*)jm->build_date, (const char*)mp->build_date,
1701             sizeof(jm->build_date)-1);
1702     jm->build_date[sizeof(jm->build_date)-1] = 0;
1703   } else {
1704     clib_error ("show version request failed(%d)", retval);
1705   }
1706   jm->retval = retval;
1707   jm->result_ready = 1;
1708 }
1709
1710 static void vl_api_want_stats_reply_t_handler (vl_api_want_stats_reply_t * mp)
1711 {
1712   vppjni_main_t * jm = &vppjni_main;
1713   jm->retval = mp->retval; // FIXME: vpp api does not do ntohl on this retval
1714   jm->result_ready = 1;
1715 }
1716
1717 // control ping needs to be very first thing called
1718 // to attach rx thread to java thread
1719 static void vl_api_control_ping_reply_t_handler
1720 (vl_api_control_ping_reply_t * mp)
1721 {
1722   vppjni_main_t * jm = &vppjni_main;
1723   i32 retval = ntohl(mp->retval);
1724   jm->retval = retval;
1725
1726   // attach to java thread if not attached
1727   int getEnvStat = (*jm->jvm)->GetEnv(jm->jvm, (void **)&(jm->jenv), JNI_VERSION_1_6);
1728   if (getEnvStat == JNI_EDETACHED) {
1729     if ((*jm->jvm)->AttachCurrentThread(jm->jvm, (void **)&(jm->jenv), NULL) != 0) {
1730       clib_warning("Failed to attach thread\n");
1731       jm->retval = -999;
1732       goto out;
1733     }
1734
1735     // workaround as we can't use pthread_cleanup_push
1736     pthread_key_create(&jm->cleanup_rx_thread_key, cleanup_rx_thread);
1737     // destructor is only called if the value of key is non null
1738     pthread_setspecific(jm->cleanup_rx_thread_key, (void *)1);
1739   } else if (getEnvStat == JNI_EVERSION) {
1740     clib_warning ("Unsupported JNI version\n");
1741     jm->retval = -999;
1742     goto out;
1743   }
1744   // jm->jenv is now stable global reference that can be reused (only within RX thread)
1745
1746 #if 0
1747   // ! callback system removed for now
1748   //
1749   // get issuer msg-id
1750   p = hash_get (jm->ping_hash, context);
1751   if (p != 0) { // ping marks end of some dump call
1752     JNIEnv *env = jm->jenv;
1753     u16 msg_id = (u16)p[0];
1754
1755     // we will no longer need this
1756     hash_unset (jm->ping_hash, context);
1757
1758     // get original caller obj
1759     p = hash_get (jm->callback_hash, context);
1760
1761     if (p == 0) // don't have callback stored
1762       goto out;
1763
1764     jobject obj = (jobject)p[0]; // object that called original call
1765
1766     switch (msg_id) {
1767       case VL_API_SW_INTERFACE_DUMP:
1768         if (0 != sw_if_dump_call_all_callbacks(obj)) {
1769           goto out2;
1770         }
1771         break;
1772       default:
1773         clib_warning("Unhandled control ping issuer msg-id: %d", msg_id);
1774         goto out2;
1775         break;
1776     }
1777   out2:
1778     // free the saved obj
1779     hash_unset (jm->callback_hash, context);
1780     // delete global reference
1781     (*env)->DeleteGlobalRef(env, obj);
1782   }
1783 #endif
1784
1785 out:
1786   jm->result_ready = 1;
1787 }
1788
1789 #define VPPJNI_DEBUG_COUNTERS 0
1790
1791 static void vl_api_vnet_interface_counters_t_handler
1792 (vl_api_vnet_interface_counters_t *mp)
1793 {
1794   vppjni_main_t *jm = &vppjni_main;
1795   CLIB_UNUSED(char *counter_name);
1796   u32 count, sw_if_index;
1797   int i;
1798   static sw_interface_stats_t empty_stats = {0, };
1799
1800   vppjni_lock (jm, 12);
1801   count = ntohl (mp->count);
1802   sw_if_index = ntohl (mp->first_sw_if_index);
1803   if (mp->is_combined == 0) {
1804     u64 * vp, v;
1805     vp = (u64 *) mp->data;
1806
1807     for (i = 0; i < count; i++) {
1808       sw_interface_details_t *sw_if = NULL;
1809
1810       v = clib_mem_unaligned (vp, u64);
1811       v = clib_net_to_host_u64 (v);
1812       vp++;
1813
1814       if (sw_if_index < vec_len(jm->sw_if_table))
1815         sw_if = vec_elt_at_index(jm->sw_if_table, sw_if_index);
1816
1817       if (sw_if /* && (sw_if->admin_up_down == 1)*/ && sw_if->interface_name[0] != 0)
1818         {
1819           vec_validate_init_empty(jm->sw_if_stats_by_sw_if_index, sw_if_index, empty_stats);
1820           sw_interface_stats_t * s = vec_elt_at_index(jm->sw_if_stats_by_sw_if_index, sw_if_index);
1821
1822           s->sw_if_index = sw_if_index;
1823           s->valid = 1;
1824
1825           switch (mp->vnet_counter_type) {
1826           case  VNET_INTERFACE_COUNTER_DROP:
1827             counter_name = "drop";
1828             s->rx.pkts.discard = v;
1829             break;
1830           case  VNET_INTERFACE_COUNTER_PUNT:
1831             counter_name = "punt";
1832             s->rx.pkts.unknown_proto = v;
1833             break;
1834           case  VNET_INTERFACE_COUNTER_IP4:
1835             counter_name = "ip4";
1836             s->rx.pkts.ip4 = v;
1837             break;
1838           case  VNET_INTERFACE_COUNTER_IP6:
1839             counter_name = "ip6";
1840             s->rx.pkts.ip6 = v;
1841             break;
1842           case  VNET_INTERFACE_COUNTER_RX_NO_BUF:
1843             counter_name = "rx-no-buf";
1844             s->rx.pkts.fifo_full = v;
1845             break;
1846           case  VNET_INTERFACE_COUNTER_RX_MISS:
1847             counter_name = "rx-miss";
1848             s->rx.pkts.miss = v;
1849             break;
1850           case  VNET_INTERFACE_COUNTER_RX_ERROR:
1851             counter_name = "rx-error";
1852             s->rx.pkts.error = v;
1853             break;
1854           case  VNET_INTERFACE_COUNTER_TX_ERROR:
1855             counter_name = "tx-error (fifo-full)";
1856             s->tx.pkts.fifo_full = v;
1857             break;
1858           default:
1859             counter_name = "bogus";
1860             break;
1861           }
1862
1863 #if VPPJNI_DEBUG_COUNTERS == 1
1864           clib_warning ("%s (%d): %s (%lld)\n", sw_if->interface_name, s->sw_if_index,
1865                         counter_name, v);
1866 #endif
1867         }
1868       sw_if_index++;
1869     }
1870   } else {
1871     vlib_counter_t *vp;
1872     u64 packets, bytes;
1873     vp = (vlib_counter_t *) mp->data;
1874
1875     for (i = 0; i < count; i++) {
1876       sw_interface_details_t *sw_if = NULL;
1877
1878       packets = clib_mem_unaligned (&vp->packets, u64);
1879       packets = clib_net_to_host_u64 (packets);
1880       bytes = clib_mem_unaligned (&vp->bytes, u64);
1881       bytes = clib_net_to_host_u64 (bytes);
1882       vp++;
1883
1884       if (sw_if_index < vec_len(jm->sw_if_table))
1885         sw_if = vec_elt_at_index(jm->sw_if_table, sw_if_index);
1886
1887       if (sw_if /* && (sw_if->admin_up_down == 1) */ && sw_if->interface_name[0] != 0)
1888         {
1889           vec_validate_init_empty(jm->sw_if_stats_by_sw_if_index, sw_if_index, empty_stats);
1890           sw_interface_stats_t * s = vec_elt_at_index(jm->sw_if_stats_by_sw_if_index, sw_if_index);
1891
1892           s->valid = 1;
1893           s->sw_if_index = sw_if_index;
1894
1895           switch (mp->vnet_counter_type) {
1896           case  VNET_INTERFACE_COUNTER_RX:
1897             s->rx.pkts.unicast = packets;
1898             s->rx.octets = bytes;
1899             counter_name = "rx";
1900             break;
1901
1902           case  VNET_INTERFACE_COUNTER_TX:
1903             s->tx.pkts.unicast = packets;
1904             s->tx.octets = bytes;
1905             counter_name = "tx";
1906             break;
1907
1908           default:
1909             counter_name = "bogus";
1910             break;
1911           }
1912
1913 #if VPPJNI_DEBUG_COUNTERS == 1
1914           clib_warning ("%s (%d): %s.packets %lld\n", 
1915                    sw_if->interface_name,
1916                    sw_if_index, counter_name, packets);
1917           clib_warning ("%s (%d): %s.bytes %lld\n", 
1918                    sw_if->interface_name,
1919                    sw_if_index, counter_name, bytes);
1920 #endif
1921         }
1922       sw_if_index++;
1923     }
1924   }
1925   vppjni_unlock (jm);
1926 }
1927
1928 jint JNI_OnLoad(JavaVM *vm, void *reserved) {
1929   vppjni_main_t * jm = &vppjni_main;
1930   JNIEnv* env;
1931   if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_6) != JNI_OK) {
1932     return JNI_ERR;
1933   }
1934
1935   jclass cls = (*env)->FindClass(env, "org/openvpp/vppjapi/vppInterfaceDetails");
1936   if ((*env)->ExceptionCheck(env)) {
1937       (*env)->ExceptionDescribe(env);
1938       return JNI_ERR;
1939   }
1940
1941   jm->jmtdIfDetails = 
1942       (*env)->GetMethodID(env, cls, "<init>", "(ILjava/lang/String;I[BBBBBIBBIIBBBBIIII)V");
1943   if ((*env)->ExceptionCheck(env)) {
1944       (*env)->ExceptionDescribe(env);
1945       return JNI_ERR;
1946   }
1947
1948   // methods are not local references they stay forever
1949   // jclass is local reference let's hold global ref to it
1950   jm->jcls = (*env)->NewGlobalRef(env, cls);
1951
1952   jm->jvm = vm;
1953   return JNI_VERSION_1_6;
1954 }
1955
1956 void JNI_OnUnload(JavaVM *vm, void *reserved) {
1957   vppjni_main_t * jm = &vppjni_main;
1958   JNIEnv* env;
1959   if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_6) != JNI_OK) {
1960     return;
1961   }
1962
1963   if (jm->jcls != NULL) {
1964     (*env)->DeleteGlobalRef(env, jm->jcls);
1965     jm->jcls = NULL;
1966   }
1967
1968   jm->jenv = NULL;
1969   jm->jvm = NULL;
1970 }
1971
1972 #define foreach_vpe_api_msg                             \
1973 _(CONTROL_PING_REPLY, control_ping_reply)               \
1974 _(SW_INTERFACE_DETAILS, sw_interface_details)           \
1975 _(SHOW_VERSION_REPLY, show_version_reply)               \
1976 _(WANT_STATS_REPLY, want_stats_reply)                   \
1977 _(VNET_INTERFACE_COUNTERS, vnet_interface_counters)     \
1978 _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags)       \
1979 _(BRIDGE_DOMAIN_DETAILS, bridge_domain_details)         \
1980 _(BRIDGE_DOMAIN_SW_IF_DETAILS, bridge_domain_sw_if_details) \
1981 _(L2_FIB_TABLE_ENTRY, l2_fib_table_entry)               \
1982 _(IP_ADDRESS_DETAILS, ip_address_details)               \
1983 _(VXLAN_TUNNEL_DETAILS, vxlan_tunnel_details)
1984
1985 static int connect_to_vpe(char *name)
1986 {
1987   vppjni_main_t * jm = &vppjni_main;
1988   api_main_t * am = &api_main;
1989
1990   if (vl_client_connect_to_vlib("/vpe-api", name, 32) < 0)
1991     return -1;
1992
1993   jm->my_client_index = am->my_client_index;
1994   jm->vl_input_queue = am->shmem_hdr->vl_input_queue;
1995
1996 #define _(N,n)                                                  \
1997     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
1998                            vl_api_##n##_t_handler,              \
1999                            vl_noop_handler,                     \
2000                            vl_api_##n##_t_endian,               \
2001                            vl_api_##n##_t_print,                \
2002                            sizeof(vl_api_##n##_t), 1); 
2003     foreach_vpe_api_msg;
2004 #undef _
2005   
2006
2007   return 0;
2008 }
2009
2010 /* Format an IP6 address. */
2011 u8 * format_ip6_address (u8 * s, va_list * args)
2012 {
2013   ip6_address_t * a = va_arg (*args, ip6_address_t *);
2014   u32 max_zero_run = 0, this_zero_run = 0;
2015   int max_zero_run_index = -1, this_zero_run_index=0;
2016   int in_zero_run = 0, i;
2017   int last_double_colon = 0;
2018
2019   /* Ugh, this is a pain. Scan forward looking for runs of 0's */
2020   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
2021     {
2022       if (a->as_u16[i] == 0)
2023         {
2024           if (in_zero_run)
2025             this_zero_run++;
2026           else
2027             {
2028               in_zero_run = 1;
2029               this_zero_run =1;
2030               this_zero_run_index = i;
2031             }
2032         }
2033       else
2034         {
2035           if (in_zero_run)
2036             {
2037               /* offer to compress the biggest run of > 1 zero */
2038               if (this_zero_run > max_zero_run && this_zero_run > 1)
2039                 {
2040                   max_zero_run_index = this_zero_run_index;
2041                   max_zero_run = this_zero_run;
2042                 }
2043             }
2044           in_zero_run = 0;
2045           this_zero_run = 0;
2046         }
2047     }
2048
2049   if (in_zero_run)
2050     {
2051       if (this_zero_run > max_zero_run && this_zero_run > 1)
2052         {
2053           max_zero_run_index = this_zero_run_index;
2054           max_zero_run = this_zero_run;
2055         }
2056     }
2057   
2058   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
2059     {
2060       if (i == max_zero_run_index)
2061         {
2062           s = format (s, "::");
2063           i += max_zero_run - 1;
2064           last_double_colon = 1;
2065         }
2066       else
2067         {
2068           s = format (s, "%s%x",
2069                       (last_double_colon || i == 0) ? "" : ":",
2070                       clib_net_to_host_u16 (a->as_u16[i]));
2071           last_double_colon = 0;
2072         }
2073     }
2074
2075   return s;
2076 }
2077
2078 /* Format an IP4 address. */
2079 u8 * format_ip4_address (u8 * s, va_list * args)
2080 {
2081   u8 * a = va_arg (*args, u8 *);
2082   return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
2083 }
2084
2085