summaryrefslogtreecommitdiff
path: root/scripts/VyattaQosMatch.pm
blob: 4b2e3fef24b94804f211b330703b9b2524e3b1c4 (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
package VyattaQosMatch;
require VyattaConfig;
use VyattaQosUtil;
use strict;

my %fields = (
	_dev      => undef,
	_vif      => undef,
	_ip	  => {
	    src      => undef,
	    dst      => undef,
	    dsfield  => undef,
	    protocol => undef,
	    sport    => undef,
	    dport    => undef,
	}
);

sub new {
    my ( $that, $config ) = @_;
    my $self = {%fields};
    my $class = ref($that) || $that;

    bless $self, $class;
    $self->_define($config);

    return $self;
}

sub _define {
    my ( $self, $config ) = @_;
    my $level = $config->setLevel();

    $self->{_vif} = VyattaQosUtil::getIfIndex($config->returnValue("vif"));
    $self->{_dev} = $config->returnValue("interface");
    if ($config->exists("ip")) {
	my %ip;

	$ip{dsfield} = VyattaQosUtil::getDsfield( $config->returnValue("ip dscp"));
	$ip{protocol} = VyattaQosUtil::getProtocol($config->returnValue("ip protocol"));
	$ip{src} = $config->returnValue("ip source address");
	$ip{dst} = $config->returnValue("ip destination address");
	$ip{sport} = $config->returnValue("ip source port");
	$ip{dport} = $config->returnValue("ip destination port");
	$self->{_ip} = \%ip;
    }
}

sub filter {
    my ( $self, $out, $dev, $id ) = @_;

    print {$out} "filter add dev $dev parent 1:0 prio 1";

    if (defined $self->{_ip}) {
	my $ip = $self->{_ip};
	print {$out} " protocol ip u32";
	print {$out} " match ip dsfield $$ip{dsfield} 0xff"	if defined $$ip{dsfield};
	print {$out} " match ip protocol $$ip{protocol} 0xff"   if defined $$ip{protocol};
	print {$out} " match ip src $$ip{src}"			if defined $$ip{src};
	print {$out} " match ip sport $$ip{sport} 0xffff"	if defined $$ip{sport};
	print {$out} " match ip dst $$ip{dst}"			if defined $$ip{dst};
	print {$out} " match ip dport $$ip{dport} 0xffff"	if defined $$ip{dport};
    }

    if (defined $self->{_dev}) {
	print {$out} " basic meta match meta \(rt_iif eq $self->{_dev}\)";
    }

    if (defined $self->{_vif}) {
	print {$out} " basic meta match meta \(vlan mask 0xfff eq $self->{_vif}\)";
    }

    print {$out} " classid 1:$id\n";
}