4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2009 Red Hat, Inc.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
28 #include "config-host.h"
31 #include <sys/ioctl.h>
34 #include <sys/socket.h>
39 #include "qemu-char.h"
40 #include "qemu-common.h"
42 #include "net/tap-linux.h"
44 /* Maximum GSO packet size (64k) plus plenty of room for
45 * the ethernet and virtio_net headers
47 #define TAP_BUFSIZE (4096 + 65536)
49 typedef struct TAPState
{
52 char down_script
[1024];
53 char down_script_arg
[128];
54 uint8_t buf
[TAP_BUFSIZE
];
55 unsigned int read_poll
: 1;
56 unsigned int write_poll
: 1;
57 unsigned int has_vnet_hdr
: 1;
58 unsigned int using_vnet_hdr
: 1;
59 unsigned int has_ufo
: 1;
62 static int launch_script(const char *setup_script
, const char *ifname
, int fd
);
64 static int tap_can_send(void *opaque
);
65 static void tap_send(void *opaque
);
66 static void tap_writable(void *opaque
);
68 static void tap_update_fd_handler(TAPState
*s
)
70 qemu_set_fd_handler2(s
->fd
,
71 s
->read_poll
? tap_can_send
: NULL
,
72 s
->read_poll
? tap_send
: NULL
,
73 s
->write_poll
? tap_writable
: NULL
,
77 static void tap_read_poll(TAPState
*s
, int enable
)
79 s
->read_poll
= !!enable
;
80 tap_update_fd_handler(s
);
83 static void tap_write_poll(TAPState
*s
, int enable
)
85 s
->write_poll
= !!enable
;
86 tap_update_fd_handler(s
);
89 static void tap_writable(void *opaque
)
95 qemu_flush_queued_packets(&s
->nc
);
98 static ssize_t
tap_write_packet(TAPState
*s
, const struct iovec
*iov
, int iovcnt
)
103 len
= writev(s
->fd
, iov
, iovcnt
);
104 } while (len
== -1 && errno
== EINTR
);
106 if (len
== -1 && errno
== EAGAIN
) {
107 tap_write_poll(s
, 1);
114 static ssize_t
tap_receive_iov(VLANClientState
*nc
, const struct iovec
*iov
,
117 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
118 const struct iovec
*iovp
= iov
;
119 struct iovec iov_copy
[iovcnt
+ 1];
120 struct virtio_net_hdr hdr
= { 0, };
122 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
123 iov_copy
[0].iov_base
= &hdr
;
124 iov_copy
[0].iov_len
= sizeof(hdr
);
125 memcpy(&iov_copy
[1], iov
, iovcnt
* sizeof(*iov
));
130 return tap_write_packet(s
, iovp
, iovcnt
);
133 static ssize_t
tap_receive_raw(VLANClientState
*nc
, const uint8_t *buf
, size_t size
)
135 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
138 struct virtio_net_hdr hdr
= { 0, };
140 if (s
->has_vnet_hdr
) {
141 iov
[iovcnt
].iov_base
= &hdr
;
142 iov
[iovcnt
].iov_len
= sizeof(hdr
);
146 iov
[iovcnt
].iov_base
= (char *)buf
;
147 iov
[iovcnt
].iov_len
= size
;
150 return tap_write_packet(s
, iov
, iovcnt
);
153 static ssize_t
tap_receive(VLANClientState
*nc
, const uint8_t *buf
, size_t size
)
155 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
158 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
159 return tap_receive_raw(nc
, buf
, size
);
162 iov
[0].iov_base
= (char *)buf
;
163 iov
[0].iov_len
= size
;
165 return tap_write_packet(s
, iov
, 1);
168 static int tap_can_send(void *opaque
)
170 TAPState
*s
= opaque
;
172 return qemu_can_send_packet(&s
->nc
);
176 ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
178 return read(tapfd
, buf
, maxlen
);
182 static void tap_send_completed(VLANClientState
*nc
, ssize_t len
)
184 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
188 static void tap_send(void *opaque
)
190 TAPState
*s
= opaque
;
194 uint8_t *buf
= s
->buf
;
196 size
= tap_read_packet(s
->fd
, s
->buf
, sizeof(s
->buf
));
201 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
202 buf
+= sizeof(struct virtio_net_hdr
);
203 size
-= sizeof(struct virtio_net_hdr
);
206 size
= qemu_send_packet_async(&s
->nc
, buf
, size
, tap_send_completed
);
210 } while (size
> 0 && qemu_can_send_packet(&s
->nc
));
213 int tap_has_ufo(VLANClientState
*nc
)
215 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
217 assert(nc
->info
->type
== NET_CLIENT_TYPE_TAP
);
222 int tap_has_vnet_hdr(VLANClientState
*nc
)
224 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
226 assert(nc
->info
->type
== NET_CLIENT_TYPE_TAP
);
228 return s
->has_vnet_hdr
;
231 void tap_using_vnet_hdr(VLANClientState
*nc
, int using_vnet_hdr
)
233 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
235 using_vnet_hdr
= using_vnet_hdr
!= 0;
237 assert(nc
->info
->type
== NET_CLIENT_TYPE_TAP
);
238 assert(s
->has_vnet_hdr
== using_vnet_hdr
);
240 s
->using_vnet_hdr
= using_vnet_hdr
;
243 void tap_set_offload(VLANClientState
*nc
, int csum
, int tso4
,
244 int tso6
, int ecn
, int ufo
)
246 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
248 return tap_fd_set_offload(s
->fd
, csum
, tso4
, tso6
, ecn
, ufo
);
251 static void tap_cleanup(VLANClientState
*nc
)
253 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
255 qemu_purge_queued_packets(nc
);
257 if (s
->down_script
[0])
258 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
);
261 tap_write_poll(s
, 0);
265 static void tap_poll(VLANClientState
*nc
, bool enable
)
267 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
268 tap_read_poll(s
, enable
);
269 tap_write_poll(s
, enable
);
274 static NetClientInfo net_tap_info
= {
275 .type
= NET_CLIENT_TYPE_TAP
,
276 .size
= sizeof(TAPState
),
277 .receive
= tap_receive
,
278 .receive_raw
= tap_receive_raw
,
279 .receive_iov
= tap_receive_iov
,
281 .cleanup
= tap_cleanup
,
284 static TAPState
*net_tap_fd_init(VLANState
*vlan
,
293 nc
= qemu_new_net_client(&net_tap_info
, vlan
, NULL
, model
, name
);
295 s
= DO_UPCAST(TAPState
, nc
, nc
);
298 s
->has_vnet_hdr
= vnet_hdr
!= 0;
299 s
->using_vnet_hdr
= 0;
300 s
->has_ufo
= tap_probe_has_ufo(s
->fd
);
301 tap_set_offload(&s
->nc
, 0, 0, 0, 0, 0);
306 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
308 sigset_t oldmask
, mask
;
314 sigaddset(&mask
, SIGCHLD
);
315 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
317 /* try to launch network script */
320 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
322 for (i
= 0; i
< open_max
; i
++) {
323 if (i
!= STDIN_FILENO
&&
324 i
!= STDOUT_FILENO
&&
325 i
!= STDERR_FILENO
&&
331 *parg
++ = (char *)setup_script
;
332 *parg
++ = (char *)ifname
;
334 execv(setup_script
, args
);
336 } else if (pid
> 0) {
337 while (waitpid(pid
, &status
, 0) != pid
) {
340 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
342 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
346 fprintf(stderr
, "%s: could not launch network script\n", setup_script
);
350 static int net_tap_init(QemuOpts
*opts
, int *vnet_hdr
)
352 int fd
, vnet_hdr_required
;
353 char ifname
[128] = {0,};
354 const char *setup_script
;
356 if (qemu_opt_get(opts
, "ifname")) {
357 pstrcpy(ifname
, sizeof(ifname
), qemu_opt_get(opts
, "ifname"));
360 *vnet_hdr
= qemu_opt_get_bool(opts
, "vnet_hdr", 1);
361 if (qemu_opt_get(opts
, "vnet_hdr")) {
362 vnet_hdr_required
= *vnet_hdr
;
364 vnet_hdr_required
= 0;
367 TFR(fd
= tap_open(ifname
, sizeof(ifname
), vnet_hdr
, vnet_hdr_required
));
372 setup_script
= qemu_opt_get(opts
, "script");
374 setup_script
[0] != '\0' &&
375 strcmp(setup_script
, "no") != 0 &&
376 launch_script(setup_script
, ifname
, fd
)) {
381 qemu_opt_set(opts
, "ifname", ifname
);
386 int net_init_tap(QemuOpts
*opts
, Monitor
*mon
, const char *name
, VLANState
*vlan
)
389 int fd
, vnet_hdr
= 0;
391 if (qemu_opt_get(opts
, "fd")) {
392 if (qemu_opt_get(opts
, "ifname") ||
393 qemu_opt_get(opts
, "script") ||
394 qemu_opt_get(opts
, "downscript") ||
395 qemu_opt_get(opts
, "vnet_hdr")) {
396 qemu_error("ifname=, script=, downscript= and vnet_hdr= is invalid with fd=\n");
400 fd
= net_handle_fd_param(mon
, qemu_opt_get(opts
, "fd"));
405 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
407 vnet_hdr
= tap_probe_vnet_hdr(fd
);
409 if (!qemu_opt_get(opts
, "script")) {
410 qemu_opt_set(opts
, "script", DEFAULT_NETWORK_SCRIPT
);
413 if (!qemu_opt_get(opts
, "downscript")) {
414 qemu_opt_set(opts
, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT
);
417 fd
= net_tap_init(opts
, &vnet_hdr
);
423 s
= net_tap_fd_init(vlan
, "tap", name
, fd
, vnet_hdr
);
429 if (tap_set_sndbuf(s
->fd
, opts
) < 0) {
433 if (qemu_opt_get(opts
, "fd")) {
434 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "fd=%d", fd
);
436 const char *ifname
, *script
, *downscript
;
438 ifname
= qemu_opt_get(opts
, "ifname");
439 script
= qemu_opt_get(opts
, "script");
440 downscript
= qemu_opt_get(opts
, "downscript");
442 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
443 "ifname=%s,script=%s,downscript=%s",
444 ifname
, script
, downscript
);
446 if (strcmp(downscript
, "no") != 0) {
447 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", downscript
);
448 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
), "%s", ifname
);