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