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