forked from contiv/libOpenflow
/
action.go
526 lines (446 loc) · 11.1 KB
/
action.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
package openflow13
import (
"encoding/binary"
"errors"
"antrea.io/libOpenflow/util"
)
// ofp_action_type
const (
ActionType_Output = 0
ActionType_CopyTtlOut = 11
ActionType_CopyTtlIn = 12
ActionType_SetMplsTtl = 15
ActionType_DecMplsTtl = 16
ActionType_PushVlan = 17
ActionType_PopVlan = 18
ActionType_PushMpls = 19
ActionType_PopMpls = 20
ActionType_SetQueue = 21
ActionType_Group = 22
ActionType_SetNwTtl = 23
ActionType_DecNwTtl = 24
ActionType_SetField = 25
ActionType_PushPbb = 26
ActionType_PopPbb = 27
ActionType_Experimenter = 0xffff
)
type Action interface {
Header() *ActionHeader
util.Message
}
type ActionHeader struct {
Type uint16
Length uint16
}
func (a *ActionHeader) Header() *ActionHeader {
return a
}
func (a *ActionHeader) Len() (n uint16) {
return 4
}
func (a *ActionHeader) MarshalBinary() (data []byte, err error) {
data = make([]byte, a.Len())
binary.BigEndian.PutUint16(data[:2], a.Type)
binary.BigEndian.PutUint16(data[2:4], a.Length)
return
}
func (a *ActionHeader) UnmarshalBinary(data []byte) error {
if len(data) < int(a.Len()) {
return errors.New("The []byte the wrong size to unmarshal an " +
"ActionHeader message.")
}
a.Type = binary.BigEndian.Uint16(data[:2])
a.Length = binary.BigEndian.Uint16(data[2:4])
return nil
}
// Decode Action types.
func DecodeAction(data []byte) (Action, error) {
t := binary.BigEndian.Uint16(data[:2])
var a Action
switch t {
case ActionType_Output:
a = new(ActionOutput)
case ActionType_CopyTtlOut:
a = new(ActionHeader)
case ActionType_CopyTtlIn:
a = new(ActionHeader)
case ActionType_SetMplsTtl:
a = new(ActionMplsTtl)
case ActionType_DecMplsTtl:
a = new(ActionHeader)
case ActionType_PushVlan:
a = new(ActionPush)
case ActionType_PopVlan:
a = new(ActionHeader)
case ActionType_PushMpls:
a = new(ActionPush)
case ActionType_PopMpls:
a = new(ActionPopMpls)
case ActionType_SetQueue:
a = new(ActionSetqueue)
case ActionType_Group:
a = new(ActionGroup)
case ActionType_SetNwTtl:
a = new(ActionNwTtl)
case ActionType_DecNwTtl:
a = new(ActionDecNwTtl)
case ActionType_SetField:
a = new(ActionSetField)
case ActionType_PushPbb:
a = new(ActionPush)
case ActionType_PopPbb:
a = new(ActionHeader)
case ActionType_Experimenter:
// For Experimenter message, the length of action should be at least 10 bytes,
// including type(2 byte), length(2 byte), vendor(4 byte), and subtype(2 byte)
if len(data) < NxActionHeaderLength {
return nil, errors.New("the []byte is too short to decode OpenFlow experimenter message")
}
v := binary.BigEndian.Uint32(data[4:8])
if v == NxExperimenterID {
a = DecodeNxAction(data)
}
}
err := a.UnmarshalBinary(data)
if err != nil {
return a, err
}
return a, nil
}
// Action structure for OFPAT_OUTPUT, which sends packets out ’port’.
// When the ’port’ is the OFPP_CONTROLLER, ’max_len’ indicates the max
// number of bytes to send. A ’max_len’ of zero means no bytes of the
// packet should be sent.
type ActionOutput struct {
ActionHeader
Port uint32
MaxLen uint16
pad []byte // 6 bytes to make it 64bit aligned
}
// ofp_controller_max_len 1.3
const (
OFPCML_MAX = 0xffe5 /* maximum max_len value which can be used to request a specific byte length. */
OFPCML_NO_BUFFER = 0xffff /* indicates that no buffering should be applied and the whole packet is to be sent to the controller. */
)
// Returns a new Action Output message which sends packets out
// port number.
func NewActionOutput(portNum uint32) *ActionOutput {
act := new(ActionOutput)
act.Type = ActionType_Output
act.Length = act.Len()
act.Port = portNum
act.MaxLen = 256
act.pad = make([]byte, 6)
return act
}
func (a *ActionOutput) Len() (n uint16) {
return a.ActionHeader.Len() + 12
}
func (a *ActionOutput) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(a.Len()))
var b []byte
n := 0
if b, err = a.ActionHeader.MarshalBinary(); err != nil {
return
}
copy(data[n:], b)
n += len(b)
binary.BigEndian.PutUint32(data[n:], a.Port)
n += 4
binary.BigEndian.PutUint16(data[n:], a.MaxLen)
n += 2
copy(data[n:], a.pad)
n += len(a.pad)
return
}
func (a *ActionOutput) UnmarshalBinary(data []byte) error {
if len(data) < int(a.Len()) {
return errors.New("The []byte the wrong size to unmarshal an " +
"ActionOutput message.")
}
n := 0
err := a.ActionHeader.UnmarshalBinary(data[n:])
n += int(a.ActionHeader.Len())
a.Port = binary.BigEndian.Uint32(data[n:])
n += 4
a.MaxLen = binary.BigEndian.Uint16(data[n:])
n += 2
copy(a.pad, data[n:n+6])
n += 6
return err
}
type ActionSetqueue struct {
ActionHeader
QueueId uint32
}
func NewActionSetQueue(queue uint32) *ActionSetqueue {
a := new(ActionSetqueue)
a.Type = ActionType_SetQueue
a.Length = a.Len()
a.QueueId = queue
return a
}
func (a *ActionSetqueue) Len() (n uint16) {
return a.ActionHeader.Len() + 4
}
func (a *ActionSetqueue) MarshalBinary() (data []byte, err error) {
data, err = a.ActionHeader.MarshalBinary()
bytes := make([]byte, 4)
binary.BigEndian.PutUint32(bytes[0:], a.QueueId)
data = append(data, bytes...)
return
}
func (a *ActionSetqueue) UnmarshalBinary(data []byte) error {
if len(data) != int(a.Len()) {
return errors.New("The []byte the wrong size to unmarshal an " +
"ActionEnqueue message.")
}
a.ActionHeader.UnmarshalBinary(data[:4])
a.QueueId = binary.BigEndian.Uint32(data[4:8])
return nil
}
type ActionGroup struct {
ActionHeader
GroupId uint32
}
func NewActionGroup(group uint32) *ActionGroup {
a := new(ActionGroup)
a.Type = ActionType_Group
a.Length = a.Len()
a.GroupId = group
return a
}
func (a *ActionGroup) Len() (n uint16) {
return a.ActionHeader.Len() + 4
}
func (a *ActionGroup) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(a.Len()))
var b []byte
n := 0
if b, err = a.ActionHeader.MarshalBinary(); err != nil {
return
}
copy(data[n:], b)
n += len(b)
binary.BigEndian.PutUint32(data[n:], a.GroupId)
n += 4
return
}
func (a *ActionGroup) UnmarshalBinary(data []byte) error {
if len(data) < int(a.Len()) {
return errors.New("The []byte the wrong size to unmarshal an " +
"ActionOutput message.")
}
n := 0
err := a.ActionHeader.UnmarshalBinary(data[n:])
n += int(a.ActionHeader.Len())
a.GroupId = binary.BigEndian.Uint32(data[n:])
n += 4
return err
}
type ActionMplsTtl struct {
ActionHeader
MplsTtl uint8
}
func (a *ActionMplsTtl) Len() uint16 {
return a.ActionHeader.Len() + 4
}
func (a *ActionMplsTtl) MarshalBinary() (data []byte, err error) {
data, err = a.ActionHeader.MarshalBinary()
if err != nil {
return
}
n := int(a.ActionHeader.Len())
data[n] = a.MplsTtl
return
}
func (a *ActionMplsTtl) UnmarshalBinary(data []byte) error {
n := 0
err := a.ActionHeader.UnmarshalBinary(data[n:])
if err != nil {
return err
}
n += int(a.ActionHeader.Len())
a.MplsTtl = data[n]
return nil
}
type ActionDecNwTtl struct {
ActionHeader
pad []byte // 4bytes
}
func NewActionDecNwTtl() *ActionDecNwTtl {
act := new(ActionDecNwTtl)
act.Type = ActionType_DecNwTtl
act.Length = act.Len()
act.pad = make([]byte, 4)
return act
}
func (a *ActionDecNwTtl) Len() (n uint16) {
return a.ActionHeader.Len() + 4
}
func (a *ActionDecNwTtl) MarshalBinary() (data []byte, err error) {
data, err = a.ActionHeader.MarshalBinary()
if err != nil {
return
}
// Padding
bytes := make([]byte, 4)
data = append(data, bytes...)
return
}
func (a *ActionDecNwTtl) UnmarshalBinary(data []byte) error {
return a.ActionHeader.UnmarshalBinary(data[:4])
}
type ActionNwTtl struct {
ActionHeader
NwTtl uint8
}
func (a *ActionNwTtl) Len() uint16 {
return a.ActionHeader.Len() + 4
}
func (a *ActionNwTtl) MarshalBinary() (data []byte, err error) {
data, err = a.ActionHeader.MarshalBinary()
if err != nil {
return
}
n := int(a.ActionHeader.Len())
data[n] = a.NwTtl
return
}
func (a *ActionNwTtl) UnmarshalBinary(data []byte) error {
n := 0
err := a.ActionHeader.UnmarshalBinary(data[n:])
if err != nil {
return err
}
n += int(a.ActionHeader.Len())
a.NwTtl = data[n]
return nil
}
type ActionPush struct {
ActionHeader
EtherType uint16
}
func NewActionPushVlan(etherType uint16) *ActionPush {
a := new(ActionPush)
a.Type = ActionType_PushVlan
a.Length = a.Len()
a.EtherType = etherType
return a
}
func NewActionPushMpls(etherType uint16) *ActionPush {
a := new(ActionPush)
a.Type = ActionType_PushMpls
a.Length = a.Len()
a.EtherType = etherType
return a
}
func (a *ActionPush) Len() (n uint16) {
return a.ActionHeader.Len() + 4
}
func (a *ActionPush) MarshalBinary() (data []byte, err error) {
data, err = a.ActionHeader.MarshalBinary()
bytes := make([]byte, 4)
binary.BigEndian.PutUint16(bytes[0:], a.EtherType)
data = append(data, bytes...)
return
}
func (a *ActionPush) UnmarshalBinary(data []byte) error {
a.ActionHeader.UnmarshalBinary(data[:4])
a.EtherType = binary.BigEndian.Uint16(data[4:])
return nil
}
type ActionPopVlan struct {
ActionHeader
}
func NewActionPopVlan() *ActionPopVlan {
act := new(ActionPopVlan)
act.Type = ActionType_PopVlan
act.Length = act.Len()
return act
}
func (a *ActionPopVlan) Len() (n uint16) {
return a.ActionHeader.Len() + 4
}
func (a *ActionPopVlan) MarshalBinary() (data []byte, err error) {
data, err = a.ActionHeader.MarshalBinary()
// Padding
bytes := make([]byte, 4)
data = append(data, bytes...)
return
}
func (a *ActionPopVlan) UnmarshalBinary(data []byte) error {
a.ActionHeader.UnmarshalBinary(data[:4])
return nil
}
type ActionPopMpls struct {
ActionHeader
EtherType uint16
}
func NewActionPopMpls(etherType uint16) *ActionPopMpls {
act := new(ActionPopMpls)
act.Type = ActionType_PopMpls
act.EtherType = etherType
act.Length = act.Len()
return act
}
func (a *ActionPopMpls) Len() (n uint16) {
return a.ActionHeader.Len() + 4
}
func (a *ActionPopMpls) MarshalBinary() (data []byte, err error) {
data, err = a.ActionHeader.MarshalBinary()
// Padding
bytes := make([]byte, 4)
binary.BigEndian.PutUint16(bytes[0:], a.EtherType)
data = append(data, bytes...)
return
}
func (a *ActionPopMpls) UnmarshalBinary(data []byte) error {
a.ActionHeader.UnmarshalBinary(data[:4])
a.EtherType = binary.BigEndian.Uint16(data[4:])
return nil
}
type ActionSetField struct {
ActionHeader
Field MatchField
}
func NewActionSetField(field MatchField) *ActionSetField {
a := new(ActionSetField)
a.Type = ActionType_SetField
a.Field = field
a.Length = a.Len()
return a
}
func (a *ActionSetField) Len() (n uint16) {
n = a.ActionHeader.Len() + a.Field.Len()
// Round it to closest multiple of 8
n = ((n + 7) / 8) * 8
return
}
func (a *ActionSetField) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(a.Len()))
var b []byte
n := 0
if b, err = a.ActionHeader.MarshalBinary(); err != nil {
return
}
copy(data, b)
n += int(a.ActionHeader.Len())
if b, err = a.Field.MarshalBinary(); err != nil {
return
}
copy(data[n:], b)
return
}
func (a *ActionSetField) UnmarshalBinary(data []byte) error {
n := 0
if err := a.ActionHeader.UnmarshalBinary(data[n:]); err != nil {
return err
}
n += int(a.ActionHeader.Len())
if err := a.Field.UnmarshalBinary(data[n:]); err != nil {
return err
}
n += int(a.Field.Len())
return nil
}