blob: 25ea9cc7f7b2614373156c39d37ec45083159af3 [file] [log] [blame]
Klement Sekeradc15be22017-06-12 06:49:33 +02001/*
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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 *------------------------------------------------------------------
16 */
17
18#include <memory>
19#include <stdio.h>
20#include <unistd.h>
21#include <assert.h>
22#include <setjmp.h>
23#include <check.h>
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>
29
30DEFINE_VAPI_MSG_IDS_VPE_API_JSON;
31DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON;
32DEFINE_VAPI_MSG_IDS_STATS_API_JSON;
33DEFINE_VAPI_MSG_IDS_FAKE_API_JSON;
34
35static char *app_name = nullptr;
36static char *api_prefix = nullptr;
37static const int max_outstanding_requests = 32;
38static const int response_queue_size = 32;
39
Mohsin Kazmi3fca5672018-01-04 18:57:26 +010040#define WAIT_FOR_RESPONSE(param, ret) \
41 do \
42 { \
43 ret = con.wait_for_response (param); \
44 } \
45 while (ret == VAPI_EAGAIN)
46
Klement Sekeradc15be22017-06-12 06:49:33 +020047using namespace vapi;
48
49void verify_show_version_reply (const Show_version_reply &r)
50{
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);
56}
57
58Connection con;
59
60void setup (void)
61{
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);
65}
66
67void teardown (void)
68{
69 con.disconnect ();
70}
71
72START_TEST (test_show_version_1)
73{
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);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +010078 WAIT_FOR_RESPONSE (sv, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +020079 ck_assert_int_eq (VAPI_OK, rv);
80 auto &r = sv.get_response ();
81 verify_show_version_reply (r);
82}
83
84END_TEST;
85
86struct Show_version_cb
87{
88 Show_version_cb () : called{0} {};
89 int called;
90 vapi_error_e operator() (Show_version &sv)
91 {
92 auto &r = sv.get_response ();
93 verify_show_version_reply (r);
94 ++called;
95 return VAPI_OK;
96 }
97};
98
99START_TEST (test_show_version_2)
100{
101 printf ("--- Show version by getting a callback ---\n");
102 Show_version_cb cb;
103 Show_version sv (con, std::ref (cb));
104 vapi_error_e rv = sv.execute ();
105 ck_assert_int_eq (VAPI_OK, rv);
106 con.dispatch (sv);
107 ck_assert_int_eq (1, cb.called);
108}
109
110END_TEST;
111
112START_TEST (test_loopbacks_1)
113{
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)
121 {
122 memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
123 mac_addresses[i][5] = i;
124 }
125 for (int i = 0; i < num_ifs; ++i)
126 {
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);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100132 vapi_error_e rv;
133 WAIT_FOR_RESPONSE (cl, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200134 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;
138 }
139 for (int i = 0; i < num_ifs; ++i)
140 {
141 printf ("Created loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
142 "sw_if_index %u\n",
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],
145 sw_if_indexes[i]);
146 }
147
148 { // new context
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);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100156 WAIT_FOR_RESPONSE (d, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200157 ck_assert_int_eq (VAPI_OK, rv);
158 auto &rs = d.get_result_set ();
159 for (auto &r : rs)
160 {
161 auto &p = r.get_payload ();
162 for (int i = 0; i < num_ifs; ++i)
163 {
164 if (sw_if_indexes[i] == p.sw_if_index)
165 {
166 ck_assert_int_eq (0, seen[i]);
167 seen[i] = true;
168 }
169 }
170 }
171 for (int i = 0; i < num_ifs; ++i)
172 {
173 ck_assert_int_eq (1, seen[i]);
174 }
175 }
176
177 for (int i = 0; i < num_ifs; ++i)
178 {
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);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100183 WAIT_FOR_RESPONSE (dl, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200184 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]);
189 }
190
191 { // new context
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);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100198 WAIT_FOR_RESPONSE (d, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200199 ck_assert_int_eq (VAPI_OK, rv);
200 auto &rs = d.get_result_set ();
201 for (auto &r : rs)
202 {
203 auto &p = r.get_payload ();
204 for (int i = 0; i < num_ifs; ++i)
205 {
206 ck_assert_int_ne (sw_if_indexes[i], p.sw_if_index);
207 }
208 }
209 }
210}
211
212END_TEST;
213
214struct Create_loopback_cb
215{
216 Create_loopback_cb () : called{0}, sw_if_index{0} {};
217 int called;
218 u32 sw_if_index;
219 bool seen;
220 vapi_error_e operator() (Create_loopback &cl)
221 {
222 auto &r = cl.get_response ();
223 sw_if_index = r.get_payload ().sw_if_index;
224 ++called;
225 return VAPI_OK;
226 }
227};
228
229struct Delete_loopback_cb
230{
231 Delete_loopback_cb () : called{0}, sw_if_index{0} {};
232 int called;
233 u32 sw_if_index;
234 bool seen;
235 vapi_error_e operator() (Delete_loopback &dl)
236 {
237 auto &r = dl.get_response ();
238 ck_assert_int_eq (0, r.get_payload ().retval);
239 ++called;
240 return VAPI_OK;
241 }
242};
243
244template <int num_ifs> struct Sw_interface_dump_cb
245{
246 Sw_interface_dump_cb (std::array<Create_loopback_cb, num_ifs> &cbs)
247 : called{0}, cbs{cbs} {};
248 int called;
249 std::array<Create_loopback_cb, num_ifs> &cbs;
250 vapi_error_e operator() (Sw_interface_dump &d)
251 {
252 for (auto &y : cbs)
253 {
254 y.seen = false;
255 }
256 for (auto &x : d.get_result_set ())
257 {
258 auto &p = x.get_payload ();
259 for (auto &y : cbs)
260 {
261 if (p.sw_if_index == y.sw_if_index)
262 {
263 y.seen = true;
264 }
265 }
266 }
267 for (auto &y : cbs)
268 {
269 ck_assert_int_eq (true, y.seen);
270 }
271 ++called;
272 return VAPI_OK;
273 }
274};
275
276START_TEST (test_loopbacks_2)
277{
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)
283 {
284 memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
285 mac_addresses[i][5] = i;
286 }
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)
290 {
291 Create_loopback *cl = new Create_loopback (con, std::ref (ccbs[i]));
292 clcs[i].reset (cl);
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);
297 }
298 con.dispatch ();
299 for (int i = 0; i < num_ifs; ++i)
300 {
301 ck_assert_int_eq (1, ccbs[i].called);
302 printf ("Created loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
303 "sw_if_index %u\n",
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);
307 }
308
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);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100316 WAIT_FOR_RESPONSE (d, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200317 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)
322 {
323 Delete_loopback *dl = new Delete_loopback (con, std::ref (dcbs[i]));
324 dlcs[i].reset (dl);
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);
330 }
331 con.dispatch ();
332 for (auto &x : dcbs)
333 {
334 ck_assert_int_eq (true, x.called);
335 printf ("Deleted loopback with sw_if_index %u\n", x.sw_if_index);
336 }
337
338 { // new context
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);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100345 WAIT_FOR_RESPONSE (d, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200346 ck_assert_int_eq (VAPI_OK, rv);
347 auto &rs = d.get_result_set ();
348 for (auto &r : rs)
349 {
350 auto &p = r.get_payload ();
351 for (int i = 0; i < num_ifs; ++i)
352 {
353 ck_assert_int_ne (ccbs[i].sw_if_index, p.sw_if_index);
354 }
355 }
356 }
357}
358
359END_TEST;
360
361START_TEST (test_stats_1)
362{
363 printf ("--- Receive single stats by waiting for response ---\n");
364 Want_stats ws (con);
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);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100371 WAIT_FOR_RESPONSE (sc, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200372 ck_assert_int_eq (VAPI_OK, rv);
373 auto &rs = sc.get_result_set ();
374 int count = 0;
375 for (auto &r : rs)
376 {
377 printf ("simple counters: first_sw_if_index=%u\n",
378 r.get_payload ().first_sw_if_index);
379 ++count;
380 }
381 ck_assert_int_ne (0, count);
382}
383
384END_TEST;
385
386struct Vnet_interface_simple_counters_cb
387{
388 Vnet_interface_simple_counters_cb () : called{0} {};
389 int called;
390 vapi_error_e
391 operator() (Event_registration<Vnet_interface_simple_counters> &e)
392 {
393 ++called;
394 auto &rs = e.get_result_set ();
395 int count = 0;
396 for (auto &r : rs)
397 {
398 printf ("simple counters: first_sw_if_index=%u\n",
399 r.get_payload ().first_sw_if_index);
400 ++count;
401 }
402 ck_assert_int_ne (0, count);
403 return VAPI_OK;
404 }
405};
406
407START_TEST (test_stats_2)
408{
409 printf ("--- Receive single stats by getting a callback ---\n");
410 Want_stats ws (con);
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));
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100418 WAIT_FOR_RESPONSE (sc, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200419 ck_assert_int_eq (VAPI_OK, rv);
420 ck_assert_int_ne (0, cb.called);
421}
422
423END_TEST;
424
425struct Vnet_interface_simple_counters_2_cb
426{
427 Vnet_interface_simple_counters_2_cb () : called{0}, total{0} {};
428 int called;
429 int total;
430 vapi_error_e
431 operator() (Event_registration<Vnet_interface_simple_counters> &e)
432 {
433 ++called;
434 auto &rs = e.get_result_set ();
435 int count = 0;
436 for (auto &r : rs)
437 {
438 printf ("simple counters: first_sw_if_index=%u\n",
439 r.get_payload ().first_sw_if_index);
440 ++count;
441 }
442 rs.free_all_responses ();
443 ck_assert_int_ne (0, count);
444 total += count;
445 return VAPI_OK;
446 }
447};
448
449START_TEST (test_stats_3)
450{
451 printf (
452 "--- Receive single stats by getting a callback - clear results ---\n");
453 Want_stats ws (con);
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)
462 {
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100463 WAIT_FOR_RESPONSE (sc, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200464 }
465 ck_assert_int_eq (VAPI_OK, rv);
466 ck_assert_int_eq (5, cb.called);
467 ck_assert_int_eq (5, cb.total);
468}
469
470END_TEST;
471
472START_TEST (test_stats_4)
473{
474 printf ("--- Receive multiple stats by waiting for response ---\n");
475 Want_stats ws (con);
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);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100483 WAIT_FOR_RESPONSE (sc, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200484 ck_assert_int_eq (VAPI_OK, rv);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100485 WAIT_FOR_RESPONSE (cc, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200486 ck_assert_int_eq (VAPI_OK, rv);
487 int count = 0;
488 for (auto &r : sc.get_result_set ())
489 {
490 printf ("simple counters: first_sw_if_index=%u\n",
491 r.get_payload ().first_sw_if_index);
492 ++count;
493 }
494 ck_assert_int_ne (0, count);
495 count = 0;
496 for (auto &r : cc.get_result_set ())
497 {
498 printf ("combined counters: first_sw_if_index=%u\n",
499 r.get_payload ().first_sw_if_index);
500 ++count;
501 }
502 ck_assert_int_ne (0, count);
503}
504
505END_TEST;
506
507START_TEST (test_unsupported)
508{
509 printf ("--- Unsupported messages ---\n");
510 bool thrown = false;
511 try
512 {
513 Test_fake_msg fake (con);
514 }
515 catch (const Msg_not_available_exception &)
516 {
517 thrown = true;
518 printf ("Constructing unsupported msg not possible - test pass.\n");
519 }
520 ck_assert_int_eq (true, thrown);
521 thrown = false;
522 try
523 {
524 Test_fake_dump fake (con);
525 }
526 catch (const Msg_not_available_exception &)
527 {
528 thrown = true;
529 printf ("Constructing unsupported dump not possible - test pass.\n");
530 }
531 ck_assert_int_eq (true, thrown);
532 thrown = false;
533 try
534 {
535 Event_registration<Test_fake_details> fake (con);
536 }
537 catch (const Msg_not_available_exception &)
538 {
539 thrown = true;
540 printf ("Constructing unsupported event registration not possible - "
541 "test pass.\n");
542 }
543 ck_assert_int_eq (true, thrown);
544}
545
546END_TEST;
547
548Suite *test_suite (void)
549{
550 Suite *s = suite_create ("VAPI test");
551
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);
565
566 return s;
567}
568
569int main (int argc, char *argv[])
570{
571 if (3 != argc)
572 {
573 printf ("Invalid argc==`%d'\n", argc);
574 return EXIT_FAILURE;
575 }
576 app_name = argv[1];
577 api_prefix = argv[2];
578 printf ("App name: `%s', API prefix: `%s'\n", app_name, api_prefix);
579
580 int number_failed;
581 Suite *s;
582 SRunner *sr;
583
584 s = test_suite ();
585 sr = srunner_create (s);
586
587 srunner_run_all (sr, CK_NORMAL);
588 number_failed = srunner_ntests_failed (sr);
589 srunner_free (sr);
590 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
591}
592
593/*
594 * fd.io coding-style-patch-verification: ON
595 *
596 * Local Variables:
597 * eval: (c-set-style "gnu")
598 * End:
599 */