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