1b755c0431dd8cf5f43ef5b581a4e05225a049ad
[pve-common.git] / data / PVE / Network.pm
1 package PVE::Network;
2
3 use strict;
4 use warnings;
5 use PVE::Tools qw(run_command);
6 use PVE::ProcFSTools;
7 use PVE::INotify;
8 use File::Basename;
9
10 # host network related utility functions
11
12 sub setup_tc_rate_limit {
13 my ($iface, $rate, $burst, $debug) = @_;
14
15 system("/sbin/tc class del dev $iface parent 1: classid 1:1 >/dev/null 2>&1");
16 system("/sbin/tc filter del dev $iface parent ffff: protocol ip prio 50 estimator 1sec 8sec >/dev/null 2>&1");
17 system("/sbin/tc qdisc del dev $iface ingress >/dev/null 2>&1");
18 system("/sbin/tc qdisc del dev $iface root >/dev/null 2>&1");
19
20 return if !$rate;
21
22 run_command("/sbin/tc qdisc add dev $iface handle ffff: ingress");
23
24 # this does not work wit virtio - don't know why (setting "mtu 64kb" does not help)
25 #run_command("/sbin/tc filter add dev $iface parent ffff: protocol ip prio 50 u32 match ip src 0.0.0.0/0 police rate ${rate}bps burst ${burst}b drop flowid :1");
26 # so we use avrate instead
27 run_command("/sbin/tc filter add dev $iface parent ffff: " .
28 "protocol ip prio 50 estimator 1sec 8sec " .
29 "u32 match ip src 0.0.0.0/0 police avrate ${rate}bps drop flowid :1");
30
31 # tbf does not work for unknown reason
32 #$TC qdisc add dev $DEV root tbf rate $RATE latency 100ms burst $BURST
33 # so we use htb instead
34 run_command("/sbin/tc qdisc add dev $iface root handle 1: htb default 1");
35 run_command("/sbin/tc class add dev $iface parent 1: classid 1:1 " .
36 "htb rate ${rate}bps burst ${burst}b");
37
38 if ($debug) {
39 print "DEBUG tc settings\n";
40 system("/sbin/tc qdisc ls dev $iface");
41 system("/sbin/tc class ls dev $iface");
42 system("/sbin/tc filter ls dev $iface parent ffff:");
43 }
44 }
45
46 sub tap_rate_limit {
47 my ($iface, $rate) = @_;
48
49 my $debug = 0;
50 $rate = int($rate*1024*1024);
51 my $burst = 1024*1024;
52
53 setup_tc_rate_limit($iface, $rate, $burst, $debug);
54 }
55
56 sub tap_create {
57 my ($iface, $bridge) = @_;
58
59 die "unable to get bridge setting\n" if !$bridge;
60
61 my $bridgemtu = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/mtu");
62 die "bridge '$bridge' does not exist\n" if !$bridgemtu;
63
64 eval{ PVE::Tools::run_command("/sbin/ifconfig $iface 0.0.0.0 promisc up mtu $bridgemtu");};
65 die "interface activation failed\n" if $@;
66 }
67
68 sub tap_plug {
69 my ($iface, $bridge, $tag) = @_;
70
71 my $newbridge = activate_bridge_vlan($bridge, $tag);
72 copy_bridge_config($bridge, $newbridge) if $bridge ne $newbridge;
73
74 system ("/sbin/brctl addif $newbridge $iface") == 0 ||
75 die "can't add interface to bridge\n";
76 }
77
78 sub tap_unplug {
79 my ($iface, $bridge, $tag) = @_;
80
81 $bridge .= "v$tag" if $tag;
82
83 system ("/sbin/brctl delif $bridge $iface") == 0 ||
84 die "can't del interface from bridge\n";
85 }
86
87 sub copy_bridge_config {
88 my ($br0, $br1) = @_;
89
90 return if $br0 eq $br1;
91
92 my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay',
93 'hello_time', 'max_age', 'multicast_snooping', 'multicast_querier'];
94
95 foreach my $sysname (@$br_configs) {
96 eval {
97 my $v0 = PVE::Tools::file_read_firstline("/sys/class/net/$br0/bridge/$sysname");
98 my $v1 = PVE::Tools::file_read_firstline("/sys/class/net/$br1/bridge/$sysname");
99 if ($v0 ne $v1) {
100 system("echo \"$v0\" > /sys/class/net/$br1/bridge/$sysname") == 0 ||
101 warn "unable to set bridge config '$sysname'\n";
102 }
103 };
104 warn $@ if $@;
105 }
106 }
107
108 sub activate_bridge_vlan {
109 my ($bridge, $tag_param) = @_;
110
111 die "bridge '$bridge' is not active\n" if ! -d "/sys/class/net/$bridge";
112
113 return $bridge if !defined($tag_param); # no vlan, simply return
114
115 my $tag = int($tag_param);
116
117 die "got strange vlan tag '$tag_param'\n" if $tag < 1 || $tag > 4094;
118
119 my $bridgevlan = "${bridge}v$tag";
120
121 my $dir = "/sys/class/net/$bridge/brif";
122
123 #check if we have an only one ethX or bondX interface in the bridge
124
125 my $iface;
126 PVE::Tools::dir_glob_foreach($dir, '((eth|bond)\d+)', sub {
127 my ($slave) = @_;
128
129 die "more then one physical interfaces on bridge '$bridge'\n" if $iface;
130 $iface = $slave;
131
132 });
133
134 die "no physical interface on bridge '$bridge'\n" if !$iface;
135
136 my $ifacevlan = "${iface}.$tag";
137
138 # create vlan on $iface is not already exist
139 if (! -d "/sys/class/net/$ifacevlan") {
140 system("/sbin/vconfig add $iface $tag") == 0 ||
141 die "can't add vlan tag $tag to interface $iface\n";
142 }
143
144 # be sure to have the $ifacevlan up
145 system("/sbin/ip link set $ifacevlan up") == 0 ||
146 die "can't up interface $ifacevlan\n";
147
148 # test if $vlaniface is already enslaved in another bridge
149 my $path= "/sys/class/net/$ifacevlan/brport/bridge";
150 if (-l $path) {
151 my $tbridge = basename(readlink($path));
152 if ($tbridge eq $bridgevlan) {
153 # already member of bridge - assume setup is already done
154 return $bridgevlan;
155 } else {
156 die "interface $ifacevlan already exist in bridge $tbridge\n";
157 }
158 }
159
160 # add bridgevlan if it doesn't already exist
161 if (! -d "/sys/class/net/$bridgevlan") {
162 system("/sbin/brctl addbr $bridgevlan") == 0 ||
163 die "can't add bridge $bridgevlan\n";
164 }
165
166 #fixme: set other bridge flags
167
168 # be sure to have the bridge up
169 system("/sbin/ip link set $bridgevlan up") == 0 ||
170 die "can't up bridge $bridgevlan\n";
171
172 # add $ifacevlan to the bridge
173 system("/sbin/brctl addif $bridgevlan $ifacevlan") == 0 ||
174 die "can't add interface $ifacevlan to bridge $bridgevlan\n";
175
176 return $bridgevlan;
177 }
178
179 1;