xen/network-multi

511 lines
19 KiB
Plaintext
Raw Normal View History

#!/bin/sh
#============================================================================
# network-multi
#
# Version = 1.0.0
# Date = 2007-05-12
#
# Maintainer(s) = Ron Terry - ron (at) pronetworkconsulting (dot) com
#
# The latest version can be found at:
#
# http://pronetworkconsulting.com/linux/scripts/network-multi.html
#
# Description:
#
# Replacement for the xen network-bridge, network-nat and network-route
# scripts. This script allows for the creation of multiple bridges.
#
# This script can create 3 types of bridges:
#
# traditional bridges: -Bridges that contain both a physical network
# device (ethX) and a virtual network device (vethX)
# from Dom0.
# -This is the traditional type of network bridge
# created in xen by the network-bridge script.
#
# host bridges: -Bridges that contain only a virtual network
# device (vethX) from Dom0.
# -These bridges can be configured in the
# following ways:
#
# -hostonly: This type of bridge will allow
# VMs connected to these networks
# to access only Dom0 and other
# VMs connected to the bridge.
# This type of bridge is similiar to
# a VMware "HOST ONLY" network.
#
# -nat: This type of bridge will allow
# VMs connected to these networks
# to access the "outside world"
# via NAT and other VMs connected
# to the bridge.
# This type of bridge is similiar to
# a VMware "NAT" network.
#
# -routed: This type of bridge will allow
# to access the "outside world"
# via routing through Dom0 and
# other VMs connected to the
# bridge.
#
# empty bridges: -Bridges that do not contain any physical or
# virtual network devices from Dom0.
# -These can be used to allow VMs in DomUs to
# communicate only with other DomUs and not Dom0.
#
# This script accepts the (start|stop|status) parameters. If not specified
# it sends the start parameter.
#
# This script depends on an unmodified version of the network-bridge script
# because it uses it to create the traditional bridges. It passes the
# (start|stop|status) parameters into the network-bridge script allowing
# the user to disassemble the traditional bridges containg physical network
# devices so that they may change the network configuration of the Physical
# network interfaces.
#
# Host bridges do not need to be disassembled to change the IP address
# of the virtual interfaces because they do not contain interfaces that
# have been renamed like the traditional briges (created by the
# network-bridge script) do. The stop parameter does however cause them
# to be disassembled and removed. If these bridges are NATed or Routed
# networks the default gateway of the VMs connected to them will also need
# to be changed for them to continue accessing the "outside world".
#
# The Empty bridges do not contain interfaces from Dom0 so are not
# affected by IP address changes. The stop parameter does cause them
# to be removed as well.
#
# This script will test for the presence of the physical interfaces
# configured to be connected to traditional bridges and only attempt to
# create bridges on the ones that are present and up. It will also test
# for the presence of virtual interfaces configured to be connected to
# host bridges and only create bridges for the ones that exist and
# are not already connected to an existing bridge.
#
# Edit the NETDEV_LIST variable to define which physical interfaces
# you wish to create traditional bridges on. The default is to create a
# traditional bridge on only the first interface (eth0).
#
# Edit the HOST_BRIDGE_LIST variable to define which virtual interfaces
# you wish to create host bridges on. The default is the 3rd and 4th
# virtual interfaces (veth2, veth3). The first host bridge (on veth2) is
# configured as a NAT network and the second host bridge (on veth3) is
# configured as a hostonly network.
#
# Edit the EMPTY_BRIDGE_LIST variable to define which empty bridges to
# create. This list should contain the numbers of the bridges to
# create (4 5 6 7)
#
# To enable this script edit the network-script field in the
# /etc/xen/xen-config.sxp file.
#
# Example: (network-script network-multi)
#
# Depends on: /etc/xen/scripts/xen-network-common.sh
# /etc/xen/scripts/network-bridge
#
# Can use: /etc/sysconfig/dom0config
#
# Usage: network-multi (start|stop|status)
#
# Vars:
#
# SCRIPT_PATH -Path to the directory conaining the xen network-bridge
# script (typically /etc/xen/scripts)
#
# NETDEV_LIST -Space delimited list of physical network devices to
# create traditional bridges on
#
# HOST_BRIDGE_LIST -Space delimited list of virtual network devices to
# create host bridges on using the following format:
#
# <virtual network device>,<mac address>,<IP address/CIDR NetMask>,<nat|hostonly|routed>
#
# Example with 2 virtual devices:
#
# "veth2,00:16:3E:01:00:02,172.22.0.1/16,nat veth3,00:16:3E:01:00:03,172.23.0.1/16,hostonly"
#
# EMPTY_BRIDGE_LIST -Space delimited list of bridge numbers to create as
# empty bridges.
#
# BRIDGE_NAME -Name of bridge to create (example: xenbr)
#
# NAT_EXTIF -Network interface to use as the external interface for
# NATed and Routed networks
#
#============================================================================
#### Read config files and set variables ##################################
. /etc/xen/scripts/xen-network-common.sh
# If you do not source the /etc/sysconfig/xend file, uncomment the variables
# below.
. /etc/sysconfig/xend
#NETDEV_LIST="eth0 eth1 eth2"
#HOST_BRIDGE_LIST="veth2,00:16:3E:01:00:02,172.22.0.1/16,nat veth3,00:16:3E:01:00:03,172.23.0.1/16,hostonly"
#EMPTY_BRIDGE_LIST="4 5 6 7"
#BRIDGE_NAME="xenbr"
#SCRIPT_PATH="/etc/xen/scripts"
#NAT_EXTIF="eth0"
#### Script Functions #####################################################
help() {
echo "Usage: $0 {start|stop|status}"
exit 1
}
get_option() {
# Determine which option was passed from the command line.
case "$1" in
start|stop|status)
CMD_OPT="$1"
;;
*)
help
;;
esac
}
setup_host_interface() {
# Configure the MAC and IP address of a virtual device.
#
# This function is called by other fuctions.
#
# usage: setup_host_interface <virtual net device> <MAC Addr> <IP Addr>
local DEV="$1"
local MAC="$2"
local IPADDR="$3"
case $CMD_OPT in
start)
# take the interface down
ip link set $DEV down
# ... and configure it
ip link set $DEV addr $MAC
ip addr flush $DEV
ip addr add $IPADDR brd + dev $DEV
# bring it back up
ip link set $DEV up
;;
stop)
# take the interface down
ip link set $DEV down
# unconfigure it
ip link set $DEV addr fe:ff:ff:ff:ff:ff
ip addr flush $DEV
;;
status)
ip addr show $DEV
;;
esac
}
create_bridges() {
# Uses the network-bridge script to create bridges on physical devices in Dom0.
#
# This fuction passes the start,stop,status parameters on to the network-bridge
# script.
for NETDEVICE in $NETDEV_LIST
do
local BRIDGE_NUM=${NETDEVICE##${NETDEVICE%%[0-9]*}}
if /sbin/ip link show $NETDEVICE | grep -qw UP
then
echo ""
echo "============================================================"
echo "Configuring Virtual Bridge: $BRIDGE_NAME$BRIDGE_NUM"
echo "using- Physical Interface: $NETDEVICE"
echo " Virtual Interface: vif$BRIDGE_NUM"
echo "============================================================"
echo ""
$SCRIPT_PATH/network-bridge $CMD_OPT netdev=$NETDEVICE bridge=$BRIDGE_NAME$BRIDGE_NUM vifnum=$BRIDGE_NUM
echo ""
echo "------------------------------------------------------------"
else
echo " Physical Interface $NETDEVICE is not up. Skipping $BRIDGE_NAME$BRIDGE_NUM"
echo "------------------------------------------------------------"
fi
done
}
create_host_bridges() {
# Creates bridges attached to virtual devices in Dom0 and enables nat or routing
# on the bridges if specified.
#
# This fuction reads the start,stop,status parameter from the $CMD_OPT variable
# and responds respectively.
for IFACE in $HOST_BRIDGE_LIST
do
local DEV=`echo $IFACE|cut -d "," -f 1`
local MAC=`echo $IFACE|cut -d "," -f 2`
local IPADDR=`echo $IFACE|cut -d "," -f 3`
local BRIDGE_TYPE=`echo $IFACE|cut -d "," -f 4`
local NAT_GW_IP=`echo $IFACE|cut -d "," -f 3|cut -d "/" -f 1`
#local NAT_EXTIF=`echo $NETDEV_LIST|cut -d " " -f 1`
local NAT_INTIF=`echo $IFACE|cut -d "," -f 1`
local BRIDGE_NUM=${NAT_INTIF##${NAT_INTIF%%[0-9]*}}
local BR_NAME=$BRIDGE_NAME$BRIDGE_NUM
local VIF=vif0.$BRIDGE_NUM
# Determine the initial state of the ip_forward parameter
#####################################################################
# FIX ME: We need to make this persistant.
# Should we write it out to a file?
#####################################################################
if [ `cat /proc/sys/net/ipv4/ip_forward` -eq "0" ]
then
local INIT_IP_FWD="off"
else
local INIT_IP_FWD="on"
fi
# Determine if we need to enable ip_forward
if echo $HOST_BRIDGE_LIST | grep -qE "(nat|NAT|route|ROUTE)"
then
local IP_FWD="on"
else
local IP_FWD="off"
fi
case $CMD_OPT in
start)
if ! brctl show | grep -qw $DEV && /sbin/ip address show $DEV
then
#------------------------------------------------------------------
# Create the bridge
#------------------------------------------------------------------
echo ""
echo "============================================================"
echo "Configuring Virtual Host Bridge: $BR_NAME"
echo " As a network of type: $BRIDGE_TYPE"
echo ""
echo " using- Virtual Interface: $VIF"
echo " Virtual Device: $DEV"
create_bridge $BR_NAME
setup_bridge_port $VIF
add_to_bridge $BR_NAME $VIF
setup_host_interface $DEV $MAC $IPADDR
#------------------------------------------------------------------
# Set up the bridge as a hostonly / NAT / Routed network
#------------------------------------------------------------------
case $BRIDGE_TYPE in
NAT|nat) # Set up the bridge as NATed network
echo " Gateway: $NAT_GW_IP"
echo " External Interface: $NAT_EXTIF"
if ! [ "$NAT_DONE" = "yes" ]
then
if [ "$IP_FWD" = "on" ]
then
echo 1 > /proc/sys/net/ipv4/ip_forward
IP_FWD="done"
fi
iptables -t nat -A POSTROUTING -o $NAT_EXTIF -j MASQUERADE
sysctl -q -w net.bridge.bridge-nf-call-iptables="0"
NAT_DONE="yes"
fi
;;
ROUTE|route) # Set up the bridge as Routed network
echo " Gateway: $NAT_GW_IP"
echo " External Interface: $NAT_EXTIF"
if [ "$IP_FWD" = "off" ]
then
echo 1 > /proc/sys/net/ipv4/ip_forward
IP_FWD="on"
fi
;;
HOSTONLY|hostonly) # Set up the bridge as hostonly network
if [ "$IP_FWD" = "on" ]
then
iptables -t nat -A PREROUTING -i $NAT_INTIF -j DROP
fi
;;
esac
echo "============================================================"
echo "------------------------------------------------------------"
else
#------------------------------------------------------------------
# Skip this bridge
#------------------------------------------------------------------
echo " Virtual Interface $DEV is already attached to a bridge or it does not exist."
echo " Skipping $BR_NAME"
echo "------------------------------------------------------------"
fi
;;
stop)
#------------------------------------------------------------------
# Remove the bridge
#------------------------------------------------------------------
echo ""
echo "============================================================"
echo "Removing Virtual Host Bridge: $BR_NAME"
echo " As a network of type: $BRIDGE_TYPE"
echo ""
echo " using- Virtual Interface: $VIF"
echo " Virtual Device: $DEV"
#------------------------------------------------------------------
# First remove the hostonly / NAT / Routed configuration
#------------------------------------------------------------------
case $BRIDGE_TYPE in
NAT|nat)
#####################################################################
# FIX ME: We need to set the ip_forward value back to the state it
# was in before the Network script ran.
# How do we determine that state?
#####################################################################
# We could use this if we set the INIT_IP_FWD value from some
# persistant source.
#----------------------------------------------------------------------
#if [ "$INIT_IP_FWD" = "off" ]
#then
# echo 0 > /proc/sys/net/ipv4/ip_forward
#fi
# This would wack all nat rules. What if they were set before we
# configured Xen networking?
#----------------------------------------------------------------------
#if ! [ "$NAT_REMOVED" = "yes" ]
#then
# # Remove all nat iptables rules (host bridge nat PREROUTING DROP, etc.)
# iptables -F -t nat
# NAT_REMOVED="yes"
#fi
# Turn off routing and clean out the bridge specific nat iptables rule
echo 0 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -D POSTROUTING -o $NAT_EXTIF -j MASQUERADE
sysctl -q -w net.bridge.bridge-nf-call-iptables="1"
;;
HOSTONLY|hostonly)
# Clean out the bridge specific nat iptables rule
iptables -t nat -D PREROUTING -i $NAT_INTIF -j DROP
;;
ROUTE|route)
#####################################################################
# FIX ME: We need to set the ip_forward value back to the state it
# was in before the Network script ran.
# How do we determine that state?
#####################################################################
# Turn off routing
echo 0 > /proc/sys/net/ipv4/ip_forward
;;
esac
echo "============================================================"
#------------------------------------------------------------------
# Then unconfigure the veth
#------------------------------------------------------------------
setup_host_interface $DEV $MAC $IPADDR
#------------------------------------------------------------------
# remove vif from the bridge
#------------------------------------------------------------------
brctl delif $BR_NAME $VIF
#------------------------------------------------------------------
# unconfigure the vif
#------------------------------------------------------------------
ip link set $VIF down
ip link set $VIF addr fe:ff:ff:ff:ff:ff
ip link set $VIF multicast on
ip link set $VIF arp on
ip addr flush $VIF
#------------------------------------------------------------------
# and finaly unconfigure the bridge
#------------------------------------------------------------------
ip link set $BR_NAME down
brctl delbr $BR_NAME
echo "------------------------------------------------------------"
;;
status)
#------------------------------------------------------------------
# Show the status of the bridge
#------------------------------------------------------------------
echo ""
echo "============================================================"
echo "Status of Virtual Host Bridge: $BR_NAME"
echo " using- Virtual Interface: $VIF"
echo " Virtual Device: $DEV"
echo "------------------------------------------------------------"
echo "This Network is: $BRIDGE_TYPE"
echo "============================================================"
brctl show | grep -w "^$BR_NAME"
echo ""
ip addr show $DEV
#echo ""
#echo "ip_forward is set to: `cat /proc/sys/net/ipv4/ip_forward`"
echo "============================================================"
;;
esac
done
}
create_empty_bridges() {
# Creates bridges attached to no devices in Dom0.
#
# This function reads the start,stop,status parameter from the $CMD_OPT
# variable and responds respectively.
echo ""
echo "============================================================"
for BRIDGE in $EMPTY_BRIDGE_LIST
do
local BRIDGE_NUM=$BRIDGE
local BR_NAME=$BRIDGE_NAME$BRIDGE_NUM
case $CMD_OPT in
start)
if ! brctl show | grep -qw "^$BR_NAME"
then
echo "Configuring Virtual Empty Bridge: $BR_NAME"
create_bridge $BR_NAME
fi
;;
stop)
if brctl show | grep -qw "^$BR_NAME"
then
echo "Unconfiguring Virtual Empty Bridge: $BR_NAME"
ip link set $BR_NAME down
brctl delbr $BR_NAME
fi
;;
status)
brctl show $BR_NAME | grep -w "^$BR_NAME"
;;
esac
done
echo "============================================================"
}
#### Call Functions #######################################################
get_option "$1"
create_bridges
create_host_bridges
create_empty_bridges
exit 0