FD.io VPP  v17.04-9-g99c0734
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 <memif/memif.h>
23 
24 #include <vlibapi/api.h>
25 #include <vlibmemory/api.h>
26 #include <vlibsocket/api.h>
27 
28 /* define message IDs */
29 #include <memif/memif_msg_enum.h>
30 
31 /* define message structures */
32 #define vl_typedefs
33 #include <memif/memif_all_api_h.h>
34 #undef vl_typedefs
35 
36 /* define generated endian-swappers */
37 #define vl_endianfun
38 #include <memif/memif_all_api_h.h>
39 #undef vl_endianfun
40 
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43 #define vl_printfun
44 #include <memif/memif_all_api_h.h>
45 #undef vl_printfun
46 
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>
50 #undef vl_api_version
51 
52 /*
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
57  * rv - return value
58  */
59 #define REPLY_MACRO(t) \
60 do { \
61  unix_shared_memory_queue_t * q = \
62  vl_api_client_index_to_input_queue (mp->client_index); \
63  if (!q) \
64  return; \
65  \
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); \
70  \
71  vl_msg_api_send_shmem (q, (u8 *)&rmp); \
72 } while(0);
73 
74 #define REPLY_MACRO2(t, body) \
75 do { \
76  unix_shared_memory_queue_t * q = \
77  vl_api_client_index_to_input_queue (mp->client_index); \
78  if (!q) \
79  return; \
80  \
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); \
87 } while(0);
88 
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) \
94 
95 /**
96  * @brief Message handler for memif_create API.
97  * @param mp vl_api_memif_create_t * mp the api message
98  */
99 void
101 {
102  memif_main_t *mm = &memif_main;
105  memif_create_if_args_t args = { 0 };
106  u32 ring_size = MEMIF_DEFAULT_RING_SIZE;
107  static const u8 empty_hw_addr[6];
108  int rv = 0;
109 
110  /* key */
111  args.key = clib_net_to_host_u64 (mp->key);
112 
113  /* socket filename */
114  mp->socket_filename[ARRAY_LEN (mp->socket_filename) - 1] = 0;
115  if (strlen ((char *) mp->socket_filename) > 0)
116  {
118  strlen ((char *) mp->socket_filename));
119  strncpy ((char *) args.socket_filename, (char *) mp->socket_filename,
120  vec_len (args.socket_filename));
121  }
122 
123  /* role */
124  args.is_master = (mp->role == 0);
125 
126  /* ring size */
127  if (mp->ring_size)
128  {
129  ring_size = ntohl (mp->ring_size);
130  }
131  if (!is_pow2 (ring_size))
132  {
133  rv = VNET_API_ERROR_INVALID_ARGUMENT;
134  goto reply;
135  }
136  args.log2_ring_size = min_log2 (ring_size);
137 
138  /* buffer size */
140  if (mp->buffer_size)
141  {
142  args.buffer_size = ntohs (mp->buffer_size);
143  }
144 
145  /* MAC address */
146  if (memcmp (mp->hw_addr, empty_hw_addr, 6) != 0)
147  {
148  memcpy (args.hw_addr, mp->hw_addr, 6);
149  args.hw_addr_set = 1;
150  }
151 
152  rv = memif_create_if (vm, &args);
153 
154 reply:
155  /* *INDENT-OFF* */
156  REPLY_MACRO2 (VL_API_MEMIF_CREATE_REPLY,
157  ({
158  rmp->sw_if_index = htonl (args.sw_if_index);
159  }));
160  /* *INDENT-ON* */
161 }
162 
163 /**
164  * @brief Message handler for memif_delete API.
165  * @param mp vl_api_memif_delete_t * mp the api message
166  */
167 void
169 {
170  memif_main_t *mm = &memif_main;
171  memif_if_t *mif;
174  u32 sw_if_index = ntohl (mp->sw_if_index);
175  int rv = 0;
176 
177  /* *INDENT-OFF* */
178  pool_foreach (mif, mm->interfaces,
179  ({
180  if (sw_if_index == mif->sw_if_index)
181  {
182  rv = memif_delete_if (vm, mif->key);
183  goto reply;
184  }
185  }));
186  /* *INDENT-ON* */
187 
188  rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
189 
190 reply:
191  REPLY_MACRO (VL_API_MEMIF_DELETE_REPLY);
192 }
193 
194 static void
196  memif_if_t * mif,
197  vnet_sw_interface_t * swif,
198  u8 * interface_name, u32 context)
199 {
201  vnet_main_t *vnm = vnet_get_main ();
202  memif_main_t *mm = &memif_main;
203  vnet_hw_interface_t *hwif;
204 
205  hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
206 
207  mp = vl_msg_api_alloc (sizeof (*mp));
208  memset (mp, 0, sizeof (*mp));
209 
210  mp->_vl_msg_id = htons (VL_API_MEMIF_DETAILS + mm->msg_id_base);
211  mp->context = context;
212 
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));
217 
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);
223 
224  mp->ring_size = htonl (1 << mif->log2_ring_size);
225  mp->buffer_size = htons (mif->buffer_size);
226 
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;
229 
230  vl_msg_api_send_shmem (q, (u8 *) & mp);
231 }
232 
233 /**
234  * @brief Message handler for memif_dump API.
235  * @param mp vl_api_memif_dump_t * mp the api message
236  */
237 void
239 {
240  memif_main_t *mm = &memif_main;
241  vnet_main_t *vnm = vnet_get_main ();
242  vnet_sw_interface_t *swif;
243  memif_if_t *mif;
244  u8 *if_name = 0;
246 
248  if (q == 0)
249  return;
250 
251  /* *INDENT-OFF* */
252  pool_foreach (mif, mm->interfaces,
253  ({
254  swif = vnet_get_sw_interface (vnm, mif->sw_if_index);
255 
256  if_name = format (if_name, "%U%c",
257  format_vnet_sw_interface_name,
258  vnm, swif, 0);
259 
260  send_memif_details (q, mif, swif, if_name, mp->context);
261  _vec_len (if_name) = 0;
262  }));
263  /* *INDENT-ON* */
264 
265  vec_free (if_name);
266 }
267 
268 /**
269  * @brief Message handler for memif_details API.
270  * @param mp vl_api_memif_details_t * mp the api message
271  */
272 void
274 {
275  clib_warning ("BUG");
276 }
277 
278 #define vl_msg_name_crc_list
279 #include <memif/memif_all_api_h.h>
280 #undef vl_msg_name_crc_list
281 
282 static void
284 {
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;
288 #undef _
289 }
290 
291 /* Set up the API message handling tables */
292 clib_error_t *
294 {
295  memif_main_t *mm = &memif_main;
296  api_main_t *am = &api_main;
297  u8 *name;
298 
299  /* Construct the API name */
300  name = format (0, "memif_%08x%c", api_version, 0);
301 
302  /* Ask for a correctly-sized block of API message decode slots */
304  ((char *) name, VL_MSG_FIRST_AVAILABLE);
305 
306 #define _(N,n) \
307  vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base), \
308  #n, \
309  vl_api_##n##_t_handler, \
310  vl_noop_handler, \
311  vl_api_##n##_t_endian, \
312  vl_api_##n##_t_print, \
313  sizeof(vl_api_##n##_t), 1);
315 #undef _
316 
317  /*
318  * Set up the (msg_name, crc, message-id) table
319  */
320  setup_message_id_table (mm, am);
321 
322  vec_free (name);
323  return 0;
324 }
325 
326 /*
327  * fd.io coding-style-patch-verification: ON
328  *
329  * Local Variables:
330  * eval: (c-set-style "gnu")
331  * End:
332  */
memif_if_t * interfaces
Definition: memif.h:143
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:436
void vl_api_memif_dump_t_handler(vl_api_memif_dump_t *mp)
Message handler for memif_dump API.
Definition: memif_api.c:238
Create memory interface.
Definition: memif.api:28
Delete host-interface response.
Definition: memif.api:72
void vl_msg_api_send_shmem(unix_shared_memory_queue_t *q, u8 *elem)
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:48
unix_shared_memory_queue_t * vl_api_client_index_to_input_queue(u32 index)
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
#define VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:379
Delete memory interface.
Definition: memif.api:60
Dump all memory interfaces.
Definition: memif.api:117
#define MEMIF_IF_FLAG_IS_SLAVE
Definition: memif.h:104
static uword min_log2(uword x)
Definition: clib.h:189
api_main_t api_main
Definition: api_shared.c:35
u8 socket_filename[128]
Definition: memif.api:35
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:376
clib_error_t * memif_plugin_api_hookup(vlib_main_t *vm)
Definition: memif_api.c:293
u16 buffer_size
Definition: memif.h:126
u8 log2_ring_size
Definition: memif.h:123
u16 msg_id_base
API message ID base.
Definition: memif.h:140
#define MEMIF_DEFAULT_RING_SIZE
Definition: memif.h:32
u8 * socket_filename
Definition: memif.h:119
static void setup_message_id_table(memif_main_t *mm, api_main_t *am)
Definition: memif_api.c:283
#define REPLY_MACRO(t)
Definition: memif_api.c:59
void vl_api_memif_create_t_handler(vl_api_memif_create_t *mp)
Message handler for memif_create API.
Definition: memif_api.c:100
u64 key
Definition: memif.h:109
int memif_create_if(vlib_main_t *vm, memif_create_if_args_t *args)
Definition: memif.c:786
void * vl_msg_api_alloc(int nbytes)
vlib_main_t * vm
Definition: buffer.c:276
Memory interface details structure.
Definition: memif.api:93
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:340
#define clib_warning(format, args...)
Definition: error.h:59
#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:168
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:536
unsigned int u32
Definition: types.h:88
u32 flags
Definition: memif.h:102
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
static uword is_pow2(uword x)
Definition: clib.h:272
void vl_api_memif_details_t_handler(vl_api_memif_details_t *mp)
Message handler for memif_details API.
Definition: memif_api.c:273
#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:104
#define foreach_memif_plugin_api_msg
Definition: memif_api.c:89
#define REPLY_MACRO2(t, body)
Definition: memif_api.c:74
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:195
u16 vl_msg_api_get_msg_ids(char *name, int n)
Definition: api_shared.c:831
memif_main_t memif_main
Definition: memif.c:47
struct _unix_shared_memory_queue unix_shared_memory_queue_t
#define MEMIF_DEFAULT_BUFFER_SIZE
Definition: memif.h:36