From 0fdf5d942a6577a625b81eab2651af5f0d7afed5 Mon Sep 17 00:00:00 2001
From: Stephen Hemminger <stephen.hemminger@vyatta.com>
Date: Tue, 9 Dec 2008 16:51:59 -0800
Subject: Add support for network emulation

New QoS feature to allow emulating WAN characteristics
---
 lib/Vyatta/Qos/NetworkEmulator.pm | 100 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 100 insertions(+)
 create mode 100644 lib/Vyatta/Qos/NetworkEmulator.pm

(limited to 'lib/Vyatta')

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;
-- 
cgit v1.2.3