diff mbox

[3/3] kvm tools: Setup bridged network by a script

Message ID 20110414043803.9279.11700.stgit@localhost6.localdomain6 (mailing list archive)
State New, archived
Headers show

Commit Message

Amos Jianjun Kong April 14, 2011, 4:38 a.m. UTC
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

Signed-off-by: Amos Kong <kongjianjun@gmail.com>
---
 tools/kvm/include/kvm/virtio-net.h |    2 +-
 tools/kvm/kvm-run.c                |    9 ++++++++-
 tools/kvm/virtio-net.c             |   14 ++++++++++----
 3 files changed, 19 insertions(+), 6 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
diff mbox

Patch

diff --git a/tools/kvm/include/kvm/virtio-net.h b/tools/kvm/include/kvm/virtio-net.h
index a1cab15..95bf049 100644
--- a/tools/kvm/include/kvm/virtio-net.h
+++ b/tools/kvm/include/kvm/virtio-net.h
@@ -2,6 +2,6 @@ 
 #define KVM__VIRTIO_NET_H
 
 struct kvm;
-void virtio_net__init(struct kvm *self);
+void virtio_net__init(struct kvm *self, const char *script);
 
 #endif /* KVM__VIRTIO_NET_H */
diff --git a/tools/kvm/kvm-run.c b/tools/kvm/kvm-run.c
index 6046a0a..9ff3a7d 100644
--- a/tools/kvm/kvm-run.c
+++ b/tools/kvm/kvm-run.c
@@ -31,6 +31,7 @@ 
 #define DEFAULT_KVM_DEV		"/dev/kvm"
 #define DEFAULT_CONSOLE		"serial"
 #define DEFAULT_NETWORK		"none"
+#define DEFAULT_SCRIPT		"none"
 
 #define MB_SHIFT		(20)
 #define MIN_RAM_SIZE_MB		(64ULL)
@@ -66,6 +67,7 @@  static const char *image_filename;
 static const char *console;
 static const char *kvm_dev;
 static const char *network;
+static const char *script;
 static bool single_step;
 static bool readonly_image;
 extern bool ioport_debug;
@@ -89,6 +91,8 @@  static const struct option options[] = {
 			"Console to use"),
 	OPT_STRING('n', "network", &network, "virtio",
 			"Network to use"),
+	OPT_STRING('\0', "tapscript", &script, "Script path",
+			 "Assign a script to process created tap device"),
 
 	OPT_GROUP("Kernel options:"),
 	OPT_STRING('k', "kernel", &kernel_filename, "kernel",
@@ -218,6 +222,9 @@  int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 	else
 		active_console  = CONSOLE_8250;
 
+	if (!script)
+		script = DEFAULT_SCRIPT;
+
 	term_init();
 
 	kvm = kvm__init(kvm_dev, ram_size);
@@ -259,7 +266,7 @@  int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 		network = DEFAULT_NETWORK;
 
 	if (!strncmp(network, "virtio", 6))
-		virtio_net__init(kvm);
+		virtio_net__init(kvm, script);
 
 	kvm__start_timer(kvm);
 
diff --git a/tools/kvm/virtio-net.c b/tools/kvm/virtio-net.c
index ec70d5c..7e9c508 100644
--- a/tools/kvm/virtio-net.c
+++ b/tools/kvm/virtio-net.c
@@ -273,9 +273,10 @@  static struct pci_device_header virtio_net_pci_device = {
 	.irq_line		= VIRTIO_NET_IRQ,
 };
 
-static void virtio_net__tap_init(void)
+static void virtio_net__tap_init(const char* script)
 {
 	struct ifreq ifr;
+	char cmd[PATH_MAX];
 
 	net_device.tap_fd = open("/dev/net/tun", O_RDWR);
 	if (net_device.tap_fd < 0)
@@ -291,8 +292,13 @@  static void virtio_net__tap_init(void)
 
 	ioctl(net_device.tap_fd, TUNSETNOCSUM, 1);
 
+
+	if (strcmp(script, "none")) {
+		sprintf(cmd, "%s tap0", script);
+		if (system(cmd) < 0)
+			warning("Fail to setup tap by %s", script);
+	} else if (system("ifconfig tap0 192.168.33.2") < 0)
 	/*FIXME: Remove this after user can specify ip address and netmask*/
-	if (system("ifconfig tap0 192.168.33.2") < 0)
 		warning("Can not set ip address on tap0");
 }
 
@@ -308,11 +314,11 @@  static void virtio_net__io_thread_init(struct kvm *self)
 	pthread_create(&net_device.io_tx_thread, NULL, virtio_net_tx_thread, (void *)self);
 }
 
-void virtio_net__init(struct kvm *self)
+void virtio_net__init(struct kvm *self, const char *script)
 {
 	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();
+	virtio_net__tap_init(script);
 	virtio_net__io_thread_init(self);
 }