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_api_error (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);
77 * @brief Message handler for memif_create API.
78 * @param mp vl_api_memif_create_t * mp the api message
81 vl_api_memif_create_t_handler (vl_api_memif_create_t * mp)
83 memif_main_t *mm = &memif_main;
84 vlib_main_t *vm = vlib_get_main ();
85 vl_api_memif_create_reply_t *rmp;
86 memif_create_if_args_t args = { 0 };
87 u32 ring_size = MEMIF_DEFAULT_RING_SIZE;
88 static const u8 empty_hw_addr[6];
93 args.id = clib_net_to_host_u32 (mp->id);
96 args.socket_id = clib_net_to_host_u32 (mp->socket_id);
99 mp->secret[ARRAY_LEN (mp->secret) - 1] = 0;
100 if (strlen ((char *) mp->secret) > 0)
102 vec_validate (args.secret, strlen ((char *) mp->secret));
103 strncpy ((char *) args.secret, (char *) mp->secret,
104 vec_len (args.secret));
108 args.is_master = (ntohl (mp->role) == MEMIF_ROLE_API_MASTER);
111 args.mode = ntohl (mp->mode);
113 args.is_zero_copy = mp->no_zero_copy ? 0 : 1;
116 if (args.is_master == 0)
118 args.rx_queues = MEMIF_DEFAULT_RX_QUEUES;
119 args.tx_queues = MEMIF_DEFAULT_TX_QUEUES;
122 args.rx_queues = mp->rx_queues;
126 args.tx_queues = mp->tx_queues;
133 ring_size = ntohl (mp->ring_size);
135 if (!is_pow2 (ring_size))
137 rv = VNET_API_ERROR_INVALID_ARGUMENT;
140 args.log2_ring_size = min_log2 (ring_size);
143 args.buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
146 args.buffer_size = ntohs (mp->buffer_size);
150 mac_address_decode (mp->hw_addr, &mac);
151 if (memcmp (&mac, empty_hw_addr, 6) != 0)
153 memcpy (args.hw_addr, &mac, 6);
154 args.hw_addr_set = 1;
157 rv = vnet_api_error (memif_create_if (vm, &args));
159 vec_free (args.secret);
163 REPLY_MACRO2 (VL_API_MEMIF_CREATE_REPLY,
165 rmp->sw_if_index = htonl (args.sw_if_index);
171 * @brief Message handler for memif_delete API.
172 * @param mp vl_api_memif_delete_t * mp the api message
175 vl_api_memif_delete_t_handler (vl_api_memif_delete_t * mp)
177 memif_main_t *mm = &memif_main;
178 vlib_main_t *vm = vlib_get_main ();
179 vnet_main_t *vnm = vnet_get_main ();
180 vl_api_memif_delete_reply_t *rmp;
181 vnet_hw_interface_t *hi;
186 vnet_get_sup_hw_interface_api_visible_or_null (vnm,
187 ntohl (mp->sw_if_index));
189 if (hi == NULL || memif_device_class.index != hi->dev_class_index)
190 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
193 mif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
194 rv = vnet_api_error (memif_delete_if (vm, mif));
197 REPLY_MACRO (VL_API_MEMIF_DELETE_REPLY);
201 send_memif_details (vl_api_registration_t * reg,
203 vnet_sw_interface_t * swif,
204 u8 * interface_name, u32 context)
206 vl_api_memif_details_t *mp;
207 vnet_main_t *vnm = vnet_get_main ();
208 memif_main_t *mm = &memif_main;
209 vnet_hw_interface_t *hwif;
210 memif_socket_file_t *msf;
212 hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
214 mp = vl_msg_api_alloc (sizeof (*mp));
215 clib_memset (mp, 0, sizeof (*mp));
217 mp->_vl_msg_id = htons (VL_API_MEMIF_DETAILS + mm->msg_id_base);
218 mp->context = context;
220 mp->sw_if_index = htonl (swif->sw_if_index);
221 strncpy ((char *) mp->if_name,
222 (char *) interface_name, ARRAY_LEN (mp->if_name) - 1);
224 if (hwif->hw_address)
226 mac_address_encode ((mac_address_t *) hwif->hw_address, mp->hw_addr);
229 mp->id = clib_host_to_net_u32 (mif->id);
231 msf = pool_elt_at_index (mm->socket_files, mif->socket_file_index);
232 mp->socket_id = clib_host_to_net_u32 (msf->socket_id);
235 (mif->flags & MEMIF_IF_FLAG_IS_SLAVE) ? MEMIF_ROLE_API_SLAVE :
236 MEMIF_ROLE_API_MASTER;
237 mp->role = htonl (mp->role);
238 mp->mode = htonl (mif->mode);
239 mp->ring_size = htonl (1 << mif->run.log2_ring_size);
240 mp->buffer_size = htons (mif->run.buffer_size);
241 mp->zero_copy = (mif->flags & MEMIF_IF_FLAG_ZERO_COPY) ? 1 : 0;
244 mp->flags |= (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
245 IF_STATUS_API_FLAG_ADMIN_UP : 0;
246 mp->flags |= (hwif->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
247 IF_STATUS_API_FLAG_LINK_UP : 0;
248 mp->flags = htonl (mp->flags);
251 vl_api_send_msg (reg, (u8 *) mp);
255 * @brief Message handler for memif_dump API.
256 * @param mp vl_api_memif_dump_t * mp the api message
259 vl_api_memif_dump_t_handler (vl_api_memif_dump_t * mp)
261 memif_main_t *mm = &memif_main;
262 vnet_main_t *vnm = vnet_get_main ();
263 vnet_sw_interface_t *swif;
266 vl_api_registration_t *reg;
268 reg = vl_api_client_index_to_registration (mp->client_index);
273 pool_foreach (mif, mm->interfaces)
275 swif = vnet_get_sw_interface (vnm, mif->sw_if_index);
277 if_name = format (if_name, "%U%c",
278 format_vnet_sw_interface_name,
281 send_memif_details (reg, mif, swif, if_name, mp->context);
282 vec_set_len (if_name, 0);
290 send_memif_socket_filename_details (vl_api_registration_t * reg,
292 u8 * socket_filename, u32 context)
294 vl_api_memif_socket_filename_details_t *mp;
295 memif_main_t *mm = &memif_main;
297 mp = vl_msg_api_alloc (sizeof (*mp));
298 clib_memset (mp, 0, sizeof (*mp));
300 mp->_vl_msg_id = htons (VL_API_MEMIF_SOCKET_FILENAME_DETAILS
302 mp->context = context;
304 mp->socket_id = clib_host_to_net_u32 (socket_id);
305 strncpy ((char *) mp->socket_filename,
306 (char *) socket_filename, ARRAY_LEN (mp->socket_filename) - 1);
308 vl_api_send_msg (reg, (u8 *) mp);
312 * @brief Message handler for memif_socket_filename_dump API.
313 * @param mp vl_api_memif_socket_filename_dump_t api message
316 vl_api_memif_socket_filename_dump_t_handler
317 (vl_api_memif_socket_filename_dump_t * mp)
319 memif_main_t *mm = &memif_main;
320 vl_api_registration_t *reg;
324 reg = vl_api_client_index_to_registration (mp->client_index);
329 hash_foreach (sock_id, msf_idx, mm->socket_file_index_by_sock_id,
331 memif_socket_file_t *msf;
334 msf = pool_elt_at_index(mm->socket_files, msf_idx);
335 filename = msf->filename;
336 send_memif_socket_filename_details(reg, sock_id, filename, mp->context);
341 /* Set up the API message handling tables */
342 #include <memif/memif.api.c>
344 memif_plugin_api_hookup (vlib_main_t * vm)
346 memif_main_t *mm = &memif_main;
348 /* Ask for a correctly-sized block of API message decode slots */
349 mm->msg_id_base = setup_message_id_table ();
354 * fd.io coding-style-patch-verification: ON
357 * eval: (c-set-style "gnu")