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 <memif/memif_msg_enum.h>
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
40 /* define message structures */
42 #include <vpp/api/vpe_all_api_h.h>
43 #include <memif/memif_all_api_h.h>
46 /* declare message handlers for each api */
48 #define vl_endianfun /* define message structures */
49 #include <memif/memif_all_api_h.h>
52 /* instantiate all the print functions we know about */
53 #define vl_print(handle, ...)
55 #include <memif/memif_all_api_h.h>
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>
65 /* API message ID base */
71 memif_test_main_t memif_test_main;
73 /* standard reply handlers */
74 #define foreach_standard_reply_retval_handler \
78 static void vl_api_##n##_t_handler \
79 (vl_api_##n##_t * mp) \
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); \
86 vam->retval = retval; \
87 vam->result_ready = 1; \
90 foreach_standard_reply_retval_handler;
94 * Table of message reply handlers, must include boilerplate handlers
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 _(MEMIF_SOCKET_FILENAME_DETAILS, memif_socket_filename_details) \
102 _(MEMIF_SOCKET_FILENAME_ADD_DEL_REPLY, memif_socket_filename_add_del_reply)
105 unformat_memif_queues (unformat_input_t * input, va_list * args)
107 u32 *rx_queues = va_arg (*args, u32 *);
108 u32 *tx_queues = va_arg (*args, u32 *);
110 if (unformat (input, "rx-queues %u", rx_queues))
112 if (unformat (input, "tx-queues %u", tx_queues))
118 /* memif_socket_filename_add_del API */
120 api_memif_socket_filename_add_del (vat_main_t * vam)
122 unformat_input_t *i = vam->input;
123 vl_api_memif_socket_filename_add_del_t *mp;
133 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
135 if (unformat (i, "id %u", &socket_id))
137 else if (unformat (i, "filename %s", &socket_filename))
139 else if (unformat (i, "del"))
141 else if (unformat (i, "add"))
145 vec_free (socket_filename);
146 clib_warning ("unknown input `%U'", format_unformat_error, i);
151 if (socket_id == 0 || socket_id == ~0)
153 vec_free (socket_filename);
154 errmsg ("Invalid socket id");
158 if (is_add && (!socket_filename || *socket_filename == 0))
160 vec_free (socket_filename);
161 errmsg ("Invalid socket filename");
165 M (MEMIF_SOCKET_FILENAME_ADD_DEL, mp);
168 mp->socket_id = htonl (socket_id);
169 strncpy ((char *) mp->socket_filename,
170 (char *) socket_filename, sizeof (mp->socket_filename) - 1);
172 vec_free (socket_filename);
180 /* memif_socket_filename_add_del reply handler */
181 static void vl_api_memif_socket_filename_add_del_reply_t_handler
182 (vl_api_memif_socket_filename_add_del_reply_t * mp)
184 vat_main_t *vam = memif_test_main.vat_main;
185 i32 retval = ntohl (mp->retval);
187 vam->retval = retval;
188 vam->result_ready = 1;
189 vam->regenerate_interface_table = 1;
192 /* memif-create API */
194 api_memif_create (vat_main_t * vam)
196 unformat_input_t *i = vam->input;
197 vl_api_memif_create_t *mp;
204 u8 hw_addr[6] = { 0 };
205 u32 rx_queues = MEMIF_DEFAULT_RX_QUEUES;
206 u32 tx_queues = MEMIF_DEFAULT_TX_QUEUES;
208 u8 mode = MEMIF_INTERFACE_MODE_ETHERNET;
210 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
212 if (unformat (i, "id %u", &id))
214 else if (unformat (i, "socket-id %u", &socket_id))
216 else if (unformat (i, "secret %s", &secret))
218 else if (unformat (i, "ring_size %u", &ring_size))
220 else if (unformat (i, "buffer_size %u", &buffer_size))
222 else if (unformat (i, "master"))
224 else if (unformat (i, "slave %U",
225 unformat_memif_queues, &rx_queues, &tx_queues))
227 else if (unformat (i, "mode ip"))
228 mode = MEMIF_INTERFACE_MODE_IP;
229 else if (unformat (i, "hw_addr %U", unformat_ethernet_address, hw_addr))
233 clib_warning ("unknown input '%U'", format_unformat_error, i);
240 errmsg ("invalid socket-id\n");
244 if (!is_pow2 (ring_size))
246 errmsg ("ring size must be power of 2\n");
250 if (rx_queues > 255 || rx_queues < 1)
252 errmsg ("rx queue must be between 1 - 255\n");
256 if (tx_queues > 255 || tx_queues < 1)
258 errmsg ("tx queue must be between 1 - 255\n");
262 M (MEMIF_CREATE, mp);
265 mp->id = clib_host_to_net_u32 (id);
267 mp->ring_size = clib_host_to_net_u32 (ring_size);
268 mp->buffer_size = clib_host_to_net_u16 (buffer_size & 0xffff);
269 mp->socket_id = clib_host_to_net_u32 (socket_id);
272 strncpy ((char *) mp->secret, (char *) secret, 16);
275 memcpy (mp->hw_addr, hw_addr, 6);
276 mp->rx_queues = rx_queues;
277 mp->tx_queues = tx_queues;
284 /* memif-create reply handler */
285 static void vl_api_memif_create_reply_t_handler
286 (vl_api_memif_create_reply_t * mp)
288 vat_main_t *vam = memif_test_main.vat_main;
289 i32 retval = ntohl (mp->retval);
293 fformat (vam->ofp, "created memif with sw_if_index %d\n",
294 ntohl (mp->sw_if_index));
297 vam->retval = retval;
298 vam->result_ready = 1;
299 vam->regenerate_interface_table = 1;
302 /* memif-delete API */
304 api_memif_delete (vat_main_t * vam)
306 unformat_input_t *i = vam->input;
307 vl_api_memif_delete_t *mp;
309 u8 index_defined = 0;
312 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
314 if (unformat (i, "sw_if_index %u", &sw_if_index))
318 clib_warning ("unknown input '%U'", format_unformat_error, i);
325 errmsg ("missing sw_if_index\n");
329 M (MEMIF_DELETE, mp);
331 mp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
340 api_memif_dump (vat_main_t * vam)
342 memif_test_main_t *mm = &memif_test_main;
343 vl_api_memif_dump_t *mp;
344 vl_api_control_ping_t *mp_ping;
347 if (vam->json_output)
349 clib_warning ("JSON output not supported for memif_dump");
356 /* Use a control ping for synchronization */
357 mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
358 mp_ping->_vl_msg_id = htons (mm->ping_id);
359 mp_ping->client_index = vam->my_client_index;
361 fformat (vam->ofp, "Sending ping id=%d\n", mm->ping_id);
363 vam->result_ready = 0;
370 /* memif-details message handler */
372 vl_api_memif_details_t_handler (vl_api_memif_details_t * mp)
374 vat_main_t *vam = memif_test_main.vat_main;
376 fformat (vam->ofp, "%s: sw_if_index %u mac %U\n"
377 " id %u socket-id %u role %s\n"
378 " ring_size %u buffer_size %u\n"
379 " state %s link %s\n",
380 mp->if_name, ntohl (mp->sw_if_index), format_ethernet_address,
381 mp->hw_addr, clib_net_to_host_u32 (mp->id),
382 clib_net_to_host_u32 (mp->socket_id),
383 mp->role ? "slave" : "master",
384 ntohl (mp->ring_size), ntohs (mp->buffer_size),
385 mp->admin_up_down ? "up" : "down",
386 mp->link_up_down ? "up" : "down");
389 /* memif_socket_filename_dump API */
391 api_memif_socket_filename_dump (vat_main_t * vam)
393 memif_test_main_t *mm = &memif_test_main;
394 vl_api_memif_socket_filename_dump_t *mp;
395 vl_api_control_ping_t *mp_ping;
398 if (vam->json_output)
401 ("JSON output not supported for memif_socket_filename_dump");
405 M (MEMIF_SOCKET_FILENAME_DUMP, mp);
408 /* Use a control ping for synchronization */
409 mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
410 mp_ping->_vl_msg_id = htons (mm->ping_id);
411 mp_ping->client_index = vam->my_client_index;
413 fformat (vam->ofp, "Sending ping id=%d\n", mm->ping_id);
415 vam->result_ready = 0;
422 /* memif_socket_format_details message handler */
423 static void vl_api_memif_socket_filename_details_t_handler
424 (vl_api_memif_socket_filename_details_t * mp)
426 vat_main_t *vam = memif_test_main.vat_main;
429 "id %u : filename %s\n",
430 ntohl (mp->socket_id), mp->socket_filename);
434 * List of messages that the api test plugin sends,
435 * and that the data plane plugin processes
437 #define foreach_vpe_api_msg \
438 _(memif_create, "[id <id>] [socket-id <id>] [ring_size <size>] " \
439 "[buffer_size <size>] [hw_addr <mac_address>] " \
440 "[secret <string>] [mode ip] <master|slave>") \
441 _(memif_delete, "<sw_if_index>") \
443 _(memif_socket_filename_dump, "") \
444 _(memif_socket_filename_add_del, "[add|del] id <id> filename <file>")
447 memif_vat_api_hookup (vat_main_t * vam)
449 memif_test_main_t *mm __attribute__ ((unused)) = &memif_test_main;
450 /* Hook up handlers for replies from the data plane plug-in */
452 vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base), \
454 vl_api_##n##_t_handler, \
456 vl_api_##n##_t_endian, \
457 vl_api_##n##_t_print, \
458 sizeof(vl_api_##n##_t), 1);
459 foreach_vpe_api_reply_msg;
462 /* API messages we can send */
464 hash_set_mem (vam->function_by_name, #n, api_##n);
469 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
475 vat_plugin_register (vat_main_t * vam)
477 memif_test_main_t *mm = &memif_test_main;
482 /* Ask the vpp engine for the first assigned message-id */
483 name = format (0, "memif_%08x%c", api_version, 0);
484 mm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
486 /* Get the control ping ID */
487 #define _(id,n,crc) \
488 const char *id ## _CRC __attribute__ ((unused)) = #n "_" #crc;
489 foreach_vl_msg_name_crc_vpe;
491 mm->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
493 if (mm->msg_id_base != (u16) ~ 0)
494 memif_vat_api_hookup (vam);
502 * fd.io coding-style-patch-verification: ON
505 * eval: (c-set-style "gnu")