blob: 14c35d5beddd60f1ecd477502a4e101e5bfda0fa [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
40using namespace vapi;
41
42void verify_show_version_reply (const Show_version_reply &r)
43{
44 auto &p = r.get_payload ();
45 printf ("show_version_reply: program: `%s', version: `%s', build directory: "
46 "`%s', build date: `%s'\n",
47 p.program, p.version, p.build_directory, p.build_date);
48 ck_assert_str_eq ("vpe", (char *)p.program);
49}
50
51Connection con;
52
53void setup (void)
54{
55 vapi_error_e rv = con.connect (
56 app_name, api_prefix, max_outstanding_requests, response_queue_size);
57 ck_assert_int_eq (VAPI_OK, rv);
58}
59
60void teardown (void)
61{
62 con.disconnect ();
63}
64
65START_TEST (test_show_version_1)
66{
67 printf ("--- Show version by reading response associated to request ---\n");
68 Show_version sv (con);
69 vapi_error_e rv = sv.execute ();
70 ck_assert_int_eq (VAPI_OK, rv);
71 rv = con.wait_for_response (sv);
72 ck_assert_int_eq (VAPI_OK, rv);
73 auto &r = sv.get_response ();
74 verify_show_version_reply (r);
75}
76
77END_TEST;
78
79struct Show_version_cb
80{
81 Show_version_cb () : called{0} {};
82 int called;
83 vapi_error_e operator() (Show_version &sv)
84 {
85 auto &r = sv.get_response ();
86 verify_show_version_reply (r);
87 ++called;
88 return VAPI_OK;
89 }
90};
91
92START_TEST (test_show_version_2)
93{
94 printf ("--- Show version by getting a callback ---\n");
95 Show_version_cb cb;
96 Show_version sv (con, std::ref (cb));
97 vapi_error_e rv = sv.execute ();
98 ck_assert_int_eq (VAPI_OK, rv);
99 con.dispatch (sv);
100 ck_assert_int_eq (1, cb.called);
101}
102
103END_TEST;
104
105START_TEST (test_loopbacks_1)
106{
107 printf ("--- Create/delete loopbacks by waiting for response ---\n");
108 const auto num_ifs = 5;
109 u8 mac_addresses[num_ifs][6];
110 memset (&mac_addresses, 0, sizeof (mac_addresses));
111 u32 sw_if_indexes[num_ifs];
112 memset (&sw_if_indexes, 0xff, sizeof (sw_if_indexes));
113 for (int i = 0; i < num_ifs; ++i)
114 {
115 memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
116 mac_addresses[i][5] = i;
117 }
118 for (int i = 0; i < num_ifs; ++i)
119 {
120 Create_loopback cl (con);
121 auto &p = cl.get_request ().get_payload ();
122 memcpy (p.mac_address, mac_addresses[i], sizeof (p.mac_address));
123 auto e = cl.execute ();
124 ck_assert_int_eq (VAPI_OK, e);
125 vapi_error_e rv = con.wait_for_response (cl);
126 ck_assert_int_eq (VAPI_OK, rv);
127 auto &rp = cl.get_response ().get_payload ();
128 ck_assert_int_eq (0, rp.retval);
129 sw_if_indexes[i] = rp.sw_if_index;
130 }
131 for (int i = 0; i < num_ifs; ++i)
132 {
133 printf ("Created loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
134 "sw_if_index %u\n",
135 mac_addresses[i][0], mac_addresses[i][1], mac_addresses[i][2],
136 mac_addresses[i][3], mac_addresses[i][4], mac_addresses[i][5],
137 sw_if_indexes[i]);
138 }
139
140 { // new context
141 bool seen[num_ifs] = {0};
142 Sw_interface_dump d (con);
143 auto &p = d.get_request ().get_payload ();
144 p.name_filter_valid = 0;
145 memset (p.name_filter, 0, sizeof (p.name_filter));
146 auto rv = d.execute ();
147 ck_assert_int_eq (VAPI_OK, rv);
148 rv = con.wait_for_response (d);
149 ck_assert_int_eq (VAPI_OK, rv);
150 auto &rs = d.get_result_set ();
151 for (auto &r : rs)
152 {
153 auto &p = r.get_payload ();
154 for (int i = 0; i < num_ifs; ++i)
155 {
156 if (sw_if_indexes[i] == p.sw_if_index)
157 {
158 ck_assert_int_eq (0, seen[i]);
159 seen[i] = true;
160 }
161 }
162 }
163 for (int i = 0; i < num_ifs; ++i)
164 {
165 ck_assert_int_eq (1, seen[i]);
166 }
167 }
168
169 for (int i = 0; i < num_ifs; ++i)
170 {
171 Delete_loopback dl (con);
172 dl.get_request ().get_payload ().sw_if_index = sw_if_indexes[i];
173 auto rv = dl.execute ();
174 ck_assert_int_eq (VAPI_OK, rv);
175 rv = con.wait_for_response (dl);
176 ck_assert_int_eq (VAPI_OK, rv);
177 auto &response = dl.get_response ();
178 auto rp = response.get_payload ();
179 ck_assert_int_eq (0, rp.retval);
180 printf ("Deleted loopback with sw_if_index %u\n", sw_if_indexes[i]);
181 }
182
183 { // new context
184 Sw_interface_dump d (con);
185 auto &p = d.get_request ().get_payload ();
186 p.name_filter_valid = 0;
187 memset (p.name_filter, 0, sizeof (p.name_filter));
188 auto rv = d.execute ();
189 ck_assert_int_eq (VAPI_OK, rv);
190 rv = con.wait_for_response (d);
191 ck_assert_int_eq (VAPI_OK, rv);
192 auto &rs = d.get_result_set ();
193 for (auto &r : rs)
194 {
195 auto &p = r.get_payload ();
196 for (int i = 0; i < num_ifs; ++i)
197 {
198 ck_assert_int_ne (sw_if_indexes[i], p.sw_if_index);
199 }
200 }
201 }
202}
203
204END_TEST;
205
206struct Create_loopback_cb
207{
208 Create_loopback_cb () : called{0}, sw_if_index{0} {};
209 int called;
210 u32 sw_if_index;
211 bool seen;
212 vapi_error_e operator() (Create_loopback &cl)
213 {
214 auto &r = cl.get_response ();
215 sw_if_index = r.get_payload ().sw_if_index;
216 ++called;
217 return VAPI_OK;
218 }
219};
220
221struct Delete_loopback_cb
222{
223 Delete_loopback_cb () : called{0}, sw_if_index{0} {};
224 int called;
225 u32 sw_if_index;
226 bool seen;
227 vapi_error_e operator() (Delete_loopback &dl)
228 {
229 auto &r = dl.get_response ();
230 ck_assert_int_eq (0, r.get_payload ().retval);
231 ++called;
232 return VAPI_OK;
233 }
234};
235
236template <int num_ifs> struct Sw_interface_dump_cb
237{
238 Sw_interface_dump_cb (std::array<Create_loopback_cb, num_ifs> &cbs)
239 : called{0}, cbs{cbs} {};
240 int called;
241 std::array<Create_loopback_cb, num_ifs> &cbs;
242 vapi_error_e operator() (Sw_interface_dump &d)
243 {
244 for (auto &y : cbs)
245 {
246 y.seen = false;
247 }
248 for (auto &x : d.get_result_set ())
249 {
250 auto &p = x.get_payload ();
251 for (auto &y : cbs)
252 {
253 if (p.sw_if_index == y.sw_if_index)
254 {
255 y.seen = true;
256 }
257 }
258 }
259 for (auto &y : cbs)
260 {
261 ck_assert_int_eq (true, y.seen);
262 }
263 ++called;
264 return VAPI_OK;
265 }
266};
267
268START_TEST (test_loopbacks_2)
269{
270 printf ("--- Create/delete loopbacks by getting a callback ---\n");
271 const auto num_ifs = 5;
272 u8 mac_addresses[num_ifs][6];
273 memset (&mac_addresses, 0, sizeof (mac_addresses));
274 for (int i = 0; i < num_ifs; ++i)
275 {
276 memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
277 mac_addresses[i][5] = i;
278 }
279 std::array<Create_loopback_cb, num_ifs> ccbs;
280 std::array<std::unique_ptr<Create_loopback>, num_ifs> clcs;
281 for (int i = 0; i < num_ifs; ++i)
282 {
283 Create_loopback *cl = new Create_loopback (con, std::ref (ccbs[i]));
284 clcs[i].reset (cl);
285 auto &p = cl->get_request ().get_payload ();
286 memcpy (p.mac_address, mac_addresses[i], sizeof (p.mac_address));
287 auto e = cl->execute ();
288 ck_assert_int_eq (VAPI_OK, e);
289 }
290 con.dispatch ();
291 for (int i = 0; i < num_ifs; ++i)
292 {
293 ck_assert_int_eq (1, ccbs[i].called);
294 printf ("Created loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
295 "sw_if_index %u\n",
296 mac_addresses[i][0], mac_addresses[i][1], mac_addresses[i][2],
297 mac_addresses[i][3], mac_addresses[i][4], mac_addresses[i][5],
298 ccbs[i].sw_if_index);
299 }
300
301 Sw_interface_dump_cb<num_ifs> swdcb (ccbs);
302 Sw_interface_dump d (con, std::ref (swdcb));
303 auto &p = d.get_request ().get_payload ();
304 p.name_filter_valid = 0;
305 memset (p.name_filter, 0, sizeof (p.name_filter));
306 auto rv = d.execute ();
307 ck_assert_int_eq (VAPI_OK, rv);
308 rv = con.wait_for_response (d);
309 ck_assert_int_eq (VAPI_OK, rv);
310 ck_assert_int_ne (0, swdcb.called);
311 std::array<Delete_loopback_cb, num_ifs> dcbs;
312 std::array<std::unique_ptr<Delete_loopback>, num_ifs> dlcs;
313 for (int i = 0; i < num_ifs; ++i)
314 {
315 Delete_loopback *dl = new Delete_loopback (con, std::ref (dcbs[i]));
316 dlcs[i].reset (dl);
317 auto &p = dl->get_request ().get_payload ();
318 p.sw_if_index = ccbs[i].sw_if_index;
319 dcbs[i].sw_if_index = ccbs[i].sw_if_index;
320 auto e = dl->execute ();
321 ck_assert_int_eq (VAPI_OK, e);
322 }
323 con.dispatch ();
324 for (auto &x : dcbs)
325 {
326 ck_assert_int_eq (true, x.called);
327 printf ("Deleted loopback with sw_if_index %u\n", x.sw_if_index);
328 }
329
330 { // new context
331 Sw_interface_dump d (con);
332 auto &p = d.get_request ().get_payload ();
333 p.name_filter_valid = 0;
334 memset (p.name_filter, 0, sizeof (p.name_filter));
335 auto rv = d.execute ();
336 ck_assert_int_eq (VAPI_OK, rv);
337 rv = con.wait_for_response (d);
338 ck_assert_int_eq (VAPI_OK, rv);
339 auto &rs = d.get_result_set ();
340 for (auto &r : rs)
341 {
342 auto &p = r.get_payload ();
343 for (int i = 0; i < num_ifs; ++i)
344 {
345 ck_assert_int_ne (ccbs[i].sw_if_index, p.sw_if_index);
346 }
347 }
348 }
349}
350
351END_TEST;
352
353START_TEST (test_stats_1)
354{
355 printf ("--- Receive single stats by waiting for response ---\n");
356 Want_stats ws (con);
357 auto &payload = ws.get_request ().get_payload ();
358 payload.enable_disable = 1;
359 payload.pid = getpid ();
360 auto rv = ws.execute ();
361 ck_assert_int_eq (VAPI_OK, rv);
362 Event_registration<Vnet_interface_simple_counters> sc (con);
363 rv = con.wait_for_response (sc);
364 ck_assert_int_eq (VAPI_OK, rv);
365 auto &rs = sc.get_result_set ();
366 int count = 0;
367 for (auto &r : rs)
368 {
369 printf ("simple counters: first_sw_if_index=%u\n",
370 r.get_payload ().first_sw_if_index);
371 ++count;
372 }
373 ck_assert_int_ne (0, count);
374}
375
376END_TEST;
377
378struct Vnet_interface_simple_counters_cb
379{
380 Vnet_interface_simple_counters_cb () : called{0} {};
381 int called;
382 vapi_error_e
383 operator() (Event_registration<Vnet_interface_simple_counters> &e)
384 {
385 ++called;
386 auto &rs = e.get_result_set ();
387 int count = 0;
388 for (auto &r : rs)
389 {
390 printf ("simple counters: first_sw_if_index=%u\n",
391 r.get_payload ().first_sw_if_index);
392 ++count;
393 }
394 ck_assert_int_ne (0, count);
395 return VAPI_OK;
396 }
397};
398
399START_TEST (test_stats_2)
400{
401 printf ("--- Receive single stats by getting a callback ---\n");
402 Want_stats ws (con);
403 auto &payload = ws.get_request ().get_payload ();
404 payload.enable_disable = 1;
405 payload.pid = getpid ();
406 auto rv = ws.execute ();
407 ck_assert_int_eq (VAPI_OK, rv);
408 Vnet_interface_simple_counters_cb cb;
409 Event_registration<Vnet_interface_simple_counters> sc (con, std::ref (cb));
410 rv = con.wait_for_response (sc);
411 ck_assert_int_eq (VAPI_OK, rv);
412 ck_assert_int_ne (0, cb.called);
413}
414
415END_TEST;
416
417struct Vnet_interface_simple_counters_2_cb
418{
419 Vnet_interface_simple_counters_2_cb () : called{0}, total{0} {};
420 int called;
421 int total;
422 vapi_error_e
423 operator() (Event_registration<Vnet_interface_simple_counters> &e)
424 {
425 ++called;
426 auto &rs = e.get_result_set ();
427 int count = 0;
428 for (auto &r : rs)
429 {
430 printf ("simple counters: first_sw_if_index=%u\n",
431 r.get_payload ().first_sw_if_index);
432 ++count;
433 }
434 rs.free_all_responses ();
435 ck_assert_int_ne (0, count);
436 total += count;
437 return VAPI_OK;
438 }
439};
440
441START_TEST (test_stats_3)
442{
443 printf (
444 "--- Receive single stats by getting a callback - clear results ---\n");
445 Want_stats ws (con);
446 auto &payload = ws.get_request ().get_payload ();
447 payload.enable_disable = 1;
448 payload.pid = getpid ();
449 auto rv = ws.execute ();
450 ck_assert_int_eq (VAPI_OK, rv);
451 Vnet_interface_simple_counters_2_cb cb;
452 Event_registration<Vnet_interface_simple_counters> sc (con, std::ref (cb));
453 for (int i = 0; i < 5; ++i)
454 {
455 rv = con.wait_for_response (sc);
456 }
457 ck_assert_int_eq (VAPI_OK, rv);
458 ck_assert_int_eq (5, cb.called);
459 ck_assert_int_eq (5, cb.total);
460}
461
462END_TEST;
463
464START_TEST (test_stats_4)
465{
466 printf ("--- Receive multiple stats by waiting for response ---\n");
467 Want_stats ws (con);
468 auto &payload = ws.get_request ().get_payload ();
469 payload.enable_disable = 1;
470 payload.pid = getpid ();
471 auto rv = ws.execute ();
472 ck_assert_int_eq (VAPI_OK, rv);
473 Event_registration<Vnet_interface_simple_counters> sc (con);
474 Event_registration<Vnet_interface_combined_counters> cc (con);
475 rv = con.wait_for_response (sc);
476 ck_assert_int_eq (VAPI_OK, rv);
477 rv = con.wait_for_response (cc);
478 ck_assert_int_eq (VAPI_OK, rv);
479 int count = 0;
480 for (auto &r : sc.get_result_set ())
481 {
482 printf ("simple counters: first_sw_if_index=%u\n",
483 r.get_payload ().first_sw_if_index);
484 ++count;
485 }
486 ck_assert_int_ne (0, count);
487 count = 0;
488 for (auto &r : cc.get_result_set ())
489 {
490 printf ("combined 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}
496
497END_TEST;
498
499START_TEST (test_unsupported)
500{
501 printf ("--- Unsupported messages ---\n");
502 bool thrown = false;
503 try
504 {
505 Test_fake_msg fake (con);
506 }
507 catch (const Msg_not_available_exception &)
508 {
509 thrown = true;
510 printf ("Constructing unsupported msg not possible - test pass.\n");
511 }
512 ck_assert_int_eq (true, thrown);
513 thrown = false;
514 try
515 {
516 Test_fake_dump fake (con);
517 }
518 catch (const Msg_not_available_exception &)
519 {
520 thrown = true;
521 printf ("Constructing unsupported dump not possible - test pass.\n");
522 }
523 ck_assert_int_eq (true, thrown);
524 thrown = false;
525 try
526 {
527 Event_registration<Test_fake_details> fake (con);
528 }
529 catch (const Msg_not_available_exception &)
530 {
531 thrown = true;
532 printf ("Constructing unsupported event registration not possible - "
533 "test pass.\n");
534 }
535 ck_assert_int_eq (true, thrown);
536}
537
538END_TEST;
539
540Suite *test_suite (void)
541{
542 Suite *s = suite_create ("VAPI test");
543
544 TCase *tc_cpp_api = tcase_create ("C++ API");
545 tcase_set_timeout (tc_cpp_api, 25);
546 tcase_add_checked_fixture (tc_cpp_api, setup, teardown);
547 tcase_add_test (tc_cpp_api, test_show_version_1);
548 tcase_add_test (tc_cpp_api, test_show_version_2);
549 tcase_add_test (tc_cpp_api, test_loopbacks_1);
550 tcase_add_test (tc_cpp_api, test_loopbacks_2);
551 tcase_add_test (tc_cpp_api, test_stats_1);
552 tcase_add_test (tc_cpp_api, test_stats_2);
553 tcase_add_test (tc_cpp_api, test_stats_3);
554 tcase_add_test (tc_cpp_api, test_stats_4);
555 tcase_add_test (tc_cpp_api, test_unsupported);
556 suite_add_tcase (s, tc_cpp_api);
557
558 return s;
559}
560
561int main (int argc, char *argv[])
562{
563 if (3 != argc)
564 {
565 printf ("Invalid argc==`%d'\n", argc);
566 return EXIT_FAILURE;
567 }
568 app_name = argv[1];
569 api_prefix = argv[2];
570 printf ("App name: `%s', API prefix: `%s'\n", app_name, api_prefix);
571
572 int number_failed;
573 Suite *s;
574 SRunner *sr;
575
576 s = test_suite ();
577 sr = srunner_create (s);
578
579 srunner_run_all (sr, CK_NORMAL);
580 number_failed = srunner_ntests_failed (sr);
581 srunner_free (sr);
582 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
583}
584
585/*
586 * fd.io coding-style-patch-verification: ON
587 *
588 * Local Variables:
589 * eval: (c-set-style "gnu")
590 * End:
591 */