-
Notifications
You must be signed in to change notification settings - Fork 177
/
fgraphFlow.go
476 lines (377 loc) · 14.2 KB
/
fgraphFlow.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
/***
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
// This file implements the forwarding graph API for the flow
import (
"net"
"encoding/json"
"github.com/shaleman/libOpenflow/openflow13"
log "github.com/Sirupsen/logrus"
)
// Small subset of openflow fields we currently support
type FlowMatch struct {
Priority uint16 // Priority of the flow
InputPort uint32
MacDa *net.HardwareAddr
MacDaMask *net.HardwareAddr
MacSa *net.HardwareAddr
MacSaMask *net.HardwareAddr
Ethertype uint16
VlanId uint16
IpSa *net.IP
IpSaMask *net.IP
IpDa *net.IP
IpDaMask *net.IP
Metadata *uint64
MetadataMask *uint64
TunnelId uint64
}
// additional actions in flow's instruction set
type FlowAction struct {
actionType string // Type of action "setVlan", "setMetadata"
vlanId uint16 // Vlan Id in case of "setVlan"
macAddr net.HardwareAddr // Mac address to set
tunnelId uint64 // Tunnel Id (used for setting VNI)
metadata uint64 // Metadata in case of "setMetadata"
metadataMask uint64 // Metadata mask
}
// State of a flow entry
type Flow struct {
Table *Table // Table where this flow resides
Match FlowMatch // Fields to be matched
NextElem FgraphElem // Next fw graph element
isInstalled bool // Is the flow installed in the switch
flowId uint64 // Unique ID for the flow
flowActions []*FlowAction // List of flow actions
}
// string key for the flow
// FIXME: simple json conversion for now. This needs to be smarter
func (self *Flow) flowKey() string {
jsonVal, err := json.Marshal(self.Match)
if (err != nil) {
log.Errorf("Error forming flowkey for %+v. Err: %v", err)
return ""
}
return string(jsonVal)
}
// Fgraph element type for the flow
func (self *Flow) Type() string {
return "flow"
}
// instruction set for flow element
func (self *Flow) GetFlowInstr() openflow13.Instruction {
log.Fatalf("Unexpected call to get flow's instruction set")
return nil
}
// Translate our match fields into openflow 1.3 match fields
func (self *Flow) xlateMatch() openflow13.Match {
ofMatch := openflow13.NewMatch()
if (self.Match.InputPort != 0) {
inportField := openflow13.NewInPortField(self.Match.InputPort)
ofMatch.AddField(*inportField)
}
if (self.Match.MacDa != nil) {
if (self.Match.MacDaMask != nil) {
macDaField := openflow13.NewEthDstField(*self.Match.MacDa, self.Match.MacDaMask)
ofMatch.AddField(*macDaField)
} else {
macDaField := openflow13.NewEthDstField(*self.Match.MacDa, nil)
ofMatch.AddField(*macDaField)
}
}
if (self.Match.MacSa != nil) {
if (self.Match.MacSaMask != nil) {
macSaField := openflow13.NewEthSrcField(*self.Match.MacSa, self.Match.MacSaMask)
ofMatch.AddField(*macSaField)
} else {
macSaField := openflow13.NewEthSrcField(*self.Match.MacSa, nil)
ofMatch.AddField(*macSaField)
}
}
if (self.Match.Ethertype != 0) {
etypeField := openflow13.NewEthTypeField(self.Match.Ethertype)
ofMatch.AddField(*etypeField)
}
if (self.Match.VlanId != 0) {
vidField := openflow13.NewVlanIdField(self.Match.VlanId)
ofMatch.AddField(*vidField)
}
if (self.Match.IpDa != nil) {
if (self.Match.IpDaMask != nil) {
ipDaField := openflow13.NewIpv4DstField(*self.Match.IpDa, self.Match.IpDaMask)
ofMatch.AddField(*ipDaField)
} else {
ipDaField := openflow13.NewIpv4DstField(*self.Match.IpDa, nil)
ofMatch.AddField(*ipDaField)
}
}
if (self.Match.IpSa != nil) {
if (self.Match.IpSaMask != nil) {
ipSaField := openflow13.NewIpv4SrcField(*self.Match.IpSa, self.Match.IpSaMask)
ofMatch.AddField(*ipSaField)
} else {
ipSaField := openflow13.NewIpv4SrcField(*self.Match.IpSa, nil)
ofMatch.AddField(*ipSaField)
}
}
if (self.Match.Metadata != nil) {
if (self.Match.MetadataMask != nil) {
metadataField := openflow13.NewMetadataField(*self.Match.Metadata, self.Match.MetadataMask)
ofMatch.AddField(*metadataField)
} else {
metadataField := openflow13.NewMetadataField(*self.Match.Metadata, nil)
ofMatch.AddField(*metadataField)
}
}
if (self.Match.TunnelId != 0) {
tunnelIdField := openflow13.NewTunnelIdField(self.Match.TunnelId)
ofMatch.AddField(*tunnelIdField)
}
return *ofMatch
}
// Install all flow actions
func (self *Flow) installFlowActions(flowMod *openflow13.FlowMod,
instr openflow13.Instruction) error {
var actInstr openflow13.Instruction
var addActn bool = false
// Create a apply_action instruction to be used if its not already created
switch instr.(type) {
case *openflow13.InstrActions:
actInstr = instr
default:
actInstr = openflow13.NewInstrApplyActions()
}
// Loop thru all actions
for _, flowAction := range self.flowActions {
switch(flowAction.actionType) {
case "setVlan":
// Push Vlan Tag action
pushVlanAction := openflow13.NewActionPushVlan(0x8100)
// Set Outer vlan tag field
vlanField := openflow13.NewVlanIdField(flowAction.vlanId)
setVlanAction := openflow13.NewActionSetField(*vlanField)
// Prepend push vlan & setvlan actions to existing instruction
actInstr.AddAction(setVlanAction, true)
actInstr.AddAction(pushVlanAction, true)
addActn = true
log.Debugf("flow install. Added pushvlan action: %+v, setVlan actions: %+v",
pushVlanAction, setVlanAction)
case "popVlan":
// Create pop vln action
popVlan := openflow13.NewActionPopVlan()
// Add it to instruction
actInstr.AddAction(popVlan, true)
addActn = true
log.Debugf("flow install. Added popVlan action: %+v", popVlan)
case "setMacDa":
// Set Outer MacDA field
macDaField := openflow13.NewEthDstField(flowAction.macAddr, nil)
setMacDaAction := openflow13.NewActionSetField(*macDaField)
// Add set macDa action to the instruction
actInstr.AddAction(setMacDaAction, true)
addActn = true
log.Debugf("flow install. Added setMacDa action: %+v", setMacDaAction)
case "setMacSa":
// Set Outer MacSA field
macSaField := openflow13.NewEthSrcField(flowAction.macAddr, nil)
setMacSaAction := openflow13.NewActionSetField(*macSaField)
// Add set macDa action to the instruction
actInstr.AddAction(setMacSaAction, true)
addActn = true
log.Debugf("flow install. Added setMacSa Action: %+v", setMacSaAction)
case "setTunnelId":
// Set tunnelId field
tunnelIdField := openflow13.NewTunnelIdField(flowAction.tunnelId)
setTunnelAction := openflow13.NewActionSetField(*tunnelIdField)
// Add set tunnel action to the instruction
actInstr.AddAction(setTunnelAction, true)
addActn = true
log.Debugf("flow install. Added setTunnelId Action: %+v", setTunnelAction)
case "setMetadata":
// Set Metadata instruction
metadataInstr := openflow13.NewInstrWriteMetadata(flowAction.metadata, flowAction.metadataMask)
// Add the instruction to flowmod
flowMod.AddInstruction(metadataInstr)
default:
log.Fatalf("Unknown action type %s", flowAction.actionType)
}
}
// Add the instruction to flow if its not already added
if ((addActn) && (actInstr != instr)) {
// Add the instrction to flowmod
flowMod.AddInstruction(actInstr)
}
return nil
}
// Install a flow entry
func (self *Flow) install() error {
// Create a flowmode entry
flowMod := openflow13.NewFlowMod()
flowMod.TableId = self.Table.TableId
flowMod.Priority = self.Match.Priority
flowMod.Cookie = self.flowId
// Add or modify
if (!self.isInstalled) {
flowMod.Command = openflow13.FC_ADD
} else {
flowMod.Command = openflow13.FC_MODIFY
}
// convert match fields to openflow 1.3 format
flowMod.Match = self.xlateMatch()
log.Debugf("flow install: Match: %+v", flowMod.Match)
// Based on the next elem, decide what to install
switch (self.NextElem.Type()) {
case "table":
// Get the instruction set from the element
instr := self.NextElem.GetFlowInstr()
// Check if there are any flow actions to perform
self.installFlowActions(flowMod, instr)
// Add the instruction to flowmod
flowMod.AddInstruction(instr)
log.Debugf("flow install: added goto table instr: %+v", instr)
case "flood": fallthrough;
case "output":
// Get the instruction set from the element
instr := self.NextElem.GetFlowInstr()
// Add the instruction to flowmod if its not nil
// a nil instruction means drop action
if (instr != nil) {
// Check if there are any flow actions to perform
self.installFlowActions(flowMod, instr)
flowMod.AddInstruction(instr)
log.Debugf("flow install: added output port instr: %+v", instr)
}
default:
log.Fatalf("Unknown Fgraph element type %s", self.NextElem.Type())
}
log.Debugf("Sending flowmod: %+v", flowMod)
// Send the message
self.Table.Switch.Send(flowMod)
// Mark it as installed
self.isInstalled = true
return nil
}
// Set Next element in the Fgraph. This determines what actions will be
// part of the flow's instruction set
func (self *Flow) Next(elem FgraphElem) error {
// Set the next element in the graph
self.NextElem = elem
// Install the flow entry
return self.install()
}
// Special actions on the flow to set vlan id
func (self *Flow) SetVlan(vlanId uint16) error {
action := new(FlowAction)
action.actionType = "setVlan"
action.vlanId = vlanId
// Add to the action list
// FIXME: detect duplicates
self.flowActions = append(self.flowActions, action)
// If the flow entry was already installed, re-install it
if (self.isInstalled) {
self.install()
}
return nil
}
// Special actions on the flow to set vlan id
func (self *Flow) PopVlan() error {
action := new(FlowAction)
action.actionType = "popVlan"
// Add to the action list
// FIXME: detect duplicates
self.flowActions = append(self.flowActions, action)
// If the flow entry was already installed, re-install it
if (self.isInstalled) {
self.install()
}
return nil
}
// Special actions on the flow to set mac dest addr
func (self *Flow) SetMacDa(macDa net.HardwareAddr) error {
action := new(FlowAction)
action.actionType = "setMacDa"
action.macAddr = macDa
// Add to the action list
// FIXME: detect duplicates
self.flowActions = append(self.flowActions, action)
// If the flow entry was already installed, re-install it
if (self.isInstalled) {
self.install()
}
return nil
}
// Special action on the flow to set mac source addr
func (self *Flow) SetMacSa(macSa net.HardwareAddr) error {
action := new(FlowAction)
action.actionType = "setMacSa"
action.macAddr = macSa
// Add to the action list
// FIXME: detect duplicates
self.flowActions = append(self.flowActions, action)
// If the flow entry was already installed, re-install it
if (self.isInstalled) {
self.install()
}
return nil
}
// Special actions on the flow to set metadata
func (self *Flow) SetMetadata(metadata , metadataMask uint64) error {
action := new(FlowAction)
action.actionType = "setMetadata"
action.metadata = metadata
action.metadataMask = metadataMask
// Add to the action list
// FIXME: detect duplicates
self.flowActions = append(self.flowActions, action)
// If the flow entry was already installed, re-install it
if (self.isInstalled) {
self.install()
}
return nil
}
// Special actions on the flow to set vlan id
func (self *Flow) SetTunnelId(tunnelId uint64) error {
action := new(FlowAction)
action.actionType = "setTunnelId"
action.tunnelId = tunnelId
// Add to the action list
// FIXME: detect duplicates
self.flowActions = append(self.flowActions, action)
// If the flow entry was already installed, re-install it
if (self.isInstalled) {
self.install()
}
return nil
}
// Delete the flow
func (self *Flow) Delete() error {
// Delete from ofswitch
if (self.isInstalled) {
// Create a flowmode entry
flowMod := openflow13.NewFlowMod()
flowMod.Command = openflow13.FC_DELETE
flowMod.TableId = self.Table.TableId
flowMod.Priority = self.Match.Priority
flowMod.Cookie = self.flowId
flowMod.CookieMask = 0xffffffffffffffff
flowMod.OutPort = openflow13.P_ANY
flowMod.OutGroup = openflow13.OFPG_ANY
log.Debugf("Sending DELETE flowmod: %+v", flowMod)
// Send the message
self.Table.Switch.Send(flowMod)
}
// Delete it from the table
flowKey := self.flowKey()
self.Table.DeleteFlow(flowKey)
return nil
}