Don't try to add the same interface to the bridge twice, and clean up old routing...
[invirt/packages/invirt-xen-config.git] / vif-invirtroute
1 #!/bin/bash
2
3 #============================================================================
4 # /etc/xen/vif-route
5 #
6 # Script for configuring a vif in routed mode.
7 # The hotplugging system will call this script if it is specified either in
8 # the device configuration given to Xend, or the default Xend configuration
9 # in /etc/xen/xend-config.sxp.  If the script is specified in neither of those
10 # places, then vif-bridge is the default.
11 #
12 # Usage:
13 # vif-route (add|remove|online|offline)
14 #
15 # Environment vars:
16 # vif         vif interface name (required).
17 # dev         qemu interface name (optional)
18 # XENBUS_PATH path to this device's details in the XenStore (required).
19 # Read from the store:
20 # ip      list of IP networks for the vif, space-separated (default given in
21 #         this script).
22 #
23 # This script will set up proxy arp  for any ip addresses that are being routed
24 # type read to determine if the device is ioemu
25
26 #============================================================================
27
28 dir=$(dirname "$0")
29 . "$dir/vif-common.sh"
30
31 main_ip=$(dom0_ip)
32 dev=${dev:-${vif}}
33
34 brname=dom${vif#vif}
35
36 case "$command" in
37     online)
38         create_bridge ${brname}
39         setup_bridge_port ${dev}
40         add_to_bridge ${brname} ${dev}
41         if [ "$dev" != "$vif" ]; then
42             setup_bridge_port ${vif}
43             add_to_bridge ${brname} ${vif}
44         fi
45         ifconfig ${brname} ${main_ip} netmask 255.255.255.255 up
46         echo 1 >/proc/sys/net/ipv4/conf/${brname}/proxy_arp
47         echo 1 >/proc/sys/net/ipv4/conf/${brname}/rp_filter
48         xenstore-write "$XENBUS_PATH/feature-gso-tcpv4" 0
49         ethtool -K ${vif} tso off
50         ipcmd='add'
51         cmdprefix=''
52         ;;
53     offline)
54         do_without_error brctl delif ${brname} ${vif}
55         do_without_error brctl delif ${brname} ${dev}
56         do_without_error ifconfig ${brname} down
57         ipcmd='del'
58         cmdprefix='do_without_error'
59         ;;
60 esac
61
62 vif_type=$(xenstore_read_default "$XENBUS_PATH/type" "viffront")
63 if [  ${vif_type} != "ioemu"  -o  x${qemu_online} = xyes ] ; then
64     if [ "${ip}" ] ; then
65     # If we've been given a list of IP addresses, then add routes from dom0 to
66     # the guest using those addresses.
67         for addr in ${ip} ; do
68             ${cmdprefix} ip route ${ipcmd} ${addr} dev ${brname} src ${main_ip}
69             if [ "$ipcmd" == "del" ]; then
70                 do_without_error ip route del ${addr} dev ${dev} src ${main_ip}
71             fi
72             if [ "$command" == "online" ]; then
73                 arpspoof -i $(invirt-getconf xen.iface) -t 18.181.0.1 ${addr}&
74                 sleep 5
75                 kill %arpspoof
76             fi
77         done 
78     fi
79 fi
80
81 if [ "$command" == "offline" ]
82 then
83     do_without_error brctl delbr ${brname}
84 fi
85
86 log debug "Successful vif-route $command for $vif."
87 if [ "$command" == "online" ]
88 then
89   success
90 fi