diff options
12 files changed, 154 insertions, 8 deletions
diff --git a/lib/Vyatta/Qos/NetworkEmulator.pm b/lib/Vyatta/Qos/NetworkEmulator.pm new file mode 100644 index 0000000..71f347f --- /dev/null +++ b/lib/Vyatta/Qos/NetworkEmulator.pm @@ -0,0 +1,100 @@ +# This is a wrapper around Network Emulator (netem) queue discipline +# +# **** 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::NetworkEmulator; + +use strict; +use warnings; + +require VyattaConfig; +use Vyatta::Qos::Util; + +my %fields = ( + _rate => undef, + _burst => undef, + _limit => undef, + _delay => undef, + _drop => undef, + _corrupt => undef, + _reorder => undef, +); + +sub new { + my ( $that, $config ) = @_; + my $level = $config->setLevel(); + my $class = ref($that) || $that; + my $self = {%fields}; + + $self->{_rate} = getRate( $config->returnValue("bandwidth") ); + $self->{_burst} = $config->returnValue("burst"); + $self->{_limit} = $config->returnValue("queue-limit"); + $self->{_delay} = getTime($config->returnValue("network-delay")); + $self->{_drop} = $config->returnValue("packet-loss"); + $self->{_corrupt} = $config->returnValue("packet-corruption"); + $self->{_reorder} = $config->returnValue("packet-reordering"); + + + return bless $self, $class; +} + +sub commands { + my ( $self, $out, $dev ) = @_; + my $rate = $self->{_rate}; + + if ($rate) { + my $burst = $self->{_burst}; + $burst or $burst = "15K"; + + printf {$out} "qdisc add dev %s root handle 1:0 tbf rate %s burst %s\n", + $dev, $rate, $burst; + printf {$out} "qdisc add dev %s parent 1:1 handle 10: netem"; + } + else { + printf {$out} "qdisc add dev %s root netem"; + } + + my $delay = $self->{_delay}; + print {$out} " delay $delay" if ($delay); + + my $limit = $self->{_limit}; + print {$out} " limit $limit" if ($limit); + + my $drop = $self->{_drop}; + print {$out} " drop $drop" if ($drop); + + my $corrupt = $self->{_corrupt}; + print {$out} " corrupt $corrupt" if ($corrupt); + + my $reorder = $self->{_reorder}; + print {$out} " reorder $reorder" if ($reorder); + + print {$out} "\n"; +} + +sub isChanged { + my ( $self, $name ) = @_; + my $config = new VyattaConfig; + + $config->setLevel("qos-policy network-emulator $name"); + foreach my $attr ( "bandwidth", "burst", "queue-limit", "network-delay", + "packet-loss", "packet-corruption", "packet-reordering", ) { + return $attr if ( $config->isChanged($attr) ); + } + return undef; # false +} + +1; diff --git a/scripts/vyatta-qos-util.pl b/scripts/vyatta-qos-util.pl index a19baee..9bb2ea1 100755 --- a/scripts/vyatta-qos-util.pl +++ b/scripts/vyatta-qos-util.pl @@ -24,13 +24,8 @@ use Vyatta::Qos::Util qw( getPercent getRate getBurstSize getProtocol use Getopt::Long; sub getPercentOrRate { - my $percent = shift; - if ( $percent =~ /%$/ ) { - return getPercent($percent); - } - else { - return getRate($percent); - } + my $rate = shift; + return ( $rate =~ /%$/ ) ? getPercent($rate) : getRate($rate); } sub usage { @@ -57,4 +52,3 @@ GetOptions( "tos=s" => sub { getDsfield( $_[1] ); }, "time=s" => sub { getTime( $_[1] ); }, ) or usage(); - diff --git a/scripts/vyatta-qos.pl b/scripts/vyatta-qos.pl index 10f8610..5db9025 100755 --- a/scripts/vyatta-qos.pl +++ b/scripts/vyatta-qos.pl @@ -45,6 +45,7 @@ my %policies = ( 'fair-queue' => 'FairQueue', 'rate-limit' => 'RateLimiter', 'drop-tail' => 'DropTail', + 'network-emulator' => 'NetworkEmulator', }, 'in' => { 'traffic-limiter' => 'TrafficLimiter', diff --git a/templates/qos-policy/network-emulator/node.def b/templates/qos-policy/network-emulator/node.def new file mode 100644 index 0000000..0139490 --- /dev/null +++ b/templates/qos-policy/network-emulator/node.def @@ -0,0 +1,7 @@ +tag: +type: txt +help: Setup network emulator policy +syntax:expression: pattern $VAR(@) "^[[:alnum:]][-_[:alnum:]]*$" + ; "only alpha-numeric policy name allowed" +update: /opt/vyatta/sbin/vyatta-qos.pl --create-policy "$VAR(.)" "$VAR(@)" +delete: /opt/vyatta/sbin/vyatta-qos.pl --delete-policy "$VAR(@)" diff --git a/templates/qos-policy/network-emulator/node.tag/bandwidth/node.def b/templates/qos-policy/network-emulator/node.tag/bandwidth/node.def new file mode 100644 index 0000000..3e48fd3 --- /dev/null +++ b/templates/qos-policy/network-emulator/node.tag/bandwidth/node.def @@ -0,0 +1,9 @@ +type: txt +help: Set the bandwidth limit +syntax:expression: exec "/opt/vyatta/sbin/vyatta-qos-util.pl --rate \"$VAR(@)\"" +comp_help: Allowed values: + <number> Bandwidth in Kbps per second + <number><suffix> Value with scaling suffix + bits per sec (kbit, mbit, gbit) + bytes per sec (kbps, mbps, gbps) + diff --git a/templates/qos-policy/network-emulator/node.tag/burst/node.def b/templates/qos-policy/network-emulator/node.tag/burst/node.def new file mode 100644 index 0000000..56174bb --- /dev/null +++ b/templates/qos-policy/network-emulator/node.tag/burst/node.def @@ -0,0 +1,7 @@ +type: txt +help: Set the burst size +default: "15k" +syntax:expression: exec "/opt/vyatta/sbin/vyatta-qos-util.pl --burst \"$VAR(@)\"" +comp_help: Allowed values: + <number> Burst size in bytes + <number><suffix> Size with scaling suffix (kb, mb, gb) diff --git a/templates/qos-policy/network-emulator/node.tag/description/node.def b/templates/qos-policy/network-emulator/node.tag/description/node.def new file mode 100644 index 0000000..1e8e64f --- /dev/null +++ b/templates/qos-policy/network-emulator/node.tag/description/node.def @@ -0,0 +1,2 @@ +type: txt +help: Set description for this queuing policy diff --git a/templates/qos-policy/network-emulator/node.tag/network-delay/node.def b/templates/qos-policy/network-emulator/node.tag/network-delay/node.def new file mode 100644 index 0000000..ac08e1c --- /dev/null +++ b/templates/qos-policy/network-emulator/node.tag/network-delay/node.def @@ -0,0 +1,6 @@ +type: txt +syntax:expression: exec "/opt/vyatta/sbin/vyatta-qos-util.pl --time \"$VAR(@)\"" +help: Setup network delay +comp_help: Additional network delay in milliseconds + <number> Latency in milliseconds + <number><suffix> Time with suffx (secs, ms, us) diff --git a/templates/qos-policy/network-emulator/node.tag/packet-corruption/node.def b/templates/qos-policy/network-emulator/node.tag/packet-corruption/node.def new file mode 100644 index 0000000..9533609 --- /dev/null +++ b/templates/qos-policy/network-emulator/node.tag/packet-corruption/node.def @@ -0,0 +1,6 @@ +type: txt +help: Set emulated packet data corruption rate +syntax:expression: exec "/opt/vyatta/sbin/vyatta-qos-util.pl --percent \"$VAR(@)\"" +comp_help: Allowed values: + <number>%% Percentage of packets affected + diff --git a/templates/qos-policy/network-emulator/node.tag/packet-loss/node.def b/templates/qos-policy/network-emulator/node.tag/packet-loss/node.def new file mode 100644 index 0000000..16bc099 --- /dev/null +++ b/templates/qos-policy/network-emulator/node.tag/packet-loss/node.def @@ -0,0 +1,6 @@ +type: txt +help: Set emulated packet loss rate +syntax:expression: exec "/opt/vyatta/sbin/vyatta-qos-util.pl --percent \"$VAR(@)\"" +comp_help: Allowed values: + <number>%% Percentage of packets affected + diff --git a/templates/qos-policy/network-emulator/node.tag/packet-reordering/node.def b/templates/qos-policy/network-emulator/node.tag/packet-reordering/node.def new file mode 100644 index 0000000..5eb7261 --- /dev/null +++ b/templates/qos-policy/network-emulator/node.tag/packet-reordering/node.def @@ -0,0 +1,6 @@ +type: txt +help: Set emulated packet reordering percentage +syntax:expression: exec "/opt/vyatta/sbin/vyatta-qos-util.pl --percent \"$VAR(@)\"" +comp_help: Allowed values: + <number>%% Percentage of packets affected + diff --git a/templates/qos-policy/network-emulator/node.tag/queue-limit/node.def b/templates/qos-policy/network-emulator/node.tag/queue-limit/node.def new file mode 100644 index 0000000..49c47b4 --- /dev/null +++ b/templates/qos-policy/network-emulator/node.tag/queue-limit/node.def @@ -0,0 +1,2 @@ +type: u32 +help: Set maximum queue size (packets) |