Fixes for issues raised by Coverity (VPP-972)
[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 #include <vlibsocket/api.h>
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
102 static uword
103 unformat_memif_queues (unformat_input_t * input, va_list * args)
104 {
105   u32 *rx_queues = va_arg (*args, u32 *);
106   u32 *tx_queues = va_arg (*args, u32 *);
107
108   if (unformat (input, "rx-queues %u", rx_queues))
109     ;
110   if (unformat (input, "tx-queues %u", tx_queues))
111     ;
112
113   return 1;
114 }
115
116 /* memif-create API */
117 static int
118 api_memif_create (vat_main_t * vam)
119 {
120   unformat_input_t *i = vam->input;
121   vl_api_memif_create_t *mp;
122   u32 id = 0;
123   u8 *socket_filename = 0;
124   u8 *secret = 0;
125   u8 role = 1;
126   u32 ring_size = 0;
127   u32 buffer_size = 0;
128   u8 hw_addr[6] = { 0 };
129   u32 rx_queues = MEMIF_DEFAULT_RX_QUEUES;
130   u32 tx_queues = MEMIF_DEFAULT_TX_QUEUES;
131   int ret;
132
133
134   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
135     {
136       if (unformat (i, "id %u", &id))
137         ;
138       else if (unformat (i, "socket %s", &socket_filename))
139         ;
140       else if (unformat (i, "secret %s", &secret))
141         ;
142       else if (unformat (i, "ring_size %u", &ring_size))
143         ;
144       else if (unformat (i, "buffer_size %u", &buffer_size))
145         ;
146       else if (unformat (i, "master"))
147         role = 0;
148       else if (unformat (i, "slave %U",
149                          unformat_memif_queues, &rx_queues, &tx_queues))
150         role = 1;
151       else if (unformat (i, "hw_addr %U", unformat_ethernet_address, hw_addr))
152         ;
153       else
154         {
155           clib_warning ("unknown input '%U'", format_unformat_error, i);
156           return -99;
157         }
158     }
159
160   if (!is_pow2 (ring_size))
161     {
162       errmsg ("ring size must be power of 2\n");
163       return -99;
164     }
165
166   if (rx_queues > 255 || rx_queues < 1)
167     {
168       errmsg ("rx queue must be between 1 - 255\n");
169       return -99;
170     }
171
172   if (tx_queues > 255 || tx_queues < 1)
173     {
174       errmsg ("tx queue must be between 1 - 255\n");
175       return -99;
176     }
177
178   M (MEMIF_CREATE, mp);
179
180   mp->id = clib_host_to_net_u32 (id);
181   mp->role = role;
182   mp->ring_size = clib_host_to_net_u32 (ring_size);
183   mp->buffer_size = clib_host_to_net_u16 (buffer_size & 0xffff);
184   if (socket_filename != 0)
185     {
186       strncpy ((char *) mp->socket_filename, (char *) socket_filename, 127);
187       vec_free (socket_filename);
188     }
189   if (secret != 0)
190     {
191       strncpy ((char *) mp->secret, (char *) secret, 16);
192       vec_free (secret);
193     }
194   memcpy (mp->hw_addr, hw_addr, 6);
195   mp->rx_queues = rx_queues;
196   mp->tx_queues = tx_queues;
197
198   S (mp);
199   W (ret);
200   return ret;
201 }
202
203 /* memif-create reply handler */
204 static void vl_api_memif_create_reply_t_handler
205   (vl_api_memif_create_reply_t * mp)
206 {
207   vat_main_t *vam = memif_test_main.vat_main;
208   i32 retval = ntohl (mp->retval);
209
210   if (retval == 0)
211     {
212       fformat (vam->ofp, "created memif with sw_if_index %d\n",
213                ntohl (mp->sw_if_index));
214     }
215
216   vam->retval = retval;
217   vam->result_ready = 1;
218 }
219
220 /* memif-delete API */
221 static int
222 api_memif_delete (vat_main_t * vam)
223 {
224   unformat_input_t *i = vam->input;
225   vl_api_memif_delete_t *mp;
226   u32 sw_if_index = 0;
227   u8 index_defined = 0;
228   int ret;
229
230   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
231     {
232       if (unformat (i, "sw_if_index %u", &sw_if_index))
233         index_defined = 1;
234       else
235         {
236           clib_warning ("unknown input '%U'", format_unformat_error, i);
237           return -99;
238         }
239     }
240
241   if (!index_defined)
242     {
243       errmsg ("missing sw_if_index\n");
244       return -99;
245     }
246
247   M (MEMIF_DELETE, mp);
248
249   mp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
250
251   S (mp);
252   W (ret);
253   return ret;
254 }
255
256 /* memif-dump API */
257 static int
258 api_memif_dump (vat_main_t * vam)
259 {
260   memif_test_main_t *mm = &memif_test_main;
261   vl_api_memif_dump_t *mp;
262   vl_api_control_ping_t *mp_ping;
263   int ret;
264
265   if (vam->json_output)
266     {
267       clib_warning ("JSON output not supported for memif_dump");
268       return -99;
269     }
270
271   M (MEMIF_DUMP, mp);
272   S (mp);
273
274   /* Use a control ping for synchronization */
275   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
276   mp_ping->_vl_msg_id = htons (mm->ping_id);
277   mp_ping->client_index = vam->my_client_index;
278
279   fformat (vam->ofp, "Sending ping id=%d\n", mm->ping_id);
280
281   vam->result_ready = 0;
282   S (mp_ping);
283
284   W (ret);
285   return ret;
286 }
287
288 /* memif-details message handler */
289 static void vl_api_memif_details_t_handler (vl_api_memif_details_t * mp)
290 {
291   vat_main_t *vam = memif_test_main.vat_main;
292
293   fformat (vam->ofp, "%s: sw_if_index %u mac %U\n"
294            "   id %u socket %s role %s\n"
295            "   ring_size %u buffer_size %u\n"
296            "   state %s link %s\n",
297            mp->if_name, ntohl (mp->sw_if_index), format_ethernet_address,
298            mp->hw_addr, clib_net_to_host_u32 (mp->id), mp->socket_filename,
299            mp->role ? "slave" : "master",
300            ntohl (mp->ring_size), ntohs (mp->buffer_size),
301            mp->admin_up_down ? "up" : "down",
302            mp->link_up_down ? "up" : "down");
303 }
304
305 /*
306  * List of messages that the api test plugin sends,
307  * and that the data plane plugin processes
308  */
309 #define foreach_vpe_api_msg                                       \
310 _(memif_create, "[id <id>] [socket <path>] [ring_size <size>] " \
311                 "[buffer_size <size>] [hw_addr <mac_address>] "   \
312                 "<master|slave>")                                 \
313 _(memif_delete, "<sw_if_index>")                                  \
314 _(memif_dump, "")
315
316 static void
317 memif_vat_api_hookup (vat_main_t * vam)
318 {
319   memif_test_main_t *mm __attribute__ ((unused)) = &memif_test_main;
320   /* Hook up handlers for replies from the data plane plug-in */
321 #define _(N,n)                                                  \
322   vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base),       \
323                           #n,                                   \
324                           vl_api_##n##_t_handler,               \
325                           vl_noop_handler,                      \
326                           vl_api_##n##_t_endian,                \
327                           vl_api_##n##_t_print,                 \
328                           sizeof(vl_api_##n##_t), 1);
329   foreach_vpe_api_reply_msg;
330 #undef _
331
332   /* API messages we can send */
333 #define _(n,h)                                          \
334   hash_set_mem (vam->function_by_name, #n, api_##n);
335   foreach_vpe_api_msg;
336 #undef _
337
338   /* Help strings */
339 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
340   foreach_vpe_api_msg;
341 #undef _
342 }
343
344 clib_error_t *
345 vat_plugin_register (vat_main_t * vam)
346 {
347   memif_test_main_t *mm = &memif_test_main;
348   u8 *name;
349
350   mm->vat_main = vam;
351
352   /* Ask the vpp engine for the first assigned message-id */
353   name = format (0, "memif_%08x%c", api_version, 0);
354   mm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
355
356   /* Get the control ping ID */
357 #define _(id,n,crc) \
358   const char *id ## _CRC __attribute__ ((unused)) = #n "_" #crc;
359   foreach_vl_msg_name_crc_vpe;
360 #undef _
361   mm->ping_id = vl_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
362
363   if (mm->msg_id_base != (u16) ~0)
364     memif_vat_api_hookup (vam);
365
366   vec_free (name);
367
368   return 0;
369 }