diff options
author | Stephen Hemminger <stephen.hemminger@vyatta.com> | 2008-02-05 16:53:46 -0800 |
---|---|---|
committer | Stephen Hemminger <stephen.hemminger@vyatta.com> | 2008-02-05 16:53:46 -0800 |
commit | 24b46144a00f1346b0c983884e3f11b91b0c4379 (patch) | |
tree | 5643fcf4e33a70e4d7f510a3e0e85b5022808e9d /scripts | |
parent | cbea4bffaf8ee64229db7624d63e47427c43d664 (diff) | |
download | vyatta-cfg-qos-24b46144a00f1346b0c983884e3f11b91b0c4379.tar.gz vyatta-cfg-qos-24b46144a00f1346b0c983884e3f11b91b0c4379.zip |
add validation for policy names, and policy update/delete
Check names of policys for syntax and uniqueness.
Add hooks for update that does refresh of interface (should fix bug).
Block deletion of policy that is in use.
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/vyatta-qos.pl | 118 |
1 files changed, 95 insertions, 23 deletions
diff --git a/scripts/vyatta-qos.pl b/scripts/vyatta-qos.pl index 4ca96b8..ea9d60d 100755 --- a/scripts/vyatta-qos.pl +++ b/scripts/vyatta-qos.pl @@ -9,23 +9,29 @@ use Getopt::Long; my $qosNode = 'qos-policy'; my $debug = $ENV{'QOS_DEBUG'}; -my @update = (); -my @delete = (); -my $list = undef; +my @updateInterface = (); +my @deleteInterface = (); +my @updatePolicy = (); +my $deletePolicy = undef; +my $listName = undef; +my $validateName = undef; GetOptions( - "list" => \$list, - "update=s{3}" => \@update, - "delete=s{2}" => \@delete, + "list-policy" => \$listName, + "validate-name=s" => \$validateName, + "update-interface=s{3}" => \@updateInterface, + "delete-interface=s{2}" => \@deleteInterface, + "update-policy=s{2}" => \@updatePolicy, + "delete-policy=s" => \$deletePolicy, ); -## list available qos policy names -sub list_available { +## list defined qos policy names +sub list_inuse { my $config = new VyattaConfig; my @nodes = (); - foreach my $policy ( $config->listNodes($qosNode) ) { - foreach my $name ( $config->listNodes("$qosNode $policy") ) { + foreach my $policy ($config->listNodes($qosNode) ) { + foreach my $name ($config->listNodes("$qosNode $policy") ) { push @nodes, $name; } } @@ -33,12 +39,29 @@ sub list_available { print join( ' ', @nodes ), "\n"; } +## check if name is okay +sub validate_name { + my $name = shift; + my $config = new VyattaConfig; + + ($name =~ '^\w[\w_-]*$') or die "Invalid policy name $name\n"; + + foreach my $policy ($config->listNodes($qosNode) ) { + foreach my $node ($config->listNodes("$qosNode $policy") ) { + if ($name eq $node) { + die "Name $name is already in use by $policy\n"; + } + } + } +} + + ## delete_interface('eth0', 'out') # remove all filters and qdisc's sub delete_interface { - my ( $interface, $direction ) = @_; + my ($interface, $direction ) = @_; - if ( $direction =~ /^out/ ) { + if ($direction eq "out" ) { # delete old qdisc - will give error if no policy in place system("tc qdisc del dev $interface root 2>/dev/null"); @@ -52,11 +75,12 @@ sub delete_interface { ## update_interface('eth0', 'out', 'my-shaper') # update policy to interface sub update_interface { - my ( $interface, $direction, $name ) = @_; + my ($interface, $direction, $name ) = @_; my $config = new VyattaConfig; + print "update_interface $interface $direction $name\n"; # TODO: add support for ingress - ( $direction =~ /^out/ ) or die "Only out direction supported"; + ( $direction eq "out" ) or die "Only out direction supported"; foreach my $policy ( $config->listNodes($qosNode) ) { if ( $config->exists("$qosNode $policy $name") ) { @@ -87,24 +111,72 @@ sub update_interface { die "Unknown $qosNode $name\n"; } -if ( defined $list ) { - list_available(); +sub delete_policy { + my ( $name ) = @_; + my $config = new VyattaConfig; + + $config->setLevel("interfaces ethernet"); + foreach my $interface ( $config->listNodes() ) { + foreach my $direction ($config->listNodes("$interface qos-policy")) { + if ($config->returnValue("$interface qos-policy $direction") eq $name) { + # can't delete active policy + die "Qos policy $name still in use on ethernet $interface $direction\n"; + } + } + } +} + +sub update_policy { + my ($shaper, $name) = @_; + my $config = new VyattaConfig; + + $config->setLevel("interfaces ethernet"); + foreach my $interface ( $config->listNodes()) { + foreach my $direction ($config->listNodes("$interface qos-policy")) { + if ($config->returnValue("$interface qos-policy $direction") eq $name) { + delete_interface($interface, $direction); + update_interface($interface, $direction, $name); + } + } + } +} + +if ( defined $listName ) { + list_inuse(); + exit 0; +} + +if ( defined $validateName ) { + validate_name($validateName); + exit 0; +} + +if ( $#deleteInterface == 1 ) { + delete_interface(@deleteInterface); + exit 0; +} + +if ( $#updateInterface == 2 ) { + update_interface(@updateInterface); exit 0; } -if ( $#delete == 1 ) { - delete_interface(@delete); +if ( defined $deletePolicy ) { + delete_policy($deletePolicy); exit 0; } -if ( $#update == 2 ) { - update_interface(@update); +if ( $#updatePolicy == 1) { + update_policy(@updatePolicy); exit 0; } print <<EOF; -usage: vyatta-qos.pl --list - vyatta-qos.pl --update interface direction policy - vyatta-qos.pl --delete interface direction +usage: vyatta-qos.pl --list-policy + vyatta-qos.pl --validate-name policy-name + vyatta-qos.pl --update-interface interface direction policy-name + vyatta-qos.pl --delete-interface interface direction + vyatta-qos.pl --update-policy policy-type policy-name + vyatta-qos.pl --delete-policy policy-type policy-name EOF exit 1; |