9fbb2a61302a09b22798cb89a0d2fdd2d383e1d3
[vpp.git] / test / ext / vapi_c_test.c
1 /*
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 <stdio.h>
19 #include <endian.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <assert.h>
23 #include <setjmp.h>
24 #include <check.h>
25 #include <vppinfra/string.h>
26 #include <vapi/vapi.h>
27 #include <vapi/vpe.api.vapi.h>
28 #include <vapi/interface.api.vapi.h>
29 #include <vapi/l2.api.vapi.h>
30 #include <vapi/stats.api.vapi.h>
31 #include <fake.api.vapi.h>
32
33 DEFINE_VAPI_MSG_IDS_VPE_API_JSON;
34 DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON;
35 DEFINE_VAPI_MSG_IDS_L2_API_JSON;
36 DEFINE_VAPI_MSG_IDS_STATS_API_JSON;
37 DEFINE_VAPI_MSG_IDS_FAKE_API_JSON;
38
39 static char *app_name = NULL;
40 static char *api_prefix = NULL;
41 static const int max_outstanding_requests = 64;
42 static const int response_queue_size = 32;
43
44 /* centos has ancient check so we hack our way around here
45  * to make it work somehow */
46 #ifndef ck_assert_ptr_eq
47 #define ck_assert_ptr_eq(X,Y) ck_assert_int_eq((long)X, (long)Y)
48 #endif
49
50 #ifndef ck_assert_ptr_ne
51 #define ck_assert_ptr_ne(X,Y) ck_assert_int_ne((long)X, (long)Y)
52 #endif
53
54 START_TEST (test_invalid_values)
55 {
56   vapi_ctx_t ctx;
57   vapi_error_e rv = vapi_ctx_alloc (&ctx);
58   ck_assert_int_eq (VAPI_OK, rv);
59   vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
60   ck_assert_ptr_eq (NULL, sv);
61   rv = vapi_send (ctx, sv);
62   ck_assert_int_eq (VAPI_EINVAL, rv);
63   rv = vapi_connect (ctx, app_name, api_prefix, max_outstanding_requests,
64                      response_queue_size, VAPI_MODE_BLOCKING, true);
65   ck_assert_int_eq (VAPI_OK, rv);
66   rv = vapi_send (ctx, NULL);
67   ck_assert_int_eq (VAPI_EINVAL, rv);
68   rv = vapi_send (NULL, NULL);
69   ck_assert_int_eq (VAPI_EINVAL, rv);
70   rv = vapi_recv (NULL, NULL, NULL, 0, 0);
71   ck_assert_int_eq (VAPI_EINVAL, rv);
72   rv = vapi_recv (ctx, NULL, NULL, 0, 0);
73   ck_assert_int_eq (VAPI_EINVAL, rv);
74   vapi_msg_show_version_reply *reply;
75   rv = vapi_recv (ctx, (void **) &reply, NULL, 0, 0);
76   ck_assert_int_eq (VAPI_EINVAL, rv);
77   rv = vapi_disconnect (ctx);
78   ck_assert_int_eq (VAPI_OK, rv);
79   vapi_ctx_free (ctx);
80 }
81
82 END_TEST;
83
84 START_TEST (test_hton_1)
85 {
86   const u16 _vl_msg_id = 1;
87   vapi_type_msg_header1_t h;
88   h._vl_msg_id = _vl_msg_id;
89   vapi_type_msg_header1_t_hton (&h);
90   ck_assert_int_eq (be16toh (h._vl_msg_id), _vl_msg_id);
91 }
92
93 END_TEST;
94
95 START_TEST (test_hton_2)
96 {
97   const u16 _vl_msg_id = 1;
98   const u32 client_index = 3;
99   vapi_type_msg_header2_t h;
100   h._vl_msg_id = _vl_msg_id;
101   h.client_index = client_index;
102   vapi_type_msg_header2_t_hton (&h);
103   ck_assert_int_eq (be16toh (h._vl_msg_id), _vl_msg_id);
104   ck_assert_int_eq (h.client_index, client_index);
105 }
106
107 END_TEST;
108
109 START_TEST (test_hton_3)
110 {
111   const size_t data_size = 10;
112   vapi_msg_vnet_interface_combined_counters *m =
113     malloc (sizeof (vapi_msg_vnet_interface_combined_counters) +
114             data_size * sizeof (vapi_type_vlib_counter));
115   ck_assert_ptr_ne (NULL, m);
116   vapi_payload_vnet_interface_combined_counters *p = &m->payload;
117   const u16 _vl_msg_id = 1;
118   p->_vl_msg_id = _vl_msg_id;
119   const u32 first_sw_if_index = 2;
120   p->first_sw_if_index = first_sw_if_index;
121   p->count = data_size;
122   const u64 packets = 1234;
123   const u64 bytes = 2345;
124   int i;
125   for (i = 0; i < data_size; ++i)
126     {
127       p->data[i].packets = packets;
128       p->data[i].bytes = bytes;
129     }
130   vapi_msg_vnet_interface_combined_counters_hton (m);
131   ck_assert_int_eq (_vl_msg_id, be16toh (p->_vl_msg_id));
132   ck_assert_int_eq (first_sw_if_index, be32toh (p->first_sw_if_index));
133   ck_assert_int_eq (data_size, be32toh (p->count));
134   for (i = 0; i < data_size; ++i)
135     {
136       ck_assert_int_eq (packets, be64toh (p->data[i].packets));
137       ck_assert_int_eq (bytes, be64toh (p->data[i].bytes));
138     }
139   free (p);
140 }
141
142 END_TEST;
143
144 #define verify_hton_swap(expr, value)           \
145   if (4 == sizeof (expr))                       \
146     {                                           \
147       ck_assert_int_eq (expr, htobe32 (value)); \
148     }                                           \
149   else if (2 == sizeof (expr))                  \
150     {                                           \
151       ck_assert_int_eq (expr, htobe16 (value)); \
152     }                                           \
153   else                                          \
154     {                                           \
155       ck_assert_int_eq (expr, value);           \
156     }
157
158 START_TEST (test_hton_4)
159 {
160   const int vla_count = 3;
161   char x[sizeof (vapi_msg_bridge_domain_details) +
162          vla_count * sizeof (vapi_type_bridge_domain_sw_if)];
163   vapi_msg_bridge_domain_details *d = (void *) x;
164   int cnt = 1;
165   d->header._vl_msg_id = cnt++;
166   d->header.context = cnt++;
167   d->payload.bd_id = cnt++;
168   d->payload.flood = cnt++;
169   d->payload.uu_flood = cnt++;
170   d->payload.forward = cnt++;
171   d->payload.learn = cnt++;
172   d->payload.arp_term = cnt++;
173   d->payload.mac_age = cnt++;
174   d->payload.bvi_sw_if_index = cnt++;
175   d->payload.n_sw_ifs = vla_count;
176   int i;
177   for (i = 0; i < vla_count; ++i)
178     {
179       vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
180       det->context = cnt++;
181       det->sw_if_index = cnt++;
182       det->shg = cnt++;
183     }
184   ck_assert_int_eq (sizeof (x), vapi_calc_bridge_domain_details_msg_size (d));
185   vapi_msg_bridge_domain_details_hton (d);
186   int tmp = 1;
187   verify_hton_swap (d->header._vl_msg_id, tmp);
188   ++tmp;
189   ck_assert_int_eq (d->header.context, tmp);
190   ++tmp;
191   verify_hton_swap (d->payload.bd_id, tmp);
192   ++tmp;
193   verify_hton_swap (d->payload.flood, tmp);
194   ++tmp;
195   verify_hton_swap (d->payload.uu_flood, tmp);
196   ++tmp;
197   verify_hton_swap (d->payload.forward, tmp);
198   ++tmp;
199   verify_hton_swap (d->payload.learn, tmp);
200   ++tmp;
201   verify_hton_swap (d->payload.arp_term, tmp);
202   ++tmp;
203   verify_hton_swap (d->payload.mac_age, tmp);
204   ++tmp;
205   verify_hton_swap (d->payload.bvi_sw_if_index, tmp);
206   ++tmp;
207   ck_assert_int_eq (d->payload.n_sw_ifs, htobe32 (vla_count));
208   for (i = 0; i < vla_count; ++i)
209     {
210       vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
211       verify_hton_swap (det->context, tmp);
212       ++tmp;
213       verify_hton_swap (det->sw_if_index, tmp);
214       ++tmp;
215       verify_hton_swap (det->shg, tmp);
216       ++tmp;
217     }
218   vapi_msg_bridge_domain_details_ntoh (d);
219   tmp = 1;
220   ck_assert_int_eq (d->header._vl_msg_id, tmp);
221   ++tmp;
222   ck_assert_int_eq (d->header.context, tmp);
223   ++tmp;
224   ck_assert_int_eq (d->payload.bd_id, tmp);
225   ++tmp;
226   ck_assert_int_eq (d->payload.flood, tmp);
227   ++tmp;
228   ck_assert_int_eq (d->payload.uu_flood, tmp);
229   ++tmp;
230   ck_assert_int_eq (d->payload.forward, tmp);
231   ++tmp;
232   ck_assert_int_eq (d->payload.learn, tmp);
233   ++tmp;
234   ck_assert_int_eq (d->payload.arp_term, tmp);
235   ++tmp;
236   ck_assert_int_eq (d->payload.mac_age, tmp);
237   ++tmp;
238   ck_assert_int_eq (d->payload.bvi_sw_if_index, tmp);
239   ++tmp;
240   ck_assert_int_eq (d->payload.n_sw_ifs, vla_count);
241   for (i = 0; i < vla_count; ++i)
242     {
243       vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
244       ck_assert_int_eq (det->context, tmp);
245       ++tmp;
246       ck_assert_int_eq (det->sw_if_index, tmp);
247       ++tmp;
248       ck_assert_int_eq (det->shg, tmp);
249       ++tmp;
250     }
251   ck_assert_int_eq (sizeof (x), vapi_calc_bridge_domain_details_msg_size (d));
252 }
253
254 END_TEST;
255
256 START_TEST (test_ntoh_1)
257 {
258   const u16 _vl_msg_id = 1;
259   vapi_type_msg_header1_t h;
260   h._vl_msg_id = _vl_msg_id;
261   vapi_type_msg_header1_t_ntoh (&h);
262   ck_assert_int_eq (htobe16 (h._vl_msg_id), _vl_msg_id);
263 }
264
265 END_TEST;
266
267 START_TEST (test_ntoh_2)
268 {
269   const u16 _vl_msg_id = 1;
270   const u32 client_index = 3;
271   vapi_type_msg_header2_t h;
272   h._vl_msg_id = _vl_msg_id;
273   h.client_index = client_index;
274   vapi_type_msg_header2_t_ntoh (&h);
275   ck_assert_int_eq (htobe16 (h._vl_msg_id), _vl_msg_id);
276   ck_assert_int_eq (h.client_index, client_index);
277 }
278
279 END_TEST;
280
281 START_TEST (test_ntoh_3)
282 {
283   const size_t data_size = 10;
284   vapi_msg_vnet_interface_combined_counters *m =
285     malloc (sizeof (vapi_msg_vnet_interface_combined_counters) +
286             data_size * sizeof (vapi_type_vlib_counter));
287   ck_assert_ptr_ne (NULL, m);
288   vapi_payload_vnet_interface_combined_counters *p = &m->payload;
289   const u16 _vl_msg_id = 1;
290   p->_vl_msg_id = _vl_msg_id;
291   const u32 first_sw_if_index = 2;
292   p->first_sw_if_index = first_sw_if_index;
293   const size_t be_data_size = htobe32 (data_size);
294   p->count = be_data_size;
295   const u64 packets = 1234;
296   const u64 bytes = 2345;
297   int i;
298   for (i = 0; i < data_size; ++i)
299     {
300       p->data[i].packets = packets;
301       p->data[i].bytes = bytes;
302     }
303   vapi_msg_vnet_interface_combined_counters_ntoh (m);
304   ck_assert_int_eq (_vl_msg_id, be16toh (p->_vl_msg_id));
305   ck_assert_int_eq (first_sw_if_index, be32toh (p->first_sw_if_index));
306   ck_assert_int_eq (be_data_size, be32toh (p->count));
307   for (i = 0; i < data_size; ++i)
308     {
309       ck_assert_int_eq (packets, htobe64 (p->data[i].packets));
310       ck_assert_int_eq (bytes, htobe64 (p->data[i].bytes));
311     }
312   free (p);
313 }
314
315 END_TEST;
316
317 #define verify_ntoh_swap(expr, value)           \
318   if (4 == sizeof (expr))                       \
319     {                                           \
320       ck_assert_int_eq (expr, be32toh (value)); \
321     }                                           \
322   else if (2 == sizeof (expr))                  \
323     {                                           \
324       ck_assert_int_eq (expr, be16toh (value)); \
325     }                                           \
326   else                                          \
327     {                                           \
328       ck_assert_int_eq (expr, value);           \
329     }
330
331 START_TEST (test_ntoh_4)
332 {
333   const int vla_count = 3;
334   char x[sizeof (vapi_msg_bridge_domain_details) +
335          vla_count * sizeof (vapi_type_bridge_domain_sw_if)];
336   vapi_msg_bridge_domain_details *d = (void *) x;
337   int cnt = 1;
338   d->header._vl_msg_id = cnt++;
339   d->header.context = cnt++;
340   d->payload.bd_id = cnt++;
341   d->payload.flood = cnt++;
342   d->payload.uu_flood = cnt++;
343   d->payload.forward = cnt++;
344   d->payload.learn = cnt++;
345   d->payload.arp_term = cnt++;
346   d->payload.mac_age = cnt++;
347   d->payload.bvi_sw_if_index = cnt++;
348   d->payload.n_sw_ifs = htobe32 (vla_count);
349   int i;
350   for (i = 0; i < vla_count; ++i)
351     {
352       vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
353       det->context = cnt++;
354       det->sw_if_index = cnt++;
355       det->shg = cnt++;
356     }
357   vapi_msg_bridge_domain_details_ntoh (d);
358   ck_assert_int_eq (sizeof (x), vapi_calc_bridge_domain_details_msg_size (d));
359   int tmp = 1;
360   verify_ntoh_swap (d->header._vl_msg_id, tmp);
361   ++tmp;
362   ck_assert_int_eq (d->header.context, tmp);
363   ++tmp;
364   verify_ntoh_swap (d->payload.bd_id, tmp);
365   ++tmp;
366   verify_ntoh_swap (d->payload.flood, tmp);
367   ++tmp;
368   verify_ntoh_swap (d->payload.uu_flood, tmp);
369   ++tmp;
370   verify_ntoh_swap (d->payload.forward, tmp);
371   ++tmp;
372   verify_ntoh_swap (d->payload.learn, tmp);
373   ++tmp;
374   verify_ntoh_swap (d->payload.arp_term, tmp);
375   ++tmp;
376   verify_ntoh_swap (d->payload.mac_age, tmp);
377   ++tmp;
378   verify_ntoh_swap (d->payload.bvi_sw_if_index, tmp);
379   ++tmp;
380   ck_assert_int_eq (d->payload.n_sw_ifs, vla_count);
381   for (i = 0; i < vla_count; ++i)
382     {
383       vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
384       verify_ntoh_swap (det->context, tmp);
385       ++tmp;
386       verify_ntoh_swap (det->sw_if_index, tmp);
387       ++tmp;
388       verify_ntoh_swap (det->shg, tmp);
389       ++tmp;
390     }
391   vapi_msg_bridge_domain_details_hton (d);
392   tmp = 1;
393   ck_assert_int_eq (d->header._vl_msg_id, tmp);
394   ++tmp;
395   ck_assert_int_eq (d->header.context, tmp);
396   ++tmp;
397   ck_assert_int_eq (d->payload.bd_id, tmp);
398   ++tmp;
399   ck_assert_int_eq (d->payload.flood, tmp);
400   ++tmp;
401   ck_assert_int_eq (d->payload.uu_flood, tmp);
402   ++tmp;
403   ck_assert_int_eq (d->payload.forward, tmp);
404   ++tmp;
405   ck_assert_int_eq (d->payload.learn, tmp);
406   ++tmp;
407   ck_assert_int_eq (d->payload.arp_term, tmp);
408   ++tmp;
409   ck_assert_int_eq (d->payload.mac_age, tmp);
410   ++tmp;
411   ck_assert_int_eq (d->payload.bvi_sw_if_index, tmp);
412   ++tmp;
413   ck_assert_int_eq (d->payload.n_sw_ifs, htobe32 (vla_count));
414   for (i = 0; i < vla_count; ++i)
415     {
416       vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
417       ck_assert_int_eq (det->context, tmp);
418       ++tmp;
419       ck_assert_int_eq (det->sw_if_index, tmp);
420       ++tmp;
421       ck_assert_int_eq (det->shg, tmp);
422       ++tmp;
423     }
424 }
425
426 END_TEST;
427
428 vapi_error_e
429 show_version_cb (vapi_ctx_t ctx, void *caller_ctx,
430                  vapi_error_e rv, bool is_last,
431                  vapi_payload_show_version_reply * p)
432 {
433   ck_assert_int_eq (VAPI_OK, rv);
434   ck_assert_int_eq (true, is_last);
435   ck_assert_str_eq ("vpe", (char *) p->program);
436   printf
437     ("show_version_reply: program: `%s', version: `%s', build directory: "
438      "`%s', build date: `%s'\n", p->program, p->version, p->build_directory,
439      p->build_date);
440   ++*(int *) caller_ctx;
441   return VAPI_OK;
442 }
443
444 typedef struct
445 {
446   int called;
447   int expected_retval;
448   u32 *sw_if_index_storage;
449 } test_create_loopback_ctx_t;
450
451 vapi_error_e
452 loopback_create_cb (vapi_ctx_t ctx, void *caller_ctx,
453                     vapi_error_e rv, bool is_last,
454                     vapi_payload_create_loopback_reply * p)
455 {
456   test_create_loopback_ctx_t *clc = caller_ctx;
457   ck_assert_int_eq (clc->expected_retval, p->retval);
458   *clc->sw_if_index_storage = p->sw_if_index;
459   ++clc->called;
460   return VAPI_OK;
461 }
462
463 typedef struct
464 {
465   int called;
466   int expected_retval;
467   u32 *sw_if_index_storage;
468 } test_delete_loopback_ctx_t;
469
470 vapi_error_e
471 loopback_delete_cb (vapi_ctx_t ctx, void *caller_ctx,
472                     vapi_error_e rv, bool is_last,
473                     vapi_payload_delete_loopback_reply * p)
474 {
475   test_delete_loopback_ctx_t *dlc = caller_ctx;
476   ck_assert_int_eq (dlc->expected_retval, p->retval);
477   ++dlc->called;
478   return VAPI_OK;
479 }
480
481 START_TEST (test_connect)
482 {
483   vapi_ctx_t ctx;
484   vapi_error_e rv = vapi_ctx_alloc (&ctx);
485   ck_assert_int_eq (VAPI_OK, rv);
486   rv = vapi_connect (ctx, app_name, api_prefix, max_outstanding_requests,
487                      response_queue_size, VAPI_MODE_BLOCKING, true);
488   ck_assert_int_eq (VAPI_OK, rv);
489   rv = vapi_disconnect (ctx);
490   ck_assert_int_eq (VAPI_OK, rv);
491   vapi_ctx_free (ctx);
492 }
493
494 END_TEST;
495
496 vapi_ctx_t ctx;
497
498 void
499 setup_blocking (void)
500 {
501   vapi_error_e rv = vapi_ctx_alloc (&ctx);
502   ck_assert_int_eq (VAPI_OK, rv);
503   rv = vapi_connect (ctx, app_name, api_prefix, max_outstanding_requests,
504                      response_queue_size, VAPI_MODE_BLOCKING, true);
505   ck_assert_int_eq (VAPI_OK, rv);
506 }
507
508 void
509 setup_nonblocking (void)
510 {
511   vapi_error_e rv = vapi_ctx_alloc (&ctx);
512   ck_assert_int_eq (VAPI_OK, rv);
513   rv = vapi_connect (ctx, app_name, api_prefix, max_outstanding_requests,
514                      response_queue_size, VAPI_MODE_NONBLOCKING, true);
515   ck_assert_int_eq (VAPI_OK, rv);
516 }
517
518 void
519 teardown (void)
520 {
521   vapi_disconnect (ctx);
522   vapi_ctx_free (ctx);
523 }
524
525 START_TEST (test_show_version_1)
526 {
527   printf ("--- Basic show version message - reply test ---\n");
528   vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
529   ck_assert_ptr_ne (NULL, sv);
530   vapi_msg_show_version_hton (sv);
531   vapi_error_e rv = vapi_send (ctx, sv);
532   ck_assert_int_eq (VAPI_OK, rv);
533   vapi_msg_show_version_reply *resp;
534   size_t size;
535   rv = vapi_recv (ctx, (void *) &resp, &size, 0, 0);
536   ck_assert_int_eq (VAPI_OK, rv);
537   int dummy;
538   show_version_cb (NULL, &dummy, VAPI_OK, true, &resp->payload);
539   vapi_msg_free (ctx, resp);
540 }
541
542 END_TEST;
543
544 START_TEST (test_show_version_2)
545 {
546   int called = 0;
547   printf ("--- Show version via blocking callback API ---\n");
548   const int attempts = response_queue_size * 4;
549   int i = 0;
550   for (i = 0; i < attempts; ++i)
551     {
552       vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
553       ck_assert_ptr_ne (NULL, sv);
554       vapi_error_e rv = vapi_show_version (ctx, sv, show_version_cb, &called);
555       ck_assert_int_eq (VAPI_OK, rv);
556     }
557   ck_assert_int_eq (attempts, called);
558 }
559
560 END_TEST;
561
562 typedef struct
563 {
564   bool last_called;
565   size_t num_ifs;
566   u32 *sw_if_indexes;
567   bool *seen;
568   int called;
569 } sw_interface_dump_ctx;
570
571 vapi_error_e
572 sw_interface_dump_cb (struct vapi_ctx_s *ctx, void *callback_ctx,
573                       vapi_error_e rv, bool is_last,
574                       vapi_payload_sw_interface_details * reply)
575 {
576   sw_interface_dump_ctx *dctx = callback_ctx;
577   ck_assert_int_eq (false, dctx->last_called);
578   if (is_last)
579     {
580       ck_assert (NULL == reply);
581       dctx->last_called = true;
582     }
583   else
584     {
585       ck_assert (NULL != reply);
586       printf ("Interface dump entry: [%u]: %s\n", reply->sw_if_index,
587               reply->interface_name);
588       size_t i = 0;
589       for (i = 0; i < dctx->num_ifs; ++i)
590         {
591           if (dctx->sw_if_indexes[i] == reply->sw_if_index)
592             {
593               ck_assert_int_eq (false, dctx->seen[i]);
594               dctx->seen[i] = true;
595             }
596         }
597     }
598   ++dctx->called;
599   return VAPI_OK;
600 }
601
602 START_TEST (test_loopbacks_1)
603 {
604   printf ("--- Create/delete loopbacks using blocking API ---\n");
605   const size_t num_ifs = 5;
606   u8 mac_addresses[num_ifs][6];
607   clib_memset (&mac_addresses, 0, sizeof (mac_addresses));
608   u32 sw_if_indexes[num_ifs];
609   clib_memset (&sw_if_indexes, 0xff, sizeof (sw_if_indexes));
610   test_create_loopback_ctx_t clcs[num_ifs];
611   clib_memset (&clcs, 0, sizeof (clcs));
612   test_delete_loopback_ctx_t dlcs[num_ifs];
613   clib_memset (&dlcs, 0, sizeof (dlcs));
614   int i;
615   for (i = 0; i < num_ifs; ++i)
616     {
617       memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
618       mac_addresses[i][5] = i;
619       clcs[i].sw_if_index_storage = &sw_if_indexes[i];
620     }
621   for (i = 0; i < num_ifs; ++i)
622     {
623       vapi_msg_create_loopback *cl = vapi_alloc_create_loopback (ctx);
624       memcpy (cl->payload.mac_address, mac_addresses[i],
625               sizeof (cl->payload.mac_address));
626       vapi_error_e rv =
627         vapi_create_loopback (ctx, cl, loopback_create_cb, &clcs[i]);
628       ck_assert_int_eq (VAPI_OK, rv);
629     }
630   for (i = 0; i < num_ifs; ++i)
631     {
632       ck_assert_int_eq (1, clcs[i].called);
633       printf ("Created loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
634               "sw_if_index %u\n",
635               mac_addresses[i][0], mac_addresses[i][1], mac_addresses[i][2],
636               mac_addresses[i][3], mac_addresses[i][4], mac_addresses[i][5],
637               sw_if_indexes[i]);
638     }
639   bool seen[num_ifs];
640   sw_interface_dump_ctx dctx = { false, num_ifs, sw_if_indexes, seen, 0 };
641   vapi_msg_sw_interface_dump *dump;
642   vapi_error_e rv;
643   const int attempts = response_queue_size * 4;
644   for (i = 0; i < attempts; ++i)
645     {
646       dctx.last_called = false;
647       clib_memset (&seen, 0, sizeof (seen));
648       dump = vapi_alloc_sw_interface_dump (ctx);
649       dump->payload.name_filter_valid = 0;
650       clib_memset (dump->payload.name_filter, 0,
651                    sizeof (dump->payload.name_filter));
652       while (VAPI_EAGAIN ==
653              (rv =
654               vapi_sw_interface_dump (ctx, dump, sw_interface_dump_cb,
655                                       &dctx)))
656         ;
657       ck_assert_int_eq (true, dctx.last_called);
658       int j = 0;
659       for (j = 0; j < num_ifs; ++j)
660         {
661           ck_assert_int_eq (true, seen[j]);
662         }
663     }
664   clib_memset (&seen, 0, sizeof (seen));
665   for (i = 0; i < num_ifs; ++i)
666     {
667       vapi_msg_delete_loopback *dl = vapi_alloc_delete_loopback (ctx);
668       dl->payload.sw_if_index = sw_if_indexes[i];
669       vapi_error_e rv =
670         vapi_delete_loopback (ctx, dl, loopback_delete_cb, &dlcs[i]);
671       ck_assert_int_eq (VAPI_OK, rv);
672     }
673   for (i = 0; i < num_ifs; ++i)
674     {
675       ck_assert_int_eq (1, dlcs[i].called);
676       printf ("Deleted loopback with sw_if_index %u\n", sw_if_indexes[i]);
677     }
678   dctx.last_called = false;
679   clib_memset (&seen, 0, sizeof (seen));
680   dump = vapi_alloc_sw_interface_dump (ctx);
681   dump->payload.name_filter_valid = 0;
682   clib_memset (dump->payload.name_filter, 0,
683                sizeof (dump->payload.name_filter));
684   while (VAPI_EAGAIN ==
685          (rv =
686           vapi_sw_interface_dump (ctx, dump, sw_interface_dump_cb, &dctx)))
687     ;
688   ck_assert_int_eq (true, dctx.last_called);
689   for (i = 0; i < num_ifs; ++i)
690     {
691       ck_assert_int_eq (false, seen[i]);
692     }
693 }
694
695 END_TEST;
696
697 START_TEST (test_show_version_3)
698 {
699   printf ("--- Show version via async callback ---\n");
700   int called = 0;
701   vapi_error_e rv;
702   vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
703   ck_assert_ptr_ne (NULL, sv);
704   while (VAPI_EAGAIN ==
705          (rv = vapi_show_version (ctx, sv, show_version_cb, &called)))
706     ;
707   ck_assert_int_eq (VAPI_OK, rv);
708   ck_assert_int_eq (0, called);
709   rv = vapi_dispatch (ctx);
710   ck_assert_int_eq (VAPI_OK, rv);
711   ck_assert_int_eq (1, called);
712   called = 0;
713   rv = vapi_dispatch (ctx);
714   ck_assert_int_eq (VAPI_OK, rv);
715   ck_assert_int_eq (0, called);
716 }
717
718 END_TEST;
719
720 START_TEST (test_show_version_4)
721 {
722   printf ("--- Show version via async callback - multiple messages ---\n");
723   vapi_error_e rv;
724   const size_t num_req = 5;
725   int contexts[num_req];
726   clib_memset (contexts, 0, sizeof (contexts));
727   int i;
728   for (i = 0; i < num_req; ++i)
729     {
730       vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
731       ck_assert_ptr_ne (NULL, sv);
732       while (VAPI_EAGAIN ==
733              (rv =
734               vapi_show_version (ctx, sv, show_version_cb, &contexts[i])))
735         ;
736       ck_assert_int_eq (VAPI_OK, rv);
737       int j;
738       for (j = 0; j < num_req; ++j)
739         {
740           ck_assert_int_eq (0, contexts[j]);
741         }
742     }
743   rv = vapi_dispatch (ctx);
744   ck_assert_int_eq (VAPI_OK, rv);
745   for (i = 0; i < num_req; ++i)
746     {
747       ck_assert_int_eq (1, contexts[i]);
748     }
749   clib_memset (contexts, 0, sizeof (contexts));
750   rv = vapi_dispatch (ctx);
751   ck_assert_int_eq (VAPI_OK, rv);
752   for (i = 0; i < num_req; ++i)
753     {
754       ck_assert_int_eq (0, contexts[i]);
755     }
756 }
757
758 END_TEST;
759
760 START_TEST (test_loopbacks_2)
761 {
762   printf ("--- Create/delete loopbacks using non-blocking API ---\n");
763   vapi_error_e rv;
764   const size_t num_ifs = 5;
765   u8 mac_addresses[num_ifs][6];
766   clib_memset (&mac_addresses, 0, sizeof (mac_addresses));
767   u32 sw_if_indexes[num_ifs];
768   clib_memset (&sw_if_indexes, 0xff, sizeof (sw_if_indexes));
769   test_create_loopback_ctx_t clcs[num_ifs];
770   clib_memset (&clcs, 0, sizeof (clcs));
771   test_delete_loopback_ctx_t dlcs[num_ifs];
772   clib_memset (&dlcs, 0, sizeof (dlcs));
773   int i;
774   for (i = 0; i < num_ifs; ++i)
775     {
776       memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
777       mac_addresses[i][5] = i;
778       clcs[i].sw_if_index_storage = &sw_if_indexes[i];
779     }
780   for (i = 0; i < num_ifs; ++i)
781     {
782       vapi_msg_create_loopback *cl = vapi_alloc_create_loopback (ctx);
783       memcpy (cl->payload.mac_address, mac_addresses[i],
784               sizeof (cl->payload.mac_address));
785       while (VAPI_EAGAIN ==
786              (rv =
787               vapi_create_loopback (ctx, cl, loopback_create_cb, &clcs[i])))
788         ;
789       ck_assert_int_eq (VAPI_OK, rv);
790     }
791   rv = vapi_dispatch (ctx);
792   ck_assert_int_eq (VAPI_OK, rv);
793   for (i = 0; i < num_ifs; ++i)
794     {
795       ck_assert_int_eq (1, clcs[i].called);
796       printf ("Loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
797               "sw_if_index %u\n",
798               mac_addresses[i][0], mac_addresses[i][1], mac_addresses[i][2],
799               mac_addresses[i][3], mac_addresses[i][4], mac_addresses[i][5],
800               sw_if_indexes[i]);
801     }
802   bool seen[num_ifs];
803   clib_memset (&seen, 0, sizeof (seen));
804   sw_interface_dump_ctx dctx = { false, num_ifs, sw_if_indexes, seen, 0 };
805   vapi_msg_sw_interface_dump *dump = vapi_alloc_sw_interface_dump (ctx);
806   dump->payload.name_filter_valid = 0;
807   clib_memset (dump->payload.name_filter, 0,
808                sizeof (dump->payload.name_filter));
809   while (VAPI_EAGAIN ==
810          (rv =
811           vapi_sw_interface_dump (ctx, dump, sw_interface_dump_cb, &dctx)))
812     ;
813   for (i = 0; i < num_ifs; ++i)
814     {
815       ck_assert_int_eq (false, seen[i]);
816     }
817   clib_memset (&seen, 0, sizeof (seen));
818   ck_assert_int_eq (false, dctx.last_called);
819   rv = vapi_dispatch (ctx);
820   ck_assert_int_eq (VAPI_OK, rv);
821   for (i = 0; i < num_ifs; ++i)
822     {
823       ck_assert_int_eq (true, seen[i]);
824     }
825   clib_memset (&seen, 0, sizeof (seen));
826   ck_assert_int_eq (true, dctx.last_called);
827   for (i = 0; i < num_ifs; ++i)
828     {
829       vapi_msg_delete_loopback *dl = vapi_alloc_delete_loopback (ctx);
830       dl->payload.sw_if_index = sw_if_indexes[i];
831       while (VAPI_EAGAIN ==
832              (rv =
833               vapi_delete_loopback (ctx, dl, loopback_delete_cb, &dlcs[i])))
834         ;
835       ck_assert_int_eq (VAPI_OK, rv);
836     }
837   rv = vapi_dispatch (ctx);
838   ck_assert_int_eq (VAPI_OK, rv);
839   for (i = 0; i < num_ifs; ++i)
840     {
841       ck_assert_int_eq (1, dlcs[i].called);
842       printf ("Deleted loopback with sw_if_index %u\n", sw_if_indexes[i]);
843     }
844   clib_memset (&seen, 0, sizeof (seen));
845   dctx.last_called = false;
846   dump = vapi_alloc_sw_interface_dump (ctx);
847   dump->payload.name_filter_valid = 0;
848   clib_memset (dump->payload.name_filter, 0,
849                sizeof (dump->payload.name_filter));
850   while (VAPI_EAGAIN ==
851          (rv =
852           vapi_sw_interface_dump (ctx, dump, sw_interface_dump_cb, &dctx)))
853     ;
854   rv = vapi_dispatch (ctx);
855   ck_assert_int_eq (VAPI_OK, rv);
856   for (i = 0; i < num_ifs; ++i)
857     {
858       ck_assert_int_eq (false, seen[i]);
859     }
860   clib_memset (&seen, 0, sizeof (seen));
861   ck_assert_int_eq (true, dctx.last_called);
862 }
863
864 END_TEST;
865
866 vapi_error_e
867 interface_simple_stats_cb (vapi_ctx_t ctx, void *callback_ctx,
868                            vapi_error_e rv, bool is_last,
869                            vapi_payload_want_interface_simple_stats_reply *
870                            payload)
871 {
872   return VAPI_OK;
873 }
874
875 vapi_error_e
876 simple_counters_cb (vapi_ctx_t ctx, void *callback_ctx,
877                     vapi_payload_vnet_interface_simple_counters * payload)
878 {
879   int *called = callback_ctx;
880   ++*called;
881   printf ("simple counters: first_sw_if_index=%u\n",
882           payload->first_sw_if_index);
883   return VAPI_OK;
884 }
885
886 START_TEST (test_stats_1)
887 {
888   printf ("--- Receive stats using generic blocking API ---\n");
889   vapi_msg_want_interface_simple_stats *ws =
890     vapi_alloc_want_interface_simple_stats (ctx);
891   ws->payload.enable_disable = 1;
892   ws->payload.pid = getpid ();
893   vapi_error_e rv;
894   rv = vapi_want_interface_simple_stats (ctx, ws, interface_simple_stats_cb,
895                                          NULL);
896   ck_assert_int_eq (VAPI_OK, rv);
897   int called = 0;
898   vapi_set_event_cb (ctx, vapi_msg_id_vnet_interface_simple_counters,
899                      (vapi_event_cb) simple_counters_cb, &called);
900   rv = vapi_dispatch_one (ctx);
901   ck_assert_int_eq (VAPI_OK, rv);
902   ck_assert_int_eq (1, called);
903 }
904
905 END_TEST;
906
907 START_TEST (test_stats_2)
908 {
909   printf ("--- Receive stats using stat-specific blocking API ---\n");
910   vapi_msg_want_interface_simple_stats *ws =
911     vapi_alloc_want_interface_simple_stats (ctx);
912   ws->payload.enable_disable = 1;
913   ws->payload.pid = getpid ();
914   vapi_error_e rv;
915   rv = vapi_want_interface_simple_stats (ctx, ws, interface_simple_stats_cb,
916                                          NULL);
917   ck_assert_int_eq (VAPI_OK, rv);
918   int called = 0;
919   vapi_set_vapi_msg_vnet_interface_simple_counters_event_cb (ctx,
920                                                              simple_counters_cb,
921                                                              &called);
922   rv = vapi_dispatch_one (ctx);
923   ck_assert_int_eq (VAPI_OK, rv);
924   ck_assert_int_eq (1, called);
925 }
926
927 END_TEST;
928
929 vapi_error_e
930 generic_cb (vapi_ctx_t ctx, void *callback_ctx, vapi_msg_id_t id, void *msg)
931 {
932   int *called = callback_ctx;
933   ck_assert_int_eq (0, *called);
934   ++*called;
935   ck_assert_int_eq (id, vapi_msg_id_show_version_reply);
936   ck_assert_ptr_ne (NULL, msg);
937   vapi_msg_show_version_reply *reply = msg;
938   ck_assert_str_eq ("vpe", (char *) reply->payload.program);
939   return VAPI_OK;
940 }
941
942 START_TEST (test_show_version_5)
943 {
944   printf ("--- Receive show version using generic callback - nonblocking "
945           "API ---\n");
946   vapi_error_e rv;
947   vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
948   ck_assert_ptr_ne (NULL, sv);
949   vapi_msg_show_version_hton (sv);
950   while (VAPI_EAGAIN == (rv = vapi_send (ctx, sv)))
951     ;
952   ck_assert_int_eq (VAPI_OK, rv);
953   int called = 0;
954   vapi_set_generic_event_cb (ctx, generic_cb, &called);
955   ck_assert_int_eq (VAPI_OK, rv);
956   rv = vapi_dispatch_one (ctx);
957   ck_assert_int_eq (VAPI_OK, rv);
958   ck_assert_int_eq (1, called);
959   sv = vapi_alloc_show_version (ctx);
960   ck_assert_ptr_ne (NULL, sv);
961   vapi_msg_show_version_hton (sv);
962   while (VAPI_EAGAIN == (rv = vapi_send (ctx, sv)))
963     ;
964   ck_assert_int_eq (VAPI_OK, rv);
965   vapi_clear_generic_event_cb (ctx);
966   rv = vapi_dispatch_one (ctx);
967   ck_assert_int_eq (VAPI_OK, rv);
968   ck_assert_int_eq (1, called); /* needs to remain unchanged */
969 }
970
971 END_TEST;
972
973 vapi_error_e
974 combined_counters_cb (struct vapi_ctx_s *ctx, void *callback_ctx,
975                       vapi_payload_vnet_interface_combined_counters * payload)
976 {
977   int *called = callback_ctx;
978   ++*called;
979   printf ("combined counters: first_sw_if_index=%u\n",
980           payload->first_sw_if_index);
981   return VAPI_OK;
982 }
983
984 vapi_error_e
985 stats_cb (vapi_ctx_t ctx, void *callback_ctx, vapi_error_e rv,
986           bool is_last, vapi_payload_want_stats_reply * payload)
987 {
988   return VAPI_OK;
989 }
990
991 START_TEST (test_stats_3)
992 {
993   printf ("--- Receive multiple stats using stat-specific non-blocking API "
994           "---\n");
995   vapi_msg_want_stats *ws = vapi_alloc_want_stats (ctx);
996   ws->payload.enable_disable = 1;
997   ws->payload.pid = getpid ();
998   vapi_error_e rv;
999   rv = vapi_want_stats (ctx, ws, stats_cb, NULL);
1000   ck_assert_int_eq (VAPI_OK, rv);
1001   int called = 0;
1002   int called2 = 0;
1003   vapi_set_vapi_msg_vnet_interface_simple_counters_event_cb (ctx,
1004                                                              simple_counters_cb,
1005                                                              &called);
1006   vapi_set_vapi_msg_vnet_interface_combined_counters_event_cb (ctx,
1007                                                                combined_counters_cb,
1008                                                                &called2);
1009   while (!called || !called2)
1010     {
1011       if (VAPI_EAGAIN != (rv = vapi_dispatch_one (ctx)))
1012         {
1013           ck_assert_int_eq (VAPI_OK, rv);
1014         }
1015     }
1016 }
1017
1018 END_TEST;
1019
1020 vapi_error_e
1021 show_version_no_cb (vapi_ctx_t ctx, void *caller_ctx,
1022                     vapi_error_e rv, bool is_last,
1023                     vapi_payload_show_version_reply * p)
1024 {
1025   ck_assert_int_eq (VAPI_ENORESP, rv);
1026   ck_assert_int_eq (true, is_last);
1027   ck_assert_ptr_eq (NULL, p);
1028   ++*(int *) caller_ctx;
1029   return VAPI_OK;
1030 }
1031
1032 START_TEST (test_no_response_1)
1033 {
1034   printf ("--- Simulate no response to regular message ---\n");
1035   vapi_error_e rv;
1036   vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
1037   ck_assert_ptr_ne (NULL, sv);
1038   sv->header._vl_msg_id = ~0;   /* malformed ID causes vpp to drop the msg */
1039   int called = 0;
1040   while (VAPI_EAGAIN ==
1041          (rv = vapi_show_version (ctx, sv, show_version_no_cb, &called)))
1042     ;
1043   ck_assert_int_eq (VAPI_OK, rv);
1044   sv = vapi_alloc_show_version (ctx);
1045   ck_assert_ptr_ne (NULL, sv);
1046   while (VAPI_EAGAIN ==
1047          (rv = vapi_show_version (ctx, sv, show_version_cb, &called)))
1048     ;
1049   ck_assert_int_eq (VAPI_OK, rv);
1050   rv = vapi_dispatch (ctx);
1051   ck_assert_int_eq (VAPI_OK, rv);
1052   ck_assert_int_eq (2, called);
1053 }
1054
1055 END_TEST;
1056
1057 vapi_error_e
1058 no_msg_cb (struct vapi_ctx_s *ctx, void *callback_ctx,
1059            vapi_error_e rv, bool is_last,
1060            vapi_payload_sw_interface_details * reply)
1061 {
1062   int *called = callback_ctx;
1063   ++*called;
1064   ck_assert_int_eq (VAPI_OK, rv);
1065   ck_assert_int_eq (true, is_last);
1066   ck_assert_ptr_eq (NULL, reply);
1067   return VAPI_OK;
1068 }
1069
1070 START_TEST (test_no_response_2)
1071 {
1072   printf ("--- Simulate no response to dump message ---\n");
1073   vapi_error_e rv;
1074   vapi_msg_sw_interface_dump *dump = vapi_alloc_sw_interface_dump (ctx);
1075   dump->header._vl_msg_id = ~0; /* malformed ID causes vpp to drop the msg */
1076   int no_called = 0;
1077   while (VAPI_EAGAIN ==
1078          (rv = vapi_sw_interface_dump (ctx, dump, no_msg_cb, &no_called)))
1079     ;
1080   ck_assert_int_eq (VAPI_OK, rv);
1081   rv = vapi_dispatch (ctx);
1082   ck_assert_int_eq (VAPI_OK, rv);
1083   ck_assert_int_eq (1, no_called);
1084 }
1085
1086 END_TEST;
1087
1088 START_TEST (test_unsupported)
1089 {
1090   printf ("--- Unsupported messages ---\n");
1091   bool available = vapi_is_msg_available (ctx, vapi_msg_id_test_fake_msg);
1092   ck_assert_int_eq (false, available);
1093 }
1094
1095 END_TEST;
1096
1097 Suite *
1098 test_suite (void)
1099 {
1100   Suite *s = suite_create ("VAPI test");
1101
1102   TCase *tc_negative = tcase_create ("Negative tests");
1103   tcase_add_test (tc_negative, test_invalid_values);
1104   suite_add_tcase (s, tc_negative);
1105
1106   TCase *tc_swap = tcase_create ("Byteswap tests");
1107   tcase_add_test (tc_swap, test_hton_1);
1108   tcase_add_test (tc_swap, test_hton_2);
1109   tcase_add_test (tc_swap, test_hton_3);
1110   tcase_add_test (tc_swap, test_hton_4);
1111   tcase_add_test (tc_swap, test_ntoh_1);
1112   tcase_add_test (tc_swap, test_ntoh_2);
1113   tcase_add_test (tc_swap, test_ntoh_3);
1114   tcase_add_test (tc_swap, test_ntoh_4);
1115   suite_add_tcase (s, tc_swap);
1116
1117   TCase *tc_connect = tcase_create ("Connect");
1118   tcase_add_test (tc_connect, test_connect);
1119   suite_add_tcase (s, tc_connect);
1120
1121   TCase *tc_block = tcase_create ("Blocking API");
1122   tcase_set_timeout (tc_block, 25);
1123   tcase_add_checked_fixture (tc_block, setup_blocking, teardown);
1124   tcase_add_test (tc_block, test_show_version_1);
1125   tcase_add_test (tc_block, test_show_version_2);
1126   tcase_add_test (tc_block, test_loopbacks_1);
1127   tcase_add_test (tc_block, test_stats_1);
1128   tcase_add_test (tc_block, test_stats_2);
1129   suite_add_tcase (s, tc_block);
1130
1131   TCase *tc_nonblock = tcase_create ("Nonblocking API");
1132   tcase_set_timeout (tc_nonblock, 25);
1133   tcase_add_checked_fixture (tc_nonblock, setup_nonblocking, teardown);
1134   tcase_add_test (tc_nonblock, test_show_version_3);
1135   tcase_add_test (tc_nonblock, test_show_version_4);
1136   tcase_add_test (tc_nonblock, test_show_version_5);
1137   tcase_add_test (tc_nonblock, test_loopbacks_2);
1138   tcase_add_test (tc_nonblock, test_stats_3);
1139   tcase_add_test (tc_nonblock, test_no_response_1);
1140   tcase_add_test (tc_nonblock, test_no_response_2);
1141   suite_add_tcase (s, tc_nonblock);
1142
1143   TCase *tc_unsupported = tcase_create ("Unsupported message");
1144   tcase_add_checked_fixture (tc_unsupported, setup_blocking, teardown);
1145   tcase_add_test (tc_unsupported, test_unsupported);
1146   suite_add_tcase (s, tc_unsupported);
1147
1148   return s;
1149 }
1150
1151 int
1152 main (int argc, char *argv[])
1153 {
1154   if (3 != argc)
1155     {
1156       printf ("Invalid argc==`%d'\n", argc);
1157       return EXIT_FAILURE;
1158     }
1159   app_name = argv[1];
1160   api_prefix = argv[2];
1161   printf ("App name: `%s', API prefix: `%s'\n", app_name, api_prefix);
1162
1163   int number_failed;
1164   Suite *s;
1165   SRunner *sr;
1166
1167   s = test_suite ();
1168   sr = srunner_create (s);
1169
1170   srunner_run_all (sr, CK_NORMAL);
1171   number_failed = srunner_ntests_failed (sr);
1172   srunner_free (sr);
1173   return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
1174 }
1175
1176 /*
1177  * fd.io coding-style-patch-verification: ON
1178  *
1179  * Local Variables:
1180  * eval: (c-set-style "gnu")
1181  * End:
1182  */