forked from contiv/ofnet
-
Notifications
You must be signed in to change notification settings - Fork 16
/
ofSwitch.go
executable file
·489 lines (422 loc) · 12.7 KB
/
ofSwitch.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
/***
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 ofctrl
import (
"context"
"encoding/binary"
"errors"
"fmt"
"net"
"sync"
"time"
"antrea.io/libOpenflow/common"
"antrea.io/libOpenflow/openflow13"
"antrea.io/libOpenflow/util"
log "github.com/sirupsen/logrus"
cmap "github.com/streamrail/concurrent-map"
)
const (
messageTimeout = 10 * time.Second
PC_NO_FLOOD = 1 << 4
)
var (
heartbeatInterval, _ = time.ParseDuration("3s")
)
type OFSwitch struct {
stream *util.MessageStream
dpid net.HardwareAddr
app AppInterface
// Following are fgraph state for the switch
tableDb map[uint8]*Table
dropAction *Output
sendToCtrler *Output
normalLookup *Output
ready bool
portMux sync.Mutex
statusMux sync.Mutex
outputPorts map[uint32]*Output
groupDb map[uint32]*Group
meterDb map[uint32]*Meter
connCh chan int // Channel to notify controller connection status is changed
mQueue chan *openflow13.MultipartRequest
monitorEnabled bool
lastUpdate time.Time // time at that receiving the last EchoReply
heartbeatCh chan struct{}
// map for receiving reply messages from OFSwitch. Key is Xid, and value is a chan created by request message sender.
txChans map[uint32]chan MessageResult
txLock sync.Mutex // lock for txChans
ctx context.Context // ctx is used in the lifecycle of a connection
cancel context.CancelFunc // cancel is used to cancel the proceeding OpenFlow message when OFSwitch is disconnected.
ctrlID uint16
tlvMgr *tlvMapMgr
}
var switchDb cmap.ConcurrentMap
var monitoredFlows cmap.ConcurrentMap
func init() {
switchDb = cmap.New()
monitoredFlows = cmap.New()
}
// Builds and populates a Switch struct then starts listening
// for OpenFlow messages on conn.
func NewSwitch(stream *util.MessageStream, dpid net.HardwareAddr, app AppInterface, connCh chan int, ctrlID uint16) *OFSwitch {
var s *OFSwitch
if getSwitch(dpid) == nil {
log.Infoln("Openflow Connection for new switch:", dpid)
s = new(OFSwitch)
s.app = app
s.stream = stream
s.dpid = dpid
s.connCh = connCh
s.txChans = make(map[uint32]chan MessageResult)
s.ctrlID = ctrlID
// Prepare a context for current connection.
s.ctx, s.cancel = context.WithCancel(context.Background())
// Initialize the fgraph elements
s.initFgraph()
// Save it
switchDb.Set(dpid.String(), s)
// Main receive loop for the switch
go s.receive()
} else {
log.Infoln("Openflow Connection for switch:", dpid)
s = getSwitch(dpid)
s.stream = stream
s.dpid = dpid
// Update context for the new connection.
s.ctx, s.cancel = context.WithCancel(context.Background())
}
s.tlvMgr = newTLVMapMgr()
// send Switch connected callback
s.switchConnected()
// Return the new switch
return s
}
// Returns a pointer to the Switch mapped to dpid.
func getSwitch(dpid net.HardwareAddr) *OFSwitch {
sw, _ := switchDb.Get(dpid.String())
if sw == nil {
return nil
}
return sw.(*OFSwitch)
}
// Returns the dpid of Switch s.
func (self *OFSwitch) DPID() net.HardwareAddr {
return self.dpid
}
// Sends an OpenFlow message to this Switch.
func (self *OFSwitch) Send(req util.Message) error {
select {
case <-time.After(messageTimeout):
return fmt.Errorf("message send timeout")
case self.stream.Outbound <- req:
return nil
case <-self.ctx.Done():
return fmt.Errorf("message is canceled because of disconnection from the Switch")
}
}
func (self *OFSwitch) Disconnect() {
self.stream.Shutdown <- true
self.switchDisconnected()
}
func (self *OFSwitch) changeStatus(status bool) {
self.statusMux.Lock()
defer self.statusMux.Unlock()
self.ready = status
}
func (self *OFSwitch) IsReady() bool {
self.statusMux.Lock()
defer self.statusMux.Unlock()
return self.ready
}
// Handle switch connected event
func (self *OFSwitch) switchConnected() {
self.changeStatus(true)
// Send new feature request
self.Send(openflow13.NewFeaturesRequest())
self.Send(openflow13.NewEchoRequest())
self.heartbeatCh = make(chan struct{})
go func() {
timer := time.NewTicker(heartbeatInterval)
defer timer.Stop()
for {
select {
case <-timer.C:
self.Send(openflow13.NewEchoRequest())
case <-self.heartbeatCh:
break
}
}
}()
self.requestTlvMap()
self.app.SwitchConnected(self)
}
// Handle switch disconnected event
func (self *OFSwitch) switchDisconnected() {
self.changeStatus(false)
self.cancel()
self.heartbeatCh <- struct{}{}
switchDb.Remove(self.DPID().String())
self.app.SwitchDisconnected(self)
if self.connCh != nil {
self.connCh <- ReConnection
}
}
// Receive loop for each Switch.
func (self *OFSwitch) receive() {
for {
select {
case msg := <-self.stream.Inbound:
// New message has been received from message
// stream.
self.handleMessages(self.dpid, msg)
case err := <-self.stream.Error:
log.Warnf("Received ERROR message from switch %v. Err: %v", self.dpid, err)
// send Switch disconnected callback
self.switchDisconnected()
return
}
}
}
// Handle openflow messages from the switch
func (self *OFSwitch) handleMessages(dpid net.HardwareAddr, msg util.Message) {
log.Debugf("Received message: %+v, on switch: %s", msg, dpid.String())
switch t := msg.(type) {
case *common.Header:
switch t.Header().Type {
case openflow13.Type_Hello:
// Send Hello response
h, err := common.NewHello(4)
if err != nil {
log.Errorf("Error creating hello message")
}
self.Send(h)
case openflow13.Type_EchoRequest:
// Send echo reply
res := openflow13.NewEchoReply()
self.Send(res)
case openflow13.Type_EchoReply:
self.lastUpdate = time.Now()
case openflow13.Type_FeaturesRequest:
case openflow13.Type_GetConfigRequest:
case openflow13.Type_BarrierRequest:
case openflow13.Type_BarrierReply:
}
case *openflow13.ErrorMsg:
errMsg := GetErrorMessage(t.Type, t.Code, 0)
msgType := GetErrorMessageType(t.Data)
log.Errorf("Received OpenFlow1.3 error: %s on message %s", errMsg, msgType)
result := MessageResult{
succeed: false,
errType: t.Type,
errCode: t.Code,
xID: t.Xid,
msgType: UnknownMessage,
}
self.publishMessage(t.Xid, result)
case *openflow13.VendorHeader:
log.Debugf("Received Experimenter message, VendorType: %d, ExperimenterType: %d, VendorData: %+v", t.Vendor, t.ExperimenterType, t.VendorData)
switch t.ExperimenterType {
case openflow13.Type_TlvTableReply:
reply := t.VendorData.(*openflow13.TLVTableReply)
status := TLVTableStatus(*reply)
self.tlvMgr.TLVMapReplyRcvd(self, &status)
case openflow13.Type_BundleCtrl:
result := MessageResult{
xID: t.Header.Xid,
succeed: true,
msgType: BundleControlMessage,
}
reply := t.VendorData.(*openflow13.BundleControl)
self.publishMessage(reply.BundleID, result)
}
case *openflow13.SwitchFeatures:
switch t.Header.Type {
case openflow13.Type_FeaturesReply:
go func() {
swConfig := openflow13.NewSetConfig()
swConfig.MissSendLen = 128
self.Send(swConfig)
self.Send(openflow13.NewSetControllerID(self.ctrlID))
}()
}
case *openflow13.SwitchConfig:
switch t.Header.Type {
case openflow13.Type_GetConfigReply:
case openflow13.Type_SetConfig:
}
case *openflow13.PacketIn:
log.Debugf("Received packet(ofctrl): %+v", t)
// send packet rcvd callback
self.app.PacketRcvd(self, (*PacketIn)(t))
case *openflow13.FlowRemoved:
case *openflow13.PortStatus:
// FIXME: This needs to propagated to the app.
case *openflow13.PacketOut:
case *openflow13.FlowMod:
case *openflow13.PortMod:
case *openflow13.MultipartRequest:
case *openflow13.MultipartReply:
log.Debugf("Received MultipartReply")
rep := (*openflow13.MultipartReply)(t)
if self.monitorEnabled {
key := fmt.Sprintf("%d", rep.Xid)
ch, found := monitoredFlows.Get(key)
if found {
replyChan := ch.(chan *openflow13.MultipartReply)
replyChan <- rep
}
}
// send packet rcvd callback
self.app.MultipartReply(self, rep)
case *openflow13.VendorError:
errData := t.ErrorMsg.Data.Bytes()
result := MessageResult{
succeed: false,
errType: t.Type,
errCode: t.Code,
experimenter: int32(t.ExperimenterID),
xID: t.Xid,
}
experimenterID := binary.BigEndian.Uint32(errData[8:12])
errMsg := GetErrorMessage(t.Type, t.Code, experimenterID)
experimenterType := binary.BigEndian.Uint32(errData[12:16])
switch experimenterID {
case openflow13.ONF_EXPERIMENTER_ID:
switch experimenterType {
case openflow13.Type_BundleCtrl:
bundleID := binary.BigEndian.Uint32(errData[16:20])
result.msgType = BundleControlMessage
self.publishMessage(bundleID, result)
log.Errorf("Received Vendor error: %s on ONFT_BUNDLE_CONTROL message", errMsg)
case openflow13.Type_BundleAdd:
bundleID := binary.BigEndian.Uint32(errData[16:20])
result.msgType = BundleAddMessage
self.publishMessage(bundleID, result)
log.Errorf("Received Vendor error: %s on ONFT_BUNDLE_ADD_MESSAGE message", errMsg)
}
default:
log.Errorf("Received Vendor error: %s", errMsg)
}
}
}
func (self *OFSwitch) getMPReq() *openflow13.MultipartRequest {
mp := &openflow13.MultipartRequest{}
mp.Type = openflow13.MultipartType_Flow
mp.Header = openflow13.NewOfp13Header()
mp.Header.Type = openflow13.Type_MultiPartRequest
return mp
}
func (self *OFSwitch) EnableMonitor() {
if self.monitorEnabled {
return
}
if self.mQueue == nil {
self.mQueue = make(chan *openflow13.MultipartRequest)
}
go func() {
for {
mp := <-self.mQueue
self.Send(mp)
log.Debugf("Send flow stats request")
}
}()
self.monitorEnabled = true
}
func (self *OFSwitch) DumpFlowStats(cookieID uint64, cookieMask *uint64, flowMatch *FlowMatch, tableID *uint8) ([]*openflow13.FlowStats, error) {
mp := self.getMPReq()
replyChan := make(chan *openflow13.MultipartReply)
go func() {
log.Debug("Add flow into monitor queue")
flowMonitorReq := openflow13.NewFlowStatsRequest()
if tableID != nil {
flowMonitorReq.TableId = *tableID
} else {
flowMonitorReq.TableId = 0xff
}
flowMonitorReq.Cookie = cookieID
if cookieMask != nil {
flowMonitorReq.CookieMask = *cookieMask
} else {
flowMonitorReq.CookieMask = ^uint64(0)
}
if flowMatch != nil {
f := &Flow{Match: *flowMatch}
flowMonitorReq.Match = f.xlateMatch()
}
mp.Body = []util.Message{flowMonitorReq}
monitoredFlows.Set(fmt.Sprintf("%d", mp.Xid), replyChan)
self.mQueue <- mp
}()
select {
case reply := <-replyChan:
flowStates := make([]*openflow13.FlowStats, 0)
if reply.Type == openflow13.MultipartType_Flow {
flowArr := reply.Body
for _, entry := range flowArr {
flowStates = append(flowStates, entry.(*openflow13.FlowStats))
}
return flowStates, nil
}
case <-time.After(2 * time.Second):
return nil, errors.New("timeout to wait for MultipartReply message")
}
return nil, nil
}
func (self *OFSwitch) CheckStatus(timeout time.Duration) bool {
return self.lastUpdate.Add(heartbeatInterval).After(time.Now())
}
func (self *OFSwitch) EnableOFPortForwarding(port int, portMAC net.HardwareAddr) error {
config := 0
config &^= openflow13.PC_NO_FWD
mask := openflow13.PC_NO_FWD
return self.sendModPortMessage(port, portMAC, config, mask)
}
func (self *OFSwitch) DisableOFPortForwarding(port int, portMAC net.HardwareAddr) error {
config := openflow13.PC_NO_FWD
mask := openflow13.PC_NO_FWD
return self.sendModPortMessage(port, portMAC, config, mask)
}
func (self *OFSwitch) subscribeMessage(xID uint32, msgChan chan MessageResult) {
self.txLock.Lock()
self.txChans[xID] = msgChan
self.txLock.Unlock()
}
func (self *OFSwitch) publishMessage(xID uint32, result MessageResult) {
go func() {
self.txLock.Lock()
defer self.txLock.Unlock()
ch, found := self.txChans[xID]
if found {
ch <- result
}
}()
}
func (self *OFSwitch) unSubscribeMessage(xID uint32) {
self.txLock.Lock()
defer self.txLock.Unlock()
_, found := self.txChans[xID]
if found {
delete(self.txChans, xID)
}
}
func (self *OFSwitch) sendModPortMessage(port int, mac net.HardwareAddr, config int, mask int) error {
msg := openflow13.NewPortMod(port)
msg.Header.Version = 0x4
msg.HWAddr = mac
msg.Config = uint32(config)
msg.Mask = uint32(mask)
return self.Send(msg)
}
func (self *OFSwitch) GetControllerID() uint16 {
return self.ctrlID
}