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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vnet/vnet.h>
17 #define vl_api_version(n,v) static u32 vpe_api_version = (v);
18 #include <api/vpe.api.h>
22 #include <japi/vppjni.h>
23 #include <japi/vppjni_bridge_domain.h>
24 #include <japi/org_openvpp_vppjapi_vppConn.h>
25 #include <japi/org_openvpp_vppjapi_vppApi.h>
27 #include <api/vpe_msg_enum.h>
28 #define vl_typedefs /* define message structures */
29 #include <api/vpe_all_api_h.h>
33 #include <api/vpe_all_api_h.h>
36 /* instantiate all the print functions we know about */
37 #define vl_print(handle, ...)
39 #include <api/vpe_all_api_h.h>
42 static int connect_to_vpe(char *name);
45 * The Java runtime isn't compile w/ -fstack-protector,
46 * so we have to supply missing external references for the
47 * regular vpp libraries. Weak reference in case folks get religion
50 void __stack_chk_guard (void) __attribute__((weak));
51 void __stack_chk_guard (void) { }
53 void vl_client_add_api_signatures (vl_api_memclnt_create_t *mp)
56 * Send the main API signature in slot 0. This bit of code must
57 * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
59 mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
62 /* Note: non-static, called once to set up the initial intfc table */
63 static int sw_interface_dump (vppjni_main_t * jm)
65 vl_api_sw_interface_dump_t *mp;
68 name_sort_t * nses = 0, * ns;
69 sw_interface_subif_t * sub = NULL;
71 /* Toss the old name table */
72 hash_foreach_pair (p, jm->sw_if_index_by_interface_name,
74 vec_add2 (nses, ns, 1);
75 ns->name = (u8 *)(p->key);
76 ns->value = (u32) p->value[0];
79 hash_free (jm->sw_if_index_by_interface_name);
81 vec_foreach (ns, nses)
86 vec_foreach (sub, jm->sw_if_subif_table) {
87 vec_free (sub->interface_name);
89 vec_free (jm->sw_if_subif_table);
91 /* recreate the interface name hash table */
92 jm->sw_if_index_by_interface_name
93 = hash_create_string (0, sizeof(uword));
95 /* Get list of ethernets */
96 M(SW_INTERFACE_DUMP, sw_interface_dump);
97 mp->name_filter_valid = 1;
98 strncpy ((char *) mp->name_filter, "Ether", sizeof(mp->name_filter-1));
101 /* and local / loopback interfaces */
102 M(SW_INTERFACE_DUMP, sw_interface_dump);
103 mp->name_filter_valid = 1;
104 strncpy ((char *) mp->name_filter, "lo", sizeof(mp->name_filter-1));
107 /* and vxlan tunnel interfaces */
108 M(SW_INTERFACE_DUMP, sw_interface_dump);
109 mp->name_filter_valid = 1;
110 strncpy ((char *) mp->name_filter, "vxlan", sizeof(mp->name_filter-1));
113 /* and tap tunnel interfaces */
114 M(SW_INTERFACE_DUMP, sw_interface_dump);
115 mp->name_filter_valid = 1;
116 strncpy ((char *) mp->name_filter, "tap", sizeof(mp->name_filter-1));
119 /* and l2tpv3 tunnel interfaces */
120 M(SW_INTERFACE_DUMP, sw_interface_dump);
121 mp->name_filter_valid = 1;
122 strncpy ((char *) mp->name_filter, "l2tpv3_tunnel",
123 sizeof(mp->name_filter-1));
126 /* Use a control ping for synchronization */
128 vl_api_control_ping_t * mp;
129 M(CONTROL_PING, control_ping);
135 JNIEXPORT jobject JNICALL Java_org_openvpp_vppjapi_vppConn_getVppVersion
136 (JNIEnv *env, jobject obj)
138 vppjni_main_t * jm = &vppjni_main;
141 jclass cls = (*env)->FindClass(env, "org/openvpp/vppjapi/vppVersion");
142 if ((*env)->ExceptionCheck(env)) {
143 (*env)->ExceptionDescribe(env);
147 constr = (*env)->GetMethodID(env, cls, "<init>", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
148 if ((*env)->ExceptionCheck(env)) {
149 (*env)->ExceptionDescribe(env);
153 vppjni_lock (jm, 11);
154 jstring progName = (*env)->NewStringUTF(env, (char *)jm->program_name);
155 jstring buildDir = (*env)->NewStringUTF(env, (char *)jm->build_directory);
156 jstring gitBranch = (*env)->NewStringUTF(env, (char *)jm->git_branch);
157 jstring buildDate = (*env)->NewStringUTF(env, (char *)jm->build_date);
160 return (*env)->NewObject(env, cls, constr, progName, buildDir, gitBranch, buildDate);
163 static int jm_show_version (vppjni_main_t *jm)
166 vl_api_show_version_t *mp;
169 vppjni_lock (jm, 10);
170 M(SHOW_VERSION, show_version);
178 static int jm_stats_enable_disable (vppjni_main_t *jm, u8 enable)
180 vl_api_want_stats_t * mp;
184 vppjni_lock (jm, 13);
186 M(WANT_STATS, want_stats);
188 mp->enable_disable = enable;
194 // already subscribed / already disabled (it's ok)
195 if (rv == -2 || rv == -3)
200 JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_clientConnect
201 (JNIEnv *env, jobject obj, jstring clientName)
204 const char *client_name;
205 void vl_msg_reply_handler_hookup(void);
206 vppjni_main_t * jm = &vppjni_main;
207 api_main_t * am = &api_main;
213 * Bail out now if we're not running as root
218 if (jm->is_connected)
222 clib_mem_init (0, 128<<20);
224 heap = clib_mem_get_per_cpu_heap();
225 h = mheap_header (heap);
227 client_name = (*env)->GetStringUTFChars (env, clientName, 0);
229 clib_time_init (&jm->clib_time);
231 rv = connect_to_vpe ((char *) client_name);
234 clib_warning ("connection failed, rv %d", rv);
236 (*env)->ReleaseStringUTFChars (env, clientName, client_name);
240 vl_msg_reply_handler_hookup ();
241 jm->is_connected = 1;
242 /* make the main heap thread-safe */
243 h->flags |= MHEAP_FLAG_THREAD_SAFE;
245 jm->reply_hash = hash_create (0, sizeof (uword));
246 jm->callback_hash = hash_create (0, sizeof (uword));
247 jm->ping_hash = hash_create (0, sizeof (uword));
249 vjbd_main_init(&jm->vjbd_main);
250 jm->sw_if_index_by_interface_name =
251 hash_create_string (0, sizeof (uword));
254 // call control ping first to attach rx thread to java thread
255 vl_api_control_ping_t * mp;
256 M(CONTROL_PING, control_ping);
261 clib_warning ("first control ping failed: %d", rv);
264 rv = jm_show_version(jm);
266 clib_warning ("unable to retrieve vpp version (rv: %d)", rv);
267 rv = sw_interface_dump(jm);
269 clib_warning ("unable to retrieve interface list (rv: %d)", rv);
270 rv = jm_stats_enable_disable(jm, 1);
272 clib_warning ("unable to subscribe to stats (rv: %d)", rv);
274 clib_warning ("clientConnect result: %d", rv);
279 JNIEXPORT void JNICALL Java_org_openvpp_vppjapi_vppConn_clientDisconnect
280 (JNIEnv *env, jobject obj)
283 vppjni_main_t * jm = &vppjni_main;
284 vl_client_disconnect_from_vlib();
286 save_heap = jm->heap;
287 memset (jm, 0, sizeof (*jm));
288 jm->heap = save_heap;
291 void vl_api_generic_reply_handler (vl_api_generic_reply_t *mp)
293 api_main_t * am = &api_main;
294 u16 msg_id = clib_net_to_host_u16 (mp->_vl_msg_id);
296 i32 retval = clib_net_to_host_u32 (mp->retval);
297 int total_bytes = sizeof(mp);
298 vppjni_main_t * jm = &vppjni_main;
299 u8 * saved_reply = 0;
300 u32 context = clib_host_to_net_u32 (mp->context);
302 cfgp = am->api_trace_cfg + msg_id;
305 clib_warning ("msg id %d: no trace configuration\n", msg_id);
307 total_bytes = cfgp->size;
309 jm->context_id_received = context;
311 clib_warning("Received generic reply for msg id %d", msg_id);
313 /* A generic reply, successful, we're done */
314 if (retval >= 0 && total_bytes == sizeof(*mp))
318 vec_validate (saved_reply, total_bytes - 1);
319 memcpy (saved_reply, mp, total_bytes);
322 hash_set (jm->reply_hash, context, saved_reply);
323 jm->saved_reply_count ++;
327 JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_getRetval
328 (JNIEnv * env, jobject obj, jint context, jint release)
330 vppjni_main_t * jm = &vppjni_main;
331 vl_api_generic_reply_t * mp;
336 if (context > jm->context_id_received)
337 return (VNET_API_ERROR_RESPONSE_NOT_READY);
340 p = hash_get (jm->reply_hash, context);
343 * Two cases: a generic "yes" reply - won't be in the hash table
344 * or "no", or "more data" which will be in the table.
349 mp = (vl_api_generic_reply_t *) (p[0]);
350 rv = clib_net_to_host_u32 (mp->retval);
354 u8 * free_me = (u8 *) mp;
356 hash_unset (jm->reply_hash, context);
357 jm->saved_reply_count --;
365 JNIEXPORT jstring JNICALL Java_org_openvpp_vppjapi_vppConn_getInterfaceList
366 (JNIEnv * env, jobject obj, jstring name_filter)
368 vppjni_main_t * jm = &vppjni_main;
371 name_sort_t * nses = 0, * ns;
372 const char *this_name;
373 const char * nf = (*env)->GetStringUTFChars (env, name_filter, NULL);
375 char *strcasestr (const char *, const char *);
379 hash_foreach_pair (p, jm->sw_if_index_by_interface_name,
381 this_name = (const char *)(p->key);
382 if (strlen (nf) == 0 || strcasestr (this_name, nf))
384 vec_add2 (nses, ns, 1);
385 ns->name = (u8 *)(p->key);
386 ns->value = (u32) p->value[0];
390 vec_sort (nses, n1, n2,
391 strcmp ((char *)n1->name, (char *)n2->name));
393 vec_foreach (ns, nses)
394 s = format (s, "%s: %d, ", ns->name, ns->value);
396 _vec_len (s) = vec_len (s) - 2;
397 vec_terminate_c_string (s);
402 (*env)->ReleaseStringUTFChars (env, name_filter, nf);
404 rv = (*env)->NewStringUTF (env, (char *) s);
410 JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_swIfIndexFromName
411 (JNIEnv * env, jobject obj, jstring interfaceName)
413 vppjni_main_t * jm = &vppjni_main;
415 const char * if_name = (*env)->GetStringUTFChars (env, interfaceName, NULL);
420 p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name);
427 (*env)->ReleaseStringUTFChars (env, interfaceName, if_name);
432 JNIEXPORT jobject JNICALL Java_org_openvpp_vppjapi_vppConn_getInterfaceCounters
433 (JNIEnv * env, jobject obj, jint swIfIndex)
435 vppjni_main_t * jm = &vppjni_main;
436 sw_interface_stats_t *s;
437 u32 sw_if_index = swIfIndex;
439 jobject result = NULL;
442 jclass cls = (*env)->FindClass(env, "org/openvpp/vppjapi/vppInterfaceCounters");
443 if ((*env)->ExceptionCheck(env)) {
444 (*env)->ExceptionDescribe(env);
448 constr = (*env)->GetMethodID(env, cls, "<init>", "(JJJJJJJJJJJJJJJJJJJJJJ)V");
449 if ((*env)->ExceptionCheck(env)) {
450 (*env)->ExceptionDescribe(env);
454 vppjni_lock (jm, 16);
456 if (sw_if_index >= vec_len(jm->sw_if_stats_by_sw_if_index)) {
459 s = &jm->sw_if_stats_by_sw_if_index[sw_if_index];
464 result = (*env)->NewObject(env, cls, constr,
465 s->rx.octets, s->rx.pkts.ip4, s->rx.pkts.ip6, s->rx.pkts.unicast,
466 s->rx.pkts.multicast, s->rx.pkts.broadcast, s->rx.pkts.discard,
467 s->rx.pkts.fifo_full, s->rx.pkts.error, s->rx.pkts.unknown_proto,
469 s->tx.octets, s->tx.pkts.ip4, s->tx.pkts.ip6, s->tx.pkts.unicast,
470 s->tx.pkts.multicast, s->tx.pkts.broadcast, s->tx.pkts.discard,
471 s->tx.pkts.fifo_full, s->tx.pkts.error, s->tx.pkts.unknown_proto,
479 JNIEXPORT jstring JNICALL Java_org_openvpp_vppjapi_vppConn_interfaceNameFromSwIfIndex
480 (JNIEnv * env, jobject obj, jint swIfIndex)
482 vppjni_main_t * jm = &vppjni_main;
483 sw_interface_details_t *sw_if_details;
485 jstring ifname = NULL;
489 sw_if_index = swIfIndex;
491 if (sw_if_index >= vec_len(jm->sw_if_table)) {
494 sw_if_details = &jm->sw_if_table[sw_if_index];
495 if (!sw_if_details->valid) {
499 u8 * s = format (0, "%s%c", sw_if_details->interface_name, 0);
500 ifname = (*env)->NewStringUTF(env, (char *)s);
508 JNIEXPORT void JNICALL Java_org_openvpp_vppjapi_vppConn_clearInterfaceTable
509 (JNIEnv * env, jobject obj)
511 vppjni_main_t * jm = &vppjni_main;
513 vppjni_lock (jm, 10);
515 vec_reset_length(jm->sw_if_table);
521 JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_swInterfaceDump
522 (JNIEnv * env, jobject obj, jbyte name_filter_valid, jbyteArray name_filter)
524 vppjni_main_t *jm = &vppjni_main;
526 vl_api_sw_interface_dump_t * mp;
529 rv = vppjni_sanity_check (jm);
532 my_context_id = vppjni_get_context_id (jm);
533 jbyte * name_filterP = (*env)->GetByteArrayElements (env, name_filter, NULL);
534 int cnt = (*env)->GetArrayLength (env, name_filter);
536 M(SW_INTERFACE_DUMP, sw_interface_dump);
537 mp->context = clib_host_to_net_u32 (my_context_id);
538 mp->name_filter_valid = name_filter_valid;
540 if (cnt > sizeof(mp->name_filter))
541 cnt = sizeof(mp->name_filter);
543 memcpy ((char *) mp->name_filter, name_filterP, cnt);
544 (*env)->ReleaseByteArrayElements (env, name_filter, name_filterP, 0);
546 clib_warning("interface filter (%d, %s, len: %d)", mp->name_filter_valid, (char *)mp->name_filter, cnt);
548 hash_set (jm->callback_hash, my_context_id, (*env)->NewGlobalRef(env, obj));
549 jm->collect_indices = 1;
553 // now send control ping so we know when it ends
554 vl_api_control_ping_t * mp;
555 M(CONTROL_PING, control_ping);
556 mp->context = clib_host_to_net_u32 (my_context_id);
558 // this control ping will mark end of interface dump
559 hash_set (jm->ping_hash, my_context_id, VL_API_SW_INTERFACE_DUMP);
565 return my_context_id;
568 static int sw_if_dump_call_all_callbacks (jobject obj)
570 vppjni_main_t * jm = &vppjni_main;
571 sw_interface_details_t *sw_if_details;
575 JNIEnv *env = jm->jenv;
577 for (i = 0; i < vec_len(jm->sw_if_dump_if_indices); i++) {
578 u32 sw_if_index = jm->sw_if_dump_if_indices[i];
579 ASSERT(sw_if_index < vec_len(jm->sw_if_table));
580 sw_if_details = &jm->sw_if_table[sw_if_index];
581 ASSERT(sw_if_details->valid);
583 u8 * s = format (0, "%s%c", sw_if_details->interface_name, 0);
585 jstring ifname = (*env)->NewStringUTF(env, (char *)s);
586 jint ifIndex = sw_if_details->sw_if_index;
587 jint supIfIndex = sw_if_details->sup_sw_if_index;
588 jbyteArray physAddr = (*env)->NewByteArray(env,
589 sw_if_details->l2_address_length);
590 (*env)->SetByteArrayRegion(env, physAddr, 0,
591 sw_if_details->l2_address_length,
592 (signed char*)sw_if_details->l2_address);
593 jint subId = sw_if_details->sub_id;
594 jint subOuterVlanId = sw_if_details->sub_outer_vlan_id;
595 jint subInnerVlanId = sw_if_details->sub_inner_vlan_id;
596 jint vtrOp = sw_if_details->vtr_op;
597 jint vtrPushDot1q = sw_if_details->vtr_push_dot1q;
598 jint vtrTag1 = sw_if_details->vtr_tag1;
599 jint vtrTag2 = sw_if_details->vtr_tag2;
601 jmethodID jmtdIfDetails = jm->jmtdIfDetails;
603 jbyte adminUpDown = sw_if_details->admin_up_down;
604 jbyte linkUpDown = sw_if_details->link_up_down;
605 jbyte linkDuplex = sw_if_details->link_duplex;
606 jbyte linkSpeed = sw_if_details->link_speed;
607 jbyte subDot1ad = sw_if_details->sub_dot1ad;
608 jbyte subNumberOfTags = sw_if_details->sub_number_of_tags;
609 jbyte subExactMatch = sw_if_details->sub_exact_match;
610 jbyte subDefault = sw_if_details->sub_default;
611 jbyte subOuterVlanIdAny = sw_if_details->sub_outer_vlan_id_any;
612 jbyte subInnerVlanIdAny = sw_if_details->sub_inner_vlan_id_any;
614 clib_warning("Method: %p, Calling method", jm->jmtdIfDetails);
615 (*env)->CallVoidMethod(env, obj, jmtdIfDetails, ifIndex, ifname,
616 supIfIndex, physAddr, adminUpDown, linkUpDown,
617 linkDuplex, linkSpeed, subId, subDot1ad,
618 subNumberOfTags, subOuterVlanId, subInnerVlanId,
619 subExactMatch, subDefault, subOuterVlanIdAny,
620 subInnerVlanIdAny, vtrOp, vtrPushDot1q, vtrTag1, vtrTag2);
621 if ((*env)->ExceptionCheck(env)) {
622 (*env)->ExceptionDescribe(env);
627 clib_warning("Method: %p, Calling method (null arg)", jm->jmtdIfDetails);
628 jmethodID jmtdIfDetails = jm->jmtdIfDetails;
629 (*env)->CallVoidMethod(env, obj, jmtdIfDetails, -1, NULL, -1, NULL, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
630 if ((*env)->ExceptionCheck(env)) {
631 (*env)->ExceptionDescribe(env);
636 clib_warning("Done");
639 jm->collect_indices = 0;
640 vec_reset_length(jm->sw_if_dump_if_indices);
644 JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_findOrAddBridgeDomainId
645 (JNIEnv * env, jobject obj, jstring bridgeDomain)
647 vppjni_main_t * jm = &vppjni_main;
649 const char * bd_name = (*env)->GetStringUTFChars (env, bridgeDomain, NULL);
653 rv = (jint)vjbd_find_or_add_bd (&jm->vjbd_main, (u8 *)bd_name);
657 (*env)->ReleaseStringUTFChars (env, bridgeDomain, bd_name);
662 JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_bridgeDomainIdFromName
663 (JNIEnv * env, jobject obj, jstring bridgeDomain)
665 vppjni_main_t * jm = &vppjni_main;
667 const char * bd_name = (*env)->GetStringUTFChars(env, bridgeDomain, NULL);
671 rv = (jint)vjbd_id_from_name(&jm->vjbd_main, (u8 *)bd_name);
675 (*env)->ReleaseStringUTFChars(env, bridgeDomain, bd_name);
680 JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_bridgeDomainIdFromInterfaceName
681 (JNIEnv * env, jobject obj, jstring interfaceName)
683 vppjni_main_t * jm = &vppjni_main;
684 vjbd_main_t * bdm = &jm->vjbd_main;
687 const char * if_name;
690 if_name = (*env)->GetStringUTFChars (env, interfaceName, NULL);
692 vppjni_lock (jm, 14);
694 p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name);
697 sw_if_index = (jint) p[0];
698 p = hash_get (bdm->bd_id_by_sw_if_index, sw_if_index);
706 (*env)->ReleaseStringUTFChars (env, interfaceName, if_name);
712 * Special-case: build the interface table, maintain
713 * the next loopback sw_if_index vbl.
715 static void vl_api_sw_interface_details_t_handler
716 (vl_api_sw_interface_details_t * mp)
718 vppjni_main_t * jm = &vppjni_main;
719 static sw_interface_details_t empty_sw_if_details = {0,};
720 sw_interface_details_t *sw_if_details;
725 sw_if_index = ntohl (mp->sw_if_index);
727 u8 * s = format (0, "%s%c", mp->interface_name, 0);
729 if (jm->collect_indices) {
730 u32 pos = vec_len(jm->sw_if_dump_if_indices);
731 vec_validate(jm->sw_if_dump_if_indices, pos);
732 jm->sw_if_dump_if_indices[pos] = sw_if_index;
735 vec_validate_init_empty(jm->sw_if_table, sw_if_index, empty_sw_if_details);
736 sw_if_details = &jm->sw_if_table[sw_if_index];
737 sw_if_details->valid = 1;
739 snprintf((char *)sw_if_details->interface_name,
740 sizeof(sw_if_details->interface_name), "%s", (char *)s);
741 sw_if_details->sw_if_index = sw_if_index;
742 sw_if_details->sup_sw_if_index = ntohl(mp->sup_sw_if_index);
743 sw_if_details->l2_address_length = ntohl (mp->l2_address_length);
744 ASSERT(sw_if_details->l2_address_length <= sizeof(sw_if_details->l2_address));
745 memcpy(sw_if_details->l2_address, mp->l2_address,
746 sw_if_details->l2_address_length);
747 sw_if_details->sub_id = ntohl (mp->sub_id);
748 sw_if_details->sub_outer_vlan_id = ntohl (mp->sub_outer_vlan_id);
749 sw_if_details->sub_inner_vlan_id = ntohl (mp->sub_inner_vlan_id);
750 sw_if_details->vtr_op = ntohl (mp->vtr_op);
751 sw_if_details->vtr_push_dot1q = ntohl (mp->vtr_push_dot1q);
752 sw_if_details->vtr_tag1 = ntohl (mp->vtr_tag1);
753 sw_if_details->vtr_tag2 = ntohl (mp->vtr_tag2);
755 sw_if_details->admin_up_down = mp->admin_up_down;
756 sw_if_details->link_up_down = mp->link_up_down;
757 sw_if_details->link_duplex = mp->link_duplex;
758 sw_if_details->link_speed = mp->link_speed;
759 sw_if_details->sub_dot1ad = mp->sub_dot1ad;
760 sw_if_details->sub_number_of_tags = mp->sub_number_of_tags;
761 sw_if_details->sub_exact_match = mp->sub_exact_match;
762 sw_if_details->sub_default = mp->sub_default;
763 sw_if_details->sub_outer_vlan_id_any = mp->sub_outer_vlan_id_any;
764 sw_if_details->sub_inner_vlan_id_any = mp->sub_inner_vlan_id_any;
766 hash_set_mem (jm->sw_if_index_by_interface_name, s, sw_if_index);
767 clib_warning("Got interface %s", (char *)s);
769 /* In sub interface case, fill the sub interface table entry */
770 if (mp->sw_if_index != mp->sup_sw_if_index) {
771 sw_interface_subif_t * sub = NULL;
773 vec_add2(jm->sw_if_subif_table, sub, 1);
775 vec_validate(sub->interface_name, strlen((char *)s) + 1);
776 strncpy((char *)sub->interface_name, (char *)s,
777 vec_len(sub->interface_name));
778 sub->sw_if_index = ntohl(mp->sw_if_index);
779 sub->sub_id = ntohl(mp->sub_id);
781 sub->sub_dot1ad = mp->sub_dot1ad;
782 sub->sub_number_of_tags = mp->sub_number_of_tags;
783 sub->sub_outer_vlan_id = ntohs(mp->sub_outer_vlan_id);
784 sub->sub_inner_vlan_id = ntohs(mp->sub_inner_vlan_id);
785 sub->sub_exact_match = mp->sub_exact_match;
786 sub->sub_default = mp->sub_default;
787 sub->sub_outer_vlan_id_any = mp->sub_outer_vlan_id_any;
788 sub->sub_inner_vlan_id_any = mp->sub_inner_vlan_id_any;
790 /* vlan tag rewrite */
791 sub->vtr_op = ntohl(mp->vtr_op);
792 sub->vtr_push_dot1q = ntohl(mp->vtr_push_dot1q);
793 sub->vtr_tag1 = ntohl(mp->vtr_tag1);
794 sub->vtr_tag2 = ntohl(mp->vtr_tag2);
799 static void vl_api_sw_interface_set_flags_t_handler
800 (vl_api_sw_interface_set_flags_t * mp)
802 /* $$$ nothing for the moment */
805 static jintArray create_array_of_bd_ids(JNIEnv * env, jint bd_id)
807 vppjni_main_t *jm = &vppjni_main;
808 vjbd_main_t * bdm = &jm->vjbd_main;
813 vec_add1(buf, bd_id);
815 for (i = 0; i < vec_len(bdm->bd_oper); i++) {
816 u32 bd_id = bdm->bd_oper[i].bd_id;
817 vec_add1(buf, bd_id);
821 jintArray bdidArray = (*env)->NewIntArray(env, vec_len(buf));
823 (*env)->SetIntArrayRegion(env, bdidArray, 0, vec_len(buf), (int*)buf);
828 static void bridge_domain_oper_free(void)
830 vppjni_main_t *jm = &vppjni_main;
831 vjbd_main_t *bdm = &jm->vjbd_main;
834 for (i = 0; i < vec_len(bdm->bd_oper); i++) {
835 vec_free(bdm->bd_oper->l2fib_oper);
837 vec_reset_length(bdm->bd_oper);
838 hash_free(bdm->bd_id_by_sw_if_index);
839 hash_free(bdm->oper_bd_index_by_bd_id);
842 JNIEXPORT jintArray JNICALL Java_org_openvpp_vppjapi_vppConn_bridgeDomainDump
843 (JNIEnv * env, jobject obj, jint bd_id)
845 vppjni_main_t *jm = &vppjni_main;
846 vl_api_bridge_domain_dump_t * mp;
850 rv = vppjni_sanity_check (jm);
853 vppjni_lock (jm, 15);
856 bridge_domain_oper_free();
859 my_context_id = vppjni_get_context_id (jm);
860 M(BRIDGE_DOMAIN_DUMP, bridge_domain_dump);
861 mp->context = clib_host_to_net_u32 (my_context_id);
862 mp->bd_id = clib_host_to_net_u32(bd_id);
865 /* Use a control ping for synchronization */
867 vl_api_control_ping_t * mp;
868 M(CONTROL_PING, control_ping);
877 jintArray ret = create_array_of_bd_ids(env, bd_id);
885 vl_api_bridge_domain_details_t_handler (vl_api_bridge_domain_details_t * mp)
887 vppjni_main_t *jm = &vppjni_main;
888 vjbd_main_t * bdm = &jm->vjbd_main;
889 vjbd_oper_t * bd_oper;
892 bd_id = ntohl (mp->bd_id);
894 bd_index = vec_len(bdm->bd_oper);
895 vec_validate (bdm->bd_oper, bd_index);
896 bd_oper = vec_elt_at_index(bdm->bd_oper, bd_index);
898 hash_set(bdm->oper_bd_index_by_bd_id, bd_id, bd_index);
900 bd_oper->bd_id = bd_id;
901 bd_oper->flood = mp->flood != 0;
902 bd_oper->forward = mp->forward != 0;
903 bd_oper->learn = mp->learn != 0;
904 bd_oper->uu_flood = mp->flood != 0;
905 bd_oper->arp_term = mp->arp_term != 0;
906 bd_oper->bvi_sw_if_index = ntohl (mp->bvi_sw_if_index);
907 bd_oper->n_sw_ifs = ntohl (mp->n_sw_ifs);
909 bd_oper->bd_sw_if_oper = 0;
913 vl_api_bridge_domain_sw_if_details_t_handler
914 (vl_api_bridge_domain_sw_if_details_t * mp)
916 vppjni_main_t *jm = &vppjni_main;
917 vjbd_main_t * bdm = &jm->vjbd_main;
918 bd_sw_if_oper_t * bd_sw_if_oper;
919 u32 bd_id, sw_if_index;
921 bd_id = ntohl (mp->bd_id);
922 sw_if_index = ntohl (mp->sw_if_index);
925 p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
927 clib_warning("Invalid bd_id %d in bridge_domain_sw_if_details_t_handler", bd_id);
930 u32 oper_bd_index = (jint) p[0];
931 vjbd_oper_t *bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
933 u32 len = vec_len(bd_oper->bd_sw_if_oper);
934 vec_validate(bd_oper->bd_sw_if_oper, len);
935 bd_sw_if_oper = &bd_oper->bd_sw_if_oper[len];
936 bd_sw_if_oper->bd_id = bd_id;
937 bd_sw_if_oper->sw_if_index = sw_if_index;
938 bd_sw_if_oper->shg = mp->shg;
940 hash_set(bdm->bd_id_by_sw_if_index, sw_if_index, bd_id);
943 static const char* interface_name_from_sw_if_index(u32 sw_if_index)
945 vppjni_main_t *jm = &vppjni_main;
947 if (sw_if_index >= vec_len(jm->sw_if_table)) {
950 if (!jm->sw_if_table[sw_if_index].valid) {
953 return (const char*)jm->sw_if_table[sw_if_index].interface_name;
956 JNIEXPORT jobject JNICALL Java_org_openvpp_vppjapi_vppConn_getBridgeDomainDetails
957 (JNIEnv * env, jobject obj, jint bdId)
959 vppjni_main_t *jm = &vppjni_main;
960 vjbd_main_t * bdm = &jm->vjbd_main;
966 vppjni_lock (jm, 16);
968 p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
973 oper_bd_index = (jint) p[0];
975 vjbd_oper_t *bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
978 /* setting BridgeDomainDetails */
980 jclass bddClass = (*env)->FindClass(env, "org/openvpp/vppjapi/vppBridgeDomainDetails");
981 if ((*env)->ExceptionCheck(env)) {
982 (*env)->ExceptionDescribe(env);
987 jmethodID midInit = (*env)->GetMethodID(env, bddClass, "<init>", "()V");
988 if ((*env)->ExceptionCheck(env)) {
989 (*env)->ExceptionDescribe(env);
993 jobject bddObj = (*env)->NewObject(env, bddClass, midInit);
995 u8 *vec_bd_name = vjbd_oper_name_from_id(bdm, bd_id);
996 if (NULL == vec_bd_name) {
1000 char *str_bd_name = (char*)format (0, "%s%c", vec_bd_name, 0);
1001 vec_free(vec_bd_name);
1002 jstring bdName = (*env)->NewStringUTF(env, str_bd_name);
1003 vec_free(str_bd_name);
1004 if (NULL == bdName) {
1008 jfieldID fidName = (*env)->GetFieldID(env, bddClass, "name", "Ljava/lang/String;");
1009 if ((*env)->ExceptionCheck(env)) {
1010 (*env)->ExceptionDescribe(env);
1014 (*env)->SetObjectField(env, bddObj, fidName, bdName);
1016 jfieldID fidBdId = (*env)->GetFieldID(env, bddClass, "bdId", "I");
1017 if ((*env)->ExceptionCheck(env)) {
1018 (*env)->ExceptionDescribe(env);
1022 (*env)->SetIntField(env, bddObj, fidBdId, bdId);
1024 jboolean flood = bd_oper->flood;
1025 jfieldID fidFlood = (*env)->GetFieldID(env, bddClass, "flood", "Z");
1026 if ((*env)->ExceptionCheck(env)) {
1027 (*env)->ExceptionDescribe(env);
1031 (*env)->SetBooleanField(env, bddObj, fidFlood, flood);
1033 jboolean uuFlood = bd_oper->uu_flood;
1034 jfieldID fidUuFlood = (*env)->GetFieldID(env, bddClass, "uuFlood", "Z");
1035 if ((*env)->ExceptionCheck(env)) {
1036 (*env)->ExceptionDescribe(env);
1040 (*env)->SetBooleanField(env, bddObj, fidUuFlood, uuFlood);
1042 jboolean forward = bd_oper->forward;
1043 jfieldID fidForward = (*env)->GetFieldID(env, bddClass, "forward", "Z");
1044 if ((*env)->ExceptionCheck(env)) {
1045 (*env)->ExceptionDescribe(env);
1049 (*env)->SetBooleanField(env, bddObj, fidForward, forward);
1051 jboolean learn = bd_oper->learn;
1052 jfieldID fidLearn = (*env)->GetFieldID(env, bddClass, "learn", "Z");
1053 if ((*env)->ExceptionCheck(env)) {
1054 (*env)->ExceptionDescribe(env);
1058 (*env)->SetBooleanField(env, bddObj, fidLearn, learn);
1060 jboolean arpTerm = bd_oper->arp_term;
1061 jfieldID fidArpTerm = (*env)->GetFieldID(env, bddClass, "arpTerm", "Z");
1062 if ((*env)->ExceptionCheck(env)) {
1063 (*env)->ExceptionDescribe(env);
1067 (*env)->SetBooleanField(env, bddObj, fidArpTerm, arpTerm);
1069 jstring bviInterfaceName = NULL;
1070 if (~0 != bd_oper->bvi_sw_if_index) {
1071 const char *str_if_name = interface_name_from_sw_if_index(bd_oper->bvi_sw_if_index);
1072 if (NULL == str_if_name) {
1073 clib_warning("Could not get interface name for sw_if_index %d", bd_oper->bvi_sw_if_index);
1077 bviInterfaceName = (*env)->NewStringUTF(env, str_if_name);
1078 if (NULL == bviInterfaceName) {
1083 jfieldID fidBviInterfaceName = (*env)->GetFieldID(env, bddClass, "bviInterfaceName", "Ljava/lang/String;");
1084 if ((*env)->ExceptionCheck(env)) {
1085 (*env)->ExceptionDescribe(env);
1089 (*env)->SetObjectField(env, bddObj, fidBviInterfaceName, bviInterfaceName);
1092 /* setting BridgeDomainInterfaceDetails */
1094 jclass bdidClass = (*env)->FindClass(env, "org/openvpp/vppjapi/vppBridgeDomainInterfaceDetails");
1095 if ((*env)->ExceptionCheck(env)) {
1096 (*env)->ExceptionDescribe(env);
1101 jfieldID fidInterfaceName = (*env)->GetFieldID(env, bdidClass, "interfaceName", "Ljava/lang/String;");
1102 if ((*env)->ExceptionCheck(env)) {
1103 (*env)->ExceptionDescribe(env);
1108 jfieldID fidSHG = (*env)->GetFieldID(env, bdidClass, "splitHorizonGroup", "B");
1109 if ((*env)->ExceptionCheck(env)) {
1110 (*env)->ExceptionDescribe(env);
1115 u32 len = vec_len(bd_oper->bd_sw_if_oper);
1116 ASSERT(len == bd_oper->n_sw_ifs);
1118 jobjectArray bdidArray = (*env)->NewObjectArray(env, len, bdidClass, NULL);
1121 for (i = 0; i < len; i++) {
1122 bd_sw_if_oper_t *sw_if_oper = &bd_oper->bd_sw_if_oper[i];
1124 jmethodID midBdidInit = (*env)->GetMethodID(env, bdidClass, "<init>", "()V");
1125 if ((*env)->ExceptionCheck(env)) {
1126 (*env)->ExceptionDescribe(env);
1130 jobject bdidObj = (*env)->NewObject(env, bdidClass, midBdidInit);
1131 (*env)->SetObjectArrayElement(env, bdidArray, i, bdidObj);
1133 u32 sw_if_index = sw_if_oper->sw_if_index;
1134 const char *str_if_name = interface_name_from_sw_if_index(sw_if_index);
1135 if (NULL == str_if_name) {
1139 jstring interfaceName = (*env)->NewStringUTF(env, str_if_name);
1140 if (NULL == interfaceName) {
1144 (*env)->SetObjectField(env, bdidObj, fidInterfaceName, interfaceName);
1146 jbyte shg = sw_if_oper->shg;
1147 (*env)->SetByteField(env, bdidObj, fidSHG, shg);
1150 jfieldID fidInterfaces = (*env)->GetFieldID(env, bddClass, "interfaces",
1151 "[Lorg/openvpp/vppjapi/vppBridgeDomainInterfaceDetails;");
1152 if ((*env)->ExceptionCheck(env)) {
1153 (*env)->ExceptionDescribe(env);
1157 (*env)->SetObjectField(env, bddObj, fidInterfaces, bdidArray);
1168 static jobject l2_fib_create_object(JNIEnv *env, bd_l2fib_oper_t *l2_fib)
1170 jclass l2FibClass = (*env)->FindClass(env, "org/openvpp/vppjapi/vppL2Fib");
1171 if ((*env)->ExceptionCheck(env)) {
1172 (*env)->ExceptionDescribe(env);
1176 jmethodID midL2FIbInit = (*env)->GetMethodID(env, l2FibClass, "<init>", "([BZLjava/lang/String;ZZ)V");
1177 if ((*env)->ExceptionCheck(env)) {
1178 (*env)->ExceptionDescribe(env);
1182 u32 sw_if_index = l2_fib->sw_if_index;
1183 const char *str_if_name = interface_name_from_sw_if_index(sw_if_index);
1184 if (NULL == str_if_name) {
1187 jstring interfaceName = (*env)->NewStringUTF(env, str_if_name);
1188 if (NULL == interfaceName) {
1192 jbyteArray physAddr = (*env)->NewByteArray(env, 6);
1193 (*env)->SetByteArrayRegion(env, physAddr, 0, 6,
1194 (signed char*)l2_fib->mac_addr.fields.mac);
1195 jboolean staticConfig = !l2_fib->learned;
1196 jstring outgoingInterface = interfaceName;
1197 jboolean filter = l2_fib->filter;
1198 jboolean bridgedVirtualInterface = l2_fib->bvi;
1200 jobject l2FibObj = (*env)->NewObject(env, l2FibClass, midL2FIbInit,
1201 physAddr, staticConfig, outgoingInterface, filter,
1202 bridgedVirtualInterface);
1207 JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_l2FibTableDump
1208 (JNIEnv * env, jobject obj, jint bd_id)
1210 vppjni_main_t *jm = &vppjni_main;
1211 vjbd_main_t * bdm = &jm->vjbd_main;
1212 vl_api_l2_fib_table_dump_t *mp;
1213 jobjectArray l2FibArray = NULL;
1214 vjbd_oper_t *bd_oper;
1221 vppjni_lock (jm, 17);
1223 //vjbd_l2fib_oper_reset (bdm);
1225 p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
1229 oper_bd_index = p[0];
1230 bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
1231 vec_reset_length (bd_oper->l2fib_oper);
1233 /* Get list of l2 fib table entries */
1234 M(L2_FIB_TABLE_DUMP, l2_fib_table_dump);
1235 mp->bd_id = ntohl(bd_id);
1238 /* Use a control ping for synchronization */
1240 vl_api_control_ping_t * mp;
1241 M(CONTROL_PING, control_ping);
1250 u32 count = vec_len(bd_oper->l2fib_oper);
1251 bd_l2fib_oper_t *l2fib_oper = bd_oper->l2fib_oper;
1253 jclass l2FibClass = (*env)->FindClass(env, "org/openvpp/vppjapi/vppL2Fib");
1254 if ((*env)->ExceptionCheck(env)) {
1255 (*env)->ExceptionDescribe(env);
1259 l2FibArray = (*env)->NewObjectArray(env, count, l2FibClass, NULL);
1262 for (i = 0; i < count; i++) {
1263 bd_l2fib_oper_t *l2_fib = &l2fib_oper[i];
1265 jobject l2FibObj = l2_fib_create_object(env, l2_fib);
1266 (*env)->SetObjectArrayElement(env, l2FibArray, pos, l2FibObj);
1278 vl_api_l2_fib_table_entry_t_handler (vl_api_l2_fib_table_entry_t * mp)
1280 //static u8 * mac_addr;
1281 vppjni_main_t *jm = &vppjni_main;
1282 vjbd_main_t * bdm = &jm->vjbd_main;
1283 vjbd_oper_t * bd_oper;
1284 u32 bd_id, oper_bd_index;
1285 //uword mhash_val_l2fi;
1286 bd_l2fib_oper_t * l2fib_oper;
1287 l2fib_u64_mac_t * l2fe_u64_mac = (l2fib_u64_mac_t *)&mp->mac;
1289 bd_id = ntohl (mp->bd_id);
1291 uword *p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
1295 oper_bd_index = (jint) p[0];
1296 bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
1299 vec_validate (mac_addr, MAC_ADDRESS_SIZE);
1300 memcpy (mac_addr, l2fe_u64_mac->fields.mac, MAC_ADDRESS_SIZE);
1301 mhash_val_l2fi = vec_len (bd_oper->l2fib_oper);
1302 if (mhash_elts (&bd_oper->l2fib_index_by_mac) == 0)
1303 mhash_init (&bd_oper->l2fib_index_by_mac, sizeof (u32), MAC_ADDRESS_SIZE);
1304 mhash_set_mem (&bd_oper->l2fib_index_by_mac, mac_addr, &mhash_val_l2fi, 0);
1307 vec_add2 (bd_oper->l2fib_oper, l2fib_oper, 1);
1309 l2fib_oper->bd_id = bd_id;
1310 l2fib_oper->mac_addr.raw = l2fib_mac_to_u64 (l2fe_u64_mac->fields.mac);
1311 l2fib_oper->sw_if_index = ntohl (mp->sw_if_index);
1312 l2fib_oper->learned = !mp->static_mac;
1313 l2fib_oper->filter = mp->filter_mac;
1314 l2fib_oper->bvi = mp->bvi_mac;
1317 /* cleanup handler for RX thread */
1318 void cleanup_rx_thread(void *arg)
1320 vppjni_main_t * jm = &vppjni_main;
1322 vppjni_lock (jm, 99);
1324 int getEnvStat = (*jm->jvm)->GetEnv(jm->jvm, (void **)&(jm->jenv), JNI_VERSION_1_6);
1325 if (getEnvStat == JNI_EVERSION) {
1326 clib_warning ("Unsupported JNI version\n");
1329 } else if (getEnvStat != JNI_EDETACHED) {
1330 (*jm->jvm)->DetachCurrentThread(jm->jvm);
1337 vl_api_show_version_reply_t_handler (vl_api_show_version_reply_t * mp)
1339 vppjni_main_t * jm = &vppjni_main;
1340 i32 retval = ntohl(mp->retval);
1343 clib_warning ("show version request succeeded(%d)");
1344 strncpy((char*)jm->program_name, (const char*)mp->program,
1345 sizeof(jm->program_name)-1);
1346 jm->program_name[sizeof(jm->program_name)-1] = 0;
1348 strncpy((char*)jm->build_directory, (const char*)mp->build_directory,
1349 sizeof(jm->build_directory)-1);
1350 jm->build_directory[sizeof(jm->build_directory)-1] = 0;
1352 strncpy((char*)jm->git_branch, (const char*)mp->git_branch,
1353 sizeof(jm->git_branch)-1);
1354 jm->git_branch[sizeof(jm->git_branch)-1] = 0;
1356 strncpy((char*)jm->build_date, (const char*)mp->build_date,
1357 sizeof(jm->build_date)-1);
1358 jm->build_date[sizeof(jm->build_date)-1] = 0;
1360 clib_error ("show version request failed(%d)", retval);
1362 jm->retval = retval;
1363 jm->result_ready = 1;
1366 static void vl_api_want_stats_reply_t_handler (vl_api_want_stats_reply_t * mp)
1368 vppjni_main_t * jm = &vppjni_main;
1369 jm->retval = mp->retval; // FIXME: vpp api does not do ntohl on this retval
1370 jm->result_ready = 1;
1373 // control ping needs to be very first thing called
1374 // to attach rx thread to java thread
1375 static void vl_api_control_ping_reply_t_handler
1376 (vl_api_control_ping_reply_t * mp)
1378 vppjni_main_t * jm = &vppjni_main;
1379 i32 retval = ntohl(mp->retval);
1380 u32 context = clib_host_to_net_u32 (mp->context);
1383 jm->retval = retval;
1385 // attach to java thread if not attached
1386 int getEnvStat = (*jm->jvm)->GetEnv(jm->jvm, (void **)&(jm->jenv), JNI_VERSION_1_6);
1387 if (getEnvStat == JNI_EDETACHED) {
1388 if ((*jm->jvm)->AttachCurrentThread(jm->jvm, (void **)&(jm->jenv), NULL) != 0) {
1389 clib_warning("Failed to attach thread\n");
1394 // workaround as we can't use pthread_cleanup_push
1395 pthread_key_create(&jm->cleanup_rx_thread_key, cleanup_rx_thread);
1396 // destructor is only called if the value of key is non null
1397 pthread_setspecific(jm->cleanup_rx_thread_key, (void *)1);
1398 } else if (getEnvStat == JNI_EVERSION) {
1399 clib_warning ("Unsupported JNI version\n");
1403 // jm->jenv is now stable global reference that can be reused
1405 // get issuer msg-id
1406 p = hash_get (jm->ping_hash, context);
1407 if (p != 0) { // ping marks end of some dump call
1408 JNIEnv *env = jm->jenv;
1409 u16 msg_id = (u16)p[0];
1411 // we will no longer need this
1412 hash_unset (jm->ping_hash, context);
1414 // get original caller obj
1415 p = hash_get (jm->callback_hash, context);
1417 if (p == 0) // don't have callback stored
1420 jobject obj = (jobject)p[0]; // object that called original call
1423 case VL_API_SW_INTERFACE_DUMP:
1424 if (0 != sw_if_dump_call_all_callbacks(obj)) {
1429 clib_warning("Unhandled control ping issuer msg-id: %d", msg_id);
1434 // free the saved obj
1435 hash_unset (jm->callback_hash, context);
1436 // delete global reference
1437 (*env)->DeleteGlobalRef(env, obj);
1440 jm->result_ready = 1;
1443 #define VPPJNI_DEBUG_COUNTERS 0
1445 static void vl_api_vnet_interface_counters_t_handler
1446 (vl_api_vnet_interface_counters_t *mp)
1448 vppjni_main_t *jm = &vppjni_main;
1449 CLIB_UNUSED(char *counter_name);
1450 u32 count, sw_if_index;
1452 static sw_interface_stats_t empty_stats = {0, };
1454 vppjni_lock (jm, 12);
1455 count = ntohl (mp->count);
1456 sw_if_index = ntohl (mp->first_sw_if_index);
1457 if (mp->is_combined == 0) {
1459 vp = (u64 *) mp->data;
1461 for (i = 0; i < count; i++) {
1462 sw_interface_details_t *sw_if = NULL;
1464 v = clib_mem_unaligned (vp, u64);
1465 v = clib_net_to_host_u64 (v);
1468 if (sw_if_index < vec_len(jm->sw_if_table))
1469 sw_if = vec_elt_at_index(jm->sw_if_table, sw_if_index);
1471 if (sw_if /* && (sw_if->admin_up_down == 1)*/ && sw_if->interface_name[0] != 0)
1473 vec_validate_init_empty(jm->sw_if_stats_by_sw_if_index, sw_if_index, empty_stats);
1474 sw_interface_stats_t * s = vec_elt_at_index(jm->sw_if_stats_by_sw_if_index, sw_if_index);
1476 s->sw_if_index = sw_if_index;
1479 switch (mp->vnet_counter_type) {
1480 case VNET_INTERFACE_COUNTER_DROP:
1481 counter_name = "drop";
1482 s->rx.pkts.discard = v;
1484 case VNET_INTERFACE_COUNTER_PUNT:
1485 counter_name = "punt";
1486 s->rx.pkts.unknown_proto = v;
1488 case VNET_INTERFACE_COUNTER_IP4:
1489 counter_name = "ip4";
1492 case VNET_INTERFACE_COUNTER_IP6:
1493 counter_name = "ip6";
1496 case VNET_INTERFACE_COUNTER_RX_NO_BUF:
1497 counter_name = "rx-no-buf";
1498 s->rx.pkts.fifo_full = v;
1500 case VNET_INTERFACE_COUNTER_RX_MISS:
1501 counter_name = "rx-miss";
1502 s->rx.pkts.miss = v;
1504 case VNET_INTERFACE_COUNTER_RX_ERROR:
1505 counter_name = "rx-error";
1506 s->rx.pkts.error = v;
1508 case VNET_INTERFACE_COUNTER_TX_ERROR:
1509 counter_name = "tx-error (fifo-full)";
1510 s->tx.pkts.fifo_full = v;
1513 counter_name = "bogus";
1517 #if VPPJNI_DEBUG_COUNTERS == 1
1518 clib_warning ("%s (%d): %s (%lld)\n", sw_if->interface_name, s->sw_if_index,
1527 vp = (vlib_counter_t *) mp->data;
1529 for (i = 0; i < count; i++) {
1530 sw_interface_details_t *sw_if = NULL;
1532 packets = clib_mem_unaligned (&vp->packets, u64);
1533 packets = clib_net_to_host_u64 (packets);
1534 bytes = clib_mem_unaligned (&vp->bytes, u64);
1535 bytes = clib_net_to_host_u64 (bytes);
1538 if (sw_if_index < vec_len(jm->sw_if_table))
1539 sw_if = vec_elt_at_index(jm->sw_if_table, sw_if_index);
1541 if (sw_if /* && (sw_if->admin_up_down == 1) */ && sw_if->interface_name[0] != 0)
1543 vec_validate_init_empty(jm->sw_if_stats_by_sw_if_index, sw_if_index, empty_stats);
1544 sw_interface_stats_t * s = vec_elt_at_index(jm->sw_if_stats_by_sw_if_index, sw_if_index);
1547 s->sw_if_index = sw_if_index;
1549 switch (mp->vnet_counter_type) {
1550 case VNET_INTERFACE_COUNTER_RX:
1551 s->rx.pkts.unicast = packets;
1552 s->rx.octets = bytes;
1553 counter_name = "rx";
1556 case VNET_INTERFACE_COUNTER_TX:
1557 s->tx.pkts.unicast = packets;
1558 s->tx.octets = bytes;
1559 counter_name = "tx";
1563 counter_name = "bogus";
1567 #if VPPJNI_DEBUG_COUNTERS == 1
1568 clib_warning ("%s (%d): %s.packets %lld\n",
1569 sw_if->interface_name,
1570 sw_if_index, counter_name, packets);
1571 clib_warning ("%s (%d): %s.bytes %lld\n",
1572 sw_if->interface_name,
1573 sw_if_index, counter_name, bytes);
1582 jint JNI_OnLoad(JavaVM *vm, void *reserved) {
1583 vppjni_main_t * jm = &vppjni_main;
1585 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_6) != JNI_OK) {
1589 jclass cls = (*env)->FindClass(env, "org/openvpp/vppjapi/vppApiCallbacks");
1590 if ((*env)->ExceptionCheck(env)) {
1591 (*env)->ExceptionDescribe(env);
1596 (*env)->GetMethodID(env, cls, "interfaceDetails", "(ILjava/lang/String;I[BBBBBIBBIIBBBBIIII)V");
1597 if ((*env)->ExceptionCheck(env)) {
1598 (*env)->ExceptionDescribe(env);
1602 // methods are not local references they stay forever
1603 // jclass is local reference let's hold global ref to it
1604 jm->jcls = (*env)->NewGlobalRef(env, cls);
1607 return JNI_VERSION_1_6;
1610 void JNI_OnUnload(JavaVM *vm, void *reserved) {
1611 vppjni_main_t * jm = &vppjni_main;
1613 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_6) != JNI_OK) {
1617 if (jm->jcls != NULL) {
1618 (*env)->DeleteGlobalRef(env, jm->jcls);
1626 #define foreach_vpe_api_msg \
1627 _(CONTROL_PING_REPLY, control_ping_reply) \
1628 _(SW_INTERFACE_DETAILS, sw_interface_details) \
1629 _(SHOW_VERSION_REPLY, show_version_reply) \
1630 _(WANT_STATS_REPLY, want_stats_reply) \
1631 _(VNET_INTERFACE_COUNTERS, vnet_interface_counters) \
1632 _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags) \
1633 _(BRIDGE_DOMAIN_DETAILS, bridge_domain_details) \
1634 _(BRIDGE_DOMAIN_SW_IF_DETAILS, bridge_domain_sw_if_details) \
1635 _(L2_FIB_TABLE_ENTRY, l2_fib_table_entry)
1637 static int connect_to_vpe(char *name)
1639 vppjni_main_t * jm = &vppjni_main;
1640 api_main_t * am = &api_main;
1642 if (vl_client_connect_to_vlib("/vpe-api", name, 32) < 0)
1645 jm->my_client_index = am->my_client_index;
1646 jm->vl_input_queue = am->shmem_hdr->vl_input_queue;
1649 vl_msg_api_set_handlers(VL_API_##N, #n, \
1650 vl_api_##n##_t_handler, \
1652 vl_api_##n##_t_endian, \
1653 vl_api_##n##_t_print, \
1654 sizeof(vl_api_##n##_t), 1);
1655 foreach_vpe_api_msg;
1662 /* Format an IP6 address. */
1663 u8 * format_ip6_address (u8 * s, va_list * args)
1665 ip6_address_t * a = va_arg (*args, ip6_address_t *);
1666 u32 max_zero_run = 0, this_zero_run = 0;
1667 int max_zero_run_index = -1, this_zero_run_index=0;
1668 int in_zero_run = 0, i;
1669 int last_double_colon = 0;
1671 /* Ugh, this is a pain. Scan forward looking for runs of 0's */
1672 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1674 if (a->as_u16[i] == 0)
1682 this_zero_run_index = i;
1689 /* offer to compress the biggest run of > 1 zero */
1690 if (this_zero_run > max_zero_run && this_zero_run > 1)
1692 max_zero_run_index = this_zero_run_index;
1693 max_zero_run = this_zero_run;
1703 if (this_zero_run > max_zero_run && this_zero_run > 1)
1705 max_zero_run_index = this_zero_run_index;
1706 max_zero_run = this_zero_run;
1710 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1712 if (i == max_zero_run_index)
1714 s = format (s, "::");
1715 i += max_zero_run - 1;
1716 last_double_colon = 1;
1720 s = format (s, "%s%x",
1721 (last_double_colon || i == 0) ? "" : ":",
1722 clib_net_to_host_u16 (a->as_u16[i]));
1723 last_double_colon = 0;
1730 /* Format an IP4 address. */
1731 u8 * format_ip4_address (u8 * s, va_list * args)
1733 u8 * a = va_arg (*args, u8 *);
1734 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);