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>
29 #include <vnet/ip/ip_types_api.h>
30 #include <vnet/ethernet/ethernet_types_api.h>
32 /* define message IDs */
33 #include <vnet/format_fns.h>
34 #include <memif/memif.api_enum.h>
35 #include <memif/memif.api_types.h>
37 #define REPLY_MSG_ID_BASE mm->msg_id_base
38 #include <vlibapi/api_helper_macros.h>
41 * @brief Message handler for memif_socket_filename_add_del API.
42 * @param mp the vl_api_memif_socket_filename_add_del_t API message
45 vl_api_memif_socket_filename_add_del_t_handler
46 (vl_api_memif_socket_filename_add_del_t * mp)
48 memif_main_t *mm = &memif_main;
51 vl_api_memif_socket_filename_add_del_reply_t *rmp;
58 socket_id = clib_net_to_host_u32 (mp->socket_id);
59 if (socket_id == 0 || socket_id == ~0)
61 rv = VNET_API_ERROR_INVALID_ARGUMENT;
66 mp->socket_filename[ARRAY_LEN (mp->socket_filename) - 1] = 0;
68 rv = vnet_get_api_error_and_free (memif_socket_filename_add_del (
69 is_add, socket_id, (char *) mp->socket_filename));
72 REPLY_MACRO (VL_API_MEMIF_SOCKET_FILENAME_ADD_DEL_REPLY);
76 * @brief Message handler for memif_socket_filename_add_del API.
77 * @param mp the vl_api_memif_socket_filename_add_del_t API message
80 vl_api_memif_socket_filename_add_del_v2_t_handler (
81 vl_api_memif_socket_filename_add_del_v2_t *mp)
83 vl_api_memif_socket_filename_add_del_v2_reply_t *rmp;
84 memif_main_t *mm = &memif_main;
85 char *socket_filename = 0;
90 socket_id = clib_net_to_host_u32 (mp->socket_id);
93 rv = VNET_API_ERROR_INVALID_ARGUMENT;
98 socket_filename = vl_api_from_api_to_new_c_string (&mp->socket_filename);
99 if (mp->is_add && socket_id == (u32) ~0)
100 socket_id = memif_get_unused_socket_id ();
102 rv = vnet_get_api_error_and_free (
103 memif_socket_filename_add_del (mp->is_add, socket_id, socket_filename));
105 vec_free (socket_filename);
108 REPLY_MACRO2 (VL_API_MEMIF_SOCKET_FILENAME_ADD_DEL_V2_REPLY,
109 ({ rmp->socket_id = htonl (socket_id); }));
113 * @brief Message handler for memif_create API.
114 * @param mp vl_api_memif_create_t * mp the api message
117 vl_api_memif_create_t_handler (vl_api_memif_create_t * mp)
119 memif_main_t *mm = &memif_main;
120 vlib_main_t *vm = vlib_get_main ();
121 vl_api_memif_create_reply_t *rmp;
122 memif_create_if_args_t args = { 0 };
123 u32 ring_size = MEMIF_DEFAULT_RING_SIZE;
124 static const u8 empty_hw_addr[6];
129 args.id = clib_net_to_host_u32 (mp->id);
132 args.socket_id = clib_net_to_host_u32 (mp->socket_id);
135 mp->secret[ARRAY_LEN (mp->secret) - 1] = 0;
136 if (strlen ((char *) mp->secret) > 0)
138 vec_validate (args.secret, strlen ((char *) mp->secret));
139 strncpy ((char *) args.secret, (char *) mp->secret,
140 vec_len (args.secret));
144 args.is_master = (ntohl (mp->role) == MEMIF_ROLE_API_MASTER);
147 args.mode = ntohl (mp->mode);
149 args.is_zero_copy = mp->no_zero_copy ? 0 : 1;
152 if (args.is_master == 0)
154 args.rx_queues = MEMIF_DEFAULT_RX_QUEUES;
155 args.tx_queues = MEMIF_DEFAULT_TX_QUEUES;
158 args.rx_queues = mp->rx_queues;
162 args.tx_queues = mp->tx_queues;
169 ring_size = ntohl (mp->ring_size);
171 if (!is_pow2 (ring_size))
173 rv = VNET_API_ERROR_INVALID_ARGUMENT;
176 args.log2_ring_size = min_log2 (ring_size);
179 args.buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
182 args.buffer_size = ntohs (mp->buffer_size);
186 mac_address_decode (mp->hw_addr, &mac);
187 if (memcmp (&mac, empty_hw_addr, 6) != 0)
189 memcpy (args.hw_addr, &mac, 6);
190 args.hw_addr_set = 1;
193 rv = vnet_get_api_error_and_free (memif_create_if (vm, &args));
195 vec_free (args.secret);
199 REPLY_MACRO2 (VL_API_MEMIF_CREATE_REPLY,
201 rmp->sw_if_index = htonl (args.sw_if_index);
207 * @brief Message handler for memif_delete API.
208 * @param mp vl_api_memif_delete_t * mp the api message
211 vl_api_memif_delete_t_handler (vl_api_memif_delete_t * mp)
213 memif_main_t *mm = &memif_main;
214 vlib_main_t *vm = vlib_get_main ();
215 vnet_main_t *vnm = vnet_get_main ();
216 vl_api_memif_delete_reply_t *rmp;
217 vnet_hw_interface_t *hi;
222 vnet_get_sup_hw_interface_api_visible_or_null (vnm,
223 ntohl (mp->sw_if_index));
225 if (hi == NULL || memif_device_class.index != hi->dev_class_index)
226 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
229 mif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
230 rv = vnet_get_api_error_and_free (memif_delete_if (vm, mif));
233 REPLY_MACRO (VL_API_MEMIF_DELETE_REPLY);
237 send_memif_details (vl_api_registration_t * reg,
239 vnet_sw_interface_t * swif,
240 u8 * interface_name, u32 context)
242 vl_api_memif_details_t *mp;
243 vnet_main_t *vnm = vnet_get_main ();
244 memif_main_t *mm = &memif_main;
245 vnet_hw_interface_t *hwif;
246 memif_socket_file_t *msf;
248 hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
250 mp = vl_msg_api_alloc (sizeof (*mp));
251 clib_memset (mp, 0, sizeof (*mp));
253 mp->_vl_msg_id = htons (VL_API_MEMIF_DETAILS + mm->msg_id_base);
254 mp->context = context;
256 mp->sw_if_index = htonl (swif->sw_if_index);
257 strncpy ((char *) mp->if_name,
258 (char *) interface_name, ARRAY_LEN (mp->if_name) - 1);
260 if (hwif->hw_address)
262 mac_address_encode ((mac_address_t *) hwif->hw_address, mp->hw_addr);
265 mp->id = clib_host_to_net_u32 (mif->id);
267 msf = pool_elt_at_index (mm->socket_files, mif->socket_file_index);
268 mp->socket_id = clib_host_to_net_u32 (msf->socket_id);
271 (mif->flags & MEMIF_IF_FLAG_IS_SLAVE) ? MEMIF_ROLE_API_SLAVE :
272 MEMIF_ROLE_API_MASTER;
273 mp->role = htonl (mp->role);
274 mp->mode = htonl (mif->mode);
275 mp->ring_size = htonl (1 << mif->run.log2_ring_size);
276 mp->buffer_size = htons (mif->run.buffer_size);
277 mp->zero_copy = (mif->flags & MEMIF_IF_FLAG_ZERO_COPY) ? 1 : 0;
280 mp->flags |= (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
281 IF_STATUS_API_FLAG_ADMIN_UP : 0;
282 mp->flags |= (hwif->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
283 IF_STATUS_API_FLAG_LINK_UP : 0;
284 mp->flags = htonl (mp->flags);
287 vl_api_send_msg (reg, (u8 *) mp);
291 * @brief Message handler for memif_dump API.
292 * @param mp vl_api_memif_dump_t * mp the api message
295 vl_api_memif_dump_t_handler (vl_api_memif_dump_t * mp)
297 memif_main_t *mm = &memif_main;
298 vnet_main_t *vnm = vnet_get_main ();
299 vnet_sw_interface_t *swif;
302 vl_api_registration_t *reg;
304 reg = vl_api_client_index_to_registration (mp->client_index);
309 pool_foreach (mif, mm->interfaces)
311 swif = vnet_get_sw_interface (vnm, mif->sw_if_index);
313 if_name = format (if_name, "%U%c",
314 format_vnet_sw_interface_name,
317 send_memif_details (reg, mif, swif, if_name, mp->context);
318 vec_set_len (if_name, 0);
326 send_memif_socket_filename_details (vl_api_registration_t * reg,
328 u8 * socket_filename, u32 context)
330 vl_api_memif_socket_filename_details_t *mp;
331 memif_main_t *mm = &memif_main;
333 mp = vl_msg_api_alloc (sizeof (*mp));
334 clib_memset (mp, 0, sizeof (*mp));
336 mp->_vl_msg_id = htons (VL_API_MEMIF_SOCKET_FILENAME_DETAILS
338 mp->context = context;
340 mp->socket_id = clib_host_to_net_u32 (socket_id);
341 strncpy ((char *) mp->socket_filename,
342 (char *) socket_filename, ARRAY_LEN (mp->socket_filename) - 1);
344 vl_api_send_msg (reg, (u8 *) mp);
348 * @brief Message handler for memif_socket_filename_dump API.
349 * @param mp vl_api_memif_socket_filename_dump_t api message
352 vl_api_memif_socket_filename_dump_t_handler
353 (vl_api_memif_socket_filename_dump_t * mp)
355 memif_main_t *mm = &memif_main;
356 vl_api_registration_t *reg;
360 reg = vl_api_client_index_to_registration (mp->client_index);
365 hash_foreach (sock_id, msf_idx, mm->socket_file_index_by_sock_id,
367 memif_socket_file_t *msf;
370 msf = pool_elt_at_index(mm->socket_files, msf_idx);
371 filename = msf->filename;
372 send_memif_socket_filename_details(reg, sock_id, filename, mp->context);
377 /* Set up the API message handling tables */
378 #include <memif/memif.api.c>
380 memif_plugin_api_hookup (vlib_main_t * vm)
382 memif_main_t *mm = &memif_main;
384 /* Ask for a correctly-sized block of API message decode slots */
385 mm->msg_id_base = setup_message_id_table ();
390 * fd.io coding-style-patch-verification: ON
393 * eval: (c-set-style "gnu")