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