-
Notifications
You must be signed in to change notification settings - Fork 0
/
address_space.go
228 lines (193 loc) · 6.04 KB
/
address_space.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
package ipam
import (
"context"
"fmt"
"net/netip"
"sync"
"github.com/containerd/log"
"github.com/docker/docker/libnetwork/internal/netiputil"
"github.com/docker/docker/libnetwork/ipamapi"
"github.com/docker/docker/libnetwork/types"
)
// addrSpace contains the pool configurations for the address space
type addrSpace struct {
// Master subnet pools, indexed by the value's stringified PoolData.Pool field.
subnets map[netip.Prefix]*PoolData
// Predefined pool for the address space
predefined []netip.Prefix
predefinedStartIndex int
mu sync.Mutex
}
func newAddrSpace(predefined []netip.Prefix) (*addrSpace, error) {
return &addrSpace{
subnets: map[netip.Prefix]*PoolData{},
predefined: predefined,
}, nil
}
// allocateSubnet adds the subnet k to the address space.
func (aSpace *addrSpace) allocateSubnet(nw, sub netip.Prefix) error {
aSpace.mu.Lock()
defer aSpace.mu.Unlock()
// Check if already allocated
if pool, ok := aSpace.subnets[nw]; ok {
var childExists bool
if sub != (netip.Prefix{}) {
_, childExists = pool.children[sub]
}
if sub == (netip.Prefix{}) || childExists {
// This means the same pool is already allocated. allocateSubnet is called when there
// is request for a pool/subpool. It should ensure there is no overlap with existing pools
return ipamapi.ErrPoolOverlap
}
}
return aSpace.allocateSubnetL(nw, sub)
}
func (aSpace *addrSpace) allocateSubnetL(nw, sub netip.Prefix) error {
// If master pool, check for overlap
if sub == (netip.Prefix{}) {
if aSpace.overlaps(nw) {
return ipamapi.ErrPoolOverlap
}
// This is a new master pool, add it along with corresponding bitmask
aSpace.subnets[nw] = newPoolData(nw)
return nil
}
// This is a new non-master pool (subPool)
if nw.Addr().BitLen() != sub.Addr().BitLen() {
return fmt.Errorf("pool and subpool are of incompatible address families")
}
// Look for parent pool
pp, ok := aSpace.subnets[nw]
if !ok {
// Parent pool does not exist, add it along with corresponding bitmask
pp = newPoolData(nw)
pp.autoRelease = true
aSpace.subnets[nw] = pp
}
pp.children[sub] = struct{}{}
return nil
}
// overlaps reports whether nw contains any IP addresses in common with any of
// the existing subnets in this address space.
func (aSpace *addrSpace) overlaps(nw netip.Prefix) bool {
for pool := range aSpace.subnets {
if pool.Overlaps(nw) {
return true
}
}
return false
}
// getPredefineds returns the predefined subnets for the address space.
//
// It should not be called concurrently with any other method on the addrSpace.
func (aSpace *addrSpace) getPredefineds() []netip.Prefix {
i := aSpace.predefinedStartIndex
// defensive in case the list changed since last update
if i >= len(aSpace.predefined) {
i = 0
}
return append(aSpace.predefined[i:], aSpace.predefined[:i]...)
}
// updatePredefinedStartIndex rotates the predefined subnet list by amt.
//
// It should not be called concurrently with any other method on the addrSpace.
func (aSpace *addrSpace) updatePredefinedStartIndex(amt int) {
i := aSpace.predefinedStartIndex + amt
if i < 0 || i >= len(aSpace.predefined) {
i = 0
}
aSpace.predefinedStartIndex = i
}
func (aSpace *addrSpace) allocatePredefinedPool(ipV6 bool) (netip.Prefix, error) {
aSpace.mu.Lock()
defer aSpace.mu.Unlock()
for i, nw := range aSpace.getPredefineds() {
if ipV6 != nw.Addr().Is6() {
continue
}
// Checks whether pool has already been allocated
if _, ok := aSpace.subnets[nw]; ok {
continue
}
// Shouldn't be necessary, but check prevents IP collisions should
// predefined pools overlap for any reason.
if !aSpace.overlaps(nw) {
aSpace.updatePredefinedStartIndex(i + 1)
err := aSpace.allocateSubnetL(nw, netip.Prefix{})
if err != nil {
return netip.Prefix{}, err
}
return nw, nil
}
}
v := 4
if ipV6 {
v = 6
}
return netip.Prefix{}, types.NotFoundErrorf("could not find an available, non-overlapping IPv%d address pool among the defaults to assign to the network", v)
}
func (aSpace *addrSpace) releaseSubnet(nw, sub netip.Prefix) error {
aSpace.mu.Lock()
defer aSpace.mu.Unlock()
p, ok := aSpace.subnets[nw]
if !ok {
return ipamapi.ErrBadPool
}
if sub != (netip.Prefix{}) {
if _, ok := p.children[sub]; !ok {
return ipamapi.ErrBadPool
}
delete(p.children, sub)
} else {
p.autoRelease = true
}
if len(p.children) == 0 && p.autoRelease {
delete(aSpace.subnets, nw)
}
return nil
}
func (aSpace *addrSpace) requestAddress(nw, sub netip.Prefix, prefAddress netip.Addr, opts map[string]string) (netip.Addr, error) {
aSpace.mu.Lock()
defer aSpace.mu.Unlock()
p, ok := aSpace.subnets[nw]
if !ok {
return netip.Addr{}, types.NotFoundErrorf("cannot find address pool for poolID:%v/%v", nw, sub)
}
if prefAddress != (netip.Addr{}) && !nw.Contains(prefAddress) {
return netip.Addr{}, ipamapi.ErrIPOutOfRange
}
if sub != (netip.Prefix{}) {
if _, ok := p.children[sub]; !ok {
return netip.Addr{}, types.NotFoundErrorf("cannot find address pool for poolID:%v/%v", nw, sub)
}
}
// In order to request for a serial ip address allocation, callers can pass in the option to request
// IP allocation serially or first available IP in the subnet
serial := opts[ipamapi.AllocSerialPrefix] == "true"
ip, err := getAddress(nw, p.addrs, prefAddress, sub, serial)
if err != nil {
return netip.Addr{}, err
}
return ip, nil
}
func (aSpace *addrSpace) releaseAddress(nw, sub netip.Prefix, address netip.Addr) error {
aSpace.mu.Lock()
defer aSpace.mu.Unlock()
p, ok := aSpace.subnets[nw]
if !ok {
return types.NotFoundErrorf("cannot find address pool for %v/%v", nw, sub)
}
if sub != (netip.Prefix{}) {
if _, ok := p.children[sub]; !ok {
return types.NotFoundErrorf("cannot find address pool for poolID:%v/%v", nw, sub)
}
}
if !address.IsValid() {
return types.InvalidParameterErrorf("invalid address")
}
if !nw.Contains(address) {
return ipamapi.ErrIPOutOfRange
}
defer log.G(context.TODO()).Debugf("Released address Address:%v Sequence:%s", address, p.addrs)
return p.addrs.Unset(netiputil.HostID(address, uint(nw.Bits())))
}