forked from contiv/libOpenflow
/
openflow13.go
891 lines (795 loc) · 21.8 KB
/
openflow13.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
package openflow13
// Package openflow13 provides OpenFlow 1.3 structs along with Read
// and Write methods for each.
// OpenFlow Wire Protocol 0x04
//
// Struct documentation is taken from the OpenFlow Switch
// Specification Version 1.3.3.
// https://www.opennetworking.org/images/stories/downloads/sdn-resources/onf-specifications/openflow/openflow-spec-v1.3.3.pdf
import (
"encoding/binary"
"errors"
"net"
"antrea.io/libOpenflow/common"
"antrea.io/libOpenflow/protocol"
"antrea.io/libOpenflow/util"
)
const (
VERSION = 4
)
// Returns a new OpenFlow header with version field set to v1.3.
var NewOfp13Header func() common.Header = common.NewHeaderGenerator(VERSION)
// Echo request/reply messages can be sent from either the
// switch or the controller, and must return an echo reply. They
// can be used to indicate the latency, bandwidth, and/or
// liveness of a controller-switch connection.
func NewEchoRequest() *common.Header {
h := NewOfp13Header()
h.Type = Type_EchoRequest
return &h
}
// Echo request/reply messages can be sent from either the
// switch or the controller, and must return an echo reply. They
// can be used to indicate the latency, bandwidth, and/or
// liveness of a controller-switch connection.
func NewEchoReply() *common.Header {
h := NewOfp13Header()
h.Type = Type_EchoReply
return &h
}
// ofp_type 1.3
const (
/* Immutable messages. */
Type_Hello = 0
Type_Error = 1
Type_EchoRequest = 2
Type_EchoReply = 3
Type_Experimenter = 4
/* Switch configuration messages. */
Type_FeaturesRequest = 5
Type_FeaturesReply = 6
Type_GetConfigRequest = 7
Type_GetConfigReply = 8
Type_SetConfig = 9
/* Asynchronous messages. */
Type_PacketIn = 10
Type_FlowRemoved = 11
Type_PortStatus = 12
/* Controller command messages. */
Type_PacketOut = 13
Type_FlowMod = 14
Type_GroupMod = 15
Type_PortMod = 16
Type_TableMod = 17
/* Multipart messages. */
Type_MultiPartRequest = 18
Type_MultiPartReply = 19
/* Barrier messages. */
Type_BarrierRequest = 20
Type_BarrierReply = 21
/* Queue Configuration messages. */
Type_QueueGetConfigRequest = 22
Type_QueueGetConfigReply = 23
/* Controller role change request messages. */
Type_RoleRequest = 24
Type_RoleReply = 25
/* Asynchronous message configuration. */
Type_GetAsyncRequest = 26
Type_GetAsyncReply = 27
Type_SetAsync = 28
/* Meters and rate limiters configuration messages. */
Type_MeterMod = 29
)
func Parse(b []byte) (message util.Message, err error) {
switch b[1] {
case Type_Hello:
message = new(common.Hello)
err = message.UnmarshalBinary(b)
case Type_Error:
errMsg := new(ErrorMsg)
err = errMsg.UnmarshalBinary(b)
if err != nil {
return
}
switch errMsg.Type {
case ET_EXPERIMENTER:
message = new(VendorError)
err = message.UnmarshalBinary(b)
default:
message = errMsg
}
case Type_EchoRequest:
message = new(common.Header)
err = message.UnmarshalBinary(b)
case Type_EchoReply:
message = new(common.Header)
err = message.UnmarshalBinary(b)
case Type_Experimenter:
message = new(VendorHeader)
err = message.UnmarshalBinary(b)
case Type_FeaturesRequest:
message = NewFeaturesRequest()
err = message.UnmarshalBinary(b)
case Type_FeaturesReply:
message = NewFeaturesReply()
err = message.UnmarshalBinary(b)
case Type_GetConfigRequest:
message = new(common.Header)
err = message.UnmarshalBinary(b)
case Type_GetConfigReply:
message = new(SwitchConfig)
err = message.UnmarshalBinary(b)
case Type_SetConfig:
message = NewSetConfig()
err = message.UnmarshalBinary(b)
case Type_PacketIn:
message = new(PacketIn)
err = message.UnmarshalBinary(b)
case Type_FlowRemoved:
message = NewFlowRemoved()
err = message.UnmarshalBinary(b)
case Type_PortStatus:
message = new(PortStatus)
err = message.UnmarshalBinary(b)
case Type_PacketOut:
break
case Type_FlowMod:
message = NewFlowMod()
err = message.UnmarshalBinary(b)
case Type_GroupMod:
break
case Type_PortMod:
break
case Type_TableMod:
break
case Type_BarrierRequest:
message = new(common.Header)
err = message.UnmarshalBinary(b)
case Type_BarrierReply:
message = new(common.Header)
err = message.UnmarshalBinary(b)
case Type_QueueGetConfigRequest:
break
case Type_QueueGetConfigReply:
break
case Type_MultiPartRequest:
message = new(MultipartRequest)
err = message.UnmarshalBinary(b)
case Type_MultiPartReply:
message = new(MultipartReply)
err = message.UnmarshalBinary(b)
default:
err = errors.New("An unknown v1.0 packet type was received. Parse function will discard data.")
}
return
}
// When the controller wishes to send a packet out through the
// datapath, it uses the OFPT_PACKET_OUT message: The buffer_id
// is the same given in the ofp_packet_in message. If the
// buffer_id is -1, then the packet data is included in the data
// array. If OFPP_TABLE is specified as the output port of an
// action, the in_port in the packet_out message is used in the
// flow table lookup.
type PacketOut struct {
common.Header
BufferId uint32
InPort uint32
ActionsLen uint16
pad []byte
Actions []Action
Data util.Message
}
func NewPacketOut() *PacketOut {
p := new(PacketOut)
p.Header = NewOfp13Header()
p.Header.Type = Type_PacketOut
p.BufferId = 0xffffffff
p.InPort = P_ANY
p.ActionsLen = 0
p.pad = make([]byte, 6)
p.Actions = make([]Action, 0)
return p
}
func (p *PacketOut) AddAction(act Action) {
p.Actions = append(p.Actions, act)
p.ActionsLen += act.Len()
}
func (p *PacketOut) Len() (n uint16) {
n += p.Header.Len()
n += 16
for _, a := range p.Actions {
n += a.Len()
}
n += p.Data.Len()
// if n < 72 { return 72 }
return
}
func (p *PacketOut) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(p.Len()))
var b []byte
n := 0
p.Header.Length = p.Len()
if b, err = p.Header.MarshalBinary(); err != nil {
return
}
copy(data[n:], b)
n += len(b)
binary.BigEndian.PutUint32(data[n:], p.BufferId)
n += 4
binary.BigEndian.PutUint32(data[n:], p.InPort)
n += 4
binary.BigEndian.PutUint16(data[n:], p.ActionsLen)
n += 2
n += 6 // for pad
for _, a := range p.Actions {
if b, err = a.MarshalBinary(); err != nil {
return
}
copy(data[n:], b)
n += len(b)
}
if b, err = p.Data.MarshalBinary(); err != nil {
return
}
copy(data[n:], b)
n += len(b)
return
}
func (p *PacketOut) UnmarshalBinary(data []byte) error {
if err := p.Header.UnmarshalBinary(data); err != nil {
return err
}
n := p.Header.Len()
p.BufferId = binary.BigEndian.Uint32(data[n:])
n += 4
p.InPort = binary.BigEndian.Uint32(data[n:])
n += 4
p.ActionsLen = binary.BigEndian.Uint16(data[n:])
n += 2
n += 6 // for pad
for n < (n + p.ActionsLen) {
a, err := DecodeAction(data[n:])
if err != nil {
return err
}
p.Actions = append(p.Actions, a)
n += a.Len()
}
return p.Data.UnmarshalBinary(data[n:])
}
// ofp_packet_in 1.3
type PacketIn struct {
common.Header
BufferId uint32
TotalLen uint16
Reason uint8
TableId uint8
Cookie uint64
Match Match
pad []uint8
Data protocol.Ethernet
}
func NewPacketIn() *PacketIn {
p := new(PacketIn)
p.Header = NewOfp13Header()
p.Header.Type = Type_PacketIn
p.BufferId = 0xffffffff
p.Reason = 0
p.TableId = 0
p.Cookie = 0
p.Match = *NewMatch()
return p
}
func (p *PacketIn) Len() (n uint16) {
n += p.Header.Len()
n += 16
n += p.Match.Len()
n += 2
n += p.Data.Len()
return
}
func (p *PacketIn) MarshalBinary() (data []byte, err error) {
if data, err = p.Header.MarshalBinary(); err != nil {
return
}
b := make([]byte, 16)
n := 0
binary.BigEndian.PutUint32(b, p.BufferId)
n += 4
binary.BigEndian.PutUint16(b[n:], p.TotalLen)
n += 2
b[n] = p.Reason
n += 1
b[n] = p.TableId
n += 1
binary.BigEndian.PutUint64(b, p.Cookie)
n += 8
data = append(data, b...)
if b, err = p.Match.MarshalBinary(); err != nil {
return
}
data = append(data, b...)
b = make([]byte, 2)
copy(b[0:], p.pad)
data = append(data, b...)
if b, err = p.Data.MarshalBinary(); err != nil {
return
}
data = append(data, b...)
return
}
func (p *PacketIn) UnmarshalBinary(data []byte) error {
if err := p.Header.UnmarshalBinary(data); err != nil {
return err
}
n := p.Header.Len()
p.BufferId = binary.BigEndian.Uint32(data[n:])
n += 4
p.TotalLen = binary.BigEndian.Uint16(data[n:])
n += 2
p.Reason = data[n]
n += 1
p.TableId = data[n]
n += 1
p.Cookie = binary.BigEndian.Uint64(data[n:])
n += 8
if err := p.Match.UnmarshalBinary(data[n:]); err != nil {
return err
}
n += p.Match.Len()
copy(p.pad, data[n:])
n += 2
return p.Data.UnmarshalBinary(data[n:])
}
// ofp_packet_in_reason 1.3
const (
R_NO_MATCH = iota /* No matching flow (table-miss flow entry). */
R_ACTION /* Action explicitly output to controller. */
R_INVALID_TTL /* Packet has invalid TTL */
R_ACTION_SET /* Output to controller in action set */
R_GROUP /* Output to controller in group bucket */
R_PACKET_OUT /* Output to controller in packet-out */
)
func NewConfigRequest() *common.Header {
h := NewOfp13Header()
h.Type = Type_GetConfigRequest
return &h
}
// ofp_config_flags 1.3
const (
C_FRAG_NORMAL = 0
C_FRAG_DROP = 1
C_FRAG_REASM = 2
C_FRAG_MASK = 3
)
// ofp_switch_config 1.3
type SwitchConfig struct {
common.Header
Flags uint16 // OFPC_* flags
MissSendLen uint16
}
func NewSetConfig() *SwitchConfig {
c := new(SwitchConfig)
c.Header = NewOfp13Header()
c.Header.Type = Type_SetConfig
c.Flags = 0
c.MissSendLen = 0
return c
}
func (c *SwitchConfig) Len() (n uint16) {
n = c.Header.Len()
n += 4
return
}
func (c *SwitchConfig) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(c.Len()))
var bytes []byte
next := 0
c.Header.Length = c.Len()
if bytes, err = c.Header.MarshalBinary(); err != nil {
return
}
copy(data[next:], bytes)
next += len(bytes)
binary.BigEndian.PutUint16(data[next:], c.Flags)
next += 2
binary.BigEndian.PutUint16(data[next:], c.MissSendLen)
next += 2
return
}
func (c *SwitchConfig) UnmarshalBinary(data []byte) error {
var err error
next := 0
err = c.Header.UnmarshalBinary(data[next:])
next += int(c.Header.Len())
c.Flags = binary.BigEndian.Uint16(data[next:])
next += 2
c.MissSendLen = binary.BigEndian.Uint16(data[next:])
next += 2
return err
}
// BEGIN: ofp13 - 7.4.4
// ofp_error_msg 1.3
type ErrorMsg struct {
common.Header
Type uint16
Code uint16
Data util.Buffer
}
func NewErrorMsg() *ErrorMsg {
e := new(ErrorMsg)
e.Data = *util.NewBuffer(make([]byte, 0))
return e
}
func (e *ErrorMsg) Len() (n uint16) {
n = e.Header.Len()
n += 2
n += 2
n += e.Data.Len()
return
}
func (e *ErrorMsg) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(e.Len()))
var bytes []byte
next := 0
if bytes, err = e.Header.MarshalBinary(); err != nil {
return
}
copy(data[next:], bytes)
next += len(bytes)
binary.BigEndian.PutUint16(data[next:], e.Type)
next += 2
binary.BigEndian.PutUint16(data[next:], e.Code)
next += 2
if bytes, err = e.Data.MarshalBinary(); err != nil {
return
}
copy(data[next:], bytes)
next += len(bytes)
return
}
func (e *ErrorMsg) UnmarshalBinary(data []byte) error {
next := 0
e.Header.UnmarshalBinary(data[next:])
next += int(e.Header.Len())
e.Type = binary.BigEndian.Uint16(data[next:])
next += 2
e.Code = binary.BigEndian.Uint16(data[next:])
next += 2
e.Data.UnmarshalBinary(data[next:])
next += int(e.Data.Len())
return nil
}
// ofp_error_type 1.3
const (
ET_HELLO_FAILED = 0 /* Hello protocol failed. */
ET_BAD_REQUEST = 1 /* Request was not understood. */
ET_BAD_ACTION = 2 /* Error in action description. */
ET_BAD_INSTRUCTION = 3 /* Error in instruction list. */
PET_BAD_MATCH = 4 /* Error in match. */
ET_FLOW_MOD_FAILED = 5 /* Problem modifying flow entry. */
ET_GROUP_MOD_FAILED = 6 /* Problem modifying group entry. */
ET_PORT_MOD_FAILED = 7 /* Port mod request failed. */
ET_TABLE_MOD_FAILED = 8 /* Table mod request failed. */
ET_QUEUE_OP_FAILED = 9 /* Queue operation failed. */
ET_ROLE_REQUEST_FAILED = 11 /* Controller Role request failed. */
ET_METER_MOD_FAILED = 12 /* Error in meter. */
ET_TABLE_FEATURES_FAILED = 13 /* Setting table features failed. */
ET_EXPERIMENTER = 0xffff /* Experimenter error messages. */
)
// ofp_hello_failed_code 1.3
const (
HFC_INCOMPATIBLE = iota
HFC_EPERM
)
// ofp_bad_request_code 1.3
const (
BRC_BAD_VERSION = iota
BRC_BAD_TYPE
BRC_BAD_MULTIPART
BRC_BAD_EXPERIMENTER
BRC_BAD_EXP_TYPE
BRC_EPERM
BRC_BAD_LEN
BRC_BUFFER_EMPTY
BRC_BUFFER_UNKNOWN
BRC_BAD_TABLE_ID
BRC_IS_SLAVE
BRC_BAD_PORT
BRC_BAD_PACKET
BRC_MULTIPART_BUFFER_OVERFLOW
)
// ofp_bad_action_code 1.3
const (
BAC_BAD_TYPE = iota
BAC_BAD_LEN
BAC_BAD_EXPERIMENTER
BAC_BAD_EXP_TYPE
BAC_BAD_OUT_PORT
BAC_BAD_ARGUMENT
BAC_EPERM
BAC_TOO_MANY
BAC_BAD_QUEUE
BAC_BAD_OUT_GROUP
BAC_MATCH_INCONSISTENT
BAC_UNSUPPORTED_ORDER
BAC_BAD_TAG
BAC_BAD_SET_TYPE
BAC_BAD_SET_LEN
BAC_BAD_SET_ARGUMENT
)
// ofp_bad_instruction_code 1.3
const (
BIC_UNKNOWN_INST = 0 /* Unknown instruction. */
BIC_UNSUP_INST = 1 /* Switch or table does not support the instruction. */
BIC_BAD_TABLE_ID = 2 /* Invalid Table-ID specified. */
BIC_UNSUP_METADATA = 3 /* Metadata value unsupported by datapath. */
BIC_UNSUP_METADATA_MASK = 4 /* Metadata mask value unsupported by datapath. */
BIC_BAD_EXPERIMENTER = 5 /* Unknown experimenter id specified. */
BIC_BAD_EXP_TYPE = 6 /* Unknown instruction for experimenter id. */
BIC_BAD_LEN = 7 /* Length problem in instructions. */
BIC_EPERM = 8 /* Permissions error. */
)
// ofp_flow_mod_failed_code 1.3
const (
FMFC_UNKNOWN = 0 /* Unspecified error. */
FMFC_TABLE_FULL = 1 /* Flow not added because table was full. */
FMFC_BAD_TABLE_ID = 2 /* Table does not exist */
FMFC_OVERLAP = 3 /* Attempted to add overlapping flow with CHECK_OVERLAP flag set. */
FMFC_EPERM = 4 /* Permissions error. */
FMFC_BAD_TIMEOUT = 5 /* Flow not added because of unsupported idle/hard timeout. */
FMFC_BAD_COMMAND = 6 /* Unsupported or unknown command. */
FMFC_BAD_FLAGS = 7 /* Unsupported or unknown flags. */
)
// ofp_bad_match_code 1.3
const (
BMC_BAD_TYPE = 0 /* Unsupported match type specified by the match */
BMC_BAD_LEN = 1 /* Length problem in match. */
BMC_BAD_TAG = 2 /* Match uses an unsupported tag/encap. */
BMC_BAD_DL_ADDR_MASK = 3 /* Unsupported datalink addr mask - switch does not support arbitrary datalink address mask. */
BMC_BAD_NW_ADDR_MASK = 4 /* Unsupported network addr mask - switch does not support arbitrary network address mask. */
BMC_BAD_WILDCARDS = 5 /* Unsupported combination of fields masked or omitted in the match. */
BMC_BAD_FIELD = 6 /* Unsupported field type in the match. */
BMC_BAD_VALUE = 7 /* Unsupported value in a match field. */
BMC_BAD_MASK = 8 /* Unsupported mask specified in the match, field is not dl-address or nw-address. */
BMC_BAD_PREREQ = 9 /* A prerequisite was not met. */
BMC_DUP_FIELD = 10 /* A field type was duplicated. */
BMC_EPERM = 11 /* Permissions error. */
)
// ofp_group_mod_failed_code 1.3
const (
GMFC_GROUP_EXISTS = 0 /* Group not added because a group ADD attempted to replace an already-present group. */
GMFC_INVALID_GROUP = 1 /* Group not added because Group specified is invalid. */
GMFC_WEIGHT_UNSUPPORTED = 2 /* Switch does not support unequal load 105 ➞ 2013; The Open Networking Foundation OpenFlow Switch Specification Version 1.3.3 sharing with select groups. */
GMFC_OUT_OF_GROUPS = 3 /* The group table is full. */
GMFC_OUT_OF_BUCKETS = 4 /* The maximum number of action buckets for a group has been exceeded. */
GMFC_CHAINING_UNSUPPORTED = 5 /* Switch does not support groups that forward to groups. */
GMFC_WATCH_UNSUPPORTED = 6 /* This group cannot watch the watch_port or watch_group specified. */
GMFC_LOOP = 7 /* Group entry would cause a loop. */
GMFC_UNKNOWN_GROUP = 8 /* Group not modified because a group MODIFY attempted to modify a non-existent group. */
GMFC_CHAINED_GROUP = 9 /* Group not deleted because another group is forwarding to it. */
GMFC_BAD_TYPE = 10 /* Unsupported or unknown group type. */
GMFC_BAD_COMMAND = 11 /* Unsupported or unknown command. */
GMFC_BAD_BUCKET = 12 /* Error in bucket. */
GMFC_BAD_WATCH = 13 /* Error in watch port/group. */
GMFC_EPERM = 14 /* Permissions error. */
)
// ofp_port_mod_failed_code 1.0
const (
PMFC_BAD_PORT = iota
PMFC_BAD_HW_ADDR
PMFC_BAD_CONFIG
PMFC_BAD_ADVERTISE
PMFC_EPERM
)
// ofp_table_mod_failed_code
const (
TMFC_BAD_TABLE = 0 /* Specified table does not exist. */
TMFC_BAD_CONFIG = 1 /* Specified config is invalid. */
TMFC_EPERM = 2 /* Permissions error. */
)
// ofp_queue_op_failed_code 1.0
const (
QOFC_BAD_PORT = iota
QOFC_BAD_QUEUE
QOFC_EPERM
)
// END: ofp13 - 7.4.4
// END: ofp13 - 7.4
type SwitchFeatures struct {
common.Header
DPID net.HardwareAddr // Size 8
Buffers uint32
NumTables uint8
AuxilaryId uint8
pad []uint8 // Size 2
Capabilities uint32
Actions uint32
Ports []PhyPort
}
// FeaturesRequest constructor
func NewFeaturesRequest() *common.Header {
req := NewOfp13Header()
req.Type = Type_FeaturesRequest
return &req
}
// FeaturesReply constructor
func NewFeaturesReply() *SwitchFeatures {
res := new(SwitchFeatures)
res.Header = NewOfp13Header()
res.Header.Type = Type_FeaturesReply
res.DPID = make([]byte, 8)
res.pad = make([]byte, 2)
res.Ports = make([]PhyPort, 0)
return res
}
func (s *SwitchFeatures) Len() (n uint16) {
n = s.Header.Len()
n += uint16(len(s.DPID))
n += 16
for _, p := range s.Ports {
n += p.Len()
}
return
}
func (s *SwitchFeatures) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(s.Len()))
var bytes []byte
next := 0
s.Header.Length = s.Len()
if bytes, err = s.Header.MarshalBinary(); err != nil {
return
}
copy(data[next:], bytes)
next += len(bytes)
binary.BigEndian.PutUint32(data[next:], s.Buffers)
next += 4
data[next] = s.NumTables
next += 1
data[next] = s.AuxilaryId
next += 1
copy(data[next:], s.pad)
next += len(s.pad)
binary.BigEndian.PutUint32(data[next:], s.Capabilities)
next += 4
binary.BigEndian.PutUint32(data[next:], s.Actions)
next += 4
for _, p := range s.Ports {
if bytes, err = p.MarshalBinary(); err != nil {
return
}
copy(data[next:], bytes)
next += len(bytes)
}
return
}
func (s *SwitchFeatures) UnmarshalBinary(data []byte) error {
var err error
next := 0
err = s.Header.UnmarshalBinary(data[next:])
next = int(s.Header.Len())
copy(s.DPID, data[next:])
next += len(s.DPID)
s.Buffers = binary.BigEndian.Uint32(data[next:])
next += 4
s.NumTables = data[next]
next += 1
s.AuxilaryId = data[next]
next += 1
copy(s.pad, data[next:])
next += len(s.pad)
s.Capabilities = binary.BigEndian.Uint32(data[next:])
next += 4
s.Actions = binary.BigEndian.Uint32(data[next:])
next += 4
for next < len(data) {
p := NewPhyPort()
err = p.UnmarshalBinary(data[next:])
next += int(p.Len())
}
return err
}
// ofp_capabilities 1.3
const (
C_FLOW_STATS = 1 << 0
C_TABLE_STATS = 1 << 1
C_PORT_STATS = 1 << 2
C_GROUP_STATS = 1 << 3
C_RESERVED = 1 << 4
C_IP_REASM = 1 << 5
C_QUEUE_STATS = 1 << 6
C_PORT_BLOCKED = 1 << 8
)
// ofp_vendor 1.3
type VendorHeader struct {
Header common.Header /*Type OFPT_VENDOR*/
Vendor uint32
ExperimenterType uint32
VendorData util.Message
}
func (v *VendorHeader) Len() (n uint16) {
length := uint16(16)
if v.VendorData != nil {
length += v.VendorData.Len()
}
return length
}
func (v *VendorHeader) MarshalBinary() (data []byte, err error) {
v.Header.Length = v.Len()
data = make([]byte, v.Len())
b, err := v.Header.MarshalBinary()
n := 0
copy(data[n:], b)
n += len(b)
binary.BigEndian.PutUint32(data[n:], v.Vendor)
n += 4
binary.BigEndian.PutUint32(data[n:], v.ExperimenterType)
n += 4
if v.VendorData != nil {
vd, err := v.VendorData.MarshalBinary()
if err != nil {
return nil, err
}
copy(data[n:], vd)
n += len(vd)
}
return
}
func (v *VendorHeader) UnmarshalBinary(data []byte) error {
if len(data) < 16 {
return errors.New("The []byte the wrong size to unmarshal an " +
"VendorHeader message.")
}
v.Header.UnmarshalBinary(data)
n := int(v.Header.Len())
v.Vendor = binary.BigEndian.Uint32(data[n:])
n += 4
v.ExperimenterType = binary.BigEndian.Uint32(data[n:])
n += 4
if n < int(v.Header.Length) {
var err error
v.VendorData, err = decodeVendorData(v.ExperimenterType, data[n:v.Header.Length])
if err != nil {
return err
}
}
return nil
}
type Property interface {
Header() *PropHeader
util.Message
}
/* Generic OpenFlow property header, as used by various messages in OF1.3+, and
* especially in OF1.4.
*
* The OpenFlow specs prefer to define a new structure with a specialized name
* each time this property structure comes up: struct
* ofp_port_desc_prop_header, struct ofp_controller_status_prop_header, struct
* ofp_table_mod_prop_header, and more. They're all the same, so it's easier
* to unify them.
*/
type PropHeader struct {
Type uint16
Length uint16
}
func (p *PropHeader) Header() *PropHeader {
return p
}
func (p *PropHeader) Len() (n uint16) {
return 4
}
func (p *PropHeader) MarshalBinary() (data []byte, err error) {
data = make([]byte, p.Len())
binary.BigEndian.PutUint16(data[:2], p.Type)
binary.BigEndian.PutUint16(data[2:4], p.Length)
return
}
func (p *PropHeader) UnmarshalBinary(data []byte) error {
if len(data) < int(p.Len()) {
return errors.New("the []byte the wrong size to unmarshal an " +
"PropHeader message")
}
p.Type = binary.BigEndian.Uint16(data[:2])
p.Length = binary.BigEndian.Uint16(data[2:4])
return nil
}