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