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/memclnt.api.vapi.h>
28 #include <vapi/vlib.api.vapi.h>
29 #include <vapi/vpe.api.vapi.h>
30 #include <vapi/interface.api.vapi.h>
31 #include <vapi/mss_clamp.api.vapi.h>
32 #include <vapi/l2.api.vapi.h>
33 #include <fake.api.vapi.h>
35 #include <vppinfra/vec.h>
36 #include <vppinfra/mem.h>
38 DEFINE_VAPI_MSG_IDS_VPE_API_JSON;
39 DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON;
40 DEFINE_VAPI_MSG_IDS_MSS_CLAMP_API_JSON;
41 DEFINE_VAPI_MSG_IDS_L2_API_JSON;
42 DEFINE_VAPI_MSG_IDS_FAKE_API_JSON;
44 static char *app_name = NULL;
45 static char *api_prefix = NULL;
46 static bool use_uds = false;
47 static const int max_outstanding_requests = 64;
48 static const int response_queue_size = 32;
50 /* centos has ancient check so we hack our way around here
51 * to make it work somehow */
52 #ifndef ck_assert_ptr_eq
53 #define ck_assert_ptr_eq(X,Y) ck_assert_int_eq((long)X, (long)Y)
56 #ifndef ck_assert_ptr_ne
57 #define ck_assert_ptr_ne(X,Y) ck_assert_int_ne((long)X, (long)Y)
60 START_TEST (test_invalid_values)
63 vapi_error_e rv = vapi_ctx_alloc (&ctx);
64 ck_assert_int_eq (VAPI_OK, rv);
65 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
66 ck_assert_ptr_eq (NULL, sv);
67 rv = vapi_send (ctx, sv);
68 ck_assert_int_eq (VAPI_EINVAL, rv);
70 vapi_connect_ex (ctx, app_name, api_prefix, max_outstanding_requests,
71 response_queue_size, VAPI_MODE_BLOCKING, true, use_uds);
72 ck_assert_int_eq (VAPI_OK, rv);
73 rv = vapi_send (ctx, NULL);
74 ck_assert_int_eq (VAPI_EINVAL, rv);
75 rv = vapi_send (NULL, NULL);
76 ck_assert_int_eq (VAPI_EINVAL, rv);
77 rv = vapi_recv (NULL, NULL, NULL, 0, 0);
78 ck_assert_int_eq (VAPI_EINVAL, rv);
79 rv = vapi_recv (ctx, NULL, NULL, 0, 0);
80 ck_assert_int_eq (VAPI_EINVAL, rv);
81 vapi_msg_show_version_reply *reply;
82 rv = vapi_recv (ctx, (void **) &reply, NULL, 0, 0);
83 ck_assert_int_eq (VAPI_EINVAL, rv);
84 rv = vapi_disconnect (ctx);
85 ck_assert_int_eq (VAPI_OK, rv);
91 START_TEST (test_hton_1)
93 const u16 _vl_msg_id = 1;
94 vapi_type_msg_header1_t h;
95 h._vl_msg_id = _vl_msg_id;
96 vapi_type_msg_header1_t_hton (&h);
97 ck_assert_int_eq (be16toh (h._vl_msg_id), _vl_msg_id);
102 START_TEST (test_hton_2)
104 const u16 _vl_msg_id = 1;
105 const u32 client_index = 3;
106 vapi_type_msg_header2_t h;
107 h._vl_msg_id = _vl_msg_id;
108 h.client_index = client_index;
109 vapi_type_msg_header2_t_hton (&h);
110 ck_assert_int_eq (be16toh (h._vl_msg_id), _vl_msg_id);
111 ck_assert_int_eq (h.client_index, client_index);
116 #define verify_hton_swap(expr, value) \
117 if (4 == sizeof (expr)) \
119 ck_assert_int_eq (expr, htobe32 (value)); \
121 else if (2 == sizeof (expr)) \
123 ck_assert_int_eq (expr, htobe16 (value)); \
127 ck_assert_int_eq (expr, value); \
130 START_TEST (test_hton_4)
132 const int vla_count = 3;
133 char x[sizeof (vapi_msg_bridge_domain_details) +
134 vla_count * sizeof (vapi_type_bridge_domain_sw_if)];
135 vapi_msg_bridge_domain_details *d = (void *) x;
137 d->header._vl_msg_id = cnt++;
138 d->header.context = cnt++;
139 d->payload.bd_id = cnt++;
140 d->payload.mac_age = cnt++;
141 d->payload.bvi_sw_if_index = cnt++;
142 d->payload.n_sw_ifs = vla_count;
144 for (i = 0; i < vla_count; ++i)
146 vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
147 det->context = cnt++;
148 det->sw_if_index = cnt++;
151 ck_assert_int_eq (sizeof (x), vapi_calc_bridge_domain_details_msg_size (d));
152 vapi_msg_bridge_domain_details_hton (d);
154 verify_hton_swap (d->header._vl_msg_id, tmp);
156 ck_assert_int_eq (d->header.context, tmp);
158 verify_hton_swap (d->payload.bd_id, tmp);
160 verify_hton_swap (d->payload.mac_age, tmp);
162 verify_hton_swap (d->payload.bvi_sw_if_index, tmp);
164 ck_assert_int_eq (d->payload.n_sw_ifs, htobe32 (vla_count));
165 for (i = 0; i < vla_count; ++i)
167 vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
168 verify_hton_swap (det->context, tmp);
170 verify_hton_swap (det->sw_if_index, tmp);
172 verify_hton_swap (det->shg, tmp);
175 vapi_msg_bridge_domain_details_ntoh (d);
177 ck_assert_int_eq (d->header._vl_msg_id, tmp);
179 ck_assert_int_eq (d->header.context, tmp);
181 ck_assert_int_eq (d->payload.bd_id, tmp);
183 ck_assert_int_eq (d->payload.mac_age, tmp);
185 ck_assert_int_eq (d->payload.bvi_sw_if_index, tmp);
187 ck_assert_int_eq (d->payload.n_sw_ifs, vla_count);
188 for (i = 0; i < vla_count; ++i)
190 vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
191 ck_assert_int_eq (det->context, tmp);
193 ck_assert_int_eq (det->sw_if_index, tmp);
195 ck_assert_int_eq (det->shg, tmp);
198 ck_assert_int_eq (sizeof (x), vapi_calc_bridge_domain_details_msg_size (d));
203 START_TEST (test_ntoh_1)
205 const u16 _vl_msg_id = 1;
206 vapi_type_msg_header1_t h;
207 h._vl_msg_id = _vl_msg_id;
208 vapi_type_msg_header1_t_ntoh (&h);
209 ck_assert_int_eq (htobe16 (h._vl_msg_id), _vl_msg_id);
214 START_TEST (test_ntoh_2)
216 const u16 _vl_msg_id = 1;
217 const u32 client_index = 3;
218 vapi_type_msg_header2_t h;
219 h._vl_msg_id = _vl_msg_id;
220 h.client_index = client_index;
221 vapi_type_msg_header2_t_ntoh (&h);
222 ck_assert_int_eq (htobe16 (h._vl_msg_id), _vl_msg_id);
223 ck_assert_int_eq (h.client_index, client_index);
228 #define verify_ntoh_swap(expr, value) \
229 if (4 == sizeof (expr)) \
231 ck_assert_int_eq (expr, be32toh (value)); \
233 else if (2 == sizeof (expr)) \
235 ck_assert_int_eq (expr, be16toh (value)); \
239 ck_assert_int_eq (expr, value); \
242 START_TEST (test_ntoh_4)
244 const int vla_count = 3;
245 char x[sizeof (vapi_msg_bridge_domain_details) +
246 vla_count * sizeof (vapi_type_bridge_domain_sw_if)];
247 vapi_msg_bridge_domain_details *d = (void *) x;
249 d->header._vl_msg_id = cnt++;
250 d->header.context = cnt++;
251 d->payload.bd_id = cnt++;
252 d->payload.mac_age = cnt++;
253 d->payload.bvi_sw_if_index = cnt++;
254 d->payload.n_sw_ifs = htobe32 (vla_count);
256 for (i = 0; i < vla_count; ++i)
258 vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
259 det->context = cnt++;
260 det->sw_if_index = cnt++;
263 vapi_msg_bridge_domain_details_ntoh (d);
264 ck_assert_int_eq (sizeof (x), vapi_calc_bridge_domain_details_msg_size (d));
266 verify_ntoh_swap (d->header._vl_msg_id, tmp);
268 ck_assert_int_eq (d->header.context, tmp);
270 verify_ntoh_swap (d->payload.bd_id, tmp);
272 verify_ntoh_swap (d->payload.mac_age, tmp);
274 verify_ntoh_swap (d->payload.bvi_sw_if_index, tmp);
276 ck_assert_int_eq (d->payload.n_sw_ifs, 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 verify_ntoh_swap (det->context, tmp);
282 verify_ntoh_swap (det->sw_if_index, tmp);
284 verify_ntoh_swap (det->shg, tmp);
287 vapi_msg_bridge_domain_details_hton (d);
289 ck_assert_int_eq (d->header._vl_msg_id, tmp);
291 ck_assert_int_eq (d->header.context, tmp);
293 ck_assert_int_eq (d->payload.bd_id, tmp);
295 ck_assert_int_eq (d->payload.mac_age, tmp);
297 ck_assert_int_eq (d->payload.bvi_sw_if_index, tmp);
299 ck_assert_int_eq (d->payload.n_sw_ifs, htobe32 (vla_count));
300 for (i = 0; i < vla_count; ++i)
302 vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
303 ck_assert_int_eq (det->context, tmp);
305 ck_assert_int_eq (det->sw_if_index, tmp);
307 ck_assert_int_eq (det->shg, tmp);
315 show_version_cb (vapi_ctx_t ctx, void *caller_ctx,
316 vapi_error_e rv, bool is_last,
317 vapi_payload_show_version_reply * p)
319 ck_assert_int_eq (VAPI_OK, rv);
320 ck_assert_int_eq (true, is_last);
321 ck_assert_str_eq ("vpe", (char *) p->program);
323 ("show_version_reply: program: `%s', version: `%s', build directory: "
324 "`%s', build date: `%s'\n", p->program, p->version, p->build_directory,
326 ++*(int *) caller_ctx;
334 u32 *sw_if_index_storage;
335 } test_create_loopback_ctx_t;
338 loopback_create_cb (vapi_ctx_t ctx, void *caller_ctx,
339 vapi_error_e rv, bool is_last,
340 vapi_payload_create_loopback_reply * p)
342 test_create_loopback_ctx_t *clc = caller_ctx;
343 ck_assert_int_eq (clc->expected_retval, p->retval);
344 *clc->sw_if_index_storage = p->sw_if_index;
353 u32 *sw_if_index_storage;
354 } test_delete_loopback_ctx_t;
357 loopback_delete_cb (vapi_ctx_t ctx, void *caller_ctx,
358 vapi_error_e rv, bool is_last,
359 vapi_payload_delete_loopback_reply * p)
361 test_delete_loopback_ctx_t *dlc = caller_ctx;
362 ck_assert_int_eq (dlc->expected_retval, p->retval);
367 START_TEST (test_connect)
370 vapi_error_e rv = vapi_ctx_alloc (&ctx);
371 ck_assert_int_eq (VAPI_OK, rv);
373 vapi_connect_ex (ctx, app_name, api_prefix, max_outstanding_requests,
374 response_queue_size, VAPI_MODE_BLOCKING, true, use_uds);
375 ck_assert_int_eq (VAPI_OK, rv);
376 rv = vapi_disconnect (ctx);
377 ck_assert_int_eq (VAPI_OK, rv);
386 setup_blocking (void)
388 vapi_error_e rv = vapi_ctx_alloc (&ctx);
389 ck_assert_int_eq (VAPI_OK, rv);
391 vapi_connect_ex (ctx, app_name, api_prefix, max_outstanding_requests,
392 response_queue_size, VAPI_MODE_BLOCKING, true, use_uds);
393 ck_assert_int_eq (VAPI_OK, rv);
397 setup_nonblocking (void)
399 vapi_error_e rv = vapi_ctx_alloc (&ctx);
400 ck_assert_int_eq (VAPI_OK, rv);
401 rv = vapi_connect_ex (ctx, app_name, api_prefix, max_outstanding_requests,
402 response_queue_size, VAPI_MODE_NONBLOCKING, true,
404 ck_assert_int_eq (VAPI_OK, rv);
410 vapi_disconnect (ctx);
414 START_TEST (test_show_version_1)
416 printf ("--- Basic show version message - reply test ---\n");
417 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
418 ck_assert_ptr_ne (NULL, sv);
419 vapi_msg_show_version_hton (sv);
420 vapi_error_e rv = vapi_send (ctx, sv);
421 ck_assert_int_eq (VAPI_OK, rv);
422 vapi_msg_show_version_reply *resp;
424 rv = vapi_recv (ctx, (void *) &resp, &size, 0, 0);
425 ck_assert_int_eq (VAPI_OK, rv);
427 show_version_cb (NULL, &placeholder, VAPI_OK, true, &resp->payload);
428 vapi_msg_free (ctx, resp);
433 START_TEST (test_show_version_2)
436 printf ("--- Show version via blocking callback API ---\n");
437 const int attempts = response_queue_size * 4;
439 for (i = 0; i < attempts; ++i)
441 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
442 ck_assert_ptr_ne (NULL, sv);
443 vapi_error_e rv = vapi_show_version (ctx, sv, show_version_cb, &called);
444 ck_assert_int_eq (VAPI_OK, rv);
446 ck_assert_int_eq (attempts, called);
458 } sw_interface_dump_ctx;
461 sw_interface_dump_cb (struct vapi_ctx_s *ctx, void *callback_ctx,
462 vapi_error_e rv, bool is_last,
463 vapi_payload_sw_interface_details * reply)
465 sw_interface_dump_ctx *dctx = callback_ctx;
466 ck_assert_int_eq (false, dctx->last_called);
469 ck_assert (NULL == reply);
470 dctx->last_called = true;
474 ck_assert (NULL != reply);
475 printf ("Interface dump entry: [%u]: %s\n", reply->sw_if_index,
476 reply->interface_name);
478 for (i = 0; i < dctx->num_ifs; ++i)
480 if (dctx->sw_if_indexes[i] == reply->sw_if_index)
482 ck_assert_int_eq (false, dctx->seen[i]);
483 dctx->seen[i] = true;
492 vapi_mss_clamp_enable_disable_reply_cb (
493 struct vapi_ctx_s *ctx, void *callback_ctx, vapi_error_e rv, bool is_last,
494 vapi_payload_mss_clamp_enable_disable_reply *reply)
496 bool *x = callback_ctx;
502 vapi_mss_clamp_get_reply_cb (struct vapi_ctx_s *ctx, void *callback_ctx,
503 vapi_error_e rv, bool is_last,
504 vapi_payload_mss_clamp_get_reply *reply)
506 int *counter = callback_ctx;
507 ck_assert_int_gt (*counter, 0); // make sure details were called first
509 ck_assert_int_eq (is_last, true);
510 printf ("Got mss clamp reply error %d\n", rv);
511 ck_assert_int_eq (rv, VAPI_OK);
512 printf ("counter is %d", *counter);
517 vapi_mss_clamp_get_details_cb (struct vapi_ctx_s *ctx, void *callback_ctx,
518 vapi_error_e rv, bool is_last,
519 vapi_payload_mss_clamp_details *details)
521 int *counter = callback_ctx;
525 printf ("Got ipv4 mss clamp to %u for sw_if_index %u\n",
526 details->ipv4_mss, details->sw_if_index);
527 ck_assert_int_eq (details->ipv4_mss, 1000 + details->sw_if_index);
529 printf ("counter is %d", *counter);
533 START_TEST (test_loopbacks_1)
535 printf ("--- Create/delete loopbacks using blocking API ---\n");
536 const size_t num_ifs = 5;
537 u8 mac_addresses[num_ifs][6];
538 clib_memset (&mac_addresses, 0, sizeof (mac_addresses));
539 u32 sw_if_indexes[num_ifs];
540 clib_memset (&sw_if_indexes, 0xff, sizeof (sw_if_indexes));
541 test_create_loopback_ctx_t clcs[num_ifs];
542 clib_memset (&clcs, 0, sizeof (clcs));
543 test_delete_loopback_ctx_t dlcs[num_ifs];
544 clib_memset (&dlcs, 0, sizeof (dlcs));
546 for (i = 0; i < num_ifs; ++i)
548 memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
549 mac_addresses[i][5] = i;
550 clcs[i].sw_if_index_storage = &sw_if_indexes[i];
552 for (i = 0; i < num_ifs; ++i)
554 vapi_msg_create_loopback *cl = vapi_alloc_create_loopback (ctx);
556 for (j = 0; j < 6; ++j)
558 cl->payload.mac_address[j] = mac_addresses[i][j];
561 vapi_create_loopback (ctx, cl, loopback_create_cb, &clcs[i]);
562 ck_assert_int_eq (VAPI_OK, rv);
564 for (i = 0; i < num_ifs; ++i)
566 ck_assert_int_eq (1, clcs[i].called);
567 printf ("Created loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
569 mac_addresses[i][0], mac_addresses[i][1], mac_addresses[i][2],
570 mac_addresses[i][3], mac_addresses[i][4], mac_addresses[i][5],
575 for (int i = 0; i < num_ifs; ++i)
577 vapi_msg_mss_clamp_enable_disable *mc =
578 vapi_alloc_mss_clamp_enable_disable (ctx);
579 mc->payload.sw_if_index = sw_if_indexes[i];
580 mc->payload.ipv4_mss = 1000 + sw_if_indexes[i];
581 mc->payload.ipv4_direction = MSS_CLAMP_DIR_RX;
582 bool reply_ctx = false;
583 printf ("Set ipv4 mss clamp to %u for sw_if_index %u\n",
584 mc->payload.ipv4_mss, mc->payload.sw_if_index);
585 vapi_error_e rv = vapi_mss_clamp_enable_disable (
586 ctx, mc, vapi_mss_clamp_enable_disable_reply_cb, &reply_ctx);
587 ck_assert_int_eq (VAPI_OK, rv);
588 ck_assert_int_eq (reply_ctx, true);
594 vapi_msg_mss_clamp_get *msg = vapi_alloc_mss_clamp_get (ctx);
595 msg->payload.sw_if_index = ~0;
597 vapi_mss_clamp_get (ctx, msg, vapi_mss_clamp_get_reply_cb, &counter,
598 vapi_mss_clamp_get_details_cb, &counter);
599 printf ("counter is %d", counter);
600 ck_assert_int_eq (VAPI_OK, rv);
601 ck_assert_int_eq (counter, num_ifs + 1);
605 sw_interface_dump_ctx dctx = { false, num_ifs, sw_if_indexes, seen, 0 };
606 vapi_msg_sw_interface_dump *dump;
608 const int attempts = response_queue_size * 4;
609 for (i = 0; i < attempts; ++i)
611 dctx.last_called = false;
612 clib_memset (&seen, 0, sizeof (seen));
613 dump = vapi_alloc_sw_interface_dump (ctx, 0);
614 while (VAPI_EAGAIN ==
616 vapi_sw_interface_dump (ctx, dump, sw_interface_dump_cb,
619 ck_assert_int_eq (true, dctx.last_called);
621 for (j = 0; j < num_ifs; ++j)
623 ck_assert_int_eq (true, seen[j]);
626 clib_memset (&seen, 0, sizeof (seen));
627 for (i = 0; i < num_ifs; ++i)
629 vapi_msg_delete_loopback *dl = vapi_alloc_delete_loopback (ctx);
630 dl->payload.sw_if_index = sw_if_indexes[i];
632 vapi_delete_loopback (ctx, dl, loopback_delete_cb, &dlcs[i]);
633 ck_assert_int_eq (VAPI_OK, rv);
635 for (i = 0; i < num_ifs; ++i)
637 ck_assert_int_eq (1, dlcs[i].called);
638 printf ("Deleted loopback with sw_if_index %u\n", sw_if_indexes[i]);
640 dctx.last_called = false;
641 clib_memset (&seen, 0, sizeof (seen));
642 dump = vapi_alloc_sw_interface_dump (ctx, 0);
643 while (VAPI_EAGAIN ==
645 vapi_sw_interface_dump (ctx, dump, sw_interface_dump_cb, &dctx)))
647 ck_assert_int_eq (true, dctx.last_called);
648 for (i = 0; i < num_ifs; ++i)
650 ck_assert_int_eq (false, seen[i]);
656 START_TEST (test_show_version_3)
658 printf ("--- Show version via async callback ---\n");
661 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
662 ck_assert_ptr_ne (NULL, sv);
663 while (VAPI_EAGAIN ==
664 (rv = vapi_show_version (ctx, sv, show_version_cb, &called)))
666 ck_assert_int_eq (VAPI_OK, rv);
667 ck_assert_int_eq (0, called);
668 while (VAPI_EAGAIN == (rv = vapi_dispatch (ctx)))
670 ck_assert_int_eq (VAPI_OK, rv);
671 ck_assert_int_eq (1, called);
673 rv = vapi_dispatch (ctx);
674 ck_assert_int_eq (VAPI_OK, rv);
675 ck_assert_int_eq (0, called);
680 START_TEST (test_show_version_4)
682 printf ("--- Show version via async callback - multiple messages ---\n");
684 const size_t num_req = 5;
685 int contexts[num_req];
686 clib_memset (contexts, 0, sizeof (contexts));
688 for (i = 0; i < num_req; ++i)
690 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
691 ck_assert_ptr_ne (NULL, sv);
692 while (VAPI_EAGAIN ==
694 vapi_show_version (ctx, sv, show_version_cb, &contexts[i])))
696 ck_assert_int_eq (VAPI_OK, rv);
698 for (j = 0; j < num_req; ++j)
700 ck_assert_int_eq (0, contexts[j]);
703 while (VAPI_EAGAIN == (rv = vapi_dispatch (ctx)))
705 ck_assert_int_eq (VAPI_OK, rv);
706 for (i = 0; i < num_req; ++i)
708 ck_assert_int_eq (1, contexts[i]);
710 clib_memset (contexts, 0, sizeof (contexts));
711 while (VAPI_EAGAIN == (rv = vapi_dispatch (ctx)))
713 ck_assert_int_eq (VAPI_OK, rv);
714 for (i = 0; i < num_req; ++i)
716 ck_assert_int_eq (0, contexts[i]);
722 START_TEST (test_loopbacks_2)
724 printf ("--- Create/delete loopbacks using non-blocking API ---\n");
726 const size_t num_ifs = 5;
727 u8 mac_addresses[num_ifs][6];
728 clib_memset (&mac_addresses, 0, sizeof (mac_addresses));
729 u32 sw_if_indexes[num_ifs];
730 clib_memset (&sw_if_indexes, 0xff, sizeof (sw_if_indexes));
731 test_create_loopback_ctx_t clcs[num_ifs];
732 clib_memset (&clcs, 0, sizeof (clcs));
733 test_delete_loopback_ctx_t dlcs[num_ifs];
734 clib_memset (&dlcs, 0, sizeof (dlcs));
736 for (i = 0; i < num_ifs; ++i)
738 memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
739 mac_addresses[i][5] = i;
740 clcs[i].sw_if_index_storage = &sw_if_indexes[i];
742 for (i = 0; i < num_ifs; ++i)
744 vapi_msg_create_loopback *cl = vapi_alloc_create_loopback (ctx);
746 for (j = 0; j < 6; ++j)
748 cl->payload.mac_address[j] = mac_addresses[i][j];
750 while (VAPI_EAGAIN ==
752 vapi_create_loopback (ctx, cl, loopback_create_cb, &clcs[i])))
754 ck_assert_int_eq (VAPI_OK, rv);
756 while (VAPI_EAGAIN == (rv = vapi_dispatch (ctx)))
758 ck_assert_int_eq (VAPI_OK, rv);
759 for (i = 0; i < num_ifs; ++i)
761 ck_assert_int_eq (1, clcs[i].called);
762 printf ("Loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
764 mac_addresses[i][0], mac_addresses[i][1], mac_addresses[i][2],
765 mac_addresses[i][3], mac_addresses[i][4], mac_addresses[i][5],
769 clib_memset (&seen, 0, sizeof (seen));
770 sw_interface_dump_ctx dctx = { false, num_ifs, sw_if_indexes, seen, 0 };
771 vapi_msg_sw_interface_dump *dump = vapi_alloc_sw_interface_dump (ctx, 0);
772 while (VAPI_EAGAIN ==
774 vapi_sw_interface_dump (ctx, dump, sw_interface_dump_cb, &dctx)))
776 for (i = 0; i < num_ifs; ++i)
778 ck_assert_int_eq (false, seen[i]);
780 clib_memset (&seen, 0, sizeof (seen));
781 ck_assert_int_eq (false, dctx.last_called);
782 while (VAPI_EAGAIN == (rv = vapi_dispatch (ctx)))
784 ck_assert_int_eq (VAPI_OK, rv);
785 for (i = 0; i < num_ifs; ++i)
787 ck_assert_int_eq (true, seen[i]);
789 clib_memset (&seen, 0, sizeof (seen));
790 ck_assert_int_eq (true, dctx.last_called);
791 for (i = 0; i < num_ifs; ++i)
793 vapi_msg_delete_loopback *dl = vapi_alloc_delete_loopback (ctx);
794 dl->payload.sw_if_index = sw_if_indexes[i];
795 while (VAPI_EAGAIN ==
797 vapi_delete_loopback (ctx, dl, loopback_delete_cb, &dlcs[i])))
799 ck_assert_int_eq (VAPI_OK, rv);
801 while (VAPI_EAGAIN == (rv = vapi_dispatch (ctx)))
803 ck_assert_int_eq (VAPI_OK, rv);
804 for (i = 0; i < num_ifs; ++i)
806 ck_assert_int_eq (1, dlcs[i].called);
807 printf ("Deleted loopback with sw_if_index %u\n", sw_if_indexes[i]);
809 clib_memset (&seen, 0, sizeof (seen));
810 dctx.last_called = false;
811 dump = vapi_alloc_sw_interface_dump (ctx, 0);
812 while (VAPI_EAGAIN ==
814 vapi_sw_interface_dump (ctx, dump, sw_interface_dump_cb, &dctx)))
816 while (VAPI_EAGAIN == (rv = vapi_dispatch (ctx)))
818 ck_assert_int_eq (VAPI_OK, rv);
819 for (i = 0; i < num_ifs; ++i)
821 ck_assert_int_eq (false, seen[i]);
823 clib_memset (&seen, 0, sizeof (seen));
824 ck_assert_int_eq (true, dctx.last_called);
830 generic_cb (vapi_ctx_t ctx, void *callback_ctx, vapi_msg_id_t id, void *msg)
832 int *called = callback_ctx;
833 ck_assert_int_eq (0, *called);
835 ck_assert_int_eq (id, vapi_msg_id_show_version_reply);
836 ck_assert_ptr_ne (NULL, msg);
837 vapi_msg_show_version_reply *reply = msg;
838 ck_assert_str_eq ("vpe", (char *) reply->payload.program);
842 START_TEST (test_show_version_5)
844 printf ("--- Receive show version using generic callback - nonblocking "
847 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
848 ck_assert_ptr_ne (NULL, sv);
849 vapi_msg_show_version_hton (sv);
850 while (VAPI_EAGAIN == (rv = vapi_send (ctx, sv)))
852 ck_assert_int_eq (VAPI_OK, rv);
854 vapi_set_generic_event_cb (ctx, generic_cb, &called);
855 ck_assert_int_eq (VAPI_OK, rv);
856 while (VAPI_EAGAIN == (rv = vapi_dispatch_one (ctx)))
858 ck_assert_int_eq (VAPI_OK, rv);
859 ck_assert_int_eq (1, called);
860 sv = vapi_alloc_show_version (ctx);
861 ck_assert_ptr_ne (NULL, sv);
862 vapi_msg_show_version_hton (sv);
863 while (VAPI_EAGAIN == (rv = vapi_send (ctx, sv)))
865 ck_assert_int_eq (VAPI_OK, rv);
866 vapi_clear_generic_event_cb (ctx);
867 while (VAPI_EAGAIN == (rv = vapi_dispatch_one (ctx)))
869 ck_assert_int_eq (VAPI_OK, rv);
870 ck_assert_int_eq (1, called); /* needs to remain unchanged */
876 show_version_no_cb (vapi_ctx_t ctx, void *caller_ctx,
877 vapi_error_e rv, bool is_last,
878 vapi_payload_show_version_reply * p)
880 ck_assert_int_eq (VAPI_ENORESP, rv);
881 ck_assert_int_eq (true, is_last);
882 ck_assert_ptr_eq (NULL, p);
883 ++*(int *) caller_ctx;
887 START_TEST (test_no_response_1)
889 printf ("--- Simulate no response to regular message ---\n");
891 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
892 ck_assert_ptr_ne (NULL, sv);
893 sv->header._vl_msg_id = ~0; /* malformed ID causes vpp to drop the msg */
895 while (VAPI_EAGAIN ==
896 (rv = vapi_show_version (ctx, sv, show_version_no_cb, &called)))
898 ck_assert_int_eq (VAPI_OK, rv);
899 sv = vapi_alloc_show_version (ctx);
900 ck_assert_ptr_ne (NULL, sv);
901 while (VAPI_EAGAIN ==
902 (rv = vapi_show_version (ctx, sv, show_version_cb, &called)))
904 ck_assert_int_eq (VAPI_OK, rv);
905 while (VAPI_EAGAIN == (rv = vapi_dispatch (ctx)))
907 ck_assert_int_eq (VAPI_OK, rv);
908 ck_assert_int_eq (2, called);
914 no_msg_cb (struct vapi_ctx_s *ctx, void *callback_ctx,
915 vapi_error_e rv, bool is_last,
916 vapi_payload_sw_interface_details * reply)
918 int *called = callback_ctx;
920 ck_assert_int_eq (VAPI_OK, rv);
921 ck_assert_int_eq (true, is_last);
922 ck_assert_ptr_eq (NULL, reply);
926 START_TEST (test_no_response_2)
928 printf ("--- Simulate no response to dump message ---\n");
930 vapi_msg_sw_interface_dump *dump = vapi_alloc_sw_interface_dump (ctx, 0);
931 dump->header._vl_msg_id = ~0; /* malformed ID causes vpp to drop the msg */
933 while (VAPI_EAGAIN ==
934 (rv = vapi_sw_interface_dump (ctx, dump, no_msg_cb, &no_called)))
936 ck_assert_int_eq (VAPI_OK, rv);
937 while (VAPI_EAGAIN == (rv = vapi_dispatch (ctx)))
939 ck_assert_int_eq (VAPI_OK, rv);
940 ck_assert_int_eq (1, no_called);
945 START_TEST (test_unsupported)
947 printf ("--- Unsupported messages ---\n");
948 bool available = vapi_is_msg_available (ctx, vapi_msg_id_test_fake_msg);
949 ck_assert_int_eq (false, available);
954 START_TEST (test_api_strings)
956 printf ("--- Invalid api strings ---\n");
958 /* test string 'TEST'
962 const char str[] = "TEST";
963 u8 *vec_str = 0, *vstr = 0;
966 vapi_msg_sw_interface_dump *dump =
967 malloc (sizeof (vapi_msg_sw_interface_dump) + strlen (str));
968 clib_mem_init (0, 1 << 20);
970 vl_api_c_string_to_api_string (str, &dump->payload.name_filter);
971 /* Assert nul terminator NOT present */
972 ck_assert_int_eq (vl_api_string_len (&dump->payload.name_filter),
975 cstr = vl_api_from_api_to_new_c_string (&dump->payload.name_filter);
976 ck_assert_ptr_ne (cstr, NULL);
977 /* Assert nul terminator present */
978 ck_assert_int_eq (vec_len (cstr), sizeof (str));
979 ck_assert_int_eq (strlen (str), strlen (cstr));
982 vstr = vl_api_from_api_to_new_vec (0 /* not really an API message */ ,
983 &dump->payload.name_filter);
984 ck_assert_ptr_ne (vstr, NULL);
985 /* Assert nul terminator NOT present */
986 ck_assert_int_eq (vec_len (vstr), strlen (str));
989 /* vector conaining NON nul terminated string 'TEST' */
990 vec_add (vec_str, str, strlen (str));
991 clib_memset (dump->payload.name_filter.buf, 0, strlen (str));
992 dump->payload.name_filter.length = 0;
994 vl_api_vec_to_api_string (vec_str, &dump->payload.name_filter);
995 /* Assert nul terminator NOT present */
996 ck_assert_int_eq (vl_api_string_len (&dump->payload.name_filter),
999 cstr = vl_api_from_api_to_new_c_string (&dump->payload.name_filter);
1000 ck_assert_ptr_ne (cstr, NULL);
1001 /* Assert nul terminator present */
1002 ck_assert_int_eq (vec_len (cstr), sizeof (str));
1003 ck_assert_int_eq (strlen (str), strlen (cstr));
1006 vstr = vl_api_from_api_to_new_vec (0 /* not a real api msg */ ,
1007 &dump->payload.name_filter);
1008 ck_assert_ptr_ne (vstr, NULL);
1009 /* Assert nul terminator NOT present */
1010 ck_assert_int_eq (vec_len (vstr), strlen (str));
1020 Suite *s = suite_create ("VAPI test");
1022 TCase *tc_negative = tcase_create ("Negative tests");
1023 tcase_add_test (tc_negative, test_invalid_values);
1024 suite_add_tcase (s, tc_negative);
1026 TCase *tc_swap = tcase_create ("Byteswap tests");
1027 tcase_add_test (tc_swap, test_hton_1);
1028 tcase_add_test (tc_swap, test_hton_2);
1029 tcase_add_test (tc_swap, test_hton_4);
1030 tcase_add_test (tc_swap, test_ntoh_1);
1031 tcase_add_test (tc_swap, test_ntoh_2);
1032 tcase_add_test (tc_swap, test_ntoh_4);
1033 suite_add_tcase (s, tc_swap);
1035 TCase *tc_connect = tcase_create ("Connect");
1036 tcase_add_test (tc_connect, test_connect);
1037 suite_add_tcase (s, tc_connect);
1039 TCase *tc_block = tcase_create ("Blocking API");
1040 tcase_set_timeout (tc_block, 25);
1041 tcase_add_checked_fixture (tc_block, setup_blocking, teardown);
1042 tcase_add_test (tc_block, test_show_version_1);
1043 tcase_add_test (tc_block, test_show_version_2);
1044 tcase_add_test (tc_block, test_loopbacks_1);
1045 suite_add_tcase (s, tc_block);
1047 TCase *tc_nonblock = tcase_create ("Nonblocking API");
1048 tcase_set_timeout (tc_nonblock, 25);
1049 tcase_add_checked_fixture (tc_nonblock, setup_nonblocking, teardown);
1050 tcase_add_test (tc_nonblock, test_show_version_3);
1051 tcase_add_test (tc_nonblock, test_show_version_4);
1052 tcase_add_test (tc_nonblock, test_show_version_5);
1053 tcase_add_test (tc_nonblock, test_loopbacks_2);
1054 tcase_add_test (tc_nonblock, test_no_response_1);
1055 tcase_add_test (tc_nonblock, test_no_response_2);
1056 suite_add_tcase (s, tc_nonblock);
1058 TCase *tc_unsupported = tcase_create ("Unsupported message");
1059 tcase_add_checked_fixture (tc_unsupported, setup_blocking, teardown);
1060 tcase_add_test (tc_unsupported, test_unsupported);
1061 suite_add_tcase (s, tc_unsupported);
1063 TCase *tc_dynamic = tcase_create ("Dynamic message size");
1064 tcase_add_test (tc_dynamic, test_api_strings);
1065 suite_add_tcase (s, tc_dynamic);
1071 main (int argc, char *argv[])
1075 printf ("Invalid argc==`%d'\n", argc);
1076 return EXIT_FAILURE;
1079 api_prefix = argv[2];
1080 if (!strcmp (argv[3], "shm"))
1082 else if (!strcmp (argv[3], "uds"))
1086 printf ("Unrecognised required argument '%s', expected 'uds' or 'shm'.",
1088 return EXIT_FAILURE;
1090 printf ("App name: `%s', API prefix: `%s'\n", app_name, api_prefix);
1097 sr = srunner_create (s);
1099 srunner_run_all (sr, CK_NORMAL);
1100 number_failed = srunner_ntests_failed (sr);
1102 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
1106 * fd.io coding-style-patch-verification: ON
1109 * eval: (c-set-style "gnu")