memif: API cleanup
[vpp.git] / src / plugins / memif / memif_test.c
1 /*
2  * memif VAT support
3  *
4  * Copyright (c) 2017 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <inttypes.h>
19
20 #include <vat/vat.h>
21 #include <vlibapi/api.h>
22 #include <vlibmemory/api.h>
23
24 #include <vppinfra/error.h>
25 #include <vnet/ip/ip.h>
26 #include <memif/memif.h>
27 #include <memif/private.h>
28
29 #define __plugin_msg_base memif_test_main.msg_id_base
30 #include <vlibapi/vat_helper_macros.h>
31
32 /* declare message IDs */
33 #include <memif/memif_msg_enum.h>
34
35 /* Get CRC codes of the messages defined outside of this plugin */
36 #define vl_msg_name_crc_list
37 #include <vpp/api/vpe_all_api_h.h>
38 #undef vl_msg_name_crc_list
39
40 /* define message structures */
41 #define vl_typedefs
42 #include <vpp/api/vpe_all_api_h.h>
43 #include <memif/memif_all_api_h.h>
44 #undef vl_typedefs
45
46 /* declare message handlers for each api */
47
48 #define vl_endianfun            /* define message structures */
49 #include <memif/memif_all_api_h.h>
50 #undef vl_endianfun
51
52 /* instantiate all the print functions we know about */
53 #define vl_print(handle, ...)
54 #define vl_printfun
55 #include <memif/memif_all_api_h.h>
56 #undef vl_printfun
57
58 /* Get the API version number. */
59 #define vl_api_version(n,v) static u32 api_version=(v);
60 #include <memif/memif_all_api_h.h>
61 #undef vl_api_version
62
63 typedef struct
64 {
65   /* API message ID base */
66   u16 msg_id_base;
67   u32 ping_id;
68   vat_main_t *vat_main;
69 } memif_test_main_t;
70
71 memif_test_main_t memif_test_main;
72
73 /* standard reply handlers */
74 #define foreach_standard_reply_retval_handler           \
75 _(memif_delete_reply)
76
77 #define _(n)                                            \
78     static void vl_api_##n##_t_handler                  \
79     (vl_api_##n##_t * mp)                               \
80     {                                                   \
81         vat_main_t * vam = memif_test_main.vat_main;    \
82         i32 retval = ntohl(mp->retval);                 \
83         if (vam->async_mode) {                          \
84             vam->async_errors += (retval < 0);          \
85         } else {                                        \
86             vam->retval = retval;                       \
87             vam->result_ready = 1;                      \
88         }                                               \
89     }
90 foreach_standard_reply_retval_handler;
91 #undef _
92
93 /*
94  * Table of message reply handlers, must include boilerplate handlers
95  * we just generated
96  */
97 #define foreach_vpe_api_reply_msg                       \
98 _(MEMIF_CREATE_REPLY, memif_create_reply)               \
99 _(MEMIF_DELETE_REPLY, memif_delete_reply)               \
100 _(MEMIF_DETAILS, memif_details)                         \
101 _(MEMIF_SOCKET_FILENAME_DETAILS, memif_socket_filename_details) \
102 _(MEMIF_SOCKET_FILENAME_ADD_DEL_REPLY, memif_socket_filename_add_del_reply)
103
104 static uword
105 unformat_memif_queues (unformat_input_t * input, va_list * args)
106 {
107   u32 *rx_queues = va_arg (*args, u32 *);
108   u32 *tx_queues = va_arg (*args, u32 *);
109
110   if (unformat (input, "rx-queues %u", rx_queues))
111     ;
112   if (unformat (input, "tx-queues %u", tx_queues))
113     ;
114
115   return 1;
116 }
117
118 /* memif_socket_filename_add_del API */
119 static int
120 api_memif_socket_filename_add_del (vat_main_t * vam)
121 {
122   unformat_input_t *i = vam->input;
123   vl_api_memif_socket_filename_add_del_t *mp;
124   u8 is_add;
125   u32 socket_id;
126   u8 *socket_filename;
127   int ret;
128
129   is_add = 1;
130   socket_id = ~0;
131   socket_filename = 0;
132
133   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
134     {
135       if (unformat (i, "id %u", &socket_id))
136         ;
137       else if (unformat (i, "filename %s", &socket_filename))
138         ;
139       else if (unformat (i, "del"))
140         is_add = 0;
141       else if (unformat (i, "add"))
142         is_add = 1;
143       else
144         {
145           vec_free (socket_filename);
146           clib_warning ("unknown input `%U'", format_unformat_error, i);
147           return -99;
148         }
149     }
150
151   if (socket_id == 0 || socket_id == ~0)
152     {
153       vec_free (socket_filename);
154       errmsg ("Invalid socket id");
155       return -99;
156     }
157
158   if (is_add && (!socket_filename || *socket_filename == 0))
159     {
160       vec_free (socket_filename);
161       errmsg ("Invalid socket filename");
162       return -99;
163     }
164
165   M2 (MEMIF_SOCKET_FILENAME_ADD_DEL, mp, strlen ((char *) socket_filename));
166
167   mp->is_add = is_add;
168   mp->socket_id = htonl (socket_id);
169   char *p = (char *) &mp->socket_filename;
170   p +=
171     vl_api_to_api_string (strlen ((char *) socket_filename),
172                           (char *) socket_filename, (vl_api_string_t *) p);
173
174   vec_free (socket_filename);
175
176   S (mp);
177   W (ret);
178
179   return ret;
180 }
181
182 /* memif_socket_filename_add_del reply handler */
183 static void vl_api_memif_socket_filename_add_del_reply_t_handler
184   (vl_api_memif_socket_filename_add_del_reply_t * mp)
185 {
186   vat_main_t *vam = memif_test_main.vat_main;
187   i32 retval = ntohl (mp->retval);
188
189   vam->retval = retval;
190   vam->result_ready = 1;
191   vam->regenerate_interface_table = 1;
192 }
193
194 /* memif-create API */
195 static int
196 api_memif_create (vat_main_t * vam)
197 {
198   unformat_input_t *i = vam->input;
199   vl_api_memif_create_t *mp;
200   u32 id = 0;
201   u32 socket_id = 0;
202   u8 *secret = 0;
203   u8 role = 1;
204   u32 ring_size = 0;
205   u32 buffer_size = 0;
206   u8 hw_addr[6] = { 0 };
207   u32 rx_queues = MEMIF_DEFAULT_RX_QUEUES;
208   u32 tx_queues = MEMIF_DEFAULT_TX_QUEUES;
209   int ret;
210   u8 mode = MEMIF_INTERFACE_MODE_ETHERNET;
211
212   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
213     {
214       if (unformat (i, "id %u", &id))
215         ;
216       else if (unformat (i, "socket-id %u", &socket_id))
217         ;
218       else if (unformat (i, "secret %s", &secret))
219         ;
220       else if (unformat (i, "ring_size %u", &ring_size))
221         ;
222       else if (unformat (i, "buffer_size %u", &buffer_size))
223         ;
224       else if (unformat (i, "master"))
225         role = 0;
226       else if (unformat (i, "slave %U",
227                          unformat_memif_queues, &rx_queues, &tx_queues))
228         role = 1;
229       else if (unformat (i, "mode ip"))
230         mode = MEMIF_INTERFACE_MODE_IP;
231       else if (unformat (i, "hw_addr %U", unformat_ethernet_address, hw_addr))
232         ;
233       else
234         {
235           clib_warning ("unknown input '%U'", format_unformat_error, i);
236           return -99;
237         }
238     }
239
240   if (socket_id == ~0)
241     {
242       errmsg ("invalid socket-id\n");
243       return -99;
244     }
245
246   if (!is_pow2 (ring_size))
247     {
248       errmsg ("ring size must be power of 2\n");
249       return -99;
250     }
251
252   if (rx_queues > 255 || rx_queues < 1)
253     {
254       errmsg ("rx queue must be between 1 - 255\n");
255       return -99;
256     }
257
258   if (tx_queues > 255 || tx_queues < 1)
259     {
260       errmsg ("tx queue must be between 1 - 255\n");
261       return -99;
262     }
263
264   M2 (MEMIF_CREATE, mp, strlen ((char *) secret));
265
266   mp->mode = mode;
267   mp->id = clib_host_to_net_u32 (id);
268   mp->role = role;
269   mp->ring_size = clib_host_to_net_u32 (ring_size);
270   mp->buffer_size = clib_host_to_net_u16 (buffer_size & 0xffff);
271   mp->socket_id = clib_host_to_net_u32 (socket_id);
272   if (secret != 0)
273     {
274       char *p = (char *) &mp->secret;
275       p += vl_api_to_api_string (strlen ((char *) secret), (char *) secret,
276                                  (vl_api_string_t *) p);
277       vec_free (secret);
278     }
279   memcpy (mp->hw_addr, hw_addr, 6);
280   mp->rx_queues = rx_queues;
281   mp->tx_queues = tx_queues;
282
283   S (mp);
284   W (ret);
285   return ret;
286 }
287
288 /* memif-create reply handler */
289 static void vl_api_memif_create_reply_t_handler
290   (vl_api_memif_create_reply_t * mp)
291 {
292   vat_main_t *vam = memif_test_main.vat_main;
293   i32 retval = ntohl (mp->retval);
294
295   if (retval == 0)
296     {
297       fformat (vam->ofp, "created memif with sw_if_index %d\n",
298                ntohl (mp->sw_if_index));
299     }
300
301   vam->retval = retval;
302   vam->result_ready = 1;
303   vam->regenerate_interface_table = 1;
304 }
305
306 /* memif-delete API */
307 static int
308 api_memif_delete (vat_main_t * vam)
309 {
310   unformat_input_t *i = vam->input;
311   vl_api_memif_delete_t *mp;
312   u32 sw_if_index = 0;
313   u8 index_defined = 0;
314   int ret;
315
316   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
317     {
318       if (unformat (i, "sw_if_index %u", &sw_if_index))
319         index_defined = 1;
320       else
321         {
322           clib_warning ("unknown input '%U'", format_unformat_error, i);
323           return -99;
324         }
325     }
326
327   if (!index_defined)
328     {
329       errmsg ("missing sw_if_index\n");
330       return -99;
331     }
332
333   M (MEMIF_DELETE, mp);
334
335   mp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
336
337   S (mp);
338   W (ret);
339   return ret;
340 }
341
342 /* memif-dump API */
343 static int
344 api_memif_dump (vat_main_t * vam)
345 {
346   memif_test_main_t *mm = &memif_test_main;
347   vl_api_memif_dump_t *mp;
348   vl_api_control_ping_t *mp_ping;
349   int ret;
350
351   if (vam->json_output)
352     {
353       clib_warning ("JSON output not supported for memif_dump");
354       return -99;
355     }
356
357   M (MEMIF_DUMP, mp);
358   S (mp);
359
360   /* Use a control ping for synchronization */
361   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
362   mp_ping->_vl_msg_id = htons (mm->ping_id);
363   mp_ping->client_index = vam->my_client_index;
364
365   fformat (vam->ofp, "Sending ping id=%d\n", mm->ping_id);
366
367   vam->result_ready = 0;
368   S (mp_ping);
369
370   W (ret);
371   return ret;
372 }
373
374 /* memif-details message handler */
375 static void
376 vl_api_memif_details_t_handler (vl_api_memif_details_t * mp)
377 {
378   vat_main_t *vam = memif_test_main.vat_main;
379
380   fformat (vam->ofp, "%s: sw_if_index %u mac %U\n"
381            "   id %u socket-id %u role %s\n"
382            "   ring_size %u buffer_size %u\n"
383            "   state %s link %s\n",
384            mp->if_name, ntohl (mp->sw_if_index), format_ethernet_address,
385            mp->hw_addr, clib_net_to_host_u32 (mp->id),
386            clib_net_to_host_u32 (mp->socket_id),
387            mp->role ? "slave" : "master",
388            ntohl (mp->ring_size), ntohs (mp->buffer_size),
389            (mp->flags & IF_STATUS_API_FLAG_ADMIN_UP) ? "up" : "down",
390            (mp->flags & IF_STATUS_API_FLAG_LINK_UP) ? "up" : "down");
391 }
392
393 /* memif_socket_filename_dump API */
394 static int
395 api_memif_socket_filename_dump (vat_main_t * vam)
396 {
397   memif_test_main_t *mm = &memif_test_main;
398   vl_api_memif_socket_filename_dump_t *mp;
399   vl_api_control_ping_t *mp_ping;
400   int ret;
401
402   if (vam->json_output)
403     {
404       clib_warning
405         ("JSON output not supported for memif_socket_filename_dump");
406       return -99;
407     }
408
409   M (MEMIF_SOCKET_FILENAME_DUMP, mp);
410   S (mp);
411
412   /* Use a control ping for synchronization */
413   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
414   mp_ping->_vl_msg_id = htons (mm->ping_id);
415   mp_ping->client_index = vam->my_client_index;
416
417   fformat (vam->ofp, "Sending ping id=%d\n", mm->ping_id);
418
419   vam->result_ready = 0;
420   S (mp_ping);
421
422   W (ret);
423   return ret;
424 }
425
426 /* memif_socket_format_details message handler */
427 static void vl_api_memif_socket_filename_details_t_handler
428   (vl_api_memif_socket_filename_details_t * mp)
429 {
430   vat_main_t *vam = memif_test_main.vat_main;
431
432   fformat (vam->ofp,
433            "id %u : filename %s\n",
434            ntohl (mp->socket_id), mp->socket_filename);
435 }
436
437 /*
438  * List of messages that the api test plugin sends,
439  * and that the data plane plugin processes
440  */
441 #define foreach_vpe_api_msg                                       \
442 _(memif_create, "[id <id>] [socket-id <id>] [ring_size <size>] " \
443                 "[buffer_size <size>] [hw_addr <mac_address>] "   \
444                 "[secret <string>] [mode ip] <master|slave>")     \
445 _(memif_delete, "<sw_if_index>")                                  \
446 _(memif_dump, "")                                                 \
447 _(memif_socket_filename_dump, "")                               \
448 _(memif_socket_filename_add_del, "[add|del] id <id> filename <file>")
449
450 static void
451 memif_vat_api_hookup (vat_main_t * vam)
452 {
453   memif_test_main_t *mm __attribute__ ((unused)) = &memif_test_main;
454   /* Hook up handlers for replies from the data plane plug-in */
455 #define _(N,n)                                                  \
456   vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base),       \
457                           #n,                                   \
458                           vl_api_##n##_t_handler,               \
459                           vl_noop_handler,                      \
460                           vl_api_##n##_t_endian,                \
461                           vl_api_##n##_t_print,                 \
462                           sizeof(vl_api_##n##_t), 1);
463   foreach_vpe_api_reply_msg;
464 #undef _
465
466   /* API messages we can send */
467 #define _(n,h)                                          \
468   hash_set_mem (vam->function_by_name, #n, api_##n);
469   foreach_vpe_api_msg;
470 #undef _
471
472   /* Help strings */
473 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
474   foreach_vpe_api_msg;
475 #undef _
476 }
477
478 clib_error_t *
479 vat_plugin_register (vat_main_t * vam)
480 {
481   memif_test_main_t *mm = &memif_test_main;
482   u8 *name;
483
484   mm->vat_main = vam;
485
486   /* Ask the vpp engine for the first assigned message-id */
487   name = format (0, "memif_%08x%c", api_version, 0);
488   mm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
489   vec_free (name);
490
491   if (mm->msg_id_base == (u16) ~ 0)
492     return clib_error_return (0, "memif plugin not loaded...");
493
494   /* Get the control ping ID */
495 #define _(id,n,crc) \
496   const char *id ## _CRC __attribute__ ((unused)) = #n "_" #crc;
497   foreach_vl_msg_name_crc_vpe;
498 #undef _
499   mm->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
500
501   if (mm->msg_id_base != (u16) ~ 0)
502     memif_vat_api_hookup (vam);
503
504   return 0;
505 }
506
507 /*
508  * fd.io coding-style-patch-verification: ON
509  *
510  * Local Variables:
511  * eval: (c-set-style "gnu")
512  * End:
513  */