From 8f90c19d1e162d5e393e958a534501c002254c01 Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Mon, 13 Apr 2009 11:29:12 -0700 Subject: Redo traffic-shaper class contstructor Simplify so it can be more general in future. --- lib/Vyatta/Qos/TrafficShaper.pm | 88 ++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 58 deletions(-) diff --git a/lib/Vyatta/Qos/TrafficShaper.pm b/lib/Vyatta/Qos/TrafficShaper.pm index 2ea710b..721dac9 100644 --- a/lib/Vyatta/Qos/TrafficShaper.pm +++ b/lib/Vyatta/Qos/TrafficShaper.pm @@ -26,43 +26,26 @@ require Vyatta::Config; require Vyatta::Qos::ShaperClass; use Vyatta::Qos::Util qw/getRate interfaceRate/; -my %fields = ( - _level => undef, - _rate => undef, - _classes => undef, -); - # Create a new instance based on config information sub new { my ( $that, $config, $name ) = @_; - my $self = {%fields}; - my $class = ref($that) || $that; + my $rate = $config->returnValue("bandwidth"); + my $level = $config->setLevel(); + my @classes = _getClasses($level); - bless $self, $class; - $self->_define($config); + _checkClasses( $level, getRate($rate), @classes ) + if ( $rate ne "auto" ); - $self->_validate($config); + my $self = {}; + my $class = ref($that) || $that; + bless $self, $class; + $self->{_rate} = $rate; + $self->{_level} = $level; + $self->{_classes} = \@classes; return $self; } -sub _validate { - my $self = shift; - - if ( $self->{_rate} ne "auto" ) { - my $classes = $self->{_classes}; - my $default = shift @$classes; - my $rate = getRate( $self->{_rate} ); - - $default->rateCheck( $rate, "$self->{_level} default" ); - - foreach my $class (@$classes) { - $class->rateCheck( $rate, "$self->{_level} class $class->{id}" ); - } - unshift @$classes, $default; - } -} - # Rate can be something like "auto" or "10.2mbit" sub _getAutoRate { my ( $rate, $dev ) = @_; @@ -82,39 +65,15 @@ sub _getAutoRate { return $rate; } -# Setup new instance. -# Assumes caller has done $config->setLevel to "traffic-shaper $name" -sub _define { - my ( $self, $config ) = @_; - my $level = $config->setLevel(); - my @classes = (); - - $self->{_rate} = $config->returnValue("bandwidth"); - $self->{_level} = $level; +sub _getClasses { + my $level = shift; + my @classes; + my $config = new Vyatta::Config; + $config->setLevel($level); $config->exists("default") or die "$level configuration not complete: missing default class\n"; - # make sure no clash of different types of tc filters - my %matchTypes = (); - foreach my $class ( $config->listNodes("class") ) { - foreach my $match ( $config->listNodes("class $class match") ) { - foreach my $type ( $config->listNodes("class $class match $match") ) - { - next if ( $type eq 'description' ); - $matchTypes{$type} = "$class match $match"; - } - } - } - - if ( scalar keys %matchTypes > 1 && $matchTypes{ip} ) { - print "Match type conflict:\n"; - while ( my ( $type, $usage ) = each(%matchTypes) ) { - print " class $usage $type\n"; - } - die "$level can not match on both ip and other types\n"; - } - $config->setLevel("$level default"); push @classes, new Vyatta::Qos::ShaperClass($config); $config->setLevel($level); @@ -123,7 +82,20 @@ sub _define { $config->setLevel("$level class $id"); push @classes, new Vyatta::Qos::ShaperClass( $config, $id ); } - $self->{_classes} = \@classes; + + return @classes; +} + +sub _checkClasses { + my $level = shift; + my $rate = shift; + my $default = shift; + + $default->rateCheck( $rate, "$level default" ); + + foreach my $class (@_) { + $class->rateCheck( $rate, "$level class $class->{id}" ); + } } sub commands { -- cgit v1.2.3