misc: api move continued
[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 reply handler */
125 #define VL_API_MEMIF_SOCKET_FILENAME_ADD_DEL_REPLY_T_HANDLER
126 static void vl_api_memif_socket_filename_add_del_reply_t_handler
127   (vl_api_memif_socket_filename_add_del_reply_t * mp)
128 {
129   vat_main_t *vam = memif_test_main.vat_main;
130   i32 retval = ntohl (mp->retval);
131
132   vam->retval = retval;
133   vam->result_ready = 1;
134   vam->regenerate_interface_table = 1;
135 }
136
137 /* memif-create API */
138 static int
139 api_memif_create (vat_main_t * vam)
140 {
141   unformat_input_t *i = vam->input;
142   vl_api_memif_create_t *mp;
143   u32 id = 0;
144   u32 socket_id = 0;
145   u8 *secret = 0;
146   u8 role = 1;
147   u32 ring_size = 0;
148   u32 buffer_size = 0;
149   u8 hw_addr[6] = { 0 };
150   u32 rx_queues = MEMIF_DEFAULT_RX_QUEUES;
151   u32 tx_queues = MEMIF_DEFAULT_TX_QUEUES;
152   int ret;
153   u8 mode = MEMIF_INTERFACE_MODE_ETHERNET;
154
155   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
156     {
157       if (unformat (i, "id %u", &id))
158         ;
159       else if (unformat (i, "socket-id %u", &socket_id))
160         ;
161       else if (unformat (i, "secret %s", &secret))
162         ;
163       else if (unformat (i, "ring_size %u", &ring_size))
164         ;
165       else if (unformat (i, "buffer_size %u", &buffer_size))
166         ;
167       else if (unformat (i, "master"))
168         role = 0;
169       else if (unformat (i, "slave %U",
170                          unformat_memif_queues, &rx_queues, &tx_queues))
171         role = 1;
172       else if (unformat (i, "mode ip"))
173         mode = MEMIF_INTERFACE_MODE_IP;
174       else if (unformat (i, "hw_addr %U", unformat_ethernet_address, hw_addr))
175         ;
176       else
177         {
178           clib_warning ("unknown input '%U'", format_unformat_error, i);
179           return -99;
180         }
181     }
182
183   if (socket_id == ~0)
184     {
185       errmsg ("invalid socket-id\n");
186       return -99;
187     }
188
189   if (!is_pow2 (ring_size))
190     {
191       errmsg ("ring size must be power of 2\n");
192       return -99;
193     }
194
195   if (rx_queues > 255 || rx_queues < 1)
196     {
197       errmsg ("rx queue must be between 1 - 255\n");
198       return -99;
199     }
200
201   if (tx_queues > 255 || tx_queues < 1)
202     {
203       errmsg ("tx queue must be between 1 - 255\n");
204       return -99;
205     }
206
207   M2 (MEMIF_CREATE, mp, strlen ((char *) secret));
208
209   mp->mode = mode;
210   mp->id = clib_host_to_net_u32 (id);
211   mp->role = role;
212   mp->ring_size = clib_host_to_net_u32 (ring_size);
213   mp->buffer_size = clib_host_to_net_u16 (buffer_size & 0xffff);
214   mp->socket_id = clib_host_to_net_u32 (socket_id);
215   if (secret != 0)
216     {
217       char *p = (char *) &mp->secret;
218       p += vl_api_vec_to_api_string (secret, (vl_api_string_t *) p);
219       vec_free (secret);
220     }
221   memcpy (mp->hw_addr, hw_addr, 6);
222   mp->rx_queues = rx_queues;
223   mp->tx_queues = tx_queues;
224
225   S (mp);
226   W (ret);
227   return ret;
228 }
229
230 /* memif-create reply handler */
231 static void vl_api_memif_create_reply_t_handler
232   (vl_api_memif_create_reply_t * mp)
233 {
234   vat_main_t *vam = memif_test_main.vat_main;
235   i32 retval = ntohl (mp->retval);
236
237   if (retval == 0)
238     {
239       fformat (vam->ofp, "created memif with sw_if_index %d\n",
240                ntohl (mp->sw_if_index));
241     }
242
243   vam->retval = retval;
244   vam->result_ready = 1;
245   vam->regenerate_interface_table = 1;
246 }
247
248 /* memif-delete API */
249 static int
250 api_memif_delete (vat_main_t * vam)
251 {
252   unformat_input_t *i = vam->input;
253   vl_api_memif_delete_t *mp;
254   u32 sw_if_index = 0;
255   u8 index_defined = 0;
256   int ret;
257
258   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
259     {
260       if (unformat (i, "sw_if_index %u", &sw_if_index))
261         index_defined = 1;
262       else
263         {
264           clib_warning ("unknown input '%U'", format_unformat_error, i);
265           return -99;
266         }
267     }
268
269   if (!index_defined)
270     {
271       errmsg ("missing sw_if_index\n");
272       return -99;
273     }
274
275   M (MEMIF_DELETE, mp);
276
277   mp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
278
279   S (mp);
280   W (ret);
281   return ret;
282 }
283
284 /* memif-dump API */
285 static int
286 api_memif_dump (vat_main_t * vam)
287 {
288   memif_test_main_t *mm = &memif_test_main;
289   vl_api_memif_dump_t *mp;
290   vl_api_control_ping_t *mp_ping;
291   int ret;
292
293   if (vam->json_output)
294     {
295       clib_warning ("JSON output not supported for memif_dump");
296       return -99;
297     }
298
299   M (MEMIF_DUMP, mp);
300   S (mp);
301
302   /* Use a control ping for synchronization */
303   if (!mm->ping_id)
304     mm->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
305   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
306   mp_ping->_vl_msg_id = htons (mm->ping_id);
307   mp_ping->client_index = vam->my_client_index;
308
309   fformat (vam->ofp, "Sending ping id=%d\n", mm->ping_id);
310
311   vam->result_ready = 0;
312   S (mp_ping);
313
314   W (ret);
315   return ret;
316 }
317
318 /* memif-details message handler */
319 static void
320 vl_api_memif_details_t_handler (vl_api_memif_details_t * mp)
321 {
322   vat_main_t *vam = memif_test_main.vat_main;
323
324   fformat (vam->ofp, "%s: sw_if_index %u mac %U\n"
325            "   id %u socket-id %u role %s\n"
326            "   ring_size %u buffer_size %u\n"
327            "   state %s link %s\n",
328            mp->if_name, ntohl (mp->sw_if_index), format_ethernet_address,
329            mp->hw_addr, clib_net_to_host_u32 (mp->id),
330            clib_net_to_host_u32 (mp->socket_id),
331            mp->role ? "slave" : "master",
332            ntohl (mp->ring_size), ntohs (mp->buffer_size),
333            (mp->flags & IF_STATUS_API_FLAG_ADMIN_UP) ? "up" : "down",
334            (mp->flags & IF_STATUS_API_FLAG_LINK_UP) ? "up" : "down");
335 }
336
337 /* memif_socket_filename_dump API */
338 static int
339 api_memif_socket_filename_dump (vat_main_t * vam)
340 {
341   memif_test_main_t *mm = &memif_test_main;
342   vl_api_memif_socket_filename_dump_t *mp;
343   vl_api_control_ping_t *mp_ping;
344   int ret;
345
346   if (vam->json_output)
347     {
348       clib_warning
349         ("JSON output not supported for memif_socket_filename_dump");
350       return -99;
351     }
352
353   M (MEMIF_SOCKET_FILENAME_DUMP, mp);
354   S (mp);
355
356   /* Use a control ping for synchronization */
357   if (!mm->ping_id)
358     mm->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
359   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
360   mp_ping->_vl_msg_id = htons (mm->ping_id);
361   mp_ping->client_index = vam->my_client_index;
362
363   fformat (vam->ofp, "Sending ping id=%d\n", mm->ping_id);
364
365   vam->result_ready = 0;
366   S (mp_ping);
367
368   W (ret);
369   return ret;
370 }
371
372 /* memif_socket_format_details message handler */
373 static void vl_api_memif_socket_filename_details_t_handler
374   (vl_api_memif_socket_filename_details_t * mp)
375 {
376   vat_main_t *vam = memif_test_main.vat_main;
377
378   fformat (vam->ofp,
379            "id %u : filename %s\n",
380            ntohl (mp->socket_id), mp->socket_filename);
381 }
382
383 #include <memif/memif.api_test.c>
384
385 /*
386  * fd.io coding-style-patch-verification: ON
387  *
388  * Local Variables:
389  * eval: (c-set-style "gnu")
390  * End:
391  */