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"
30 #include <sys/ioctl.h>
33 #include <sys/socket.h>
38 #include "qemu-char.h"
39 #include "qemu-common.h"
40 #include "qemu-error.h"
42 #include "net/tap-linux.h"
44 #include "hw/vhost_net.h"
46 /* Maximum GSO packet size (64k) plus plenty of room for
47 * the ethernet and virtio_net headers
49 #define TAP_BUFSIZE (4096 + 65536)
51 typedef struct TAPState
{
54 char down_script
[1024];
55 char down_script_arg
[128];
56 uint8_t buf
[TAP_BUFSIZE
];
57 unsigned int read_poll
: 1;
58 unsigned int write_poll
: 1;
59 unsigned int using_vnet_hdr
: 1;
60 unsigned int has_ufo
: 1;
61 VHostNetState
*vhost_net
;
62 unsigned host_vnet_hdr_len
;
65 static int launch_script(const char *setup_script
, const char *ifname
, int fd
);
67 static int tap_can_send(void *opaque
);
68 static void tap_send(void *opaque
);
69 static void tap_writable(void *opaque
);
71 static void tap_update_fd_handler(TAPState
*s
)
73 qemu_set_fd_handler2(s
->fd
,
74 s
->read_poll
? tap_can_send
: NULL
,
75 s
->read_poll
? tap_send
: NULL
,
76 s
->write_poll
? tap_writable
: NULL
,
80 static void tap_read_poll(TAPState
*s
, int enable
)
82 s
->read_poll
= !!enable
;
83 tap_update_fd_handler(s
);
86 static void tap_write_poll(TAPState
*s
, int enable
)
88 s
->write_poll
= !!enable
;
89 tap_update_fd_handler(s
);
92 static void tap_writable(void *opaque
)
98 qemu_flush_queued_packets(&s
->nc
);
101 static ssize_t
tap_write_packet(TAPState
*s
, const struct iovec
*iov
, int iovcnt
)
106 len
= writev(s
->fd
, iov
, iovcnt
);
107 } while (len
== -1 && errno
== EINTR
);
109 if (len
== -1 && errno
== EAGAIN
) {
110 tap_write_poll(s
, 1);
117 static ssize_t
tap_receive_iov(VLANClientState
*nc
, const struct iovec
*iov
,
120 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
121 const struct iovec
*iovp
= iov
;
122 struct iovec iov_copy
[iovcnt
+ 1];
123 struct virtio_net_hdr_mrg_rxbuf hdr
= { };
125 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
126 iov_copy
[0].iov_base
= &hdr
;
127 iov_copy
[0].iov_len
= s
->host_vnet_hdr_len
;
128 memcpy(&iov_copy
[1], iov
, iovcnt
* sizeof(*iov
));
133 return tap_write_packet(s
, iovp
, iovcnt
);
136 static ssize_t
tap_receive_raw(VLANClientState
*nc
, const uint8_t *buf
, size_t size
)
138 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
141 struct virtio_net_hdr_mrg_rxbuf hdr
= { };
143 if (s
->host_vnet_hdr_len
) {
144 iov
[iovcnt
].iov_base
= &hdr
;
145 iov
[iovcnt
].iov_len
= s
->host_vnet_hdr_len
;
149 iov
[iovcnt
].iov_base
= (char *)buf
;
150 iov
[iovcnt
].iov_len
= size
;
153 return tap_write_packet(s
, iov
, iovcnt
);
156 static ssize_t
tap_receive(VLANClientState
*nc
, const uint8_t *buf
, size_t size
)
158 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
161 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
162 return tap_receive_raw(nc
, buf
, size
);
165 iov
[0].iov_base
= (char *)buf
;
166 iov
[0].iov_len
= size
;
168 return tap_write_packet(s
, iov
, 1);
171 static int tap_can_send(void *opaque
)
173 TAPState
*s
= opaque
;
175 return qemu_can_send_packet(&s
->nc
);
179 ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
181 return read(tapfd
, buf
, maxlen
);
185 static void tap_send_completed(VLANClientState
*nc
, ssize_t len
)
187 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
191 static void tap_send(void *opaque
)
193 TAPState
*s
= opaque
;
197 uint8_t *buf
= s
->buf
;
199 size
= tap_read_packet(s
->fd
, s
->buf
, sizeof(s
->buf
));
204 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
205 buf
+= s
->host_vnet_hdr_len
;
206 size
-= s
->host_vnet_hdr_len
;
209 size
= qemu_send_packet_async(&s
->nc
, buf
, size
, tap_send_completed
);
213 } while (size
> 0 && qemu_can_send_packet(&s
->nc
));
216 int tap_has_ufo(VLANClientState
*nc
)
218 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
220 assert(nc
->info
->type
== NET_CLIENT_TYPE_TAP
);
225 int tap_has_vnet_hdr(VLANClientState
*nc
)
227 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
229 assert(nc
->info
->type
== NET_CLIENT_TYPE_TAP
);
231 return !!s
->host_vnet_hdr_len
;
234 int tap_has_vnet_hdr_len(VLANClientState
*nc
, int len
)
236 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
238 assert(nc
->info
->type
== NET_CLIENT_TYPE_TAP
);
240 return tap_probe_vnet_hdr_len(s
->fd
, len
);
243 void tap_set_vnet_hdr_len(VLANClientState
*nc
, int len
)
245 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
247 assert(nc
->info
->type
== NET_CLIENT_TYPE_TAP
);
248 assert(len
== sizeof(struct virtio_net_hdr_mrg_rxbuf
) ||
249 len
== sizeof(struct virtio_net_hdr
));
251 tap_fd_set_vnet_hdr_len(s
->fd
, len
);
252 s
->host_vnet_hdr_len
= len
;
255 void tap_using_vnet_hdr(VLANClientState
*nc
, int using_vnet_hdr
)
257 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
259 using_vnet_hdr
= using_vnet_hdr
!= 0;
261 assert(nc
->info
->type
== NET_CLIENT_TYPE_TAP
);
262 assert(!!s
->host_vnet_hdr_len
== using_vnet_hdr
);
264 s
->using_vnet_hdr
= using_vnet_hdr
;
267 void tap_set_offload(VLANClientState
*nc
, int csum
, int tso4
,
268 int tso6
, int ecn
, int ufo
)
270 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
275 tap_fd_set_offload(s
->fd
, csum
, tso4
, tso6
, ecn
, ufo
);
278 static void tap_cleanup(VLANClientState
*nc
)
280 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
283 vhost_net_cleanup(s
->vhost_net
);
287 qemu_purge_queued_packets(nc
);
289 if (s
->down_script
[0])
290 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
);
293 tap_write_poll(s
, 0);
298 static void tap_poll(VLANClientState
*nc
, bool enable
)
300 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
301 tap_read_poll(s
, enable
);
302 tap_write_poll(s
, enable
);
305 int tap_get_fd(VLANClientState
*nc
)
307 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
308 assert(nc
->info
->type
== NET_CLIENT_TYPE_TAP
);
314 static NetClientInfo net_tap_info
= {
315 .type
= NET_CLIENT_TYPE_TAP
,
316 .size
= sizeof(TAPState
),
317 .receive
= tap_receive
,
318 .receive_raw
= tap_receive_raw
,
319 .receive_iov
= tap_receive_iov
,
321 .cleanup
= tap_cleanup
,
324 static TAPState
*net_tap_fd_init(VLANState
*vlan
,
333 nc
= qemu_new_net_client(&net_tap_info
, vlan
, NULL
, model
, name
);
335 s
= DO_UPCAST(TAPState
, nc
, nc
);
338 s
->host_vnet_hdr_len
= vnet_hdr
? sizeof(struct virtio_net_hdr
) : 0;
339 s
->using_vnet_hdr
= 0;
340 s
->has_ufo
= tap_probe_has_ufo(s
->fd
);
341 tap_set_offload(&s
->nc
, 0, 0, 0, 0, 0);
347 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
353 /* try to launch network script */
356 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
358 for (i
= 0; i
< open_max
; i
++) {
359 if (i
!= STDIN_FILENO
&&
360 i
!= STDOUT_FILENO
&&
361 i
!= STDERR_FILENO
&&
367 *parg
++ = (char *)setup_script
;
368 *parg
++ = (char *)ifname
;
370 execv(setup_script
, args
);
372 } else if (pid
> 0) {
373 while (waitpid(pid
, &status
, 0) != pid
) {
377 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
381 fprintf(stderr
, "%s: could not launch network script\n", setup_script
);
385 static int recv_fd(int c
)
388 uint8_t msgbuf
[CMSG_SPACE(sizeof(fd
))];
389 struct msghdr msg
= {
390 .msg_control
= msgbuf
,
391 .msg_controllen
= sizeof(msgbuf
),
393 struct cmsghdr
*cmsg
;
398 cmsg
= CMSG_FIRSTHDR(&msg
);
399 cmsg
->cmsg_level
= SOL_SOCKET
;
400 cmsg
->cmsg_type
= SCM_RIGHTS
;
401 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
402 msg
.msg_controllen
= cmsg
->cmsg_len
;
405 iov
.iov_len
= sizeof(req
);
410 len
= recvmsg(c
, &msg
, 0);
412 memcpy(&fd
, CMSG_DATA(cmsg
), sizeof(fd
));
419 static int net_bridge_run_helper(const char *helper
, const char *bridge
)
421 sigset_t oldmask
, mask
;
428 sigaddset(&mask
, SIGCHLD
);
429 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
431 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
) == -1) {
435 /* try to launch bridge helper */
438 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
440 char br_buf
[6+IFNAMSIZ
] = {0};
441 char helper_cmd
[PATH_MAX
+ sizeof(fd_buf
) + sizeof(br_buf
) + 15];
443 for (i
= 0; i
< open_max
; i
++) {
444 if (i
!= STDIN_FILENO
&&
445 i
!= STDOUT_FILENO
&&
446 i
!= STDERR_FILENO
&&
452 snprintf(fd_buf
, sizeof(fd_buf
), "%s%d", "--fd=", sv
[1]);
454 if (strrchr(helper
, ' ') || strrchr(helper
, '\t')) {
455 /* assume helper is a command */
457 if (strstr(helper
, "--br=") == NULL
) {
458 snprintf(br_buf
, sizeof(br_buf
), "%s%s", "--br=", bridge
);
461 snprintf(helper_cmd
, sizeof(helper_cmd
), "%s %s %s %s",
462 helper
, "--use-vnet", fd_buf
, br_buf
);
465 *parg
++ = (char *)"sh";
466 *parg
++ = (char *)"-c";
467 *parg
++ = helper_cmd
;
470 execv("/bin/sh", args
);
472 /* assume helper is just the executable path name */
474 snprintf(br_buf
, sizeof(br_buf
), "%s%s", "--br=", bridge
);
477 *parg
++ = (char *)helper
;
478 *parg
++ = (char *)"--use-vnet";
487 } else if (pid
> 0) {
494 } while (fd
== -1 && errno
== EINTR
);
498 while (waitpid(pid
, &status
, 0) != pid
) {
501 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
503 fprintf(stderr
, "failed to recv file descriptor\n");
507 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
511 fprintf(stderr
, "failed to launch bridge helper\n");
515 int net_init_bridge(QemuOpts
*opts
, const char *name
, VLANState
*vlan
)
520 if (!qemu_opt_get(opts
, "br")) {
521 qemu_opt_set(opts
, "br", DEFAULT_BRIDGE_INTERFACE
);
523 if (!qemu_opt_get(opts
, "helper")) {
524 qemu_opt_set(opts
, "helper", DEFAULT_BRIDGE_HELPER
);
527 fd
= net_bridge_run_helper(qemu_opt_get(opts
, "helper"),
528 qemu_opt_get(opts
, "br"));
533 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
535 vnet_hdr
= tap_probe_vnet_hdr(fd
);
537 s
= net_tap_fd_init(vlan
, "bridge", name
, fd
, vnet_hdr
);
543 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "helper=%s,br=%s",
544 qemu_opt_get(opts
, "helper"), qemu_opt_get(opts
, "br"));
549 static int net_tap_init(QemuOpts
*opts
, int *vnet_hdr
)
551 int fd
, vnet_hdr_required
;
552 char ifname
[128] = {0,};
553 const char *setup_script
;
555 if (qemu_opt_get(opts
, "ifname")) {
556 pstrcpy(ifname
, sizeof(ifname
), qemu_opt_get(opts
, "ifname"));
559 *vnet_hdr
= qemu_opt_get_bool(opts
, "vnet_hdr", 1);
560 if (qemu_opt_get(opts
, "vnet_hdr")) {
561 vnet_hdr_required
= *vnet_hdr
;
563 vnet_hdr_required
= 0;
566 TFR(fd
= tap_open(ifname
, sizeof(ifname
), vnet_hdr
, vnet_hdr_required
));
571 setup_script
= qemu_opt_get(opts
, "script");
573 setup_script
[0] != '\0' &&
574 strcmp(setup_script
, "no") != 0 &&
575 launch_script(setup_script
, ifname
, fd
)) {
580 qemu_opt_set(opts
, "ifname", ifname
);
585 int net_init_tap(QemuOpts
*opts
, const char *name
, VLANState
*vlan
)
588 int fd
, vnet_hdr
= 0;
591 if (qemu_opt_get(opts
, "fd")) {
592 if (qemu_opt_get(opts
, "ifname") ||
593 qemu_opt_get(opts
, "script") ||
594 qemu_opt_get(opts
, "downscript") ||
595 qemu_opt_get(opts
, "vnet_hdr") ||
596 qemu_opt_get(opts
, "helper")) {
597 error_report("ifname=, script=, downscript=, vnet_hdr=, "
598 "and helper= are invalid with fd=");
602 fd
= net_handle_fd_param(cur_mon
, qemu_opt_get(opts
, "fd"));
607 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
609 vnet_hdr
= tap_probe_vnet_hdr(fd
);
613 } else if (qemu_opt_get(opts
, "helper")) {
614 if (qemu_opt_get(opts
, "ifname") ||
615 qemu_opt_get(opts
, "script") ||
616 qemu_opt_get(opts
, "downscript") ||
617 qemu_opt_get(opts
, "vnet_hdr")) {
618 error_report("ifname=, script=, downscript=, and vnet_hdr= "
619 "are invalid with helper=");
623 fd
= net_bridge_run_helper(qemu_opt_get(opts
, "helper"),
624 DEFAULT_BRIDGE_INTERFACE
);
629 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
631 vnet_hdr
= tap_probe_vnet_hdr(fd
);
636 if (!qemu_opt_get(opts
, "script")) {
637 qemu_opt_set(opts
, "script", DEFAULT_NETWORK_SCRIPT
);
640 if (!qemu_opt_get(opts
, "downscript")) {
641 qemu_opt_set(opts
, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT
);
644 fd
= net_tap_init(opts
, &vnet_hdr
);
652 s
= net_tap_fd_init(vlan
, model
, name
, fd
, vnet_hdr
);
658 if (tap_set_sndbuf(s
->fd
, opts
) < 0) {
662 if (qemu_opt_get(opts
, "fd")) {
663 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "fd=%d", fd
);
664 } else if (qemu_opt_get(opts
, "helper")) {
665 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
666 "helper=%s", qemu_opt_get(opts
, "helper"));
668 const char *ifname
, *script
, *downscript
;
670 ifname
= qemu_opt_get(opts
, "ifname");
671 script
= qemu_opt_get(opts
, "script");
672 downscript
= qemu_opt_get(opts
, "downscript");
674 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
675 "ifname=%s,script=%s,downscript=%s",
676 ifname
, script
, downscript
);
678 if (strcmp(downscript
, "no") != 0) {
679 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", downscript
);
680 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
), "%s", ifname
);
684 if (qemu_opt_get_bool(opts
, "vhost", !!qemu_opt_get(opts
, "vhostfd") ||
685 qemu_opt_get_bool(opts
, "vhostforce", false))) {
687 bool force
= qemu_opt_get_bool(opts
, "vhostforce", false);
688 if (qemu_opt_get(opts
, "vhostfd")) {
689 r
= net_handle_fd_param(cur_mon
, qemu_opt_get(opts
, "vhostfd"));
697 s
->vhost_net
= vhost_net_init(&s
->nc
, vhostfd
, force
);
699 error_report("vhost-net requested but could not be initialized");
702 } else if (qemu_opt_get(opts
, "vhostfd")) {
703 error_report("vhostfd= is not valid without vhost");
710 VHostNetState
*tap_get_vhost_net(VLANClientState
*nc
)
712 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
713 assert(nc
->info
->type
== NET_CLIENT_TYPE_TAP
);