فهرست منبع

libnetwork/ipam: refactor prefix-overlap checks

I am finally convinced that, given two netip.Prefix values a and b, the
expression

    a.Contains(b.Addr()) || b.Contains(a.Addr())

is functionally equivalent to

    a.Overlaps(b)

The (netip.Prefix).Contains method works by masking the address with the
prefix's mask and testing whether the remaining most-significant bits
are equal to the same bits in the prefix. The (netip.Prefix).Overlaps
method works by masking the longer prefix to the length of the shorter
prefix and testing whether the remaining most-significant bits are
equal. This is equivalent to
shorterPrefix.Contains(longerPrefix.Addr()), therefore applying Contains
symmetrically to two prefixes will always yield the same result as
applying Overlaps to the two prefixes in either order.

Signed-off-by: Cory Snider <csnider@mirantis.com>
Cory Snider 1 سال پیش
والد
کامیت
7257c77e19
2فایلهای تغییر یافته به همراه6 افزوده شده و 5 حذف شده
  1. 1 1
      libnetwork/ipam/allocator.go
  2. 5 4
      libnetwork/ipam/structures.go

+ 1 - 1
libnetwork/ipam/allocator.go

@@ -214,7 +214,7 @@ func (aSpace *addrSpace) allocatePredefinedPool(ipV6 bool) (netip.Prefix, error)
 		}
 		}
 		// Shouldn't be necessary, but check prevents IP collisions should
 		// Shouldn't be necessary, but check prevents IP collisions should
 		// predefined pools overlap for any reason.
 		// predefined pools overlap for any reason.
-		if !aSpace.contains(nw) {
+		if !aSpace.overlaps(nw) {
 			aSpace.updatePredefinedStartIndex(i + 1)
 			aSpace.updatePredefinedStartIndex(i + 1)
 			err := aSpace.allocateSubnetL(nw, netip.Prefix{})
 			err := aSpace.allocateSubnetL(nw, netip.Prefix{})
 			if err != nil {
 			if err != nil {

+ 5 - 4
libnetwork/ipam/structures.go

@@ -107,7 +107,7 @@ func (aSpace *addrSpace) allocateSubnet(nw, sub netip.Prefix) error {
 func (aSpace *addrSpace) allocateSubnetL(nw, sub netip.Prefix) error {
 func (aSpace *addrSpace) allocateSubnetL(nw, sub netip.Prefix) error {
 	// If master pool, check for overlap
 	// If master pool, check for overlap
 	if sub == (netip.Prefix{}) {
 	if sub == (netip.Prefix{}) {
-		if aSpace.contains(nw) {
+		if aSpace.overlaps(nw) {
 			return ipamapi.ErrPoolOverlap
 			return ipamapi.ErrPoolOverlap
 		}
 		}
 		// This is a new master pool, add it along with corresponding bitmask
 		// This is a new master pool, add it along with corresponding bitmask
@@ -157,10 +157,11 @@ func (aSpace *addrSpace) releaseSubnet(nw, sub netip.Prefix) error {
 	return nil
 	return nil
 }
 }
 
 
-// contains checks whether nw is a superset or subset of any of the existing subnets in this address space.
-func (aSpace *addrSpace) contains(nw netip.Prefix) bool {
+// overlaps reports whether nw contains any IP addresses in common with any of
+// the existing subnets in this address space.
+func (aSpace *addrSpace) overlaps(nw netip.Prefix) bool {
 	for pool := range aSpace.subnets {
 	for pool := range aSpace.subnets {
-		if nw.Contains(pool.Addr()) || pool.Contains(nw.Addr()) {
+		if pool.Overlaps(nw) {
 			return true
 			return true
 		}
 		}
 	}
 	}