Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions api/src/main/java/com/cloud/vm/NicProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class NicProfile implements InternalIdentity, Serializable {
String iPv4Dns1;
String iPv4Dns2;
String requestedIPv4;
boolean ipv4AllocationRaceCheck;

// IPv6
String iPv6Address;
Expand Down Expand Up @@ -405,6 +406,13 @@ public void setMtu(Integer mtu) {
this.mtu = mtu;
}

public boolean getIpv4AllocationRaceCheck() {
return this.ipv4AllocationRaceCheck;
}

public void setIpv4AllocationRaceCheck(boolean ipv4AllocationRaceCheck) {
this.ipv4AllocationRaceCheck = ipv4AllocationRaceCheck;
}

//
// OTHER METHODS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,32 @@ public void saveExtraDhcpOptions(final String networkUuid, final Long nicId, fin
}
}

private NicVO persistNicAfterRaceCheck(final NicVO nic, final Long networkId, final NicProfile profile, int deviceId) {
return Transaction.execute(new TransactionCallback<NicVO>() {
@Override
public NicVO doInTransaction(TransactionStatus status) {
NicVO vo = _nicDao.findByIp4AddressAndNetworkId(profile.getIPv4Address(), networkId);
if (vo == null) {
applyProfileToNic(nic, profile, deviceId);
vo = _nicDao.persist(nic);
return vo;
} else {
return null;
}
}
});
}

private NicVO checkForRaceAndAllocateNic(NicVO vo, Long networkId, final NicProfile profile, int deviceId) {
if (profile.getIpv4AllocationRaceCheck()) {
vo = persistNicAfterRaceCheck(vo, networkId, profile, deviceId);
} else {
applyProfileToNic(vo, profile, deviceId);
vo = _nicDao.persist(vo);
}
return vo;
}

@DB
@Override
public Pair<NicProfile, Integer> allocateNic(final NicProfile requested, final Network network, final Boolean isDefaultNic, int deviceId, final VirtualMachineProfile vm)
Expand All @@ -1024,30 +1050,43 @@ public Pair<NicProfile, Integer> allocateNic(final NicProfile requested, final N
if (requested != null && requested.getMode() == null) {
requested.setMode(network.getMode());
}
final NicProfile profile = guru.allocate(network, requested, vm);
if (profile == null) {
return null;
}

if (isDefaultNic != null) {
profile.setDefaultNic(isDefaultNic);
}
NicVO vo = null;
boolean retryIpAllocation;
do {
retryIpAllocation = false;
final NicProfile profile = guru.allocate(network, requested, vm);
if (profile == null) {
return null;
}

if (requested != null && requested.getMode() == null) {
profile.setMode(requested.getMode());
} else {
profile.setMode(network.getMode());
}
if (isDefaultNic != null) {
profile.setDefaultNic(isDefaultNic);
}

NicVO vo = new NicVO(guru.getName(), vm.getId(), network.getId(), vm.getType());
if (requested != null && requested.getMode() == null) {
profile.setMode(requested.getMode());
} else {
profile.setMode(network.getMode());
}

DataCenterVO dcVo = _dcDao.findById(network.getDataCenterId());
if (dcVo.getNetworkType() == NetworkType.Basic) {
configureNicProfileBasedOnRequestedIp(requested, profile, network);
}
vo = new NicVO(guru.getName(), vm.getId(), network.getId(), vm.getType());

DataCenterVO dcVo = _dcDao.findById(network.getDataCenterId());
if (dcVo.getNetworkType() == NetworkType.Basic) {
configureNicProfileBasedOnRequestedIp(requested, profile, network);
}

deviceId = applyProfileToNic(vo, profile, deviceId);
vo = _nicDao.persist(vo);
vo = checkForRaceAndAllocateNic(vo, network.getId(), profile, deviceId);
if (vo == null) {
if (requested.getRequestedIPv4() != null) {
throw new InsufficientVirtualNetworkCapacityException("Unable to acquire requested Guest IP address " + requested.getRequestedIPv4() + " for network " + network, DataCenter.class, dcVo.getId());
} else {
requested.setIPv4Address(null);
}
retryIpAllocation = true;
}
} while (retryIpAllocation);
Comment thread
sureshanaparti marked this conversation as resolved.

final Integer networkRate = _networkModel.getNetworkRate(network.getId(), vm.getId());
final NicProfile vmNic = new NicProfile(vo, network, vo.getBroadcastUri(), vo.getIsolationUri(), networkRate, _networkModel.isSecurityGroupSupportedInNetwork(network),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ public NicProfile allocate(final Network network, NicProfile nic, final VirtualM
} else {
guestIp = _ipAddrMgr.acquireGuestIpAddress(network, nic.getRequestedIPv4());
}
nic.setIpv4AllocationRaceCheck(true);
}
if (guestIp == null && network.getGuestType() != GuestType.L2 && !_networkModel.listNetworkOfferingServices(network.getNetworkOfferingId()).isEmpty()) {
throw new InsufficientVirtualNetworkCapacityException("Unable to acquire Guest IP" + " address for network " + network, DataCenter.class,
Expand Down