summaryrefslogtreecommitdiff
path: root/scripts/VyattaQosUtil.pm
blob: ef2e9166b5300d65096bff294a41f9a6175d4922 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package VyattaQosUtil;
use POSIX;
require Exporter;
@EXPORT	= qw/getRate getSize getProtocol getDsfield interfaceRate/;

sub get_num  {
    my ($str) = @_;
    
    # clear errno
    $! = 0;
    my ($num, $unparsed) = POSIX::strtod($str);
    if (($str eq '') || $!) {
	die "Non-numeric input \"$str\"" . ($! ? ": $!\n" : "\n");
    }

    if ($unparsed > 0) { return $num, substr($str, -$unparsed); }
    else	       { return $num; }
}

## get_rate("10mbit")
# convert rate specification to number
# from tc/tc_util.c
sub getRate {
    my ($num, $suffix) = get_num(@_);

    if (defined $suffix) {
      SWITCH: {
	  ($suffix eq 'bit')	&& do { last SWITCH; };
	  ($suffix eq 'kibit')	&& do { $num *= 1024.; last SWITCH };
	  ($suffix eq 'kbit')	&& do { $num *= 1000.,; last SWITCH; };
	  ($suffix eq 'mibit')	&& do { $num *= 1048576.,; last SWITCH; };
	  ($suffix eq 'mbit')	&& do { $num *= 1000000.,; last SWITCH; };
	  ($suffix eq 'gibit')	&& do { $num *= 1073741824.,; last SWITCH; };
	  ($suffix eq 'gbit')	&& do { $num *= 1000000000.,; last SWITCH; };
	  ($suffix eq 'tibit')	&& do { $num *= 1099511627776.,; last SWITCH; };
	  ($suffix eq 'tbit')	&& do { $num *= 1000000000000.,; last SWITCH; };
	  ($suffix eq 'bps')	&& do { $num *= 8.,; last SWITCH; };
	  ($suffix eq 'kibps')	&& do { $num *= 8192.,; last SWITCH; };
	  ($suffix eq 'kbps')	&& do { $num *= 8000.,; last SWITCH; };
	  ($suffix eq 'mibps')	&& do { $num *= 8388608.,; last SWITCH; };
	  ($suffix eq 'mbps')	&& do { $num *= 8000000.,; last SWITCH; };
	  ($suffix eq 'gibps')	&& do { $num *= 8589934592.,; last SWITCH; };
	  ($suffix eq 'gbps')	&& do { $num *= 8000000000.,; last SWITCH; };
	  ($suffix eq 'tibps')	&& do { $num *= 8796093022208.,; last SWITCH; };
	  ($suffix eq 'tbps')	&& do { $num *= 8000000000000.,; last SWITCH; };

	  die "Rate must be a number followed by a optional suffix (kbit, mbps, ...)\n";
	}
    }
    
    die "Negative rate not allowed\n"  if ($num < 0);
    return $num;
}

sub getSize {
    my ($num, $suffix) = get_num(@_);

    if (defined $suffix) {
      SWITCH: {
	  ($suffix eq 'b')		&& do { $num *= 1.,; last SWITCH; };
	  ($suffix eq 'k')		&& do { $num *= 1024.,; last SWITCH; };
	  ($suffix eq 'kb')		&& do { $num *= 1024.,; last SWITCH; };
	  ($suffix eq 'kbit')	&& do { $num *= 128.,; last SWITCH; };
	  ($suffix eq 'm')		&& do { $num *= 1048576.,; last SWITCH; };
	  ($suffix eq 'mb')		&& do { $num *= 1048576.,; last SWITCH; };
	  ($suffix eq 'mbit')	&& do { $num *= 131072.,; last SWITCH; };
	  ($suffix eq 'g')		&& do { $num *= 1073741824.,; last SWITCH; };
	  ($suffix eq 'gb')		&& do { $num *= 1073741824.,; last SWITCH; };
	  ($suffix eq 'gbit')	&& do { $num *= 134217728.,; last SWITCH; };
	  
	  die "Unknown suffix \"$suffix\"\n";
	}
    }
    
    die "Negative size not allowed\n"  if ($num < 0);
    return $num;
}

sub getProtocol {
    my ($p) = @_;

    if ($p =~ /^([0-9]+)|(0x[0-9a-fA-F]+)$/) {
	if ($p < 0 || $p > 255) {
	    die "$p is not a valid protocol number\n";
	}
	return $p;
    }

    my ($name, $aliases, $proto) =  getprotobyname($p);
    (defined $proto)  or die "\"$p\" unknown protocol\n";
    return $proto;
}

# Parse /etc/iproute/rt_dsfield 
# return a hex string "0x10" or undefined
sub getDsfield {
    my ($str) = @_;
    my $match = undef;
    my $dsFileName = '/etc/iproute2/rt_dsfield';
    
    if ($str =~ /^([0-9]+)|(0x[0-9a-fA-F]+)$/) {
	if ($str < 0 || $str > 255) {
	    die "$str is not a valid dsfield value\n";
	}
	return $str;
    }

    open my $ds, '<', $dsFileName || die "Can't open $dsFileName, $!\n";
    while (<$ds>) {
	next if /^#/;
	chomp;
	my @fields = split;
	if ($str eq $fields[1]) {
	    $match = $fields[0];
	    last;
	}
    }
    close($ds);

    return $match;
}

# Utility routines

## interfaceRate("eth0")
# return result in bits per second
sub interfaceRate {
    my ($interface) = @_;
    my $config = new VyattaConfig;

    $config->setLevel("interfaces ethernet");
    if ($config->exists("$interface")) {
	my $speed  = $config->returnValue("$interface speed");
	if (defined($speed) && $speed ne "auto") {
	    return $speed * 1000000;
	}
    } 

    return ethtoolRate($interface);
}

## ethtoolRate("eth0")
# Fetch actual rate using ethtool and format to valid tc rate
sub ethtoolRate {
    my ($dev) = @_;
    my $rate = undef;

    open my $ethtool, "ethtool $dev |" or die "ethtool failed: $!\n";

    # ethtool produces:
    #
    # Settings for eth1:
    # Supported ports: [ TP ]
    # ...
    # Speed: 1000Mb/s
    while (<$ethtool>) {
	my @line = split;
	if ($line[0] =~ /^Speed:/) {
	    $rate = $line[1];
	    $rate =~ s#Mb/s#000000#;
	    last;
	}
    }
    close $ethtool;
    return $rate;
}