allocators

package
v0.0.0-...-28988eb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2024 License: MIT Imports: 6 Imported by: 4

Documentation

Overview

Package allocators provides the interface and the algorithm(s) for allocation of ipv6 prefixes of various sizes within a larger prefix. There are many many parallels with memory allocation.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNoAddrAvail = errors.New("no address available to allocate")

ErrNoAddrAvail is returned when we can't allocate an IP because there's no unallocated space left

View Source
var ErrOverflow = errors.New("Operation overflows")

ErrOverflow is returned when arithmetic operations on IPs carry bits over/under the 0th or 128th bit respectively

Functions

func AddPrefixes

func AddPrefixes(ip net.IP, n, unit uint64) (net.IP, error)

AddPrefixes returns the `n`th /`unit` subnet after the `ip` base subnet. It is the converse operation of Offset(), used to retrieve a prefix from the index within the allocator table

Example
fmt.Println(AddPrefixes(net.ParseIP("2001:db8::"), 0xff, 64))
fmt.Println(AddPrefixes(net.ParseIP("2001:db8::"), 0x1, 128))
fmt.Println(AddPrefixes(net.ParseIP("2001:db8::"), 0xff, 32))
fmt.Println(AddPrefixes(net.ParseIP("2001:db8::"), 0x1, 16))
fmt.Println(AddPrefixes(net.ParseIP("2001:db8::"), 0xff, 65))
// Error cases
fmt.Println(AddPrefixes(net.ParseIP("2001:db8::"), 0xff, 8))
fmt.Println(AddPrefixes(net.IP{10, 0, 0, 1}, 64, 32))
Output:

2001:db8:0:ff:: <nil>
2001:db8::1 <nil>
2001:eb7:: <nil>
2002:db8:: <nil>
2001:db8:0:7f:8000:: <nil>
<nil> Operation overflows
<nil> AddPrefixes needs 128-bit IPs

func Offset

func Offset(a, b net.IP, prefixLength int) (uint64, error)

Offset returns the absolute distance between addresses `a` and `b` in units of /`prefixLength` subnets. Both addresses will have a /`prefixLength` mask applied to them, any differences of less than that will be discarded If the distance is larger than 2^64 units of /`prefixLength` an error is returned

This function is used in allocators to index bitmaps by an offset from the first ip of the range

Example
fmt.Println(Offset(net.ParseIP("2001:db8:0:aabb::"), net.ParseIP("2001:db8:ff::34"), 0))
fmt.Println(Offset(net.ParseIP("2001:db8:0:aabb::"), net.ParseIP("2001:db8:ff::34"), 16))
fmt.Println(Offset(net.ParseIP("2001:db8:0:aabb::"), net.ParseIP("2001:db8:ff::34"), 32))
fmt.Println(Offset(net.ParseIP("2001:db8:0:aabb::"), net.ParseIP("2001:db8:ff::34"), 48))
fmt.Println(Offset(net.ParseIP("2001:db8:0:aabb::"), net.ParseIP("2001:db8:ff::34"), 64))
fmt.Println(Offset(net.ParseIP("2001:db8:0:aabb::"), net.ParseIP("2001:db8:ff::34"), 73))
fmt.Println(Offset(net.ParseIP("2001:db8:0:aabb::"), net.ParseIP("2001:db8:ff::34"), 80))
fmt.Println(Offset(net.ParseIP("2001:db8:0:aabb::"), net.ParseIP("2001:db8:ff::34"), 96))
fmt.Println(Offset(net.ParseIP("2001:db8:0:aabb::"), net.ParseIP("2001:db8:ff::34"), 112))
fmt.Println(Offset(net.ParseIP("2001:db8:0:aabb::"), net.ParseIP("2001:db8:ff::34"), 128))
Output:

0 <nil>
0 <nil>
0 <nil>
254 <nil>
16667973 <nil>
8534002176 <nil>
1092352278528 <nil>
71588398925611008 <nil>
0 Operation overflows
0 Operation overflows

Types

type Allocator

type Allocator interface {
	// Allocate finds a suitable prefix of the given size and returns it.
	//
	// hint is a prefix, which the client desires especially, and that the
	// allocator MAY try to return; the allocator SHOULD try to return a prefix of
	// the same size as the given hint prefix. The allocator MUST NOT return an
	// error if a prefix was successfully assigned, even if the prefix has nothing
	// in common with the hinted prefix
	Allocate(hint net.IPNet) (net.IPNet, error)

	// Free returns the prefix containing the given network to the pool
	//
	// Free may return a DoubleFreeError if the prefix being returned was not
	// previously allocated
	Free(net.IPNet) error
}

Allocator is the interface to the address allocator. It only finds and allocates blocks and is not concerned with lease-specific questions like expiration (ie garbage collection needs to be handled separately)

type ErrDoubleFree

type ErrDoubleFree struct {
	Loc net.IPNet
}

ErrDoubleFree is an error type returned by Allocator.Free() when a non-allocated block is passed

func (*ErrDoubleFree) Error

func (err *ErrDoubleFree) Error() string

String returns a human-readable error message for a DoubleFree error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL