xref: /openbmc/qemu/net/tap-linux.c (revision efb91426)
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 
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     fd = RETRY_ON_EINTR(open(PATH_NET_TUN, O_RDWR));
49     if (fd < 0) {
50         error_setg_errno(errp, errno, "could not open %s", PATH_NET_TUN);
51         return -1;
52     }
53     memset(&ifr, 0, sizeof(ifr));
54     ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
55 
56     if (ioctl(fd, TUNGETFEATURES, &features) == -1) {
57         warn_report("TUNGETFEATURES failed: %s", strerror(errno));
58         features = 0;
59     }
60 
61     if (features & IFF_ONE_QUEUE) {
62         ifr.ifr_flags |= IFF_ONE_QUEUE;
63     }
64 
65     if (*vnet_hdr) {
66         if (features & IFF_VNET_HDR) {
67             *vnet_hdr = 1;
68             ifr.ifr_flags |= IFF_VNET_HDR;
69         } else {
70             *vnet_hdr = 0;
71         }
72 
73         if (vnet_hdr_required && !*vnet_hdr) {
74             error_setg(errp, "vnet_hdr=1 requested, but no kernel "
75                        "support for IFF_VNET_HDR available");
76             close(fd);
77             return -1;
78         }
79         /*
80          * Make sure vnet header size has the default value: for a persistent
81          * tap it might have been modified e.g. by another instance of qemu.
82          * Ignore errors since old kernels do not support this ioctl: in this
83          * case the header size implicitly has the correct value.
84          */
85         ioctl(fd, TUNSETVNETHDRSZ, &len);
86     }
87 
88     if (mq_required) {
89         if (!(features & IFF_MULTI_QUEUE)) {
90             error_setg(errp, "multiqueue required, but no kernel "
91                        "support for IFF_MULTI_QUEUE available");
92             close(fd);
93             return -1;
94         } else {
95             ifr.ifr_flags |= IFF_MULTI_QUEUE;
96         }
97     }
98 
99     if (ifname[0] != '\0')
100         pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
101     else
102         pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
103     ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
104     if (ret != 0) {
105         if (ifname[0] != '\0') {
106             error_setg_errno(errp, errno, "could not configure %s (%s)",
107                              PATH_NET_TUN, ifr.ifr_name);
108         } else {
109             error_setg_errno(errp, errno, "could not configure %s",
110                              PATH_NET_TUN);
111         }
112         close(fd);
113         return -1;
114     }
115     pstrcpy(ifname, ifname_size, ifr.ifr_name);
116     g_unix_set_fd_nonblocking(fd, true, NULL);
117     return fd;
118 }
119 
120 /* sndbuf implements a kind of flow control for tap.
121  * Unfortunately when it's enabled, and packets are sent
122  * to other guests on the same host, the receiver
123  * can lock up the transmitter indefinitely.
124  *
125  * To avoid packet loss, sndbuf should be set to a value lower than the tx
126  * queue capacity of any destination network interface.
127  * Ethernet NICs generally have txqueuelen=1000, so 1Mb is
128  * a good value, given a 1500 byte MTU.
129  */
130 #define TAP_DEFAULT_SNDBUF 0
131 
132 void tap_set_sndbuf(int fd, const NetdevTapOptions *tap, Error **errp)
133 {
134     int sndbuf;
135 
136     sndbuf = !tap->has_sndbuf       ? TAP_DEFAULT_SNDBUF :
137              tap->sndbuf > INT_MAX  ? INT_MAX :
138              tap->sndbuf;
139 
140     if (!sndbuf) {
141         sndbuf = INT_MAX;
142     }
143 
144     if (ioctl(fd, TUNSETSNDBUF, &sndbuf) == -1 && tap->has_sndbuf) {
145         error_setg_errno(errp, errno, "TUNSETSNDBUF ioctl failed");
146     }
147 }
148 
149 int tap_probe_vnet_hdr(int fd, Error **errp)
150 {
151     struct ifreq ifr;
152     memset(&ifr, 0, sizeof(ifr));
153 
154     if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
155         /* TUNGETIFF is available since kernel v2.6.27 */
156         error_setg_errno(errp, errno,
157                          "Unable to query TUNGETIFF on FD %d", fd);
158         return -1;
159     }
160 
161     return ifr.ifr_flags & IFF_VNET_HDR;
162 }
163 
164 int tap_probe_has_ufo(int fd)
165 {
166     unsigned offload;
167 
168     offload = TUN_F_CSUM | TUN_F_UFO;
169 
170     if (ioctl(fd, TUNSETOFFLOAD, offload) < 0)
171         return 0;
172 
173     return 1;
174 }
175 
176 int tap_probe_has_uso(int fd)
177 {
178     unsigned offload;
179 
180     offload = TUN_F_CSUM | TUN_F_USO4 | TUN_F_USO6;
181 
182     if (ioctl(fd, TUNSETOFFLOAD, offload) < 0) {
183         return 0;
184     }
185     return 1;
186 }
187 
188 /* Verify that we can assign given length */
189 int tap_probe_vnet_hdr_len(int fd, int len)
190 {
191     int orig;
192     if (ioctl(fd, TUNGETVNETHDRSZ, &orig) == -1) {
193         return 0;
194     }
195     if (ioctl(fd, TUNSETVNETHDRSZ, &len) == -1) {
196         return 0;
197     }
198     /* Restore original length: we can't handle failure. */
199     if (ioctl(fd, TUNSETVNETHDRSZ, &orig) == -1) {
200         fprintf(stderr, "TUNGETVNETHDRSZ ioctl() failed: %s. Exiting.\n",
201                 strerror(errno));
202         abort();
203         return -errno;
204     }
205     return 1;
206 }
207 
208 void tap_fd_set_vnet_hdr_len(int fd, int len)
209 {
210     if (ioctl(fd, TUNSETVNETHDRSZ, &len) == -1) {
211         fprintf(stderr, "TUNSETVNETHDRSZ ioctl() failed: %s. Exiting.\n",
212                 strerror(errno));
213         abort();
214     }
215 }
216 
217 int tap_fd_set_vnet_le(int fd, int is_le)
218 {
219     int arg = is_le ? 1 : 0;
220 
221     if (!ioctl(fd, TUNSETVNETLE, &arg)) {
222         return 0;
223     }
224 
225     /* Check if our kernel supports TUNSETVNETLE */
226     if (errno == EINVAL) {
227         return -errno;
228     }
229 
230     error_report("TUNSETVNETLE ioctl() failed: %s.", strerror(errno));
231     abort();
232 }
233 
234 int tap_fd_set_vnet_be(int fd, int is_be)
235 {
236     int arg = is_be ? 1 : 0;
237 
238     if (!ioctl(fd, TUNSETVNETBE, &arg)) {
239         return 0;
240     }
241 
242     /* Check if our kernel supports TUNSETVNETBE */
243     if (errno == EINVAL) {
244         return -errno;
245     }
246 
247     error_report("TUNSETVNETBE ioctl() failed: %s.", strerror(errno));
248     abort();
249 }
250 
251 void tap_fd_set_offload(int fd, int csum, int tso4,
252                         int tso6, int ecn, int ufo, int uso4, int uso6)
253 {
254     unsigned int offload = 0;
255 
256     /* Check if our kernel supports TUNSETOFFLOAD */
257     if (ioctl(fd, TUNSETOFFLOAD, 0) != 0 && errno == EINVAL) {
258         return;
259     }
260 
261     if (csum) {
262         offload |= TUN_F_CSUM;
263         if (tso4)
264             offload |= TUN_F_TSO4;
265         if (tso6)
266             offload |= TUN_F_TSO6;
267         if ((tso4 || tso6) && ecn)
268             offload |= TUN_F_TSO_ECN;
269         if (ufo)
270             offload |= TUN_F_UFO;
271         if (uso4) {
272             offload |= TUN_F_USO4;
273         }
274         if (uso6) {
275             offload |= TUN_F_USO6;
276         }
277     }
278 
279     if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
280         offload &= ~(TUN_F_USO4 | TUN_F_USO6);
281         if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
282             offload &= ~TUN_F_UFO;
283             if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
284                 fprintf(stderr, "TUNSETOFFLOAD ioctl() failed: %s\n",
285                     strerror(errno));
286             }
287         }
288     }
289 }
290 
291 /* Enable a specific queue of tap. */
292 int tap_fd_enable(int fd)
293 {
294     struct ifreq ifr;
295     int ret;
296 
297     memset(&ifr, 0, sizeof(ifr));
298 
299     ifr.ifr_flags = IFF_ATTACH_QUEUE;
300     ret = ioctl(fd, TUNSETQUEUE, (void *) &ifr);
301 
302     if (ret != 0) {
303         error_report("could not enable queue");
304     }
305 
306     return ret;
307 }
308 
309 /* Disable a specific queue of tap/ */
310 int tap_fd_disable(int fd)
311 {
312     struct ifreq ifr;
313     int ret;
314 
315     memset(&ifr, 0, sizeof(ifr));
316 
317     ifr.ifr_flags = IFF_DETACH_QUEUE;
318     ret = ioctl(fd, TUNSETQUEUE, (void *) &ifr);
319 
320     if (ret != 0) {
321         error_report("could not disable queue");
322     }
323 
324     return ret;
325 }
326 
327 int tap_fd_get_ifname(int fd, char *ifname)
328 {
329     struct ifreq ifr;
330 
331     if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
332         error_report("TUNGETIFF ioctl() failed: %s",
333                      strerror(errno));
334         return -1;
335     }
336 
337     pstrcpy(ifname, sizeof(ifr.ifr_name), ifr.ifr_name);
338     return 0;
339 }
340 
341 int tap_fd_set_steering_ebpf(int fd, int prog_fd)
342 {
343     if (ioctl(fd, TUNSETSTEERINGEBPF, (void *) &prog_fd) != 0) {
344         error_report("Issue while setting TUNSETSTEERINGEBPF:"
345                     " %s with fd: %d, prog_fd: %d",
346                     strerror(errno), fd, prog_fd);
347 
348        return -1;
349     }
350 
351     return 0;
352 }
353