Skip to content

Commit 1f14679

Browse files
JayapalUradiMurali Reddy
authored andcommitted
CLOUDSTACK-1828 Source Nat on private gateway feature
1 parent 101d89c commit 1f14679

23 files changed

Lines changed: 136 additions & 41 deletions

File tree

api/src/com/cloud/network/NetworkService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ IpAddress associateIPToNetwork(long ipId, long networkId) throws InsufficientAdd
138138
ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException;
139139

140140
/**
141+
*
141142
* @param networkName
142143
* @param displayText
143144
* @param physicalNetworkId
@@ -148,13 +149,14 @@ IpAddress associateIPToNetwork(long ipId, long networkId) throws InsufficientAdd
148149
* @param netmask
149150
* @param networkOwnerId
150151
* @param vpcId TODO
152+
* @param sourceNat
151153
* @return
152154
* @throws InsufficientCapacityException
153155
* @throws ConcurrentOperationException
154156
* @throws ResourceAllocationException
155157
*/
156158
Network createPrivateNetwork(String networkName, String displayText, long physicalNetworkId, String vlan,
157-
String startIp, String endIP, String gateway, String netmask, long networkOwnerId, Long vpcId)
159+
String startIp, String endIP, String gateway, String netmask, long networkOwnerId, Long vpcId, Boolean sourceNat)
158160
throws ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException;
159161

160162
/* Requests an IP address for the guest nic */

api/src/com/cloud/network/vpc/PrivateIp.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@ public interface PrivateIp {
4444
String getMacAddress();
4545

4646
long getNetworkId();
47+
boolean getSourceNat();
4748

4849
}

api/src/com/cloud/network/vpc/VpcGateway.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,8 @@ public enum State {
7777
* @return
7878
*/
7979
State getState();
80+
/**
81+
* @return
82+
*/
83+
boolean getSourceNat();
8084
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,22 @@ boolean startVpc(long vpcId, boolean destroyOnFailure) throws ConcurrentOperatio
163163
/**
164164
* Persists VPC private gateway in the Database.
165165
*
166+
*
166167
* @param vpcId TODO
167168
* @param physicalNetworkId
168169
* @param vlan
169170
* @param ipAddress
170171
* @param gateway
171172
* @param netmask
172173
* @param gatewayOwnerId
174+
* @param isSourceNat
173175
* @return
174176
* @throws InsufficientCapacityException
175177
* @throws ConcurrentOperationException
176178
* @throws ResourceAllocationException
177179
*/
178180
public PrivateGateway createVpcPrivateGateway(long vpcId, Long physicalNetworkId, String vlan, String ipAddress,
179-
String gateway, String netmask, long gatewayOwnerId) throws ResourceAllocationException,
181+
String gateway, String netmask, long gatewayOwnerId, Boolean isSourceNat) throws ResourceAllocationException,
180182
ConcurrentOperationException, InsufficientCapacityException;
181183

182184
/**

api/src/org/apache/cloudstack/api/command/admin/vpc/CreatePrivateGatewayCmd.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public class CreatePrivateGatewayCmd extends BaseAsyncCreateCmd {
6969
required=true, description="the VPC network belongs to")
7070
private Long vpcId;
7171

72+
@Parameter(name=ApiConstants.SOURCE_NAT_SUPPORTED, type=CommandType.BOOLEAN, required=false,
73+
description="source NAT supported value. Default value false. If 'true' source NAT is enabled on the private gateway" +
74+
" 'false': sourcenat is not supported")
75+
private Boolean isSourceNat;
76+
7277
/////////////////////////////////////////////////////
7378
/////////////////// Accessors ///////////////////////
7479
/////////////////////////////////////////////////////
@@ -97,6 +102,13 @@ public Long getVpcId() {
97102
return vpcId;
98103
}
99104

105+
public Boolean getIsSourceNat () {
106+
if (isSourceNat == null) {
107+
return false;
108+
}
109+
return true;
110+
}
111+
100112
/////////////////////////////////////////////////////
101113
/////////////// API Implementation///////////////////
102114
/////////////////////////////////////////////////////
@@ -111,7 +123,7 @@ public void create() throws ResourceAllocationException {
111123
PrivateGateway result = null;
112124
try {
113125
result = _vpcService.createVpcPrivateGateway(getVpcId(), getPhysicalNetworkId(),
114-
getVlan(), getStartIp(), getGateway(), getNetmask(), getEntityOwnerId());
126+
getVlan(), getStartIp(), getGateway(), getNetmask(), getEntityOwnerId(), getIsSourceNat());
115127
} catch (InsufficientCapacityException ex){
116128
s_logger.info(ex);
117129
s_logger.trace(ex);

api/src/org/apache/cloudstack/api/response/PrivateGatewayResponse.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public class PrivateGatewayResponse extends BaseResponse implements ControlledEn
7676
private String state;
7777

7878

79+
@SerializedName(ApiConstants.SOURCE_NAT_SUPPORTED) @Param(description = "Souce Nat enable status")
80+
private Boolean sourceNat;
81+
82+
7983
@Override
8084
public String getObjectId() {
8185
return this.id;
@@ -145,5 +149,11 @@ public void setProjectName(String projectName) {
145149
public void setState(String state) {
146150
this.state = state;
147151
}
152+
153+
public void setSourceNat(Boolean sourceNat) {
154+
this.sourceNat = sourceNat;
155+
}
156+
157+
148158
}
149159

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,13 +863,16 @@ private SetPortForwardingRulesAnswer execute(SetPortForwardingRulesVpcCommand cm
863863
}
864864

865865
public void assignVpcIpToRouter(final String routerIP, final boolean add, final String pubIP,
866-
final String nicname, final String gateway, final String netmask, final String subnet) throws InternalErrorException {
866+
final String nicname, final String gateway, final String netmask, final String subnet, boolean sourceNat) throws InternalErrorException {
867867
String args = "";
868+
String snatArgs = "";
868869

869870
if (add) {
870871
args += " -A ";
872+
snatArgs += " -A ";
871873
} else {
872874
args += " -D ";
875+
snatArgs += " -D ";
873876
}
874877

875878
args += " -l ";
@@ -887,6 +890,16 @@ public void assignVpcIpToRouter(final String routerIP, final boolean add, final
887890
if (result != null) {
888891
throw new InternalErrorException("KVM plugin \"vpc_ipassoc\" failed:"+result);
889892
}
893+
if (sourceNat) {
894+
snatArgs += " -l " + pubIP;
895+
snatArgs += " -c " + nicname;
896+
897+
result = routerProxy("vpc_privateGateway.sh", routerIP, snatArgs);
898+
if (result != null) {
899+
throw new InternalErrorException("KVM plugin \"vpc_privateGateway\" failed:"+result);
900+
}
901+
902+
}
890903
}
891904

892905
private SetStaticRouteAnswer execute(SetStaticRouteCommand cmd) {

patches/systemvm/debian/config/opt/cloud/bin/vpc_privateGateway.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fi
9191

9292
if [ "$Dflag" == "1" ]
9393
then
94-
remove_sat $publicIp
94+
remove_snat $publicIp
9595
unlock_exit $? $lock $locked
9696
fi
9797

plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1756,7 +1756,7 @@ protected IpAssocAnswer execute(IpAssocVpcCommand cmd) {
17561756
String netmask = Long.toString(NetUtils.getCidrSize(ip.getVlanNetmask()));
17571757
String subnet = NetUtils.getSubNet(ip.getPublicIp(), ip.getVlanNetmask());
17581758
_virtRouterResource.assignVpcIpToRouter(routerIP, ip.isAdd(), ip.getPublicIp(),
1759-
nicName, ip.getVlanGateway(), netmask, subnet);
1759+
nicName, ip.getVlanGateway(), netmask, subnet, ip.isSourceNat());
17601760
results[i++] = ip.getPublicIp() + " - success";
17611761
}
17621762

plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,10 +1441,14 @@ protected void assignVPCPublicIpAddress(String domrName, String routerIp, IpAddr
14411441
}
14421442

14431443
String args = "";
1444+
String snatArgs = "";
1445+
14441446
if (ip.isAdd()) {
14451447
args += " -A ";
1448+
snatArgs += " -A ";
14461449
} else {
14471450
args += " -D ";
1451+
snatArgs += " -D ";
14481452
}
14491453

14501454
args += " -l ";
@@ -1468,6 +1472,21 @@ protected void assignVPCPublicIpAddress(String domrName, String routerIp, IpAddr
14681472
if (!result.first()) {
14691473
throw new InternalErrorException("Unable to assign public IP address");
14701474
}
1475+
1476+
if (ip.isSourceNat()) {
1477+
snatArgs += " -l ";
1478+
snatArgs += ip.getPublicIp();
1479+
snatArgs += " -c ";
1480+
snatArgs += "eth" + ethDeviceNum;
1481+
1482+
Pair<Boolean, String> result = SshHelper.sshExecute(routerIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null,
1483+
"/opt/cloud/bin/vpc_privateGateway.sh " + args);
1484+
1485+
if (!result.first()) {
1486+
throw new InternalErrorException("Unable to assign public IP address");
1487+
}
1488+
1489+
}
14711490
}
14721491

14731492
protected void assignPublicIpAddress(VirtualMachineMO vmMo, final String vmName, final String privateIpAddress, final String publicIpAddress, final boolean add, final boolean firstIP,

0 commit comments

Comments
 (0)