e8fb37c949f3cc33b30eead1c95ef85584702ef2
[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 <vnet/format_fns.h>
34 #include <memif/memif.api_enum.h>
35 #include <memif/memif.api_types.h>
36 #include <vlibmemory/vlib.api_types.h>
37
38 typedef struct
39 {
40   /* API message ID base */
41   u16 msg_id_base;
42   u32 ping_id;
43   vat_main_t *vat_main;
44 } memif_test_main_t;
45
46 memif_test_main_t memif_test_main;
47
48 static uword
49 unformat_memif_queues (unformat_input_t * input, va_list * args)
50 {
51   u32 *rx_queues = va_arg (*args, u32 *);
52   u32 *tx_queues = va_arg (*args, u32 *);
53
54   if (unformat (input, "rx-queues %u", rx_queues))
55     ;
56   if (unformat (input, "tx-queues %u", tx_queues))
57     ;
58
59   return 1;
60 }
61
62 /* memif_socket_filename_add_del API */
63 static int
64 api_memif_socket_filename_add_del (vat_main_t * vam)
65 {
66   unformat_input_t *i = vam->input;
67   vl_api_memif_socket_filename_add_del_t *mp;
68   u8 is_add;
69   u32 socket_id;
70   u8 *socket_filename;
71   int ret;
72
73   is_add = 1;
74   socket_id = ~0;
75   socket_filename = 0;
76
77   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
78     {
79       if (unformat (i, "id %u", &socket_id))
80         ;
81       else if (unformat (i, "filename %s", &socket_filename))
82         ;
83       else if (unformat (i, "del"))
84         is_add = 0;
85       else if (unformat (i, "add"))
86         is_add = 1;
87       else
88         {
89           vec_free (socket_filename);
90           clib_warning ("unknown input `%U'", format_unformat_error, i);
91           return -99;
92         }
93     }
94
95   if (socket_id == 0 || socket_id == ~0)
96     {
97       vec_free (socket_filename);
98       errmsg ("Invalid socket id");
99       return -99;
100     }
101
102   if (is_add && (!socket_filename || *socket_filename == 0))
103     {
104       vec_free (socket_filename);
105       errmsg ("Invalid socket filename");
106       return -99;
107     }
108
109   M2 (MEMIF_SOCKET_FILENAME_ADD_DEL, mp, strlen ((char *) socket_filename));
110
111   mp->is_add = is_add;
112   mp->socket_id = htonl (socket_id);
113   char *p = (char *) &mp->socket_filename;
114   p += vl_api_vec_to_api_string (socket_filename, (vl_api_string_t *) p);
115
116   vec_free (socket_filename);
117
118   S (mp);
119   W (ret);
120
121   return ret;
122 }
123
124 /* memif_socket_filename_add_del API */
125 static int
126 api_memif_socket_filename_add_del_v2 (vat_main_t *vam)
127 {
128   unformat_input_t *i = vam->input;
129   vl_api_memif_socket_filename_add_del_v2_t *mp;
130   u8 is_add;
131   u32 socket_id;
132   u8 *socket_filename;
133   int ret;
134
135   is_add = 1;
136   socket_id = ~0;
137   socket_filename = 0;
138
139   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
140     {
141       if (unformat (i, "id %u", &socket_id))
142         ;
143       else if (unformat (i, "filename %s", &socket_filename))
144         ;
145       else if (unformat (i, "del"))
146         is_add = 0;
147       else if (unformat (i, "add"))
148         is_add = 1;
149       else
150         {
151           vec_free (socket_filename);
152           clib_warning ("unknown input `%U'", format_unformat_error, i);
153           return -99;
154         }
155     }
156
157   if (socket_id == 0 || socket_id == ~0)
158     {
159       vec_free (socket_filename);
160       errmsg ("Invalid socket id");
161       return -99;
162     }
163
164   if (is_add && (!socket_filename || *socket_filename == 0))
165     {
166       vec_free (socket_filename);
167       errmsg ("Invalid socket filename");
168       return -99;
169     }
170
171   M2 (MEMIF_SOCKET_FILENAME_ADD_DEL_V2, mp, strlen ((char *) socket_filename));
172
173   mp->is_add = is_add;
174   mp->socket_id = htonl (socket_id);
175   char *p = (char *) &mp->socket_filename;
176   p += vl_api_vec_to_api_string (socket_filename, (vl_api_string_t *) p);
177
178   vec_free (socket_filename);
179
180   S (mp);
181   W (ret);
182
183   return ret;
184 }
185
186 /* memif socket-create reply handler */
187 static void
188 vl_api_memif_socket_filename_add_del_v2_reply_t_handler (
189   vl_api_memif_socket_filename_add_del_v2_reply_t *mp)
190 {
191   vat_main_t *vam = memif_test_main.vat_main;
192   i32 retval = ntohl (mp->retval);
193
194   if (retval == 0)
195     {
196       fformat (vam->ofp, "created memif socket with socket_id %d\n",
197                ntohl (mp->socket_id));
198     }
199
200   vam->retval = retval;
201   vam->result_ready = 1;
202 }
203
204 /* memif_socket_filename_add_del reply handler */
205 #define VL_API_MEMIF_SOCKET_FILENAME_ADD_DEL_REPLY_T_HANDLER
206 static void vl_api_memif_socket_filename_add_del_reply_t_handler
207   (vl_api_memif_socket_filename_add_del_reply_t * mp)
208 {
209   vat_main_t *vam = memif_test_main.vat_main;
210   i32 retval = ntohl (mp->retval);
211
212   vam->retval = retval;
213   vam->result_ready = 1;
214   vam->regenerate_interface_table = 1;
215 }
216
217 /* memif-create API */
218 static int
219 api_memif_create (vat_main_t * vam)
220 {
221   unformat_input_t *i = vam->input;
222   vl_api_memif_create_t *mp;
223   u32 id = 0;
224   u32 socket_id = 0;
225   u8 *secret = 0;
226   u8 role = 1;
227   u32 ring_size = 0;
228   u32 buffer_size = 0;
229   u8 hw_addr[6] = { 0 };
230   u32 rx_queues = MEMIF_DEFAULT_RX_QUEUES;
231   u32 tx_queues = MEMIF_DEFAULT_TX_QUEUES;
232   int ret;
233   u8 mode = MEMIF_INTERFACE_MODE_ETHERNET;
234
235   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
236     {
237       if (unformat (i, "id %u", &id))
238         ;
239       else if (unformat (i, "socket-id %u", &socket_id))
240         ;
241       else if (unformat (i, "secret %s", &secret))
242         ;
243       else if (unformat (i, "ring_size %u", &ring_size))
244         ;
245       else if (unformat (i, "buffer_size %u", &buffer_size))
246         ;
247       else if (unformat (i, "master"))
248         role = 0;
249       else if (unformat (i, "slave %U",
250                          unformat_memif_queues, &rx_queues, &tx_queues))
251         role = 1;
252       else if (unformat (i, "mode ip"))
253         mode = MEMIF_INTERFACE_MODE_IP;
254       else if (unformat (i, "hw_addr %U", unformat_ethernet_address, hw_addr))
255         ;
256       else
257         {
258           clib_warning ("unknown input '%U'", format_unformat_error, i);
259           return -99;
260         }
261     }
262
263   if (socket_id == ~0)
264     {
265       errmsg ("invalid socket-id\n");
266       return -99;
267     }
268
269   if (!is_pow2 (ring_size))
270     {
271       errmsg ("ring size must be power of 2\n");
272       return -99;
273     }
274
275   if (rx_queues > 255 || rx_queues < 1)
276     {
277       errmsg ("rx queue must be between 1 - 255\n");
278       return -99;
279     }
280
281   if (tx_queues > 255 || tx_queues < 1)
282     {
283       errmsg ("tx queue must be between 1 - 255\n");
284       return -99;
285     }
286
287   M2 (MEMIF_CREATE, mp, strlen ((char *) secret));
288
289   mp->mode = mode;
290   mp->id = clib_host_to_net_u32 (id);
291   mp->role = role;
292   mp->ring_size = clib_host_to_net_u32 (ring_size);
293   mp->buffer_size = clib_host_to_net_u16 (buffer_size & 0xffff);
294   mp->socket_id = clib_host_to_net_u32 (socket_id);
295   if (secret != 0)
296     {
297       char *p = (char *) &mp->secret;
298       p += vl_api_vec_to_api_string (secret, (vl_api_string_t *) p);
299       vec_free (secret);
300     }
301   memcpy (mp->hw_addr, hw_addr, 6);
302   mp->rx_queues = rx_queues;
303   mp->tx_queues = tx_queues;
304
305   S (mp);
306   W (ret);
307   return ret;
308 }
309
310 /* memif-create reply handler */
311 static void vl_api_memif_create_reply_t_handler
312   (vl_api_memif_create_reply_t * mp)
313 {
314   vat_main_t *vam = memif_test_main.vat_main;
315   i32 retval = ntohl (mp->retval);
316
317   if (retval == 0)
318     {
319       fformat (vam->ofp, "created memif with sw_if_index %d\n",
320                ntohl (mp->sw_if_index));
321     }
322
323   vam->retval = retval;
324   vam->result_ready = 1;
325   vam->regenerate_interface_table = 1;
326 }
327
328 /* memif-create_v2 API */
329 static int
330 api_memif_create_v2 (vat_main_t *vam)
331 {
332   unformat_input_t *i = vam->input;
333   vl_api_memif_create_v2_t *mp;
334   u32 id = 0;
335   u32 socket_id = 0;
336   u8 *secret = 0;
337   u8 role = 1;
338   u32 ring_size = 0;
339   u8 use_dma = 0;
340   u32 buffer_size = 0;
341   u8 hw_addr[6] = { 0 };
342   u32 rx_queues = MEMIF_DEFAULT_RX_QUEUES;
343   u32 tx_queues = MEMIF_DEFAULT_TX_QUEUES;
344   int ret;
345   u8 mode = MEMIF_INTERFACE_MODE_ETHERNET;
346
347   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
348     {
349       if (unformat (i, "id %u", &id))
350         ;
351       else if (unformat (i, "socket-id %u", &socket_id))
352         ;
353       else if (unformat (i, "secret %s", &secret))
354         ;
355       else if (unformat (i, "ring_size %u", &ring_size))
356         ;
357       else if (unformat (i, "buffer_size %u", &buffer_size))
358         ;
359       else if (unformat (i, "master"))
360         role = 0;
361       else if (unformat (i, "use_dma %u", &use_dma))
362         ;
363       else if (unformat (i, "slave %U", unformat_memif_queues, &rx_queues,
364                          &tx_queues))
365         role = 1;
366       else if (unformat (i, "mode ip"))
367         mode = MEMIF_INTERFACE_MODE_IP;
368       else if (unformat (i, "hw_addr %U", unformat_ethernet_address, hw_addr))
369         ;
370       else
371         {
372           clib_warning ("unknown input '%U'", format_unformat_error, i);
373           return -99;
374         }
375     }
376
377   if (socket_id == ~0)
378     {
379       errmsg ("invalid socket-id\n");
380       return -99;
381     }
382
383   if (!is_pow2 (ring_size))
384     {
385       errmsg ("ring size must be power of 2\n");
386       return -99;
387     }
388
389   if (rx_queues > 255 || rx_queues < 1)
390     {
391       errmsg ("rx queue must be between 1 - 255\n");
392       return -99;
393     }
394
395   if (tx_queues > 255 || tx_queues < 1)
396     {
397       errmsg ("tx queue must be between 1 - 255\n");
398       return -99;
399     }
400
401   M2 (MEMIF_CREATE, mp, strlen ((char *) secret));
402
403   mp->mode = mode;
404   mp->id = clib_host_to_net_u32 (id);
405   mp->role = role;
406   mp->use_dma = use_dma;
407   mp->ring_size = clib_host_to_net_u32 (ring_size);
408   mp->buffer_size = clib_host_to_net_u16 (buffer_size & 0xffff);
409   mp->socket_id = clib_host_to_net_u32 (socket_id);
410   if (secret != 0)
411     {
412       char *p = (char *) &mp->secret;
413       p += vl_api_vec_to_api_string (secret, (vl_api_string_t *) p);
414       vec_free (secret);
415     }
416   memcpy (mp->hw_addr, hw_addr, 6);
417   mp->rx_queues = rx_queues;
418   mp->tx_queues = tx_queues;
419
420   S (mp);
421   W (ret);
422   return ret;
423 }
424
425 /* memif-create_v2 reply handler */
426 static void
427 vl_api_memif_create_v2_reply_t_handler (vl_api_memif_create_reply_t *mp)
428 {
429   vat_main_t *vam = memif_test_main.vat_main;
430   i32 retval = ntohl (mp->retval);
431
432   if (retval == 0)
433     {
434       fformat (vam->ofp, "created memif with sw_if_index %d\n",
435                ntohl (mp->sw_if_index));
436     }
437
438   vam->retval = retval;
439   vam->result_ready = 1;
440   vam->regenerate_interface_table = 1;
441 }
442
443 /* memif-delete API */
444 static int
445 api_memif_delete (vat_main_t * vam)
446 {
447   unformat_input_t *i = vam->input;
448   vl_api_memif_delete_t *mp;
449   u32 sw_if_index = 0;
450   u8 index_defined = 0;
451   int ret;
452
453   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
454     {
455       if (unformat (i, "sw_if_index %u", &sw_if_index))
456         index_defined = 1;
457       else
458         {
459           clib_warning ("unknown input '%U'", format_unformat_error, i);
460           return -99;
461         }
462     }
463
464   if (!index_defined)
465     {
466       errmsg ("missing sw_if_index\n");
467       return -99;
468     }
469
470   M (MEMIF_DELETE, mp);
471
472   mp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
473
474   S (mp);
475   W (ret);
476   return ret;
477 }
478
479 /* memif-dump API */
480 static int
481 api_memif_dump (vat_main_t * vam)
482 {
483   memif_test_main_t *mm = &memif_test_main;
484   vl_api_memif_dump_t *mp;
485   vl_api_control_ping_t *mp_ping;
486   int ret;
487
488   if (vam->json_output)
489     {
490       clib_warning ("JSON output not supported for memif_dump");
491       return -99;
492     }
493
494   M (MEMIF_DUMP, mp);
495   S (mp);
496
497   /* Use a control ping for synchronization */
498   if (!mm->ping_id)
499     mm->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
500   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
501   mp_ping->_vl_msg_id = htons (mm->ping_id);
502   mp_ping->client_index = vam->my_client_index;
503
504   fformat (vam->ofp, "Sending ping id=%d\n", mm->ping_id);
505
506   vam->result_ready = 0;
507   S (mp_ping);
508
509   W (ret);
510   return ret;
511 }
512
513 /* memif-details message handler */
514 static void
515 vl_api_memif_details_t_handler (vl_api_memif_details_t * mp)
516 {
517   vat_main_t *vam = memif_test_main.vat_main;
518
519   fformat (vam->ofp, "%s: sw_if_index %u mac %U\n"
520            "   id %u socket-id %u role %s\n"
521            "   ring_size %u buffer_size %u\n"
522            "   state %s link %s\n",
523            mp->if_name, ntohl (mp->sw_if_index), format_ethernet_address,
524            mp->hw_addr, clib_net_to_host_u32 (mp->id),
525            clib_net_to_host_u32 (mp->socket_id),
526            mp->role ? "slave" : "master",
527            ntohl (mp->ring_size), ntohs (mp->buffer_size),
528            (mp->flags & IF_STATUS_API_FLAG_ADMIN_UP) ? "up" : "down",
529            (mp->flags & IF_STATUS_API_FLAG_LINK_UP) ? "up" : "down");
530 }
531
532 /* memif_socket_filename_dump API */
533 static int
534 api_memif_socket_filename_dump (vat_main_t * vam)
535 {
536   memif_test_main_t *mm = &memif_test_main;
537   vl_api_memif_socket_filename_dump_t *mp;
538   vl_api_control_ping_t *mp_ping;
539   int ret;
540
541   if (vam->json_output)
542     {
543       clib_warning
544         ("JSON output not supported for memif_socket_filename_dump");
545       return -99;
546     }
547
548   M (MEMIF_SOCKET_FILENAME_DUMP, mp);
549   S (mp);
550
551   /* Use a control ping for synchronization */
552   if (!mm->ping_id)
553     mm->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
554   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
555   mp_ping->_vl_msg_id = htons (mm->ping_id);
556   mp_ping->client_index = vam->my_client_index;
557
558   fformat (vam->ofp, "Sending ping id=%d\n", mm->ping_id);
559
560   vam->result_ready = 0;
561   S (mp_ping);
562
563   W (ret);
564   return ret;
565 }
566
567 /* memif_socket_format_details message handler */
568 static void vl_api_memif_socket_filename_details_t_handler
569   (vl_api_memif_socket_filename_details_t * mp)
570 {
571   vat_main_t *vam = memif_test_main.vat_main;
572
573   fformat (vam->ofp,
574            "id %u : filename %s\n",
575            ntohl (mp->socket_id), mp->socket_filename);
576 }
577
578 #include <memif/memif.api_test.c>
579
580 /*
581  * fd.io coding-style-patch-verification: ON
582  *
583  * Local Variables:
584  * eval: (c-set-style "gnu")
585  * End:
586  */