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