-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
allocator.go
423 lines (356 loc) · 11.6 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package ipam
import (
"errors"
"fmt"
"net"
"strings"
"time"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"github.com/cilium/cilium/pkg/metrics"
)
const (
metricAllocate = "allocate"
metricRelease = "release"
familyIPv4 = "ipv4"
familyIPv6 = "ipv6"
)
// Error definitions
var (
// ErrIPv4Disabled is returned when IPv4 allocation is disabled
ErrIPv4Disabled = errors.New("IPv4 allocation disabled")
// ErrIPv6Disabled is returned when Ipv6 allocation is disabled
ErrIPv6Disabled = errors.New("IPv6 allocation disabled")
)
func (ipam *IPAM) lookupIPsByOwner(owner string) (ips []net.IP) {
ipam.allocatorMutex.RLock()
defer ipam.allocatorMutex.RUnlock()
for ip, o := range ipam.owner {
if o == owner {
if parsedIP := net.ParseIP(ip); parsedIP != nil {
ips = append(ips, parsedIP)
}
}
}
return
}
// AllocateIP allocates a IP address.
func (ipam *IPAM) AllocateIP(ip net.IP, owner string) (err error) {
needSyncUpstream := true
_, err = ipam.allocateIP(ip, owner, needSyncUpstream)
return
}
// AllocateIPWithAllocationResult allocates an IP address, and returns the
// allocation result.
func (ipam *IPAM) AllocateIPWithAllocationResult(ip net.IP, owner string) (result *AllocationResult, err error) {
needSyncUpstream := true
return ipam.allocateIP(ip, owner, needSyncUpstream)
}
// AllocateIPWithoutSyncUpstream allocates a IP address without syncing upstream.
func (ipam *IPAM) AllocateIPWithoutSyncUpstream(ip net.IP, owner string) (result *AllocationResult, err error) {
needSyncUpstream := false
return ipam.allocateIP(ip, owner, needSyncUpstream)
}
// AllocateIPString is identical to AllocateIP but takes a string
func (ipam *IPAM) AllocateIPString(ipAddr, owner string) error {
ip := net.ParseIP(ipAddr)
if ip == nil {
return fmt.Errorf("Invalid IP address: %s", ipAddr)
}
return ipam.AllocateIP(ip, owner)
}
func (ipam *IPAM) allocateIP(ip net.IP, owner string, needSyncUpstream bool) (result *AllocationResult, err error) {
ipam.allocatorMutex.Lock()
defer ipam.allocatorMutex.Unlock()
if ipam.blacklist.Contains(ip) {
err = fmt.Errorf("IP %s is blacklisted, owned by %s", ip.String(), owner)
return
}
family := familyIPv4
if ip.To4() != nil {
if ipam.IPv4Allocator == nil {
err = ErrIPv4Disabled
return
}
if needSyncUpstream {
if result, err = ipam.IPv4Allocator.Allocate(ip, owner); err != nil {
return
}
} else {
if result, err = ipam.IPv4Allocator.AllocateWithoutSyncUpstream(ip, owner); err != nil {
return
}
}
} else {
family = familyIPv6
if ipam.IPv6Allocator == nil {
err = ErrIPv6Disabled
return
}
if needSyncUpstream {
if _, err = ipam.IPv6Allocator.Allocate(ip, owner); err != nil {
return
}
} else {
if _, err = ipam.IPv6Allocator.AllocateWithoutSyncUpstream(ip, owner); err != nil {
return
}
}
}
log.WithFields(logrus.Fields{
"ip": ip.String(),
"owner": owner,
}).Debugf("Allocated specific IP")
ipam.owner[ip.String()] = owner
metrics.IpamEvent.WithLabelValues(metricAllocate, family).Inc()
return
}
func (ipam *IPAM) allocateNextFamily(family Family, owner string, needSyncUpstream bool) (result *AllocationResult, err error) {
var allocator Allocator
switch family {
case IPv6:
allocator = ipam.IPv6Allocator
case IPv4:
allocator = ipam.IPv4Allocator
default:
err = fmt.Errorf("unknown address \"%s\" family requested", family)
return
}
if allocator == nil {
err = fmt.Errorf("%s allocator not available", family)
return
}
for {
if needSyncUpstream {
result, err = allocator.AllocateNext(owner)
} else {
result, err = allocator.AllocateNextWithoutSyncUpstream(owner)
}
if err != nil {
return
}
if !ipam.blacklist.Contains(result.IP) {
log.WithFields(logrus.Fields{
"ip": result.IP.String(),
"owner": owner,
}).Debugf("Allocated random IP")
ipam.owner[result.IP.String()] = owner
metrics.IpamEvent.WithLabelValues(metricAllocate, string(family)).Inc()
return
}
// The allocated IP is blacklisted, do not use it. The
// blacklisted IP is now allocated so it won't be allocated in
// the next iteration.
ipam.owner[result.IP.String()] = fmt.Sprintf("%s (blacklisted)", owner)
}
}
// AllocateNextFamily allocates the next IP of the requested address family
func (ipam *IPAM) AllocateNextFamily(family Family, owner string) (result *AllocationResult, err error) {
ipam.allocatorMutex.Lock()
defer ipam.allocatorMutex.Unlock()
needSyncUpstream := true
return ipam.allocateNextFamily(family, owner, needSyncUpstream)
}
// AllocateNextFamilyWithoutSyncUpstream allocates the next IP of the requested address family
// without syncing upstream
func (ipam *IPAM) AllocateNextFamilyWithoutSyncUpstream(family Family, owner string) (result *AllocationResult, err error) {
ipam.allocatorMutex.Lock()
defer ipam.allocatorMutex.Unlock()
needSyncUpstream := false
return ipam.allocateNextFamily(family, owner, needSyncUpstream)
}
// AllocateNext allocates the next available IPv4 and IPv6 address out of the
// configured address pool. If family is set to "ipv4" or "ipv6", then
// allocation is limited to the specified address family. If the pool has been
// drained of addresses, an error will be returned.
func (ipam *IPAM) AllocateNext(family, owner string) (ipv4Result, ipv6Result *AllocationResult, err error) {
if (family == "ipv6" || family == "") && ipam.IPv6Allocator != nil {
ipv6Result, err = ipam.AllocateNextFamily(IPv6, owner)
if err != nil {
return
}
}
if (family == "ipv4" || family == "") && ipam.IPv4Allocator != nil {
ipv4Result, err = ipam.AllocateNextFamily(IPv4, owner)
if err != nil {
if ipv6Result != nil {
ipam.ReleaseIP(ipv6Result.IP)
}
return
}
}
return
}
// AllocateNextWithExpiration is identical to AllocateNext but registers an
// expiration timer as well. This is identical to using AllocateNext() in
// combination with StartExpirationTimer()
func (ipam *IPAM) AllocateNextWithExpiration(family, owner string, timeout time.Duration) (ipv4Result, ipv6Result *AllocationResult, err error) {
ipv4Result, ipv6Result, err = ipam.AllocateNext(family, owner)
if err != nil {
return nil, nil, err
}
if timeout != time.Duration(0) {
for _, result := range []*AllocationResult{ipv4Result, ipv6Result} {
if result != nil {
result.ExpirationUUID, err = ipam.StartExpirationTimer(result.IP, timeout)
if err != nil {
if ipv4Result != nil {
ipam.ReleaseIP(ipv4Result.IP)
}
if ipv6Result != nil {
ipam.ReleaseIP(ipv6Result.IP)
}
return
}
}
}
}
return
}
func (ipam *IPAM) releaseIPLocked(ip net.IP) error {
family := familyIPv4
if ip.To4() != nil {
if ipam.IPv4Allocator == nil {
return ErrIPv4Disabled
}
if err := ipam.IPv4Allocator.Release(ip); err != nil {
return err
}
} else {
family = familyIPv6
if ipam.IPv6Allocator == nil {
return ErrIPv6Disabled
}
if err := ipam.IPv6Allocator.Release(ip); err != nil {
return err
}
}
owner := ipam.owner[ip.String()]
log.WithFields(logrus.Fields{
"ip": ip.String(),
"owner": owner,
}).Debugf("Released IP")
delete(ipam.owner, ip.String())
delete(ipam.expirationTimers, ip.String())
metrics.IpamEvent.WithLabelValues(metricRelease, family).Inc()
return nil
}
// ReleaseIP release a IP address.
func (ipam *IPAM) ReleaseIP(ip net.IP) error {
ipam.allocatorMutex.Lock()
defer ipam.allocatorMutex.Unlock()
return ipam.releaseIPLocked(ip)
}
// ReleaseIPString is identical to ReleaseIP but takes a string and supports
// referring to the IPs to be released with the IP itself or the owner name
// used during allocation. If the owner can be referred to multiple IPs, then
// all IPs are being released.
func (ipam *IPAM) ReleaseIPString(releaseArg string) (err error) {
var ips []net.IP
ip := net.ParseIP(releaseArg)
if ip == nil {
ips = ipam.lookupIPsByOwner(releaseArg)
if len(ips) == 0 {
return fmt.Errorf("Invalid IP address or owner name: %s", releaseArg)
}
} else {
ips = append(ips, ip)
}
for _, parsedIP := range ips {
// If any of the releases fail, report the failure
if err2 := ipam.ReleaseIP(parsedIP); err2 != nil {
err = err2
}
}
return
}
// Dump dumps the list of allocated IP addresses
func (ipam *IPAM) Dump() (allocv4 map[string]string, allocv6 map[string]string, status string) {
var st4, st6 string
ipam.allocatorMutex.RLock()
defer ipam.allocatorMutex.RUnlock()
if ipam.IPv4Allocator != nil {
allocv4, st4 = ipam.IPv4Allocator.Dump()
st4 = "IPv4: " + st4
for ip := range allocv4 {
owner, _ := ipam.owner[ip]
// If owner is not available, report IP but leave owner empty
allocv4[ip] = owner
}
}
if ipam.IPv6Allocator != nil {
allocv6, st6 = ipam.IPv6Allocator.Dump()
st6 = "IPv6: " + st6
for ip := range allocv6 {
owner, _ := ipam.owner[ip]
// If owner is not available, report IP but leave owner empty
allocv6[ip] = owner
}
}
status = strings.Join([]string{st4, st6}, ", ")
if status == "" {
status = "Not running"
}
return
}
// StartExpirationTimer installs an expiration timer for a previously allocated
// IP. Unless StopExpirationTimer is called in time, the IP will be released
// again after expiration of the specified timeout. The function will return a
// UUID representing the unique allocation attempt. The same UUID must be
// passed into StopExpirationTimer again.
//
// This function is to be used as allocation and use of an IP can be controlled
// by an external entity and that external entity can disappear. Therefore such
// users should register an expiration timer before returning the IP and then
// stop the expiration timer when the IP has been used.
func (ipam *IPAM) StartExpirationTimer(ip net.IP, timeout time.Duration) (string, error) {
ipam.allocatorMutex.Lock()
defer ipam.allocatorMutex.Unlock()
ipString := ip.String()
if _, ok := ipam.expirationTimers[ipString]; ok {
return "", fmt.Errorf("expiration timer already registered")
}
allocationUUID := uuid.New().String()
ipam.expirationTimers[ipString] = allocationUUID
go func(ip net.IP, allocationUUID string, timeout time.Duration) {
ipString := ip.String()
time.Sleep(timeout)
ipam.allocatorMutex.Lock()
defer ipam.allocatorMutex.Unlock()
if currentUUID, ok := ipam.expirationTimers[ipString]; ok {
if currentUUID == allocationUUID {
scopedLog := log.WithFields(logrus.Fields{"ip": ipString, "uuid": allocationUUID})
if err := ipam.releaseIPLocked(ip); err != nil {
scopedLog.WithError(err).Warning("Unable to release IP after expiration")
} else {
scopedLog.Warning("Released IP after expiration")
}
} else {
// This is an obsolete expiration timer. The IP
// was reused and a new expiration timer is
// already attached
}
} else {
// Expiration timer was removed. No action is required
}
}(ip, allocationUUID, timeout)
return allocationUUID, nil
}
// StopExpirationTimer will remove the expiration timer for a particular IP.
// The UUID returned by the symmetric StartExpirationTimer must be provided.
// The expiration timer will only be removed if the UUIDs match. Releasing an
// IP will also stop the expiration timer.
func (ipam *IPAM) StopExpirationTimer(ip net.IP, allocationUUID string) error {
ipam.allocatorMutex.Lock()
defer ipam.allocatorMutex.Unlock()
ipString := ip.String()
if currentUUID, ok := ipam.expirationTimers[ipString]; !ok {
return fmt.Errorf("no expiration timer registered")
} else if currentUUID != allocationUUID {
return fmt.Errorf("UUID mismatch, not stopping expiration timer")
}
delete(ipam.expirationTimers, ipString)
return nil
}