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 <memif/memif.h>
24 #include <vlibapi/api.h>
25 #include <vlibmemory/api.h>
26 #include <vlibsocket/api.h>
28 /* define message IDs */
29 #include <memif/memif_msg_enum.h>
31 /* define message structures */
33 #include <memif/memif_all_api_h.h>
36 /* define generated endian-swappers */
38 #include <memif/memif_all_api_h.h>
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 #include <memif/memif_all_api_h.h>
47 /* Get the API version number */
48 #define vl_api_version(n,v) static u32 api_version=(v);
49 #include <memif/memif_all_api_h.h>
53 * A handy macro to set up a message reply.
54 * Assumes that the following variables are available:
55 * mp - pointer to request message
56 * rmp - pointer to reply message type
59 #define REPLY_MACRO(t) \
61 unix_shared_memory_queue_t * q = \
62 vl_api_client_index_to_input_queue (mp->client_index); \
66 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
67 rmp->_vl_msg_id = htons ((t)+mm->msg_id_base); \
68 rmp->context = mp->context; \
69 rmp->retval = htonl (rv); \
71 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
74 #define REPLY_MACRO2(t, body) \
76 unix_shared_memory_queue_t * q = \
77 vl_api_client_index_to_input_queue (mp->client_index); \
81 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
82 rmp->_vl_msg_id = htons ((t)+mm->msg_id_base); \
83 rmp->context = mp->context; \
84 rmp->retval = htonl (rv); \
85 do {body;} while (0); \
86 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
89 #define foreach_memif_plugin_api_msg \
90 _(MEMIF_CREATE, memif_create) \
91 _(MEMIF_DELETE, memif_delete) \
92 _(MEMIF_DETAILS, memif_details) \
93 _(MEMIF_DUMP, memif_dump) \
96 * @brief Message handler for memif_create API.
97 * @param mp vl_api_memif_create_t * mp the api message
100 vl_api_memif_create_t_handler (vl_api_memif_create_t * mp)
102 memif_main_t *mm = &memif_main;
103 vlib_main_t *vm = vlib_get_main ();
104 vl_api_memif_create_reply_t *rmp;
105 memif_create_if_args_t args = { 0 };
106 u32 ring_size = MEMIF_DEFAULT_RING_SIZE;
107 static const u8 empty_hw_addr[6];
111 args.key = clib_net_to_host_u64 (mp->key);
113 /* socket filename */
114 mp->socket_filename[ARRAY_LEN (mp->socket_filename) - 1] = 0;
115 if (strlen ((char *) mp->socket_filename) > 0)
117 vec_validate (args.socket_filename,
118 strlen ((char *) mp->socket_filename));
119 strncpy ((char *) args.socket_filename, (char *) mp->socket_filename,
120 vec_len (args.socket_filename));
124 args.is_master = (mp->role == 0);
125 if (args.is_master == 0)
127 args.rx_queues = mp->rx_queues;
128 args.tx_queues = mp->tx_queues;
134 ring_size = ntohl (mp->ring_size);
136 if (!is_pow2 (ring_size))
138 rv = VNET_API_ERROR_INVALID_ARGUMENT;
141 args.log2_ring_size = min_log2 (ring_size);
144 args.buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
147 args.buffer_size = ntohs (mp->buffer_size);
151 if (memcmp (mp->hw_addr, empty_hw_addr, 6) != 0)
153 memcpy (args.hw_addr, mp->hw_addr, 6);
154 args.hw_addr_set = 1;
157 rv = memif_create_if (vm, &args);
161 REPLY_MACRO2 (VL_API_MEMIF_CREATE_REPLY,
163 rmp->sw_if_index = htonl (args.sw_if_index);
169 * @brief Message handler for memif_delete API.
170 * @param mp vl_api_memif_delete_t * mp the api message
173 vl_api_memif_delete_t_handler (vl_api_memif_delete_t * mp)
175 memif_main_t *mm = &memif_main;
177 vlib_main_t *vm = vlib_get_main ();
178 vl_api_memif_delete_reply_t *rmp;
179 u32 sw_if_index = ntohl (mp->sw_if_index);
183 pool_foreach (mif, mm->interfaces,
185 if (sw_if_index == mif->sw_if_index)
187 rv = memif_delete_if (vm, mif->key);
193 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
196 REPLY_MACRO (VL_API_MEMIF_DELETE_REPLY);
200 send_memif_details (unix_shared_memory_queue_t * q,
202 vnet_sw_interface_t * swif,
203 u8 * interface_name, u32 context)
205 vl_api_memif_details_t *mp;
206 vnet_main_t *vnm = vnet_get_main ();
207 memif_main_t *mm = &memif_main;
208 vnet_hw_interface_t *hwif;
210 hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
212 mp = vl_msg_api_alloc (sizeof (*mp));
213 memset (mp, 0, sizeof (*mp));
215 mp->_vl_msg_id = htons (VL_API_MEMIF_DETAILS + mm->msg_id_base);
216 mp->context = context;
218 mp->sw_if_index = htonl (swif->sw_if_index);
219 strncpy ((char *) mp->if_name,
220 (char *) interface_name, ARRAY_LEN (mp->if_name) - 1);
221 memcpy (mp->hw_addr, hwif->hw_address, ARRAY_LEN (mp->hw_addr));
223 mp->key = clib_host_to_net_u64 (mif->key);
224 mp->role = (mif->flags & MEMIF_IF_FLAG_IS_SLAVE) ? 1 : 0;
225 strncpy ((char *) mp->socket_filename,
226 (char *) mif->socket_filename,
227 ARRAY_LEN (mp->socket_filename) - 1);
229 mp->ring_size = htonl (1 << mif->log2_ring_size);
230 mp->buffer_size = htons (mif->buffer_size);
232 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
233 mp->link_up_down = (hwif->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
235 vl_msg_api_send_shmem (q, (u8 *) & mp);
239 * @brief Message handler for memif_dump API.
240 * @param mp vl_api_memif_dump_t * mp the api message
243 vl_api_memif_dump_t_handler (vl_api_memif_dump_t * mp)
245 memif_main_t *mm = &memif_main;
246 vnet_main_t *vnm = vnet_get_main ();
247 vnet_sw_interface_t *swif;
250 unix_shared_memory_queue_t *q;
252 q = vl_api_client_index_to_input_queue (mp->client_index);
257 pool_foreach (mif, mm->interfaces,
259 swif = vnet_get_sw_interface (vnm, mif->sw_if_index);
261 if_name = format (if_name, "%U%c",
262 format_vnet_sw_interface_name,
265 send_memif_details (q, mif, swif, if_name, mp->context);
266 _vec_len (if_name) = 0;
274 * @brief Message handler for memif_details API.
275 * @param mp vl_api_memif_details_t * mp the api message
278 vl_api_memif_details_t_handler (vl_api_memif_details_t * mp)
280 clib_warning ("BUG");
283 #define vl_msg_name_crc_list
284 #include <memif/memif_all_api_h.h>
285 #undef vl_msg_name_crc_list
288 setup_message_id_table (memif_main_t * mm, api_main_t * am)
290 #define _(id,n,crc) \
291 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + mm->msg_id_base);
292 foreach_vl_msg_name_crc_memif;
296 /* Set up the API message handling tables */
298 memif_plugin_api_hookup (vlib_main_t * vm)
300 memif_main_t *mm = &memif_main;
301 api_main_t *am = &api_main;
304 /* Construct the API name */
305 name = format (0, "memif_%08x%c", api_version, 0);
307 /* Ask for a correctly-sized block of API message decode slots */
308 mm->msg_id_base = vl_msg_api_get_msg_ids
309 ((char *) name, VL_MSG_FIRST_AVAILABLE);
312 vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base), \
314 vl_api_##n##_t_handler, \
316 vl_api_##n##_t_endian, \
317 vl_api_##n##_t_print, \
318 sizeof(vl_api_##n##_t), 1);
319 foreach_memif_plugin_api_msg;
323 * Set up the (msg_name, crc, message-id) table
325 setup_message_id_table (mm, am);
332 * fd.io coding-style-patch-verification: ON
335 * eval: (c-set-style "gnu")