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