* THE SOFTWARE.
*/
+#include "qemu/osdep.h"
#include "tap_int.h"
-#include "config-host.h"
#include <sys/ioctl.h>
-#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <net/if.h>
#include "clients.h"
#include "monitor/monitor.h"
#include "sysemu/sysemu.h"
+#include "qapi/error.h"
#include "qemu-common.h"
+#include "qemu/cutils.h"
#include "qemu/error-report.h"
+#include "qemu/main-loop.h"
+#include "qemu/sockets.h"
#include "net/tap.h"
bool enabled;
VHostNetState *vhost_net;
unsigned host_vnet_hdr_len;
+ Notifier exit;
} TAPState;
static void launch_script(const char *setup_script, const char *ifname,
{
TAPState *s = DO_UPCAST(TAPState, nc, nc);
- assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
+ assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
return s->has_ufo;
}
{
TAPState *s = DO_UPCAST(TAPState, nc, nc);
- assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
+ assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
return !!s->host_vnet_hdr_len;
}
{
TAPState *s = DO_UPCAST(TAPState, nc, nc);
- assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
+ assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
return !!tap_probe_vnet_hdr_len(s->fd, len);
}
{
TAPState *s = DO_UPCAST(TAPState, nc, nc);
- assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
+ assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
len == sizeof(struct virtio_net_hdr));
{
TAPState *s = DO_UPCAST(TAPState, nc, nc);
- assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
+ assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
s->using_vnet_hdr = using_vnet_hdr;
tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
}
+static void tap_exit_notify(Notifier *notifier, void *data)
+{
+ TAPState *s = container_of(notifier, TAPState, exit);
+ Error *err = NULL;
+
+ if (s->down_script[0]) {
+ launch_script(s->down_script, s->down_script_arg, s->fd, &err);
+ if (err) {
+ error_report_err(err);
+ }
+ }
+}
+
static void tap_cleanup(NetClientState *nc)
{
TAPState *s = DO_UPCAST(TAPState, nc, nc);
- Error *err = NULL;
if (s->vhost_net) {
vhost_net_cleanup(s->vhost_net);
+ g_free(s->vhost_net);
s->vhost_net = NULL;
}
qemu_purge_queued_packets(nc);
- if (s->down_script[0]) {
- launch_script(s->down_script, s->down_script_arg, s->fd, &err);
- if (err) {
- error_report_err(err);
- }
- }
+ tap_exit_notify(&s->exit, NULL);
+ qemu_remove_exit_notifier(&s->exit);
tap_read_poll(s, false);
tap_write_poll(s, false);
int tap_get_fd(NetClientState *nc)
{
TAPState *s = DO_UPCAST(TAPState, nc, nc);
- assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
+ assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
return s->fd;
}
/* fd support */
static NetClientInfo net_tap_info = {
- .type = NET_CLIENT_OPTIONS_KIND_TAP,
+ .type = NET_CLIENT_DRIVER_TAP,
.size = sizeof(TAPState),
.receive = tap_receive,
.receive_raw = tap_receive_raw,
}
tap_read_poll(s, true);
s->vhost_net = NULL;
+
+ s->exit.notify = tap_exit_notify;
+ qemu_add_exit_notifier(&s->exit);
+
return s;
}
}
if (pid == 0) {
int open_max = sysconf(_SC_OPEN_MAX), i;
- char fd_buf[6+10];
- char br_buf[6+IFNAMSIZ] = {0};
- char helper_cmd[PATH_MAX + sizeof(fd_buf) + sizeof(br_buf) + 15];
+ char *fd_buf = NULL;
+ char *br_buf = NULL;
+ char *helper_cmd = NULL;
for (i = 3; i < open_max; i++) {
if (i != sv[1]) {
}
}
- snprintf(fd_buf, sizeof(fd_buf), "%s%d", "--fd=", sv[1]);
+ fd_buf = g_strdup_printf("%s%d", "--fd=", sv[1]);
if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
/* assume helper is a command */
if (strstr(helper, "--br=") == NULL) {
- snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
+ br_buf = g_strdup_printf("%s%s", "--br=", bridge);
}
- snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s",
- helper, "--use-vnet", fd_buf, br_buf);
+ helper_cmd = g_strdup_printf("%s %s %s %s", helper,
+ "--use-vnet", fd_buf, br_buf ? br_buf : "");
parg = args;
*parg++ = (char *)"sh";
*parg++ = NULL;
execv("/bin/sh", args);
+ g_free(helper_cmd);
} else {
/* assume helper is just the executable path name */
- snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
+ br_buf = g_strdup_printf("%s%s", "--br=", bridge);
parg = args;
*parg++ = (char *)helper;
execv(helper, args);
}
+ g_free(fd_buf);
+ g_free(br_buf);
_exit(1);
} else {
}
}
-int net_init_bridge(const NetClientOptions *opts, const char *name,
+int net_init_bridge(const Netdev *netdev, const char *name,
NetClientState *peer, Error **errp)
{
const NetdevBridgeOptions *bridge;
TAPState *s;
int fd, vnet_hdr;
- assert(opts->kind == NET_CLIENT_OPTIONS_KIND_BRIDGE);
- bridge = opts->bridge;
+ assert(netdev->type == NET_CLIENT_DRIVER_BRIDGE);
+ bridge = &netdev->u.bridge;
helper = bridge->has_helper ? bridge->helper : DEFAULT_BRIDGE_HELPER;
br = bridge->has_br ? bridge->br : DEFAULT_BRIDGE_INTERFACE;
return -1;
}
- fcntl(fd, F_SETFL, O_NONBLOCK);
+ qemu_set_nonblock(fd);
vnet_hdr = tap_probe_vnet_hdr(fd);
s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
options.backend_type = VHOST_BACKEND_TYPE_KERNEL;
options.net_backend = &s->nc;
+ if (tap->has_poll_us) {
+ options.busyloop_timeout = tap->poll_us;
+ } else {
+ options.busyloop_timeout = 0;
+ }
- if (tap->has_vhostfd || tap->has_vhostfds) {
+ if (vhostfdname) {
vhostfd = monitor_fd_param(cur_mon, vhostfdname, &err);
if (vhostfd == -1) {
- error_propagate(errp, err);
+ if (tap->has_vhostforce && tap->vhostforce) {
+ error_propagate(errp, err);
+ } else {
+ warn_report_err(err);
+ }
return;
}
+ qemu_set_nonblock(vhostfd);
} else {
vhostfd = open("/dev/vhost-net", O_RDWR);
if (vhostfd < 0) {
- error_setg_errno(errp, errno,
- "tap: open vhost char device failed");
+ if (tap->has_vhostforce && tap->vhostforce) {
+ error_setg_errno(errp, errno,
+ "tap: open vhost char device failed");
+ } else {
+ warn_report("tap: open vhost char device failed: %s",
+ strerror(errno));
+ }
return;
}
+ qemu_set_nonblock(vhostfd);
}
options.opaque = (void *)(uintptr_t)vhostfd;
s->vhost_net = vhost_net_init(&options);
if (!s->vhost_net) {
- error_setg(errp,
- "vhost-net requested but could not be initialized");
+ if (tap->has_vhostforce && tap->vhostforce) {
+ error_setg(errp, VHOST_NET_INIT_FAILED);
+ } else {
+ warn_report(VHOST_NET_INIT_FAILED);
+ }
return;
}
- } else if (tap->has_vhostfd || tap->has_vhostfds) {
- error_setg(errp, "vhostfd= is not valid without vhost");
+ } else if (vhostfdname) {
+ error_setg(errp, "vhostfd(s)= is not valid without vhost");
}
}
return i;
}
-int net_init_tap(const NetClientOptions *opts, const char *name,
+int net_init_tap(const Netdev *netdev, const char *name,
NetClientState *peer, Error **errp)
{
const NetdevTapOptions *tap;
const char *vhostfdname;
char ifname[128];
- assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP);
- tap = opts->tap;
+ assert(netdev->type == NET_CLIENT_DRIVER_TAP);
+ tap = &netdev->u.tap;
queues = tap->has_queues ? tap->queues : 1;
vhostfdname = tap->has_vhostfd ? tap->vhostfd : NULL;
- /* QEMU vlans does not support multiqueue tap, in this case peer is set.
+ /* QEMU hubs do not support multiqueue tap, in this case peer is set.
* For -netdev, peer is always NULL. */
if (peer && (tap->has_queues || tap->has_fds || tap->has_vhostfds)) {
- error_setg(errp, "Multiqueue tap cannot be used with QEMU vlans");
+ error_setg(errp, "Multiqueue tap cannot be used with hubs");
return -1;
}
return -1;
}
- fcntl(fd, F_SETFL, O_NONBLOCK);
+ qemu_set_nonblock(fd);
vnet_hdr = tap_probe_vnet_hdr(fd);
return -1;
}
} else if (tap->has_fds) {
- char *fds[MAX_TAP_QUEUES];
- char *vhost_fds[MAX_TAP_QUEUES];
- int nfds, nvhosts;
+ char **fds;
+ char **vhost_fds;
+ int nfds = 0, nvhosts = 0;
+ int ret = 0;
if (tap->has_ifname || tap->has_script || tap->has_downscript ||
tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
return -1;
}
+ fds = g_new0(char *, MAX_TAP_QUEUES);
+ vhost_fds = g_new0(char *, MAX_TAP_QUEUES);
+
nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES);
if (tap->has_vhostfds) {
nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES);
if (nfds != nvhosts) {
error_setg(errp, "The number of fds passed does not match "
"the number of vhostfds passed");
- return -1;
+ ret = -1;
+ goto free_fail;
}
}
fd = monitor_fd_param(cur_mon, fds[i], &err);
if (fd == -1) {
error_propagate(errp, err);
- return -1;
+ ret = -1;
+ goto free_fail;
}
- fcntl(fd, F_SETFL, O_NONBLOCK);
+ qemu_set_nonblock(fd);
if (i == 0) {
vnet_hdr = tap_probe_vnet_hdr(fd);
} else if (vnet_hdr != tap_probe_vnet_hdr(fd)) {
error_setg(errp,
"vnet_hdr not consistent across given tap fds");
- return -1;
+ ret = -1;
+ goto free_fail;
}
net_init_tap_one(tap, peer, "tap", name, ifname,
vnet_hdr, fd, &err);
if (err) {
error_propagate(errp, err);
- return -1;
+ ret = -1;
+ goto free_fail;
}
}
+
+free_fail:
+ for (i = 0; i < nvhosts; i++) {
+ g_free(vhost_fds[i]);
+ }
+ for (i = 0; i < nfds; i++) {
+ g_free(fds[i]);
+ }
+ g_free(fds);
+ g_free(vhost_fds);
+ return ret;
} else if (tap->has_helper) {
if (tap->has_ifname || tap->has_script || tap->has_downscript ||
tap->has_vnet_hdr || tap->has_queues || tap->has_vhostfds) {
return -1;
}
- fd = net_bridge_run_helper(tap->helper, DEFAULT_BRIDGE_INTERFACE,
+ fd = net_bridge_run_helper(tap->helper,
+ tap->has_br ?
+ tap->br : DEFAULT_BRIDGE_INTERFACE,
errp);
if (fd == -1) {
return -1;
}
- fcntl(fd, F_SETFL, O_NONBLOCK);
+ qemu_set_nonblock(fd);
vnet_hdr = tap_probe_vnet_hdr(fd);
net_init_tap_one(tap, peer, "bridge", name, ifname,
VHostNetState *tap_get_vhost_net(NetClientState *nc)
{
TAPState *s = DO_UPCAST(TAPState, nc, nc);
- assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
+ assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
return s->vhost_net;
}