[pve-devel] [PATCH container] implement rate limiting

Wolfgang Bumiller w.bumiller at proxmox.com
Tue Mar 8 13:55:40 CET 2016


---
Doing the same as in qemu...

 src/PVE/LXC.pm        | 24 ++++++++++++++++--------
 src/PVE/LXC/Config.pm |  6 ++++++
 src/lxcnetaddbr       |  3 ++-
 3 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index e93f8a6..e19e07b 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -586,9 +586,10 @@ sub update_net {
 
 	    hotplug_net($vmid, $conf, $opt, $newnet, $netid);
 
-	} elsif (&$safe_string_ne($oldnet->{bridge}, $newnet->{bridge}) ||
-		 &$safe_num_ne($oldnet->{tag}, $newnet->{tag}) ||
-		 &$safe_num_ne($oldnet->{firewall}, $newnet->{firewall})) {
+	} else {
+	    if (&$safe_string_ne($oldnet->{bridge}, $newnet->{bridge}) ||
+		&$safe_num_ne($oldnet->{tag}, $newnet->{tag}) ||
+		&$safe_num_ne($oldnet->{firewall}, $newnet->{firewall})) {
 
 		if ($oldnet->{bridge}) {
 		    PVE::Network::tap_unplug($veth);
@@ -599,12 +600,19 @@ sub update_net {
 		    PVE::LXC::Config->write_config($vmid, $conf);
 		}
 
-		PVE::Network::tap_plug($veth, $newnet->{bridge}, $newnet->{tag}, $newnet->{firewall}, $newnet->{trunks});
-		foreach (qw(bridge tag firewall)) {
+		PVE::Network::tap_plug($veth, $newnet->{bridge}, $newnet->{tag}, $newnet->{firewall}, $newnet->{trunks}, $newnet->{rate});
+		# This includes the rate:
+		foreach (qw(bridge tag firewall rate)) {
 		    $oldnet->{$_} = $newnet->{$_} if $newnet->{$_};
 		}
-		$conf->{$opt} = PVE::LXC::Config->print_lxc_network($oldnet);
-		PVE::LXC::Config->write_config($vmid, $conf);
+	    } elsif (&$safe_string_ne($oldnet->{rate}, $newnet->{rate})) {
+		# Rate can be applied on its own but any change above needs to
+		# include the rate in tap_plug since OVS resets everything.
+		PVE::Network::tap_rate_limit($veth, $newnet->{rate});
+		$oldnet->{rate} = $newnet->{rate}
+	    }
+	    $conf->{$opt} = PVE::LXC::Config->print_lxc_network($oldnet);
+	    PVE::LXC::Config->write_config($vmid, $conf);
 	}
     } else {
 	hotplug_net($vmid, $conf, $opt, $newnet, $netid);
@@ -621,7 +629,7 @@ sub hotplug_net {
     my $eth = $newnet->{name};
 
     PVE::Network::veth_create($veth, $vethpeer, $newnet->{bridge}, $newnet->{hwaddr});
-    PVE::Network::tap_plug($veth, $newnet->{bridge}, $newnet->{tag}, $newnet->{firewall}, $newnet->{trunks});
+    PVE::Network::tap_plug($veth, $newnet->{bridge}, $newnet->{tag}, $newnet->{firewall}, $newnet->{trunks}, $newnet->{rate});
 
     # attach peer in container
     my $cmd = ['lxc-device', '-n', $vmid, 'add', $vethpeer, "$eth" ];
diff --git a/src/PVE/LXC/Config.pm b/src/PVE/LXC/Config.pm
index ff8f7b3..cc70f02 100644
--- a/src/PVE/LXC/Config.pm
+++ b/src/PVE/LXC/Config.pm
@@ -494,6 +494,12 @@ my $netconf_desc = {
 	description => "VLAN ids to pass through the interface",
 	optional => 1,
     },
+    rate => {
+	type => 'number',
+	format_description => 'mbps',
+	description => "Apply rate limiting to the interface",
+	optional => 1,
+    },
 };
 PVE::JSONSchema::register_format('pve-lxc-network', $netconf_desc);
 
diff --git a/src/lxcnetaddbr b/src/lxcnetaddbr
index c336657..5cb6b0b 100755
--- a/src/lxcnetaddbr
+++ b/src/lxcnetaddbr
@@ -41,6 +41,7 @@ my $tag = $net->{tag};
 my $firewall = $net->{firewall};
 my $bridge = $net->{bridge};
 my $trunks = $net->{trunks};
+my $rate = $net->{rate};
 
 die "missing bridge configuration" if !$bridge;
 
@@ -53,7 +54,7 @@ if (-d "/sys/class/net/$iface") {
 
     PVE::Tools::run_command("/sbin/ip link set dev $iface up mtu $bridgemtu");
     PVE::Tools::run_command("/sbin/ip addr add 0.0.0.0/0 dev $iface");
-    PVE::Network::tap_plug($iface, $bridge, $tag, $firewall, $trunks);
+    PVE::Network::tap_plug($iface, $bridge, $tag, $firewall, $trunks, $rate);
 }
 
 exit 0;
-- 
2.1.4





More information about the pve-devel mailing list