Skip to content

Commit ab680bc

Browse files
author
Alena Prokharchyk
committed
Start Virtual Router as a part of VPC start
Conflicts: server/src/com/cloud/network/NetworkManagerImpl.java
1 parent 9debd3a commit ab680bc

9 files changed

Lines changed: 195 additions & 110 deletions

‎server/src/com/cloud/network/ExternalFirewallDeviceManagerImpl.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public boolean manageGuestNetworkWithExternalFirewall(boolean add, Network netwo
380380
IPAddressVO sourceNatIp = null;
381381
if (!sharedSourceNat) {
382382
// Get the source NAT IP address for this network
383-
List<IPAddressVO> sourceNatIps = _networkMgr.listPublicIpAddressesInVirtualNetwork(network.getAccountId(), zoneId, true, null);
383+
List<IPAddressVO> sourceNatIps = _networkMgr.listPublicIpsAssignedToGuestNtwk(network.getAccountId(), zoneId, true, null);
384384

385385
if (sourceNatIps.size() != 1) {
386386
String errorMsg = "External firewall was unable to find the source NAT IP address for account " + account.getAccountName();

‎server/src/com/cloud/network/NetworkManager.java‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import com.cloud.network.guru.NetworkGuru;
4343
import com.cloud.network.rules.FirewallRule;
4444
import com.cloud.network.rules.StaticNat;
45+
import com.cloud.network.vpc.Vpc;
4546
import com.cloud.offering.NetworkOffering;
4647
import com.cloud.offerings.NetworkOfferingVO;
4748
import com.cloud.user.Account;
@@ -77,17 +78,6 @@ public interface NetworkManager extends NetworkService {
7778

7879
PublicIp assignPublicIpAddress(long dcId, Long podId, Account owner, VlanType type, Long networkId, String requestedIp, boolean isSystem) throws InsufficientAddressCapacityException;
7980

80-
/**
81-
* assigns a source nat ip address to an account within a network.
82-
*
83-
* @param owner
84-
* @param network
85-
* @param callerId
86-
* @return
87-
* @throws ConcurrentOperationException
88-
* @throws InsufficientAddressCapacityException
89-
*/
90-
PublicIp assignSourceNatIpAddress(Account owner, Network network, long callerId) throws ConcurrentOperationException, InsufficientAddressCapacityException;
9181

9282
/**
9383
* Do all of the work of releasing public ip addresses. Note that if this method fails, there can be side effects.
@@ -113,7 +103,7 @@ public interface NetworkManager extends NetworkService {
113103
* TODO
114104
* @return - list of IP addresses
115105
*/
116-
List<IPAddressVO> listPublicIpAddressesInVirtualNetwork(long accountId, long dcId, Boolean sourceNat, Long associatedNetworkId);
106+
List<IPAddressVO> listPublicIpsAssignedToGuestNtwk(long accountId, long dcId, Boolean sourceNat, Long associatedNetworkId);
117107

118108
List<NetworkVO> setupNetwork(Account owner, NetworkOfferingVO offering, DeploymentPlan plan, String name, String displayText, boolean isDefault)
119109
throws ConcurrentOperationException;
@@ -314,4 +304,23 @@ void allocateDirectIp(NicProfile nic, DataCenter dc,
314304
* @return
315305
*/
316306
NetworkElement getElementImplementingProvider(String providerName);
307+
308+
/**
309+
* @param owner
310+
* @param guestNetwork
311+
* @return
312+
* @throws ConcurrentOperationException
313+
* @throws InsufficientAddressCapacityException
314+
*/
315+
PublicIp assignSourceNatIpAddressToGuestNetwork(Account owner, Network guestNetwork) throws InsufficientAddressCapacityException, ConcurrentOperationException;
316+
317+
318+
/**
319+
* @param owner
320+
* @param vpc
321+
* @return
322+
* @throws ConcurrentOperationException
323+
* @throws InsufficientAddressCapacityException
324+
*/
325+
PublicIp assignSourceNatIpAddressToVpc(Account owner, Vpc vpc) throws InsufficientAddressCapacityException, ConcurrentOperationException;
317326
}

‎server/src/com/cloud/network/NetworkManagerImpl.java‎

Lines changed: 110 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
import com.cloud.network.rules.StaticNatRule;
150150
import com.cloud.network.rules.StaticNatRuleImpl;
151151
import com.cloud.network.rules.dao.PortForwardingRulesDao;
152+
import com.cloud.network.vpc.Vpc;
152153
import com.cloud.network.vpn.RemoteAccessVpnService;
153154
import com.cloud.offering.NetworkOffering;
154155
import com.cloud.offering.NetworkOffering.Availability;
@@ -354,11 +355,12 @@ public boolean canElementEnableIndividualServices(Provider provider) {
354355

355356
@Override
356357
public PublicIp assignPublicIpAddress(long dcId, Long podId, Account owner, VlanType type, Long networkId, String requestedIp, boolean isSystem) throws InsufficientAddressCapacityException {
357-
return fetchNewPublicIp(dcId, podId, null, owner, type, networkId, false, true, requestedIp, isSystem);
358+
return fetchNewPublicIp(dcId, podId, null, owner, type, networkId, false, true, requestedIp, isSystem, null);
358359
}
359360

360361
@DB
361-
public PublicIp fetchNewPublicIp(long dcId, Long podId, Long vlanDbId, Account owner, VlanType vlanUse, Long networkId, boolean sourceNat, boolean assign, String requestedIp, boolean isSystem)
362+
public PublicIp fetchNewPublicIp(long dcId, Long podId, Long vlanDbId, Account owner, VlanType vlanUse,
363+
Long guestNetworkId, boolean sourceNat, boolean assign, String requestedIp, boolean isSystem, Long vpcId)
362364
throws InsufficientAddressCapacityException {
363365
StringBuilder errorMessage = new StringBuilder("Unable to get ip adress in ");
364366
Transaction txn = Transaction.currentTxn();
@@ -384,8 +386,8 @@ public PublicIp fetchNewPublicIp(long dcId, Long podId, Long vlanDbId, Account o
384386

385387
// for direct network take ip addresses only from the vlans belonging to the network
386388
if (vlanUse == VlanType.DirectAttached) {
387-
sc.setJoinParameters("vlan", "networkId", networkId);
388-
errorMessage.append(", network id=" + networkId);
389+
sc.setJoinParameters("vlan", "networkId", guestNetworkId);
390+
errorMessage.append(", network id=" + guestNetworkId);
389391
}
390392
sc.setJoinParameters("vlan", "type", vlanUse);
391393

@@ -423,7 +425,8 @@ public PublicIp fetchNewPublicIp(long dcId, Long podId, Long vlanDbId, Account o
423425
addr.setState(assign ? IpAddress.State.Allocated : IpAddress.State.Allocating);
424426

425427
if (vlanUse != VlanType.DirectAttached || zone.getNetworkType() == NetworkType.Basic) {
426-
addr.setAssociatedWithNetworkId(networkId);
428+
addr.setAssociatedWithNetworkId(guestNetworkId);
429+
addr.setVpcId(vpcId);
427430
}
428431

429432
_ipAddressDao.update(addr.getId(), addr);
@@ -468,17 +471,80 @@ protected void markPublicIpAsAllocated(IPAddressVO addr) {
468471

469472
txn.commit();
470473
}
474+
475+
476+
@Override
477+
public PublicIp assignSourceNatIpAddressToVpc(Account owner, Vpc vpc) throws InsufficientAddressCapacityException, ConcurrentOperationException {
478+
long dcId = vpc.getZoneId();
479+
480+
List<IPAddressVO> addrs = listPublicIpsAssignedToVpc(owner.getId(), true, vpc.getId());
481+
482+
PublicIp ipToReturn = null;
483+
if (!addrs.isEmpty()) {
484+
IPAddressVO sourceNatIp = null;
485+
// Account already has ip addresses
486+
for (IPAddressVO addr : addrs) {
487+
if (addr.isSourceNat()) {
488+
sourceNatIp = addr;
489+
break;
490+
}
491+
}
471492

493+
assert (sourceNatIp != null) : "How do we get a bunch of ip addresses but none of them are source nat? " +
494+
"account=" + owner.getId() + "; vpc=" + vpc;
495+
ipToReturn = new PublicIp(sourceNatIp, _vlanDao.findById(sourceNatIp.getVlanId()),
496+
NetUtils.createSequenceBasedMacAddress(sourceNatIp.getMacAddress()));
497+
} else {
498+
ipToReturn = assignSourceNatIpAddress(owner, null, vpc.getId(), dcId);
499+
}
500+
501+
return ipToReturn;
502+
}
503+
472504
@Override
505+
public PublicIp assignSourceNatIpAddressToGuestNetwork(Account owner, Network guestNetwork) throws InsufficientAddressCapacityException, ConcurrentOperationException {
506+
assert (guestNetwork.getTrafficType() != null) : "You're asking for a source nat but your network " +
507+
"can't participate in source nat. What do you have to say for yourself?";
508+
long dcId = guestNetwork.getDataCenterId();
509+
List<IPAddressVO> addrs = listPublicIpsAssignedToGuestNtwk(owner.getId(), dcId, null, guestNetwork.getId());
510+
511+
PublicIp ipToReturn = null;
512+
if (!addrs.isEmpty()) {
513+
IPAddressVO sourceNatIp = null;
514+
// Account already has ip addresses
515+
for (IPAddressVO addr : addrs) {
516+
if (addr.isSourceNat()) {
517+
sourceNatIp = addr;
518+
break;
519+
}
520+
}
521+
522+
assert (sourceNatIp != null) : "How do we get a bunch of ip addresses but none of them are source nat? " +
523+
"account=" + owner.getId() + "; guestNetwork=" + guestNetwork;
524+
ipToReturn = new PublicIp(sourceNatIp, _vlanDao.findById(sourceNatIp.getVlanId()),
525+
NetUtils.createSequenceBasedMacAddress(sourceNatIp.getMacAddress()));
526+
} else {
527+
ipToReturn = assignSourceNatIpAddress(owner, guestNetwork.getId(), null, dcId);
528+
}
529+
530+
return ipToReturn;
531+
}
532+
473533
@DB
474-
public PublicIp assignSourceNatIpAddress(Account owner, Network network, long callerId) throws ConcurrentOperationException, InsufficientAddressCapacityException {
475-
assert (network.getTrafficType() != null) : "You're asking for a source nat but your network can't participate in source nat. What do you have to say for yourself?";
534+
public PublicIp assignSourceNatIpAddress(Account owner, Long guestNtwkId, Long vpcId, long dcId)
535+
throws ConcurrentOperationException, InsufficientAddressCapacityException {
476536

477-
long dcId = network.getDataCenterId();
478537
long ownerId = owner.getId();
479-
538+
539+
// Check that the maximum number of public IPs for the given accountId will not be exceeded
540+
try {
541+
_resourceLimitMgr.checkResourceLimit(owner, ResourceType.public_ip);
542+
} catch (ResourceAllocationException ex) {
543+
s_logger.warn("Failed to allocate resource of type " + ex.getResourceType() + " for account " + owner);
544+
throw new AccountLimitException("Maximum number of public IP addresses for account: " + owner.getAccountName() + " has been exceeded.");
545+
}
546+
480547
PublicIp ip = null;
481-
482548
Transaction txn = Transaction.currentTxn();
483549
try {
484550
txn.start();
@@ -493,51 +559,21 @@ public PublicIp assignSourceNatIpAddress(Account owner, Network network, long ca
493559
if (s_logger.isDebugEnabled()) {
494560
s_logger.debug("lock account " + ownerId + " is acquired");
495561
}
562+
563+
// If account has Account specific ip ranges, try to allocate ip from there
564+
Long vlanId = null;
565+
List<AccountVlanMapVO> maps = _accountVlanMapDao.listAccountVlanMapsByAccount(ownerId);
566+
if (maps != null && !maps.isEmpty()) {
567+
vlanId = maps.get(0).getVlanDbId();
568+
}
496569

497-
IPAddressVO sourceNat = null;
498-
List<IPAddressVO> addrs = listPublicIpAddressesInVirtualNetwork(ownerId, dcId, null, network.getId());
499-
if (addrs.size() == 0) {
500-
501-
// Check that the maximum number of public IPs for the given accountId will not be exceeded
502-
try {
503-
_resourceLimitMgr.checkResourceLimit(owner, ResourceType.public_ip);
504-
} catch (ResourceAllocationException ex) {
505-
s_logger.warn("Failed to allocate resource of type " + ex.getResourceType() + " for account " + owner);
506-
throw new AccountLimitException("Maximum number of public IP addresses for account: " + owner.getAccountName() + " has been exceeded.");
507-
}
508-
509-
if (s_logger.isDebugEnabled()) {
510-
s_logger.debug("assigning a new ip address in " + dcId + " to " + owner);
511-
}
512-
513-
// If account has Account specific ip ranges, try to allocate ip from there
514-
Long vlanId = null;
515-
List<AccountVlanMapVO> maps = _accountVlanMapDao.listAccountVlanMapsByAccount(ownerId);
516-
if (maps != null && !maps.isEmpty()) {
517-
//check if the ips from this vlan are associated with this network
518-
List<IPAddressVO> ips = _ipAddressDao.listByVlanId(maps.get(0).getVlanDbId());
519-
if (ips != null && !ips.isEmpty() && ips.get(0).getAssociatedWithNetworkId() == network.getId()) {
520-
vlanId = maps.get(0).getVlanDbId();
521-
}
522-
}
523-
524-
ip = fetchNewPublicIp(dcId, null, vlanId, owner, VlanType.VirtualNetwork, network.getId(), true, false, null, false);
525-
sourceNat = ip.ip();
526570

527-
markPublicIpAsAllocated(sourceNat);
528-
_ipAddressDao.update(sourceNat.getId(), sourceNat);
529-
} else {
530-
// Account already has ip addresses
531-
for (IPAddressVO addr : addrs) {
532-
if (addr.isSourceNat()) {
533-
sourceNat = addr;
534-
break;
535-
}
536-
}
571+
ip = fetchNewPublicIp(dcId, null, vlanId, owner, VlanType.VirtualNetwork, guestNtwkId,
572+
true, false, null, false, vpcId);
573+
IPAddressVO sourceNatIp = ip.ip();
537574

538-
assert (sourceNat != null) : "How do we get a bunch of ip addresses but none of them are source nat? account=" + ownerId + "; dc=" + dcId;
539-
ip = new PublicIp(sourceNat, _vlanDao.findById(sourceNat.getVlanId()), NetUtils.createSequenceBasedMacAddress(sourceNat.getMacAddress()));
540-
}
575+
markPublicIpAsAllocated(sourceNatIp);
576+
_ipAddressDao.update(sourceNatIp.getId(), sourceNatIp);
541577

542578
txn.commit();
543579
return ip;
@@ -1013,8 +1049,7 @@ public IpAddress allocateIP(long networkId, Account ipOwner, boolean isSystem) t
10131049
}
10141050

10151051
// Check that network belongs to IP owner - skip this check for Basic zone as there is just one guest network,
1016-
// and it
1017-
// belongs to the system
1052+
// and it belongs to the system
10181053
if (zone.getNetworkType() != NetworkType.Basic && network.getAccountId() != ipOwner.getId()) {
10191054
throw new InvalidParameterValueException("The owner of the network is not the same as owner of the IP");
10201055
}
@@ -1060,14 +1095,15 @@ public IpAddress allocateIP(long networkId, Account ipOwner, boolean isSystem) t
10601095

10611096
if (!sharedSourceNat) {
10621097
// First IP address should be source nat when it's being associated with Guest Virtual network
1063-
List<IPAddressVO> addrs = listPublicIpAddressesInVirtualNetwork(ownerId, zone.getId(), true, networkId);
1098+
List<IPAddressVO> addrs = listPublicIpsAssignedToGuestNtwk(ownerId, zone.getId(), true, networkId);
10641099

10651100
if (addrs.isEmpty() && network.getGuestType() == Network.GuestType.Isolated) {
10661101
isSourceNat = true;
10671102
}
10681103
}
10691104

1070-
ip = fetchNewPublicIp(zone.getId(), null, null, ipOwner, vlanType, network.getId(), isSourceNat, assign, null, isSystem);
1105+
ip = fetchNewPublicIp(zone.getId(), null, null, ipOwner, vlanType, network.getId(),
1106+
isSourceNat, assign, null, isSystem, network.getVpcId());
10711107

10721108
if (ip == null) {
10731109
InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Unable to find available public IP addresses", DataCenter.class, zone.getId());
@@ -1343,6 +1379,7 @@ public boolean configure(final String name, final Map<String, Object> params) th
13431379
IpAddressSearch = _ipAddressDao.createSearchBuilder();
13441380
IpAddressSearch.and("accountId", IpAddressSearch.entity().getAllocatedToAccountId(), Op.EQ);
13451381
IpAddressSearch.and("dataCenterId", IpAddressSearch.entity().getDataCenterId(), Op.EQ);
1382+
IpAddressSearch.and("vpcId", IpAddressSearch.entity().getVpcId(), Op.EQ);
13461383
IpAddressSearch.and("associatedWithNetworkId", IpAddressSearch.entity().getAssociatedWithNetworkId(), Op.EQ);
13471384
SearchBuilder<VlanVO> virtualNetworkVlanSB = _vlanDao.createSearchBuilder();
13481385
virtualNetworkVlanSB.and("vlanType", virtualNetworkVlanSB.entity().getVlanType(), Op.EQ);
@@ -1414,7 +1451,7 @@ protected NetworkManagerImpl() {
14141451
}
14151452

14161453
@Override
1417-
public List<IPAddressVO> listPublicIpAddressesInVirtualNetwork(long accountId, long dcId, Boolean sourceNat, Long associatedNetworkId) {
1454+
public List<IPAddressVO> listPublicIpsAssignedToGuestNtwk(long accountId, long dcId, Boolean sourceNat, Long associatedNetworkId) {
14181455
SearchCriteria<IPAddressVO> sc = IpAddressSearch.create();
14191456
sc.setParameters("accountId", accountId);
14201457
sc.setParameters("dataCenterId", dcId);
@@ -1429,6 +1466,19 @@ public List<IPAddressVO> listPublicIpAddressesInVirtualNetwork(long accountId, l
14291466

14301467
return _ipAddressDao.search(sc, null);
14311468
}
1469+
1470+
protected List<IPAddressVO> listPublicIpsAssignedToVpc(long accountId, Boolean sourceNat, long vpcId) {
1471+
SearchCriteria<IPAddressVO> sc = IpAddressSearch.create();
1472+
sc.setParameters("accountId", accountId);
1473+
sc.setParameters("vpcId", vpcId);
1474+
1475+
if (sourceNat != null) {
1476+
sc.addAnd("sourceNat", SearchCriteria.Op.EQ, sourceNat);
1477+
}
1478+
sc.setJoinParameters("virtualNetworkVlanSB", "vlanType", VlanType.VirtualNetwork);
1479+
1480+
return _ipAddressDao.search(sc, null);
1481+
}
14321482

14331483
@Override
14341484
public List<NetworkVO> setupNetwork(Account owner, NetworkOfferingVO offering, DeploymentPlan plan, String name, String displayText, boolean isDefault)
@@ -1613,7 +1663,8 @@ public void allocate(VirtualMachineProfile<? extends VMInstanceVO> vm, List<Pair
16131663
nics.add(vo);
16141664

16151665
Integer networkRate = getNetworkRate(config.getId(), vm.getId());
1616-
vm.addNic(new NicProfile(vo, network.first(), vo.getBroadcastUri(), vo.getIsolationUri(), networkRate, isSecurityGroupSupportedInNetwork(network.first()), getNetworkTag(vm.getHypervisorType(),
1666+
vm.addNic(new NicProfile(vo, network.first(), vo.getBroadcastUri(), vo.getIsolationUri(), networkRate,
1667+
isSecurityGroupSupportedInNetwork(network.first()), getNetworkTag(vm.getHypervisorType(),
16171668
network.first())));
16181669
}
16191670

@@ -1795,7 +1846,7 @@ private void implementNetworkElementsAndResources(DeployDestination dest, Reserv
17951846
if (ips.isEmpty()) {
17961847
s_logger.debug("Creating a source nat ip for " + network);
17971848
Account owner = _accountMgr.getAccount(network.getAccountId());
1798-
assignSourceNatIpAddress(owner, network, context.getCaller().getId());
1849+
assignSourceNatIpAddressToGuestNetwork(owner, network);
17991850
}
18001851
}
18011852

‎server/src/com/cloud/network/addr/PublicIp.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,20 @@ public Long getAllocatedInDomainId() {
180180
public boolean getSystem() {
181181
return _addr.getSystem();
182182
}
183+
184+
/* (non-Javadoc)
185+
* @see com.cloud.network.IpAddress#getVpcId()
186+
*/
187+
@Override
188+
public Long getVpcId() {
189+
return _addr.getVpcId();
190+
}
191+
192+
/* (non-Javadoc)
193+
* @see com.cloud.network.IpAddress#setVpcId(java.lang.Long)
194+
*/
195+
@Override
196+
public void setVpcId(Long vpcId) {
197+
_addr.setVpcId(vpcId);
198+
}
183199
}

0 commit comments

Comments
 (0)