6 [ -z "$CTDB_PER_IP_ROUTING_STATE" ] && {
7 CTDB_PER_IP_ROUTING_STATE="$CTDB_BASE/state/per_ip_routing"
12 case "$CTDB_PER_IP_ROUTING_CONF" in
15 CTDB_PER_IP_ROUTING_CONF="$CTDB_PER_IP_ROUTING_STATE/auto_link_local.conf"
18 [ -z "$CTDB_PER_IP_ROUTING_CONF" ] && {
19 #echo "No config file found. Nothing to do for 13.per_ip_routing"
25 _low=$CTDB_PER_IP_ROUTING_TABLE_ID_LOW
26 _high=$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH
29 echo "$0: CTDB_PER_IP_ROUTING_TABLE_ID_LOW not configured";
33 echo "$0: CTDB_PER_IP_ROUTING_TABLE_ID_HIGH not configured";
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]";
41 test -z "$CTDB_PER_IP_ROUTING_RULE_PREF" && {
42 echo "$0: CTDB_PER_IP_ROUTING_RULE_PREF not configured";
47 lock_root="$CTDB_PER_IP_ROUTING_STATE"
55 ############################
56 # grab a lock file. Not atomic, but close :)
57 # tries to cope with NFS
59 if [ -z "$lock_root" ]; then
63 machine=`cat "$lckf" 2> /dev/null | cut -d: -f1`
64 pid=`cat "$lckf" 2> /dev/null | cut -d: -f2`
66 if [ "$pid" = "$$" ]; then
67 locknesting=`expr $locknesting + 1`
68 lock_debug "lock nesting now $locknesting"
72 if test -f "$lckf"; then
73 test $machine = $host || {
74 lock_debug "lock file $lckf is valid for other machine $machine"
78 /bin/kill -0 $pid && {
79 lock_debug "lock file $lckf is valid for process $pid"
83 lock_debug "stale lock file $lckf for $machine:$pid"
87 echo "$host:$$" > "$lckf"
91 ############################
94 if [ -z "$lock_root" ]; then
97 if [ "$locknesting" != "0" ]; then
98 locknesting=`expr $locknesting - 1`
99 lock_debug "lock nesting now $locknesting"
106 generate_table_id () {
108 local _ipsdir="$CTDB_PER_IP_ROUTING_STATE/ips"
109 local _ipdir="$_ipsdir/$_ip"
113 #echo "generate_table_id $_ip"
115 local _id=`cat $_ipdir/table_id 2>/dev/null| xargs`
117 #echo "IP: $_ip => OLD TABLE: $_id"
122 local _low="$CTDB_PER_IP_ROUTING_TABLE_ID_LOW"
123 local _high="$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH"
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 || {
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"
138 echo "$_newid" > $_ipdir/table_id
139 unlock_file $_table_lck
143 test -z "$_newid" && {
144 echo "generate_table_id: out of table ids: $_low - $_high"
148 #echo "IP: $_ip => NEW TABLE: $_newid"
153 run_release_script_once()
157 #echo "run_release_script_once[$_script]"
159 test -x "$_script" && {
160 #echo "run it: start"
162 echo "release_script: $_script - failed $?"
168 echo '#!/bin/sh' > $_script
177 generate_auto_link_local()
182 #echo "generate_auto_link_local $_ip $_maskbits"
184 local _netip=`ipv4_host_addr_to_net_addr $_ip $_maskbits`
186 local _line="$_ip $_netip/$_maskbits"
188 local _lockfile="$CTDB_PER_IP_ROUTING_CONF.lock"
189 local _script="$CTDB_PER_IP_ROUTING_CONF.$$.sh"
191 echo "#!/bin/sh" > $_script
194 echo "_config=\`cat $CTDB_PER_IP_ROUTING_CONF 2>/dev/null\`" >> $_script
195 echo "_exact=\`echo -n \"\$_config\" | grep \"^$_line\$\" | wc -l | xargs\`" >> $_script
198 echo "test x\"\$_exact\" = x\"1\" && {" >> $_script
199 echo " exit 0;" >> $_script
203 echo "_tmp=\"$CTDB_PER_IP_ROUTING_CONF.$$.tmp\"" >> $_script
204 echo "echo -n \"\$_config\" | grep -v \"^$_ip \" | cat > \$_tmp || {" >> $_script
205 echo " echo \"echo -n \\\"\$_config\\\" | grep -v \\\"^$_ip \\\" > \$_tmp - failed\"" >> $_script
206 echo " exit 1;" >> $_script
208 echo "echo \"$_line\" >> \$_tmp || {" >> $_script
209 echo " echo \"echo \\\"$_line\\\" >> \$_tmp - failed\"" >> $_script
210 echo " exit 1;" >> $_script
214 echo "mv \$_tmp $CTDB_PER_IP_ROUTING_CONF || {" >> $_script
215 echo " echo \"mv \$_tmp $CTDB_PER_IP_ROUTING_CONF - failed\"" >> $_script
216 echo " exit 1;" >> $_script
220 echo "echo \"Added '$_line' to $CTDB_PER_IP_ROUTING_CONF\"">> $_script
221 echo "exit 0" >> $_script
225 test -f $_lockfile || {
229 flock --timeout 30 $_lockfile $_script
235 generate_per_ip_routing()
241 local _ipdir="$CTDB_PER_IP_ROUTING_STATE/ips/$_ip"
244 release_script="$_ipdir/per_ip_routing_release.sh"
245 setup_script="$_ipdir/per_ip_routing_setup.sh"
247 test x"$_readonly" = x"yes" && {
254 mkdir -p $_ipdir || {
255 echo "mkdir -p $_ipdir failed"
258 echo "$_ip" > $_ipdir/ip
260 generate_table_id $_ip
262 test x"$AUTO_LINK_LOCAL" = x"yes" && {
263 generate_auto_link_local $_ip $_maskbits
266 run_release_script_once $release_script
268 echo '#!/bin/sh' > $setup_script
269 echo '#' >> $setup_script
270 echo >> $setup_script
271 chmod +x $setup_script
276 setup_per_ip_routing()
281 local _release_script=$4
282 local _setup_script=$5
284 local _config=`cat $CTDB_PER_IP_ROUTING_CONF`
285 local _lines=`echo -n "$_config" | grep -n "^$_ip " | cut -d ':' -f1 | xargs`
287 local _pref="$CTDB_PER_IP_ROUTING_RULE_PREF"
289 test -n "$_lines" && {
290 echo "ip rule del from $_ip pref $_pref table $_table_id" >> $_release_script
291 echo "ip route flush table $_table_id 2>/dev/null" >> $_release_script
293 cmd="ip rule del from $_ip pref $_pref 2>/dev/null"
294 echo "$cmd" >> $_setup_script
296 cmd="ip route flush table $_table_id 2>/dev/null"
297 echo "$cmd" >> $_setup_script
299 cmd="ip rule add from $_ip pref $_pref table $_table_id"
300 echo "$cmd || {" >> $_setup_script
301 echo " echo \"$cmd - failed \$ret\"" >> $_setup_script
302 echo " exit \$ret" >> $_setup_script
303 echo "}" >> $_setup_script
306 for _l in $_lines; do
307 local _line=`echo -n "$_config" | head -n $_l | tail -n 1`
308 local _dest=`echo -n "$_line" | cut -d ' ' -f 2`
309 local _gw=`echo -n "$_line" | cut -d ' ' -f 3`
316 cmd="ip route add $_dest $_via dev $_iface table $_table_id"
317 echo "$cmd || {" >> $_setup_script
318 echo " echo \"$cmd - failed \$ret\"" >> $_setup_script
319 echo " exit \$ret" >> $_setup_script
320 echo "}" >> $_setup_script
328 #############################
329 # called when ctdbd starts up
332 pref=$CTDB_PER_IP_ROUTING_RULE_PREF
333 rules=`ip rule show | grep "^$pref:" | sed -e 's/.*from \([^ ][^ ]*\) lookup \([^ ][^ ]*\)/\2;\1/' | xargs`
335 table_id=`echo -n "$r" | cut -d ';' -f1`
336 ip=`echo -n "$r" | cut -d ';' -f2-`
338 echo "Removing ip rule for public address $ip for routing table $table_id"
339 cmd="ip rule del from $ip table $table_id pref $pref"
342 cmd="ip route flush table $table_id"
344 eval $cmd 2>/dev/null
347 # make sure that we only respond to ARP messages from the NIC where
348 # a particular ip address is associated.
349 [ -f /proc/sys/net/ipv4/conf/all/arp_filter ] && {
350 echo 1 > /proc/sys/net/ipv4/conf/all/arp_filter
353 mkdir -p $CTDB_PER_IP_ROUTING_STATE
359 for s in $CTDB_PER_IP_ROUTING_STATE/ips/*/per_ip_routing_release.sh; do
360 run_release_script_once "$s"
362 rm -rf $CTDB_PER_IP_ROUTING_STATE
366 ################################################
367 # called when ctdbd wants to claim an IP address
370 echo "must supply interface, IP and maskbits"
377 ipv4_is_valid_addr $ip || {
378 echo "$0: $1 not an ipv4 address skipping IP:$ip"
382 [ ! -d "$CTDB_PER_IP_ROUTING_STATE" ] && {
383 echo "$0: $1 No state directory found, waiting for startup."
387 generate_per_ip_routing $ip $maskbits $iface "no" || {
388 echo "$0: $1: generate_per_ip_routing $ip $maskbits $iface no - failed"
392 setup_per_ip_routing $ip $iface $table_id $release_script $setup_script || {
393 echo "$0: $1: setup_per_ip_routing $ip $iface $table_id $release_script $setup_script - failed"
397 setup_iface_ip_readd_script $iface $ip $maskbits $setup_script || {
398 echo "$0: $1: setup_iface_ip_readd_script $iface $ip $maskbits $setup_script - failed"
402 # flush our route cache
403 echo 1 > /proc/sys/net/ipv4/route/flush
404 ctdb gratiousarp $ip $iface
408 ################################################
409 # called when ctdbd wants to claim an IP address
412 echo "must supply old interface, new interface, IP and maskbits"
420 ipv4_is_valid_addr $ip || {
421 echo "$0: $1 not an ipv4 address skipping IP:$ip"
425 [ ! -d "$CTDB_PER_IP_ROUTING_STATE" ] && {
426 echo "$0: $1 No state directory found, waiting for startup."
430 generate_per_ip_routing $ip $maskbits $niface "no" || {
431 echo "$0: $1: generate_per_ip_routing $ip $maskbits $niface no - failed"
435 setup_per_ip_routing $ip $niface $table_id $release_script $setup_script || {
436 echo "$0: $1: setup_per_ip_routing $ip $niface $table_id $release_script $setup_script - failed"
440 setup_iface_ip_readd_script $niface $ip $maskbits $setup_script || {
441 echo "$0: $1: setup_iface_ip_readd_script $niface $ip $maskbits $setup_script - failed"
445 # flush our route cache
446 echo 1 > /proc/sys/net/ipv4/route/flush
448 ctdb gratiousarp $ip $niface
449 tickle_tcp_connections $ip
453 ##################################################
454 # called when ctdbd wants to release an IP address
457 echo "must supply interface, IP and maskbits"
465 ipv4_is_valid_addr $ip || {
466 echo "$0: $1 not an ipv4 address skipping IP:$ip"
470 [ ! -d "$CTDB_PER_IP_ROUTING_STATE" ] && {
471 echo "$0: $1 No state directory found, waiting for startup."
475 generate_per_ip_routing $ip $maskbits $iface "yes" || {
476 echo "$0: $1: generate_per_ip_routing $ip $maskbits $iface yes - failed"
480 run_release_script_once "$release_script"
485 ###########################################
486 # called when ctdbd has finished a recovery
490 ####################################
491 # called when ctdbd is shutting down
498 ctdb_standard_event_handler "$@"