2015-10-03 21:51:53 -04:00
|
|
|
package ipam
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/docker/libnetwork/datastore"
|
|
|
|
"github.com/docker/libnetwork/ipamapi"
|
|
|
|
"github.com/docker/libnetwork/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SubnetKey is the pointer to the configured pools in each address space
|
|
|
|
type SubnetKey struct {
|
|
|
|
AddressSpace string
|
|
|
|
Subnet string
|
|
|
|
ChildSubnet string
|
|
|
|
}
|
|
|
|
|
|
|
|
// PoolData contains the configured pool data
|
|
|
|
type PoolData struct {
|
|
|
|
ParentKey SubnetKey
|
|
|
|
Pool *net.IPNet
|
|
|
|
Range *AddressRange `json:",omitempty"`
|
|
|
|
RefCount int
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:24:44 -04:00
|
|
|
// addrSpace contains the pool configurations for the address space
|
|
|
|
type addrSpace struct {
|
2015-10-03 21:51:53 -04:00
|
|
|
subnets map[SubnetKey]*PoolData
|
|
|
|
dbIndex uint64
|
|
|
|
dbExists bool
|
|
|
|
id string
|
2015-10-05 07:24:44 -04:00
|
|
|
scope string
|
2015-10-03 21:51:53 -04:00
|
|
|
ds datastore.DataStore
|
|
|
|
alloc *Allocator
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddressRange specifies first and last ip ordinal which
|
2016-02-28 11:34:30 -05:00
|
|
|
// identifies a range in a pool of addresses
|
2015-10-03 21:51:53 -04:00
|
|
|
type AddressRange struct {
|
|
|
|
Sub *net.IPNet
|
2015-10-08 23:04:13 -04:00
|
|
|
Start, End uint64
|
2015-10-03 21:51:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the string form of the AddressRange object
|
|
|
|
func (r *AddressRange) String() string {
|
|
|
|
return fmt.Sprintf("Sub: %s, range [%d, %d]", r.Sub, r.Start, r.End)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON returns the JSON encoding of the Range object
|
|
|
|
func (r *AddressRange) MarshalJSON() ([]byte, error) {
|
|
|
|
m := map[string]interface{}{
|
|
|
|
"Sub": r.Sub.String(),
|
|
|
|
"Start": r.Start,
|
|
|
|
"End": r.End,
|
|
|
|
}
|
|
|
|
return json.Marshal(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes data into the Range object
|
|
|
|
func (r *AddressRange) UnmarshalJSON(data []byte) error {
|
|
|
|
m := map[string]interface{}{}
|
|
|
|
err := json.Unmarshal(data, &m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if r.Sub, err = types.ParseCIDR(m["Sub"].(string)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-08 23:04:13 -04:00
|
|
|
r.Start = uint64(m["Start"].(float64))
|
|
|
|
r.End = uint64(m["End"].(float64))
|
2015-10-03 21:51:53 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the string form of the SubnetKey object
|
|
|
|
func (s *SubnetKey) String() string {
|
|
|
|
k := fmt.Sprintf("%s/%s", s.AddressSpace, s.Subnet)
|
|
|
|
if s.ChildSubnet != "" {
|
|
|
|
k = fmt.Sprintf("%s/%s", k, s.ChildSubnet)
|
|
|
|
}
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
|
2016-02-28 11:34:30 -05:00
|
|
|
// FromString populates the SubnetKey object reading it from string
|
2015-10-03 21:51:53 -04:00
|
|
|
func (s *SubnetKey) FromString(str string) error {
|
|
|
|
if str == "" || !strings.Contains(str, "/") {
|
2015-10-20 20:05:01 -04:00
|
|
|
return types.BadRequestErrorf("invalid string form for subnetkey: %s", str)
|
2015-10-03 21:51:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
p := strings.Split(str, "/")
|
|
|
|
if len(p) != 3 && len(p) != 5 {
|
2015-10-20 20:05:01 -04:00
|
|
|
return types.BadRequestErrorf("invalid string form for subnetkey: %s", str)
|
2015-10-03 21:51:53 -04:00
|
|
|
}
|
|
|
|
s.AddressSpace = p[0]
|
|
|
|
s.Subnet = fmt.Sprintf("%s/%s", p[1], p[2])
|
|
|
|
if len(p) == 5 {
|
|
|
|
s.ChildSubnet = fmt.Sprintf("%s/%s", p[3], p[4])
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the string form of the PoolData object
|
|
|
|
func (p *PoolData) String() string {
|
|
|
|
return fmt.Sprintf("ParentKey: %s, Pool: %s, Range: %s, RefCount: %d",
|
|
|
|
p.ParentKey.String(), p.Pool.String(), p.Range, p.RefCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON returns the JSON encoding of the PoolData object
|
|
|
|
func (p *PoolData) MarshalJSON() ([]byte, error) {
|
|
|
|
m := map[string]interface{}{
|
|
|
|
"ParentKey": p.ParentKey,
|
|
|
|
"RefCount": p.RefCount,
|
|
|
|
}
|
|
|
|
if p.Pool != nil {
|
|
|
|
m["Pool"] = p.Pool.String()
|
|
|
|
}
|
|
|
|
if p.Range != nil {
|
|
|
|
m["Range"] = p.Range
|
|
|
|
}
|
|
|
|
return json.Marshal(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes data into the PoolData object
|
|
|
|
func (p *PoolData) UnmarshalJSON(data []byte) error {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
t struct {
|
|
|
|
ParentKey SubnetKey
|
|
|
|
Pool string
|
|
|
|
Range *AddressRange `json:",omitempty"`
|
|
|
|
RefCount int
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
if err = json.Unmarshal(data, &t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.ParentKey = t.ParentKey
|
|
|
|
p.Range = t.Range
|
|
|
|
p.RefCount = t.RefCount
|
|
|
|
if t.Pool != "" {
|
|
|
|
if p.Pool, err = types.ParseCIDR(t.Pool); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:24:44 -04:00
|
|
|
// MarshalJSON returns the JSON encoding of the addrSpace object
|
|
|
|
func (aSpace *addrSpace) MarshalJSON() ([]byte, error) {
|
|
|
|
aSpace.Lock()
|
|
|
|
defer aSpace.Unlock()
|
2015-10-03 21:51:53 -04:00
|
|
|
|
|
|
|
m := map[string]interface{}{
|
2015-10-05 07:24:44 -04:00
|
|
|
"Scope": string(aSpace.scope),
|
2015-10-03 21:51:53 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 07:24:44 -04:00
|
|
|
if aSpace.subnets != nil {
|
2015-10-03 21:51:53 -04:00
|
|
|
s := map[string]*PoolData{}
|
2015-10-05 07:24:44 -04:00
|
|
|
for k, v := range aSpace.subnets {
|
2015-10-03 21:51:53 -04:00
|
|
|
s[k.String()] = v
|
|
|
|
}
|
|
|
|
m["Subnets"] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(m)
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:24:44 -04:00
|
|
|
// UnmarshalJSON decodes data into the addrSpace object
|
|
|
|
func (aSpace *addrSpace) UnmarshalJSON(data []byte) error {
|
|
|
|
aSpace.Lock()
|
|
|
|
defer aSpace.Unlock()
|
2015-10-03 21:51:53 -04:00
|
|
|
|
|
|
|
m := map[string]interface{}{}
|
|
|
|
err := json.Unmarshal(data, &m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:24:44 -04:00
|
|
|
aSpace.scope = datastore.LocalScope
|
2015-10-03 21:51:53 -04:00
|
|
|
s := m["Scope"].(string)
|
|
|
|
if s == string(datastore.GlobalScope) {
|
2015-10-05 07:24:44 -04:00
|
|
|
aSpace.scope = datastore.GlobalScope
|
2015-10-03 21:51:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := m["Subnets"]; ok {
|
|
|
|
sb, _ := json.Marshal(v)
|
|
|
|
var s map[string]*PoolData
|
|
|
|
err := json.Unmarshal(sb, &s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for ks, v := range s {
|
|
|
|
k := SubnetKey{}
|
|
|
|
k.FromString(ks)
|
2015-10-05 07:24:44 -04:00
|
|
|
aSpace.subnets[k] = v
|
2015-10-03 21:51:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:24:44 -04:00
|
|
|
// CopyTo deep copies the pool data to the destination pooldata
|
|
|
|
func (p *PoolData) CopyTo(dstP *PoolData) error {
|
|
|
|
dstP.ParentKey = p.ParentKey
|
|
|
|
dstP.Pool = types.GetIPNetCopy(p.Pool)
|
|
|
|
|
|
|
|
if p.Range != nil {
|
|
|
|
dstP.Range = &AddressRange{}
|
|
|
|
dstP.Range.Sub = types.GetIPNetCopy(p.Range.Sub)
|
|
|
|
dstP.Range.Start = p.Range.Start
|
|
|
|
dstP.Range.End = p.Range.End
|
|
|
|
}
|
|
|
|
|
|
|
|
dstP.RefCount = p.RefCount
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (aSpace *addrSpace) CopyTo(o datastore.KVObject) error {
|
|
|
|
aSpace.Lock()
|
|
|
|
defer aSpace.Unlock()
|
|
|
|
|
|
|
|
dstAspace := o.(*addrSpace)
|
|
|
|
|
|
|
|
dstAspace.id = aSpace.id
|
|
|
|
dstAspace.ds = aSpace.ds
|
|
|
|
dstAspace.alloc = aSpace.alloc
|
|
|
|
dstAspace.scope = aSpace.scope
|
|
|
|
dstAspace.dbIndex = aSpace.dbIndex
|
|
|
|
dstAspace.dbExists = aSpace.dbExists
|
|
|
|
|
|
|
|
dstAspace.subnets = make(map[SubnetKey]*PoolData)
|
|
|
|
for k, v := range aSpace.subnets {
|
|
|
|
dstAspace.subnets[k] = &PoolData{}
|
|
|
|
v.CopyTo(dstAspace.subnets[k])
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (aSpace *addrSpace) New() datastore.KVObject {
|
|
|
|
aSpace.Lock()
|
|
|
|
defer aSpace.Unlock()
|
|
|
|
|
|
|
|
return &addrSpace{
|
|
|
|
id: aSpace.id,
|
|
|
|
ds: aSpace.ds,
|
|
|
|
alloc: aSpace.alloc,
|
|
|
|
scope: aSpace.scope,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 13:46:05 -05:00
|
|
|
func (aSpace *addrSpace) updatePoolDBOnAdd(k SubnetKey, nw *net.IPNet, ipr *AddressRange, pdf bool) (func() error, error) {
|
2015-10-05 07:24:44 -04:00
|
|
|
aSpace.Lock()
|
|
|
|
defer aSpace.Unlock()
|
2015-10-03 21:51:53 -04:00
|
|
|
|
|
|
|
// Check if already allocated
|
2015-10-05 07:24:44 -04:00
|
|
|
if p, ok := aSpace.subnets[k]; ok {
|
2016-01-22 13:46:05 -05:00
|
|
|
if pdf {
|
|
|
|
return nil, types.InternalMaskableErrorf("predefined pool %s is already reserved", nw)
|
|
|
|
}
|
2015-10-05 07:24:44 -04:00
|
|
|
aSpace.incRefCount(p, 1)
|
2015-10-03 21:51:53 -04:00
|
|
|
return func() error { return nil }, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If master pool, check for overlap
|
|
|
|
if ipr == nil {
|
2015-10-05 07:24:44 -04:00
|
|
|
if aSpace.contains(k.AddressSpace, nw) {
|
2015-10-03 21:51:53 -04:00
|
|
|
return nil, ipamapi.ErrPoolOverlap
|
|
|
|
}
|
|
|
|
// This is a new master pool, add it along with corresponding bitmask
|
2015-10-05 07:24:44 -04:00
|
|
|
aSpace.subnets[k] = &PoolData{Pool: nw, RefCount: 1}
|
|
|
|
return func() error { return aSpace.alloc.insertBitMask(k, nw) }, nil
|
2015-10-03 21:51:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is a new non-master pool
|
|
|
|
p := &PoolData{
|
|
|
|
ParentKey: SubnetKey{AddressSpace: k.AddressSpace, Subnet: k.Subnet},
|
|
|
|
Pool: nw,
|
|
|
|
Range: ipr,
|
|
|
|
RefCount: 1,
|
|
|
|
}
|
2015-10-05 07:24:44 -04:00
|
|
|
aSpace.subnets[k] = p
|
2015-10-03 21:51:53 -04:00
|
|
|
|
|
|
|
// Look for parent pool
|
2015-10-05 07:24:44 -04:00
|
|
|
pp, ok := aSpace.subnets[p.ParentKey]
|
2015-10-03 21:51:53 -04:00
|
|
|
if ok {
|
2015-10-05 07:24:44 -04:00
|
|
|
aSpace.incRefCount(pp, 1)
|
2015-10-03 21:51:53 -04:00
|
|
|
return func() error { return nil }, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent pool does not exist, add it along with corresponding bitmask
|
2015-10-05 07:24:44 -04:00
|
|
|
aSpace.subnets[p.ParentKey] = &PoolData{Pool: nw, RefCount: 1}
|
|
|
|
return func() error { return aSpace.alloc.insertBitMask(p.ParentKey, nw) }, nil
|
2015-10-03 21:51:53 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 07:24:44 -04:00
|
|
|
func (aSpace *addrSpace) updatePoolDBOnRemoval(k SubnetKey) (func() error, error) {
|
|
|
|
aSpace.Lock()
|
|
|
|
defer aSpace.Unlock()
|
2015-10-03 21:51:53 -04:00
|
|
|
|
2015-10-05 07:24:44 -04:00
|
|
|
p, ok := aSpace.subnets[k]
|
2015-10-03 21:51:53 -04:00
|
|
|
if !ok {
|
|
|
|
return nil, ipamapi.ErrBadPool
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:24:44 -04:00
|
|
|
aSpace.incRefCount(p, -1)
|
2015-10-03 21:51:53 -04:00
|
|
|
|
|
|
|
c := p
|
|
|
|
for ok {
|
|
|
|
if c.RefCount == 0 {
|
2015-10-05 07:24:44 -04:00
|
|
|
delete(aSpace.subnets, k)
|
2015-10-03 21:51:53 -04:00
|
|
|
if c.Range == nil {
|
|
|
|
return func() error {
|
2015-10-05 07:24:44 -04:00
|
|
|
bm, err := aSpace.alloc.retrieveBitmask(k, c.Pool)
|
2015-10-03 21:51:53 -04:00
|
|
|
if err != nil {
|
2015-10-20 20:05:01 -04:00
|
|
|
return types.InternalErrorf("could not find bitmask in datastore for pool %s removal: %v", k.String(), err)
|
2015-10-03 21:51:53 -04:00
|
|
|
}
|
|
|
|
return bm.Destroy()
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
k = c.ParentKey
|
2015-10-05 07:24:44 -04:00
|
|
|
c, ok = aSpace.subnets[k]
|
2015-10-03 21:51:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return func() error { return nil }, nil
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:24:44 -04:00
|
|
|
func (aSpace *addrSpace) incRefCount(p *PoolData, delta int) {
|
2015-10-03 21:51:53 -04:00
|
|
|
c := p
|
|
|
|
ok := true
|
|
|
|
for ok {
|
|
|
|
c.RefCount += delta
|
2015-10-05 07:24:44 -04:00
|
|
|
c, ok = aSpace.subnets[c.ParentKey]
|
2015-10-03 21:51:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks whether the passed subnet is a superset or subset of any of the subset in this config db
|
2015-10-05 07:24:44 -04:00
|
|
|
func (aSpace *addrSpace) contains(space string, nw *net.IPNet) bool {
|
|
|
|
for k, v := range aSpace.subnets {
|
2015-10-03 21:51:53 -04:00
|
|
|
if space == k.AddressSpace && k.ChildSubnet == "" {
|
|
|
|
if nw.Contains(v.Pool.IP) || v.Pool.Contains(nw.IP) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2015-10-05 07:24:44 -04:00
|
|
|
|
|
|
|
func (aSpace *addrSpace) store() datastore.DataStore {
|
|
|
|
aSpace.Lock()
|
|
|
|
defer aSpace.Unlock()
|
|
|
|
|
|
|
|
return aSpace.ds
|
|
|
|
}
|