root / net / tap.c @ a5fd2c34
History | View | Annotate | Download (13.4 kB)
1 | 5281d757 | Mark McLoughlin | /*
|
---|---|---|---|
2 | 5281d757 | Mark McLoughlin | * QEMU System Emulator
|
3 | 5281d757 | Mark McLoughlin | *
|
4 | 5281d757 | Mark McLoughlin | * Copyright (c) 2003-2008 Fabrice Bellard
|
5 | 5281d757 | Mark McLoughlin | * Copyright (c) 2009 Red Hat, Inc.
|
6 | 5281d757 | Mark McLoughlin | *
|
7 | 5281d757 | Mark McLoughlin | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
8 | 5281d757 | Mark McLoughlin | * of this software and associated documentation files (the "Software"), to deal
|
9 | 5281d757 | Mark McLoughlin | * in the Software without restriction, including without limitation the rights
|
10 | 5281d757 | Mark McLoughlin | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 | 5281d757 | Mark McLoughlin | * copies of the Software, and to permit persons to whom the Software is
|
12 | 5281d757 | Mark McLoughlin | * furnished to do so, subject to the following conditions:
|
13 | 5281d757 | Mark McLoughlin | *
|
14 | 5281d757 | Mark McLoughlin | * The above copyright notice and this permission notice shall be included in
|
15 | 5281d757 | Mark McLoughlin | * all copies or substantial portions of the Software.
|
16 | 5281d757 | Mark McLoughlin | *
|
17 | 5281d757 | Mark McLoughlin | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18 | 5281d757 | Mark McLoughlin | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19 | 5281d757 | Mark McLoughlin | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
20 | 5281d757 | Mark McLoughlin | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21 | 5281d757 | Mark McLoughlin | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22 | 5281d757 | Mark McLoughlin | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
23 | 5281d757 | Mark McLoughlin | * THE SOFTWARE.
|
24 | 5281d757 | Mark McLoughlin | */
|
25 | 5281d757 | Mark McLoughlin | |
26 | 5281d757 | Mark McLoughlin | #include "net/tap.h" |
27 | 5281d757 | Mark McLoughlin | |
28 | 5281d757 | Mark McLoughlin | #include "config-host.h" |
29 | 5281d757 | Mark McLoughlin | |
30 | 5281d757 | Mark McLoughlin | #include <signal.h> |
31 | 5281d757 | Mark McLoughlin | #include <sys/ioctl.h> |
32 | 5281d757 | Mark McLoughlin | #include <sys/stat.h> |
33 | 5281d757 | Mark McLoughlin | #include <sys/wait.h> |
34 | 71f4effc | Alexander Graf | #include <sys/socket.h> |
35 | 5281d757 | Mark McLoughlin | #include <net/if.h> |
36 | 5281d757 | Mark McLoughlin | |
37 | 5281d757 | Mark McLoughlin | #include "net.h" |
38 | 5281d757 | Mark McLoughlin | #include "sysemu.h" |
39 | 5281d757 | Mark McLoughlin | #include "qemu-char.h" |
40 | 5281d757 | Mark McLoughlin | #include "qemu-common.h" |
41 | 2f792016 | Markus Armbruster | #include "qemu-error.h" |
42 | 5281d757 | Mark McLoughlin | |
43 | 5281d757 | Mark McLoughlin | #include "net/tap-linux.h" |
44 | 5281d757 | Mark McLoughlin | |
45 | 82b0d80e | Michael S. Tsirkin | #include "hw/vhost_net.h" |
46 | 82b0d80e | Michael S. Tsirkin | |
47 | 5281d757 | Mark McLoughlin | /* Maximum GSO packet size (64k) plus plenty of room for
|
48 | 5281d757 | Mark McLoughlin | * the ethernet and virtio_net headers
|
49 | 5281d757 | Mark McLoughlin | */
|
50 | 5281d757 | Mark McLoughlin | #define TAP_BUFSIZE (4096 + 65536) |
51 | 5281d757 | Mark McLoughlin | |
52 | 5281d757 | Mark McLoughlin | typedef struct TAPState { |
53 | 3e35ba93 | Mark McLoughlin | VLANClientState nc; |
54 | 5281d757 | Mark McLoughlin | int fd;
|
55 | 5281d757 | Mark McLoughlin | char down_script[1024]; |
56 | 5281d757 | Mark McLoughlin | char down_script_arg[128]; |
57 | 5281d757 | Mark McLoughlin | uint8_t buf[TAP_BUFSIZE]; |
58 | 5281d757 | Mark McLoughlin | unsigned int read_poll : 1; |
59 | 5281d757 | Mark McLoughlin | unsigned int write_poll : 1; |
60 | 5281d757 | Mark McLoughlin | unsigned int using_vnet_hdr : 1; |
61 | 5281d757 | Mark McLoughlin | unsigned int has_ufo: 1; |
62 | 82b0d80e | Michael S. Tsirkin | VHostNetState *vhost_net; |
63 | ef4252b1 | Michael S. Tsirkin | unsigned host_vnet_hdr_len;
|
64 | 5281d757 | Mark McLoughlin | } TAPState; |
65 | 5281d757 | Mark McLoughlin | |
66 | 5281d757 | Mark McLoughlin | static int launch_script(const char *setup_script, const char *ifname, int fd); |
67 | 5281d757 | Mark McLoughlin | |
68 | 5281d757 | Mark McLoughlin | static int tap_can_send(void *opaque); |
69 | 5281d757 | Mark McLoughlin | static void tap_send(void *opaque); |
70 | 5281d757 | Mark McLoughlin | static void tap_writable(void *opaque); |
71 | 5281d757 | Mark McLoughlin | |
72 | 5281d757 | Mark McLoughlin | static void tap_update_fd_handler(TAPState *s) |
73 | 5281d757 | Mark McLoughlin | { |
74 | 5281d757 | Mark McLoughlin | qemu_set_fd_handler2(s->fd, |
75 | 5281d757 | Mark McLoughlin | s->read_poll ? tap_can_send : NULL,
|
76 | 5281d757 | Mark McLoughlin | s->read_poll ? tap_send : NULL,
|
77 | 5281d757 | Mark McLoughlin | s->write_poll ? tap_writable : NULL,
|
78 | 5281d757 | Mark McLoughlin | s); |
79 | 5281d757 | Mark McLoughlin | } |
80 | 5281d757 | Mark McLoughlin | |
81 | 5281d757 | Mark McLoughlin | static void tap_read_poll(TAPState *s, int enable) |
82 | 5281d757 | Mark McLoughlin | { |
83 | 5281d757 | Mark McLoughlin | s->read_poll = !!enable; |
84 | 5281d757 | Mark McLoughlin | tap_update_fd_handler(s); |
85 | 5281d757 | Mark McLoughlin | } |
86 | 5281d757 | Mark McLoughlin | |
87 | 5281d757 | Mark McLoughlin | static void tap_write_poll(TAPState *s, int enable) |
88 | 5281d757 | Mark McLoughlin | { |
89 | 5281d757 | Mark McLoughlin | s->write_poll = !!enable; |
90 | 5281d757 | Mark McLoughlin | tap_update_fd_handler(s); |
91 | 5281d757 | Mark McLoughlin | } |
92 | 5281d757 | Mark McLoughlin | |
93 | 5281d757 | Mark McLoughlin | static void tap_writable(void *opaque) |
94 | 5281d757 | Mark McLoughlin | { |
95 | 5281d757 | Mark McLoughlin | TAPState *s = opaque; |
96 | 5281d757 | Mark McLoughlin | |
97 | 5281d757 | Mark McLoughlin | tap_write_poll(s, 0);
|
98 | 5281d757 | Mark McLoughlin | |
99 | 3e35ba93 | Mark McLoughlin | qemu_flush_queued_packets(&s->nc); |
100 | 5281d757 | Mark McLoughlin | } |
101 | 5281d757 | Mark McLoughlin | |
102 | 5281d757 | Mark McLoughlin | static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt) |
103 | 5281d757 | Mark McLoughlin | { |
104 | 5281d757 | Mark McLoughlin | ssize_t len; |
105 | 5281d757 | Mark McLoughlin | |
106 | 5281d757 | Mark McLoughlin | do {
|
107 | 5281d757 | Mark McLoughlin | len = writev(s->fd, iov, iovcnt); |
108 | 5281d757 | Mark McLoughlin | } while (len == -1 && errno == EINTR); |
109 | 5281d757 | Mark McLoughlin | |
110 | 5281d757 | Mark McLoughlin | if (len == -1 && errno == EAGAIN) { |
111 | 5281d757 | Mark McLoughlin | tap_write_poll(s, 1);
|
112 | 5281d757 | Mark McLoughlin | return 0; |
113 | 5281d757 | Mark McLoughlin | } |
114 | 5281d757 | Mark McLoughlin | |
115 | 5281d757 | Mark McLoughlin | return len;
|
116 | 5281d757 | Mark McLoughlin | } |
117 | 5281d757 | Mark McLoughlin | |
118 | 3e35ba93 | Mark McLoughlin | static ssize_t tap_receive_iov(VLANClientState *nc, const struct iovec *iov, |
119 | 5281d757 | Mark McLoughlin | int iovcnt)
|
120 | 5281d757 | Mark McLoughlin | { |
121 | 3e35ba93 | Mark McLoughlin | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
122 | 5281d757 | Mark McLoughlin | const struct iovec *iovp = iov; |
123 | 5281d757 | Mark McLoughlin | struct iovec iov_copy[iovcnt + 1]; |
124 | ef4252b1 | Michael S. Tsirkin | struct virtio_net_hdr_mrg_rxbuf hdr = { };
|
125 | 5281d757 | Mark McLoughlin | |
126 | ef4252b1 | Michael S. Tsirkin | if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
|
127 | 5281d757 | Mark McLoughlin | iov_copy[0].iov_base = &hdr;
|
128 | ef4252b1 | Michael S. Tsirkin | iov_copy[0].iov_len = s->host_vnet_hdr_len;
|
129 | 5281d757 | Mark McLoughlin | memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov)); |
130 | 5281d757 | Mark McLoughlin | iovp = iov_copy; |
131 | 5281d757 | Mark McLoughlin | iovcnt++; |
132 | 5281d757 | Mark McLoughlin | } |
133 | 5281d757 | Mark McLoughlin | |
134 | 5281d757 | Mark McLoughlin | return tap_write_packet(s, iovp, iovcnt);
|
135 | 5281d757 | Mark McLoughlin | } |
136 | 5281d757 | Mark McLoughlin | |
137 | 3e35ba93 | Mark McLoughlin | static ssize_t tap_receive_raw(VLANClientState *nc, const uint8_t *buf, size_t size) |
138 | 5281d757 | Mark McLoughlin | { |
139 | 3e35ba93 | Mark McLoughlin | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
140 | 5281d757 | Mark McLoughlin | struct iovec iov[2]; |
141 | 5281d757 | Mark McLoughlin | int iovcnt = 0; |
142 | ef4252b1 | Michael S. Tsirkin | struct virtio_net_hdr_mrg_rxbuf hdr = { };
|
143 | 5281d757 | Mark McLoughlin | |
144 | ef4252b1 | Michael S. Tsirkin | if (s->host_vnet_hdr_len) {
|
145 | 5281d757 | Mark McLoughlin | iov[iovcnt].iov_base = &hdr; |
146 | ef4252b1 | Michael S. Tsirkin | iov[iovcnt].iov_len = s->host_vnet_hdr_len; |
147 | 5281d757 | Mark McLoughlin | iovcnt++; |
148 | 5281d757 | Mark McLoughlin | } |
149 | 5281d757 | Mark McLoughlin | |
150 | 5281d757 | Mark McLoughlin | iov[iovcnt].iov_base = (char *)buf;
|
151 | 5281d757 | Mark McLoughlin | iov[iovcnt].iov_len = size; |
152 | 5281d757 | Mark McLoughlin | iovcnt++; |
153 | 5281d757 | Mark McLoughlin | |
154 | 5281d757 | Mark McLoughlin | return tap_write_packet(s, iov, iovcnt);
|
155 | 5281d757 | Mark McLoughlin | } |
156 | 5281d757 | Mark McLoughlin | |
157 | 3e35ba93 | Mark McLoughlin | static ssize_t tap_receive(VLANClientState *nc, const uint8_t *buf, size_t size) |
158 | 5281d757 | Mark McLoughlin | { |
159 | 3e35ba93 | Mark McLoughlin | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
160 | 5281d757 | Mark McLoughlin | struct iovec iov[1]; |
161 | 5281d757 | Mark McLoughlin | |
162 | ef4252b1 | Michael S. Tsirkin | if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
|
163 | 3e35ba93 | Mark McLoughlin | return tap_receive_raw(nc, buf, size);
|
164 | 5281d757 | Mark McLoughlin | } |
165 | 5281d757 | Mark McLoughlin | |
166 | 5281d757 | Mark McLoughlin | iov[0].iov_base = (char *)buf; |
167 | 5281d757 | Mark McLoughlin | iov[0].iov_len = size;
|
168 | 5281d757 | Mark McLoughlin | |
169 | 5281d757 | Mark McLoughlin | return tap_write_packet(s, iov, 1); |
170 | 5281d757 | Mark McLoughlin | } |
171 | 5281d757 | Mark McLoughlin | |
172 | 5281d757 | Mark McLoughlin | static int tap_can_send(void *opaque) |
173 | 5281d757 | Mark McLoughlin | { |
174 | 5281d757 | Mark McLoughlin | TAPState *s = opaque; |
175 | 5281d757 | Mark McLoughlin | |
176 | 3e35ba93 | Mark McLoughlin | return qemu_can_send_packet(&s->nc);
|
177 | 5281d757 | Mark McLoughlin | } |
178 | 5281d757 | Mark McLoughlin | |
179 | 966ea5ec | Mark McLoughlin | #ifndef __sun__
|
180 | 966ea5ec | Mark McLoughlin | ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen) |
181 | 5281d757 | Mark McLoughlin | { |
182 | 5281d757 | Mark McLoughlin | return read(tapfd, buf, maxlen);
|
183 | 5281d757 | Mark McLoughlin | } |
184 | 5281d757 | Mark McLoughlin | #endif
|
185 | 5281d757 | Mark McLoughlin | |
186 | 3e35ba93 | Mark McLoughlin | static void tap_send_completed(VLANClientState *nc, ssize_t len) |
187 | 5281d757 | Mark McLoughlin | { |
188 | 3e35ba93 | Mark McLoughlin | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
189 | 5281d757 | Mark McLoughlin | tap_read_poll(s, 1);
|
190 | 5281d757 | Mark McLoughlin | } |
191 | 5281d757 | Mark McLoughlin | |
192 | 5281d757 | Mark McLoughlin | static void tap_send(void *opaque) |
193 | 5281d757 | Mark McLoughlin | { |
194 | 5281d757 | Mark McLoughlin | TAPState *s = opaque; |
195 | 5281d757 | Mark McLoughlin | int size;
|
196 | 5281d757 | Mark McLoughlin | |
197 | 5819c918 | Mark McLoughlin | do {
|
198 | 5819c918 | Mark McLoughlin | uint8_t *buf = s->buf; |
199 | 5819c918 | Mark McLoughlin | |
200 | 5819c918 | Mark McLoughlin | size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
|
201 | 5819c918 | Mark McLoughlin | if (size <= 0) { |
202 | 5819c918 | Mark McLoughlin | break;
|
203 | 5819c918 | Mark McLoughlin | } |
204 | 5819c918 | Mark McLoughlin | |
205 | ef4252b1 | Michael S. Tsirkin | if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
|
206 | ef4252b1 | Michael S. Tsirkin | buf += s->host_vnet_hdr_len; |
207 | ef4252b1 | Michael S. Tsirkin | size -= s->host_vnet_hdr_len; |
208 | 5819c918 | Mark McLoughlin | } |
209 | 5819c918 | Mark McLoughlin | |
210 | 3e35ba93 | Mark McLoughlin | size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed); |
211 | 5819c918 | Mark McLoughlin | if (size == 0) { |
212 | 5819c918 | Mark McLoughlin | tap_read_poll(s, 0);
|
213 | 5819c918 | Mark McLoughlin | } |
214 | 3e35ba93 | Mark McLoughlin | } while (size > 0 && qemu_can_send_packet(&s->nc)); |
215 | 5281d757 | Mark McLoughlin | } |
216 | 5281d757 | Mark McLoughlin | |
217 | 3e35ba93 | Mark McLoughlin | int tap_has_ufo(VLANClientState *nc)
|
218 | 5281d757 | Mark McLoughlin | { |
219 | 3e35ba93 | Mark McLoughlin | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
220 | 5281d757 | Mark McLoughlin | |
221 | 665a3b07 | Mark McLoughlin | assert(nc->info->type == NET_CLIENT_TYPE_TAP); |
222 | 5281d757 | Mark McLoughlin | |
223 | 5281d757 | Mark McLoughlin | return s->has_ufo;
|
224 | 5281d757 | Mark McLoughlin | } |
225 | 5281d757 | Mark McLoughlin | |
226 | 3e35ba93 | Mark McLoughlin | int tap_has_vnet_hdr(VLANClientState *nc)
|
227 | 5281d757 | Mark McLoughlin | { |
228 | 3e35ba93 | Mark McLoughlin | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
229 | 5281d757 | Mark McLoughlin | |
230 | 665a3b07 | Mark McLoughlin | assert(nc->info->type == NET_CLIENT_TYPE_TAP); |
231 | 5281d757 | Mark McLoughlin | |
232 | ef4252b1 | Michael S. Tsirkin | return !!s->host_vnet_hdr_len;
|
233 | 5281d757 | Mark McLoughlin | } |
234 | 5281d757 | Mark McLoughlin | |
235 | 445d892f | Michael S. Tsirkin | int tap_has_vnet_hdr_len(VLANClientState *nc, int len) |
236 | 445d892f | Michael S. Tsirkin | { |
237 | 445d892f | Michael S. Tsirkin | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
238 | 445d892f | Michael S. Tsirkin | |
239 | 445d892f | Michael S. Tsirkin | assert(nc->info->type == NET_CLIENT_TYPE_TAP); |
240 | 445d892f | Michael S. Tsirkin | |
241 | 445d892f | Michael S. Tsirkin | return tap_probe_vnet_hdr_len(s->fd, len);
|
242 | 445d892f | Michael S. Tsirkin | } |
243 | 445d892f | Michael S. Tsirkin | |
244 | 445d892f | Michael S. Tsirkin | void tap_set_vnet_hdr_len(VLANClientState *nc, int len) |
245 | 445d892f | Michael S. Tsirkin | { |
246 | 445d892f | Michael S. Tsirkin | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
247 | 445d892f | Michael S. Tsirkin | |
248 | 445d892f | Michael S. Tsirkin | assert(nc->info->type == NET_CLIENT_TYPE_TAP); |
249 | 445d892f | Michael S. Tsirkin | assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) || |
250 | 445d892f | Michael S. Tsirkin | len == sizeof(struct virtio_net_hdr)); |
251 | 445d892f | Michael S. Tsirkin | |
252 | 445d892f | Michael S. Tsirkin | tap_fd_set_vnet_hdr_len(s->fd, len); |
253 | 445d892f | Michael S. Tsirkin | s->host_vnet_hdr_len = len; |
254 | 445d892f | Michael S. Tsirkin | } |
255 | 445d892f | Michael S. Tsirkin | |
256 | 3e35ba93 | Mark McLoughlin | void tap_using_vnet_hdr(VLANClientState *nc, int using_vnet_hdr) |
257 | 5281d757 | Mark McLoughlin | { |
258 | 3e35ba93 | Mark McLoughlin | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
259 | 5281d757 | Mark McLoughlin | |
260 | 5281d757 | Mark McLoughlin | using_vnet_hdr = using_vnet_hdr != 0;
|
261 | 5281d757 | Mark McLoughlin | |
262 | 665a3b07 | Mark McLoughlin | assert(nc->info->type == NET_CLIENT_TYPE_TAP); |
263 | ef4252b1 | Michael S. Tsirkin | assert(!!s->host_vnet_hdr_len == using_vnet_hdr); |
264 | 5281d757 | Mark McLoughlin | |
265 | 5281d757 | Mark McLoughlin | s->using_vnet_hdr = using_vnet_hdr; |
266 | 5281d757 | Mark McLoughlin | } |
267 | 5281d757 | Mark McLoughlin | |
268 | 3e35ba93 | Mark McLoughlin | void tap_set_offload(VLANClientState *nc, int csum, int tso4, |
269 | 5281d757 | Mark McLoughlin | int tso6, int ecn, int ufo) |
270 | 5281d757 | Mark McLoughlin | { |
271 | 3e35ba93 | Mark McLoughlin | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
272 | a5fd2c34 | Michael S. Tsirkin | if (s->fd < 0) { |
273 | a5fd2c34 | Michael S. Tsirkin | return;
|
274 | a5fd2c34 | Michael S. Tsirkin | } |
275 | 5281d757 | Mark McLoughlin | |
276 | a5fd2c34 | Michael S. Tsirkin | tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo); |
277 | 5281d757 | Mark McLoughlin | } |
278 | 5281d757 | Mark McLoughlin | |
279 | 3e35ba93 | Mark McLoughlin | static void tap_cleanup(VLANClientState *nc) |
280 | 5281d757 | Mark McLoughlin | { |
281 | 3e35ba93 | Mark McLoughlin | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
282 | 5281d757 | Mark McLoughlin | |
283 | 82b0d80e | Michael S. Tsirkin | if (s->vhost_net) {
|
284 | 82b0d80e | Michael S. Tsirkin | vhost_net_cleanup(s->vhost_net); |
285 | 54cdaa1b | Michael S. Tsirkin | s->vhost_net = NULL;
|
286 | 82b0d80e | Michael S. Tsirkin | } |
287 | 82b0d80e | Michael S. Tsirkin | |
288 | 3e35ba93 | Mark McLoughlin | qemu_purge_queued_packets(nc); |
289 | 5281d757 | Mark McLoughlin | |
290 | 5281d757 | Mark McLoughlin | if (s->down_script[0]) |
291 | 5281d757 | Mark McLoughlin | launch_script(s->down_script, s->down_script_arg, s->fd); |
292 | 5281d757 | Mark McLoughlin | |
293 | 5281d757 | Mark McLoughlin | tap_read_poll(s, 0);
|
294 | 5281d757 | Mark McLoughlin | tap_write_poll(s, 0);
|
295 | 5281d757 | Mark McLoughlin | close(s->fd); |
296 | a5fd2c34 | Michael S. Tsirkin | s->fd = -1;
|
297 | 5281d757 | Mark McLoughlin | } |
298 | 5281d757 | Mark McLoughlin | |
299 | ceb69615 | Michael S. Tsirkin | static void tap_poll(VLANClientState *nc, bool enable) |
300 | ceb69615 | Michael S. Tsirkin | { |
301 | ceb69615 | Michael S. Tsirkin | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
302 | ceb69615 | Michael S. Tsirkin | tap_read_poll(s, enable); |
303 | ceb69615 | Michael S. Tsirkin | tap_write_poll(s, enable); |
304 | ceb69615 | Michael S. Tsirkin | } |
305 | ceb69615 | Michael S. Tsirkin | |
306 | 95d528a2 | Michael S. Tsirkin | int tap_get_fd(VLANClientState *nc)
|
307 | 95d528a2 | Michael S. Tsirkin | { |
308 | 95d528a2 | Michael S. Tsirkin | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
309 | 95d528a2 | Michael S. Tsirkin | assert(nc->info->type == NET_CLIENT_TYPE_TAP); |
310 | 95d528a2 | Michael S. Tsirkin | return s->fd;
|
311 | 95d528a2 | Michael S. Tsirkin | } |
312 | 95d528a2 | Michael S. Tsirkin | |
313 | 5281d757 | Mark McLoughlin | /* fd support */
|
314 | 5281d757 | Mark McLoughlin | |
315 | 3e35ba93 | Mark McLoughlin | static NetClientInfo net_tap_info = {
|
316 | 3e35ba93 | Mark McLoughlin | .type = NET_CLIENT_TYPE_TAP, |
317 | 3e35ba93 | Mark McLoughlin | .size = sizeof(TAPState),
|
318 | 3e35ba93 | Mark McLoughlin | .receive = tap_receive, |
319 | 3e35ba93 | Mark McLoughlin | .receive_raw = tap_receive_raw, |
320 | 3e35ba93 | Mark McLoughlin | .receive_iov = tap_receive_iov, |
321 | ceb69615 | Michael S. Tsirkin | .poll = tap_poll, |
322 | 3e35ba93 | Mark McLoughlin | .cleanup = tap_cleanup, |
323 | 3e35ba93 | Mark McLoughlin | }; |
324 | 3e35ba93 | Mark McLoughlin | |
325 | 5281d757 | Mark McLoughlin | static TAPState *net_tap_fd_init(VLANState *vlan,
|
326 | 5281d757 | Mark McLoughlin | const char *model, |
327 | 5281d757 | Mark McLoughlin | const char *name, |
328 | 5281d757 | Mark McLoughlin | int fd,
|
329 | 5281d757 | Mark McLoughlin | int vnet_hdr)
|
330 | 5281d757 | Mark McLoughlin | { |
331 | 3e35ba93 | Mark McLoughlin | VLANClientState *nc; |
332 | 5281d757 | Mark McLoughlin | TAPState *s; |
333 | 5281d757 | Mark McLoughlin | |
334 | 3e35ba93 | Mark McLoughlin | nc = qemu_new_net_client(&net_tap_info, vlan, NULL, model, name);
|
335 | 3e35ba93 | Mark McLoughlin | |
336 | 3e35ba93 | Mark McLoughlin | s = DO_UPCAST(TAPState, nc, nc); |
337 | 3e35ba93 | Mark McLoughlin | |
338 | 5281d757 | Mark McLoughlin | s->fd = fd; |
339 | ef4252b1 | Michael S. Tsirkin | s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0; |
340 | 5281d757 | Mark McLoughlin | s->using_vnet_hdr = 0;
|
341 | 9c282718 | Mark McLoughlin | s->has_ufo = tap_probe_has_ufo(s->fd); |
342 | 3e35ba93 | Mark McLoughlin | tap_set_offload(&s->nc, 0, 0, 0, 0, 0); |
343 | 5281d757 | Mark McLoughlin | tap_read_poll(s, 1);
|
344 | 82b0d80e | Michael S. Tsirkin | s->vhost_net = NULL;
|
345 | 5281d757 | Mark McLoughlin | return s;
|
346 | 5281d757 | Mark McLoughlin | } |
347 | 5281d757 | Mark McLoughlin | |
348 | 5281d757 | Mark McLoughlin | static int launch_script(const char *setup_script, const char *ifname, int fd) |
349 | 5281d757 | Mark McLoughlin | { |
350 | 5281d757 | Mark McLoughlin | sigset_t oldmask, mask; |
351 | 5281d757 | Mark McLoughlin | int pid, status;
|
352 | 5281d757 | Mark McLoughlin | char *args[3]; |
353 | 5281d757 | Mark McLoughlin | char **parg;
|
354 | 5281d757 | Mark McLoughlin | |
355 | 5281d757 | Mark McLoughlin | sigemptyset(&mask); |
356 | 5281d757 | Mark McLoughlin | sigaddset(&mask, SIGCHLD); |
357 | 5281d757 | Mark McLoughlin | sigprocmask(SIG_BLOCK, &mask, &oldmask); |
358 | 5281d757 | Mark McLoughlin | |
359 | 5281d757 | Mark McLoughlin | /* try to launch network script */
|
360 | 5281d757 | Mark McLoughlin | pid = fork(); |
361 | 5281d757 | Mark McLoughlin | if (pid == 0) { |
362 | 5281d757 | Mark McLoughlin | int open_max = sysconf(_SC_OPEN_MAX), i;
|
363 | 5281d757 | Mark McLoughlin | |
364 | 5281d757 | Mark McLoughlin | for (i = 0; i < open_max; i++) { |
365 | 5281d757 | Mark McLoughlin | if (i != STDIN_FILENO &&
|
366 | 5281d757 | Mark McLoughlin | i != STDOUT_FILENO && |
367 | 5281d757 | Mark McLoughlin | i != STDERR_FILENO && |
368 | 5281d757 | Mark McLoughlin | i != fd) { |
369 | 5281d757 | Mark McLoughlin | close(i); |
370 | 5281d757 | Mark McLoughlin | } |
371 | 5281d757 | Mark McLoughlin | } |
372 | 5281d757 | Mark McLoughlin | parg = args; |
373 | 5281d757 | Mark McLoughlin | *parg++ = (char *)setup_script;
|
374 | 5281d757 | Mark McLoughlin | *parg++ = (char *)ifname;
|
375 | 9678d950 | Blue Swirl | *parg = NULL;
|
376 | 5281d757 | Mark McLoughlin | execv(setup_script, args); |
377 | 5281d757 | Mark McLoughlin | _exit(1);
|
378 | 5281d757 | Mark McLoughlin | } else if (pid > 0) { |
379 | 5281d757 | Mark McLoughlin | while (waitpid(pid, &status, 0) != pid) { |
380 | 5281d757 | Mark McLoughlin | /* loop */
|
381 | 5281d757 | Mark McLoughlin | } |
382 | 5281d757 | Mark McLoughlin | sigprocmask(SIG_SETMASK, &oldmask, NULL);
|
383 | 5281d757 | Mark McLoughlin | |
384 | 5281d757 | Mark McLoughlin | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
385 | 5281d757 | Mark McLoughlin | return 0; |
386 | 5281d757 | Mark McLoughlin | } |
387 | 5281d757 | Mark McLoughlin | } |
388 | 5281d757 | Mark McLoughlin | fprintf(stderr, "%s: could not launch network script\n", setup_script);
|
389 | 5281d757 | Mark McLoughlin | return -1; |
390 | 5281d757 | Mark McLoughlin | } |
391 | 5281d757 | Mark McLoughlin | |
392 | 5281d757 | Mark McLoughlin | static int net_tap_init(QemuOpts *opts, int *vnet_hdr) |
393 | 5281d757 | Mark McLoughlin | { |
394 | 5281d757 | Mark McLoughlin | int fd, vnet_hdr_required;
|
395 | 5281d757 | Mark McLoughlin | char ifname[128] = {0,}; |
396 | 5281d757 | Mark McLoughlin | const char *setup_script; |
397 | 5281d757 | Mark McLoughlin | |
398 | 5281d757 | Mark McLoughlin | if (qemu_opt_get(opts, "ifname")) { |
399 | 5281d757 | Mark McLoughlin | pstrcpy(ifname, sizeof(ifname), qemu_opt_get(opts, "ifname")); |
400 | 5281d757 | Mark McLoughlin | } |
401 | 5281d757 | Mark McLoughlin | |
402 | 5281d757 | Mark McLoughlin | *vnet_hdr = qemu_opt_get_bool(opts, "vnet_hdr", 1); |
403 | 5281d757 | Mark McLoughlin | if (qemu_opt_get(opts, "vnet_hdr")) { |
404 | 5281d757 | Mark McLoughlin | vnet_hdr_required = *vnet_hdr; |
405 | 5281d757 | Mark McLoughlin | } else {
|
406 | 5281d757 | Mark McLoughlin | vnet_hdr_required = 0;
|
407 | 5281d757 | Mark McLoughlin | } |
408 | 5281d757 | Mark McLoughlin | |
409 | 5281d757 | Mark McLoughlin | TFR(fd = tap_open(ifname, sizeof(ifname), vnet_hdr, vnet_hdr_required));
|
410 | 5281d757 | Mark McLoughlin | if (fd < 0) { |
411 | 5281d757 | Mark McLoughlin | return -1; |
412 | 5281d757 | Mark McLoughlin | } |
413 | 5281d757 | Mark McLoughlin | |
414 | 5281d757 | Mark McLoughlin | setup_script = qemu_opt_get(opts, "script");
|
415 | 5281d757 | Mark McLoughlin | if (setup_script &&
|
416 | 5281d757 | Mark McLoughlin | setup_script[0] != '\0' && |
417 | 5281d757 | Mark McLoughlin | strcmp(setup_script, "no") != 0 && |
418 | 5281d757 | Mark McLoughlin | launch_script(setup_script, ifname, fd)) { |
419 | 5281d757 | Mark McLoughlin | close(fd); |
420 | 5281d757 | Mark McLoughlin | return -1; |
421 | 5281d757 | Mark McLoughlin | } |
422 | 5281d757 | Mark McLoughlin | |
423 | 5281d757 | Mark McLoughlin | qemu_opt_set(opts, "ifname", ifname);
|
424 | 5281d757 | Mark McLoughlin | |
425 | 5281d757 | Mark McLoughlin | return fd;
|
426 | 5281d757 | Mark McLoughlin | } |
427 | 5281d757 | Mark McLoughlin | |
428 | 5281d757 | Mark McLoughlin | int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan) |
429 | 5281d757 | Mark McLoughlin | { |
430 | 5281d757 | Mark McLoughlin | TAPState *s; |
431 | df6c2a0f | Mark McLoughlin | int fd, vnet_hdr = 0; |
432 | 5281d757 | Mark McLoughlin | |
433 | 5281d757 | Mark McLoughlin | if (qemu_opt_get(opts, "fd")) { |
434 | 5281d757 | Mark McLoughlin | if (qemu_opt_get(opts, "ifname") || |
435 | 5281d757 | Mark McLoughlin | qemu_opt_get(opts, "script") ||
|
436 | 5281d757 | Mark McLoughlin | qemu_opt_get(opts, "downscript") ||
|
437 | 5281d757 | Mark McLoughlin | qemu_opt_get(opts, "vnet_hdr")) {
|
438 | 1ecda02b | Markus Armbruster | error_report("ifname=, script=, downscript= and vnet_hdr= is invalid with fd=");
|
439 | 5281d757 | Mark McLoughlin | return -1; |
440 | 5281d757 | Mark McLoughlin | } |
441 | 5281d757 | Mark McLoughlin | |
442 | 5281d757 | Mark McLoughlin | fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
|
443 | 5281d757 | Mark McLoughlin | if (fd == -1) { |
444 | 5281d757 | Mark McLoughlin | return -1; |
445 | 5281d757 | Mark McLoughlin | } |
446 | 5281d757 | Mark McLoughlin | |
447 | 5281d757 | Mark McLoughlin | fcntl(fd, F_SETFL, O_NONBLOCK); |
448 | 5281d757 | Mark McLoughlin | |
449 | 5281d757 | Mark McLoughlin | vnet_hdr = tap_probe_vnet_hdr(fd); |
450 | 5281d757 | Mark McLoughlin | } else {
|
451 | 5281d757 | Mark McLoughlin | if (!qemu_opt_get(opts, "script")) { |
452 | 5281d757 | Mark McLoughlin | qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT);
|
453 | 5281d757 | Mark McLoughlin | } |
454 | 5281d757 | Mark McLoughlin | |
455 | 5281d757 | Mark McLoughlin | if (!qemu_opt_get(opts, "downscript")) { |
456 | 5281d757 | Mark McLoughlin | qemu_opt_set(opts, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT);
|
457 | 5281d757 | Mark McLoughlin | } |
458 | 5281d757 | Mark McLoughlin | |
459 | 5281d757 | Mark McLoughlin | fd = net_tap_init(opts, &vnet_hdr); |
460 | 929fe497 | Juergen Lock | if (fd == -1) { |
461 | 929fe497 | Juergen Lock | return -1; |
462 | 929fe497 | Juergen Lock | } |
463 | 5281d757 | Mark McLoughlin | } |
464 | 5281d757 | Mark McLoughlin | |
465 | 5281d757 | Mark McLoughlin | s = net_tap_fd_init(vlan, "tap", name, fd, vnet_hdr);
|
466 | 5281d757 | Mark McLoughlin | if (!s) {
|
467 | 5281d757 | Mark McLoughlin | close(fd); |
468 | 5281d757 | Mark McLoughlin | return -1; |
469 | 5281d757 | Mark McLoughlin | } |
470 | 5281d757 | Mark McLoughlin | |
471 | 15ac913b | Mark McLoughlin | if (tap_set_sndbuf(s->fd, opts) < 0) { |
472 | 5281d757 | Mark McLoughlin | return -1; |
473 | 5281d757 | Mark McLoughlin | } |
474 | 5281d757 | Mark McLoughlin | |
475 | 5281d757 | Mark McLoughlin | if (qemu_opt_get(opts, "fd")) { |
476 | 3e35ba93 | Mark McLoughlin | snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd); |
477 | 5281d757 | Mark McLoughlin | } else {
|
478 | 5281d757 | Mark McLoughlin | const char *ifname, *script, *downscript; |
479 | 5281d757 | Mark McLoughlin | |
480 | 5281d757 | Mark McLoughlin | ifname = qemu_opt_get(opts, "ifname");
|
481 | 5281d757 | Mark McLoughlin | script = qemu_opt_get(opts, "script");
|
482 | 5281d757 | Mark McLoughlin | downscript = qemu_opt_get(opts, "downscript");
|
483 | 5281d757 | Mark McLoughlin | |
484 | 3e35ba93 | Mark McLoughlin | snprintf(s->nc.info_str, sizeof(s->nc.info_str),
|
485 | 5281d757 | Mark McLoughlin | "ifname=%s,script=%s,downscript=%s",
|
486 | 5281d757 | Mark McLoughlin | ifname, script, downscript); |
487 | 5281d757 | Mark McLoughlin | |
488 | 5281d757 | Mark McLoughlin | if (strcmp(downscript, "no") != 0) { |
489 | 5281d757 | Mark McLoughlin | snprintf(s->down_script, sizeof(s->down_script), "%s", downscript); |
490 | 5281d757 | Mark McLoughlin | snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname); |
491 | 5281d757 | Mark McLoughlin | } |
492 | 5281d757 | Mark McLoughlin | } |
493 | 5281d757 | Mark McLoughlin | |
494 | 82b0d80e | Michael S. Tsirkin | if (qemu_opt_get_bool(opts, "vhost", !!qemu_opt_get(opts, "vhostfd"))) { |
495 | 82b0d80e | Michael S. Tsirkin | int vhostfd, r;
|
496 | 82b0d80e | Michael S. Tsirkin | if (qemu_opt_get(opts, "vhostfd")) { |
497 | 82b0d80e | Michael S. Tsirkin | r = net_handle_fd_param(mon, qemu_opt_get(opts, "vhostfd"));
|
498 | 82b0d80e | Michael S. Tsirkin | if (r == -1) { |
499 | 82b0d80e | Michael S. Tsirkin | return -1; |
500 | 82b0d80e | Michael S. Tsirkin | } |
501 | 82b0d80e | Michael S. Tsirkin | vhostfd = r; |
502 | 82b0d80e | Michael S. Tsirkin | } else {
|
503 | 82b0d80e | Michael S. Tsirkin | vhostfd = -1;
|
504 | 82b0d80e | Michael S. Tsirkin | } |
505 | 82b0d80e | Michael S. Tsirkin | s->vhost_net = vhost_net_init(&s->nc, vhostfd); |
506 | 82b0d80e | Michael S. Tsirkin | if (!s->vhost_net) {
|
507 | 82b0d80e | Michael S. Tsirkin | error_report("vhost-net requested but could not be initialized");
|
508 | 82b0d80e | Michael S. Tsirkin | return -1; |
509 | 82b0d80e | Michael S. Tsirkin | } |
510 | 82b0d80e | Michael S. Tsirkin | } else if (qemu_opt_get(opts, "vhostfd")) { |
511 | 82b0d80e | Michael S. Tsirkin | error_report("vhostfd= is not valid without vhost");
|
512 | 82b0d80e | Michael S. Tsirkin | return -1; |
513 | 82b0d80e | Michael S. Tsirkin | } |
514 | 82b0d80e | Michael S. Tsirkin | |
515 | 5281d757 | Mark McLoughlin | return 0; |
516 | 5281d757 | Mark McLoughlin | } |
517 | b202554c | Michael S. Tsirkin | |
518 | b202554c | Michael S. Tsirkin | VHostNetState *tap_get_vhost_net(VLANClientState *nc) |
519 | b202554c | Michael S. Tsirkin | { |
520 | b202554c | Michael S. Tsirkin | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
521 | b202554c | Michael S. Tsirkin | assert(nc->info->type == NET_CLIENT_TYPE_TAP); |
522 | b202554c | Michael S. Tsirkin | return s->vhost_net;
|
523 | b202554c | Michael S. Tsirkin | } |