summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniil Baturin <daniil.baturin@vyatta.com>2012-08-07 22:40:25 +0700
committerDaniil Baturin <daniil.baturin@vyatta.com>2012-08-07 22:40:25 +0700
commitee38e5e657960e7174cce702c0eb5907ae90ac00 (patch)
tree2414c4e5584cf347b81197ba6187e6687e5875c6
parent01e35627bddd17c5edf1af6abfc966b4fa208cc7 (diff)
downloadvyatta-cfg-qos-ee38e5e657960e7174cce702c0eb5907ae90ac00.tar.gz
vyatta-cfg-qos-ee38e5e657960e7174cce702c0eb5907ae90ac00.zip
Add a module for functions shared between conf and op mode scripts.
-rw-r--r--Makefile.am1
-rw-r--r--lib/Vyatta/Qos/Shared.pm126
2 files changed, 127 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am
index 0d15ff1..4078948 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -19,6 +19,7 @@ share_perl5_DATA += lib/Vyatta/Qos/NetworkEmulator.pm
share_perl5_DATA += lib/Vyatta/Qos/RandomDetect.pm
share_perl5_DATA += lib/Vyatta/Qos/TrafficLimiter.pm
share_perl5_DATA += lib/Vyatta/Qos/Priority.pm
+share_perl5_DATA += lib/Vyatta/Qos/Shared.pm
etcdir = /etc
etc_SCRIPTS =
diff --git a/lib/Vyatta/Qos/Shared.pm b/lib/Vyatta/Qos/Shared.pm
new file mode 100644
index 0000000..b6b8b5d
--- /dev/null
+++ b/lib/Vyatta/Qos/Shared.pm
@@ -0,0 +1,126 @@
+# Wrappers for iproute2 utilities
+#
+# **** License ****
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# This code was originally developed by Vyatta, Inc.
+# Portions created by Vyatta are Copyright (C) 2008 Vyatta, Inc.
+# All Rights Reserved.
+# **** End License ****
+
+package Vyatta::Qos::Shared;
+use strict;
+use warnings;
+use lib '/opt/vyatta/share/perl5';
+use Vyatta::Config;
+use Vyatta::Interface;
+use Vyatta::Misc;
+use vars qw(%policies);
+
+our @EXPORT =
+ qw(find_policy interfaces_using get_policy_names list_policy %policies);
+#our @EXPORT_OK = qw(getIfIndex);
+use base qw(Exporter);
+
+our %policies = (
+ 'out' => {
+ 'shaper' => 'TrafficShaper',
+ 'fair-queue' => 'FairQueue',
+ 'rate-control' => 'RateLimiter',
+ 'drop-tail' => 'DropTail',
+ 'network-emulator' => 'NetworkEmulator',
+ 'round-robin' => 'RoundRobin',
+ 'priority-queue' => 'Priority',
+ 'random-detect' => 'RandomDetect',
+ },
+ 'in' => {
+ 'limiter' => 'TrafficLimiter',
+ }
+);
+
+# find policy for name - also check for duplicates
+# find_policy('limited')
+sub find_policy {
+ my $name = shift;
+ my $config = new Vyatta::Config;
+
+ # op or conf mode?
+ my $mode = $config->inSession();
+ my $exists = ($mode ? 'exists': 'isEffective');
+ my $listNodes = ($mode ? 'listNodes' : 'listEffectiveNodes');
+
+ $config->setLevel('traffic-policy');
+ my @policy = grep { $config->$exists("$_ $name") } $config->$listNodes();
+
+ die "Policy name \"$name\" conflict, used by: ", join( ' ', @policy ), "\n"
+ if ( $#policy > 0 );
+
+ return $policy[0];
+}
+
+# return array of references to (name, direction, policy)
+sub interfaces_using {
+ my $policy = shift;
+ my $config = new Vyatta::Config;
+ my @inuse = ();
+
+ # op or conf mode?
+ my $mode = $config->inSession();
+ my $listNodes = ($mode ? 'listNodes' : 'listEffectiveNodes');
+ my $returnValue = ($mode ? 'returnValue' : 'returnEffectiveValue');
+
+ foreach my $name ( getInterfaces() ) {
+ my $intf = new Vyatta::Interface($name);
+ next unless $intf;
+ my $level = $intf->path() . ' traffic-policy';
+ $config->setLevel($level);
+
+ foreach my $direction ($config->$listNodes()) {
+ my $cur = $config->$returnValue($direction);
+ next unless $cur;
+
+ # these are arguments to update_interface()
+ push @inuse, [ $name, $direction, $policy ]
+ if ($cur eq $policy);
+ }
+ }
+ return @inuse;
+}
+
+# return array of defined qos policy names
+sub get_policy_names {
+ my $config = new Vyatta::Config;
+ my @names = ();
+ my @args = @_;
+
+ # op or conf mode?
+ my $mode = $config->inSession();
+ my $listNodes = ($mode ? 'listNodes' : 'listEffectiveNodes');
+
+ $config->setLevel('traffic-policy');
+
+ foreach my $direction ( @args ) {
+ my @qos = grep { $policies{$direction}{$_} } $config->$listNodes();
+ foreach my $type (@qos) {
+ my @n = $config->$listNodes($type);
+ push @names, @n;
+ }
+ }
+ return @names;
+}
+
+# list all policy names
+sub list_policy {
+ my @args = @_;
+ my @names = get_policy_names(@args);
+ print join( ' ', @names ), "\n";
+}
+
+1;