1 /*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2009 Red Hat, Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
26 #include "qemu/osdep.h"
27 #include "tap_int.h"
28 #include "tap-linux.h"
29 #include "net/tap.h"
30
31 #include <net/if.h>
32 #include <sys/ioctl.h>
33
34 #include "qapi/error.h"
35 #include "qemu/error-report.h"
36 #include "qemu/cutils.h"
37
38 #define PATH_NET_TUN "/dev/net/tun"
39
tap_open(char * ifname,int ifname_size,int * vnet_hdr,int vnet_hdr_required,int mq_required,Error ** errp)40 int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
41 int vnet_hdr_required, int mq_required, Error **errp)
42 {
43 struct ifreq ifr;
44 int fd, ret;
45 int len = sizeof(struct virtio_net_hdr);
46 unsigned int features;
47
48
49 ret = if_nametoindex(ifname);
50 if (ret) {
51 g_autofree char *file = g_strdup_printf("/dev/tap%d", ret);
52 fd = open(file, O_RDWR);
53 } else {
54 fd = -1;
55 }
56
57 if (fd < 0) {
58 fd = RETRY_ON_EINTR(open(PATH_NET_TUN, O_RDWR));
59 if (fd < 0) {
60 error_setg_errno(errp, errno, "could not open %s", PATH_NET_TUN);
61 return -1;
62 }
63 }
64 memset(&ifr, 0, sizeof(ifr));
65 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
66
67 if (ioctl(fd, TUNGETFEATURES, &features) == -1) {
68 warn_report("TUNGETFEATURES failed: %s", strerror(errno));
69 features = 0;
70 }
71
72 if (features & IFF_ONE_QUEUE) {
73 ifr.ifr_flags |= IFF_ONE_QUEUE;
74 }
75
76 if (*vnet_hdr) {
77 if (features & IFF_VNET_HDR) {
78 *vnet_hdr = 1;
79 ifr.ifr_flags |= IFF_VNET_HDR;
80 } else {
81 *vnet_hdr = 0;
82 }
83
84 if (vnet_hdr_required && !*vnet_hdr) {
85 error_setg(errp, "vnet_hdr=1 requested, but no kernel "
86 "support for IFF_VNET_HDR available");
87 close(fd);
88 return -1;
89 }
90 /*
91 * Make sure vnet header size has the default value: for a persistent
92 * tap it might have been modified e.g. by another instance of qemu.
93 * Ignore errors since old kernels do not support this ioctl: in this
94 * case the header size implicitly has the correct value.
95 */
96 ioctl(fd, TUNSETVNETHDRSZ, &len);
97 }
98
99 if (mq_required) {
100 if (!(features & IFF_MULTI_QUEUE)) {
101 error_setg(errp, "multiqueue required, but no kernel "
102 "support for IFF_MULTI_QUEUE available");
103 close(fd);
104 return -1;
105 } else {
106 ifr.ifr_flags |= IFF_MULTI_QUEUE;
107 }
108 }
109
110 if (ifname[0] != '\0')
111 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
112 else
113 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
114 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
115 if (ret != 0) {
116 if (ifname[0] != '\0') {
117 error_setg_errno(errp, errno, "could not configure %s (%s)",
118 PATH_NET_TUN, ifr.ifr_name);
119 } else {
120 error_setg_errno(errp, errno, "could not configure %s",
121 PATH_NET_TUN);
122 }
123 close(fd);
124 return -1;
125 }
126 pstrcpy(ifname, ifname_size, ifr.ifr_name);
127 g_unix_set_fd_nonblocking(fd, true, NULL);
128 return fd;
129 }
130
131 /* sndbuf implements a kind of flow control for tap.
132 * Unfortunately when it's enabled, and packets are sent
133 * to other guests on the same host, the receiver
134 * can lock up the transmitter indefinitely.
135 *
136 * To avoid packet loss, sndbuf should be set to a value lower than the tx
137 * queue capacity of any destination network interface.
138 * Ethernet NICs generally have txqueuelen=1000, so 1Mb is
139 * a good value, given a 1500 byte MTU.
140 */
141 #define TAP_DEFAULT_SNDBUF 0
142
tap_set_sndbuf(int fd,const NetdevTapOptions * tap,Error ** errp)143 void tap_set_sndbuf(int fd, const NetdevTapOptions *tap, Error **errp)
144 {
145 int sndbuf;
146
147 sndbuf = !tap->has_sndbuf ? TAP_DEFAULT_SNDBUF :
148 tap->sndbuf > INT_MAX ? INT_MAX :
149 tap->sndbuf;
150
151 if (!sndbuf) {
152 sndbuf = INT_MAX;
153 }
154
155 if (ioctl(fd, TUNSETSNDBUF, &sndbuf) == -1 && tap->has_sndbuf) {
156 error_setg_errno(errp, errno, "TUNSETSNDBUF ioctl failed");
157 }
158 }
159
tap_probe_vnet_hdr(int fd,Error ** errp)160 int tap_probe_vnet_hdr(int fd, Error **errp)
161 {
162 struct ifreq ifr;
163 memset(&ifr, 0, sizeof(ifr));
164
165 if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
166 /* TUNGETIFF is available since kernel v2.6.27 */
167 error_setg_errno(errp, errno,
168 "Unable to query TUNGETIFF on FD %d", fd);
169 return -1;
170 }
171
172 return ifr.ifr_flags & IFF_VNET_HDR;
173 }
174
tap_probe_has_ufo(int fd)175 int tap_probe_has_ufo(int fd)
176 {
177 unsigned offload;
178
179 offload = TUN_F_CSUM | TUN_F_UFO;
180
181 if (ioctl(fd, TUNSETOFFLOAD, offload) < 0)
182 return 0;
183
184 return 1;
185 }
186
tap_probe_has_uso(int fd)187 int tap_probe_has_uso(int fd)
188 {
189 unsigned offload;
190
191 offload = TUN_F_CSUM | TUN_F_USO4 | TUN_F_USO6;
192
193 if (ioctl(fd, TUNSETOFFLOAD, offload) < 0) {
194 return 0;
195 }
196 return 1;
197 }
198
tap_fd_set_vnet_hdr_len(int fd,int len)199 void tap_fd_set_vnet_hdr_len(int fd, int len)
200 {
201 if (ioctl(fd, TUNSETVNETHDRSZ, &len) == -1) {
202 fprintf(stderr, "TUNSETVNETHDRSZ ioctl() failed: %s. Exiting.\n",
203 strerror(errno));
204 abort();
205 }
206 }
207
tap_fd_set_vnet_le(int fd,int is_le)208 int tap_fd_set_vnet_le(int fd, int is_le)
209 {
210 int arg = is_le ? 1 : 0;
211
212 if (!ioctl(fd, TUNSETVNETLE, &arg)) {
213 return 0;
214 }
215
216 /* Check if our kernel supports TUNSETVNETLE */
217 if (errno == EINVAL) {
218 return -errno;
219 }
220
221 error_report("TUNSETVNETLE ioctl() failed: %s.", strerror(errno));
222 abort();
223 }
224
tap_fd_set_vnet_be(int fd,int is_be)225 int tap_fd_set_vnet_be(int fd, int is_be)
226 {
227 int arg = is_be ? 1 : 0;
228
229 if (!ioctl(fd, TUNSETVNETBE, &arg)) {
230 return 0;
231 }
232
233 /* Check if our kernel supports TUNSETVNETBE */
234 if (errno == EINVAL) {
235 return -errno;
236 }
237
238 error_report("TUNSETVNETBE ioctl() failed: %s.", strerror(errno));
239 abort();
240 }
241
tap_fd_set_offload(int fd,int csum,int tso4,int tso6,int ecn,int ufo,int uso4,int uso6)242 void tap_fd_set_offload(int fd, int csum, int tso4,
243 int tso6, int ecn, int ufo, int uso4, int uso6)
244 {
245 unsigned int offload = 0;
246
247 /* Check if our kernel supports TUNSETOFFLOAD */
248 if (ioctl(fd, TUNSETOFFLOAD, 0) != 0 && errno == EINVAL) {
249 return;
250 }
251
252 if (csum) {
253 offload |= TUN_F_CSUM;
254 if (tso4)
255 offload |= TUN_F_TSO4;
256 if (tso6)
257 offload |= TUN_F_TSO6;
258 if ((tso4 || tso6) && ecn)
259 offload |= TUN_F_TSO_ECN;
260 if (ufo)
261 offload |= TUN_F_UFO;
262 if (uso4) {
263 offload |= TUN_F_USO4;
264 }
265 if (uso6) {
266 offload |= TUN_F_USO6;
267 }
268 }
269
270 if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
271 offload &= ~(TUN_F_USO4 | TUN_F_USO6);
272 if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
273 offload &= ~TUN_F_UFO;
274 if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
275 fprintf(stderr, "TUNSETOFFLOAD ioctl() failed: %s\n",
276 strerror(errno));
277 }
278 }
279 }
280 }
281
282 /* Enable a specific queue of tap. */
tap_fd_enable(int fd)283 int tap_fd_enable(int fd)
284 {
285 struct ifreq ifr;
286 int ret;
287
288 memset(&ifr, 0, sizeof(ifr));
289
290 ifr.ifr_flags = IFF_ATTACH_QUEUE;
291 ret = ioctl(fd, TUNSETQUEUE, (void *) &ifr);
292
293 if (ret != 0) {
294 error_report("could not enable queue");
295 }
296
297 return ret;
298 }
299
300 /* Disable a specific queue of tap/ */
tap_fd_disable(int fd)301 int tap_fd_disable(int fd)
302 {
303 struct ifreq ifr;
304 int ret;
305
306 memset(&ifr, 0, sizeof(ifr));
307
308 ifr.ifr_flags = IFF_DETACH_QUEUE;
309 ret = ioctl(fd, TUNSETQUEUE, (void *) &ifr);
310
311 if (ret != 0) {
312 error_report("could not disable queue");
313 }
314
315 return ret;
316 }
317
tap_fd_get_ifname(int fd,char * ifname)318 int tap_fd_get_ifname(int fd, char *ifname)
319 {
320 struct ifreq ifr;
321
322 if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
323 error_report("TUNGETIFF ioctl() failed: %s",
324 strerror(errno));
325 return -1;
326 }
327
328 pstrcpy(ifname, sizeof(ifr.ifr_name), ifr.ifr_name);
329 return 0;
330 }
331
tap_fd_set_steering_ebpf(int fd,int prog_fd)332 int tap_fd_set_steering_ebpf(int fd, int prog_fd)
333 {
334 if (ioctl(fd, TUNSETSTEERINGEBPF, (void *) &prog_fd) != 0) {
335 error_report("Issue while setting TUNSETSTEERINGEBPF:"
336 " %s with fd: %d, prog_fd: %d",
337 strerror(errno), fd, prog_fd);
338
339 return -1;
340 }
341
342 return 0;
343 }
344