2 *------------------------------------------------------------------
3 * Copyright (c) 2017 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
25 #include <vppinfra/string.h>
26 #include <vapi/vapi.h>
27 #include <vapi/vpe.api.vapi.h>
28 #include <vapi/interface.api.vapi.h>
29 #include <vapi/l2.api.vapi.h>
30 #include <fake.api.vapi.h>
32 DEFINE_VAPI_MSG_IDS_VPE_API_JSON;
33 DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON;
34 DEFINE_VAPI_MSG_IDS_L2_API_JSON;
35 DEFINE_VAPI_MSG_IDS_FAKE_API_JSON;
37 static char *app_name = NULL;
38 static char *api_prefix = NULL;
39 static const int max_outstanding_requests = 64;
40 static const int response_queue_size = 32;
42 /* centos has ancient check so we hack our way around here
43 * to make it work somehow */
44 #ifndef ck_assert_ptr_eq
45 #define ck_assert_ptr_eq(X,Y) ck_assert_int_eq((long)X, (long)Y)
48 #ifndef ck_assert_ptr_ne
49 #define ck_assert_ptr_ne(X,Y) ck_assert_int_ne((long)X, (long)Y)
52 START_TEST (test_invalid_values)
55 vapi_error_e rv = vapi_ctx_alloc (&ctx);
56 ck_assert_int_eq (VAPI_OK, rv);
57 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
58 ck_assert_ptr_eq (NULL, sv);
59 rv = vapi_send (ctx, sv);
60 ck_assert_int_eq (VAPI_EINVAL, rv);
61 rv = vapi_connect (ctx, app_name, api_prefix, max_outstanding_requests,
62 response_queue_size, VAPI_MODE_BLOCKING, true);
63 ck_assert_int_eq (VAPI_OK, rv);
64 rv = vapi_send (ctx, NULL);
65 ck_assert_int_eq (VAPI_EINVAL, rv);
66 rv = vapi_send (NULL, NULL);
67 ck_assert_int_eq (VAPI_EINVAL, rv);
68 rv = vapi_recv (NULL, NULL, NULL, 0, 0);
69 ck_assert_int_eq (VAPI_EINVAL, rv);
70 rv = vapi_recv (ctx, NULL, NULL, 0, 0);
71 ck_assert_int_eq (VAPI_EINVAL, rv);
72 vapi_msg_show_version_reply *reply;
73 rv = vapi_recv (ctx, (void **) &reply, NULL, 0, 0);
74 ck_assert_int_eq (VAPI_EINVAL, rv);
75 rv = vapi_disconnect (ctx);
76 ck_assert_int_eq (VAPI_OK, rv);
82 START_TEST (test_hton_1)
84 const u16 _vl_msg_id = 1;
85 vapi_type_msg_header1_t h;
86 h._vl_msg_id = _vl_msg_id;
87 vapi_type_msg_header1_t_hton (&h);
88 ck_assert_int_eq (be16toh (h._vl_msg_id), _vl_msg_id);
93 START_TEST (test_hton_2)
95 const u16 _vl_msg_id = 1;
96 const u32 client_index = 3;
97 vapi_type_msg_header2_t h;
98 h._vl_msg_id = _vl_msg_id;
99 h.client_index = client_index;
100 vapi_type_msg_header2_t_hton (&h);
101 ck_assert_int_eq (be16toh (h._vl_msg_id), _vl_msg_id);
102 ck_assert_int_eq (h.client_index, client_index);
107 #define verify_hton_swap(expr, value) \
108 if (4 == sizeof (expr)) \
110 ck_assert_int_eq (expr, htobe32 (value)); \
112 else if (2 == sizeof (expr)) \
114 ck_assert_int_eq (expr, htobe16 (value)); \
118 ck_assert_int_eq (expr, value); \
121 START_TEST (test_hton_4)
123 const int vla_count = 3;
124 char x[sizeof (vapi_msg_bridge_domain_details) +
125 vla_count * sizeof (vapi_type_bridge_domain_sw_if)];
126 vapi_msg_bridge_domain_details *d = (void *) x;
128 d->header._vl_msg_id = cnt++;
129 d->header.context = cnt++;
130 d->payload.bd_id = cnt++;
131 d->payload.flood = cnt++;
132 d->payload.uu_flood = cnt++;
133 d->payload.forward = cnt++;
134 d->payload.learn = cnt++;
135 d->payload.arp_term = cnt++;
136 d->payload.mac_age = cnt++;
137 d->payload.bvi_sw_if_index = cnt++;
138 d->payload.n_sw_ifs = vla_count;
140 for (i = 0; i < vla_count; ++i)
142 vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
143 det->context = cnt++;
144 det->sw_if_index = cnt++;
147 ck_assert_int_eq (sizeof (x), vapi_calc_bridge_domain_details_msg_size (d));
148 vapi_msg_bridge_domain_details_hton (d);
150 verify_hton_swap (d->header._vl_msg_id, tmp);
152 ck_assert_int_eq (d->header.context, tmp);
154 verify_hton_swap (d->payload.bd_id, tmp);
156 verify_hton_swap (d->payload.flood, tmp);
158 verify_hton_swap (d->payload.uu_flood, tmp);
160 verify_hton_swap (d->payload.forward, tmp);
162 verify_hton_swap (d->payload.learn, tmp);
164 verify_hton_swap (d->payload.arp_term, tmp);
166 verify_hton_swap (d->payload.mac_age, tmp);
168 verify_hton_swap (d->payload.bvi_sw_if_index, tmp);
170 ck_assert_int_eq (d->payload.n_sw_ifs, htobe32 (vla_count));
171 for (i = 0; i < vla_count; ++i)
173 vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
174 verify_hton_swap (det->context, tmp);
176 verify_hton_swap (det->sw_if_index, tmp);
178 verify_hton_swap (det->shg, tmp);
181 vapi_msg_bridge_domain_details_ntoh (d);
183 ck_assert_int_eq (d->header._vl_msg_id, tmp);
185 ck_assert_int_eq (d->header.context, tmp);
187 ck_assert_int_eq (d->payload.bd_id, tmp);
189 ck_assert_int_eq (d->payload.flood, tmp);
191 ck_assert_int_eq (d->payload.uu_flood, tmp);
193 ck_assert_int_eq (d->payload.forward, tmp);
195 ck_assert_int_eq (d->payload.learn, tmp);
197 ck_assert_int_eq (d->payload.arp_term, tmp);
199 ck_assert_int_eq (d->payload.mac_age, tmp);
201 ck_assert_int_eq (d->payload.bvi_sw_if_index, tmp);
203 ck_assert_int_eq (d->payload.n_sw_ifs, vla_count);
204 for (i = 0; i < vla_count; ++i)
206 vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
207 ck_assert_int_eq (det->context, tmp);
209 ck_assert_int_eq (det->sw_if_index, tmp);
211 ck_assert_int_eq (det->shg, tmp);
214 ck_assert_int_eq (sizeof (x), vapi_calc_bridge_domain_details_msg_size (d));
219 START_TEST (test_ntoh_1)
221 const u16 _vl_msg_id = 1;
222 vapi_type_msg_header1_t h;
223 h._vl_msg_id = _vl_msg_id;
224 vapi_type_msg_header1_t_ntoh (&h);
225 ck_assert_int_eq (htobe16 (h._vl_msg_id), _vl_msg_id);
230 START_TEST (test_ntoh_2)
232 const u16 _vl_msg_id = 1;
233 const u32 client_index = 3;
234 vapi_type_msg_header2_t h;
235 h._vl_msg_id = _vl_msg_id;
236 h.client_index = client_index;
237 vapi_type_msg_header2_t_ntoh (&h);
238 ck_assert_int_eq (htobe16 (h._vl_msg_id), _vl_msg_id);
239 ck_assert_int_eq (h.client_index, client_index);
244 #define verify_ntoh_swap(expr, value) \
245 if (4 == sizeof (expr)) \
247 ck_assert_int_eq (expr, be32toh (value)); \
249 else if (2 == sizeof (expr)) \
251 ck_assert_int_eq (expr, be16toh (value)); \
255 ck_assert_int_eq (expr, value); \
258 START_TEST (test_ntoh_4)
260 const int vla_count = 3;
261 char x[sizeof (vapi_msg_bridge_domain_details) +
262 vla_count * sizeof (vapi_type_bridge_domain_sw_if)];
263 vapi_msg_bridge_domain_details *d = (void *) x;
265 d->header._vl_msg_id = cnt++;
266 d->header.context = cnt++;
267 d->payload.bd_id = cnt++;
268 d->payload.flood = cnt++;
269 d->payload.uu_flood = cnt++;
270 d->payload.forward = cnt++;
271 d->payload.learn = cnt++;
272 d->payload.arp_term = cnt++;
273 d->payload.mac_age = cnt++;
274 d->payload.bvi_sw_if_index = cnt++;
275 d->payload.n_sw_ifs = htobe32 (vla_count);
277 for (i = 0; i < vla_count; ++i)
279 vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
280 det->context = cnt++;
281 det->sw_if_index = cnt++;
284 vapi_msg_bridge_domain_details_ntoh (d);
285 ck_assert_int_eq (sizeof (x), vapi_calc_bridge_domain_details_msg_size (d));
287 verify_ntoh_swap (d->header._vl_msg_id, tmp);
289 ck_assert_int_eq (d->header.context, tmp);
291 verify_ntoh_swap (d->payload.bd_id, tmp);
293 verify_ntoh_swap (d->payload.flood, tmp);
295 verify_ntoh_swap (d->payload.uu_flood, tmp);
297 verify_ntoh_swap (d->payload.forward, tmp);
299 verify_ntoh_swap (d->payload.learn, tmp);
301 verify_ntoh_swap (d->payload.arp_term, tmp);
303 verify_ntoh_swap (d->payload.mac_age, tmp);
305 verify_ntoh_swap (d->payload.bvi_sw_if_index, tmp);
307 ck_assert_int_eq (d->payload.n_sw_ifs, vla_count);
308 for (i = 0; i < vla_count; ++i)
310 vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
311 verify_ntoh_swap (det->context, tmp);
313 verify_ntoh_swap (det->sw_if_index, tmp);
315 verify_ntoh_swap (det->shg, tmp);
318 vapi_msg_bridge_domain_details_hton (d);
320 ck_assert_int_eq (d->header._vl_msg_id, tmp);
322 ck_assert_int_eq (d->header.context, tmp);
324 ck_assert_int_eq (d->payload.bd_id, tmp);
326 ck_assert_int_eq (d->payload.flood, tmp);
328 ck_assert_int_eq (d->payload.uu_flood, tmp);
330 ck_assert_int_eq (d->payload.forward, tmp);
332 ck_assert_int_eq (d->payload.learn, tmp);
334 ck_assert_int_eq (d->payload.arp_term, tmp);
336 ck_assert_int_eq (d->payload.mac_age, tmp);
338 ck_assert_int_eq (d->payload.bvi_sw_if_index, tmp);
340 ck_assert_int_eq (d->payload.n_sw_ifs, htobe32 (vla_count));
341 for (i = 0; i < vla_count; ++i)
343 vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
344 ck_assert_int_eq (det->context, tmp);
346 ck_assert_int_eq (det->sw_if_index, tmp);
348 ck_assert_int_eq (det->shg, tmp);
356 show_version_cb (vapi_ctx_t ctx, void *caller_ctx,
357 vapi_error_e rv, bool is_last,
358 vapi_payload_show_version_reply * p)
360 ck_assert_int_eq (VAPI_OK, rv);
361 ck_assert_int_eq (true, is_last);
362 ck_assert_str_eq ("vpe", (char *) vl_api_from_api_string (&p->program));
364 ("show_version_reply: program: `%s', version: `%s', build directory: "
365 "`%s', build date: `%s'\n",
366 vl_api_from_api_string (&p->program),
367 vl_api_from_api_string (&p->version),
368 vl_api_from_api_string (&p->build_directory),
369 vl_api_from_api_string (&p->build_date));
370 ++*(int *) caller_ctx;
378 u32 *sw_if_index_storage;
379 } test_create_loopback_ctx_t;
382 loopback_create_cb (vapi_ctx_t ctx, void *caller_ctx,
383 vapi_error_e rv, bool is_last,
384 vapi_payload_create_loopback_reply * p)
386 test_create_loopback_ctx_t *clc = caller_ctx;
387 ck_assert_int_eq (clc->expected_retval, p->retval);
388 *clc->sw_if_index_storage = p->sw_if_index;
397 u32 *sw_if_index_storage;
398 } test_delete_loopback_ctx_t;
401 loopback_delete_cb (vapi_ctx_t ctx, void *caller_ctx,
402 vapi_error_e rv, bool is_last,
403 vapi_payload_delete_loopback_reply * p)
405 test_delete_loopback_ctx_t *dlc = caller_ctx;
406 ck_assert_int_eq (dlc->expected_retval, p->retval);
411 START_TEST (test_connect)
414 vapi_error_e rv = vapi_ctx_alloc (&ctx);
415 ck_assert_int_eq (VAPI_OK, rv);
416 rv = vapi_connect (ctx, app_name, api_prefix, max_outstanding_requests,
417 response_queue_size, VAPI_MODE_BLOCKING, true);
418 ck_assert_int_eq (VAPI_OK, rv);
419 rv = vapi_disconnect (ctx);
420 ck_assert_int_eq (VAPI_OK, rv);
429 setup_blocking (void)
431 vapi_error_e rv = vapi_ctx_alloc (&ctx);
432 ck_assert_int_eq (VAPI_OK, rv);
433 rv = vapi_connect (ctx, app_name, api_prefix, max_outstanding_requests,
434 response_queue_size, VAPI_MODE_BLOCKING, true);
435 ck_assert_int_eq (VAPI_OK, rv);
439 setup_nonblocking (void)
441 vapi_error_e rv = vapi_ctx_alloc (&ctx);
442 ck_assert_int_eq (VAPI_OK, rv);
443 rv = vapi_connect (ctx, app_name, api_prefix, max_outstanding_requests,
444 response_queue_size, VAPI_MODE_NONBLOCKING, true);
445 ck_assert_int_eq (VAPI_OK, rv);
451 vapi_disconnect (ctx);
455 START_TEST (test_show_version_1)
457 printf ("--- Basic show version message - reply test ---\n");
458 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
459 ck_assert_ptr_ne (NULL, sv);
460 vapi_msg_show_version_hton (sv);
461 vapi_error_e rv = vapi_send (ctx, sv);
462 ck_assert_int_eq (VAPI_OK, rv);
463 vapi_msg_show_version_reply *resp;
465 rv = vapi_recv (ctx, (void *) &resp, &size, 0, 0);
466 ck_assert_int_eq (VAPI_OK, rv);
468 show_version_cb (NULL, &dummy, VAPI_OK, true, &resp->payload);
469 vapi_msg_free (ctx, resp);
474 START_TEST (test_show_version_2)
477 printf ("--- Show version via blocking callback API ---\n");
478 const int attempts = response_queue_size * 4;
480 for (i = 0; i < attempts; ++i)
482 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
483 ck_assert_ptr_ne (NULL, sv);
484 vapi_error_e rv = vapi_show_version (ctx, sv, show_version_cb, &called);
485 ck_assert_int_eq (VAPI_OK, rv);
487 ck_assert_int_eq (attempts, called);
499 } sw_interface_dump_ctx;
502 sw_interface_dump_cb (struct vapi_ctx_s *ctx, void *callback_ctx,
503 vapi_error_e rv, bool is_last,
504 vapi_payload_sw_interface_details * reply)
506 sw_interface_dump_ctx *dctx = callback_ctx;
507 ck_assert_int_eq (false, dctx->last_called);
510 ck_assert (NULL == reply);
511 dctx->last_called = true;
515 ck_assert (NULL != reply);
516 printf ("Interface dump entry: [%u]: %s\n", reply->sw_if_index,
517 reply->interface_name);
519 for (i = 0; i < dctx->num_ifs; ++i)
521 if (dctx->sw_if_indexes[i] == reply->sw_if_index)
523 ck_assert_int_eq (false, dctx->seen[i]);
524 dctx->seen[i] = true;
532 START_TEST (test_loopbacks_1)
534 printf ("--- Create/delete loopbacks using blocking API ---\n");
535 const size_t num_ifs = 5;
536 u8 mac_addresses[num_ifs][6];
537 clib_memset (&mac_addresses, 0, sizeof (mac_addresses));
538 u32 sw_if_indexes[num_ifs];
539 clib_memset (&sw_if_indexes, 0xff, sizeof (sw_if_indexes));
540 test_create_loopback_ctx_t clcs[num_ifs];
541 clib_memset (&clcs, 0, sizeof (clcs));
542 test_delete_loopback_ctx_t dlcs[num_ifs];
543 clib_memset (&dlcs, 0, sizeof (dlcs));
545 for (i = 0; i < num_ifs; ++i)
547 memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
548 mac_addresses[i][5] = i;
549 clcs[i].sw_if_index_storage = &sw_if_indexes[i];
551 for (i = 0; i < num_ifs; ++i)
553 vapi_msg_create_loopback *cl = vapi_alloc_create_loopback (ctx);
554 memcpy (cl->payload.mac_address, mac_addresses[i],
555 sizeof (cl->payload.mac_address));
557 vapi_create_loopback (ctx, cl, loopback_create_cb, &clcs[i]);
558 ck_assert_int_eq (VAPI_OK, rv);
560 for (i = 0; i < num_ifs; ++i)
562 ck_assert_int_eq (1, clcs[i].called);
563 printf ("Created loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
565 mac_addresses[i][0], mac_addresses[i][1], mac_addresses[i][2],
566 mac_addresses[i][3], mac_addresses[i][4], mac_addresses[i][5],
570 sw_interface_dump_ctx dctx = { false, num_ifs, sw_if_indexes, seen, 0 };
571 vapi_msg_sw_interface_dump *dump;
573 const int attempts = response_queue_size * 4;
574 for (i = 0; i < attempts; ++i)
576 dctx.last_called = false;
577 clib_memset (&seen, 0, sizeof (seen));
578 dump = vapi_alloc_sw_interface_dump (ctx);
579 dump->payload.name_filter_valid = 0;
580 clib_memset (dump->payload.name_filter, 0,
581 sizeof (dump->payload.name_filter));
582 while (VAPI_EAGAIN ==
584 vapi_sw_interface_dump (ctx, dump, sw_interface_dump_cb,
587 ck_assert_int_eq (true, dctx.last_called);
589 for (j = 0; j < num_ifs; ++j)
591 ck_assert_int_eq (true, seen[j]);
594 clib_memset (&seen, 0, sizeof (seen));
595 for (i = 0; i < num_ifs; ++i)
597 vapi_msg_delete_loopback *dl = vapi_alloc_delete_loopback (ctx);
598 dl->payload.sw_if_index = sw_if_indexes[i];
600 vapi_delete_loopback (ctx, dl, loopback_delete_cb, &dlcs[i]);
601 ck_assert_int_eq (VAPI_OK, rv);
603 for (i = 0; i < num_ifs; ++i)
605 ck_assert_int_eq (1, dlcs[i].called);
606 printf ("Deleted loopback with sw_if_index %u\n", sw_if_indexes[i]);
608 dctx.last_called = false;
609 clib_memset (&seen, 0, sizeof (seen));
610 dump = vapi_alloc_sw_interface_dump (ctx);
611 dump->payload.name_filter_valid = 0;
612 clib_memset (dump->payload.name_filter, 0,
613 sizeof (dump->payload.name_filter));
614 while (VAPI_EAGAIN ==
616 vapi_sw_interface_dump (ctx, dump, sw_interface_dump_cb, &dctx)))
618 ck_assert_int_eq (true, dctx.last_called);
619 for (i = 0; i < num_ifs; ++i)
621 ck_assert_int_eq (false, seen[i]);
627 START_TEST (test_show_version_3)
629 printf ("--- Show version via async callback ---\n");
632 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
633 ck_assert_ptr_ne (NULL, sv);
634 while (VAPI_EAGAIN ==
635 (rv = vapi_show_version (ctx, sv, show_version_cb, &called)))
637 ck_assert_int_eq (VAPI_OK, rv);
638 ck_assert_int_eq (0, called);
639 rv = vapi_dispatch (ctx);
640 ck_assert_int_eq (VAPI_OK, rv);
641 ck_assert_int_eq (1, called);
643 rv = vapi_dispatch (ctx);
644 ck_assert_int_eq (VAPI_OK, rv);
645 ck_assert_int_eq (0, called);
650 START_TEST (test_show_version_4)
652 printf ("--- Show version via async callback - multiple messages ---\n");
654 const size_t num_req = 5;
655 int contexts[num_req];
656 clib_memset (contexts, 0, sizeof (contexts));
658 for (i = 0; i < num_req; ++i)
660 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
661 ck_assert_ptr_ne (NULL, sv);
662 while (VAPI_EAGAIN ==
664 vapi_show_version (ctx, sv, show_version_cb, &contexts[i])))
666 ck_assert_int_eq (VAPI_OK, rv);
668 for (j = 0; j < num_req; ++j)
670 ck_assert_int_eq (0, contexts[j]);
673 rv = vapi_dispatch (ctx);
674 ck_assert_int_eq (VAPI_OK, rv);
675 for (i = 0; i < num_req; ++i)
677 ck_assert_int_eq (1, contexts[i]);
679 clib_memset (contexts, 0, sizeof (contexts));
680 rv = vapi_dispatch (ctx);
681 ck_assert_int_eq (VAPI_OK, rv);
682 for (i = 0; i < num_req; ++i)
684 ck_assert_int_eq (0, contexts[i]);
690 START_TEST (test_loopbacks_2)
692 printf ("--- Create/delete loopbacks using non-blocking API ---\n");
694 const size_t num_ifs = 5;
695 u8 mac_addresses[num_ifs][6];
696 clib_memset (&mac_addresses, 0, sizeof (mac_addresses));
697 u32 sw_if_indexes[num_ifs];
698 clib_memset (&sw_if_indexes, 0xff, sizeof (sw_if_indexes));
699 test_create_loopback_ctx_t clcs[num_ifs];
700 clib_memset (&clcs, 0, sizeof (clcs));
701 test_delete_loopback_ctx_t dlcs[num_ifs];
702 clib_memset (&dlcs, 0, sizeof (dlcs));
704 for (i = 0; i < num_ifs; ++i)
706 memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
707 mac_addresses[i][5] = i;
708 clcs[i].sw_if_index_storage = &sw_if_indexes[i];
710 for (i = 0; i < num_ifs; ++i)
712 vapi_msg_create_loopback *cl = vapi_alloc_create_loopback (ctx);
713 memcpy (cl->payload.mac_address, mac_addresses[i],
714 sizeof (cl->payload.mac_address));
715 while (VAPI_EAGAIN ==
717 vapi_create_loopback (ctx, cl, loopback_create_cb, &clcs[i])))
719 ck_assert_int_eq (VAPI_OK, rv);
721 rv = vapi_dispatch (ctx);
722 ck_assert_int_eq (VAPI_OK, rv);
723 for (i = 0; i < num_ifs; ++i)
725 ck_assert_int_eq (1, clcs[i].called);
726 printf ("Loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
728 mac_addresses[i][0], mac_addresses[i][1], mac_addresses[i][2],
729 mac_addresses[i][3], mac_addresses[i][4], mac_addresses[i][5],
733 clib_memset (&seen, 0, sizeof (seen));
734 sw_interface_dump_ctx dctx = { false, num_ifs, sw_if_indexes, seen, 0 };
735 vapi_msg_sw_interface_dump *dump = vapi_alloc_sw_interface_dump (ctx);
736 dump->payload.name_filter_valid = 0;
737 clib_memset (dump->payload.name_filter, 0,
738 sizeof (dump->payload.name_filter));
739 while (VAPI_EAGAIN ==
741 vapi_sw_interface_dump (ctx, dump, sw_interface_dump_cb, &dctx)))
743 for (i = 0; i < num_ifs; ++i)
745 ck_assert_int_eq (false, seen[i]);
747 clib_memset (&seen, 0, sizeof (seen));
748 ck_assert_int_eq (false, dctx.last_called);
749 rv = vapi_dispatch (ctx);
750 ck_assert_int_eq (VAPI_OK, rv);
751 for (i = 0; i < num_ifs; ++i)
753 ck_assert_int_eq (true, seen[i]);
755 clib_memset (&seen, 0, sizeof (seen));
756 ck_assert_int_eq (true, dctx.last_called);
757 for (i = 0; i < num_ifs; ++i)
759 vapi_msg_delete_loopback *dl = vapi_alloc_delete_loopback (ctx);
760 dl->payload.sw_if_index = sw_if_indexes[i];
761 while (VAPI_EAGAIN ==
763 vapi_delete_loopback (ctx, dl, loopback_delete_cb, &dlcs[i])))
765 ck_assert_int_eq (VAPI_OK, rv);
767 rv = vapi_dispatch (ctx);
768 ck_assert_int_eq (VAPI_OK, rv);
769 for (i = 0; i < num_ifs; ++i)
771 ck_assert_int_eq (1, dlcs[i].called);
772 printf ("Deleted loopback with sw_if_index %u\n", sw_if_indexes[i]);
774 clib_memset (&seen, 0, sizeof (seen));
775 dctx.last_called = false;
776 dump = vapi_alloc_sw_interface_dump (ctx);
777 dump->payload.name_filter_valid = 0;
778 clib_memset (dump->payload.name_filter, 0,
779 sizeof (dump->payload.name_filter));
780 while (VAPI_EAGAIN ==
782 vapi_sw_interface_dump (ctx, dump, sw_interface_dump_cb, &dctx)))
784 rv = vapi_dispatch (ctx);
785 ck_assert_int_eq (VAPI_OK, rv);
786 for (i = 0; i < num_ifs; ++i)
788 ck_assert_int_eq (false, seen[i]);
790 clib_memset (&seen, 0, sizeof (seen));
791 ck_assert_int_eq (true, dctx.last_called);
797 generic_cb (vapi_ctx_t ctx, void *callback_ctx, vapi_msg_id_t id, void *msg)
799 int *called = callback_ctx;
800 ck_assert_int_eq (0, *called);
802 ck_assert_int_eq (id, vapi_msg_id_show_version_reply);
803 ck_assert_ptr_ne (NULL, msg);
804 vapi_msg_show_version_reply *reply = msg;
805 ck_assert_str_eq ("vpe",
806 (char *) vl_api_from_api_string (&reply->
811 START_TEST (test_show_version_5)
813 printf ("--- Receive show version using generic callback - nonblocking "
816 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
817 ck_assert_ptr_ne (NULL, sv);
818 vapi_msg_show_version_hton (sv);
819 while (VAPI_EAGAIN == (rv = vapi_send (ctx, sv)))
821 ck_assert_int_eq (VAPI_OK, rv);
823 vapi_set_generic_event_cb (ctx, generic_cb, &called);
824 ck_assert_int_eq (VAPI_OK, rv);
825 rv = vapi_dispatch_one (ctx);
826 ck_assert_int_eq (VAPI_OK, rv);
827 ck_assert_int_eq (1, called);
828 sv = vapi_alloc_show_version (ctx);
829 ck_assert_ptr_ne (NULL, sv);
830 vapi_msg_show_version_hton (sv);
831 while (VAPI_EAGAIN == (rv = vapi_send (ctx, sv)))
833 ck_assert_int_eq (VAPI_OK, rv);
834 vapi_clear_generic_event_cb (ctx);
835 rv = vapi_dispatch_one (ctx);
836 ck_assert_int_eq (VAPI_OK, rv);
837 ck_assert_int_eq (1, called); /* needs to remain unchanged */
843 show_version_no_cb (vapi_ctx_t ctx, void *caller_ctx,
844 vapi_error_e rv, bool is_last,
845 vapi_payload_show_version_reply * p)
847 ck_assert_int_eq (VAPI_ENORESP, rv);
848 ck_assert_int_eq (true, is_last);
849 ck_assert_ptr_eq (NULL, p);
850 ++*(int *) caller_ctx;
854 START_TEST (test_no_response_1)
856 printf ("--- Simulate no response to regular message ---\n");
858 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
859 ck_assert_ptr_ne (NULL, sv);
860 sv->header._vl_msg_id = ~0; /* malformed ID causes vpp to drop the msg */
862 while (VAPI_EAGAIN ==
863 (rv = vapi_show_version (ctx, sv, show_version_no_cb, &called)))
865 ck_assert_int_eq (VAPI_OK, rv);
866 sv = vapi_alloc_show_version (ctx);
867 ck_assert_ptr_ne (NULL, sv);
868 while (VAPI_EAGAIN ==
869 (rv = vapi_show_version (ctx, sv, show_version_cb, &called)))
871 ck_assert_int_eq (VAPI_OK, rv);
872 rv = vapi_dispatch (ctx);
873 ck_assert_int_eq (VAPI_OK, rv);
874 ck_assert_int_eq (2, called);
880 no_msg_cb (struct vapi_ctx_s *ctx, void *callback_ctx,
881 vapi_error_e rv, bool is_last,
882 vapi_payload_sw_interface_details * reply)
884 int *called = callback_ctx;
886 ck_assert_int_eq (VAPI_OK, rv);
887 ck_assert_int_eq (true, is_last);
888 ck_assert_ptr_eq (NULL, reply);
892 START_TEST (test_no_response_2)
894 printf ("--- Simulate no response to dump message ---\n");
896 vapi_msg_sw_interface_dump *dump = vapi_alloc_sw_interface_dump (ctx);
897 dump->header._vl_msg_id = ~0; /* malformed ID causes vpp to drop the msg */
899 while (VAPI_EAGAIN ==
900 (rv = vapi_sw_interface_dump (ctx, dump, no_msg_cb, &no_called)))
902 ck_assert_int_eq (VAPI_OK, rv);
903 rv = vapi_dispatch (ctx);
904 ck_assert_int_eq (VAPI_OK, rv);
905 ck_assert_int_eq (1, no_called);
910 START_TEST (test_unsupported)
912 printf ("--- Unsupported messages ---\n");
913 bool available = vapi_is_msg_available (ctx, vapi_msg_id_test_fake_msg);
914 ck_assert_int_eq (false, available);
922 Suite *s = suite_create ("VAPI test");
924 TCase *tc_negative = tcase_create ("Negative tests");
925 tcase_add_test (tc_negative, test_invalid_values);
926 suite_add_tcase (s, tc_negative);
928 TCase *tc_swap = tcase_create ("Byteswap tests");
929 tcase_add_test (tc_swap, test_hton_1);
930 tcase_add_test (tc_swap, test_hton_2);
931 tcase_add_test (tc_swap, test_hton_4);
932 tcase_add_test (tc_swap, test_ntoh_1);
933 tcase_add_test (tc_swap, test_ntoh_2);
934 tcase_add_test (tc_swap, test_ntoh_4);
935 suite_add_tcase (s, tc_swap);
937 TCase *tc_connect = tcase_create ("Connect");
938 tcase_add_test (tc_connect, test_connect);
939 suite_add_tcase (s, tc_connect);
941 TCase *tc_block = tcase_create ("Blocking API");
942 tcase_set_timeout (tc_block, 25);
943 tcase_add_checked_fixture (tc_block, setup_blocking, teardown);
944 tcase_add_test (tc_block, test_show_version_1);
945 tcase_add_test (tc_block, test_show_version_2);
946 tcase_add_test (tc_block, test_loopbacks_1);
947 suite_add_tcase (s, tc_block);
949 TCase *tc_nonblock = tcase_create ("Nonblocking API");
950 tcase_set_timeout (tc_nonblock, 25);
951 tcase_add_checked_fixture (tc_nonblock, setup_nonblocking, teardown);
952 tcase_add_test (tc_nonblock, test_show_version_3);
953 tcase_add_test (tc_nonblock, test_show_version_4);
954 tcase_add_test (tc_nonblock, test_show_version_5);
955 tcase_add_test (tc_nonblock, test_loopbacks_2);
956 tcase_add_test (tc_nonblock, test_no_response_1);
957 tcase_add_test (tc_nonblock, test_no_response_2);
958 suite_add_tcase (s, tc_nonblock);
960 TCase *tc_unsupported = tcase_create ("Unsupported message");
961 tcase_add_checked_fixture (tc_unsupported, setup_blocking, teardown);
962 tcase_add_test (tc_unsupported, test_unsupported);
963 suite_add_tcase (s, tc_unsupported);
969 main (int argc, char *argv[])
973 printf ("Invalid argc==`%d'\n", argc);
977 api_prefix = argv[2];
978 printf ("App name: `%s', API prefix: `%s'\n", app_name, api_prefix);
985 sr = srunner_create (s);
987 srunner_run_all (sr, CK_NORMAL);
988 number_failed = srunner_ntests_failed (sr);
990 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
994 * fd.io coding-style-patch-verification: ON
997 * eval: (c-set-style "gnu")