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 <vpp/api/vpe.api_types.h>
37 //#include <vnet/ethernet/ethernet_types.api_types.h>
41 /* API message ID base */
47 memif_test_main_t memif_test_main;
50 unformat_memif_queues (unformat_input_t * input, va_list * args)
52 u32 *rx_queues = va_arg (*args, u32 *);
53 u32 *tx_queues = va_arg (*args, u32 *);
55 if (unformat (input, "rx-queues %u", rx_queues))
57 if (unformat (input, "tx-queues %u", tx_queues))
63 /* memif_socket_filename_add_del API */
65 api_memif_socket_filename_add_del (vat_main_t * vam)
67 unformat_input_t *i = vam->input;
68 vl_api_memif_socket_filename_add_del_t *mp;
78 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
80 if (unformat (i, "id %u", &socket_id))
82 else if (unformat (i, "filename %s", &socket_filename))
84 else if (unformat (i, "del"))
86 else if (unformat (i, "add"))
90 vec_free (socket_filename);
91 clib_warning ("unknown input `%U'", format_unformat_error, i);
96 if (socket_id == 0 || socket_id == ~0)
98 vec_free (socket_filename);
99 errmsg ("Invalid socket id");
103 if (is_add && (!socket_filename || *socket_filename == 0))
105 vec_free (socket_filename);
106 errmsg ("Invalid socket filename");
110 M2 (MEMIF_SOCKET_FILENAME_ADD_DEL, mp, strlen ((char *) socket_filename));
113 mp->socket_id = htonl (socket_id);
114 char *p = (char *) &mp->socket_filename;
116 vl_api_to_api_string (strlen ((char *) socket_filename),
117 (char *) socket_filename, (vl_api_string_t *) p);
119 vec_free (socket_filename);
127 /* memif_socket_filename_add_del reply handler */
128 #define VL_API_MEMIF_SOCKET_FILENAME_ADD_DEL_REPLY_T_HANLDER
129 static void vl_api_memif_socket_filename_add_del_reply_t_handler
130 (vl_api_memif_socket_filename_add_del_reply_t * mp)
132 vat_main_t *vam = memif_test_main.vat_main;
133 i32 retval = ntohl (mp->retval);
135 vam->retval = retval;
136 vam->result_ready = 1;
137 vam->regenerate_interface_table = 1;
140 /* memif-create API */
142 api_memif_create (vat_main_t * vam)
144 unformat_input_t *i = vam->input;
145 vl_api_memif_create_t *mp;
152 u8 hw_addr[6] = { 0 };
153 u32 rx_queues = MEMIF_DEFAULT_RX_QUEUES;
154 u32 tx_queues = MEMIF_DEFAULT_TX_QUEUES;
156 u8 mode = MEMIF_INTERFACE_MODE_ETHERNET;
158 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
160 if (unformat (i, "id %u", &id))
162 else if (unformat (i, "socket-id %u", &socket_id))
164 else if (unformat (i, "secret %s", &secret))
166 else if (unformat (i, "ring_size %u", &ring_size))
168 else if (unformat (i, "buffer_size %u", &buffer_size))
170 else if (unformat (i, "master"))
172 else if (unformat (i, "slave %U",
173 unformat_memif_queues, &rx_queues, &tx_queues))
175 else if (unformat (i, "mode ip"))
176 mode = MEMIF_INTERFACE_MODE_IP;
177 else if (unformat (i, "hw_addr %U", unformat_ethernet_address, hw_addr))
181 clib_warning ("unknown input '%U'", format_unformat_error, i);
188 errmsg ("invalid socket-id\n");
192 if (!is_pow2 (ring_size))
194 errmsg ("ring size must be power of 2\n");
198 if (rx_queues > 255 || rx_queues < 1)
200 errmsg ("rx queue must be between 1 - 255\n");
204 if (tx_queues > 255 || tx_queues < 1)
206 errmsg ("tx queue must be between 1 - 255\n");
210 M2 (MEMIF_CREATE, mp, strlen ((char *) secret));
213 mp->id = clib_host_to_net_u32 (id);
215 mp->ring_size = clib_host_to_net_u32 (ring_size);
216 mp->buffer_size = clib_host_to_net_u16 (buffer_size & 0xffff);
217 mp->socket_id = clib_host_to_net_u32 (socket_id);
220 char *p = (char *) &mp->secret;
221 p += vl_api_to_api_string (strlen ((char *) secret), (char *) secret,
222 (vl_api_string_t *) p);
225 memcpy (mp->hw_addr, hw_addr, 6);
226 mp->rx_queues = rx_queues;
227 mp->tx_queues = tx_queues;
234 /* memif-create reply handler */
235 static void vl_api_memif_create_reply_t_handler
236 (vl_api_memif_create_reply_t * mp)
238 vat_main_t *vam = memif_test_main.vat_main;
239 i32 retval = ntohl (mp->retval);
243 fformat (vam->ofp, "created memif with sw_if_index %d\n",
244 ntohl (mp->sw_if_index));
247 vam->retval = retval;
248 vam->result_ready = 1;
249 vam->regenerate_interface_table = 1;
252 /* memif-delete API */
254 api_memif_delete (vat_main_t * vam)
256 unformat_input_t *i = vam->input;
257 vl_api_memif_delete_t *mp;
259 u8 index_defined = 0;
262 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
264 if (unformat (i, "sw_if_index %u", &sw_if_index))
268 clib_warning ("unknown input '%U'", format_unformat_error, i);
275 errmsg ("missing sw_if_index\n");
279 M (MEMIF_DELETE, mp);
281 mp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
290 api_memif_dump (vat_main_t * vam)
292 memif_test_main_t *mm = &memif_test_main;
293 vl_api_memif_dump_t *mp;
294 vl_api_control_ping_t *mp_ping;
297 if (vam->json_output)
299 clib_warning ("JSON output not supported for memif_dump");
306 /* Use a control ping for synchronization */
307 mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
308 mp_ping->_vl_msg_id = htons (mm->ping_id);
309 mp_ping->client_index = vam->my_client_index;
311 fformat (vam->ofp, "Sending ping id=%d\n", mm->ping_id);
313 vam->result_ready = 0;
320 /* memif-details message handler */
322 vl_api_memif_details_t_handler (vl_api_memif_details_t * mp)
324 vat_main_t *vam = memif_test_main.vat_main;
326 fformat (vam->ofp, "%s: sw_if_index %u mac %U\n"
327 " id %u socket-id %u role %s\n"
328 " ring_size %u buffer_size %u\n"
329 " state %s link %s\n",
330 mp->if_name, ntohl (mp->sw_if_index), format_ethernet_address,
331 mp->hw_addr, clib_net_to_host_u32 (mp->id),
332 clib_net_to_host_u32 (mp->socket_id),
333 mp->role ? "slave" : "master",
334 ntohl (mp->ring_size), ntohs (mp->buffer_size),
335 (mp->flags & IF_STATUS_API_FLAG_ADMIN_UP) ? "up" : "down",
336 (mp->flags & IF_STATUS_API_FLAG_LINK_UP) ? "up" : "down");
339 /* memif_socket_filename_dump API */
341 api_memif_socket_filename_dump (vat_main_t * vam)
343 memif_test_main_t *mm = &memif_test_main;
344 vl_api_memif_socket_filename_dump_t *mp;
345 vl_api_control_ping_t *mp_ping;
348 if (vam->json_output)
351 ("JSON output not supported for memif_socket_filename_dump");
355 M (MEMIF_SOCKET_FILENAME_DUMP, mp);
358 /* Use a control ping for synchronization */
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;
363 fformat (vam->ofp, "Sending ping id=%d\n", mm->ping_id);
365 vam->result_ready = 0;
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)
376 vat_main_t *vam = memif_test_main.vat_main;
379 "id %u : filename %s\n",
380 ntohl (mp->socket_id), mp->socket_filename);
383 #include <memif/memif.api_test.c>
386 * fd.io coding-style-patch-verification: ON
389 * eval: (c-set-style "gnu")