* [PATCH 1/3] kvm tools: Add a script to setup private bridge
@ 2011-04-14 4:37 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
0 siblings, 2 replies; 4+ messages in thread
From: Amos Kong @ 2011-04-14 4:37 UTC (permalink / raw)
To: kvm; +Cc: penberg, asias.hejun, sirouni
We can use this script to create/delete a private bridge,
and launch a dhcp server on the bridge by dnsmasq,
setup forware rule of iptable, then guest can access public network.
# ./set_private_br.sh vbr0 192.168.33
add new private bridge: vbr0
# brctl show
bridge name bridge id STP enabled interfaces
vbr0 8000.000000000000 yes
# ifconfig vbr0
vbr0 Link encap:Ethernet HWaddr 82:0f:f5:8f:92:47
inet addr:192.168.33.1 Bcast:192.168.33.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:11 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:1979 (1.9 KB)
# ps aux |grep dnsmasq
nobody .. dnsmasq --strict-order --bind-interfaces --listen-address 192.168.33.1 \
--dhcp-range 192.168.33.1,192.168.33.254
Signed-off-by: Amos Kong <kongjianjun@gmail.com>
---
tools/kvm/util/set_private_br.sh | 51 ++++++++++++++++++++++++++++++++++++++
1 files changed, 51 insertions(+), 0 deletions(-)
create mode 100755 tools/kvm/util/set_private_br.sh
diff --git a/tools/kvm/util/set_private_br.sh b/tools/kvm/util/set_private_br.sh
new file mode 100755
index 0000000..49867dd
--- /dev/null
+++ b/tools/kvm/util/set_private_br.sh
@@ -0,0 +1,51 @@
+#!/bin/bash
+#
+# Author: Amos Kong <kongjianjun@gmail.com>
+# Date: Apr 14, 2011
+# Description: this script is used to create/delete a private bridge,
+# launch a dhcp server on the bridge by dnsmasq.
+#
+# @ ./set_private_br.sh $bridge_name $subnet_prefix
+# @ ./set_private_br.sh vbr0 192.168.33
+
+brname='vbr0'
+subnet='192.168.33'
+
+add_br()
+{
+ echo "add new private bridge: $brname"
+ /usr/sbin/brctl addbr $brname
+ echo 1 > /proc/sys/net/ipv6/conf/$brname/disable_ipv6
+ echo 1 > /proc/sys/net/ipv4/ip_forward
+ /usr/sbin/brctl stp $brname on
+ /usr/sbin/brctl setfd $brname 0
+ ifconfig $brname $subnet.1
+ ifconfig $brname up
+ # Add forward rule, then guest can access public network
+ iptables -t nat -A POSTROUTING -s $subnet.254/24 ! -d $subnet.254/24 -j MASQUERADE
+ /etc/init.d/dnsmasq stop
+ /etc/init.d/tftpd-hpa stop 2>/dev/null
+ dnsmasq --strict-order --bind-interfaces --listen-address $subnet.1 --dhcp-range $subnet.1,$subnet.254 $tftp_cmd
+}
+
+del_br()
+{
+ echo "cleanup bridge setup"
+ kill -9 `pgrep dnsmasq|tail -1`
+ ifconfig $brname down
+ /usr/sbin/brctl delbr $brname
+ iptables -t nat -D POSTROUTING -s $subnet.254/24 ! -d $subnet.254/24 -j MASQUERADE
+}
+
+
+if [ $# = 0 ]; then
+ del_br 2>/dev/null
+ exit
+fi
+if [ $# > 1 ]; then
+ brname="$1"
+fi
+if [ $# = 2 ]; then
+ subnet="$2"
+fi
+add_br
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 2/3] kvm tools: Add a script to setup tap device
2011-04-14 4:37 [PATCH 1/3] kvm tools: Add a script to setup private bridge Amos Kong
@ 2011-04-14 4:37 ` Amos Kong
2011-04-14 4:38 ` [PATCH 3/3] kvm tools: Setup bridged network by a script Amos Kong
1 sibling, 0 replies; 4+ messages in thread
From: Amos Kong @ 2011-04-14 4:37 UTC (permalink / raw)
To: kvm; +Cc: penberg, asias.hejun, sirouni
# ./kvm-ifup-vbr0 $tap_name
Signed-off-by: Amos Kong <kongjianjun@gmail.com>
---
tools/kvm/util/kvm-ifup-vbr0 | 6 ++++++
1 files changed, 6 insertions(+), 0 deletions(-)
create mode 100755 tools/kvm/util/kvm-ifup-vbr0
diff --git a/tools/kvm/util/kvm-ifup-vbr0 b/tools/kvm/util/kvm-ifup-vbr0
new file mode 100755
index 0000000..a91c37f
--- /dev/null
+++ b/tools/kvm/util/kvm-ifup-vbr0
@@ -0,0 +1,6 @@
+#!/bin/sh
+switch=vbr0
+/sbin/ifconfig $1 0.0.0.0 up
+/usr/sbin/brctl addif ${switch} $1
+/usr/sbin/brctl setfd ${switch} 0
+/usr/sbin/brctl stp ${switch} off
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 3/3] kvm tools: Setup bridged network by a script
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 ` Amos Kong
2011-04-15 10:19 ` [PATCH 3/3 v2] " Amos Kong
1 sibling, 1 reply; 4+ messages in thread
From: Amos Kong @ 2011-04-14 4:38 UTC (permalink / raw)
To: kvm; +Cc: penberg, asias.hejun, sirouni
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(-)
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);
}
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 3/3 v2] kvm tools: Setup bridged network by a script
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
0 siblings, 0 replies; 4+ messages in thread
From: Amos Kong @ 2011-04-15 10:19 UTC (permalink / raw)
To: kvm; +Cc: penberg, asias.hejun, sirouni
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));
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2011-04-15 10:19 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH 3/3 v2] " Amos Kong
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox