2009-10-22 20:49:07 +04:00
|
|
|
/*
|
|
|
|
* QEMU System Emulator
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2008 Fabrice Bellard
|
|
|
|
* Copyright (c) 2009 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "net/tap.h"
|
|
|
|
|
|
|
|
#include "config-host.h"
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
2009-10-31 00:27:00 +03:00
|
|
|
#include <sys/socket.h>
|
2009-10-22 20:49:07 +04:00
|
|
|
#include <net/if.h>
|
|
|
|
|
|
|
|
#include "net.h"
|
2012-06-07 06:02:21 +04:00
|
|
|
#include "monitor.h"
|
2009-10-22 20:49:07 +04:00
|
|
|
#include "sysemu.h"
|
|
|
|
#include "qemu-char.h"
|
|
|
|
#include "qemu-common.h"
|
2010-02-18 18:24:31 +03:00
|
|
|
#include "qemu-error.h"
|
2009-10-22 20:49:07 +04:00
|
|
|
|
|
|
|
#include "net/tap-linux.h"
|
|
|
|
|
2010-03-17 14:08:24 +03:00
|
|
|
#include "hw/vhost_net.h"
|
|
|
|
|
2009-10-22 20:49:07 +04:00
|
|
|
/* Maximum GSO packet size (64k) plus plenty of room for
|
|
|
|
* the ethernet and virtio_net headers
|
|
|
|
*/
|
|
|
|
#define TAP_BUFSIZE (4096 + 65536)
|
|
|
|
|
|
|
|
typedef struct TAPState {
|
2012-07-24 19:35:13 +04:00
|
|
|
NetClientState nc;
|
2009-10-22 20:49:07 +04:00
|
|
|
int fd;
|
|
|
|
char down_script[1024];
|
|
|
|
char down_script_arg[128];
|
|
|
|
uint8_t buf[TAP_BUFSIZE];
|
|
|
|
unsigned int read_poll : 1;
|
|
|
|
unsigned int write_poll : 1;
|
|
|
|
unsigned int using_vnet_hdr : 1;
|
|
|
|
unsigned int has_ufo: 1;
|
2010-03-17 14:08:24 +03:00
|
|
|
VHostNetState *vhost_net;
|
2010-07-13 18:55:31 +04:00
|
|
|
unsigned host_vnet_hdr_len;
|
2009-10-22 20:49:07 +04:00
|
|
|
} TAPState;
|
|
|
|
|
|
|
|
static int launch_script(const char *setup_script, const char *ifname, int fd);
|
|
|
|
|
|
|
|
static int tap_can_send(void *opaque);
|
|
|
|
static void tap_send(void *opaque);
|
|
|
|
static void tap_writable(void *opaque);
|
|
|
|
|
|
|
|
static void tap_update_fd_handler(TAPState *s)
|
|
|
|
{
|
|
|
|
qemu_set_fd_handler2(s->fd,
|
|
|
|
s->read_poll ? tap_can_send : NULL,
|
|
|
|
s->read_poll ? tap_send : NULL,
|
|
|
|
s->write_poll ? tap_writable : NULL,
|
|
|
|
s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tap_read_poll(TAPState *s, int enable)
|
|
|
|
{
|
|
|
|
s->read_poll = !!enable;
|
|
|
|
tap_update_fd_handler(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tap_write_poll(TAPState *s, int enable)
|
|
|
|
{
|
|
|
|
s->write_poll = !!enable;
|
|
|
|
tap_update_fd_handler(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tap_writable(void *opaque)
|
|
|
|
{
|
|
|
|
TAPState *s = opaque;
|
|
|
|
|
|
|
|
tap_write_poll(s, 0);
|
|
|
|
|
2009-11-25 21:49:04 +03:00
|
|
|
qemu_flush_queued_packets(&s->nc);
|
2009-10-22 20:49:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
|
|
|
|
{
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
do {
|
|
|
|
len = writev(s->fd, iov, iovcnt);
|
|
|
|
} while (len == -1 && errno == EINTR);
|
|
|
|
|
|
|
|
if (len == -1 && errno == EAGAIN) {
|
|
|
|
tap_write_poll(s, 1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
|
2009-10-22 20:49:07 +04:00
|
|
|
int iovcnt)
|
|
|
|
{
|
2009-11-25 21:49:04 +03:00
|
|
|
TAPState *s = DO_UPCAST(TAPState, nc, nc);
|
2009-10-22 20:49:07 +04:00
|
|
|
const struct iovec *iovp = iov;
|
|
|
|
struct iovec iov_copy[iovcnt + 1];
|
2010-07-13 18:55:31 +04:00
|
|
|
struct virtio_net_hdr_mrg_rxbuf hdr = { };
|
2009-10-22 20:49:07 +04:00
|
|
|
|
2010-07-13 18:55:31 +04:00
|
|
|
if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
|
2009-10-22 20:49:07 +04:00
|
|
|
iov_copy[0].iov_base = &hdr;
|
2010-07-13 18:55:31 +04:00
|
|
|
iov_copy[0].iov_len = s->host_vnet_hdr_len;
|
2009-10-22 20:49:07 +04:00
|
|
|
memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
|
|
|
|
iovp = iov_copy;
|
|
|
|
iovcnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tap_write_packet(s, iovp, iovcnt);
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
|
2009-10-22 20:49:07 +04:00
|
|
|
{
|
2009-11-25 21:49:04 +03:00
|
|
|
TAPState *s = DO_UPCAST(TAPState, nc, nc);
|
2009-10-22 20:49:07 +04:00
|
|
|
struct iovec iov[2];
|
|
|
|
int iovcnt = 0;
|
2010-07-13 18:55:31 +04:00
|
|
|
struct virtio_net_hdr_mrg_rxbuf hdr = { };
|
2009-10-22 20:49:07 +04:00
|
|
|
|
2010-07-13 18:55:31 +04:00
|
|
|
if (s->host_vnet_hdr_len) {
|
2009-10-22 20:49:07 +04:00
|
|
|
iov[iovcnt].iov_base = &hdr;
|
2010-07-13 18:55:31 +04:00
|
|
|
iov[iovcnt].iov_len = s->host_vnet_hdr_len;
|
2009-10-22 20:49:07 +04:00
|
|
|
iovcnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
iov[iovcnt].iov_base = (char *)buf;
|
|
|
|
iov[iovcnt].iov_len = size;
|
|
|
|
iovcnt++;
|
|
|
|
|
|
|
|
return tap_write_packet(s, iov, iovcnt);
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
|
2009-10-22 20:49:07 +04:00
|
|
|
{
|
2009-11-25 21:49:04 +03:00
|
|
|
TAPState *s = DO_UPCAST(TAPState, nc, nc);
|
2009-10-22 20:49:07 +04:00
|
|
|
struct iovec iov[1];
|
|
|
|
|
2010-07-13 18:55:31 +04:00
|
|
|
if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
|
2009-11-25 21:49:04 +03:00
|
|
|
return tap_receive_raw(nc, buf, size);
|
2009-10-22 20:49:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
iov[0].iov_base = (char *)buf;
|
|
|
|
iov[0].iov_len = size;
|
|
|
|
|
|
|
|
return tap_write_packet(s, iov, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tap_can_send(void *opaque)
|
|
|
|
{
|
|
|
|
TAPState *s = opaque;
|
|
|
|
|
2009-11-25 21:49:04 +03:00
|
|
|
return qemu_can_send_packet(&s->nc);
|
2009-10-22 20:49:07 +04:00
|
|
|
}
|
|
|
|
|
2009-10-22 20:49:09 +04:00
|
|
|
#ifndef __sun__
|
|
|
|
ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
|
2009-10-22 20:49:07 +04:00
|
|
|
{
|
|
|
|
return read(tapfd, buf, maxlen);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static void tap_send_completed(NetClientState *nc, ssize_t len)
|
2009-10-22 20:49:07 +04:00
|
|
|
{
|
2009-11-25 21:49:04 +03:00
|
|
|
TAPState *s = DO_UPCAST(TAPState, nc, nc);
|
2009-10-22 20:49:07 +04:00
|
|
|
tap_read_poll(s, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tap_send(void *opaque)
|
|
|
|
{
|
|
|
|
TAPState *s = opaque;
|
|
|
|
int size;
|
|
|
|
|
2009-10-27 21:16:39 +03:00
|
|
|
do {
|
|
|
|
uint8_t *buf = s->buf;
|
|
|
|
|
|
|
|
size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
|
|
|
|
if (size <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-07-13 18:55:31 +04:00
|
|
|
if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
|
|
|
|
buf += s->host_vnet_hdr_len;
|
|
|
|
size -= s->host_vnet_hdr_len;
|
2009-10-27 21:16:39 +03:00
|
|
|
}
|
|
|
|
|
2009-11-25 21:49:04 +03:00
|
|
|
size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
|
2009-10-27 21:16:39 +03:00
|
|
|
if (size == 0) {
|
|
|
|
tap_read_poll(s, 0);
|
|
|
|
}
|
2009-11-25 21:49:04 +03:00
|
|
|
} while (size > 0 && qemu_can_send_packet(&s->nc));
|
2009-10-22 20:49:07 +04:00
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
int tap_has_ufo(NetClientState *nc)
|
2009-10-22 20:49:07 +04:00
|
|
|
{
|
2009-11-25 21:49:04 +03:00
|
|
|
TAPState *s = DO_UPCAST(TAPState, nc, nc);
|
2009-10-22 20:49:07 +04:00
|
|
|
|
2012-07-17 18:17:12 +04:00
|
|
|
assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
|
2009-10-22 20:49:07 +04:00
|
|
|
|
|
|
|
return s->has_ufo;
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
int tap_has_vnet_hdr(NetClientState *nc)
|
2009-10-22 20:49:07 +04:00
|
|
|
{
|
2009-11-25 21:49:04 +03:00
|
|
|
TAPState *s = DO_UPCAST(TAPState, nc, nc);
|
2009-10-22 20:49:07 +04:00
|
|
|
|
2012-07-17 18:17:12 +04:00
|
|
|
assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
|
2009-10-22 20:49:07 +04:00
|
|
|
|
2010-07-13 18:55:31 +04:00
|
|
|
return !!s->host_vnet_hdr_len;
|
2009-10-22 20:49:07 +04:00
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
int tap_has_vnet_hdr_len(NetClientState *nc, int len)
|
2010-07-16 12:16:06 +04:00
|
|
|
{
|
|
|
|
TAPState *s = DO_UPCAST(TAPState, nc, nc);
|
|
|
|
|
2012-07-17 18:17:12 +04:00
|
|
|
assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
|
2010-07-16 12:16:06 +04:00
|
|
|
|
|
|
|
return tap_probe_vnet_hdr_len(s->fd, len);
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
void tap_set_vnet_hdr_len(NetClientState *nc, int len)
|
2010-07-16 12:16:06 +04:00
|
|
|
{
|
|
|
|
TAPState *s = DO_UPCAST(TAPState, nc, nc);
|
|
|
|
|
2012-07-17 18:17:12 +04:00
|
|
|
assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
|
2010-07-16 12:16:06 +04:00
|
|
|
assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
|
|
|
|
len == sizeof(struct virtio_net_hdr));
|
|
|
|
|
|
|
|
tap_fd_set_vnet_hdr_len(s->fd, len);
|
|
|
|
s->host_vnet_hdr_len = len;
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
void tap_using_vnet_hdr(NetClientState *nc, int using_vnet_hdr)
|
2009-10-22 20:49:07 +04:00
|
|
|
{
|
2009-11-25 21:49:04 +03:00
|
|
|
TAPState *s = DO_UPCAST(TAPState, nc, nc);
|
2009-10-22 20:49:07 +04:00
|
|
|
|
|
|
|
using_vnet_hdr = using_vnet_hdr != 0;
|
|
|
|
|
2012-07-17 18:17:12 +04:00
|
|
|
assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
|
2010-07-13 18:55:31 +04:00
|
|
|
assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
|
2009-10-22 20:49:07 +04:00
|
|
|
|
|
|
|
s->using_vnet_hdr = using_vnet_hdr;
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
void tap_set_offload(NetClientState *nc, int csum, int tso4,
|
2009-10-22 20:49:07 +04:00
|
|
|
int tso6, int ecn, int ufo)
|
|
|
|
{
|
2009-11-25 21:49:04 +03:00
|
|
|
TAPState *s = DO_UPCAST(TAPState, nc, nc);
|
2010-10-31 20:06:47 +03:00
|
|
|
if (s->fd < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2009-10-22 20:49:07 +04:00
|
|
|
|
2010-10-31 20:06:47 +03:00
|
|
|
tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
|
2009-10-22 20:49:07 +04:00
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static void tap_cleanup(NetClientState *nc)
|
2009-10-22 20:49:07 +04:00
|
|
|
{
|
2009-11-25 21:49:04 +03:00
|
|
|
TAPState *s = DO_UPCAST(TAPState, nc, nc);
|
2009-10-22 20:49:07 +04:00
|
|
|
|
2010-03-17 14:08:24 +03:00
|
|
|
if (s->vhost_net) {
|
|
|
|
vhost_net_cleanup(s->vhost_net);
|
2010-10-27 22:03:43 +04:00
|
|
|
s->vhost_net = NULL;
|
2010-03-17 14:08:24 +03:00
|
|
|
}
|
|
|
|
|
2009-11-25 21:49:04 +03:00
|
|
|
qemu_purge_queued_packets(nc);
|
2009-10-22 20:49:07 +04:00
|
|
|
|
|
|
|
if (s->down_script[0])
|
|
|
|
launch_script(s->down_script, s->down_script_arg, s->fd);
|
|
|
|
|
|
|
|
tap_read_poll(s, 0);
|
|
|
|
tap_write_poll(s, 0);
|
|
|
|
close(s->fd);
|
2010-10-31 20:06:47 +03:00
|
|
|
s->fd = -1;
|
2009-10-22 20:49:07 +04:00
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static void tap_poll(NetClientState *nc, bool enable)
|
2009-12-24 15:46:29 +03:00
|
|
|
{
|
|
|
|
TAPState *s = DO_UPCAST(TAPState, nc, nc);
|
|
|
|
tap_read_poll(s, enable);
|
|
|
|
tap_write_poll(s, enable);
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
int tap_get_fd(NetClientState *nc)
|
2010-03-17 14:07:50 +03:00
|
|
|
{
|
|
|
|
TAPState *s = DO_UPCAST(TAPState, nc, nc);
|
2012-07-17 18:17:12 +04:00
|
|
|
assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
|
2010-03-17 14:07:50 +03:00
|
|
|
return s->fd;
|
|
|
|
}
|
|
|
|
|
2009-10-22 20:49:07 +04:00
|
|
|
/* fd support */
|
|
|
|
|
2009-11-25 21:49:04 +03:00
|
|
|
static NetClientInfo net_tap_info = {
|
2012-07-17 18:17:12 +04:00
|
|
|
.type = NET_CLIENT_OPTIONS_KIND_TAP,
|
2009-11-25 21:49:04 +03:00
|
|
|
.size = sizeof(TAPState),
|
|
|
|
.receive = tap_receive,
|
|
|
|
.receive_raw = tap_receive_raw,
|
|
|
|
.receive_iov = tap_receive_iov,
|
2009-12-24 15:46:29 +03:00
|
|
|
.poll = tap_poll,
|
2009-11-25 21:49:04 +03:00
|
|
|
.cleanup = tap_cleanup,
|
|
|
|
};
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static TAPState *net_tap_fd_init(NetClientState *peer,
|
2009-10-22 20:49:07 +04:00
|
|
|
const char *model,
|
|
|
|
const char *name,
|
|
|
|
int fd,
|
|
|
|
int vnet_hdr)
|
|
|
|
{
|
2012-07-24 19:35:13 +04:00
|
|
|
NetClientState *nc;
|
2009-10-22 20:49:07 +04:00
|
|
|
TAPState *s;
|
|
|
|
|
2012-07-24 19:35:08 +04:00
|
|
|
nc = qemu_new_net_client(&net_tap_info, peer, model, name);
|
2009-11-25 21:49:04 +03:00
|
|
|
|
|
|
|
s = DO_UPCAST(TAPState, nc, nc);
|
|
|
|
|
2009-10-22 20:49:07 +04:00
|
|
|
s->fd = fd;
|
2010-07-13 18:55:31 +04:00
|
|
|
s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
|
2009-10-22 20:49:07 +04:00
|
|
|
s->using_vnet_hdr = 0;
|
2009-10-22 20:49:16 +04:00
|
|
|
s->has_ufo = tap_probe_has_ufo(s->fd);
|
2009-11-25 21:49:04 +03:00
|
|
|
tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
|
2009-10-22 20:49:07 +04:00
|
|
|
tap_read_poll(s, 1);
|
2010-03-17 14:08:24 +03:00
|
|
|
s->vhost_net = NULL;
|
2009-10-22 20:49:07 +04:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int launch_script(const char *setup_script, const char *ifname, int fd)
|
|
|
|
{
|
|
|
|
int pid, status;
|
|
|
|
char *args[3];
|
|
|
|
char **parg;
|
|
|
|
|
|
|
|
/* try to launch network script */
|
|
|
|
pid = fork();
|
|
|
|
if (pid == 0) {
|
|
|
|
int open_max = sysconf(_SC_OPEN_MAX), i;
|
|
|
|
|
|
|
|
for (i = 0; i < open_max; i++) {
|
|
|
|
if (i != STDIN_FILENO &&
|
|
|
|
i != STDOUT_FILENO &&
|
|
|
|
i != STDERR_FILENO &&
|
|
|
|
i != fd) {
|
|
|
|
close(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parg = args;
|
|
|
|
*parg++ = (char *)setup_script;
|
|
|
|
*parg++ = (char *)ifname;
|
2010-04-25 22:35:52 +04:00
|
|
|
*parg = NULL;
|
2009-10-22 20:49:07 +04:00
|
|
|
execv(setup_script, args);
|
|
|
|
_exit(1);
|
|
|
|
} else if (pid > 0) {
|
|
|
|
while (waitpid(pid, &status, 0) != pid) {
|
|
|
|
/* loop */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stderr, "%s: could not launch network script\n", setup_script);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
Add support for net bridge
The most common use of -net tap is to connect a tap device to a bridge. This
requires the use of a script and running qemu as root in order to allocate a
tap device to pass to the script.
This model is great for portability and flexibility but it's incredibly
difficult to eliminate the need to run qemu as root. The only really viable
mechanism is to use tunctl to create a tap device, attach it to a bridge as
root, and then hand that tap device to qemu. The problem with this mechanism
is that it requires administrator intervention whenever a user wants to create
a guest.
By essentially writing a helper that implements the most common qemu-ifup
script that can be safely given cap_net_admin, we can dramatically simplify
things for non-privileged users. We still support existing -net tap options
as a mechanism for advanced users and backwards compatibility.
Currently, this is very Linux centric but there's really no reason why it
couldn't be extended for other Unixes.
A typical invocation would be similar to one of the following:
qemu linux.img -net bridge -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper"
-net nic,model=virtio
qemu linux.img -netdev bridge,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
The default bridge that we attach to is br0. The thinking is that a distro
could preconfigure such an interface to allow out-of-the-box bridged networking.
Alternatively, if a user wants to use a different bridge, a typical invocation
would be simliar to one of the following:
qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0"
-net nic,model=virtio
qemu linux.img -netdev bridge,br=qemubr0,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Richa Marwaha <rmarwah@linux.vnet.ibm.com>
Signed-off-by: Corey Bryant <coreyb@linux.vnet.ibm.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
2012-01-26 18:42:27 +04:00
|
|
|
static int recv_fd(int c)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
|
|
|
|
struct msghdr msg = {
|
|
|
|
.msg_control = msgbuf,
|
|
|
|
.msg_controllen = sizeof(msgbuf),
|
|
|
|
};
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
struct iovec iov;
|
|
|
|
uint8_t req[1];
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
cmsg = CMSG_FIRSTHDR(&msg);
|
|
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
|
|
cmsg->cmsg_type = SCM_RIGHTS;
|
|
|
|
cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
|
|
|
|
msg.msg_controllen = cmsg->cmsg_len;
|
|
|
|
|
|
|
|
iov.iov_base = req;
|
|
|
|
iov.iov_len = sizeof(req);
|
|
|
|
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
|
|
|
|
len = recvmsg(c, &msg, 0);
|
|
|
|
if (len > 0) {
|
|
|
|
memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int net_bridge_run_helper(const char *helper, const char *bridge)
|
|
|
|
{
|
|
|
|
sigset_t oldmask, mask;
|
|
|
|
int pid, status;
|
|
|
|
char *args[5];
|
|
|
|
char **parg;
|
|
|
|
int sv[2];
|
|
|
|
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGCHLD);
|
|
|
|
sigprocmask(SIG_BLOCK, &mask, &oldmask);
|
|
|
|
|
|
|
|
if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* try to launch bridge helper */
|
|
|
|
pid = fork();
|
|
|
|
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];
|
|
|
|
|
|
|
|
for (i = 0; i < open_max; i++) {
|
|
|
|
if (i != STDIN_FILENO &&
|
|
|
|
i != STDOUT_FILENO &&
|
|
|
|
i != STDERR_FILENO &&
|
|
|
|
i != sv[1]) {
|
|
|
|
close(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(fd_buf, sizeof(fd_buf), "%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);
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s",
|
|
|
|
helper, "--use-vnet", fd_buf, br_buf);
|
|
|
|
|
|
|
|
parg = args;
|
|
|
|
*parg++ = (char *)"sh";
|
|
|
|
*parg++ = (char *)"-c";
|
|
|
|
*parg++ = helper_cmd;
|
|
|
|
*parg++ = NULL;
|
|
|
|
|
|
|
|
execv("/bin/sh", args);
|
|
|
|
} else {
|
|
|
|
/* assume helper is just the executable path name */
|
|
|
|
|
|
|
|
snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
|
|
|
|
|
|
|
|
parg = args;
|
|
|
|
*parg++ = (char *)helper;
|
|
|
|
*parg++ = (char *)"--use-vnet";
|
|
|
|
*parg++ = fd_buf;
|
|
|
|
*parg++ = br_buf;
|
|
|
|
*parg++ = NULL;
|
|
|
|
|
|
|
|
execv(helper, args);
|
|
|
|
}
|
|
|
|
_exit(1);
|
|
|
|
|
|
|
|
} else if (pid > 0) {
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
close(sv[1]);
|
|
|
|
|
|
|
|
do {
|
|
|
|
fd = recv_fd(sv[0]);
|
|
|
|
} while (fd == -1 && errno == EINTR);
|
|
|
|
|
|
|
|
close(sv[0]);
|
|
|
|
|
|
|
|
while (waitpid(pid, &status, 0) != pid) {
|
|
|
|
/* loop */
|
|
|
|
}
|
|
|
|
sigprocmask(SIG_SETMASK, &oldmask, NULL);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "failed to recv file descriptor\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stderr, "failed to launch bridge helper\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-07-17 18:17:21 +04:00
|
|
|
int net_init_bridge(const NetClientOptions *opts, const char *name,
|
2012-07-24 19:35:13 +04:00
|
|
|
NetClientState *peer)
|
Add support for net bridge
The most common use of -net tap is to connect a tap device to a bridge. This
requires the use of a script and running qemu as root in order to allocate a
tap device to pass to the script.
This model is great for portability and flexibility but it's incredibly
difficult to eliminate the need to run qemu as root. The only really viable
mechanism is to use tunctl to create a tap device, attach it to a bridge as
root, and then hand that tap device to qemu. The problem with this mechanism
is that it requires administrator intervention whenever a user wants to create
a guest.
By essentially writing a helper that implements the most common qemu-ifup
script that can be safely given cap_net_admin, we can dramatically simplify
things for non-privileged users. We still support existing -net tap options
as a mechanism for advanced users and backwards compatibility.
Currently, this is very Linux centric but there's really no reason why it
couldn't be extended for other Unixes.
A typical invocation would be similar to one of the following:
qemu linux.img -net bridge -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper"
-net nic,model=virtio
qemu linux.img -netdev bridge,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
The default bridge that we attach to is br0. The thinking is that a distro
could preconfigure such an interface to allow out-of-the-box bridged networking.
Alternatively, if a user wants to use a different bridge, a typical invocation
would be simliar to one of the following:
qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0"
-net nic,model=virtio
qemu linux.img -netdev bridge,br=qemubr0,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Richa Marwaha <rmarwah@linux.vnet.ibm.com>
Signed-off-by: Corey Bryant <coreyb@linux.vnet.ibm.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
2012-01-26 18:42:27 +04:00
|
|
|
{
|
2012-07-17 18:17:20 +04:00
|
|
|
const NetdevBridgeOptions *bridge;
|
|
|
|
const char *helper, *br;
|
|
|
|
|
Add support for net bridge
The most common use of -net tap is to connect a tap device to a bridge. This
requires the use of a script and running qemu as root in order to allocate a
tap device to pass to the script.
This model is great for portability and flexibility but it's incredibly
difficult to eliminate the need to run qemu as root. The only really viable
mechanism is to use tunctl to create a tap device, attach it to a bridge as
root, and then hand that tap device to qemu. The problem with this mechanism
is that it requires administrator intervention whenever a user wants to create
a guest.
By essentially writing a helper that implements the most common qemu-ifup
script that can be safely given cap_net_admin, we can dramatically simplify
things for non-privileged users. We still support existing -net tap options
as a mechanism for advanced users and backwards compatibility.
Currently, this is very Linux centric but there's really no reason why it
couldn't be extended for other Unixes.
A typical invocation would be similar to one of the following:
qemu linux.img -net bridge -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper"
-net nic,model=virtio
qemu linux.img -netdev bridge,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
The default bridge that we attach to is br0. The thinking is that a distro
could preconfigure such an interface to allow out-of-the-box bridged networking.
Alternatively, if a user wants to use a different bridge, a typical invocation
would be simliar to one of the following:
qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0"
-net nic,model=virtio
qemu linux.img -netdev bridge,br=qemubr0,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Richa Marwaha <rmarwah@linux.vnet.ibm.com>
Signed-off-by: Corey Bryant <coreyb@linux.vnet.ibm.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
2012-01-26 18:42:27 +04:00
|
|
|
TAPState *s;
|
|
|
|
int fd, vnet_hdr;
|
|
|
|
|
2012-07-17 18:17:20 +04:00
|
|
|
assert(opts->kind == NET_CLIENT_OPTIONS_KIND_BRIDGE);
|
|
|
|
bridge = opts->bridge;
|
|
|
|
|
|
|
|
helper = bridge->has_helper ? bridge->helper : DEFAULT_BRIDGE_HELPER;
|
|
|
|
br = bridge->has_br ? bridge->br : DEFAULT_BRIDGE_INTERFACE;
|
Add support for net bridge
The most common use of -net tap is to connect a tap device to a bridge. This
requires the use of a script and running qemu as root in order to allocate a
tap device to pass to the script.
This model is great for portability and flexibility but it's incredibly
difficult to eliminate the need to run qemu as root. The only really viable
mechanism is to use tunctl to create a tap device, attach it to a bridge as
root, and then hand that tap device to qemu. The problem with this mechanism
is that it requires administrator intervention whenever a user wants to create
a guest.
By essentially writing a helper that implements the most common qemu-ifup
script that can be safely given cap_net_admin, we can dramatically simplify
things for non-privileged users. We still support existing -net tap options
as a mechanism for advanced users and backwards compatibility.
Currently, this is very Linux centric but there's really no reason why it
couldn't be extended for other Unixes.
A typical invocation would be similar to one of the following:
qemu linux.img -net bridge -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper"
-net nic,model=virtio
qemu linux.img -netdev bridge,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
The default bridge that we attach to is br0. The thinking is that a distro
could preconfigure such an interface to allow out-of-the-box bridged networking.
Alternatively, if a user wants to use a different bridge, a typical invocation
would be simliar to one of the following:
qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0"
-net nic,model=virtio
qemu linux.img -netdev bridge,br=qemubr0,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Richa Marwaha <rmarwah@linux.vnet.ibm.com>
Signed-off-by: Corey Bryant <coreyb@linux.vnet.ibm.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
2012-01-26 18:42:27 +04:00
|
|
|
|
2012-07-17 18:17:20 +04:00
|
|
|
fd = net_bridge_run_helper(helper, br);
|
Add support for net bridge
The most common use of -net tap is to connect a tap device to a bridge. This
requires the use of a script and running qemu as root in order to allocate a
tap device to pass to the script.
This model is great for portability and flexibility but it's incredibly
difficult to eliminate the need to run qemu as root. The only really viable
mechanism is to use tunctl to create a tap device, attach it to a bridge as
root, and then hand that tap device to qemu. The problem with this mechanism
is that it requires administrator intervention whenever a user wants to create
a guest.
By essentially writing a helper that implements the most common qemu-ifup
script that can be safely given cap_net_admin, we can dramatically simplify
things for non-privileged users. We still support existing -net tap options
as a mechanism for advanced users and backwards compatibility.
Currently, this is very Linux centric but there's really no reason why it
couldn't be extended for other Unixes.
A typical invocation would be similar to one of the following:
qemu linux.img -net bridge -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper"
-net nic,model=virtio
qemu linux.img -netdev bridge,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
The default bridge that we attach to is br0. The thinking is that a distro
could preconfigure such an interface to allow out-of-the-box bridged networking.
Alternatively, if a user wants to use a different bridge, a typical invocation
would be simliar to one of the following:
qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0"
-net nic,model=virtio
qemu linux.img -netdev bridge,br=qemubr0,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Richa Marwaha <rmarwah@linux.vnet.ibm.com>
Signed-off-by: Corey Bryant <coreyb@linux.vnet.ibm.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
2012-01-26 18:42:27 +04:00
|
|
|
if (fd == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fcntl(fd, F_SETFL, O_NONBLOCK);
|
|
|
|
|
|
|
|
vnet_hdr = tap_probe_vnet_hdr(fd);
|
|
|
|
|
2012-07-24 19:35:05 +04:00
|
|
|
s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
|
Add support for net bridge
The most common use of -net tap is to connect a tap device to a bridge. This
requires the use of a script and running qemu as root in order to allocate a
tap device to pass to the script.
This model is great for portability and flexibility but it's incredibly
difficult to eliminate the need to run qemu as root. The only really viable
mechanism is to use tunctl to create a tap device, attach it to a bridge as
root, and then hand that tap device to qemu. The problem with this mechanism
is that it requires administrator intervention whenever a user wants to create
a guest.
By essentially writing a helper that implements the most common qemu-ifup
script that can be safely given cap_net_admin, we can dramatically simplify
things for non-privileged users. We still support existing -net tap options
as a mechanism for advanced users and backwards compatibility.
Currently, this is very Linux centric but there's really no reason why it
couldn't be extended for other Unixes.
A typical invocation would be similar to one of the following:
qemu linux.img -net bridge -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper"
-net nic,model=virtio
qemu linux.img -netdev bridge,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
The default bridge that we attach to is br0. The thinking is that a distro
could preconfigure such an interface to allow out-of-the-box bridged networking.
Alternatively, if a user wants to use a different bridge, a typical invocation
would be simliar to one of the following:
qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0"
-net nic,model=virtio
qemu linux.img -netdev bridge,br=qemubr0,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Richa Marwaha <rmarwah@linux.vnet.ibm.com>
Signed-off-by: Corey Bryant <coreyb@linux.vnet.ibm.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
2012-01-26 18:42:27 +04:00
|
|
|
if (!s) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-07-17 18:17:20 +04:00
|
|
|
snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper,
|
|
|
|
br);
|
Add support for net bridge
The most common use of -net tap is to connect a tap device to a bridge. This
requires the use of a script and running qemu as root in order to allocate a
tap device to pass to the script.
This model is great for portability and flexibility but it's incredibly
difficult to eliminate the need to run qemu as root. The only really viable
mechanism is to use tunctl to create a tap device, attach it to a bridge as
root, and then hand that tap device to qemu. The problem with this mechanism
is that it requires administrator intervention whenever a user wants to create
a guest.
By essentially writing a helper that implements the most common qemu-ifup
script that can be safely given cap_net_admin, we can dramatically simplify
things for non-privileged users. We still support existing -net tap options
as a mechanism for advanced users and backwards compatibility.
Currently, this is very Linux centric but there's really no reason why it
couldn't be extended for other Unixes.
A typical invocation would be similar to one of the following:
qemu linux.img -net bridge -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper"
-net nic,model=virtio
qemu linux.img -netdev bridge,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
The default bridge that we attach to is br0. The thinking is that a distro
could preconfigure such an interface to allow out-of-the-box bridged networking.
Alternatively, if a user wants to use a different bridge, a typical invocation
would be simliar to one of the following:
qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0"
-net nic,model=virtio
qemu linux.img -netdev bridge,br=qemubr0,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Richa Marwaha <rmarwah@linux.vnet.ibm.com>
Signed-off-by: Corey Bryant <coreyb@linux.vnet.ibm.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
2012-01-26 18:42:27 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-17 18:17:19 +04:00
|
|
|
static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
|
|
|
|
const char *setup_script, char *ifname,
|
|
|
|
size_t ifname_sz)
|
2009-10-22 20:49:07 +04:00
|
|
|
{
|
|
|
|
int fd, vnet_hdr_required;
|
|
|
|
|
2012-07-17 18:17:19 +04:00
|
|
|
if (tap->has_ifname) {
|
|
|
|
pstrcpy(ifname, ifname_sz, tap->ifname);
|
|
|
|
} else {
|
|
|
|
assert(ifname_sz > 0);
|
|
|
|
ifname[0] = '\0';
|
2009-10-22 20:49:07 +04:00
|
|
|
}
|
|
|
|
|
2012-07-17 18:17:19 +04:00
|
|
|
if (tap->has_vnet_hdr) {
|
|
|
|
*vnet_hdr = tap->vnet_hdr;
|
2009-10-22 20:49:07 +04:00
|
|
|
vnet_hdr_required = *vnet_hdr;
|
|
|
|
} else {
|
2012-07-17 18:17:19 +04:00
|
|
|
*vnet_hdr = 1;
|
2009-10-22 20:49:07 +04:00
|
|
|
vnet_hdr_required = 0;
|
|
|
|
}
|
|
|
|
|
2012-07-17 18:17:19 +04:00
|
|
|
TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required));
|
2009-10-22 20:49:07 +04:00
|
|
|
if (fd < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setup_script &&
|
|
|
|
setup_script[0] != '\0' &&
|
|
|
|
strcmp(setup_script, "no") != 0 &&
|
|
|
|
launch_script(setup_script, ifname, fd)) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2012-07-17 18:17:21 +04:00
|
|
|
int net_init_tap(const NetClientOptions *opts, const char *name,
|
2012-07-24 19:35:13 +04:00
|
|
|
NetClientState *peer)
|
2009-10-22 20:49:07 +04:00
|
|
|
{
|
2012-07-17 18:17:19 +04:00
|
|
|
const NetdevTapOptions *tap;
|
|
|
|
|
2009-11-25 21:49:36 +03:00
|
|
|
int fd, vnet_hdr = 0;
|
Add support for net bridge
The most common use of -net tap is to connect a tap device to a bridge. This
requires the use of a script and running qemu as root in order to allocate a
tap device to pass to the script.
This model is great for portability and flexibility but it's incredibly
difficult to eliminate the need to run qemu as root. The only really viable
mechanism is to use tunctl to create a tap device, attach it to a bridge as
root, and then hand that tap device to qemu. The problem with this mechanism
is that it requires administrator intervention whenever a user wants to create
a guest.
By essentially writing a helper that implements the most common qemu-ifup
script that can be safely given cap_net_admin, we can dramatically simplify
things for non-privileged users. We still support existing -net tap options
as a mechanism for advanced users and backwards compatibility.
Currently, this is very Linux centric but there's really no reason why it
couldn't be extended for other Unixes.
A typical invocation would be similar to one of the following:
qemu linux.img -net bridge -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper"
-net nic,model=virtio
qemu linux.img -netdev bridge,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
The default bridge that we attach to is br0. The thinking is that a distro
could preconfigure such an interface to allow out-of-the-box bridged networking.
Alternatively, if a user wants to use a different bridge, a typical invocation
would be simliar to one of the following:
qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0"
-net nic,model=virtio
qemu linux.img -netdev bridge,br=qemubr0,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Richa Marwaha <rmarwah@linux.vnet.ibm.com>
Signed-off-by: Corey Bryant <coreyb@linux.vnet.ibm.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
2012-01-26 18:42:27 +04:00
|
|
|
const char *model;
|
2012-07-17 18:17:19 +04:00
|
|
|
TAPState *s;
|
|
|
|
|
|
|
|
/* for the no-fd, no-helper case */
|
|
|
|
const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */
|
|
|
|
char ifname[128];
|
|
|
|
|
|
|
|
assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP);
|
|
|
|
tap = opts->tap;
|
2009-10-22 20:49:07 +04:00
|
|
|
|
2012-07-17 18:17:19 +04:00
|
|
|
if (tap->has_fd) {
|
|
|
|
if (tap->has_ifname || tap->has_script || tap->has_downscript ||
|
|
|
|
tap->has_vnet_hdr || tap->has_helper) {
|
Add support for net bridge
The most common use of -net tap is to connect a tap device to a bridge. This
requires the use of a script and running qemu as root in order to allocate a
tap device to pass to the script.
This model is great for portability and flexibility but it's incredibly
difficult to eliminate the need to run qemu as root. The only really viable
mechanism is to use tunctl to create a tap device, attach it to a bridge as
root, and then hand that tap device to qemu. The problem with this mechanism
is that it requires administrator intervention whenever a user wants to create
a guest.
By essentially writing a helper that implements the most common qemu-ifup
script that can be safely given cap_net_admin, we can dramatically simplify
things for non-privileged users. We still support existing -net tap options
as a mechanism for advanced users and backwards compatibility.
Currently, this is very Linux centric but there's really no reason why it
couldn't be extended for other Unixes.
A typical invocation would be similar to one of the following:
qemu linux.img -net bridge -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper"
-net nic,model=virtio
qemu linux.img -netdev bridge,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
The default bridge that we attach to is br0. The thinking is that a distro
could preconfigure such an interface to allow out-of-the-box bridged networking.
Alternatively, if a user wants to use a different bridge, a typical invocation
would be simliar to one of the following:
qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0"
-net nic,model=virtio
qemu linux.img -netdev bridge,br=qemubr0,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Richa Marwaha <rmarwah@linux.vnet.ibm.com>
Signed-off-by: Corey Bryant <coreyb@linux.vnet.ibm.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
2012-01-26 18:42:27 +04:00
|
|
|
error_report("ifname=, script=, downscript=, vnet_hdr=, "
|
|
|
|
"and helper= are invalid with fd=");
|
2009-10-22 20:49:07 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-08-22 00:52:07 +04:00
|
|
|
fd = monitor_handle_fd_param(cur_mon, tap->fd);
|
2009-10-22 20:49:07 +04:00
|
|
|
if (fd == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fcntl(fd, F_SETFL, O_NONBLOCK);
|
|
|
|
|
|
|
|
vnet_hdr = tap_probe_vnet_hdr(fd);
|
Add support for net bridge
The most common use of -net tap is to connect a tap device to a bridge. This
requires the use of a script and running qemu as root in order to allocate a
tap device to pass to the script.
This model is great for portability and flexibility but it's incredibly
difficult to eliminate the need to run qemu as root. The only really viable
mechanism is to use tunctl to create a tap device, attach it to a bridge as
root, and then hand that tap device to qemu. The problem with this mechanism
is that it requires administrator intervention whenever a user wants to create
a guest.
By essentially writing a helper that implements the most common qemu-ifup
script that can be safely given cap_net_admin, we can dramatically simplify
things for non-privileged users. We still support existing -net tap options
as a mechanism for advanced users and backwards compatibility.
Currently, this is very Linux centric but there's really no reason why it
couldn't be extended for other Unixes.
A typical invocation would be similar to one of the following:
qemu linux.img -net bridge -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper"
-net nic,model=virtio
qemu linux.img -netdev bridge,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
The default bridge that we attach to is br0. The thinking is that a distro
could preconfigure such an interface to allow out-of-the-box bridged networking.
Alternatively, if a user wants to use a different bridge, a typical invocation
would be simliar to one of the following:
qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0"
-net nic,model=virtio
qemu linux.img -netdev bridge,br=qemubr0,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Richa Marwaha <rmarwah@linux.vnet.ibm.com>
Signed-off-by: Corey Bryant <coreyb@linux.vnet.ibm.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
2012-01-26 18:42:27 +04:00
|
|
|
|
|
|
|
model = "tap";
|
|
|
|
|
2012-07-17 18:17:19 +04:00
|
|
|
} else if (tap->has_helper) {
|
|
|
|
if (tap->has_ifname || tap->has_script || tap->has_downscript ||
|
|
|
|
tap->has_vnet_hdr) {
|
Add support for net bridge
The most common use of -net tap is to connect a tap device to a bridge. This
requires the use of a script and running qemu as root in order to allocate a
tap device to pass to the script.
This model is great for portability and flexibility but it's incredibly
difficult to eliminate the need to run qemu as root. The only really viable
mechanism is to use tunctl to create a tap device, attach it to a bridge as
root, and then hand that tap device to qemu. The problem with this mechanism
is that it requires administrator intervention whenever a user wants to create
a guest.
By essentially writing a helper that implements the most common qemu-ifup
script that can be safely given cap_net_admin, we can dramatically simplify
things for non-privileged users. We still support existing -net tap options
as a mechanism for advanced users and backwards compatibility.
Currently, this is very Linux centric but there's really no reason why it
couldn't be extended for other Unixes.
A typical invocation would be similar to one of the following:
qemu linux.img -net bridge -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper"
-net nic,model=virtio
qemu linux.img -netdev bridge,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
The default bridge that we attach to is br0. The thinking is that a distro
could preconfigure such an interface to allow out-of-the-box bridged networking.
Alternatively, if a user wants to use a different bridge, a typical invocation
would be simliar to one of the following:
qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0"
-net nic,model=virtio
qemu linux.img -netdev bridge,br=qemubr0,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Richa Marwaha <rmarwah@linux.vnet.ibm.com>
Signed-off-by: Corey Bryant <coreyb@linux.vnet.ibm.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
2012-01-26 18:42:27 +04:00
|
|
|
error_report("ifname=, script=, downscript=, and vnet_hdr= "
|
|
|
|
"are invalid with helper=");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-07-17 18:17:19 +04:00
|
|
|
fd = net_bridge_run_helper(tap->helper, DEFAULT_BRIDGE_INTERFACE);
|
Add support for net bridge
The most common use of -net tap is to connect a tap device to a bridge. This
requires the use of a script and running qemu as root in order to allocate a
tap device to pass to the script.
This model is great for portability and flexibility but it's incredibly
difficult to eliminate the need to run qemu as root. The only really viable
mechanism is to use tunctl to create a tap device, attach it to a bridge as
root, and then hand that tap device to qemu. The problem with this mechanism
is that it requires administrator intervention whenever a user wants to create
a guest.
By essentially writing a helper that implements the most common qemu-ifup
script that can be safely given cap_net_admin, we can dramatically simplify
things for non-privileged users. We still support existing -net tap options
as a mechanism for advanced users and backwards compatibility.
Currently, this is very Linux centric but there's really no reason why it
couldn't be extended for other Unixes.
A typical invocation would be similar to one of the following:
qemu linux.img -net bridge -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper"
-net nic,model=virtio
qemu linux.img -netdev bridge,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
The default bridge that we attach to is br0. The thinking is that a distro
could preconfigure such an interface to allow out-of-the-box bridged networking.
Alternatively, if a user wants to use a different bridge, a typical invocation
would be simliar to one of the following:
qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0"
-net nic,model=virtio
qemu linux.img -netdev bridge,br=qemubr0,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Richa Marwaha <rmarwah@linux.vnet.ibm.com>
Signed-off-by: Corey Bryant <coreyb@linux.vnet.ibm.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
2012-01-26 18:42:27 +04:00
|
|
|
if (fd == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fcntl(fd, F_SETFL, O_NONBLOCK);
|
|
|
|
|
|
|
|
vnet_hdr = tap_probe_vnet_hdr(fd);
|
|
|
|
|
|
|
|
model = "bridge";
|
|
|
|
|
2009-10-22 20:49:07 +04:00
|
|
|
} else {
|
2012-07-17 18:17:19 +04:00
|
|
|
script = tap->has_script ? tap->script : DEFAULT_NETWORK_SCRIPT;
|
|
|
|
fd = net_tap_init(tap, &vnet_hdr, script, ifname, sizeof ifname);
|
2009-11-21 01:23:03 +03:00
|
|
|
if (fd == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
Add support for net bridge
The most common use of -net tap is to connect a tap device to a bridge. This
requires the use of a script and running qemu as root in order to allocate a
tap device to pass to the script.
This model is great for portability and flexibility but it's incredibly
difficult to eliminate the need to run qemu as root. The only really viable
mechanism is to use tunctl to create a tap device, attach it to a bridge as
root, and then hand that tap device to qemu. The problem with this mechanism
is that it requires administrator intervention whenever a user wants to create
a guest.
By essentially writing a helper that implements the most common qemu-ifup
script that can be safely given cap_net_admin, we can dramatically simplify
things for non-privileged users. We still support existing -net tap options
as a mechanism for advanced users and backwards compatibility.
Currently, this is very Linux centric but there's really no reason why it
couldn't be extended for other Unixes.
A typical invocation would be similar to one of the following:
qemu linux.img -net bridge -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper"
-net nic,model=virtio
qemu linux.img -netdev bridge,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
The default bridge that we attach to is br0. The thinking is that a distro
could preconfigure such an interface to allow out-of-the-box bridged networking.
Alternatively, if a user wants to use a different bridge, a typical invocation
would be simliar to one of the following:
qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
qemu linux.img -net tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0"
-net nic,model=virtio
qemu linux.img -netdev bridge,br=qemubr0,id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
qemu linux.img -netdev tap,helper="/usr/local/libexec/qemu-bridge-helper --br=qemubr0",id=hn0
-device virtio-net-pci,netdev=hn0,id=nic1
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Richa Marwaha <rmarwah@linux.vnet.ibm.com>
Signed-off-by: Corey Bryant <coreyb@linux.vnet.ibm.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
2012-01-26 18:42:27 +04:00
|
|
|
|
|
|
|
model = "tap";
|
2009-10-22 20:49:07 +04:00
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:05 +04:00
|
|
|
s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
|
2009-10-22 20:49:07 +04:00
|
|
|
if (!s) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-07-17 18:17:19 +04:00
|
|
|
if (tap_set_sndbuf(s->fd, tap) < 0) {
|
2009-10-22 20:49:07 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-07-17 18:17:19 +04:00
|
|
|
if (tap->has_fd) {
|
2009-11-25 21:49:04 +03:00
|
|
|
snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
|
2012-07-17 18:17:19 +04:00
|
|
|
} else if (tap->has_helper) {
|
|
|
|
snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s",
|
|
|
|
tap->helper);
|
2009-10-22 20:49:07 +04:00
|
|
|
} else {
|
2012-07-17 18:17:19 +04:00
|
|
|
const char *downscript;
|
2009-10-22 20:49:07 +04:00
|
|
|
|
2012-07-17 18:17:19 +04:00
|
|
|
downscript = tap->has_downscript ? tap->downscript :
|
|
|
|
DEFAULT_NETWORK_DOWN_SCRIPT;
|
2009-10-22 20:49:07 +04:00
|
|
|
|
2009-11-25 21:49:04 +03:00
|
|
|
snprintf(s->nc.info_str, sizeof(s->nc.info_str),
|
2012-07-17 18:17:19 +04:00
|
|
|
"ifname=%s,script=%s,downscript=%s", ifname, script,
|
|
|
|
downscript);
|
2009-10-22 20:49:07 +04:00
|
|
|
|
|
|
|
if (strcmp(downscript, "no") != 0) {
|
|
|
|
snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
|
|
|
|
snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-17 18:17:19 +04:00
|
|
|
if (tap->has_vhost ? tap->vhost :
|
|
|
|
tap->has_vhostfd || (tap->has_vhostforce && tap->vhostforce)) {
|
|
|
|
int vhostfd;
|
|
|
|
|
|
|
|
if (tap->has_vhostfd) {
|
2012-08-22 00:52:07 +04:00
|
|
|
vhostfd = monitor_handle_fd_param(cur_mon, tap->vhostfd);
|
2012-07-17 18:17:19 +04:00
|
|
|
if (vhostfd == -1) {
|
2010-03-17 14:08:24 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vhostfd = -1;
|
|
|
|
}
|
2012-07-17 18:17:19 +04:00
|
|
|
|
|
|
|
s->vhost_net = vhost_net_init(&s->nc, vhostfd,
|
|
|
|
tap->has_vhostforce && tap->vhostforce);
|
2010-03-17 14:08:24 +03:00
|
|
|
if (!s->vhost_net) {
|
|
|
|
error_report("vhost-net requested but could not be initialized");
|
|
|
|
return -1;
|
|
|
|
}
|
2012-07-17 18:17:19 +04:00
|
|
|
} else if (tap->has_vhostfd) {
|
2010-03-17 14:08:24 +03:00
|
|
|
error_report("vhostfd= is not valid without vhost");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-10-22 20:49:07 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2010-03-17 14:08:38 +03:00
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
VHostNetState *tap_get_vhost_net(NetClientState *nc)
|
2010-03-17 14:08:38 +03:00
|
|
|
{
|
|
|
|
TAPState *s = DO_UPCAST(TAPState, nc, nc);
|
2012-07-17 18:17:12 +04:00
|
|
|
assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
|
2010-03-17 14:08:38 +03:00
|
|
|
return s->vhost_net;
|
|
|
|
}
|