From patchwork Fri Apr 15 10:19:34 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: 710491 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 p3FAJhGp017815 for ; Fri, 15 Apr 2011 10:19:43 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754046Ab1DOKTk (ORCPT ); Fri, 15 Apr 2011 06:19:40 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49462 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753981Ab1DOKTj (ORCPT ); Fri, 15 Apr 2011 06:19:39 -0400 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p3FAJbH1020532 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 15 Apr 2011 06:19:37 -0400 Received: from localhost6.localdomain6 (unused-65-224.nay.redhat.com [10.66.65.224] (may be forged)) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p3FAJYPC005404; Fri, 15 Apr 2011 06:19:35 -0400 Subject: [PATCH 3/3 v2] 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: Fri, 15 Apr 2011 18:19:34 +0800 Message-ID: <20110415101933.11014.83707.stgit@localhost6.localdomain6> In-Reply-To: <20110414043803.9279.11700.stgit@localhost6.localdomain6> References: <20110414043803.9279.11700.stgit@localhost6.localdomain6> User-Agent: StGit/0.15 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25 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]); Fri, 15 Apr 2011 10:19:43 +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 Changes from v1: - rebased to latest tree - replace system() by execv() Signed-off-by: Amos Kong --- 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 diff --git a/tools/kvm/include/kvm/virtio-net.h b/tools/kvm/include/kvm/virtio-net.h index c889854..959ccb5 100644 --- a/tools/kvm/include/kvm/virtio-net.h +++ b/tools/kvm/include/kvm/virtio-net.h @@ -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); diff --git a/tools/kvm/kvm-run.c b/tools/kvm/kvm-run.c index 4007402..80e1a6c 100644 --- a/tools/kvm/kvm-run.c +++ b/tools/kvm/kvm-run.c @@ -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, diff --git a/tools/kvm/virtio-net.c b/tools/kvm/virtio-net.c index 8d08272..f8d7276 100644 --- a/tools/kvm/virtio-net.c +++ b/tools/kvm/virtio-net.c @@ -17,6 +17,8 @@ #include #include #include +#include +#include #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));