-
Notifications
You must be signed in to change notification settings - Fork 0
/
allocator.go
329 lines (281 loc) · 10.2 KB
/
allocator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package defaultipam
import (
"context"
"fmt"
"net"
"net/netip"
"github.com/containerd/log"
"github.com/docker/docker/libnetwork/bitmap"
"github.com/docker/docker/libnetwork/internal/netiputil"
"github.com/docker/docker/libnetwork/ipamapi"
"github.com/docker/docker/libnetwork/ipamutils"
"github.com/docker/docker/libnetwork/ipbits"
"github.com/docker/docker/libnetwork/types"
)
const (
localAddressSpace = "LocalDefault"
globalAddressSpace = "GlobalDefault"
)
// Register registers the default ipam driver with libnetwork. It takes
// two optional address pools respectively containing the list of user-defined
// address pools for 'local' and 'global' address spaces.
func Register(ic ipamapi.Registerer, lAddrPools, gAddrPools []*ipamutils.NetworkToSplit) error {
localAddressPools := ipamutils.GetLocalScopeDefaultNetworks()
if len(lAddrPools) > 0 {
var err error
localAddressPools, err = ipamutils.SplitNetworks(lAddrPools)
if err != nil {
return err
}
}
globalAddressPools := ipamutils.GetGlobalScopeDefaultNetworks()
if len(gAddrPools) > 0 {
var err error
globalAddressPools, err = ipamutils.SplitNetworks(gAddrPools)
if err != nil {
return err
}
}
a, err := NewAllocator(localAddressPools, globalAddressPools)
if err != nil {
return err
}
cps := &ipamapi.Capability{RequiresRequestReplay: true}
return ic.RegisterIpamDriverWithCapabilities(ipamapi.DefaultIPAM, a, cps)
}
// Allocator provides per address space ipv4/ipv6 book keeping
type Allocator struct {
// The address spaces
local4, local6, global4, global6 *addrSpace
}
// NewAllocator returns an instance of libnetwork ipam
func NewAllocator(lcAs, glAs []*net.IPNet) (*Allocator, error) {
var (
a Allocator
err error
lcAs4, lcAs6, glAs4, glAs6 []netip.Prefix
)
lcAs4, lcAs6, err = splitByIPFamily(lcAs)
if err != nil {
return nil, fmt.Errorf("could not construct local address space: %w", err)
}
glAs4, glAs6, err = splitByIPFamily(glAs)
if err != nil {
return nil, fmt.Errorf("could not construct global address space: %w", err)
}
a.local4, err = newAddrSpace(lcAs4)
if err != nil {
return nil, fmt.Errorf("could not construct local v4 address space: %w", err)
}
a.local6, err = newAddrSpace(lcAs6)
if err != nil {
return nil, fmt.Errorf("could not construct local v6 address space: %w", err)
}
a.global4, err = newAddrSpace(glAs4)
if err != nil {
return nil, fmt.Errorf("could not construct global v4 address space: %w", err)
}
a.global6, err = newAddrSpace(glAs6)
if err != nil {
return nil, fmt.Errorf("could not construct global v6 address space: %w", err)
}
return &a, nil
}
func splitByIPFamily(s []*net.IPNet) ([]netip.Prefix, []netip.Prefix, error) {
var v4, v6 []netip.Prefix
for i, n := range s {
p, ok := netiputil.ToPrefix(n)
if !ok {
return []netip.Prefix{}, []netip.Prefix{}, fmt.Errorf("network at index %d (%v) is not in canonical form", i, n)
}
if p.Addr().Is4() {
v4 = append(v4, p)
} else {
v6 = append(v6, p)
}
}
return v4, v6, nil
}
// GetDefaultAddressSpaces returns the local and global default address spaces
func (a *Allocator) GetDefaultAddressSpaces() (string, string, error) {
return localAddressSpace, globalAddressSpace, nil
}
// RequestPool returns an address pool along with its unique id.
// addressSpace must be a valid address space name and must not be the empty string.
// If requestedPool is the empty string then the default predefined pool for addressSpace will be used, otherwise pool must be a valid IP address and length in CIDR notation.
// If requestedSubPool is not empty, it must be a valid IP address and length in CIDR notation which is a sub-range of requestedPool.
// requestedSubPool must be empty if requestedPool is empty.
func (a *Allocator) RequestPool(req ipamapi.PoolRequest) (ipamapi.AllocatedPool, error) {
log.G(context.TODO()).Debugf("RequestPool: %+v", req)
parseErr := func(err error) error {
return types.InternalErrorf("failed to parse pool request for address space %q pool %q subpool %q: %v", req.AddressSpace, req.Pool, req.SubPool, err)
}
if req.AddressSpace == "" {
return ipamapi.AllocatedPool{}, parseErr(ipamapi.ErrInvalidAddressSpace)
}
aSpace, err := a.getAddrSpace(req.AddressSpace, req.V6)
if err != nil {
return ipamapi.AllocatedPool{}, err
}
if req.Pool == "" && req.SubPool != "" {
return ipamapi.AllocatedPool{}, parseErr(ipamapi.ErrInvalidSubPool)
}
k := PoolID{AddressSpace: req.AddressSpace}
if req.Pool == "" {
if k.Subnet, err = aSpace.allocatePredefinedPool(req.V6); err != nil {
return ipamapi.AllocatedPool{}, err
}
return ipamapi.AllocatedPool{PoolID: k.String(), Pool: k.Subnet}, nil
}
if k.Subnet, err = netip.ParsePrefix(req.Pool); err != nil {
return ipamapi.AllocatedPool{}, parseErr(ipamapi.ErrInvalidPool)
}
if req.SubPool != "" {
if k.ChildSubnet, err = netip.ParsePrefix(req.SubPool); err != nil {
return ipamapi.AllocatedPool{}, types.InternalErrorf("invalid pool request: %v", ipamapi.ErrInvalidSubPool)
}
}
k.Subnet, k.ChildSubnet = k.Subnet.Masked(), k.ChildSubnet.Masked()
// Prior to https://github.com/moby/moby/pull/44968, libnetwork would happily accept a ChildSubnet with a bigger
// mask than its parent subnet. In such case, it was producing IP addresses based on the parent subnet, and the
// child subnet was not allocated from the address pool. Following condition take care of restoring this behavior
// for networks created before upgrading to v24.0.
if k.ChildSubnet.IsValid() && k.ChildSubnet.Bits() < k.Subnet.Bits() {
k.ChildSubnet = k.Subnet
}
err = aSpace.allocateSubnet(k.Subnet, k.ChildSubnet)
if err != nil {
return ipamapi.AllocatedPool{}, types.ForbiddenErrorf("invalid pool request: %v", err)
}
return ipamapi.AllocatedPool{PoolID: k.String(), Pool: k.Subnet}, nil
}
// ReleasePool releases the address pool identified by the passed id
func (a *Allocator) ReleasePool(poolID string) error {
log.G(context.TODO()).Debugf("ReleasePool(%s)", poolID)
k, err := PoolIDFromString(poolID)
if err != nil {
return types.InvalidParameterErrorf("invalid pool id: %s", poolID)
}
aSpace, err := a.getAddrSpace(k.AddressSpace, k.Is6())
if err != nil {
return err
}
return aSpace.releaseSubnet(k.Subnet, k.ChildSubnet)
}
// Given the address space, returns the local or global PoolConfig based on whether the
// address space is local or global. AddressSpace locality is registered with IPAM out of band.
func (a *Allocator) getAddrSpace(as string, v6 bool) (*addrSpace, error) {
switch as {
case localAddressSpace:
if v6 {
return a.local6, nil
}
return a.local4, nil
case globalAddressSpace:
if v6 {
return a.global6, nil
}
return a.global4, nil
}
return nil, types.InvalidParameterErrorf("cannot find address space %s", as)
}
func newPoolData(pool netip.Prefix) *PoolData {
ones, bits := pool.Bits(), pool.Addr().BitLen()
numAddresses := uint64(1 << uint(bits-ones))
// Allow /64 subnet
if pool.Addr().Is6() && numAddresses == 0 {
numAddresses--
}
// Generate the new address masks.
h := bitmap.New(numAddresses)
// Pre-reserve the network address on IPv4 networks large
// enough to have one (i.e., anything bigger than a /31.
if !(pool.Addr().Is4() && numAddresses <= 2) {
h.Set(0)
}
// Pre-reserve the broadcast address on IPv4 networks large
// enough to have one (i.e., anything bigger than a /31).
if pool.Addr().Is4() && numAddresses > 2 {
h.Set(numAddresses - 1)
}
return &PoolData{addrs: h, children: map[netip.Prefix]struct{}{}}
}
// 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) {
log.G(context.TODO()).Debugf("RequestAddress(%s, %v, %v)", poolID, prefAddress, opts)
k, err := PoolIDFromString(poolID)
if err != nil {
return nil, nil, types.InvalidParameterErrorf("invalid pool id: %s", poolID)
}
aSpace, err := a.getAddrSpace(k.AddressSpace, k.Is6())
if err != nil {
return nil, nil, err
}
var pref netip.Addr
if prefAddress != nil {
var ok bool
pref, ok = netip.AddrFromSlice(prefAddress)
if !ok {
return nil, nil, types.InvalidParameterErrorf("invalid preferred address: %v", prefAddress)
}
}
p, err := aSpace.requestAddress(k.Subnet, k.ChildSubnet, pref.Unmap(), opts)
if err != nil {
return nil, nil, err
}
return &net.IPNet{
IP: p.AsSlice(),
Mask: net.CIDRMask(k.Subnet.Bits(), k.Subnet.Addr().BitLen()),
}, nil, nil
}
// ReleaseAddress releases the address from the specified pool ID
func (a *Allocator) ReleaseAddress(poolID string, address net.IP) error {
log.G(context.TODO()).Debugf("ReleaseAddress(%s, %v)", poolID, address)
k, err := PoolIDFromString(poolID)
if err != nil {
return types.InvalidParameterErrorf("invalid pool id: %s", poolID)
}
aSpace, err := a.getAddrSpace(k.AddressSpace, k.Is6())
if err != nil {
return err
}
addr, ok := netip.AddrFromSlice(address)
if !ok {
return types.InvalidParameterErrorf("invalid address: %v", address)
}
return aSpace.releaseAddress(k.Subnet, k.ChildSubnet, addr.Unmap())
}
func getAddress(base netip.Prefix, bitmask *bitmap.Bitmap, prefAddress netip.Addr, ipr netip.Prefix, serial bool) (netip.Addr, error) {
var (
ordinal uint64
err error
)
log.G(context.TODO()).Debugf("Request address PoolID:%v %s Serial:%v PrefAddress:%v ", base, bitmask, serial, prefAddress)
if bitmask.Unselected() == 0 {
return netip.Addr{}, ipamapi.ErrNoAvailableIPs
}
if ipr == (netip.Prefix{}) && prefAddress == (netip.Addr{}) {
ordinal, err = bitmask.SetAny(serial)
} else if prefAddress != (netip.Addr{}) {
ordinal = netiputil.HostID(prefAddress, uint(base.Bits()))
err = bitmask.Set(ordinal)
} else {
start, end := netiputil.SubnetRange(base, ipr)
ordinal, err = bitmask.SetAnyInRange(start, end, serial)
}
switch err {
case nil:
// Convert IP ordinal for this subnet into IP address
return ipbits.Add(base.Addr(), ordinal, 0), nil
case bitmap.ErrBitAllocated:
return netip.Addr{}, ipamapi.ErrIPAlreadyAllocated
case bitmap.ErrNoBitAvailable:
return netip.Addr{}, ipamapi.ErrNoAvailableIPs
default:
return netip.Addr{}, err
}
}
// IsBuiltIn returns true for builtin drivers
func (a *Allocator) IsBuiltIn() bool {
return true
}