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>
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>
54 #define REPLY_MSG_ID_BASE mm->msg_id_base
55 #include <vlibapi/api_helper_macros.h>
57 #define foreach_memif_plugin_api_msg \
58 _(MEMIF_SOCKET_FILENAME_ADD_DEL, memif_socket_filename_add_del) \
59 _(MEMIF_CREATE, memif_create) \
60 _(MEMIF_DELETE, memif_delete) \
61 _(MEMIF_SOCKET_FILENAME_DUMP, memif_socket_filename_dump) \
62 _(MEMIF_DUMP, memif_dump) \
66 * @brief Message handler for memif_socket_filename_add_del API.
67 * @param mp the vl_api_memif_socket_filename_add_del_t API message
70 vl_api_memif_socket_filename_add_del_t_handler
71 (vl_api_memif_socket_filename_add_del_t * mp)
73 memif_main_t *mm = &memif_main;
78 vl_api_memif_socket_filename_add_del_reply_t *rmp;
85 socket_id = clib_net_to_host_u32 (mp->socket_id);
86 if (socket_id == 0 || socket_id == ~0)
88 rv = VNET_API_ERROR_INVALID_ARGUMENT;
94 mp->socket_filename[ARRAY_LEN (mp->socket_filename) - 1] = 0;
95 len = strlen ((char *) mp->socket_filename);
98 vec_validate (socket_filename, len);
99 memcpy (socket_filename, mp->socket_filename, len);
102 rv = memif_socket_filename_add_del (is_add, socket_id, socket_filename);
104 vec_free (socket_filename);
107 REPLY_MACRO (VL_API_MEMIF_SOCKET_FILENAME_ADD_DEL_REPLY);
112 * @brief Message handler for memif_create API.
113 * @param mp vl_api_memif_create_t * mp the api message
116 vl_api_memif_create_t_handler (vl_api_memif_create_t * mp)
118 memif_main_t *mm = &memif_main;
119 vlib_main_t *vm = vlib_get_main ();
120 vl_api_memif_create_reply_t *rmp;
121 memif_create_if_args_t args = { 0 };
122 u32 ring_size = MEMIF_DEFAULT_RING_SIZE;
123 static const u8 empty_hw_addr[6];
127 args.id = clib_net_to_host_u32 (mp->id);
130 args.socket_id = clib_net_to_host_u32 (mp->socket_id);
133 mp->secret[ARRAY_LEN (mp->secret) - 1] = 0;
134 if (strlen ((char *) mp->secret) > 0)
136 vec_validate (args.secret, strlen ((char *) mp->secret));
137 strncpy ((char *) args.secret, (char *) mp->secret,
138 vec_len (args.secret));
142 args.is_master = (mp->role == 0);
145 args.mode = mp->mode;
147 /* enable zero-copy */
148 args.is_zero_copy = 1;
151 if (args.is_master == 0)
153 args.rx_queues = MEMIF_DEFAULT_RX_QUEUES;
154 args.tx_queues = MEMIF_DEFAULT_TX_QUEUES;
157 args.rx_queues = mp->rx_queues;
161 args.tx_queues = mp->tx_queues;
168 ring_size = ntohl (mp->ring_size);
170 if (!is_pow2 (ring_size))
172 rv = VNET_API_ERROR_INVALID_ARGUMENT;
175 args.log2_ring_size = min_log2 (ring_size);
178 args.buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
181 args.buffer_size = ntohs (mp->buffer_size);
185 if (memcmp (mp->hw_addr, empty_hw_addr, 6) != 0)
187 memcpy (args.hw_addr, mp->hw_addr, 6);
188 args.hw_addr_set = 1;
191 rv = memif_create_if (vm, &args);
193 vec_free (args.secret);
197 REPLY_MACRO2 (VL_API_MEMIF_CREATE_REPLY,
199 rmp->sw_if_index = htonl (args.sw_if_index);
205 * @brief Message handler for memif_delete API.
206 * @param mp vl_api_memif_delete_t * mp the api message
209 vl_api_memif_delete_t_handler (vl_api_memif_delete_t * mp)
211 memif_main_t *mm = &memif_main;
212 vlib_main_t *vm = vlib_get_main ();
213 vnet_main_t *vnm = vnet_get_main ();
214 vl_api_memif_delete_reply_t *rmp;
215 vnet_hw_interface_t *hi;
220 vnet_get_sup_hw_interface_api_visible_or_null (vnm,
221 ntohl (mp->sw_if_index));
223 if (hi == NULL || memif_device_class.index != hi->dev_class_index)
224 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
227 mif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
228 rv = memif_delete_if (vm, mif);
231 REPLY_MACRO (VL_API_MEMIF_DELETE_REPLY);
235 send_memif_details (vl_api_registration_t * reg,
237 vnet_sw_interface_t * swif,
238 u8 * interface_name, u32 context)
240 vl_api_memif_details_t *mp;
241 vnet_main_t *vnm = vnet_get_main ();
242 memif_main_t *mm = &memif_main;
243 vnet_hw_interface_t *hwif;
244 memif_socket_file_t *msf;
246 hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
248 mp = vl_msg_api_alloc (sizeof (*mp));
249 clib_memset (mp, 0, sizeof (*mp));
251 mp->_vl_msg_id = htons (VL_API_MEMIF_DETAILS + mm->msg_id_base);
252 mp->context = context;
254 mp->sw_if_index = htonl (swif->sw_if_index);
255 strncpy ((char *) mp->if_name,
256 (char *) interface_name, ARRAY_LEN (mp->if_name) - 1);
258 if (hwif->hw_address)
260 memcpy (mp->hw_addr, hwif->hw_address, ARRAY_LEN (mp->hw_addr));
263 mp->id = clib_host_to_net_u32 (mif->id);
265 msf = pool_elt_at_index (mm->socket_files, mif->socket_file_index);
266 mp->socket_id = clib_host_to_net_u32 (msf->socket_id);
268 mp->role = (mif->flags & MEMIF_IF_FLAG_IS_SLAVE) ? 1 : 0;
269 mp->ring_size = htonl (1 << mif->run.log2_ring_size);
270 mp->buffer_size = htons (mif->run.buffer_size);
272 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
273 mp->link_up_down = (hwif->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
275 vl_api_send_msg (reg, (u8 *) mp);
279 * @brief Message handler for memif_dump API.
280 * @param mp vl_api_memif_dump_t * mp the api message
283 vl_api_memif_dump_t_handler (vl_api_memif_dump_t * mp)
285 memif_main_t *mm = &memif_main;
286 vnet_main_t *vnm = vnet_get_main ();
287 vnet_sw_interface_t *swif;
290 vl_api_registration_t *reg;
292 reg = vl_api_client_index_to_registration (mp->client_index);
297 pool_foreach (mif, mm->interfaces,
299 swif = vnet_get_sw_interface (vnm, mif->sw_if_index);
301 if_name = format (if_name, "%U%c",
302 format_vnet_sw_interface_name,
305 send_memif_details (reg, mif, swif, if_name, mp->context);
306 _vec_len (if_name) = 0;
314 send_memif_socket_filename_details (vl_api_registration_t * reg,
316 u8 * socket_filename, u32 context)
318 vl_api_memif_socket_filename_details_t *mp;
319 memif_main_t *mm = &memif_main;
321 mp = vl_msg_api_alloc (sizeof (*mp));
322 clib_memset (mp, 0, sizeof (*mp));
324 mp->_vl_msg_id = htons (VL_API_MEMIF_SOCKET_FILENAME_DETAILS
326 mp->context = context;
328 mp->socket_id = clib_host_to_net_u32 (socket_id);
329 strncpy ((char *) mp->socket_filename,
330 (char *) socket_filename, ARRAY_LEN (mp->socket_filename) - 1);
332 vl_api_send_msg (reg, (u8 *) mp);
336 * @brief Message handler for memif_socket_filename_dump API.
337 * @param mp vl_api_memif_socket_filename_dump_t api message
340 vl_api_memif_socket_filename_dump_t_handler
341 (vl_api_memif_socket_filename_dump_t * mp)
343 memif_main_t *mm = &memif_main;
344 vl_api_registration_t *reg;
348 reg = vl_api_client_index_to_registration (mp->client_index);
353 hash_foreach (sock_id, msf_idx, mm->socket_file_index_by_sock_id,
355 memif_socket_file_t *msf;
358 msf = pool_elt_at_index(mm->socket_files, msf_idx);
359 filename = msf->filename;
360 send_memif_socket_filename_details(reg, sock_id, filename, mp->context);
365 #define vl_msg_name_crc_list
366 #include <memif/memif_all_api_h.h>
367 #undef vl_msg_name_crc_list
370 setup_message_id_table (memif_main_t * mm, api_main_t * am)
372 #define _(id,n,crc) \
373 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + mm->msg_id_base);
374 foreach_vl_msg_name_crc_memif;
378 /* Set up the API message handling tables */
380 memif_plugin_api_hookup (vlib_main_t * vm)
382 memif_main_t *mm = &memif_main;
383 api_main_t *am = &api_main;
386 /* Construct the API name */
387 name = format (0, "memif_%08x%c", api_version, 0);
389 /* Ask for a correctly-sized block of API message decode slots */
390 mm->msg_id_base = vl_msg_api_get_msg_ids
391 ((char *) name, VL_MSG_FIRST_AVAILABLE);
394 vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base), \
396 vl_api_##n##_t_handler, \
398 vl_api_##n##_t_endian, \
399 vl_api_##n##_t_print, \
400 sizeof(vl_api_##n##_t), 1);
401 foreach_memif_plugin_api_msg;
405 * Set up the (msg_name, crc, message-id) table
407 setup_message_id_table (mm, am);
414 * fd.io coding-style-patch-verification: ON
417 * eval: (c-set-style "gnu")