-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathofSwitch.go
executable file
·223 lines (173 loc) · 5.13 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
/***
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 (
"net"
"sync"
"time"
"github.com/contiv/libOpenflow/common"
"github.com/contiv/libOpenflow/openflow13"
"github.com/contiv/libOpenflow/util"
log "github.com/Sirupsen/logrus"
cmap "github.com/streamrail/concurrent-map"
)
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
portMux sync.Mutex
outputPorts map[uint32]*Output
}
var switchDb cmap.ConcurrentMap
func init() {
switchDb = 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) *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
// 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
}
// 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) {
self.stream.Outbound <- req
}
func (self *OFSwitch) Disconnect() {
self.stream.Shutdown <- true
self.switchDisconnected()
}
// Handle switch connected event
func (self *OFSwitch) switchConnected() {
self.app.SwitchConnected(self)
// Send new feature request
self.Send(openflow13.NewFeaturesRequest())
// FIXME: This is too fragile. Create a periodic timer
// Start the periodic echo request loop
self.Send(openflow13.NewEchoRequest())
}
// Handle switch disconnected event
func (self *OFSwitch) switchDisconnected() {
self.app.SwitchDisconnected(self)
switchDb.Remove(self.DPID().String())
}
// 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:
// FIXME: This is too fragile. Create a periodic timer
// Wait three seconds then send an echo_request message.
go func() {
<-time.After(time.Second * 3)
// Send echo request
res := openflow13.NewEchoRequest()
self.Send(res)
}()
case openflow13.Type_FeaturesRequest:
case openflow13.Type_GetConfigRequest:
case openflow13.Type_BarrierRequest:
case openflow13.Type_BarrierReply:
}
case *openflow13.ErrorMsg:
log.Errorf("Received ofp1.3 error msg: %+v", *t)
case *openflow13.VendorHeader:
case *openflow13.SwitchFeatures:
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")
// send packet rcvd callback
self.app.MultipartReply(self, (*openflow13.MultipartReply)(t))
}
}