From: Amos Kong <kongjianjun@gmail.com>
To: kvm@vger.kernel.org
Cc: penberg@kernel.org, asias.hejun@gmail.com, sirouni@gmail.com
Subject: [PATCH 3/3 v2] kvm tools: Setup bridged network by a script
Date: Fri, 15 Apr 2011 18:19:34 +0800 [thread overview]
Message-ID: <20110415101933.11014.83707.stgit@localhost6.localdomain6> (raw)
In-Reply-To: <20110414043803.9279.11700.stgit@localhost6.localdomain6>
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(-)
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 <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));
prev parent reply other threads:[~2011-04-15 10:19 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-04-14 4:37 [PATCH 1/3] kvm tools: Add a script to setup private bridge Amos Kong
2011-04-14 4:37 ` [PATCH 2/3] kvm tools: Add a script to setup tap device Amos Kong
2011-04-14 4:38 ` [PATCH 3/3] kvm tools: Setup bridged network by a script Amos Kong
2011-04-15 10:19 ` Amos Kong [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20110415101933.11014.83707.stgit@localhost6.localdomain6 \
--to=kongjianjun@gmail.com \
--cc=asias.hejun@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=penberg@kernel.org \
--cc=sirouni@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox