forked from contiv/ofnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vlrouter.go
executable file
·1351 lines (1156 loc) · 38.3 KB
/
vlrouter.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 2014 Cisco Systems Inc. All rights reserved.
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 ofnet
// This file implements the virtual router functionality using Vxlan overlay
// VXLAN tables are structured as follows
//
// +-------+ +-------------------+
// | Valid +---------------------------------------->| ARP to Controller |
// | Pkts +-->+-------+ +-------------------+
// +-------+ | Vlan | +---------+
// | Table +------->| IP Dst | +--------------+
// +-------+ | Lookup +--------->| Ucast Output |
// +---------- +--------------+
//
//
import (
"errors"
"fmt"
"net"
"net/rpc"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/contiv/libOpenflow/openflow13"
"github.com/contiv/libOpenflow/protocol"
"github.com/contiv/ofnet/ofctrl"
cmap "github.com/streamrail/concurrent-map"
)
// Vlrouter state.
// One Vlrouter instance exists on each host
type Vlrouter struct {
agent *OfnetAgent // Pointer back to ofnet agent that owns this
ofSwitch *ofctrl.OFSwitch // openflow switch we are talking to
policyAgent *PolicyAgent // Policy agent
svcProxy *ServiceProxy // Service proxy
// Fgraph tables
inputTable *ofctrl.Table // Packet lookup starts here
vlanTable *ofctrl.Table // Vlan Table. map port or VNI to vlan
ipTable *ofctrl.Table // IP lookup table
// Flow Database
flowDb map[string]*ofctrl.Flow // Database of flow entries
portVlanFlowDb map[uint32]*ofctrl.Flow // Database of flow entries
dscpFlowDb map[uint32][]*ofctrl.Flow // Database of flow entries
portDnsFlowDb cmap.ConcurrentMap // Database of flow entries
uplinkPortDb cmap.ConcurrentMap // Database of uplink ports
myRouterMac net.HardwareAddr //Router mac used for external proxy
anycastMac net.HardwareAddr //Anycast mac used for local endpoints
myBgpPeer string // bgp neighbor
unresolvedEPs cmap.ConcurrentMap // unresolved endpoint map
uplinkOfp uint32 // uplink intf portno mapping
}
// GetUplink API gets the uplink port with uplinkID from uplink DB
func (vl *Vlrouter) GetUplink(uplinkID string) *PortInfo {
uplink, ok := vl.uplinkPortDb.Get(uplinkID)
if !ok {
return nil
}
return uplink.(*PortInfo)
}
// NewVlrouter creates a new vlrouter instance
func NewVlrouter(agent *OfnetAgent, rpcServ *rpc.Server) *Vlrouter {
vlrouter := new(Vlrouter)
// Keep a reference to the agent
vlrouter.agent = agent
// Create policy agent
vlrouter.policyAgent = NewPolicyAgent(agent, rpcServ)
vlrouter.svcProxy = NewServiceProxy(agent)
// Create a flow dbs and my router mac
vlrouter.flowDb = make(map[string]*ofctrl.Flow)
vlrouter.portVlanFlowDb = make(map[uint32]*ofctrl.Flow)
vlrouter.dscpFlowDb = make(map[uint32][]*ofctrl.Flow)
vlrouter.portDnsFlowDb = cmap.New()
vlrouter.anycastMac, _ = net.ParseMAC("00:00:11:11:11:11")
vlrouter.unresolvedEPs = cmap.New()
vlrouter.uplinkPortDb = cmap.New()
return vlrouter
}
// MasterAdded handles new master added event
func (vl *Vlrouter) MasterAdded(master *OfnetNode) error {
return nil
}
// Handle switch connected notification
func (vl *Vlrouter) SwitchConnected(sw *ofctrl.OFSwitch) {
// Keep a reference to the switch
vl.ofSwitch = sw
log.Infof("Switch connected(vlrouter). installing flows")
vl.svcProxy.SwitchConnected(sw)
// Tell the policy agent about the switch
vl.policyAgent.SwitchConnected(sw)
// Init the Fgraph
vl.initFgraph()
}
// Handle switch disconnected notification
func (vl *Vlrouter) SwitchDisconnected(sw *ofctrl.OFSwitch) {
vl.policyAgent.SwitchDisconnected(sw)
vl.ofSwitch = nil
}
// Handle incoming packet
func (vl *Vlrouter) PacketRcvd(sw *ofctrl.OFSwitch, pkt *ofctrl.PacketIn) {
if pkt.TableId == SRV_PROXY_SNAT_TBL_ID || pkt.TableId == SRV_PROXY_DNAT_TBL_ID {
// these are destined to service proxy
vl.svcProxy.HandlePkt(pkt)
return
}
switch pkt.Data.Ethertype {
case 0x0806:
if (pkt.Match.Type == openflow13.MatchType_OXM) &&
(pkt.Match.Fields[0].Class == openflow13.OXM_CLASS_OPENFLOW_BASIC) &&
(pkt.Match.Fields[0].Field == openflow13.OXM_FIELD_IN_PORT) {
// Get the input port number
switch t := pkt.Match.Fields[0].Value.(type) {
case *openflow13.InPortField:
var inPortFld openflow13.InPortField
inPortFld = *t
vl.processArp(pkt.Data, inPortFld.InPort)
}
}
case protocol.IPv4_MSG:
var inPort uint32
if (pkt.TableId == 0) && (pkt.Match.Type == openflow13.MatchType_OXM) &&
(pkt.Match.Fields[0].Class == openflow13.OXM_CLASS_OPENFLOW_BASIC) &&
(pkt.Match.Fields[0].Field == openflow13.OXM_FIELD_IN_PORT) {
// Get the input port number
switch t := pkt.Match.Fields[0].Value.(type) {
case *openflow13.InPortField:
inPort = t.InPort
default:
log.Debugf("unknown match type %v for ipv4 pkt", t)
return
}
}
ipPkt := pkt.Data.Data.(*protocol.IPv4)
switch ipPkt.Protocol {
case protocol.Type_UDP:
udpPkt := ipPkt.Data.(*protocol.UDP)
switch udpPkt.PortDst {
case 53:
if pkt.Data.VLANID.VID != 0 {
vl.agent.incrErrStats("dnsPktUplink")
return
}
if dnsResp, err := processDNSPkt(vl.agent, inPort, udpPkt.Data); err == nil {
if respPkt, err := buildUDPRespPkt(&pkt.Data, dnsResp); err == nil {
vl.agent.incrStats("dnsPktReply")
pktOut := openflow13.NewPacketOut()
pktOut.Data = respPkt
pktOut.AddAction(openflow13.NewActionOutput(inPort))
vl.ofSwitch.Send(pktOut)
return
}
}
// re-inject DNS packet
ethPkt := buildDnsForwardPkt(&pkt.Data)
pktOut := openflow13.NewPacketOut()
pktOut.Data = ethPkt
pktOut.InPort = inPort
pktOut.AddAction(openflow13.NewActionOutput(openflow13.P_TABLE))
vl.agent.incrStats("dnsPktForward")
vl.ofSwitch.Send(pktOut)
return
}
}
default:
log.Errorf("Received unknown ethertype: %x", pkt.Data.Ethertype)
}
}
// InjectGARPs not implemented
func (vl *Vlrouter) InjectGARPs(epgID int) {
}
// GlobalConfigUpdate not implemented
func (vl *Vlrouter) GlobalConfigUpdate(cfg OfnetGlobalConfig) error {
return nil
}
/*AddLocalEndpoint does the following:
1) Adds endpoint to the OVS and the associated flows
2) Populates BGP RIB with local route to be propogated to neighbor
*/
func (vl *Vlrouter) AddLocalEndpoint(endpoint OfnetEndpoint) error {
log.Infof("Received add Local Endpoint for %v", endpoint)
if vl.agent.ctrler == nil {
return nil
}
dNATTbl := vl.ofSwitch.GetTable(SRV_PROXY_DNAT_TBL_ID)
// Install a flow entry for vlan mapping and point it to next table
portVlanFlow, err := createPortVlanFlow(vl.agent, vl.vlanTable, dNATTbl, &endpoint)
if err != nil {
log.Errorf("Error creating portvlan entry. Err: %v", err)
return err
}
// save the flow entry
vl.portVlanFlowDb[endpoint.PortNo] = portVlanFlow
// install DSCP flow entries if required
if endpoint.Dscp != 0 {
dscpV4Flow, dscpV6Flow, err := createDscpFlow(vl.agent, vl.vlanTable, dNATTbl, &endpoint)
if err != nil {
log.Errorf("Error installing DSCP flows. Err: %v", err)
return err
}
// save it for tracking
vl.dscpFlowDb[endpoint.PortNo] = []*ofctrl.Flow{dscpV4Flow, dscpV6Flow}
}
// get output flow
outPort, err := vl.ofSwitch.OutputPort(endpoint.PortNo)
if err != nil {
log.Errorf("Error creating output port %d. Err: %v", endpoint.PortNo, err)
return err
}
// Install the IP address
ipFlow, err := vl.ipTable.NewFlow(ofctrl.FlowMatch{
Priority: LOCAL_ENDPOINT_FLOW_TAGGED_PRIORITY,
Ethertype: 0x0800,
VlanId: endpoint.Vlan,
IpDa: &endpoint.IpAddr,
})
if err != nil {
log.Errorf("Error creating flow for endpoint: %+v. Err: %v", endpoint, err)
return err
}
ipFlow.PopVlan()
ipFlow2, err := vl.ipTable.NewFlow(ofctrl.FlowMatch{
Priority: LOCAL_ENDPOINT_FLOW_PRIORITY,
Ethertype: 0x0800,
IpDa: &endpoint.IpAddr,
})
if err != nil {
log.Errorf("Error creating flow for endpoint: %+v. Err: %v", endpoint, err)
return err
}
destMacAddr, _ := net.ParseMAC(endpoint.MacAddrStr)
// Set Mac addresses
ipFlow.SetMacDa(destMacAddr)
ipFlow.SetMacSa(vl.anycastMac)
ipFlow2.SetMacDa(destMacAddr)
ipFlow2.SetMacSa(vl.anycastMac)
// Point the route at output port
err = ipFlow2.Next(outPort)
if err != nil {
log.Errorf("Error installing IP flow for endpoint: %+v. Err: %v", endpoint, err)
return err
}
// Store the flow
flowId := vl.agent.getEndpointIdByIpVlan(endpoint.IpAddr, endpoint.Vlan)
vl.flowDb[flowId+"vlan"] = ipFlow2
// Point the route at output port
err = ipFlow.Next(outPort)
if err != nil {
log.Errorf("Error installing IP flow for endpoint: %+v. Err: %v", endpoint, err)
return err
}
// Store the flow
flowId = vl.agent.getEndpointIdByIpVlan(endpoint.IpAddr, endpoint.Vlan)
vl.flowDb[flowId] = ipFlow
if !vl.agent.isInternalBgp(&endpoint) {
// Install dst group entry for the endpoint
err = vl.policyAgent.AddEndpoint(&endpoint)
if err != nil {
log.Errorf("Error adding endpoint to policy agent{%+v}. Err: %v", endpoint, err)
return err
}
path := &OfnetProtoRouteInfo{
ProtocolType: "bgp",
localEpIP: endpoint.IpAddr.String(),
nextHopIP: "",
}
if vl.agent.GetRouterInfo() != nil {
path.nextHopIP = vl.agent.GetRouterInfo().RouterIP
}
vl.agent.AddLocalProtoRoute([]*OfnetProtoRouteInfo{path})
}
if endpoint.Ipv6Addr != nil && endpoint.Ipv6Addr.String() != "" {
err = vl.AddLocalIpv6Flow(endpoint)
if err != nil {
return err
}
}
return nil
}
/* RemoveLocalEndpoint does the following
1) Removes the local endpoint and associated flows from OVS
2) Withdraws the route from BGP RIB
*/
func (vl *Vlrouter) RemoveLocalEndpoint(endpoint OfnetEndpoint) error {
log.Infof("Received Remove Local Endpoint for endpoint:{%+v}", endpoint)
// Remove the port vlan flow.
portVlanFlow := vl.portVlanFlowDb[endpoint.PortNo]
if portVlanFlow != nil {
err := portVlanFlow.Delete()
if err != nil {
log.Errorf("Error deleting portvlan flow. Err: %v", err)
}
}
// Remove dscp flows.
dscpFlows, found := vl.dscpFlowDb[endpoint.PortNo]
if found {
for _, dflow := range dscpFlows {
err := dflow.Delete()
if err != nil {
log.Errorf("Error deleting dscp flow {%+v}. Err: %v", dflow, err)
}
}
}
// Find the flow entry
flowId := endpoint.EndpointID
ipFlow := vl.flowDb[flowId]
if ipFlow == nil {
log.Errorf("Error finding the flow for endpoint: %+v", endpoint)
return errors.New("Flow not found")
}
// Delete the Fgraph entry
err := ipFlow.Delete()
if err != nil {
log.Errorf("Error deleting the endpoint: %+v. Err: %v", endpoint, err)
}
flowId = endpoint.EndpointID + "vlan"
ipFlow = vl.flowDb[flowId]
if ipFlow == nil {
log.Errorf("Error finding the tagged flow for endpoint: %+v", endpoint)
return errors.New("Flow not found")
}
// Delete the Fgraph entry
err = ipFlow.Delete()
if err != nil {
log.Errorf("Error deleting the endpoint: %+v. Err: %v", endpoint, err)
}
vl.svcProxy.DelEndpoint(&endpoint)
// Remove the endpoint from policy tables
if !vl.agent.isInternalBgp(&endpoint) {
err = vl.policyAgent.DelEndpoint(&endpoint)
if err != nil {
log.Errorf("Error deleting endpoint to policy agent{%+v}. Err: %v", endpoint, err)
return err
}
}
path := &OfnetProtoRouteInfo{
ProtocolType: "bgp",
localEpIP: endpoint.IpAddr.String(),
nextHopIP: "",
}
if vl.agent.GetRouterInfo() != nil {
path.nextHopIP = vl.agent.GetRouterInfo().RouterIP
}
vl.agent.DeleteLocalProtoRoute([]*OfnetProtoRouteInfo{path})
if endpoint.Ipv6Addr != nil && endpoint.Ipv6Addr.String() != "" {
err = vl.RemoveLocalIpv6Flow(endpoint)
if err != nil {
return err
}
}
return nil
}
// UpdateLocalEndpoint update local endpoint state
func (vl *Vlrouter) UpdateLocalEndpoint(endpoint *OfnetEndpoint, epInfo EndpointInfo) error {
oldDscp := endpoint.Dscp
// Remove existing DSCP flows if required
if epInfo.Dscp == 0 || epInfo.Dscp != endpoint.Dscp {
// remove old DSCP flows
dscpFlows, found := vl.dscpFlowDb[endpoint.PortNo]
if found {
for _, dflow := range dscpFlows {
err := dflow.Delete()
if err != nil {
log.Errorf("Error deleting dscp flow {%+v}. Err: %v", dflow, err)
return err
}
}
}
}
// change DSCP value
endpoint.Dscp = epInfo.Dscp
// Add new DSCP flows if required
if epInfo.Dscp != 0 && epInfo.Dscp != oldDscp {
dNATTbl := vl.ofSwitch.GetTable(SRV_PROXY_DNAT_TBL_ID)
// add new dscp flows
dscpV4Flow, dscpV6Flow, err := createDscpFlow(vl.agent, vl.vlanTable, dNATTbl, endpoint)
if err != nil {
log.Errorf("Error installing DSCP flows. Err: %v", err)
return err
}
// save it for tracking
vl.dscpFlowDb[endpoint.PortNo] = []*ofctrl.Flow{dscpV4Flow, dscpV6Flow}
}
return nil
}
// Add IPv6 flows
func (vl *Vlrouter) AddLocalIpv6Flow(endpoint OfnetEndpoint) error {
outPort, err := vl.ofSwitch.OutputPort(endpoint.PortNo)
if err != nil {
log.Errorf("Error creating output port %d. Err: %v", endpoint.PortNo, err)
return err
}
// Install the IPv6 address
ipv6Flow, err := vl.ipTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
Ethertype: 0x86DD,
Ipv6Da: &endpoint.Ipv6Addr,
})
if err != nil {
log.Errorf("Error creating IPv6 flow for endpoint: %+v. Err: %v", endpoint, err)
return err
}
destMacAddr, _ := net.ParseMAC(endpoint.MacAddrStr)
// Set Mac addresses
ipv6Flow.SetMacDa(destMacAddr)
ipv6Flow.SetMacSa(vl.anycastMac)
// Point the route at output port
err = ipv6Flow.Next(outPort)
if err != nil {
log.Errorf("Error installing IPv6 flow for endpoint: %+v. Err: %v", endpoint, err)
return err
}
// Store the flow
flowId := vl.agent.getEndpointIdByIpVlan(endpoint.Ipv6Addr, endpoint.Vlan)
vl.flowDb[flowId] = ipv6Flow
if !vl.agent.isInternalBgp(&endpoint) {
// Install dst group entry for IPv6 endpoint
err = vl.policyAgent.AddIpv6Endpoint(&endpoint)
if err != nil {
log.Errorf("Error adding IPv6 endpoint to policy agent{%+v}. Err: %v", endpoint, err)
return err
}
// Add IPv6 route in BGP
path := &OfnetProtoRouteInfo{
ProtocolType: "bgp",
localEpIP: endpoint.Ipv6Addr.String(),
nextHopIP: "",
}
if vl.agent.GetRouterInfo() != nil {
path.nextHopIP = vl.agent.GetRouterInfo().RouterIP
}
vl.agent.AddLocalProtoRoute([]*OfnetProtoRouteInfo{path})
}
return nil
}
// Remove the IPv6 flow
func (vl *Vlrouter) RemoveLocalIpv6Flow(endpoint OfnetEndpoint) error {
// Find the IPv6 flow entry
flowId := vl.agent.getEndpointIdByIpVlan(endpoint.Ipv6Addr, endpoint.Vlan)
ipv6Flow := vl.flowDb[flowId]
if ipv6Flow == nil {
log.Errorf("Error finding the flow for endpoint: %+v", endpoint)
return errors.New("Flow not found")
}
// Delete the Fgraph entry
err := ipv6Flow.Delete()
if err != nil {
log.Errorf("Error deleting IPv6 endpoint: %+v. Err: %v", endpoint, err)
}
// Remove the endpoint from policy tables
if !vl.agent.isInternalBgp(&endpoint) {
err = vl.policyAgent.DelIpv6Endpoint(&endpoint)
if err != nil {
log.Errorf("Error deleting IPv6 endpoint from policy agent{%+v}. Err: %v", endpoint, err)
return err
}
}
path := &OfnetProtoRouteInfo{
ProtocolType: "bgp",
localEpIP: endpoint.Ipv6Addr.String(),
nextHopIP: "",
}
if vl.agent.GetRouterInfo() != nil {
path.nextHopIP = vl.agent.GetRouterInfo().RouterIP
}
vl.agent.DeleteLocalProtoRoute([]*OfnetProtoRouteInfo{path})
return nil
}
// Add a vlan.
// This is mainly used for mapping vlan id to Vxlan VNI
func (vl *Vlrouter) AddVlan(vlanId uint16, vni uint32, vrf string) error {
log.Infof("Received Add Vlan for vlanid :%d,vni %d", vlanId, vni)
vrf = "default"
vl.agent.vlanVrfMutex.Lock()
vl.agent.vlanVrf[vlanId] = &vrf
vl.agent.vlanVrfMutex.Unlock()
vl.agent.createVrf(vrf)
return nil
}
// Remove a vlan
func (vl *Vlrouter) RemoveVlan(vlanId uint16, vni uint32, vrf string) error {
// FIXME: Add this for multiple VRF support
vl.agent.vlanVrfMutex.Lock()
delete(vl.agent.vlanVrf, vlanId)
vl.agent.vlanVrfMutex.Unlock()
vl.agent.deleteVrf(vrf)
return nil
}
/* AddEndpoint does the following :
1)Adds a remote endpoint and associated flows to OVS
2)The remotes routes can be 3 endpoint types :
a) internal - json rpc based learning from peer netplugins/ofnetagents in the cluster
b) external - remote endpoint learn via BGP
c) external-bgp - endpoint of BGP peer
*/
func (vl *Vlrouter) AddEndpoint(endpoint *OfnetEndpoint) error {
priority := uint16(FLOW_MATCH_PRIORITY)
log.Infof("Received AddEndpoint for endpoint: %+v", endpoint)
if endpoint.Vni != 0 {
return nil
}
flowId := vl.agent.getEndpointIdByIpVlan(endpoint.IpAddr, endpoint.Vlan)
//nexthopEp := vl.agent.getEndpointByIpVrf(net.ParseIP(vl.agent.GetNeighbor()), "default")
if vl.agent.isExternal(endpoint) {
endpoint.Vlan = 0
priority = EXTERNAL_FLOW_PRIORITY
flowId = flowId + "external"
} else {
//All Contiv endpoints will be stamped with originator host mac
if endpoint.OriginatorMac != "" {
endpoint.PortNo = vl.uplinkOfp
endpoint.MacAddrStr = endpoint.OriginatorMac
}
if endpoint.PortNo == 0 {
if !vl.agent.isExternalBgp(endpoint) {
//for the remote endpoints maintain a cache of
//routes that need to be resolved to next hop.
// bgp peer resolution happens via ARP and hence not
//maintained in cache.
log.Infof("Storing endpoint info in cache")
//vl.unresolvedEPs.Set(endpoint.EndpointID, endpoint.EndpointID)
return nil
}
}
}
if vl.agent.isExternalBgp(endpoint) {
endpoint.Vlan = 0
if endpoint.PortNo == 0 {
return nil
}
flowId = flowId + "external"
}
vrfid := vl.agent.getvrfId(endpoint.Vrf)
if *vrfid == 0 {
log.Errorf("Invalid vrf name:%v", endpoint.Vrf)
return errors.New("Invalid vrf name")
}
//set vrf id as METADATA
//metadata, metadataMask := Vrfmetadata(*vrfid)
outPort, err := vl.ofSwitch.OutputPort(endpoint.PortNo)
if err != nil {
log.Errorf("Error creating output port %d. Err: %v", endpoint.PortNo, err)
return err
}
// Install the IP address
ipFlow, err := vl.ipTable.NewFlow(ofctrl.FlowMatch{
Priority: priority,
Ethertype: 0x0800,
IpDa: &endpoint.IpAddr,
IpDaMask: &endpoint.IpMask,
})
if err != nil {
log.Errorf("Error creating flow for endpoint: %+v. Err: %v", endpoint, err)
return err
}
// Set Mac addresses
if endpoint.Vlan != 0 {
ipFlow.SetVlan(endpoint.Vlan)
}
DAMac, _ := net.ParseMAC(endpoint.MacAddrStr)
ipFlow.SetMacDa(DAMac)
ipFlow.SetMacSa(vl.myRouterMac)
// Point it to output port
err = ipFlow.Next(outPort)
if err != nil {
log.Errorf("Error installing flow for endpoint: %+v. Err: %v", endpoint, err)
return err
}
// Install dst group entry for the endpoint
if vl.agent.isInternal(endpoint) {
err = vl.policyAgent.AddEndpoint(endpoint)
if err != nil {
log.Errorf("Error adding endpoint to policy agent{%+v}. Err: %v", endpoint, err)
return err
}
}
// Store it in flow db
vl.flowDb[flowId] = ipFlow
if endpoint.Ipv6Addr != nil && endpoint.Ipv6Addr.String() != "" {
err = vl.AddRemoteIpv6Flow(endpoint)
if err != nil {
log.Errorf("Error adding IPv6 flow for remote endpoint {%+v}. Err: %v", endpoint, err)
return err
}
}
return nil
}
// RemoveEndpoint removes an endpoint from the datapath
func (vl *Vlrouter) RemoveEndpoint(endpoint *OfnetEndpoint) error {
log.Infof("Received Remove endpoint for endpoint: %+v", endpoint)
if endpoint.Vni != 0 {
return nil
}
flowId := endpoint.EndpointID
if vl.agent.isExternalBgp(endpoint) {
flowId = flowId + "external"
vl.myBgpPeer = ""
}
//Delete the endpoint if it is in the cache
//if _, ok := vl.unresolvedEPs.Get(endpoint.EndpointID); ok {
// vl.unresolvedEPs.Remove(endpoint.EndpointID)
// return nil
//}
// Find the flow entry
if vl.agent.isExternal(endpoint) {
//This scenrio occurs when bgp unsets the external endpointtype
if _, ok := vl.flowDb[flowId+"external"]; ok {
flowId = flowId + "external"
}
}
ipFlow := vl.flowDb[flowId]
if ipFlow == nil {
log.Errorf("Error finding the flow for endpoint: %+v", endpoint)
return errors.New("Flow not found")
}
// Delete the Fgraph entry
err := ipFlow.Delete()
if err != nil {
log.Errorf("Error deleting the endpoint: %+v. Err: %v", endpoint, err)
}
//Remove the endpoint from policy tables
if vl.agent.isInternal(endpoint) {
err = vl.policyAgent.DelEndpoint(endpoint)
if err != nil {
log.Errorf("Error deleting endpoint to policy agent{%+v}. Err: %v", endpoint, err)
return err
}
}
if endpoint.Ipv6Addr != nil && endpoint.Ipv6Addr.String() != "" {
err = vl.RemoveRemoteIpv6Flow(endpoint)
if err != nil {
log.Errorf("Error deleting IPv6 endpoint from policy agent{%+v}. Err: %v", endpoint, err)
return err
}
}
return nil
}
// Add IPv6 flow for the remote endpoint
func (vl *Vlrouter) AddRemoteIpv6Flow(endpoint *OfnetEndpoint) error {
ipv6EpId := vl.agent.getEndpointIdByIpVlan(endpoint.Ipv6Addr, endpoint.Vlan)
if vl.agent.isExternal(endpoint) { //nexthopEp != nil && nexthopEp.PortNo != 0 {
// endpoint.MacAddrStr = nexthopEp.MacAddrStr
// endpoint.PortNo = nexthopEp.PortNo
} else {
if endpoint.OriginatorMac != "" {
endpoint.PortNo = vl.uplinkOfp
endpoint.MacAddrStr = endpoint.OriginatorMac
} else {
endpoint.PortNo = 0
endpoint.MacAddrStr = " "
//for the remote endpoints maintain a cache of
//routes that need to be resolved to next hop.
// bgp peer resolution happens via ARP and hence not
//maintainer in cache.
log.Debugf("Storing endpoint info in cache")
//vl.unresolvedEPs.Set(ipv6EpId, ipv6EpId)
}
}
if vl.agent.isExternalBgp(endpoint) {
//vl.myBgpPeer = endpoint.IpAddr.String()
}
log.Infof("AddRemoteIpv6Flow for endpoint: %+v", endpoint)
vrfid := vl.agent.getvrfId(endpoint.Vrf)
if *vrfid == 0 {
log.Errorf("Invalid vrf name:%v", endpoint.Vrf)
return errors.New("Invalid vrf name")
}
//set vrf id as METADATA
//metadata, metadataMask := Vrfmetadata(*vrfid)
outPort, err := vl.ofSwitch.OutputPort(endpoint.PortNo)
if err != nil {
log.Errorf("Error creating output port %d. Err: %v", endpoint.PortNo, err)
return err
}
// Install the IP address
ipv6Flow, err := vl.ipTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
Ethertype: 0x86DD,
Ipv6Da: &endpoint.Ipv6Addr,
Ipv6DaMask: &endpoint.Ipv6Mask,
})
if err != nil {
log.Errorf("Error creating flow for endpoint: %+v. Err: %v", endpoint, err)
return err
}
// Set Mac addresses
DAMac, _ := net.ParseMAC(endpoint.MacAddrStr)
ipv6Flow.SetMacDa(DAMac)
ipv6Flow.SetMacSa(vl.myRouterMac)
// Point it to output port
err = ipv6Flow.Next(outPort)
if err != nil {
log.Errorf("Error installing flow for endpoint: %+v. Err: %v", endpoint, err)
return err
}
// Install dst group entry for the endpoint
if vl.agent.isInternal(endpoint) {
err = vl.policyAgent.AddIpv6Endpoint(endpoint)
if err != nil {
log.Errorf("Error adding IPv6 endpoint to policy agent{%+v}. Err: %v", endpoint, err)
return err
}
}
// Store it in flow db
vl.flowDb[ipv6EpId] = ipv6Flow
return nil
}
// Remove IPv6 flow for the remote endpoint
func (vl *Vlrouter) RemoveRemoteIpv6Flow(endpoint *OfnetEndpoint) error {
//Delete the endpoint if it is in the cache
ipv6EpId := vl.agent.getEndpointIdByIpVlan(endpoint.Ipv6Addr, endpoint.Vlan)
//vl.unresolvedEPs.Remove(ipv6EpId)
// Find the flow entry
ipv6Flow := vl.flowDb[ipv6EpId]
if ipv6Flow == nil {
log.Errorf("Error finding the flow for endpoint: %+v", endpoint)
return errors.New("Flow not found")
}
// Delete the Fgraph entry
err := ipv6Flow.Delete()
if err != nil {
log.Errorf("Error deleting the endpoint: %+v. Err: %v", endpoint, err)
}
//Remove the endpoint from policy tables
if vl.agent.isInternal(endpoint) {
err = vl.policyAgent.DelIpv6Endpoint(endpoint)
if err != nil {
log.Errorf("Error deleting IPv6 endpoint from policy agent{%+v}. Err: %v", endpoint, err)
return err
}
}
return nil
}
// initialize Fgraph on the switch
func (vl *Vlrouter) initFgraph() error {
sw := vl.ofSwitch
// Create all tables
vl.inputTable = sw.DefaultTable()
vl.vlanTable, _ = sw.NewTable(VLAN_TBL_ID)
vl.ipTable, _ = sw.NewTable(IP_TBL_ID)
// setup SNAT table
// Matches in SNAT table (i.e. incoming) go to IP look up
vl.svcProxy.InitSNATTable(IP_TBL_ID)
// Init policy tables
err := vl.policyAgent.InitTables(SRV_PROXY_SNAT_TBL_ID)
if err != nil {
log.Fatalf("Error installing policy table. Err: %v", err)
return err
}
// Matches in DNAT go to Policy
vl.svcProxy.InitDNATTable(DST_GRP_TBL_ID)
//Create all drop entries
// Drop mcast source mac
bcastMac, _ := net.ParseMAC("01:00:00:00:00:00")
bcastSrcFlow, _ := vl.inputTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
MacSa: &bcastMac,
MacSaMask: &bcastMac,
})
bcastSrcFlow.Next(sw.DropAction())
// redirect dns requests from containers (oui 02:02:xx) to controller
macSaMask := net.HardwareAddr{0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00}
macSa := net.HardwareAddr{0x02, 0x02, 0x00, 0x00, 0x00, 0x00}
dnsRedirectFlow, _ := vl.inputTable.NewFlow(ofctrl.FlowMatch{
Priority: DNS_FLOW_MATCH_PRIORITY,
MacSa: &macSa,
MacSaMask: &macSaMask,
Ethertype: protocol.IPv4_MSG,
IpProto: protocol.Type_UDP,
UdpDstPort: 53,
})
dnsRedirectFlow.Next(sw.SendToController())
// re-inject dns requests
dnsReinjectFlow, _ := vl.inputTable.NewFlow(ofctrl.FlowMatch{
Priority: DNS_FLOW_MATCH_PRIORITY + 1,
MacSa: &macSa,
MacSaMask: &macSaMask,
VlanId: nameServerInternalVlanId,
Ethertype: protocol.IPv4_MSG,
IpProto: protocol.Type_UDP,
UdpDstPort: 53,
})
dnsReinjectFlow.PopVlan()
dnsReinjectFlow.Next(vl.vlanTable)
// Redirect ARP packets to controller
arpFlow, _ := vl.inputTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
Ethertype: 0x0806,
})
arpFlow.Next(sw.SendToController())
//All ARP replies will need IP table lookup
Mac, _ := net.ParseMAC("00:00:11:11:11:11")
arpFlow, _ = vl.inputTable.NewFlow(ofctrl.FlowMatch{
Priority: 300,
Ethertype: 0x0806,
MacSa: &Mac,
})
arpFlow.Next(vl.ipTable)
// Send all valid packets to vlan table
// This is installed at lower priority so that all packets that miss above
// flows will match entry
validPktFlow, _ := vl.inputTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MISS_PRIORITY,
})
validPktFlow.Next(vl.vlanTable)
// Drop all packets that miss Vlan lookup
vlanMissFlow, _ := vl.vlanTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MISS_PRIORITY,
})
vlanMissFlow.Next(sw.DropAction())
// Drop all packets that miss IP lookup
ipMissFlow, _ := vl.ipTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MISS_PRIORITY,
})
ipMissFlow.Next(sw.DropAction())
return nil
}
/*processArp does the following :
1) Process incoming ARP packets
2) Proxy with Router mac if arp request is from local internal endpoint
3) Proxy with interface mac is arp request is from remote endpoint
4) Learn MAC,Port of the source if its not learnt and it is bgp peer endpoint
*/
func (vl *Vlrouter) processArp(pkt protocol.Ethernet, inPort uint32) {
log.Infof("processing ARP packet on port %d", inPort)
switch t := pkt.Data.(type) {
case *protocol.ARP:
log.Infof("ARP packet: %+v", *t)
var arpHdr protocol.ARP = *t
var srcMac net.HardwareAddr
var intf *net.Interface
var err error
vl.agent.incrStats("ArpPktRcvd")
switch arpHdr.Operation {
case protocol.Type_Request:
vl.agent.incrStats("ArpReqRcvd")
// Lookup the Dest IP in the endpoint table
endpoint := vl.agent.getEndpointByIpVrf(arpHdr.IPDst, "default")
if endpoint == nil {
// Look for a service entry for the target IP
proxyMac := vl.svcProxy.GetSvcProxyMAC(arpHdr.IPDst)
if proxyMac == "" {
// If we dont know the IP address, dont send an ARP response
log.Debugf("Received ARP request for unknown IP: %v", arpHdr.IPDst)
vl.agent.incrStats("ArpReqUnknownDest")
return
}
srcMac, _ = net.ParseMAC(proxyMac)
} else {