@@ -7,6 +7,7 @@ struct virtio_net_parameters {
struct kvm *self;
const char *host_ip;
char guest_mac[6];
+ const char *script;
};
void virtio_net__init(const struct virtio_net_parameters *params);
@@ -33,6 +33,7 @@
#define DEFAULT_NETWORK "virtio"
#define DEFAULT_HOST_ADDR "192.168.33.2"
#define DEFAULT_GUEST_MAC "00:11:22:33:44:55"
+#define DEFAULT_SCRIPT "none"
#define MB_SHIFT (20)
#define MIN_RAM_SIZE_MB (64ULL)
@@ -65,6 +66,7 @@ static const char *kvm_dev;
static const char *network;
static const char *host_ip_addr;
static const char *guest_mac;
+static const char *script;
static bool single_step;
static bool readonly_image;
extern bool ioport_debug;
@@ -102,6 +104,8 @@ static const struct option options[] = {
"Assign this address to the host side networking"),
OPT_STRING('\0', "guest-mac", &guest_mac, "aa:bb:cc:dd:ee:ff",
"Assign this address to the guest side NIC"),
+ OPT_STRING('\0', "tapscript", &script, "Script path",
+ "Assign a script to process created tap device"),
OPT_GROUP("Debug options:"),
OPT_STRING('d', "kvm-dev", &kvm_dev, "kvm-dev", "KVM device file"),
OPT_BOOLEAN('s', "single-step", &single_step,
@@ -277,6 +281,9 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
if (!guest_mac)
guest_mac = DEFAULT_GUEST_MAC;
+ if (!script)
+ script = DEFAULT_SCRIPT;
+
term_init();
kvm = kvm__init(kvm_dev, ram_size);
@@ -320,7 +327,8 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
if (!strncmp(network, "virtio", 6)) {
net_params = (struct virtio_net_parameters) {
.host_ip = host_ip_addr,
- .self = kvm
+ .self = kvm,
+ .script = script
};
sscanf(guest_mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
net_params.guest_mac,
@@ -17,6 +17,8 @@
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
+#include <unistd.h>
+#include <sys/wait.h>
#define VIRTIO_NET_IRQ 14
#define VIRTIO_NET_QUEUE_SIZE 128
@@ -280,7 +282,7 @@ static bool virtio_net__tap_init(const struct virtio_net_parameters *params)
{
struct ifreq ifr;
int sock = socket(AF_INET, SOCK_STREAM, 0);
- int i;
+ int i, pid, status;
struct sockaddr_in sin = {0};
for (i = 0 ; i < 6 ; i++)
@@ -304,18 +306,31 @@ static bool virtio_net__tap_init(const struct virtio_net_parameters *params)
ioctl(net_device.tap_fd, TUNSETNOCSUM, 1);
+ if (strcmp(params->script, "none")) {
+ pid = fork();
+ if (pid == 0) {
+ execl(params->script, params->script, net_device.tap_name, NULL);
+ _exit(1);
+ } else {
+ waitpid(pid, &status, 0);
+ if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
+ warning("Fail to setup tap by %s", params->script);
+ goto fail;
+ }
+ }
+ } else {
+ memset(&ifr, 0, sizeof(ifr));
- memset(&ifr, 0, sizeof(ifr));
-
- strncpy(ifr.ifr_name, net_device.tap_name, sizeof(net_device.tap_name));
+ strncpy(ifr.ifr_name, net_device.tap_name, sizeof(net_device.tap_name));
- sin.sin_addr.s_addr = inet_addr(params->host_ip);
- memcpy(&(ifr.ifr_addr), &sin, sizeof(ifr.ifr_addr));
- ifr.ifr_addr.sa_family = AF_INET;
+ sin.sin_addr.s_addr = inet_addr(params->host_ip);
+ memcpy(&(ifr.ifr_addr), &sin, sizeof(ifr.ifr_addr));
+ ifr.ifr_addr.sa_family = AF_INET;
- if (ioctl(sock, SIOCSIFADDR, &ifr) < 0) {
- warning("Can not set ip address on tap device");
- goto fail;
+ if (ioctl(sock, SIOCSIFADDR, &ifr) < 0) {
+ warning("Can not set ip address on tap device");
+ goto fail;
+ }
}
memset(&ifr, 0, sizeof(ifr));
Use original hardcode network by default. #./kvm run ... -n virtio --tapscript=./util/kvm-ifup-vbr0 # brctl show bridge name bridge id STP enabled interfaces vbr0 8000.e272c7c391f4 no tap0 guest)# ifconfig eth6 eth6 Link encap:Ethernet HWaddr 00:11:22:33:44:55 inet addr:192.168.33.192 Bcast:192.168.33.255 Mask:255.255.255.0 inet6 addr: fe80::211:22ff:fe33:4455/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:22 errors:0 dropped:0 overruns:0 frame:0 TX packets:8 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:3725 (3.6 KiB) TX bytes:852 (852.0 b) guest)# ping amosk.info PING amosk.info (69.175.108.82) 56(84) bytes of data. 64 bytes from nurpulat.uz (69.175.108.82): icmp_seq=1 ttl=43 time=306 ms Changes from v1: - rebased to latest tree - replace system() by execv() Signed-off-by: Amos Kong <kongjianjun@gmail.com> --- tools/kvm/include/kvm/virtio-net.h | 1 + tools/kvm/kvm-run.c | 10 +++++++++- tools/kvm/virtio-net.c | 35 +++++++++++++++++++++++++---------- 3 files changed, 35 insertions(+), 11 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html