summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Hemminger <stephen.hemminger@vyatta.com>2008-03-05 15:05:42 -0800
committerStephen Hemminger <stephen.hemminger@vyatta.com>2008-03-05 15:05:42 -0800
commit4ef3988035e980e62da5d1557ebec817cba7f3a5 (patch)
tree6513a9e0f50deea7095a2880a48f73f2030f1812
parent095dfa147f6e3ea833e9c4a4824672a2060b1b2b (diff)
downloadvyatta-cfg-qos-4ef3988035e980e62da5d1557ebec817cba7f3a5.tar.gz
vyatta-cfg-qos-4ef3988035e980e62da5d1557ebec817cba7f3a5.zip
use object factory rather than hardcoded switch for policy config
Having a hardcoded switch statement is harder to update than using a hash.
-rw-r--r--scripts/VyattaQosFairQueue.pm1
-rw-r--r--scripts/VyattaQosPolicy.pm34
-rw-r--r--scripts/VyattaQosTrafficShaper.pm6
-rwxr-xr-xscripts/vyatta-qos.pl33
4 files changed, 26 insertions, 48 deletions
diff --git a/scripts/VyattaQosFairQueue.pm b/scripts/VyattaQosFairQueue.pm
index e3b1631..efae834 100644
--- a/scripts/VyattaQosFairQueue.pm
+++ b/scripts/VyattaQosFairQueue.pm
@@ -1,5 +1,4 @@
package VyattaQosFairQueue;
-@ISA = qw/VyattaQosPolicy/;
#
# This is a wrapper around Stochastic Fair Queue(SFQ) queue discipline
diff --git a/scripts/VyattaQosPolicy.pm b/scripts/VyattaQosPolicy.pm
deleted file mode 100644
index 76f86eb..0000000
--- a/scripts/VyattaQosPolicy.pm
+++ /dev/null
@@ -1,34 +0,0 @@
-package VyattaQosPolicy;
-
-use strict;
-
-require VyattaConfig;
-use VyattaQosTrafficShaper;
-use VyattaQosFairQueue;
-
-# Main class for all QoS policys
-# It is a base class, and actual policies are subclass instances.
-
-# Build a new traffic shaper of the proper type based
-# on the configuration information.
-sub config {
- my ( $class, $config, $type ) = @_;
- my $object = undef;
-
- SWITCH: {
- ( $type eq 'fair-queue' ) && do {
- $object = new VyattaQosFairQueue($config);
- last SWITCH;
- };
-
- ( $type eq 'traffic-shaper' ) && do {
- $object = new VyattaQosTrafficShaper($config);
- last SWITCH;
- };
-
- die "Unknown policy type \"$type\"\n";
- }
- return $object;
-}
-
-1;
diff --git a/scripts/VyattaQosTrafficShaper.pm b/scripts/VyattaQosTrafficShaper.pm
index 1c35ba2..bb0afbd 100644
--- a/scripts/VyattaQosTrafficShaper.pm
+++ b/scripts/VyattaQosTrafficShaper.pm
@@ -203,12 +203,12 @@
}
package VyattaQosTrafficShaper;
-@ISA = qw/VyattaQosPolicy/;
use strict;
require VyattaConfig;
use VyattaQosUtil;
my %fields = (
+ _name => undef,
_rate => undef,
_classes => undef,
);
@@ -216,11 +216,13 @@ my %fields = (
# new VyattaQosTrafficShaper($config)
# Create a new instance based on config information
sub new {
- my ( $that, $config ) = @_;
+ my ( $that, $config, $name ) = @_;
my $self = {%fields};
my $class = ref($that) || $that;
+
bless $self, $class;
+ $self->{_name} = $name;
$self->_define($config);
return $self;
diff --git a/scripts/vyatta-qos.pl b/scripts/vyatta-qos.pl
index d5936f9..295b409 100755
--- a/scripts/vyatta-qos.pl
+++ b/scripts/vyatta-qos.pl
@@ -2,7 +2,6 @@
use lib "/opt/vyatta/share/perl5/";
use VyattaConfig;
-use VyattaQosPolicy;
use strict;
use Getopt::Long;
@@ -14,6 +13,13 @@ my @updatePolicy = ();
my @deletePolicy = ();
my $listPolicy = undef;
+# map from template name to perl object
+# TODO: subdirectory
+my %policies = (
+ 'traffic-shaper' => "VyattaQosTrafficShaper",
+ 'fair-queue' => "VyattaQosFairQueue",
+);
+
GetOptions(
"update-interface=s{3}" => \@updateInterface,
"delete-interface=s{2}" => \@deleteInterface,
@@ -45,7 +51,7 @@ sub delete_interface {
if ($direction eq "out" ) {
# delete old qdisc - will give error if no policy in place
- qx(sudo tc qdisc del dev "$interface" root 2>/dev/null);
+ qx(sudo /sbin/tc qdisc del dev "$interface" root 2>/dev/null);
}
}
@@ -58,12 +64,16 @@ sub update_interface {
( $direction eq "out" ) or die "Only out direction supported";
$config->setLevel('qos-policy');
- foreach my $policy ( $config->listNodes() ) {
- if ( $config->exists("$policy $name") ) {
- $config->setLevel("qos-policy $policy $name");
+ foreach my $type ( $config->listNodes() ) {
+ if ( $config->exists("$type $name") ) {
+ $config->setLevel("qos-policy $type $name");
+
+ my $class = $policies{$type};
+ # This means template exists but we don't know what it is.
+ defined $class or die "Unknown policy type $type";
- my $policy = VyattaQosPolicy->config( $config, $policy );
- defined $policy or die "undefined policy";
+ require "$class.pm";
+ my $shaper = $class->new($config, $name);
# When doing debugging just echo the commands
my $out;
@@ -71,19 +81,20 @@ sub update_interface {
open $out, '>-'
or die "can't open stdout: $!";
} else {
- open $out, "|-" or exec qw/sudo tc -batch -/
+ open $out, "|-" or exec qw:sudo /sbin/tc -batch -:
or die "Tc setup failed: $!\n";
}
- $policy->commands($out, $interface);
+ $shaper->commands($out, $interface);
if (! close $out && ! defined $debug) {
+ # cleanup any partial commands
delete_interface($interface, $direction);
# replay commands to stdout
open $out, '>-';
- $policy->commands($out, $interface);
+ $shaper->commands($out, $interface);
close $out;
- die "Conversion of configuration to tc command error\n";
+ die "TC command failed.";
}
exit 0;
}