forked from contiv/ofnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vlanBridge.go
executable file
·1100 lines (928 loc) · 30 KB
/
vlanBridge.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 vlan bridging datapath
import (
"fmt"
"net"
"net/rpc"
"sync"
"syscall"
"time"
"github.com/contiv/ofnet/ofctrl"
"github.com/shaleman/libOpenflow/openflow13"
"github.com/shaleman/libOpenflow/protocol"
cmap "github.com/streamrail/concurrent-map"
"github.com/vishvananda/netlink"
log "github.com/Sirupsen/logrus"
)
// Vlan bridging currently uses native OVS bridging.
// This is mostly stub code.
const (
GARPRepeats = 15
GARPDELAY = 3
)
// VlanBridge has Vlan state.
type VlanBridge 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
nmlTable *ofctrl.Table // OVS normal lookup table
// Flow Database
portVlanFlowDb map[uint32]*ofctrl.Flow // Database of flow entries
portDnsFlowDb cmap.ConcurrentMap // Database of flow entries
dscpFlowDb map[uint32][]*ofctrl.Flow // Database of flow entries
// Arp Flow
arpRedirectFlow *ofctrl.Flow // ARP redirect flow entry
uplinkPortDb cmap.ConcurrentMap // Database of uplink ports
linkDb cmap.ConcurrentMap // Database of all links
garpMutex *sync.Mutex
epgToEPs map[int]epgGARPInfo // Database of eps per epg
garpBGActive bool
updChan chan netlink.LinkUpdate // channel to monitor for link events
nlChan chan struct{} // channel to close the netlink listener
}
// epgGARPInfo holds info for epg
type epgGARPInfo struct {
garpCount int // number of garps yet to be sent on this epg
epMap map[uint32]GARPInfo // map of eps on this epg
}
// GARPInfo for each EP
type GARPInfo struct {
ip net.IP
mac net.HardwareAddr
vlan uint16
}
// GetUplink API gets the uplink port with uplinkID from uplink DB
func (vl *VlanBridge) GetUplink(uplinkID string) *PortInfo {
uplink, ok := vl.uplinkPortDb.Get(uplinkID)
if !ok {
return nil
}
return uplink.(*PortInfo)
}
// GetLink API gets the interface with linkID from interface DB
func (vl *VlanBridge) GetLink(linkID string) *LinkInfo {
link, ok := vl.linkDb.Get(linkID)
if !ok {
return nil
}
return link.(*LinkInfo)
}
// NewVlanBridge Create a new vlan instance
func NewVlanBridge(agent *OfnetAgent, rpcServ *rpc.Server) *VlanBridge {
vlan := new(VlanBridge)
// Keep a reference to the agent
vlan.agent = agent
// init maps
vlan.portVlanFlowDb = make(map[uint32]*ofctrl.Flow)
vlan.portDnsFlowDb = cmap.New()
vlan.dscpFlowDb = make(map[uint32][]*ofctrl.Flow)
vlan.uplinkPortDb = cmap.New()
vlan.linkDb = cmap.New()
vlan.epgToEPs = make(map[int]epgGARPInfo)
vlan.garpMutex = &sync.Mutex{}
vlan.garpBGActive = false
vlan.svcProxy = NewServiceProxy(agent)
// Create policy agent
vlan.policyAgent = NewPolicyAgent(agent, rpcServ)
return vlan
}
// MasterAdded Handle new master added event
func (vl *VlanBridge) MasterAdded(master *OfnetNode) error {
return nil
}
// SwitchConnected Handle switch connected notification
func (vl *VlanBridge) SwitchConnected(sw *ofctrl.OFSwitch) {
// Keep a reference to the switch
vl.ofSwitch = sw
vl.svcProxy.SwitchConnected(sw)
// Tell the policy agent about the switch
vl.policyAgent.SwitchConnected(sw)
// Init the Fgraph
vl.initFgraph()
log.Infof("Switch connected(vlan)")
}
// SwitchDisconnected Handle switch disconnected notification
func (vl *VlanBridge) SwitchDisconnected(sw *ofctrl.OFSwitch) {
vl.svcProxy.SwitchDisconnected(sw)
// Tell the policy agent about the switch disconnected
vl.policyAgent.SwitchDisconnected(sw)
vl.ofSwitch = nil
}
// PacketRcvd Handle incoming packet
func (vl *VlanBridge) 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
}
}
}
}
func (vl *VlanBridge) backGroundGARPs() {
for {
vl.garpMutex.Lock()
workDone := false
for epgID, epgInfo := range vl.epgToEPs {
if epgInfo.garpCount <= 0 {
continue
}
epgInfo.garpCount--
for _, ep := range epgInfo.epMap {
err := vl.sendGARP(ep.ip, ep.mac, ep.vlan)
if err == nil {
vl.agent.incrStats("GARPSent")
} else {
log.Warnf("Send GARP failed for ep IP: %v", ep.ip)
}
workDone = true
}
vl.epgToEPs[epgID] = epgInfo
}
if !workDone { // No epgs pending. Time to exit
vl.garpBGActive = false
vl.garpMutex.Unlock()
return
}
vl.garpMutex.Unlock()
time.Sleep(GARPDELAY * time.Second)
}
}
// InjectGARPs for all endpoints on the epg
func (vl *VlanBridge) InjectGARPs(epgID int) {
vl.garpMutex.Lock()
defer vl.garpMutex.Unlock()
epgInfo, found := vl.epgToEPs[epgID]
if found { // only if this epg has endpoints here
epgInfo.garpCount = GARPRepeats
vl.epgToEPs[epgID] = epgInfo
if !vl.garpBGActive {
go vl.backGroundGARPs()
}
}
}
func (vl *VlanBridge) sendGARPAll() {
vl.garpMutex.Lock()
defer vl.garpMutex.Unlock()
count := 0
for epgID, epgInfo := range vl.epgToEPs {
epgInfo.garpCount = GARPRepeats
vl.epgToEPs[epgID] = epgInfo
count++
}
if !vl.garpBGActive && count > 0 {
log.Infof("GARPs will be sent for %d epgs", count)
go vl.backGroundGARPs()
}
}
// Update global config
func (vl *VlanBridge) GlobalConfigUpdate(cfg OfnetGlobalConfig) error {
if vl.agent.arpMode == cfg.ArpMode {
log.Warnf("no change in ARP mode %s", vl.agent.arpMode)
} else {
vl.agent.arpMode = cfg.ArpMode
vl.updateArpRedirectFlow(cfg.ArpMode, cfg.ArpMode == ArpFlood)
}
return nil
}
// AddLocalEndpoint Add a local endpoint and install associated local route
func (vl *VlanBridge) AddLocalEndpoint(endpoint OfnetEndpoint) error {
log.Infof("Adding local endpoint: %+v", endpoint)
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}
}
// 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
}
// Send GARP
mac, _ := net.ParseMAC(endpoint.MacAddrStr)
err = vl.sendGARP(endpoint.IpAddr, mac, endpoint.Vlan)
if err != nil {
log.Warnf("Error in sending GARP packet for (%s,%s) in vlan %d. Err: %+v",
endpoint.IpAddr.String(), endpoint.MacAddrStr, endpoint.Vlan, err)
}
// update epgDB
if endpoint.EndpointGroupVlan != 0 {
vl.garpMutex.Lock()
gInfo := GARPInfo{mac: mac,
ip: endpoint.IpAddr,
vlan: endpoint.EndpointGroupVlan}
epgInfo, found := vl.epgToEPs[endpoint.EndpointGroup]
if !found {
epMap := make(map[uint32]GARPInfo)
epgInfo = epgGARPInfo{garpCount: 0,
epMap: epMap,
}
}
epgInfo.epMap[endpoint.PortNo] = gInfo
vl.epgToEPs[endpoint.EndpointGroup] = epgInfo
vl.garpMutex.Unlock()
vl.InjectGARPs(endpoint.EndpointGroup) // inject background arps as well
}
return nil
}
// RemoveLocalEndpoint Remove local endpoint
func (vl *VlanBridge) RemoveLocalEndpoint(endpoint OfnetEndpoint) error {
// 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)
}
}
}
// Remove from epg DB
vl.garpMutex.Lock()
defer vl.garpMutex.Unlock()
epgInfo, found := vl.epgToEPs[endpoint.EndpointGroup]
if found {
delete(epgInfo.epMap, endpoint.PortNo)
}
vl.svcProxy.DelEndpoint(&endpoint)
// Remove the endpoint from policy tables
err := vl.policyAgent.DelEndpoint(&endpoint)
if err != nil {
log.Errorf("Error deleting endpoint to policy agent{%+v}. Err: %v", endpoint, err)
return err
}
return nil
}
// UpdateLocalEndpoint update local endpoint state
func (vl *VlanBridge) 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
}
// AddVtepPort Add virtual tunnel end point.
func (vl *VlanBridge) AddVtepPort(portNo uint32, remoteIP net.IP) error {
return nil
}
// RemoveVtepPort Remove a VTEP port
func (vl *VlanBridge) RemoveVtepPort(portNo uint32, remoteIP net.IP) error {
return nil
}
// AddVlan Add a vlan.
func (vl *VlanBridge) AddVlan(vlanID uint16, vni uint32, vrf string) error {
vl.agent.vlanVrfMutex.Lock()
vl.agent.vlanVrf[vlanID] = &vrf
vl.agent.vlanVrfMutex.Unlock()
vl.agent.createVrf(vrf)
return nil
}
// RemoveVlan Remove a vlan
func (vl *VlanBridge) RemoveVlan(vlanID uint16, vni uint32, vrf string) error {
vl.agent.vlanVrfMutex.Lock()
delete(vl.agent.vlanVrf, vlanID)
vl.agent.vlanVrfMutex.Unlock()
vl.agent.deleteVrf(vrf)
return nil
}
// AddEndpoint Add an endpoint to the datapath
func (vl *VlanBridge) AddEndpoint(endpoint *OfnetEndpoint) error {
if endpoint.Vni != 0 {
return nil
}
log.Infof("Received endpoint: %+v", 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
}
return nil
}
// RemoveEndpoint removes an endpoint from the datapath
func (vl *VlanBridge) RemoveEndpoint(endpoint *OfnetEndpoint) error {
log.Infof("Received DELETE endpoint: %+v", endpoint)
if endpoint.Vni != 0 {
return nil
}
// Remove the endpoint from policy tables
err := vl.policyAgent.DelEndpoint(endpoint)
if err != nil {
log.Errorf("Error deleting endpoint to policy agent{%+v}. Err: %v", endpoint, err)
return err
}
return nil
}
// handleLinkUpDown triggers GARPs on all eps when a link flap is detected.
func (vl *VlanBridge) handleLinkUpDown() {
for {
select {
case update := <-vl.updChan:
if vl.linkDb.Count() == 0 {
// We do not have any links that we are interested in
// Skip further processing
break
}
link := vl.GetLink(update.Link.Attrs().Name)
if link == nil {
// We are not interested in this interface
// Skip further processing
break
}
if update.IfInfomsg.Flags&syscall.IFF_UP != 0 {
log.Infof("Link up received for %s", link.Name)
vl.agent.incrStats("LinkupRcvd")
port := link.Port
prevPortStatus := port.LinkStatus
link.setLinkStatus(linkUp)
// If the uplink's link status has changed, send GARPs
if prevPortStatus != port.LinkStatus {
vl.sendGARPAll()
}
} else if update.IfInfomsg.Flags&^syscall.IFF_UP != 0 {
log.Infof("Link down received for %s", link.Name)
vl.agent.incrStats("LinkDownRcvd")
link.setLinkStatus(linkDown)
}
case <-vl.nlChan:
log.Debugf("Stop listening for netlink events")
return
}
}
}
// startMonitoringLinks starts monitoring for Link Updates
func (vl *VlanBridge) startMonitoringLinks() {
vl.updChan = make(chan netlink.LinkUpdate)
vl.nlChan = make(chan struct{})
if err := netlink.LinkSubscribe(vl.updChan, vl.nlChan); err != nil {
log.Errorf("Error listening on netlink: %v", err)
return
}
// Handle port up/down events
go vl.handleLinkUpDown()
}
// AddUplink adds an uplink to the switch
func (vl *VlanBridge) AddUplink(uplinkPort *PortInfo) error {
log.Infof("Adding uplink port: %+v", uplinkPort)
for _, link := range uplinkPort.MbrLinks {
dnsUplinkFlow, err := vl.inputTable.NewFlow(ofctrl.FlowMatch{
Priority: DNS_FLOW_MATCH_PRIORITY + 2,
InputPort: link.OfPort,
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
}
dnsUplinkFlow.Next(vl.vlanTable)
// Install a flow entry for vlan mapping and point it to Mac table
portVlanFlow, err := vl.vlanTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
InputPort: link.OfPort,
})
if err != nil {
log.Errorf("Error creating portvlan entry. Err: %v", err)
return err
}
// Packets coming from uplink go thru normal lookup(bypass policy)
sNATTbl := vl.ofSwitch.GetTable(SRV_PROXY_SNAT_TBL_ID)
err = portVlanFlow.Next(sNATTbl)
if err != nil {
log.Errorf("Error installing portvlan entry. Err: %v", err)
return err
}
// save the flow entry
vl.portVlanFlowDb[link.OfPort] = portVlanFlow
vl.portDnsFlowDb.Set(fmt.Sprintf("%d", link.OfPort), dnsUplinkFlow)
}
err := uplinkPort.checkLinkStatus()
if err != nil {
log.Errorf("Error checking link status. Err: %+v", err)
return err
}
if vl.uplinkPortDb.Count() == 0 {
vl.startMonitoringLinks()
}
vl.uplinkPortDb.Set(uplinkPort.Name, uplinkPort)
for _, link := range uplinkPort.MbrLinks {
vl.linkDb.Set(link.Name, link)
}
return nil
}
// UpdateUplink updates uplink info
func (vl *VlanBridge) UpdateUplink(uplinkName string, updates PortUpdates) error {
for uplinkObj := range vl.uplinkPortDb.IterBuffered() {
uplink := uplinkObj.Val.(*PortInfo)
if uplink.Name != uplinkName {
continue
}
for _, update := range updates.Updates {
switch update.UpdateType {
case LacpUpdate:
lacpUpd := update.UpdateInfo.(LinkUpdateInfo)
linkName := lacpUpd.LinkName
for _, link := range uplink.MbrLinks {
if link.Name == linkName {
link.handleLacpUpdate(lacpUpd.LacpStatus)
}
}
default:
log.Errorf("Unknown update: (%s, %+v)", update.UpdateType, update.UpdateInfo)
}
}
}
return nil
}
// RemoveUplink remove an uplink to the switch
func (vl *VlanBridge) RemoveUplink(uplinkName string) error {
uplinkPort := vl.GetUplink(uplinkName)
if uplinkPort == nil {
err := fmt.Errorf("Could not get uplink with name: %s", uplinkName)
return err
}
// Stop monitoring links in the port
for _, link := range uplinkPort.MbrLinks {
// Uninstall the flow entry
portVlanFlow := vl.portVlanFlowDb[link.OfPort]
if portVlanFlow != nil {
portVlanFlow.Delete()
delete(vl.portVlanFlowDb, link.OfPort)
}
// Remove from linkDb
vl.linkDb.Remove(link.Name)
if f, ok := vl.portDnsFlowDb.Get(fmt.Sprintf("%d", link.OfPort)); ok {
if dnsUplinkFlow, ok := f.(*ofctrl.Flow); ok {
if err := dnsUplinkFlow.Delete(); err != nil {
log.Errorf("Error deleting nameserver flow. Err: %v", err)
}
}
}
vl.portDnsFlowDb.Remove(fmt.Sprintf("%d", link.OfPort))
}
vl.uplinkPortDb.Remove(uplinkName)
// Stop receving link updates when there are no more ports to monitor
if vl.uplinkPortDb.Count() == 0 {
close(vl.nlChan)
}
return nil
}
// AddHostPort is not implemented
func (vl *VlanBridge) AddHostPort(hp HostPortInfo) error {
return nil
}
// RemoveHostPort is not implemented
func (vl *VlanBridge) RemoveHostPort(hp uint32) error {
return nil
}
// AddSvcSpec adds a service spec to proxy
func (vl *VlanBridge) AddSvcSpec(svcName string, spec *ServiceSpec) error {
return vl.svcProxy.AddSvcSpec(svcName, spec)
}
// DelSvcSpec removes a service spec from proxy
func (vl *VlanBridge) DelSvcSpec(svcName string, spec *ServiceSpec) error {
return vl.svcProxy.DelSvcSpec(svcName, spec)
}
// SvcProviderUpdate Service Proxy Back End update
func (vl *VlanBridge) SvcProviderUpdate(svcName string, providers []string) {
vl.svcProxy.ProviderUpdate(svcName, providers)
}
// GetEndpointStats fetches ep stats
func (vl *VlanBridge) GetEndpointStats() (map[string]*OfnetEndpointStats, error) {
return vl.svcProxy.GetEndpointStats()
}
// MultipartReply handles stats reply
func (vl *VlanBridge) MultipartReply(sw *ofctrl.OFSwitch, reply *openflow13.MultipartReply) {
if reply.Type == openflow13.MultipartType_Flow {
vl.svcProxy.FlowStats(reply)
}
}
// InspectState returns current state
func (vl *VlanBridge) InspectState() (interface{}, error) {
vlExport := struct {
PolicyAgent *PolicyAgent // Policy agent
SvcProxy interface{} // Service proxy
// VlanDb map[uint16]*Vlan // Database of known vlans
}{
vl.policyAgent,
vl.svcProxy.InspectState(),
// vr.vlanDb,
}
return vlExport, nil
}
// initialize Fgraph on the switch
func (vl *VlanBridge) initFgraph() error {
sw := vl.ofSwitch
log.Infof("Installing initial flow entries")
// Create all tables
vl.inputTable = sw.DefaultTable()
vl.vlanTable, _ = sw.NewTable(VLAN_TBL_ID)
vl.nmlTable, _ = sw.NewTable(MAC_DEST_TBL_ID)
// setup SNAT table
// Matches in SNAT table (i.e. incoming) go to mac dest
vl.svcProxy.InitSNATTable(MAC_DEST_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)
// Send all packets to vlan lookup
validPktFlow, _ := vl.inputTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MISS_PRIORITY,
})
validPktFlow.Next(vl.vlanTable)
// If we miss Vlan lookup, continue to next lookup
vlanMissFlow, _ := vl.vlanTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MISS_PRIORITY,
})
dstGrpTbl := vl.ofSwitch.GetTable(DST_GRP_TBL_ID)
vlanMissFlow.Next(dstGrpTbl)
// if arp-mode is ArpProxy, redirect ARP packets to controller
// In ArpFlood mode, ARP packets are flooded in datapath and
// there is no proxy-arp functionality
if vl.agent.arpMode == ArpProxy {
vl.updateArpRedirectFlow(vl.agent.arpMode, false)
}
// 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)
// All packets that have gone thru policy lookup go thru normal OVS switching
normalLookupFlow, _ := vl.nmlTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MISS_PRIORITY,
})
normalLookupFlow.Next(sw.NormalLookup())
// Drop all
return nil
}
func getProxyARPResp(arpIn *protocol.ARP, tgtMac string, vid uint16, inPort uint32) *openflow13.PacketOut {
arpPkt, _ := protocol.NewARP(protocol.Type_Reply)
arpPkt.HWSrc, _ = net.ParseMAC(tgtMac)
arpPkt.IPSrc = arpIn.IPDst
arpPkt.HWDst = arpIn.HWSrc
arpPkt.IPDst = arpIn.IPSrc
log.Debugf("Sending Proxy ARP response: %+v", arpPkt)
// Build the ethernet packet
ethPkt := protocol.NewEthernet()
ethPkt.VLANID.VID = vid
ethPkt.HWDst = arpPkt.HWDst
ethPkt.HWSrc = arpPkt.HWSrc
ethPkt.Ethertype = 0x0806
ethPkt.Data = arpPkt
log.Debugf("Sending Proxy ARP response Ethernet: %+v", ethPkt)
// Construct Packet out
pktOut := openflow13.NewPacketOut()
pktOut.Data = ethPkt
pktOut.AddAction(openflow13.NewActionOutput(inPort))
return pktOut
}
// add a flow to redirect ARP packet to controller for arp-proxy
func (vl *VlanBridge) updateArpRedirectFlow(newArpMode ArpModeT, sendGARPs bool) {
sw := vl.ofSwitch
add := (newArpMode == ArpProxy)
if add {
// Redirect ARP Request packets to controller
arpFlow, _ := vl.inputTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
Ethertype: 0x0806,
ArpOper: protocol.Type_Request,
})
arpFlow.Next(sw.SendToController())
vl.arpRedirectFlow = arpFlow
} else {
if vl.arpRedirectFlow != nil {
vl.arpRedirectFlow.Delete()
}
}
if sendGARPs {
// When arp mode changes to ArpFlood, send GARP for all endpoints
// so that external network can learn endpoints from ARP packets
vl.sendGARPAll()
}
}
/*
* Process incoming ARP packets
* ARP request handling in various scenarios:
* Src and Dest EP known:
* - Proxy ARP if Dest EP is present locally on the host
* Src EP known, Dest EP not known:
* - ARP Request to a router/VM scenario. Reinject ARP request to uplinks
* Src EP not known, Dest EP known:
* - Proxy ARP if Dest EP is present locally on the host
* Src and Dest EP not known:
* - Ignore processing the request
*/
func (vl *VlanBridge) processArp(pkt protocol.Ethernet, inPort uint32) {
switch t := pkt.Data.(type) {
case *protocol.ARP:
log.Debugf("Processing ARP packet on port %d: %+v", inPort, *t)
var arpIn protocol.ARP = *t
vl.agent.incrStats("ArpPktRcvd")
switch arpIn.Operation {
case protocol.Type_Request:
// If it's a GARP packet, ignore processing
if arpIn.IPSrc.String() == arpIn.IPDst.String() {
log.Debugf("Ignoring GARP packet")
return
}
vl.agent.incrStats("ArpReqRcvd")
// Lookup the Source and Dest IP in the endpoint table
//Vrf derivation logic :
var vlan uint16
fromUplink := false
for uplinkObj := range vl.uplinkPortDb.IterBuffered() {
uplink := uplinkObj.Val.(*PortInfo)
for _, link := range uplink.MbrLinks {
if link.OfPort == inPort {
fromUplink = true
break
}
}
if fromUplink {
break
}
}
if fromUplink {
//arp packet came in from uplink hence tagged
vlan = pkt.VLANID.VID
} else {
//arp packet came from local endpoints - derive vrf from inport
if pVl := vl.agent.getPortVlanMap(inPort); pVl != nil {
vlan = *(pVl)
} else {
log.Debugf("Invalid port vlan mapping. Ignoring arp packet")
vl.agent.incrStats("ArpReqInvalidPortVlan")
return
}
}
srcEp := vl.agent.getEndpointByIpVlan(arpIn.IPSrc, vlan)
dstEp := vl.agent.getEndpointByIpVlan(arpIn.IPDst, vlan)
// No information about the src or dest EP. Drop the pkt.
if srcEp == nil && dstEp == nil {
log.Debugf("No information on source/destination. Ignoring ARP request.")
vl.agent.incrStats("ArpRequestUnknownSrcDst")
return
}
// if it came from uplink and the destination is not local, drop it
if fromUplink {
if dstEp == nil {
vl.agent.incrStats("ArpReqUnknownDestFromUplink")
return
}
if dstEp.OriginatorIp.String() != vl.agent.localIp.String() {
vl.agent.incrStats("ArpReqNonLocalDestFromUplink")
return
}
}
// If we know the dstEp to be present locally, send the Proxy ARP response
if dstEp != nil {
// Container to Container communication. Send proxy ARP response.
// Unknown node to Container communication
// -> Send proxy ARP response only if Endpoint is local.
// -> This is to avoid sending ARP responses from ofnet agent on multiple hosts
if srcEp != nil ||
(srcEp == nil && dstEp.OriginatorIp.String() == vl.agent.localIp.String()) {
// Send the packet out
pktOut := getProxyARPResp(&arpIn, dstEp.MacAddrStr,
pkt.VLANID.VID, inPort)
vl.ofSwitch.Send(pktOut)
vl.agent.incrStats("ArpReqRespSent")
return
}
}
proxyMac := vl.svcProxy.GetSvcProxyMAC(arpIn.IPDst)
if proxyMac != "" {
pktOut := getProxyARPResp(&arpIn, proxyMac,
pkt.VLANID.VID, inPort)
vl.ofSwitch.Send(pktOut)
vl.agent.incrStats("ArpReqRespSent")
return
}
if srcEp != nil && dstEp == nil {
// ARP request from local container to unknown IP