network : add tap_unplug
[pve-common.git] / data / PVE / Network.pm
CommitLineData
b9436cda
DM
1package PVE::Network;
2
3use strict;
74d1b045 4use PVE::Tools qw(run_command);
b9436cda
DM
5use PVE::ProcFSTools;
6use PVE::INotify;
7use File::Basename;
8
9# host network related utility functions
10
74d1b045
DM
11sub setup_tc_rate_limit {
12 my ($iface, $rate, $burst, $debug) = @_;
13
957753df
AD
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");
edde1d46 16 system("/sbin/tc qdisc del dev $iface ingress >/dev/null 2>&1");
74d1b045
DM
17 system("/sbin/tc qdisc del dev $iface root >/dev/null 2>&1");
18
957753df
AD
19 return if (!$rate || ($rate && $rate == 0));
20
74d1b045
DM
21 run_command("/sbin/tc qdisc add dev $iface handle ffff: ingress");
22
0aaf0ca4
DM
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
ca402c95 26 run_command("/sbin/tc filter add dev $iface parent ffff: " .
0aaf0ca4
DM
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");
74d1b045
DM
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
ec9ada18
AD
45sub 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}
74d1b045 54
3aa99c70
AD
55sub 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
f0c190ee
AD
67sub 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
a84b65c0
AD
77sub 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
b9436cda
DM
86sub 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'];
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
107sub 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";
02c9a6b4
DM
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
b9436cda
DM
135 my $ifacevlan = "${iface}.$tag";
136
137 # create vlan on $iface is not already exist
138 if (! -d "/sys/class/net/$ifacevlan") {
02c9a6b4
DM
139 system("/sbin/vconfig add $iface $tag") == 0 ||
140 die "can't add vlan tag $tag to interface $iface\n";
b9436cda
DM
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
1781;