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/memclnt.api.vapi.h>
25 #include <vapi/vapi.hpp>
26 #include <vapi/vpe.api.vapi.hpp>
27 #include <vapi/interface.api.vapi.hpp>
28 #include <vapi/mss_clamp.api.vapi.hpp>
29 #include <fake.api.vapi.hpp>
31 DEFINE_VAPI_MSG_IDS_VPE_API_JSON;
32 DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON;
33 DEFINE_VAPI_MSG_IDS_MSS_CLAMP_API_JSON;
34 DEFINE_VAPI_MSG_IDS_FAKE_API_JSON;
36 static char *app_name = nullptr;
37 static char *api_prefix = nullptr;
38 static bool use_uds = false;
39 static const int max_outstanding_requests = 32;
40 static const int response_queue_size = 32;
42 #define WAIT_FOR_RESPONSE(param, ret) \
45 ret = con.wait_for_response (param); \
47 while (ret == VAPI_EAGAIN)
51 void verify_show_version_reply (const Show_version_reply &r)
53 auto &p = r.get_payload ();
54 printf ("show_version_reply: program: `%s', version: `%s', build directory: "
55 "`%s', build date: `%s'\n",
56 p.program, p.version, p.build_directory, p.build_date);
57 ck_assert_str_eq ("vpe", (char *)p.program);
65 con.connect (app_name, api_prefix, max_outstanding_requests,
66 response_queue_size, true, use_uds);
67 ck_assert_int_eq (VAPI_OK, rv);
75 START_TEST (test_show_version_1)
77 printf ("--- Show version by reading response associated to request ---\n");
78 Show_version sv (con);
79 vapi_error_e rv = sv.execute ();
80 ck_assert_int_eq (VAPI_OK, rv);
81 WAIT_FOR_RESPONSE (sv, rv);
82 ck_assert_int_eq (VAPI_OK, rv);
83 auto &r = sv.get_response ();
84 verify_show_version_reply (r);
89 struct Show_version_cb
91 Show_version_cb () : called{0} {};
93 vapi_error_e operator() (Show_version &sv)
95 auto &r = sv.get_response ();
96 verify_show_version_reply (r);
102 START_TEST (test_show_version_2)
104 printf ("--- Show version by getting a callback ---\n");
106 Show_version sv (con, std::ref (cb));
107 vapi_error_e rv = sv.execute ();
108 ck_assert_int_eq (VAPI_OK, rv);
110 ck_assert_int_eq (1, cb.called);
115 START_TEST (test_loopbacks_1)
117 printf ("--- Create/delete loopbacks by waiting for response ---\n");
118 const auto num_ifs = 5;
119 u8 mac_addresses[num_ifs][6];
120 memset (&mac_addresses, 0, sizeof (mac_addresses));
121 u32 sw_if_indexes[num_ifs];
122 memset (&sw_if_indexes, 0xff, sizeof (sw_if_indexes));
123 for (int i = 0; i < num_ifs; ++i)
125 memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
126 mac_addresses[i][5] = i;
128 for (int i = 0; i < num_ifs; ++i)
130 Create_loopback cl (con);
131 auto &p = cl.get_request ().get_payload ();
132 memcpy (p.mac_address, mac_addresses[i], sizeof (p.mac_address));
133 auto e = cl.execute ();
134 ck_assert_int_eq (VAPI_OK, e);
136 WAIT_FOR_RESPONSE (cl, rv);
137 ck_assert_int_eq (VAPI_OK, rv);
138 auto &rp = cl.get_response ().get_payload ();
139 ck_assert_int_eq (0, rp.retval);
140 sw_if_indexes[i] = rp.sw_if_index;
142 for (int i = 0; i < num_ifs; ++i)
144 printf ("Created loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
146 mac_addresses[i][0], mac_addresses[i][1], mac_addresses[i][2],
147 mac_addresses[i][3], mac_addresses[i][4], mac_addresses[i][5],
152 for (int i = 0; i < num_ifs; ++i)
154 Mss_clamp_enable_disable d (con);
155 auto &req = d.get_request ().get_payload ();
156 req.sw_if_index = sw_if_indexes[i];
158 req.ipv4_direction = vapi_enum_mss_clamp_dir::MSS_CLAMP_DIR_RX;
159 auto rv = d.execute ();
160 ck_assert_int_eq (VAPI_OK, rv);
161 WAIT_FOR_RESPONSE (d, rv);
162 ck_assert_int_eq (VAPI_OK, rv);
167 bool seen[num_ifs] = { 0 };
168 Mss_clamp_get d (con);
169 d.get_request ().get_payload ().sw_if_index = ~0;
170 auto rv = d.execute ();
171 ck_assert_int_eq (VAPI_OK, rv);
172 WAIT_FOR_RESPONSE (d, rv);
173 ck_assert_int_eq (VAPI_OK, rv);
174 auto &rs = d.get_result_set ();
177 auto &p = r.get_payload ();
178 ck_assert_int_eq (p.ipv4_mss, 1420);
179 printf ("tcp-clamp: sw_if_idx %u ip4-mss %d dir %d\n", p.sw_if_index,
180 p.ipv4_mss, p.ipv4_direction);
181 for (int i = 0; i < num_ifs; ++i)
183 if (sw_if_indexes[i] == p.sw_if_index)
185 ck_assert_int_eq (0, seen[i]);
190 for (int i = 0; i < num_ifs; ++i)
192 ck_assert_int_eq (1, seen[i]);
197 bool seen[num_ifs] = {0};
198 Sw_interface_dump d (con, 0);
199 auto rv = d.execute ();
200 ck_assert_int_eq (VAPI_OK, rv);
201 WAIT_FOR_RESPONSE (d, rv);
202 ck_assert_int_eq (VAPI_OK, rv);
203 auto &rs = d.get_result_set ();
206 auto &p = r.get_payload ();
207 for (int i = 0; i < num_ifs; ++i)
209 if (sw_if_indexes[i] == p.sw_if_index)
211 ck_assert_int_eq (0, seen[i]);
216 for (int i = 0; i < num_ifs; ++i)
218 ck_assert_int_eq (1, seen[i]);
222 for (int i = 0; i < num_ifs; ++i)
224 Delete_loopback dl (con);
225 dl.get_request ().get_payload ().sw_if_index = sw_if_indexes[i];
226 auto rv = dl.execute ();
227 ck_assert_int_eq (VAPI_OK, rv);
228 WAIT_FOR_RESPONSE (dl, rv);
229 ck_assert_int_eq (VAPI_OK, rv);
230 auto &response = dl.get_response ();
231 auto rp = response.get_payload ();
232 ck_assert_int_eq (0, rp.retval);
233 printf ("Deleted loopback with sw_if_index %u\n", sw_if_indexes[i]);
237 Sw_interface_dump d (con, 0);
238 auto rv = d.execute ();
239 ck_assert_int_eq (VAPI_OK, rv);
240 WAIT_FOR_RESPONSE (d, rv);
241 ck_assert_int_eq (VAPI_OK, rv);
242 auto &rs = d.get_result_set ();
245 auto &p = r.get_payload ();
246 for (int i = 0; i < num_ifs; ++i)
248 ck_assert_int_ne (sw_if_indexes[i], p.sw_if_index);
256 struct Create_loopback_cb
258 Create_loopback_cb () : called{ 0 }, sw_if_index{ 0 }, seen{ false } {};
262 vapi_error_e operator() (Create_loopback &cl)
264 auto &r = cl.get_response ();
265 sw_if_index = r.get_payload ().sw_if_index;
271 struct Delete_loopback_cb
273 Delete_loopback_cb () : called{ 0 }, sw_if_index{ 0 }, seen{ false } {};
277 vapi_error_e operator() (Delete_loopback &dl)
279 auto &r = dl.get_response ();
280 ck_assert_int_eq (0, r.get_payload ().retval);
286 template <int num_ifs> struct Sw_interface_dump_cb
288 Sw_interface_dump_cb (std::array<Create_loopback_cb, num_ifs> &cbs)
289 : called{0}, cbs{cbs} {};
291 std::array<Create_loopback_cb, num_ifs> &cbs;
292 vapi_error_e operator() (Sw_interface_dump &d)
298 for (auto &x : d.get_result_set ())
300 auto &p = x.get_payload ();
303 if (p.sw_if_index == y.sw_if_index)
311 ck_assert_int_eq (true, y.seen);
318 START_TEST (test_loopbacks_2)
320 printf ("--- Create/delete loopbacks by getting a callback ---\n");
321 const auto num_ifs = 5;
322 u8 mac_addresses[num_ifs][6];
323 memset (&mac_addresses, 0, sizeof (mac_addresses));
324 for (int i = 0; i < num_ifs; ++i)
326 memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
327 mac_addresses[i][5] = i;
329 std::array<Create_loopback_cb, num_ifs> ccbs;
330 std::array<std::unique_ptr<Create_loopback>, num_ifs> clcs;
331 for (int i = 0; i < num_ifs; ++i)
333 Create_loopback *cl = new Create_loopback (con, std::ref (ccbs[i]));
335 auto &p = cl->get_request ().get_payload ();
336 memcpy (p.mac_address, mac_addresses[i], sizeof (p.mac_address));
337 auto e = cl->execute ();
338 ck_assert_int_eq (VAPI_OK, e);
341 for (int i = 0; i < num_ifs; ++i)
343 ck_assert_int_eq (1, ccbs[i].called);
344 printf ("Created loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
346 mac_addresses[i][0], mac_addresses[i][1], mac_addresses[i][2],
347 mac_addresses[i][3], mac_addresses[i][4], mac_addresses[i][5],
348 ccbs[i].sw_if_index);
351 Sw_interface_dump_cb<num_ifs> swdcb (ccbs);
352 Sw_interface_dump d (con, 0, std::ref (swdcb));
353 auto rv = d.execute ();
354 ck_assert_int_eq (VAPI_OK, rv);
355 WAIT_FOR_RESPONSE (d, rv);
356 ck_assert_int_eq (VAPI_OK, rv);
357 ck_assert_int_ne (0, swdcb.called);
358 std::array<Delete_loopback_cb, num_ifs> dcbs;
359 std::array<std::unique_ptr<Delete_loopback>, num_ifs> dlcs;
360 for (int i = 0; i < num_ifs; ++i)
362 Delete_loopback *dl = new Delete_loopback (con, std::ref (dcbs[i]));
364 auto &p = dl->get_request ().get_payload ();
365 p.sw_if_index = ccbs[i].sw_if_index;
366 dcbs[i].sw_if_index = ccbs[i].sw_if_index;
367 auto e = dl->execute ();
368 ck_assert_int_eq (VAPI_OK, e);
373 ck_assert_int_eq (true, x.called);
374 printf ("Deleted loopback with sw_if_index %u\n", x.sw_if_index);
378 Sw_interface_dump d (con, 0);
379 auto rv = d.execute ();
380 ck_assert_int_eq (VAPI_OK, rv);
381 WAIT_FOR_RESPONSE (d, rv);
382 ck_assert_int_eq (VAPI_OK, rv);
383 auto &rs = d.get_result_set ();
386 auto &p = r.get_payload ();
387 for (int i = 0; i < num_ifs; ++i)
389 ck_assert_int_ne (ccbs[i].sw_if_index, p.sw_if_index);
397 START_TEST (test_unsupported)
399 printf ("--- Unsupported messages ---\n");
403 Test_fake_msg fake (con);
405 catch (const Msg_not_available_exception &)
408 printf ("Constructing unsupported msg not possible - test pass.\n");
410 ck_assert_int_eq (true, thrown);
414 Test_fake_dump fake (con);
416 catch (const Msg_not_available_exception &)
419 printf ("Constructing unsupported dump not possible - test pass.\n");
421 ck_assert_int_eq (true, thrown);
425 Event_registration<Test_fake_details> fake (con);
427 catch (const Msg_not_available_exception &)
430 printf ("Constructing unsupported event registration not possible - "
433 ck_assert_int_eq (true, thrown);
438 Suite *test_suite (void)
440 Suite *s = suite_create ("VAPI test");
442 TCase *tc_cpp_api = tcase_create ("C++ API");
443 tcase_set_timeout (tc_cpp_api, 25);
444 tcase_add_checked_fixture (tc_cpp_api, setup, teardown);
445 tcase_add_test (tc_cpp_api, test_show_version_1);
446 tcase_add_test (tc_cpp_api, test_show_version_2);
447 tcase_add_test (tc_cpp_api, test_loopbacks_1);
448 tcase_add_test (tc_cpp_api, test_loopbacks_2);
449 tcase_add_test (tc_cpp_api, test_unsupported);
450 suite_add_tcase (s, tc_cpp_api);
455 int main (int argc, char *argv[])
459 printf ("Invalid argc==`%d'\n", argc);
463 api_prefix = argv[2];
464 if (!strcmp (argv[3], "shm"))
466 else if (!strcmp (argv[3], "uds"))
470 printf ("Unrecognised required argument '%s', expected 'uds' or 'shm'.",
474 printf ("App name: `%s', API prefix: `%s', use unix sockets %d\n", app_name,
475 api_prefix, use_uds);
482 sr = srunner_create (s);
484 srunner_run_all (sr, CK_NORMAL);
485 number_failed = srunner_ntests_failed (sr);
487 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
491 * fd.io coding-style-patch-verification: ON
494 * eval: (c-set-style "gnu")