-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathnode.go
1512 lines (1355 loc) · 54.8 KB
/
node.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2018 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ipnet
import (
"fmt"
"net"
"github.com/contiv/vpp/plugins/contivconf"
controller "github.com/contiv/vpp/plugins/controller/api"
customnetmodel "github.com/contiv/vpp/plugins/crd/handler/customnetwork/model"
extifmodel "github.com/contiv/vpp/plugins/crd/handler/externalinterface/model"
podmodel "github.com/contiv/vpp/plugins/ksr/model/pod"
"github.com/contiv/vpp/plugins/nodesync"
"github.com/contiv/vpp/plugins/podmanager"
"github.com/golang/protobuf/proto"
"go.ligato.io/cn-infra/v2/idxmap"
"go.ligato.io/vpp-agent/v3/pkg/models"
vpp_interfaces "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces"
vpp_l2 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l2"
vpp_l3 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l3"
vpp_srv6 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/srv6"
"github.com/pkg/errors"
)
/* Main VPP interface */
const (
loopbackNICLogicalName = "loopbackNIC" // logical name of the loopback interface configured instead of physical NICs
)
/* VXLANs */
const (
// VXLAN Network Identifier (or VXLAN Segment ID) for the default pod network
defaultPodVxlanVNI = 10
// name of the VXLAN for the default pod network
defaultPodVxlanName = "default"
// as VXLAN tunnels are added to a BD, they must be configured with the same
// and non-zero Split Horizon Group (SHG) number. Otherwise, flood packet may
// loop among servers with the same VXLAN segment because VXLAN tunnels are fully
// meshed among servers.
vxlanSplitHorizonGroup = 1
// vxlanBVIInterfacePrefix is the name prefix of the VXLAN BVI interface.
vxlanBVIInterfacePrefix = "vxlanBVI"
// DefaultVxlanBVIInterfaceName name of the VXLAN interface for the default pod network.
DefaultVxlanBVIInterfaceName = vxlanBVIInterfacePrefix
// name prefix of the VXLAN bridge domain
vxlanBDNamePrefix = "vxlanBD"
// podGwLoopbackInterfaceName is the name of the POD gateway loopback interface.
podGwLoopbackInterfaceName = "podGwLoop"
// VxlanVniPoolName is name for the ID pool of VXLAN VNIs
VxlanVniPoolName = "vni"
vxlanVNIPoolStart = 5000 // to leave enough space for custom config of the vswitch
vxlanVNIPoolEnd = 1 << 24 // given by VXLAN header
// vrfPoolName is name for the ID pool of VRFs
vrfPoolName = "vrf"
vrfPoolStart = 10 // to leave enough space for custom config of the vswitch
vrfPoolEnd = ^uint32(0) // VRF is uint32
)
// customNetworkInfo holds information about a custom network
type customNetworkInfo struct {
config *customnetmodel.CustomNetwork
// list of local pods in custom network
localPods map[string]*podmanager.LocalPod
// list of local interfaces (pod + external) in custom network
localInterfaces []string
// list of all pods in custom network
pods map[string]*podmanager.Pod
// list of all external interfaces in custom network
extInterfaces map[string]*extifmodel.ExternalInterface
// list of all interfaces (pod + external) in custom network
// (map[pod ID/external interface name]=list of interfaces for that pod/external interface)
interfaces map[string][]string
}
// prefix for the hardware address of VXLAN interfaces
var vxlanBVIHwAddrPrefix = []byte{0x12, 0x2b}
/********************** Other Node Connectivity Configuration ***********************/
// otherNodeConnectivityConfig return configuration used to connect this node with the given other node.
func (n *IPNet) otherNodeConnectivityConfig(node *nodesync.Node) (config controller.KeyValuePairs, err error) {
config = make(controller.KeyValuePairs)
// VXLAN for the default pod network
if n.ContivConf.GetRoutingConfig().NodeToNodeTransport == contivconf.VXLANTransport && len(n.nodeIP) > 0 {
vxlanCfg := n.vxlanToOtherNodeConfig(node, DefaultPodNetworkName, defaultPodVxlanVNI)
mergeConfiguration(config, vxlanCfg)
}
// VXLANs for custom networks
for _, nw := range n.customNetworks {
// get the VNI of the VXLAN
vni, err := n.GetOrAllocateVxlanVNI(nw.config.Name)
if err != nil {
return config, err
}
if nw.config != nil && nw.config.Type == customnetmodel.CustomNetwork_L2 {
vxlanCfg := n.vxlanToOtherNodeConfig(node, nw.config.Name, vni)
mergeConfiguration(config, vxlanCfg)
}
if nw.config != nil && nw.config.Type == customnetmodel.CustomNetwork_L3 &&
n.ContivConf.GetRoutingConfig().NodeToNodeTransport == contivconf.VXLANTransport {
vxlanCfg := n.vxlanToOtherNodeConfig(node, nw.config.Name, vni)
mergeConfiguration(config, vxlanCfg)
}
}
nextHop, err := n.otherNodeNextHopIP(node)
if err != nil {
n.Log.Error(err)
return config, err
}
// route to pods of the other node
if !n.ContivConf.GetIPAMConfig().UseExternalIPAM { // skip in case that external IPAM is in use
podsCfg, err := n.connectivityToOtherNodePods(DefaultPodNetworkName, node.ID, nextHop)
if err != nil {
n.Log.Error(err)
return config, err
}
mergeConfiguration(config, podsCfg)
}
// routes to pods in L3 custom networks
for _, nw := range n.customNetworks {
if nw.config != nil && nw.config.Type == customnetmodel.CustomNetwork_L3 {
podsCfg, err := n.connectivityToOtherNodePods(nw.config.Name, node.ID, nextHop)
if err != nil {
n.Log.Error(err)
return config, err
}
mergeConfiguration(config, podsCfg)
}
}
// route to the host stack of the other node
hostStackCfg, err := n.connectivityToOtherNodeHostStack(node.ID, nextHop)
if err != nil {
n.Log.Error(err)
return config, err
}
mergeConfiguration(config, hostStackCfg)
// route to management IPs of the other node
mgmIPConf, err := n.connectivityToOtherNodeManagementIPAddresses(node, nextHop)
if err != nil {
n.Log.Error(err)
return config, err
}
mergeConfiguration(config, mgmIPConf)
return config, nil
}
// vxlanToOtherNodeConfig returns configuration of the vxlan tunnel towards a remote node.
// If staticFib is true, also creates static ARP and FIB entries pointing to the remote node's BVI IP address.
func (n *IPNet) vxlanToOtherNodeConfig(node *nodesync.Node, network string, vni uint32) (
config controller.KeyValuePairs) {
config = make(controller.KeyValuePairs)
// get IP address of the node
nodeIP, err := n.otherNodeIP(node)
if err != nil {
return config
}
// VXLAN interface
key, vxlanIf := n.vxlanIfToOtherNode(network, vni, node.ID, nodeIP)
config[key] = vxlanIf
// ARP entry for the IP address on the opposite side
vxlanIP, _, err := n.IPAM.VxlanIPAddress(node.ID)
if err != nil {
n.Log.Error(err)
return config
}
key, vxlanArp := n.vxlanArpEntry(network, node.ID, vxlanIP)
config[key] = vxlanArp
// L2 FIB for the hardware address on the opposite side
key, vxlanFib := n.vxlanFibEntry(network, node.ID)
config[key] = vxlanFib
return config
}
// otherNodeNextHopIP returns next hop address for routes towards the other node.
func (n *IPNet) otherNodeNextHopIP(node *nodesync.Node) (nextHop net.IP, err error) {
switch n.ContivConf.GetRoutingConfig().NodeToNodeTransport {
case contivconf.SRv6Transport:
fallthrough // use NoOverlayTransport for other variables
case contivconf.NoOverlayTransport:
// route traffic destined to the other node directly
if len(node.VppIPAddresses) > 0 {
nextHop = node.VppIPAddresses[0].Address
} else {
nextHop, err = n.otherNodeIPFromID(node.ID)
if err != nil {
n.Log.Error(err)
return nextHop, err
}
}
case contivconf.VXLANTransport:
// route traffic destined to the other node via VXLANs
vxlanNextHop, _, err := n.IPAM.VxlanIPAddress(node.ID)
if err != nil {
n.Log.Error(err)
return nextHop, err
}
nextHop = vxlanNextHop
}
return nextHop, err
}
/*********************************** DHCP *************************************/
var (
// variable used only in the context of go routines running handleDHCPNotification
lastDHCPLease *vpp_interfaces.DHCPLease
)
// handleDHCPNotifications handles DHCP state change notifications
func (n *IPNet) handleDHCPNotification(notif idxmap.NamedMappingGenericEvent) {
n.Log.Info("DHCP notification received")
// check for validity of the DHCP event
if notif.Del {
lastDHCPLease = nil
n.Log.Info("Ignoring event of removed DHCP lease")
return
}
if !n.useDHCP {
n.Log.Info("Ignoring DHCP event, dynamic IP address assignment is disabled")
return
}
if notif.Value == nil {
n.Log.Warn("DHCP notification metadata is empty")
return
}
dhcpLease, isDHCPLease := notif.Value.(*vpp_interfaces.DHCPLease)
if !isDHCPLease {
n.Log.Warn("Received invalid DHCP notification")
return
}
if dhcpLease.InterfaceName != n.ContivConf.GetMainInterfaceName() {
n.Log.Debugf("DHCP notification for a non-main interface (%s)",
dhcpLease.InterfaceName)
return
}
if proto.Equal(dhcpLease, lastDHCPLease) {
// nothing has really changed, ignore
n.Log.Info("Ignoring DHCP event - this lease was already processed")
return
}
lastDHCPLease = dhcpLease
// parse DHCP lease fields
hostAddr, hostNet, defaultGw, err := n.parseDHCPLease(dhcpLease)
if err != nil {
return
}
// push event into the event loop
n.EventLoop.PushEvent(&NodeIPv4Change{
NodeIP: hostAddr,
NodeIPNet: hostNet,
DefaultGw: defaultGw,
})
n.Log.Infof("Sent NodeIPv4Change event to the event loop for DHCP lease: %+v", *dhcpLease)
}
// parseDHCPLease parses fields of a DHCP lease.
func (n *IPNet) parseDHCPLease(lease *vpp_interfaces.DHCPLease) (hostAddr net.IP, hostNet *net.IPNet, defaultGw net.IP, err error) {
// parse IP address of the default gateway
if lease.RouterIpAddress != "" {
defaultGw, _, err = net.ParseCIDR(lease.RouterIpAddress)
if err != nil {
n.Log.Errorf("Failed to parse DHCP route IP address: %v", err)
return
}
}
// parse host IP address and network
if lease.HostIpAddress != "" {
hostAddr, hostNet, err = net.ParseCIDR(lease.HostIpAddress)
if err != nil {
n.Log.Errorf("Failed to parse DHCP host IP address: %v", err)
return
}
}
return
}
/*********************** Global vswitch configuration *************************/
// enabledIPNeighborScan returns configuration for enabled IP neighbor scanning
// (used to clean up old ARP entries).
func (n *IPNet) enabledIPNeighborScan() (key string, config *vpp_l3.IPScanNeighbor) {
ipScanConfig := n.ContivConf.GetIPNeighborScanConfig()
config = &vpp_l3.IPScanNeighbor{
Mode: vpp_l3.IPScanNeighbor_IPV4,
ScanInterval: uint32(ipScanConfig.IPNeighborScanInterval),
StaleThreshold: uint32(ipScanConfig.IPNeighborStaleThreshold),
}
key = vpp_l3.IPScanNeighborKey()
return key, config
}
/************************************ NICs ************************************/
// externalInterfaceConfig returns configuration of an external interface of the vswitch VPP.
func (n *IPNet) externalInterfaceConfig(extIf *extifmodel.ExternalInterface, eventType configEventType) (
config controller.KeyValuePairs, updateConfig controller.KeyValuePairs, err error) {
config = make(controller.KeyValuePairs)
updateConfig = make(controller.KeyValuePairs)
myNodeName := n.ServiceLabel.GetAgentLabel()
for _, nodeIf := range extIf.Nodes {
if nodeIf.Node == myNodeName {
// parse IP address
var ip contivconf.IPsWithNetworks
if nodeIf.Ip != "" {
ipAddr, ipNet, err := net.ParseCIDR(nodeIf.Ip)
if err != nil {
n.Log.Warnf("Unable to parse interface %s IP: %v", nodeIf.VppInterfaceName, err)
} else {
ip = contivconf.IPsWithNetworks{&contivconf.IPWithNetwork{Address: ipAddr, Network: ipNet}}
}
}
vppIfName := nodeIf.VppInterfaceName
vrf := n.ContivConf.GetRoutingConfig().MainVRFID
if n.isDefaultPodNetwork(extIf.Network) || n.isL3Network(extIf.Network) {
vrf, _ = n.GetOrAllocateVrfID(extIf.Network)
}
if nodeIf.Vlan == 0 {
// standard interface config
key, iface := n.physicalInterface(nodeIf.VppInterfaceName, vrf, ip)
config[key] = iface
} else {
// VLAN subinterface config (main interface with no IP + subinterface)
key, iface := n.physicalInterface(nodeIf.VppInterfaceName, vrf, nil)
config[key] = iface
key, iface = n.subInterface(nodeIf.VppInterfaceName, vrf, nodeIf.Vlan, ip)
config[key] = iface
vppIfName = iface.Name
}
if !n.isDefaultPodNetwork(extIf.Network) && !n.isStubNetwork(extIf.Network) {
// post-configure interface in custom network
n.cacheCustomNetworkInterface(extIf.Network, nil, nil, extIf, vppIfName,
true, eventType != configDelete)
if n.isL2Network(extIf.Network) {
bdKey, bd := n.l2CustomNwBridgeDomain(n.customNetworks[extIf.Network])
updateConfig[bdKey] = bd
}
}
}
}
return
}
// physicalInterface returns configuration for physical interface - either the main interface
// connecting node with the rest of the cluster or an extra physical interface requested
// in the config file.
func (n *IPNet) physicalInterface(name string, vrf uint32, ips contivconf.IPsWithNetworks) (key string, config *vpp_interfaces.Interface) {
ifConfig := n.ContivConf.GetInterfaceConfig()
iface := &vpp_interfaces.Interface{
Name: name,
Type: vpp_interfaces.Interface_DPDK,
Enabled: true,
Vrf: vrf,
}
for _, ip := range ips {
iface.IpAddresses = append(iface.IpAddresses, ipNetToString(combineAddrWithNet(ip.Address, ip.Network)))
}
if n.ContivConf.UseVmxnet3() {
iface.Type = vpp_interfaces.Interface_VMXNET3_INTERFACE
iface.Link = &vpp_interfaces.Interface_VmxNet3{
VmxNet3: &vpp_interfaces.VmxNet3Link{
RxqSize: uint32(ifConfig.Vmxnet3RxRingSize),
TxqSize: uint32(ifConfig.Vmxnet3TxRingSize),
},
}
}
if interfaceRxModeType(ifConfig.InterfaceRxMode) != vpp_interfaces.Interface_RxMode_DEFAULT {
iface.RxModes = []*vpp_interfaces.Interface_RxMode{
{
DefaultMode: true,
Mode: interfaceRxModeType(ifConfig.InterfaceRxMode),
},
}
}
key = vpp_interfaces.InterfaceKey(name)
return key, iface
}
// subInterface returns configuration for a VLAN subinterface of an interface.
func (n *IPNet) subInterface(parentIfName string, vrf uint32, vlan uint32, ips contivconf.IPsWithNetworks) (
key string, config *vpp_interfaces.Interface) {
iface := &vpp_interfaces.Interface{
Name: n.getSubInterfaceName(parentIfName, vlan),
Type: vpp_interfaces.Interface_SUB_INTERFACE,
Enabled: true,
Vrf: vrf,
Link: &vpp_interfaces.Interface_Sub{
Sub: &vpp_interfaces.SubInterface{
ParentName: parentIfName,
SubId: vlan,
},
},
}
for _, ip := range ips {
iface.IpAddresses = append(iface.IpAddresses, ipNetToString(combineAddrWithNet(ip.Address, ip.Network)))
}
key = vpp_interfaces.InterfaceKey(iface.Name)
return key, iface
}
// getSubInterfaceName returns logical name for a VLAN subinterface of an interface.
func (n *IPNet) getSubInterfaceName(parentIfName string, vlan uint32) string {
return fmt.Sprintf("%s.%d", parentIfName, vlan)
}
// loopbackInterface returns configuration for loopback created when no physical interfaces
// are configured.
func (n *IPNet) loopbackInterface(ips contivconf.IPsWithNetworks) (key string, config *vpp_interfaces.Interface) {
iface := &vpp_interfaces.Interface{
Name: loopbackNICLogicalName,
Type: vpp_interfaces.Interface_SOFTWARE_LOOPBACK,
Enabled: true,
Vrf: n.ContivConf.GetRoutingConfig().MainVRFID,
}
for _, ip := range ips {
iface.IpAddresses = append(iface.IpAddresses, ipNetToString(combineAddrWithNet(ip.Address, ip.Network)))
}
key = vpp_interfaces.InterfaceKey(loopbackNICLogicalName)
return key, iface
}
// defaultRoute return configuration for default route connecting the node with the outside world.
func (n *IPNet) defaultRoute(gwIP net.IP, outIfName string) (key string, config *vpp_l3.Route) {
route := &vpp_l3.Route{
DstNetwork: anyNetAddrForAF(gwIP),
NextHopAddr: gwIP.String(),
OutgoingInterface: outIfName,
VrfId: n.ContivConf.GetRoutingConfig().MainVRFID,
}
key = models.Key(route)
return key, route
}
/************************************ VRFs ************************************/
// vrfMainTables returns main VRF tables (each for each sub-address family (SAFI), e.g. IPv4, IPv6)
func (n *IPNet) vrfMainTables() map[string]*vpp_l3.VrfTable {
tables := make(map[string]*vpp_l3.VrfTable)
routingCfg := n.ContivConf.GetRoutingConfig()
// Note: we are explicitly creating vrf tables even with zero vrf id (zero vrf tables are created automatically)
// to get uniform vrf table labeling in all cases
k, v := n.vrfTable(routingCfg.MainVRFID, vpp_l3.VrfTable_IPV4, "mainVRF")
tables[k] = v
if n.ContivConf.GetIPAMConfig().UseIPv6 ||
n.ContivConf.GetRoutingConfig().UseSRv6ForServices ||
n.ContivConf.GetRoutingConfig().NodeToNodeTransport == contivconf.SRv6Transport {
k, v := n.vrfTable(routingCfg.MainVRFID, vpp_l3.VrfTable_IPV6, "mainVRF")
tables[k] = v
}
return tables
}
// vrfTablesForPods returns VRF tables for networking between pods.
func (n *IPNet) vrfTablesForPods() map[string]*vpp_l3.VrfTable {
tables := make(map[string]*vpp_l3.VrfTable)
routingCfg := n.ContivConf.GetRoutingConfig()
if !n.ContivConf.GetIPAMConfig().UseIPv6 {
k, v := n.vrfTable(routingCfg.PodVRFID, vpp_l3.VrfTable_IPV4, "podVRF")
tables[k] = v
}
if n.ContivConf.GetIPAMConfig().UseIPv6 ||
n.ContivConf.GetRoutingConfig().UseSRv6ForServices {
k, v := n.vrfTable(routingCfg.PodVRFID, vpp_l3.VrfTable_IPV6, "podVRF")
tables[k] = v
}
return tables
}
// vrfTable creates configuration for VRF table and adds it to the tables <tables>
func (n *IPNet) vrfTable(vrfID uint32, protocol vpp_l3.VrfTable_Protocol, label string) (string, *vpp_l3.VrfTable) {
n.Log.Infof("Creating VRF table configuration: vrfID=%v, protocol=%v, label(without protocol)=%v", vrfID, protocol, label)
// creating vrf config
protocolStr := "IPv4"
if protocol == vpp_l3.VrfTable_IPV6 {
protocolStr = "IPv6"
}
vrf := &vpp_l3.VrfTable{
Id: vrfID,
Protocol: protocol,
Label: label + "-" + protocolStr,
}
// adding it to tables
key := vpp_l3.VrfTableKey(vrf.Id, vrf.Protocol)
return key, vrf
}
// routesPodToMainVRF returns non-drop routes from default Pod VRF to Main VRF.
func (n *IPNet) routesPodToMainVRF() map[string]*vpp_l3.Route {
routes := make(map[string]*vpp_l3.Route)
routingCfg := n.ContivConf.GetRoutingConfig()
// by default to go from Pod VRF via Main VRF
r1 := &vpp_l3.Route{
Type: vpp_l3.Route_INTER_VRF,
DstNetwork: anyNetAddrForAF(n.IPAM.PodGatewayIP(DefaultPodNetworkName)),
VrfId: routingCfg.PodVRFID,
ViaVrfId: routingCfg.MainVRFID,
NextHopAddr: anyAddrForAF(n.IPAM.PodGatewayIP(DefaultPodNetworkName)),
}
r1Key := models.Key(r1)
routes[r1Key] = r1
if n.ContivConf.GetRoutingConfig().NodeToNodeTransport == contivconf.VXLANTransport {
// host network (this node) routed from Pod VRF via Main VRF
// (only needed for overly mode (VXLAN), to have better prefix match so that the drop route is not in effect)
r2 := &vpp_l3.Route{
Type: vpp_l3.Route_INTER_VRF,
DstNetwork: n.IPAM.HostInterconnectSubnetThisNode().String(),
VrfId: routingCfg.PodVRFID,
ViaVrfId: routingCfg.MainVRFID,
NextHopAddr: anyAddrForAF(n.IPAM.HostInterconnectSubnetThisNode().IP),
}
r2Key := models.Key(r2)
routes[r2Key] = r2
}
return routes
}
// routesMainToPodVRF returns non-drop routes from Main VRF to default Pod VRF.
func (n *IPNet) routesMainToPodVRF() map[string]*vpp_l3.Route {
routes := make(map[string]*vpp_l3.Route)
routingCfg := n.ContivConf.GetRoutingConfig()
if n.ContivConf.GetRoutingConfig().NodeToNodeTransport == contivconf.VXLANTransport {
// pod subnet (all nodes) routed from Main VRF via Pod VRF (to go via VXLANs)
r1 := &vpp_l3.Route{
Type: vpp_l3.Route_INTER_VRF,
DstNetwork: n.IPAM.PodSubnetAllNodes(DefaultPodNetworkName).String(),
VrfId: routingCfg.MainVRFID,
ViaVrfId: routingCfg.PodVRFID,
NextHopAddr: anyAddrForAF(n.IPAM.PodSubnetAllNodes(DefaultPodNetworkName).IP),
}
r1Key := models.Key(r1)
routes[r1Key] = r1
// host network (all nodes) routed from Main VRF via Pod VRF (to go via VXLANs)
r2 := &vpp_l3.Route{
Type: vpp_l3.Route_INTER_VRF,
DstNetwork: n.IPAM.HostInterconnectSubnetAllNodes().String(),
VrfId: routingCfg.MainVRFID,
ViaVrfId: routingCfg.PodVRFID,
NextHopAddr: anyAddrForAF(n.IPAM.HostInterconnectSubnetAllNodes().IP),
}
r2Key := models.Key(r2)
routes[r2Key] = r2
} else {
// pod subnet (this node only) routed from Main VRF to Pod VRF
r1 := &vpp_l3.Route{
Type: vpp_l3.Route_INTER_VRF,
DstNetwork: n.IPAM.PodSubnetThisNode(DefaultPodNetworkName).String(),
VrfId: routingCfg.MainVRFID,
ViaVrfId: routingCfg.PodVRFID,
NextHopAddr: anyAddrForAF(n.IPAM.PodSubnetThisNode(DefaultPodNetworkName).IP),
}
r1Key := models.Key(r1)
routes[r1Key] = r1
}
if n.ContivConf.GetIPAMConfig().UseIPv6 {
// service subnet routed from Main VRF to Pod VRF
r1 := &vpp_l3.Route{
Type: vpp_l3.Route_INTER_VRF,
DstNetwork: n.IPAM.ServiceNetwork().String(),
VrfId: routingCfg.MainVRFID,
ViaVrfId: routingCfg.PodVRFID,
NextHopAddr: anyAddrForAF(n.IPAM.ServiceNetwork().IP),
}
r1Key := models.Key(r1)
routes[r1Key] = r1
}
return routes
}
// dropRoutesIntoPodVRF returns drop routes for default Pod VRF.
func (n *IPNet) dropRoutesIntoPodVRF() map[string]*vpp_l3.Route {
routes := make(map[string]*vpp_l3.Route)
routingCfg := n.ContivConf.GetRoutingConfig()
if n.ContivConf.GetIPAMConfig().UseIPv6 {
// drop packets destined to service subnet with no more specific routes
r1 := n.dropRoute(routingCfg.PodVRFID, n.IPAM.ServiceNetwork())
r1Key := models.Key(r1)
routes[r1Key] = r1
}
if n.ContivConf.GetRoutingConfig().NodeToNodeTransport == contivconf.VXLANTransport {
// drop packets destined to pods no longer deployed
r1 := n.dropRoute(routingCfg.PodVRFID, n.IPAM.PodSubnetAllNodes(DefaultPodNetworkName))
r1Key := models.Key(r1)
routes[r1Key] = r1
// drop packets destined to nodes no longer deployed
r2 := n.dropRoute(routingCfg.PodVRFID, n.IPAM.HostInterconnectSubnetAllNodes())
r2Key := models.Key(r2)
routes[r2Key] = r2
}
return routes
}
// dropRoute is a helper method to construct drop route.
func (n *IPNet) dropRoute(vrfID uint32, dstAddr *net.IPNet) *vpp_l3.Route {
return &vpp_l3.Route{
Type: vpp_l3.Route_DROP,
DstNetwork: dstAddr.String(),
VrfId: vrfID,
NextHopAddr: anyAddrForAF(dstAddr.IP),
}
}
// podGwLoopback returns configuration of the loopback interface used in the POD VRF
// to respond on POD gateway IP address.
func (n *IPNet) podGwLoopback(network string, vrf uint32) (key string, config *vpp_interfaces.Interface) {
lo := &vpp_interfaces.Interface{
Name: n.podGwLoopbackInterfaceName(network),
Type: vpp_interfaces.Interface_SOFTWARE_LOOPBACK,
Enabled: true,
IpAddresses: []string{ipNetToString(combineAddrWithNet(
n.IPAM.PodGatewayIP(network), n.IPAM.PodSubnetThisNode(network)))},
Vrf: vrf,
}
key = vpp_interfaces.InterfaceKey(lo.Name)
return key, lo
}
// podGwLoopbackInterfaceName returns the name of the loopback interface for pod default gateway.
func (n *IPNet) podGwLoopbackInterfaceName(network string) string {
if network == "" || network == DefaultPodNetworkName {
return podGwLoopbackInterfaceName
}
return podGwLoopbackInterfaceName + "-" + network
}
/************************** Custom Networks **************************/
// customNetworkConfig returns configuration of a custom netwok on the vswitch VPP.
func (n *IPNet) customNetworkConfig(nwConfig *customnetmodel.CustomNetwork, eventType configEventType) (
config controller.KeyValuePairs, err error) {
config = make(controller.KeyValuePairs)
nw := n.customNetworks[nwConfig.Name]
if nw == nil {
nw = &customNetworkInfo{
config: nwConfig,
localPods: map[string]*podmanager.LocalPod{},
pods: map[string]*podmanager.Pod{},
extInterfaces: map[string]*extifmodel.ExternalInterface{},
interfaces: map[string][]string{},
}
n.customNetworks[nwConfig.Name] = nw
} else {
nw.config = nwConfig
}
// get / allocate a VNI for the VXLAN
vni, err := n.GetOrAllocateVxlanVNI(nwConfig.Name)
if err != nil {
return config, err
}
if nwConfig.Type == customnetmodel.CustomNetwork_L2 {
// VXLANs to the other nodes
for _, node := range n.getRemoteNodesWithIP() {
vxlanCfg := n.vxlanToOtherNodeConfig(node, nw.config.Name, vni)
mergeConfiguration(config, vxlanCfg)
}
// bridge domain for local & VXLAN interfaces
bdKey, bd := n.l2CustomNwBridgeDomain(nw)
config[bdKey] = bd
// in case of delete event, release the VXLAN VNI
if eventType == configDelete {
n.ReleaseVxlanVNI(nwConfig.Name)
}
}
if nwConfig.Type == customnetmodel.CustomNetwork_L3 {
// get / allocate a VRF ID
vrfID, err := n.GetOrAllocateVrfID(nwConfig.Name)
if err != nil {
return config, err
}
// VRF for custom interfaces
proto := vpp_l3.VrfTable_IPV4
if isIPv6Str(nwConfig.SubnetCIDR) {
proto = vpp_l3.VrfTable_IPV6
}
vrfKey, vrf := n.vrfTable(vrfID, proto, nwConfig.Name)
config[vrfKey] = vrf
// loopback with the gateway IP address for PODs
// - used as the unnumbered IP for the POD facing interfaces
key, loop := n.podGwLoopback(nwConfig.Name, vrfID)
config[key] = loop
// VXLAN BD + BVI
key, bd := n.vxlanBridgeDomain(nwConfig.Name)
config[key] = bd
key, bvi, _ := n.vxlanBVILoopback(nwConfig.Name, vrfID)
config[key] = bvi
// connectivity to the other nodes
for _, node := range n.getRemoteNodesWithIP() {
// VXLANs to the other nodes
if n.ContivConf.GetRoutingConfig().NodeToNodeTransport == contivconf.VXLANTransport {
vxlanCfg := n.vxlanToOtherNodeConfig(node, nw.config.Name, vni)
mergeConfiguration(config, vxlanCfg)
}
// routes to pods in L3 custom networks
nextHop, err := n.otherNodeNextHopIP(node)
if err != nil {
n.Log.Error(err)
return config, err
}
routesCfg, err := n.connectivityToOtherNodePods(nw.config.Name, node.ID, nextHop)
if err != nil {
n.Log.Error(err)
return config, err
}
mergeConfiguration(config, routesCfg)
}
// configure local pods with interfaces that belong to this network
for _, pod := range nw.localPods {
podCfg, _ := n.podCustomIfsConfig(pod, eventType)
mergeConfiguration(config, podCfg)
}
// configure external interfaces that belong to this network
for _, extIf := range nw.extInterfaces {
ifCfg, _, _ := n.externalInterfaceConfig(extIf, eventType)
mergeConfiguration(config, ifCfg)
}
// in case of delete event, release the VRF & VNI
if eventType == configDelete {
n.ReleaseVxlanVNI(nwConfig.Name)
n.ReleaseVrfID(nwConfig.Name)
}
}
if eventType == configDelete {
n.customNetworks[nwConfig.Name].config = nil
}
return config, nil
}
// l2CustomNwBridgeDomain returns configuration for the bridge domain of a L2 custom network.
func (n *IPNet) l2CustomNwBridgeDomain(nw *customNetworkInfo) (key string, config *vpp_l2.BridgeDomain) {
if nw == nil {
return "", nil
}
bd := &vpp_l2.BridgeDomain{
Name: nw.config.Name,
Learn: true,
Forward: true,
Flood: true,
UnknownUnicastFlood: true,
}
// local interfaces
// SplitHorizonGroup must be zero for these!
for _, iface := range nw.localInterfaces {
bd.Interfaces = append(bd.Interfaces, &vpp_l2.BridgeDomain_Interface{
Name: iface,
})
}
// VXLANs to the other nodes
for _, node := range n.getRemoteNodesWithIP() {
bd.Interfaces = append(bd.Interfaces, &vpp_l2.BridgeDomain_Interface{
Name: n.nameForVxlanToOtherNode(nw.config.Name, node.ID),
SplitHorizonGroup: vxlanSplitHorizonGroup,
})
}
key = vpp_l2.BridgeDomainKey(bd.Name)
return key, bd
}
// cacheCustomNetworkInterface caches interface-related information for later use in custom networks.
// The local pod, pod or extIf arguments can be null.
func (n *IPNet) cacheCustomNetworkInterface(customNwName string, localPod *podmanager.LocalPod,
pod *podmanager.Pod, extIf *extifmodel.ExternalInterface, ifName string, cacheForLocal bool, isAdd bool) {
// custom network is not known yet create one
nw := n.customNetworks[customNwName]
if nw == nil {
nw = &customNetworkInfo{
localPods: map[string]*podmanager.LocalPod{},
pods: map[string]*podmanager.Pod{},
extInterfaces: map[string]*extifmodel.ExternalInterface{},
interfaces: map[string][]string{},
}
n.customNetworks[customNwName] = nw
}
// cache pods / interfaces belonging to this network
if isAdd {
if cacheForLocal {
nw.localInterfaces = sliceAppendIfNotExists(nw.localInterfaces, ifName)
if localPod != nil {
nw.localPods[localPod.ID.String()] = localPod
}
} else {
var key string
if pod != nil {
key = pod.ID.String()
} else {
key = extIf.Name
}
nw.interfaces[key] = sliceAppendIfNotExists(nw.interfaces[key], ifName)
if pod != nil {
nw.pods[pod.ID.String()] = pod
}
if extIf != nil {
nw.extInterfaces[extIf.Name] = extIf
}
}
if extIf != nil {
nw.extInterfaces[extIf.Name] = extIf
}
} else {
if cacheForLocal {
nw.localInterfaces = sliceRemove(nw.localInterfaces, ifName)
if localPod != nil {
delete(nw.localPods, localPod.ID.String())
}
} else {
var key string
if pod != nil {
key = pod.ID.String()
} else {
key = extIf.Name
}
nw.interfaces[key] = sliceRemove(nw.interfaces[key], ifName)
if pod != nil {
delete(nw.pods, pod.ID.String())
}
if extIf != nil {
delete(nw.extInterfaces, extIf.Name)
}
}
if extIf != nil {
delete(nw.extInterfaces, extIf.Name)
}
}
}
// isDefaultPodNetwork returns true if provided network name is the default pod network.
func (n *IPNet) isDefaultPodNetwork(nwName string) bool {
return nwName == DefaultPodNetworkName || nwName == ""
}
// isStubNetwork returns true if provided network name is the "stub" network (not connected anywhere).
func (n *IPNet) isStubNetwork(nwName string) bool {
return nwName == stubNetworkName
}
// isL2Network returns true if provided network name is a layer 2 (switched) network.
func (n *IPNet) isL2Network(nwName string) bool {
nw := n.customNetworks[nwName]
if nw == nil || nw.config == nil {
return false
}
return nw.config.Type == customnetmodel.CustomNetwork_L2
}
// isL3Network returns true if provided network name is a layer 3 (routed) network.
func (n *IPNet) isL3Network(nwName string) bool {
nw := n.customNetworks[nwName]
if nw == nil || nw.config == nil {
return false
}
return nw.config.Type == customnetmodel.CustomNetwork_L3
}
/************************** Bridge Domain with VXLANs **************************/
// vxlanBVILoopback returns configuration of the loopback interfaces acting as BVI
// for the bridge domain with VXLAN interfaces.
func (n *IPNet) vxlanBVILoopback(network string, vrf uint32) (key string, config *vpp_interfaces.Interface, err error) {
vxlanIP, vxlanIPNet, err := n.IPAM.VxlanIPAddress(n.NodeSync.GetNodeID())
if err != nil {
return "", nil, err
}
vxlan := &vpp_interfaces.Interface{
Name: n.vxlanBVIInterfaceName(network),
Type: vpp_interfaces.Interface_SOFTWARE_LOOPBACK,
Enabled: true,
IpAddresses: []string{ipNetToString(combineAddrWithNet(vxlanIP, vxlanIPNet))},
PhysAddress: hwAddrForNodeInterface(n.NodeSync.GetNodeID(), vxlanBVIHwAddrPrefix),
Vrf: vrf,
}
key = vpp_interfaces.InterfaceKey(vxlan.Name)
return key, vxlan, nil
}
// vxlanBridgeDomain returns configuration for the bridge domain with VXLAN interfaces.
func (n *IPNet) vxlanBridgeDomain(network string) (key string, config *vpp_l2.BridgeDomain) {
bd := &vpp_l2.BridgeDomain{
Name: n.vxlanBDName(network),
Learn: false,
Forward: true,
Flood: false,
UnknownUnicastFlood: false,
Interfaces: []*vpp_l2.BridgeDomain_Interface{
{
Name: n.vxlanBVIInterfaceName(network),
BridgedVirtualInterface: true,
SplitHorizonGroup: vxlanSplitHorizonGroup,
},
},
}
for _, node := range n.getRemoteNodesWithIP() {
bd.Interfaces = append(bd.Interfaces, &vpp_l2.BridgeDomain_Interface{
Name: n.nameForVxlanToOtherNode(network, node.ID),
SplitHorizonGroup: vxlanSplitHorizonGroup,
})
}
key = vpp_l2.BridgeDomainKey(bd.Name)
return key, bd
}
// vxlanBDName returns name of the VXLAN bridge domain.
func (n *IPNet) vxlanBDName(network string) string {
if network == "" || network == DefaultPodNetworkName {
return vxlanBDNamePrefix
}
return vxlanBDNamePrefix + "-" + network
}
// vxlanBVIInterfaceName returns the name of the VXLAN BVI interface.
func (n *IPNet) vxlanBVIInterfaceName(network string) string {
if network == "" || network == DefaultPodNetworkName {
return vxlanBVIInterfacePrefix
}
return vxlanBVIInterfacePrefix + "-" + network
}
// nameForVxlanToOtherNode returns logical name to use for VXLAN interface
// connecting this node with the given other node.
func (n *IPNet) nameForVxlanToOtherNode(vxlanName string, otherNodeID uint32) string {
return fmt.Sprintf("vxlan-%s-%d", vxlanName, otherNodeID)
}
// vxlanIfToOtherNode returns configuration for VXLAN interface connecting this node
// with the given other node.
func (n *IPNet) vxlanIfToOtherNode(vxlanName string, vni uint32, otherNodeID uint32, otherNodeIP net.IP) (
key string, config *vpp_interfaces.Interface) {