Workaround for http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=1275
[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
33 dev=${dev:-${vif}}
34 intfname=${dev}
35
36 if [ ${dev} != ${vif} ]; then
37     # This is an HVM and it has two interfaces, so we'll set up a bridge.
38     brname=dom${vif#vif}
39     intfname=${brname}
40 fi
41
42 case "$command" in
43     online)
44         if [ $brname ]; then
45             create_bridge ${brname}
46             setup_bridge_port ${dev}
47             setup_bridge_port ${vif}
48             add_to_bridge ${brname} ${dev}
49             add_to_bridge ${brname} ${vif}
50         fi
51         ifconfig ${intfname} ${main_ip} netmask 255.255.255.255 up
52         echo 1 >/proc/sys/net/ipv4/conf/${intfname}/proxy_arp
53         echo 1 >/proc/sys/net/ipv4/conf/${intfname}/rp_filter
54         xenstore-write "$XENBUS_PATH/feature-gso-tcpv4" 0
55         ethtool -K ${vif} tx off
56         ipcmd='add'
57         cmdprefix=''
58         ;;
59     offline)
60         if [ $brname ]; then
61             do_without_error brctl delif ${brname} ${vif}
62             do_without_error brctl delif ${brname} ${dev}
63             do_without_error ifconfig ${brname} down
64         fi
65         ipcmd='del'
66         cmdprefix='do_without_error'
67         ;;
68 esac
69
70 vif_type=$(xenstore_read_default "$XENBUS_PATH/type" "viffront")
71 if [  ${vif_type} != "ioemu"  -o  x${qemu_online} = xyes ] ; then
72     if [ "${ip}" ] ; then
73     # If we've been given a list of IP addresses, then add routes from dom0 to
74     # the guest using those addresses.
75         for addr in ${ip} ; do
76             ${cmdprefix} ip route ${ipcmd} ${addr} dev ${intfname} src ${main_ip}
77             if [ "$ipcmd" == "del" ]; then
78                 do_without_error ip route del ${addr} dev ${dev} src ${main_ip}
79             fi
80             if [ "$command" == "online" ]; then
81                 arpspoof -i $(invirt-getconf xen.iface) -t 18.181.0.1 ${addr}&
82                 sleep 5
83                 kill %arpspoof
84             fi
85         done 
86     fi
87 fi
88
89 if [[ "$command" == "offline" && -n "$brname" ]]
90 then
91     do_without_error brctl delbr ${brname}
92 fi
93
94 log debug "Successful vif-route $command for $vif."
95 if [ "$command" == "online" ]
96 then
97   success
98 fi