forked from contiv/libOpenflow
/
multipart.go
3599 lines (3167 loc) · 78.6 KB
/
multipart.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
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package openflow15
import (
"encoding/binary"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"antrea.io/libOpenflow/common"
"antrea.io/libOpenflow/util"
)
// ofp_multipart_request
type MultipartRequest struct {
common.Header
Type uint16
Flags uint16
pad []byte // 4 bytes
Body []util.Message
}
func (s *MultipartRequest) Len() (n uint16) {
n = s.Header.Len() + 8
for _, body := range s.Body {
n += body.Len()
}
return
}
func (s *MultipartRequest) MarshalBinary() (data []byte, err error) {
s.Header.Length = s.Len()
if data, err = s.Header.MarshalBinary(); err != nil {
return
}
b := make([]byte, 8)
n := 0
binary.BigEndian.PutUint16(b[n:], s.Type)
n += 2
binary.BigEndian.PutUint16(b[n:], s.Flags)
n += 2
n += 4 // for padding
data = append(data, b...)
for _, body := range s.Body {
b, err = body.MarshalBinary()
if err != nil {
return
}
data = append(data, b...)
}
log.Debugf("Sending MultipartRequest (%d): %v", len(data), data)
return
}
func (s *MultipartRequest) UnmarshalBinary(data []byte) error {
err := s.Header.UnmarshalBinary(data)
if err != nil {
return err
}
n := s.Header.Len()
s.Type = binary.BigEndian.Uint16(data[n:])
n += 2
s.Flags = binary.BigEndian.Uint16(data[n:])
n += 2
n += 4 // for padding
for n < s.Header.Length {
var req util.Message
switch s.Type {
case MultipartType_Desc:
// The request body is empty.
case MultipartType_FlowDesc:
// The request body is struct ofp_flow_stats_request.
req = NewFlowStatsRequest()
case MultipartType_AggregateStats:
// The request body is struct ofp_aggregate_stats_request.
req = new(AggregateStatsRequest)
case MultipartType_TableStats:
// The request body is empty.
case MultipartType_Port:
// The request body is struct ofp_port_multipart_request.
req = new(PortMultipartRequst)
case MultipartType_QueueStats:
// The request body is struct ofp_queue_multipart_request.
req = new(QueueMultipartRequest)
case MultipartType_GroupStats:
// The request body is struct ofp_group_multipart_request.
req = new(GroupMultipartRequest)
case MultipartType_GroupDesc:
// The request body is struct ofp_group_multipart_request.
req = new(GroupMultipartRequest)
case MultipartType_GroupFeatures:
// The request body is empty.
case MultipartType_MeterStats:
// The request body is struct ofp_meter_multipart_request.
req = new(MeterMultipartRequest)
case MultipartType_MeterDesc:
// The request body is struct ofp_meter_multipart_request.
req = new(MeterMultipartRequest)
case MultipartType_MeterFeatures:
// The request body is empty.
case MultipartType_TableFeatures:
// The request body is either empty or contains an array of
// struct ofp_table_features containing the controller’s
// desired view of the switch. If the switch is unable to
// set the specified view an error is returned.
req = new(TableFeatures)
case MultipartType_PortDesc:
// The request body is struct ofp_port_multipart_request.
req = new(PortMultipartRequest)
case MultipartType_TableDesc:
// The request body is empty.
case MultipartType_QueueDesc:
// The request body is struct ofp_queue_multipart_request.
req = new(QueueMultipartRequest)
case MultipartType_FlowMonitor:
// The request body is an array of struct ofp_flow_monitor_request.
req = new(FlowMonitorRequest)
case MultipartType_FlowStats:
// The request body is struct ofp_flow_stats_request.
req = NewFlowStatsRequest()
case MultipartType_ControllerStatus:
// The request body is empty.
case MultipartType_BundleFeatures:
// The request body is ofp_bundle_features_request.
req = new(BundleFeaturesRequest)
case MultipartType_Experimenter:
}
if req != nil {
err = req.UnmarshalBinary(data[n:])
if err != nil {
return err
}
n += req.Len()
s.Body = append(s.Body, req)
}
}
return nil
}
// ofp_multipart_reply
type MultipartReply struct {
common.Header
Type uint16
Flags uint16
pad []byte // 4 bytes
Body []util.Message
}
func (s *MultipartReply) Len() (n uint16) {
n = s.Header.Len()
n += 8
for _, r := range s.Body {
n += uint16(r.Len())
}
return
}
func (s *MultipartReply) MarshalBinary() (data []byte, err error) {
s.Header.Length = s.Len()
data, err = s.Header.MarshalBinary()
b := make([]byte, 8)
n := 0
binary.BigEndian.PutUint16(b[n:], s.Type)
n += 2
binary.BigEndian.PutUint16(b[n:], s.Flags)
n += 2
n += 4 // for padding
data = append(data, b...)
for _, r := range s.Body {
b, err = r.MarshalBinary()
data = append(data, b...)
}
return
}
func (s *MultipartReply) UnmarshalBinary(data []byte) error {
err := s.Header.UnmarshalBinary(data)
if err != nil {
return err
}
n := s.Header.Len()
s.Type = binary.BigEndian.Uint16(data[n:])
n += 2
s.Flags = binary.BigEndian.Uint16(data[n:])
n += 2
n += 4 // for padding
var req []util.Message
for n < s.Header.Length {
var repl util.Message
switch s.Type {
case MultipartType_Desc:
// The reply body is struct ofp_desc.
repl = NewDescStats()
case MultipartType_FlowDesc:
// The reply body is an array of struct ofp_flow_desc.
repl = NewFlowDesc()
case MultipartType_AggregateStats:
// The reply body is struct ofp_aggregate_stats_reply.
repl = new(AggregateStatsReply)
case MultipartType_TableStats:
// The reply body is an array of struct ofp_table_stats.
repl = new(TableStats)
case MultipartType_Port:
// The reply body is an array of struct ofp_port_stats.
repl = new(PortStats)
case MultipartType_QueueStats:
// The reply body is an array of struct ofp_queue_stats.
repl = new(QueueStats)
case MultipartType_GroupStats:
// The reply is an array of struct ofp_group_stats.
repl = new(GroupStats)
case MultipartType_GroupDesc:
// The reply body is an array of struct ofp_group_desc.
repl = new(GroupDesc)
case MultipartType_GroupFeatures:
// The reply body is struct ofp_group_features.
repl = NewGroupFeatures()
case MultipartType_MeterStats:
// The reply body is an array of struct ofp_meter_stats.
repl = new(MeterStats)
case MultipartType_MeterDesc:
// The reply body is an array of struct ofp_meter_desc.
repl = new(MeterDesc)
case MultipartType_MeterFeatures:
// The reply body is struct ofp_meter_features.
repl = new(MeterFeatures)
case MultipartType_TableFeatures:
// The reply body is an array of struct ofp_table_features.
repl = new(TableFeatures)
case MultipartType_PortDesc:
// The reply body is an array of struct ofp_port.
repl = NewPort(0)
case MultipartType_TableDesc:
// The reply body is an array of struct ofp_table_desc.
repl = new(TableDesc)
case MultipartType_QueueDesc:
// The reply body is an array of struct ofp_queue_desc.
repl = new(QueueDesc)
case MultipartType_FlowMonitor:
// The reply body is an array of struct ofp_flow_update_header.
// switch on event
switch binary.BigEndian.Uint16(data[n+2:]) {
case FME_INITIAL:
repl = NewFlowUpdateFull(FME_INITIAL)
case FME_ADDED:
repl = NewFlowUpdateFull(FME_ADDED)
case FME_REMOVED:
repl = NewFlowUpdateFull(FME_REMOVED)
case FME_MODIFIED:
repl = NewFlowUpdateFull(FME_MODIFIED)
case FME_ABBREV:
repl = NewFlowUpdateAbbrev()
case FME_PAUSED:
repl = NewFlowUpdatePaused(FME_PAUSED)
case FME_RESUMED:
repl = NewFlowUpdatePaused(FME_RESUMED)
default:
return fmt.Errorf("Unknown Event type %d", binary.BigEndian.Uint16(data[n+2:]))
}
case MultipartType_FlowStats:
// The reply body is an array of struct ofp_flow_stats.
repl = NewFlowStats()
case MultipartType_ControllerStatus:
// The reply body is an array of struct ofp_controller_status.
repl = NewControllerStatus()
case MultipartType_BundleFeatures:
// The reply body is struct ofp_bundle_features.
repl = NewBundleFeatures()
case MultipartType_Experimenter:
break
}
err = repl.UnmarshalBinary(data[n:])
if err != nil {
log.Printf("Error parsing stats reply")
return err
}
if repl == nil {
return fmt.Errorf("reply structure is nil in MultipartReply UnmarshalBinary")
}
n += repl.Len()
req = append(req, repl)
}
s.Body = req
return err
}
// ofp_multipart_request_flags & ofp_multipart_reply_flags
const (
OFPMPF_REQ_MORE = 1 << 0 /* More requests to follow. */
OFPMPF_REPLY_MORE = 1 << 0 /* More replies to follow. */
)
// _stats_types
const (
/* Description of this OpenFlow switch.
* The request body is empty.
* The reply body is struct ofp_desc_stats. */
MultipartType_Desc = iota
/* Individual flow statistics.
* The request body is struct ofp_flow_stats_request.
* The reply body is an array of struct ofp_flow_stats. */
MultipartType_FlowDesc = 1
/* Aggregate flow statistics.
* The request body is struct ofp_aggregate_stats_request.
* The reply body is struct ofp_aggregate_stats_reply. */
MultipartType_AggregateStats = 2
/* Flow table statistics.
* The request body is empty.
* The reply body is an array of struct ofp_table_stats. */
MultipartType_TableStats = 3
/* Port statistics.
* The request body is struct ofp_port_stats_request.
* The reply body is an array of struct ofp_port_stats. */
MultipartType_Port = 4
/* Queue statistics for a port
* The request body is struct _queue_stats_request.
* The reply body is an array of struct ofp_queue_stats */
MultipartType_QueueStats = 5
/* Group counter statistics.
* The request body is struct ofp_group_stats_request.
* The reply is an array of struct ofp_group_stats. */
MultipartType_GroupStats = 6
/* Group description.
* The request body is empty.
* The reply body is an array of struct ofp_group_desc. */
MultipartType_GroupDesc = 7
/* Group features.
* The request body is empty.
* The reply body is struct ofp_group_features. */
MultipartType_GroupFeatures = 8
/* Meter statistics.
* The request body is struct ofp_meter_multipart_requests.
* The reply body is an array of struct ofp_meter_stats. */
MultipartType_MeterStats = 9
/* Meter configuration.
* The request body is struct ofp_meter_multipart_request.
* The reply body is an array of struct ofp_meter_desc. */
MultipartType_MeterDesc = 10
/* Meter features.
* The request body is empty.
* The reply body is struct ofp_meter_features. */
MultipartType_MeterFeatures = 11
/* Table features.
* The request body is either empty or contains an array of
* struct ofp_table_features containing the controller’s
* desired view of the switch. If the switch is unable to
* set the specified view an error is returned.
* The reply body is an array of struct ofp_table_features. */
MultipartType_TableFeatures = 12
/* Port description.
* The request body is struct ofp_port_multipart_request.
* The reply body is an array of struct ofp_port. */
MultipartType_PortDesc = 13
/* Table description.
* The request body is empty.
* The reply body is an array of struct ofp_table_desc. */
MultipartType_TableDesc = 14
/* Queue description.
* The request body is struct ofp_queue_multipart_request.
* The reply body is an array of struct ofp_queue_desc. */
MultipartType_QueueDesc = 15
/* Flow monitors. Reply may be an asynchronous message.
* The request body is an array of struct ofp_flow_monitor_request.
* The reply body is an array of struct ofp_flow_update_header. */
MultipartType_FlowMonitor = 16
/* Individual flow statistics (without description).
* The request body is struct ofp_flow_stats_request.
* The reply body is an array of struct ofp_flow_stats. */
MultipartType_FlowStats = 17
/* Controller status.
* The request body is empty.
* The reply body is an array of struct ofp_controller_status. */
MultipartType_ControllerStatus = 18
/* Bundle features.
* The request body is ofp_bundle_features_request.
* The reply body is struct ofp_bundle_features. */
MultipartType_BundleFeatures = 19
/* Experimenter extension.
* The request and reply bodies begin with
* struct ofp_experimenter_multipart_header.
* The request and reply bodies are otherwise experimenter-defined. */
MultipartType_Experimenter = 0xffff
)
func NewMpRequest(mpType uint16) *MultipartRequest {
m := new(MultipartRequest)
m.Header = NewOfp15Header()
m.Header.Type = Type_MultiPartRequest
m.Type = mpType
m.pad = make([]byte, 4)
m.Body = make([]util.Message, 0)
return m
}
func NewMpReply(mpType uint16) *MultipartReply {
m := new(MultipartReply)
m.Header = NewOfp15Header()
m.Header.Type = Type_MultiPartReply
m.Type = mpType
m.pad = make([]byte, 4)
m.Body = make([]util.Message, 0)
return m
}
// ofp_desc_stats 1.5
type DescStats struct {
MfrDesc []byte // Size DESC_STR_LEN
HWDesc []byte // Size DESC_STR_LEN
SWDesc []byte // Size DESC_STR_LEN
SerialNum []byte // Size SERIAL_NUM_LEN
DPDesc []byte // Size DESC_STR_LEN
}
func NewDescStats() *DescStats {
s := new(DescStats)
s.MfrDesc = make([]byte, DESC_STR_LEN)
s.HWDesc = make([]byte, DESC_STR_LEN)
s.SWDesc = make([]byte, DESC_STR_LEN)
s.SerialNum = make([]byte, SERIAL_NUM_LEN)
s.DPDesc = make([]byte, DESC_STR_LEN)
return s
}
func (s *DescStats) Len() (n uint16) {
return uint16(DESC_STR_LEN*4 + SERIAL_NUM_LEN)
}
func (s *DescStats) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(s.Len()))
n := 0
copy(data[n:], s.MfrDesc)
n += len(s.MfrDesc)
copy(data[n:], s.HWDesc)
n += len(s.HWDesc)
copy(data[n:], s.SWDesc)
n += len(s.SWDesc)
copy(data[n:], s.SerialNum)
n += len(s.SerialNum)
copy(data[n:], s.DPDesc)
n += len(s.DPDesc)
return
}
func (s *DescStats) UnmarshalBinary(data []byte) error {
n := 0
copy(s.MfrDesc, data[n:])
n += len(s.MfrDesc)
copy(s.HWDesc, data[n:])
n += len(s.HWDesc)
copy(s.SWDesc, data[n:])
n += len(s.SWDesc)
copy(s.SerialNum, data[n:])
n += len(s.SerialNum)
copy(s.DPDesc, data[n:])
n += len(s.DPDesc)
return nil
}
const (
DESC_STR_LEN = 256
SERIAL_NUM_LEN = 32
)
const (
OFPTT_MAX = 0xfe
/* Fake tables. */
OFPTT_ALL = 0xff /* Wildcard table used for table config, flow stats and flow deletes. */
)
// ofp_flow_stats_request
type FlowStatsRequest struct {
TableId uint8
pad []byte // 3 bytes
OutPort uint32
OutGroup uint32
pad2 []byte // 4 bytes
Cookie uint64
CookieMask uint64
Match Match
}
func NewFlowStatsRequest() *FlowStatsRequest {
s := new(FlowStatsRequest)
s.OutPort = P_ANY
s.OutGroup = OFPG_ANY
s.pad = make([]byte, 3)
s.pad2 = make([]byte, 4)
s.Match = *NewMatch()
return s
}
func (s *FlowStatsRequest) Len() (n uint16) {
return s.Match.Len() + 32
}
func (s *FlowStatsRequest) MarshalBinary() (data []byte, err error) {
data = make([]byte, 32)
n := 0
data[n] = s.TableId
n += 1
copy(data[n:], s.pad)
n += 3
binary.BigEndian.PutUint32(data[n:], s.OutPort)
n += 4
binary.BigEndian.PutUint32(data[n:], s.OutGroup)
n += 4
copy(data[n:], s.pad2)
n += 4
binary.BigEndian.PutUint64(data[n:], s.Cookie)
n += 8
binary.BigEndian.PutUint64(data[n:], s.CookieMask)
n += 8
b, err := s.Match.MarshalBinary()
data = append(data, b...)
return
}
func (s *FlowStatsRequest) UnmarshalBinary(data []byte) error {
n := 0
s.TableId = data[n]
n += 1
copy(s.pad, data[n:n+3])
n += 3
s.OutPort = binary.BigEndian.Uint32(data[n:])
n += 4
s.OutGroup = binary.BigEndian.Uint32(data[n:])
n += 4
copy(s.pad2, data[n:n+4])
n += 4
s.Cookie = binary.BigEndian.Uint64(data[n:])
n += 8
s.CookieMask = binary.BigEndian.Uint64(data[n:])
n += 8
err := s.Match.UnmarshalBinary(data[n:])
n += int(s.Match.Len())
return err
}
// ofp_flow_stats
type FlowStats struct {
Length uint16
Pad2 []byte // 2 bytes
TableId uint8
Reason uint8
Priority uint16
Match
Stats []Stats
}
// ofp_flow_stats_reason
const (
FSR_STATS_REQUEST = 0 /* Reply to a OFPMP_FLOW_STATS request. */
FSR_STAT_TRIGGER = 1 /* Status generated by OFPIT_STAT_TRIGGER. */
)
func NewFlowStats() *FlowStats {
f := new(FlowStats)
f.Match = *NewMatch()
f.Pad2 = make([]byte, 2)
return f
}
func (s *FlowStats) Len() (n uint16) {
n = 8 + s.Match.Len()
for _, stat := range s.Stats {
n += stat.Len()
}
return
}
func (s *FlowStats) MarshalBinary() (data []byte, err error) {
data = make([]byte, 8)
n := 0
s.Length = s.Len()
binary.BigEndian.PutUint16(data[n:], s.Length)
n += 2
n += 2 // Pad2
data[n] = s.TableId
n += 1
data[n] = s.Reason
n += 1
binary.BigEndian.PutUint16(data[n:], s.Priority)
n += 2
b, err := s.Match.MarshalBinary()
data = append(data, b...)
n += len(b)
for _, stat := range s.Stats {
b, err = stat.MarshalBinary()
data = append(data, b...)
n += len(b)
}
return
}
func (s *FlowStats) UnmarshalBinary(data []byte) error {
var n uint16
s.Length = binary.BigEndian.Uint16(data[n:])
n += 2
n += 2 // Pad2
s.TableId = data[n]
n += 1
s.Reason = data[n]
n += 1
s.Priority = binary.BigEndian.Uint16(data[n:])
n += 2
err := s.Match.UnmarshalBinary(data[n:])
if err != nil {
return err
}
n += s.Match.Len()
for n < s.Length {
stat := new(Stats)
err = stat.UnmarshalBinary(data[n:])
if err != nil {
return err
}
s.Stats = append(s.Stats, *stat)
n += stat.Len()
}
return err
}
// ofp_aggregate_stats_request
type AggregateStatsRequest struct {
TableId uint8
pad []byte // 3 bytes
OutPort uint32
OutGroup uint32
pad2 []byte // 4 bytes
Cookie uint64
CookieMask uint64
Match
}
func NewAggregateStatsRequest() *AggregateStatsRequest {
a := new(AggregateStatsRequest)
a.pad = make([]byte, 3)
a.pad2 = make([]byte, 4)
a.OutPort = P_ANY
a.OutGroup = OFPG_ANY
a.Match = *NewMatch()
return a
}
func (s *AggregateStatsRequest) Len() (n uint16) {
return s.Match.Len() + 32
}
func (s *AggregateStatsRequest) MarshalBinary() (data []byte, err error) {
data = make([]byte, 32)
n := 0
data[n] = s.TableId
n += 1
copy(data[n:], s.pad)
n += 3
binary.BigEndian.PutUint32(data[n:], s.OutPort)
n += 4
binary.BigEndian.PutUint32(data[n:], s.OutGroup)
n += 4
copy(data[n:], s.pad2)
n += 4
binary.BigEndian.PutUint64(data[n:], s.Cookie)
n += 8
binary.BigEndian.PutUint64(data[n:], s.CookieMask)
n += 8
b, err := s.Match.MarshalBinary()
data = append(data, b...)
return
}
func (s *AggregateStatsRequest) UnmarshalBinary(data []byte) error {
n := 0
s.TableId = data[n]
n += 1
copy(s.pad, data[n:n+3])
n += 3
s.OutPort = binary.BigEndian.Uint32(data[n:])
n += 4
s.OutGroup = binary.BigEndian.Uint32(data[n:])
n += 4
copy(s.pad2, data[n:n+4])
n += 4
s.Cookie = binary.BigEndian.Uint64(data[n:])
n += 8
s.CookieMask = binary.BigEndian.Uint64(data[n:])
n += 8
s.Match.UnmarshalBinary(data[n:])
n += int(s.Match.Len())
return nil
}
// ofp_aggregate_stats_reply
type AggregateStatsReply struct {
Stats
}
func NewAggregateStatsReply() *AggregateStatsReply {
n := new(AggregateStatsReply)
return n
}
func (a *AggregateStatsReply) Len() uint16 {
return a.Stats.Len()
}
func (s *AggregateStatsReply) MarshalBinary() (data []byte, err error) {
return s.Stats.MarshalBinary()
}
func (s *AggregateStatsReply) UnmarshalBinary(data []byte) error {
return s.Stats.UnmarshalBinary(data)
}
// ofp_aggregate_stats_reply
type AggregateStats struct {
PacketCount uint64
ByteCount uint64
FlowCount uint32
pad []uint8 // Size 4
}
func NewAggregateStats() *AggregateStats {
s := new(AggregateStats)
s.pad = make([]byte, 4)
return s
}
func (s *AggregateStats) Len() (n uint16) {
return 24
}
func (s *AggregateStats) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(s.Len()))
n := 0
binary.BigEndian.PutUint64(data[n:], s.PacketCount)
n += 8
binary.BigEndian.PutUint64(data[n:], s.ByteCount)
n += 8
binary.BigEndian.PutUint32(data[n:], s.FlowCount)
n += 4
copy(data[n:], s.pad)
n += 4
return
}
func (s *AggregateStats) UnmarshalBinary(data []byte) error {
n := 0
s.PacketCount = binary.BigEndian.Uint64(data[n:])
n += 8
s.ByteCount = binary.BigEndian.Uint64(data[n:])
n += 8
s.FlowCount = binary.BigEndian.Uint32(data[n:])
n += 4
copy(s.pad, data[n:])
return nil
}
// ofp_table_stats
type TableStats struct {
TableId uint8
pad []uint8 // Size 3
ActiveCount uint32
LookupCount uint64
MatchedCount uint64
}
func NewTableStats() *TableStats {
s := new(TableStats)
s.pad = make([]byte, 3)
return s
}
func (s *TableStats) Len() (n uint16) {
return 24
}
func (s *TableStats) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(s.Len()))
n := 0
data[n] = s.TableId
n += 1
n += 3 // Pad
binary.BigEndian.PutUint32(data[n:], s.ActiveCount)
n += 4
binary.BigEndian.PutUint64(data[n:], s.LookupCount)
n += 8
binary.BigEndian.PutUint64(data[n:], s.MatchedCount)
n += 8
return
}
func (s *TableStats) UnmarshalBinary(data []byte) error {
n := 0
s.TableId = data[0]
n += 1
n += 3
s.ActiveCount = binary.BigEndian.Uint32(data[n:])
n += 4
s.LookupCount = binary.BigEndian.Uint64(data[n:])
n += 8
s.MatchedCount = binary.BigEndian.Uint64(data[n:])
n += 8
return nil
}
const (
MAX_TABLE_NAME_LEN = 32
)
// ofp_port_multipart_request
type PortMultipartRequst struct {
PortNo uint32
pad []uint8 // Size 4
}
func NewPortStatsRequest(port uint32) *PortMultipartRequst {
p := new(PortMultipartRequst)
p.pad = make([]byte, 4)
p.PortNo = port
return p
}
func (s *PortMultipartRequst) Len() (n uint16) {
return 8
}
func (s *PortMultipartRequst) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(s.Len()))
binary.BigEndian.PutUint32(data, s.PortNo)
return
}
func (s *PortMultipartRequst) UnmarshalBinary(data []byte) error {
s.PortNo = binary.BigEndian.Uint32(data)
return nil
}
// ofp_port_stats
type PortStats struct {
Length uint16
pad []byte // Size 2
PortNo uint32
DurationSec uint32
DurationNSec uint32
RxPackets uint64
TxPackets uint64
RxBytes uint64
TxBytes uint64
RxDropped uint64
TxDropped uint64
RxErrors uint64
TxErrors uint64
Properties []util.Message
}
func NewPortStats(port uint32) *PortStats {
p := new(PortStats)
p.pad = make([]byte, 2)
p.PortNo = port
return p
}
func (s *PortStats) Len() (n uint16) {
n = 80
for _, p := range s.Properties {
n += p.Len()
}
return
}
func (s *PortStats) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(s.Len()))
var n uint16
s.Length = s.Len()
binary.BigEndian.PutUint16(data[n:], s.Length)
n += 2
n += 2 // Pad
binary.BigEndian.PutUint32(data[n:], s.PortNo)
n += 4
binary.BigEndian.PutUint32(data[n:], s.DurationSec)
n += 4
binary.BigEndian.PutUint32(data[n:], s.DurationNSec)
n += 4
binary.BigEndian.PutUint64(data[n:], s.RxPackets)
n += 8
binary.BigEndian.PutUint64(data[n:], s.TxPackets)
n += 8
binary.BigEndian.PutUint64(data[n:], s.RxBytes)
n += 8
binary.BigEndian.PutUint64(data[n:], s.TxBytes)
n += 8
binary.BigEndian.PutUint64(data[n:], s.RxDropped)
n += 8
binary.BigEndian.PutUint64(data[n:], s.TxDropped)
n += 8
binary.BigEndian.PutUint64(data[n:], s.RxErrors)
n += 8
binary.BigEndian.PutUint64(data[n:], s.TxErrors)
n += 8
for _, p := range s.Properties {
var b []byte
b, err = p.MarshalBinary()
if err != nil {
return
}
copy(data[n:], b)
n += p.Len()
}
return
}
func (s *PortStats) UnmarshalBinary(data []byte) (err error) {
var n uint16
s.Length = binary.BigEndian.Uint16(data[n:])
n += 2
n += 2 // Pad
s.PortNo = binary.BigEndian.Uint32(data[n:])
n += 4
s.DurationSec = binary.BigEndian.Uint32(data[n:])
n += 4
s.DurationNSec = binary.BigEndian.Uint32(data[n:])
n += 4
s.RxPackets = binary.BigEndian.Uint64(data[n:])
n += 8
s.TxPackets = binary.BigEndian.Uint64(data[n:])
n += 8
s.RxBytes = binary.BigEndian.Uint64(data[n:])
n += 8
s.TxBytes = binary.BigEndian.Uint64(data[n:])
n += 8
s.RxDropped = binary.BigEndian.Uint64(data[n:])
n += 8
s.TxDropped = binary.BigEndian.Uint64(data[n:])
n += 8
s.RxErrors = binary.BigEndian.Uint64(data[n:])
n += 8
s.TxErrors = binary.BigEndian.Uint64(data[n:])
n += 8
for n < s.Length {
var p util.Message
switch binary.BigEndian.Uint16(data[n:]) {
case PSPT_ETHERNET:
p = new(PortStatsPropEthernet)
case PSPT_OPTICAL:
p = new(PortStatsPropOptical)
case PSPT_EXPERIMENTER:
p = new(PropExperimenter)
default:
err = errors.New("An unknown property type was received")
return
}
err = p.UnmarshalBinary(data[n:])
if err != nil {