Skip to content

Commit 8dfc11a

Browse files
authored
router: Save PlaceHolder nic for VR if network does not have source nat (#3902)
This PR aims to fix the issue below Create a network offering for isolated network, services: Dns/Dhcp/Userdata, and enable it create a isolated network with the new offering create a vm check the guest IP of virtual router, restart network with cleanup check the guest IP of new virtual router The IP in step4 and step6 should be the same, but they are different actually.
1 parent 0501575 commit 8dfc11a

5 files changed

Lines changed: 43 additions & 10 deletions

File tree

api/src/main/java/com/cloud/vm/NicProfile.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ public String toString() {
423423
.append(iPv4Address)
424424
.append("-")
425425
.append(broadcastUri)
426+
.append("]")
426427
.toString();
427428
}
428-
}
429+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,6 +2438,9 @@ public void doInTransactionWithoutResult(TransactionStatus status) {
24382438
// log assign usage events for new offering
24392439
List<NicVO> nics = _nicDao.listByNetworkId(networkId);
24402440
for (NicVO nic : nics) {
2441+
if (nic.getReservationStrategy() == Nic.ReservationStrategy.PlaceHolder) {
2442+
continue;
2443+
}
24412444
long vmId = nic.getInstanceId();
24422445
VMInstanceVO vm = _vmDao.findById(vmId);
24432446
if (vm == null) {

server/src/main/java/com/cloud/network/guru/ExternalGuestNetworkGuru.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import com.cloud.network.IpAddressManager;
3131
import com.cloud.network.Network;
3232
import com.cloud.network.Network.GuestType;
33+
import com.cloud.network.Network.Provider;
34+
import com.cloud.network.Network.Service;
3335
import com.cloud.network.Network.State;
3436
import com.cloud.network.Networks.BroadcastDomainType;
3537
import com.cloud.network.PhysicalNetwork;
@@ -51,10 +53,12 @@
5153
import com.cloud.utils.exception.CloudRuntimeException;
5254
import com.cloud.utils.net.Ip;
5355
import com.cloud.utils.net.NetUtils;
56+
import com.cloud.vm.Nic;
5457
import com.cloud.vm.Nic.ReservationStrategy;
5558
import com.cloud.vm.NicProfile;
5659
import com.cloud.vm.NicVO;
5760
import com.cloud.vm.ReservationContext;
61+
import com.cloud.vm.VirtualMachine;
5862
import com.cloud.vm.VirtualMachineProfile;
5963
import org.apache.cloudstack.context.CallContext;
6064
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
@@ -261,6 +265,17 @@ public NicProfile allocate(Network config, NicProfile nic, VirtualMachineProfile
261265
profile.setIPv4Netmask(null);
262266
}
263267

268+
if (config.getVpcId() == null && vm.getType() == VirtualMachine.Type.DomainRouter) {
269+
boolean isPublicNetwork = _networkModel.isProviderSupportServiceInNetwork(config.getId(), Service.SourceNat, Provider.VirtualRouter);
270+
if (!isPublicNetwork) {
271+
Nic placeholderNic = _networkModel.getPlaceholderNicForRouter(config, null);
272+
if (placeholderNic == null) {
273+
s_logger.debug("Saving placeholder nic with ip4 address " + profile.getIPv4Address() +
274+
" and ipv6 address " + profile.getIPv6Address() + " for the network " + config);
275+
_networkMgr.savePlaceholderNic(config, profile.getIPv4Address(), profile.getIPv6Address(), VirtualMachine.Type.DomainRouter);
276+
}
277+
}
278+
}
264279
return profile;
265280
}
266281

server/src/main/java/com/cloud/network/guru/GuestNetworkGuru.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import com.cloud.utils.db.TransactionStatus;
7676
import com.cloud.utils.exception.CloudRuntimeException;
7777
import com.cloud.utils.net.NetUtils;
78+
import com.cloud.vm.Nic;
7879
import com.cloud.vm.Nic.ReservationStrategy;
7980
import com.cloud.vm.NicProfile;
8081
import com.cloud.vm.ReservationContext;
@@ -372,15 +373,25 @@ public NicProfile allocate(final Network network, NicProfile nic, final VirtualM
372373

373374
if (isGateway) {
374375
guestIp = network.getGateway();
375-
} else if (vm.getVirtualMachine().getType() == VirtualMachine.Type.DomainRouter) {
376-
guestIp = _ipAddrMgr.acquireGuestIpAddressByPlacement(network, nic.getRequestedIPv4());
377376
} else {
378-
guestIp = _ipAddrMgr.acquireGuestIpAddress(network, nic.getRequestedIPv4());
379-
}
380-
381-
if (!isGateway && guestIp == null && network.getGuestType() != GuestType.L2 && !_networkModel.listNetworkOfferingServices(network.getNetworkOfferingId()).isEmpty()) {
382-
throw new InsufficientVirtualNetworkCapacityException("Unable to acquire Guest IP" + " address for network " + network, DataCenter.class,
383-
dc.getId());
377+
if (network.getGuestType() != GuestType.L2 && vm.getType() == VirtualMachine.Type.DomainRouter) {
378+
Nic placeholderNic = _networkModel.getPlaceholderNicForRouter(network, null);
379+
if (placeholderNic != null) {
380+
s_logger.debug("Nic got an ip address " + placeholderNic.getIPv4Address() + " stored in placeholder nic for the network " + network);
381+
guestIp = placeholderNic.getIPv4Address();
382+
}
383+
}
384+
if (guestIp == null) {
385+
if (vm.getVirtualMachine().getType() == VirtualMachine.Type.DomainRouter) {
386+
guestIp = _ipAddrMgr.acquireGuestIpAddressByPlacement(network, nic.getRequestedIPv4());
387+
} else {
388+
guestIp = _ipAddrMgr.acquireGuestIpAddress(network, nic.getRequestedIPv4());
389+
}
390+
}
391+
if (guestIp == null && network.getGuestType() != GuestType.L2 && !_networkModel.listNetworkOfferingServices(network.getNetworkOfferingId()).isEmpty()) {
392+
throw new InsufficientVirtualNetworkCapacityException("Unable to acquire Guest IP" + " address for network " + network, DataCenter.class,
393+
dc.getId());
394+
}
384395
}
385396

386397
nic.setIPv4Address(guestIp);

server/src/main/java/com/cloud/network/router/NetworkHelperImpl.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,8 +710,8 @@ public LinkedHashMap<Network, List<? extends NicProfile>> configureGuestNic(fina
710710
if (guestNetwork != null) {
711711
s_logger.debug("Adding nic for Virtual Router in Guest network " + guestNetwork);
712712
String defaultNetworkStartIp = null, defaultNetworkStartIpv6 = null;
713+
final Nic placeholder = _networkModel.getPlaceholderNicForRouter(guestNetwork, routerDeploymentDefinition.getPodId());
713714
if (!routerDeploymentDefinition.isPublicNetwork()) {
714-
final Nic placeholder = _networkModel.getPlaceholderNicForRouter(guestNetwork, routerDeploymentDefinition.getPodId());
715715
if (guestNetwork.getCidr() != null) {
716716
if (placeholder != null && placeholder.getIPv4Address() != null) {
717717
s_logger.debug("Requesting ipv4 address " + placeholder.getIPv4Address() + " stored in placeholder nic for the network "
@@ -744,6 +744,9 @@ public LinkedHashMap<Network, List<? extends NicProfile>> configureGuestNic(fina
744744
}
745745
}
746746
}
747+
} else if (placeholder != null) {
748+
// Remove placeholder nic if router has public network
749+
_nicDao.remove(placeholder.getId());
747750
}
748751

749752
final NicProfile gatewayNic = new NicProfile(defaultNetworkStartIp, defaultNetworkStartIpv6);

0 commit comments

Comments
 (0)