memif: autogenerate socket_ids
[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  * @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_api_error (
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_api_error (memif_create_if (vm, &args));
194
195   vec_free (args.secret);
196
197 reply:
198   /* *INDENT-OFF* */
199   REPLY_MACRO2 (VL_API_MEMIF_CREATE_REPLY,
200     ({
201       rmp->sw_if_index = htonl (args.sw_if_index);
202     }));
203   /* *INDENT-ON* */
204 }
205
206 /**
207  * @brief Message handler for memif_delete API.
208  * @param mp vl_api_memif_delete_t * mp the api message
209  */
210 void
211 vl_api_memif_delete_t_handler (vl_api_memif_delete_t * mp)
212 {
213   memif_main_t *mm = &memif_main;
214   vlib_main_t *vm = vlib_get_main ();
215   vnet_main_t *vnm = vnet_get_main ();
216   vl_api_memif_delete_reply_t *rmp;
217   vnet_hw_interface_t *hi;
218   memif_if_t *mif;
219   int rv = 0;
220
221   hi =
222     vnet_get_sup_hw_interface_api_visible_or_null (vnm,
223                                                    ntohl (mp->sw_if_index));
224
225   if (hi == NULL || memif_device_class.index != hi->dev_class_index)
226     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
227   else
228     {
229       mif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
230       rv = vnet_api_error (memif_delete_if (vm, mif));
231     }
232
233   REPLY_MACRO (VL_API_MEMIF_DELETE_REPLY);
234 }
235
236 static void
237 send_memif_details (vl_api_registration_t * reg,
238                     memif_if_t * mif,
239                     vnet_sw_interface_t * swif,
240                     u8 * interface_name, u32 context)
241 {
242   vl_api_memif_details_t *mp;
243   vnet_main_t *vnm = vnet_get_main ();
244   memif_main_t *mm = &memif_main;
245   vnet_hw_interface_t *hwif;
246   memif_socket_file_t *msf;
247
248   hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
249
250   mp = vl_msg_api_alloc (sizeof (*mp));
251   clib_memset (mp, 0, sizeof (*mp));
252
253   mp->_vl_msg_id = htons (VL_API_MEMIF_DETAILS + mm->msg_id_base);
254   mp->context = context;
255
256   mp->sw_if_index = htonl (swif->sw_if_index);
257   strncpy ((char *) mp->if_name,
258            (char *) interface_name, ARRAY_LEN (mp->if_name) - 1);
259
260   if (hwif->hw_address)
261     {
262       mac_address_encode ((mac_address_t *) hwif->hw_address, mp->hw_addr);
263     }
264
265   mp->id = clib_host_to_net_u32 (mif->id);
266
267   msf = pool_elt_at_index (mm->socket_files, mif->socket_file_index);
268   mp->socket_id = clib_host_to_net_u32 (msf->socket_id);
269
270   mp->role =
271     (mif->flags & MEMIF_IF_FLAG_IS_SLAVE) ? MEMIF_ROLE_API_SLAVE :
272     MEMIF_ROLE_API_MASTER;
273   mp->role = htonl (mp->role);
274   mp->mode = htonl (mif->mode);
275   mp->ring_size = htonl (1 << mif->run.log2_ring_size);
276   mp->buffer_size = htons (mif->run.buffer_size);
277   mp->zero_copy = (mif->flags & MEMIF_IF_FLAG_ZERO_COPY) ? 1 : 0;
278
279   mp->flags = 0;
280   mp->flags |= (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
281     IF_STATUS_API_FLAG_ADMIN_UP : 0;
282   mp->flags |= (hwif->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
283     IF_STATUS_API_FLAG_LINK_UP : 0;
284   mp->flags = htonl (mp->flags);
285
286
287   vl_api_send_msg (reg, (u8 *) mp);
288 }
289
290 /**
291  * @brief Message handler for memif_dump API.
292  * @param mp vl_api_memif_dump_t * mp the api message
293  */
294 void
295 vl_api_memif_dump_t_handler (vl_api_memif_dump_t * mp)
296 {
297   memif_main_t *mm = &memif_main;
298   vnet_main_t *vnm = vnet_get_main ();
299   vnet_sw_interface_t *swif;
300   memif_if_t *mif;
301   u8 *if_name = 0;
302   vl_api_registration_t *reg;
303
304   reg = vl_api_client_index_to_registration (mp->client_index);
305   if (!reg)
306     return;
307
308   /* *INDENT-OFF* */
309   pool_foreach (mif, mm->interfaces)
310      {
311       swif = vnet_get_sw_interface (vnm, mif->sw_if_index);
312
313       if_name = format (if_name, "%U%c",
314                         format_vnet_sw_interface_name,
315                         vnm, swif, 0);
316
317       send_memif_details (reg, mif, swif, if_name, mp->context);
318       vec_set_len (if_name, 0);
319     }
320   /* *INDENT-ON* */
321
322   vec_free (if_name);
323 }
324
325 static void
326 send_memif_socket_filename_details (vl_api_registration_t * reg,
327                                     u32 socket_id,
328                                     u8 * socket_filename, u32 context)
329 {
330   vl_api_memif_socket_filename_details_t *mp;
331   memif_main_t *mm = &memif_main;
332
333   mp = vl_msg_api_alloc (sizeof (*mp));
334   clib_memset (mp, 0, sizeof (*mp));
335
336   mp->_vl_msg_id = htons (VL_API_MEMIF_SOCKET_FILENAME_DETAILS
337                           + mm->msg_id_base);
338   mp->context = context;
339
340   mp->socket_id = clib_host_to_net_u32 (socket_id);
341   strncpy ((char *) mp->socket_filename,
342            (char *) socket_filename, ARRAY_LEN (mp->socket_filename) - 1);
343
344   vl_api_send_msg (reg, (u8 *) mp);
345 }
346
347 /**
348  * @brief Message handler for memif_socket_filename_dump API.
349  * @param mp vl_api_memif_socket_filename_dump_t api message
350  */
351 void
352   vl_api_memif_socket_filename_dump_t_handler
353   (vl_api_memif_socket_filename_dump_t * mp)
354 {
355   memif_main_t *mm = &memif_main;
356   vl_api_registration_t *reg;
357   u32 sock_id;
358   u32 msf_idx;
359
360   reg = vl_api_client_index_to_registration (mp->client_index);
361   if (!reg)
362     return;
363
364   /* *INDENT-OFF* */
365   hash_foreach (sock_id, msf_idx, mm->socket_file_index_by_sock_id,
366     ({
367       memif_socket_file_t *msf;
368       u8 *filename;
369
370       msf = pool_elt_at_index(mm->socket_files, msf_idx);
371       filename = msf->filename;
372       send_memif_socket_filename_details(reg, sock_id, filename, mp->context);
373     }));
374   /* *INDENT-ON* */
375 }
376
377 /* Set up the API message handling tables */
378 #include <memif/memif.api.c>
379 clib_error_t *
380 memif_plugin_api_hookup (vlib_main_t * vm)
381 {
382   memif_main_t *mm = &memif_main;
383
384   /* Ask for a correctly-sized block of API message decode slots */
385   mm->msg_id_base = setup_message_id_table ();
386   return 0;
387 }
388
389 /*
390  * fd.io coding-style-patch-verification: ON
391  *
392  * Local Variables:
393  * eval: (c-set-style "gnu")
394  * End:
395  */