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);
129 ring_size = ntohl (mp->ring_size);
131 if (!is_pow2 (ring_size))
133 rv = VNET_API_ERROR_INVALID_ARGUMENT;
136 args.log2_ring_size = min_log2 (ring_size);
139 args.buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
142 args.buffer_size = ntohs (mp->buffer_size);
146 if (memcmp (mp->hw_addr, empty_hw_addr, 6) != 0)
148 memcpy (args.hw_addr, mp->hw_addr, 6);
149 args.hw_addr_set = 1;
152 rv = memif_create_if (vm, &args);
156 REPLY_MACRO2 (VL_API_MEMIF_CREATE_REPLY,
158 rmp->sw_if_index = htonl (args.sw_if_index);
164 * @brief Message handler for memif_delete API.
165 * @param mp vl_api_memif_delete_t * mp the api message
168 vl_api_memif_delete_t_handler (vl_api_memif_delete_t * mp)
170 memif_main_t *mm = &memif_main;
172 vlib_main_t *vm = vlib_get_main ();
173 vl_api_memif_delete_reply_t *rmp;
174 u32 sw_if_index = ntohl (mp->sw_if_index);
178 pool_foreach (mif, mm->interfaces,
180 if (sw_if_index == mif->sw_if_index)
182 rv = memif_delete_if (vm, mif->key);
188 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
191 REPLY_MACRO (VL_API_MEMIF_DELETE_REPLY);
195 send_memif_details (unix_shared_memory_queue_t * q,
197 vnet_sw_interface_t * swif,
198 u8 * interface_name, u32 context)
200 vl_api_memif_details_t *mp;
201 vnet_main_t *vnm = vnet_get_main ();
202 memif_main_t *mm = &memif_main;
203 vnet_hw_interface_t *hwif;
205 hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
207 mp = vl_msg_api_alloc (sizeof (*mp));
208 memset (mp, 0, sizeof (*mp));
210 mp->_vl_msg_id = htons (VL_API_MEMIF_DETAILS + mm->msg_id_base);
211 mp->context = context;
213 mp->sw_if_index = htonl (swif->sw_if_index);
214 strncpy ((char *) mp->if_name,
215 (char *) interface_name, ARRAY_LEN (mp->if_name) - 1);
216 memcpy (mp->hw_addr, hwif->hw_address, ARRAY_LEN (mp->hw_addr));
218 mp->key = clib_host_to_net_u64 (mif->key);
219 mp->role = (mif->flags & MEMIF_IF_FLAG_IS_SLAVE) ? 1 : 0;
220 strncpy ((char *) mp->socket_filename,
221 (char *) mif->socket_filename,
222 ARRAY_LEN (mp->socket_filename) - 1);
224 mp->ring_size = htonl (1 << mif->log2_ring_size);
225 mp->buffer_size = htons (mif->buffer_size);
227 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
228 mp->link_up_down = (hwif->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
230 vl_msg_api_send_shmem (q, (u8 *) & mp);
234 * @brief Message handler for memif_dump API.
235 * @param mp vl_api_memif_dump_t * mp the api message
238 vl_api_memif_dump_t_handler (vl_api_memif_dump_t * mp)
240 memif_main_t *mm = &memif_main;
241 vnet_main_t *vnm = vnet_get_main ();
242 vnet_sw_interface_t *swif;
245 unix_shared_memory_queue_t *q;
247 q = vl_api_client_index_to_input_queue (mp->client_index);
252 pool_foreach (mif, mm->interfaces,
254 swif = vnet_get_sw_interface (vnm, mif->sw_if_index);
256 if_name = format (if_name, "%U%c",
257 format_vnet_sw_interface_name,
260 send_memif_details (q, mif, swif, if_name, mp->context);
261 _vec_len (if_name) = 0;
269 * @brief Message handler for memif_details API.
270 * @param mp vl_api_memif_details_t * mp the api message
273 vl_api_memif_details_t_handler (vl_api_memif_details_t * mp)
275 clib_warning ("BUG");
278 #define vl_msg_name_crc_list
279 #include <memif/memif_all_api_h.h>
280 #undef vl_msg_name_crc_list
283 setup_message_id_table (memif_main_t * mm, api_main_t * am)
285 #define _(id,n,crc) \
286 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + mm->msg_id_base);
287 foreach_vl_msg_name_crc_memif;
291 /* Set up the API message handling tables */
293 memif_plugin_api_hookup (vlib_main_t * vm)
295 memif_main_t *mm = &memif_main;
296 api_main_t *am = &api_main;
299 /* Construct the API name */
300 name = format (0, "memif_%08x%c", api_version, 0);
302 /* Ask for a correctly-sized block of API message decode slots */
303 mm->msg_id_base = vl_msg_api_get_msg_ids
304 ((char *) name, VL_MSG_FIRST_AVAILABLE);
307 vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base), \
309 vl_api_##n##_t_handler, \
311 vl_api_##n##_t_endian, \
312 vl_api_##n##_t_print, \
313 sizeof(vl_api_##n##_t), 1);
314 foreach_memif_plugin_api_msg;
318 * Set up the (msg_name, crc, message-id) table
320 setup_message_id_table (mm, am);
327 * fd.io coding-style-patch-verification: ON
330 * eval: (c-set-style "gnu")