TMP: add a ctdb snapshot of current ctdb master (git://git.samba.org/ctdb.git) to...
[obnox/samba/samba-obnox.git] / ctdb / config / events.d / 10.interface
1 #!/bin/sh
2
3 #################################
4 # interface event script for ctdb
5 # this adds/removes IPs from your 
6 # public interface
7
8 . $CTDB_BASE/functions
9 loadconfig
10
11 [ -z "$CTDB_PUBLIC_ADDRESSES" ] && {
12         CTDB_PUBLIC_ADDRESSES=$CTDB_BASE/public_addresses
13 }
14
15 [ ! -f "$CTDB_PUBLIC_ADDRESSES" ] && {
16         if [ "$1" = "init" ]; then
17                 echo "No public addresses file found. Nothing to do for 10.interfaces"
18         fi
19         exit 0
20 }
21
22 mark_up ()
23 {
24     up_interfaces_found=true
25     ctdb setifacelink $1 up >/dev/null 2>&1
26 }
27
28 mark_down ()
29 {
30     fail=true
31     ctdb setifacelink $1 down >/dev/null 2>&1
32 }
33
34 # This sets $all_interfaces as a side-effect.
35 get_all_interfaces ()
36 {
37     # Get all the interfaces listed in the public_addresses file
38     all_interfaces=$(sed -e "s/^[^\t ]*[\t ]*//" -e "s/,/ /g" -e "s/[\t ]*$//" $CTDB_PUBLIC_ADDRESSES)
39
40     # Add some special interfaces if they're defined
41     [ "$CTDB_PUBLIC_INTERFACE" ] && all_interfaces="$CTDB_PUBLIC_INTERFACE $all_interfaces"
42     [ "$CTDB_NATGW_PUBLIC_IFACE" ] && all_interfaces="$CTDB_NATGW_PUBLIC_IFACE $all_interfaces"
43
44     # For all but the 1st line, get the 2nd last field with commas
45     # changes to spaces.
46     ctdb_ifaces=$(ctdb -Y ip -v | sed -e '1d' -e 's/:[^:]*:$//' -e 's/^.*://' -e 's/,/ /g')
47
48     # Add $ctdb_interfaces and uniquify
49     all_interfaces=$(echo $all_interfaces $ctdb_ifaces | tr ' ' '\n' | sort -u)
50 }
51
52 monitor_interfaces()
53 {
54         get_all_interfaces
55
56         fail=false
57         up_interfaces_found=false
58
59         for iface in $all_interfaces ; do
60
61             ip addr show $iface 2>/dev/null >/dev/null || {
62                 echo "WARNING: Interface $iface does not exist but it is used by public addresses."
63                 continue
64             }
65
66             # These interfaces are sometimes bond devices
67             # When we use VLANs for bond interfaces, there will only
68             # be an entry in /proc for the underlying real interface
69             realiface=`echo $iface |sed -e 's/\..*$//'`
70             bi=$(get_proc "net/bonding/$realiface" 2>/dev/null) && {
71                 echo "$bi" | grep -q 'Currently Active Slave: None' && {
72                         echo "ERROR: No active slaves for bond device $realiface"
73                         mark_down $iface
74                         continue
75                 }
76                 echo "$bi" | grep -q '^MII Status: up' || {
77                         echo "ERROR: public network interface $realiface is down"
78                         mark_down $iface
79                         continue
80                 }
81                 echo "$bi" | grep -q '^Bonding Mode: IEEE 802.3ad Dynamic link aggregation' && {
82                         # This works around a bug in the driver where the
83                         # overall bond status can be up but none of the actual
84                         # physical interfaces have a link.
85                         echo "$bi" | grep 'MII Status:' | tail -n +2 | grep -q '^MII Status: up' || {
86                                 echo "ERROR: No active slaves for 802.ad bond device $realiface"
87                                 mark_down $iface
88                                 continue
89                         }
90                 }
91                 mark_up $iface
92                 continue
93             }
94
95             case $iface in
96             lo*)
97                 # loopback is always working
98                 mark_up $iface
99                 ;;
100             ib*)
101                 # we dont know how to test ib links
102                 mark_up $iface
103                 ;;
104             *)
105                 [ -z "$iface" ] || {
106                     [ "$(basename $(readlink /sys/class/net/$iface/device/driver) 2>/dev/null)" = virtio_net ] ||
107                     ethtool $iface | grep -q 'Link detected: yes' || {
108                         # On some systems, this is not successful when a
109                         # cable is plugged but the interface has not been
110                         # brought up previously. Bring the interface up and
111                         # try again...
112                         ip link set $iface up
113                         ethtool $iface | grep -q 'Link detected: yes' || {
114                             echo "ERROR: No link on the public network interface $iface"
115                             mark_down $iface
116                             continue
117                         }
118                     }
119                     mark_up $iface
120                 }
121                 ;;
122             esac
123
124         done
125
126         $fail || return 0
127
128         $up_interfaces_found && \
129             [ "$CTDB_PARTIALLY_ONLINE_INTERFACES" = "yes" ] && \
130             return 0
131
132         return 1
133 }
134
135 ctdb_check_args "$@"
136
137 case "$1" in 
138      #############################
139      # called when ctdbd starts up
140      init)
141         # make sure that we only respond to ARP messages from the NIC where
142         # a particular ip address is associated.
143         get_proc sys/net/ipv4/conf/all/arp_filter >/dev/null 2>&1 && {
144             set_proc sys/net/ipv4/conf/all/arp_filter 1
145         }
146         ;;
147
148      #############################
149      # called after ctdbd has done its initial recovery
150      # and we start the services to become healthy
151      startup)
152         # Assume all links are good initially
153         get_all_interfaces
154         for iface in $all_interfaces ; do
155                 ctdb setifacelink $iface up >/dev/null 2>/dev/null
156         done
157         
158         monitor_interfaces
159
160         ;;
161
162
163      ################################################
164      # called when ctdbd wants to claim an IP address
165      takeip)
166         iface=$2
167         ip=$3
168         maskbits=$4
169
170         add_ip_to_iface $iface $ip $maskbits || {
171                 exit 1;
172         }
173
174         # cope with the script being killed while we have the interface blocked
175         iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
176
177         # flush our route cache
178         set_proc sys/net/ipv4/route/flush 1
179         ;;
180
181
182      ##################################################
183      # called when ctdbd wants to release an IP address
184      releaseip)
185         # releasing an IP is a bit more complex than it seems. Once the IP
186         # is released, any open tcp connections to that IP on this host will end
187         # up being stuck. Some of them (such as NFS connections) will be unkillable
188         # so we need to use the killtcp ctdb function to kill them off. We also
189         # need to make sure that no new connections get established while we are 
190         # doing this! So what we do is this:
191         # 1) firewall this IP, so no new external packets arrive for it
192         # 2) use netstat -tn to find existing connections, and kill them 
193         # 3) remove the IP from the interface
194         # 4) remove the firewall rule
195         iface=$2
196         ip=$3
197         maskbits=$4
198
199         failed=0
200         # we do an extra delete to cope with the script being killed
201         iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
202         iptables -I INPUT -i $iface -d $ip -j DROP
203         kill_tcp_connections $ip
204
205         delete_ip_from_iface $iface $ip $maskbits || {
206                 iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
207                 exit 1;
208         }
209
210         iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
211
212         # flush our route cache
213         set_proc sys/net/ipv4/route/flush 1
214         ;;
215
216      ##################################################
217      # called when ctdbd wants to update an IP address
218      updateip)
219         # moving an IP is a bit more complex than it seems.
220         # First we drop all traffic on the old interface.
221         # Then we try to add the ip to the new interface and before
222         # we finally remove it from the old interface.
223         #
224         # 1) firewall this IP, so no new external packets arrive for it
225         # 2) add the IP to the new interface
226         # 3) remove the IP from the old interface
227         # 4) remove the firewall rule
228         # 5) use ctdb gratiousarp to propagate the new mac address
229         # 6) use netstat -tn to find existing connections, and tickle them
230         oiface=$2
231         niface=$3
232         ip=$4
233         maskbits=$5
234
235         failed=0
236         # we do an extra delete to cope with the script being killed
237         iptables -D INPUT -i $oiface -d $ip -j DROP 2> /dev/null
238         iptables -I INPUT -i $oiface -d $ip -j DROP
239
240         delete_ip_from_iface $oiface $ip $maskbits 2>/dev/null
241         delete_ip_from_iface $niface $ip $maskbits 2>/dev/null
242
243         add_ip_to_iface $niface $ip $maskbits || {
244                 iptables -D INPUT -i $oiface -d $ip -j DROP 2> /dev/null
245                 exit 1;
246         }
247
248         # cope with the script being killed while we have the interface blocked
249         iptables -D INPUT -i $oiface -d $ip -j DROP 2> /dev/null
250
251         # flush our route cache
252         set_proc sys/net/ipv4/route/flush 1
253
254         # propagate the new mac address
255         ctdb gratiousarp $ip $niface
256
257         # tickle all existing connections, so that dropped packets
258         # are retransmited and the tcp streams work
259
260         tickle_tcp_connections $ip
261
262         ;;
263
264
265      ###########################################
266      # called when ctdbd has finished a recovery
267      recovered)
268         ;;
269
270      ####################################
271      # called when ctdbd is shutting down
272      shutdown)
273         ;;
274
275      monitor)
276         monitor_interfaces || exit 1
277         ;;
278     *)
279         ctdb_standard_event_handler "$@"
280         ;;
281 esac
282
283 exit 0
284