FD.io VPP  v18.01-8-g0eacf49
Vector Packet Processing
memif_api.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * memif_api.c - memif api
4  *
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:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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  *------------------------------------------------------------------
18  */
19 
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>
25 
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28 
29 
30 /* define message IDs */
31 #include <memif/memif_msg_enum.h>
32 
33 /* define message structures */
34 #define vl_typedefs
35 #include <memif/memif_all_api_h.h>
36 #undef vl_typedefs
37 
38 /* define generated endian-swappers */
39 #define vl_endianfun
40 #include <memif/memif_all_api_h.h>
41 #undef vl_endianfun
42 
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #define vl_printfun
46 #include <memif/memif_all_api_h.h>
47 #undef vl_printfun
48 
49 /* Get the API version number */
50 #define vl_api_version(n,v) static u32 api_version=(v);
51 #include <memif/memif_all_api_h.h>
52 #undef vl_api_version
53 
54 /*
55  * A handy macro to set up a message reply.
56  * Assumes that the following variables are available:
57  * mp - pointer to request message
58  * rmp - pointer to reply message type
59  * rv - return value
60  */
61 #define REPLY_MACRO(t) \
62 do { \
63  unix_shared_memory_queue_t * q = \
64  vl_api_client_index_to_input_queue (mp->client_index); \
65  if (!q) \
66  return; \
67  \
68  rmp = vl_msg_api_alloc (sizeof (*rmp)); \
69  rmp->_vl_msg_id = htons ((t)+mm->msg_id_base); \
70  rmp->context = mp->context; \
71  rmp->retval = htonl (rv); \
72  \
73  vl_msg_api_send_shmem (q, (u8 *)&rmp); \
74 } while(0);
75 
76 #define REPLY_MACRO2(t, body) \
77 do { \
78  unix_shared_memory_queue_t * q = \
79  vl_api_client_index_to_input_queue (mp->client_index); \
80  if (!q) \
81  return; \
82  \
83  rmp = vl_msg_api_alloc (sizeof (*rmp)); \
84  rmp->_vl_msg_id = htons ((t)+mm->msg_id_base); \
85  rmp->context = mp->context; \
86  rmp->retval = htonl (rv); \
87  do {body;} while (0); \
88  vl_msg_api_send_shmem (q, (u8 *)&rmp); \
89 } while(0);
90 
91 #define foreach_memif_plugin_api_msg \
92 _(MEMIF_CREATE, memif_create) \
93 _(MEMIF_DELETE, memif_delete) \
94 _(MEMIF_DUMP, memif_dump) \
95 
96 /**
97  * @brief Message handler for memif_create API.
98  * @param mp vl_api_memif_create_t * mp the api message
99  */
100 void
102 {
103  memif_main_t *mm = &memif_main;
106  memif_create_if_args_t args = { 0 };
107  u32 ring_size = MEMIF_DEFAULT_RING_SIZE;
108  static const u8 empty_hw_addr[6];
109  int rv = 0;
110 
111  /* id */
112  args.id = clib_net_to_host_u32 (mp->id);
113 
114  /* socket filename */
115  mp->socket_filename[ARRAY_LEN (mp->socket_filename) - 1] = 0;
116  if (strlen ((char *) mp->socket_filename) > 0)
117  {
119  strlen ((char *) mp->socket_filename));
120  strncpy ((char *) args.socket_filename, (char *) mp->socket_filename,
121  vec_len (args.socket_filename));
122  }
123 
124  /* secret */
125  mp->secret[ARRAY_LEN (mp->secret) - 1] = 0;
126  if (strlen ((char *) mp->secret) > 0)
127  {
128  vec_validate (args.secret, strlen ((char *) mp->secret));
129  strncpy ((char *) args.secret, (char *) mp->secret,
130  vec_len (args.secret));
131  }
132 
133  /* role */
134  args.is_master = (mp->role == 0);
135 
136  /* mode */
137  args.mode = mp->mode;
138 
139  /* rx/tx queues */
140  if (args.is_master == 0)
141  {
144  if (mp->rx_queues)
145  {
146  args.rx_queues = mp->rx_queues;
147  }
148  if (mp->tx_queues)
149  {
150  args.tx_queues = mp->tx_queues;
151  }
152  }
153 
154  /* ring size */
155  if (mp->ring_size)
156  {
157  ring_size = ntohl (mp->ring_size);
158  }
159  if (!is_pow2 (ring_size))
160  {
161  rv = VNET_API_ERROR_INVALID_ARGUMENT;
162  goto reply;
163  }
164  args.log2_ring_size = min_log2 (ring_size);
165 
166  /* buffer size */
168  if (mp->buffer_size)
169  {
170  args.buffer_size = ntohs (mp->buffer_size);
171  }
172 
173  /* MAC address */
174  if (memcmp (mp->hw_addr, empty_hw_addr, 6) != 0)
175  {
176  memcpy (args.hw_addr, mp->hw_addr, 6);
177  args.hw_addr_set = 1;
178  }
179 
180  rv = memif_create_if (vm, &args);
181 
182  vec_free (args.socket_filename);
183  vec_free (args.secret);
184 
185 reply:
186  /* *INDENT-OFF* */
187  REPLY_MACRO2 (VL_API_MEMIF_CREATE_REPLY,
188  ({
189  rmp->sw_if_index = htonl (args.sw_if_index);
190  }));
191  /* *INDENT-ON* */
192 }
193 
194 /**
195  * @brief Message handler for memif_delete API.
196  * @param mp vl_api_memif_delete_t * mp the api message
197  */
198 void
200 {
201  memif_main_t *mm = &memif_main;
203  vnet_main_t *vnm = vnet_get_main ();
204  vl_api_memif_delete_reply_t *rmp;
206  vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
207  memif_if_t *mif;
208  int rv = 0;
209 
210  if (hi == NULL || memif_device_class.index != hi->dev_class_index)
211  rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
212  else
213  {
214  mif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
215  rv = memif_delete_if (vm, mif);
216  }
217 
218  REPLY_MACRO (VL_API_MEMIF_DELETE_REPLY);
219 }
220 
221 static void
223  memif_if_t * mif,
224  vnet_sw_interface_t * swif,
225  u8 * interface_name, u32 context)
226 {
228  vnet_main_t *vnm = vnet_get_main ();
229  memif_main_t *mm = &memif_main;
231  mif->socket_file_index);
232  vnet_hw_interface_t *hwif;
233 
234  hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
235 
236  mp = vl_msg_api_alloc (sizeof (*mp));
237  memset (mp, 0, sizeof (*mp));
238 
239  mp->_vl_msg_id = htons (VL_API_MEMIF_DETAILS + mm->msg_id_base);
240  mp->context = context;
241 
242  mp->sw_if_index = htonl (swif->sw_if_index);
243  strncpy ((char *) mp->if_name,
244  (char *) interface_name, ARRAY_LEN (mp->if_name) - 1);
245  memcpy (mp->hw_addr, hwif->hw_address, ARRAY_LEN (mp->hw_addr));
246 
247  mp->id = clib_host_to_net_u32 (mif->id);
248  mp->role = (mif->flags & MEMIF_IF_FLAG_IS_SLAVE) ? 1 : 0;
249  strncpy ((char *) mp->socket_filename,
250  (char *) msf->filename, ARRAY_LEN (mp->socket_filename) - 1);
251 
252  mp->ring_size = htonl (1 << mif->run.log2_ring_size);
253  mp->buffer_size = htons (mif->run.buffer_size);
254 
255  mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
256  mp->link_up_down = (hwif->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
257 
258  vl_msg_api_send_shmem (q, (u8 *) & mp);
259 }
260 
261 /**
262  * @brief Message handler for memif_dump API.
263  * @param mp vl_api_memif_dump_t * mp the api message
264  */
265 void
267 {
268  memif_main_t *mm = &memif_main;
269  vnet_main_t *vnm = vnet_get_main ();
270  vnet_sw_interface_t *swif;
271  memif_if_t *mif;
272  u8 *if_name = 0;
274 
276  if (q == 0)
277  return;
278 
279  /* *INDENT-OFF* */
280  pool_foreach (mif, mm->interfaces,
281  ({
282  swif = vnet_get_sw_interface (vnm, mif->sw_if_index);
283 
284  if_name = format (if_name, "%U%c",
285  format_vnet_sw_interface_name,
286  vnm, swif, 0);
287 
288  send_memif_details (q, mif, swif, if_name, mp->context);
289  _vec_len (if_name) = 0;
290  }));
291  /* *INDENT-ON* */
292 
293  vec_free (if_name);
294 }
295 
296 #define vl_msg_name_crc_list
297 #include <memif/memif_all_api_h.h>
298 #undef vl_msg_name_crc_list
299 
300 static void
302 {
303 #define _(id,n,crc) \
304  vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + mm->msg_id_base);
305  foreach_vl_msg_name_crc_memif;
306 #undef _
307 }
308 
309 /* Set up the API message handling tables */
310 clib_error_t *
312 {
313  memif_main_t *mm = &memif_main;
314  api_main_t *am = &api_main;
315  u8 *name;
316 
317  /* Construct the API name */
318  name = format (0, "memif_%08x%c", api_version, 0);
319 
320  /* Ask for a correctly-sized block of API message decode slots */
322  ((char *) name, VL_MSG_FIRST_AVAILABLE);
323 
324 #define _(N,n) \
325  vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base), \
326  #n, \
327  vl_api_##n##_t_handler, \
328  vl_noop_handler, \
329  vl_api_##n##_t_endian, \
330  vl_api_##n##_t_print, \
331  sizeof(vl_api_##n##_t), 1);
333 #undef _
334 
335  /*
336  * Set up the (msg_name, crc, message-id) table
337  */
338  setup_message_id_table (mm, am);
339 
340  vec_free (name);
341  return 0;
342 }
343 
344 /*
345  * fd.io coding-style-patch-verification: ON
346  *
347  * Local Variables:
348  * eval: (c-set-style "gnu")
349  * End:
350  */
memif_if_t * interfaces
Definition: private.h:181
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:432
vmrglw vmrglh hi
u16 vl_msg_api_get_msg_ids(const char *name, int n)
Definition: api_shared.c:872
void vl_api_memif_dump_t_handler(vl_api_memif_dump_t *mp)
Message handler for memif_dump API.
Definition: memif_api.c:266
Create memory interface.
Definition: memif.api:33
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
Create memory interface response.
Definition: memif.api:55
memif_socket_file_t * socket_files
Definition: private.h:184
memif_log2_ring_size_t log2_ring_size
Definition: private.h:154
#define NULL
Definition: clib.h:55
struct memif_if_t::@366 run
memif_interface_mode_t mode
Definition: private.h:208
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
#define VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:394
Delete memory interface.
Definition: memif.api:67
Dump all memory interfaces.
Definition: memif.api:116
static uword min_log2(uword x)
Definition: clib.h:197
u8 socket_filename[128]
Definition: memif.api:43
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:438
clib_error_t * memif_plugin_api_hookup(vlib_main_t *vm)
Definition: memif_api.c:311
uword socket_file_index
Definition: private.h:139
u16 buffer_size
Definition: private.h:157
memif_log2_ring_size_t log2_ring_size
Definition: private.h:209
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
vnet_device_class_t memif_device_class
u16 msg_id_base
API message ID base.
Definition: private.h:178
void * vl_msg_api_alloc(int nbytes)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:459
static void setup_message_id_table(memif_main_t *mm, api_main_t *am)
Definition: memif_api.c:301
#define REPLY_MACRO(t)
Definition: memif_api.c:61
int memif_delete_if(vlib_main_t *vm, memif_if_t *mif)
Definition: memif.c:466
void vl_api_memif_create_t_handler(vl_api_memif_create_t *mp)
Message handler for memif_create API.
Definition: memif_api.c:101
memif_interface_id_t id
Definition: private.h:204
int memif_create_if(vlib_main_t *vm, memif_create_if_args_t *args)
Definition: memif.c:543
#define MEMIF_DEFAULT_TX_QUEUES
Definition: private.h:23
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:198
api_main_t api_main
Definition: api_shared.c:35
vlib_main_t * vm
Definition: buffer.c:283
Memory interface details structure.
Definition: memif.api:91
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:336
unix_shared_memory_queue_t * vl_api_client_index_to_input_queue(u32 index)
#define ARRAY_LEN(x)
Definition: clib.h:59
void vl_api_memif_delete_t_handler(vl_api_memif_delete_t *mp)
Message handler for memif_delete API.
Definition: memif_api.c:199
#define MEMIF_DEFAULT_BUFFER_SIZE
Definition: private.h:24
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:576
void vl_msg_api_send_shmem(unix_shared_memory_queue_t *q, u8 *elem)
unsigned int u32
Definition: types.h:88
u32 flags
Definition: private.h:128
#define MEMIF_DEFAULT_RX_QUEUES
Definition: private.h:22
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
static uword is_pow2(uword x)
Definition: clib.h:280
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
u8 socket_filename[128]
Definition: memif.api:103
#define foreach_memif_plugin_api_msg
Definition: memif_api.c:91
#define REPLY_MACRO2(t, body)
Definition: memif_api.c:76
memif_interface_id_t id
Definition: private.h:129
static void send_memif_details(unix_shared_memory_queue_t *q, memif_if_t *mif, vnet_sw_interface_t *swif, u8 *interface_name, u32 context)
Definition: memif_api.c:222
#define MEMIF_DEFAULT_RING_SIZE
Definition: private.h:21
memif_main_t memif_main
Definition: memif.c:43
struct _unix_shared_memory_queue unix_shared_memory_queue_t