root / net / tap-linux.c @ c0424934
History | View | Annotate | Download (5.5 kB)
1 | c28b1c10 | Mark McLoughlin | /*
|
---|---|---|---|
2 | c28b1c10 | Mark McLoughlin | * QEMU System Emulator
|
3 | c28b1c10 | Mark McLoughlin | *
|
4 | c28b1c10 | Mark McLoughlin | * Copyright (c) 2003-2008 Fabrice Bellard
|
5 | c28b1c10 | Mark McLoughlin | * Copyright (c) 2009 Red Hat, Inc.
|
6 | c28b1c10 | Mark McLoughlin | *
|
7 | c28b1c10 | Mark McLoughlin | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
8 | c28b1c10 | Mark McLoughlin | * of this software and associated documentation files (the "Software"), to deal
|
9 | c28b1c10 | Mark McLoughlin | * in the Software without restriction, including without limitation the rights
|
10 | c28b1c10 | Mark McLoughlin | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 | c28b1c10 | Mark McLoughlin | * copies of the Software, and to permit persons to whom the Software is
|
12 | c28b1c10 | Mark McLoughlin | * furnished to do so, subject to the following conditions:
|
13 | c28b1c10 | Mark McLoughlin | *
|
14 | c28b1c10 | Mark McLoughlin | * The above copyright notice and this permission notice shall be included in
|
15 | c28b1c10 | Mark McLoughlin | * all copies or substantial portions of the Software.
|
16 | c28b1c10 | Mark McLoughlin | *
|
17 | c28b1c10 | Mark McLoughlin | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18 | c28b1c10 | Mark McLoughlin | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19 | c28b1c10 | Mark McLoughlin | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
20 | c28b1c10 | Mark McLoughlin | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21 | c28b1c10 | Mark McLoughlin | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22 | c28b1c10 | Mark McLoughlin | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
23 | c28b1c10 | Mark McLoughlin | * THE SOFTWARE.
|
24 | c28b1c10 | Mark McLoughlin | */
|
25 | c28b1c10 | Mark McLoughlin | |
26 | c28b1c10 | Mark McLoughlin | #include "net/tap.h" |
27 | c28b1c10 | Mark McLoughlin | #include "net/tap-linux.h" |
28 | c28b1c10 | Mark McLoughlin | |
29 | c28b1c10 | Mark McLoughlin | #include <net/if.h> |
30 | c28b1c10 | Mark McLoughlin | #include <sys/ioctl.h> |
31 | c28b1c10 | Mark McLoughlin | |
32 | c28b1c10 | Mark McLoughlin | #include "sysemu.h" |
33 | c28b1c10 | Mark McLoughlin | #include "qemu-common.h" |
34 | 2f792016 | Markus Armbruster | #include "qemu-error.h" |
35 | c28b1c10 | Mark McLoughlin | |
36 | 91ca60e0 | Michael Tokarev | #define PATH_NET_TUN "/dev/net/tun" |
37 | 91ca60e0 | Michael Tokarev | |
38 | c28b1c10 | Mark McLoughlin | int tap_open(char *ifname, int ifname_size, int *vnet_hdr, int vnet_hdr_required) |
39 | c28b1c10 | Mark McLoughlin | { |
40 | c28b1c10 | Mark McLoughlin | struct ifreq ifr;
|
41 | c28b1c10 | Mark McLoughlin | int fd, ret;
|
42 | c28b1c10 | Mark McLoughlin | |
43 | 91ca60e0 | Michael Tokarev | TFR(fd = open(PATH_NET_TUN, O_RDWR)); |
44 | c28b1c10 | Mark McLoughlin | if (fd < 0) { |
45 | 91ca60e0 | Michael Tokarev | error_report("could not open %s: %m", PATH_NET_TUN);
|
46 | c28b1c10 | Mark McLoughlin | return -1; |
47 | c28b1c10 | Mark McLoughlin | } |
48 | c28b1c10 | Mark McLoughlin | memset(&ifr, 0, sizeof(ifr)); |
49 | c28b1c10 | Mark McLoughlin | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
50 | c28b1c10 | Mark McLoughlin | |
51 | c28b1c10 | Mark McLoughlin | if (*vnet_hdr) {
|
52 | c28b1c10 | Mark McLoughlin | unsigned int features; |
53 | c28b1c10 | Mark McLoughlin | |
54 | c28b1c10 | Mark McLoughlin | if (ioctl(fd, TUNGETFEATURES, &features) == 0 && |
55 | c28b1c10 | Mark McLoughlin | features & IFF_VNET_HDR) { |
56 | c28b1c10 | Mark McLoughlin | *vnet_hdr = 1;
|
57 | c28b1c10 | Mark McLoughlin | ifr.ifr_flags |= IFF_VNET_HDR; |
58 | 6720b35b | Pierre Riteau | } else {
|
59 | 6720b35b | Pierre Riteau | *vnet_hdr = 0;
|
60 | c28b1c10 | Mark McLoughlin | } |
61 | c28b1c10 | Mark McLoughlin | |
62 | c28b1c10 | Mark McLoughlin | if (vnet_hdr_required && !*vnet_hdr) {
|
63 | 1ecda02b | Markus Armbruster | error_report("vnet_hdr=1 requested, but no kernel "
|
64 | 1ecda02b | Markus Armbruster | "support for IFF_VNET_HDR available");
|
65 | c28b1c10 | Mark McLoughlin | close(fd); |
66 | c28b1c10 | Mark McLoughlin | return -1; |
67 | c28b1c10 | Mark McLoughlin | } |
68 | c28b1c10 | Mark McLoughlin | } |
69 | c28b1c10 | Mark McLoughlin | |
70 | c28b1c10 | Mark McLoughlin | if (ifname[0] != '\0') |
71 | c28b1c10 | Mark McLoughlin | pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); |
72 | c28b1c10 | Mark McLoughlin | else
|
73 | c28b1c10 | Mark McLoughlin | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
|
74 | c28b1c10 | Mark McLoughlin | ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
|
75 | c28b1c10 | Mark McLoughlin | if (ret != 0) { |
76 | 93a7320e | Luiz Capitulino | if (ifname[0] != '\0') { |
77 | 93a7320e | Luiz Capitulino | error_report("could not configure %s (%s): %m", PATH_NET_TUN, ifr.ifr_name);
|
78 | 93a7320e | Luiz Capitulino | } else {
|
79 | 93a7320e | Luiz Capitulino | error_report("could not configure %s: %m", PATH_NET_TUN);
|
80 | 93a7320e | Luiz Capitulino | } |
81 | c28b1c10 | Mark McLoughlin | close(fd); |
82 | c28b1c10 | Mark McLoughlin | return -1; |
83 | c28b1c10 | Mark McLoughlin | } |
84 | c28b1c10 | Mark McLoughlin | pstrcpy(ifname, ifname_size, ifr.ifr_name); |
85 | c28b1c10 | Mark McLoughlin | fcntl(fd, F_SETFL, O_NONBLOCK); |
86 | c28b1c10 | Mark McLoughlin | return fd;
|
87 | c28b1c10 | Mark McLoughlin | } |
88 | 15ac913b | Mark McLoughlin | |
89 | f157ed20 | Michael S. Tsirkin | /* sndbuf implements a kind of flow control for tap.
|
90 | f157ed20 | Michael S. Tsirkin | * Unfortunately when it's enabled, and packets are sent
|
91 | f157ed20 | Michael S. Tsirkin | * to other guests on the same host, the receiver
|
92 | f157ed20 | Michael S. Tsirkin | * can lock up the transmitter indefinitely.
|
93 | f157ed20 | Michael S. Tsirkin | *
|
94 | f157ed20 | Michael S. Tsirkin | * To avoid packet loss, sndbuf should be set to a value lower than the tx
|
95 | f157ed20 | Michael S. Tsirkin | * queue capacity of any destination network interface.
|
96 | 15ac913b | Mark McLoughlin | * Ethernet NICs generally have txqueuelen=1000, so 1Mb is
|
97 | f157ed20 | Michael S. Tsirkin | * a good value, given a 1500 byte MTU.
|
98 | 15ac913b | Mark McLoughlin | */
|
99 | f157ed20 | Michael S. Tsirkin | #define TAP_DEFAULT_SNDBUF 0 |
100 | 15ac913b | Mark McLoughlin | |
101 | 15ac913b | Mark McLoughlin | int tap_set_sndbuf(int fd, QemuOpts *opts) |
102 | 15ac913b | Mark McLoughlin | { |
103 | 15ac913b | Mark McLoughlin | int sndbuf;
|
104 | 15ac913b | Mark McLoughlin | |
105 | 15ac913b | Mark McLoughlin | sndbuf = qemu_opt_get_size(opts, "sndbuf", TAP_DEFAULT_SNDBUF);
|
106 | 15ac913b | Mark McLoughlin | if (!sndbuf) {
|
107 | 15ac913b | Mark McLoughlin | sndbuf = INT_MAX; |
108 | 15ac913b | Mark McLoughlin | } |
109 | 15ac913b | Mark McLoughlin | |
110 | 15ac913b | Mark McLoughlin | if (ioctl(fd, TUNSETSNDBUF, &sndbuf) == -1 && qemu_opt_get(opts, "sndbuf")) { |
111 | 1ecda02b | Markus Armbruster | error_report("TUNSETSNDBUF ioctl failed: %s", strerror(errno));
|
112 | 15ac913b | Mark McLoughlin | return -1; |
113 | 15ac913b | Mark McLoughlin | } |
114 | 15ac913b | Mark McLoughlin | return 0; |
115 | 15ac913b | Mark McLoughlin | } |
116 | dc69004c | Mark McLoughlin | |
117 | dc69004c | Mark McLoughlin | int tap_probe_vnet_hdr(int fd) |
118 | dc69004c | Mark McLoughlin | { |
119 | dc69004c | Mark McLoughlin | struct ifreq ifr;
|
120 | dc69004c | Mark McLoughlin | |
121 | dc69004c | Mark McLoughlin | if (ioctl(fd, TUNGETIFF, &ifr) != 0) { |
122 | 1ecda02b | Markus Armbruster | error_report("TUNGETIFF ioctl() failed: %s", strerror(errno));
|
123 | dc69004c | Mark McLoughlin | return 0; |
124 | dc69004c | Mark McLoughlin | } |
125 | dc69004c | Mark McLoughlin | |
126 | dc69004c | Mark McLoughlin | return ifr.ifr_flags & IFF_VNET_HDR;
|
127 | dc69004c | Mark McLoughlin | } |
128 | 1faac1f7 | Mark McLoughlin | |
129 | 9c282718 | Mark McLoughlin | int tap_probe_has_ufo(int fd) |
130 | 9c282718 | Mark McLoughlin | { |
131 | 9c282718 | Mark McLoughlin | unsigned offload;
|
132 | 9c282718 | Mark McLoughlin | |
133 | 9c282718 | Mark McLoughlin | offload = TUN_F_CSUM | TUN_F_UFO; |
134 | 9c282718 | Mark McLoughlin | |
135 | 9c282718 | Mark McLoughlin | if (ioctl(fd, TUNSETOFFLOAD, offload) < 0) |
136 | 9c282718 | Mark McLoughlin | return 0; |
137 | 9c282718 | Mark McLoughlin | |
138 | 9c282718 | Mark McLoughlin | return 1; |
139 | 9c282718 | Mark McLoughlin | } |
140 | 9c282718 | Mark McLoughlin | |
141 | 445d892f | Michael S. Tsirkin | /* Verify that we can assign given length */
|
142 | 445d892f | Michael S. Tsirkin | int tap_probe_vnet_hdr_len(int fd, int len) |
143 | 445d892f | Michael S. Tsirkin | { |
144 | 445d892f | Michael S. Tsirkin | int orig;
|
145 | 445d892f | Michael S. Tsirkin | if (ioctl(fd, TUNGETVNETHDRSZ, &orig) == -1) { |
146 | 445d892f | Michael S. Tsirkin | return 0; |
147 | 445d892f | Michael S. Tsirkin | } |
148 | 445d892f | Michael S. Tsirkin | if (ioctl(fd, TUNSETVNETHDRSZ, &len) == -1) { |
149 | 445d892f | Michael S. Tsirkin | return 0; |
150 | 445d892f | Michael S. Tsirkin | } |
151 | 445d892f | Michael S. Tsirkin | /* Restore original length: we can't handle failure. */
|
152 | 445d892f | Michael S. Tsirkin | if (ioctl(fd, TUNSETVNETHDRSZ, &orig) == -1) { |
153 | 445d892f | Michael S. Tsirkin | fprintf(stderr, "TUNGETVNETHDRSZ ioctl() failed: %s. Exiting.\n",
|
154 | 445d892f | Michael S. Tsirkin | strerror(errno)); |
155 | 445d892f | Michael S. Tsirkin | assert(0);
|
156 | 445d892f | Michael S. Tsirkin | return -errno;
|
157 | 445d892f | Michael S. Tsirkin | } |
158 | 445d892f | Michael S. Tsirkin | return 1; |
159 | 445d892f | Michael S. Tsirkin | } |
160 | 445d892f | Michael S. Tsirkin | |
161 | 445d892f | Michael S. Tsirkin | void tap_fd_set_vnet_hdr_len(int fd, int len) |
162 | 445d892f | Michael S. Tsirkin | { |
163 | 445d892f | Michael S. Tsirkin | if (ioctl(fd, TUNSETVNETHDRSZ, &len) == -1) { |
164 | 445d892f | Michael S. Tsirkin | fprintf(stderr, "TUNSETVNETHDRSZ ioctl() failed: %s. Exiting.\n",
|
165 | 445d892f | Michael S. Tsirkin | strerror(errno)); |
166 | 445d892f | Michael S. Tsirkin | assert(0);
|
167 | 445d892f | Michael S. Tsirkin | } |
168 | 445d892f | Michael S. Tsirkin | } |
169 | 445d892f | Michael S. Tsirkin | |
170 | 1faac1f7 | Mark McLoughlin | void tap_fd_set_offload(int fd, int csum, int tso4, |
171 | 1faac1f7 | Mark McLoughlin | int tso6, int ecn, int ufo) |
172 | 1faac1f7 | Mark McLoughlin | { |
173 | 1faac1f7 | Mark McLoughlin | unsigned int offload = 0; |
174 | 1faac1f7 | Mark McLoughlin | |
175 | 2e50326c | Pierre Riteau | /* Check if our kernel supports TUNSETOFFLOAD */
|
176 | 2e50326c | Pierre Riteau | if (ioctl(fd, TUNSETOFFLOAD, 0) != 0 && errno == EINVAL) { |
177 | 2e50326c | Pierre Riteau | return;
|
178 | 2e50326c | Pierre Riteau | } |
179 | 2e50326c | Pierre Riteau | |
180 | 1faac1f7 | Mark McLoughlin | if (csum) {
|
181 | 1faac1f7 | Mark McLoughlin | offload |= TUN_F_CSUM; |
182 | 1faac1f7 | Mark McLoughlin | if (tso4)
|
183 | 1faac1f7 | Mark McLoughlin | offload |= TUN_F_TSO4; |
184 | 1faac1f7 | Mark McLoughlin | if (tso6)
|
185 | 1faac1f7 | Mark McLoughlin | offload |= TUN_F_TSO6; |
186 | 1faac1f7 | Mark McLoughlin | if ((tso4 || tso6) && ecn)
|
187 | 1faac1f7 | Mark McLoughlin | offload |= TUN_F_TSO_ECN; |
188 | 1faac1f7 | Mark McLoughlin | if (ufo)
|
189 | 1faac1f7 | Mark McLoughlin | offload |= TUN_F_UFO; |
190 | 1faac1f7 | Mark McLoughlin | } |
191 | 1faac1f7 | Mark McLoughlin | |
192 | 1faac1f7 | Mark McLoughlin | if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) { |
193 | 1faac1f7 | Mark McLoughlin | offload &= ~TUN_F_UFO; |
194 | 1faac1f7 | Mark McLoughlin | if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) { |
195 | 1faac1f7 | Mark McLoughlin | fprintf(stderr, "TUNSETOFFLOAD ioctl() failed: %s\n",
|
196 | 1faac1f7 | Mark McLoughlin | strerror(errno)); |
197 | 1faac1f7 | Mark McLoughlin | } |
198 | 1faac1f7 | Mark McLoughlin | } |
199 | 1faac1f7 | Mark McLoughlin | } |