blob: 740a3425d908904838b51f764823781a31a42e84 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#include <vnet/vnet.h>
16
17#define vl_api_version(n,v) static u32 vpe_api_version = (v);
18#include <api/vpe.api.h>
19#undef vl_api_version
20
21#include <jni.h>
22#include <japi/vppjni.h>
23#include <japi/vppjni_bridge_domain.h>
24#include <japi/org_openvpp_vppjapi_vppConn.h>
25#include <japi/org_openvpp_vppjapi_vppApi.h>
26
27#include <api/vpe_msg_enum.h>
28#define vl_typedefs /* define message structures */
29#include <api/vpe_all_api_h.h>
30#undef vl_typedefs
31
32#define vl_endianfun
33#include <api/vpe_all_api_h.h>
34#undef vl_endianfun
35
36/* instantiate all the print functions we know about */
37#define vl_print(handle, ...)
38#define vl_printfun
39#include <api/vpe_all_api_h.h>
40#undef vl_printfun
41
42static int connect_to_vpe(char *name);
43
44/*
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
48 * at a later date...
49 */
50void __stack_chk_guard (void) __attribute__((weak));
51void __stack_chk_guard (void) { }
52
53void vl_client_add_api_signatures (vl_api_memclnt_create_t *mp)
54{
55 /*
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().
58 */
59 mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
60}
61
62/* Note: non-static, called once to set up the initial intfc table */
63static int sw_interface_dump (vppjni_main_t * jm)
64{
65 vl_api_sw_interface_dump_t *mp;
66 f64 timeout;
67 hash_pair_t * p;
68 name_sort_t * nses = 0, * ns;
69 sw_interface_subif_t * sub = NULL;
70
71 /* Toss the old name table */
72 hash_foreach_pair (p, jm->sw_if_index_by_interface_name,
73 ({
74 vec_add2 (nses, ns, 1);
75 ns->name = (u8 *)(p->key);
76 ns->value = (u32) p->value[0];
77 }));
78
79 hash_free (jm->sw_if_index_by_interface_name);
80
81 vec_foreach (ns, nses)
82 vec_free (ns->name);
83
84 vec_free (nses);
85
86 vec_foreach (sub, jm->sw_if_subif_table) {
87 vec_free (sub->interface_name);
88 }
89 vec_free (jm->sw_if_subif_table);
90
91 /* recreate the interface name hash table */
92 jm->sw_if_index_by_interface_name
93 = hash_create_string (0, sizeof(uword));
94
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));
99 S;
100
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));
105 S;
106
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));
111 S;
112
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));
117 S;
118
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));
124 S;
125
126 /* Use a control ping for synchronization */
127 {
128 vl_api_control_ping_t * mp;
129 M(CONTROL_PING, control_ping);
130 S;
131 }
132 W;
133}
134
135JNIEXPORT jobject JNICALL Java_org_openvpp_vppjapi_vppConn_getVppVersion
136 (JNIEnv *env, jobject obj)
137{
138 vppjni_main_t * jm = &vppjni_main;
139 jmethodID constr;
140 // TODO: cache this
141 jclass cls = (*env)->FindClass(env, "org/openvpp/vppjapi/vppVersion");
142 if ((*env)->ExceptionCheck(env)) {
143 (*env)->ExceptionDescribe(env);
144 return NULL;
145 }
146
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);
150 return NULL;
151 }
152
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);
Damjan Mariona0d4a1a2015-12-12 14:40:59 +0100156 jstring version = (*env)->NewStringUTF(env, (char *)jm->version);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157 jstring buildDate = (*env)->NewStringUTF(env, (char *)jm->build_date);
158 vppjni_unlock (jm);
159
Damjan Mariona0d4a1a2015-12-12 14:40:59 +0100160 return (*env)->NewObject(env, cls, constr, progName, buildDir, version, buildDate);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161}
162
163static int jm_show_version (vppjni_main_t *jm)
164{
165 int rv;
166 vl_api_show_version_t *mp;
167 f64 timeout;
168
169 vppjni_lock (jm, 10);
170 M(SHOW_VERSION, show_version);
171
172 S;
173 vppjni_unlock (jm);
174 WNR;
175 return rv;
176}
177
178static int jm_stats_enable_disable (vppjni_main_t *jm, u8 enable)
179{
180 vl_api_want_stats_t * mp;
181 f64 timeout;
182 int rv;
183
184 vppjni_lock (jm, 13);
185
186 M(WANT_STATS, want_stats);
187
188 mp->enable_disable = enable;
189
190 S;
191 vppjni_unlock (jm);
192 WNR;
193
194 // already subscribed / already disabled (it's ok)
195 if (rv == -2 || rv == -3)
196 rv = 0;
197 return rv;
198}
199
200JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_clientConnect
201 (JNIEnv *env, jobject obj, jstring clientName)
202{
203 int rv;
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;
208 u8 * heap;
209 mheap_t * h;
210 f64 timeout;
211
212 /*
213 * Bail out now if we're not running as root
214 */
215 if (geteuid() != 0)
216 return -1;
217
218 if (jm->is_connected)
219 return -2;
220
221 if (jm->heap == 0)
222 clib_mem_init (0, 128<<20);
223
224 heap = clib_mem_get_per_cpu_heap();
225 h = mheap_header (heap);
226
227 client_name = (*env)->GetStringUTFChars (env, clientName, 0);
228
229 clib_time_init (&jm->clib_time);
230
231 rv = connect_to_vpe ((char *) client_name);
232
233 if (rv < 0)
234 clib_warning ("connection failed, rv %d", rv);
235
236 (*env)->ReleaseStringUTFChars (env, clientName, client_name);
237
238 if (rv == 0)
239 {
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;
244
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));
248 jm->api_main = am;
249 vjbd_main_init(&jm->vjbd_main);
250 jm->sw_if_index_by_interface_name =
251 hash_create_string (0, sizeof (uword));
252
253 {
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);
257 S;
258 WNR;
259
260 if (rv != 0) {
261 clib_warning ("first control ping failed: %d", rv);
262 }
263 }
264 rv = jm_show_version(jm);
265 if (rv != 0)
266 clib_warning ("unable to retrieve vpp version (rv: %d)", rv);
267 rv = sw_interface_dump(jm);
268 if (rv != 0)
269 clib_warning ("unable to retrieve interface list (rv: %d)", rv);
270 rv = jm_stats_enable_disable(jm, 1);
271 if (rv != 0)
272 clib_warning ("unable to subscribe to stats (rv: %d)", rv);
273 }
274 clib_warning ("clientConnect result: %d", rv);
275
276 return rv;
277}
278
279JNIEXPORT void JNICALL Java_org_openvpp_vppjapi_vppConn_clientDisconnect
280 (JNIEnv *env, jobject obj)
281{
282 u8 *save_heap;
283 vppjni_main_t * jm = &vppjni_main;
284 vl_client_disconnect_from_vlib();
285
286 save_heap = jm->heap;
287 memset (jm, 0, sizeof (*jm));
288 jm->heap = save_heap;
289}
290
291void vl_api_generic_reply_handler (vl_api_generic_reply_t *mp)
292{
293 api_main_t * am = &api_main;
294 u16 msg_id = clib_net_to_host_u16 (mp->_vl_msg_id);
295 trace_cfg_t *cfgp;
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);
301
302 cfgp = am->api_trace_cfg + msg_id;
303
304 if (!cfgp)
305 clib_warning ("msg id %d: no trace configuration\n", msg_id);
306 else
307 total_bytes = cfgp->size;
308
309 jm->context_id_received = context;
310
311 clib_warning("Received generic reply for msg id %d", msg_id);
312
313 /* A generic reply, successful, we're done */
314 if (retval >= 0 && total_bytes == sizeof(*mp))
315 return;
316
317 /* Save the reply */
318 vec_validate (saved_reply, total_bytes - 1);
319 memcpy (saved_reply, mp, total_bytes);
320
321 vppjni_lock (jm, 2);
322 hash_set (jm->reply_hash, context, saved_reply);
323 jm->saved_reply_count ++;
324 vppjni_unlock (jm);
325}
326
327JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_getRetval
328(JNIEnv * env, jobject obj, jint context, jint release)
329{
330 vppjni_main_t * jm = &vppjni_main;
331 vl_api_generic_reply_t * mp;
332 uword * p;
333 int rv = 0;
334
335 /* Dunno yet? */
336 if (context > jm->context_id_received)
337 return (VNET_API_ERROR_RESPONSE_NOT_READY);
338
339 vppjni_lock (jm, 1);
340 p = hash_get (jm->reply_hash, context);
341
342 /*
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.
345 */
346 if (p == 0)
347 goto out;
348
349 mp = (vl_api_generic_reply_t *) (p[0]);
350 rv = clib_net_to_host_u32 (mp->retval);
351
352 if (release)
353 {
354 u8 * free_me = (u8 *) mp;
355 vec_free (free_me);
356 hash_unset (jm->reply_hash, context);
357 jm->saved_reply_count --;
358 }
359
360out:
361 vppjni_unlock (jm);
362 return (rv);
363}
364
365JNIEXPORT jstring JNICALL Java_org_openvpp_vppjapi_vppConn_getInterfaceList
366 (JNIEnv * env, jobject obj, jstring name_filter)
367{
368 vppjni_main_t * jm = &vppjni_main;
369 jstring rv;
370 hash_pair_t * p;
371 name_sort_t * nses = 0, * ns;
372 const char *this_name;
373 const char * nf = (*env)->GetStringUTFChars (env, name_filter, NULL);
374 u8 * s = 0;
375 char *strcasestr (const char *, const char *);
376
377 vppjni_lock (jm, 4);
378
379 hash_foreach_pair (p, jm->sw_if_index_by_interface_name,
380 ({
381 this_name = (const char *)(p->key);
382 if (strlen (nf) == 0 || strcasestr (this_name, nf))
383 {
384 vec_add2 (nses, ns, 1);
385 ns->name = (u8 *)(p->key);
386 ns->value = (u32) p->value[0];
387 }
388 }));
389
390 vec_sort (nses, n1, n2,
391 strcmp ((char *)n1->name, (char *)n2->name));
392
393 vec_foreach (ns, nses)
394 s = format (s, "%s: %d, ", ns->name, ns->value);
395
396 _vec_len (s) = vec_len (s) - 2;
397 vec_terminate_c_string (s);
398 vppjni_unlock (jm);
399
400 vec_free (nses);
401
402 (*env)->ReleaseStringUTFChars (env, name_filter, nf);
403
404 rv = (*env)->NewStringUTF (env, (char *) s);
405 vec_free (s);
406
407 return rv;
408}
409
410JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_swIfIndexFromName
411 (JNIEnv * env, jobject obj, jstring interfaceName)
412{
413 vppjni_main_t * jm = &vppjni_main;
414 jint rv = -1;
415 const char * if_name = (*env)->GetStringUTFChars (env, interfaceName, NULL);
416 uword * p;
417
418 vppjni_lock (jm, 5);
419
420 p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name);
421
422 if (p != 0)
423 rv = (jint) p[0];
424
425 vppjni_unlock (jm);
426
427 (*env)->ReleaseStringUTFChars (env, interfaceName, if_name);
428
429 return rv;
430}
431
432JNIEXPORT jobject JNICALL Java_org_openvpp_vppjapi_vppConn_getInterfaceCounters
433(JNIEnv * env, jobject obj, jint swIfIndex)
434{
435 vppjni_main_t * jm = &vppjni_main;
436 sw_interface_stats_t *s;
437 u32 sw_if_index = swIfIndex;
438 jmethodID constr;
439 jobject result = NULL;
440
441 // TODO: cache this
442 jclass cls = (*env)->FindClass(env, "org/openvpp/vppjapi/vppInterfaceCounters");
443 if ((*env)->ExceptionCheck(env)) {
444 (*env)->ExceptionDescribe(env);
445 return NULL;
446 }
447
448 constr = (*env)->GetMethodID(env, cls, "<init>", "(JJJJJJJJJJJJJJJJJJJJJJ)V");
449 if ((*env)->ExceptionCheck(env)) {
450 (*env)->ExceptionDescribe(env);
451 return NULL;
452 }
453
454 vppjni_lock (jm, 16);
455
456 if (sw_if_index >= vec_len(jm->sw_if_stats_by_sw_if_index)) {
457 goto out;
458 }
459 s = &jm->sw_if_stats_by_sw_if_index[sw_if_index];
460 if (!s->valid) {
461 goto out;
462 }
463
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,
468 s->rx.pkts.miss,
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,
472 s->tx.pkts.miss);
473
474out:
475 vppjni_unlock (jm);
476 return result;
477}
478
479JNIEXPORT jstring JNICALL Java_org_openvpp_vppjapi_vppConn_interfaceNameFromSwIfIndex
480(JNIEnv * env, jobject obj, jint swIfIndex)
481{
482 vppjni_main_t * jm = &vppjni_main;
483 sw_interface_details_t *sw_if_details;
484 u32 sw_if_index;
485 jstring ifname = NULL;
486
487 vppjni_lock (jm, 8);
488
489 sw_if_index = swIfIndex;
490
491 if (sw_if_index >= vec_len(jm->sw_if_table)) {
492 goto out;
493 }
494 sw_if_details = &jm->sw_if_table[sw_if_index];
495 if (!sw_if_details->valid) {
496 goto out;
497 }
498
499 u8 * s = format (0, "%s%c", sw_if_details->interface_name, 0);
500 ifname = (*env)->NewStringUTF(env, (char *)s);
501
502out:
503 vppjni_unlock (jm);
504
505 return ifname;
506}
507
508JNIEXPORT void JNICALL Java_org_openvpp_vppjapi_vppConn_clearInterfaceTable
509(JNIEnv * env, jobject obj)
510{
511 vppjni_main_t * jm = &vppjni_main;
512
513 vppjni_lock (jm, 10);
514
515 vec_reset_length(jm->sw_if_table);
516
517 vppjni_unlock (jm);
518}
519
520
521JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_swInterfaceDump
522(JNIEnv * env, jobject obj, jbyte name_filter_valid, jbyteArray name_filter)
523{
524 vppjni_main_t *jm = &vppjni_main;
525 f64 timeout;
526 vl_api_sw_interface_dump_t * mp;
527 u32 my_context_id;
528 int rv;
529 rv = vppjni_sanity_check (jm);
530 if (rv) return rv;
531 vppjni_lock (jm, 7);
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);
535
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;
539
540 if (cnt > sizeof(mp->name_filter))
541 cnt = sizeof(mp->name_filter);
542
543 memcpy ((char *) mp->name_filter, name_filterP, cnt);
544 (*env)->ReleaseByteArrayElements (env, name_filter, name_filterP, 0);
545
546 clib_warning("interface filter (%d, %s, len: %d)", mp->name_filter_valid, (char *)mp->name_filter, cnt);
547
548 hash_set (jm->callback_hash, my_context_id, (*env)->NewGlobalRef(env, obj));
549 jm->collect_indices = 1;
550
551 S;
552 {
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);
557
558 // this control ping will mark end of interface dump
559 hash_set (jm->ping_hash, my_context_id, VL_API_SW_INTERFACE_DUMP);
560
561 S;
562 }
563 vppjni_unlock (jm);
564 WNR;
565 return my_context_id;
566}
567
568static int sw_if_dump_call_all_callbacks (jobject obj)
569{
570 vppjni_main_t * jm = &vppjni_main;
571 sw_interface_details_t *sw_if_details;
572 int rc = 0;
573 u32 i;
574
575 JNIEnv *env = jm->jenv;
576
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);
582
583 u8 * s = format (0, "%s%c", sw_if_details->interface_name, 0);
584
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;
600
601 jmethodID jmtdIfDetails = jm->jmtdIfDetails;
602
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;
613
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);
623 rc = 1;
624 goto out;
625 }
626 }
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);
632 rc = 1;
633 goto out;
634 }
635
636 clib_warning("Done");
637
638out:
639 jm->collect_indices = 0;
640 vec_reset_length(jm->sw_if_dump_if_indices);
641 return rc;
642}
643
644JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_findOrAddBridgeDomainId
645 (JNIEnv * env, jobject obj, jstring bridgeDomain)
646{
647 vppjni_main_t * jm = &vppjni_main;
648 jint rv = -1;
649 const char * bd_name = (*env)->GetStringUTFChars (env, bridgeDomain, NULL);
650
651 vppjni_lock (jm, 6);
652
653 rv = (jint)vjbd_find_or_add_bd (&jm->vjbd_main, (u8 *)bd_name);
654
655 vppjni_unlock (jm);
656
657 (*env)->ReleaseStringUTFChars (env, bridgeDomain, bd_name);
658
659 return rv;
660}
661
662JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_bridgeDomainIdFromName
663 (JNIEnv * env, jobject obj, jstring bridgeDomain)
664{
665 vppjni_main_t * jm = &vppjni_main;
666 jint rv = -1;
667 const char * bd_name = (*env)->GetStringUTFChars(env, bridgeDomain, NULL);
668
669 vppjni_lock(jm, 7);
670
671 rv = (jint)vjbd_id_from_name(&jm->vjbd_main, (u8 *)bd_name);
672
673 vppjni_unlock(jm);
674
675 (*env)->ReleaseStringUTFChars(env, bridgeDomain, bd_name);
676
677 return rv;
678}
679
680JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_bridgeDomainIdFromInterfaceName
681 (JNIEnv * env, jobject obj, jstring interfaceName)
682{
683 vppjni_main_t * jm = &vppjni_main;
684 vjbd_main_t * bdm = &jm->vjbd_main;
685 u32 sw_if_index;
686 jint rv = -1;
687 const char * if_name;
688 uword * p;
689
690 if_name = (*env)->GetStringUTFChars (env, interfaceName, NULL);
691
692 vppjni_lock (jm, 14);
693
694 p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name);
695
696 if (p != 0) {
697 sw_if_index = (jint) p[0];
698 p = hash_get (bdm->bd_id_by_sw_if_index, sw_if_index);
699 if (p != 0) {
700 rv = (jint) p[0];
701 }
702 }
703
704 vppjni_unlock (jm);
705
706 (*env)->ReleaseStringUTFChars (env, interfaceName, if_name);
707
708 return rv;
709}
710
711/*
712 * Special-case: build the interface table, maintain
713 * the next loopback sw_if_index vbl.
714 */
715static void vl_api_sw_interface_details_t_handler
716(vl_api_sw_interface_details_t * mp)
717{
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;
721 u32 sw_if_index;
722
723 vppjni_lock (jm, 1);
724
725 sw_if_index = ntohl (mp->sw_if_index);
726
727 u8 * s = format (0, "%s%c", mp->interface_name, 0);
728
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;
733 }
734
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;
738
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);
754
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;
765
766 hash_set_mem (jm->sw_if_index_by_interface_name, s, sw_if_index);
767 clib_warning("Got interface %s", (char *)s);
768
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;
772
773 vec_add2(jm->sw_if_subif_table, sub, 1);
774
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);
780
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;
789
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);
795 }
796 vppjni_unlock (jm);
797}
798
799static void vl_api_sw_interface_set_flags_t_handler
800(vl_api_sw_interface_set_flags_t * mp)
801{
802 /* $$$ nothing for the moment */
803}
804
805static jintArray create_array_of_bd_ids(JNIEnv * env, jint bd_id)
806{
807 vppjni_main_t *jm = &vppjni_main;
808 vjbd_main_t * bdm = &jm->vjbd_main;
809 u32 *buf = NULL;
810 u32 i;
811
812 if (bd_id != ~0) {
813 vec_add1(buf, bd_id);
814 } else {
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);
818 }
819 }
820
821 jintArray bdidArray = (*env)->NewIntArray(env, vec_len(buf));
822
823 (*env)->SetIntArrayRegion(env, bdidArray, 0, vec_len(buf), (int*)buf);
824
825 return bdidArray;
826}
827
828static void bridge_domain_oper_free(void)
829{
830 vppjni_main_t *jm = &vppjni_main;
831 vjbd_main_t *bdm = &jm->vjbd_main;
832 u32 i;
833
834 for (i = 0; i < vec_len(bdm->bd_oper); i++) {
835 vec_free(bdm->bd_oper->l2fib_oper);
836 }
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);
840}
841
842JNIEXPORT jintArray JNICALL Java_org_openvpp_vppjapi_vppConn_bridgeDomainDump
843(JNIEnv * env, jobject obj, jint bd_id)
844{
845 vppjni_main_t *jm = &vppjni_main;
846 vl_api_bridge_domain_dump_t * mp;
847 u32 my_context_id;
848 f64 timeout;
849 int rv;
850 rv = vppjni_sanity_check (jm);
851 if (rv) return NULL;
852
853 vppjni_lock (jm, 15);
854
855 if (~0 == bd_id) {
856 bridge_domain_oper_free();
857 }
858
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);
863 S;
864
865 /* Use a control ping for synchronization */
866 {
867 vl_api_control_ping_t * mp;
868 M(CONTROL_PING, control_ping);
869 S;
870 }
871
872 WNR;
873 if (0 != rv) {
874 return NULL;
875 }
876
877 jintArray ret = create_array_of_bd_ids(env, bd_id);
878
879 vppjni_unlock (jm);
880
881 return ret;
882}
883
884static void
885vl_api_bridge_domain_details_t_handler (vl_api_bridge_domain_details_t * mp)
886{
887 vppjni_main_t *jm = &vppjni_main;
888 vjbd_main_t * bdm = &jm->vjbd_main;
889 vjbd_oper_t * bd_oper;
890 u32 bd_id, bd_index;
891
892 bd_id = ntohl (mp->bd_id);
893
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);
897
898 hash_set(bdm->oper_bd_index_by_bd_id, bd_id, bd_index);
899
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);
908
909 bd_oper->bd_sw_if_oper = 0;
910}
911
912static void
913vl_api_bridge_domain_sw_if_details_t_handler
914(vl_api_bridge_domain_sw_if_details_t * mp)
915{
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;
920
921 bd_id = ntohl (mp->bd_id);
922 sw_if_index = ntohl (mp->sw_if_index);
923
924 uword *p;
925 p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
926 if (p == 0) {
927 clib_warning("Invalid bd_id %d in bridge_domain_sw_if_details_t_handler", bd_id);
928 return;
929 }
930 u32 oper_bd_index = (jint) p[0];
931 vjbd_oper_t *bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
932
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;
939
940 hash_set(bdm->bd_id_by_sw_if_index, sw_if_index, bd_id);
941}
942
943static const char* interface_name_from_sw_if_index(u32 sw_if_index)
944{
945 vppjni_main_t *jm = &vppjni_main;
946
947 if (sw_if_index >= vec_len(jm->sw_if_table)) {
948 return NULL;
949 }
950 if (!jm->sw_if_table[sw_if_index].valid) {
951 return NULL;
952 }
953 return (const char*)jm->sw_if_table[sw_if_index].interface_name;
954}
955
956JNIEXPORT jobject JNICALL Java_org_openvpp_vppjapi_vppConn_getBridgeDomainDetails
957(JNIEnv * env, jobject obj, jint bdId)
958{
959 vppjni_main_t *jm = &vppjni_main;
960 vjbd_main_t * bdm = &jm->vjbd_main;
961 u32 oper_bd_index;
962 u32 bd_id = bdId;
963 jobject rv = NULL;
964 uword *p;
965
966 vppjni_lock (jm, 16);
967
968 p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
969 if (p == 0) {
970 rv = NULL;
971 goto out;
972 }
973 oper_bd_index = (jint) p[0];
974
975 vjbd_oper_t *bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
976
977
978 /* setting BridgeDomainDetails */
979
980 jclass bddClass = (*env)->FindClass(env, "org/openvpp/vppjapi/vppBridgeDomainDetails");
981 if ((*env)->ExceptionCheck(env)) {
982 (*env)->ExceptionDescribe(env);
983 rv = NULL;
984 goto out;
985 }
986
987 jmethodID midInit = (*env)->GetMethodID(env, bddClass, "<init>", "()V");
988 if ((*env)->ExceptionCheck(env)) {
989 (*env)->ExceptionDescribe(env);
990 rv = NULL;
991 goto out;
992 }
993 jobject bddObj = (*env)->NewObject(env, bddClass, midInit);
994
995 u8 *vec_bd_name = vjbd_oper_name_from_id(bdm, bd_id);
996 if (NULL == vec_bd_name) {
997 rv = NULL;
998 goto out;
999 }
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) {
1005 rv = NULL;
1006 goto out;
1007 }
1008 jfieldID fidName = (*env)->GetFieldID(env, bddClass, "name", "Ljava/lang/String;");
1009 if ((*env)->ExceptionCheck(env)) {
1010 (*env)->ExceptionDescribe(env);
1011 rv = NULL;
1012 goto out;
1013 }
1014 (*env)->SetObjectField(env, bddObj, fidName, bdName);
1015
1016 jfieldID fidBdId = (*env)->GetFieldID(env, bddClass, "bdId", "I");
1017 if ((*env)->ExceptionCheck(env)) {
1018 (*env)->ExceptionDescribe(env);
1019 rv = NULL;
1020 goto out;
1021 }
1022 (*env)->SetIntField(env, bddObj, fidBdId, bdId);
1023
1024 jboolean flood = bd_oper->flood;
1025 jfieldID fidFlood = (*env)->GetFieldID(env, bddClass, "flood", "Z");
1026 if ((*env)->ExceptionCheck(env)) {
1027 (*env)->ExceptionDescribe(env);
1028 rv = NULL;
1029 goto out;
1030 }
1031 (*env)->SetBooleanField(env, bddObj, fidFlood, flood);
1032
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);
1037 rv = NULL;
1038 goto out;
1039 }
1040 (*env)->SetBooleanField(env, bddObj, fidUuFlood, uuFlood);
1041
1042 jboolean forward = bd_oper->forward;
1043 jfieldID fidForward = (*env)->GetFieldID(env, bddClass, "forward", "Z");
1044 if ((*env)->ExceptionCheck(env)) {
1045 (*env)->ExceptionDescribe(env);
1046 rv = NULL;
1047 goto out;
1048 }
1049 (*env)->SetBooleanField(env, bddObj, fidForward, forward);
1050
1051 jboolean learn = bd_oper->learn;
1052 jfieldID fidLearn = (*env)->GetFieldID(env, bddClass, "learn", "Z");
1053 if ((*env)->ExceptionCheck(env)) {
1054 (*env)->ExceptionDescribe(env);
1055 rv = NULL;
1056 goto out;
1057 }
1058 (*env)->SetBooleanField(env, bddObj, fidLearn, learn);
1059
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);
1064 rv = NULL;
1065 goto out;
1066 }
1067 (*env)->SetBooleanField(env, bddObj, fidArpTerm, arpTerm);
1068
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);
1074 rv = NULL;
1075 goto out;
1076 }
1077 bviInterfaceName = (*env)->NewStringUTF(env, str_if_name);
1078 if (NULL == bviInterfaceName) {
1079 rv = NULL;
1080 goto out;
1081 }
1082 }
1083 jfieldID fidBviInterfaceName = (*env)->GetFieldID(env, bddClass, "bviInterfaceName", "Ljava/lang/String;");
1084 if ((*env)->ExceptionCheck(env)) {
1085 (*env)->ExceptionDescribe(env);
1086 rv = NULL;
1087 goto out;
1088 }
1089 (*env)->SetObjectField(env, bddObj, fidBviInterfaceName, bviInterfaceName);
1090
1091
1092 /* setting BridgeDomainInterfaceDetails */
1093
1094 jclass bdidClass = (*env)->FindClass(env, "org/openvpp/vppjapi/vppBridgeDomainInterfaceDetails");
1095 if ((*env)->ExceptionCheck(env)) {
1096 (*env)->ExceptionDescribe(env);
1097 rv = NULL;
1098 goto out;
1099 }
1100
1101 jfieldID fidInterfaceName = (*env)->GetFieldID(env, bdidClass, "interfaceName", "Ljava/lang/String;");
1102 if ((*env)->ExceptionCheck(env)) {
1103 (*env)->ExceptionDescribe(env);
1104 rv = NULL;
1105 goto out;
1106 }
1107
1108 jfieldID fidSHG = (*env)->GetFieldID(env, bdidClass, "splitHorizonGroup", "B");
1109 if ((*env)->ExceptionCheck(env)) {
1110 (*env)->ExceptionDescribe(env);
1111 rv = NULL;
1112 goto out;
1113 }
1114
1115 u32 len = vec_len(bd_oper->bd_sw_if_oper);
1116 ASSERT(len == bd_oper->n_sw_ifs);
1117
1118 jobjectArray bdidArray = (*env)->NewObjectArray(env, len, bdidClass, NULL);
1119
1120 u32 i;
1121 for (i = 0; i < len; i++) {
1122 bd_sw_if_oper_t *sw_if_oper = &bd_oper->bd_sw_if_oper[i];
1123
1124 jmethodID midBdidInit = (*env)->GetMethodID(env, bdidClass, "<init>", "()V");
1125 if ((*env)->ExceptionCheck(env)) {
1126 (*env)->ExceptionDescribe(env);
1127 rv = NULL;
1128 goto out;
1129 }
1130 jobject bdidObj = (*env)->NewObject(env, bdidClass, midBdidInit);
1131 (*env)->SetObjectArrayElement(env, bdidArray, i, bdidObj);
1132
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) {
1136 rv = NULL;
1137 goto out;
1138 }
1139 jstring interfaceName = (*env)->NewStringUTF(env, str_if_name);
1140 if (NULL == interfaceName) {
1141 rv = NULL;
1142 goto out;
1143 }
1144 (*env)->SetObjectField(env, bdidObj, fidInterfaceName, interfaceName);
1145
1146 jbyte shg = sw_if_oper->shg;
1147 (*env)->SetByteField(env, bdidObj, fidSHG, shg);
1148 }
1149
1150 jfieldID fidInterfaces = (*env)->GetFieldID(env, bddClass, "interfaces",
1151 "[Lorg/openvpp/vppjapi/vppBridgeDomainInterfaceDetails;");
1152 if ((*env)->ExceptionCheck(env)) {
1153 (*env)->ExceptionDescribe(env);
1154 rv = NULL;
1155 goto out;
1156 }
1157 (*env)->SetObjectField(env, bddObj, fidInterfaces, bdidArray);
1158
1159 rv = bddObj;
1160
1161out:
1162
1163 vppjni_unlock (jm);
1164
1165 return rv;
1166}
1167
1168static jobject l2_fib_create_object(JNIEnv *env, bd_l2fib_oper_t *l2_fib)
1169{
1170 jclass l2FibClass = (*env)->FindClass(env, "org/openvpp/vppjapi/vppL2Fib");
1171 if ((*env)->ExceptionCheck(env)) {
1172 (*env)->ExceptionDescribe(env);
1173 return NULL;
1174 }
1175
1176 jmethodID midL2FIbInit = (*env)->GetMethodID(env, l2FibClass, "<init>", "([BZLjava/lang/String;ZZ)V");
1177 if ((*env)->ExceptionCheck(env)) {
1178 (*env)->ExceptionDescribe(env);
1179 return NULL;
1180 }
1181
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) {
1185 return NULL;
1186 }
1187 jstring interfaceName = (*env)->NewStringUTF(env, str_if_name);
1188 if (NULL == interfaceName) {
1189 return NULL;
1190 }
1191
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;
1199
1200 jobject l2FibObj = (*env)->NewObject(env, l2FibClass, midL2FIbInit,
1201 physAddr, staticConfig, outgoingInterface, filter,
1202 bridgedVirtualInterface);
1203
1204 return l2FibObj;
1205}
1206
1207JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_l2FibTableDump
1208(JNIEnv * env, jobject obj, jint bd_id)
1209{
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;
1215 u32 oper_bd_index;
1216 uword *p;
1217 f64 timeout;
1218 int rv;
1219 u32 i;
1220
1221 vppjni_lock (jm, 17);
1222
1223 //vjbd_l2fib_oper_reset (bdm);
1224
1225 p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
1226 if (p == 0) {
1227 goto done;
1228 }
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);
1232
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);
1236 S;
1237
1238 /* Use a control ping for synchronization */
1239 {
1240 vl_api_control_ping_t * mp;
1241 M(CONTROL_PING, control_ping);
1242 S;
1243 }
1244
1245 WNR;
1246 if (0 != rv) {
1247 goto done;
1248 }
1249
1250 u32 count = vec_len(bd_oper->l2fib_oper);
1251 bd_l2fib_oper_t *l2fib_oper = bd_oper->l2fib_oper;
1252
1253 jclass l2FibClass = (*env)->FindClass(env, "org/openvpp/vppjapi/vppL2Fib");
1254 if ((*env)->ExceptionCheck(env)) {
1255 (*env)->ExceptionDescribe(env);
1256 goto done;
1257 }
1258
1259 l2FibArray = (*env)->NewObjectArray(env, count, l2FibClass, NULL);
1260
1261 u32 pos = 0;
1262 for (i = 0; i < count; i++) {
1263 bd_l2fib_oper_t *l2_fib = &l2fib_oper[i];
1264 if (1) {
1265 jobject l2FibObj = l2_fib_create_object(env, l2_fib);
1266 (*env)->SetObjectArrayElement(env, l2FibArray, pos, l2FibObj);
1267 pos++;
1268 }
1269 }
1270
1271done:
1272 vppjni_unlock (jm);
1273
1274 return l2FibArray;
1275}
1276
1277static void
1278vl_api_l2_fib_table_entry_t_handler (vl_api_l2_fib_table_entry_t * mp)
1279{
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;
1288
1289 bd_id = ntohl (mp->bd_id);
1290
1291 uword *p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
1292 if (p == 0) {
1293 return;
1294 }
1295 oper_bd_index = (jint) p[0];
1296 bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
1297
1298#if 0
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);
1305#endif
1306
1307 vec_add2 (bd_oper->l2fib_oper, l2fib_oper, 1);
1308
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;
1315}
1316
1317/* cleanup handler for RX thread */
1318void cleanup_rx_thread(void *arg)
1319{
1320 vppjni_main_t * jm = &vppjni_main;
1321
1322 vppjni_lock (jm, 99);
1323
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");
1327 jm->retval = -999;
1328 goto out;
1329 } else if (getEnvStat != JNI_EDETACHED) {
1330 (*jm->jvm)->DetachCurrentThread(jm->jvm);
1331 }
1332out:
1333 vppjni_unlock (jm);
1334}
1335
1336static void
1337vl_api_show_version_reply_t_handler (vl_api_show_version_reply_t * mp)
1338{
1339 vppjni_main_t * jm = &vppjni_main;
1340 i32 retval = ntohl(mp->retval);
1341
1342 if (retval >= 0) {
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;
1347
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;
1351
Damjan Mariona0d4a1a2015-12-12 14:40:59 +01001352 strncpy((char*)jm->version, (const char*)mp->version,
1353 sizeof(jm->version)-1);
1354 jm->version[sizeof(jm->version)-1] = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001355
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;
1359 } else {
1360 clib_error ("show version request failed(%d)", retval);
1361 }
1362 jm->retval = retval;
1363 jm->result_ready = 1;
1364}
1365
1366static void vl_api_want_stats_reply_t_handler (vl_api_want_stats_reply_t * mp)
1367{
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;
1371}
1372
1373// control ping needs to be very first thing called
1374// to attach rx thread to java thread
1375static void vl_api_control_ping_reply_t_handler
1376(vl_api_control_ping_reply_t * mp)
1377{
1378 vppjni_main_t * jm = &vppjni_main;
1379 i32 retval = ntohl(mp->retval);
1380 u32 context = clib_host_to_net_u32 (mp->context);
1381 uword *p = NULL;
1382
1383 jm->retval = retval;
1384
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");
1390 jm->retval = -999;
1391 goto out;
1392 }
1393
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");
1400 jm->retval = -999;
1401 goto out;
1402 }
1403 // jm->jenv is now stable global reference that can be reused
1404
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];
1410
1411 // we will no longer need this
1412 hash_unset (jm->ping_hash, context);
1413
1414 // get original caller obj
1415 p = hash_get (jm->callback_hash, context);
1416
1417 if (p == 0) // don't have callback stored
1418 goto out;
1419
1420 jobject obj = (jobject)p[0]; // object that called original call
1421
1422 switch (msg_id) {
1423 case VL_API_SW_INTERFACE_DUMP:
1424 if (0 != sw_if_dump_call_all_callbacks(obj)) {
1425 goto out2;
1426 }
1427 break;
1428 default:
1429 clib_warning("Unhandled control ping issuer msg-id: %d", msg_id);
1430 goto out2;
1431 break;
1432 }
1433 out2:
1434 // free the saved obj
1435 hash_unset (jm->callback_hash, context);
1436 // delete global reference
1437 (*env)->DeleteGlobalRef(env, obj);
1438 }
1439out:
1440 jm->result_ready = 1;
1441}
1442
1443#define VPPJNI_DEBUG_COUNTERS 0
1444
1445static void vl_api_vnet_interface_counters_t_handler
1446(vl_api_vnet_interface_counters_t *mp)
1447{
1448 vppjni_main_t *jm = &vppjni_main;
1449 CLIB_UNUSED(char *counter_name);
1450 u32 count, sw_if_index;
1451 int i;
1452 static sw_interface_stats_t empty_stats = {0, };
1453
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) {
1458 u64 * vp, v;
1459 vp = (u64 *) mp->data;
1460
1461 for (i = 0; i < count; i++) {
1462 sw_interface_details_t *sw_if = NULL;
1463
1464 v = clib_mem_unaligned (vp, u64);
1465 v = clib_net_to_host_u64 (v);
1466 vp++;
1467
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);
1470
1471 if (sw_if /* && (sw_if->admin_up_down == 1)*/ && sw_if->interface_name[0] != 0)
1472 {
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);
1475
1476 s->sw_if_index = sw_if_index;
1477 s->valid = 1;
1478
1479 switch (mp->vnet_counter_type) {
1480 case VNET_INTERFACE_COUNTER_DROP:
1481 counter_name = "drop";
1482 s->rx.pkts.discard = v;
1483 break;
1484 case VNET_INTERFACE_COUNTER_PUNT:
1485 counter_name = "punt";
1486 s->rx.pkts.unknown_proto = v;
1487 break;
1488 case VNET_INTERFACE_COUNTER_IP4:
1489 counter_name = "ip4";
1490 s->rx.pkts.ip4 = v;
1491 break;
1492 case VNET_INTERFACE_COUNTER_IP6:
1493 counter_name = "ip6";
1494 s->rx.pkts.ip6 = v;
1495 break;
1496 case VNET_INTERFACE_COUNTER_RX_NO_BUF:
1497 counter_name = "rx-no-buf";
1498 s->rx.pkts.fifo_full = v;
1499 break;
1500 case VNET_INTERFACE_COUNTER_RX_MISS:
1501 counter_name = "rx-miss";
1502 s->rx.pkts.miss = v;
1503 break;
1504 case VNET_INTERFACE_COUNTER_RX_ERROR:
1505 counter_name = "rx-error";
1506 s->rx.pkts.error = v;
1507 break;
1508 case VNET_INTERFACE_COUNTER_TX_ERROR:
1509 counter_name = "tx-error (fifo-full)";
1510 s->tx.pkts.fifo_full = v;
1511 break;
1512 default:
1513 counter_name = "bogus";
1514 break;
1515 }
1516
1517#if VPPJNI_DEBUG_COUNTERS == 1
1518 clib_warning ("%s (%d): %s (%lld)\n", sw_if->interface_name, s->sw_if_index,
1519 counter_name, v);
1520#endif
1521 }
1522 sw_if_index++;
1523 }
1524 } else {
1525 vlib_counter_t *vp;
1526 u64 packets, bytes;
1527 vp = (vlib_counter_t *) mp->data;
1528
1529 for (i = 0; i < count; i++) {
1530 sw_interface_details_t *sw_if = NULL;
1531
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);
1536 vp++;
1537
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);
1540
1541 if (sw_if /* && (sw_if->admin_up_down == 1) */ && sw_if->interface_name[0] != 0)
1542 {
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);
1545
1546 s->valid = 1;
1547 s->sw_if_index = sw_if_index;
1548
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";
1554 break;
1555
1556 case VNET_INTERFACE_COUNTER_TX:
1557 s->tx.pkts.unicast = packets;
1558 s->tx.octets = bytes;
1559 counter_name = "tx";
1560 break;
1561
1562 default:
1563 counter_name = "bogus";
1564 break;
1565 }
1566
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);
1574#endif
1575 }
1576 sw_if_index++;
1577 }
1578 }
1579 vppjni_unlock (jm);
1580}
1581
1582jint JNI_OnLoad(JavaVM *vm, void *reserved) {
1583 vppjni_main_t * jm = &vppjni_main;
1584 JNIEnv* env;
1585 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_6) != JNI_OK) {
1586 return JNI_ERR;
1587 }
1588
1589 jclass cls = (*env)->FindClass(env, "org/openvpp/vppjapi/vppApiCallbacks");
1590 if ((*env)->ExceptionCheck(env)) {
1591 (*env)->ExceptionDescribe(env);
1592 return JNI_ERR;
1593 }
1594
1595 jm->jmtdIfDetails =
1596 (*env)->GetMethodID(env, cls, "interfaceDetails", "(ILjava/lang/String;I[BBBBBIBBIIBBBBIIII)V");
1597 if ((*env)->ExceptionCheck(env)) {
1598 (*env)->ExceptionDescribe(env);
1599 return JNI_ERR;
1600 }
1601
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);
1605
1606 jm->jvm = vm;
1607 return JNI_VERSION_1_6;
1608}
1609
1610void JNI_OnUnload(JavaVM *vm, void *reserved) {
1611 vppjni_main_t * jm = &vppjni_main;
1612 JNIEnv* env;
1613 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_6) != JNI_OK) {
1614 return;
1615 }
1616
1617 if (jm->jcls != NULL) {
1618 (*env)->DeleteGlobalRef(env, jm->jcls);
1619 jm->jcls = NULL;
1620 }
1621
1622 jm->jenv = NULL;
1623 jm->jvm = NULL;
1624}
1625
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)
1636
1637static int connect_to_vpe(char *name)
1638{
1639 vppjni_main_t * jm = &vppjni_main;
1640 api_main_t * am = &api_main;
1641
1642 if (vl_client_connect_to_vlib("/vpe-api", name, 32) < 0)
1643 return -1;
1644
1645 jm->my_client_index = am->my_client_index;
1646 jm->vl_input_queue = am->shmem_hdr->vl_input_queue;
1647
1648#define _(N,n) \
1649 vl_msg_api_set_handlers(VL_API_##N, #n, \
1650 vl_api_##n##_t_handler, \
1651 vl_noop_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;
1656#undef _
1657
1658
1659 return 0;
1660}
1661
1662/* Format an IP6 address. */
1663u8 * format_ip6_address (u8 * s, va_list * args)
1664{
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;
1670
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++)
1673 {
1674 if (a->as_u16[i] == 0)
1675 {
1676 if (in_zero_run)
1677 this_zero_run++;
1678 else
1679 {
1680 in_zero_run = 1;
1681 this_zero_run =1;
1682 this_zero_run_index = i;
1683 }
1684 }
1685 else
1686 {
1687 if (in_zero_run)
1688 {
1689 /* offer to compress the biggest run of > 1 zero */
1690 if (this_zero_run > max_zero_run && this_zero_run > 1)
1691 {
1692 max_zero_run_index = this_zero_run_index;
1693 max_zero_run = this_zero_run;
1694 }
1695 }
1696 in_zero_run = 0;
1697 this_zero_run = 0;
1698 }
1699 }
1700
1701 if (in_zero_run)
1702 {
1703 if (this_zero_run > max_zero_run && this_zero_run > 1)
1704 {
1705 max_zero_run_index = this_zero_run_index;
1706 max_zero_run = this_zero_run;
1707 }
1708 }
1709
1710 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1711 {
1712 if (i == max_zero_run_index)
1713 {
1714 s = format (s, "::");
1715 i += max_zero_run - 1;
1716 last_double_colon = 1;
1717 }
1718 else
1719 {
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;
1724 }
1725 }
1726
1727 return s;
1728}
1729
1730/* Format an IP4 address. */
1731u8 * format_ip4_address (u8 * s, va_list * args)
1732{
1733 u8 * a = va_arg (*args, u8 *);
1734 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
1735}
1736
1737