virtio: fast TAP interfaces with vhost-net backend
[vpp.git] / src / vnet / devices / virtio / tap.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2017 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <net/if.h>
22 #include <linux/if_tun.h>
23 #include <sys/ioctl.h>
24 #include <linux/virtio_net.h>
25 #include <linux/vhost.h>
26 #include <sys/eventfd.h>
27
28 #include <linux/netlink.h>
29 #include <linux/rtnetlink.h>
30
31 #include <vlib/vlib.h>
32 #include <vlib/unix/unix.h>
33 #include <vnet/ethernet/ethernet.h>
34 #include <vnet/devices/virtio/virtio.h>
35 #include <vnet/devices/virtio/tap.h>
36
37 #define _IOCTL(fd,a,...) \
38   if (ioctl (fd, a, __VA_ARGS__) < 0) \
39     { \
40       err = clib_error_return_unix (0, "ioctl(" #a ")"); \
41       goto error; \
42     }
43
44 static u32
45 virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
46                         u32 flags)
47 {
48   /* nothing for now */
49   return 0;
50 }
51
52
53 clib_error_t *
54 clib_netlink_set_if_attr (int ifindex, unsigned short rta_type, void *data,
55                           int data_len)
56 {
57   clib_error_t *err = 0;
58   int sock;
59   struct sockaddr_nl ra = { 0 };
60   struct
61   {
62     struct nlmsghdr nh;
63     struct ifinfomsg ifmsg;
64     char attrbuf[512];
65   } req;
66   struct rtattr *rta;
67
68   memset (&req, 0, sizeof (req));
69   if ((sock = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1)
70     {
71       err = clib_error_return_unix (0, "socket(AF_NETLINK)");
72       goto error;
73     }
74
75   ra.nl_family = AF_NETLINK;
76   ra.nl_pid = getpid ();
77
78   if ((bind (sock, (struct sockaddr *) &ra, sizeof (ra))) == -1)
79     return clib_error_return_unix (0, "bind");
80
81   req.nh.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifinfomsg));
82   req.nh.nlmsg_flags = NLM_F_REQUEST;
83   req.nh.nlmsg_type = RTM_SETLINK;
84   req.ifmsg.ifi_family = AF_UNSPEC;
85   req.ifmsg.ifi_index = ifindex;
86   req.ifmsg.ifi_change = 0xffffffff;
87   rta = (struct rtattr *) (((char *) &req) + NLMSG_ALIGN (req.nh.nlmsg_len));
88   rta->rta_type = rta_type;
89   rta->rta_len = RTA_LENGTH (data_len);
90   req.nh.nlmsg_len = NLMSG_ALIGN (req.nh.nlmsg_len) + RTA_LENGTH (data_len);
91   memcpy (RTA_DATA (rta), data, data_len);
92
93   if ((send (sock, &req, req.nh.nlmsg_len, 0)) == -1)
94     err = clib_error_return_unix (0, "send");
95
96 error:
97   return err;
98 }
99
100 clib_error_t *
101 clib_netlink_set_if_mtu (int ifindex, int mtu)
102 {
103   clib_error_t *err;
104
105   err = clib_netlink_set_if_attr (ifindex, IFLA_MTU, &mtu, sizeof (int));
106   return err;
107 }
108
109 clib_error_t *
110 clib_netlink_set_if_namespace (int ifindex, char *net_ns)
111 {
112   clib_error_t *err;
113   int ns_fd;
114   u8 *s;
115   s = format (0, "/var/run/netns/%s%c", net_ns, 0);
116   ns_fd = open ((char *) s, O_RDONLY);
117   vec_free (s);
118   if (ns_fd == -1)
119     return clib_error_return (0, "namespace '%s' doesn't exist", net_ns);
120
121   err =
122     clib_netlink_set_if_attr (ifindex, IFLA_NET_NS_FD, &ns_fd, sizeof (int));
123   close (ns_fd);
124   return err;
125 }
126
127 int
128 tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
129 {
130   vnet_main_t *vnm = vnet_get_main ();
131   virtio_main_t *vim = &virtio_main;
132   vnet_sw_interface_t *sw;
133   vnet_hw_interface_t *hw;
134   int i;
135   clib_error_t *err = 0;
136   struct ifreq ifr;
137   size_t hdrsz;
138   struct vhost_memory *vhost_mem = 0;
139   virtio_if_t *vif = 0;
140   int rv = 0;
141
142   memset (&ifr, 0, sizeof (ifr));
143   pool_get (vim->interfaces, vif);
144   vif->dev_instance = vif - vim->interfaces;
145   vif->tap_fd = -1;
146
147   if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
148     {
149       rv = VNET_API_ERROR_SYSCALL_ERROR_1;
150       goto error;
151     }
152
153   _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features);
154
155   if ((vif->remote_features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) == 0)
156     {
157       rv = VNET_API_ERROR_UNSUPPORTED;
158       goto error;
159     }
160
161   if ((vif->remote_features & (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) == 0)
162     {
163       rv = VNET_API_ERROR_UNSUPPORTED;
164       goto error;
165     }
166
167   if ((vif->remote_features & (1ULL << VIRTIO_F_VERSION_1)) == 0)
168     {
169       rv = VNET_API_ERROR_UNSUPPORTED;
170       goto error;
171     }
172
173   vif->features |= 1ULL << VIRTIO_NET_F_MRG_RXBUF;
174   vif->features |= 1ULL << VIRTIO_F_VERSION_1;
175   vif->features |= 1ULL << VIRTIO_RING_F_INDIRECT_DESC;
176
177   _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features);
178
179   if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
180     {
181       rv = VNET_API_ERROR_SYSCALL_ERROR_2;
182       goto error;
183     }
184
185   ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
186   strncpy (ifr.ifr_ifrn.ifrn_name, (char *) args->name, IF_NAMESIZE);
187   _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr);
188
189   vif->ifindex = if_nametoindex ((char *) args->name);
190
191   unsigned int offload = 0;
192   hdrsz = sizeof (struct virtio_net_hdr_v1);
193   _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
194   _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz);
195   _IOCTL (vif->fd, VHOST_SET_OWNER, 0);
196
197   if (args->net_ns)
198     {
199       err = clib_netlink_set_if_namespace (vif->ifindex,
200                                            (char *) args->net_ns);
201       if (err)
202         {
203           rv = VNET_API_ERROR_NAMESPACE_CREATE;
204           goto error;
205         }
206     }
207
208   /* Set vhost memory table */
209   i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
210   vhost_mem = clib_mem_alloc (i);
211   memset (vhost_mem, 0, i);
212   vhost_mem->nregions = 1;
213   vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096;
214   _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem);
215
216   if ((err = virtio_vring_init (vm, vif, 0, args->rx_ring_sz)))
217     {
218       rv = VNET_API_ERROR_VIRTIO_INIT;
219       goto error;
220     }
221
222   if ((err = virtio_vring_init (vm, vif, 1, args->tx_ring_sz)))
223     {
224       rv = VNET_API_ERROR_VIRTIO_INIT;
225       goto error;
226     }
227
228   if (!args->hw_addr_set)
229     {
230       f64 now = vlib_time_now (vm);
231       u32 rnd;
232       rnd = (u32) (now * 1e6);
233       rnd = random_u32 (&rnd);
234
235       memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
236       args->hw_addr[0] = 2;
237       args->hw_addr[1] = 0xfe;
238     }
239   vif->name = args->name;
240   args->name = 0;
241   vif->net_ns = args->net_ns;
242   args->net_ns = 0;
243   err = ethernet_register_interface (vnm, virtio_device_class.index,
244                                      vif->dev_instance, args->hw_addr,
245                                      &vif->hw_if_index,
246                                      virtio_eth_flag_change);
247   if (err)
248     rv = VNET_API_ERROR_INVALID_REGISTRATION;
249
250   sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
251   vif->sw_if_index = sw->sw_if_index;
252   args->sw_if_index = vif->sw_if_index;
253   hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
254   hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
255   vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
256                                     virtio_input_node.index);
257   vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
258   vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
259                                  VNET_HW_INTERFACE_RX_MODE_DEFAULT);
260   vif->per_interface_next_index = ~0;
261   vif->type = VIRTIO_IF_TYPE_TAP;
262   vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
263   vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
264                                VNET_HW_INTERFACE_FLAG_LINK_UP);
265   goto done;
266
267 error:
268   if (vif->tap_fd != -1)
269     close (vif->tap_fd);
270   if (vif->fd != -1)
271     close (vif->fd);
272   vec_foreach_index (i, vif->vrings) virtio_vring_free (vif, i);
273   memset (vif, 0, sizeof (virtio_if_t));
274   pool_put (vim->interfaces, vif);
275
276 done:
277   if (vhost_mem)
278     clib_mem_free (vhost_mem);
279
280   return rv;
281 }
282
283 int
284 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
285 {
286   vnet_main_t *vnm = vnet_get_main ();
287   virtio_main_t *mm = &virtio_main;
288   int i;
289   virtio_if_t *vif;
290   vnet_hw_interface_t *hw;
291
292   hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
293   if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
294     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
295
296   vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
297
298   /* bring down the interface */
299   vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
300   vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
301
302   ethernet_delete_interface (vnm, vif->hw_if_index);
303   vif->hw_if_index = ~0;
304
305   if (vif->tap_fd != -1)
306     close (vif->tap_fd);
307   if (vif->fd != -1)
308     close (vif->fd);
309
310   vec_foreach_index (i, vif->vrings) virtio_vring_free (vif, i);
311   vec_free (vif->vrings);
312
313   memset (vif, 0, sizeof (*vif));
314   pool_put (mm->interfaces, vif);
315
316   return 0;
317 }
318
319 int
320 tap_dump_ifs (tap_interface_details_t ** out_tapids)
321 {
322   vnet_main_t *vnm = vnet_get_main ();
323   virtio_main_t *mm = &virtio_main;
324   virtio_if_t *vif;
325   vnet_hw_interface_t *hi;
326   tap_interface_details_t *r_tapids = NULL;
327   tap_interface_details_t *tapid = NULL;
328
329   /* *INDENT-OFF* */
330   pool_foreach (vif, mm->interfaces,
331     vec_add2(r_tapids, tapid, 1);
332     memset (tapid, 0, sizeof (*tapid));
333     tapid->sw_if_index = vif->sw_if_index;
334     hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
335     clib_memcpy(tapid->dev_name, hi->name,
336                 MIN (ARRAY_LEN (tapid->dev_name) - 1,
337                      strlen ((const char *) hi->name)));
338   );
339   /* *INDENT-ON* */
340
341   *out_tapids = r_tapids;
342
343   return 0;
344 }
345
346 static clib_error_t *
347 tap_init (vlib_main_t * vm)
348 {
349
350   return 0;
351 }
352
353 VLIB_INIT_FUNCTION (tap_init);
354
355 /*
356  * fd.io coding-style-patch-verification: ON
357  *
358  * Local Variables:
359  * eval: (c-set-style "gnu")
360  * End:
361  */