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