memif: improve error reporting
[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   u32 len;
52   u8 *socket_filename;
53   vl_api_memif_socket_filename_add_del_reply_t *rmp;
54   int rv;
55
56   /* is_add */
57   is_add = mp->is_add;
58
59   /* socket_id */
60   socket_id = clib_net_to_host_u32 (mp->socket_id);
61   if (socket_id == 0 || socket_id == ~0)
62     {
63       rv = VNET_API_ERROR_INVALID_ARGUMENT;
64       goto reply;
65     }
66
67   /* socket filename */
68   socket_filename = 0;
69   mp->socket_filename[ARRAY_LEN (mp->socket_filename) - 1] = 0;
70   len = strlen ((char *) mp->socket_filename);
71   if (mp->is_add)
72     {
73       vec_validate (socket_filename, len);
74       memcpy (socket_filename, mp->socket_filename, len);
75     }
76
77   rv = vnet_api_error (
78     memif_socket_filename_add_del (is_add, socket_id, socket_filename));
79
80   vec_free (socket_filename);
81
82 reply:
83   REPLY_MACRO (VL_API_MEMIF_SOCKET_FILENAME_ADD_DEL_REPLY);
84 }
85
86
87 /**
88  * @brief Message handler for memif_create API.
89  * @param mp vl_api_memif_create_t * mp the api message
90  */
91 void
92 vl_api_memif_create_t_handler (vl_api_memif_create_t * mp)
93 {
94   memif_main_t *mm = &memif_main;
95   vlib_main_t *vm = vlib_get_main ();
96   vl_api_memif_create_reply_t *rmp;
97   memif_create_if_args_t args = { 0 };
98   u32 ring_size = MEMIF_DEFAULT_RING_SIZE;
99   static const u8 empty_hw_addr[6];
100   int rv = 0;
101   mac_address_t mac;
102
103   /* id */
104   args.id = clib_net_to_host_u32 (mp->id);
105
106   /* socket-id */
107   args.socket_id = clib_net_to_host_u32 (mp->socket_id);
108
109   /* secret */
110   mp->secret[ARRAY_LEN (mp->secret) - 1] = 0;
111   if (strlen ((char *) mp->secret) > 0)
112     {
113       vec_validate (args.secret, strlen ((char *) mp->secret));
114       strncpy ((char *) args.secret, (char *) mp->secret,
115                vec_len (args.secret));
116     }
117
118   /* role */
119   args.is_master = (ntohl (mp->role) == MEMIF_ROLE_API_MASTER);
120
121   /* mode */
122   args.mode = ntohl (mp->mode);
123
124   args.is_zero_copy = mp->no_zero_copy ? 0 : 1;
125
126   /* rx/tx queues */
127   if (args.is_master == 0)
128     {
129       args.rx_queues = MEMIF_DEFAULT_RX_QUEUES;
130       args.tx_queues = MEMIF_DEFAULT_TX_QUEUES;
131       if (mp->rx_queues)
132         {
133           args.rx_queues = mp->rx_queues;
134         }
135       if (mp->tx_queues)
136         {
137           args.tx_queues = mp->tx_queues;
138         }
139     }
140
141   /* ring size */
142   if (mp->ring_size)
143     {
144       ring_size = ntohl (mp->ring_size);
145     }
146   if (!is_pow2 (ring_size))
147     {
148       rv = VNET_API_ERROR_INVALID_ARGUMENT;
149       goto reply;
150     }
151   args.log2_ring_size = min_log2 (ring_size);
152
153   /* buffer size */
154   args.buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
155   if (mp->buffer_size)
156     {
157       args.buffer_size = ntohs (mp->buffer_size);
158     }
159
160   /* MAC address */
161   mac_address_decode (mp->hw_addr, &mac);
162   if (memcmp (&mac, empty_hw_addr, 6) != 0)
163     {
164       memcpy (args.hw_addr, &mac, 6);
165       args.hw_addr_set = 1;
166     }
167
168   rv = vnet_api_error (memif_create_if (vm, &args));
169
170   vec_free (args.secret);
171
172 reply:
173   /* *INDENT-OFF* */
174   REPLY_MACRO2 (VL_API_MEMIF_CREATE_REPLY,
175     ({
176       rmp->sw_if_index = htonl (args.sw_if_index);
177     }));
178   /* *INDENT-ON* */
179 }
180
181 /**
182  * @brief Message handler for memif_delete API.
183  * @param mp vl_api_memif_delete_t * mp the api message
184  */
185 void
186 vl_api_memif_delete_t_handler (vl_api_memif_delete_t * mp)
187 {
188   memif_main_t *mm = &memif_main;
189   vlib_main_t *vm = vlib_get_main ();
190   vnet_main_t *vnm = vnet_get_main ();
191   vl_api_memif_delete_reply_t *rmp;
192   vnet_hw_interface_t *hi;
193   memif_if_t *mif;
194   int rv = 0;
195
196   hi =
197     vnet_get_sup_hw_interface_api_visible_or_null (vnm,
198                                                    ntohl (mp->sw_if_index));
199
200   if (hi == NULL || memif_device_class.index != hi->dev_class_index)
201     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
202   else
203     {
204       mif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
205       rv = vnet_api_error (memif_delete_if (vm, mif));
206     }
207
208   REPLY_MACRO (VL_API_MEMIF_DELETE_REPLY);
209 }
210
211 static void
212 send_memif_details (vl_api_registration_t * reg,
213                     memif_if_t * mif,
214                     vnet_sw_interface_t * swif,
215                     u8 * interface_name, u32 context)
216 {
217   vl_api_memif_details_t *mp;
218   vnet_main_t *vnm = vnet_get_main ();
219   memif_main_t *mm = &memif_main;
220   vnet_hw_interface_t *hwif;
221   memif_socket_file_t *msf;
222
223   hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
224
225   mp = vl_msg_api_alloc (sizeof (*mp));
226   clib_memset (mp, 0, sizeof (*mp));
227
228   mp->_vl_msg_id = htons (VL_API_MEMIF_DETAILS + mm->msg_id_base);
229   mp->context = context;
230
231   mp->sw_if_index = htonl (swif->sw_if_index);
232   strncpy ((char *) mp->if_name,
233            (char *) interface_name, ARRAY_LEN (mp->if_name) - 1);
234
235   if (hwif->hw_address)
236     {
237       mac_address_encode ((mac_address_t *) hwif->hw_address, mp->hw_addr);
238     }
239
240   mp->id = clib_host_to_net_u32 (mif->id);
241
242   msf = pool_elt_at_index (mm->socket_files, mif->socket_file_index);
243   mp->socket_id = clib_host_to_net_u32 (msf->socket_id);
244
245   mp->role =
246     (mif->flags & MEMIF_IF_FLAG_IS_SLAVE) ? MEMIF_ROLE_API_SLAVE :
247     MEMIF_ROLE_API_MASTER;
248   mp->role = htonl (mp->role);
249   mp->mode = htonl (mif->mode);
250   mp->ring_size = htonl (1 << mif->run.log2_ring_size);
251   mp->buffer_size = htons (mif->run.buffer_size);
252   mp->zero_copy = (mif->flags & MEMIF_IF_FLAG_ZERO_COPY) ? 1 : 0;
253
254   mp->flags = 0;
255   mp->flags |= (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
256     IF_STATUS_API_FLAG_ADMIN_UP : 0;
257   mp->flags |= (hwif->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
258     IF_STATUS_API_FLAG_LINK_UP : 0;
259   mp->flags = htonl (mp->flags);
260
261
262   vl_api_send_msg (reg, (u8 *) mp);
263 }
264
265 /**
266  * @brief Message handler for memif_dump API.
267  * @param mp vl_api_memif_dump_t * mp the api message
268  */
269 void
270 vl_api_memif_dump_t_handler (vl_api_memif_dump_t * mp)
271 {
272   memif_main_t *mm = &memif_main;
273   vnet_main_t *vnm = vnet_get_main ();
274   vnet_sw_interface_t *swif;
275   memif_if_t *mif;
276   u8 *if_name = 0;
277   vl_api_registration_t *reg;
278
279   reg = vl_api_client_index_to_registration (mp->client_index);
280   if (!reg)
281     return;
282
283   /* *INDENT-OFF* */
284   pool_foreach (mif, mm->interfaces)
285      {
286       swif = vnet_get_sw_interface (vnm, mif->sw_if_index);
287
288       if_name = format (if_name, "%U%c",
289                         format_vnet_sw_interface_name,
290                         vnm, swif, 0);
291
292       send_memif_details (reg, mif, swif, if_name, mp->context);
293       vec_set_len (if_name, 0);
294     }
295   /* *INDENT-ON* */
296
297   vec_free (if_name);
298 }
299
300 static void
301 send_memif_socket_filename_details (vl_api_registration_t * reg,
302                                     u32 socket_id,
303                                     u8 * socket_filename, u32 context)
304 {
305   vl_api_memif_socket_filename_details_t *mp;
306   memif_main_t *mm = &memif_main;
307
308   mp = vl_msg_api_alloc (sizeof (*mp));
309   clib_memset (mp, 0, sizeof (*mp));
310
311   mp->_vl_msg_id = htons (VL_API_MEMIF_SOCKET_FILENAME_DETAILS
312                           + mm->msg_id_base);
313   mp->context = context;
314
315   mp->socket_id = clib_host_to_net_u32 (socket_id);
316   strncpy ((char *) mp->socket_filename,
317            (char *) socket_filename, ARRAY_LEN (mp->socket_filename) - 1);
318
319   vl_api_send_msg (reg, (u8 *) mp);
320 }
321
322 /**
323  * @brief Message handler for memif_socket_filename_dump API.
324  * @param mp vl_api_memif_socket_filename_dump_t api message
325  */
326 void
327   vl_api_memif_socket_filename_dump_t_handler
328   (vl_api_memif_socket_filename_dump_t * mp)
329 {
330   memif_main_t *mm = &memif_main;
331   vl_api_registration_t *reg;
332   u32 sock_id;
333   u32 msf_idx;
334
335   reg = vl_api_client_index_to_registration (mp->client_index);
336   if (!reg)
337     return;
338
339   /* *INDENT-OFF* */
340   hash_foreach (sock_id, msf_idx, mm->socket_file_index_by_sock_id,
341     ({
342       memif_socket_file_t *msf;
343       u8 *filename;
344
345       msf = pool_elt_at_index(mm->socket_files, msf_idx);
346       filename = msf->filename;
347       send_memif_socket_filename_details(reg, sock_id, filename, mp->context);
348     }));
349   /* *INDENT-ON* */
350 }
351
352 /* Set up the API message handling tables */
353 #include <memif/memif.api.c>
354 clib_error_t *
355 memif_plugin_api_hookup (vlib_main_t * vm)
356 {
357   memif_main_t *mm = &memif_main;
358
359   /* Ask for a correctly-sized block of API message decode slots */
360   mm->msg_id_base = setup_message_id_table ();
361   return 0;
362 }
363
364 /*
365  * fd.io coding-style-patch-verification: ON
366  *
367  * Local Variables:
368  * eval: (c-set-style "gnu")
369  * End:
370  */