Repository navigation
network: custom DHCP range for isolated networks (API and UI) #14217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rootnomad
wants to merge
4
commits into
apache:main
Choose a base branch
from
rootnomad:feature/isolated-dhcp-range
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
077b75c
network: allow a custom DHCP range for isolated guest networks
6d687a9
ui: expose the custom DHCP range on isolated networks
dfdc7f5
network: validate the isolated DHCP range before persisting and guard…
rootnomad 8f911ef
network: store the isolated network custom DHCP range as network details
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2682,10 +2682,23 @@ public Network createGuestNetwork(CreateNetworkCmd cmd) throws InsufficientCapac | |
| } | ||
| } | ||
|
|
||
| // For an isolated network a start/end IP defines a custom DHCP range within the CIDR that guest IPs are | ||
| // allocated from; without it the whole CIDR is used, which is the default. Validate it up front so a bad | ||
| // range is rejected before the network is persisted, rather than throwing after commit and leaving an | ||
| // orphaned network. | ||
| boolean hasIsolatedDhcpRange = ntwkOff.getGuestType() == GuestType.Isolated && StringUtils.isNotBlank(startIP); | ||
| if (hasIsolatedDhcpRange) { | ||
| validateIsolatedNetworkDhcpRange(startIP, endIP, cidr, gateway); | ||
| } | ||
|
Comment on lines
+2685
to
+2692
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you extract this as a method with javadoc? |
||
|
|
||
| Network network = commitNetwork(networkOfferingId, gateway, startIP, endIP, netmask, networkDomain, vlanId, bypassVlanOverlapCheck, name, displayText, caller, physicalNetworkId, zone.getId(), | ||
| domainId, isDomainSpecific, subdomainAccess, vpcId, startIPv6, endIPv6, ip6Gateway, ip6Cidr, displayNetwork, aclId, secondaryVlanId, privateVlanType, ntwkOff, pNtwk, aclType, owner, cidr, createVlan, | ||
| externalId, routerIPv4, routerIPv6, associatedNetwork, ip4Dns1, ip4Dns2, ip6Dns1, ip6Dns2, interfaceMTUs, networkCidrSize, keepMacAddressOnPublicNic); | ||
|
|
||
| if (hasIsolatedDhcpRange) { | ||
| storeIsolatedNetworkDhcpRange(network.getId(), startIP, endIP); | ||
|
RosiKyu marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| // retrieve, acquire and associate the correct IP addresses | ||
| checkAndSetRouterSourceNatIp(owner, cmd, network); | ||
|
|
||
|
|
@@ -2720,6 +2733,51 @@ private boolean isNonVpcNetworkSupportingDynamicRouting(NetworkOffering networkO | |
| return !networkOffering.isForVpc() && NetworkOffering.RoutingMode.Dynamic == networkOffering.getRoutingMode(); | ||
| } | ||
|
|
||
| protected void validateIsolatedNetworkDhcpRange(String startIP, String endIP, String cidr, String gateway) { | ||
| if (endIP == null) { | ||
| endIP = startIP; | ||
| } | ||
| if (!NetUtils.isIpWithInCidrRange(startIP, cidr) || !NetUtils.isIpWithInCidrRange(endIP, cidr)) { | ||
| throw new InvalidParameterValueException(String.format("The DHCP range %s-%s is not within the network CIDR %s", startIP, endIP, cidr)); | ||
| } | ||
| if (NetUtils.ip2Long(startIP) > NetUtils.ip2Long(endIP)) { | ||
| throw new InvalidParameterValueException(String.format("The DHCP start IP %s is greater than the end IP %s", startIP, endIP)); | ||
| } | ||
| if (gateway != null && NetUtils.ip2Long(gateway) >= NetUtils.ip2Long(startIP) && NetUtils.ip2Long(gateway) <= NetUtils.ip2Long(endIP)) { | ||
| throw new InvalidParameterValueException(String.format("The DHCP range %s-%s must not include the gateway %s", startIP, endIP, gateway)); | ||
| } | ||
| } | ||
|
|
||
| protected void storeIsolatedNetworkDhcpRange(long networkId, String startIP, String endIP) { | ||
| NetworkVO network = _networksDao.findById(networkId); | ||
| if (endIP == null) { | ||
| endIP = startIP; | ||
| } | ||
| validateIsolatedNetworkDhcpRange(startIP, endIP, network.getCidr(), network.getGateway()); | ||
| _networkDetailsDao.persist(new NetworkDetailVO(networkId, Network.dhcpStartIp, startIP, true)); | ||
| _networkDetailsDao.persist(new NetworkDetailVO(networkId, Network.dhcpEndIp, endIP, true)); | ||
| } | ||
|
|
||
| /** | ||
| * Reject applying a Guest VM CIDR reservation that would leave the isolated network's custom DHCP | ||
| * range (stored as network details) partly or wholly outside the reserved CIDR, which would starve | ||
| * guest IP allocation. A network with no custom range is left untouched. | ||
| */ | ||
| protected void assertCustomDhcpRangeWithinGuestVmCidr(Network network, long startIp, long endIp, String guestVmCidr) { | ||
| NetworkDetailVO dhcpStartDetail = _networkDetailsDao.findDetail(network.getId(), Network.dhcpStartIp); | ||
| NetworkDetailVO dhcpEndDetail = _networkDetailsDao.findDetail(network.getId(), Network.dhcpEndIp); | ||
| if (dhcpStartDetail == null || dhcpEndDetail == null | ||
| || StringUtils.isAnyBlank(dhcpStartDetail.getValue(), dhcpEndDetail.getValue())) { | ||
| return; | ||
| } | ||
| long dhcpStart = NetUtils.ip2Long(dhcpStartDetail.getValue()); | ||
| long dhcpEnd = NetUtils.ip2Long(dhcpEndDetail.getValue()); | ||
| if (dhcpStart < startIp || dhcpEnd > endIp) { | ||
| throw new InvalidParameterValueException(String.format("The network has a custom DHCP range %s-%s that falls outside the requested Guest VM CIDR %s. Remove or adjust the DHCP range before applying the reservation.", | ||
| dhcpStartDetail.getValue(), dhcpEndDetail.getValue(), guestVmCidr)); | ||
| } | ||
| } | ||
|
|
||
| private void validateNetworkCreationSupported(long zoneId, String zoneName, GuestType guestType) { | ||
| NsxProviderVO nsxProviderVO = nsxProviderDao.findByZoneId(zoneId); | ||
| if (Objects.nonNull(nsxProviderVO) && GuestType.L2.equals(guestType)) { | ||
|
|
@@ -4271,6 +4329,10 @@ public Network updateGuestNetwork(final UpdateNetworkCmd cmd) { | |
| } | ||
| } | ||
|
|
||
| // A custom DHCP range must stay within the reserved Guest VM CIDR; reject a reservation that would | ||
| // leave the stored range partly or wholly outside it, otherwise guest IP allocation could be starved. | ||
| assertCustomDhcpRangeWithinGuestVmCidr(network, startIp, endIp, guestVmCidr); | ||
|
|
||
| // In some scenarios even though guesVmCidr and network CIDR do not appear similar but | ||
| // the IP ranges exactly matches, in these special cases make sure no Reservation gets applied | ||
| if (network.getNetworkCidr() == null) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.