-
Notifications
You must be signed in to change notification settings - Fork 438
/
subnet.go
497 lines (449 loc) · 14.3 KB
/
subnet.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package ipam
import (
"net"
"strings"
"sync"
kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
"github.com/kubeovn/kube-ovn/pkg/util"
"k8s.io/klog/v2"
)
type Subnet struct {
Name string
mutex sync.RWMutex
Protocol string
V4CIDR *net.IPNet
V4FreeIPList IPRangeList
V4ReleasedIPList IPRangeList
V4ReservedIPList IPRangeList
V4PodToIP map[string]IP
V4IPToPod map[IP]string
V6CIDR *net.IPNet
V6FreeIPList IPRangeList
V6ReleasedIPList IPRangeList
V6ReservedIPList IPRangeList
V6PodToIP map[string]IP
V6IPToPod map[IP]string
PodToMac map[string]string
MacToPod map[string]string
}
func NewSubnet(name, cidrStr string, excludeIps []string) (*Subnet, error) {
excludeIps = util.ExpandExcludeIPs(excludeIps, cidrStr)
var cidrs []*net.IPNet
for _, cidrBlock := range strings.Split(cidrStr, ",") {
if _, cidr, err := net.ParseCIDR(cidrBlock); err != nil {
return nil, InvalidCIDRError
} else {
cidrs = append(cidrs, cidr)
}
}
// subnet.Spec.ExcludeIps contains both v4 and v6 addresses
v4ExcludeIps, v6ExcludeIps := util.SplitIpsByProtocol(excludeIps)
subnet := Subnet{}
protocol := util.CheckProtocol(cidrStr)
if protocol == kubeovnv1.ProtocolIPv4 {
firstIP, _ := util.FirstIP(cidrStr)
lastIP, _ := util.LastIP(cidrStr)
subnet = Subnet{
Name: name,
mutex: sync.RWMutex{},
Protocol: protocol,
V4CIDR: cidrs[0],
V4FreeIPList: IPRangeList{&IPRange{Start: IP(firstIP), End: IP(lastIP)}},
V4ReleasedIPList: IPRangeList{},
V4ReservedIPList: convertExcludeIps(v4ExcludeIps),
V4PodToIP: map[string]IP{},
V4IPToPod: map[IP]string{},
V6PodToIP: map[string]IP{},
V6IPToPod: map[IP]string{},
MacToPod: map[string]string{},
PodToMac: map[string]string{},
}
subnet.joinFreeWithReserve()
} else if protocol == kubeovnv1.ProtocolIPv6 {
firstIP, _ := util.FirstIP(cidrStr)
lastIP, _ := util.LastIP(cidrStr)
subnet = Subnet{
Name: name,
mutex: sync.RWMutex{},
Protocol: protocol,
V6CIDR: cidrs[0],
V6FreeIPList: IPRangeList{&IPRange{Start: IP(firstIP), End: IP(lastIP)}},
V6ReleasedIPList: IPRangeList{},
V6ReservedIPList: convertExcludeIps(v6ExcludeIps),
V4PodToIP: map[string]IP{},
V4IPToPod: map[IP]string{},
V6PodToIP: map[string]IP{},
V6IPToPod: map[IP]string{},
MacToPod: map[string]string{},
PodToMac: map[string]string{},
}
subnet.joinFreeWithReserve()
} else {
cidrBlocks := strings.Split(cidrStr, ",")
v4FirstIP, _ := util.FirstIP(cidrBlocks[0])
v4LastIP, _ := util.LastIP(cidrBlocks[0])
v6FirstIP, _ := util.FirstIP(cidrBlocks[1])
v6LastIP, _ := util.LastIP(cidrBlocks[1])
subnet = Subnet{
Name: name,
mutex: sync.RWMutex{},
Protocol: protocol,
V4CIDR: cidrs[0],
V4FreeIPList: IPRangeList{&IPRange{Start: IP(v4FirstIP), End: IP(v4LastIP)}},
V4ReleasedIPList: IPRangeList{},
V4ReservedIPList: convertExcludeIps(v4ExcludeIps),
V4PodToIP: map[string]IP{},
V4IPToPod: map[IP]string{},
V6CIDR: cidrs[1],
V6FreeIPList: IPRangeList{&IPRange{Start: IP(v6FirstIP), End: IP(v6LastIP)}},
V6ReleasedIPList: IPRangeList{},
V6ReservedIPList: convertExcludeIps(v6ExcludeIps),
V6PodToIP: map[string]IP{},
V6IPToPod: map[IP]string{},
MacToPod: map[string]string{},
PodToMac: map[string]string{},
}
subnet.joinFreeWithReserve()
}
return &subnet, nil
}
func (subnet *Subnet) GetRandomMac(podName string) string {
if mac, ok := subnet.PodToMac[podName]; ok {
return mac
}
for {
mac := util.GenerateMac()
if _, ok := subnet.MacToPod[mac]; !ok {
subnet.MacToPod[mac] = podName
subnet.PodToMac[podName] = mac
return mac
}
}
}
func (subnet *Subnet) GetStaticMac(podName, mac string) error {
if p, ok := subnet.MacToPod[mac]; ok && p != podName {
klog.Errorf("existPod %s, allocated pod %s, mac %s", p, podName, mac)
return ConflictError
}
subnet.MacToPod[mac] = podName
subnet.PodToMac[podName] = mac
return nil
}
func (subnet *Subnet) GetRandomAddress(podName string, skippedAddrs []string) (IP, IP, string, error) {
subnet.mutex.Lock()
defer subnet.mutex.Unlock()
if subnet.Protocol == kubeovnv1.ProtocolDual {
return subnet.getDualRandomAddress(podName, skippedAddrs)
} else if subnet.Protocol == kubeovnv1.ProtocolIPv4 {
return subnet.getV4RandomAddress(podName, skippedAddrs)
} else {
return subnet.getV6RandomAddress(podName, skippedAddrs)
}
}
func (subnet *Subnet) getDualRandomAddress(podName string, skippedAddrs []string) (IP, IP, string, error) {
v4IP, _, _, err := subnet.getV4RandomAddress(podName, skippedAddrs)
if err != nil {
return "", "", "", err
}
_, v6IP, mac, err := subnet.getV6RandomAddress(podName, skippedAddrs)
if err != nil {
return "", "", "", err
}
// allocated IPv4 address may be released in getV6RandomAddress()
if subnet.V4PodToIP[podName] != v4IP {
v4IP, _, _, _ = subnet.getV4RandomAddress(podName, skippedAddrs)
}
return v4IP, v6IP, mac, nil
}
func (subnet *Subnet) getV4RandomAddress(podName string, skippedAddrs []string) (IP, IP, string, error) {
if ip, ok := subnet.V4PodToIP[podName]; ok {
if !util.ContainsString(skippedAddrs, string(ip)) {
return ip, "", subnet.PodToMac[podName], nil
}
subnet.releaseAddr(podName)
}
if len(subnet.V4FreeIPList) == 0 {
if len(subnet.V4ReleasedIPList) == 0 {
return "", "", "", NoAvailableError
}
subnet.V4FreeIPList = subnet.V4ReleasedIPList
subnet.V4ReleasedIPList = IPRangeList{}
}
var ip IP
var idx int
for i, ipr := range subnet.V4FreeIPList {
for next := ipr.Start; !next.GreaterThan(ipr.End); next = next.Add(1) {
if !util.ContainsString(skippedAddrs, string(next)) {
ip = next
break
}
}
if ip != "" {
idx = i
break
}
}
if ip == "" {
return "", "", "", ConflictError
}
ipr := subnet.V4FreeIPList[idx]
part1 := &IPRange{Start: ipr.Start, End: ip.Sub(1)}
part2 := &IPRange{Start: ip.Add(1), End: ipr.End}
subnet.V4FreeIPList = append(subnet.V4FreeIPList[:idx], subnet.V4FreeIPList[idx+1:]...)
if !part1.Start.GreaterThan(part1.End) {
subnet.V4FreeIPList = append(subnet.V4FreeIPList, part1)
}
if !part2.Start.GreaterThan(part2.End) {
subnet.V4FreeIPList = append(subnet.V4FreeIPList, part2)
}
subnet.V4PodToIP[podName] = ip
subnet.V4IPToPod[ip] = podName
return ip, "", subnet.GetRandomMac(podName), nil
}
func (subnet *Subnet) getV6RandomAddress(podName string, skippedAddrs []string) (IP, IP, string, error) {
if ip, ok := subnet.V6PodToIP[podName]; ok {
if !util.ContainsString(skippedAddrs, string(ip)) {
return "", ip, subnet.PodToMac[podName], nil
}
subnet.releaseAddr(podName)
}
if len(subnet.V6FreeIPList) == 0 {
if len(subnet.V6ReleasedIPList) == 0 {
return "", "", "", NoAvailableError
}
subnet.V6FreeIPList = subnet.V6ReleasedIPList
subnet.V6ReleasedIPList = IPRangeList{}
}
var ip IP
var idx int
for i, ipr := range subnet.V6FreeIPList {
for next := ipr.Start; !next.GreaterThan(ipr.End); next = next.Add(1) {
if !util.ContainsString(skippedAddrs, string(next)) {
ip = next
break
}
}
if ip != "" {
idx = i
break
}
}
if ip == "" {
return "", "", "", ConflictError
}
ipr := subnet.V6FreeIPList[idx]
part1 := &IPRange{Start: ipr.Start, End: ip.Sub(1)}
part2 := &IPRange{Start: ip.Add(1), End: ipr.End}
subnet.V6FreeIPList = append(subnet.V6FreeIPList[:idx], subnet.V6FreeIPList[idx+1:]...)
if !part1.Start.GreaterThan(part1.End) {
subnet.V6FreeIPList = append(subnet.V6FreeIPList, part1)
}
if !part2.Start.GreaterThan(part2.End) {
subnet.V6FreeIPList = append(subnet.V6FreeIPList, part2)
}
subnet.V6PodToIP[podName] = ip
subnet.V6IPToPod[ip] = podName
return "", ip, subnet.GetRandomMac(podName), nil
}
func (subnet *Subnet) GetStaticAddress(podName string, ip IP, mac string, force bool) (IP, string, error) {
subnet.mutex.Lock()
defer subnet.mutex.Unlock()
var v4, v6 bool
if net.ParseIP(string(ip)).To4() != nil {
v4 = subnet.V4CIDR != nil
} else {
v6 = subnet.V6CIDR != nil
}
if v4 && !subnet.V4CIDR.Contains(net.ParseIP(string(ip))) {
return ip, mac, OutOfRangeError
}
if v6 && !subnet.V6CIDR.Contains(net.ParseIP(string(ip))) {
return ip, mac, OutOfRangeError
}
if mac == "" {
if m, ok := subnet.PodToMac[podName]; ok {
mac = m
} else {
mac = subnet.GetRandomMac(podName)
}
} else {
if err := subnet.GetStaticMac(podName, mac); err != nil {
return ip, mac, err
}
}
if v4 {
if existPod, ok := subnet.V4IPToPod[ip]; ok {
if existPod != podName {
klog.Errorf("existPod %s, allocated pod %s, ip %s", existPod, podName, ip)
return ip, mac, ConflictError
}
if !force {
return ip, mac, nil
}
}
if subnet.V4ReservedIPList.Contains(ip) {
subnet.V4PodToIP[podName] = ip
subnet.V4IPToPod[ip] = podName
return ip, mac, nil
}
if split, newFreeList := splitIPRangeList(subnet.V4FreeIPList, ip); split {
subnet.V4FreeIPList = newFreeList
subnet.V4PodToIP[podName] = ip
subnet.V4IPToPod[ip] = podName
return ip, mac, nil
} else {
if split, newReleasedList := splitIPRangeList(subnet.V4ReleasedIPList, ip); split {
subnet.V4ReleasedIPList = newReleasedList
subnet.V4PodToIP[podName] = ip
subnet.V4IPToPod[ip] = podName
return ip, mac, nil
}
}
} else if v6 {
if existPod, ok := subnet.V6IPToPod[ip]; ok {
if existPod != podName {
klog.Errorf("existPod %s, allocated pod %s, ip %s", existPod, podName, ip)
return ip, mac, ConflictError
}
if !force {
return ip, mac, nil
}
}
if subnet.V6ReservedIPList.Contains(ip) {
subnet.V6PodToIP[podName] = ip
subnet.V6IPToPod[ip] = podName
return ip, mac, nil
}
if split, newFreeList := splitIPRangeList(subnet.V6FreeIPList, ip); split {
subnet.V6FreeIPList = newFreeList
subnet.V6PodToIP[podName] = ip
subnet.V6IPToPod[ip] = podName
return ip, mac, nil
} else {
if split, newReleasedList := splitIPRangeList(subnet.V6ReleasedIPList, ip); split {
subnet.V6ReleasedIPList = newReleasedList
subnet.V6PodToIP[podName] = ip
subnet.V6IPToPod[ip] = podName
return ip, mac, nil
}
}
}
return ip, mac, NoAvailableError
}
func (subnet *Subnet) releaseAddr(podName string) {
ip, mac := IP(""), ""
var ok, changed bool
if ip, ok = subnet.V4PodToIP[podName]; ok {
delete(subnet.V4PodToIP, podName)
delete(subnet.V4IPToPod, ip)
if mac, ok = subnet.PodToMac[podName]; ok {
delete(subnet.PodToMac, podName)
delete(subnet.MacToPod, mac)
}
// When CIDR changed, do not relocate ip to CIDR list
if !subnet.V4CIDR.Contains(net.ParseIP(string(ip))) {
// Continue to release IPv6 address
klog.Infof("release v4 %s mac %s for %s, ignore ip", ip, mac, podName)
changed = true
}
if subnet.V4ReservedIPList.Contains(ip) {
klog.Infof("release v4 %s mac %s for %s, ip is in reserved list", ip, mac, podName)
changed = true
}
if merged, newReleasedList := mergeIPRangeList(subnet.V4ReleasedIPList, ip); !changed && merged {
subnet.V4ReleasedIPList = newReleasedList
klog.Infof("release v4 %s mac %s for %s, add ip to released list", ip, mac, podName)
}
}
if ip, ok = subnet.V6PodToIP[podName]; ok {
delete(subnet.V6PodToIP, podName)
delete(subnet.V6IPToPod, ip)
if mac, ok = subnet.PodToMac[podName]; ok {
delete(subnet.PodToMac, podName)
delete(subnet.MacToPod, mac)
}
changed = false
// When CIDR changed, do not relocate ip to CIDR list
if !subnet.V6CIDR.Contains(net.ParseIP(string(ip))) {
klog.Infof("release v6 %s mac %s for %s, ignore ip", ip, mac, podName)
changed = true
}
if subnet.V6ReservedIPList.Contains(ip) {
klog.Infof("release v6 %s mac %s for %s, ip is in reserved list", ip, mac, podName)
changed = true
}
if merged, newReleasedList := mergeIPRangeList(subnet.V6ReleasedIPList, ip); !changed && merged {
subnet.V6ReleasedIPList = newReleasedList
klog.Infof("release v6 %s mac %s for %s, add ip to released list", ip, mac, podName)
}
}
}
func (subnet *Subnet) ReleaseAddress(podName string) {
subnet.mutex.Lock()
defer subnet.mutex.Unlock()
subnet.releaseAddr(podName)
}
func (subnet *Subnet) ContainAddress(address IP) bool {
subnet.mutex.RLock()
defer subnet.mutex.RUnlock()
if _, ok := subnet.V4IPToPod[address]; ok {
return true
} else if _, ok := subnet.V6IPToPod[address]; ok {
return true
}
return false
}
func (subnet *Subnet) joinFreeWithReserve() {
protocol := subnet.Protocol
if protocol == kubeovnv1.ProtocolDual || protocol == kubeovnv1.ProtocolIPv4 {
for _, reserveIpr := range subnet.V4ReservedIPList {
newFreeList := IPRangeList{}
for _, freeIpr := range subnet.V4FreeIPList {
if iprl := splitRange(freeIpr, reserveIpr); iprl != nil {
newFreeList = append(newFreeList, iprl...)
}
}
subnet.V4FreeIPList = newFreeList
}
}
if protocol == kubeovnv1.ProtocolDual || protocol == kubeovnv1.ProtocolIPv6 {
for _, reserveIpr := range subnet.V6ReservedIPList {
newFreeList := IPRangeList{}
for _, freeIpr := range subnet.V6FreeIPList {
if iprl := splitRange(freeIpr, reserveIpr); iprl != nil {
newFreeList = append(newFreeList, iprl...)
}
}
subnet.V6FreeIPList = newFreeList
}
}
}
func (subnet *Subnet) GetPodAddress(podName string) (IP, IP, string, string) {
subnet.mutex.RLock()
defer subnet.mutex.RUnlock()
if subnet.Protocol == kubeovnv1.ProtocolIPv4 {
ip, mac := subnet.V4PodToIP[podName], subnet.PodToMac[podName]
return ip, "", mac, kubeovnv1.ProtocolIPv4
} else if subnet.Protocol == kubeovnv1.ProtocolIPv6 {
ip, mac := subnet.V6PodToIP[podName], subnet.PodToMac[podName]
return "", ip, mac, kubeovnv1.ProtocolIPv6
} else {
v4IP, v6IP, mac := subnet.V4PodToIP[podName], subnet.V6PodToIP[podName], subnet.PodToMac[podName]
return v4IP, v6IP, mac, kubeovnv1.ProtocolDual
}
}
func (subnet *Subnet) isIPAssignedToPod(ip, podName string) bool {
if existPod, ok := subnet.V4IPToPod[IP(ip)]; ok {
klog.V(4).Infof("v4 check ip assigned, existPod %s, podName %s", existPod, podName)
if existPod != podName {
return true
}
}
if existPod, ok := subnet.V6IPToPod[IP(ip)]; ok {
klog.V(4).Infof("v6 check ip assigned, existPod %s, podName %s", existPod, podName)
if existPod != podName {
return true
}
}
return false
}