65a4a09a09ed8bb37fba853d91d7062eaca1f9eb
[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 #include <vlibsocket/api.h>
29
30 /* define message IDs */
31 #include <memif/memif_msg_enum.h>
32
33 /* define message structures */
34 #define vl_typedefs
35 #include <memif/memif_all_api_h.h>
36 #undef vl_typedefs
37
38 /* define generated endian-swappers */
39 #define vl_endianfun
40 #include <memif/memif_all_api_h.h>
41 #undef vl_endianfun
42
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #define vl_printfun
46 #include <memif/memif_all_api_h.h>
47 #undef vl_printfun
48
49 /* Get the API version number */
50 #define vl_api_version(n,v) static u32 api_version=(v);
51 #include <memif/memif_all_api_h.h>
52 #undef vl_api_version
53
54 /*
55  * A handy macro to set up a message reply.
56  * Assumes that the following variables are available:
57  * mp - pointer to request message
58  * rmp - pointer to reply message type
59  * rv - return value
60  */
61 #define REPLY_MACRO(t)                                          \
62 do {                                                            \
63     unix_shared_memory_queue_t * q =                            \
64     vl_api_client_index_to_input_queue (mp->client_index);      \
65     if (!q)                                                     \
66         return;                                                 \
67                                                                 \
68     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
69     rmp->_vl_msg_id = htons ((t)+mm->msg_id_base);              \
70     rmp->context = mp->context;                                 \
71     rmp->retval = htonl (rv);                                   \
72                                                                 \
73     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
74 } while(0);
75
76 #define REPLY_MACRO2(t, body)                                   \
77 do {                                                            \
78     unix_shared_memory_queue_t * q =                            \
79     vl_api_client_index_to_input_queue (mp->client_index);      \
80     if (!q)                                                     \
81         return;                                                 \
82                                                                 \
83     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
84     rmp->_vl_msg_id = htons ((t)+mm->msg_id_base);              \
85     rmp->context = mp->context;                                 \
86     rmp->retval = htonl (rv);                                   \
87     do {body;} while (0);                                       \
88     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
89 } while(0);
90
91 #define foreach_memif_plugin_api_msg                     \
92 _(MEMIF_CREATE, memif_create)                            \
93 _(MEMIF_DELETE, memif_delete)                            \
94 _(MEMIF_DETAILS, memif_details)                          \
95 _(MEMIF_DUMP, memif_dump)                                \
96
97 /**
98  * @brief Message handler for memif_create API.
99  * @param mp vl_api_memif_create_t * mp the api message
100  */
101 void
102 vl_api_memif_create_t_handler (vl_api_memif_create_t * mp)
103 {
104   memif_main_t *mm = &memif_main;
105   vlib_main_t *vm = vlib_get_main ();
106   vl_api_memif_create_reply_t *rmp;
107   memif_create_if_args_t args = { 0 };
108   u32 ring_size = MEMIF_DEFAULT_RING_SIZE;
109   static const u8 empty_hw_addr[6];
110   int rv = 0;
111
112   /* id */
113   args.id = clib_net_to_host_u32 (mp->id);
114
115   /* socket filename */
116   mp->socket_filename[ARRAY_LEN (mp->socket_filename) - 1] = 0;
117   if (strlen ((char *) mp->socket_filename) > 0)
118     {
119       vec_validate (args.socket_filename,
120                     strlen ((char *) mp->socket_filename));
121       strncpy ((char *) args.socket_filename, (char *) mp->socket_filename,
122                vec_len (args.socket_filename));
123     }
124
125   /* secret */
126   mp->secret[ARRAY_LEN (mp->secret) - 1] = 0;
127   if (strlen ((char *) mp->secret) > 0)
128     {
129       vec_validate (args.secret, strlen ((char *) mp->secret));
130       strncpy ((char *) args.secret, (char *) mp->secret,
131                vec_len (args.secret));
132     }
133
134   /* role */
135   args.is_master = (mp->role == 0);
136
137   /* rx/tx queues */
138   if (args.is_master == 0)
139     {
140       args.rx_queues = MEMIF_DEFAULT_RX_QUEUES;
141       args.tx_queues = MEMIF_DEFAULT_TX_QUEUES;
142       if (mp->rx_queues)
143         {
144           args.rx_queues = mp->rx_queues;
145         }
146       if (mp->tx_queues)
147         {
148           args.tx_queues = mp->tx_queues;
149         }
150     }
151
152   /* ring size */
153   if (mp->ring_size)
154     {
155       ring_size = ntohl (mp->ring_size);
156     }
157   if (!is_pow2 (ring_size))
158     {
159       rv = VNET_API_ERROR_INVALID_ARGUMENT;
160       goto reply;
161     }
162   args.log2_ring_size = min_log2 (ring_size);
163
164   /* buffer size */
165   args.buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
166   if (mp->buffer_size)
167     {
168       args.buffer_size = ntohs (mp->buffer_size);
169     }
170
171   /* MAC address */
172   if (memcmp (mp->hw_addr, empty_hw_addr, 6) != 0)
173     {
174       memcpy (args.hw_addr, mp->hw_addr, 6);
175       args.hw_addr_set = 1;
176     }
177
178   rv = memif_create_if (vm, &args);
179
180   vec_free (args.socket_filename);
181   vec_free (args.secret);
182
183 reply:
184   /* *INDENT-OFF* */
185   REPLY_MACRO2 (VL_API_MEMIF_CREATE_REPLY,
186     ({
187        rmp->sw_if_index = htonl (args.sw_if_index);
188     }));
189   /* *INDENT-ON* */
190 }
191
192 /**
193  * @brief Message handler for memif_delete API.
194  * @param mp vl_api_memif_delete_t * mp the api message
195  */
196 void
197 vl_api_memif_delete_t_handler (vl_api_memif_delete_t * mp)
198 {
199   memif_main_t *mm = &memif_main;
200   vlib_main_t *vm = vlib_get_main ();
201   vnet_main_t *vnm = vnet_get_main ();
202   vl_api_memif_delete_reply_t *rmp;
203   vnet_hw_interface_t *hi =
204     vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
205   memif_if_t *mif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
206   int rv = 0;
207
208   if (hi == NULL || memif_device_class.index != hi->dev_class_index)
209     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
210   else
211     rv = memif_delete_if (vm, mif);
212
213   REPLY_MACRO (VL_API_MEMIF_DELETE_REPLY);
214 }
215
216 static void
217 send_memif_details (unix_shared_memory_queue_t * q,
218                     memif_if_t * mif,
219                     vnet_sw_interface_t * swif,
220                     u8 * interface_name, u32 context)
221 {
222   vl_api_memif_details_t *mp;
223   vnet_main_t *vnm = vnet_get_main ();
224   memif_main_t *mm = &memif_main;
225   memif_socket_file_t *msf = vec_elt_at_index (mm->socket_files,
226                                                mif->socket_file_index);
227   vnet_hw_interface_t *hwif;
228
229   hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
230
231   mp = vl_msg_api_alloc (sizeof (*mp));
232   memset (mp, 0, sizeof (*mp));
233
234   mp->_vl_msg_id = htons (VL_API_MEMIF_DETAILS + mm->msg_id_base);
235   mp->context = context;
236
237   mp->sw_if_index = htonl (swif->sw_if_index);
238   strncpy ((char *) mp->if_name,
239            (char *) interface_name, ARRAY_LEN (mp->if_name) - 1);
240   memcpy (mp->hw_addr, hwif->hw_address, ARRAY_LEN (mp->hw_addr));
241
242   mp->id = clib_host_to_net_u32 (mif->id);
243   mp->role = (mif->flags & MEMIF_IF_FLAG_IS_SLAVE) ? 1 : 0;
244   strncpy ((char *) mp->socket_filename,
245            (char *) msf->filename, ARRAY_LEN (mp->socket_filename) - 1);
246
247   mp->ring_size = htonl (1 << mif->run.log2_ring_size);
248   mp->buffer_size = htons (mif->run.buffer_size);
249
250   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
251   mp->link_up_down = (hwif->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
252
253   vl_msg_api_send_shmem (q, (u8 *) & mp);
254 }
255
256 /**
257  * @brief Message handler for memif_dump API.
258  * @param mp vl_api_memif_dump_t * mp the api message
259  */
260 void
261 vl_api_memif_dump_t_handler (vl_api_memif_dump_t * mp)
262 {
263   memif_main_t *mm = &memif_main;
264   vnet_main_t *vnm = vnet_get_main ();
265   vnet_sw_interface_t *swif;
266   memif_if_t *mif;
267   u8 *if_name = 0;
268   unix_shared_memory_queue_t *q;
269
270   q = vl_api_client_index_to_input_queue (mp->client_index);
271   if (q == 0)
272     return;
273
274   /* *INDENT-OFF* */
275   pool_foreach (mif, mm->interfaces,
276     ({
277       swif = vnet_get_sw_interface (vnm, mif->sw_if_index);
278
279       if_name = format (if_name, "%U%c",
280                         format_vnet_sw_interface_name,
281                         vnm, swif, 0);
282
283       send_memif_details (q, mif, swif, if_name, mp->context);
284       _vec_len (if_name) = 0;
285     }));
286   /* *INDENT-ON* */
287
288   vec_free (if_name);
289 }
290
291 /**
292  * @brief Message handler for memif_details API.
293  * @param mp vl_api_memif_details_t * mp the api message
294  */
295 void
296 vl_api_memif_details_t_handler (vl_api_memif_details_t * mp)
297 {
298   clib_warning ("BUG");
299 }
300
301 #define vl_msg_name_crc_list
302 #include <memif/memif_all_api_h.h>
303 #undef vl_msg_name_crc_list
304
305 static void
306 setup_message_id_table (memif_main_t * mm, api_main_t * am)
307 {
308 #define _(id,n,crc) \
309   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + mm->msg_id_base);
310   foreach_vl_msg_name_crc_memif;
311 #undef _
312 }
313
314 /* Set up the API message handling tables */
315 clib_error_t *
316 memif_plugin_api_hookup (vlib_main_t * vm)
317 {
318   memif_main_t *mm = &memif_main;
319   api_main_t *am = &api_main;
320   u8 *name;
321
322   /* Construct the API name */
323   name = format (0, "memif_%08x%c", api_version, 0);
324
325   /* Ask for a correctly-sized block of API message decode slots */
326   mm->msg_id_base = vl_msg_api_get_msg_ids
327     ((char *) name, VL_MSG_FIRST_AVAILABLE);
328
329 #define _(N,n)                                                  \
330     vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base),     \
331                            #n,                                  \
332                            vl_api_##n##_t_handler,              \
333                            vl_noop_handler,                     \
334                            vl_api_##n##_t_endian,               \
335                            vl_api_##n##_t_print,                \
336                            sizeof(vl_api_##n##_t), 1);
337   foreach_memif_plugin_api_msg;
338 #undef _
339
340   /*
341    * Set up the (msg_name, crc, message-id) table
342    */
343   setup_message_id_table (mm, am);
344
345   vec_free (name);
346   return 0;
347 }
348
349 /*
350  * fd.io coding-style-patch-verification: ON
351  *
352  * Local Variables:
353  * eval: (c-set-style "gnu")
354  * End:
355  */