b8a1e3e003520f21293df8d9aad470b19f3893f8
[metze/ctdb/wip.git] / config / events.d / 13.per_ip_routing
1 #!/bin/sh
2
3 . $CTDB_BASE/functions
4 loadconfig
5
6 [ -z "$CTDB_PER_IP_ROUTING_STATE" ] && {
7         CTDB_PER_IP_ROUTING_STATE="$CTDB_BASE/state/per_ip_routing"
8 }
9
10 AUTO_LINK_LOCAL="no"
11
12 case "$CTDB_PER_IP_ROUTING_CONF" in
13         __auto_link_local__)
14                 AUTO_LINK_LOCAL="yes"
15                 CTDB_PER_IP_ROUTING_CONF="$CTDB_PER_IP_ROUTING_STATE/auto_link_local.conf"
16                 ;;
17         *)
18                 [ -z "$CTDB_PER_IP_ROUTING_CONF" ] && {
19                         #echo "No config file found. Nothing to do for 13.per_ip_routing"
20                         exit 0;
21                 }
22                 ;;
23 esac
24
25 _low=$CTDB_PER_IP_ROUTING_TABLE_ID_LOW
26 _high=$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH
27
28 test -z "$_low" && {
29         echo "$0: CTDB_PER_IP_ROUTING_TABLE_ID_LOW not configured";
30         exit 1;
31 }
32 test -z "$_high" && {
33         echo "$0: CTDB_PER_IP_ROUTING_TABLE_ID_HIGH not configured";
34         exit 1;
35 }
36 test "$_low" -ge "$_high" && {
37         echo "$0: CTDB_PER_IP_ROUTING_TABLE_ID_LOW[$_low] needs to be below CTDB_PER_IP_ROUTING_TABLE_ID_HIGH[$_high]";
38         exit 1;
39 }
40
41 test -z "$CTDB_PER_IP_ROUTING_RULE_PREF" && {
42         echo "$0: CTDB_PER_IP_ROUTING_RULE_PREF not configured";
43         exit 1;
44 }
45
46 locknesting=0
47 lock_root="$CTDB_PER_IP_ROUTING_STATE"
48 host=`hostname`
49
50 lock_debug()
51 {
52         echo -n ""
53 }
54
55 ############################
56 # grab a lock file. Not atomic, but close :)
57 # tries to cope with NFS
58 lock_file() {
59         if [ -z "$lock_root" ]; then
60                 lock_root=`pwd`;
61         fi
62         lckf="$lock_root/$1"
63         machine=`cat "$lckf" 2> /dev/null | cut -d: -f1`
64         pid=`cat "$lckf" 2> /dev/null | cut -d: -f2`
65
66         if [ "$pid" = "$$" ]; then
67                 locknesting=`expr $locknesting + 1`
68                 lock_debug "lock nesting now $locknesting"
69                 return 0
70         fi
71
72         if test -f "$lckf"; then
73                 test $machine = $host || {
74                         lock_debug "lock file $lckf is valid for other machine $machine"
75                         stat -c%y "$lckf"
76                         return 1
77                 }
78                 kill -0 $pid && {
79                         lock_debug "lock file $lckf is valid for process $pid"
80                         stat -c%y "$lckf"
81                         return 1
82                 }
83                 lock_debug "stale lock file $lckf for $machine:$pid"
84                 cat "$lckf"
85                 /bin/rm -f "$lckf"
86         fi
87         echo "$host:$$" > "$lckf"
88         return 0
89 }
90
91 ############################
92 # unlock a lock file
93 unlock_file() {
94         if [ -z "$lock_root" ]; then
95                 lock_root=`pwd`;
96         fi
97         if [ "$locknesting" != "0" ]; then
98                 locknesting=`expr $locknesting - 1`
99                 lock_debug "lock nesting now $locknesting"
100         else
101                 lckf="$lock_root/$1"
102                 /bin/rm -f "$lckf"
103         fi
104 }
105
106 generate_table_id () {
107         local _ip=$1
108         local _ipsdir="$CTDB_PER_IP_ROUTING_STATE/ips"
109         local _ipdir="$_ipsdir/$_ip"
110
111         mkdir -p $_ipdir
112
113         #echo "generate_table_id $_ip"
114
115         local _id=`cat $_ipdir/table_id 2>/dev/null| xargs`
116         test -n "$_id" && {
117                 #echo "IP: $_ip => OLD TABLE: $_id"
118                 table_id=$_id
119                 return 0;
120         }
121
122         local _low="$CTDB_PER_IP_ROUTING_TABLE_ID_LOW"
123         local _high="$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH"
124
125         local _newid=""
126         for _id in `seq $_low $_high | xargs`; do
127                 local _table_lck="table_id_$_id.lock"
128                 lock_file $_table_lck 2>/dev/null || {
129                         continue;
130                 }
131                 local _taken=`grep "^$_id$" $_ipsdir/*/table_id 2>/dev/null| wc -l | xargs`
132                 test x"$_taken" != x"0" && {
133                         unlock_file $_table_lck
134                         #echo "tableid: $_id taken"
135                         continue
136                 }
137                 _newid=$_id;
138                 echo "$_newid" > $_ipdir/table_id
139                 unlock_file $_table_lck
140                 break;
141         done
142
143         test -z "$_newid" && {
144                 echo "generate_table_id: out of table ids: $_low - $_high"
145                 exit 1;
146         }
147
148         #echo "IP: $_ip => NEW TABLE: $_newid"
149         table_id=$_newid
150         return 0;
151 }
152
153 run_release_script_once()
154 {
155         local _script=$1
156
157         #echo "run_release_script_once[$_script]"
158
159         test -x "$_script" && {
160                 #echo "run it: start"
161                 $_script || {
162                         echo "release_script: $_script - failed $?"
163                         return $?;
164                 }
165                 #echo "run it: end"
166         }
167
168         echo -e "#!/bin/sh\n#\n" > $_script
169         chmod +x $_script
170
171         return 0;
172 }
173
174 generate_auto_link_local()
175 {
176         local _ip=$1
177         local _maskbits=$2
178
179         #echo "generate_auto_link_local $_ip $_maskbits"
180
181         local _netip=`ipv4_host_addr_to_net_addr $_ip $_maskbits`
182
183         local _line="$_ip $_netip/$_maskbits"
184
185         local _config=`cat $CTDB_PER_IP_ROUTING_CONF 2>/dev/null`
186
187         local _exact=`echo -n "$_config" | grep "^$line$" | wc -l | xargs`
188
189         test x"$_exact" = x"1" && {
190                 return 0;
191         }
192
193         local _tmp="$CTDB_PER_IP_ROUTING_CONF.$$.tmp"
194         echo -n "$_config" | grep -v "^$_ip " > $_tmp
195         echo "$_line" >> $_tmp
196
197         mv $_tmp $CTDB_PER_IP_ROUTING_CONF
198 }
199
200 generate_per_ip_routing()
201 {
202         local _ip=$1
203         local _maskbits=$2
204         local _iface=$3
205         local _readonly=$4
206         local _ipdir="$CTDB_PER_IP_ROUTING_STATE/ips/$_ip"
207
208         table_id=""
209         release_script="$_ipdir/per_ip_routing_release.sh"
210         setup_script="$_ipdir/per_ip_routing_setup.sh"
211
212         test x"$_readonly" = x"yes" && {
213                 test -d $_ipdir || {
214                         return 1;
215                 }
216                 return 0;
217         }
218
219         mkdir -p $_ipdir || {
220                 echo "mkdir -p $_ipdir failed"
221                 return 1;
222         }
223         echo "$_ip" > $_ipdir/ip
224
225         generate_table_id $_ip
226
227         test x"$AUTO_LINK_LOCAL" = x"yes" && {
228                 generate_auto_link_local $_ip $_maskbits
229         }
230
231         run_release_script_once $release_script
232
233         echo -e "#!/bin/sh\n#\n" > $setup_script
234         chmod +x $setup_script
235
236         return 0;
237 }
238
239 setup_per_ip_routing()
240 {
241         local _ip=$1
242         local _iface=$2
243         local _table_id=$3
244         local _release_script=$4
245         local _setup_script=$5
246
247         local _config=`cat $CTDB_PER_IP_ROUTING_CONF`
248         local _lines=`echo -n "$_config" | grep -n "^$_ip " | cut -d ':' -f1 | xargs`
249
250         local _pref="$CTDB_PER_IP_ROUTING_RULE_PREF"
251
252         test -n "$_lines" && {
253                 echo "ip rule del from $_ip pref $_pref table $_table_id" >> $_release_script
254                 echo "ip route flush table $_table_id 2>/dev/null" >> $_release_script
255
256                 cmd="ip rule del from $_ip pref $_pref 2>/dev/null"
257                 echo "$cmd" >> $_setup_script
258
259                 cmd="ip route flush table $_table_id 2>/dev/null"
260                 echo "$cmd" >> $_setup_script
261
262                 cmd="ip rule add from $_ip pref $_pref table $_table_id"
263                 echo "$cmd || {" >> $_setup_script
264                 echo "    echo \"$cmd - failed \$ret\"" >> $_setup_script
265                 echo "    exit \$ret" >> $_setup_script
266                 echo "}" >> $_setup_script
267         }
268         local _l
269         for _l in $_lines; do
270                 local _line=`echo -n "$_config" | head -n $_l | tail -n 1`
271                 local _dest=`echo -n "$_line" | cut -d ' ' -f 2`
272                 local _gw=`echo -n "$_line" | cut -d ' ' -f 3`
273
274                 local _via=""
275                 test -n "$_gw" && {
276                         _via="via $_gw"
277                 }
278
279                 cmd="ip route add $_dest $_via dev $_iface table $_table_id"
280                 echo "$cmd || {" >> $_setup_script
281                 echo "    echo \"$cmd - failed \$ret\"" >> $_setup_script
282                 echo "    exit \$ret" >> $_setup_script
283                 echo "}" >> $_setup_script
284         done
285
286         $_setup_script
287         return $?;
288 }
289
290 case "$1" in
291      #############################
292      # called when ctdbd starts up
293      startup)
294         # cleanup old rules
295         pref=$CTDB_PER_IP_ROUTING_RULE_PREF
296         rules=`ip rule show | grep "^$pref:" | sed -e 's/.*from \([^ ][^ ]*\) lookup \([^ ][^ ]*\)/\2;\1/' | xargs`
297         for r in $rules; do
298                 table_id=`echo -n "$r" | cut -d ';' -f1`
299                 ip=`echo -n "$r" | cut -d ';' -f2-`
300
301                 echo "Removing ip rule for public address $ip for routing table $table_id"
302                 cmd="ip rule del from $ip table $table_id pref $pref"
303                 #echo $cmd
304                 eval $cmd
305                 cmd="ip route flush table $table_id"
306                 #echo $cmd
307                 eval $cmd 2>/dev/null
308         done
309
310         # make sure that we only respond to ARP messages from the NIC where
311         # a particular ip address is associated.
312         [ -f /proc/sys/net/ipv4/conf/all/arp_filter ] && {
313             echo 1 > /proc/sys/net/ipv4/conf/all/arp_filter
314         }
315
316         mkdir -p $CTDB_PER_IP_ROUTING_STATE
317
318         ;;
319
320      shutdown)
321
322         for s in $CTDB_PER_IP_ROUTING_STATE/ips/*/per_ip_routing_release.sh; do
323                 run_release_script_once "$s"
324         done
325         rm -rf $CTDB_PER_IP_ROUTING_STATE
326
327         ;;
328
329      ################################################
330      # called when ctdbd wants to claim an IP address
331      takeip)
332         if [ $# != 4 ]; then
333            echo "must supply interface, IP and maskbits"
334            exit 1
335         fi
336         iface=$2
337         ip=$3
338         maskbits=$4
339
340         ipv4_is_valid_addr $ip || {
341                 echo "$0: $1 not an ipv4 address skipping IP:$ip"
342                 exit 0;
343         }
344
345         [ ! -d "$CTDB_PER_IP_ROUTING_STATE" ] && {
346                 echo "$0: $1 No state directory found, waiting for startup."
347                 exit 0;
348         }
349
350         generate_per_ip_routing $ip $maskbits $iface "no" || {
351                 echo "$0: $1: generate_per_ip_routing $ip $maskbits $iface no - failed"
352                 exit 1;
353         }
354
355         setup_per_ip_routing $ip $iface $table_id $release_script $setup_script || {
356                 echo "$0: $1: setup_per_ip_routing $ip $iface $table_id $release_script $setup_script - failed"
357                 exit 1;
358         }
359
360         setup_iface_ip_readd_script $iface $ip $maskbits $setup_script || {
361                 echo "$0: $1: setup_iface_ip_readd_script $iface $ip $maskbits $setup_script - failed"
362                 exit 1;
363         }
364
365         # flush our route cache
366         echo 1 > /proc/sys/net/ipv4/route/flush
367         ctdb gratiousarp $ip $iface
368
369         ;;
370
371      ################################################
372      # called when ctdbd wants to claim an IP address
373      updateip)
374         if [ $# != 5 ]; then
375            echo "must supply old interface, new interface, IP and maskbits"
376            exit 1
377         fi
378         oiface=$2
379         niface=$3
380         ip=$4
381         maskbits=$5
382
383         ipv4_is_valid_addr $ip || {
384                 echo "$0: $1 not an ipv4 address skipping IP:$ip"
385                 exit 0;
386         }
387
388         [ ! -d "$CTDB_PER_IP_ROUTING_STATE" ] && {
389                 echo "$0: $1 No state directory found, waiting for startup."
390                 exit 0;
391         }
392
393         generate_per_ip_routing $ip $maskbits $niface "no" || {
394                 echo "$0: $1: generate_per_ip_routing $ip $maskbits $niface no - failed"
395                 exit 1;
396         }
397
398         setup_per_ip_routing $ip $niface $table_id $release_script $setup_script || {
399                 echo "$0: $1: setup_per_ip_routing $ip $niface $table_id $release_script $setup_script - failed"
400                 exit 1;
401         }
402
403         setup_iface_ip_readd_script $niface $ip $maskbits $setup_script || {
404                 echo "$0: $1: setup_iface_ip_readd_script $niface $ip $maskbits $setup_script - failed"
405                 exit 1;
406         }
407
408         # flush our route cache
409         echo 1 > /proc/sys/net/ipv4/route/flush
410
411         ctdb gratiousarp $ip $niface
412         tickle_tcp_connections $ip
413
414         ;;
415
416      ##################################################
417      # called when ctdbd wants to release an IP address
418      releaseip)
419         if [ $# != 4 ]; then
420            echo "must supply interface, IP and maskbits"
421            exit 1
422         fi
423
424         iface=$2
425         ip=$3
426         maskbits=$4
427
428         ipv4_is_valid_addr $ip || {
429                 echo "$0: $1 not an ipv4 address skipping IP:$ip"
430                 exit 0;
431         }
432
433         [ ! -d "$CTDB_PER_IP_ROUTING_STATE" ] && {
434                 echo "$0: $1 No state directory found, waiting for startup."
435                 exit 0;
436         }
437
438         generate_per_ip_routing $ip $maskbits $iface "yes" || {
439                 echo "$0: $1: generate_per_ip_routing $ip $maskbits $iface yes - failed"
440                 exit 1;
441         }
442
443         run_release_script_once "$release_script"
444
445         ;;
446
447
448      ###########################################
449      # called when ctdbd has finished a recovery
450      recovered)
451         ;;
452
453      ####################################
454      # called when ctdbd is shutting down
455      shutdown)
456         ;;
457
458      monitor)
459         ;;
460     *)
461         ctdb_standard_event_handler "$@"
462         ;;
463 esac
464
465 exit 0
466