forked from contiv/ofnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vrouter.go
executable file
·1315 lines (1109 loc) · 37.3 KB
/
vrouter.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"
"github.com/contiv/libOpenflow/openflow13"
"github.com/contiv/libOpenflow/protocol"
"github.com/contiv/ofnet/ofctrl"
"github.com/spf13/pflag"
log "github.com/Sirupsen/logrus"
cmap "github.com/streamrail/concurrent-map"
)
// Vrouter state.
// One Vrouter instance exists on each host
type Vrouter 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
proxySNATTable *ofctrl.Table // Svc proxy SNAT table
hostSNATTable *ofctrl.Table // Egress via host nat port
hostDNATTable *ofctrl.Table // Ingress via host nat port
// 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
vlanDb map[uint16]*Vlan // Database of known vlans
myRouterMac net.HardwareAddr // Router Mac to be used
hostNATInfo HostPortInfo // Information for host NAT access
hostNATFlowDB cmap.ConcurrentMap // Database of host NAT flows
}
// Create a new vrouter instance
func NewVrouter(agent *OfnetAgent, rpcServ *rpc.Server) *Vrouter {
vrouter := new(Vrouter)
// Keep a reference to the agent
vrouter.agent = agent
// Create policy agent
vrouter.policyAgent = NewPolicyAgent(agent, rpcServ)
vrouter.svcProxy = NewServiceProxy(agent)
// Create a flow dbs and my router mac
vrouter.flowDb = make(map[string]*ofctrl.Flow)
vrouter.portVlanFlowDb = make(map[uint32]*ofctrl.Flow)
vrouter.dscpFlowDb = make(map[uint32][]*ofctrl.Flow)
vrouter.portDnsFlowDb = cmap.New()
vrouter.myRouterMac, _ = net.ParseMAC("00:00:11:11:11:11")
vrouter.vlanDb = make(map[uint16]*Vlan)
vrouter.hostNATFlowDB = cmap.New()
return vrouter
}
// Handle new master added event
func (self *Vrouter) MasterAdded(master *OfnetNode) error {
return nil
}
// Handle switch connected notification
func (self *Vrouter) SwitchConnected(sw *ofctrl.OFSwitch) {
// Keep a reference to the switch
self.ofSwitch = sw
log.Infof("Switch connected(vrouter). installing flows")
self.svcProxy.SwitchConnected(sw)
// Tell the policy agent about the switch
self.policyAgent.SwitchConnected(sw)
// Init the Fgraph
self.initFgraph()
}
// Handle switch disconnected notification
func (self *Vrouter) SwitchDisconnected(sw *ofctrl.OFSwitch) {
self.svcProxy.SwitchDisconnected(sw)
// Tell the policy agent about the switch disconnected
self.policyAgent.SwitchDisconnected(sw)
self.ofSwitch = nil
}
// Handle incoming packet
func (self *Vrouter) 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
self.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
self.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.TableId == 0) && (pkt.Match.Type == openflow13.MatchType_OXM) &&
len(pkt.Match.Fields) >= 2 &&
(pkt.Match.Fields[1].Class == openflow13.OXM_CLASS_OPENFLOW_BASIC) &&
(pkt.Match.Fields[1].Field == openflow13.OXM_FIELD_TUNNEL_ID) {
self.agent.incrErrStats("dnsPktVtep")
return
}
if dnsResp, err := processDNSPkt(self.agent, inPort, udpPkt.Data); err == nil {
if respPkt, err := buildUDPRespPkt(&pkt.Data, dnsResp); err == nil {
self.agent.incrStats("dnsPktReply")
pktOut := openflow13.NewPacketOut()
pktOut.Data = respPkt
pktOut.AddAction(openflow13.NewActionOutput(inPort))
self.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))
self.agent.incrStats("dnsPktForward")
self.ofSwitch.Send(pktOut)
return
}
}
default:
log.Errorf("Received unknown ethertype: %x", pkt.Data.Ethertype)
}
}
// InjectGARPs not implemented
func (self *Vrouter) InjectGARPs(epgID int) {
}
// GlobalConfigUpdate not implemented
func (self *Vrouter) GlobalConfigUpdate(cfg OfnetGlobalConfig) error {
return nil
}
// Add a local endpoint and install associated local route
func (self *Vrouter) AddLocalEndpoint(endpoint OfnetEndpoint) error {
dNATTbl := self.ofSwitch.GetTable(SRV_PROXY_DNAT_TBL_ID)
// check to make sure we have vlan to vni mapping
vni := self.agent.getvlanVniMap(endpoint.Vlan)
if vni == nil {
log.Errorf("VNI for vlan %d is not known", endpoint.Vlan)
return errors.New("Unknown Vlan")
}
// Install a flow entry for vlan mapping and point it to next table
portVlanFlow, err := createPortVlanFlow(self.agent, self.vlanTable, dNATTbl, &endpoint)
if err != nil {
log.Errorf("Error creating portvlan entry. Err: %v", err)
return err
}
// save the flow entry
self.portVlanFlowDb[endpoint.PortNo] = portVlanFlow
// install DSCP flow entries if required
if endpoint.Dscp != 0 {
dscpV4Flow, dscpV6Flow, err := createDscpFlow(self.agent, self.vlanTable, dNATTbl, &endpoint)
if err != nil {
log.Errorf("Error installing DSCP flows. Err: %v", err)
return err
}
// save it for tracking
self.dscpFlowDb[endpoint.PortNo] = []*ofctrl.Flow{dscpV4Flow, dscpV6Flow}
}
// Create the output port
outPort, err := self.ofSwitch.OutputPort(endpoint.PortNo)
if err != nil {
log.Errorf("Error creating output port %d. Err: %v", endpoint.PortNo, err)
return err
}
//Ip table look up will be vrf,ip
vrfid := self.agent.getvrfId(endpoint.Vrf)
if vrfid == nil {
log.Errorf("Invalid vrf name:%v", endpoint.Vrf)
return errors.New("Invalid vrf name")
}
vrfmetadata, vrfmetadataMask := Vrfmetadata(*vrfid)
// Install the IP address
ipFlow, err := self.ipTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
Ethertype: 0x0800,
IpDa: &endpoint.IpAddr,
Metadata: &vrfmetadata,
MetadataMask: &vrfmetadataMask,
})
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(self.myRouterMac)
// Point the route at 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
err = self.policyAgent.AddEndpoint(&endpoint)
if err != nil {
log.Errorf("Error adding endpoint to policy agent{%+v}. Err: %v", endpoint, err)
return err
}
// Store the flow
//using endpointId as key for flowDb because
//endpointId is a combination of ip and vrf
flowId := self.agent.getEndpointIdByIpVlan(endpoint.IpAddr, endpoint.Vlan)
self.flowDb[flowId] = ipFlow
// Install the IPv6 address
if endpoint.Ipv6Addr != nil && endpoint.Ipv6Addr.String() != "" {
err = self.AddLocalIpv6Flow(endpoint)
if err != nil {
return err
}
}
if endpoint.HostPvtIP.String() != "<nil>" && self.hostNATInfo.PortNo != 0 {
return self.setupHostNAT(endpoint, vrfmetadata, vrfmetadataMask)
}
return nil
}
func (self *Vrouter) setupHostNAT(endpoint OfnetEndpoint, vrfmetadata, vrfmetadataMask uint64) error {
// setup the host NAT flows
hsNAT, err := self.hostSNATTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
Ethertype: 0x0800,
InputPort: endpoint.PortNo,
})
if err != nil {
log.Errorf("Error adding endpoint to host SNAT {%+v}. Err: %v", endpoint, err)
return err
}
hsNAT.SetIPField(endpoint.HostPvtIP, "Src")
hsNAT.SetMacDa(self.hostNATInfo.MacAddr)
natOut, err := self.ofSwitch.OutputPort(self.hostNATInfo.PortNo)
if err != nil {
return err
}
// Point the route at output port
err = hsNAT.Next(natOut)
if err != nil {
log.Errorf("Error installing host NAT out flow for endpoint: %+v. Err: %v", endpoint, err)
return err
}
snatFlowKey := endpoint.HostPvtIP.String() + ".snat"
self.hostNATFlowDB.Set(snatFlowKey, hsNAT)
hdNAT, err := self.hostDNATTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
InputPort: self.hostNATInfo.PortNo,
Ethertype: 0x0800,
IpDa: &endpoint.HostPvtIP,
})
if err != nil {
log.Errorf("Error adding endpoint to host DNAT {%+v}. Err: %v", endpoint, err)
return err
}
hdNAT.SetIPField(endpoint.IpAddr, "Dst")
eMac, err := net.ParseMAC(endpoint.MacAddrStr)
if err != nil {
log.Errorf("Error parsing ep MAC: %s", endpoint.MacAddrStr)
return err
}
hdNAT.SetMacDa(eMac)
hdNAT.SetMetadata(vrfmetadata, vrfmetadataMask)
// Point to SNAT table
err = hdNAT.Next(self.proxySNATTable)
if err != nil {
log.Errorf("Error installing host NAT in flow for endpoint: %+v. Err: %v", endpoint, err)
return err
}
dnatFlowKey := endpoint.HostPvtIP.String() + ".dnat"
self.hostNATFlowDB.Set(dnatFlowKey, hdNAT)
return nil
}
// Remove local endpoint
func (self *Vrouter) RemoveLocalEndpoint(endpoint OfnetEndpoint) error {
// Remove host nat flows
dnatFlowKey := endpoint.HostPvtIP.String() + ".dnat"
hdFlow, _ := self.hostNATFlowDB.Get(dnatFlowKey)
if hdFlow != nil {
hdFlow.(*ofctrl.Flow).Delete()
self.hostNATFlowDB.Remove(dnatFlowKey)
}
snatFlowKey := endpoint.HostPvtIP.String() + ".snat"
hsFlow, _ := self.hostNATFlowDB.Get(snatFlowKey)
if hsFlow != nil {
hsFlow.(*ofctrl.Flow).Delete()
self.hostNATFlowDB.Remove(snatFlowKey)
}
// Remove the port vlan flow.
portVlanFlow := self.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 := self.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 := self.agent.getEndpointIdByIpVlan(endpoint.IpAddr, endpoint.Vlan)
ipFlow := self.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)
}
self.svcProxy.DelEndpoint(&endpoint)
// Remove the endpoint from policy tables
err = self.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 = self.RemoveLocalIpv6Flow(endpoint)
if err != nil {
return err
}
}
return nil
}
// UpdateLocalEndpoint update local endpoint state
func (self *Vrouter) 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 := self.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 := self.ofSwitch.GetTable(SRV_PROXY_DNAT_TBL_ID)
// add new dscp flows
dscpV4Flow, dscpV6Flow, err := createDscpFlow(self.agent, self.vlanTable, dNATTbl, endpoint)
if err != nil {
log.Errorf("Error installing DSCP flows. Err: %v", err)
return err
}
// save it for tracking
self.dscpFlowDb[endpoint.PortNo] = []*ofctrl.Flow{dscpV4Flow, dscpV6Flow}
}
return nil
}
// AddHostPort sets up host access.
func (self *Vrouter) AddHostPort(hp HostPortInfo) error {
if hp.Kind == "NAT" && self.hostNATInfo.PortNo != 0 {
log.Errorf("Host NAT port exists: %+v", self.hostNATInfo)
return fmt.Errorf("Host NAT port exists")
}
ipDa, daMask, err := ParseIPAddrMaskString(hp.IpAddr)
if err != nil {
log.Errorf("Bad host route - %v", err)
return err
}
netMask := pflag.ParseIPv4Mask(daMask.String())
maskedIP := ipDa.Mask(netMask)
// Save the info
self.hostNATInfo = hp
// Set up DNAT for ingress traffic
inNATFlow, _ := self.inputTable.NewFlow(ofctrl.FlowMatch{
InputPort: self.hostNATInfo.PortNo,
// Put at lower priority than ARP rule
Priority: FLOW_MATCH_PRIORITY - 1,
})
inNATFlow.Next(self.hostDNATTable)
// Packets from hostport that miss in IP table are dropped
inNATMiss, _ := self.ipTable.NewFlow(ofctrl.FlowMatch{
InputPort: self.hostNATInfo.PortNo,
Priority: FLOW_FLOOD_PRIORITY,
})
inNATMiss.Next(self.ofSwitch.DropAction())
// Deny packets explicitly addressed to the NAT subnet
denyFlow, _ := self.hostSNATTable.NewFlow(ofctrl.FlowMatch{
Priority: HOST_SNAT_DENY_PRIORITY,
Ethertype: 0x0800,
IpDa: &maskedIP,
IpDaMask: daMask,
})
denyFlow.Next(self.ofSwitch.DropAction())
self.hostNATFlowDB.Set("inNATFlow", inNATFlow)
self.hostNATFlowDB.Set("inNATMiss", inNATMiss)
self.hostNATFlowDB.Set("denyFlow", denyFlow)
return nil
}
// RemoveHostPort sets up host access.
func (self *Vrouter) RemoveHostPort(hp uint32) error {
if hp == self.hostNATInfo.PortNo {
self.hostNATInfo.PortNo = 0
inNATFlow, _ := self.hostNATFlowDB.Get("inNATFlow")
inNATMiss, _ := self.hostNATFlowDB.Get("inNATMiss")
denyFlow, _ := self.hostNATFlowDB.Get("denyFlow")
if inNATFlow != nil {
inNATFlow.(*ofctrl.Flow).Delete()
}
if inNATMiss != nil {
inNATMiss.(*ofctrl.Flow).Delete()
}
if denyFlow != nil {
denyFlow.(*ofctrl.Flow).Delete()
}
self.hostNATFlowDB.Remove("inNATFlow")
self.hostNATFlowDB.Remove("inNATMiss")
self.hostNATFlowDB.Remove("denyFlow")
}
return nil
}
// Add a local IPv6 flow
func (self *Vrouter) AddLocalIpv6Flow(endpoint OfnetEndpoint) error {
vrfid := self.agent.getvrfId(endpoint.Vrf)
if vrfid == nil {
log.Errorf("Invalid vrf name:%v", endpoint.Vrf)
return errors.New("Invalid vrf name")
}
// Create the output port
outPort, err := self.ofSwitch.OutputPort(endpoint.PortNo)
if err != nil {
log.Errorf("Error creating output port %d. Err: %v", endpoint.PortNo, err)
return err
}
//Ip table look up will be vrf,ip
vrfmetadata, vrfmetadataMask := Vrfmetadata(*vrfid)
// Install the IPv6 address
ipv6Flow, err := self.ipTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
Ethertype: 0x86DD,
Ipv6Da: &endpoint.Ipv6Addr,
Metadata: &vrfmetadata,
MetadataMask: &vrfmetadataMask,
})
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
ipv6Flow.SetMacDa(destMacAddr)
ipv6Flow.SetMacSa(self.myRouterMac)
// Point the route at 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
err = self.policyAgent.AddIpv6Endpoint(&endpoint)
if err != nil {
log.Errorf("Error adding IPv6 endpoint to policy agent{%+v}. Err: %v", endpoint, err)
return err
}
// Store the flow
//using endpointId as key for flowDb because
//endpointId is a combination of ip and vrf
flowId := self.agent.getEndpointIdByIpVlan(endpoint.Ipv6Addr, endpoint.Vlan)
self.flowDb[flowId] = ipv6Flow
return nil
}
// Remove local IPv6 flow
func (self *Vrouter) RemoveLocalIpv6Flow(endpoint OfnetEndpoint) error {
// Find the flow entry
flowId := self.agent.getEndpointIdByIpVlan(endpoint.Ipv6Addr, endpoint.Vlan)
ipv6Flow := self.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 the endpoint: %+v. Err: %v", endpoint, err)
}
// TODO: where do we add svcProxy endpoint? Do we need it for IPv6?
//self.svcProxy.DelEndpoint(&endpoint)
// Remove the endpoint from policy tables
err = self.policyAgent.DelIpv6Endpoint(&endpoint)
if err != nil {
log.Errorf("Error deleting IPv6 endpoint to policy agent{%+v}. Err: %v", endpoint, err)
return err
}
return nil
}
// Add virtual tunnel end point. This is mainly used for mapping remote vtep IP
// to ofp port number.
func (self *Vrouter) AddVtepPort(portNo uint32, remoteIp net.IP) error {
// Install VNI to vlan mapping for each vni
log.Infof("Adding VTEP for portno %v , remote IP : %v", portNo, remoteIp)
dnsVtepFlow, err := self.inputTable.NewFlow(ofctrl.FlowMatch{
Priority: DNS_FLOW_MATCH_PRIORITY + 2,
InputPort: portNo,
Ethertype: protocol.IPv4_MSG,
IpProto: protocol.Type_UDP,
UdpDstPort: 53,
})
if err != nil {
log.Errorf("Error creating nameserver flow entry. Err: %v", err)
return err
}
dnsVtepFlow.Next(self.vlanTable)
self.portDnsFlowDb.Set(fmt.Sprintf("%d", portNo), dnsVtepFlow)
self.agent.vlanVniMutex.RLock()
for vni, vlan := range self.agent.vniVlanMap {
// Install a flow entry for VNI/vlan and point it to Ip table
portVlanFlow, err := self.vlanTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
InputPort: portNo,
TunnelId: uint64(vni),
})
if err != nil && strings.Contains(err.Error(), "Flow already exists") {
log.Infof("VTEP %s already exists", remoteIp.String())
self.agent.vlanVniMutex.RUnlock()
return nil
} else if err != nil {
log.Errorf("Error adding Flow for VTEP %v. Err: %v", remoteIp, err)
self.agent.vlanVniMutex.RUnlock()
return err
}
// Set the metadata to indicate packet came in from VTEP port
vrf := self.agent.getvlanVrf(*vlan)
if vrf == nil {
log.Errorf("Invalid vlan to Vrf mapping for %v", *vlan)
self.agent.vlanVniMutex.RUnlock()
return errors.New("Invalid vlan to Vrf mapping")
}
vrfid := self.agent.getvrfId(*vrf)
if vrfid == nil || *vrfid == 0 {
log.Errorf("Invalid vrf name:%v", *vrf)
self.agent.vlanVniMutex.RUnlock()
return errors.New("Invalid vrf name")
}
//set vrf id as METADATA
vrfmetadata, vrfmetadataMask := Vrfmetadata(*vrfid)
metadata := METADATA_RX_VTEP | vrfmetadata
metadataMask := METADATA_RX_VTEP | vrfmetadataMask
portVlanFlow.SetMetadata(metadata, metadataMask)
// Point it to next table.
// Note that we bypass policy lookup on dest host.
sNATTbl := self.ofSwitch.GetTable(SRV_PROXY_SNAT_TBL_ID)
portVlanFlow.Next(sNATTbl)
// save the port vlan flow for cleaning up later
self.vlanDb[*vlan].vtepVlanFlowDb[portNo] = portVlanFlow
}
self.agent.vlanVniMutex.RUnlock()
var ep *OfnetEndpoint
// walk all routes and see if we need to install it
for endpoint := range self.agent.endpointDb.IterBuffered() {
ep = endpoint.Val.(*OfnetEndpoint)
if ep.OriginatorIp.String() == remoteIp.String() {
err := self.AddEndpoint(ep)
if err != nil {
log.Errorf("Error installing endpoint during vtep add(%v) EP: %+v. Err: %v", remoteIp, ep, err)
return err
}
}
}
return nil
}
// Remove a VTEP port
func (self *Vrouter) RemoveVtepPort(portNo uint32, remoteIp net.IP) error {
if f, ok := self.portDnsFlowDb.Get(fmt.Sprintf("%d", portNo)); ok {
if dnsVtepFlow, ok := f.(*ofctrl.Flow); ok {
if err := dnsVtepFlow.Delete(); err != nil {
log.Errorf("Error deleting nameserver flow. Err: %v", err)
}
}
}
self.portDnsFlowDb.Remove(fmt.Sprintf("%d", portNo))
for _, vlan := range self.vlanDb {
portVlanFlow := vlan.vtepVlanFlowDb[portNo]
portVlanFlow.Delete()
delete(vlan.vtepVlanFlowDb, portNo)
}
return nil
}
// Add a vlan.
// This is mainly used for mapping vlan id to Vxlan VNI
func (self *Vrouter) AddVlan(vlanId uint16, vni uint32, vrf string) error {
// check if the vlan already exists. if it does, we are done
if self.vlanDb[vlanId] != nil {
return nil
}
// create new vlan object
vlan := new(Vlan)
vlan.Vni = vni
vlan.vtepVlanFlowDb = make(map[uint32]*ofctrl.Flow)
_, ok := self.agent.createVrf(vrf)
if !ok {
return errors.New("Error creating Vrf")
}
self.agent.vlanVrfMutex.Lock()
self.agent.vlanVrf[vlanId] = &vrf
self.agent.vlanVrfMutex.Unlock()
// Walk all VTEP ports and add vni-vlan mapping for new VNI
self.agent.vtepTableMutex.RLock()
defer self.agent.vtepTableMutex.RUnlock()
for _, vtepPort := range self.agent.vtepTable {
// Install a flow entry for VNI/vlan and point it to macDest table
portVlanFlow, err := self.vlanTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
InputPort: *vtepPort,
TunnelId: uint64(vni),
})
if err != nil {
log.Errorf("Error creating port vlan flow for vlan %d. Err: %v", vlanId, err)
return err
}
var vrfid *uint16
if vrfid = self.agent.getvrfId(vrf); vrfid == nil {
log.Errorf("Invalid vrf name:%v", vrf)
return errors.New("Invalid vrf name")
}
//set vrf id as METADATA
vrfmetadata, vrfmetadataMask := Vrfmetadata(*vrfid)
// Set the metadata to indicate packet came in from VTEP port
metadata := METADATA_RX_VTEP | vrfmetadata
metadataMask := METADATA_RX_VTEP | vrfmetadataMask
portVlanFlow.SetMetadata(metadata, metadataMask)
// Point to next table
sNATTbl := self.ofSwitch.GetTable(SRV_PROXY_SNAT_TBL_ID)
portVlanFlow.Next(sNATTbl)
vlan.vtepVlanFlowDb[*vtepPort] = portVlanFlow
}
// store it in DB
self.vlanDb[vlanId] = vlan
return nil
}
// Remove a vlan
func (self *Vrouter) RemoveVlan(vlanId uint16, vni uint32, vrf string) error {
vlan := self.vlanDb[vlanId]
if vlan == nil {
log.Fatalf("Could not find the vlan %d", vlanId)
}
// uninstall vtep vlan mapping flows
for _, portVlanFlow := range vlan.vtepVlanFlowDb {
portVlanFlow.Delete()
}
// Remove it from DB
delete(self.vlanDb, vlanId)
err := self.agent.deleteVrf(vrf)
if err != nil {
return err
}
self.agent.vlanVrfMutex.Lock()
delete(self.agent.vlanVrf, vlanId)
self.agent.vlanVrfMutex.Unlock()
return nil
}
// AddEndpoint Add an endpoint to the datapath
func (self *Vrouter) AddEndpoint(endpoint *OfnetEndpoint) error {
if endpoint.Vni == 0 {
return nil
}
// Lookup the VTEP for the endpoint
vtepPort := self.agent.getvtepTablePort(endpoint.OriginatorIp.String())
if vtepPort == nil {
log.Warnf("Could not find the VTEP for endpoint: %+v", endpoint)
// Return if VTEP is not found. We'll install the route when VTEP is added
return nil
}
// Install the endpoint in OVS
// Create an output port for the vtep
outPort, err := self.ofSwitch.OutputPort(*vtepPort)
if err != nil {
log.Errorf("Error creating output port %d. Err: %v", *vtepPort, err)
return err
}
vrfid := self.agent.getvrfId(endpoint.Vrf)
if vrfid == nil || *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)
// Install the IP address
ipFlow, err := self.ipTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
Ethertype: 0x0800,
IpDa: &endpoint.IpAddr,
Metadata: &metadata,
MetadataMask: &metadataMask,
})
if err != nil {
log.Errorf("Error creating flow for endpoint: %+v. Err: %v", endpoint, err)
return err
}
// Set Mac addresses
ipFlow.SetMacDa(self.myRouterMac)
// Set VNI
ipFlow.SetTunnelId(uint64(endpoint.Vni))
ipFlow.Next(outPort)
// 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
err = self.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
flowId := self.agent.getEndpointIdByIpVlan(endpoint.IpAddr, endpoint.Vlan)
self.flowDb[flowId] = ipFlow
// Install dst group entry for IPv6 endpoint
if endpoint.Ipv6Addr != nil && endpoint.Ipv6Addr.String() != "" {
err = self.AddRemoteIpv6Flow(endpoint)
if err != nil {
log.Errorf("Error adding IPv6 flow {%+v}. Err: %v", endpoint, err)
return err
}
}
return nil
}
// RemoveEndpoint removes an endpoint from the datapath
func (self *Vrouter) RemoveEndpoint(endpoint *OfnetEndpoint) error {
// Find the flow entry
if endpoint.Vni == 0 {
return nil
}
flowId := self.agent.getEndpointIdByIpVlan(endpoint.IpAddr, endpoint.Vlan)
ipFlow := self.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
err = self.policyAgent.DelEndpoint(endpoint)
if err != nil {
log.Errorf("Error deleting endpoint to policy agent{%+v}. Err: %v", endpoint, err)
return err
}
// Remove IPv6 endpoint from policy tables
if endpoint.Ipv6Addr != nil && endpoint.Ipv6Addr.String() != "" {
err = self.RemoveRemoteIpv6Flow(endpoint)
if err != nil {
log.Errorf("Error deleting IPv6 flow for endpoint {%+v}. Err: %v", endpoint, err)
return err
}
}
return nil
}
// Add IPv6 flow for remote endpoint
func (self *Vrouter) AddRemoteIpv6Flow(endpoint *OfnetEndpoint) error {
// Lookup the VTEP for the endpoint
vtepPort := self.agent.getvtepTablePort(endpoint.OriginatorIp.String())
if vtepPort == nil {
log.Warnf("Could not find the VTEP for endpoint: %+v", endpoint)
// Return if VTEP is not found. We'll install the route when VTEP is added
return nil
}
// Install the endpoint in OVS
// Create an output port for the vtep
outPort, err := self.ofSwitch.OutputPort(*vtepPort)
if err != nil {
log.Errorf("Error creating output port %d. Err: %v", *vtepPort, err)
return err
}
var vrfid *uint16
if vrfid = self.agent.getvrfId(endpoint.Vrf); vrfid == nil {
log.Errorf("Invalid vrf name:%v", endpoint.Vrf)
return errors.New("Invalid vrf name")
}
//set vrf id as METADATA
metadata, metadataMask := Vrfmetadata(*vrfid)
// Install the IP address
ipv6Flow, err := self.ipTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
Ethertype: 0x86DD,
Ipv6Da: &endpoint.Ipv6Addr,
Metadata: &metadata,