2015-06-12 19:43:18 +00:00
|
|
|
package ipam
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2015-06-12 22:36:28 +00:00
|
|
|
"sync"
|
2015-06-12 19:43:18 +00:00
|
|
|
|
2015-06-16 01:28:00 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-06-12 19:43:18 +00:00
|
|
|
"github.com/docker/libnetwork/bitseq"
|
2015-06-15 18:43:02 +00:00
|
|
|
"github.com/docker/libnetwork/datastore"
|
2015-09-22 20:20:55 +00:00
|
|
|
"github.com/docker/libnetwork/ipamapi"
|
2015-10-04 21:31:01 +00:00
|
|
|
"github.com/docker/libnetwork/ipamutils"
|
2015-06-16 01:28:00 +00:00
|
|
|
"github.com/docker/libnetwork/types"
|
2015-06-12 19:43:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-09-22 20:20:55 +00:00
|
|
|
localAddressSpace = "LocalDefault"
|
|
|
|
globalAddressSpace = "GlobalDefault"
|
2015-06-12 19:43:18 +00:00
|
|
|
// The biggest configurable host subnets
|
2015-10-09 03:04:13 +00:00
|
|
|
minNetSize = 8
|
|
|
|
minNetSizeV6 = 64
|
2015-06-25 02:11:44 +00:00
|
|
|
// datastore keyes for ipam objects
|
2015-09-22 20:20:55 +00:00
|
|
|
dsConfigKey = "ipam/" + ipamapi.DefaultIPAM + "/config"
|
|
|
|
dsDataKey = "ipam/" + ipamapi.DefaultIPAM + "/data"
|
2015-06-12 19:43:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Allocator provides per address space ipv4/ipv6 book keeping
|
|
|
|
type Allocator struct {
|
2015-09-22 20:20:55 +00:00
|
|
|
// Predefined pools for default address spaces
|
|
|
|
predefined map[string][]*net.IPNet
|
2015-10-05 11:24:44 +00:00
|
|
|
addrSpaces map[string]*addrSpace
|
|
|
|
// stores []datastore.Datastore
|
2015-09-22 20:20:55 +00:00
|
|
|
// Allocated addresses in each address space's subnet
|
|
|
|
addresses map[SubnetKey]*bitseq.Handle
|
2015-06-12 22:36:28 +00:00
|
|
|
sync.Mutex
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAllocator returns an instance of libnetwork ipam
|
2015-09-22 20:20:55 +00:00
|
|
|
func NewAllocator(lcDs, glDs datastore.DataStore) (*Allocator, error) {
|
2015-06-12 19:43:18 +00:00
|
|
|
a := &Allocator{}
|
2015-06-16 00:13:49 +00:00
|
|
|
|
2015-10-05 11:24:44 +00:00
|
|
|
// Load predefined subnet pools
|
2015-10-04 01:51:53 +00:00
|
|
|
a.predefined = map[string][]*net.IPNet{
|
2015-10-04 21:31:01 +00:00
|
|
|
localAddressSpace: ipamutils.PredefinedBroadNetworks,
|
|
|
|
globalAddressSpace: ipamutils.PredefinedGranularNetworks,
|
2015-06-16 21:46:51 +00:00
|
|
|
}
|
2015-09-22 20:20:55 +00:00
|
|
|
|
2015-10-05 11:24:44 +00:00
|
|
|
// Initialize bitseq map
|
2015-10-04 01:51:53 +00:00
|
|
|
a.addresses = make(map[SubnetKey]*bitseq.Handle)
|
2015-09-22 20:20:55 +00:00
|
|
|
|
2015-10-05 11:24:44 +00:00
|
|
|
// Initialize address spaces
|
|
|
|
a.addrSpaces = make(map[string]*addrSpace)
|
|
|
|
for _, aspc := range []struct {
|
|
|
|
as string
|
|
|
|
ds datastore.DataStore
|
2015-10-04 01:51:53 +00:00
|
|
|
}{
|
2015-10-05 11:24:44 +00:00
|
|
|
{localAddressSpace, lcDs},
|
|
|
|
{globalAddressSpace, glDs},
|
|
|
|
} {
|
|
|
|
if aspc.ds == nil {
|
2015-10-04 01:51:53 +00:00
|
|
|
continue
|
2015-09-22 20:20:55 +00:00
|
|
|
}
|
2015-10-05 11:24:44 +00:00
|
|
|
|
|
|
|
a.addrSpaces[aspc.as] = &addrSpace{
|
|
|
|
subnets: map[SubnetKey]*PoolData{},
|
|
|
|
id: dsConfigKey + "/" + aspc.as,
|
|
|
|
scope: aspc.ds.Scope(),
|
|
|
|
ds: aspc.ds,
|
|
|
|
alloc: a,
|
2015-10-04 01:51:53 +00:00
|
|
|
}
|
2015-10-05 11:24:44 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 16:26:14 +00:00
|
|
|
a.checkConsistency(localAddressSpace)
|
|
|
|
a.checkConsistency(globalAddressSpace)
|
|
|
|
|
2015-10-05 11:24:44 +00:00
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Allocator) refresh(as string) error {
|
|
|
|
aSpace, err := a.getAddressSpaceFromStore(as)
|
|
|
|
if err != nil {
|
2015-10-21 00:05:01 +00:00
|
|
|
return types.InternalErrorf("error getting pools config from store: %v", err)
|
2015-10-05 11:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if aSpace == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
a.Lock()
|
|
|
|
a.addrSpaces[as] = aSpace
|
|
|
|
a.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Allocator) updateBitMasks(aSpace *addrSpace) error {
|
|
|
|
var inserterList []func() error
|
|
|
|
|
|
|
|
aSpace.Lock()
|
|
|
|
for k, v := range aSpace.subnets {
|
|
|
|
if v.Range == nil {
|
2015-10-05 21:53:25 +00:00
|
|
|
kk := k
|
|
|
|
vv := v
|
|
|
|
inserterList = append(inserterList, func() error { return a.insertBitMask(kk, vv.Pool) })
|
2015-10-04 01:51:53 +00:00
|
|
|
}
|
2015-09-22 20:20:55 +00:00
|
|
|
}
|
2015-10-05 11:24:44 +00:00
|
|
|
aSpace.Unlock()
|
|
|
|
|
2015-10-04 01:51:53 +00:00
|
|
|
// Add the bitmasks (data could come from datastore)
|
|
|
|
if inserterList != nil {
|
|
|
|
for _, f := range inserterList {
|
|
|
|
if err := f(); err != nil {
|
2015-10-05 11:24:44 +00:00
|
|
|
return err
|
2015-10-04 01:51:53 +00:00
|
|
|
}
|
2015-09-22 20:20:55 +00:00
|
|
|
}
|
2015-06-16 21:46:51 +00:00
|
|
|
}
|
2015-09-22 20:20:55 +00:00
|
|
|
|
2015-10-05 11:24:44 +00:00
|
|
|
return nil
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 16:26:14 +00:00
|
|
|
// Checks for and fixes damaged bitmask. Meant to be called in constructor only.
|
|
|
|
func (a *Allocator) checkConsistency(as string) {
|
|
|
|
// Retrieve this address space's configuration and bitmasks from the datastore
|
|
|
|
a.refresh(as)
|
|
|
|
aSpace, ok := a.addrSpaces[as]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
a.updateBitMasks(aSpace)
|
|
|
|
for sk, pd := range aSpace.subnets {
|
|
|
|
if pd.Range != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := a.addresses[sk].CheckConsistency(); err != nil {
|
|
|
|
log.Warnf("Error while running consistency check for %s: %v", sk, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
// GetDefaultAddressSpaces returns the local and global default address spaces
|
|
|
|
func (a *Allocator) GetDefaultAddressSpaces() (string, string, error) {
|
|
|
|
return localAddressSpace, globalAddressSpace, nil
|
|
|
|
}
|
2015-06-12 19:43:18 +00:00
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
// RequestPool returns an address pool along with its unique id.
|
|
|
|
func (a *Allocator) RequestPool(addressSpace, pool, subPool string, options map[string]string, v6 bool) (string, *net.IPNet, map[string]string, error) {
|
2015-10-05 21:53:25 +00:00
|
|
|
log.Debugf("RequestPool(%s, %s, %s, %v, %t)", addressSpace, pool, subPool, options, v6)
|
2016-01-22 18:46:05 +00:00
|
|
|
retry:
|
|
|
|
k, nw, ipr, pdf, err := a.parsePoolRequest(addressSpace, pool, subPool, v6)
|
2015-06-12 19:43:18 +00:00
|
|
|
if err != nil {
|
2015-10-12 17:52:58 +00:00
|
|
|
return "", nil, nil, types.InternalErrorf("failed to parse pool request for address space %q pool %q subpool %q: %v", addressSpace, pool, subPool, err)
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
2015-10-04 01:51:53 +00:00
|
|
|
|
2015-10-05 11:24:44 +00:00
|
|
|
if err := a.refresh(addressSpace); err != nil {
|
|
|
|
return "", nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
aSpace, err := a.getAddrSpace(addressSpace)
|
2015-10-04 01:51:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, nil, err
|
|
|
|
}
|
|
|
|
|
2016-01-22 18:46:05 +00:00
|
|
|
insert, err := aSpace.updatePoolDBOnAdd(*k, nw, ipr, pdf)
|
2015-06-16 21:46:51 +00:00
|
|
|
if err != nil {
|
2016-01-22 18:46:05 +00:00
|
|
|
if _, ok := err.(types.MaskableError); ok {
|
|
|
|
log.Debugf("Retrying predefined pool search: %v", err)
|
|
|
|
goto retry
|
|
|
|
}
|
2015-09-22 20:20:55 +00:00
|
|
|
return "", nil, nil, err
|
|
|
|
}
|
2015-10-05 11:24:44 +00:00
|
|
|
|
|
|
|
if err := a.writeToStore(aSpace); err != nil {
|
2015-06-16 21:46:51 +00:00
|
|
|
if _, ok := err.(types.RetryError); !ok {
|
2015-09-22 20:20:55 +00:00
|
|
|
return "", nil, nil, types.InternalErrorf("pool configuration failed because of %s", err.Error())
|
2015-06-16 21:46:51 +00:00
|
|
|
}
|
2015-10-05 11:24:44 +00:00
|
|
|
|
2015-06-16 21:46:51 +00:00
|
|
|
goto retry
|
|
|
|
}
|
2015-10-05 11:24:44 +00:00
|
|
|
|
2015-10-09 03:04:13 +00:00
|
|
|
return k.String(), nw, nil, insert()
|
2015-06-16 21:46:51 +00:00
|
|
|
}
|
2015-06-12 19:43:18 +00:00
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
// ReleasePool releases the address pool identified by the passed id
|
|
|
|
func (a *Allocator) ReleasePool(poolID string) error {
|
2015-10-05 21:53:25 +00:00
|
|
|
log.Debugf("ReleasePool(%s)", poolID)
|
2015-09-22 20:20:55 +00:00
|
|
|
k := SubnetKey{}
|
|
|
|
if err := k.FromString(poolID); err != nil {
|
|
|
|
return types.BadRequestErrorf("invalid pool id: %s", poolID)
|
|
|
|
}
|
2015-08-12 02:22:38 +00:00
|
|
|
|
2015-10-05 11:24:44 +00:00
|
|
|
retry:
|
|
|
|
if err := a.refresh(k.AddressSpace); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
aSpace, err := a.getAddrSpace(k.AddressSpace)
|
2015-10-04 01:51:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:24:44 +00:00
|
|
|
remove, err := aSpace.updatePoolDBOnRemoval(k)
|
2015-09-22 20:20:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-05 11:24:44 +00:00
|
|
|
|
|
|
|
if err = a.writeToStore(aSpace); err != nil {
|
2015-09-22 20:20:55 +00:00
|
|
|
if _, ok := err.(types.RetryError); !ok {
|
|
|
|
return types.InternalErrorf("pool (%s) removal failed because of %v", poolID, err)
|
2015-08-12 02:22:38 +00:00
|
|
|
}
|
2015-09-22 20:20:55 +00:00
|
|
|
goto retry
|
|
|
|
}
|
2015-08-12 02:22:38 +00:00
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
return remove()
|
|
|
|
}
|
2015-08-12 02:22:38 +00:00
|
|
|
|
2015-10-04 01:51:53 +00:00
|
|
|
// Given the address space, returns the local or global PoolConfig based on the
|
|
|
|
// address space is local or global. AddressSpace locality is being registered with IPAM out of band.
|
2015-10-05 11:24:44 +00:00
|
|
|
func (a *Allocator) getAddrSpace(as string) (*addrSpace, error) {
|
2015-10-04 01:51:53 +00:00
|
|
|
a.Lock()
|
|
|
|
defer a.Unlock()
|
2015-10-05 11:24:44 +00:00
|
|
|
aSpace, ok := a.addrSpaces[as]
|
2015-10-04 01:51:53 +00:00
|
|
|
if !ok {
|
2015-10-30 17:18:50 +00:00
|
|
|
return nil, types.BadRequestErrorf("cannot find address space %s (most likely the backing datastore is not configured)", as)
|
2015-10-04 01:51:53 +00:00
|
|
|
}
|
2015-10-05 11:24:44 +00:00
|
|
|
return aSpace, nil
|
2015-10-04 01:51:53 +00:00
|
|
|
}
|
|
|
|
|
2016-01-22 18:46:05 +00:00
|
|
|
func (a *Allocator) parsePoolRequest(addressSpace, pool, subPool string, v6 bool) (*SubnetKey, *net.IPNet, *AddressRange, bool, error) {
|
2015-09-22 20:20:55 +00:00
|
|
|
var (
|
2015-10-09 03:04:13 +00:00
|
|
|
nw *net.IPNet
|
|
|
|
ipr *AddressRange
|
|
|
|
err error
|
2016-01-22 18:46:05 +00:00
|
|
|
pdf = false
|
2015-09-22 20:20:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if addressSpace == "" {
|
2016-01-22 18:46:05 +00:00
|
|
|
return nil, nil, nil, false, ipamapi.ErrInvalidAddressSpace
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
if pool == "" && subPool != "" {
|
2016-01-22 18:46:05 +00:00
|
|
|
return nil, nil, nil, false, ipamapi.ErrInvalidSubPool
|
2015-09-22 20:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if pool != "" {
|
|
|
|
if _, nw, err = net.ParseCIDR(pool); err != nil {
|
2016-01-22 18:46:05 +00:00
|
|
|
return nil, nil, nil, false, ipamapi.ErrInvalidPool
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
2015-09-22 20:20:55 +00:00
|
|
|
if subPool != "" {
|
2015-11-08 19:18:10 +00:00
|
|
|
if ipr, err = getAddressRange(subPool, nw); err != nil {
|
2016-01-22 18:46:05 +00:00
|
|
|
return nil, nil, nil, false, err
|
2015-09-22 20:20:55 +00:00
|
|
|
}
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-09-22 20:20:55 +00:00
|
|
|
if nw, err = a.getPredefinedPool(addressSpace, v6); err != nil {
|
2016-01-22 18:46:05 +00:00
|
|
|
return nil, nil, nil, false, err
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
2016-01-22 18:46:05 +00:00
|
|
|
pdf = true
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
2015-09-22 20:20:55 +00:00
|
|
|
|
2016-01-22 18:46:05 +00:00
|
|
|
return &SubnetKey{AddressSpace: addressSpace, Subnet: nw.String(), ChildSubnet: subPool}, nw, ipr, pdf, nil
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:24:44 +00:00
|
|
|
func (a *Allocator) insertBitMask(key SubnetKey, pool *net.IPNet) error {
|
2015-10-09 19:43:11 +00:00
|
|
|
//log.Debugf("Inserting bitmask (%s, %s)", key.String(), pool.String())
|
2015-10-05 11:24:44 +00:00
|
|
|
|
|
|
|
store := a.getStore(key.AddressSpace)
|
|
|
|
if store == nil {
|
2015-10-21 00:05:01 +00:00
|
|
|
return types.InternalErrorf("could not find store for address space %s while inserting bit mask", key.AddressSpace)
|
2015-10-05 11:24:44 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
ipVer := getAddressVersion(pool.IP)
|
|
|
|
ones, bits := pool.Mask.Size()
|
2015-10-09 03:04:13 +00:00
|
|
|
numAddresses := uint64(1 << uint(bits-ones))
|
2015-09-22 20:20:55 +00:00
|
|
|
|
2015-10-09 03:04:13 +00:00
|
|
|
// Allow /64 subnet
|
|
|
|
if ipVer == v6 && numAddresses == 0 {
|
|
|
|
numAddresses--
|
|
|
|
}
|
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
// Generate the new address masks. AddressMask content may come from datastore
|
2015-10-04 01:51:53 +00:00
|
|
|
h, err := bitseq.NewHandle(dsDataKey, store, key.String(), numAddresses)
|
2015-06-13 20:12:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-09 03:04:13 +00:00
|
|
|
// Do not let network identifier address be reserved
|
|
|
|
// Do the same for IPv6 so that bridge ip starts with XXXX...::1
|
|
|
|
h.Set(0)
|
2015-06-12 19:43:18 +00:00
|
|
|
|
2015-10-22 19:58:45 +00:00
|
|
|
// Do not let broadcast address be reserved
|
|
|
|
if ipVer == v4 {
|
|
|
|
h.Set(numAddresses - 1)
|
|
|
|
}
|
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
a.Lock()
|
|
|
|
a.addresses[key] = h
|
|
|
|
a.Unlock()
|
2015-06-12 19:43:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:24:44 +00:00
|
|
|
func (a *Allocator) retrieveBitmask(k SubnetKey, n *net.IPNet) (*bitseq.Handle, error) {
|
2015-09-22 20:20:55 +00:00
|
|
|
a.Lock()
|
|
|
|
bm, ok := a.addresses[k]
|
|
|
|
a.Unlock()
|
|
|
|
if !ok {
|
|
|
|
log.Debugf("Retrieving bitmask (%s, %s)", k.String(), n.String())
|
2015-10-05 11:24:44 +00:00
|
|
|
if err := a.insertBitMask(k, n); err != nil {
|
2015-10-21 00:05:01 +00:00
|
|
|
return nil, types.InternalErrorf("could not find bitmask in datastore for %s", k.String())
|
2015-09-22 20:20:55 +00:00
|
|
|
}
|
|
|
|
a.Lock()
|
|
|
|
bm = a.addresses[k]
|
|
|
|
a.Unlock()
|
|
|
|
}
|
|
|
|
return bm, nil
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
func (a *Allocator) getPredefineds(as string) []*net.IPNet {
|
|
|
|
a.Lock()
|
|
|
|
defer a.Unlock()
|
|
|
|
l := make([]*net.IPNet, 0, len(a.predefined[as]))
|
|
|
|
for _, pool := range a.predefined[as] {
|
|
|
|
l = append(l, pool)
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
2015-09-22 20:20:55 +00:00
|
|
|
return l
|
|
|
|
}
|
2015-06-12 19:43:18 +00:00
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
func (a *Allocator) getPredefinedPool(as string, ipV6 bool) (*net.IPNet, error) {
|
|
|
|
var v ipVersion
|
|
|
|
v = v4
|
|
|
|
if ipV6 {
|
|
|
|
v = v6
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
if as != localAddressSpace && as != globalAddressSpace {
|
2015-10-21 00:05:01 +00:00
|
|
|
return nil, types.NotImplementedErrorf("no default pool availbale for non-default addresss spaces")
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:24:44 +00:00
|
|
|
aSpace, err := a.getAddrSpace(as)
|
2015-10-04 01:51:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
for _, nw := range a.getPredefineds(as) {
|
|
|
|
if v != getAddressVersion(nw.IP) {
|
|
|
|
continue
|
|
|
|
}
|
2015-10-05 11:24:44 +00:00
|
|
|
aSpace.Lock()
|
|
|
|
_, ok := aSpace.subnets[SubnetKey{AddressSpace: as, Subnet: nw.String()}]
|
|
|
|
aSpace.Unlock()
|
2015-09-22 20:20:55 +00:00
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:24:44 +00:00
|
|
|
if !aSpace.contains(as, nw) {
|
2015-09-22 20:20:55 +00:00
|
|
|
if as == localAddressSpace {
|
2015-10-04 21:31:01 +00:00
|
|
|
// Check if nw overlap with system routes, name servers
|
|
|
|
if _, err := ipamutils.FindAvailableNetwork([]*net.IPNet{nw}); err == nil {
|
2015-09-22 20:20:55 +00:00
|
|
|
return nw, nil
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nw, nil
|
|
|
|
}
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
return nil, types.NotFoundErrorf("could not find an available predefined network")
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
// RequestAddress returns an address from the specified pool ID
|
|
|
|
func (a *Allocator) RequestAddress(poolID string, prefAddress net.IP, opts map[string]string) (*net.IPNet, map[string]string, error) {
|
2015-10-05 21:53:25 +00:00
|
|
|
log.Debugf("RequestAddress(%s, %v, %v)", poolID, prefAddress, opts)
|
2015-09-22 20:20:55 +00:00
|
|
|
k := SubnetKey{}
|
|
|
|
if err := k.FromString(poolID); err != nil {
|
|
|
|
return nil, nil, types.BadRequestErrorf("invalid pool id: %s", poolID)
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
2015-06-25 02:11:44 +00:00
|
|
|
|
2015-10-05 11:24:44 +00:00
|
|
|
if err := a.refresh(k.AddressSpace); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
aSpace, err := a.getAddrSpace(k.AddressSpace)
|
2015-10-04 01:51:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:24:44 +00:00
|
|
|
aSpace.Lock()
|
|
|
|
p, ok := aSpace.subnets[k]
|
2015-09-22 20:20:55 +00:00
|
|
|
if !ok {
|
2015-10-05 11:24:44 +00:00
|
|
|
aSpace.Unlock()
|
2015-09-22 20:20:55 +00:00
|
|
|
return nil, nil, types.NotFoundErrorf("cannot find address pool for poolID:%s", poolID)
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
2015-06-25 02:11:44 +00:00
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
if prefAddress != nil && !p.Pool.Contains(prefAddress) {
|
2015-10-05 11:24:44 +00:00
|
|
|
aSpace.Unlock()
|
2015-09-22 20:20:55 +00:00
|
|
|
return nil, nil, ipamapi.ErrIPOutOfRange
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
2015-09-22 20:20:55 +00:00
|
|
|
|
|
|
|
c := p
|
|
|
|
for c.Range != nil {
|
|
|
|
k = c.ParentKey
|
2015-10-05 11:24:44 +00:00
|
|
|
c, ok = aSpace.subnets[k]
|
2015-06-25 02:11:44 +00:00
|
|
|
}
|
2015-10-05 11:24:44 +00:00
|
|
|
aSpace.Unlock()
|
2015-06-25 02:11:44 +00:00
|
|
|
|
2015-10-05 11:24:44 +00:00
|
|
|
bm, err := a.retrieveBitmask(k, c.Pool)
|
2015-06-25 02:11:44 +00:00
|
|
|
if err != nil {
|
2015-10-21 00:05:01 +00:00
|
|
|
return nil, nil, types.InternalErrorf("could not find bitmask in datastore for %s on address %v request from pool %s: %v",
|
2015-09-22 20:20:55 +00:00
|
|
|
k.String(), prefAddress, poolID, err)
|
2015-06-25 02:11:44 +00:00
|
|
|
}
|
2015-09-22 20:20:55 +00:00
|
|
|
ip, err := a.getAddress(p.Pool, bm, prefAddress, p.Range)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2015-06-25 02:11:44 +00:00
|
|
|
}
|
2015-09-22 20:20:55 +00:00
|
|
|
|
|
|
|
return &net.IPNet{IP: ip, Mask: p.Pool.Mask}, nil, nil
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
// ReleaseAddress releases the address from the specified pool ID
|
|
|
|
func (a *Allocator) ReleaseAddress(poolID string, address net.IP) error {
|
2015-10-05 21:53:25 +00:00
|
|
|
log.Debugf("ReleaseAddress(%s, %v)", poolID, address)
|
2015-09-22 20:20:55 +00:00
|
|
|
k := SubnetKey{}
|
|
|
|
if err := k.FromString(poolID); err != nil {
|
|
|
|
return types.BadRequestErrorf("invalid pool id: %s", poolID)
|
|
|
|
}
|
2015-06-12 19:43:18 +00:00
|
|
|
|
2015-10-05 11:24:44 +00:00
|
|
|
if err := a.refresh(k.AddressSpace); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
aSpace, err := a.getAddrSpace(k.AddressSpace)
|
2015-10-04 01:51:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:24:44 +00:00
|
|
|
aSpace.Lock()
|
|
|
|
p, ok := aSpace.subnets[k]
|
2015-09-22 20:20:55 +00:00
|
|
|
if !ok {
|
2015-10-05 11:24:44 +00:00
|
|
|
aSpace.Unlock()
|
2015-10-21 00:05:01 +00:00
|
|
|
return types.NotFoundErrorf("cannot find address pool for poolID:%s", poolID)
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 03:29:30 +00:00
|
|
|
if address == nil {
|
2015-10-05 11:24:44 +00:00
|
|
|
aSpace.Unlock()
|
2015-10-21 00:05:01 +00:00
|
|
|
return types.BadRequestErrorf("invalid address: nil")
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 03:29:30 +00:00
|
|
|
if !p.Pool.Contains(address) {
|
|
|
|
aSpace.Unlock()
|
|
|
|
return ipamapi.ErrIPOutOfRange
|
|
|
|
}
|
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
c := p
|
|
|
|
for c.Range != nil {
|
|
|
|
k = c.ParentKey
|
2015-10-05 11:24:44 +00:00
|
|
|
c = aSpace.subnets[k]
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
2015-10-05 11:24:44 +00:00
|
|
|
aSpace.Unlock()
|
2015-09-22 20:20:55 +00:00
|
|
|
|
|
|
|
mask := p.Pool.Mask
|
2015-10-09 03:04:13 +00:00
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
h, err := types.GetHostPartIP(address, mask)
|
|
|
|
if err != nil {
|
2015-10-21 00:05:01 +00:00
|
|
|
return types.InternalErrorf("failed to release address %s: %v", address.String(), err)
|
2015-09-22 20:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:24:44 +00:00
|
|
|
bm, err := a.retrieveBitmask(k, c.Pool)
|
2015-09-22 20:20:55 +00:00
|
|
|
if err != nil {
|
2015-10-21 00:05:01 +00:00
|
|
|
return types.InternalErrorf("could not find bitmask in datastore for %s on address %v release from pool %s: %v",
|
2015-09-22 20:20:55 +00:00
|
|
|
k.String(), address, poolID, err)
|
|
|
|
}
|
2015-10-09 03:04:13 +00:00
|
|
|
|
|
|
|
return bm.Unset(ipToUint64(h))
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
func (a *Allocator) getAddress(nw *net.IPNet, bitmask *bitseq.Handle, prefAddress net.IP, ipr *AddressRange) (net.IP, error) {
|
2015-06-12 19:43:18 +00:00
|
|
|
var (
|
2015-10-09 03:04:13 +00:00
|
|
|
ordinal uint64
|
2015-06-24 22:02:08 +00:00
|
|
|
err error
|
2015-09-22 20:20:55 +00:00
|
|
|
base *net.IPNet
|
2015-06-12 19:43:18 +00:00
|
|
|
)
|
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
base = types.GetIPNetCopy(nw)
|
|
|
|
|
2015-08-12 02:22:38 +00:00
|
|
|
if bitmask.Unselected() <= 0 {
|
2015-09-22 20:20:55 +00:00
|
|
|
return nil, ipamapi.ErrNoAvailableIPs
|
2015-08-12 02:22:38 +00:00
|
|
|
}
|
2015-09-22 20:20:55 +00:00
|
|
|
if ipr == nil && prefAddress == nil {
|
2015-08-12 02:22:38 +00:00
|
|
|
ordinal, err = bitmask.SetAny()
|
2015-10-05 07:40:17 +00:00
|
|
|
} else if prefAddress != nil {
|
2015-09-22 20:20:55 +00:00
|
|
|
hostPart, e := types.GetHostPartIP(prefAddress, base.Mask)
|
2015-08-12 02:22:38 +00:00
|
|
|
if e != nil {
|
2015-10-21 00:05:01 +00:00
|
|
|
return nil, types.InternalErrorf("failed to allocate preferred address %s: %v", prefAddress.String(), e)
|
2015-06-16 21:46:51 +00:00
|
|
|
}
|
2015-10-09 03:04:13 +00:00
|
|
|
ordinal = ipToUint64(types.GetMinimalIP(hostPart))
|
2015-08-12 02:22:38 +00:00
|
|
|
err = bitmask.Set(ordinal)
|
2015-10-05 07:40:17 +00:00
|
|
|
} else {
|
|
|
|
ordinal, err = bitmask.SetAnyInRange(ipr.Start, ipr.End)
|
2015-08-12 02:22:38 +00:00
|
|
|
}
|
2015-10-21 00:05:01 +00:00
|
|
|
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
// Convert IP ordinal for this subnet into IP address
|
|
|
|
return generateAddress(ordinal, base), nil
|
|
|
|
case bitseq.ErrBitAllocated:
|
|
|
|
return nil, ipamapi.ErrIPAlreadyAllocated
|
|
|
|
case bitseq.ErrNoBitAvailable:
|
2015-09-22 20:20:55 +00:00
|
|
|
return nil, ipamapi.ErrNoAvailableIPs
|
2015-10-21 00:05:01 +00:00
|
|
|
default:
|
|
|
|
return nil, err
|
2015-06-12 19:43:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DumpDatabase dumps the internal info
|
2015-09-22 20:20:55 +00:00
|
|
|
func (a *Allocator) DumpDatabase() string {
|
2015-06-12 22:36:28 +00:00
|
|
|
a.Lock()
|
2015-12-13 08:35:05 +00:00
|
|
|
aspaces := make(map[string]*addrSpace, len(a.addrSpaces))
|
|
|
|
for as, aSpace := range a.addrSpaces {
|
|
|
|
aspaces[as] = aSpace
|
|
|
|
}
|
|
|
|
a.Unlock()
|
2015-09-22 20:20:55 +00:00
|
|
|
|
2015-10-05 11:24:44 +00:00
|
|
|
var s string
|
2015-12-13 08:35:05 +00:00
|
|
|
for as, aSpace := range aspaces {
|
2015-10-05 11:24:44 +00:00
|
|
|
s = fmt.Sprintf("\n\n%s Config", as)
|
|
|
|
aSpace.Lock()
|
|
|
|
for k, config := range aSpace.subnets {
|
|
|
|
s = fmt.Sprintf("%s%s", s, fmt.Sprintf("\n%v: %v", k, config))
|
2015-12-13 08:35:05 +00:00
|
|
|
if config.Range == nil {
|
|
|
|
a.retrieveBitmask(k, config.Pool)
|
|
|
|
}
|
2015-10-05 11:24:44 +00:00
|
|
|
}
|
|
|
|
aSpace.Unlock()
|
2015-10-04 01:51:53 +00:00
|
|
|
}
|
2015-06-12 19:43:18 +00:00
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
s = fmt.Sprintf("%s\n\nBitmasks", s)
|
|
|
|
for k, bm := range a.addresses {
|
2015-12-13 08:35:05 +00:00
|
|
|
s = fmt.Sprintf("%s%s", s, fmt.Sprintf("\n%s: %s", k, bm))
|
2015-09-22 20:20:55 +00:00
|
|
|
}
|
2015-10-05 11:24:44 +00:00
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
return s
|
2015-08-12 02:22:38 +00:00
|
|
|
}
|