diff --git a/bitseq/sequence.go b/bitseq/sequence.go index d304f1c..094a74a 100644 --- a/bitseq/sequence.go +++ b/bitseq/sequence.go @@ -134,13 +134,13 @@ func (s *Sequence) FromByteArray(data []byte) error { } // GetFirstAvailable returns the byte and bit position of the first unset bit -func (h *Handle) GetFirstAvailable() (int, int) { +func (h *Handle) GetFirstAvailable() (int, int, error) { return GetFirstAvailable(h.Head) } // CheckIfAvailable checks if the bit correspondent to the specified ordinal is unset // If the ordinal is beyond the Sequence limits, a negative response is returned -func (h *Handle) CheckIfAvailable(ordinal int) (int, int) { +func (h *Handle) CheckIfAvailable(ordinal int) (int, int, error) { return CheckIfAvailable(h.Head, ordinal) } @@ -150,23 +150,23 @@ func (h *Handle) PushReservation(bytePos, bitPos int, release bool) { } // GetFirstAvailable looks for the first unset bit in passed mask -func GetFirstAvailable(head *Sequence) (int, int) { +func GetFirstAvailable(head *Sequence) (int, int, error) { byteIndex := 0 current := head for current != nil { if current.Block != blockMAX { bytePos, bitPos := current.GetAvailableBit() - return byteIndex + bytePos, bitPos + return byteIndex + bytePos, bitPos, nil } byteIndex += int(current.Count * blockBytes) current = current.Next } - return -1, -1 + return -1, -1, fmt.Errorf("no bit available") } // CheckIfAvailable checks if the bit correspondent to the specified ordinal is unset // If the ordinal is beyond the Sequence limits, a negative response is returned -func CheckIfAvailable(head *Sequence, ordinal int) (int, int) { +func CheckIfAvailable(head *Sequence, ordinal int) (int, int, error) { bytePos := ordinal / 8 bitPos := ordinal % 8 @@ -177,11 +177,11 @@ func CheckIfAvailable(head *Sequence, ordinal int) (int, int) { // Check whether the bit corresponding to the ordinal address is unset bitSel := uint32(blockFirstBit >> uint(inBlockBytePos*8+bitPos)) if current.Block&bitSel == 0 { - return bytePos, bitPos + return bytePos, bitPos, nil } } - return -1, -1 + return -1, -1, fmt.Errorf("requested bit is not available") } // Given the byte position and the sequences list head, return the pointer to the diff --git a/bitseq/sequence_test.go b/bitseq/sequence_test.go index cebdf43..b0936bf 100644 --- a/bitseq/sequence_test.go +++ b/bitseq/sequence_test.go @@ -127,7 +127,7 @@ func TestGetFirstAvailable(t *testing.T) { } for n, i := range input { - bytePos, bitPos := GetFirstAvailable(i.mask) + bytePos, bitPos, _ := GetFirstAvailable(i.mask) if bytePos != i.bytePos || bitPos != i.bitPos { t.Fatalf("Error in (%d) getFirstAvailable(). Expected (%d, %d). Got (%d, %d)", n, i.bytePos, i.bitPos, bytePos, bitPos) } @@ -201,7 +201,7 @@ func TestCheckIfAvailable(t *testing.T) { } for n, i := range input { - bytePos, bitPos := CheckIfAvailable(i.head, i.ordinal) + bytePos, bitPos, _ := CheckIfAvailable(i.head, i.ordinal) if bytePos != i.bytePos || bitPos != i.bitPos { t.Fatalf("Error in (%d) checkIfAvailable(ord:%d). Expected (%d, %d). Got (%d, %d)", n, i.ordinal, i.bytePos, i.bitPos, bytePos, bitPos) } diff --git a/idm/idm.go b/idm/idm.go new file mode 100644 index 0000000..f9b1c1e --- /dev/null +++ b/idm/idm.go @@ -0,0 +1,72 @@ +// Package idm manages resevation/release of numerical ids from a configured set of contiguos ids +package idm + +import ( + "fmt" + + "github.com/docker/libnetwork/bitseq" +) + +// Idm manages the reservation/release of numerical ids from a contiguos set +type Idm struct { + start uint32 + end uint32 + handle *bitseq.Handle +} + +// New returns an instance of id manager for a set of [start-end] numerical ids +func New(id string, start, end uint32) (*Idm, error) { + if id == "" { + return nil, fmt.Errorf("Invalid id") + } + if end <= start { + return nil, fmt.Errorf("Invalid set range: [%d, %d]", start, end) + } + return &Idm{start: start, end: end, handle: bitseq.NewHandle(id, 1+end-start)}, nil +} + +// GetID returns the first available id in the set +func (i *Idm) GetID() (uint32, error) { + if i.handle == nil { + return 0, fmt.Errorf("ID set is not initialized") + } + + bytePos, bitPos, err := i.handle.GetFirstAvailable() + if err != nil { + return 0, fmt.Errorf("no available ids") + } + id := i.start + uint32(bitPos+bytePos*8) + + // for sets which length is non multiple of 32 this check is needed + if i.end < id { + return 0, fmt.Errorf("no available ids") + } + + i.handle.PushReservation(bytePos, bitPos, false) + + return id, nil +} + +// GetSpecificID tries to reserve the specified id +func (i *Idm) GetSpecificID(id uint32) error { + if i.handle == nil { + return fmt.Errorf("ID set is not initialized") + } + + if id < i.start || id > i.end { + return fmt.Errorf("Requested id does not belong to the set") + } + + if bytePos, bitPos, err := i.handle.CheckIfAvailable(int(id - i.start)); err == nil { + i.handle.PushReservation(bytePos, bitPos, false) + return nil + } + + return fmt.Errorf("requested id is not available") +} + +// Release releases the specified id +func (i *Idm) Release(id uint32) { + ordinal := id - i.start + i.handle.PushReservation(int(ordinal/8), int(ordinal%8), true) +} diff --git a/idm/idm_test.go b/idm/idm_test.go new file mode 100644 index 0000000..9f94694 --- /dev/null +++ b/idm/idm_test.go @@ -0,0 +1,108 @@ +package idm + +import ( + "testing" +) + +func TestNew(t *testing.T) { + _, err := New("", 0, 1) + if err == nil { + t.Fatalf("Expected failure, but succeeded") + } + + _, err = New("myset", 1<<10, 0) + if err == nil { + t.Fatalf("Expected failure, but succeeded") + } + + i, err := New("myset", 0, 10) + if err != nil { + t.Fatalf("Unexpected failure: %v", err) + } + if i.handle == nil { + t.Fatalf("set is not initialized") + } + if i.start != 0 { + t.Fatalf("unexpected start") + } + if i.end != 10 { + t.Fatalf("unexpected end") + } +} + +func TestAllocate(t *testing.T) { + i, err := New("myids", 50, 52) + if err != nil { + t.Fatal(err) + } + + if err = i.GetSpecificID(49); err == nil { + t.Fatalf("Expected failure but succeeded") + } + + if err = i.GetSpecificID(53); err == nil { + t.Fatalf("Expected failure but succeeded") + } + + o, err := i.GetID() + if err != nil { + t.Fatal(err) + } + if o != 50 { + t.Fatalf("Unexpected first id returned: %d", o) + } + + err = i.GetSpecificID(50) + if err == nil { + t.Fatal(err) + } + + o, err = i.GetID() + if err != nil { + t.Fatal(err) + } + if o != 51 { + t.Fatalf("Unexpected id returned: %d", o) + } + + o, err = i.GetID() + if err != nil { + t.Fatal(err) + } + if o != 52 { + t.Fatalf("Unexpected id returned: %d", o) + } + + o, err = i.GetID() + if err == nil { + t.Fatalf("Expected failure but succeeded: %d", o) + } + + i.Release(50) + + o, err = i.GetID() + if err != nil { + t.Fatal(err) + } + if o != 50 { + t.Fatalf("Unexpected id returned") + } + + i.Release(52) + err = i.GetSpecificID(52) + if err != nil { + t.Fatal(err) + } +} + +func TestUninitialized(t *testing.T) { + i := &Idm{} + + if _, err := i.GetID(); err == nil { + t.Fatalf("Expected failure but succeeded") + } + + if err := i.GetSpecificID(44); err == nil { + t.Fatalf("Expected failure but succeeded") + } +} diff --git a/ipam/allocator.go b/ipam/allocator.go index 3a132a7..ce1a121 100644 --- a/ipam/allocator.go +++ b/ipam/allocator.go @@ -355,6 +355,7 @@ func (a *Allocator) getSubnetList(addrSpace AddressSpace, ver ipVersion) []subne func (a *Allocator) getAddress(smallSubnet *bitmask, prefAddress net.IP, ver ipVersion) (net.IP, error) { var ( bytePos, bitPos int + err error ) // Look for free IP, skip .0 and .255, they will be automatically reserved again: @@ -362,12 +363,12 @@ again: return nil, ErrNoAvailableIPs } if prefAddress == nil { - bytePos, bitPos = smallSubnet.addressMask.GetFirstAvailable() + bytePos, bitPos, err = smallSubnet.addressMask.GetFirstAvailable() } else { ordinal := ipToInt(getHostPortionIP(prefAddress, smallSubnet.subnet)) - bytePos, bitPos = smallSubnet.addressMask.CheckIfAvailable(ordinal) + bytePos, bitPos, err = smallSubnet.addressMask.CheckIfAvailable(ordinal) } - if bytePos == -1 { + if err != nil { return nil, ErrNoAvailableIPs }