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;
53 vl_api_memif_socket_filename_add_del_reply_t *rmp;
60 socket_id = clib_net_to_host_u32 (mp->socket_id);
61 if (socket_id == 0 || socket_id == ~0)
63 rv = VNET_API_ERROR_INVALID_ARGUMENT;
69 mp->socket_filename[ARRAY_LEN (mp->socket_filename) - 1] = 0;
70 len = strlen ((char *) mp->socket_filename);
73 vec_validate (socket_filename, len);
74 memcpy (socket_filename, mp->socket_filename, len);
77 rv = memif_socket_filename_add_del (is_add, socket_id, socket_filename);
79 vec_free (socket_filename);
82 REPLY_MACRO (VL_API_MEMIF_SOCKET_FILENAME_ADD_DEL_REPLY);
87 * @brief Message handler for memif_create API.
88 * @param mp vl_api_memif_create_t * mp the api message
91 vl_api_memif_create_t_handler (vl_api_memif_create_t * mp)
93 memif_main_t *mm = &memif_main;
94 vlib_main_t *vm = vlib_get_main ();
95 vl_api_memif_create_reply_t *rmp;
96 memif_create_if_args_t args = { 0 };
97 u32 ring_size = MEMIF_DEFAULT_RING_SIZE;
98 static const u8 empty_hw_addr[6];
103 args.id = clib_net_to_host_u32 (mp->id);
106 args.socket_id = clib_net_to_host_u32 (mp->socket_id);
109 mp->secret[ARRAY_LEN (mp->secret) - 1] = 0;
110 if (strlen ((char *) mp->secret) > 0)
112 vec_validate (args.secret, strlen ((char *) mp->secret));
113 strncpy ((char *) args.secret, (char *) mp->secret,
114 vec_len (args.secret));
118 args.is_master = (ntohl (mp->role) == MEMIF_ROLE_API_MASTER);
121 args.mode = ntohl (mp->mode);
123 args.is_zero_copy = mp->no_zero_copy ? 0 : 1;
126 if (args.is_master == 0)
128 args.rx_queues = MEMIF_DEFAULT_RX_QUEUES;
129 args.tx_queues = MEMIF_DEFAULT_TX_QUEUES;
132 args.rx_queues = mp->rx_queues;
136 args.tx_queues = mp->tx_queues;
143 ring_size = ntohl (mp->ring_size);
145 if (!is_pow2 (ring_size))
147 rv = VNET_API_ERROR_INVALID_ARGUMENT;
150 args.log2_ring_size = min_log2 (ring_size);
153 args.buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
156 args.buffer_size = ntohs (mp->buffer_size);
160 mac_address_decode (mp->hw_addr, &mac);
161 if (memcmp (&mac, empty_hw_addr, 6) != 0)
163 memcpy (args.hw_addr, &mac, 6);
164 args.hw_addr_set = 1;
167 rv = memif_create_if (vm, &args);
169 vec_free (args.secret);
173 REPLY_MACRO2 (VL_API_MEMIF_CREATE_REPLY,
175 rmp->sw_if_index = htonl (args.sw_if_index);
181 * @brief Message handler for memif_delete API.
182 * @param mp vl_api_memif_delete_t * mp the api message
185 vl_api_memif_delete_t_handler (vl_api_memif_delete_t * mp)
187 memif_main_t *mm = &memif_main;
188 vlib_main_t *vm = vlib_get_main ();
189 vnet_main_t *vnm = vnet_get_main ();
190 vl_api_memif_delete_reply_t *rmp;
191 vnet_hw_interface_t *hi;
196 vnet_get_sup_hw_interface_api_visible_or_null (vnm,
197 ntohl (mp->sw_if_index));
199 if (hi == NULL || memif_device_class.index != hi->dev_class_index)
200 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
203 mif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
204 rv = memif_delete_if (vm, mif);
207 REPLY_MACRO (VL_API_MEMIF_DELETE_REPLY);
211 send_memif_details (vl_api_registration_t * reg,
213 vnet_sw_interface_t * swif,
214 u8 * interface_name, u32 context)
216 vl_api_memif_details_t *mp;
217 vnet_main_t *vnm = vnet_get_main ();
218 memif_main_t *mm = &memif_main;
219 vnet_hw_interface_t *hwif;
220 memif_socket_file_t *msf;
222 hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
224 mp = vl_msg_api_alloc (sizeof (*mp));
225 clib_memset (mp, 0, sizeof (*mp));
227 mp->_vl_msg_id = htons (VL_API_MEMIF_DETAILS + mm->msg_id_base);
228 mp->context = context;
230 mp->sw_if_index = htonl (swif->sw_if_index);
231 strncpy ((char *) mp->if_name,
232 (char *) interface_name, ARRAY_LEN (mp->if_name) - 1);
234 if (hwif->hw_address)
236 mac_address_encode ((mac_address_t *) hwif->hw_address, mp->hw_addr);
239 mp->id = clib_host_to_net_u32 (mif->id);
241 msf = pool_elt_at_index (mm->socket_files, mif->socket_file_index);
242 mp->socket_id = clib_host_to_net_u32 (msf->socket_id);
245 (mif->flags & MEMIF_IF_FLAG_IS_SLAVE) ? MEMIF_ROLE_API_SLAVE :
246 MEMIF_ROLE_API_MASTER;
247 mp->role = htonl (mp->role);
248 mp->mode = htonl (mif->mode);
249 mp->ring_size = htonl (1 << mif->run.log2_ring_size);
250 mp->buffer_size = htons (mif->run.buffer_size);
251 mp->zero_copy = (mif->flags & MEMIF_IF_FLAG_ZERO_COPY) ? 1 : 0;
254 mp->flags |= (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
255 IF_STATUS_API_FLAG_ADMIN_UP : 0;
256 mp->flags |= (hwif->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
257 IF_STATUS_API_FLAG_LINK_UP : 0;
258 mp->flags = htonl (mp->flags);
261 vl_api_send_msg (reg, (u8 *) mp);
265 * @brief Message handler for memif_dump API.
266 * @param mp vl_api_memif_dump_t * mp the api message
269 vl_api_memif_dump_t_handler (vl_api_memif_dump_t * mp)
271 memif_main_t *mm = &memif_main;
272 vnet_main_t *vnm = vnet_get_main ();
273 vnet_sw_interface_t *swif;
276 vl_api_registration_t *reg;
278 reg = vl_api_client_index_to_registration (mp->client_index);
283 pool_foreach (mif, mm->interfaces,
285 swif = vnet_get_sw_interface (vnm, mif->sw_if_index);
287 if_name = format (if_name, "%U%c",
288 format_vnet_sw_interface_name,
291 send_memif_details (reg, mif, swif, if_name, mp->context);
292 _vec_len (if_name) = 0;
300 send_memif_socket_filename_details (vl_api_registration_t * reg,
302 u8 * socket_filename, u32 context)
304 vl_api_memif_socket_filename_details_t *mp;
305 memif_main_t *mm = &memif_main;
307 mp = vl_msg_api_alloc (sizeof (*mp));
308 clib_memset (mp, 0, sizeof (*mp));
310 mp->_vl_msg_id = htons (VL_API_MEMIF_SOCKET_FILENAME_DETAILS
312 mp->context = context;
314 mp->socket_id = clib_host_to_net_u32 (socket_id);
315 strncpy ((char *) mp->socket_filename,
316 (char *) socket_filename, ARRAY_LEN (mp->socket_filename) - 1);
318 vl_api_send_msg (reg, (u8 *) mp);
322 * @brief Message handler for memif_socket_filename_dump API.
323 * @param mp vl_api_memif_socket_filename_dump_t api message
326 vl_api_memif_socket_filename_dump_t_handler
327 (vl_api_memif_socket_filename_dump_t * mp)
329 memif_main_t *mm = &memif_main;
330 vl_api_registration_t *reg;
334 reg = vl_api_client_index_to_registration (mp->client_index);
339 hash_foreach (sock_id, msf_idx, mm->socket_file_index_by_sock_id,
341 memif_socket_file_t *msf;
344 msf = pool_elt_at_index(mm->socket_files, msf_idx);
345 filename = msf->filename;
346 send_memif_socket_filename_details(reg, sock_id, filename, mp->context);
351 /* Set up the API message handling tables */
352 #include <memif/memif.api.c>
354 memif_plugin_api_hookup (vlib_main_t * vm)
356 memif_main_t *mm = &memif_main;
358 /* Ask for a correctly-sized block of API message decode slots */
359 mm->msg_id_base = setup_message_id_table ();
364 * fd.io coding-style-patch-verification: ON
367 * eval: (c-set-style "gnu")