mirror of
https://github.com/clearlinux/kvmtool.git
synced 2026-09-04 04:31:27 +00:00
kvm tools: Enable network by default
Enable virtio networking by default, warn if user doesn't have tun/tap interface or not enough permissions to start it. Signed-off-by: Sasha Levin <levinsasha928@gmail.com> Signed-off-by: Pekka Enberg <penberg@kernel.org>
This commit is contained in:
@@ -30,7 +30,7 @@
|
||||
|
||||
#define DEFAULT_KVM_DEV "/dev/kvm"
|
||||
#define DEFAULT_CONSOLE "serial"
|
||||
#define DEFAULT_NETWORK "none"
|
||||
#define DEFAULT_NETWORK "virtio"
|
||||
#define DEFAULT_HOST_ADDR "192.168.33.2"
|
||||
|
||||
#define MB_SHIFT (20)
|
||||
@@ -311,7 +311,7 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
|
||||
if (!network)
|
||||
network = DEFAULT_NETWORK;
|
||||
|
||||
if (!strncmp(network, "virtio", 6)) {
|
||||
if (!strncmp(network, "virtio", 6)) {
|
||||
net_params = (struct virtio_net_parameters) {
|
||||
.host_ip = host_ip_addr,
|
||||
.self = kvm
|
||||
|
||||
+27
-10
@@ -276,21 +276,25 @@ static struct pci_device_header virtio_net_pci_device = {
|
||||
.irq_line = VIRTIO_NET_IRQ,
|
||||
};
|
||||
|
||||
static void virtio_net__tap_init(const struct virtio_net_parameters *params)
|
||||
static bool virtio_net__tap_init(const struct virtio_net_parameters *params)
|
||||
{
|
||||
struct ifreq ifr;
|
||||
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
struct sockaddr_in sin = {0};
|
||||
|
||||
net_device.tap_fd = open("/dev/net/tun", O_RDWR);
|
||||
if (net_device.tap_fd < 0)
|
||||
die("Unable to open /dev/net/tun\n");
|
||||
if (net_device.tap_fd < 0) {
|
||||
warning("Unable to open /dev/net/tun\n");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
|
||||
|
||||
if (ioctl(net_device.tap_fd, TUNSETIFF, &ifr) < 0)
|
||||
die("Config tap device error. Are you root?");
|
||||
if (ioctl(net_device.tap_fd, TUNSETIFF, &ifr) < 0) {
|
||||
warning("Config tap device error. Are you root?");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
strncpy(net_device.tap_name, ifr.ifr_name, sizeof(net_device.tap_name));
|
||||
|
||||
@@ -305,8 +309,10 @@ static void virtio_net__tap_init(const struct virtio_net_parameters *params)
|
||||
memcpy(&(ifr.ifr_addr), &sin, sizeof(ifr.ifr_addr));
|
||||
ifr.ifr_addr.sa_family = AF_INET;
|
||||
|
||||
if (ioctl(sock, SIOCSIFADDR, &ifr) < 0)
|
||||
if (ioctl(sock, SIOCSIFADDR, &ifr) < 0) {
|
||||
warning("Can not set ip address on tap device");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
strncpy(ifr.ifr_name, net_device.tap_name, sizeof(net_device.tap_name));
|
||||
@@ -316,6 +322,16 @@ static void virtio_net__tap_init(const struct virtio_net_parameters *params)
|
||||
warning("Could not bring tap device up");
|
||||
|
||||
close(sock);
|
||||
|
||||
return 1;
|
||||
|
||||
fail:
|
||||
if (sock >= 0)
|
||||
close(sock);
|
||||
if (net_device.tap_fd >= 0)
|
||||
close(net_device.tap_fd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void virtio_net__io_thread_init(struct kvm *self)
|
||||
@@ -332,9 +348,10 @@ static void virtio_net__io_thread_init(struct kvm *self)
|
||||
|
||||
void virtio_net__init(const struct virtio_net_parameters *params)
|
||||
{
|
||||
pci__register(&virtio_net_pci_device, PCI_VIRTIO_NET_DEVNUM);
|
||||
ioport__register(IOPORT_VIRTIO_NET, &virtio_net_io_ops, IOPORT_VIRTIO_NET_SIZE);
|
||||
if (virtio_net__tap_init(params)) {
|
||||
pci__register(&virtio_net_pci_device, PCI_VIRTIO_NET_DEVNUM);
|
||||
ioport__register(IOPORT_VIRTIO_NET, &virtio_net_io_ops, IOPORT_VIRTIO_NET_SIZE);
|
||||
|
||||
virtio_net__tap_init(params);
|
||||
virtio_net__io_thread_init(params->self);
|
||||
virtio_net__io_thread_init(params->self);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user