summaryrefslogtreecommitdiff
path: root/scripts/vyatta-qos.pl
blob: 7680ea7a0c06298f0135a10abbd747a23ae52adf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/perl

use lib "/opt/vyatta/share/perl5/";
use VyattaConfig;
use VyattaQosPolicy;
use strict;

use Getopt::Long;

my $qosNode = 'qos-policy';
my $debug = $ENV{'QOS_DEBUG'};
my @update = ();
my @delete = ();
my $list = undef;

GetOptions(
    "list"        => \$list,
    "update=s{3}" => \@update,
    "delete=s{2}" => \@delete,
);

## list available qos policy names
sub list_available {
    my $config = new VyattaConfig;
    my @nodes  = ();

    foreach my $policy ( $config->listNodes($qosNode) ) {
        foreach my $name ( $config->listNodes("$qosNode $policy") ) {
            push @nodes, $name;
        }
    }

    print join( ' ', @nodes ), "\n";
}

## delete_interface('eth0', 'out')
# remove all filters and qdisc's
sub delete_interface {
    my ( $interface, $direction ) = @_;

    if ( $direction =~ /^out/ ) {

        # delete old qdisc - will give error if no policy in place
        system("tc qdisc del dev $interface root 2>/dev/null");
        system("tc filter del dev $interface 2>/dev/null");
    }
    else {
        return -1;
    }
}

## update_interface('eth0', 'out', 'my-shaper')
# update policy to interface
sub update_interface {
    my ( $interface, $direction, $name ) = @_;
    my $config = new VyattaConfig;

    # TODO: add support for ingress
    ( $direction =~ /^out/ ) or die "Only out direction supported";

    foreach my $policy ( $config->listNodes($qosNode) ) {
        if ( $config->exists("$qosNode $policy $name") ) {
            $config->setLevel("$qosNode $policy $name");

            my $policy = VyattaQosPolicy->config( $config, $policy );
            defined $policy or die "undefined policy";

	    # When doing debugging just echo the commands
	    my $out;
	    if (defined $debug) {
		open $out, '>-'
		    or die "can't open stdout: $!";
	    } else {
		open $out, "|-" or exec qw/sudo tc -batch -/
		    or die "Tc setup failed: $!\n";
	    }

            $policy->commands($out, $interface);
	    close $out or die "Tc command failed: $!\n";
            exit 0;
        }
    }

    die "Unknown $qosNode $name\n";
}

if ( defined $list ) {
    list_available();
    exit 0;
}

if ( $#delete == 1 ) {
    delete_interface(@delete);
    exit 0;
}

if ( $#update == 2 ) {
    update_interface(@update);
    exit 0;
}

print <<EOF;
usage: vyatta-qos.pl --list
       vyatta-qos.pl --update interface direction policy
       vyatta-qos.pl --delete interface direction
EOF
exit 1;