From patchwork Thu Apr 14 04:38:03 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amos Jianjun Kong X-Patchwork-Id: 706201 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p3E4cXhH027911 for ; Thu, 14 Apr 2011 04:38:33 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751761Ab1DNEi3 (ORCPT ); Thu, 14 Apr 2011 00:38:29 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37132 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751617Ab1DNEi0 (ORCPT ); Thu, 14 Apr 2011 00:38:26 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p3E4c5ra032220 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 14 Apr 2011 00:38:06 -0400 Received: from localhost6.localdomain6 (unused-65-224.nay.redhat.com [10.66.65.224] (may be forged)) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p3E4c3dj016466; Thu, 14 Apr 2011 00:38:04 -0400 Subject: [PATCH 3/3] kvm tools: Setup bridged network by a script To: kvm@vger.kernel.org From: Amos Kong Cc: penberg@kernel.org, asias.hejun@gmail.com, sirouni@gmail.com Date: Thu, 14 Apr 2011 12:38:03 +0800 Message-ID: <20110414043803.9279.11700.stgit@localhost6.localdomain6> In-Reply-To: <20110414043745.9279.89159.stgit@localhost6.localdomain6> References: <20110414043745.9279.89159.stgit@localhost6.localdomain6> User-Agent: StGit/0.15 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Thu, 14 Apr 2011 04:38:33 +0000 (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 --- 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 --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); }