From 0f9f9a132eb27c2a8ed8f3115e560eb90b40fadf Mon Sep 17 00:00:00 2001 From: Rob Murray Date: Wed, 6 Dec 2023 11:54:45 +0000 Subject: [PATCH] Move 'netip' utils from 'ipam' to 'internal'. Signed-off-by: Rob Murray --- .../utils.go => internal/netiputil/netiputil.go} | 12 ++++++------ libnetwork/ipam/allocator.go | 13 +++++++------ 2 files changed, 13 insertions(+), 12 deletions(-) rename libnetwork/{ipam/utils.go => internal/netiputil/netiputil.go} (75%) diff --git a/libnetwork/ipam/utils.go b/libnetwork/internal/netiputil/netiputil.go similarity index 75% rename from libnetwork/ipam/utils.go rename to libnetwork/internal/netiputil/netiputil.go index 4e7d72c2e0..3fa543be92 100644 --- a/libnetwork/ipam/utils.go +++ b/libnetwork/internal/netiputil/netiputil.go @@ -1,4 +1,4 @@ -package ipam +package netiputil import ( "net" @@ -7,7 +7,7 @@ import ( "github.com/docker/docker/libnetwork/ipbits" ) -func toIPNet(p netip.Prefix) *net.IPNet { +func ToIPNet(p netip.Prefix) *net.IPNet { if !p.IsValid() { return nil } @@ -17,7 +17,7 @@ func toIPNet(p netip.Prefix) *net.IPNet { } } -func toPrefix(n *net.IPNet) (netip.Prefix, bool) { +func ToPrefix(n *net.IPNet) (netip.Prefix, bool) { if ll := len(n.Mask); ll != net.IPv4len && ll != net.IPv6len { return netip.Prefix{}, false } @@ -35,14 +35,14 @@ func toPrefix(n *net.IPNet) (netip.Prefix, bool) { return netip.PrefixFrom(addr.Unmap(), ones), true } -func hostID(addr netip.Addr, bits uint) uint64 { +func HostID(addr netip.Addr, bits uint) uint64 { return ipbits.Field(addr, bits, uint(addr.BitLen())) } // subnetRange returns the amount to add to network.Addr() in order to yield the // first and last addresses in subnet, respectively. -func subnetRange(network, subnet netip.Prefix) (start, end uint64) { - start = hostID(subnet.Addr(), uint(network.Bits())) +func SubnetRange(network, subnet netip.Prefix) (start, end uint64) { + start = HostID(subnet.Addr(), uint(network.Bits())) end = start + (1 << uint64(subnet.Addr().BitLen()-subnet.Bits())) - 1 return start, end } diff --git a/libnetwork/ipam/allocator.go b/libnetwork/ipam/allocator.go index 55b51643d1..79376c49bb 100644 --- a/libnetwork/ipam/allocator.go +++ b/libnetwork/ipam/allocator.go @@ -9,6 +9,7 @@ import ( "github.com/containerd/log" "github.com/docker/docker/libnetwork/bitmap" + "github.com/docker/docker/libnetwork/internal/netiputil" "github.com/docker/docker/libnetwork/ipamapi" "github.com/docker/docker/libnetwork/ipbits" "github.com/docker/docker/libnetwork/types" @@ -46,7 +47,7 @@ func newAddrSpace(predefined []*net.IPNet) (*addrSpace, error) { pdf := make([]netip.Prefix, len(predefined)) for i, n := range predefined { var ok bool - pdf[i], ok = toPrefix(n) + pdf[i], ok = netiputil.ToPrefix(n) if !ok { return nil, fmt.Errorf("network at index %d (%v) is not in canonical form", i, n) } @@ -91,7 +92,7 @@ func (a *Allocator) RequestPool(addressSpace, requestedPool, requestedSubPool st if err != nil { return "", nil, nil, err } - return k.String(), toIPNet(k.Subnet), nil, nil + return k.String(), netiputil.ToIPNet(k.Subnet), nil, nil } if k.Subnet, err = netip.ParsePrefix(requestedPool); err != nil { @@ -119,7 +120,7 @@ func (a *Allocator) RequestPool(addressSpace, requestedPool, requestedSubPool st return "", nil, nil, err } - return k.String(), toIPNet(k.Subnet), nil, nil + return k.String(), netiputil.ToIPNet(k.Subnet), nil, nil } // ReleasePool releases the address pool identified by the passed id @@ -336,7 +337,7 @@ func (aSpace *addrSpace) releaseAddress(nw, sub netip.Prefix, address netip.Addr defer log.G(context.TODO()).Debugf("Released address Address:%v Sequence:%s", address, p.addrs) - return p.addrs.Unset(hostID(address, uint(nw.Bits()))) + return p.addrs.Unset(netiputil.HostID(address, uint(nw.Bits()))) } func getAddress(base netip.Prefix, bitmask *bitmap.Bitmap, prefAddress netip.Addr, ipr netip.Prefix, serial bool) (netip.Addr, error) { @@ -353,10 +354,10 @@ func getAddress(base netip.Prefix, bitmask *bitmap.Bitmap, prefAddress netip.Add if ipr == (netip.Prefix{}) && prefAddress == (netip.Addr{}) { ordinal, err = bitmask.SetAny(serial) } else if prefAddress != (netip.Addr{}) { - ordinal = hostID(prefAddress, uint(base.Bits())) + ordinal = netiputil.HostID(prefAddress, uint(base.Bits())) err = bitmask.Set(ordinal) } else { - start, end := subnetRange(base, ipr) + start, end := netiputil.SubnetRange(base, ipr) ordinal, err = bitmask.SetAnyInRange(start, end, serial) }