Move 'netip' utils from 'ipam' to 'internal'.

Signed-off-by: Rob Murray <rob.murray@docker.com>
This commit is contained in:
Rob Murray 2023-12-06 11:54:45 +00:00
parent 029519a149
commit 0f9f9a132e
2 changed files with 13 additions and 12 deletions

View file

@ -1,4 +1,4 @@
package ipam package netiputil
import ( import (
"net" "net"
@ -7,7 +7,7 @@ import (
"github.com/docker/docker/libnetwork/ipbits" "github.com/docker/docker/libnetwork/ipbits"
) )
func toIPNet(p netip.Prefix) *net.IPNet { func ToIPNet(p netip.Prefix) *net.IPNet {
if !p.IsValid() { if !p.IsValid() {
return nil 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 { if ll := len(n.Mask); ll != net.IPv4len && ll != net.IPv6len {
return netip.Prefix{}, false return netip.Prefix{}, false
} }
@ -35,14 +35,14 @@ func toPrefix(n *net.IPNet) (netip.Prefix, bool) {
return netip.PrefixFrom(addr.Unmap(), ones), true 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())) return ipbits.Field(addr, bits, uint(addr.BitLen()))
} }
// subnetRange returns the amount to add to network.Addr() in order to yield the // subnetRange returns the amount to add to network.Addr() in order to yield the
// first and last addresses in subnet, respectively. // first and last addresses in subnet, respectively.
func subnetRange(network, subnet netip.Prefix) (start, end uint64) { func SubnetRange(network, subnet netip.Prefix) (start, end uint64) {
start = hostID(subnet.Addr(), uint(network.Bits())) start = HostID(subnet.Addr(), uint(network.Bits()))
end = start + (1 << uint64(subnet.Addr().BitLen()-subnet.Bits())) - 1 end = start + (1 << uint64(subnet.Addr().BitLen()-subnet.Bits())) - 1
return start, end return start, end
} }

View file

@ -9,6 +9,7 @@ import (
"github.com/containerd/log" "github.com/containerd/log"
"github.com/docker/docker/libnetwork/bitmap" "github.com/docker/docker/libnetwork/bitmap"
"github.com/docker/docker/libnetwork/internal/netiputil"
"github.com/docker/docker/libnetwork/ipamapi" "github.com/docker/docker/libnetwork/ipamapi"
"github.com/docker/docker/libnetwork/ipbits" "github.com/docker/docker/libnetwork/ipbits"
"github.com/docker/docker/libnetwork/types" "github.com/docker/docker/libnetwork/types"
@ -46,7 +47,7 @@ func newAddrSpace(predefined []*net.IPNet) (*addrSpace, error) {
pdf := make([]netip.Prefix, len(predefined)) pdf := make([]netip.Prefix, len(predefined))
for i, n := range predefined { for i, n := range predefined {
var ok bool var ok bool
pdf[i], ok = toPrefix(n) pdf[i], ok = netiputil.ToPrefix(n)
if !ok { if !ok {
return nil, fmt.Errorf("network at index %d (%v) is not in canonical form", i, n) 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 { if err != nil {
return "", nil, nil, err 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 { 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 "", 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 // 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) 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) { 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{}) { if ipr == (netip.Prefix{}) && prefAddress == (netip.Addr{}) {
ordinal, err = bitmask.SetAny(serial) ordinal, err = bitmask.SetAny(serial)
} else if prefAddress != (netip.Addr{}) { } else if prefAddress != (netip.Addr{}) {
ordinal = hostID(prefAddress, uint(base.Bits())) ordinal = netiputil.HostID(prefAddress, uint(base.Bits()))
err = bitmask.Set(ordinal) err = bitmask.Set(ordinal)
} else { } else {
start, end := subnetRange(base, ipr) start, end := netiputil.SubnetRange(base, ipr)
ordinal, err = bitmask.SetAnyInRange(start, end, serial) ordinal, err = bitmask.SetAnyInRange(start, end, serial)
} }