]> git.proxmox.com Git - qemu.git/blame - net/tap-linux.c
Fix BSD and win32 builds
[qemu.git] / net / tap-linux.c
CommitLineData
c28b1c10
MM
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 "net/tap.h"
27#include "net/tap-linux.h"
28
29#include <net/if.h>
30#include <sys/ioctl.h>
31
32#include "sysemu.h"
33#include "qemu-common.h"
2f792016 34#include "qemu-error.h"
c28b1c10
MM
35
36int tap_open(char *ifname, int ifname_size, int *vnet_hdr, int vnet_hdr_required)
37{
38 struct ifreq ifr;
39 int fd, ret;
40
41 TFR(fd = open("/dev/net/tun", O_RDWR));
42 if (fd < 0) {
43 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
44 return -1;
45 }
46 memset(&ifr, 0, sizeof(ifr));
47 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
48
49 if (*vnet_hdr) {
50 unsigned int features;
51
52 if (ioctl(fd, TUNGETFEATURES, &features) == 0 &&
53 features & IFF_VNET_HDR) {
54 *vnet_hdr = 1;
55 ifr.ifr_flags |= IFF_VNET_HDR;
6720b35b
PR
56 } else {
57 *vnet_hdr = 0;
c28b1c10
MM
58 }
59
60 if (vnet_hdr_required && !*vnet_hdr) {
1ecda02b
MA
61 error_report("vnet_hdr=1 requested, but no kernel "
62 "support for IFF_VNET_HDR available");
c28b1c10
MM
63 close(fd);
64 return -1;
65 }
66 }
67
68 if (ifname[0] != '\0')
69 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
70 else
71 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
72 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
73 if (ret != 0) {
74 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
75 close(fd);
76 return -1;
77 }
78 pstrcpy(ifname, ifname_size, ifr.ifr_name);
79 fcntl(fd, F_SETFL, O_NONBLOCK);
80 return fd;
81}
15ac913b
MM
82
83/* sndbuf should be set to a value lower than the tx queue
84 * capacity of any destination network interface.
85 * Ethernet NICs generally have txqueuelen=1000, so 1Mb is
86 * a good default, given a 1500 byte MTU.
87 */
88#define TAP_DEFAULT_SNDBUF 1024*1024
89
90int tap_set_sndbuf(int fd, QemuOpts *opts)
91{
92 int sndbuf;
93
94 sndbuf = qemu_opt_get_size(opts, "sndbuf", TAP_DEFAULT_SNDBUF);
95 if (!sndbuf) {
96 sndbuf = INT_MAX;
97 }
98
99 if (ioctl(fd, TUNSETSNDBUF, &sndbuf) == -1 && qemu_opt_get(opts, "sndbuf")) {
1ecda02b 100 error_report("TUNSETSNDBUF ioctl failed: %s", strerror(errno));
15ac913b
MM
101 return -1;
102 }
103 return 0;
104}
dc69004c
MM
105
106int tap_probe_vnet_hdr(int fd)
107{
108 struct ifreq ifr;
109
110 if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
1ecda02b 111 error_report("TUNGETIFF ioctl() failed: %s", strerror(errno));
dc69004c
MM
112 return 0;
113 }
114
115 return ifr.ifr_flags & IFF_VNET_HDR;
116}
1faac1f7 117
9c282718
MM
118int tap_probe_has_ufo(int fd)
119{
120 unsigned offload;
121
122 offload = TUN_F_CSUM | TUN_F_UFO;
123
124 if (ioctl(fd, TUNSETOFFLOAD, offload) < 0)
125 return 0;
126
127 return 1;
128}
129
1faac1f7
MM
130void tap_fd_set_offload(int fd, int csum, int tso4,
131 int tso6, int ecn, int ufo)
132{
133 unsigned int offload = 0;
134
2e50326c
PR
135 /* Check if our kernel supports TUNSETOFFLOAD */
136 if (ioctl(fd, TUNSETOFFLOAD, 0) != 0 && errno == EINVAL) {
137 return;
138 }
139
1faac1f7
MM
140 if (csum) {
141 offload |= TUN_F_CSUM;
142 if (tso4)
143 offload |= TUN_F_TSO4;
144 if (tso6)
145 offload |= TUN_F_TSO6;
146 if ((tso4 || tso6) && ecn)
147 offload |= TUN_F_TSO_ECN;
148 if (ufo)
149 offload |= TUN_F_UFO;
150 }
151
152 if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
153 offload &= ~TUN_F_UFO;
154 if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
155 fprintf(stderr, "TUNSETOFFLOAD ioctl() failed: %s\n",
156 strerror(errno));
157 }
158 }
159}