c11 safe string handling support
[vpp.git] / src / plugins / memif / memif_api.c
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     svm_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     svm_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_SOCKET_FILENAME_ADD_DEL, memif_socket_filename_add_del) \
93 _(MEMIF_CREATE, memif_create)                                           \
94 _(MEMIF_DELETE, memif_delete)                                           \
95 _(MEMIF_SOCKET_FILENAME_DUMP, memif_socket_filename_dump)               \
96 _(MEMIF_DUMP, memif_dump)                                               \
97
98
99 /**
100  * @brief Message handler for memif_socket_filename_add_del API.
101  * @param mp the vl_api_memif_socket_filename_add_del_t API message
102  */
103 void
104   vl_api_memif_socket_filename_add_del_t_handler
105   (vl_api_memif_socket_filename_add_del_t * mp)
106 {
107   memif_main_t *mm = &memif_main;
108   u8 is_add;
109   u32 socket_id;
110   u32 len;
111   u8 *socket_filename;
112   vl_api_memif_socket_filename_add_del_reply_t *rmp;
113   int rv;
114
115   /* is_add */
116   is_add = mp->is_add;
117
118   /* socket_id */
119   socket_id = clib_net_to_host_u32 (mp->socket_id);
120   if (socket_id == 0 || socket_id == ~0)
121     {
122       rv = VNET_API_ERROR_INVALID_ARGUMENT;
123       goto reply;
124     }
125
126   /* socket filename */
127   socket_filename = 0;
128   mp->socket_filename[ARRAY_LEN (mp->socket_filename) - 1] = 0;
129   len = strlen ((char *) mp->socket_filename);
130   if (len > 0)
131     {
132       vec_validate (socket_filename, len);
133       memcpy (socket_filename, mp->socket_filename, len);
134     }
135
136   rv = memif_socket_filename_add_del (is_add, socket_id, socket_filename);
137
138   vec_free (socket_filename);
139
140 reply:
141   REPLY_MACRO (VL_API_MEMIF_SOCKET_FILENAME_ADD_DEL_REPLY);
142 }
143
144
145 /**
146  * @brief Message handler for memif_create API.
147  * @param mp vl_api_memif_create_t * mp the api message
148  */
149 void
150 vl_api_memif_create_t_handler (vl_api_memif_create_t * mp)
151 {
152   memif_main_t *mm = &memif_main;
153   vlib_main_t *vm = vlib_get_main ();
154   vl_api_memif_create_reply_t *rmp;
155   memif_create_if_args_t args = { 0 };
156   u32 ring_size = MEMIF_DEFAULT_RING_SIZE;
157   static const u8 empty_hw_addr[6];
158   int rv = 0;
159
160   /* id */
161   args.id = clib_net_to_host_u32 (mp->id);
162
163   /* socket-id */
164   args.socket_id = clib_net_to_host_u32 (mp->socket_id);
165
166   /* secret */
167   mp->secret[ARRAY_LEN (mp->secret) - 1] = 0;
168   if (strlen ((char *) mp->secret) > 0)
169     {
170       vec_validate (args.secret, strlen ((char *) mp->secret));
171       strncpy ((char *) args.secret, (char *) mp->secret,
172                vec_len (args.secret));
173     }
174
175   /* role */
176   args.is_master = (mp->role == 0);
177
178   /* mode */
179   args.mode = mp->mode;
180
181   /* rx/tx queues */
182   if (args.is_master == 0)
183     {
184       args.rx_queues = MEMIF_DEFAULT_RX_QUEUES;
185       args.tx_queues = MEMIF_DEFAULT_TX_QUEUES;
186       if (mp->rx_queues)
187         {
188           args.rx_queues = mp->rx_queues;
189         }
190       if (mp->tx_queues)
191         {
192           args.tx_queues = mp->tx_queues;
193         }
194     }
195
196   /* ring size */
197   if (mp->ring_size)
198     {
199       ring_size = ntohl (mp->ring_size);
200     }
201   if (!is_pow2 (ring_size))
202     {
203       rv = VNET_API_ERROR_INVALID_ARGUMENT;
204       goto reply;
205     }
206   args.log2_ring_size = min_log2 (ring_size);
207
208   /* buffer size */
209   args.buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
210   if (mp->buffer_size)
211     {
212       args.buffer_size = ntohs (mp->buffer_size);
213     }
214
215   /* MAC address */
216   if (memcmp (mp->hw_addr, empty_hw_addr, 6) != 0)
217     {
218       memcpy (args.hw_addr, mp->hw_addr, 6);
219       args.hw_addr_set = 1;
220     }
221
222   rv = memif_create_if (vm, &args);
223
224   vec_free (args.secret);
225
226 reply:
227   /* *INDENT-OFF* */
228   REPLY_MACRO2 (VL_API_MEMIF_CREATE_REPLY,
229     ({
230       rmp->sw_if_index = htonl (args.sw_if_index);
231     }));
232   /* *INDENT-ON* */
233 }
234
235 /**
236  * @brief Message handler for memif_delete API.
237  * @param mp vl_api_memif_delete_t * mp the api message
238  */
239 void
240 vl_api_memif_delete_t_handler (vl_api_memif_delete_t * mp)
241 {
242   memif_main_t *mm = &memif_main;
243   vlib_main_t *vm = vlib_get_main ();
244   vnet_main_t *vnm = vnet_get_main ();
245   vl_api_memif_delete_reply_t *rmp;
246   vnet_hw_interface_t *hi =
247     vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
248   memif_if_t *mif;
249   int rv = 0;
250
251   if (hi == NULL || memif_device_class.index != hi->dev_class_index)
252     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
253   else
254     {
255       mif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
256       rv = memif_delete_if (vm, mif);
257     }
258
259   REPLY_MACRO (VL_API_MEMIF_DELETE_REPLY);
260 }
261
262 static void
263 send_memif_details (vl_api_registration_t * reg,
264                     memif_if_t * mif,
265                     vnet_sw_interface_t * swif,
266                     u8 * interface_name, u32 context)
267 {
268   vl_api_memif_details_t *mp;
269   vnet_main_t *vnm = vnet_get_main ();
270   memif_main_t *mm = &memif_main;
271   vnet_hw_interface_t *hwif;
272   memif_socket_file_t *msf;
273
274   hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
275
276   mp = vl_msg_api_alloc (sizeof (*mp));
277   clib_memset (mp, 0, sizeof (*mp));
278
279   mp->_vl_msg_id = htons (VL_API_MEMIF_DETAILS + mm->msg_id_base);
280   mp->context = context;
281
282   mp->sw_if_index = htonl (swif->sw_if_index);
283   strncpy ((char *) mp->if_name,
284            (char *) interface_name, ARRAY_LEN (mp->if_name) - 1);
285
286   if (hwif->hw_address)
287     {
288       memcpy (mp->hw_addr, hwif->hw_address, ARRAY_LEN (mp->hw_addr));
289     }
290
291   mp->id = clib_host_to_net_u32 (mif->id);
292
293   msf = pool_elt_at_index (mm->socket_files, mif->socket_file_index);
294   mp->socket_id = clib_host_to_net_u32 (msf->socket_id);
295
296   mp->role = (mif->flags & MEMIF_IF_FLAG_IS_SLAVE) ? 1 : 0;
297   mp->ring_size = htonl (1 << mif->run.log2_ring_size);
298   mp->buffer_size = htons (mif->run.buffer_size);
299
300   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
301   mp->link_up_down = (hwif->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
302
303   vl_api_send_msg (reg, (u8 *) mp);
304 }
305
306 /**
307  * @brief Message handler for memif_dump API.
308  * @param mp vl_api_memif_dump_t * mp the api message
309  */
310 void
311 vl_api_memif_dump_t_handler (vl_api_memif_dump_t * mp)
312 {
313   memif_main_t *mm = &memif_main;
314   vnet_main_t *vnm = vnet_get_main ();
315   vnet_sw_interface_t *swif;
316   memif_if_t *mif;
317   u8 *if_name = 0;
318   vl_api_registration_t *reg;
319
320   reg = vl_api_client_index_to_registration (mp->client_index);
321   if (!reg)
322     return;
323
324   /* *INDENT-OFF* */
325   pool_foreach (mif, mm->interfaces,
326     ({
327       swif = vnet_get_sw_interface (vnm, mif->sw_if_index);
328
329       if_name = format (if_name, "%U%c",
330                         format_vnet_sw_interface_name,
331                         vnm, swif, 0);
332
333       send_memif_details (reg, mif, swif, if_name, mp->context);
334       _vec_len (if_name) = 0;
335     }));
336   /* *INDENT-ON* */
337
338   vec_free (if_name);
339 }
340
341 static void
342 send_memif_socket_filename_details (vl_api_registration_t * reg,
343                                     u32 socket_id,
344                                     u8 * socket_filename, u32 context)
345 {
346   vl_api_memif_socket_filename_details_t *mp;
347   memif_main_t *mm = &memif_main;
348
349   mp = vl_msg_api_alloc (sizeof (*mp));
350   clib_memset (mp, 0, sizeof (*mp));
351
352   mp->_vl_msg_id = htons (VL_API_MEMIF_SOCKET_FILENAME_DETAILS
353                           + mm->msg_id_base);
354   mp->context = context;
355
356   mp->socket_id = clib_host_to_net_u32 (socket_id);
357   strncpy ((char *) mp->socket_filename,
358            (char *) socket_filename, ARRAY_LEN (mp->socket_filename) - 1);
359
360   vl_api_send_msg (reg, (u8 *) mp);
361 }
362
363 /**
364  * @brief Message handler for memif_socket_filename_dump API.
365  * @param mp vl_api_memif_socket_filename_dump_t api message
366  */
367 void
368   vl_api_memif_socket_filename_dump_t_handler
369   (vl_api_memif_socket_filename_dump_t * mp)
370 {
371   memif_main_t *mm = &memif_main;
372   vl_api_registration_t *reg;
373   u32 sock_id;
374   u32 msf_idx;
375
376   reg = vl_api_client_index_to_registration (mp->client_index);
377   if (!reg)
378     return;
379
380   /* *INDENT-OFF* */
381   hash_foreach (sock_id, msf_idx, mm->socket_file_index_by_sock_id,
382     ({
383       memif_socket_file_t *msf;
384       u8 *filename;
385
386       msf = pool_elt_at_index(mm->socket_files, msf_idx);
387       filename = msf->filename;
388       send_memif_socket_filename_details(reg, sock_id, filename, mp->context);
389     }));
390   /* *INDENT-ON* */
391 }
392
393 #define vl_msg_name_crc_list
394 #include <memif/memif_all_api_h.h>
395 #undef vl_msg_name_crc_list
396
397 static void
398 setup_message_id_table (memif_main_t * mm, api_main_t * am)
399 {
400 #define _(id,n,crc) \
401   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + mm->msg_id_base);
402   foreach_vl_msg_name_crc_memif;
403 #undef _
404 }
405
406 /* Set up the API message handling tables */
407 clib_error_t *
408 memif_plugin_api_hookup (vlib_main_t * vm)
409 {
410   memif_main_t *mm = &memif_main;
411   api_main_t *am = &api_main;
412   u8 *name;
413
414   /* Construct the API name */
415   name = format (0, "memif_%08x%c", api_version, 0);
416
417   /* Ask for a correctly-sized block of API message decode slots */
418   mm->msg_id_base = vl_msg_api_get_msg_ids
419     ((char *) name, VL_MSG_FIRST_AVAILABLE);
420
421 #define _(N,n)                                                  \
422     vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base),     \
423                            #n,                                  \
424                            vl_api_##n##_t_handler,              \
425                            vl_noop_handler,                     \
426                            vl_api_##n##_t_endian,               \
427                            vl_api_##n##_t_print,                \
428                            sizeof(vl_api_##n##_t), 1);
429   foreach_memif_plugin_api_msg;
430 #undef _
431
432   /*
433    * Set up the (msg_name, crc, message-id) table
434    */
435   setup_message_id_table (mm, am);
436
437   vec_free (name);
438   return 0;
439 }
440
441 /*
442  * fd.io coding-style-patch-verification: ON
443  *
444  * Local Variables:
445  * eval: (c-set-style "gnu")
446  * End:
447  */