Add new script vif-ganeti that could be used instead of
default vif-bridge in order NIC customization can be more
flexible.

Extract common code from kvm-ifup and insert it in a new
file net-common that is being sourced by kvm-ifup and
vif-ganeti and located under package lib dir (/usr/lib/ganeti/).

TODO: in debian/links add the following symbolic link;
/etc/xen/scripts/vif-ganeti ->/usr/lib/ganeti/vif-ganeti.
This is needed because Xen expects network related scripts
to be located under /etc/xen/scripts.

Signed-off-by: Dimitris Aragiorgis <[email protected]>
---
 Makefile.am         |   14 ++++++++
 tools/kvm-ifup.in   |   61 +++-----------------------------
 tools/net-common.in |   97 +++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/vif-ganeti.in |   48 +++++++++++++++++++++++++
 4 files changed, 164 insertions(+), 56 deletions(-)
 create mode 100644 tools/net-common.in
 create mode 100755 tools/vif-ganeti.in

diff --git a/Makefile.am b/Makefile.am
index 037cf53..9133a8b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -192,6 +192,8 @@ CLEANFILES = \
        $(man_MANS) \
        $(manhtml) \
        tools/kvm-ifup \
+       tools/vif-ganeti \
+       tools/net-common \
        tools/users-setup \
        tools/vcluster-setup \
        stamp-directories \
@@ -904,6 +906,8 @@ nodist_pkglib_python_scripts = \
 myexeclib_SCRIPTS = \
        daemons/daemon-util \
        tools/kvm-ifup \
+       tools/vif-ganeti \
+       tools/net-common \
        $(pkglib_python_scripts) \
        $(HS_MYEXECLIB_PROGS)
 
@@ -939,6 +943,8 @@ EXTRA_DIST = \
        devel/upload \
        devel/webserver \
        tools/kvm-ifup.in \
+       tools/vif-ganeti.in \
+       tools/net-common.in \
        tools/users-setup.in \
        tools/vcluster-setup.in \
        $(docinput) \
@@ -1337,6 +1343,14 @@ tools/kvm-ifup: tools/kvm-ifup.in $(REPLACE_VARS_SED)
        sed -f $(REPLACE_VARS_SED) < $< > $@
        chmod +x $@
 
+tools/vif-ganeti: tools/vif-ganeti.in $(REPLACE_VARS_SED)
+       sed -f $(REPLACE_VARS_SED) < $< > $@
+       chmod +x $@
+
+tools/net-common: tools/net-common.in $(REPLACE_VARS_SED)
+       sed -f $(REPLACE_VARS_SED) < $< > $@
+       chmod +x $@
+
 tools/users-setup: tools/users-setup.in $(REPLACE_VARS_SED)
        sed -f $(REPLACE_VARS_SED) < $< > $@
        chmod +x $@
diff --git a/tools/kvm-ifup.in b/tools/kvm-ifup.in
index e506d73..5a53cee 100644
--- a/tools/kvm-ifup.in
+++ b/tools/kvm-ifup.in
@@ -18,65 +18,14 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 # 02110-1301, USA.
 
-@SHELL_ENV_INIT@
-
-if [ -z "$INTERFACE" ]; then
-  echo "No network interface specified"
-  exit 1
-fi
-
-if [ -z "$MODE" ]; then
-  echo "MODE not specified"
-  exit 1
-fi
+source @PKGLIBDIR@/net-common
 
 # Execute the user-supplied network script, if applicable
 if [ -x "$CONF_DIR/kvm-vif-bridge" ]; then
   exec $CONF_DIR/kvm-vif-bridge
 fi
 
-if [ "$MODE" = "bridged" ]; then
-  # Fix the autogenerated MAC to have the first octet set to "fe"
-  # to discourage the bridge from using the TAP dev's MAC
-  FIXED_MAC=$(ip link show $INTERFACE | \
-    awk '{if ($1 == "link/ether") printf("fe%s",substr($2,3,15))}')
-  ip link set $INTERFACE address $FIXED_MAC
-
-  ip link set $INTERFACE up
-  ip link set $INTERFACE mtu $(</sys/class/net/${BRIDGE}/mtu)
-
-  # Connect the interface to the bridge
-  brctl addif $BRIDGE $INTERFACE
-
-elif [ "$MODE" = "openvswitch" ]; then
-  ovs-vsctl add-port ${LINK} $INTERFACE
-
-else
-  ip link set $INTERFACE up
-
-  if [ -z "$IP" ]; then
-    echo "Routed NIC but no IP address specified"
-    exit 1
-  fi
-
-  # Route traffic targeted at the IP to the interface
-  if [ -n "$LINK" ]; then
-    while ip rule del dev $INTERFACE; do :; done
-    ip rule add dev $INTERFACE table $LINK
-    ip route replace $IP table $LINK proto static dev $INTERFACE
-
-  else
-    ip route replace $IP proto static dev $INTERFACE
-  fi
-
-  # Allow routing and arp proxying, or ndp proxying (IPv6)
-  if [ -d "/proc/sys/net/ipv4/conf/$INTERFACE" ]; then
-    echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/proxy_arp
-    echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/forwarding
-  fi
-
-  if [ -d "/proc/sys/net/ipv6/conf/$INTERFACE" ]; then
-    echo 1 > /proc/sys/net/ipv6/conf/$INTERFACE/proxy_ndp
-    echo 1 > /proc/sys/net/ipv6/conf/$INTERFACE/forwarding
-  fi
-fi
+check
+setup_bridge
+setup_ovs
+setup_route
diff --git a/tools/net-common.in b/tools/net-common.in
new file mode 100644
index 0000000..419d17d
--- /dev/null
+++ b/tools/net-common.in
@@ -0,0 +1,97 @@
+#!/bin/bash
+#
+
+# Copyright (C) 2011, 2012 Google Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.
+
+@SHELL_ENV_INIT@
+
+function check {
+
+  if [ -z "$INTERFACE" ]; then
+    echo "No network interface specified"
+    exit 1
+  fi
+
+  if [ -z "$MODE" ]; then
+    echo "MODE not specified"
+    exit 1
+  fi
+
+}
+
+function fix_mac {
+
+  # Fix the autogenerated MAC to have the first octet set to "fe"
+  # to discourage the bridge from using the TAP dev's MAC
+  FIXED_MAC=$(ip link show $INTERFACE | \
+    awk '{if ($1 == "link/ether") printf("fe%s",substr($2,3,15))}')
+  # in case of a vif (xen_netback device) this action is not allowed
+  ip link set $INTERFACE address $FIXED_MAC || true
+
+}
+
+function setup_bridge {
+
+  if [ "$MODE" = "bridged" ]; then
+    fix_mac
+    ip link set $INTERFACE up
+    ip link set $INTERFACE mtu $(</sys/class/net/${BRIDGE}/mtu)
+
+    # Connect the interface to the bridge
+    brctl addif $BRIDGE $INTERFACE
+  fi
+
+}
+
+function setup_ovs {
+  if [ "$MODE" = "openvswitch" ]; then
+    ovs-vsctl add-port ${LINK} $INTERFACE
+  fi
+}
+
+function setup_route {
+  if [ "$MODE" = "openvswitch" ]; then
+    ip link set $INTERFACE up
+
+    if [ -z "$IP" ]; then
+      echo "Routed NIC but no IP address specified"
+      exit 1
+    fi
+
+    # Route traffic targeted at the IP to the interface
+    if [ -n "$LINK" ]; then
+      while ip rule del dev $INTERFACE; do :; done
+      ip rule add dev $INTERFACE table $LINK
+      ip route replace $IP table $LINK proto static dev $INTERFACE
+
+    else
+      ip route replace $IP proto static dev $INTERFACE
+    fi
+
+    # Allow routing and arp proxying, or ndp proxying (IPv6)
+    if [ -d "/proc/sys/net/ipv4/conf/$INTERFACE" ]; then
+      echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/proxy_arp
+      echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/forwarding
+    fi
+
+    if [ -d "/proc/sys/net/ipv6/conf/$INTERFACE" ]; then
+      echo 1 > /proc/sys/net/ipv6/conf/$INTERFACE/proxy_ndp
+      echo 1 > /proc/sys/net/ipv6/conf/$INTERFACE/forwarding
+    fi
+  fi
+}
diff --git a/tools/vif-ganeti.in b/tools/vif-ganeti.in
new file mode 100755
index 0000000..0b9b219
--- /dev/null
+++ b/tools/vif-ganeti.in
@@ -0,0 +1,48 @@
+#!/bin/bash
+#
+
+# Copyright (C) 2011, 2012 Google Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.
+
+
+if [ -x "@XEN_CONFIG_DIR@/scripts/vif-custom" ]; then
+       exec @XEN_CONFIG_DIR@/scripts/vif-custom $*
+fi
+
+source @PKGLIBDIR@/net-common
+
+dir=$(dirname "$0")
+. "$dir"/vif-common.sh
+
+# taken from older vif-common.sh
+dev=$vif
+dev_=${dev#vif}
+domid=${dev_%.*}
+devid=${dev_#*.}
+domname=$(xm domname $domid)
+
+NIC_DIR=$RUN_DIR/xen-hypervisor/nic
+INTERFACE=$dev
+INSTANCE=$domname
+
+source $NIC_DIR/$domname/$devid
+
+setup_bridge
+setup_ovs
+setup_route
+
+success
-- 
1.7.10.4

Attachment: signature.asc
Description: Digital signature

Reply via email to