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>
40 #include "qemu-char.h"
41 #include "qemu-common.h"
42 #include "qemu-error.h"
44 #include "net/tap-linux.h"
46 #include "hw/vhost_net.h"
48 /* Maximum GSO packet size (64k) plus plenty of room for
49 * the ethernet and virtio_net headers
51 #define TAP_BUFSIZE (4096 + 65536)
53 typedef struct TAPState
{
56 char down_script
[1024];
57 char down_script_arg
[128];
58 uint8_t buf
[TAP_BUFSIZE
];
59 unsigned int read_poll
: 1;
60 unsigned int write_poll
: 1;
61 unsigned int using_vnet_hdr
: 1;
62 unsigned int has_ufo
: 1;
63 VHostNetState
*vhost_net
;
64 unsigned host_vnet_hdr_len
;
67 static int launch_script(const char *setup_script
, const char *ifname
, int fd
);
69 static int tap_can_send(void *opaque
);
70 static void tap_send(void *opaque
);
71 static void tap_writable(void *opaque
);
73 static void tap_update_fd_handler(TAPState
*s
)
75 qemu_set_fd_handler2(s
->fd
,
76 s
->read_poll
? tap_can_send
: NULL
,
77 s
->read_poll
? tap_send
: NULL
,
78 s
->write_poll
? tap_writable
: NULL
,
82 static void tap_read_poll(TAPState
*s
, int enable
)
84 s
->read_poll
= !!enable
;
85 tap_update_fd_handler(s
);
88 static void tap_write_poll(TAPState
*s
, int enable
)
90 s
->write_poll
= !!enable
;
91 tap_update_fd_handler(s
);
94 static void tap_writable(void *opaque
)
100 qemu_flush_queued_packets(&s
->nc
);
103 static ssize_t
tap_write_packet(TAPState
*s
, const struct iovec
*iov
, int iovcnt
)
108 len
= writev(s
->fd
, iov
, iovcnt
);
109 } while (len
== -1 && errno
== EINTR
);
111 if (len
== -1 && errno
== EAGAIN
) {
112 tap_write_poll(s
, 1);
119 static ssize_t
tap_receive_iov(NetClientState
*nc
, const struct iovec
*iov
,
122 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
123 const struct iovec
*iovp
= iov
;
124 struct iovec iov_copy
[iovcnt
+ 1];
125 struct virtio_net_hdr_mrg_rxbuf hdr
= { };
127 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
128 iov_copy
[0].iov_base
= &hdr
;
129 iov_copy
[0].iov_len
= s
->host_vnet_hdr_len
;
130 memcpy(&iov_copy
[1], iov
, iovcnt
* sizeof(*iov
));
135 return tap_write_packet(s
, iovp
, iovcnt
);
138 static ssize_t
tap_receive_raw(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
140 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
143 struct virtio_net_hdr_mrg_rxbuf hdr
= { };
145 if (s
->host_vnet_hdr_len
) {
146 iov
[iovcnt
].iov_base
= &hdr
;
147 iov
[iovcnt
].iov_len
= s
->host_vnet_hdr_len
;
151 iov
[iovcnt
].iov_base
= (char *)buf
;
152 iov
[iovcnt
].iov_len
= size
;
155 return tap_write_packet(s
, iov
, iovcnt
);
158 static ssize_t
tap_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
160 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
163 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
164 return tap_receive_raw(nc
, buf
, size
);
167 iov
[0].iov_base
= (char *)buf
;
168 iov
[0].iov_len
= size
;
170 return tap_write_packet(s
, iov
, 1);
173 static int tap_can_send(void *opaque
)
175 TAPState
*s
= opaque
;
177 return qemu_can_send_packet(&s
->nc
);
181 ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
183 return read(tapfd
, buf
, maxlen
);
187 static void tap_send_completed(NetClientState
*nc
, ssize_t len
)
189 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
193 static void tap_send(void *opaque
)
195 TAPState
*s
= opaque
;
199 uint8_t *buf
= s
->buf
;
201 size
= tap_read_packet(s
->fd
, s
->buf
, sizeof(s
->buf
));
206 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
207 buf
+= s
->host_vnet_hdr_len
;
208 size
-= s
->host_vnet_hdr_len
;
211 size
= qemu_send_packet_async(&s
->nc
, buf
, size
, tap_send_completed
);
215 } while (size
> 0 && qemu_can_send_packet(&s
->nc
));
218 int tap_has_ufo(NetClientState
*nc
)
220 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
222 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
227 int tap_has_vnet_hdr(NetClientState
*nc
)
229 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
231 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
233 return !!s
->host_vnet_hdr_len
;
236 int tap_has_vnet_hdr_len(NetClientState
*nc
, int len
)
238 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
240 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
242 return tap_probe_vnet_hdr_len(s
->fd
, len
);
245 void tap_set_vnet_hdr_len(NetClientState
*nc
, int len
)
247 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
249 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
250 assert(len
== sizeof(struct virtio_net_hdr_mrg_rxbuf
) ||
251 len
== sizeof(struct virtio_net_hdr
));
253 tap_fd_set_vnet_hdr_len(s
->fd
, len
);
254 s
->host_vnet_hdr_len
= len
;
257 void tap_using_vnet_hdr(NetClientState
*nc
, int using_vnet_hdr
)
259 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
261 using_vnet_hdr
= using_vnet_hdr
!= 0;
263 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
264 assert(!!s
->host_vnet_hdr_len
== using_vnet_hdr
);
266 s
->using_vnet_hdr
= using_vnet_hdr
;
269 void tap_set_offload(NetClientState
*nc
, int csum
, int tso4
,
270 int tso6
, int ecn
, int ufo
)
272 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
277 tap_fd_set_offload(s
->fd
, csum
, tso4
, tso6
, ecn
, ufo
);
280 static void tap_cleanup(NetClientState
*nc
)
282 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
285 vhost_net_cleanup(s
->vhost_net
);
289 qemu_purge_queued_packets(nc
);
291 if (s
->down_script
[0])
292 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
);
295 tap_write_poll(s
, 0);
300 static void tap_poll(NetClientState
*nc
, bool enable
)
302 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
303 tap_read_poll(s
, enable
);
304 tap_write_poll(s
, enable
);
307 int tap_get_fd(NetClientState
*nc
)
309 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
310 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
316 static NetClientInfo net_tap_info
= {
317 .type
= NET_CLIENT_OPTIONS_KIND_TAP
,
318 .size
= sizeof(TAPState
),
319 .receive
= tap_receive
,
320 .receive_raw
= tap_receive_raw
,
321 .receive_iov
= tap_receive_iov
,
323 .cleanup
= tap_cleanup
,
326 static TAPState
*net_tap_fd_init(NetClientState
*peer
,
335 nc
= qemu_new_net_client(&net_tap_info
, peer
, model
, name
);
337 s
= DO_UPCAST(TAPState
, nc
, nc
);
340 s
->host_vnet_hdr_len
= vnet_hdr
? sizeof(struct virtio_net_hdr
) : 0;
341 s
->using_vnet_hdr
= 0;
342 s
->has_ufo
= tap_probe_has_ufo(s
->fd
);
343 tap_set_offload(&s
->nc
, 0, 0, 0, 0, 0);
345 * Make sure host header length is set correctly in tap:
346 * it might have been modified by another instance of qemu.
348 if (tap_probe_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
)) {
349 tap_fd_set_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
);
356 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
362 /* try to launch network script */
365 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
367 for (i
= 0; i
< open_max
; i
++) {
368 if (i
!= STDIN_FILENO
&&
369 i
!= STDOUT_FILENO
&&
370 i
!= STDERR_FILENO
&&
376 *parg
++ = (char *)setup_script
;
377 *parg
++ = (char *)ifname
;
379 execv(setup_script
, args
);
381 } else if (pid
> 0) {
382 while (waitpid(pid
, &status
, 0) != pid
) {
386 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
390 fprintf(stderr
, "%s: could not launch network script\n", setup_script
);
394 static int recv_fd(int c
)
397 uint8_t msgbuf
[CMSG_SPACE(sizeof(fd
))];
398 struct msghdr msg
= {
399 .msg_control
= msgbuf
,
400 .msg_controllen
= sizeof(msgbuf
),
402 struct cmsghdr
*cmsg
;
407 cmsg
= CMSG_FIRSTHDR(&msg
);
408 cmsg
->cmsg_level
= SOL_SOCKET
;
409 cmsg
->cmsg_type
= SCM_RIGHTS
;
410 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
411 msg
.msg_controllen
= cmsg
->cmsg_len
;
414 iov
.iov_len
= sizeof(req
);
419 len
= recvmsg(c
, &msg
, 0);
421 memcpy(&fd
, CMSG_DATA(cmsg
), sizeof(fd
));
428 static int net_bridge_run_helper(const char *helper
, const char *bridge
)
430 sigset_t oldmask
, mask
;
437 sigaddset(&mask
, SIGCHLD
);
438 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
440 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
) == -1) {
444 /* try to launch bridge helper */
447 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
449 char br_buf
[6+IFNAMSIZ
] = {0};
450 char helper_cmd
[PATH_MAX
+ sizeof(fd_buf
) + sizeof(br_buf
) + 15];
452 for (i
= 0; i
< open_max
; i
++) {
453 if (i
!= STDIN_FILENO
&&
454 i
!= STDOUT_FILENO
&&
455 i
!= STDERR_FILENO
&&
461 snprintf(fd_buf
, sizeof(fd_buf
), "%s%d", "--fd=", sv
[1]);
463 if (strrchr(helper
, ' ') || strrchr(helper
, '\t')) {
464 /* assume helper is a command */
466 if (strstr(helper
, "--br=") == NULL
) {
467 snprintf(br_buf
, sizeof(br_buf
), "%s%s", "--br=", bridge
);
470 snprintf(helper_cmd
, sizeof(helper_cmd
), "%s %s %s %s",
471 helper
, "--use-vnet", fd_buf
, br_buf
);
474 *parg
++ = (char *)"sh";
475 *parg
++ = (char *)"-c";
476 *parg
++ = helper_cmd
;
479 execv("/bin/sh", args
);
481 /* assume helper is just the executable path name */
483 snprintf(br_buf
, sizeof(br_buf
), "%s%s", "--br=", bridge
);
486 *parg
++ = (char *)helper
;
487 *parg
++ = (char *)"--use-vnet";
496 } else if (pid
> 0) {
503 } while (fd
== -1 && errno
== EINTR
);
507 while (waitpid(pid
, &status
, 0) != pid
) {
510 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
512 fprintf(stderr
, "failed to recv file descriptor\n");
516 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
520 fprintf(stderr
, "failed to launch bridge helper\n");
524 int net_init_bridge(const NetClientOptions
*opts
, const char *name
,
525 NetClientState
*peer
)
527 const NetdevBridgeOptions
*bridge
;
528 const char *helper
, *br
;
533 assert(opts
->kind
== NET_CLIENT_OPTIONS_KIND_BRIDGE
);
534 bridge
= opts
->bridge
;
536 helper
= bridge
->has_helper
? bridge
->helper
: DEFAULT_BRIDGE_HELPER
;
537 br
= bridge
->has_br
? bridge
->br
: DEFAULT_BRIDGE_INTERFACE
;
539 fd
= net_bridge_run_helper(helper
, br
);
544 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
546 vnet_hdr
= tap_probe_vnet_hdr(fd
);
548 s
= net_tap_fd_init(peer
, "bridge", name
, fd
, vnet_hdr
);
554 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "helper=%s,br=%s", helper
,
560 static int net_tap_init(const NetdevTapOptions
*tap
, int *vnet_hdr
,
561 const char *setup_script
, char *ifname
,
564 int fd
, vnet_hdr_required
;
566 if (tap
->has_ifname
) {
567 pstrcpy(ifname
, ifname_sz
, tap
->ifname
);
569 assert(ifname_sz
> 0);
573 if (tap
->has_vnet_hdr
) {
574 *vnet_hdr
= tap
->vnet_hdr
;
575 vnet_hdr_required
= *vnet_hdr
;
578 vnet_hdr_required
= 0;
581 TFR(fd
= tap_open(ifname
, ifname_sz
, vnet_hdr
, vnet_hdr_required
));
587 setup_script
[0] != '\0' &&
588 strcmp(setup_script
, "no") != 0 &&
589 launch_script(setup_script
, ifname
, fd
)) {
597 int net_init_tap(const NetClientOptions
*opts
, const char *name
,
598 NetClientState
*peer
)
600 const NetdevTapOptions
*tap
;
602 int fd
, vnet_hdr
= 0;
606 /* for the no-fd, no-helper case */
607 const char *script
= NULL
; /* suppress wrong "uninit'd use" gcc warning */
610 assert(opts
->kind
== NET_CLIENT_OPTIONS_KIND_TAP
);
614 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
615 tap
->has_vnet_hdr
|| tap
->has_helper
) {
616 error_report("ifname=, script=, downscript=, vnet_hdr=, "
617 "and helper= are invalid with fd=");
621 fd
= monitor_handle_fd_param(cur_mon
, tap
->fd
);
626 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
628 vnet_hdr
= tap_probe_vnet_hdr(fd
);
632 } else if (tap
->has_helper
) {
633 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
635 error_report("ifname=, script=, downscript=, and vnet_hdr= "
636 "are invalid with helper=");
640 fd
= net_bridge_run_helper(tap
->helper
, DEFAULT_BRIDGE_INTERFACE
);
645 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
647 vnet_hdr
= tap_probe_vnet_hdr(fd
);
652 script
= tap
->has_script
? tap
->script
: DEFAULT_NETWORK_SCRIPT
;
653 fd
= net_tap_init(tap
, &vnet_hdr
, script
, ifname
, sizeof ifname
);
661 s
= net_tap_fd_init(peer
, model
, name
, fd
, vnet_hdr
);
667 if (tap_set_sndbuf(s
->fd
, tap
) < 0) {
672 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "fd=%d", fd
);
673 } else if (tap
->has_helper
) {
674 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "helper=%s",
677 const char *downscript
;
679 downscript
= tap
->has_downscript
? tap
->downscript
:
680 DEFAULT_NETWORK_DOWN_SCRIPT
;
682 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
683 "ifname=%s,script=%s,downscript=%s", ifname
, script
,
686 if (strcmp(downscript
, "no") != 0) {
687 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", downscript
);
688 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
), "%s", ifname
);
692 if (tap
->has_vhost
? tap
->vhost
:
693 tap
->has_vhostfd
|| (tap
->has_vhostforce
&& tap
->vhostforce
)) {
696 if (tap
->has_vhostfd
) {
697 vhostfd
= monitor_handle_fd_param(cur_mon
, tap
->vhostfd
);
705 s
->vhost_net
= vhost_net_init(&s
->nc
, vhostfd
,
706 tap
->has_vhostforce
&& tap
->vhostforce
);
708 error_report("vhost-net requested but could not be initialized");
711 } else if (tap
->has_vhostfd
) {
712 error_report("vhostfd= is not valid without vhost");
719 VHostNetState
*tap_get_vhost_net(NetClientState
*nc
)
721 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
722 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);