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 "monitor/monitor.h"
39 #include "sysemu/sysemu.h"
40 #include "qemu-common.h"
41 #include "qemu/error-report.h"
45 #include "net/vhost_net.h"
47 typedef struct TAPState
{
50 char down_script
[1024];
51 char down_script_arg
[128];
52 uint8_t buf
[NET_BUFSIZE
];
58 VHostNetState
*vhost_net
;
59 unsigned host_vnet_hdr_len
;
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
&& s
->enabled
? tap_can_send
: NULL
,
72 s
->read_poll
&& s
->enabled
? tap_send
: NULL
,
73 s
->write_poll
&& s
->enabled
? tap_writable
: NULL
,
77 static void tap_read_poll(TAPState
*s
, bool enable
)
79 s
->read_poll
= enable
;
80 tap_update_fd_handler(s
);
83 static void tap_write_poll(TAPState
*s
, bool enable
)
85 s
->write_poll
= enable
;
86 tap_update_fd_handler(s
);
89 static void tap_writable(void *opaque
)
93 tap_write_poll(s
, false);
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
, true);
114 static ssize_t
tap_receive_iov(NetClientState
*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_mrg_rxbuf hdr
= { };
122 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
123 iov_copy
[0].iov_base
= &hdr
;
124 iov_copy
[0].iov_len
= s
->host_vnet_hdr_len
;
125 memcpy(&iov_copy
[1], iov
, iovcnt
* sizeof(*iov
));
130 return tap_write_packet(s
, iovp
, iovcnt
);
133 static ssize_t
tap_receive_raw(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
135 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
138 struct virtio_net_hdr_mrg_rxbuf hdr
= { };
140 if (s
->host_vnet_hdr_len
) {
141 iov
[iovcnt
].iov_base
= &hdr
;
142 iov
[iovcnt
].iov_len
= s
->host_vnet_hdr_len
;
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(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
155 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
158 if (s
->host_vnet_hdr_len
&& !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(NetClientState
*nc
, ssize_t len
)
184 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
185 tap_read_poll(s
, true);
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
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
202 buf
+= s
->host_vnet_hdr_len
;
203 size
-= s
->host_vnet_hdr_len
;
206 size
= qemu_send_packet_async(&s
->nc
, buf
, size
, tap_send_completed
);
208 tap_read_poll(s
, false);
210 } while (size
> 0 && qemu_can_send_packet(&s
->nc
));
213 bool tap_has_ufo(NetClientState
*nc
)
215 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
217 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
222 int tap_has_vnet_hdr(NetClientState
*nc
)
224 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
226 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
228 return !!s
->host_vnet_hdr_len
;
231 int tap_has_vnet_hdr_len(NetClientState
*nc
, int len
)
233 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
235 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
237 return tap_probe_vnet_hdr_len(s
->fd
, len
);
240 void tap_set_vnet_hdr_len(NetClientState
*nc
, int len
)
242 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
244 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
245 assert(len
== sizeof(struct virtio_net_hdr_mrg_rxbuf
) ||
246 len
== sizeof(struct virtio_net_hdr
));
248 tap_fd_set_vnet_hdr_len(s
->fd
, len
);
249 s
->host_vnet_hdr_len
= len
;
252 void tap_using_vnet_hdr(NetClientState
*nc
, bool using_vnet_hdr
)
254 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
256 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
257 assert(!!s
->host_vnet_hdr_len
== using_vnet_hdr
);
259 s
->using_vnet_hdr
= using_vnet_hdr
;
262 void tap_set_offload(NetClientState
*nc
, int csum
, int tso4
,
263 int tso6
, int ecn
, int ufo
)
265 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
270 tap_fd_set_offload(s
->fd
, csum
, tso4
, tso6
, ecn
, ufo
);
273 static void tap_cleanup(NetClientState
*nc
)
275 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
278 vhost_net_cleanup(s
->vhost_net
);
282 qemu_purge_queued_packets(nc
);
284 if (s
->down_script
[0])
285 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
);
287 tap_read_poll(s
, false);
288 tap_write_poll(s
, false);
293 static void tap_poll(NetClientState
*nc
, bool enable
)
295 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
296 tap_read_poll(s
, enable
);
297 tap_write_poll(s
, enable
);
300 int tap_get_fd(NetClientState
*nc
)
302 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
303 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
309 static NetClientInfo net_tap_info
= {
310 .type
= NET_CLIENT_OPTIONS_KIND_TAP
,
311 .size
= sizeof(TAPState
),
312 .receive
= tap_receive
,
313 .receive_raw
= tap_receive_raw
,
314 .receive_iov
= tap_receive_iov
,
316 .cleanup
= tap_cleanup
,
319 static TAPState
*net_tap_fd_init(NetClientState
*peer
,
328 nc
= qemu_new_net_client(&net_tap_info
, peer
, model
, name
);
330 s
= DO_UPCAST(TAPState
, nc
, nc
);
333 s
->host_vnet_hdr_len
= vnet_hdr
? sizeof(struct virtio_net_hdr
) : 0;
334 s
->using_vnet_hdr
= false;
335 s
->has_ufo
= tap_probe_has_ufo(s
->fd
);
337 tap_set_offload(&s
->nc
, 0, 0, 0, 0, 0);
339 * Make sure host header length is set correctly in tap:
340 * it might have been modified by another instance of qemu.
342 if (tap_probe_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
)) {
343 tap_fd_set_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
);
345 tap_read_poll(s
, true);
350 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
356 /* try to launch network script */
359 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
361 for (i
= 0; i
< open_max
; i
++) {
362 if (i
!= STDIN_FILENO
&&
363 i
!= STDOUT_FILENO
&&
364 i
!= STDERR_FILENO
&&
370 *parg
++ = (char *)setup_script
;
371 *parg
++ = (char *)ifname
;
373 execv(setup_script
, args
);
375 } else if (pid
> 0) {
376 while (waitpid(pid
, &status
, 0) != pid
) {
380 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
384 fprintf(stderr
, "%s: could not launch network script\n", setup_script
);
388 static int recv_fd(int c
)
391 uint8_t msgbuf
[CMSG_SPACE(sizeof(fd
))];
392 struct msghdr msg
= {
393 .msg_control
= msgbuf
,
394 .msg_controllen
= sizeof(msgbuf
),
396 struct cmsghdr
*cmsg
;
401 cmsg
= CMSG_FIRSTHDR(&msg
);
402 cmsg
->cmsg_level
= SOL_SOCKET
;
403 cmsg
->cmsg_type
= SCM_RIGHTS
;
404 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
405 msg
.msg_controllen
= cmsg
->cmsg_len
;
408 iov
.iov_len
= sizeof(req
);
413 len
= recvmsg(c
, &msg
, 0);
415 memcpy(&fd
, CMSG_DATA(cmsg
), sizeof(fd
));
422 static int net_bridge_run_helper(const char *helper
, const char *bridge
)
424 sigset_t oldmask
, mask
;
431 sigaddset(&mask
, SIGCHLD
);
432 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
434 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
) == -1) {
438 /* try to launch bridge helper */
441 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
443 char br_buf
[6+IFNAMSIZ
] = {0};
444 char helper_cmd
[PATH_MAX
+ sizeof(fd_buf
) + sizeof(br_buf
) + 15];
446 for (i
= 0; i
< open_max
; i
++) {
447 if (i
!= STDIN_FILENO
&&
448 i
!= STDOUT_FILENO
&&
449 i
!= STDERR_FILENO
&&
455 snprintf(fd_buf
, sizeof(fd_buf
), "%s%d", "--fd=", sv
[1]);
457 if (strrchr(helper
, ' ') || strrchr(helper
, '\t')) {
458 /* assume helper is a command */
460 if (strstr(helper
, "--br=") == NULL
) {
461 snprintf(br_buf
, sizeof(br_buf
), "%s%s", "--br=", bridge
);
464 snprintf(helper_cmd
, sizeof(helper_cmd
), "%s %s %s %s",
465 helper
, "--use-vnet", fd_buf
, br_buf
);
468 *parg
++ = (char *)"sh";
469 *parg
++ = (char *)"-c";
470 *parg
++ = helper_cmd
;
473 execv("/bin/sh", args
);
475 /* assume helper is just the executable path name */
477 snprintf(br_buf
, sizeof(br_buf
), "%s%s", "--br=", bridge
);
480 *parg
++ = (char *)helper
;
481 *parg
++ = (char *)"--use-vnet";
490 } else if (pid
> 0) {
497 } while (fd
== -1 && errno
== EINTR
);
501 while (waitpid(pid
, &status
, 0) != pid
) {
504 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
506 fprintf(stderr
, "failed to recv file descriptor\n");
510 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
514 fprintf(stderr
, "failed to launch bridge helper\n");
518 int net_init_bridge(const NetClientOptions
*opts
, const char *name
,
519 NetClientState
*peer
)
521 const NetdevBridgeOptions
*bridge
;
522 const char *helper
, *br
;
527 assert(opts
->kind
== NET_CLIENT_OPTIONS_KIND_BRIDGE
);
528 bridge
= opts
->bridge
;
530 helper
= bridge
->has_helper
? bridge
->helper
: DEFAULT_BRIDGE_HELPER
;
531 br
= bridge
->has_br
? bridge
->br
: DEFAULT_BRIDGE_INTERFACE
;
533 fd
= net_bridge_run_helper(helper
, br
);
538 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
540 vnet_hdr
= tap_probe_vnet_hdr(fd
);
542 s
= net_tap_fd_init(peer
, "bridge", name
, fd
, vnet_hdr
);
548 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "helper=%s,br=%s", helper
,
554 static int net_tap_init(const NetdevTapOptions
*tap
, int *vnet_hdr
,
555 const char *setup_script
, char *ifname
,
556 size_t ifname_sz
, int mq_required
)
558 int fd
, vnet_hdr_required
;
560 if (tap
->has_vnet_hdr
) {
561 *vnet_hdr
= tap
->vnet_hdr
;
562 vnet_hdr_required
= *vnet_hdr
;
565 vnet_hdr_required
= 0;
568 TFR(fd
= tap_open(ifname
, ifname_sz
, vnet_hdr
, vnet_hdr_required
,
575 setup_script
[0] != '\0' &&
576 strcmp(setup_script
, "no") != 0 &&
577 launch_script(setup_script
, ifname
, fd
)) {
585 #define MAX_TAP_QUEUES 1024
587 static int net_init_tap_one(const NetdevTapOptions
*tap
, NetClientState
*peer
,
588 const char *model
, const char *name
,
589 const char *ifname
, const char *script
,
590 const char *downscript
, const char *vhostfdname
,
591 int vnet_hdr
, int fd
)
595 s
= net_tap_fd_init(peer
, model
, name
, fd
, vnet_hdr
);
601 if (tap_set_sndbuf(s
->fd
, tap
) < 0) {
605 if (tap
->has_fd
|| tap
->has_fds
) {
606 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "fd=%d", fd
);
607 } else if (tap
->has_helper
) {
608 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "helper=%s",
611 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
612 "ifname=%s,script=%s,downscript=%s", ifname
, script
,
615 if (strcmp(downscript
, "no") != 0) {
616 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", downscript
);
617 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
),
622 if (tap
->has_vhost
? tap
->vhost
:
623 vhostfdname
|| (tap
->has_vhostforce
&& tap
->vhostforce
)) {
626 if (tap
->has_vhostfd
|| tap
->has_vhostfds
) {
627 vhostfd
= monitor_handle_fd_param(cur_mon
, vhostfdname
);
635 s
->vhost_net
= vhost_net_init(&s
->nc
, vhostfd
,
636 tap
->has_vhostforce
&& tap
->vhostforce
);
638 error_report("vhost-net requested but could not be initialized");
641 } else if (tap
->has_vhostfd
|| tap
->has_vhostfds
) {
642 error_report("vhostfd= is not valid without vhost");
649 static int get_fds(char *str
, char *fds
[], int max
)
651 char *ptr
= str
, *this;
652 size_t len
= strlen(str
);
655 while (i
< max
&& ptr
< str
+ len
) {
656 this = strchr(ptr
, ':');
659 fds
[i
] = g_strdup(ptr
);
661 fds
[i
] = g_strndup(ptr
, this - ptr
);
675 int net_init_tap(const NetClientOptions
*opts
, const char *name
,
676 NetClientState
*peer
)
678 const NetdevTapOptions
*tap
;
679 int fd
, vnet_hdr
= 0, i
= 0, queues
;
680 /* for the no-fd, no-helper case */
681 const char *script
= NULL
; /* suppress wrong "uninit'd use" gcc warning */
682 const char *downscript
= NULL
;
683 const char *vhostfdname
;
686 assert(opts
->kind
== NET_CLIENT_OPTIONS_KIND_TAP
);
688 queues
= tap
->has_queues
? tap
->queues
: 1;
689 vhostfdname
= tap
->has_vhostfd
? tap
->vhostfd
: NULL
;
691 /* QEMU vlans does not support multiqueue tap, in this case peer is set.
692 * For -netdev, peer is always NULL. */
693 if (peer
&& (tap
->has_queues
|| tap
->has_fds
|| tap
->has_vhostfds
)) {
694 error_report("Multiqueue tap cannot be used with QEMU vlans");
699 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
700 tap
->has_vnet_hdr
|| tap
->has_helper
|| tap
->has_queues
||
702 error_report("ifname=, script=, downscript=, vnet_hdr=, "
703 "helper=, queues=, and fds= are invalid with fd=");
707 fd
= monitor_handle_fd_param(cur_mon
, tap
->fd
);
712 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
714 vnet_hdr
= tap_probe_vnet_hdr(fd
);
716 if (net_init_tap_one(tap
, peer
, "tap", name
, NULL
,
718 vhostfdname
, vnet_hdr
, fd
)) {
721 } else if (tap
->has_fds
) {
722 char *fds
[MAX_TAP_QUEUES
];
723 char *vhost_fds
[MAX_TAP_QUEUES
];
726 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
727 tap
->has_vnet_hdr
|| tap
->has_helper
|| tap
->has_queues
||
729 error_report("ifname=, script=, downscript=, vnet_hdr=, "
730 "helper=, queues=, and fd= are invalid with fds=");
734 nfds
= get_fds(tap
->fds
, fds
, MAX_TAP_QUEUES
);
735 if (tap
->has_vhostfds
) {
736 nvhosts
= get_fds(tap
->vhostfds
, vhost_fds
, MAX_TAP_QUEUES
);
737 if (nfds
!= nvhosts
) {
738 error_report("The number of fds passed does not match the "
739 "number of vhostfds passed");
744 for (i
= 0; i
< nfds
; i
++) {
745 fd
= monitor_handle_fd_param(cur_mon
, fds
[i
]);
750 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
753 vnet_hdr
= tap_probe_vnet_hdr(fd
);
754 } else if (vnet_hdr
!= tap_probe_vnet_hdr(fd
)) {
755 error_report("vnet_hdr not consistent across given tap fds");
759 if (net_init_tap_one(tap
, peer
, "tap", name
, ifname
,
761 tap
->has_vhostfds
? vhost_fds
[i
] : NULL
,
766 } else if (tap
->has_helper
) {
767 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
768 tap
->has_vnet_hdr
|| tap
->has_queues
|| tap
->has_fds
) {
769 error_report("ifname=, script=, downscript=, and vnet_hdr= "
770 "queues=, and fds= are invalid with helper=");
774 fd
= net_bridge_run_helper(tap
->helper
, DEFAULT_BRIDGE_INTERFACE
);
779 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
780 vnet_hdr
= tap_probe_vnet_hdr(fd
);
782 if (net_init_tap_one(tap
, peer
, "bridge", name
, ifname
,
783 script
, downscript
, vhostfdname
,
788 script
= tap
->has_script
? tap
->script
: DEFAULT_NETWORK_SCRIPT
;
789 downscript
= tap
->has_downscript
? tap
->downscript
:
790 DEFAULT_NETWORK_DOWN_SCRIPT
;
792 if (tap
->has_ifname
) {
793 pstrcpy(ifname
, sizeof ifname
, tap
->ifname
);
798 for (i
= 0; i
< queues
; i
++) {
799 fd
= net_tap_init(tap
, &vnet_hdr
, i
>= 1 ? "no" : script
,
800 ifname
, sizeof ifname
, queues
> 1);
805 if (queues
> 1 && i
== 0 && !tap
->has_ifname
) {
806 if (tap_fd_get_ifname(fd
, ifname
)) {
807 error_report("Fail to get ifname");
812 if (net_init_tap_one(tap
, peer
, "tap", name
, ifname
,
813 i
>= 1 ? "no" : script
,
814 i
>= 1 ? "no" : downscript
,
815 vhostfdname
, vnet_hdr
, fd
)) {
824 VHostNetState
*tap_get_vhost_net(NetClientState
*nc
)
826 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
827 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
831 int tap_enable(NetClientState
*nc
)
833 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
839 ret
= tap_fd_enable(s
->fd
);
842 tap_update_fd_handler(s
);
848 int tap_disable(NetClientState
*nc
)
850 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
853 if (s
->enabled
== 0) {
856 ret
= tap_fd_disable(s
->fd
);
858 qemu_purge_queued_packets(nc
);
860 tap_update_fd_handler(s
);