root / net / tap.c @ 2966b390
History | View | Annotate | Download (11.1 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 | 5281d757 | Mark McLoughlin | |
42 | 5281d757 | Mark McLoughlin | #include "net/tap-linux.h" |
43 | 5281d757 | Mark McLoughlin | |
44 | 5281d757 | Mark McLoughlin | /* Maximum GSO packet size (64k) plus plenty of room for
|
45 | 5281d757 | Mark McLoughlin | * the ethernet and virtio_net headers
|
46 | 5281d757 | Mark McLoughlin | */
|
47 | 5281d757 | Mark McLoughlin | #define TAP_BUFSIZE (4096 + 65536) |
48 | 5281d757 | Mark McLoughlin | |
49 | 5281d757 | Mark McLoughlin | typedef struct TAPState { |
50 | 5281d757 | Mark McLoughlin | VLANClientState *vc; |
51 | 5281d757 | Mark McLoughlin | int fd;
|
52 | 5281d757 | Mark McLoughlin | char down_script[1024]; |
53 | 5281d757 | Mark McLoughlin | char down_script_arg[128]; |
54 | 5281d757 | Mark McLoughlin | uint8_t buf[TAP_BUFSIZE]; |
55 | 5281d757 | Mark McLoughlin | unsigned int read_poll : 1; |
56 | 5281d757 | Mark McLoughlin | unsigned int write_poll : 1; |
57 | 5281d757 | Mark McLoughlin | unsigned int has_vnet_hdr : 1; |
58 | 5281d757 | Mark McLoughlin | unsigned int using_vnet_hdr : 1; |
59 | 5281d757 | Mark McLoughlin | unsigned int has_ufo: 1; |
60 | 5281d757 | Mark McLoughlin | } TAPState; |
61 | 5281d757 | Mark McLoughlin | |
62 | 5281d757 | Mark McLoughlin | static int launch_script(const char *setup_script, const char *ifname, int fd); |
63 | 5281d757 | Mark McLoughlin | |
64 | 5281d757 | Mark McLoughlin | static int tap_can_send(void *opaque); |
65 | 5281d757 | Mark McLoughlin | static void tap_send(void *opaque); |
66 | 5281d757 | Mark McLoughlin | static void tap_writable(void *opaque); |
67 | 5281d757 | Mark McLoughlin | |
68 | 5281d757 | Mark McLoughlin | static void tap_update_fd_handler(TAPState *s) |
69 | 5281d757 | Mark McLoughlin | { |
70 | 5281d757 | Mark McLoughlin | qemu_set_fd_handler2(s->fd, |
71 | 5281d757 | Mark McLoughlin | s->read_poll ? tap_can_send : NULL,
|
72 | 5281d757 | Mark McLoughlin | s->read_poll ? tap_send : NULL,
|
73 | 5281d757 | Mark McLoughlin | s->write_poll ? tap_writable : NULL,
|
74 | 5281d757 | Mark McLoughlin | s); |
75 | 5281d757 | Mark McLoughlin | } |
76 | 5281d757 | Mark McLoughlin | |
77 | 5281d757 | Mark McLoughlin | static void tap_read_poll(TAPState *s, int enable) |
78 | 5281d757 | Mark McLoughlin | { |
79 | 5281d757 | Mark McLoughlin | s->read_poll = !!enable; |
80 | 5281d757 | Mark McLoughlin | tap_update_fd_handler(s); |
81 | 5281d757 | Mark McLoughlin | } |
82 | 5281d757 | Mark McLoughlin | |
83 | 5281d757 | Mark McLoughlin | static void tap_write_poll(TAPState *s, int enable) |
84 | 5281d757 | Mark McLoughlin | { |
85 | 5281d757 | Mark McLoughlin | s->write_poll = !!enable; |
86 | 5281d757 | Mark McLoughlin | tap_update_fd_handler(s); |
87 | 5281d757 | Mark McLoughlin | } |
88 | 5281d757 | Mark McLoughlin | |
89 | 5281d757 | Mark McLoughlin | static void tap_writable(void *opaque) |
90 | 5281d757 | Mark McLoughlin | { |
91 | 5281d757 | Mark McLoughlin | TAPState *s = opaque; |
92 | 5281d757 | Mark McLoughlin | |
93 | 5281d757 | Mark McLoughlin | tap_write_poll(s, 0);
|
94 | 5281d757 | Mark McLoughlin | |
95 | 5281d757 | Mark McLoughlin | qemu_flush_queued_packets(s->vc); |
96 | 5281d757 | Mark McLoughlin | } |
97 | 5281d757 | Mark McLoughlin | |
98 | 5281d757 | Mark McLoughlin | static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt) |
99 | 5281d757 | Mark McLoughlin | { |
100 | 5281d757 | Mark McLoughlin | ssize_t len; |
101 | 5281d757 | Mark McLoughlin | |
102 | 5281d757 | Mark McLoughlin | do {
|
103 | 5281d757 | Mark McLoughlin | len = writev(s->fd, iov, iovcnt); |
104 | 5281d757 | Mark McLoughlin | } while (len == -1 && errno == EINTR); |
105 | 5281d757 | Mark McLoughlin | |
106 | 5281d757 | Mark McLoughlin | if (len == -1 && errno == EAGAIN) { |
107 | 5281d757 | Mark McLoughlin | tap_write_poll(s, 1);
|
108 | 5281d757 | Mark McLoughlin | return 0; |
109 | 5281d757 | Mark McLoughlin | } |
110 | 5281d757 | Mark McLoughlin | |
111 | 5281d757 | Mark McLoughlin | return len;
|
112 | 5281d757 | Mark McLoughlin | } |
113 | 5281d757 | Mark McLoughlin | |
114 | 5281d757 | Mark McLoughlin | static ssize_t tap_receive_iov(VLANClientState *vc, const struct iovec *iov, |
115 | 5281d757 | Mark McLoughlin | int iovcnt)
|
116 | 5281d757 | Mark McLoughlin | { |
117 | 5281d757 | Mark McLoughlin | TAPState *s = vc->opaque; |
118 | 5281d757 | Mark McLoughlin | const struct iovec *iovp = iov; |
119 | 5281d757 | Mark McLoughlin | struct iovec iov_copy[iovcnt + 1]; |
120 | 5281d757 | Mark McLoughlin | struct virtio_net_hdr hdr = { 0, }; |
121 | 5281d757 | Mark McLoughlin | |
122 | 5281d757 | Mark McLoughlin | if (s->has_vnet_hdr && !s->using_vnet_hdr) {
|
123 | 5281d757 | Mark McLoughlin | iov_copy[0].iov_base = &hdr;
|
124 | 5281d757 | Mark McLoughlin | iov_copy[0].iov_len = sizeof(hdr); |
125 | 5281d757 | Mark McLoughlin | memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov)); |
126 | 5281d757 | Mark McLoughlin | iovp = iov_copy; |
127 | 5281d757 | Mark McLoughlin | iovcnt++; |
128 | 5281d757 | Mark McLoughlin | } |
129 | 5281d757 | Mark McLoughlin | |
130 | 5281d757 | Mark McLoughlin | return tap_write_packet(s, iovp, iovcnt);
|
131 | 5281d757 | Mark McLoughlin | } |
132 | 5281d757 | Mark McLoughlin | |
133 | 5281d757 | Mark McLoughlin | static ssize_t tap_receive_raw(VLANClientState *vc, const uint8_t *buf, size_t size) |
134 | 5281d757 | Mark McLoughlin | { |
135 | 5281d757 | Mark McLoughlin | TAPState *s = vc->opaque; |
136 | 5281d757 | Mark McLoughlin | struct iovec iov[2]; |
137 | 5281d757 | Mark McLoughlin | int iovcnt = 0; |
138 | 5281d757 | Mark McLoughlin | struct virtio_net_hdr hdr = { 0, }; |
139 | 5281d757 | Mark McLoughlin | |
140 | 5281d757 | Mark McLoughlin | if (s->has_vnet_hdr) {
|
141 | 5281d757 | Mark McLoughlin | iov[iovcnt].iov_base = &hdr; |
142 | 5281d757 | Mark McLoughlin | iov[iovcnt].iov_len = sizeof(hdr);
|
143 | 5281d757 | Mark McLoughlin | iovcnt++; |
144 | 5281d757 | Mark McLoughlin | } |
145 | 5281d757 | Mark McLoughlin | |
146 | 5281d757 | Mark McLoughlin | iov[iovcnt].iov_base = (char *)buf;
|
147 | 5281d757 | Mark McLoughlin | iov[iovcnt].iov_len = size; |
148 | 5281d757 | Mark McLoughlin | iovcnt++; |
149 | 5281d757 | Mark McLoughlin | |
150 | 5281d757 | Mark McLoughlin | return tap_write_packet(s, iov, iovcnt);
|
151 | 5281d757 | Mark McLoughlin | } |
152 | 5281d757 | Mark McLoughlin | |
153 | 5281d757 | Mark McLoughlin | static ssize_t tap_receive(VLANClientState *vc, const uint8_t *buf, size_t size) |
154 | 5281d757 | Mark McLoughlin | { |
155 | 5281d757 | Mark McLoughlin | TAPState *s = vc->opaque; |
156 | 5281d757 | Mark McLoughlin | struct iovec iov[1]; |
157 | 5281d757 | Mark McLoughlin | |
158 | 5281d757 | Mark McLoughlin | if (s->has_vnet_hdr && !s->using_vnet_hdr) {
|
159 | 5281d757 | Mark McLoughlin | return tap_receive_raw(vc, buf, size);
|
160 | 5281d757 | Mark McLoughlin | } |
161 | 5281d757 | Mark McLoughlin | |
162 | 5281d757 | Mark McLoughlin | iov[0].iov_base = (char *)buf; |
163 | 5281d757 | Mark McLoughlin | iov[0].iov_len = size;
|
164 | 5281d757 | Mark McLoughlin | |
165 | 5281d757 | Mark McLoughlin | return tap_write_packet(s, iov, 1); |
166 | 5281d757 | Mark McLoughlin | } |
167 | 5281d757 | Mark McLoughlin | |
168 | 5281d757 | Mark McLoughlin | static int tap_can_send(void *opaque) |
169 | 5281d757 | Mark McLoughlin | { |
170 | 5281d757 | Mark McLoughlin | TAPState *s = opaque; |
171 | 5281d757 | Mark McLoughlin | |
172 | 5281d757 | Mark McLoughlin | return qemu_can_send_packet(s->vc);
|
173 | 5281d757 | Mark McLoughlin | } |
174 | 5281d757 | Mark McLoughlin | |
175 | 966ea5ec | Mark McLoughlin | #ifndef __sun__
|
176 | 966ea5ec | Mark McLoughlin | ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen) |
177 | 5281d757 | Mark McLoughlin | { |
178 | 5281d757 | Mark McLoughlin | return read(tapfd, buf, maxlen);
|
179 | 5281d757 | Mark McLoughlin | } |
180 | 5281d757 | Mark McLoughlin | #endif
|
181 | 5281d757 | Mark McLoughlin | |
182 | 5281d757 | Mark McLoughlin | static void tap_send_completed(VLANClientState *vc, ssize_t len) |
183 | 5281d757 | Mark McLoughlin | { |
184 | 5281d757 | Mark McLoughlin | TAPState *s = vc->opaque; |
185 | 5281d757 | Mark McLoughlin | tap_read_poll(s, 1);
|
186 | 5281d757 | Mark McLoughlin | } |
187 | 5281d757 | Mark McLoughlin | |
188 | 5281d757 | Mark McLoughlin | static void tap_send(void *opaque) |
189 | 5281d757 | Mark McLoughlin | { |
190 | 5281d757 | Mark McLoughlin | TAPState *s = opaque; |
191 | 5281d757 | Mark McLoughlin | int size;
|
192 | 5281d757 | Mark McLoughlin | |
193 | 5819c918 | Mark McLoughlin | do {
|
194 | 5819c918 | Mark McLoughlin | uint8_t *buf = s->buf; |
195 | 5819c918 | Mark McLoughlin | |
196 | 5819c918 | Mark McLoughlin | size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
|
197 | 5819c918 | Mark McLoughlin | if (size <= 0) { |
198 | 5819c918 | Mark McLoughlin | break;
|
199 | 5819c918 | Mark McLoughlin | } |
200 | 5819c918 | Mark McLoughlin | |
201 | 5819c918 | Mark McLoughlin | if (s->has_vnet_hdr && !s->using_vnet_hdr) {
|
202 | 5819c918 | Mark McLoughlin | buf += sizeof(struct virtio_net_hdr); |
203 | 5819c918 | Mark McLoughlin | size -= sizeof(struct virtio_net_hdr); |
204 | 5819c918 | Mark McLoughlin | } |
205 | 5819c918 | Mark McLoughlin | |
206 | 5819c918 | Mark McLoughlin | size = qemu_send_packet_async(s->vc, buf, size, tap_send_completed); |
207 | 5819c918 | Mark McLoughlin | if (size == 0) { |
208 | 5819c918 | Mark McLoughlin | tap_read_poll(s, 0);
|
209 | 5819c918 | Mark McLoughlin | } |
210 | 5819c918 | Mark McLoughlin | } while (size > 0 && qemu_can_send_packet(s->vc)); |
211 | 5281d757 | Mark McLoughlin | } |
212 | 5281d757 | Mark McLoughlin | |
213 | 5281d757 | Mark McLoughlin | int tap_has_ufo(VLANClientState *vc)
|
214 | 5281d757 | Mark McLoughlin | { |
215 | 5281d757 | Mark McLoughlin | TAPState *s = vc->opaque; |
216 | 5281d757 | Mark McLoughlin | |
217 | 5281d757 | Mark McLoughlin | assert(vc->type == NET_CLIENT_TYPE_TAP); |
218 | 5281d757 | Mark McLoughlin | |
219 | 5281d757 | Mark McLoughlin | return s->has_ufo;
|
220 | 5281d757 | Mark McLoughlin | } |
221 | 5281d757 | Mark McLoughlin | |
222 | 5281d757 | Mark McLoughlin | int tap_has_vnet_hdr(VLANClientState *vc)
|
223 | 5281d757 | Mark McLoughlin | { |
224 | 5281d757 | Mark McLoughlin | TAPState *s = vc->opaque; |
225 | 5281d757 | Mark McLoughlin | |
226 | 5281d757 | Mark McLoughlin | assert(vc->type == NET_CLIENT_TYPE_TAP); |
227 | 5281d757 | Mark McLoughlin | |
228 | 5281d757 | Mark McLoughlin | return s->has_vnet_hdr;
|
229 | 5281d757 | Mark McLoughlin | } |
230 | 5281d757 | Mark McLoughlin | |
231 | 5281d757 | Mark McLoughlin | void tap_using_vnet_hdr(VLANClientState *vc, int using_vnet_hdr) |
232 | 5281d757 | Mark McLoughlin | { |
233 | 5281d757 | Mark McLoughlin | TAPState *s = vc->opaque; |
234 | 5281d757 | Mark McLoughlin | |
235 | 5281d757 | Mark McLoughlin | using_vnet_hdr = using_vnet_hdr != 0;
|
236 | 5281d757 | Mark McLoughlin | |
237 | 5281d757 | Mark McLoughlin | assert(vc->type == NET_CLIENT_TYPE_TAP); |
238 | 5281d757 | Mark McLoughlin | assert(s->has_vnet_hdr == using_vnet_hdr); |
239 | 5281d757 | Mark McLoughlin | |
240 | 5281d757 | Mark McLoughlin | s->using_vnet_hdr = using_vnet_hdr; |
241 | 5281d757 | Mark McLoughlin | } |
242 | 5281d757 | Mark McLoughlin | |
243 | 5281d757 | Mark McLoughlin | void tap_set_offload(VLANClientState *vc, int csum, int tso4, |
244 | 5281d757 | Mark McLoughlin | int tso6, int ecn, int ufo) |
245 | 5281d757 | Mark McLoughlin | { |
246 | 5281d757 | Mark McLoughlin | TAPState *s = vc->opaque; |
247 | 5281d757 | Mark McLoughlin | |
248 | 1faac1f7 | Mark McLoughlin | return tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
|
249 | 5281d757 | Mark McLoughlin | } |
250 | 5281d757 | Mark McLoughlin | |
251 | 5281d757 | Mark McLoughlin | static void tap_cleanup(VLANClientState *vc) |
252 | 5281d757 | Mark McLoughlin | { |
253 | 5281d757 | Mark McLoughlin | TAPState *s = vc->opaque; |
254 | 5281d757 | Mark McLoughlin | |
255 | 5281d757 | Mark McLoughlin | qemu_purge_queued_packets(vc); |
256 | 5281d757 | Mark McLoughlin | |
257 | 5281d757 | Mark McLoughlin | if (s->down_script[0]) |
258 | 5281d757 | Mark McLoughlin | launch_script(s->down_script, s->down_script_arg, s->fd); |
259 | 5281d757 | Mark McLoughlin | |
260 | 5281d757 | Mark McLoughlin | tap_read_poll(s, 0);
|
261 | 5281d757 | Mark McLoughlin | tap_write_poll(s, 0);
|
262 | 5281d757 | Mark McLoughlin | close(s->fd); |
263 | 5281d757 | Mark McLoughlin | qemu_free(s); |
264 | 5281d757 | Mark McLoughlin | } |
265 | 5281d757 | Mark McLoughlin | |
266 | 5281d757 | Mark McLoughlin | /* fd support */
|
267 | 5281d757 | Mark McLoughlin | |
268 | 5281d757 | Mark McLoughlin | static TAPState *net_tap_fd_init(VLANState *vlan,
|
269 | 5281d757 | Mark McLoughlin | const char *model, |
270 | 5281d757 | Mark McLoughlin | const char *name, |
271 | 5281d757 | Mark McLoughlin | int fd,
|
272 | 5281d757 | Mark McLoughlin | int vnet_hdr)
|
273 | 5281d757 | Mark McLoughlin | { |
274 | 5281d757 | Mark McLoughlin | TAPState *s; |
275 | 5281d757 | Mark McLoughlin | |
276 | 5281d757 | Mark McLoughlin | s = qemu_mallocz(sizeof(TAPState));
|
277 | 5281d757 | Mark McLoughlin | s->fd = fd; |
278 | 5281d757 | Mark McLoughlin | s->has_vnet_hdr = vnet_hdr != 0;
|
279 | 5281d757 | Mark McLoughlin | s->using_vnet_hdr = 0;
|
280 | 5281d757 | Mark McLoughlin | s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_TAP, |
281 | 5281d757 | Mark McLoughlin | vlan, NULL, model, name, NULL, |
282 | 5281d757 | Mark McLoughlin | tap_receive, tap_receive_raw, |
283 | 5281d757 | Mark McLoughlin | tap_receive_iov, tap_cleanup, s); |
284 | 9c282718 | Mark McLoughlin | s->has_ufo = tap_probe_has_ufo(s->fd); |
285 | 5281d757 | Mark McLoughlin | tap_set_offload(s->vc, 0, 0, 0, 0, 0); |
286 | 5281d757 | Mark McLoughlin | tap_read_poll(s, 1);
|
287 | 5281d757 | Mark McLoughlin | return s;
|
288 | 5281d757 | Mark McLoughlin | } |
289 | 5281d757 | Mark McLoughlin | |
290 | 5281d757 | Mark McLoughlin | static int launch_script(const char *setup_script, const char *ifname, int fd) |
291 | 5281d757 | Mark McLoughlin | { |
292 | 5281d757 | Mark McLoughlin | sigset_t oldmask, mask; |
293 | 5281d757 | Mark McLoughlin | int pid, status;
|
294 | 5281d757 | Mark McLoughlin | char *args[3]; |
295 | 5281d757 | Mark McLoughlin | char **parg;
|
296 | 5281d757 | Mark McLoughlin | |
297 | 5281d757 | Mark McLoughlin | sigemptyset(&mask); |
298 | 5281d757 | Mark McLoughlin | sigaddset(&mask, SIGCHLD); |
299 | 5281d757 | Mark McLoughlin | sigprocmask(SIG_BLOCK, &mask, &oldmask); |
300 | 5281d757 | Mark McLoughlin | |
301 | 5281d757 | Mark McLoughlin | /* try to launch network script */
|
302 | 5281d757 | Mark McLoughlin | pid = fork(); |
303 | 5281d757 | Mark McLoughlin | if (pid == 0) { |
304 | 5281d757 | Mark McLoughlin | int open_max = sysconf(_SC_OPEN_MAX), i;
|
305 | 5281d757 | Mark McLoughlin | |
306 | 5281d757 | Mark McLoughlin | for (i = 0; i < open_max; i++) { |
307 | 5281d757 | Mark McLoughlin | if (i != STDIN_FILENO &&
|
308 | 5281d757 | Mark McLoughlin | i != STDOUT_FILENO && |
309 | 5281d757 | Mark McLoughlin | i != STDERR_FILENO && |
310 | 5281d757 | Mark McLoughlin | i != fd) { |
311 | 5281d757 | Mark McLoughlin | close(i); |
312 | 5281d757 | Mark McLoughlin | } |
313 | 5281d757 | Mark McLoughlin | } |
314 | 5281d757 | Mark McLoughlin | parg = args; |
315 | 5281d757 | Mark McLoughlin | *parg++ = (char *)setup_script;
|
316 | 5281d757 | Mark McLoughlin | *parg++ = (char *)ifname;
|
317 | 5281d757 | Mark McLoughlin | *parg++ = NULL;
|
318 | 5281d757 | Mark McLoughlin | execv(setup_script, args); |
319 | 5281d757 | Mark McLoughlin | _exit(1);
|
320 | 5281d757 | Mark McLoughlin | } else if (pid > 0) { |
321 | 5281d757 | Mark McLoughlin | while (waitpid(pid, &status, 0) != pid) { |
322 | 5281d757 | Mark McLoughlin | /* loop */
|
323 | 5281d757 | Mark McLoughlin | } |
324 | 5281d757 | Mark McLoughlin | sigprocmask(SIG_SETMASK, &oldmask, NULL);
|
325 | 5281d757 | Mark McLoughlin | |
326 | 5281d757 | Mark McLoughlin | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
327 | 5281d757 | Mark McLoughlin | return 0; |
328 | 5281d757 | Mark McLoughlin | } |
329 | 5281d757 | Mark McLoughlin | } |
330 | 5281d757 | Mark McLoughlin | fprintf(stderr, "%s: could not launch network script\n", setup_script);
|
331 | 5281d757 | Mark McLoughlin | return -1; |
332 | 5281d757 | Mark McLoughlin | } |
333 | 5281d757 | Mark McLoughlin | |
334 | 5281d757 | Mark McLoughlin | static int net_tap_init(QemuOpts *opts, int *vnet_hdr) |
335 | 5281d757 | Mark McLoughlin | { |
336 | 5281d757 | Mark McLoughlin | int fd, vnet_hdr_required;
|
337 | 5281d757 | Mark McLoughlin | char ifname[128] = {0,}; |
338 | 5281d757 | Mark McLoughlin | const char *setup_script; |
339 | 5281d757 | Mark McLoughlin | |
340 | 5281d757 | Mark McLoughlin | if (qemu_opt_get(opts, "ifname")) { |
341 | 5281d757 | Mark McLoughlin | pstrcpy(ifname, sizeof(ifname), qemu_opt_get(opts, "ifname")); |
342 | 5281d757 | Mark McLoughlin | } |
343 | 5281d757 | Mark McLoughlin | |
344 | 5281d757 | Mark McLoughlin | *vnet_hdr = qemu_opt_get_bool(opts, "vnet_hdr", 1); |
345 | 5281d757 | Mark McLoughlin | if (qemu_opt_get(opts, "vnet_hdr")) { |
346 | 5281d757 | Mark McLoughlin | vnet_hdr_required = *vnet_hdr; |
347 | 5281d757 | Mark McLoughlin | } else {
|
348 | 5281d757 | Mark McLoughlin | vnet_hdr_required = 0;
|
349 | 5281d757 | Mark McLoughlin | } |
350 | 5281d757 | Mark McLoughlin | |
351 | 5281d757 | Mark McLoughlin | TFR(fd = tap_open(ifname, sizeof(ifname), vnet_hdr, vnet_hdr_required));
|
352 | 5281d757 | Mark McLoughlin | if (fd < 0) { |
353 | 5281d757 | Mark McLoughlin | return -1; |
354 | 5281d757 | Mark McLoughlin | } |
355 | 5281d757 | Mark McLoughlin | |
356 | 5281d757 | Mark McLoughlin | setup_script = qemu_opt_get(opts, "script");
|
357 | 5281d757 | Mark McLoughlin | if (setup_script &&
|
358 | 5281d757 | Mark McLoughlin | setup_script[0] != '\0' && |
359 | 5281d757 | Mark McLoughlin | strcmp(setup_script, "no") != 0 && |
360 | 5281d757 | Mark McLoughlin | launch_script(setup_script, ifname, fd)) { |
361 | 5281d757 | Mark McLoughlin | close(fd); |
362 | 5281d757 | Mark McLoughlin | return -1; |
363 | 5281d757 | Mark McLoughlin | } |
364 | 5281d757 | Mark McLoughlin | |
365 | 5281d757 | Mark McLoughlin | qemu_opt_set(opts, "ifname", ifname);
|
366 | 5281d757 | Mark McLoughlin | |
367 | 5281d757 | Mark McLoughlin | return fd;
|
368 | 5281d757 | Mark McLoughlin | } |
369 | 5281d757 | Mark McLoughlin | |
370 | 5281d757 | Mark McLoughlin | int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan) |
371 | 5281d757 | Mark McLoughlin | { |
372 | 5281d757 | Mark McLoughlin | TAPState *s; |
373 | 5281d757 | Mark McLoughlin | int fd, vnet_hdr;
|
374 | 5281d757 | Mark McLoughlin | |
375 | 5281d757 | Mark McLoughlin | if (qemu_opt_get(opts, "fd")) { |
376 | 5281d757 | Mark McLoughlin | if (qemu_opt_get(opts, "ifname") || |
377 | 5281d757 | Mark McLoughlin | qemu_opt_get(opts, "script") ||
|
378 | 5281d757 | Mark McLoughlin | qemu_opt_get(opts, "downscript") ||
|
379 | 5281d757 | Mark McLoughlin | qemu_opt_get(opts, "vnet_hdr")) {
|
380 | 5281d757 | Mark McLoughlin | qemu_error("ifname=, script=, downscript= and vnet_hdr= is invalid with fd=\n");
|
381 | 5281d757 | Mark McLoughlin | return -1; |
382 | 5281d757 | Mark McLoughlin | } |
383 | 5281d757 | Mark McLoughlin | |
384 | 5281d757 | Mark McLoughlin | fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
|
385 | 5281d757 | Mark McLoughlin | if (fd == -1) { |
386 | 5281d757 | Mark McLoughlin | return -1; |
387 | 5281d757 | Mark McLoughlin | } |
388 | 5281d757 | Mark McLoughlin | |
389 | 5281d757 | Mark McLoughlin | fcntl(fd, F_SETFL, O_NONBLOCK); |
390 | 5281d757 | Mark McLoughlin | |
391 | 5281d757 | Mark McLoughlin | vnet_hdr = tap_probe_vnet_hdr(fd); |
392 | 5281d757 | Mark McLoughlin | } else {
|
393 | 5281d757 | Mark McLoughlin | if (!qemu_opt_get(opts, "script")) { |
394 | 5281d757 | Mark McLoughlin | qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT);
|
395 | 5281d757 | Mark McLoughlin | } |
396 | 5281d757 | Mark McLoughlin | |
397 | 5281d757 | Mark McLoughlin | if (!qemu_opt_get(opts, "downscript")) { |
398 | 5281d757 | Mark McLoughlin | qemu_opt_set(opts, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT);
|
399 | 5281d757 | Mark McLoughlin | } |
400 | 5281d757 | Mark McLoughlin | |
401 | 5281d757 | Mark McLoughlin | fd = net_tap_init(opts, &vnet_hdr); |
402 | 5281d757 | Mark McLoughlin | } |
403 | 5281d757 | Mark McLoughlin | |
404 | 5281d757 | Mark McLoughlin | s = net_tap_fd_init(vlan, "tap", name, fd, vnet_hdr);
|
405 | 5281d757 | Mark McLoughlin | if (!s) {
|
406 | 5281d757 | Mark McLoughlin | close(fd); |
407 | 5281d757 | Mark McLoughlin | return -1; |
408 | 5281d757 | Mark McLoughlin | } |
409 | 5281d757 | Mark McLoughlin | |
410 | 15ac913b | Mark McLoughlin | if (tap_set_sndbuf(s->fd, opts) < 0) { |
411 | 5281d757 | Mark McLoughlin | return -1; |
412 | 5281d757 | Mark McLoughlin | } |
413 | 5281d757 | Mark McLoughlin | |
414 | 5281d757 | Mark McLoughlin | if (qemu_opt_get(opts, "fd")) { |
415 | 5281d757 | Mark McLoughlin | snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd); |
416 | 5281d757 | Mark McLoughlin | } else {
|
417 | 5281d757 | Mark McLoughlin | const char *ifname, *script, *downscript; |
418 | 5281d757 | Mark McLoughlin | |
419 | 5281d757 | Mark McLoughlin | ifname = qemu_opt_get(opts, "ifname");
|
420 | 5281d757 | Mark McLoughlin | script = qemu_opt_get(opts, "script");
|
421 | 5281d757 | Mark McLoughlin | downscript = qemu_opt_get(opts, "downscript");
|
422 | 5281d757 | Mark McLoughlin | |
423 | 5281d757 | Mark McLoughlin | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
424 | 5281d757 | Mark McLoughlin | "ifname=%s,script=%s,downscript=%s",
|
425 | 5281d757 | Mark McLoughlin | ifname, script, downscript); |
426 | 5281d757 | Mark McLoughlin | |
427 | 5281d757 | Mark McLoughlin | if (strcmp(downscript, "no") != 0) { |
428 | 5281d757 | Mark McLoughlin | snprintf(s->down_script, sizeof(s->down_script), "%s", downscript); |
429 | 5281d757 | Mark McLoughlin | snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname); |
430 | 5281d757 | Mark McLoughlin | } |
431 | 5281d757 | Mark McLoughlin | } |
432 | 5281d757 | Mark McLoughlin | |
433 | 5281d757 | Mark McLoughlin | if (vlan) {
|
434 | 5281d757 | Mark McLoughlin | vlan->nb_host_devs++; |
435 | 5281d757 | Mark McLoughlin | } |
436 | 5281d757 | Mark McLoughlin | |
437 | 5281d757 | Mark McLoughlin | return 0; |
438 | 5281d757 | Mark McLoughlin | } |