Skip to content

Commit f39c826

Browse files
committed
Wire up update_bandwidth.sh script for dynamic rate limiting in Virtual Routers
1 parent b6aee9b commit f39c826

4 files changed

Lines changed: 35 additions & 13 deletions

File tree

core/src/main/java/com/cloud/agent/api/routing/UpdateInterfaceBandwidthCommand.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717

1818
package com.cloud.agent.api.routing;
1919

20-
import com.cloud.agent.api.Command;
20+
2121

2222
/**
2323
* Sent to a Virtual Router to dynamically apply bandwidth shaping on a
2424
* named network interface using Linux tc (traffic control). A value of 0
2525
* or negative for ingressMbps / egressMbps removes an existing limit.
2626
*/
27-
public class UpdateInterfaceBandwidthCommand extends Command {
27+
public class UpdateInterfaceBandwidthCommand extends NetworkElementCommand {
2828

2929
/** The interface IP used to identify which VR interface to shape. */
3030
private String interfaceIp;
@@ -35,21 +35,21 @@ public class UpdateInterfaceBandwidthCommand extends Command {
3535
*/
3636
private String trafficType;
3737

38-
/** Ingress (inbound) rate limit in Mbps; <= 0 means no limit. */
39-
private int ingressMbps;
38+
/** Ingress (inbound) rate limit in kbps; <= 0 means no limit. */
39+
private int ingressKbps;
4040

41-
/** Egress (outbound) rate limit in Mbps; &lt;= 0 means no limit. */
42-
private int egressMbps;
41+
/** Egress (outbound) rate limit in kbps; <= 0 means no limit. */
42+
private int egressKbps;
4343

4444
protected UpdateInterfaceBandwidthCommand() {
4545
// For serialization
4646
}
4747

48-
public UpdateInterfaceBandwidthCommand(String interfaceIp, String trafficType, int ingressMbps, int egressMbps) {
48+
public UpdateInterfaceBandwidthCommand(String interfaceIp, String trafficType, int ingressKbps, int egressKbps) {
4949
this.interfaceIp = interfaceIp;
5050
this.trafficType = trafficType;
51-
this.ingressMbps = ingressMbps;
52-
this.egressMbps = egressMbps;
51+
this.ingressKbps = ingressKbps;
52+
this.egressKbps = egressKbps;
5353
}
5454

5555
public String getInterfaceIp() {
@@ -60,12 +60,12 @@ public String getTrafficType() {
6060
return trafficType;
6161
}
6262

63-
public int getIngressMbps() {
64-
return ingressMbps;
63+
public int getIngressKbps() {
64+
return ingressKbps;
6565
}
6666

67-
public int getEgressMbps() {
68-
return egressMbps;
67+
public int getEgressKbps() {
68+
return egressKbps;
6969
}
7070

7171
@Override

core/src/main/java/com/cloud/agent/resource/virtualnetwork/VRScripts.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public class VRScripts {
8080
public static final String VR_FILE_CLEANUP = "cleanup.sh";
8181

8282
public static final String VR_UPDATE_INTERFACE_CONFIG = "update_interface_config.sh";
83+
public static final String VR_UPDATE_BANDWIDTH = "update_bandwidth.sh";
8384

8485
public static final String ROUTER_FILESYSTEM_WRITABLE_CHECK = "filesystem_writable_check.py";
8586

core/src/main/java/com/cloud/agent/resource/virtualnetwork/VirtualRoutingResource.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
import com.cloud.agent.api.HandleCksIsoCommand;
3838
import org.apache.cloudstack.agent.routing.ManageServiceCommand;
39+
import com.cloud.agent.api.routing.UpdateInterfaceBandwidthCommand;
3940
import com.cloud.agent.api.routing.UpdateNetworkCommand;
4041
import com.cloud.agent.api.to.IpAddressTO;
4142
import com.cloud.network.router.VirtualRouter;
@@ -146,6 +147,10 @@ public Answer executeRequest(final NetworkElementCommand cmd) {
146147
return execute((UpdateNetworkCommand) cmd);
147148
}
148149

150+
if (cmd instanceof UpdateInterfaceBandwidthCommand) {
151+
return execute((UpdateInterfaceBandwidthCommand) cmd);
152+
}
153+
149154
if (cmd instanceof HandleCksIsoCommand) {
150155
return execute((HandleCksIsoCommand) cmd);
151156
}
@@ -250,6 +255,17 @@ private String getRouterSshControlIp(NetworkElementCommand cmd) {
250255
return routerIp;
251256
}
252257

258+
private Answer execute(UpdateInterfaceBandwidthCommand cmd) {
259+
String routerIp = getRouterSshControlIp(cmd);
260+
String args = String.format("%s %d %d", cmd.getInterfaceIp(), cmd.getIngressKbps(), cmd.getEgressKbps());
261+
ExecutionResult result = _vrDeployer.executeInVR(routerIp, VRScripts.VR_UPDATE_BANDWIDTH, args);
262+
if (result.isSuccess()) {
263+
return new Answer(cmd, true, "Successfully updated bandwidth. Details: " + result.getDetails());
264+
} else {
265+
return new Answer(cmd, false, "Failed to update bandwidth. Details: " + result.getDetails());
266+
}
267+
}
268+
253269
private Answer execute(UpdateNetworkCommand cmd) {
254270
IpAddressTO[] ipAddresses = cmd.getIpAddresses();
255271
String routerIp = getRouterSshControlIp(cmd);

server/src/main/java/com/cloud/network/NetworkServiceImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
import com.cloud.agent.api.to.NicTO;
9797
import com.cloud.agent.manager.Commands;
9898
import com.cloud.agent.api.routing.UpdateInterfaceBandwidthCommand;
99+
import com.cloud.agent.api.routing.NetworkElementCommand;
99100
import com.cloud.alert.AlertManager;
100101
import com.cloud.api.ApiDBUtils;
101102
import com.cloud.api.query.dao.DomainRouterJoinDao;
@@ -186,6 +187,7 @@
186187
import com.cloud.network.nsx.NsxService;
187188
import com.cloud.network.router.CommandSetupHelper;
188189
import com.cloud.network.router.NetworkHelper;
190+
import com.cloud.network.router.RouterControlHelper;
189191
import com.cloud.network.router.VirtualRouter;
190192
import com.cloud.network.rules.FirewallRule.Purpose;
191193
import com.cloud.network.rules.FirewallRuleVO;
@@ -445,6 +447,8 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService, C
445447
@Autowired
446448
@Qualifier("networkHelper")
447449
protected NetworkHelper networkHelper;
450+
@Inject
451+
protected RouterControlHelper _routerControlHelper;
448452

449453
int _cidrLimit;
450454
boolean _allowSubdomainNetworkAccess;
@@ -3820,6 +3824,7 @@ protected void applyDynamicNetworkRate(NetworkVO network, List<DomainRouterVO> r
38203824
String guestIp = guestNic.getIPv4Address();
38213825
UpdateInterfaceBandwidthCommand banCmd =
38223826
new UpdateInterfaceBandwidthCommand(guestIp, TrafficType.Guest.name(), kbps, kbps);
3827+
banCmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, _routerControlHelper.getRouterControlIp(router.getId()));
38233828
Commands cmds = new Commands(Command.OnError.Continue);
38243829
cmds.addCommand("updateBandwidth", banCmd);
38253830
try {

0 commit comments

Comments
 (0)