File: | net/tap.c |
Location: | line 482, column 14 |
Description: | Value stored to 'parg' is never read |
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 | |
28 | #include "config-host.h" |
29 | |
30 | #include <sys/ioctl.h> |
31 | #include <sys/stat.h> |
32 | #include <sys/wait.h> |
33 | #include <sys/socket.h> |
34 | #include <net/if.h> |
35 | |
36 | #include "net.h" |
37 | #include "monitor.h" |
38 | #include "sysemu.h" |
39 | #include "qemu-char.h" |
40 | #include "qemu-common.h" |
41 | #include "qemu-error.h" |
42 | |
43 | #include "net/tap-linux.h" |
44 | |
45 | #include "hw/vhost_net.h" |
46 | |
47 | /* Maximum GSO packet size (64k) plus plenty of room for |
48 | * the ethernet and virtio_net headers |
49 | */ |
50 | #define TAP_BUFSIZE(4096 + 65536) (4096 + 65536) |
51 | |
52 | typedef struct TAPState { |
53 | VLANClientState nc; |
54 | int fd; |
55 | char down_script[1024]; |
56 | char down_script_arg[128]; |
57 | uint8_t buf[TAP_BUFSIZE(4096 + 65536)]; |
58 | unsigned int read_poll : 1; |
59 | unsigned int write_poll : 1; |
60 | unsigned int using_vnet_hdr : 1; |
61 | unsigned int has_ufo: 1; |
62 | VHostNetState *vhost_net; |
63 | unsigned host_vnet_hdr_len; |
64 | } TAPState; |
65 | |
66 | static int launch_script(const char *setup_script, const char *ifname, int fd); |
67 | |
68 | static int tap_can_send(void *opaque); |
69 | static void tap_send(void *opaque); |
70 | static void tap_writable(void *opaque); |
71 | |
72 | static void tap_update_fd_handler(TAPState *s) |
73 | { |
74 | qemu_set_fd_handler2(s->fd, |
75 | s->read_poll ? tap_can_send : NULL((void*)0), |
76 | s->read_poll ? tap_send : NULL((void*)0), |
77 | s->write_poll ? tap_writable : NULL((void*)0), |
78 | s); |
79 | } |
80 | |
81 | static void tap_read_poll(TAPState *s, int enable) |
82 | { |
83 | s->read_poll = !!enable; |
84 | tap_update_fd_handler(s); |
85 | } |
86 | |
87 | static void tap_write_poll(TAPState *s, int enable) |
88 | { |
89 | s->write_poll = !!enable; |
90 | tap_update_fd_handler(s); |
91 | } |
92 | |
93 | static void tap_writable(void *opaque) |
94 | { |
95 | TAPState *s = opaque; |
96 | |
97 | tap_write_poll(s, 0); |
98 | |
99 | qemu_flush_queued_packets(&s->nc); |
100 | } |
101 | |
102 | static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt) |
103 | { |
104 | ssize_t len; |
105 | |
106 | do { |
107 | len = writev(s->fd, iov, iovcnt); |
108 | } while (len == -1 && errno(*__errno_location ()) == EINTR4); |
109 | |
110 | if (len == -1 && errno(*__errno_location ()) == EAGAIN11) { |
111 | tap_write_poll(s, 1); |
112 | return 0; |
113 | } |
114 | |
115 | return len; |
116 | } |
117 | |
118 | static ssize_t tap_receive_iov(VLANClientState *nc, const struct iovec *iov, |
119 | int iovcnt) |
120 | { |
121 | TAPState *s = DO_UPCAST(TAPState, nc, nc)( __extension__ ( { char __attribute__((unused)) offset_must_be_zero [ -__builtin_offsetof(TAPState, nc)]; ({ const typeof(((TAPState *) 0)->nc) *__mptr = (nc); (TAPState *) ((char *) __mptr - __builtin_offsetof(TAPState, nc));});})); |
122 | const struct iovec *iovp = iov; |
123 | struct iovec iov_copy[iovcnt + 1]; |
124 | struct virtio_net_hdr_mrg_rxbuf hdr = { }; |
125 | |
126 | if (s->host_vnet_hdr_len && !s->using_vnet_hdr) { |
127 | iov_copy[0].iov_base = &hdr; |
128 | iov_copy[0].iov_len = s->host_vnet_hdr_len; |
129 | memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov)); |
130 | iovp = iov_copy; |
131 | iovcnt++; |
132 | } |
133 | |
134 | return tap_write_packet(s, iovp, iovcnt); |
135 | } |
136 | |
137 | static ssize_t tap_receive_raw(VLANClientState *nc, const uint8_t *buf, size_t size) |
138 | { |
139 | TAPState *s = DO_UPCAST(TAPState, nc, nc)( __extension__ ( { char __attribute__((unused)) offset_must_be_zero [ -__builtin_offsetof(TAPState, nc)]; ({ const typeof(((TAPState *) 0)->nc) *__mptr = (nc); (TAPState *) ((char *) __mptr - __builtin_offsetof(TAPState, nc));});})); |
140 | struct iovec iov[2]; |
141 | int iovcnt = 0; |
142 | struct virtio_net_hdr_mrg_rxbuf hdr = { }; |
143 | |
144 | if (s->host_vnet_hdr_len) { |
145 | iov[iovcnt].iov_base = &hdr; |
146 | iov[iovcnt].iov_len = s->host_vnet_hdr_len; |
147 | iovcnt++; |
148 | } |
149 | |
150 | iov[iovcnt].iov_base = (char *)buf; |
151 | iov[iovcnt].iov_len = size; |
152 | iovcnt++; |
153 | |
154 | return tap_write_packet(s, iov, iovcnt); |
155 | } |
156 | |
157 | static ssize_t tap_receive(VLANClientState *nc, const uint8_t *buf, size_t size) |
158 | { |
159 | TAPState *s = DO_UPCAST(TAPState, nc, nc)( __extension__ ( { char __attribute__((unused)) offset_must_be_zero [ -__builtin_offsetof(TAPState, nc)]; ({ const typeof(((TAPState *) 0)->nc) *__mptr = (nc); (TAPState *) ((char *) __mptr - __builtin_offsetof(TAPState, nc));});})); |
160 | struct iovec iov[1]; |
161 | |
162 | if (s->host_vnet_hdr_len && !s->using_vnet_hdr) { |
163 | return tap_receive_raw(nc, buf, size); |
164 | } |
165 | |
166 | iov[0].iov_base = (char *)buf; |
167 | iov[0].iov_len = size; |
168 | |
169 | return tap_write_packet(s, iov, 1); |
170 | } |
171 | |
172 | static int tap_can_send(void *opaque) |
173 | { |
174 | TAPState *s = opaque; |
175 | |
176 | return qemu_can_send_packet(&s->nc); |
177 | } |
178 | |
179 | #ifndef __sun__ |
180 | ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen) |
181 | { |
182 | return read(tapfd, buf, maxlen); |
183 | } |
184 | #endif |
185 | |
186 | static void tap_send_completed(VLANClientState *nc, ssize_t len) |
187 | { |
188 | TAPState *s = DO_UPCAST(TAPState, nc, nc)( __extension__ ( { char __attribute__((unused)) offset_must_be_zero [ -__builtin_offsetof(TAPState, nc)]; ({ const typeof(((TAPState *) 0)->nc) *__mptr = (nc); (TAPState *) ((char *) __mptr - __builtin_offsetof(TAPState, nc));});})); |
189 | tap_read_poll(s, 1); |
190 | } |
191 | |
192 | static void tap_send(void *opaque) |
193 | { |
194 | TAPState *s = opaque; |
195 | int size; |
196 | |
197 | do { |
198 | uint8_t *buf = s->buf; |
199 | |
200 | size = tap_read_packet(s->fd, s->buf, sizeof(s->buf)); |
201 | if (size <= 0) { |
202 | break; |
203 | } |
204 | |
205 | if (s->host_vnet_hdr_len && !s->using_vnet_hdr) { |
206 | buf += s->host_vnet_hdr_len; |
207 | size -= s->host_vnet_hdr_len; |
208 | } |
209 | |
210 | size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed); |
211 | if (size == 0) { |
212 | tap_read_poll(s, 0); |
213 | } |
214 | } while (size > 0 && qemu_can_send_packet(&s->nc)); |
215 | } |
216 | |
217 | int tap_has_ufo(VLANClientState *nc) |
218 | { |
219 | TAPState *s = DO_UPCAST(TAPState, nc, nc)( __extension__ ( { char __attribute__((unused)) offset_must_be_zero [ -__builtin_offsetof(TAPState, nc)]; ({ const typeof(((TAPState *) 0)->nc) *__mptr = (nc); (TAPState *) ((char *) __mptr - __builtin_offsetof(TAPState, nc));});})); |
220 | |
221 | assert(nc->info->type == NET_CLIENT_TYPE_TAP)((nc->info->type == NET_CLIENT_TYPE_TAP) ? (void) (0) : __assert_fail ("nc->info->type == NET_CLIENT_TYPE_TAP" , "/home/stefan/src/qemu/qemu.org/qemu/net/tap.c", 221, __PRETTY_FUNCTION__ )); |
222 | |
223 | return s->has_ufo; |
224 | } |
225 | |
226 | int tap_has_vnet_hdr(VLANClientState *nc) |
227 | { |
228 | TAPState *s = DO_UPCAST(TAPState, nc, nc)( __extension__ ( { char __attribute__((unused)) offset_must_be_zero [ -__builtin_offsetof(TAPState, nc)]; ({ const typeof(((TAPState *) 0)->nc) *__mptr = (nc); (TAPState *) ((char *) __mptr - __builtin_offsetof(TAPState, nc));});})); |
229 | |
230 | assert(nc->info->type == NET_CLIENT_TYPE_TAP)((nc->info->type == NET_CLIENT_TYPE_TAP) ? (void) (0) : __assert_fail ("nc->info->type == NET_CLIENT_TYPE_TAP" , "/home/stefan/src/qemu/qemu.org/qemu/net/tap.c", 230, __PRETTY_FUNCTION__ )); |
231 | |
232 | return !!s->host_vnet_hdr_len; |
233 | } |
234 | |
235 | int tap_has_vnet_hdr_len(VLANClientState *nc, int len) |
236 | { |
237 | TAPState *s = DO_UPCAST(TAPState, nc, nc)( __extension__ ( { char __attribute__((unused)) offset_must_be_zero [ -__builtin_offsetof(TAPState, nc)]; ({ const typeof(((TAPState *) 0)->nc) *__mptr = (nc); (TAPState *) ((char *) __mptr - __builtin_offsetof(TAPState, nc));});})); |
238 | |
239 | assert(nc->info->type == NET_CLIENT_TYPE_TAP)((nc->info->type == NET_CLIENT_TYPE_TAP) ? (void) (0) : __assert_fail ("nc->info->type == NET_CLIENT_TYPE_TAP" , "/home/stefan/src/qemu/qemu.org/qemu/net/tap.c", 239, __PRETTY_FUNCTION__ )); |
240 | |
241 | return tap_probe_vnet_hdr_len(s->fd, len); |
242 | } |
243 | |
244 | void tap_set_vnet_hdr_len(VLANClientState *nc, int len) |
245 | { |
246 | TAPState *s = DO_UPCAST(TAPState, nc, nc)( __extension__ ( { char __attribute__((unused)) offset_must_be_zero [ -__builtin_offsetof(TAPState, nc)]; ({ const typeof(((TAPState *) 0)->nc) *__mptr = (nc); (TAPState *) ((char *) __mptr - __builtin_offsetof(TAPState, nc));});})); |
247 | |
248 | assert(nc->info->type == NET_CLIENT_TYPE_TAP)((nc->info->type == NET_CLIENT_TYPE_TAP) ? (void) (0) : __assert_fail ("nc->info->type == NET_CLIENT_TYPE_TAP" , "/home/stefan/src/qemu/qemu.org/qemu/net/tap.c", 248, __PRETTY_FUNCTION__ )); |
249 | assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||((len == sizeof(struct virtio_net_hdr_mrg_rxbuf) || len == sizeof (struct virtio_net_hdr)) ? (void) (0) : __assert_fail ("len == sizeof(struct virtio_net_hdr_mrg_rxbuf) || len == sizeof(struct virtio_net_hdr)" , "/home/stefan/src/qemu/qemu.org/qemu/net/tap.c", 250, __PRETTY_FUNCTION__ )) |
250 | len == sizeof(struct virtio_net_hdr))((len == sizeof(struct virtio_net_hdr_mrg_rxbuf) || len == sizeof (struct virtio_net_hdr)) ? (void) (0) : __assert_fail ("len == sizeof(struct virtio_net_hdr_mrg_rxbuf) || len == sizeof(struct virtio_net_hdr)" , "/home/stefan/src/qemu/qemu.org/qemu/net/tap.c", 250, __PRETTY_FUNCTION__ )); |
251 | |
252 | tap_fd_set_vnet_hdr_len(s->fd, len); |
253 | s->host_vnet_hdr_len = len; |
254 | } |
255 | |
256 | void tap_using_vnet_hdr(VLANClientState *nc, int using_vnet_hdr) |
257 | { |
258 | TAPState *s = DO_UPCAST(TAPState, nc, nc)( __extension__ ( { char __attribute__((unused)) offset_must_be_zero [ -__builtin_offsetof(TAPState, nc)]; ({ const typeof(((TAPState *) 0)->nc) *__mptr = (nc); (TAPState *) ((char *) __mptr - __builtin_offsetof(TAPState, nc));});})); |
259 | |
260 | using_vnet_hdr = using_vnet_hdr != 0; |
261 | |
262 | assert(nc->info->type == NET_CLIENT_TYPE_TAP)((nc->info->type == NET_CLIENT_TYPE_TAP) ? (void) (0) : __assert_fail ("nc->info->type == NET_CLIENT_TYPE_TAP" , "/home/stefan/src/qemu/qemu.org/qemu/net/tap.c", 262, __PRETTY_FUNCTION__ )); |
263 | assert(!!s->host_vnet_hdr_len == using_vnet_hdr)((!!s->host_vnet_hdr_len == using_vnet_hdr) ? (void) (0) : __assert_fail ("!!s->host_vnet_hdr_len == using_vnet_hdr" , "/home/stefan/src/qemu/qemu.org/qemu/net/tap.c", 263, __PRETTY_FUNCTION__ )); |
264 | |
265 | s->using_vnet_hdr = using_vnet_hdr; |
266 | } |
267 | |
268 | void tap_set_offload(VLANClientState *nc, int csum, int tso4, |
269 | int tso6, int ecn, int ufo) |
270 | { |
271 | TAPState *s = DO_UPCAST(TAPState, nc, nc)( __extension__ ( { char __attribute__((unused)) offset_must_be_zero [ -__builtin_offsetof(TAPState, nc)]; ({ const typeof(((TAPState *) 0)->nc) *__mptr = (nc); (TAPState *) ((char *) __mptr - __builtin_offsetof(TAPState, nc));});})); |
272 | if (s->fd < 0) { |
273 | return; |
274 | } |
275 | |
276 | tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo); |
277 | } |
278 | |
279 | static void tap_cleanup(VLANClientState *nc) |
280 | { |
281 | TAPState *s = DO_UPCAST(TAPState, nc, nc)( __extension__ ( { char __attribute__((unused)) offset_must_be_zero [ -__builtin_offsetof(TAPState, nc)]; ({ const typeof(((TAPState *) 0)->nc) *__mptr = (nc); (TAPState *) ((char *) __mptr - __builtin_offsetof(TAPState, nc));});})); |
282 | |
283 | if (s->vhost_net) { |
284 | vhost_net_cleanup(s->vhost_net); |
285 | s->vhost_net = NULL((void*)0); |
286 | } |
287 | |
288 | qemu_purge_queued_packets(nc); |
289 | |
290 | if (s->down_script[0]) |
291 | launch_script(s->down_script, s->down_script_arg, s->fd); |
292 | |
293 | tap_read_poll(s, 0); |
294 | tap_write_poll(s, 0); |
295 | close(s->fd); |
296 | s->fd = -1; |
297 | } |
298 | |
299 | static void tap_poll(VLANClientState *nc, bool_Bool enable) |
300 | { |
301 | TAPState *s = DO_UPCAST(TAPState, nc, nc)( __extension__ ( { char __attribute__((unused)) offset_must_be_zero [ -__builtin_offsetof(TAPState, nc)]; ({ const typeof(((TAPState *) 0)->nc) *__mptr = (nc); (TAPState *) ((char *) __mptr - __builtin_offsetof(TAPState, nc));});})); |
302 | tap_read_poll(s, enable); |
303 | tap_write_poll(s, enable); |
304 | } |
305 | |
306 | int tap_get_fd(VLANClientState *nc) |
307 | { |
308 | TAPState *s = DO_UPCAST(TAPState, nc, nc)( __extension__ ( { char __attribute__((unused)) offset_must_be_zero [ -__builtin_offsetof(TAPState, nc)]; ({ const typeof(((TAPState *) 0)->nc) *__mptr = (nc); (TAPState *) ((char *) __mptr - __builtin_offsetof(TAPState, nc));});})); |
309 | assert(nc->info->type == NET_CLIENT_TYPE_TAP)((nc->info->type == NET_CLIENT_TYPE_TAP) ? (void) (0) : __assert_fail ("nc->info->type == NET_CLIENT_TYPE_TAP" , "/home/stefan/src/qemu/qemu.org/qemu/net/tap.c", 309, __PRETTY_FUNCTION__ )); |
310 | return s->fd; |
311 | } |
312 | |
313 | /* fd support */ |
314 | |
315 | static NetClientInfo net_tap_info = { |
316 | .type = NET_CLIENT_TYPE_TAP, |
317 | .size = sizeof(TAPState), |
318 | .receive = tap_receive, |
319 | .receive_raw = tap_receive_raw, |
320 | .receive_iov = tap_receive_iov, |
321 | .poll = tap_poll, |
322 | .cleanup = tap_cleanup, |
323 | }; |
324 | |
325 | static TAPState *net_tap_fd_init(VLANState *vlan, |
326 | const char *model, |
327 | const char *name, |
328 | int fd, |
329 | int vnet_hdr) |
330 | { |
331 | VLANClientState *nc; |
332 | TAPState *s; |
333 | |
334 | nc = qemu_new_net_client(&net_tap_info, vlan, NULL((void*)0), model, name); |
335 | |
336 | s = DO_UPCAST(TAPState, nc, nc)( __extension__ ( { char __attribute__((unused)) offset_must_be_zero [ -__builtin_offsetof(TAPState, nc)]; ({ const typeof(((TAPState *) 0)->nc) *__mptr = (nc); (TAPState *) ((char *) __mptr - __builtin_offsetof(TAPState, nc));});})); |
337 | |
338 | s->fd = fd; |
339 | s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0; |
340 | s->using_vnet_hdr = 0; |
341 | s->has_ufo = tap_probe_has_ufo(s->fd); |
342 | tap_set_offload(&s->nc, 0, 0, 0, 0, 0); |
343 | tap_read_poll(s, 1); |
344 | s->vhost_net = NULL((void*)0); |
345 | return s; |
346 | } |
347 | |
348 | static int launch_script(const char *setup_script, const char *ifname, int fd) |
349 | { |
350 | int pid, status; |
351 | char *args[3]; |
352 | char **parg; |
353 | |
354 | /* try to launch network script */ |
355 | pid = fork(); |
356 | if (pid == 0) { |
357 | int open_max = sysconf(_SC_OPEN_MAX_SC_OPEN_MAX), i; |
358 | |
359 | for (i = 0; i < open_max; i++) { |
360 | if (i != STDIN_FILENO0 && |
361 | i != STDOUT_FILENO1 && |
362 | i != STDERR_FILENO2 && |
363 | i != fd) { |
364 | close(i); |
365 | } |
366 | } |
367 | parg = args; |
368 | *parg++ = (char *)setup_script; |
369 | *parg++ = (char *)ifname; |
370 | *parg = NULL((void*)0); |
371 | execv(setup_script, args); |
372 | _exit(1); |
373 | } else if (pid > 0) { |
374 | while (waitpid(pid, &status, 0) != pid) { |
375 | /* loop */ |
376 | } |
377 | |
378 | if (WIFEXITED(status)((((__extension__ (((union { __typeof(status) __in; int __i; } ) { .__in = (status) }).__i))) & 0x7f) == 0) && WEXITSTATUS(status)((((__extension__ (((union { __typeof(status) __in; int __i; } ) { .__in = (status) }).__i))) & 0xff00) >> 8) == 0) { |
379 | return 0; |
380 | } |
381 | } |
382 | fprintf(stderrstderr, "%s: could not launch network script\n", setup_script); |
383 | return -1; |
384 | } |
385 | |
386 | static int recv_fd(int c) |
387 | { |
388 | int fd; |
389 | uint8_t msgbuf[CMSG_SPACE(sizeof(fd))((((sizeof(fd)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (size_t ) - 1) & (size_t) ~(sizeof (size_t) - 1)))]; |
390 | struct msghdr msg = { |
391 | .msg_control = msgbuf, |
392 | .msg_controllen = sizeof(msgbuf), |
393 | }; |
394 | struct cmsghdr *cmsg; |
395 | struct iovec iov; |
396 | uint8_t req[1]; |
397 | ssize_t len; |
398 | |
399 | cmsg = CMSG_FIRSTHDR(&msg)((size_t) (&msg)->msg_controllen >= sizeof (struct cmsghdr ) ? (struct cmsghdr *) (&msg)->msg_control : (struct cmsghdr *) 0); |
400 | cmsg->cmsg_level = SOL_SOCKET1; |
401 | cmsg->cmsg_type = SCM_RIGHTSSCM_RIGHTS; |
402 | cmsg->cmsg_len = CMSG_LEN(sizeof(fd))((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t ) ~(sizeof (size_t) - 1)) + (sizeof(fd))); |
403 | msg.msg_controllen = cmsg->cmsg_len; |
404 | |
405 | iov.iov_base = req; |
406 | iov.iov_len = sizeof(req); |
407 | |
408 | msg.msg_iov = &iov; |
409 | msg.msg_iovlen = 1; |
410 | |
411 | len = recvmsg(c, &msg, 0); |
412 | if (len > 0) { |
413 | memcpy(&fd, CMSG_DATA(cmsg)((cmsg)->__cmsg_data), sizeof(fd)); |
414 | return fd; |
415 | } |
416 | |
417 | return len; |
418 | } |
419 | |
420 | static int net_bridge_run_helper(const char *helper, const char *bridge) |
421 | { |
422 | sigset_t oldmask, mask; |
423 | int pid, status; |
424 | char *args[5]; |
425 | char **parg; |
426 | int sv[2]; |
427 | |
428 | sigemptyset(&mask); |
429 | sigaddset(&mask, SIGCHLD17); |
430 | sigprocmask(SIG_BLOCK0, &mask, &oldmask); |
431 | |
432 | if (socketpair(PF_UNIX1, SOCK_STREAMSOCK_STREAM, 0, sv) == -1) { |
433 | return -1; |
434 | } |
435 | |
436 | /* try to launch bridge helper */ |
437 | pid = fork(); |
438 | if (pid == 0) { |
439 | int open_max = sysconf(_SC_OPEN_MAX_SC_OPEN_MAX), i; |
440 | char fd_buf[6+10]; |
441 | char br_buf[6+IFNAMSIZ16] = {0}; |
442 | char helper_cmd[PATH_MAX4096 + sizeof(fd_buf) + sizeof(br_buf) + 15]; |
443 | |
444 | for (i = 0; i < open_max; i++) { |
445 | if (i != STDIN_FILENO0 && |
446 | i != STDOUT_FILENO1 && |
447 | i != STDERR_FILENO2 && |
448 | i != sv[1]) { |
449 | close(i); |
450 | } |
451 | } |
452 | |
453 | snprintf(fd_buf, sizeof(fd_buf), "%s%d", "--fd=", sv[1]); |
454 | |
455 | if (strrchr(helper, ' ') || strrchr(helper, '\t')) { |
456 | /* assume helper is a command */ |
457 | |
458 | if (strstr(helper, "--br=") == NULL((void*)0)) { |
459 | snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge); |
460 | } |
461 | |
462 | snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s", |
463 | helper, "--use-vnet", fd_buf, br_buf); |
464 | |
465 | parg = args; |
466 | *parg++ = (char *)"sh"; |
467 | *parg++ = (char *)"-c"; |
468 | *parg++ = helper_cmd; |
469 | *parg++ = NULL((void*)0); |
470 | |
471 | execv("/bin/sh", args); |
472 | } else { |
473 | /* assume helper is just the executable path name */ |
474 | |
475 | snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge); |
476 | |
477 | parg = args; |
478 | *parg++ = (char *)helper; |
479 | *parg++ = (char *)"--use-vnet"; |
480 | *parg++ = fd_buf; |
481 | *parg++ = br_buf; |
482 | *parg++ = NULL((void*)0); |
Value stored to 'parg' is never read | |
483 | |
484 | execv(helper, args); |
485 | } |
486 | _exit(1); |
487 | |
488 | } else if (pid > 0) { |
489 | int fd; |
490 | |
491 | close(sv[1]); |
492 | |
493 | do { |
494 | fd = recv_fd(sv[0]); |
495 | } while (fd == -1 && errno(*__errno_location ()) == EINTR4); |
496 | |
497 | close(sv[0]); |
498 | |
499 | while (waitpid(pid, &status, 0) != pid) { |
500 | /* loop */ |
501 | } |
502 | sigprocmask(SIG_SETMASK2, &oldmask, NULL((void*)0)); |
503 | if (fd < 0) { |
504 | fprintf(stderrstderr, "failed to recv file descriptor\n"); |
505 | return -1; |
506 | } |
507 | |
508 | if (WIFEXITED(status)((((__extension__ (((union { __typeof(status) __in; int __i; } ) { .__in = (status) }).__i))) & 0x7f) == 0) && WEXITSTATUS(status)((((__extension__ (((union { __typeof(status) __in; int __i; } ) { .__in = (status) }).__i))) & 0xff00) >> 8) == 0) { |
509 | return fd; |
510 | } |
511 | } |
512 | fprintf(stderrstderr, "failed to launch bridge helper\n"); |
513 | return -1; |
514 | } |
515 | |
516 | int net_init_bridge(QemuOpts *opts, const char *name, VLANState *vlan) |
517 | { |
518 | TAPState *s; |
519 | int fd, vnet_hdr; |
520 | |
521 | if (!qemu_opt_get(opts, "br")) { |
522 | qemu_opt_set(opts, "br", DEFAULT_BRIDGE_INTERFACE"br0"); |
523 | } |
524 | if (!qemu_opt_get(opts, "helper")) { |
525 | qemu_opt_set(opts, "helper", DEFAULT_BRIDGE_HELPER"/usr/local/libexec" "/qemu-bridge-helper"); |
526 | } |
527 | |
528 | fd = net_bridge_run_helper(qemu_opt_get(opts, "helper"), |
529 | qemu_opt_get(opts, "br")); |
530 | if (fd == -1) { |
531 | return -1; |
532 | } |
533 | |
534 | fcntl(fd, F_SETFL4, O_NONBLOCK04000); |
535 | |
536 | vnet_hdr = tap_probe_vnet_hdr(fd); |
537 | |
538 | s = net_tap_fd_init(vlan, "bridge", name, fd, vnet_hdr); |
539 | if (!s) { |
540 | close(fd); |
541 | return -1; |
542 | } |
543 | |
544 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", |
545 | qemu_opt_get(opts, "helper"), qemu_opt_get(opts, "br")); |
546 | |
547 | return 0; |
548 | } |
549 | |
550 | static int net_tap_init(QemuOpts *opts, int *vnet_hdr) |
551 | { |
552 | int fd, vnet_hdr_required; |
553 | char ifname[128] = {0,}; |
554 | const char *setup_script; |
555 | |
556 | if (qemu_opt_get(opts, "ifname")) { |
557 | pstrcpy(ifname, sizeof(ifname), qemu_opt_get(opts, "ifname")); |
558 | } |
559 | |
560 | *vnet_hdr = qemu_opt_get_bool(opts, "vnet_hdr", 1); |
561 | if (qemu_opt_get(opts, "vnet_hdr")) { |
562 | vnet_hdr_required = *vnet_hdr; |
563 | } else { |
564 | vnet_hdr_required = 0; |
565 | } |
566 | |
567 | TFR(fd = tap_open(ifname, sizeof(ifname), vnet_hdr, vnet_hdr_required))do { if ((fd = tap_open(ifname, sizeof(ifname), vnet_hdr, vnet_hdr_required )) != -1) break; } while ((*__errno_location ()) == 4); |
568 | if (fd < 0) { |
569 | return -1; |
570 | } |
571 | |
572 | setup_script = qemu_opt_get(opts, "script"); |
573 | if (setup_script && |
574 | setup_script[0] != '\0' && |
575 | strcmp(setup_script, "no") != 0 && |
576 | launch_script(setup_script, ifname, fd)) { |
577 | close(fd); |
578 | return -1; |
579 | } |
580 | |
581 | qemu_opt_set(opts, "ifname", ifname); |
582 | |
583 | return fd; |
584 | } |
585 | |
586 | int net_init_tap(QemuOpts *opts, const char *name, VLANState *vlan) |
587 | { |
588 | TAPState *s; |
589 | int fd, vnet_hdr = 0; |
590 | const char *model; |
591 | |
592 | if (qemu_opt_get(opts, "fd")) { |
593 | if (qemu_opt_get(opts, "ifname") || |
594 | qemu_opt_get(opts, "script") || |
595 | qemu_opt_get(opts, "downscript") || |
596 | qemu_opt_get(opts, "vnet_hdr") || |
597 | qemu_opt_get(opts, "helper")) { |
598 | error_report("ifname=, script=, downscript=, vnet_hdr=, " |
599 | "and helper= are invalid with fd="); |
600 | return -1; |
601 | } |
602 | |
603 | fd = net_handle_fd_param(cur_mon, qemu_opt_get(opts, "fd")); |
604 | if (fd == -1) { |
605 | return -1; |
606 | } |
607 | |
608 | fcntl(fd, F_SETFL4, O_NONBLOCK04000); |
609 | |
610 | vnet_hdr = tap_probe_vnet_hdr(fd); |
611 | |
612 | model = "tap"; |
613 | |
614 | } else if (qemu_opt_get(opts, "helper")) { |
615 | if (qemu_opt_get(opts, "ifname") || |
616 | qemu_opt_get(opts, "script") || |
617 | qemu_opt_get(opts, "downscript") || |
618 | qemu_opt_get(opts, "vnet_hdr")) { |
619 | error_report("ifname=, script=, downscript=, and vnet_hdr= " |
620 | "are invalid with helper="); |
621 | return -1; |
622 | } |
623 | |
624 | fd = net_bridge_run_helper(qemu_opt_get(opts, "helper"), |
625 | DEFAULT_BRIDGE_INTERFACE"br0"); |
626 | if (fd == -1) { |
627 | return -1; |
628 | } |
629 | |
630 | fcntl(fd, F_SETFL4, O_NONBLOCK04000); |
631 | |
632 | vnet_hdr = tap_probe_vnet_hdr(fd); |
633 | |
634 | model = "bridge"; |
635 | |
636 | } else { |
637 | if (!qemu_opt_get(opts, "script")) { |
638 | qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT"/etc/qemu-ifup"); |
639 | } |
640 | |
641 | if (!qemu_opt_get(opts, "downscript")) { |
642 | qemu_opt_set(opts, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT"/etc/qemu-ifdown"); |
643 | } |
644 | |
645 | fd = net_tap_init(opts, &vnet_hdr); |
646 | if (fd == -1) { |
647 | return -1; |
648 | } |
649 | |
650 | model = "tap"; |
651 | } |
652 | |
653 | s = net_tap_fd_init(vlan, model, name, fd, vnet_hdr); |
654 | if (!s) { |
655 | close(fd); |
656 | return -1; |
657 | } |
658 | |
659 | if (tap_set_sndbuf(s->fd, opts) < 0) { |
660 | return -1; |
661 | } |
662 | |
663 | if (qemu_opt_get(opts, "fd")) { |
664 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd); |
665 | } else if (qemu_opt_get(opts, "helper")) { |
666 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), |
667 | "helper=%s", qemu_opt_get(opts, "helper")); |
668 | } else { |
669 | const char *ifname, *script, *downscript; |
670 | |
671 | ifname = qemu_opt_get(opts, "ifname"); |
672 | script = qemu_opt_get(opts, "script"); |
673 | downscript = qemu_opt_get(opts, "downscript"); |
674 | |
675 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), |
676 | "ifname=%s,script=%s,downscript=%s", |
677 | ifname, script, downscript); |
678 | |
679 | if (strcmp(downscript, "no") != 0) { |
680 | snprintf(s->down_script, sizeof(s->down_script), "%s", downscript); |
681 | snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname); |
682 | } |
683 | } |
684 | |
685 | if (qemu_opt_get_bool(opts, "vhost", !!qemu_opt_get(opts, "vhostfd") || |
686 | qemu_opt_get_bool(opts, "vhostforce", false0))) { |
687 | int vhostfd, r; |
688 | bool_Bool force = qemu_opt_get_bool(opts, "vhostforce", false0); |
689 | if (qemu_opt_get(opts, "vhostfd")) { |
690 | r = net_handle_fd_param(cur_mon, qemu_opt_get(opts, "vhostfd")); |
691 | if (r == -1) { |
692 | return -1; |
693 | } |
694 | vhostfd = r; |
695 | } else { |
696 | vhostfd = -1; |
697 | } |
698 | s->vhost_net = vhost_net_init(&s->nc, vhostfd, force); |
699 | if (!s->vhost_net) { |
700 | error_report("vhost-net requested but could not be initialized"); |
701 | return -1; |
702 | } |
703 | } else if (qemu_opt_get(opts, "vhostfd")) { |
704 | error_report("vhostfd= is not valid without vhost"); |
705 | return -1; |
706 | } |
707 | |
708 | return 0; |
709 | } |
710 | |
711 | VHostNetState *tap_get_vhost_net(VLANClientState *nc) |
712 | { |
713 | TAPState *s = DO_UPCAST(TAPState, nc, nc)( __extension__ ( { char __attribute__((unused)) offset_must_be_zero [ -__builtin_offsetof(TAPState, nc)]; ({ const typeof(((TAPState *) 0)->nc) *__mptr = (nc); (TAPState *) ((char *) __mptr - __builtin_offsetof(TAPState, nc));});})); |
714 | assert(nc->info->type == NET_CLIENT_TYPE_TAP)((nc->info->type == NET_CLIENT_TYPE_TAP) ? (void) (0) : __assert_fail ("nc->info->type == NET_CLIENT_TYPE_TAP" , "/home/stefan/src/qemu/qemu.org/qemu/net/tap.c", 714, __PRETTY_FUNCTION__ )); |
715 | return s->vhost_net; |
716 | } |