-
Notifications
You must be signed in to change notification settings - Fork 26
/
switch.go
287 lines (265 loc) · 7 KB
/
switch.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
package ogo
import (
"log"
"net"
"sync"
"github.com/jonstout/ogo/protocol/ofp10"
"github.com/jonstout/ogo/protocol/ofpxx"
"github.com/jonstout/ogo/protocol/util"
)
// A map from DPIDs to all Switches that have connected since
// Ogo started.
type Network struct {
sync.RWMutex
Switches map[string]*OFSwitch
}
func NewNetwork() *Network {
n := new(Network)
n.Switches = make(map[string]*OFSwitch)
return n
}
var network *Network
type OFSwitch struct {
stream *MessageStream
appInstance []interface{}
dpid net.HardwareAddr
ports map[uint16]ofp10.PhyPort
portsMu sync.RWMutex
links map[string]*Link
linksMu sync.RWMutex
reqs map[uint32]chan util.Message
reqsMu sync.RWMutex
}
// Builds and populates a Switch struct then starts listening
// for OpenFlow messages on conn.
func NewSwitch(stream *MessageStream, msg ofp10.SwitchFeatures) {
network.Lock()
if sw, ok := network.Switches[msg.DPID.String()]; ok {
log.Println("Recovered connection from:", sw.DPID())
sw.stream = stream
go sw.receive()
} else {
log.Println("Openflow Connection:", msg.DPID)
s := new(OFSwitch)
s.stream = stream
s.appInstance = *new([]interface{})
s.dpid = msg.DPID
s.ports = make(map[uint16]ofp10.PhyPort)
s.links = make(map[string]*Link)
s.reqs = make(map[uint32]chan util.Message)
for _, p := range msg.Ports {
s.ports[p.PortNo] = p
}
network.Switches[msg.DPID.String()] = s
go s.receive()
}
network.Unlock()
}
func (sw *OFSwitch) AddInstance(inst interface{}) {
if actor, ok := inst.(ofp10.ConnectionUpReactor); ok {
actor.ConnectionUp(sw.DPID())
}
sw.appInstance = append(sw.appInstance, inst)
}
func (sw *OFSwitch) SetPort(portNo uint16, port ofp10.PhyPort) {
sw.portsMu.Lock()
defer sw.portsMu.Unlock()
sw.ports[portNo] = port
}
// Returns a pointer to the Switch mapped to dpid.
func Switch(dpid net.HardwareAddr) (*OFSwitch, bool) {
network.RLock()
defer network.RUnlock()
if sw, ok := network.Switches[dpid.String()]; ok {
return sw, ok
}
return nil, false
}
// Returns a slice of *OFPSwitches for operations across all
// switches.
func Switches() []*OFSwitch {
network.RLock()
defer network.RUnlock()
a := make([]*OFSwitch, len(network.Switches))
i := 0
for _, v := range network.Switches {
a[i] = v
i++
}
return a
}
// Disconnects Switch dpid.
func disconnect(dpid net.HardwareAddr) {
network.Lock()
defer network.Unlock()
log.Printf("Closing connection with: %s", dpid)
network.Switches[dpid.String()].stream.Shutdown <- true
delete(network.Switches, dpid.String())
}
// Returns a slice of all links connected to Switch s.
func (s *OFSwitch) Links() []Link {
s.linksMu.RLock()
a := make([]Link, 0)
for _, v := range s.links {
a = append(a, *v)
}
s.linksMu.RUnlock()
return a
}
// Returns the link between Switch s and the Switch dpid.
func (s *OFSwitch) Link(dpid net.HardwareAddr) (l Link, ok bool) {
s.linksMu.RLock()
if n, k := s.links[dpid.String()]; k {
l = *n
ok = true
}
s.linksMu.RUnlock()
return
}
// Updates the link between s.DPID and l.DPID.
func (s *OFSwitch) setLink(dpid net.HardwareAddr, l *Link) {
s.linksMu.Lock()
if _, ok := s.links[l.DPID.String()]; !ok {
log.Println("Link discovered:", dpid, l.Port, l.DPID)
}
s.links[l.DPID.String()] = l
s.linksMu.Unlock()
}
// Returns the dpid of Switch s.
func (s *OFSwitch) DPID() net.HardwareAddr {
return s.dpid
}
// Returns a slice of all the ports from Switch s.
func (s *OFSwitch) Ports() []ofp10.PhyPort {
s.portsMu.RLock()
a := make([]ofp10.PhyPort, len(s.ports))
i := 0
for _, v := range s.ports {
a[i] = v
i++
}
s.portsMu.RUnlock()
return a
}
// Returns a pointer to the OfpPhyPort at port number from Switch s.
func (sw *OFSwitch) Port(portNo uint16) (port ofp10.PhyPort, ok bool) {
sw.portsMu.RLock()
defer sw.portsMu.RUnlock()
port, ok = sw.ports[portNo]
return
}
// Sends an OpenFlow message to this Switch.
func (s *OFSwitch) Send(req util.Message) {
s.stream.Outbound <- req
}
// Receive loop for each Switch.
func (s *OFSwitch) receive() {
for {
select {
case msg := <-s.stream.Inbound:
// New message has been received from message
// stream.
go s.distributeMessages(s.dpid, msg)
case err := <-s.stream.Error:
// Message stream has been disconnected.
for _, app := range s.appInstance {
if actor, ok := app.(ofp10.ConnectionDownReactor); ok {
actor.ConnectionDown(s.DPID(), err)
}
}
return
}
}
}
func (s *OFSwitch) distributeMessages(dpid net.HardwareAddr, msg util.Message) {
for _, app := range s.appInstance {
switch t := msg.(type) {
case *ofpxx.Header:
switch t.Header().Type {
case ofp10.Type_Hello:
if actor, ok := app.(ofp10.HelloReactor); ok {
actor.Hello(t)
}
case ofp10.Type_EchoRequest:
if actor, ok := app.(ofp10.EchoRequestReactor); ok {
actor.EchoRequest(s.DPID())
}
case ofp10.Type_EchoReply:
if actor, ok := app.(ofp10.EchoReplyReactor); ok {
actor.EchoReply(s.DPID())
}
case ofp10.Type_FeaturesRequest:
if actor, ok := app.(ofp10.FeaturesRequestReactor); ok {
actor.FeaturesRequest(t)
}
case ofp10.Type_GetConfigRequest:
if actor, ok := app.(ofp10.GetConfigRequestReactor); ok {
actor.GetConfigRequest(t)
}
case ofp10.Type_BarrierRequest:
if actor, ok := app.(ofp10.BarrierRequestReactor); ok {
actor.BarrierRequest(t)
}
case ofp10.Type_BarrierReply:
if actor, ok := app.(ofp10.BarrierReplyReactor); ok {
actor.BarrierReply(s.DPID(), t)
}
}
case *ofp10.ErrorMsg:
if actor, ok := app.(ofp10.ErrorReactor); ok {
actor.Error(s.DPID(), t)
}
case *ofp10.VendorHeader:
if actor, ok := app.(ofp10.VendorReactor); ok {
actor.VendorHeader(s.DPID(), t)
}
case *ofp10.SwitchFeatures:
if actor, ok := app.(ofp10.FeaturesReplyReactor); ok {
actor.FeaturesReply(s.DPID(), t)
}
case *ofp10.SwitchConfig:
switch t.Header.Type {
case ofp10.Type_GetConfigReply:
if actor, ok := app.(ofp10.GetConfigReplyReactor); ok {
actor.GetConfigReply(s.DPID(), t)
}
case ofp10.Type_SetConfig:
if actor, ok := app.(ofp10.SetConfigReactor); ok {
actor.SetConfig(t)
}
}
case *ofp10.PacketIn:
if actor, ok := app.(ofp10.PacketInReactor); ok {
actor.PacketIn(s.DPID(), t)
}
case *ofp10.FlowRemoved:
if actor, ok := app.(ofp10.FlowRemovedReactor); ok {
actor.FlowRemoved(s.DPID(), t)
}
case *ofp10.PortStatus:
if actor, ok := app.(ofp10.PortStatusReactor); ok {
actor.PortStatus(s.DPID(), t)
}
case *ofp10.PacketOut:
if actor, ok := app.(ofp10.PacketOutReactor); ok {
actor.PacketOut(t)
}
case *ofp10.FlowMod:
if actor, ok := app.(ofp10.FlowModReactor); ok {
actor.FlowMod(t)
}
case *ofp10.PortMod:
if actor, ok := app.(ofp10.PortModReactor); ok {
actor.PortMod(t)
}
case *ofp10.StatsRequest:
if actor, ok := app.(ofp10.StatsRequestReactor); ok {
actor.StatsRequest(t)
}
case *ofp10.StatsReply:
if actor, ok := app.(ofp10.StatsReplyReactor); ok {
actor.StatsReply(s.DPID(), t)
}
}
}
}