forked from contiv/ofnet
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ofnetSvcProxy.go
950 lines (808 loc) · 24.7 KB
/
ofnetSvcProxy.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
/***
Copyright 2016 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
import (
"errors"
"fmt"
"net"
"strconv"
"sync"
"time"
"github.com/contiv/libOpenflow/openflow13"
"github.com/contiv/libOpenflow/protocol"
"github.com/contiv/ofnet/pqueue"
log "github.com/Sirupsen/logrus"
"github.com/contiv/ofnet/ofctrl"
)
// service proxy implementation
const (
watchedFlowMax = 2
spDNAT = "Dst"
spSNAT = "Src"
)
// PortSpec defines protocol/port info required to host the service
type PortSpec struct {
Protocol string
SvcPort uint16 // advertised port
ProvPort uint16 // actual port of provider
}
// ServiceSpec defines a service to be proxied
type ServiceSpec struct {
IpAddress string
Ports []PortSpec
}
// Providers holds the current providers of a given service
type Providers struct {
Providers map[string]bool // Provider IP as key
}
// svcCatalogue holds information about all services to be proxied
// Accessible by north-bound API
type svcCatalogue struct {
SvcMap map[string]ServiceSpec // service name as key
ProvMap map[string]Providers // service name as key
}
// provOper holds operational info for each provider
type provOper struct {
ClientEPs map[string]bool // IP's of endpoints served by the provider
pqHdl *pqueue.Item // handle into the providers pq
}
// proxyOper is operational state of the proxy
type proxyOper struct {
Ports []PortSpec
ProvHdl map[string]provOper // provider IP as key
provPQ *pqueue.MinPQueue // provider priority queue for load balancing
watchedFlows []*ofctrl.Flow // flows this service is watching
natFlows map[string]*ofctrl.Flow // epIP.[in|out] as key
}
// flow info for service
type flowHdl struct {
SvcIP string
flow *ofctrl.Flow
}
// ServiceProxy is an instance of a service proxy
type ServiceProxy struct {
ofSwitch *ofctrl.OFSwitch // openflow switch we are talking to
agent *OfnetAgent // Pointer back to ofnet agent that owns this
dNATTable *ofctrl.Table // proxy dNAT rules table
sNATTable *ofctrl.Table // proxy sNAT rules table
dNATNext *ofctrl.Table // Next table to goto for dNAT'ed packets
sNATNext *ofctrl.Table // Next table to goto for sNAT'ed packets
catalogue svcCatalogue // Services and providers added to the proxy
oMutex sync.Mutex // mutex between management and datapath
operState map[string]*proxyOper // Operational state info, with service IP as key
epStats map[string]*OfnetEndpointStats // stats for the service
flowMap map[uint64]flowHdl // flowId to Info map
statsPollStarted bool // has the stats polling been started?
}
func getIPProto(prot string) uint8 {
switch prot {
case "TCP":
return ofctrl.IP_PROTO_TCP
case "UDP":
return ofctrl.IP_PROTO_UDP
default:
return 0
}
}
func matchSpec(s1, s2 *ServiceSpec) bool {
if s1.IpAddress != s2.IpAddress {
return false
}
if len(s1.Ports) != len(s2.Ports) {
return false
}
// if the order or ports changes, this will treat as a mismatch
// but, not a big deal...
for ix := 0; ix < len(s1.Ports); ix++ {
if s1.Ports[ix].Protocol != s2.Ports[ix].Protocol {
return false
}
if s1.Ports[ix].SvcPort != s2.Ports[ix].SvcPort {
return false
}
if s1.Ports[ix].ProvPort != s2.Ports[ix].ProvPort {
return false
}
}
return true
}
// allocateProvider gets the provider with least load
// also updates the provider to client linkage
func (svcOp *proxyOper) allocateProvider(clientIP string) (net.IP, error) {
if svcOp.provPQ.Len() <= 0 {
return net.ParseIP("0.0.0.0"), errors.New("No provider")
}
prov := svcOp.provPQ.GetMin()
svcOp.provPQ.IncreaseMin()
svcOp.ProvHdl[prov].ClientEPs[clientIP] = true
return net.ParseIP(prov), nil
}
func getNATKey(epIP, natT string, p *PortSpec) string {
key := epIP + "." + natT + "." + p.Protocol + strconv.Itoa(int(p.SvcPort))
return key
}
// addNATFlow sets up a NAT flow
// natT must be "Src" or "Dst"
func (svcOp *proxyOper) addNATFlow(this, next *ofctrl.Table, p *PortSpec,
ipSa, ipDa, ipNew *net.IP, natT, macDA string) (*ofctrl.Flow, error) {
// Check if we already installed this flow
key := ""
if natT == "Dst" {
key = getNATKey(ipSa.String(), natT, p)
} else {
key = getNATKey(ipDa.String(), natT, p)
}
f, found := svcOp.natFlows[key]
if found && f != nil {
log.Infof("Flow already exists for %v", key)
return f, nil
}
match := ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
Ethertype: 0x0800,
IpSa: ipSa,
IpDa: ipDa,
IpProto: getIPProto(p.Protocol),
}
if p.Protocol == "TCP" {
if natT == spDNAT {
match.TcpDstPort = p.SvcPort
} else {
match.TcpSrcPort = p.ProvPort
}
} else {
if natT == spDNAT {
match.UdpDstPort = p.SvcPort
} else {
match.UdpSrcPort = p.ProvPort
}
}
natFlow, err := this.NewFlow(match)
if err != nil {
log.Errorf("Proxy addNATFlow failed")
return nil, errors.New("Proxy addNATFlow failed")
}
l4field := p.Protocol + natT // evaluates to TCP[Src,Dst] or UDP[Src,Dst]
// add actions to update ipda and dest port
natFlow.SetIPField(*ipNew, natT)
if natT == spDNAT {
natFlow.SetL4Field(p.ProvPort, l4field)
} else {
natFlow.SetL4Field(p.SvcPort, l4field)
}
if macDA != "" {
// Update dmac as well
dmac, err := net.ParseMAC(macDA)
if err != nil {
log.Errorf("Error parsing mac: %v", err)
return nil, errors.New("Proxy addNATFlow failed to parse MAC")
}
log.Infof("Rewrite mac to %s for provider", macDA)
natFlow.SetMacDa(dmac)
}
natFlow.Next(next)
svcOp.natFlows[key] = natFlow
log.Infof("Added NAT %s to %s", key, ipNew.String())
return natFlow, nil
}
func (svcOp *proxyOper) delNATFlow(proxy *ServiceProxy, epIP, natT string, p *PortSpec) {
key := getNATKey(epIP, natT, p)
flow, found := svcOp.natFlows[key]
if found {
log.Infof("Deleting NAT %s", key)
delete(proxy.flowMap, flow.FlowID)
flow.Delete()
delete(svcOp.natFlows, key)
} else {
log.Infof("DEL NAT %s flow not found", key)
}
}
func (svcOp *proxyOper) addProvHdl(provIP string) {
clientMap := make(map[string]bool)
item := pqueue.NewItem(provIP)
pOper := provOper{
ClientEPs: clientMap,
pqHdl: item,
}
svcOp.ProvHdl[provIP] = pOper
svcOp.provPQ.PushItem(item)
}
func (proxy *ServiceProxy) addService(svcName string) error {
// make sure we have a spec and at least one provider
services := proxy.catalogue.SvcMap
spec, found := services[svcName]
if !found {
log.Debugf("No spec for %s", svcName)
return nil
}
providers := proxy.catalogue.ProvMap
prov, found := providers[svcName]
if !found {
log.Debugf("No providers for %s", svcName)
return nil
}
// Build operational state
oper := proxy.operState
proxy.oMutex.Lock()
defer proxy.oMutex.Unlock()
_, found = oper[spec.IpAddress]
if found {
log.Errorf("Unexpected... operstate found for %s", spec.IpAddress)
return errors.New("Service IP already exists")
}
wFlows := make([]*ofctrl.Flow, watchedFlowMax)
pq := pqueue.NewMinPQueue()
pHdl := make(map[string]provOper)
nFlows := make(map[string]*ofctrl.Flow)
oState := &proxyOper{Ports: spec.Ports,
provPQ: pq,
watchedFlows: wFlows,
ProvHdl: pHdl,
natFlows: nFlows,
}
// add all providers
for p := range prov.Providers {
oState.addProvHdl(p)
}
// add the service state to oper map
oper[spec.IpAddress] = oState
// add ovs rule to catch service traffic
// TBD -- handle arps to cover service in same subnet case
protMap := make(map[string]uint8)
protMap["TCP"] = 6
protMap["UDP"] = 17
count := 0
svcDA := net.ParseIP(spec.IpAddress)
for _, port := range spec.Ports {
prot, found := protMap[port.Protocol]
if found {
if count >= watchedFlowMax {
log.Errorf("Flow count exceeded")
break
}
watchedFlow, err := proxy.dNATTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_FLOOD_PRIORITY,
Ethertype: 0x0800,
IpDa: &svcDA,
IpProto: prot,
})
if err != nil {
log.Errorf("Watch %s proto: %d err: %v", spec.IpAddress,
prot, err)
continue
}
watchedFlow.Next(proxy.ofSwitch.SendToController())
oState.watchedFlows[count] = watchedFlow
delete(protMap, port.Protocol) // add only once
count++
}
}
return nil
}
// delService deletes a service
func (proxy *ServiceProxy) delService(svcName string) {
// make sure we have a spec
services := proxy.catalogue.SvcMap
spec, found := services[svcName]
if !found {
log.Debugf("delService %s not found", svcName)
return // service does not exist
}
// Clean up operational state
proxy.oMutex.Lock()
defer proxy.oMutex.Unlock()
oper := proxy.operState
operEntry, found := oper[spec.IpAddress]
if !found {
log.Debugf("delService operEntry not found for %s", svcName)
return
}
// delete the watched flows from OVS
for _, flow := range operEntry.watchedFlows {
if flow != nil {
flow.Delete()
}
}
// delete the nat'ed flows
for key, flow := range operEntry.natFlows {
if flow != nil {
flow.Delete()
log.Infof("NAT flow %s deleted", key)
} else {
log.Infof("NAT flow %s not found", key)
}
}
// remove the operEntry
delete(oper, spec.IpAddress)
}
// AddSvcSpec adds or updates a service spec.
func (proxy *ServiceProxy) AddSvcSpec(svcName string, spec *ServiceSpec) error {
log.Infof("AddSvcSpec %s %v", svcName, spec)
// Make sure spec is valid
for _, p := range spec.Ports {
if p.Protocol != "TCP" && p.Protocol != "UDP" {
return errors.New("Invalid protocol in spec")
}
}
services := proxy.catalogue.SvcMap
oldSpec, found := services[svcName]
if found {
if matchSpec(&oldSpec, spec) {
log.Debugf("No change in spec for %s", svcName)
return nil
}
// delete existing spec and add the new one
proxy.DelSvcSpec(svcName, &oldSpec)
}
services[svcName] = *spec
return proxy.addService(svcName)
}
// DelSvcSpec deletes a service spec.
func (proxy *ServiceProxy) DelSvcSpec(svcName string, spec *ServiceSpec) error {
log.Infof("DelSvcSpec %s %v", svcName, spec)
services := proxy.catalogue.SvcMap
_, found := services[svcName]
if !found {
log.Warnf("DelSvcSpec service %s not found", svcName)
return errors.New("Service not found")
} else {
proxy.delService(svcName)
delete(services, svcName)
}
return nil
}
// GetEndpointStats fetches ep stats
func (proxy *ServiceProxy) GetEndpointStats() (map[string]*OfnetEndpointStats, error) {
return proxy.epStats, nil
}
// addProvider adds the given provider to operational State
func (proxy *ServiceProxy) addProvider(svcIP, provIP string) error {
oper := proxy.operState
proxy.oMutex.Lock()
defer proxy.oMutex.Unlock()
operEntry, found := oper[svcIP]
if !found {
log.Errorf("addProvider operEntry not found for %s", svcIP)
return errors.New("operEntry not found")
}
operEntry.addProvHdl(provIP)
log.Infof("Added provider %s for serviceIP %s", provIP, svcIP)
return nil
}
// delProvider deletes the given provider from operational State
func (proxy *ServiceProxy) delProvider(svcIP, provIP string) error {
oper := proxy.operState
proxy.oMutex.Lock()
defer proxy.oMutex.Unlock()
operEntry, found := oper[svcIP]
if !found {
log.Errorf("delProvider operEntry not found for %s", svcIP)
return errors.New("operEntry not found")
}
// Remove flows NAT'ed to this provider
for epIP := range operEntry.ProvHdl[provIP].ClientEPs {
for _, p := range operEntry.Ports {
operEntry.delNATFlow(proxy, epIP, "Dst", &p)
operEntry.delNATFlow(proxy, epIP, "Src", &p)
}
}
// Remove provider from the loadbalancer pq
pqItem := operEntry.ProvHdl[provIP].pqHdl
operEntry.provPQ.RemoveItem(pqItem)
// remove the provider handle for this provider
delete(operEntry.ProvHdl, provIP)
log.Infof("Removed provider %s for serviceIP %s", provIP, svcIP)
return nil
}
// ProviderUpdate updates the list of providers of the service
func (proxy *ServiceProxy) ProviderUpdate(svcName string, providers []string) {
log.Infof("ProviderUpdate %s %v", svcName, providers)
newProvs := make(map[string]bool)
for _, p := range providers {
newProvs[p] = true
}
pMap := Providers{
Providers: newProvs,
}
// if we don't have the service spec yet, just save the provider
// map and return
sSpec, found := proxy.catalogue.SvcMap[svcName]
if !found {
proxy.catalogue.ProvMap[svcName] = pMap
log.Debugf("Service %s -- no spec yet", svcName)
return
}
// if the service is not created, just use the new map and
// add the service
svcIP := sSpec.IpAddress
_, found = proxy.operState[svcIP]
if !found && len(providers) == 0 {
log.Debugf("Service %s -- no providers", svcName)
return
}
if !found {
proxy.catalogue.ProvMap[svcName] = pMap
err := proxy.addService(svcName)
if err != nil {
log.Errorf("ProviderUpdate failed for %s", svcName)
}
return
}
currProvs := proxy.catalogue.ProvMap[svcName]
proxy.catalogue.ProvMap[svcName] = pMap
// if the new provider list is empty, delete the service
if len(providers) == 0 {
log.Infof("No providers for service %s, deleting", svcName)
proxy.delService(svcName)
return
}
// Add any new providers first
for _, p := range providers {
_, found = currProvs.Providers[p]
if !found {
proxy.addProvider(sSpec.IpAddress, p)
}
}
// Delete any providers that disappeared
for p := range currProvs.Providers {
_, found = newProvs[p]
if !found {
proxy.delProvider(sSpec.IpAddress, p)
}
}
}
// NewServiceProxy Creates a new service proxy
func NewServiceProxy(agent *OfnetAgent) *ServiceProxy {
svcProxy := new(ServiceProxy)
// initialize
svcProxy.agent = agent
svcProxy.catalogue.SvcMap = make(map[string]ServiceSpec)
svcProxy.catalogue.ProvMap = make(map[string]Providers)
svcProxy.operState = make(map[string]*proxyOper)
svcProxy.flowMap = make(map[uint64]flowHdl)
svcProxy.epStats = make(map[string]*OfnetEndpointStats)
svcProxy.statsPollStarted = false
return svcProxy
}
// Handle switch connected notification
func (proxy *ServiceProxy) SwitchConnected(sw *ofctrl.OFSwitch) {
// Keep a reference to the switch
proxy.ofSwitch = sw
log.Infof("Switch connected(svcProxy).")
}
// Handle switch disconnected notification
func (proxy *ServiceProxy) SwitchDisconnected(sw *ofctrl.OFSwitch) {
// FIXME: ??
}
// DelEndpoint handles an endpoint delete
func (proxy *ServiceProxy) DelEndpoint(endpoint *OfnetEndpoint) {
epIP := endpoint.IpAddr.String()
// delete all nat'ed flows and update loadbalancer
proxy.oMutex.Lock()
defer proxy.oMutex.Unlock()
for _, operEntry := range proxy.operState {
for _, p := range operEntry.Ports {
// this client exists iff DNAT flow exists
key := getNATKey(epIP, "Dst", &p)
flow, found := operEntry.natFlows[key]
if found {
provIP := flow.Match.IpDa.String()
// delete both flows and remove the client
operEntry.delNATFlow(proxy, epIP, "Dst", &p)
operEntry.delNATFlow(proxy, epIP, "Src", &p)
hdl, ok := operEntry.ProvHdl[provIP]
if ok {
delete(hdl.ClientEPs, epIP)
pqItem := hdl.pqHdl
operEntry.provPQ.DecreaseItem(pqItem)
}
}
}
}
}
func getInPort(pkt *ofctrl.PacketIn) uint32 {
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
return inPortFld.InPort
}
}
return openflow13.P_ANY
}
// getRewriteMAC returns the providers mac if the client and provider are
// in the same subnet
func (proxy *ServiceProxy) getRewriteMAC(inPort uint32, provIP net.IP) string {
clientEP := proxy.agent.getLocalEndpoint(inPort)
if clientEP != nil {
provEP := proxy.agent.getEndpointByIpVlan(provIP, clientEP.Vlan)
if provEP != nil {
if provEP.Vlan == clientEP.Vlan {
return provEP.MacAddrStr
}
}
log.Debugf("provEP not found for vlan %d", clientEP.Vlan)
} else {
log.Debugf("ClientEP not found for port %d", inPort)
}
log.Debugf("ProvMac not found for %s", provIP.String())
return ""
}
// HandlePkt processes a received pkt from a matching table entry
func (proxy *ServiceProxy) HandlePkt(pkt *ofctrl.PacketIn) {
if pkt.TableId != SRV_PROXY_DNAT_TBL_ID {
return // ignore other packets
}
if pkt.Data.Ethertype != protocol.IPv4_MSG {
return // ignore non-IP pkts
}
if pkt.Data.HWSrc.String() == "00:00:11:11:11:11" {
log.Warnf("Pkt received with our src mac. Loop???")
return // pkt we sent
}
ip := pkt.Data.Data.(*protocol.IPv4)
svcIP := ip.NWDst.String()
log.Infof("HandlePkt svcIP: %s", svcIP)
proxy.oMutex.Lock()
defer proxy.oMutex.Unlock()
operEntry, found := proxy.operState[svcIP]
if !found {
return // this means service was just deleted
}
clientIP := ip.NWSrc.String()
provIP, err := operEntry.allocateProvider(clientIP)
if err != nil {
log.Warnf("allocateProvider failed for %s - %v", svcIP, err)
return
}
inPort := getInPort(pkt)
provMac := proxy.getRewriteMAC(inPort, provIP)
// use copies of fields from the pkt
ipSrc := net.ParseIP(ip.NWSrc.String())
ipDst := net.ParseIP(ip.NWDst.String())
fInfo := flowHdl{SvcIP: svcIP}
// setup nat rules in both directions for all ports of the service
for _, p := range operEntry.Ports {
// set up outgoing NAT
f, err := operEntry.addNATFlow(proxy.dNATTable, proxy.dNATNext, &p, &ipSrc, &ipDst, &provIP, spDNAT, provMac)
if err == nil {
fInfo.flow = f
proxy.flowMap[f.FlowID] = fInfo
} else {
continue
}
// set up incoming NAT
f, err = operEntry.addNATFlow(proxy.sNATTable, proxy.sNATNext, &p, &provIP, &ipSrc, &ipDst, spSNAT, "")
if err == nil {
fInfo.flow = f
proxy.flowMap[f.FlowID] = fInfo
}
}
if pkt.Data.HWSrc.String() == "00:00:00:00:00:00" {
return // don't inject crafted pkt
}
// re-inject this pkt, change src mac to allow loop detection
pkt.Data.HWSrc, _ = net.ParseMAC("00:00:11:11:11:11")
// Packet out
pktOut := openflow13.NewPacketOut()
pktOut.InPort = inPort
pktOut.Data = &pkt.Data
pktOut.AddAction(openflow13.NewActionOutput(openflow13.P_TABLE))
// Send it out
proxy.ofSwitch.Send(pktOut)
// Start stats polling if it hasnt started already
if !proxy.statsPollStarted {
proxy.statsPollStarted = true
go proxy.pollStats()
}
}
func (proxy *ServiceProxy) updateSNATStats(fs *openflow13.FlowStats) {
// find svcIP
flowInfo, found := proxy.flowMap[fs.Cookie]
if !found {
return // Flow is probably deleted
}
svcIP := flowInfo.SvcIP
fm := flowInfo.flow.Match
provIP := fm.IpSa.String()
epIP := fm.IpDa.String()
entry, found := proxy.epStats[epIP]
if !found {
entry = &OfnetEndpointStats{}
entry.EndpointIP = epIP
entry.SvcStats = make(map[string]OfnetSvcStats)
proxy.epStats[epIP] = entry
}
_, found = entry.SvcStats[svcIP]
if !found {
entry.SvcStats[svcIP] = OfnetSvcStats{
ServiceIP: svcIP,
ProvStats: make(map[string]OfnetSvcProviderStats),
}
}
stats := entry.SvcStats[svcIP]
if fm.IpProto == ofctrl.IP_PROTO_TCP {
stats.Protocol = "TCP"
stats.ProvPort = strconv.Itoa(int(fm.TcpSrcPort))
} else {
stats.Protocol = "UDP"
stats.ProvPort = strconv.Itoa(int(fm.UdpSrcPort))
}
_, found = stats.ProvStats[provIP]
if !found {
stats.ProvStats[provIP] = OfnetSvcProviderStats{ProviderIP: provIP}
}
provStats := stats.ProvStats[provIP]
provStats.PacketsIn = fs.PacketCount
provStats.BytesIn = fs.ByteCount
stats.ProvStats[provIP] = provStats
entry.SvcStats[svcIP] = stats
log.Debugf("SNAT Stats: epIP: %s, svcIp: %s, entry: %+v", epIP, svcIP, entry)
}
func (proxy *ServiceProxy) updateDNATStats(fs *openflow13.FlowStats) {
flowInfo, found := proxy.flowMap[fs.Cookie]
if !found {
return // Flow is probably deleted
}
svcIP := flowInfo.SvcIP
fm := flowInfo.flow.Match
if fm.TcpSrcPort == 0 && fm.TcpDstPort == 0 &&
fm.UdpSrcPort == 0 && fm.UdpDstPort == 0 {
return // watch flow
}
provIP := fm.IpSa.String()
epIP := fm.IpSa.String()
entry, found := proxy.epStats[epIP]
if !found {
entry = &OfnetEndpointStats{}
entry.EndpointIP = epIP
entry.SvcStats = make(map[string]OfnetSvcStats)
proxy.epStats[epIP] = entry
}
_, found = entry.SvcStats[svcIP]
if !found {
entry.SvcStats[svcIP] = OfnetSvcStats{
ServiceIP: svcIP,
ProvStats: make(map[string]OfnetSvcProviderStats),
}
}
stats := entry.SvcStats[svcIP]
if fm.IpProto == ofctrl.IP_PROTO_TCP {
stats.Protocol = "TCP"
stats.SvcPort = strconv.Itoa(int(fm.TcpDstPort))
} else {
stats.Protocol = "UDP"
stats.SvcPort = strconv.Itoa(int(fm.UdpDstPort))
}
_, found = stats.ProvStats[provIP]
if !found {
stats.ProvStats[provIP] = OfnetSvcProviderStats{ProviderIP: provIP}
}
provStats := stats.ProvStats[provIP]
provStats.PacketsOut = fs.PacketCount
provStats.BytesOut = fs.ByteCount
stats.ProvStats[provIP] = provStats
entry.SvcStats[svcIP] = stats
log.Debugf("DNAT Stats: epIP: %s, svcIp: %s, entry: %+v", epIP, svcIP, entry)
}
// FlowStats handles a stats response from the switch
func (proxy *ServiceProxy) FlowStats(reply *openflow13.MultipartReply) {
if reply.Type != openflow13.MultipartType_Flow {
log.Warnf("Unexpected MP Reply type: %+v", reply)
return
}
flowArr := reply.Body
for _, entry := range flowArr {
flowStats := entry.(*openflow13.FlowStats)
log.Debugf("Got flow stats: %+v", flowStats)
if flowStats.TableId == SRV_PROXY_DNAT_TBL_ID {
proxy.updateDNATStats(flowStats)
}
if flowStats.TableId == SRV_PROXY_SNAT_TBL_ID {
proxy.updateSNATStats(flowStats)
}
}
}
func getMPReq() *openflow13.MultipartRequest {
mp := &openflow13.MultipartRequest{}
mp.Type = openflow13.MultipartType_Flow
mp.Header = openflow13.NewOfp13Header()
mp.Header.Type = openflow13.Type_MultiPartRequest
return mp
}
func (proxy *ServiceProxy) pollStats() {
time.Sleep(20 * time.Second)
for {
time.Sleep(1 * time.Second)
//inject a stats request
dnatReq := openflow13.NewFlowStatsRequest()
dnatReq.TableId = SRV_PROXY_DNAT_TBL_ID
mp1 := getMPReq()
mp1.Body = dnatReq
proxy.ofSwitch.Send(mp1)
log.Debugf("Sent DNAT stats req")
time.Sleep(1 * time.Second)
//inject a stats request
snatReq := openflow13.NewFlowStatsRequest()
snatReq.TableId = SRV_PROXY_SNAT_TBL_ID
mp2 := getMPReq()
mp2.Body = snatReq
proxy.ofSwitch.Send(mp2)
log.Debugf("Sent SNAT stats req")
}
}
// InitSNATTable initializes the sNAT table
func (proxy *ServiceProxy) InitSNATTable(nextIDsNAT uint8) error {
sw := proxy.ofSwitch
nextTbl := sw.GetTable(nextIDsNAT)
if nextTbl == nil {
log.Fatalf("Error getting table id: %d", nextIDsNAT)
}
proxy.sNATNext = nextTbl
// Create table
proxy.sNATTable, _ = sw.NewTable(SRV_PROXY_SNAT_TBL_ID)
// Packets that didnt match any rule go to next table
proxyMissFlow, _ := proxy.sNATTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MISS_PRIORITY,
})
proxyMissFlow.Next(proxy.sNATNext)
return nil
}
// InitDNATTable initializes the dNAT table
func (proxy *ServiceProxy) InitDNATTable(nextIDdNAT uint8) error {
sw := proxy.ofSwitch
nextTbl := sw.GetTable(nextIDdNAT)
if nextTbl == nil {
log.Fatalf("Error getting table id: %d", nextIDdNAT)
}
proxy.dNATNext = nextTbl
// Create dNAT table
proxy.dNATTable, _ = sw.NewTable(SRV_PROXY_DNAT_TBL_ID)
proxyMissFlow, _ := proxy.dNATTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MISS_PRIORITY,
})
proxyMissFlow.Next(proxy.dNATNext)
return nil
}
// InspectState returns current state of svc proxy
func (proxy *ServiceProxy) InspectState() interface{} {
proxyExport := struct {
Catalogue svcCatalogue // Services and providers added to the proxy
OperState map[string]*proxyOper // Operational state info, with service IP as key
EpStats map[string]*OfnetEndpointStats // stats for the service
// FlowMap map[uint64]flowHdl // flowId to Info map
}{
proxy.catalogue,
proxy.operState,
proxy.epStats,
// proxy.flowMap,
}
return &proxyExport
}
// GetSvcProxyMAC looks up the service IP and returns a mac if the IP exists.
// if the svc IP is not found, returns empty string
func (proxy *ServiceProxy) GetSvcProxyMAC(svcIP net.IP) string {
ip := svcIP.String()
proxy.oMutex.Lock()
defer proxy.oMutex.Unlock()
_, found := proxy.operState[ip]
if found {
ipv4 := svcIP.To4()
return fmt.Sprintf("02:02:%02x:%02x:%02x:%02x", ipv4[0], ipv4[1], ipv4[2], ipv4[3])
}
return ""
}