blob: 45011467ebd461d686f7ce5b175a44b4e7a2947c [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>
Robert Varga81d99ac2016-01-30 18:30:36 +010024#include <japi/vppjni_env.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070025#include <japi/org_openvpp_vppjapi_vppConn.h>
26#include <japi/org_openvpp_vppjapi_vppApi.h>
27
28#include <api/vpe_msg_enum.h>
29#define vl_typedefs /* define message structures */
Robert Vargad0f92092016-02-10 14:39:57 +010030#include <api/vpe_all_api_h.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070031#undef vl_typedefs
32
Robert Vargad0f92092016-02-10 14:39:57 +010033#define vl_endianfun
34#include <api/vpe_all_api_h.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070035#undef vl_endianfun
36
37/* instantiate all the print functions we know about */
38#define vl_print(handle, ...)
39#define vl_printfun
40#include <api/vpe_all_api_h.h>
41#undef vl_printfun
42
Dave Wallacebf8c15e2015-12-17 20:54:54 -050043#define VPPJNI_DEBUG 0
44
45#if VPPJNI_DEBUG == 1
46 #define DEBUG_LOG(...) clib_warning(__VA_ARGS__)
47#else
48 #define DEBUG_LOG(...)
49#endif
50
Ed Warnickecb9cada2015-12-08 15:45:58 -070051static int connect_to_vpe(char *name);
52
Robert Vargad0f92092016-02-10 14:39:57 +010053/*
Ed Warnickecb9cada2015-12-08 15:45:58 -070054 * The Java runtime isn't compile w/ -fstack-protector,
55 * so we have to supply missing external references for the
56 * regular vpp libraries. Weak reference in case folks get religion
57 * at a later date...
58 */
59void __stack_chk_guard (void) __attribute__((weak));
60void __stack_chk_guard (void) { }
61
Robert Varga81d99ac2016-01-30 18:30:36 +010062BIND_JAPI_CLASS(vppBridgeDomainDetails, "()V");
63BIND_JAPI_BOOL_FIELD(vppBridgeDomainDetails, arpTerm);
64BIND_JAPI_BOOL_FIELD(vppBridgeDomainDetails, flood);
65BIND_JAPI_BOOL_FIELD(vppBridgeDomainDetails, forward);
66BIND_JAPI_BOOL_FIELD(vppBridgeDomainDetails, learn);
67BIND_JAPI_BOOL_FIELD(vppBridgeDomainDetails, uuFlood);
68BIND_JAPI_INT_FIELD(vppBridgeDomainDetails, bdId);
69BIND_JAPI_STRING_FIELD(vppBridgeDomainDetails, name);
70BIND_JAPI_STRING_FIELD(vppBridgeDomainDetails, bviInterfaceName);
71BIND_JAPI_OBJ_FIELD(vppBridgeDomainDetails, interfaces, "[Lorg/openvpp/vppjapi/vppBridgeDomainInterfaceDetails;");
72
73BIND_JAPI_CLASS(vppBridgeDomainInterfaceDetails, "()V");
74BIND_JAPI_BYTE_FIELD(vppBridgeDomainInterfaceDetails, splitHorizonGroup);
75BIND_JAPI_STRING_FIELD(vppBridgeDomainInterfaceDetails, interfaceName);
76
77BIND_JAPI_CLASS(vppInterfaceCounters, "(JJJJJJJJJJJJJJJJJJJJJJ)V");
78BIND_JAPI_CLASS(vppInterfaceDetails, "(ILjava/lang/String;I[BBBBBIBBIIBBBBIIII)V");
79BIND_JAPI_CLASS(vppIPv4Address, "(IB)V");
80BIND_JAPI_CLASS(vppIPv6Address, "([BB)V");
81BIND_JAPI_CLASS(vppL2Fib, "([BZLjava/lang/String;ZZ)V");
82BIND_JAPI_CLASS(vppVersion, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
83BIND_JAPI_CLASS(vppVxlanTunnelDetails, "(IIIII)V");
84
Robert Vargad0f92092016-02-10 14:39:57 +010085void vl_client_add_api_signatures (vl_api_memclnt_create_t *mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -070086{
Robert Vargad0f92092016-02-10 14:39:57 +010087 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -070088 * Send the main API signature in slot 0. This bit of code must
89 * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
90 */
91 mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
92}
93
94/* Note: non-static, called once to set up the initial intfc table */
95static int sw_interface_dump (vppjni_main_t * jm)
96{
97 vl_api_sw_interface_dump_t *mp;
98 f64 timeout;
99 hash_pair_t * p;
100 name_sort_t * nses = 0, * ns;
101 sw_interface_subif_t * sub = NULL;
102
103 /* Toss the old name table */
Robert Vargad0f92092016-02-10 14:39:57 +0100104 hash_foreach_pair (p, jm->sw_if_index_by_interface_name,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105 ({
106 vec_add2 (nses, ns, 1);
107 ns->name = (u8 *)(p->key);
108 ns->value = (u32) p->value[0];
109 }));
110
111 hash_free (jm->sw_if_index_by_interface_name);
112
113 vec_foreach (ns, nses)
114 vec_free (ns->name);
115
116 vec_free (nses);
117
118 vec_foreach (sub, jm->sw_if_subif_table) {
119 vec_free (sub->interface_name);
120 }
121 vec_free (jm->sw_if_subif_table);
122
123 /* recreate the interface name hash table */
Robert Vargad0f92092016-02-10 14:39:57 +0100124 jm->sw_if_index_by_interface_name
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125 = hash_create_string (0, sizeof(uword));
126
127 /* Get list of ethernets */
128 M(SW_INTERFACE_DUMP, sw_interface_dump);
129 mp->name_filter_valid = 1;
Robert Vargad0f92092016-02-10 14:39:57 +0100130 strncpy ((char *) mp->name_filter, "Ether", sizeof(mp->name_filter-1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131 S;
132
133 /* and local / loopback interfaces */
134 M(SW_INTERFACE_DUMP, sw_interface_dump);
135 mp->name_filter_valid = 1;
Robert Vargad0f92092016-02-10 14:39:57 +0100136 strncpy ((char *) mp->name_filter, "lo", sizeof(mp->name_filter-1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137 S;
138
139 /* and vxlan tunnel interfaces */
140 M(SW_INTERFACE_DUMP, sw_interface_dump);
141 mp->name_filter_valid = 1;
Robert Vargad0f92092016-02-10 14:39:57 +0100142 strncpy ((char *) mp->name_filter, "vxlan", sizeof(mp->name_filter-1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143 S;
144
145 /* and tap tunnel interfaces */
146 M(SW_INTERFACE_DUMP, sw_interface_dump);
147 mp->name_filter_valid = 1;
Robert Vargad0f92092016-02-10 14:39:57 +0100148 strncpy ((char *) mp->name_filter, "tap", sizeof(mp->name_filter-1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 S;
150
151 /* and l2tpv3 tunnel interfaces */
152 M(SW_INTERFACE_DUMP, sw_interface_dump);
153 mp->name_filter_valid = 1;
Robert Vargad0f92092016-02-10 14:39:57 +0100154 strncpy ((char *) mp->name_filter, "l2tpv3_tunnel",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155 sizeof(mp->name_filter-1));
156 S;
157
158 /* Use a control ping for synchronization */
159 {
160 vl_api_control_ping_t * mp;
161 M(CONTROL_PING, control_ping);
162 S;
163 }
164 W;
165}
166
Dave Wallaceba474a22016-02-09 23:09:41 -0500167JNIEXPORT jobject JNICALL Java_org_openvpp_vppjapi_vppConn_getVppVersion0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168 (JNIEnv *env, jobject obj)
169{
Robert Vargad0f92092016-02-10 14:39:57 +0100170 vppjni_main_t * jm = &vppjni_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171
Robert Vargad0f92092016-02-10 14:39:57 +0100172 vppjni_lock (jm, 11);
173 jstring progName = (*env)->NewStringUTF(env, (char *)jm->program_name);
174 jstring buildDir = (*env)->NewStringUTF(env, (char *)jm->build_directory);
175 jstring version = (*env)->NewStringUTF(env, (char *)jm->version);
176 jstring buildDate = (*env)->NewStringUTF(env, (char *)jm->build_date);
177 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178
Robert Vargad0f92092016-02-10 14:39:57 +0100179 return vppVersionObject(env, progName, buildDir, version, buildDate);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180}
181
182static int jm_show_version (vppjni_main_t *jm)
183{
Robert Vargad0f92092016-02-10 14:39:57 +0100184 int rv;
185 vl_api_show_version_t *mp;
186 f64 timeout;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187
Robert Vargad0f92092016-02-10 14:39:57 +0100188 vppjni_lock (jm, 10);
189 M(SHOW_VERSION, show_version);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190
Robert Vargad0f92092016-02-10 14:39:57 +0100191 S;
192 vppjni_unlock (jm);
193 WNR;
194 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195}
196
197static int jm_stats_enable_disable (vppjni_main_t *jm, u8 enable)
198{
Robert Vargad0f92092016-02-10 14:39:57 +0100199 vl_api_want_stats_t * mp;
200 f64 timeout;
201 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202
Robert Vargad0f92092016-02-10 14:39:57 +0100203 vppjni_lock (jm, 13);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204
Robert Vargad0f92092016-02-10 14:39:57 +0100205 M(WANT_STATS, want_stats);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206
Robert Vargad0f92092016-02-10 14:39:57 +0100207 mp->enable_disable = enable;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208
Robert Vargad0f92092016-02-10 14:39:57 +0100209 S;
210 vppjni_unlock (jm);
211 WNR;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212
Robert Vargad0f92092016-02-10 14:39:57 +0100213 // already subscribed / already disabled (it's ok)
214 if (rv == -2 || rv == -3)
215 rv = 0;
216 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217}
218
Dave Wallaceba474a22016-02-09 23:09:41 -0500219JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_setInterfaceDescription0
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500220 (JNIEnv *env, jobject obj, jstring ifName, jstring ifDesc)
221{
222 int rv = 0;
223 vppjni_main_t * jm = &vppjni_main;
224 uword * p;
225 u32 sw_if_index = ~0;
226 sw_if_config_t *cfg;
227
228 const char *if_name_str = (*env)->GetStringUTFChars (env, ifName, 0);
229 const char *if_desc_str = (*env)->GetStringUTFChars (env, ifDesc, 0);
230
231 vppjni_lock (jm, 23);
232
233 p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name_str);
234 if (p == 0) {
235 rv = -1;
236 goto out;
237 }
238 sw_if_index = (jint) p[0];
239
240 u8 *if_desc = 0;
241 vec_validate_init_c_string (if_desc, if_desc_str, strlen(if_desc_str));
242 (*env)->ReleaseStringUTFChars (env, ifDesc, if_desc_str);
243
244 p = hash_get (jm->sw_if_config_by_sw_if_index, sw_if_index);
245 if (p != 0) {
246 cfg = (sw_if_config_t *) (p[0]);
247 if (cfg->desc)
248 vec_free(cfg->desc);
Robert Vargad0f92092016-02-10 14:39:57 +0100249 } else {
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500250 cfg = (sw_if_config_t *) clib_mem_alloc(sizeof(sw_if_config_t));
251 hash_set (jm->sw_if_config_by_sw_if_index, sw_if_index, cfg);
252 }
253
254 cfg->desc = if_desc;
255
256out:
257 (*env)->ReleaseStringUTFChars (env, ifName, if_name_str);
258 vppjni_unlock (jm);
259 return rv;
260}
261
Dave Wallaceba474a22016-02-09 23:09:41 -0500262JNIEXPORT jstring JNICALL Java_org_openvpp_vppjapi_vppConn_getInterfaceDescription0
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500263(JNIEnv * env, jobject obj, jstring ifName)
264{
265 vppjni_main_t * jm = &vppjni_main;
266 u32 sw_if_index = ~0;
267 uword * p;
268 const char *if_name_str = (*env)->GetStringUTFChars (env, ifName, 0);
269 jstring ifDesc = NULL;
270
271 vppjni_lock (jm, 24);
272 p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name_str);
273 if (p == 0)
274 goto out;
275
276 sw_if_index = (jint) p[0];
277
278 p = hash_get (jm->sw_if_config_by_sw_if_index, sw_if_index);
279 if (p == 0)
280 goto out;
281
282 sw_if_config_t *cfg = (sw_if_config_t *) (p[0]);
283 u8 * s = format (0, "%s%c", cfg->desc, 0);
284 ifDesc = (*env)->NewStringUTF(env, (char *)s);
285
286out:
287 vppjni_unlock (jm);
288
289 return ifDesc;
290}
291
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_clientConnect
293 (JNIEnv *env, jobject obj, jstring clientName)
294{
Robert Vargad0f92092016-02-10 14:39:57 +0100295 int rv;
296 const char *client_name;
297 void vl_msg_reply_handler_hookup(void);
298 vppjni_main_t * jm = &vppjni_main;
299 api_main_t * am = &api_main;
300 u8 * heap;
301 mheap_t * h;
302 f64 timeout;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303
Robert Vargad0f92092016-02-10 14:39:57 +0100304 /*
305 * Bail out now if we're not running as root
306 */
307 if (geteuid() != 0)
308 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309
Robert Vargad0f92092016-02-10 14:39:57 +0100310 if (jm->is_connected)
311 return -2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312
Robert Vargad0f92092016-02-10 14:39:57 +0100313 if (jm->heap == 0)
314 clib_mem_init (0, 128<<20);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315
Robert Vargad0f92092016-02-10 14:39:57 +0100316 heap = clib_mem_get_per_cpu_heap();
317 h = mheap_header (heap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318
Robert Vargad0f92092016-02-10 14:39:57 +0100319 client_name = (*env)->GetStringUTFChars (env, clientName, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320
Robert Vargad0f92092016-02-10 14:39:57 +0100321 clib_time_init (&jm->clib_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322
Robert Vargad0f92092016-02-10 14:39:57 +0100323 rv = connect_to_vpe ((char *) client_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324
Robert Vargad0f92092016-02-10 14:39:57 +0100325 if (rv < 0)
326 clib_warning ("connection failed, rv %d", rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327
Robert Vargad0f92092016-02-10 14:39:57 +0100328 (*env)->ReleaseStringUTFChars (env, clientName, client_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329
Robert Vargad0f92092016-02-10 14:39:57 +0100330 if (rv == 0) {
331 vl_msg_reply_handler_hookup ();
332 jm->is_connected = 1;
333 /* make the main heap thread-safe */
334 h->flags |= MHEAP_FLAG_THREAD_SAFE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335
Robert Vargad0f92092016-02-10 14:39:57 +0100336 jm->reply_hash = hash_create (0, sizeof (uword));
337 //jm->callback_hash = hash_create (0, sizeof (uword));
338 //jm->ping_hash = hash_create (0, sizeof (uword));
339 jm->api_main = am;
340 vjbd_main_init(&jm->vjbd_main);
341 jm->sw_if_index_by_interface_name =
342 hash_create_string (0, sizeof (uword));
343 jm->sw_if_config_by_sw_if_index =
344 hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345
Robert Vargad0f92092016-02-10 14:39:57 +0100346 {
347 // call control ping first to attach rx thread to java thread
348 vl_api_control_ping_t * mp;
349 M(CONTROL_PING, control_ping);
350 S;
351 WNR;
352
353 if (rv != 0) {
354 clib_warning ("first control ping failed: %d", rv);
355 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356 }
Robert Vargad0f92092016-02-10 14:39:57 +0100357 rv = jm_show_version(jm);
358 if (rv != 0)
359 clib_warning ("unable to retrieve vpp version (rv: %d)", rv);
360 rv = sw_interface_dump(jm);
361 if (rv != 0)
362 clib_warning ("unable to retrieve interface list (rv: %d)", rv);
363 rv = jm_stats_enable_disable(jm, 1);
364 if (rv != 0)
365 clib_warning ("unable to subscribe to stats (rv: %d)", rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700366 }
Robert Vargad0f92092016-02-10 14:39:57 +0100367 DEBUG_LOG ("clientConnect result: %d", rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368
Robert Vargad0f92092016-02-10 14:39:57 +0100369 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370}
371
372JNIEXPORT void JNICALL Java_org_openvpp_vppjapi_vppConn_clientDisconnect
373 (JNIEnv *env, jobject obj)
374{
Robert Vargad0f92092016-02-10 14:39:57 +0100375 u8 *save_heap;
376 vppjni_main_t * jm = &vppjni_main;
377 vl_client_disconnect_from_vlib();
378
379 save_heap = jm->heap;
380 memset (jm, 0, sizeof (*jm));
381 jm->heap = save_heap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700382}
383
384void vl_api_generic_reply_handler (vl_api_generic_reply_t *mp)
385{
Robert Vargad0f92092016-02-10 14:39:57 +0100386 api_main_t * am = &api_main;
387 u16 msg_id = clib_net_to_host_u16 (mp->_vl_msg_id);
388 trace_cfg_t *cfgp;
389 i32 retval = clib_net_to_host_u32 (mp->retval);
390 int total_bytes = sizeof(mp);
391 vppjni_main_t * jm = &vppjni_main;
392 u8 * saved_reply = 0;
393 u32 context = clib_host_to_net_u32 (mp->context);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394
Robert Vargad0f92092016-02-10 14:39:57 +0100395 cfgp = am->api_trace_cfg + msg_id;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396
Robert Vargad0f92092016-02-10 14:39:57 +0100397 if (!cfgp)
398 clib_warning ("msg id %d: no trace configuration\n", msg_id);
399 else
400 total_bytes = cfgp->size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700401
Robert Vargad0f92092016-02-10 14:39:57 +0100402 jm->context_id_received = context;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403
Robert Vargad0f92092016-02-10 14:39:57 +0100404 DEBUG_LOG ("Received generic reply for msg id %d", msg_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405
Robert Vargad0f92092016-02-10 14:39:57 +0100406 /* A generic reply, successful, we're done */
407 if (retval >= 0 && total_bytes == sizeof(*mp))
408 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409
Robert Vargad0f92092016-02-10 14:39:57 +0100410 /* Save the reply */
411 vec_validate (saved_reply, total_bytes - 1);
412 memcpy (saved_reply, mp, total_bytes);
413
414 vppjni_lock (jm, 2);
415 hash_set (jm->reply_hash, context, saved_reply);
416 jm->saved_reply_count ++;
417 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418}
419
Dave Wallaceba474a22016-02-09 23:09:41 -0500420JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_getRetval0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421(JNIEnv * env, jobject obj, jint context, jint release)
422{
Robert Vargad0f92092016-02-10 14:39:57 +0100423 vppjni_main_t * jm = &vppjni_main;
424 vl_api_generic_reply_t * mp;
425 uword * p;
426 int rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427
Robert Vargad0f92092016-02-10 14:39:57 +0100428 /* Dunno yet? */
429 if (context > jm->context_id_received)
430 return (VNET_API_ERROR_RESPONSE_NOT_READY);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431
Robert Vargad0f92092016-02-10 14:39:57 +0100432 vppjni_lock (jm, 1);
433 p = hash_get (jm->reply_hash, context);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434
Robert Vargad0f92092016-02-10 14:39:57 +0100435 /*
436 * Two cases: a generic "yes" reply - won't be in the hash table
437 * or "no", or "more data" which will be in the table.
438 */
439 if (p == 0)
440 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441
Robert Vargad0f92092016-02-10 14:39:57 +0100442 mp = (vl_api_generic_reply_t *) (p[0]);
443 rv = clib_net_to_host_u32 (mp->retval);
444
445 if (release) {
446 u8 * free_me = (u8 *) mp;
447 vec_free (free_me);
448 hash_unset (jm->reply_hash, context);
449 jm->saved_reply_count --;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450 }
451
452out:
Robert Vargad0f92092016-02-10 14:39:57 +0100453 vppjni_unlock (jm);
454 return (rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455}
456
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500457static int
458name_sort_cmp (void * a1, void * a2)
459{
Robert Vargad0f92092016-02-10 14:39:57 +0100460 name_sort_t * n1 = a1;
461 name_sort_t * n2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500462
Robert Vargad0f92092016-02-10 14:39:57 +0100463 return strcmp ((char *)n1->name, (char *)n2->name);
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500464}
465
Dave Wallaceba474a22016-02-09 23:09:41 -0500466JNIEXPORT jstring JNICALL Java_org_openvpp_vppjapi_vppConn_getInterfaceList0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467 (JNIEnv * env, jobject obj, jstring name_filter)
468{
Robert Vargad0f92092016-02-10 14:39:57 +0100469 vppjni_main_t * jm = &vppjni_main;
470 jstring rv;
471 hash_pair_t * p;
472 name_sort_t * nses = 0, * ns;
473 const char *this_name;
474 const char * nf = (*env)->GetStringUTFChars (env, name_filter, NULL);
475 u8 * s = 0;
476 char *strcasestr (const char *, const char *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700477
Robert Vargad0f92092016-02-10 14:39:57 +0100478 vppjni_lock (jm, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479
Robert Vargad0f92092016-02-10 14:39:57 +0100480 hash_foreach_pair (p, jm->sw_if_index_by_interface_name,
481 ({
482 this_name = (const char *)(p->key);
483 if (strlen (nf) == 0 || strcasestr (this_name, nf)) {
484 vec_add2 (nses, ns, 1);
485 ns->name = (u8 *)(p->key);
486 ns->value = (u32) p->value[0];
487 }
488 }));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700489
Robert Vargad0f92092016-02-10 14:39:57 +0100490 vec_sort_with_function (nses, name_sort_cmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700491
Robert Vargad0f92092016-02-10 14:39:57 +0100492 vec_foreach (ns, nses)
493 s = format (s, "%s: %d, ", ns->name, ns->value);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494
Robert Vargad0f92092016-02-10 14:39:57 +0100495 _vec_len (s) = vec_len (s) - 2;
496 vec_terminate_c_string (s);
497 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498
Robert Vargad0f92092016-02-10 14:39:57 +0100499 vec_free (nses);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500
Robert Vargad0f92092016-02-10 14:39:57 +0100501 (*env)->ReleaseStringUTFChars (env, name_filter, nf);
502
503 rv = (*env)->NewStringUTF (env, (char *) s);
504 vec_free (s);
505
506 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507}
508
Dave Wallaceba474a22016-02-09 23:09:41 -0500509JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_swIfIndexFromName0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510 (JNIEnv * env, jobject obj, jstring interfaceName)
511{
Robert Vargad0f92092016-02-10 14:39:57 +0100512 vppjni_main_t * jm = &vppjni_main;
513 jint rv = -1;
514 const char * if_name = (*env)->GetStringUTFChars (env, interfaceName, NULL);
515 uword * p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700516
Robert Vargad0f92092016-02-10 14:39:57 +0100517 vppjni_lock (jm, 5);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518
Robert Vargad0f92092016-02-10 14:39:57 +0100519 p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520
Robert Vargad0f92092016-02-10 14:39:57 +0100521 if (p != 0)
522 rv = (jint) p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523
Robert Vargad0f92092016-02-10 14:39:57 +0100524 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525
Robert Vargad0f92092016-02-10 14:39:57 +0100526 (*env)->ReleaseStringUTFChars (env, interfaceName, if_name);
527
528 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529}
530
Dave Wallaceba474a22016-02-09 23:09:41 -0500531JNIEXPORT jobject JNICALL Java_org_openvpp_vppjapi_vppConn_getInterfaceCounters0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532(JNIEnv * env, jobject obj, jint swIfIndex)
533{
534 vppjni_main_t * jm = &vppjni_main;
535 sw_interface_stats_t *s;
536 u32 sw_if_index = swIfIndex;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537 jobject result = NULL;
538
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539 vppjni_lock (jm, 16);
540
541 if (sw_if_index >= vec_len(jm->sw_if_stats_by_sw_if_index)) {
542 goto out;
543 }
544 s = &jm->sw_if_stats_by_sw_if_index[sw_if_index];
545 if (!s->valid) {
546 goto out;
547 }
548
Robert Varga81d99ac2016-01-30 18:30:36 +0100549 result = vppInterfaceCountersObject(env,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550 s->rx.octets, s->rx.pkts.ip4, s->rx.pkts.ip6, s->rx.pkts.unicast,
551 s->rx.pkts.multicast, s->rx.pkts.broadcast, s->rx.pkts.discard,
552 s->rx.pkts.fifo_full, s->rx.pkts.error, s->rx.pkts.unknown_proto,
553 s->rx.pkts.miss,
554 s->tx.octets, s->tx.pkts.ip4, s->tx.pkts.ip6, s->tx.pkts.unicast,
555 s->tx.pkts.multicast, s->tx.pkts.broadcast, s->tx.pkts.discard,
556 s->tx.pkts.fifo_full, s->tx.pkts.error, s->tx.pkts.unknown_proto,
557 s->tx.pkts.miss);
558
559out:
560 vppjni_unlock (jm);
561 return result;
562}
563
Dave Wallaceba474a22016-02-09 23:09:41 -0500564JNIEXPORT jstring JNICALL Java_org_openvpp_vppjapi_vppConn_interfaceNameFromSwIfIndex0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565(JNIEnv * env, jobject obj, jint swIfIndex)
566{
567 vppjni_main_t * jm = &vppjni_main;
568 sw_interface_details_t *sw_if_details;
569 u32 sw_if_index;
570 jstring ifname = NULL;
571
572 vppjni_lock (jm, 8);
573
574 sw_if_index = swIfIndex;
575
576 if (sw_if_index >= vec_len(jm->sw_if_table)) {
577 goto out;
578 }
579 sw_if_details = &jm->sw_if_table[sw_if_index];
580 if (!sw_if_details->valid) {
581 goto out;
582 }
583
584 u8 * s = format (0, "%s%c", sw_if_details->interface_name, 0);
585 ifname = (*env)->NewStringUTF(env, (char *)s);
586
587out:
588 vppjni_unlock (jm);
589
590 return ifname;
591}
592
Dave Wallaceba474a22016-02-09 23:09:41 -0500593JNIEXPORT void JNICALL Java_org_openvpp_vppjapi_vppConn_clearInterfaceTable0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700594(JNIEnv * env, jobject obj)
595{
596 vppjni_main_t * jm = &vppjni_main;
597
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500598 vppjni_lock (jm, 21);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599
600 vec_reset_length(jm->sw_if_table);
601
602 vppjni_unlock (jm);
603}
604
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500605static jobjectArray sw_if_dump_get_interfaces ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606
Dave Wallaceba474a22016-02-09 23:09:41 -0500607JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_swInterfaceDump0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608(JNIEnv * env, jobject obj, jbyte name_filter_valid, jbyteArray name_filter)
609{
610 vppjni_main_t *jm = &vppjni_main;
611 f64 timeout;
612 vl_api_sw_interface_dump_t * mp;
613 u32 my_context_id;
614 int rv;
615 rv = vppjni_sanity_check (jm);
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500616 if (rv) {
617 clib_warning("swInterfaceDump sanity_check rv = %d", rv);
618 return NULL;
619 }
620
Ed Warnickecb9cada2015-12-08 15:45:58 -0700621 vppjni_lock (jm, 7);
622 my_context_id = vppjni_get_context_id (jm);
Robert Varga427ce222016-02-01 18:12:18 +0100623 jsize cnt = (*env)->GetArrayLength (env, name_filter);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700624
625 M(SW_INTERFACE_DUMP, sw_interface_dump);
626 mp->context = clib_host_to_net_u32 (my_context_id);
627 mp->name_filter_valid = name_filter_valid;
628
629 if (cnt > sizeof(mp->name_filter))
630 cnt = sizeof(mp->name_filter);
631
Robert Varga427ce222016-02-01 18:12:18 +0100632 (*env)->GetByteArrayRegion(env, name_filter, 0, cnt, (jbyte *)mp->name_filter);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500634 DEBUG_LOG ("interface filter (%d, %s, len: %d)", mp->name_filter_valid, (char *)mp->name_filter, cnt);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635
Ed Warnickecb9cada2015-12-08 15:45:58 -0700636 jm->collect_indices = 1;
637
638 S;
639 {
640 // now send control ping so we know when it ends
641 vl_api_control_ping_t * mp;
642 M(CONTROL_PING, control_ping);
643 mp->context = clib_host_to_net_u32 (my_context_id);
644
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645 S;
646 }
647 vppjni_unlock (jm);
648 WNR;
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500649
650 vppjni_lock (jm, 7);
651 jobjectArray result = sw_if_dump_get_interfaces(env);
652 vppjni_unlock (jm);
653 return result;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654}
655
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500656static jobjectArray sw_if_dump_get_interfaces (JNIEnv * env)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657{
658 vppjni_main_t * jm = &vppjni_main;
659 sw_interface_details_t *sw_if_details;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700660 u32 i;
661
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500662 int len = vec_len(jm->sw_if_dump_if_indices);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700663
Robert Varga81d99ac2016-01-30 18:30:36 +0100664 jobjectArray ifArray = vppInterfaceDetailsArray(env, len);
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500665
666 for (i = 0; i < len; i++) {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700667 u32 sw_if_index = jm->sw_if_dump_if_indices[i];
668 ASSERT(sw_if_index < vec_len(jm->sw_if_table));
669 sw_if_details = &jm->sw_if_table[sw_if_index];
670 ASSERT(sw_if_details->valid);
671
672 u8 * s = format (0, "%s%c", sw_if_details->interface_name, 0);
673
674 jstring ifname = (*env)->NewStringUTF(env, (char *)s);
675 jint ifIndex = sw_if_details->sw_if_index;
676 jint supIfIndex = sw_if_details->sup_sw_if_index;
677 jbyteArray physAddr = (*env)->NewByteArray(env,
678 sw_if_details->l2_address_length);
679 (*env)->SetByteArrayRegion(env, physAddr, 0,
680 sw_if_details->l2_address_length,
681 (signed char*)sw_if_details->l2_address);
682 jint subId = sw_if_details->sub_id;
683 jint subOuterVlanId = sw_if_details->sub_outer_vlan_id;
684 jint subInnerVlanId = sw_if_details->sub_inner_vlan_id;
685 jint vtrOp = sw_if_details->vtr_op;
686 jint vtrPushDot1q = sw_if_details->vtr_push_dot1q;
687 jint vtrTag1 = sw_if_details->vtr_tag1;
688 jint vtrTag2 = sw_if_details->vtr_tag2;
689
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690 jbyte adminUpDown = sw_if_details->admin_up_down;
691 jbyte linkUpDown = sw_if_details->link_up_down;
692 jbyte linkDuplex = sw_if_details->link_duplex;
693 jbyte linkSpeed = sw_if_details->link_speed;
694 jbyte subDot1ad = sw_if_details->sub_dot1ad;
695 jbyte subNumberOfTags = sw_if_details->sub_number_of_tags;
696 jbyte subExactMatch = sw_if_details->sub_exact_match;
697 jbyte subDefault = sw_if_details->sub_default;
698 jbyte subOuterVlanIdAny = sw_if_details->sub_outer_vlan_id_any;
699 jbyte subInnerVlanIdAny = sw_if_details->sub_inner_vlan_id_any;
700
Robert Varga81d99ac2016-01-30 18:30:36 +0100701 jobject ifObj = vppInterfaceDetailsObject(env,
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500702 ifIndex, ifname,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700703 supIfIndex, physAddr, adminUpDown, linkUpDown,
704 linkDuplex, linkSpeed, subId, subDot1ad,
705 subNumberOfTags, subOuterVlanId, subInnerVlanId,
706 subExactMatch, subDefault, subOuterVlanIdAny,
707 subInnerVlanIdAny, vtrOp, vtrPushDot1q, vtrTag1, vtrTag2);
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500708 (*env)->SetObjectArrayElement(env, ifArray, i, ifObj);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700709 }
710
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711 jm->collect_indices = 0;
712 vec_reset_length(jm->sw_if_dump_if_indices);
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500713 return ifArray;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714}
715
Dave Wallaceba474a22016-02-09 23:09:41 -0500716JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_findOrAddBridgeDomainId0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717 (JNIEnv * env, jobject obj, jstring bridgeDomain)
718{
Robert Vargad0f92092016-02-10 14:39:57 +0100719 vppjni_main_t * jm = &vppjni_main;
720 static u8 * bd_name = 0;
721 jint rv = -1;
722 const char * bdName = (*env)->GetStringUTFChars (env, bridgeDomain, NULL);
Dave Wallacebf8c15e2015-12-17 20:54:54 -0500723
Robert Vargad0f92092016-02-10 14:39:57 +0100724 vec_validate_init_c_string (bd_name, bdName, strlen(bdName));
725 (*env)->ReleaseStringUTFChars (env, bridgeDomain, bdName);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726
Robert Vargad0f92092016-02-10 14:39:57 +0100727 vppjni_lock (jm, 6);
728 rv = (jint)vjbd_find_or_add_bd (&jm->vjbd_main, bd_name);
729 vppjni_unlock (jm);
730
731 _vec_len(bd_name) = 0;
732 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700733}
734
Dave Wallaceba474a22016-02-09 23:09:41 -0500735JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_bridgeDomainIdFromName0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700736 (JNIEnv * env, jobject obj, jstring bridgeDomain)
737{
Robert Vargad0f92092016-02-10 14:39:57 +0100738 vppjni_main_t * jm = &vppjni_main;
739 static u8 * bd_name = 0;
740 jint rv = -1;
741 const char * bdName = (*env)->GetStringUTFChars (env, bridgeDomain, NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700742
Robert Vargad0f92092016-02-10 14:39:57 +0100743 vec_validate_init_c_string (bd_name, bdName, strlen(bdName));
744 (*env)->ReleaseStringUTFChars (env, bridgeDomain, bdName);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745
Robert Vargad0f92092016-02-10 14:39:57 +0100746 vppjni_lock (jm, 20);
747 rv = (jint)vjbd_id_from_name(&jm->vjbd_main, (u8 *)bd_name);
748 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749
Robert Vargad0f92092016-02-10 14:39:57 +0100750 _vec_len(bd_name) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751
Robert Vargad0f92092016-02-10 14:39:57 +0100752 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700753}
754
Dave Wallaceba474a22016-02-09 23:09:41 -0500755JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_vppConn_bridgeDomainIdFromInterfaceName0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700756 (JNIEnv * env, jobject obj, jstring interfaceName)
757{
Robert Vargad0f92092016-02-10 14:39:57 +0100758 vppjni_main_t * jm = &vppjni_main;
759 vjbd_main_t * bdm = &jm->vjbd_main;
760 u32 sw_if_index;
761 jint rv = -1;
762 const char * if_name;
763 uword * p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700764
Robert Vargad0f92092016-02-10 14:39:57 +0100765 if_name = (*env)->GetStringUTFChars (env, interfaceName, NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766
Robert Vargad0f92092016-02-10 14:39:57 +0100767 vppjni_lock (jm, 14);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768
Robert Vargad0f92092016-02-10 14:39:57 +0100769 p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700770
Robert Vargad0f92092016-02-10 14:39:57 +0100771 if (p != 0) {
772 sw_if_index = (jint) p[0];
773 p = hash_get (bdm->bd_id_by_sw_if_index, sw_if_index);
774 if (p != 0) {
775 rv = (jint) p[0];
776 }
777 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700778
Robert Vargad0f92092016-02-10 14:39:57 +0100779 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780
Robert Vargad0f92092016-02-10 14:39:57 +0100781 (*env)->ReleaseStringUTFChars (env, interfaceName, if_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782
Robert Vargad0f92092016-02-10 14:39:57 +0100783 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700784}
785
Robert Vargad0f92092016-02-10 14:39:57 +0100786/*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787 * Special-case: build the interface table, maintain
788 * the next loopback sw_if_index vbl.
789 */
790static void vl_api_sw_interface_details_t_handler
791(vl_api_sw_interface_details_t * mp)
792{
Robert Vargad0f92092016-02-10 14:39:57 +0100793 vppjni_main_t * jm = &vppjni_main;
794 static sw_interface_details_t empty_sw_if_details = {0,};
795 sw_interface_details_t *sw_if_details;
796 u32 sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797
Robert Vargad0f92092016-02-10 14:39:57 +0100798 vppjni_lock (jm, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799
Robert Vargad0f92092016-02-10 14:39:57 +0100800 sw_if_index = ntohl (mp->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700801
Robert Vargad0f92092016-02-10 14:39:57 +0100802 u8 * s = format (0, "%s%c", mp->interface_name, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700803
Robert Vargad0f92092016-02-10 14:39:57 +0100804 if (jm->collect_indices) {
805 u32 pos = vec_len(jm->sw_if_dump_if_indices);
806 vec_validate(jm->sw_if_dump_if_indices, pos);
807 jm->sw_if_dump_if_indices[pos] = sw_if_index;
808 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700809
Robert Vargad0f92092016-02-10 14:39:57 +0100810 vec_validate_init_empty(jm->sw_if_table, sw_if_index, empty_sw_if_details);
811 sw_if_details = &jm->sw_if_table[sw_if_index];
812 sw_if_details->valid = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700813
Robert Vargad0f92092016-02-10 14:39:57 +0100814 snprintf((char *)sw_if_details->interface_name,
815 sizeof(sw_if_details->interface_name), "%s", (char *)s);
816 sw_if_details->sw_if_index = sw_if_index;
817 sw_if_details->sup_sw_if_index = ntohl(mp->sup_sw_if_index);
818 sw_if_details->l2_address_length = ntohl (mp->l2_address_length);
819 ASSERT(sw_if_details->l2_address_length <= sizeof(sw_if_details->l2_address));
820 memcpy(sw_if_details->l2_address, mp->l2_address,
821 sw_if_details->l2_address_length);
822 sw_if_details->sub_id = ntohl (mp->sub_id);
823 sw_if_details->sub_outer_vlan_id = ntohl (mp->sub_outer_vlan_id);
824 sw_if_details->sub_inner_vlan_id = ntohl (mp->sub_inner_vlan_id);
825 sw_if_details->vtr_op = ntohl (mp->vtr_op);
826 sw_if_details->vtr_push_dot1q = ntohl (mp->vtr_push_dot1q);
827 sw_if_details->vtr_tag1 = ntohl (mp->vtr_tag1);
828 sw_if_details->vtr_tag2 = ntohl (mp->vtr_tag2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700829
Robert Vargad0f92092016-02-10 14:39:57 +0100830 sw_if_details->admin_up_down = mp->admin_up_down;
831 sw_if_details->link_up_down = mp->link_up_down;
832 sw_if_details->link_duplex = mp->link_duplex;
833 sw_if_details->link_speed = mp->link_speed;
834 sw_if_details->sub_dot1ad = mp->sub_dot1ad;
835 sw_if_details->sub_number_of_tags = mp->sub_number_of_tags;
836 sw_if_details->sub_exact_match = mp->sub_exact_match;
837 sw_if_details->sub_default = mp->sub_default;
838 sw_if_details->sub_outer_vlan_id_any = mp->sub_outer_vlan_id_any;
839 sw_if_details->sub_inner_vlan_id_any = mp->sub_inner_vlan_id_any;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700840
Robert Vargad0f92092016-02-10 14:39:57 +0100841 hash_set_mem (jm->sw_if_index_by_interface_name, s, sw_if_index);
842 DEBUG_LOG ("Got interface %s", (char *)s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700843
Robert Vargad0f92092016-02-10 14:39:57 +0100844 /* In sub interface case, fill the sub interface table entry */
845 if (mp->sw_if_index != mp->sup_sw_if_index) {
846 sw_interface_subif_t * sub = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700847
Robert Vargad0f92092016-02-10 14:39:57 +0100848 vec_add2(jm->sw_if_subif_table, sub, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700849
Robert Vargad0f92092016-02-10 14:39:57 +0100850 vec_validate(sub->interface_name, strlen((char *)s) + 1);
851 strncpy((char *)sub->interface_name, (char *)s,
852 vec_len(sub->interface_name));
853 sub->sw_if_index = ntohl(mp->sw_if_index);
854 sub->sub_id = ntohl(mp->sub_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700855
Robert Vargad0f92092016-02-10 14:39:57 +0100856 sub->sub_dot1ad = mp->sub_dot1ad;
857 sub->sub_number_of_tags = mp->sub_number_of_tags;
858 sub->sub_outer_vlan_id = ntohs(mp->sub_outer_vlan_id);
859 sub->sub_inner_vlan_id = ntohs(mp->sub_inner_vlan_id);
860 sub->sub_exact_match = mp->sub_exact_match;
861 sub->sub_default = mp->sub_default;
862 sub->sub_outer_vlan_id_any = mp->sub_outer_vlan_id_any;
863 sub->sub_inner_vlan_id_any = mp->sub_inner_vlan_id_any;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700864
Robert Vargad0f92092016-02-10 14:39:57 +0100865 /* vlan tag rewrite */
866 sub->vtr_op = ntohl(mp->vtr_op);
867 sub->vtr_push_dot1q = ntohl(mp->vtr_push_dot1q);
868 sub->vtr_tag1 = ntohl(mp->vtr_tag1);
869 sub->vtr_tag2 = ntohl(mp->vtr_tag2);
870 }
871 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700872}
873
874static void vl_api_sw_interface_set_flags_t_handler
875(vl_api_sw_interface_set_flags_t * mp)
876{
877 /* $$$ nothing for the moment */
878}
879
880static jintArray create_array_of_bd_ids(JNIEnv * env, jint bd_id)
881{
882 vppjni_main_t *jm = &vppjni_main;
883 vjbd_main_t * bdm = &jm->vjbd_main;
884 u32 *buf = NULL;
885 u32 i;
886
887 if (bd_id != ~0) {
888 vec_add1(buf, bd_id);
889 } else {
890 for (i = 0; i < vec_len(bdm->bd_oper); i++) {
891 u32 bd_id = bdm->bd_oper[i].bd_id;
892 vec_add1(buf, bd_id);
893 }
894 }
895
896 jintArray bdidArray = (*env)->NewIntArray(env, vec_len(buf));
897
898 (*env)->SetIntArrayRegion(env, bdidArray, 0, vec_len(buf), (int*)buf);
899
Robert Varga7a224a02016-02-01 18:33:38 +0100900 vec_free(buf);
901
Ed Warnickecb9cada2015-12-08 15:45:58 -0700902 return bdidArray;
903}
904
905static void bridge_domain_oper_free(void)
906{
907 vppjni_main_t *jm = &vppjni_main;
908 vjbd_main_t *bdm = &jm->vjbd_main;
909 u32 i;
910
911 for (i = 0; i < vec_len(bdm->bd_oper); i++) {
912 vec_free(bdm->bd_oper->l2fib_oper);
913 }
914 vec_reset_length(bdm->bd_oper);
915 hash_free(bdm->bd_id_by_sw_if_index);
916 hash_free(bdm->oper_bd_index_by_bd_id);
917}
918
Dave Wallaceba474a22016-02-09 23:09:41 -0500919JNIEXPORT jintArray JNICALL Java_org_openvpp_vppjapi_vppConn_bridgeDomainDump0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700920(JNIEnv * env, jobject obj, jint bd_id)
921{
922 vppjni_main_t *jm = &vppjni_main;
923 vl_api_bridge_domain_dump_t * mp;
924 u32 my_context_id;
925 f64 timeout;
926 int rv;
927 rv = vppjni_sanity_check (jm);
928 if (rv) return NULL;
929
930 vppjni_lock (jm, 15);
931
932 if (~0 == bd_id) {
933 bridge_domain_oper_free();
934 }
935
936 my_context_id = vppjni_get_context_id (jm);
937 M(BRIDGE_DOMAIN_DUMP, bridge_domain_dump);
938 mp->context = clib_host_to_net_u32 (my_context_id);
939 mp->bd_id = clib_host_to_net_u32(bd_id);
940 S;
941
942 /* Use a control ping for synchronization */
943 {
944 vl_api_control_ping_t * mp;
945 M(CONTROL_PING, control_ping);
946 S;
947 }
948
949 WNR;
950 if (0 != rv) {
951 return NULL;
952 }
953
954 jintArray ret = create_array_of_bd_ids(env, bd_id);
955
956 vppjni_unlock (jm);
957
958 return ret;
959}
960
961static void
962vl_api_bridge_domain_details_t_handler (vl_api_bridge_domain_details_t * mp)
963{
Robert Vargad0f92092016-02-10 14:39:57 +0100964 vppjni_main_t *jm = &vppjni_main;
965 vjbd_main_t * bdm = &jm->vjbd_main;
966 vjbd_oper_t * bd_oper;
967 u32 bd_id, bd_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700968
Robert Vargad0f92092016-02-10 14:39:57 +0100969 bd_id = ntohl (mp->bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700970
Robert Vargad0f92092016-02-10 14:39:57 +0100971 bd_index = vec_len(bdm->bd_oper);
972 vec_validate (bdm->bd_oper, bd_index);
973 bd_oper = vec_elt_at_index(bdm->bd_oper, bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700974
Robert Vargad0f92092016-02-10 14:39:57 +0100975 hash_set(bdm->oper_bd_index_by_bd_id, bd_id, bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700976
Robert Vargad0f92092016-02-10 14:39:57 +0100977 bd_oper->bd_id = bd_id;
978 bd_oper->flood = mp->flood != 0;
979 bd_oper->forward = mp->forward != 0;
980 bd_oper->learn = mp->learn != 0;
981 bd_oper->uu_flood = mp->uu_flood != 0;
982 bd_oper->arp_term = mp->arp_term != 0;
983 bd_oper->bvi_sw_if_index = ntohl (mp->bvi_sw_if_index);
984 bd_oper->n_sw_ifs = ntohl (mp->n_sw_ifs);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985
Robert Vargad0f92092016-02-10 14:39:57 +0100986 bd_oper->bd_sw_if_oper = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700987}
988
989static void
990vl_api_bridge_domain_sw_if_details_t_handler
991(vl_api_bridge_domain_sw_if_details_t * mp)
992{
Robert Vargad0f92092016-02-10 14:39:57 +0100993 vppjni_main_t *jm = &vppjni_main;
994 vjbd_main_t * bdm = &jm->vjbd_main;
995 bd_sw_if_oper_t * bd_sw_if_oper;
996 u32 bd_id, sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700997
Robert Vargad0f92092016-02-10 14:39:57 +0100998 bd_id = ntohl (mp->bd_id);
999 sw_if_index = ntohl (mp->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001000
Robert Vargad0f92092016-02-10 14:39:57 +01001001 uword *p;
1002 p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
1003 if (p == 0) {
1004 clib_warning("Invalid bd_id %d in bridge_domain_sw_if_details_t_handler", bd_id);
1005 return;
1006 }
1007 u32 oper_bd_index = (jint) p[0];
1008 vjbd_oper_t *bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001009
Robert Vargad0f92092016-02-10 14:39:57 +01001010 u32 len = vec_len(bd_oper->bd_sw_if_oper);
1011 vec_validate(bd_oper->bd_sw_if_oper, len);
1012 bd_sw_if_oper = &bd_oper->bd_sw_if_oper[len];
1013 bd_sw_if_oper->bd_id = bd_id;
1014 bd_sw_if_oper->sw_if_index = sw_if_index;
1015 bd_sw_if_oper->shg = mp->shg;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001016
Robert Vargad0f92092016-02-10 14:39:57 +01001017 hash_set(bdm->bd_id_by_sw_if_index, sw_if_index, bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001018}
1019
1020static const char* interface_name_from_sw_if_index(u32 sw_if_index)
1021{
1022 vppjni_main_t *jm = &vppjni_main;
1023
1024 if (sw_if_index >= vec_len(jm->sw_if_table)) {
1025 return NULL;
1026 }
1027 if (!jm->sw_if_table[sw_if_index].valid) {
1028 return NULL;
1029 }
1030 return (const char*)jm->sw_if_table[sw_if_index].interface_name;
1031}
1032
Dave Wallaceba474a22016-02-09 23:09:41 -05001033JNIEXPORT jobject JNICALL Java_org_openvpp_vppjapi_vppConn_getBridgeDomainDetails0
Ed Warnickecb9cada2015-12-08 15:45:58 -07001034(JNIEnv * env, jobject obj, jint bdId)
1035{
1036 vppjni_main_t *jm = &vppjni_main;
1037 vjbd_main_t * bdm = &jm->vjbd_main;
1038 u32 oper_bd_index;
1039 u32 bd_id = bdId;
1040 jobject rv = NULL;
1041 uword *p;
1042
1043 vppjni_lock (jm, 16);
1044
1045 p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
1046 if (p == 0) {
1047 rv = NULL;
1048 goto out;
1049 }
1050 oper_bd_index = (jint) p[0];
1051
1052 vjbd_oper_t *bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
1053
1054
1055 /* setting BridgeDomainDetails */
1056
Robert Varga81d99ac2016-01-30 18:30:36 +01001057 jobject bddObj = vppBridgeDomainDetailsObject(env);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001058
1059 u8 *vec_bd_name = vjbd_oper_name_from_id(bdm, bd_id);
1060 if (NULL == vec_bd_name) {
1061 rv = NULL;
1062 goto out;
1063 }
1064 char *str_bd_name = (char*)format (0, "%s%c", vec_bd_name, 0);
1065 vec_free(vec_bd_name);
1066 jstring bdName = (*env)->NewStringUTF(env, str_bd_name);
1067 vec_free(str_bd_name);
1068 if (NULL == bdName) {
1069 rv = NULL;
1070 goto out;
1071 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001072
Robert Varga81d99ac2016-01-30 18:30:36 +01001073 set_vppBridgeDomainDetails_name(env, bddObj, bdName);
1074 set_vppBridgeDomainDetails_bdId(env, bddObj, bdId);
1075 set_vppBridgeDomainDetails_flood(env, bddObj, (jboolean)bd_oper->flood);
1076 set_vppBridgeDomainDetails_uuFlood(env, bddObj, (jboolean)bd_oper->uu_flood);
1077 set_vppBridgeDomainDetails_forward(env, bddObj, (jboolean)bd_oper->forward);
1078 set_vppBridgeDomainDetails_learn(env, bddObj, (jboolean)bd_oper->learn);
1079 set_vppBridgeDomainDetails_arpTerm(env, bddObj, (jboolean)bd_oper->arp_term);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001080
1081 jstring bviInterfaceName = NULL;
1082 if (~0 != bd_oper->bvi_sw_if_index) {
1083 const char *str_if_name = interface_name_from_sw_if_index(bd_oper->bvi_sw_if_index);
1084 if (NULL == str_if_name) {
1085 clib_warning("Could not get interface name for sw_if_index %d", bd_oper->bvi_sw_if_index);
1086 rv = NULL;
1087 goto out;
1088 }
1089 bviInterfaceName = (*env)->NewStringUTF(env, str_if_name);
1090 if (NULL == bviInterfaceName) {
1091 rv = NULL;
1092 goto out;
1093 }
1094 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001095
Robert Varga81d99ac2016-01-30 18:30:36 +01001096 set_vppBridgeDomainDetails_bviInterfaceName(env, bddObj, bviInterfaceName);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001097
1098 /* setting BridgeDomainInterfaceDetails */
1099
Ed Warnickecb9cada2015-12-08 15:45:58 -07001100 u32 len = vec_len(bd_oper->bd_sw_if_oper);
1101 ASSERT(len == bd_oper->n_sw_ifs);
1102
Robert Varga81d99ac2016-01-30 18:30:36 +01001103 jobjectArray bdidArray = vppBridgeDomainInterfaceDetailsArray(env, len);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001104
1105 u32 i;
1106 for (i = 0; i < len; i++) {
1107 bd_sw_if_oper_t *sw_if_oper = &bd_oper->bd_sw_if_oper[i];
1108
Robert Varga81d99ac2016-01-30 18:30:36 +01001109 jobject bdidObj = vppBridgeDomainInterfaceDetailsObject(env);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001110 (*env)->SetObjectArrayElement(env, bdidArray, i, bdidObj);
1111
1112 u32 sw_if_index = sw_if_oper->sw_if_index;
1113 const char *str_if_name = interface_name_from_sw_if_index(sw_if_index);
1114 if (NULL == str_if_name) {
1115 rv = NULL;
1116 goto out;
1117 }
1118 jstring interfaceName = (*env)->NewStringUTF(env, str_if_name);
1119 if (NULL == interfaceName) {
1120 rv = NULL;
1121 goto out;
1122 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001123
Robert Varga81d99ac2016-01-30 18:30:36 +01001124 set_vppBridgeDomainInterfaceDetails_interfaceName(env, bdidObj, interfaceName);
1125 set_vppBridgeDomainInterfaceDetails_splitHorizonGroup(env, bdidObj, (jbyte)sw_if_oper->shg);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001126 }
1127
Robert Varga81d99ac2016-01-30 18:30:36 +01001128 set_vppBridgeDomainDetails_interfaces(env, bddObj, bdidArray);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001129
1130 rv = bddObj;
1131
1132out:
1133
1134 vppjni_unlock (jm);
1135
1136 return rv;
1137}
1138
1139static jobject l2_fib_create_object(JNIEnv *env, bd_l2fib_oper_t *l2_fib)
1140{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001141 u32 sw_if_index = l2_fib->sw_if_index;
1142 const char *str_if_name = interface_name_from_sw_if_index(sw_if_index);
1143 if (NULL == str_if_name) {
1144 return NULL;
1145 }
1146 jstring interfaceName = (*env)->NewStringUTF(env, str_if_name);
1147 if (NULL == interfaceName) {
1148 return NULL;
1149 }
1150
1151 jbyteArray physAddr = (*env)->NewByteArray(env, 6);
1152 (*env)->SetByteArrayRegion(env, physAddr, 0, 6,
1153 (signed char*)l2_fib->mac_addr.fields.mac);
1154 jboolean staticConfig = !l2_fib->learned;
1155 jstring outgoingInterface = interfaceName;
1156 jboolean filter = l2_fib->filter;
1157 jboolean bridgedVirtualInterface = l2_fib->bvi;
1158
Robert Varga81d99ac2016-01-30 18:30:36 +01001159 return vppL2FibObject(env, physAddr, staticConfig, outgoingInterface, filter, bridgedVirtualInterface);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001160}
1161
Dave Wallaceba474a22016-02-09 23:09:41 -05001162JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_l2FibTableDump0
Ed Warnickecb9cada2015-12-08 15:45:58 -07001163(JNIEnv * env, jobject obj, jint bd_id)
1164{
Robert Vargad0f92092016-02-10 14:39:57 +01001165 vppjni_main_t *jm = &vppjni_main;
1166 vjbd_main_t * bdm = &jm->vjbd_main;
1167 vl_api_l2_fib_table_dump_t *mp;
1168 jobjectArray l2FibArray = NULL;
1169 vjbd_oper_t *bd_oper;
1170 u32 oper_bd_index;
1171 uword *p;
1172 f64 timeout;
1173 int rv;
1174 u32 i;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001175
Robert Vargad0f92092016-02-10 14:39:57 +01001176 vppjni_lock (jm, 17);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001177
Robert Vargad0f92092016-02-10 14:39:57 +01001178 //vjbd_l2fib_oper_reset (bdm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001179
Robert Vargad0f92092016-02-10 14:39:57 +01001180 p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
1181 if (p == 0) {
1182 goto done;
1183 }
1184 oper_bd_index = p[0];
1185 bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
1186 vec_reset_length (bd_oper->l2fib_oper);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001187
Robert Vargad0f92092016-02-10 14:39:57 +01001188 /* Get list of l2 fib table entries */
1189 M(L2_FIB_TABLE_DUMP, l2_fib_table_dump);
1190 mp->bd_id = ntohl(bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001191 S;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001192
Robert Vargad0f92092016-02-10 14:39:57 +01001193 /* Use a control ping for synchronization */
1194 {
1195 vl_api_control_ping_t * mp;
1196 M(CONTROL_PING, control_ping);
1197 S;
1198 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001199
Robert Vargad0f92092016-02-10 14:39:57 +01001200 WNR;
1201 if (0 != rv) {
1202 goto done;
1203 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001204
Robert Vargad0f92092016-02-10 14:39:57 +01001205 u32 count = vec_len(bd_oper->l2fib_oper);
1206 bd_l2fib_oper_t *l2fib_oper = bd_oper->l2fib_oper;
1207
1208 l2FibArray = vppL2FibArray(env, count);
1209 for (i = 0; i < count; i++) {
1210 bd_l2fib_oper_t *l2_fib = &l2fib_oper[i];
1211 jobject l2FibObj = l2_fib_create_object(env, l2_fib);
1212 (*env)->SetObjectArrayElement(env, l2FibArray, i, l2FibObj);
1213 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001214
1215done:
Robert Vargad0f92092016-02-10 14:39:57 +01001216 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001217
Robert Vargad0f92092016-02-10 14:39:57 +01001218 return l2FibArray;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001219}
1220
1221static void
1222vl_api_l2_fib_table_entry_t_handler (vl_api_l2_fib_table_entry_t * mp)
1223{
Robert Vargad0f92092016-02-10 14:39:57 +01001224 //static u8 * mac_addr;
1225 vppjni_main_t *jm = &vppjni_main;
1226 vjbd_main_t * bdm = &jm->vjbd_main;
1227 vjbd_oper_t * bd_oper;
1228 u32 bd_id, oper_bd_index;
1229 //uword mhash_val_l2fi;
1230 bd_l2fib_oper_t * l2fib_oper;
1231 l2fib_u64_mac_t * l2fe_u64_mac = (l2fib_u64_mac_t *)&mp->mac;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001232
Robert Vargad0f92092016-02-10 14:39:57 +01001233 bd_id = ntohl (mp->bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001234
Robert Vargad0f92092016-02-10 14:39:57 +01001235 uword *p = hash_get (bdm->oper_bd_index_by_bd_id, bd_id);
1236 if (p == 0) {
1237 return;
1238 }
1239 oper_bd_index = (jint) p[0];
1240 bd_oper = vec_elt_at_index(bdm->bd_oper, oper_bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001241
1242#if 0
Robert Vargad0f92092016-02-10 14:39:57 +01001243 vec_validate (mac_addr, MAC_ADDRESS_SIZE);
1244 memcpy (mac_addr, l2fe_u64_mac->fields.mac, MAC_ADDRESS_SIZE);
1245 mhash_val_l2fi = vec_len (bd_oper->l2fib_oper);
1246 if (mhash_elts (&bd_oper->l2fib_index_by_mac) == 0)
1247 mhash_init (&bd_oper->l2fib_index_by_mac, sizeof (u32), MAC_ADDRESS_SIZE);
1248 mhash_set_mem (&bd_oper->l2fib_index_by_mac, mac_addr, &mhash_val_l2fi, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001249#endif
1250
Robert Vargad0f92092016-02-10 14:39:57 +01001251 vec_add2 (bd_oper->l2fib_oper, l2fib_oper, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001252
Robert Vargad0f92092016-02-10 14:39:57 +01001253 l2fib_oper->bd_id = bd_id;
1254 l2fib_oper->mac_addr.raw = l2fib_mac_to_u64 (l2fe_u64_mac->fields.mac);
1255 l2fib_oper->sw_if_index = ntohl (mp->sw_if_index);
1256 l2fib_oper->learned = !mp->static_mac;
1257 l2fib_oper->filter = mp->filter_mac;
1258 l2fib_oper->bvi = mp->bvi_mac;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001259}
1260
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001261static int ipAddressDump
1262(JNIEnv * env, jobject obj, jstring interfaceName, jboolean isIPv6)
1263{
1264 vppjni_main_t *jm = &vppjni_main;
1265 vl_api_ip_address_dump_t * mp;
1266 const char *if_name;
1267 u32 my_context_id;
1268 u32 sw_if_index;
1269 f64 timeout;
1270 uword *p;
1271 int rv = 0;
1272
1273 if (NULL == interfaceName) {
1274 return -1;
1275 }
1276
1277 if_name = (*env)->GetStringUTFChars (env, interfaceName, NULL);
1278 p = hash_get_mem (jm->sw_if_index_by_interface_name, if_name);
1279 (*env)->ReleaseStringUTFChars (env, interfaceName, if_name);
1280 if (p == 0) {
1281 return -1;
1282 }
1283 sw_if_index = (u32) p[0];
1284
1285 rv = vppjni_sanity_check (jm);
1286 if (0 != rv) {
1287 return rv;
1288 }
1289
1290 my_context_id = vppjni_get_context_id (jm);
1291 M(IP_ADDRESS_DUMP, ip_address_dump);
1292 mp->context = clib_host_to_net_u32 (my_context_id);
1293 mp->sw_if_index = clib_host_to_net_u32(sw_if_index);
1294 mp->is_ipv6 = isIPv6;
1295 jm->is_ipv6 = isIPv6;
1296 S;
1297
1298 /* Use a control ping for synchronization */
1299 {
1300 vl_api_control_ping_t * mp;
1301 M(CONTROL_PING, control_ping);
1302 S;
1303 }
1304
1305 WNR;
1306
1307 return rv;
1308}
1309
Dave Wallaceba474a22016-02-09 23:09:41 -05001310JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_ipv4AddressDump0
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001311(JNIEnv * env, jobject obj, jstring interfaceName)
1312{
1313 vppjni_main_t *jm = &vppjni_main;
1314 jobject returnArray = NULL;
1315 int i;
1316
1317 vppjni_lock (jm, 18);
1318
1319 vec_reset_length(jm->ipv4_addresses);
1320
1321 if (0 != ipAddressDump(env, obj, interfaceName, 0)) {
1322 goto done;
1323 }
1324
1325 u32 count = vec_len(jm->ipv4_addresses);
1326 ipv4_address_t *ipv4_address = jm->ipv4_addresses;
1327
Robert Varga81d99ac2016-01-30 18:30:36 +01001328 jobjectArray ipv4AddressArray = vppIPv4AddressArray(env, count);
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001329
1330 for (i = 0; i < count; i++) {
1331 ipv4_address_t *address = &ipv4_address[i];
1332
1333 jint ip = address->ip;
1334 jbyte prefixLength = address->prefix_length;
1335
Robert Varga81d99ac2016-01-30 18:30:36 +01001336 jobject ipv4AddressObj = vppIPv4AddressObject(env, ip, prefixLength);
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001337
1338 (*env)->SetObjectArrayElement(env, ipv4AddressArray, i, ipv4AddressObj);
1339 }
1340
1341 returnArray = ipv4AddressArray;
1342
1343done:
1344 vppjni_unlock (jm);
1345 return returnArray;
1346}
1347
Dave Wallaceba474a22016-02-09 23:09:41 -05001348JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_ipv6AddressDump0
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001349(JNIEnv * env, jobject obj, jstring interfaceName)
1350{
1351 vppjni_main_t *jm = &vppjni_main;
1352 jobject returnArray = NULL;
1353 int i;
1354
1355 vppjni_lock (jm, 19);
1356
1357 vec_reset_length(jm->ipv6_addresses);
1358
1359 if (0 != ipAddressDump(env, obj, interfaceName, 1)) {
1360 goto done;
1361 }
1362
1363 u32 count = vec_len(jm->ipv6_addresses);
1364 ipv6_address_t *ipv6_address = jm->ipv6_addresses;
1365
Robert Varga81d99ac2016-01-30 18:30:36 +01001366 jobjectArray ipv6AddressArray = vppIPv6AddressArray(env, count);
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001367
1368 for (i = 0; i < count; i++) {
1369 ipv6_address_t *address = &ipv6_address[i];
1370
1371 jbyteArray ip = (*env)->NewByteArray(env, 16);
1372 (*env)->SetByteArrayRegion(env, ip, 0, 16,
1373 (signed char*)address->ip);
1374
1375 jbyte prefixLength = address->prefix_length;
1376
Robert Varga81d99ac2016-01-30 18:30:36 +01001377 jobject ipv6AddressObj = vppIPv6AddressObject(env, ip, prefixLength);
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001378
1379 (*env)->SetObjectArrayElement(env, ipv6AddressArray, i, ipv6AddressObj);
1380 }
1381
1382 returnArray = ipv6AddressArray;
1383
1384done:
1385 vppjni_unlock (jm);
1386 return returnArray;
1387}
1388
1389static void vl_api_ip_address_details_t_handler (vl_api_ip_address_details_t * mp)
1390{
1391 vppjni_main_t * jm = &vppjni_main;
1392
1393 if (!jm->is_ipv6) {
1394 ipv4_address_t *address = 0;
1395 vec_add2(jm->ipv4_addresses, address, 1);
Dave Wallace4afb2812016-01-04 22:14:40 -05001396 memcpy(&address->ip, mp->ip, 4);
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001397 address->prefix_length = mp->prefix_length;
1398 } else {
1399 ipv6_address_t *address = 0;
1400 vec_add2(jm->ipv6_addresses, address, 1);
1401 memcpy(address->ip, mp->ip, 16);
1402 address->prefix_length = mp->prefix_length;
1403 }
1404}
1405
1406#define VXLAN_TUNNEL_INTERFACE_NAME_PREFIX "vxlan_tunnel"
1407
Dave Wallaceba474a22016-02-09 23:09:41 -05001408JNIEXPORT jobjectArray JNICALL Java_org_openvpp_vppjapi_vppConn_vxlanTunnelDump0
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001409(JNIEnv * env, jobject obj, jint swIfIndex)
1410{
1411 vppjni_main_t *jm = &vppjni_main;
1412 vl_api_vxlan_tunnel_dump_t * mp;
1413 jobjectArray returnArray = NULL;
1414 u32 my_context_id;
1415 f64 timeout;
1416 int rv = 0;
1417 int i;
1418
1419 vppjni_lock (jm, 22);
1420
1421 vec_reset_length(jm->vxlan_tunnel_details);
1422
1423 my_context_id = vppjni_get_context_id (jm);
1424 M(VXLAN_TUNNEL_DUMP, vxlan_tunnel_dump);
1425 mp->context = clib_host_to_net_u32 (my_context_id);
1426 mp->sw_if_index = clib_host_to_net_u32 (swIfIndex);
1427 S;
1428
1429 /* Use a control ping for synchronization */
1430 {
1431 vl_api_control_ping_t * mp;
1432 M(CONTROL_PING, control_ping);
1433 S;
1434 }
1435
1436 WNR;
1437 if (0 != rv) {
1438 goto done;
1439 }
1440
1441 u32 count = vec_len(jm->vxlan_tunnel_details);
1442
Robert Varga81d99ac2016-01-30 18:30:36 +01001443 jobjectArray vxlanTunnelDetailsArray = vppVxlanTunnelDetailsArray(env, count);
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001444
1445 for (i = 0; i < count; i++) {
1446 vxlan_tunnel_details_t *details = &jm->vxlan_tunnel_details[i];
1447
1448 jint src_address = details->src_address;
1449 jint dst_address = details->dst_address;
1450 jint encap_vrf_id = details->encap_vrf_id;
1451 jint vni = details->vni;
1452 jint decap_next_index = details->decap_next_index;
1453
Robert Varga81d99ac2016-01-30 18:30:36 +01001454 jobject vxlanTunnelDetailsObj = vppVxlanTunnelDetailsObject(env,
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001455 src_address, dst_address, encap_vrf_id, vni, decap_next_index);
1456
1457 (*env)->SetObjectArrayElement(env, vxlanTunnelDetailsArray, i,
1458 vxlanTunnelDetailsObj);
1459 }
1460
1461 returnArray = vxlanTunnelDetailsArray;
1462
1463done:
1464 vppjni_unlock (jm);
1465 return returnArray;
1466}
1467
1468static void vl_api_vxlan_tunnel_details_t_handler
1469(vl_api_vxlan_tunnel_details_t * mp)
1470{
1471 vppjni_main_t * jm = &vppjni_main;
1472 vxlan_tunnel_details_t *tunnel_details;
1473
1474 vec_add2(jm->vxlan_tunnel_details, tunnel_details, 1);
1475 tunnel_details->src_address = ntohl(mp->src_address);
1476 tunnel_details->dst_address = ntohl(mp->dst_address);
1477 tunnel_details->encap_vrf_id = ntohl(mp->encap_vrf_id);
1478 tunnel_details->vni = ntohl(mp->vni);
1479 tunnel_details->decap_next_index = ntohl(mp->decap_next_index);
1480}
1481
Ed Warnickecb9cada2015-12-08 15:45:58 -07001482/* cleanup handler for RX thread */
Robert Varga190efbc2016-02-09 17:16:36 +01001483static void cleanup_rx_thread(void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001484{
Robert Vargad0f92092016-02-10 14:39:57 +01001485 vppjni_main_t * jm = &vppjni_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001486
Robert Vargad0f92092016-02-10 14:39:57 +01001487 vppjni_lock (jm, 99);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001488
Robert Vargad0f92092016-02-10 14:39:57 +01001489 int getEnvStat = (*jm->jvm)->GetEnv(jm->jvm, (void **)&(jm->jenv), JNI_VERSION_1_6);
1490 if (getEnvStat == JNI_EVERSION) {
1491 clib_warning ("Unsupported JNI version\n");
1492 jm->retval = -999;
1493 goto out;
1494 } else if (getEnvStat != JNI_EDETACHED) {
1495 (*jm->jvm)->DetachCurrentThread(jm->jvm);
1496 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001497out:
Robert Vargad0f92092016-02-10 14:39:57 +01001498 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001499}
1500
1501static void
1502vl_api_show_version_reply_t_handler (vl_api_show_version_reply_t * mp)
1503{
Robert Vargad0f92092016-02-10 14:39:57 +01001504 vppjni_main_t * jm = &vppjni_main;
1505 i32 retval = ntohl(mp->retval);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001506
Robert Vargad0f92092016-02-10 14:39:57 +01001507 if (retval >= 0) {
1508 DEBUG_LOG ("show version request succeeded(%d)");
1509 strncpy((char*)jm->program_name, (const char*)mp->program,
1510 sizeof(jm->program_name)-1);
1511 jm->program_name[sizeof(jm->program_name)-1] = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001512
Robert Vargad0f92092016-02-10 14:39:57 +01001513 strncpy((char*)jm->build_directory, (const char*)mp->build_directory,
1514 sizeof(jm->build_directory)-1);
1515 jm->build_directory[sizeof(jm->build_directory)-1] = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001516
Robert Vargad0f92092016-02-10 14:39:57 +01001517 strncpy((char*)jm->version, (const char*)mp->version,
1518 sizeof(jm->version)-1);
1519 jm->version[sizeof(jm->version)-1] = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001520
Robert Vargad0f92092016-02-10 14:39:57 +01001521 strncpy((char*)jm->build_date, (const char*)mp->build_date,
1522 sizeof(jm->build_date)-1);
1523 jm->build_date[sizeof(jm->build_date)-1] = 0;
1524 } else {
1525 clib_error ("show version request failed(%d)", retval);
1526 }
1527 jm->retval = retval;
1528 jm->result_ready = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001529}
1530
1531static void vl_api_want_stats_reply_t_handler (vl_api_want_stats_reply_t * mp)
1532{
Robert Vargad0f92092016-02-10 14:39:57 +01001533 vppjni_main_t * jm = &vppjni_main;
1534 jm->retval = mp->retval; // FIXME: vpp api does not do ntohl on this retval
1535 jm->result_ready = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001536}
1537
1538// control ping needs to be very first thing called
1539// to attach rx thread to java thread
1540static void vl_api_control_ping_reply_t_handler
1541(vl_api_control_ping_reply_t * mp)
1542{
Robert Vargad0f92092016-02-10 14:39:57 +01001543 vppjni_main_t * jm = &vppjni_main;
1544 i32 retval = ntohl(mp->retval);
1545 jm->retval = retval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001546
Robert Vargad0f92092016-02-10 14:39:57 +01001547 // attach to java thread if not attached
1548 int getEnvStat = (*jm->jvm)->GetEnv(jm->jvm, (void **)&(jm->jenv), JNI_VERSION_1_6);
1549 if (getEnvStat == JNI_EDETACHED) {
1550 if ((*jm->jvm)->AttachCurrentThread(jm->jvm, (void **)&(jm->jenv), NULL) != 0) {
1551 clib_warning("Failed to attach thread\n");
1552 jm->retval = -999;
1553 goto out;
1554 }
1555
1556 // workaround as we can't use pthread_cleanup_push
1557 pthread_key_create(&jm->cleanup_rx_thread_key, cleanup_rx_thread);
1558 // destructor is only called if the value of key is non null
1559 pthread_setspecific(jm->cleanup_rx_thread_key, (void *)1);
1560 } else if (getEnvStat == JNI_EVERSION) {
1561 clib_warning ("Unsupported JNI version\n");
1562 jm->retval = -999;
1563 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001564 }
Robert Vargad0f92092016-02-10 14:39:57 +01001565 // jm->jenv is now stable global reference that can be reused (only within RX thread)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001566
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001567#if 0
Robert Vargad0f92092016-02-10 14:39:57 +01001568 // ! callback system removed for now
1569 //
1570 // get issuer msg-id
1571 p = hash_get (jm->ping_hash, context);
1572 if (p != 0) { // ping marks end of some dump call
1573 JNIEnv *env = jm->jenv;
1574 u16 msg_id = (u16)p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001575
Robert Vargad0f92092016-02-10 14:39:57 +01001576 // we will no longer need this
1577 hash_unset (jm->ping_hash, context);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001578
Robert Vargad0f92092016-02-10 14:39:57 +01001579 // get original caller obj
1580 p = hash_get (jm->callback_hash, context);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001581
Robert Vargad0f92092016-02-10 14:39:57 +01001582 if (p == 0) // don't have callback stored
1583 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001584
Robert Vargad0f92092016-02-10 14:39:57 +01001585 jobject obj = (jobject)p[0]; // object that called original call
Ed Warnickecb9cada2015-12-08 15:45:58 -07001586
Robert Vargad0f92092016-02-10 14:39:57 +01001587 switch (msg_id) {
1588 case VL_API_SW_INTERFACE_DUMP:
1589 if (0 != sw_if_dump_call_all_callbacks(obj)) {
1590 goto out2;
1591 }
1592 break;
1593 default:
1594 clib_warning("Unhandled control ping issuer msg-id: %d", msg_id);
1595 goto out2;
1596 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001597 }
Robert Vargad0f92092016-02-10 14:39:57 +01001598out2:
1599 // free the saved obj
1600 hash_unset (jm->callback_hash, context);
1601 // delete global reference
1602 (*env)->DeleteGlobalRef(env, obj);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001603 }
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001604#endif
1605
Ed Warnickecb9cada2015-12-08 15:45:58 -07001606out:
Robert Vargad0f92092016-02-10 14:39:57 +01001607 jm->result_ready = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001608}
1609
1610#define VPPJNI_DEBUG_COUNTERS 0
1611
1612static void vl_api_vnet_interface_counters_t_handler
1613(vl_api_vnet_interface_counters_t *mp)
1614{
Robert Vargad0f92092016-02-10 14:39:57 +01001615 vppjni_main_t *jm = &vppjni_main;
1616 CLIB_UNUSED(char *counter_name);
1617 u32 count, sw_if_index;
1618 int i;
1619 static sw_interface_stats_t empty_stats = {0, };
Ed Warnickecb9cada2015-12-08 15:45:58 -07001620
Robert Vargad0f92092016-02-10 14:39:57 +01001621 vppjni_lock (jm, 12);
1622 count = ntohl (mp->count);
1623 sw_if_index = ntohl (mp->first_sw_if_index);
1624 if (mp->is_combined == 0) {
1625 u64 * vp, v;
1626 vp = (u64 *) mp->data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001627
Robert Vargad0f92092016-02-10 14:39:57 +01001628 for (i = 0; i < count; i++) {
1629 sw_interface_details_t *sw_if = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001630
Robert Vargad0f92092016-02-10 14:39:57 +01001631 v = clib_mem_unaligned (vp, u64);
1632 v = clib_net_to_host_u64 (v);
1633 vp++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001634
Robert Vargad0f92092016-02-10 14:39:57 +01001635 if (sw_if_index < vec_len(jm->sw_if_table))
1636 sw_if = vec_elt_at_index(jm->sw_if_table, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001637
Robert Vargad0f92092016-02-10 14:39:57 +01001638 if (sw_if /* && (sw_if->admin_up_down == 1)*/ && sw_if->interface_name[0] != 0) {
1639 vec_validate_init_empty(jm->sw_if_stats_by_sw_if_index, sw_if_index, empty_stats);
1640 sw_interface_stats_t * s = vec_elt_at_index(jm->sw_if_stats_by_sw_if_index, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001641
Robert Vargad0f92092016-02-10 14:39:57 +01001642 s->sw_if_index = sw_if_index;
1643 s->valid = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001644
Robert Vargad0f92092016-02-10 14:39:57 +01001645 switch (mp->vnet_counter_type) {
1646 case VNET_INTERFACE_COUNTER_DROP:
1647 counter_name = "drop";
1648 s->rx.pkts.discard = v;
1649 break;
1650 case VNET_INTERFACE_COUNTER_PUNT:
1651 counter_name = "punt";
1652 s->rx.pkts.unknown_proto = v;
1653 break;
1654 case VNET_INTERFACE_COUNTER_IP4:
1655 counter_name = "ip4";
1656 s->rx.pkts.ip4 = v;
1657 break;
1658 case VNET_INTERFACE_COUNTER_IP6:
1659 counter_name = "ip6";
1660 s->rx.pkts.ip6 = v;
1661 break;
1662 case VNET_INTERFACE_COUNTER_RX_NO_BUF:
1663 counter_name = "rx-no-buf";
1664 s->rx.pkts.fifo_full = v;
1665 break;
1666 case VNET_INTERFACE_COUNTER_RX_MISS:
1667 counter_name = "rx-miss";
1668 s->rx.pkts.miss = v;
1669 break;
1670 case VNET_INTERFACE_COUNTER_RX_ERROR:
1671 counter_name = "rx-error";
1672 s->rx.pkts.error = v;
1673 break;
1674 case VNET_INTERFACE_COUNTER_TX_ERROR:
1675 counter_name = "tx-error (fifo-full)";
1676 s->tx.pkts.fifo_full = v;
1677 break;
1678 default:
1679 counter_name = "bogus";
1680 break;
1681 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001682
1683#if VPPJNI_DEBUG_COUNTERS == 1
Robert Vargad0f92092016-02-10 14:39:57 +01001684 clib_warning ("%s (%d): %s (%lld)\n", sw_if->interface_name, s->sw_if_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001685 counter_name, v);
1686#endif
Robert Vargad0f92092016-02-10 14:39:57 +01001687 }
1688 sw_if_index++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001689 }
Robert Vargad0f92092016-02-10 14:39:57 +01001690 } else {
1691 vlib_counter_t *vp;
1692 u64 packets, bytes;
1693 vp = (vlib_counter_t *) mp->data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001694
Robert Vargad0f92092016-02-10 14:39:57 +01001695 for (i = 0; i < count; i++) {
1696 sw_interface_details_t *sw_if = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001697
Robert Vargad0f92092016-02-10 14:39:57 +01001698 packets = clib_mem_unaligned (&vp->packets, u64);
1699 packets = clib_net_to_host_u64 (packets);
1700 bytes = clib_mem_unaligned (&vp->bytes, u64);
1701 bytes = clib_net_to_host_u64 (bytes);
1702 vp++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001703
Robert Vargad0f92092016-02-10 14:39:57 +01001704 if (sw_if_index < vec_len(jm->sw_if_table))
1705 sw_if = vec_elt_at_index(jm->sw_if_table, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001706
Robert Vargad0f92092016-02-10 14:39:57 +01001707 if (sw_if /* && (sw_if->admin_up_down == 1) */ && sw_if->interface_name[0] != 0) {
1708 vec_validate_init_empty(jm->sw_if_stats_by_sw_if_index, sw_if_index, empty_stats);
1709 sw_interface_stats_t * s = vec_elt_at_index(jm->sw_if_stats_by_sw_if_index, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001710
Robert Vargad0f92092016-02-10 14:39:57 +01001711 s->valid = 1;
1712 s->sw_if_index = sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001713
Robert Vargad0f92092016-02-10 14:39:57 +01001714 switch (mp->vnet_counter_type) {
1715 case VNET_INTERFACE_COUNTER_RX:
1716 s->rx.pkts.unicast = packets;
1717 s->rx.octets = bytes;
1718 counter_name = "rx";
1719 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001720
Robert Vargad0f92092016-02-10 14:39:57 +01001721 case VNET_INTERFACE_COUNTER_TX:
1722 s->tx.pkts.unicast = packets;
1723 s->tx.octets = bytes;
1724 counter_name = "tx";
1725 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001726
Robert Vargad0f92092016-02-10 14:39:57 +01001727 default:
1728 counter_name = "bogus";
1729 break;
1730 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001731
1732#if VPPJNI_DEBUG_COUNTERS == 1
Robert Vargad0f92092016-02-10 14:39:57 +01001733 clib_warning ("%s (%d): %s.packets %lld\n",
1734 sw_if->interface_name,
1735 sw_if_index, counter_name, packets);
1736 clib_warning ("%s (%d): %s.bytes %lld\n",
1737 sw_if->interface_name,
1738 sw_if_index, counter_name, bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001739#endif
Robert Vargad0f92092016-02-10 14:39:57 +01001740 }
1741 sw_if_index++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001742 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001743 }
Robert Vargad0f92092016-02-10 14:39:57 +01001744 vppjni_unlock (jm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001745}
1746
1747jint JNI_OnLoad(JavaVM *vm, void *reserved) {
Robert Vargad0f92092016-02-10 14:39:57 +01001748 vppjni_main_t * jm = &vppjni_main;
1749 JNIEnv* env;
1750 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_6) != JNI_OK) {
1751 return JNI_ERR;
1752 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001753
Robert Vargad0f92092016-02-10 14:39:57 +01001754 if (vppjni_init(env) != 0) {
1755 return JNI_ERR;
1756 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001757
Robert Vargad0f92092016-02-10 14:39:57 +01001758 jm->jvm = vm;
1759 return JNI_VERSION_1_6;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001760}
1761
1762void JNI_OnUnload(JavaVM *vm, void *reserved) {
Robert Vargad0f92092016-02-10 14:39:57 +01001763 vppjni_main_t * jm = &vppjni_main;
1764 JNIEnv* env;
1765 if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_6) != JNI_OK) {
1766 return;
1767 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001768
Robert Vargad0f92092016-02-10 14:39:57 +01001769 vppjni_uninit(env);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001770
Robert Vargad0f92092016-02-10 14:39:57 +01001771 jm->jenv = NULL;
1772 jm->jvm = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001773}
1774
1775#define foreach_vpe_api_msg \
1776_(CONTROL_PING_REPLY, control_ping_reply) \
1777_(SW_INTERFACE_DETAILS, sw_interface_details) \
1778_(SHOW_VERSION_REPLY, show_version_reply) \
1779_(WANT_STATS_REPLY, want_stats_reply) \
1780_(VNET_INTERFACE_COUNTERS, vnet_interface_counters) \
1781_(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags) \
1782_(BRIDGE_DOMAIN_DETAILS, bridge_domain_details) \
1783_(BRIDGE_DOMAIN_SW_IF_DETAILS, bridge_domain_sw_if_details) \
Dave Wallacebf8c15e2015-12-17 20:54:54 -05001784_(L2_FIB_TABLE_ENTRY, l2_fib_table_entry) \
1785_(IP_ADDRESS_DETAILS, ip_address_details) \
1786_(VXLAN_TUNNEL_DETAILS, vxlan_tunnel_details)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001787
1788static int connect_to_vpe(char *name)
1789{
Robert Vargad0f92092016-02-10 14:39:57 +01001790 vppjni_main_t * jm = &vppjni_main;
1791 api_main_t * am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001792
Robert Vargad0f92092016-02-10 14:39:57 +01001793 if (vl_client_connect_to_vlib("/vpe-api", name, 32) < 0)
1794 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001795
Robert Vargad0f92092016-02-10 14:39:57 +01001796 jm->my_client_index = am->my_client_index;
1797 jm->vl_input_queue = am->shmem_hdr->vl_input_queue;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001798
Robert Vargad0f92092016-02-10 14:39:57 +01001799#define _(N,n) \
1800 vl_msg_api_set_handlers(VL_API_##N, #n, \
1801 vl_api_##n##_t_handler, \
1802 vl_noop_handler, \
1803 vl_api_##n##_t_endian, \
1804 vl_api_##n##_t_print, \
1805 sizeof(vl_api_##n##_t), 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001806 foreach_vpe_api_msg;
1807#undef _
Ed Warnickecb9cada2015-12-08 15:45:58 -07001808
Robert Vargad0f92092016-02-10 14:39:57 +01001809 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001810}
1811
1812/* Format an IP6 address. */
1813u8 * format_ip6_address (u8 * s, va_list * args)
1814{
Robert Vargad0f92092016-02-10 14:39:57 +01001815 ip6_address_t * a = va_arg (*args, ip6_address_t *);
1816 u32 max_zero_run = 0, this_zero_run = 0;
1817 int max_zero_run_index = -1, this_zero_run_index=0;
1818 int in_zero_run = 0, i;
1819 int last_double_colon = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001820
Robert Vargad0f92092016-02-10 14:39:57 +01001821 /* Ugh, this is a pain. Scan forward looking for runs of 0's */
1822 for (i = 0; i < ARRAY_LEN (a->as_u16); i++) {
1823 if (a->as_u16[i] == 0) {
1824 if (in_zero_run) {
1825 this_zero_run++;
1826 } else {
1827 in_zero_run = 1;
1828 this_zero_run =1;
1829 this_zero_run_index = i;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001830 }
Robert Vargad0f92092016-02-10 14:39:57 +01001831 } else {
1832 if (in_zero_run) {
1833 /* offer to compress the biggest run of > 1 zero */
1834 if (this_zero_run > max_zero_run && this_zero_run > 1) {
1835 max_zero_run_index = this_zero_run_index;
1836 max_zero_run = this_zero_run;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001837 }
1838 }
Robert Vargad0f92092016-02-10 14:39:57 +01001839 in_zero_run = 0;
1840 this_zero_run = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001841 }
1842 }
1843
Robert Vargad0f92092016-02-10 14:39:57 +01001844 if (in_zero_run) {
1845 if (this_zero_run > max_zero_run && this_zero_run > 1) {
1846 max_zero_run_index = this_zero_run_index;
1847 max_zero_run = this_zero_run;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001848 }
1849 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001850
Robert Vargad0f92092016-02-10 14:39:57 +01001851 for (i = 0; i < ARRAY_LEN (a->as_u16); i++) {
1852 if (i == max_zero_run_index) {
1853 s = format (s, "::");
1854 i += max_zero_run - 1;
1855 last_double_colon = 1;
1856 } else {
1857 s = format (s, "%s%x",
1858 (last_double_colon || i == 0) ? "" : ":",
1859 clib_net_to_host_u16 (a->as_u16[i]));
1860 last_double_colon = 0;
1861 }
1862 }
1863
1864 return s;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001865}
1866
1867/* Format an IP4 address. */
1868u8 * format_ip4_address (u8 * s, va_list * args)
1869{
Robert Vargad0f92092016-02-10 14:39:57 +01001870 u8 * a = va_arg (*args, u8 *);
1871 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001872}
1873
1874