Add support for memif API to VAT.
[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
28 #define __plugin_msg_base memif_test_main.msg_id_base
29 #include <vlibapi/vat_helper_macros.h>
30
31 /* declare message IDs */
32 #include <memif/memif_msg_enum.h>
33
34 /* Get CRC codes of the messages defined outside of this plugin */
35 #define vl_msg_name_crc_list
36 #include <vpp/api/vpe_all_api_h.h>
37 #undef vl_msg_name_crc_list
38
39 /* define message structures */
40 #define vl_typedefs
41 #include <vpp/api/vpe_all_api_h.h>
42 #include <memif/memif_all_api_h.h>
43 #undef vl_typedefs
44
45 /* declare message handlers for each api */
46
47 #define vl_endianfun            /* define message structures */
48 #include <memif/memif_all_api_h.h>
49 #undef vl_endianfun
50
51 /* instantiate all the print functions we know about */
52 #define vl_print(handle, ...)
53 #define vl_printfun
54 #include <memif/memif_all_api_h.h>
55 #undef vl_printfun
56
57 /* Get the API version number. */
58 #define vl_api_version(n,v) static u32 api_version=(v);
59 #include <memif/memif_all_api_h.h>
60 #undef vl_api_version
61
62 typedef struct
63 {
64   /* API message ID base */
65   u16 msg_id_base;
66   u32 ping_id;
67   vat_main_t *vat_main;
68 } memif_test_main_t;
69
70 memif_test_main_t memif_test_main;
71
72 /* standard reply handlers */
73 #define foreach_standard_reply_retval_handler           \
74 _(memif_delete_reply)
75
76 #define _(n)                                            \
77     static void vl_api_##n##_t_handler                  \
78     (vl_api_##n##_t * mp)                               \
79     {                                                   \
80         vat_main_t * vam = memif_test_main.vat_main;    \
81         i32 retval = ntohl(mp->retval);                 \
82         if (vam->async_mode) {                          \
83             vam->async_errors += (retval < 0);          \
84         } else {                                        \
85             vam->retval = retval;                       \
86             vam->result_ready = 1;                      \
87         }                                               \
88     }
89 foreach_standard_reply_retval_handler;
90 #undef _
91
92 /*
93  * Table of message reply handlers, must include boilerplate handlers
94  * we just generated
95  */
96 #define foreach_vpe_api_reply_msg                       \
97 _(MEMIF_CREATE_REPLY, memif_create_reply)               \
98 _(MEMIF_DELETE_REPLY, memif_delete_reply)               \
99 _(MEMIF_DETAILS, memif_details)
100
101 static uword
102 unformat_memif_queues (unformat_input_t * input, va_list * args)
103 {
104   u32 *rx_queues = va_arg (*args, u32 *);
105   u32 *tx_queues = va_arg (*args, u32 *);
106
107   if (unformat (input, "rx-queues %u", rx_queues))
108     ;
109   if (unformat (input, "tx-queues %u", tx_queues))
110     ;
111
112   return 1;
113 }
114
115 /* memif-create API */
116 static int
117 api_memif_create (vat_main_t * vam)
118 {
119   unformat_input_t *i = vam->input;
120   vl_api_memif_create_t *mp;
121   u64 key = 0;
122   u8 *socket_filename = 0;
123   u8 role = 1;
124   u32 ring_size = 0;
125   u32 buffer_size = 0;
126   u8 hw_addr[6] = { 0 };
127   u32 rx_queues = MEMIF_DEFAULT_RX_QUEUES;
128   u32 tx_queues = MEMIF_DEFAULT_TX_QUEUES;
129   int ret;
130
131
132   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
133     {
134       if (unformat (i, "key 0x%" PRIx64, &key))
135         ;
136       else if (unformat (i, "socket %s", &socket_filename))
137         ;
138       else if (unformat (i, "ring_size %u", &ring_size))
139         ;
140       else if (unformat (i, "buffer_size %u", &buffer_size))
141         ;
142       else if (unformat (i, "master"))
143         role = 0;
144       else if (unformat (i, "slave %U",
145                          unformat_memif_queues, &rx_queues, &tx_queues))
146         role = 1;
147       else if (unformat (i, "hw_addr %U", unformat_ethernet_address, hw_addr))
148         ;
149       else
150         {
151           clib_warning ("unknown input '%U'", format_unformat_error, i);
152           return -99;
153         }
154     }
155
156   if (!is_pow2 (ring_size))
157     {
158       errmsg ("ring size must be power of 2\n");
159       return -99;
160     }
161
162   if (rx_queues > 255 || rx_queues < 1)
163     {
164       errmsg ("rx queue must be between 1 - 255\n");
165       return -99;
166     }
167
168   if (tx_queues > 255 || tx_queues < 1)
169     {
170       errmsg ("tx queue must be between 1 - 255\n");
171       return -99;
172     }
173
174   M (MEMIF_CREATE, mp);
175
176   mp->key = clib_host_to_net_u64 (key);
177   mp->role = role;
178   mp->ring_size = clib_host_to_net_u32 (ring_size);
179   mp->buffer_size = clib_host_to_net_u16 (buffer_size & 0xffff);
180   if (socket_filename != 0)
181     {
182       strncpy ((char *) mp->socket_filename, (char *) socket_filename, 127);
183       vec_free (socket_filename);
184     }
185   memcpy (mp->hw_addr, hw_addr, 6);
186   mp->rx_queues = rx_queues;
187   mp->tx_queues = tx_queues;
188
189   S (mp);
190   W (ret);
191   return ret;
192 }
193
194 /* memif-create reply handler */
195 static void vl_api_memif_create_reply_t_handler
196   (vl_api_memif_create_reply_t * mp)
197 {
198   vat_main_t *vam = memif_test_main.vat_main;
199   i32 retval = ntohl (mp->retval);
200
201   if (retval == 0)
202     {
203       fformat (vam->ofp, "created memif with sw_if_index %d\n",
204                ntohl (mp->sw_if_index));
205     }
206
207   vam->retval = retval;
208   vam->result_ready = 1;
209 }
210
211 /* memif-delete API */
212 static int
213 api_memif_delete (vat_main_t * vam)
214 {
215   unformat_input_t *i = vam->input;
216   vl_api_memif_delete_t *mp;
217   u32 sw_if_index = 0;
218   u8 index_defined = 0;
219   int ret;
220
221   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
222     {
223       if (unformat (i, "sw_if_index %u", &sw_if_index))
224         index_defined = 1;
225       else
226         {
227           clib_warning ("unknown input '%U'", format_unformat_error, i);
228           return -99;
229         }
230     }
231
232   if (!index_defined)
233     {
234       errmsg ("missing sw_if_index\n");
235       return -99;
236     }
237
238   M (MEMIF_DELETE, mp);
239
240   mp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
241
242   S (mp);
243   W (ret);
244   return ret;
245 }
246
247 /* memif-dump API */
248 static int
249 api_memif_dump (vat_main_t * vam)
250 {
251   memif_test_main_t *mm = &memif_test_main;
252   vl_api_memif_dump_t *mp;
253   vl_api_control_ping_t *mp_ping;
254   int ret;
255
256   if (vam->json_output)
257     {
258       clib_warning ("JSON output not supported for memif_dump");
259       return -99;
260     }
261
262   M (MEMIF_DUMP, mp);
263   S (mp);
264
265   /* Use a control ping for synchronization */
266   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
267   mp_ping->_vl_msg_id = htons (mm->ping_id);
268   mp_ping->client_index = vam->my_client_index;
269
270   fformat (vam->ofp, "Sending ping id=%d\n", mm->ping_id);
271
272   vam->result_ready = 0;
273   S (mp_ping);
274
275   W (ret);
276   return ret;
277 }
278
279 /* memif-details message handler */
280 static void vl_api_memif_details_t_handler (vl_api_memif_details_t * mp)
281 {
282   vat_main_t *vam = memif_test_main.vat_main;
283
284   fformat (vam->ofp, "%s: sw_if_index %u mac %U\n"
285            "   key 0x%" PRIx64 " socket %s role %s\n"
286            "   ring_size %u buffer_size %u\n"
287            "   state %s link %s\n",
288            mp->if_name, ntohl (mp->sw_if_index), format_ethernet_address,
289            mp->hw_addr, clib_net_to_host_u64 (mp->key), mp->socket_filename,
290            mp->role ? "slave" : "master",
291            ntohl (mp->ring_size), ntohs (mp->buffer_size),
292            mp->admin_up_down ? "up" : "down",
293            mp->link_up_down ? "up" : "down");
294 }
295
296 /*
297  * List of messages that the api test plugin sends,
298  * and that the data plane plugin processes
299  */
300 #define foreach_vpe_api_msg                                       \
301 _(memif_create, "[key <key>] [socket <path>] [ring_size <size>] " \
302                 "[buffer_size <size>] [hw_addr <mac_address>] "   \
303                 "<master|slave>")                                 \
304 _(memif_delete, "<sw_if_index>")                                  \
305 _(memif_dump, "")
306
307 static void
308 memif_vat_api_hookup (vat_main_t * vam)
309 {
310   memif_test_main_t *mm __attribute__ ((unused)) = &memif_test_main;
311   /* Hook up handlers for replies from the data plane plug-in */
312 #define _(N,n)                                                  \
313   vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base),       \
314                           #n,                                   \
315                           vl_api_##n##_t_handler,               \
316                           vl_noop_handler,                      \
317                           vl_api_##n##_t_endian,                \
318                           vl_api_##n##_t_print,                 \
319                           sizeof(vl_api_##n##_t), 1);
320   foreach_vpe_api_reply_msg;
321 #undef _
322
323   /* API messages we can send */
324 #define _(n,h)                                          \
325   hash_set_mem (vam->function_by_name, #n, api_##n);
326   foreach_vpe_api_msg;
327 #undef _
328
329   /* Help strings */
330 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
331   foreach_vpe_api_msg;
332 #undef _
333 }
334
335 clib_error_t *
336 vat_plugin_register (vat_main_t * vam)
337 {
338   memif_test_main_t *mm = &memif_test_main;
339   u8 *name;
340
341   mm->vat_main = vam;
342
343   /* Ask the vpp engine for the first assigned message-id */
344   name = format (0, "memif_%08x%c", api_version, 0);
345   mm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
346
347   /* Get the control ping ID */
348 #define _(id,n,crc) \
349   const char *id ## _CRC __attribute__ ((unused)) = #n "_" #crc;
350   foreach_vl_msg_name_crc_vpe;
351 #undef _
352   mm->ping_id = vl_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
353
354   if (mm->msg_id_base != (u16) ~0)
355     memif_vat_api_hookup (vam);
356
357   vec_free (name);
358
359   return 0;
360 }