Add memif - packet memory interface for intra-host communication
[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
126   /* ring size */
127   if (mp->ring_size)
128     {
129       ring_size = ntohl (mp->ring_size);
130     }
131   if (!is_pow2 (ring_size))
132     {
133       rv = VNET_API_ERROR_INVALID_ARGUMENT;
134       goto reply;
135     }
136   args.log2_ring_size = min_log2 (ring_size);
137
138   /* buffer size */
139   args.buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
140   if (mp->buffer_size)
141     {
142       args.buffer_size = ntohs (mp->buffer_size);
143     }
144
145   /* MAC address */
146   if (memcmp (mp->hw_addr, empty_hw_addr, 6) != 0)
147     {
148       memcpy (args.hw_addr, mp->hw_addr, 6);
149       args.hw_addr_set = 1;
150     }
151
152   rv = memif_create_if (vm, &args);
153
154 reply:
155   /* *INDENT-OFF* */
156   REPLY_MACRO2 (VL_API_MEMIF_CREATE_REPLY,
157     ({
158        rmp->sw_if_index = htonl (args.sw_if_index);
159     }));
160   /* *INDENT-ON* */
161 }
162
163 /**
164  * @brief Message handler for memif_delete API.
165  * @param mp vl_api_memif_delete_t * mp the api message
166  */
167 void
168 vl_api_memif_delete_t_handler (vl_api_memif_delete_t * mp)
169 {
170   memif_main_t *mm = &memif_main;
171   memif_if_t *mif;
172   vlib_main_t *vm = vlib_get_main ();
173   vl_api_memif_delete_reply_t *rmp;
174   u32 sw_if_index = ntohl (mp->sw_if_index);
175   int rv = 0;
176
177   /* *INDENT-OFF* */
178   pool_foreach (mif, mm->interfaces,
179     ({
180       if (sw_if_index == mif->sw_if_index)
181         {
182           rv = memif_delete_if (vm, mif->key);
183           goto reply;
184         }
185     }));
186   /* *INDENT-ON* */
187
188   rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
189
190 reply:
191   REPLY_MACRO (VL_API_MEMIF_DELETE_REPLY);
192 }
193
194 static void
195 send_memif_details (unix_shared_memory_queue_t * q,
196                     memif_if_t * mif,
197                     vnet_sw_interface_t * swif,
198                     u8 * interface_name, u32 context)
199 {
200   vl_api_memif_details_t *mp;
201   vnet_main_t *vnm = vnet_get_main ();
202   memif_main_t *mm = &memif_main;
203   vnet_hw_interface_t *hwif;
204
205   hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
206
207   mp = vl_msg_api_alloc (sizeof (*mp));
208   memset (mp, 0, sizeof (*mp));
209
210   mp->_vl_msg_id = htons (VL_API_MEMIF_DETAILS + mm->msg_id_base);
211   mp->context = context;
212
213   mp->sw_if_index = htonl (swif->sw_if_index);
214   strncpy ((char *) mp->if_name,
215            (char *) interface_name, ARRAY_LEN (mp->if_name) - 1);
216   memcpy (mp->hw_addr, hwif->hw_address, ARRAY_LEN (mp->hw_addr));
217
218   mp->key = clib_host_to_net_u64 (mif->key);
219   mp->role = (mif->flags & MEMIF_IF_FLAG_IS_SLAVE) ? 1 : 0;
220   strncpy ((char *) mp->socket_filename,
221            (char *) mif->socket_filename,
222            ARRAY_LEN (mp->socket_filename) - 1);
223
224   mp->ring_size = htonl (1 << mif->log2_ring_size);
225   mp->buffer_size = htons (mif->buffer_size);
226
227   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
228   mp->link_up_down = (hwif->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
229
230   vl_msg_api_send_shmem (q, (u8 *) & mp);
231 }
232
233 /**
234  * @brief Message handler for memif_dump API.
235  * @param mp vl_api_memif_dump_t * mp the api message
236  */
237 void
238 vl_api_memif_dump_t_handler (vl_api_memif_dump_t * mp)
239 {
240   memif_main_t *mm = &memif_main;
241   vnet_main_t *vnm = vnet_get_main ();
242   vnet_sw_interface_t *swif;
243   memif_if_t *mif;
244   u8 *if_name = 0;
245   unix_shared_memory_queue_t *q;
246
247   q = vl_api_client_index_to_input_queue (mp->client_index);
248   if (q == 0)
249     return;
250
251   /* *INDENT-OFF* */
252   pool_foreach (mif, mm->interfaces,
253     ({
254       swif = vnet_get_sw_interface (vnm, mif->sw_if_index);
255
256       if_name = format (if_name, "%U%c",
257                         format_vnet_sw_interface_name,
258                         vnm, swif, 0);
259
260       send_memif_details (q, mif, swif, if_name, mp->context);
261       _vec_len (if_name) = 0;
262     }));
263   /* *INDENT-ON* */
264
265   vec_free (if_name);
266 }
267
268 /**
269  * @brief Message handler for memif_details API.
270  * @param mp vl_api_memif_details_t * mp the api message
271  */
272 void
273 vl_api_memif_details_t_handler (vl_api_memif_details_t * mp)
274 {
275   clib_warning ("BUG");
276 }
277
278 #define vl_msg_name_crc_list
279 #include <memif/memif_all_api_h.h>
280 #undef vl_msg_name_crc_list
281
282 static void
283 setup_message_id_table (memif_main_t * mm, api_main_t * am)
284 {
285 #define _(id,n,crc) \
286   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + mm->msg_id_base);
287   foreach_vl_msg_name_crc_memif;
288 #undef _
289 }
290
291 /* Set up the API message handling tables */
292 clib_error_t *
293 memif_plugin_api_hookup (vlib_main_t * vm)
294 {
295   memif_main_t *mm = &memif_main;
296   api_main_t *am = &api_main;
297   u8 *name;
298
299   /* Construct the API name */
300   name = format (0, "memif_%08x%c", api_version, 0);
301
302   /* Ask for a correctly-sized block of API message decode slots */
303   mm->msg_id_base = vl_msg_api_get_msg_ids
304     ((char *) name, VL_MSG_FIRST_AVAILABLE);
305
306 #define _(N,n)                                                  \
307     vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base),     \
308                            #n,                                  \
309                            vl_api_##n##_t_handler,              \
310                            vl_noop_handler,                     \
311                            vl_api_##n##_t_endian,               \
312                            vl_api_##n##_t_print,                \
313                            sizeof(vl_api_##n##_t), 1);
314   foreach_memif_plugin_api_msg;
315 #undef _
316
317   /*
318    * Set up the (msg_name, crc, message-id) table
319    */
320   setup_message_id_table (mm, am);
321
322   vec_free (name);
323   return 0;
324 }
325
326 /*
327  * fd.io coding-style-patch-verification: ON
328  *
329  * Local Variables:
330  * eval: (c-set-style "gnu")
331  * End:
332  */