api: improve api string safety
[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 <vpp/api/vpe.api_types.h>
37 //#include <vnet/ethernet/ethernet_types.api_types.h>
38
39 typedef struct
40 {
41   /* API message ID base */
42   u16 msg_id_base;
43   u32 ping_id;
44   vat_main_t *vat_main;
45 } memif_test_main_t;
46
47 memif_test_main_t memif_test_main;
48
49 static uword
50 unformat_memif_queues (unformat_input_t * input, va_list * args)
51 {
52   u32 *rx_queues = va_arg (*args, u32 *);
53   u32 *tx_queues = va_arg (*args, u32 *);
54
55   if (unformat (input, "rx-queues %u", rx_queues))
56     ;
57   if (unformat (input, "tx-queues %u", tx_queues))
58     ;
59
60   return 1;
61 }
62
63 /* memif_socket_filename_add_del API */
64 static int
65 api_memif_socket_filename_add_del (vat_main_t * vam)
66 {
67   unformat_input_t *i = vam->input;
68   vl_api_memif_socket_filename_add_del_t *mp;
69   u8 is_add;
70   u32 socket_id;
71   u8 *socket_filename;
72   int ret;
73
74   is_add = 1;
75   socket_id = ~0;
76   socket_filename = 0;
77
78   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
79     {
80       if (unformat (i, "id %u", &socket_id))
81         ;
82       else if (unformat (i, "filename %s", &socket_filename))
83         ;
84       else if (unformat (i, "del"))
85         is_add = 0;
86       else if (unformat (i, "add"))
87         is_add = 1;
88       else
89         {
90           vec_free (socket_filename);
91           clib_warning ("unknown input `%U'", format_unformat_error, i);
92           return -99;
93         }
94     }
95
96   if (socket_id == 0 || socket_id == ~0)
97     {
98       vec_free (socket_filename);
99       errmsg ("Invalid socket id");
100       return -99;
101     }
102
103   if (is_add && (!socket_filename || *socket_filename == 0))
104     {
105       vec_free (socket_filename);
106       errmsg ("Invalid socket filename");
107       return -99;
108     }
109
110   M2 (MEMIF_SOCKET_FILENAME_ADD_DEL, mp, strlen ((char *) socket_filename));
111
112   mp->is_add = is_add;
113   mp->socket_id = htonl (socket_id);
114   char *p = (char *) &mp->socket_filename;
115   p += vl_api_vec_to_api_string (socket_filename, (vl_api_string_t *) p);
116
117   vec_free (socket_filename);
118
119   S (mp);
120   W (ret);
121
122   return ret;
123 }
124
125 /* memif_socket_filename_add_del reply handler */
126 #define VL_API_MEMIF_SOCKET_FILENAME_ADD_DEL_REPLY_T_HANDLER
127 static void vl_api_memif_socket_filename_add_del_reply_t_handler
128   (vl_api_memif_socket_filename_add_del_reply_t * mp)
129 {
130   vat_main_t *vam = memif_test_main.vat_main;
131   i32 retval = ntohl (mp->retval);
132
133   vam->retval = retval;
134   vam->result_ready = 1;
135   vam->regenerate_interface_table = 1;
136 }
137
138 /* memif-create API */
139 static int
140 api_memif_create (vat_main_t * vam)
141 {
142   unformat_input_t *i = vam->input;
143   vl_api_memif_create_t *mp;
144   u32 id = 0;
145   u32 socket_id = 0;
146   u8 *secret = 0;
147   u8 role = 1;
148   u32 ring_size = 0;
149   u32 buffer_size = 0;
150   u8 hw_addr[6] = { 0 };
151   u32 rx_queues = MEMIF_DEFAULT_RX_QUEUES;
152   u32 tx_queues = MEMIF_DEFAULT_TX_QUEUES;
153   int ret;
154   u8 mode = MEMIF_INTERFACE_MODE_ETHERNET;
155
156   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
157     {
158       if (unformat (i, "id %u", &id))
159         ;
160       else if (unformat (i, "socket-id %u", &socket_id))
161         ;
162       else if (unformat (i, "secret %s", &secret))
163         ;
164       else if (unformat (i, "ring_size %u", &ring_size))
165         ;
166       else if (unformat (i, "buffer_size %u", &buffer_size))
167         ;
168       else if (unformat (i, "master"))
169         role = 0;
170       else if (unformat (i, "slave %U",
171                          unformat_memif_queues, &rx_queues, &tx_queues))
172         role = 1;
173       else if (unformat (i, "mode ip"))
174         mode = MEMIF_INTERFACE_MODE_IP;
175       else if (unformat (i, "hw_addr %U", unformat_ethernet_address, hw_addr))
176         ;
177       else
178         {
179           clib_warning ("unknown input '%U'", format_unformat_error, i);
180           return -99;
181         }
182     }
183
184   if (socket_id == ~0)
185     {
186       errmsg ("invalid socket-id\n");
187       return -99;
188     }
189
190   if (!is_pow2 (ring_size))
191     {
192       errmsg ("ring size must be power of 2\n");
193       return -99;
194     }
195
196   if (rx_queues > 255 || rx_queues < 1)
197     {
198       errmsg ("rx queue must be between 1 - 255\n");
199       return -99;
200     }
201
202   if (tx_queues > 255 || tx_queues < 1)
203     {
204       errmsg ("tx queue must be between 1 - 255\n");
205       return -99;
206     }
207
208   M2 (MEMIF_CREATE, mp, strlen ((char *) secret));
209
210   mp->mode = mode;
211   mp->id = clib_host_to_net_u32 (id);
212   mp->role = role;
213   mp->ring_size = clib_host_to_net_u32 (ring_size);
214   mp->buffer_size = clib_host_to_net_u16 (buffer_size & 0xffff);
215   mp->socket_id = clib_host_to_net_u32 (socket_id);
216   if (secret != 0)
217     {
218       char *p = (char *) &mp->secret;
219       p += vl_api_vec_to_api_string (secret, (vl_api_string_t *) p);
220       vec_free (secret);
221     }
222   memcpy (mp->hw_addr, hw_addr, 6);
223   mp->rx_queues = rx_queues;
224   mp->tx_queues = tx_queues;
225
226   S (mp);
227   W (ret);
228   return ret;
229 }
230
231 /* memif-create reply handler */
232 static void vl_api_memif_create_reply_t_handler
233   (vl_api_memif_create_reply_t * mp)
234 {
235   vat_main_t *vam = memif_test_main.vat_main;
236   i32 retval = ntohl (mp->retval);
237
238   if (retval == 0)
239     {
240       fformat (vam->ofp, "created memif with sw_if_index %d\n",
241                ntohl (mp->sw_if_index));
242     }
243
244   vam->retval = retval;
245   vam->result_ready = 1;
246   vam->regenerate_interface_table = 1;
247 }
248
249 /* memif-delete API */
250 static int
251 api_memif_delete (vat_main_t * vam)
252 {
253   unformat_input_t *i = vam->input;
254   vl_api_memif_delete_t *mp;
255   u32 sw_if_index = 0;
256   u8 index_defined = 0;
257   int ret;
258
259   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
260     {
261       if (unformat (i, "sw_if_index %u", &sw_if_index))
262         index_defined = 1;
263       else
264         {
265           clib_warning ("unknown input '%U'", format_unformat_error, i);
266           return -99;
267         }
268     }
269
270   if (!index_defined)
271     {
272       errmsg ("missing sw_if_index\n");
273       return -99;
274     }
275
276   M (MEMIF_DELETE, mp);
277
278   mp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
279
280   S (mp);
281   W (ret);
282   return ret;
283 }
284
285 /* memif-dump API */
286 static int
287 api_memif_dump (vat_main_t * vam)
288 {
289   memif_test_main_t *mm = &memif_test_main;
290   vl_api_memif_dump_t *mp;
291   vl_api_control_ping_t *mp_ping;
292   int ret;
293
294   if (vam->json_output)
295     {
296       clib_warning ("JSON output not supported for memif_dump");
297       return -99;
298     }
299
300   M (MEMIF_DUMP, mp);
301   S (mp);
302
303   /* Use a control ping for synchronization */
304   if (!mm->ping_id)
305     mm->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
306   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
307   mp_ping->_vl_msg_id = htons (mm->ping_id);
308   mp_ping->client_index = vam->my_client_index;
309
310   fformat (vam->ofp, "Sending ping id=%d\n", mm->ping_id);
311
312   vam->result_ready = 0;
313   S (mp_ping);
314
315   W (ret);
316   return ret;
317 }
318
319 /* memif-details message handler */
320 static void
321 vl_api_memif_details_t_handler (vl_api_memif_details_t * mp)
322 {
323   vat_main_t *vam = memif_test_main.vat_main;
324
325   fformat (vam->ofp, "%s: sw_if_index %u mac %U\n"
326            "   id %u socket-id %u role %s\n"
327            "   ring_size %u buffer_size %u\n"
328            "   state %s link %s\n",
329            mp->if_name, ntohl (mp->sw_if_index), format_ethernet_address,
330            mp->hw_addr, clib_net_to_host_u32 (mp->id),
331            clib_net_to_host_u32 (mp->socket_id),
332            mp->role ? "slave" : "master",
333            ntohl (mp->ring_size), ntohs (mp->buffer_size),
334            (mp->flags & IF_STATUS_API_FLAG_ADMIN_UP) ? "up" : "down",
335            (mp->flags & IF_STATUS_API_FLAG_LINK_UP) ? "up" : "down");
336 }
337
338 /* memif_socket_filename_dump API */
339 static int
340 api_memif_socket_filename_dump (vat_main_t * vam)
341 {
342   memif_test_main_t *mm = &memif_test_main;
343   vl_api_memif_socket_filename_dump_t *mp;
344   vl_api_control_ping_t *mp_ping;
345   int ret;
346
347   if (vam->json_output)
348     {
349       clib_warning
350         ("JSON output not supported for memif_socket_filename_dump");
351       return -99;
352     }
353
354   M (MEMIF_SOCKET_FILENAME_DUMP, mp);
355   S (mp);
356
357   /* Use a control ping for synchronization */
358   if (!mm->ping_id)
359     mm->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
360   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
361   mp_ping->_vl_msg_id = htons (mm->ping_id);
362   mp_ping->client_index = vam->my_client_index;
363
364   fformat (vam->ofp, "Sending ping id=%d\n", mm->ping_id);
365
366   vam->result_ready = 0;
367   S (mp_ping);
368
369   W (ret);
370   return ret;
371 }
372
373 /* memif_socket_format_details message handler */
374 static void vl_api_memif_socket_filename_details_t_handler
375   (vl_api_memif_socket_filename_details_t * mp)
376 {
377   vat_main_t *vam = memif_test_main.vat_main;
378
379   fformat (vam->ofp,
380            "id %u : filename %s\n",
381            ntohl (mp->socket_id), mp->socket_filename);
382 }
383
384 #include <memif/memif.api_test.c>
385
386 /*
387  * fd.io coding-style-patch-verification: ON
388  *
389  * Local Variables:
390  * eval: (c-set-style "gnu")
391  * End:
392  */