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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <vlibapi/api.h>
22 #include <vlibmemory/api.h>
24 #include <vppinfra/error.h>
25 #include <vnet/ip/ip.h>
26 #include <memif/memif.h>
27 #include <memif/private.h>
29 #define __plugin_msg_base memif_test_main.msg_id_base
30 #include <vlibapi/vat_helper_macros.h>
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>
40 /* API message ID base */
46 memif_test_main_t memif_test_main;
49 unformat_memif_queues (unformat_input_t * input, va_list * args)
51 u32 *rx_queues = va_arg (*args, u32 *);
52 u32 *tx_queues = va_arg (*args, u32 *);
54 if (unformat (input, "rx-queues %u", rx_queues))
56 if (unformat (input, "tx-queues %u", tx_queues))
62 /* memif_socket_filename_add_del API */
64 api_memif_socket_filename_add_del (vat_main_t * vam)
66 unformat_input_t *i = vam->input;
67 vl_api_memif_socket_filename_add_del_t *mp;
77 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
79 if (unformat (i, "id %u", &socket_id))
81 else if (unformat (i, "filename %s", &socket_filename))
83 else if (unformat (i, "del"))
85 else if (unformat (i, "add"))
89 vec_free (socket_filename);
90 clib_warning ("unknown input `%U'", format_unformat_error, i);
95 if (socket_id == 0 || socket_id == ~0)
97 vec_free (socket_filename);
98 errmsg ("Invalid socket id");
102 if (is_add && (!socket_filename || *socket_filename == 0))
104 vec_free (socket_filename);
105 errmsg ("Invalid socket filename");
109 M2 (MEMIF_SOCKET_FILENAME_ADD_DEL, mp, strlen ((char *) socket_filename));
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);
116 vec_free (socket_filename);
124 /* memif_socket_filename_add_del API */
126 api_memif_socket_filename_add_del_v2 (vat_main_t *vam)
128 unformat_input_t *i = vam->input;
129 vl_api_memif_socket_filename_add_del_v2_t *mp;
139 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
141 if (unformat (i, "id %u", &socket_id))
143 else if (unformat (i, "filename %s", &socket_filename))
145 else if (unformat (i, "del"))
147 else if (unformat (i, "add"))
151 vec_free (socket_filename);
152 clib_warning ("unknown input `%U'", format_unformat_error, i);
157 if (socket_id == 0 || socket_id == ~0)
159 vec_free (socket_filename);
160 errmsg ("Invalid socket id");
164 if (is_add && (!socket_filename || *socket_filename == 0))
166 vec_free (socket_filename);
167 errmsg ("Invalid socket filename");
171 M2 (MEMIF_SOCKET_FILENAME_ADD_DEL_V2, mp, strlen ((char *) socket_filename));
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);
178 vec_free (socket_filename);
186 /* memif socket-create reply handler */
188 vl_api_memif_socket_filename_add_del_v2_reply_t_handler (
189 vl_api_memif_socket_filename_add_del_v2_reply_t *mp)
191 vat_main_t *vam = memif_test_main.vat_main;
192 i32 retval = ntohl (mp->retval);
196 fformat (vam->ofp, "created memif socket with socket_id %d\n",
197 ntohl (mp->socket_id));
200 vam->retval = retval;
201 vam->result_ready = 1;
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)
209 vat_main_t *vam = memif_test_main.vat_main;
210 i32 retval = ntohl (mp->retval);
212 vam->retval = retval;
213 vam->result_ready = 1;
214 vam->regenerate_interface_table = 1;
217 /* memif-create API */
219 api_memif_create (vat_main_t * vam)
221 unformat_input_t *i = vam->input;
222 vl_api_memif_create_t *mp;
229 u8 hw_addr[6] = { 0 };
230 u32 rx_queues = MEMIF_DEFAULT_RX_QUEUES;
231 u32 tx_queues = MEMIF_DEFAULT_TX_QUEUES;
233 u8 mode = MEMIF_INTERFACE_MODE_ETHERNET;
235 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
237 if (unformat (i, "id %u", &id))
239 else if (unformat (i, "socket-id %u", &socket_id))
241 else if (unformat (i, "secret %s", &secret))
243 else if (unformat (i, "ring_size %u", &ring_size))
245 else if (unformat (i, "buffer_size %u", &buffer_size))
247 else if (unformat (i, "master"))
249 else if (unformat (i, "slave %U",
250 unformat_memif_queues, &rx_queues, &tx_queues))
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))
258 clib_warning ("unknown input '%U'", format_unformat_error, i);
265 errmsg ("invalid socket-id\n");
269 if (!is_pow2 (ring_size))
271 errmsg ("ring size must be power of 2\n");
275 if (rx_queues > 255 || rx_queues < 1)
277 errmsg ("rx queue must be between 1 - 255\n");
281 if (tx_queues > 255 || tx_queues < 1)
283 errmsg ("tx queue must be between 1 - 255\n");
287 M2 (MEMIF_CREATE, mp, strlen ((char *) secret));
290 mp->id = clib_host_to_net_u32 (id);
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);
297 char *p = (char *) &mp->secret;
298 p += vl_api_vec_to_api_string (secret, (vl_api_string_t *) p);
301 memcpy (mp->hw_addr, hw_addr, 6);
302 mp->rx_queues = rx_queues;
303 mp->tx_queues = tx_queues;
310 /* memif-create reply handler */
311 static void vl_api_memif_create_reply_t_handler
312 (vl_api_memif_create_reply_t * mp)
314 vat_main_t *vam = memif_test_main.vat_main;
315 i32 retval = ntohl (mp->retval);
319 fformat (vam->ofp, "created memif with sw_if_index %d\n",
320 ntohl (mp->sw_if_index));
323 vam->retval = retval;
324 vam->result_ready = 1;
325 vam->regenerate_interface_table = 1;
328 /* memif-create_v2 API */
330 api_memif_create_v2 (vat_main_t *vam)
332 unformat_input_t *i = vam->input;
333 vl_api_memif_create_v2_t *mp;
341 u8 hw_addr[6] = { 0 };
342 u32 rx_queues = MEMIF_DEFAULT_RX_QUEUES;
343 u32 tx_queues = MEMIF_DEFAULT_TX_QUEUES;
345 u8 mode = MEMIF_INTERFACE_MODE_ETHERNET;
347 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
349 if (unformat (i, "id %u", &id))
351 else if (unformat (i, "socket-id %u", &socket_id))
353 else if (unformat (i, "secret %s", &secret))
355 else if (unformat (i, "ring_size %u", &ring_size))
357 else if (unformat (i, "buffer_size %u", &buffer_size))
359 else if (unformat (i, "master"))
361 else if (unformat (i, "use_dma %u", &use_dma))
363 else if (unformat (i, "slave %U", unformat_memif_queues, &rx_queues,
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))
372 clib_warning ("unknown input '%U'", format_unformat_error, i);
379 errmsg ("invalid socket-id\n");
383 if (!is_pow2 (ring_size))
385 errmsg ("ring size must be power of 2\n");
389 if (rx_queues > 255 || rx_queues < 1)
391 errmsg ("rx queue must be between 1 - 255\n");
395 if (tx_queues > 255 || tx_queues < 1)
397 errmsg ("tx queue must be between 1 - 255\n");
401 M2 (MEMIF_CREATE, mp, strlen ((char *) secret));
404 mp->id = clib_host_to_net_u32 (id);
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);
411 char *p = (char *) &mp->secret;
412 p += vl_api_vec_to_api_string (secret, (vl_api_string_t *) p);
415 memcpy (mp->hw_addr, hw_addr, 6);
416 mp->rx_queues = rx_queues;
417 mp->tx_queues = tx_queues;
424 /* memif-create_v2 reply handler */
426 vl_api_memif_create_v2_reply_t_handler (vl_api_memif_create_reply_t *mp)
428 vat_main_t *vam = memif_test_main.vat_main;
429 i32 retval = ntohl (mp->retval);
433 fformat (vam->ofp, "created memif with sw_if_index %d\n",
434 ntohl (mp->sw_if_index));
437 vam->retval = retval;
438 vam->result_ready = 1;
439 vam->regenerate_interface_table = 1;
442 /* memif-delete API */
444 api_memif_delete (vat_main_t * vam)
446 unformat_input_t *i = vam->input;
447 vl_api_memif_delete_t *mp;
449 u8 index_defined = 0;
452 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
454 if (unformat (i, "sw_if_index %u", &sw_if_index))
458 clib_warning ("unknown input '%U'", format_unformat_error, i);
465 errmsg ("missing sw_if_index\n");
469 M (MEMIF_DELETE, mp);
471 mp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
480 api_memif_dump (vat_main_t * vam)
482 memif_test_main_t *mm = &memif_test_main;
483 vl_api_memif_dump_t *mp;
484 vl_api_control_ping_t *mp_ping;
487 if (vam->json_output)
489 clib_warning ("JSON output not supported for memif_dump");
496 /* Use a control ping for synchronization */
498 mm->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
499 mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
500 mp_ping->_vl_msg_id = htons (mm->ping_id);
501 mp_ping->client_index = vam->my_client_index;
503 fformat (vam->ofp, "Sending ping id=%d\n", mm->ping_id);
505 vam->result_ready = 0;
512 /* memif-details message handler */
514 vl_api_memif_details_t_handler (vl_api_memif_details_t * mp)
516 vat_main_t *vam = memif_test_main.vat_main;
518 fformat (vam->ofp, "%s: sw_if_index %u mac %U\n"
519 " id %u socket-id %u role %s\n"
520 " ring_size %u buffer_size %u\n"
521 " state %s link %s\n",
522 mp->if_name, ntohl (mp->sw_if_index), format_ethernet_address,
523 mp->hw_addr, clib_net_to_host_u32 (mp->id),
524 clib_net_to_host_u32 (mp->socket_id),
525 mp->role ? "slave" : "master",
526 ntohl (mp->ring_size), ntohs (mp->buffer_size),
527 (mp->flags & IF_STATUS_API_FLAG_ADMIN_UP) ? "up" : "down",
528 (mp->flags & IF_STATUS_API_FLAG_LINK_UP) ? "up" : "down");
531 /* memif_socket_filename_dump API */
533 api_memif_socket_filename_dump (vat_main_t * vam)
535 memif_test_main_t *mm = &memif_test_main;
536 vl_api_memif_socket_filename_dump_t *mp;
537 vl_api_control_ping_t *mp_ping;
540 if (vam->json_output)
543 ("JSON output not supported for memif_socket_filename_dump");
547 M (MEMIF_SOCKET_FILENAME_DUMP, mp);
550 /* Use a control ping for synchronization */
552 mm->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
553 mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
554 mp_ping->_vl_msg_id = htons (mm->ping_id);
555 mp_ping->client_index = vam->my_client_index;
557 fformat (vam->ofp, "Sending ping id=%d\n", mm->ping_id);
559 vam->result_ready = 0;
566 /* memif_socket_format_details message handler */
567 static void vl_api_memif_socket_filename_details_t_handler
568 (vl_api_memif_socket_filename_details_t * mp)
570 vat_main_t *vam = memif_test_main.vat_main;
573 "id %u : filename %s\n",
574 ntohl (mp->socket_id), mp->socket_filename);
577 #include <memif/memif.api_test.c>
580 * fd.io coding-style-patch-verification: ON
583 * eval: (c-set-style "gnu")