net/virtio-user: support kernel vhost
[dpdk.git] / drivers / net / virtio / virtio_user / vhost_kernel_tap.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <net/if.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <limits.h>
42
43 #include "vhost_kernel_tap.h"
44 #include "../virtio_logs.h"
45
46 int
47 vhost_kernel_open_tap(char **p_ifname, int hdr_size)
48 {
49         unsigned int tap_features;
50         int sndbuf = INT_MAX;
51         struct ifreq ifr;
52         int tapfd;
53
54         /* TODO:
55          * 1. verify we can get/set vnet_hdr_len, tap_probe_vnet_hdr_len
56          * 2. get number of memory regions from vhost module parameter
57          * max_mem_regions, supported in newer version linux kernel
58          */
59         tapfd = open(PATH_NET_TUN, O_RDWR);
60         if (tapfd < 0) {
61                 PMD_DRV_LOG(ERR, "fail to open %s: %s",
62                             PATH_NET_TUN, strerror(errno));
63                 return -1;
64         }
65
66         /* Construct ifr */
67         memset(&ifr, 0, sizeof(ifr));
68         ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
69
70         if (ioctl(tapfd, TUNGETFEATURES, &tap_features) == -1) {
71                 PMD_DRV_LOG(ERR, "TUNGETFEATURES failed: %s", strerror(errno));
72                 goto error;
73         }
74         if (tap_features & IFF_ONE_QUEUE)
75                 ifr.ifr_flags |= IFF_ONE_QUEUE;
76
77         /* Let tap instead of vhost-net handle vnet header, as the latter does
78          * not support offloading. And in this case, we should not set feature
79          * bit VHOST_NET_F_VIRTIO_NET_HDR.
80          */
81         if (tap_features & IFF_VNET_HDR) {
82                 ifr.ifr_flags |= IFF_VNET_HDR;
83         } else {
84                 PMD_DRV_LOG(ERR, "TAP does not support IFF_VNET_HDR");
85                 goto error;
86         }
87
88         if (*p_ifname)
89                 strncpy(ifr.ifr_name, *p_ifname, IFNAMSIZ);
90         else
91                 strncpy(ifr.ifr_name, "tap%d", IFNAMSIZ);
92         if (ioctl(tapfd, TUNSETIFF, (void *)&ifr) == -1) {
93                 PMD_DRV_LOG(ERR, "TUNSETIFF failed: %s", strerror(errno));
94                 goto error;
95         }
96
97         fcntl(tapfd, F_SETFL, O_NONBLOCK);
98
99         if (ioctl(tapfd, TUNSETVNETHDRSZ, &hdr_size) < 0) {
100                 PMD_DRV_LOG(ERR, "TUNSETVNETHDRSZ failed: %s", strerror(errno));
101                 goto error;
102         }
103
104         if (ioctl(tapfd, TUNSETSNDBUF, &sndbuf) < 0) {
105                 PMD_DRV_LOG(ERR, "TUNSETSNDBUF failed: %s", strerror(errno));
106                 goto error;
107         }
108
109         if (!(*p_ifname))
110                 *p_ifname = strdup(ifr.ifr_name);
111
112         return tapfd;
113 error:
114         close(tapfd);
115         return -1;
116 }