summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaulo Alcantara <paulo@paulo.ac>2018-05-19 23:54:50 -0300
committerPaulo Alcantara <paulo@paulo.ac>2018-05-19 23:54:50 -0300
commit8b71240ff9b2f5600a660b94f72f013026c313cc (patch)
tree758573ce29fee3d0321dccb03f6ecd9f9400f423
parent574386fb31b6a90313d35faa4bbc5ad24f34bd51 (diff)
downloadvmhtool-8b71240ff9b2f5600a660b94f72f013026c313cc.tar.gz
vmhtool-8b71240ff9b2f5600a660b94f72f013026c313cc.tar.xz
vmhtool-8b71240ff9b2f5600a660b94f72f013026c313cc.zip
Create bridge on tap
That way no need to assign a different ip address for every new tap dev. Signed-off-by: Paulo Alcantara <paulo@paulo.ac>
-rwxr-xr-xvmhelper78
1 files changed, 45 insertions, 33 deletions
diff --git a/vmhelper b/vmhelper
index 4209ef7..d62d934 100755
--- a/vmhelper
+++ b/vmhelper
@@ -86,7 +86,7 @@ my %vm_conf_opts = (
BRIDGE_CFG_METHOD => 'auto',
BRIDGE_IP_ADDR => '',
BRIDGE_GW_ADDR => '',
- TAP_IP_ADDR => '0.0.0.0'
+ TAP_IP_ADDR => '192.168.0.1'
);
sub in2opts {
@@ -207,24 +207,34 @@ sub is_net_iface_created {
}
sub create_bridge_dev {
- system("sudo //usr/sbin/brctl addbr $bridge_iface");
- system("sudo /usr/sbin/brctl addif $bridge_iface $host_iface");
+ my ($host, $bridge) = @_;
- if ($vm_conf_opts{'BRIDGE_CFG_METHOD'} eq 'static') {
- my $ip = $vm_conf_opts{'BRIDGE_IP_ADDR'};
+ system("sudo //usr/sbin/brctl addbr $bridge");
+ system("sudo /usr/sbin/brctl addif $bridge $host");
- system("sudo /bin/ip addr add dev $bridge_iface $ip/24");
- } else {
- system("sudo dhclient --no-pid $bridge_iface");
- }
+ # make traffic go through bridge iface only
+ system("sudo /bin/ip addr flush dev $host");
+
+ if ($bridge_enabled) {
+ if ($vm_conf_opts{'BRIDGE_CFG_METHOD'} eq 'static') {
+ my $ipaddr = "$vm_conf_opts{'BRIDGE_IP_ADDR'}/24";
- my $gw = $vm_conf_opts{'BRIDGE_GW_ADDR'};
- if ($gw) {
- system("sudo /bin/ip route delete default dev $bridge_iface >/dev/null 2>&1");
- system("sudo /bin/ip route add default via $gw dev $bridge_iface");
+ system("sudo /bin/ip addr add dev $bridge $ipaddr");
+ } else {
+ system("sudo dhclient --no-pid $bridge");
+ }
+
+ my $gw = $vm_conf_opts{'BRIDGE_GW_ADDR'};
+ if ($gw) {
+ system("sudo /bin/ip route delete default dev $bridge >/dev/null 2>&1");
+ system("sudo /bin/ip route add default via $gw dev $bridge");
+ }
+ } else {
+ my $ipaddr = !$vm_conf_opts{'TAP_IP_ADDR'} ? "0.0.0.0/24" : "$vm_conf_opts{'TAP_IP_ADDR'}/24";
+ system("sudo /bin/ip addr add dev $bridge $ipaddr");
}
- system("sudo /bin/ip link set dev $bridge_iface up");
+ system("sudo /bin/ip link set dev $bridge up");
}
sub destroy_tap_dev {
@@ -241,19 +251,13 @@ sub create_tap_dev {
$tap_iface = `sudo /sbin/tunctl -u $user -b`;
$tap_iface =~ s/^\s+|\s+$//g;
my $ret;
- if ($bridge_enabled) {
- if (system("sudo /usr/sbin/brctl addif $iface $tap_iface")) {
- $ret = $?;
- destroy_tap_dev();
- die "failed to create tap dev: $ret";
- }
- } else {
- my $ipaddr = !$vm_conf_opts{'TAP_IP_ADDR'} ? "0.0.0.0/24" : "$vm_conf_opts{'TAP_IP_ADDR'}/24";
- if (system("sudo /bin/ip addr add $ipaddr dev $tap_iface")) {
- $ret = $?;
- destroy_tap_dev();
- die "failed to create tap dev: $ret";
- }
+ my $is_bridge_on_tap = $iface ne $host_iface;
+
+ if (($bridge_enabled || $is_bridge_on_tap) &&
+ system("sudo /usr/sbin/brctl addif $iface $tap_iface")) {
+ $ret = $?;
+ destroy_tap_dev();
+ die "failed to create tap dev: $ret";
}
if (system("sudo /bin/ip link set dev $tap_iface up")) {
$ret = $?;
@@ -262,15 +266,23 @@ sub create_tap_dev {
}
}
+my $tap_bridge_iface = 'vmhbr0';
+
sub setup_vm_net {
- my $iface = '';
if ($bridge_enabled) {
- create_bridge_dev() unless is_net_iface_created($bridge_iface);
- $iface = $bridge_iface;
- } else {
- $iface = $host_iface;
+ create_bridge_dev($host_iface, $bridge_iface) unless is_net_iface_created($bridge_iface);
+ create_tap_dev($bridge_iface);
+ return;
+ }
+ #
+ # create a bridge dev on tap so we don't have to assign an ip address for
+ # every new tap dev.
+ #
+ unless (is_net_iface_created($tap_bridge_iface)) {
+ create_tap_dev($host_iface);
+ create_bridge_dev($tap_iface, $tap_bridge_iface);
}
- create_tap_dev($iface);
+ create_tap_dev($tap_bridge_iface);
}
sub destroy_vm_net {