FD.io VPP  v19.08-27-gf4dcae4
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 #define REPLY_MSG_ID_BASE mm->msg_id_base
56 
57 #define foreach_memif_plugin_api_msg \
58 _(MEMIF_SOCKET_FILENAME_ADD_DEL, memif_socket_filename_add_del) \
59 _(MEMIF_CREATE, memif_create) \
60 _(MEMIF_DELETE, memif_delete) \
61 _(MEMIF_SOCKET_FILENAME_DUMP, memif_socket_filename_dump) \
62 _(MEMIF_DUMP, memif_dump) \
63 
64 
65 /**
66  * @brief Message handler for memif_socket_filename_add_del API.
67  * @param mp the vl_api_memif_socket_filename_add_del_t API message
68  */
69 void
72 {
73  memif_main_t *mm = &memif_main;
74  u8 is_add;
75  u32 socket_id;
76  u32 len;
77  u8 *socket_filename;
78  vl_api_memif_socket_filename_add_del_reply_t *rmp;
79  int rv;
80 
81  /* is_add */
82  is_add = mp->is_add;
83 
84  /* socket_id */
85  socket_id = clib_net_to_host_u32 (mp->socket_id);
86  if (socket_id == 0 || socket_id == ~0)
87  {
88  rv = VNET_API_ERROR_INVALID_ARGUMENT;
89  goto reply;
90  }
91 
92  /* socket filename */
93  socket_filename = 0;
94  mp->socket_filename[ARRAY_LEN (mp->socket_filename) - 1] = 0;
95  len = strlen ((char *) mp->socket_filename);
96  if (len > 0)
97  {
98  vec_validate (socket_filename, len);
99  memcpy (socket_filename, mp->socket_filename, len);
100  }
101 
102  rv = memif_socket_filename_add_del (is_add, socket_id, socket_filename);
103 
104  vec_free (socket_filename);
105 
106 reply:
107  REPLY_MACRO (VL_API_MEMIF_SOCKET_FILENAME_ADD_DEL_REPLY);
108 }
109 
110 
111 /**
112  * @brief Message handler for memif_create API.
113  * @param mp vl_api_memif_create_t * mp the api message
114  */
115 void
117 {
118  memif_main_t *mm = &memif_main;
121  memif_create_if_args_t args = { 0 };
122  u32 ring_size = MEMIF_DEFAULT_RING_SIZE;
123  static const u8 empty_hw_addr[6];
124  int rv = 0;
125 
126  /* id */
127  args.id = clib_net_to_host_u32 (mp->id);
128 
129  /* socket-id */
130  args.socket_id = clib_net_to_host_u32 (mp->socket_id);
131 
132  /* secret */
133  mp->secret[ARRAY_LEN (mp->secret) - 1] = 0;
134  if (strlen ((char *) mp->secret) > 0)
135  {
136  vec_validate (args.secret, strlen ((char *) mp->secret));
137  strncpy ((char *) args.secret, (char *) mp->secret,
138  vec_len (args.secret));
139  }
140 
141  /* role */
142  args.is_master = (mp->role == 0);
143 
144  /* mode */
145  args.mode = mp->mode;
146 
147  /* enable zero-copy */
148  args.is_zero_copy = 1;
149 
150  /* rx/tx queues */
151  if (args.is_master == 0)
152  {
155  if (mp->rx_queues)
156  {
157  args.rx_queues = mp->rx_queues;
158  }
159  if (mp->tx_queues)
160  {
161  args.tx_queues = mp->tx_queues;
162  }
163  }
164 
165  /* ring size */
166  if (mp->ring_size)
167  {
168  ring_size = ntohl (mp->ring_size);
169  }
170  if (!is_pow2 (ring_size))
171  {
172  rv = VNET_API_ERROR_INVALID_ARGUMENT;
173  goto reply;
174  }
175  args.log2_ring_size = min_log2 (ring_size);
176 
177  /* buffer size */
179  if (mp->buffer_size)
180  {
181  args.buffer_size = ntohs (mp->buffer_size);
182  }
183 
184  /* MAC address */
185  if (memcmp (mp->hw_addr, empty_hw_addr, 6) != 0)
186  {
187  memcpy (args.hw_addr, mp->hw_addr, 6);
188  args.hw_addr_set = 1;
189  }
190 
191  rv = memif_create_if (vm, &args);
192 
193  vec_free (args.secret);
194 
195 reply:
196  /* *INDENT-OFF* */
197  REPLY_MACRO2 (VL_API_MEMIF_CREATE_REPLY,
198  ({
199  rmp->sw_if_index = htonl (args.sw_if_index);
200  }));
201  /* *INDENT-ON* */
202 }
203 
204 /**
205  * @brief Message handler for memif_delete API.
206  * @param mp vl_api_memif_delete_t * mp the api message
207  */
208 void
210 {
211  memif_main_t *mm = &memif_main;
213  vnet_main_t *vnm = vnet_get_main ();
214  vl_api_memif_delete_reply_t *rmp;
216  memif_if_t *mif;
217  int rv = 0;
218 
219  hi =
221  ntohl (mp->sw_if_index));
222 
223  if (hi == NULL || memif_device_class.index != hi->dev_class_index)
224  rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
225  else
226  {
227  mif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
228  rv = memif_delete_if (vm, mif);
229  }
230 
231  REPLY_MACRO (VL_API_MEMIF_DELETE_REPLY);
232 }
233 
234 static void
236  memif_if_t * mif,
237  vnet_sw_interface_t * swif,
238  u8 * interface_name, u32 context)
239 {
241  vnet_main_t *vnm = vnet_get_main ();
242  memif_main_t *mm = &memif_main;
243  vnet_hw_interface_t *hwif;
244  memif_socket_file_t *msf;
245 
246  hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
247 
248  mp = vl_msg_api_alloc (sizeof (*mp));
249  clib_memset (mp, 0, sizeof (*mp));
250 
251  mp->_vl_msg_id = htons (VL_API_MEMIF_DETAILS + mm->msg_id_base);
252  mp->context = context;
253 
254  mp->sw_if_index = htonl (swif->sw_if_index);
255  strncpy ((char *) mp->if_name,
256  (char *) interface_name, ARRAY_LEN (mp->if_name) - 1);
257 
258  if (hwif->hw_address)
259  {
260  memcpy (mp->hw_addr, hwif->hw_address, ARRAY_LEN (mp->hw_addr));
261  }
262 
263  mp->id = clib_host_to_net_u32 (mif->id);
264 
266  mp->socket_id = clib_host_to_net_u32 (msf->socket_id);
267 
268  mp->role = (mif->flags & MEMIF_IF_FLAG_IS_SLAVE) ? 1 : 0;
269  mp->ring_size = htonl (1 << mif->run.log2_ring_size);
270  mp->buffer_size = htons (mif->run.buffer_size);
271 
272  mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
273  mp->link_up_down = (hwif->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
274 
275  vl_api_send_msg (reg, (u8 *) mp);
276 }
277 
278 /**
279  * @brief Message handler for memif_dump API.
280  * @param mp vl_api_memif_dump_t * mp the api message
281  */
282 void
284 {
285  memif_main_t *mm = &memif_main;
286  vnet_main_t *vnm = vnet_get_main ();
287  vnet_sw_interface_t *swif;
288  memif_if_t *mif;
289  u8 *if_name = 0;
291 
293  if (!reg)
294  return;
295 
296  /* *INDENT-OFF* */
297  pool_foreach (mif, mm->interfaces,
298  ({
299  swif = vnet_get_sw_interface (vnm, mif->sw_if_index);
300 
301  if_name = format (if_name, "%U%c",
302  format_vnet_sw_interface_name,
303  vnm, swif, 0);
304 
305  send_memif_details (reg, mif, swif, if_name, mp->context);
306  _vec_len (if_name) = 0;
307  }));
308  /* *INDENT-ON* */
309 
310  vec_free (if_name);
311 }
312 
313 static void
315  u32 socket_id,
316  u8 * socket_filename, u32 context)
317 {
319  memif_main_t *mm = &memif_main;
320 
321  mp = vl_msg_api_alloc (sizeof (*mp));
322  clib_memset (mp, 0, sizeof (*mp));
323 
324  mp->_vl_msg_id = htons (VL_API_MEMIF_SOCKET_FILENAME_DETAILS
325  + mm->msg_id_base);
326  mp->context = context;
327 
328  mp->socket_id = clib_host_to_net_u32 (socket_id);
329  strncpy ((char *) mp->socket_filename,
330  (char *) socket_filename, ARRAY_LEN (mp->socket_filename) - 1);
331 
332  vl_api_send_msg (reg, (u8 *) mp);
333 }
334 
335 /**
336  * @brief Message handler for memif_socket_filename_dump API.
337  * @param mp vl_api_memif_socket_filename_dump_t api message
338  */
339 void
342 {
343  memif_main_t *mm = &memif_main;
345  u32 sock_id;
346  u32 msf_idx;
347 
349  if (!reg)
350  return;
351 
352  /* *INDENT-OFF* */
353  hash_foreach (sock_id, msf_idx, mm->socket_file_index_by_sock_id,
354  ({
355  memif_socket_file_t *msf;
356  u8 *filename;
357 
358  msf = pool_elt_at_index(mm->socket_files, msf_idx);
359  filename = msf->filename;
360  send_memif_socket_filename_details(reg, sock_id, filename, mp->context);
361  }));
362  /* *INDENT-ON* */
363 }
364 
365 #define vl_msg_name_crc_list
366 #include <memif/memif_all_api_h.h>
367 #undef vl_msg_name_crc_list
368 
369 static void
371 {
372 #define _(id,n,crc) \
373  vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + mm->msg_id_base);
374  foreach_vl_msg_name_crc_memif;
375 #undef _
376 }
377 
378 /* Set up the API message handling tables */
379 clib_error_t *
381 {
382  memif_main_t *mm = &memif_main;
383  api_main_t *am = &api_main;
384  u8 *name;
385 
386  /* Construct the API name */
387  name = format (0, "memif_%08x%c", api_version, 0);
388 
389  /* Ask for a correctly-sized block of API message decode slots */
391  ((char *) name, VL_MSG_FIRST_AVAILABLE);
392 
393 #define _(N,n) \
394  vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base), \
395  #n, \
396  vl_api_##n##_t_handler, \
397  vl_noop_handler, \
398  vl_api_##n##_t_endian, \
399  vl_api_##n##_t_print, \
400  sizeof(vl_api_##n##_t), 1);
402 #undef _
403 
404  /*
405  * Set up the (msg_name, crc, message-id) table
406  */
407  setup_message_id_table (mm, am);
408 
409  vec_free (name);
410  return 0;
411 }
412 
413 /*
414  * fd.io coding-style-patch-verification: ON
415  *
416  * Local Variables:
417  * eval: (c-set-style "gnu")
418  * End:
419  */
memif_if_t * interfaces
Definition: private.h:241
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:439
vmrglw vmrglh hi
void vl_api_memif_dump_t_handler(vl_api_memif_dump_t *mp)
Message handler for memif_dump API.
Definition: memif_api.c:283
Create memory interface.
Definition: memif.api:51
Memory interface details structure.
Definition: memif.api:98
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
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:73
Create or remove named socket file for memif interfaces.
Definition: memif.api:27
memif_socket_file_t * socket_files
Definition: private.h:244
memif_log2_ring_size_t log2_ring_size
Definition: private.h:183
#define REPLY_MACRO2(t, body)
#define NULL
Definition: clib.h:58
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:34
clib_memset(h->entries, 0, sizeof(h->entries[0])*entries)
memif_interface_mode_t mode
Definition: private.h:271
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
Delete memory interface.
Definition: memif.api:85
Dump all memory interfaces.
Definition: memif.api:156
void * vl_msg_api_alloc(int nbytes)
unsigned char u8
Definition: types.h:56
static uword min_log2(uword x)
Definition: clib.h:144
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
clib_error_t * memif_plugin_api_hookup(vlib_main_t *vm)
Definition: memif_api.c:380
void vl_api_memif_socket_filename_dump_t_handler(vl_api_memif_socket_filename_dump_t *mp)
Message handler for memif_socket_filename_dump API.
Definition: memif_api.c:341
uword socket_file_index
Definition: private.h:168
u16 buffer_size
Definition: private.h:186
memif_log2_ring_size_t log2_ring_size
Definition: private.h:272
vnet_hw_interface_flags_t flags
Definition: interface.h:505
#define hash_foreach(key_var, value_var, h, body)
Definition: hash.h:442
vnet_device_class_t memif_device_class
u16 msg_id_base
API message ID base.
Definition: private.h:238
unsigned int u32
Definition: types.h:88
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
static void setup_message_id_table(memif_main_t *mm, api_main_t *am)
Definition: memif_api.c:370
int memif_delete_if(vlib_main_t *vm, memif_if_t *mif)
Definition: memif.c:703
void vl_api_memif_create_t_handler(vl_api_memif_create_t *mp)
Message handler for memif_create API.
Definition: memif_api.c:116
#define REPLY_MACRO(t)
vnet_sw_interface_flags_t flags
Definition: interface.h:699
memif_interface_id_t id
Definition: private.h:266
int memif_create_if(vlib_main_t *vm, memif_create_if_args_t *args)
Definition: memif.c:783
#define MEMIF_DEFAULT_TX_QUEUES
Definition: private.h:24
u8 name[64]
Definition: memclnt.api:152
static void send_memif_details(vl_api_registration_t *reg, memif_if_t *mif, vnet_sw_interface_t *swif, u8 *interface_name, u32 context)
Definition: memif_api.c:235
u8 len
Definition: ip_types.api:90
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:203
An API client registration, only in vpp/vlib.
Definition: api_common.h:46
vlib_main_t * vm
Definition: buffer.c:312
Memory interface details structure.
Definition: memif.api:131
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
static vnet_hw_interface_t * vnet_get_sup_hw_interface_api_visible_or_null(vnet_main_t *vnm, u32 sw_if_index)
static void send_memif_socket_filename_details(vl_api_registration_t *reg, u32 socket_id, u8 *socket_filename, u32 context)
Definition: memif_api.c:314
int memif_socket_filename_add_del(u8 is_add, u32 sock_id, u8 *sock_filename)
Definition: memif.c:624
#define ARRAY_LEN(x)
Definition: clib.h:62
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:56
void vl_api_memif_delete_t_handler(vl_api_memif_delete_t *mp)
Message handler for memif_delete API.
Definition: memif_api.c:209
#define MEMIF_DEFAULT_BUFFER_SIZE
Definition: private.h:25
struct memif_if_t::@542 run
u32 flags
Definition: private.h:157
#define MEMIF_DEFAULT_RX_QUEUES
Definition: private.h:23
Dump the table of socket ids and corresponding filenames.
Definition: memif.api:109
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
static uword is_pow2(uword x)
Definition: clib.h:235
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define foreach_memif_plugin_api_msg
Definition: memif_api.c:57
memif_interface_id_t id
Definition: private.h:158
#define MEMIF_DEFAULT_RING_SIZE
Definition: private.h:22
uword * socket_file_index_by_sock_id
Definition: private.h:245
void vl_api_memif_socket_filename_add_del_t_handler(vl_api_memif_socket_filename_add_del_t *mp)
Message handler for memif_socket_filename_add_del API.
Definition: memif_api.c:71
memif_main_t memif_main
Definition: memif.c:43
u32 context
Definition: gre.api:45
api_main_t api_main
Definition: api_shared.c:35
u16 vl_msg_api_get_msg_ids(const char *name, int n)
Definition: api_shared.c:957