Skip to content

Commit b6aee9b

Browse files
committed
network throttling code changes
1 parent e1521f1 commit b6aee9b

37 files changed

Lines changed: 1309 additions & 6 deletions

api/src/main/java/com/cloud/event/EventTypes.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ public class EventTypes {
173173
public static final String EVENT_NETWORK_CREATE = "NETWORK.CREATE";
174174
public static final String EVENT_NETWORK_DELETE = "NETWORK.DELETE";
175175
public static final String EVENT_NETWORK_UPDATE = "NETWORK.UPDATE";
176+
public static final String EVENT_NETWORK_RATE_UPDATE = "NETWORK.RATE.UPDATE";
176177
public static final String EVENT_NETWORK_MIGRATE = "NETWORK.MIGRATE";
177178
public static final String EVENT_FIREWALL_OPEN = "FIREWALL.OPEN";
178179
public static final String EVENT_FIREWALL_CLOSE = "FIREWALL.CLOSE";
@@ -584,6 +585,7 @@ public class EventTypes {
584585
public static final String EVENT_VPC_UPDATE = "VPC.UPDATE";
585586
public static final String EVENT_VPC_DELETE = "VPC.DELETE";
586587
public static final String EVENT_VPC_RESTART = "VPC.RESTART";
588+
public static final String EVENT_VPC_PUBLIC_RATE_UPDATE = "VPC.PUBLICRATE.UPDATE";
587589

588590
// Network ACL
589591
public static final String EVENT_NETWORK_ACL_CREATE = "NETWORK.ACL.CREATE";

api/src/main/java/com/cloud/network/vpc/Vpc.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,10 @@ public enum State {
109109
boolean useRouterIpAsResolver();
110110

111111
boolean getKeepMacAddressOnPublicNic();
112+
113+
/**
114+
* @return the maximum network rate (Mbps) on the VR's public interface for this VPC;
115+
* null means fall back to the VPC offering value; 0 or negative means unlimited.
116+
*/
117+
Integer getPublicNetworkRate();
112118
}

api/src/main/java/com/cloud/network/vpc/VpcOffering.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,10 @@ public enum State {
8686
Boolean isSpecifyAsNumber();
8787

8888
boolean isConserveMode();
89+
90+
/**
91+
* @return maximum network rate in Mbps on the VR's public interface; null means unlimited
92+
*/
93+
Integer getPublicNetworkRate();
8994
}
95+

api/src/main/java/com/cloud/network/vpc/VpcService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ Vpc createVpc(long zoneId, long vpcOffId, long vpcOwnerId, String vpcName, Strin
6060
String ip4Dns1, String ip4Dns2, String ip6Dns1, String ip6Dns2, Boolean displayVpc, Integer publicMtu, Integer cidrSize,
6161
Long asNumber, List<Long> bgpPeerIds, Boolean useVrIpResolver, boolean keepMacAddressOnPublicNic) throws ResourceAllocationException;
6262

63+
/** Overload that also accepts a per-VPC public interface rate cap (Mbps). */
64+
Vpc createVpc(long zoneId, long vpcOffId, long vpcOwnerId, String vpcName, String displayText, String cidr, String networkDomain,
65+
String ip4Dns1, String ip4Dns2, String ip6Dns1, String ip6Dns2, Boolean displayVpc, Integer publicMtu, Integer cidrSize,
66+
Long asNumber, List<Long> bgpPeerIds, Boolean useVrIpResolver, boolean keepMacAddressOnPublicNic,
67+
Integer publicNetworkRate) throws ResourceAllocationException;
68+
6369
/**
6470
* Persists VPC record in the database
6571
*

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ public class ApiConstants {
709709
public static final String IS_USER_DEFINED = "isuserdefined";
710710
public static final String AVAILABILITY = "availability";
711711
public static final String NETWORKRATE = "networkrate";
712+
public static final String PUBLIC_NETWORK_RATE = "publicnetworkrate";
712713
public static final String HOST_TAGS = "hosttags";
713714
public static final String SSH_KEYPAIR = "keypair";
714715
public static final String SSH_KEYPAIRS = "keypairs";
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.cloudstack.api.command.admin.network;
19+
20+
import java.util.List;
21+
22+
import javax.inject.Inject;
23+
24+
import org.apache.cloudstack.acl.RoleType;
25+
import org.apache.cloudstack.api.APICommand;
26+
import org.apache.cloudstack.api.ApiConstants;
27+
import org.apache.cloudstack.api.BaseListCmd;
28+
import org.apache.cloudstack.api.Parameter;
29+
import org.apache.cloudstack.api.response.ListResponse;
30+
import org.apache.cloudstack.api.response.NetworkRateLimitResponse;
31+
import org.apache.cloudstack.api.response.NetworkResponse;
32+
import org.apache.cloudstack.api.response.VpcResponse;
33+
import org.apache.cloudstack.api.response.UserVmResponse;
34+
import org.apache.cloudstack.network.NetworkRateLimitService;
35+
36+
37+
@APICommand(name = "listNetworkRateLimits",
38+
description = "Lists the effective network rate limits on interfaces of Networks, VPCs, " +
39+
"Virtual Routers and Virtual Machines. Admin only.",
40+
responseObject = NetworkRateLimitResponse.class,
41+
authorized = {RoleType.Admin},
42+
requestHasSensitiveInfo = false,
43+
responseHasSensitiveInfo = false,
44+
since = "4.21.0")
45+
public class ListNetworkRateLimitsCmd extends BaseListCmd {
46+
47+
// ── Parameters ────────────────────────────────────────────────────────────
48+
49+
@Parameter(name = "type",
50+
type = CommandType.STRING,
51+
description = "Filter by resource type. Allowed values: vm, network, vpc, vr. " +
52+
"Leave empty to list all types.")
53+
private String type;
54+
55+
@Parameter(name = ApiConstants.NETWORK_ID,
56+
type = CommandType.UUID,
57+
entityType = NetworkResponse.class,
58+
description = "UUID of a specific network to query (optional)")
59+
private Long networkId;
60+
61+
@Parameter(name = ApiConstants.VPC_ID,
62+
type = CommandType.UUID,
63+
entityType = VpcResponse.class,
64+
description = "UUID of a specific VPC to query (optional)")
65+
private Long vpcId;
66+
67+
@Parameter(name = ApiConstants.VIRTUAL_MACHINE_ID,
68+
type = CommandType.UUID,
69+
entityType = UserVmResponse.class,
70+
description = "UUID of a specific VM to query (optional)")
71+
private Long vmId;
72+
73+
// ── Service ───────────────────────────────────────────────────────────────
74+
75+
@Inject
76+
NetworkRateLimitService networkRateLimitService;
77+
78+
// ── Accessors ─────────────────────────────────────────────────────────────
79+
80+
public String getType() {
81+
return type;
82+
}
83+
84+
public Long getNetworkId() {
85+
return networkId;
86+
}
87+
88+
public Long getVpcId() {
89+
return vpcId;
90+
}
91+
92+
public Long getVmId() {
93+
return vmId;
94+
}
95+
96+
// ── Execution ─────────────────────────────────────────────────────────────
97+
98+
@Override
99+
public void execute() {
100+
List<NetworkRateLimitResponse> entries = networkRateLimitService.listNetworkRateLimits(this);
101+
ListResponse<NetworkRateLimitResponse> response = new ListResponse<>();
102+
response.setResponses(entries, entries.size());
103+
response.setResponseName(getCommandName());
104+
setResponseObject(response);
105+
}
106+
}

api/src/main/java/org/apache/cloudstack/api/command/admin/vpc/CreateVPCOfferingCmd.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ public class CreateVPCOfferingCmd extends BaseAsyncCreateCmd {
165165
description = "True if the VPC offering is IP conserve mode enabled, allowing public IPs to be used across multiple VPC tiers. Default value is false")
166166
private Boolean conserveMode;
167167

168+
@Parameter(name = ApiConstants.PUBLIC_NETWORK_RATE,
169+
type = CommandType.INTEGER,
170+
since = "4.21.0",
171+
description = "Maximum network rate (Mbps) for the VR's public interface when this offering is used. Applies only to VPCs. Null or 0 means unlimited.")
172+
private Integer publicNetworkRate;
168173

169174
/////////////////////////////////////////////////////
170175
/////////////////// Accessors ///////////////////////
@@ -318,6 +323,10 @@ public boolean isConserveMode() {
318323
return BooleanUtils.toBoolean(conserveMode);
319324
}
320325

326+
public Integer getPublicNetworkRate() {
327+
return publicNetworkRate;
328+
}
329+
321330
@Override
322331
public void create() throws ResourceAllocationException {
323332
VpcOffering vpcOff = _vpcProvSvc.createVpcOffering(this);

api/src/main/java/org/apache/cloudstack/api/command/admin/vpc/UpdateVPCOfferingCmd.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ public class UpdateVPCOfferingCmd extends BaseAsyncCmd implements DomainAndZoneI
6868
@Parameter(name = ApiConstants.SORT_KEY, type = CommandType.INTEGER, description = "Sort key of the VPC offering, integer")
6969
private Integer sortKey;
7070

71+
@Parameter(name = ApiConstants.PUBLIC_NETWORK_RATE,
72+
type = CommandType.INTEGER,
73+
since = "4.21.0",
74+
description = "Maximum network rate (Mbps) for the VR's public interface. Use -1 to remove the limit. Applies only to VPCs.")
75+
private Integer publicNetworkRate;
76+
7177
/////////////////////////////////////////////////////
7278
/////////////////// Accessors ///////////////////////
7379
/////////////////////////////////////////////////////
@@ -100,6 +106,10 @@ public Integer getSortKey() {
100106
return sortKey;
101107
}
102108

109+
public Integer getPublicNetworkRate() {
110+
return publicNetworkRate;
111+
}
112+
103113
/////////////////////////////////////////////////////
104114
/////////////// API Implementation///////////////////
105115
/////////////////////////////////////////////////////

api/src/main/java/org/apache/cloudstack/api/command/user/network/UpdateNetworkCmd.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@ public class UpdateNetworkCmd extends BaseAsyncCustomIdCmd implements UserCmd {
110110
type = CommandType.BOOLEAN, since = "4.23.0", authorized = {RoleType.Admin})
111111
private Boolean keepMacAddressOnPublicNic;
112112

113+
@Parameter(name = ApiConstants.NETWORKRATE,
114+
type = CommandType.INTEGER,
115+
since = "4.21.0",
116+
description = "Maximum network rate (Mbps) to be applied to the network's VR guest interface. "
117+
+ "Overrides the value from the network offering. Use -1 to revert to the offering value. "
118+
+ "Applied dynamically on KVM without requiring a network restart.",
119+
authorized = {RoleType.Admin})
120+
private Integer networkRate;
121+
113122
/////////////////////////////////////////////////////
114123
/////////////////// Accessors ///////////////////////
115124
/////////////////////////////////////////////////////
@@ -195,6 +204,10 @@ public Boolean getKeepMacAddressOnPublicNic() {
195204
return keepMacAddressOnPublicNic;
196205
}
197206

207+
public Integer getNetworkRate() {
208+
return networkRate;
209+
}
210+
198211
/////////////////////////////////////////////////////
199212
/////////////// API Implementation///////////////////
200213
/////////////////////////////////////////////////////

api/src/main/java/org/apache/cloudstack/api/command/user/vm/UpdateVmNicCmd.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ public class UpdateVmNicCmd extends BaseAsyncCmd {
4747
@Parameter(name = ApiConstants.ENABLED, type = CommandType.BOOLEAN, description = "If true, sets the NIC state to UP; otherwise, sets the NIC state to DOWN")
4848
private Boolean enabled;
4949

50+
@Parameter(name = ApiConstants.NETWORKRATE,
51+
type = CommandType.INTEGER,
52+
since = "4.21.0",
53+
description = "Maximum network rate in Mbps to apply to this NIC. Applies dynamically on KVM via libvirt " +
54+
"without requiring a VM restart. Use -1 to remove any override and revert to the offering rate. " +
55+
"Persisted in nic_details so it survives VM stop/start.",
56+
authorized = {RoleType.Admin})
57+
private Integer networkRate;
58+
5059
public Long getNicId() {
5160
return nicId;
5261
}
@@ -55,6 +64,10 @@ public Boolean isEnabled() {
5564
return enabled;
5665
}
5766

67+
public Integer getNetworkRate() {
68+
return networkRate;
69+
}
70+
5871
@Override
5972
public String getEventType() {
6073
return EventTypes.EVENT_NIC_UPDATE;

0 commit comments

Comments
 (0)