2 *------------------------------------------------------------------
3 * memif_api.c - memif api
5 * Copyright (c) 2017 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
20 #include <vlib/vlib.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vlib/unix/unix.h>
23 #include <memif/memif.h>
24 #include <memif/private.h>
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28 #include <vlibsocket/api.h>
30 /* define message IDs */
31 #include <memif/memif_msg_enum.h>
33 /* define message structures */
35 #include <memif/memif_all_api_h.h>
38 /* define generated endian-swappers */
40 #include <memif/memif_all_api_h.h>
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
46 #include <memif/memif_all_api_h.h>
49 /* Get the API version number */
50 #define vl_api_version(n,v) static u32 api_version=(v);
51 #include <memif/memif_all_api_h.h>
55 * A handy macro to set up a message reply.
56 * Assumes that the following variables are available:
57 * mp - pointer to request message
58 * rmp - pointer to reply message type
61 #define REPLY_MACRO(t) \
63 unix_shared_memory_queue_t * q = \
64 vl_api_client_index_to_input_queue (mp->client_index); \
68 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
69 rmp->_vl_msg_id = htons ((t)+mm->msg_id_base); \
70 rmp->context = mp->context; \
71 rmp->retval = htonl (rv); \
73 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
76 #define REPLY_MACRO2(t, body) \
78 unix_shared_memory_queue_t * q = \
79 vl_api_client_index_to_input_queue (mp->client_index); \
83 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
84 rmp->_vl_msg_id = htons ((t)+mm->msg_id_base); \
85 rmp->context = mp->context; \
86 rmp->retval = htonl (rv); \
87 do {body;} while (0); \
88 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
91 #define foreach_memif_plugin_api_msg \
92 _(MEMIF_CREATE, memif_create) \
93 _(MEMIF_DELETE, memif_delete) \
94 _(MEMIF_DUMP, memif_dump) \
97 * @brief Message handler for memif_create API.
98 * @param mp vl_api_memif_create_t * mp the api message
101 vl_api_memif_create_t_handler (vl_api_memif_create_t * mp)
103 memif_main_t *mm = &memif_main;
104 vlib_main_t *vm = vlib_get_main ();
105 vl_api_memif_create_reply_t *rmp;
106 memif_create_if_args_t args = { 0 };
107 u32 ring_size = MEMIF_DEFAULT_RING_SIZE;
108 static const u8 empty_hw_addr[6];
112 args.id = clib_net_to_host_u32 (mp->id);
114 /* socket filename */
115 mp->socket_filename[ARRAY_LEN (mp->socket_filename) - 1] = 0;
116 if (strlen ((char *) mp->socket_filename) > 0)
118 vec_validate (args.socket_filename,
119 strlen ((char *) mp->socket_filename));
120 strncpy ((char *) args.socket_filename, (char *) mp->socket_filename,
121 vec_len (args.socket_filename));
125 mp->secret[ARRAY_LEN (mp->secret) - 1] = 0;
126 if (strlen ((char *) mp->secret) > 0)
128 vec_validate (args.secret, strlen ((char *) mp->secret));
129 strncpy ((char *) args.secret, (char *) mp->secret,
130 vec_len (args.secret));
134 args.is_master = (mp->role == 0);
137 if (args.is_master == 0)
139 args.rx_queues = MEMIF_DEFAULT_RX_QUEUES;
140 args.tx_queues = MEMIF_DEFAULT_TX_QUEUES;
143 args.rx_queues = mp->rx_queues;
147 args.tx_queues = mp->tx_queues;
154 ring_size = ntohl (mp->ring_size);
156 if (!is_pow2 (ring_size))
158 rv = VNET_API_ERROR_INVALID_ARGUMENT;
161 args.log2_ring_size = min_log2 (ring_size);
164 args.buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
167 args.buffer_size = ntohs (mp->buffer_size);
171 if (memcmp (mp->hw_addr, empty_hw_addr, 6) != 0)
173 memcpy (args.hw_addr, mp->hw_addr, 6);
174 args.hw_addr_set = 1;
177 rv = memif_create_if (vm, &args);
179 vec_free (args.socket_filename);
180 vec_free (args.secret);
184 REPLY_MACRO2 (VL_API_MEMIF_CREATE_REPLY,
186 rmp->sw_if_index = htonl (args.sw_if_index);
192 * @brief Message handler for memif_delete API.
193 * @param mp vl_api_memif_delete_t * mp the api message
196 vl_api_memif_delete_t_handler (vl_api_memif_delete_t * mp)
198 memif_main_t *mm = &memif_main;
199 vlib_main_t *vm = vlib_get_main ();
200 vnet_main_t *vnm = vnet_get_main ();
201 vl_api_memif_delete_reply_t *rmp;
202 vnet_hw_interface_t *hi =
203 vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
204 memif_if_t *mif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
207 if (hi == NULL || memif_device_class.index != hi->dev_class_index)
208 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
210 rv = memif_delete_if (vm, mif);
212 REPLY_MACRO (VL_API_MEMIF_DELETE_REPLY);
216 send_memif_details (unix_shared_memory_queue_t * q,
218 vnet_sw_interface_t * swif,
219 u8 * interface_name, u32 context)
221 vl_api_memif_details_t *mp;
222 vnet_main_t *vnm = vnet_get_main ();
223 memif_main_t *mm = &memif_main;
224 memif_socket_file_t *msf = vec_elt_at_index (mm->socket_files,
225 mif->socket_file_index);
226 vnet_hw_interface_t *hwif;
228 hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
230 mp = vl_msg_api_alloc (sizeof (*mp));
231 memset (mp, 0, sizeof (*mp));
233 mp->_vl_msg_id = htons (VL_API_MEMIF_DETAILS + mm->msg_id_base);
234 mp->context = context;
236 mp->sw_if_index = htonl (swif->sw_if_index);
237 strncpy ((char *) mp->if_name,
238 (char *) interface_name, ARRAY_LEN (mp->if_name) - 1);
239 memcpy (mp->hw_addr, hwif->hw_address, ARRAY_LEN (mp->hw_addr));
241 mp->id = clib_host_to_net_u32 (mif->id);
242 mp->role = (mif->flags & MEMIF_IF_FLAG_IS_SLAVE) ? 1 : 0;
243 strncpy ((char *) mp->socket_filename,
244 (char *) msf->filename, ARRAY_LEN (mp->socket_filename) - 1);
246 mp->ring_size = htonl (1 << mif->run.log2_ring_size);
247 mp->buffer_size = htons (mif->run.buffer_size);
249 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
250 mp->link_up_down = (hwif->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
252 vl_msg_api_send_shmem (q, (u8 *) & mp);
256 * @brief Message handler for memif_dump API.
257 * @param mp vl_api_memif_dump_t * mp the api message
260 vl_api_memif_dump_t_handler (vl_api_memif_dump_t * mp)
262 memif_main_t *mm = &memif_main;
263 vnet_main_t *vnm = vnet_get_main ();
264 vnet_sw_interface_t *swif;
267 unix_shared_memory_queue_t *q;
269 q = vl_api_client_index_to_input_queue (mp->client_index);
274 pool_foreach (mif, mm->interfaces,
276 swif = vnet_get_sw_interface (vnm, mif->sw_if_index);
278 if_name = format (if_name, "%U%c",
279 format_vnet_sw_interface_name,
282 send_memif_details (q, mif, swif, if_name, mp->context);
283 _vec_len (if_name) = 0;
290 #define vl_msg_name_crc_list
291 #include <memif/memif_all_api_h.h>
292 #undef vl_msg_name_crc_list
295 setup_message_id_table (memif_main_t * mm, api_main_t * am)
297 #define _(id,n,crc) \
298 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + mm->msg_id_base);
299 foreach_vl_msg_name_crc_memif;
303 /* Set up the API message handling tables */
305 memif_plugin_api_hookup (vlib_main_t * vm)
307 memif_main_t *mm = &memif_main;
308 api_main_t *am = &api_main;
311 /* Construct the API name */
312 name = format (0, "memif_%08x%c", api_version, 0);
314 /* Ask for a correctly-sized block of API message decode slots */
315 mm->msg_id_base = vl_msg_api_get_msg_ids
316 ((char *) name, VL_MSG_FIRST_AVAILABLE);
319 vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base), \
321 vl_api_##n##_t_handler, \
323 vl_api_##n##_t_endian, \
324 vl_api_##n##_t_print, \
325 sizeof(vl_api_##n##_t), 1);
326 foreach_memif_plugin_api_msg;
330 * Set up the (msg_name, crc, message-id) table
332 setup_message_id_table (mm, am);
339 * fd.io coding-style-patch-verification: ON
342 * eval: (c-set-style "gnu")