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