root / net / tap-linux.c @ feature-archipelago
History | View | Annotate | Download (7.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 | 1422e32d | Paolo Bonzini | #include "tap_int.h" |
27 | 1422e32d | Paolo Bonzini | #include "tap-linux.h" |
28 | c28b1c10 | Mark McLoughlin | #include "net/tap.h" |
29 | c28b1c10 | Mark McLoughlin | |
30 | c28b1c10 | Mark McLoughlin | #include <net/if.h> |
31 | c28b1c10 | Mark McLoughlin | #include <sys/ioctl.h> |
32 | c28b1c10 | Mark McLoughlin | |
33 | 9c17d615 | Paolo Bonzini | #include "sysemu/sysemu.h" |
34 | c28b1c10 | Mark McLoughlin | #include "qemu-common.h" |
35 | 1de7afc9 | Paolo Bonzini | #include "qemu/error-report.h" |
36 | c28b1c10 | Mark McLoughlin | |
37 | 91ca60e0 | Michael Tokarev | #define PATH_NET_TUN "/dev/net/tun" |
38 | 91ca60e0 | Michael Tokarev | |
39 | 264986e2 | Jason Wang | int tap_open(char *ifname, int ifname_size, int *vnet_hdr, |
40 | 264986e2 | Jason Wang | int vnet_hdr_required, int mq_required) |
41 | c28b1c10 | Mark McLoughlin | { |
42 | c28b1c10 | Mark McLoughlin | struct ifreq ifr;
|
43 | c28b1c10 | Mark McLoughlin | int fd, ret;
|
44 | 89e6d68e | Michael S. Tsirkin | int len = sizeof(struct virtio_net_hdr); |
45 | d26e445c | Peter Lieven | unsigned int features; |
46 | c28b1c10 | Mark McLoughlin | |
47 | 91ca60e0 | Michael Tokarev | TFR(fd = open(PATH_NET_TUN, O_RDWR)); |
48 | c28b1c10 | Mark McLoughlin | if (fd < 0) { |
49 | 91ca60e0 | Michael Tokarev | error_report("could not open %s: %m", PATH_NET_TUN);
|
50 | c28b1c10 | Mark McLoughlin | return -1; |
51 | c28b1c10 | Mark McLoughlin | } |
52 | c28b1c10 | Mark McLoughlin | memset(&ifr, 0, sizeof(ifr)); |
53 | c28b1c10 | Mark McLoughlin | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
54 | c28b1c10 | Mark McLoughlin | |
55 | 1f149e72 | Kusanagi Kouichi | if (ioctl(fd, TUNGETFEATURES, &features) == -1) { |
56 | 1f149e72 | Kusanagi Kouichi | error_report("warning: TUNGETFEATURES failed: %s", strerror(errno));
|
57 | 1f149e72 | Kusanagi Kouichi | features = 0;
|
58 | 1f149e72 | Kusanagi Kouichi | } |
59 | 1f149e72 | Kusanagi Kouichi | |
60 | 1f149e72 | Kusanagi Kouichi | if (features & IFF_ONE_QUEUE) {
|
61 | d26e445c | Peter Lieven | ifr.ifr_flags |= IFF_ONE_QUEUE; |
62 | d26e445c | Peter Lieven | } |
63 | c28b1c10 | Mark McLoughlin | |
64 | d26e445c | Peter Lieven | if (*vnet_hdr) {
|
65 | 1f149e72 | Kusanagi Kouichi | if (features & IFF_VNET_HDR) {
|
66 | c28b1c10 | Mark McLoughlin | *vnet_hdr = 1;
|
67 | c28b1c10 | Mark McLoughlin | ifr.ifr_flags |= IFF_VNET_HDR; |
68 | 6720b35b | Pierre Riteau | } else {
|
69 | 6720b35b | Pierre Riteau | *vnet_hdr = 0;
|
70 | c28b1c10 | Mark McLoughlin | } |
71 | c28b1c10 | Mark McLoughlin | |
72 | c28b1c10 | Mark McLoughlin | if (vnet_hdr_required && !*vnet_hdr) {
|
73 | 1ecda02b | Markus Armbruster | error_report("vnet_hdr=1 requested, but no kernel "
|
74 | 1ecda02b | Markus Armbruster | "support for IFF_VNET_HDR available");
|
75 | c28b1c10 | Mark McLoughlin | close(fd); |
76 | c28b1c10 | Mark McLoughlin | return -1; |
77 | c28b1c10 | Mark McLoughlin | } |
78 | 89e6d68e | Michael S. Tsirkin | /*
|
79 | 89e6d68e | Michael S. Tsirkin | * Make sure vnet header size has the default value: for a persistent
|
80 | 89e6d68e | Michael S. Tsirkin | * tap it might have been modified e.g. by another instance of qemu.
|
81 | 89e6d68e | Michael S. Tsirkin | * Ignore errors since old kernels do not support this ioctl: in this
|
82 | 89e6d68e | Michael S. Tsirkin | * case the header size implicitly has the correct value.
|
83 | 89e6d68e | Michael S. Tsirkin | */
|
84 | 89e6d68e | Michael S. Tsirkin | ioctl(fd, TUNSETVNETHDRSZ, &len); |
85 | c28b1c10 | Mark McLoughlin | } |
86 | c28b1c10 | Mark McLoughlin | |
87 | 94fdc6d0 | Jason Wang | if (mq_required) {
|
88 | 1f149e72 | Kusanagi Kouichi | if (!(features & IFF_MULTI_QUEUE)) {
|
89 | 94fdc6d0 | Jason Wang | error_report("multiqueue required, but no kernel "
|
90 | 94fdc6d0 | Jason Wang | "support for IFF_MULTI_QUEUE available");
|
91 | 94fdc6d0 | Jason Wang | close(fd); |
92 | 94fdc6d0 | Jason Wang | return -1; |
93 | 94fdc6d0 | Jason Wang | } else {
|
94 | 94fdc6d0 | Jason Wang | ifr.ifr_flags |= IFF_MULTI_QUEUE; |
95 | 94fdc6d0 | Jason Wang | } |
96 | 94fdc6d0 | Jason Wang | } |
97 | 94fdc6d0 | Jason Wang | |
98 | c28b1c10 | Mark McLoughlin | if (ifname[0] != '\0') |
99 | c28b1c10 | Mark McLoughlin | pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); |
100 | c28b1c10 | Mark McLoughlin | else
|
101 | c28b1c10 | Mark McLoughlin | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
|
102 | c28b1c10 | Mark McLoughlin | ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
|
103 | c28b1c10 | Mark McLoughlin | if (ret != 0) { |
104 | 93a7320e | Luiz Capitulino | if (ifname[0] != '\0') { |
105 | 93a7320e | Luiz Capitulino | error_report("could not configure %s (%s): %m", PATH_NET_TUN, ifr.ifr_name);
|
106 | 93a7320e | Luiz Capitulino | } else {
|
107 | 93a7320e | Luiz Capitulino | error_report("could not configure %s: %m", PATH_NET_TUN);
|
108 | 93a7320e | Luiz Capitulino | } |
109 | c28b1c10 | Mark McLoughlin | close(fd); |
110 | c28b1c10 | Mark McLoughlin | return -1; |
111 | c28b1c10 | Mark McLoughlin | } |
112 | c28b1c10 | Mark McLoughlin | pstrcpy(ifname, ifname_size, ifr.ifr_name); |
113 | c28b1c10 | Mark McLoughlin | fcntl(fd, F_SETFL, O_NONBLOCK); |
114 | c28b1c10 | Mark McLoughlin | return fd;
|
115 | c28b1c10 | Mark McLoughlin | } |
116 | 15ac913b | Mark McLoughlin | |
117 | f157ed20 | Michael S. Tsirkin | /* sndbuf implements a kind of flow control for tap.
|
118 | f157ed20 | Michael S. Tsirkin | * Unfortunately when it's enabled, and packets are sent
|
119 | f157ed20 | Michael S. Tsirkin | * to other guests on the same host, the receiver
|
120 | f157ed20 | Michael S. Tsirkin | * can lock up the transmitter indefinitely.
|
121 | f157ed20 | Michael S. Tsirkin | *
|
122 | f157ed20 | Michael S. Tsirkin | * To avoid packet loss, sndbuf should be set to a value lower than the tx
|
123 | f157ed20 | Michael S. Tsirkin | * queue capacity of any destination network interface.
|
124 | 15ac913b | Mark McLoughlin | * Ethernet NICs generally have txqueuelen=1000, so 1Mb is
|
125 | f157ed20 | Michael S. Tsirkin | * a good value, given a 1500 byte MTU.
|
126 | 15ac913b | Mark McLoughlin | */
|
127 | f157ed20 | Michael S. Tsirkin | #define TAP_DEFAULT_SNDBUF 0 |
128 | 15ac913b | Mark McLoughlin | |
129 | 08c573a8 | Laszlo Ersek | int tap_set_sndbuf(int fd, const NetdevTapOptions *tap) |
130 | 15ac913b | Mark McLoughlin | { |
131 | 15ac913b | Mark McLoughlin | int sndbuf;
|
132 | 15ac913b | Mark McLoughlin | |
133 | 08c573a8 | Laszlo Ersek | sndbuf = !tap->has_sndbuf ? TAP_DEFAULT_SNDBUF : |
134 | 08c573a8 | Laszlo Ersek | tap->sndbuf > INT_MAX ? INT_MAX : |
135 | 08c573a8 | Laszlo Ersek | tap->sndbuf; |
136 | 08c573a8 | Laszlo Ersek | |
137 | 15ac913b | Mark McLoughlin | if (!sndbuf) {
|
138 | 15ac913b | Mark McLoughlin | sndbuf = INT_MAX; |
139 | 15ac913b | Mark McLoughlin | } |
140 | 15ac913b | Mark McLoughlin | |
141 | 08c573a8 | Laszlo Ersek | if (ioctl(fd, TUNSETSNDBUF, &sndbuf) == -1 && tap->has_sndbuf) { |
142 | 1ecda02b | Markus Armbruster | error_report("TUNSETSNDBUF ioctl failed: %s", strerror(errno));
|
143 | 15ac913b | Mark McLoughlin | return -1; |
144 | 15ac913b | Mark McLoughlin | } |
145 | 15ac913b | Mark McLoughlin | return 0; |
146 | 15ac913b | Mark McLoughlin | } |
147 | dc69004c | Mark McLoughlin | |
148 | dc69004c | Mark McLoughlin | int tap_probe_vnet_hdr(int fd) |
149 | dc69004c | Mark McLoughlin | { |
150 | dc69004c | Mark McLoughlin | struct ifreq ifr;
|
151 | dc69004c | Mark McLoughlin | |
152 | dc69004c | Mark McLoughlin | if (ioctl(fd, TUNGETIFF, &ifr) != 0) { |
153 | 1ecda02b | Markus Armbruster | error_report("TUNGETIFF ioctl() failed: %s", strerror(errno));
|
154 | dc69004c | Mark McLoughlin | return 0; |
155 | dc69004c | Mark McLoughlin | } |
156 | dc69004c | Mark McLoughlin | |
157 | dc69004c | Mark McLoughlin | return ifr.ifr_flags & IFF_VNET_HDR;
|
158 | dc69004c | Mark McLoughlin | } |
159 | 1faac1f7 | Mark McLoughlin | |
160 | 9c282718 | Mark McLoughlin | int tap_probe_has_ufo(int fd) |
161 | 9c282718 | Mark McLoughlin | { |
162 | 9c282718 | Mark McLoughlin | unsigned offload;
|
163 | 9c282718 | Mark McLoughlin | |
164 | 9c282718 | Mark McLoughlin | offload = TUN_F_CSUM | TUN_F_UFO; |
165 | 9c282718 | Mark McLoughlin | |
166 | 9c282718 | Mark McLoughlin | if (ioctl(fd, TUNSETOFFLOAD, offload) < 0) |
167 | 9c282718 | Mark McLoughlin | return 0; |
168 | 9c282718 | Mark McLoughlin | |
169 | 9c282718 | Mark McLoughlin | return 1; |
170 | 9c282718 | Mark McLoughlin | } |
171 | 9c282718 | Mark McLoughlin | |
172 | 445d892f | Michael S. Tsirkin | /* Verify that we can assign given length */
|
173 | 445d892f | Michael S. Tsirkin | int tap_probe_vnet_hdr_len(int fd, int len) |
174 | 445d892f | Michael S. Tsirkin | { |
175 | 445d892f | Michael S. Tsirkin | int orig;
|
176 | 445d892f | Michael S. Tsirkin | if (ioctl(fd, TUNGETVNETHDRSZ, &orig) == -1) { |
177 | 445d892f | Michael S. Tsirkin | return 0; |
178 | 445d892f | Michael S. Tsirkin | } |
179 | 445d892f | Michael S. Tsirkin | if (ioctl(fd, TUNSETVNETHDRSZ, &len) == -1) { |
180 | 445d892f | Michael S. Tsirkin | return 0; |
181 | 445d892f | Michael S. Tsirkin | } |
182 | 445d892f | Michael S. Tsirkin | /* Restore original length: we can't handle failure. */
|
183 | 445d892f | Michael S. Tsirkin | if (ioctl(fd, TUNSETVNETHDRSZ, &orig) == -1) { |
184 | 445d892f | Michael S. Tsirkin | fprintf(stderr, "TUNGETVNETHDRSZ ioctl() failed: %s. Exiting.\n",
|
185 | 445d892f | Michael S. Tsirkin | strerror(errno)); |
186 | 28a65891 | Jason Wang | abort(); |
187 | 445d892f | Michael S. Tsirkin | return -errno;
|
188 | 445d892f | Michael S. Tsirkin | } |
189 | 445d892f | Michael S. Tsirkin | return 1; |
190 | 445d892f | Michael S. Tsirkin | } |
191 | 445d892f | Michael S. Tsirkin | |
192 | 445d892f | Michael S. Tsirkin | void tap_fd_set_vnet_hdr_len(int fd, int len) |
193 | 445d892f | Michael S. Tsirkin | { |
194 | 445d892f | Michael S. Tsirkin | if (ioctl(fd, TUNSETVNETHDRSZ, &len) == -1) { |
195 | 445d892f | Michael S. Tsirkin | fprintf(stderr, "TUNSETVNETHDRSZ ioctl() failed: %s. Exiting.\n",
|
196 | 445d892f | Michael S. Tsirkin | strerror(errno)); |
197 | 28a65891 | Jason Wang | abort(); |
198 | 445d892f | Michael S. Tsirkin | } |
199 | 445d892f | Michael S. Tsirkin | } |
200 | 445d892f | Michael S. Tsirkin | |
201 | 1faac1f7 | Mark McLoughlin | void tap_fd_set_offload(int fd, int csum, int tso4, |
202 | 1faac1f7 | Mark McLoughlin | int tso6, int ecn, int ufo) |
203 | 1faac1f7 | Mark McLoughlin | { |
204 | 1faac1f7 | Mark McLoughlin | unsigned int offload = 0; |
205 | 1faac1f7 | Mark McLoughlin | |
206 | 2e50326c | Pierre Riteau | /* Check if our kernel supports TUNSETOFFLOAD */
|
207 | 2e50326c | Pierre Riteau | if (ioctl(fd, TUNSETOFFLOAD, 0) != 0 && errno == EINVAL) { |
208 | 2e50326c | Pierre Riteau | return;
|
209 | 2e50326c | Pierre Riteau | } |
210 | 2e50326c | Pierre Riteau | |
211 | 1faac1f7 | Mark McLoughlin | if (csum) {
|
212 | 1faac1f7 | Mark McLoughlin | offload |= TUN_F_CSUM; |
213 | 1faac1f7 | Mark McLoughlin | if (tso4)
|
214 | 1faac1f7 | Mark McLoughlin | offload |= TUN_F_TSO4; |
215 | 1faac1f7 | Mark McLoughlin | if (tso6)
|
216 | 1faac1f7 | Mark McLoughlin | offload |= TUN_F_TSO6; |
217 | 1faac1f7 | Mark McLoughlin | if ((tso4 || tso6) && ecn)
|
218 | 1faac1f7 | Mark McLoughlin | offload |= TUN_F_TSO_ECN; |
219 | 1faac1f7 | Mark McLoughlin | if (ufo)
|
220 | 1faac1f7 | Mark McLoughlin | offload |= TUN_F_UFO; |
221 | 1faac1f7 | Mark McLoughlin | } |
222 | 1faac1f7 | Mark McLoughlin | |
223 | 1faac1f7 | Mark McLoughlin | if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) { |
224 | 1faac1f7 | Mark McLoughlin | offload &= ~TUN_F_UFO; |
225 | 1faac1f7 | Mark McLoughlin | if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) { |
226 | 1faac1f7 | Mark McLoughlin | fprintf(stderr, "TUNSETOFFLOAD ioctl() failed: %s\n",
|
227 | 1faac1f7 | Mark McLoughlin | strerror(errno)); |
228 | 1faac1f7 | Mark McLoughlin | } |
229 | 1faac1f7 | Mark McLoughlin | } |
230 | 1faac1f7 | Mark McLoughlin | } |
231 | 94fdc6d0 | Jason Wang | |
232 | 94fdc6d0 | Jason Wang | /* Enable a specific queue of tap. */
|
233 | 94fdc6d0 | Jason Wang | int tap_fd_enable(int fd) |
234 | 94fdc6d0 | Jason Wang | { |
235 | 94fdc6d0 | Jason Wang | struct ifreq ifr;
|
236 | 94fdc6d0 | Jason Wang | int ret;
|
237 | 94fdc6d0 | Jason Wang | |
238 | 94fdc6d0 | Jason Wang | memset(&ifr, 0, sizeof(ifr)); |
239 | 94fdc6d0 | Jason Wang | |
240 | 94fdc6d0 | Jason Wang | ifr.ifr_flags = IFF_ATTACH_QUEUE; |
241 | 94fdc6d0 | Jason Wang | ret = ioctl(fd, TUNSETQUEUE, (void *) &ifr);
|
242 | 94fdc6d0 | Jason Wang | |
243 | 94fdc6d0 | Jason Wang | if (ret != 0) { |
244 | 94fdc6d0 | Jason Wang | error_report("could not enable queue");
|
245 | 94fdc6d0 | Jason Wang | } |
246 | 94fdc6d0 | Jason Wang | |
247 | 94fdc6d0 | Jason Wang | return ret;
|
248 | 94fdc6d0 | Jason Wang | } |
249 | 94fdc6d0 | Jason Wang | |
250 | 94fdc6d0 | Jason Wang | /* Disable a specific queue of tap/ */
|
251 | 94fdc6d0 | Jason Wang | int tap_fd_disable(int fd) |
252 | 94fdc6d0 | Jason Wang | { |
253 | 94fdc6d0 | Jason Wang | struct ifreq ifr;
|
254 | 94fdc6d0 | Jason Wang | int ret;
|
255 | 94fdc6d0 | Jason Wang | |
256 | 94fdc6d0 | Jason Wang | memset(&ifr, 0, sizeof(ifr)); |
257 | 94fdc6d0 | Jason Wang | |
258 | 94fdc6d0 | Jason Wang | ifr.ifr_flags = IFF_DETACH_QUEUE; |
259 | 94fdc6d0 | Jason Wang | ret = ioctl(fd, TUNSETQUEUE, (void *) &ifr);
|
260 | 94fdc6d0 | Jason Wang | |
261 | 94fdc6d0 | Jason Wang | if (ret != 0) { |
262 | 94fdc6d0 | Jason Wang | error_report("could not disable queue");
|
263 | 94fdc6d0 | Jason Wang | } |
264 | 94fdc6d0 | Jason Wang | |
265 | 94fdc6d0 | Jason Wang | return ret;
|
266 | 94fdc6d0 | Jason Wang | } |
267 | 94fdc6d0 | Jason Wang | |
268 | e5dc0b40 | Jason Wang | int tap_fd_get_ifname(int fd, char *ifname) |
269 | e5dc0b40 | Jason Wang | { |
270 | e5dc0b40 | Jason Wang | struct ifreq ifr;
|
271 | e5dc0b40 | Jason Wang | |
272 | e5dc0b40 | Jason Wang | if (ioctl(fd, TUNGETIFF, &ifr) != 0) { |
273 | e5dc0b40 | Jason Wang | error_report("TUNGETIFF ioctl() failed: %s",
|
274 | e5dc0b40 | Jason Wang | strerror(errno)); |
275 | e5dc0b40 | Jason Wang | return -1; |
276 | e5dc0b40 | Jason Wang | } |
277 | e5dc0b40 | Jason Wang | |
278 | e5dc0b40 | Jason Wang | pstrcpy(ifname, sizeof(ifr.ifr_name), ifr.ifr_name);
|
279 | e5dc0b40 | Jason Wang | return 0; |
280 | e5dc0b40 | Jason Wang | } |