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