vhost: convert vhost device driver to a plugin
[vpp.git] / src / plugins / vhost / vhost_user_api.c
1 /*
2  *------------------------------------------------------------------
3  * vhost-user_api.c - vhost-user api
4  *
5  * Copyright (c) 2016 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 <vnet/vnet.h>
21 #include <vlibmemory/api.h>
22
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vhost/vhost_user.h>
26 #include <vnet/ethernet/ethernet.h>
27 #include <vnet/ethernet/ethernet_types_api.h>
28
29 #include <vnet/format_fns.h>
30 #include <vhost/vhost_user.api_enum.h>
31 #include <vhost/vhost_user.api_types.h>
32
33 #define REPLY_MSG_ID_BASE msg_id_base
34 #include <vlibapi/api_helper_macros.h>
35
36 static u16 msg_id_base;
37
38 static void
39 vl_api_create_vhost_user_if_t_handler (vl_api_create_vhost_user_if_t * mp)
40 {
41   int rv = 0;
42   vl_api_create_vhost_user_if_reply_t *rmp;
43   vnet_main_t *vnm = vnet_get_main ();
44   vlib_main_t *vm = vlib_get_main ();
45   u64 disabled_features = (u64) (0ULL);
46   vhost_user_create_if_args_t args = { 0 };
47
48   args.sw_if_index = (u32) ~ 0;
49   args.feature_mask = (u64) ~ (0ULL);
50   if (mp->disable_mrg_rxbuf)
51     disabled_features = VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
52
53   if (mp->disable_indirect_desc)
54     disabled_features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
55
56   /*
57    * GSO and PACKED are not supported by feature mask via binary API. We
58    * disable GSO and PACKED feature in the feature mask. They may be enabled
59    * explicitly via enable_gso and enable_packed argument
60    */
61   disabled_features |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS |
62     VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
63
64   /* EVENT_IDX is disabled by default */
65   disabled_features |= VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
66   args.feature_mask &= ~disabled_features;
67
68   if (mp->use_custom_mac)
69     mac_address_decode (mp->mac_address, (mac_address_t *) args.hwaddr);
70
71   args.use_custom_mac = mp->use_custom_mac;
72   args.is_server = mp->is_server;
73   args.sock_filename = (char *) mp->sock_filename;
74   args.renumber = mp->renumber;
75   args.custom_dev_instance = ntohl (mp->custom_dev_instance);
76   args.enable_gso = mp->enable_gso;
77   args.enable_packed = mp->enable_packed;
78   rv = vhost_user_create_if (vnm, vm, &args);
79
80   /* Remember an interface tag for the new interface */
81   if (rv == 0)
82     {
83       /* If a tag was supplied... */
84       if (mp->tag[0])
85         {
86           /* Make sure it's a proper C-string */
87           mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
88           u8 *tag = format (0, "%s%c", mp->tag, 0);
89           vnet_set_sw_interface_tag (vnm, tag, args.sw_if_index);
90         }
91     }
92
93   /* *INDENT-OFF* */
94   REPLY_MACRO2(VL_API_CREATE_VHOST_USER_IF_REPLY,
95   ({
96     rmp->sw_if_index = ntohl (args.sw_if_index);
97   }));
98   /* *INDENT-ON* */
99 }
100
101 static void
102 vl_api_modify_vhost_user_if_t_handler (vl_api_modify_vhost_user_if_t * mp)
103 {
104   int rv = 0;
105   vl_api_modify_vhost_user_if_reply_t *rmp;
106   u64 disabled_features = (u64) (0ULL);
107   vhost_user_create_if_args_t args = { 0 };
108   vnet_main_t *vnm = vnet_get_main ();
109   vlib_main_t *vm = vlib_get_main ();
110
111   args.feature_mask = (u64) ~ (0ULL);
112   /*
113    * GSO and PACKED are not supported by feature mask via binary API. We
114    * disable GSO and PACKED feature in the feature mask. They may be enabled
115    * explicitly via enable_gso and enable_packed argument
116    */
117   disabled_features |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS |
118     VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
119
120   /* EVENT_IDX is disabled by default */
121   disabled_features |= VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
122   args.feature_mask &= ~disabled_features;
123
124   args.sw_if_index = ntohl (mp->sw_if_index);
125   args.sock_filename = (char *) mp->sock_filename;
126   args.is_server = mp->is_server;
127   args.renumber = mp->renumber;
128   args.custom_dev_instance = ntohl (mp->custom_dev_instance);
129   args.enable_gso = mp->enable_gso;
130   args.enable_packed = mp->enable_packed;
131   rv = vhost_user_modify_if (vnm, vm, &args);
132
133   REPLY_MACRO (VL_API_MODIFY_VHOST_USER_IF_REPLY);
134 }
135
136 static void
137 vl_api_create_vhost_user_if_v2_t_handler (vl_api_create_vhost_user_if_v2_t *
138                                           mp)
139 {
140   int rv = 0;
141   vl_api_create_vhost_user_if_v2_reply_t *rmp;
142   vnet_main_t *vnm = vnet_get_main ();
143   vlib_main_t *vm = vlib_get_main ();
144   u64 disabled_features = (u64) (0ULL);
145   vhost_user_create_if_args_t args = { 0 };
146
147   args.sw_if_index = (u32) ~ 0;
148   args.feature_mask = (u64) ~ (0ULL);
149   if (mp->disable_mrg_rxbuf)
150     disabled_features = VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
151
152   if (mp->disable_indirect_desc)
153     disabled_features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
154
155   /*
156    * GSO and PACKED are not supported by feature mask via binary API. We
157    * disable GSO and PACKED feature in the feature mask. They may be enabled
158    * explicitly via enable_gso and enable_packed argument
159    */
160   disabled_features |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS |
161     VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
162
163   /* EVENT_IDX is disabled by default */
164   disabled_features |= VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
165   args.feature_mask &= ~disabled_features;
166
167   if (mp->use_custom_mac)
168     mac_address_decode (mp->mac_address, (mac_address_t *) args.hwaddr);
169
170   args.use_custom_mac = mp->use_custom_mac;
171   args.is_server = mp->is_server;
172   args.sock_filename = (char *) mp->sock_filename;
173   args.renumber = mp->renumber;
174   args.custom_dev_instance = ntohl (mp->custom_dev_instance);
175   args.enable_gso = mp->enable_gso;
176   args.enable_packed = mp->enable_packed;
177   args.enable_event_idx = mp->enable_event_idx;
178   rv = vhost_user_create_if (vnm, vm, &args);
179
180   /* Remember an interface tag for the new interface */
181   if (rv == 0)
182     {
183       /* If a tag was supplied... */
184       if (mp->tag[0])
185         {
186           /* Make sure it's a proper C-string */
187           mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
188           u8 *tag = format (0, "%s%c", mp->tag, 0);
189           vnet_set_sw_interface_tag (vnm, tag, args.sw_if_index);
190         }
191     }
192
193   /* *INDENT-OFF* */
194   REPLY_MACRO2(VL_API_CREATE_VHOST_USER_IF_V2_REPLY,
195   ({
196     rmp->sw_if_index = ntohl (args.sw_if_index);
197   }));
198   /* *INDENT-ON* */
199 }
200
201 static void
202 vl_api_modify_vhost_user_if_v2_t_handler (vl_api_modify_vhost_user_if_v2_t *
203                                           mp)
204 {
205   int rv = 0;
206   vl_api_modify_vhost_user_if_v2_reply_t *rmp;
207   u64 disabled_features = (u64) (0ULL);
208   vhost_user_create_if_args_t args = { 0 };
209   vnet_main_t *vnm = vnet_get_main ();
210   vlib_main_t *vm = vlib_get_main ();
211
212   args.feature_mask = (u64) ~ (0ULL);
213   /*
214    * GSO and PACKED are not supported by feature mask via binary API. We
215    * disable GSO and PACKED feature in the feature mask. They may be enabled
216    * explicitly via enable_gso and enable_packed argument
217    */
218   disabled_features |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS |
219     VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
220
221   /* EVENT_IDX is disabled by default */
222   disabled_features |= VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
223   args.feature_mask &= ~disabled_features;
224
225   args.sw_if_index = ntohl (mp->sw_if_index);
226   args.sock_filename = (char *) mp->sock_filename;
227   args.is_server = mp->is_server;
228   args.renumber = mp->renumber;
229   args.custom_dev_instance = ntohl (mp->custom_dev_instance);
230   args.enable_gso = mp->enable_gso;
231   args.enable_packed = mp->enable_packed;
232   args.enable_event_idx = mp->enable_event_idx;
233   rv = vhost_user_modify_if (vnm, vm, &args);
234
235   REPLY_MACRO (VL_API_MODIFY_VHOST_USER_IF_V2_REPLY);
236 }
237
238 static void
239 vl_api_delete_vhost_user_if_t_handler (vl_api_delete_vhost_user_if_t * mp)
240 {
241   int rv = 0;
242   vl_api_delete_vhost_user_if_reply_t *rmp;
243   u32 sw_if_index = ntohl (mp->sw_if_index);
244   vl_api_registration_t *reg;
245
246   vnet_main_t *vnm = vnet_get_main ();
247   vlib_main_t *vm = vlib_get_main ();
248
249   rv = vhost_user_delete_if (vnm, vm, sw_if_index);
250
251   REPLY_MACRO (VL_API_DELETE_VHOST_USER_IF_REPLY);
252   if (!rv)
253     {
254       reg = vl_api_client_index_to_registration (mp->client_index);
255       if (!reg)
256         return;
257
258       vnet_clear_sw_interface_tag (vnm, sw_if_index);
259     }
260 }
261
262 static void
263 vhost_user_features_encode (u64 features, u32 *first, u32 *last)
264 {
265   *first = clib_net_to_host_u32 (features);
266   *last = clib_net_to_host_u32 (features >> 32);
267 }
268
269 static void
270 send_sw_interface_vhost_user_details (vpe_api_main_t * am,
271                                       vl_api_registration_t * reg,
272                                       vhost_user_intf_details_t * vui,
273                                       u32 context)
274 {
275   vl_api_sw_interface_vhost_user_details_t *mp;
276
277   mp = vl_msg_api_alloc (sizeof (*mp));
278   clib_memset (mp, 0, sizeof (*mp));
279   mp->_vl_msg_id =
280     ntohs (REPLY_MSG_ID_BASE + VL_API_SW_INTERFACE_VHOST_USER_DETAILS);
281   mp->sw_if_index = ntohl (vui->sw_if_index);
282   mp->virtio_net_hdr_sz = ntohl (vui->virtio_net_hdr_sz);
283   vhost_user_features_encode (vui->features, (u32 *) &mp->features_first_32,
284                               (u32 *) &mp->features_last_32);
285   mp->is_server = vui->is_server;
286   mp->num_regions = ntohl (vui->num_regions);
287   mp->sock_errno = ntohl (vui->sock_errno);
288   mp->context = context;
289
290   strncpy ((char *) mp->sock_filename,
291            (char *) vui->sock_filename, ARRAY_LEN (mp->sock_filename) - 1);
292   strncpy ((char *) mp->interface_name,
293            (char *) vui->if_name, ARRAY_LEN (mp->interface_name) - 1);
294
295   vl_api_send_msg (reg, (u8 *) mp);
296 }
297
298 static void
299   vl_api_sw_interface_vhost_user_dump_t_handler
300   (vl_api_sw_interface_vhost_user_dump_t * mp)
301 {
302   int rv = 0;
303   vpe_api_main_t *am = &vpe_api_main;
304   vnet_main_t *vnm = vnet_get_main ();
305   vlib_main_t *vm = vlib_get_main ();
306   vhost_user_intf_details_t *ifaces = NULL;
307   vhost_user_intf_details_t *vuid = NULL;
308   vl_api_registration_t *reg;
309   u32 filter_sw_if_index;
310
311   reg = vl_api_client_index_to_registration (mp->client_index);
312   if (!reg)
313     return;
314
315   filter_sw_if_index = htonl (mp->sw_if_index);
316   if (filter_sw_if_index != ~0)
317     VALIDATE_SW_IF_INDEX (mp);
318
319   rv = vhost_user_dump_ifs (vnm, vm, &ifaces);
320   if (rv)
321     return;
322
323   vec_foreach (vuid, ifaces)
324   {
325     if ((filter_sw_if_index == ~0) ||
326         (vuid->sw_if_index == filter_sw_if_index))
327       send_sw_interface_vhost_user_details (am, reg, vuid, mp->context);
328   }
329   BAD_SW_IF_INDEX_LABEL;
330   vec_free (ifaces);
331 }
332
333 #include <vhost/vhost_user.api.c>
334 static clib_error_t *
335 vhost_user_api_hookup (vlib_main_t * vm)
336 {
337   api_main_t *am = vlibapi_get_main ();
338   /* Mark CREATE_VHOST_USER_IF as mp safe */
339   vl_api_set_msg_thread_safe (am, VL_API_CREATE_VHOST_USER_IF, 1);
340   vl_api_set_msg_thread_safe (am, VL_API_CREATE_VHOST_USER_IF_V2, 1);
341
342   /*
343    * Set up the (msg_name, crc, message-id) table
344    */
345   REPLY_MSG_ID_BASE = setup_message_id_table ();
346
347   return 0;
348 }
349
350 VLIB_API_INIT_FUNCTION (vhost_user_api_hookup);
351
352 /*
353  * fd.io coding-style-patch-verification: ON
354  *
355  * Local Variables:
356  * eval: (c-set-style "gnu")
357  * End:
358  */