Implement BALANCE table

Signed-off-by: Tom Eastep <teastep@shorewall.net>
This commit is contained in:
Tom Eastep 2011-10-08 13:54:39 -07:00
parent 8ec8056268
commit e6b120a805

View File

@ -45,6 +45,7 @@ our $VERSION = 'MODULEVERSION';
use constant { LOCAL_TABLE => 255,
MAIN_TABLE => 254,
DEFAULT_TABLE => 253,
BALANCE_TABLE => 250,
UNSPEC_TABLE => 0
};
@ -93,6 +94,7 @@ sub initialize( $ ) {
%providers = ( local => { number => LOCAL_TABLE , mark => 0 , optional => 0 ,routes => [], rules => [] } ,
main => { number => MAIN_TABLE , mark => 0 , optional => 0 ,routes => [], rules => [] } ,
default => { number => DEFAULT_TABLE , mark => 0 , optional => 0 ,routes => [], rules => [] } ,
balance => { number => BALANCE_TABLE , mark => 0 , optional => 0 ,routes => [], rules => [] } ,
unspec => { number => UNSPEC_TABLE , mark => 0 , optional => 0 ,routes => [], rules => [] } );
@providers = ();
}
@ -347,23 +349,17 @@ sub process_a_provider() {
$mtu = "mtu $1 ";
} elsif ( $option =~ /^fallback=(\d+)$/ ) {
fatal_error q('fallback' is not available in IPv6) if $family == F_IPV6;
if ( $config{USE_DEFAULT_RT} ) {
warning_message "'fallback' is ignored when USE_DEFAULT_RT=Yes";
} else {
$default = $1;
fatal_error 'fallback must be non-zero' unless $default;
}
$default = $1;
$default_balance = 0;
fatal_error 'fallback must be non-zero' unless $default;
} elsif ( $option eq 'fallback' ) {
fatal_error q('fallback' is not available in IPv6) if $family == F_IPV6;
if ( $config{USE_DEFAULT_RT} ) {
warning_message "'fallback' is ignored when USE_DEFAULT_RT=Yes";
} else {
$default = -1;
}
$default = -1;
$default_balance = 0;
} elsif ( $option eq 'local' ) {
$local = 1;
$track = 0 if $config{TRACK_PROVIDERS};
$default_balance = 0 if$config{USE_DEFAULT_RT};
$default_balance = 0 if $config{USE_DEFAULT_RT};
} else {
fatal_error "Invalid option ($option)";
}
@ -559,18 +555,19 @@ sub add_a_provider( $$ ) {
emit "qt \$IP -6 route del $gateway src $address dev $physical ${mtu}table $number $realm";
emit "run_ip route add $gateway src $address dev $physical ${mtu}table $number $realm";
}
emit "run_ip route add default via $gateway src $address dev $physical ${mtu}table $number $realm";
}
balance_default_route( $balance , $gateway, $physical, $realm ) if $balance;
if ( $default > 0 ) {
if ( $balance ) {
balance_default_route( $balance , $gateway, $physical, $realm );
} elsif ( $default > 0 ) {
balance_fallback_route( $default , $gateway, $physical, $realm );
} elsif ( $default ) {
emit '';
if ( $gateway ) {
if ( $family == F_IPV4 ) {
emit qq(run_ip route replace $gateway dev $physical table ) . DEFAULT_TABLE;
emit qq(run_ip route replace default via $gateway src $address dev $physical table ) . DEFAULT_TABLE . qq( metric $number);
} else {
emit qq(qt \$IP -6 route del default via $gateway src $address dev $physical table ) . DEFAULT_TABLE . qq( metric $number);
@ -581,6 +578,8 @@ sub add_a_provider( $$ ) {
emit qq(run_ip route add default table ) . DEFAULT_TABLE . qq( dev $physical metric $number);
emit qq(echo "qt \$IP -$family route del default dev $physical table ) . DEFAULT_TABLE . qq(" >> \${VARDIR}/undo_${table}_routing);
}
$default = 1;
}
unless ( $local ) {
@ -630,7 +629,7 @@ sub add_a_provider( $$ ) {
push_indent;
if ( $balance || $default ) {
$tbl = $default || $config{USE_DEFAULT_RT} ? DEFAULT_TABLE : MAIN_TABLE;
$tbl = $default ? DEFAULT_TABLE : $config{USE_DEFAULT_RT} ? BALANCE_TABLE : MAIN_TABLE;
$weight = $balance ? $balance : $default;
if ( $gateway ) {
@ -702,7 +701,7 @@ sub add_a_provider( $$ ) {
" > $undo" );
if ( $balance || $default ) {
$tbl = $fallback || ( $config{USE_DEFAULT_RT} ? DEFAULT_TABLE : MAIN_TABLE );
$tbl = $default ? DEFAULT_TABLE : $config{USE_DEFAULT_RT} ? BALANCE_TABLE : MAIN_TABLE;
$weight = $balance ? $balance : $default;
my $via = 'via';
@ -914,12 +913,14 @@ sub finish_providers() {
my $table = MAIN_TABLE;
if ( $config{USE_DEFAULT_RT} ) {
emit ( 'run_ip rule add from ' . ALLIP . ' table ' . MAIN_TABLE . ' pref 999',
emit ( 'run_ip rule add from ' . ALLIP . ' table ' . MAIN_TABLE . ' pref 999',
'run_ip rule add from ' . ALLIP . ' table ' . BALANCE_TABLE . ' pref 32765',
"\$IP -$family rule del from " . ALLIP . ' table ' . MAIN_TABLE . ' pref 32766',
qq(echo "qt \$IP -$family rule add from ) . ALLIP . ' table ' . MAIN_TABLE . ' pref 32766" >> ${VARDIR}/undo_main_routing',
qq(echo "qt \$IP -$family rule del from ) . ALLIP . ' table ' . MAIN_TABLE . ' pref 999" >> ${VARDIR}/undo_main_routing',
qq(echo "qt \$IP -$family rule add from ) . ALLIP . ' table ' . MAIN_TABLE . ' pref 32766" >> ${VARDIR}/undo_main_routing',
qq(echo "qt \$IP -$family rule del from ) . ALLIP . ' table ' . MAIN_TABLE . ' pref 999" >> ${VARDIR}/undo_main_routing',
qq(echo "qt \$IP -$family rule del from ) . ALLIP . ' table ' . BALANCE_TABLE . ' pref 32765" >> ${VARDIR}/undo_balance_routing',
'' );
$table = DEFAULT_TABLE;
$table = BALANCE_TABLE;
}
emit ( 'if [ -n "$DEFAULT_ROUTE" ]; then' );
@ -970,6 +971,8 @@ sub finish_providers() {
emit( " progress_message \"Fallback route '\$(echo \$FALLBACK_ROUTE | sed 's/\$\\s*//')' Added\"",
'fi',
'' );
} elsif ( $config{USE_DEFAULT_RT} ) {
emit "qt \$IP -$family route del default table " . DEFAULT_TABLE;
}
unless ( $config{KEEP_RT_TABLES} ) {
@ -982,7 +985,7 @@ sub finish_providers() {
'#',
LOCAL_TABLE . "\tlocal",
MAIN_TABLE . "\tmain",
DEFAULT_TABLE . "\tdefault",
$config{USE_DEFAULT_RT} ? ( DEFAULT_TABLE . "\tdefault\n" . BALANCE_TABLE . "\tbalance" ) : DEFAULT_TABLE . "\tdefault",
"0\tunspec",
'#',
'# local',