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 *------------------------------------------------------------------
24 #include <vapi/vapi.hpp>
25 #include <vapi/vpe.api.vapi.hpp>
26 #include <vapi/interface.api.vapi.hpp>
27 #include <vapi/stats.api.vapi.hpp>
28 #include <fake.api.vapi.hpp>
30 DEFINE_VAPI_MSG_IDS_VPE_API_JSON;
31 DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON;
32 DEFINE_VAPI_MSG_IDS_STATS_API_JSON;
33 DEFINE_VAPI_MSG_IDS_FAKE_API_JSON;
35 static char *app_name = nullptr;
36 static char *api_prefix = nullptr;
37 static const int max_outstanding_requests = 32;
38 static const int response_queue_size = 32;
40 #define WAIT_FOR_RESPONSE(param, ret) \
43 ret = con.wait_for_response (param); \
45 while (ret == VAPI_EAGAIN)
49 void verify_show_version_reply (const Show_version_reply &r)
51 auto &p = r.get_payload ();
52 printf ("show_version_reply: program: `%s', version: `%s', build directory: "
53 "`%s', build date: `%s'\n",
54 p.program, p.version, p.build_directory, p.build_date);
55 ck_assert_str_eq ("vpe", (char *)p.program);
62 vapi_error_e rv = con.connect (
63 app_name, api_prefix, max_outstanding_requests, response_queue_size);
64 ck_assert_int_eq (VAPI_OK, rv);
72 START_TEST (test_show_version_1)
74 printf ("--- Show version by reading response associated to request ---\n");
75 Show_version sv (con);
76 vapi_error_e rv = sv.execute ();
77 ck_assert_int_eq (VAPI_OK, rv);
78 WAIT_FOR_RESPONSE (sv, rv);
79 ck_assert_int_eq (VAPI_OK, rv);
80 auto &r = sv.get_response ();
81 verify_show_version_reply (r);
86 struct Show_version_cb
88 Show_version_cb () : called{0} {};
90 vapi_error_e operator() (Show_version &sv)
92 auto &r = sv.get_response ();
93 verify_show_version_reply (r);
99 START_TEST (test_show_version_2)
101 printf ("--- Show version by getting a callback ---\n");
103 Show_version sv (con, std::ref (cb));
104 vapi_error_e rv = sv.execute ();
105 ck_assert_int_eq (VAPI_OK, rv);
107 ck_assert_int_eq (1, cb.called);
112 START_TEST (test_loopbacks_1)
114 printf ("--- Create/delete loopbacks by waiting for response ---\n");
115 const auto num_ifs = 5;
116 u8 mac_addresses[num_ifs][6];
117 memset (&mac_addresses, 0, sizeof (mac_addresses));
118 u32 sw_if_indexes[num_ifs];
119 memset (&sw_if_indexes, 0xff, sizeof (sw_if_indexes));
120 for (int i = 0; i < num_ifs; ++i)
122 memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
123 mac_addresses[i][5] = i;
125 for (int i = 0; i < num_ifs; ++i)
127 Create_loopback cl (con);
128 auto &p = cl.get_request ().get_payload ();
129 memcpy (p.mac_address, mac_addresses[i], sizeof (p.mac_address));
130 auto e = cl.execute ();
131 ck_assert_int_eq (VAPI_OK, e);
133 WAIT_FOR_RESPONSE (cl, rv);
134 ck_assert_int_eq (VAPI_OK, rv);
135 auto &rp = cl.get_response ().get_payload ();
136 ck_assert_int_eq (0, rp.retval);
137 sw_if_indexes[i] = rp.sw_if_index;
139 for (int i = 0; i < num_ifs; ++i)
141 printf ("Created loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
143 mac_addresses[i][0], mac_addresses[i][1], mac_addresses[i][2],
144 mac_addresses[i][3], mac_addresses[i][4], mac_addresses[i][5],
149 bool seen[num_ifs] = {0};
150 Sw_interface_dump d (con);
151 auto &p = d.get_request ().get_payload ();
152 p.name_filter_valid = 0;
153 memset (p.name_filter, 0, sizeof (p.name_filter));
154 auto rv = d.execute ();
155 ck_assert_int_eq (VAPI_OK, rv);
156 WAIT_FOR_RESPONSE (d, rv);
157 ck_assert_int_eq (VAPI_OK, rv);
158 auto &rs = d.get_result_set ();
161 auto &p = r.get_payload ();
162 for (int i = 0; i < num_ifs; ++i)
164 if (sw_if_indexes[i] == p.sw_if_index)
166 ck_assert_int_eq (0, seen[i]);
171 for (int i = 0; i < num_ifs; ++i)
173 ck_assert_int_eq (1, seen[i]);
177 for (int i = 0; i < num_ifs; ++i)
179 Delete_loopback dl (con);
180 dl.get_request ().get_payload ().sw_if_index = sw_if_indexes[i];
181 auto rv = dl.execute ();
182 ck_assert_int_eq (VAPI_OK, rv);
183 WAIT_FOR_RESPONSE (dl, rv);
184 ck_assert_int_eq (VAPI_OK, rv);
185 auto &response = dl.get_response ();
186 auto rp = response.get_payload ();
187 ck_assert_int_eq (0, rp.retval);
188 printf ("Deleted loopback with sw_if_index %u\n", sw_if_indexes[i]);
192 Sw_interface_dump d (con);
193 auto &p = d.get_request ().get_payload ();
194 p.name_filter_valid = 0;
195 memset (p.name_filter, 0, sizeof (p.name_filter));
196 auto rv = d.execute ();
197 ck_assert_int_eq (VAPI_OK, rv);
198 WAIT_FOR_RESPONSE (d, rv);
199 ck_assert_int_eq (VAPI_OK, rv);
200 auto &rs = d.get_result_set ();
203 auto &p = r.get_payload ();
204 for (int i = 0; i < num_ifs; ++i)
206 ck_assert_int_ne (sw_if_indexes[i], p.sw_if_index);
214 struct Create_loopback_cb
216 Create_loopback_cb () : called{0}, sw_if_index{0} {};
220 vapi_error_e operator() (Create_loopback &cl)
222 auto &r = cl.get_response ();
223 sw_if_index = r.get_payload ().sw_if_index;
229 struct Delete_loopback_cb
231 Delete_loopback_cb () : called{0}, sw_if_index{0} {};
235 vapi_error_e operator() (Delete_loopback &dl)
237 auto &r = dl.get_response ();
238 ck_assert_int_eq (0, r.get_payload ().retval);
244 template <int num_ifs> struct Sw_interface_dump_cb
246 Sw_interface_dump_cb (std::array<Create_loopback_cb, num_ifs> &cbs)
247 : called{0}, cbs{cbs} {};
249 std::array<Create_loopback_cb, num_ifs> &cbs;
250 vapi_error_e operator() (Sw_interface_dump &d)
256 for (auto &x : d.get_result_set ())
258 auto &p = x.get_payload ();
261 if (p.sw_if_index == y.sw_if_index)
269 ck_assert_int_eq (true, y.seen);
276 START_TEST (test_loopbacks_2)
278 printf ("--- Create/delete loopbacks by getting a callback ---\n");
279 const auto num_ifs = 5;
280 u8 mac_addresses[num_ifs][6];
281 memset (&mac_addresses, 0, sizeof (mac_addresses));
282 for (int i = 0; i < num_ifs; ++i)
284 memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
285 mac_addresses[i][5] = i;
287 std::array<Create_loopback_cb, num_ifs> ccbs;
288 std::array<std::unique_ptr<Create_loopback>, num_ifs> clcs;
289 for (int i = 0; i < num_ifs; ++i)
291 Create_loopback *cl = new Create_loopback (con, std::ref (ccbs[i]));
293 auto &p = cl->get_request ().get_payload ();
294 memcpy (p.mac_address, mac_addresses[i], sizeof (p.mac_address));
295 auto e = cl->execute ();
296 ck_assert_int_eq (VAPI_OK, e);
299 for (int i = 0; i < num_ifs; ++i)
301 ck_assert_int_eq (1, ccbs[i].called);
302 printf ("Created loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
304 mac_addresses[i][0], mac_addresses[i][1], mac_addresses[i][2],
305 mac_addresses[i][3], mac_addresses[i][4], mac_addresses[i][5],
306 ccbs[i].sw_if_index);
309 Sw_interface_dump_cb<num_ifs> swdcb (ccbs);
310 Sw_interface_dump d (con, std::ref (swdcb));
311 auto &p = d.get_request ().get_payload ();
312 p.name_filter_valid = 0;
313 memset (p.name_filter, 0, sizeof (p.name_filter));
314 auto rv = d.execute ();
315 ck_assert_int_eq (VAPI_OK, rv);
316 WAIT_FOR_RESPONSE (d, rv);
317 ck_assert_int_eq (VAPI_OK, rv);
318 ck_assert_int_ne (0, swdcb.called);
319 std::array<Delete_loopback_cb, num_ifs> dcbs;
320 std::array<std::unique_ptr<Delete_loopback>, num_ifs> dlcs;
321 for (int i = 0; i < num_ifs; ++i)
323 Delete_loopback *dl = new Delete_loopback (con, std::ref (dcbs[i]));
325 auto &p = dl->get_request ().get_payload ();
326 p.sw_if_index = ccbs[i].sw_if_index;
327 dcbs[i].sw_if_index = ccbs[i].sw_if_index;
328 auto e = dl->execute ();
329 ck_assert_int_eq (VAPI_OK, e);
334 ck_assert_int_eq (true, x.called);
335 printf ("Deleted loopback with sw_if_index %u\n", x.sw_if_index);
339 Sw_interface_dump d (con);
340 auto &p = d.get_request ().get_payload ();
341 p.name_filter_valid = 0;
342 memset (p.name_filter, 0, sizeof (p.name_filter));
343 auto rv = d.execute ();
344 ck_assert_int_eq (VAPI_OK, rv);
345 WAIT_FOR_RESPONSE (d, rv);
346 ck_assert_int_eq (VAPI_OK, rv);
347 auto &rs = d.get_result_set ();
350 auto &p = r.get_payload ();
351 for (int i = 0; i < num_ifs; ++i)
353 ck_assert_int_ne (ccbs[i].sw_if_index, p.sw_if_index);
361 START_TEST (test_stats_1)
363 printf ("--- Receive single stats by waiting for response ---\n");
365 auto &payload = ws.get_request ().get_payload ();
366 payload.enable_disable = 1;
367 payload.pid = getpid ();
368 auto rv = ws.execute ();
369 ck_assert_int_eq (VAPI_OK, rv);
370 Event_registration<Vnet_interface_simple_counters> sc (con);
371 WAIT_FOR_RESPONSE (sc, rv);
372 ck_assert_int_eq (VAPI_OK, rv);
373 auto &rs = sc.get_result_set ();
377 printf ("simple counters: first_sw_if_index=%u\n",
378 r.get_payload ().first_sw_if_index);
381 ck_assert_int_ne (0, count);
386 struct Vnet_interface_simple_counters_cb
388 Vnet_interface_simple_counters_cb () : called{0} {};
391 operator() (Event_registration<Vnet_interface_simple_counters> &e)
394 auto &rs = e.get_result_set ();
398 printf ("simple counters: first_sw_if_index=%u\n",
399 r.get_payload ().first_sw_if_index);
402 ck_assert_int_ne (0, count);
407 START_TEST (test_stats_2)
409 printf ("--- Receive single stats by getting a callback ---\n");
411 auto &payload = ws.get_request ().get_payload ();
412 payload.enable_disable = 1;
413 payload.pid = getpid ();
414 auto rv = ws.execute ();
415 ck_assert_int_eq (VAPI_OK, rv);
416 Vnet_interface_simple_counters_cb cb;
417 Event_registration<Vnet_interface_simple_counters> sc (con, std::ref (cb));
418 WAIT_FOR_RESPONSE (sc, rv);
419 ck_assert_int_eq (VAPI_OK, rv);
420 ck_assert_int_ne (0, cb.called);
425 struct Vnet_interface_simple_counters_2_cb
427 Vnet_interface_simple_counters_2_cb () : called{0}, total{0} {};
431 operator() (Event_registration<Vnet_interface_simple_counters> &e)
434 auto &rs = e.get_result_set ();
438 printf ("simple counters: first_sw_if_index=%u\n",
439 r.get_payload ().first_sw_if_index);
442 rs.free_all_responses ();
443 ck_assert_int_ne (0, count);
449 START_TEST (test_stats_3)
452 "--- Receive single stats by getting a callback - clear results ---\n");
454 auto &payload = ws.get_request ().get_payload ();
455 payload.enable_disable = 1;
456 payload.pid = getpid ();
457 auto rv = ws.execute ();
458 ck_assert_int_eq (VAPI_OK, rv);
459 Vnet_interface_simple_counters_2_cb cb;
460 Event_registration<Vnet_interface_simple_counters> sc (con, std::ref (cb));
461 for (int i = 0; i < 5; ++i)
463 WAIT_FOR_RESPONSE (sc, rv);
465 ck_assert_int_eq (VAPI_OK, rv);
466 ck_assert_int_eq (5, cb.called);
467 ck_assert_int_eq (5, cb.total);
472 START_TEST (test_stats_4)
474 printf ("--- Receive multiple stats by waiting for response ---\n");
476 auto &payload = ws.get_request ().get_payload ();
477 payload.enable_disable = 1;
478 payload.pid = getpid ();
479 auto rv = ws.execute ();
480 ck_assert_int_eq (VAPI_OK, rv);
481 Event_registration<Vnet_interface_simple_counters> sc (con);
482 Event_registration<Vnet_interface_combined_counters> cc (con);
483 WAIT_FOR_RESPONSE (sc, rv);
484 ck_assert_int_eq (VAPI_OK, rv);
485 WAIT_FOR_RESPONSE (cc, rv);
486 ck_assert_int_eq (VAPI_OK, rv);
488 for (auto &r : sc.get_result_set ())
490 printf ("simple counters: first_sw_if_index=%u\n",
491 r.get_payload ().first_sw_if_index);
494 ck_assert_int_ne (0, count);
496 for (auto &r : cc.get_result_set ())
498 printf ("combined counters: first_sw_if_index=%u\n",
499 r.get_payload ().first_sw_if_index);
502 ck_assert_int_ne (0, count);
507 START_TEST (test_unsupported)
509 printf ("--- Unsupported messages ---\n");
513 Test_fake_msg fake (con);
515 catch (const Msg_not_available_exception &)
518 printf ("Constructing unsupported msg not possible - test pass.\n");
520 ck_assert_int_eq (true, thrown);
524 Test_fake_dump fake (con);
526 catch (const Msg_not_available_exception &)
529 printf ("Constructing unsupported dump not possible - test pass.\n");
531 ck_assert_int_eq (true, thrown);
535 Event_registration<Test_fake_details> fake (con);
537 catch (const Msg_not_available_exception &)
540 printf ("Constructing unsupported event registration not possible - "
543 ck_assert_int_eq (true, thrown);
548 Suite *test_suite (void)
550 Suite *s = suite_create ("VAPI test");
552 TCase *tc_cpp_api = tcase_create ("C++ API");
553 tcase_set_timeout (tc_cpp_api, 25);
554 tcase_add_checked_fixture (tc_cpp_api, setup, teardown);
555 tcase_add_test (tc_cpp_api, test_show_version_1);
556 tcase_add_test (tc_cpp_api, test_show_version_2);
557 tcase_add_test (tc_cpp_api, test_loopbacks_1);
558 tcase_add_test (tc_cpp_api, test_loopbacks_2);
559 tcase_add_test (tc_cpp_api, test_stats_1);
560 tcase_add_test (tc_cpp_api, test_stats_2);
561 tcase_add_test (tc_cpp_api, test_stats_3);
562 tcase_add_test (tc_cpp_api, test_stats_4);
563 tcase_add_test (tc_cpp_api, test_unsupported);
564 suite_add_tcase (s, tc_cpp_api);
569 int main (int argc, char *argv[])
573 printf ("Invalid argc==`%d'\n", argc);
577 api_prefix = argv[2];
578 printf ("App name: `%s', API prefix: `%s'\n", app_name, api_prefix);
585 sr = srunner_create (s);
587 srunner_run_all (sr, CK_NORMAL);
588 number_failed = srunner_ntests_failed (sr);
590 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
594 * fd.io coding-style-patch-verification: ON
597 * eval: (c-set-style "gnu")