New upstream version 17.11.5
[deb_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 <net/if_arp.h>
40 #include <errno.h>
41 #include <string.h>
42 #include <limits.h>
43
44 #include <rte_ether.h>
45
46 #include "vhost_kernel_tap.h"
47 #include "../virtio_logs.h"
48 #include "../virtio_pci.h"
49
50 static int
51 vhost_kernel_tap_set_offload(int fd, uint64_t features)
52 {
53         unsigned int offload = 0;
54
55         if (features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
56                 offload |= TUN_F_CSUM;
57                 if (features & (1ULL << VIRTIO_NET_F_GUEST_TSO4))
58                         offload |= TUN_F_TSO4;
59                 if (features & (1ULL << VIRTIO_NET_F_GUEST_TSO6))
60                         offload |= TUN_F_TSO6;
61                 if (features & ((1ULL << VIRTIO_NET_F_GUEST_TSO4) |
62                         (1ULL << VIRTIO_NET_F_GUEST_TSO6)) &&
63                         (features & (1ULL << VIRTIO_NET_F_GUEST_ECN)))
64                         offload |= TUN_F_TSO_ECN;
65                 if (features & (1ULL << VIRTIO_NET_F_GUEST_UFO))
66                         offload |= TUN_F_UFO;
67         }
68
69         if (offload != 0) {
70                 /* Check if our kernel supports TUNSETOFFLOAD */
71                 if (ioctl(fd, TUNSETOFFLOAD, 0) != 0 && errno == EINVAL) {
72                         PMD_DRV_LOG(ERR, "Kernel does't support TUNSETOFFLOAD\n");
73                         return -ENOTSUP;
74                 }
75
76                 if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
77                         offload &= ~TUN_F_UFO;
78                         if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
79                                 PMD_DRV_LOG(ERR, "TUNSETOFFLOAD ioctl() failed: %s\n",
80                                         strerror(errno));
81                                 return -1;
82                         }
83                 }
84         }
85
86         return 0;
87 }
88
89 int
90 vhost_kernel_open_tap(char **p_ifname, int hdr_size, int req_mq,
91                          const char *mac, uint64_t features)
92 {
93         unsigned int tap_features;
94         int sndbuf = INT_MAX;
95         struct ifreq ifr;
96         int tapfd;
97
98         /* TODO:
99          * 1. verify we can get/set vnet_hdr_len, tap_probe_vnet_hdr_len
100          * 2. get number of memory regions from vhost module parameter
101          * max_mem_regions, supported in newer version linux kernel
102          */
103         tapfd = open(PATH_NET_TUN, O_RDWR);
104         if (tapfd < 0) {
105                 PMD_DRV_LOG(ERR, "fail to open %s: %s",
106                             PATH_NET_TUN, strerror(errno));
107                 return -1;
108         }
109
110         /* Construct ifr */
111         memset(&ifr, 0, sizeof(ifr));
112         ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
113
114         if (ioctl(tapfd, TUNGETFEATURES, &tap_features) == -1) {
115                 PMD_DRV_LOG(ERR, "TUNGETFEATURES failed: %s", strerror(errno));
116                 goto error;
117         }
118         if (tap_features & IFF_ONE_QUEUE)
119                 ifr.ifr_flags |= IFF_ONE_QUEUE;
120
121         /* Let tap instead of vhost-net handle vnet header, as the latter does
122          * not support offloading. And in this case, we should not set feature
123          * bit VHOST_NET_F_VIRTIO_NET_HDR.
124          */
125         if (tap_features & IFF_VNET_HDR) {
126                 ifr.ifr_flags |= IFF_VNET_HDR;
127         } else {
128                 PMD_DRV_LOG(ERR, "TAP does not support IFF_VNET_HDR");
129                 goto error;
130         }
131
132         if (req_mq)
133                 ifr.ifr_flags |= IFF_MULTI_QUEUE;
134
135         if (*p_ifname)
136                 strncpy(ifr.ifr_name, *p_ifname, IFNAMSIZ - 1);
137         else
138                 strncpy(ifr.ifr_name, "tap%d", IFNAMSIZ - 1);
139         if (ioctl(tapfd, TUNSETIFF, (void *)&ifr) == -1) {
140                 PMD_DRV_LOG(ERR, "TUNSETIFF failed: %s", strerror(errno));
141                 goto error;
142         }
143
144         fcntl(tapfd, F_SETFL, O_NONBLOCK);
145
146         if (ioctl(tapfd, TUNSETVNETHDRSZ, &hdr_size) < 0) {
147                 PMD_DRV_LOG(ERR, "TUNSETVNETHDRSZ failed: %s", strerror(errno));
148                 goto error;
149         }
150
151         if (ioctl(tapfd, TUNSETSNDBUF, &sndbuf) < 0) {
152                 PMD_DRV_LOG(ERR, "TUNSETSNDBUF failed: %s", strerror(errno));
153                 goto error;
154         }
155
156         vhost_kernel_tap_set_offload(tapfd, features);
157
158         memset(&ifr, 0, sizeof(ifr));
159         ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
160         memcpy(ifr.ifr_hwaddr.sa_data, mac, ETHER_ADDR_LEN);
161         if (ioctl(tapfd, SIOCSIFHWADDR, (void *)&ifr) == -1) {
162                 PMD_DRV_LOG(ERR, "SIOCSIFHWADDR failed: %s", strerror(errno));
163                 goto error;
164         }
165
166         if (!(*p_ifname))
167                 *p_ifname = strdup(ifr.ifr_name);
168
169         return tapfd;
170 error:
171         close(tapfd);
172         return -1;
173 }