forked from contiv/libOpenflow
/
nx_action.go
1926 lines (1707 loc) · 47.6 KB
/
nx_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
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 openflow13
import (
"encoding/binary"
"errors"
"net"
)
// NX Action constants
const (
NxExperimenterID = 0x00002320 // Vendor ID for Nicira extension messages
NxActionHeaderLength = 10 // Length of Nicira extension message header
)
// NX Action subtypes
const (
NXAST_RESUBMIT = 1 // Nicira extended action: resubmit(port)
NXAST_SET_TUNNEL = 2 // Nicira extended action: set_tunnel
NXAST_DROP_SPOOFED_ARP = 3 // Nicira extended action: drop spoofed arp packets
NXAST_SET_QUEUE = 4 // Nicira extended action: set_queue
NXAST_POP_QUEUE = 5 // Nicira extended action: pop_tunnel
NXAST_REG_MOVE = 6 // Nicira extended action: move:srcField[m1..n1]->dstField[m2..n2]
NXAST_REG_LOAD = 7 // Nicira extended action: load:data->dstField[m..n]
NXAST_NOTE = 8 // Nicira extended action: note
NXAST_SET_TUNNEL_V6 = 9 // Nicira extended action: set_tunnel64
NXAST_MULTIPATH = 10 // Nicira extended action: set_tunnel
NXAST_AUTOPATH = 11 // Nicira extended action: multipath
NXAST_BUNDLE = 12 // Nicira extended action: bundle
NXAST_BUNDLE_LOAD = 13 // Nicira extended action: bundle_load
NXAST_RESUBMIT_TABLE = 14 // Nicira extended action: resubmit(port, table)
NXAST_OUTPUT_REG = 15 // Nicira extended action: output:field
NXAST_LEARN = 16 // Nicira extended action: learn
NXAST_EXIT = 17 // Nicira extended action: exit
NXAST_DEC_TTL = 18 // Nicira extended action: dec_ttl
NXAST_FIN_TIMEOUT = 19 // Nicira extended action: fin_timeout
NXAST_CONTROLLER = 20 // Nicira extended action: controller(reason=xx,max_len=xx,id=xx)
NXAST_DEC_TTL_CNT_IDS = 21 // Nicira extended action: dec_ttl(id1,[id2]...)
NXAST_PUSH_MPLS = 23 // Nicira extended action: push_mpls
NXAST_POP_MPLS = 24 // Nicira extended action: pop_mpls
NXAST_SET_MPLS_TTL = 25 // Nicira extended action: set_mpls_ttl
NXAST_DEC_MPLS_TTL = 26 // Nicira extended action: dec_mpls_ttl
NXAST_STACK_PUSH = 27 // Nicira extended action: push:src
NXAST_STACK_POP = 28 // Nicira extended action: pop:dst
NXAST_SAMPLE = 29 // Nicira extended action: sample
NXAST_SET_MPLS_LABEL = 30 // Nicira extended action: set_mpls_label
NXAST_SET_MPLS_TC = 31 // Nicira extended action: set_mpls_tc
NXAST_OUTPUT_REG2 = 32 // Nicira extended action: output(port=port,max_len)
NXAST_REG_LOAD2 = 33 // Nicira extended action: load
NXAST_CONJUNCTION = 34 // Nicira extended action: conjunction
NXAST_CT = 35 // Nicira extended action: ct
NXAST_NAT = 36 // Nicira extended action: nat, need to be along with ct action
NXAST_CONTROLLER2 = 37 // Nicira extended action: controller(userdata=xxx,pause)
NXAST_SAMPLE2 = 38 // Nicira extended action: sample, support for exporting egress tunnel
NXAST_OUTPUT_TRUNC = 39 // Nicira extended action: truncate output action
NXAST_CT_CLEAR = 43 // Nicira extended action: ct_clear
NXAST_CT_RESUBMIT = 44 // Nicira extended action: resubmit to table in ct
NXAST_RAW_ENCAP = 46 // Nicira extended action: encap
NXAST_RAW_DECAP = 47 // Nicira extended action: decap
NXAST_DEC_NSH_TTL = 48 // Nicira extended action: dec_nsh_ttl
)
type NXActionHeader struct {
*ActionHeader
Vendor uint32
Subtype uint16
}
func (a *NXActionHeader) Header() *ActionHeader {
return a.ActionHeader
}
func (a *NXActionHeader) NXHeader() *NXActionHeader {
return a
}
func (a *NXActionHeader) Len() (n uint16) {
return NxActionHeaderLength
}
func (a *NXActionHeader) MarshalBinary() (data []byte, err error) {
data = make([]byte, a.Len())
var b []byte
n := 0
b, err = a.ActionHeader.MarshalBinary()
copy(data[n:], b)
n += len(b)
binary.BigEndian.PutUint32(data[n:], a.Vendor)
n += 4
binary.BigEndian.PutUint16(data[n:], a.Subtype)
return
}
func (a *NXActionHeader) UnmarshalBinary(data []byte) error {
if len(data) < int(NxActionHeaderLength) {
return errors.New("the []byte is too short to unmarshal a full NXActionHeader message")
}
a.ActionHeader = new(ActionHeader)
n := 0
err := a.ActionHeader.UnmarshalBinary(data[:4])
n += 4
a.Vendor = binary.BigEndian.Uint32(data[n:])
n += 4
a.Subtype = binary.BigEndian.Uint16(data[n:])
return err
}
func NewNxActionHeader(subtype uint16) *NXActionHeader {
actionHeader := &ActionHeader{Type: ActionType_Experimenter, Length: NxActionHeaderLength}
return &NXActionHeader{ActionHeader: actionHeader, Vendor: NxExperimenterID, Subtype: subtype}
}
func DecodeNxAction(data []byte) Action {
var a Action
// Previous 8 bytes in the data includes type(2 byte), length(2 byte), and vendor(4 byte)
subtype := binary.BigEndian.Uint16(data[8:])
switch subtype {
case NXAST_RESUBMIT:
a = new(NXActionResubmit)
case NXAST_SET_TUNNEL:
case NXAST_DROP_SPOOFED_ARP:
case NXAST_SET_QUEUE:
case NXAST_POP_QUEUE:
case NXAST_REG_MOVE:
a = new(NXActionRegMove)
case NXAST_REG_LOAD:
a = new(NXActionRegLoad)
case NXAST_NOTE:
a = new(NXActionNote)
case NXAST_SET_TUNNEL_V6:
case NXAST_MULTIPATH:
case NXAST_AUTOPATH:
case NXAST_BUNDLE:
case NXAST_BUNDLE_LOAD:
case NXAST_RESUBMIT_TABLE:
a = new(NXActionResubmitTable)
case NXAST_OUTPUT_REG:
a = new(NXActionOutputReg)
case NXAST_LEARN:
a = new(NXActionLearn)
case NXAST_EXIT:
case NXAST_DEC_TTL:
a = new(NXActionDecTTL)
case NXAST_FIN_TIMEOUT:
case NXAST_CONTROLLER:
a = new(NXActionController)
case NXAST_DEC_TTL_CNT_IDS:
a = new(NXActionDecTTLCntIDs)
case NXAST_PUSH_MPLS:
case NXAST_POP_MPLS:
case NXAST_SET_MPLS_TTL:
case NXAST_DEC_MPLS_TTL:
case NXAST_STACK_PUSH:
case NXAST_STACK_POP:
case NXAST_SAMPLE:
case NXAST_SET_MPLS_LABEL:
case NXAST_SET_MPLS_TC:
case NXAST_OUTPUT_REG2:
a = new(NXActionOutputReg)
case NXAST_REG_LOAD2:
a = new(NXActionRegLoad2)
case NXAST_CONJUNCTION:
a = new(NXActionConjunction)
case NXAST_CT:
a = new(NXActionConnTrack)
case NXAST_NAT:
a = new(NXActionCTNAT)
case NXAST_CONTROLLER2:
a = new(NXActionController2)
case NXAST_SAMPLE2:
case NXAST_OUTPUT_TRUNC:
case NXAST_CT_CLEAR:
case NXAST_CT_RESUBMIT:
a = new(NXActionResubmitTable)
a.(*NXActionResubmitTable).withCT = true
case NXAST_RAW_ENCAP:
case NXAST_RAW_DECAP:
case NXAST_DEC_NSH_TTL:
}
return a
}
// NXActionConjunction is NX action to configure conjunctive match flows.
type NXActionConjunction struct {
*NXActionHeader
Clause uint8
NClause uint8
ID uint32
}
// NewNXActionConjunction creates NXActionConjunction, the action in flow entry is like conjunction(ID, Clause/nclause).
func NewNXActionConjunction(clause uint8, nclause uint8, id uint32) *NXActionConjunction {
a := new(NXActionConjunction)
a.NXActionHeader = NewNxActionHeader(NXAST_CONJUNCTION)
a.Length = a.NXActionHeader.Len() + 6
a.Clause = clause
a.NClause = nclause
a.ID = id
return a
}
func (a *NXActionConjunction) Len() (n uint16) {
return a.Length
}
func (a *NXActionConjunction) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(a.Len()))
var b []byte
n := 0
b, err = a.NXActionHeader.MarshalBinary()
copy(data[n:], b)
n += len(b)
data[n] = a.Clause
n++
data[n] = a.NClause
n++
binary.BigEndian.PutUint32(data[n:], a.ID)
n += 4
return
}
func (a *NXActionConjunction) UnmarshalBinary(data []byte) error {
n := 0
a.NXActionHeader = new(NXActionHeader)
err := a.NXActionHeader.UnmarshalBinary(data[n:])
n += int(a.NXActionHeader.Len())
if len(data) < int(a.Len()) {
return errors.New("the []byte is too short to unmarshal a full NXActionConjunction message")
}
a.Clause = uint8(data[n])
n++
a.NClause = uint8(data[n])
n++
a.ID = binary.BigEndian.Uint32(data[n:])
return err
}
// NXActionConnTrack is NX action for conntrack.
type NXActionConnTrack struct {
*NXActionHeader
Flags uint16
ZoneSrc uint32
ZoneOfsNbits uint16
RecircTable uint8
pad []byte // 3bytes
Alg uint16
actions []Action
}
func (a *NXActionConnTrack) Len() (n uint16) {
return a.Length
}
func (a *NXActionConnTrack) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(a.Length))
var b []byte
n := 0
b, err = a.NXActionHeader.MarshalBinary()
copy(data[n:], b)
n += len(b)
binary.BigEndian.PutUint16(data[n:], a.Flags)
n += 2
binary.BigEndian.PutUint32(data[n:], a.ZoneSrc)
n += 4
binary.BigEndian.PutUint16(data[n:], a.ZoneOfsNbits)
n += 2
data[n] = a.RecircTable
n++
copy(data[n:], a.pad)
n += 3
binary.BigEndian.PutUint16(data[n:], a.Alg)
n += 2
// Marshal ct actions
for _, action := range a.actions {
actionBytes, err := action.MarshalBinary()
if err != nil {
return data, errors.New("failed to Marshal ct subActions")
}
copy(data[n:], actionBytes)
n += len(actionBytes)
}
return
}
func (a *NXActionConnTrack) UnmarshalBinary(data []byte) error {
n := 0
a.NXActionHeader = new(NXActionHeader)
err := a.NXActionHeader.UnmarshalBinary(data[n:])
n += int(a.NXActionHeader.Len())
if len(data) < int(a.Len()) {
return errors.New("the []byte is too short to unmarshal a full NXActionConnTrack message")
}
a.Flags = binary.BigEndian.Uint16(data[n:])
n += 2
a.ZoneSrc = binary.BigEndian.Uint32(data[n:])
n += 4
a.ZoneOfsNbits = binary.BigEndian.Uint16(data[n:])
n += 2
a.RecircTable = data[n]
n++
copy(a.pad, data[n:n+3])
n += 3
a.Alg = binary.BigEndian.Uint16(data[n:])
n += 2
for n < int(a.Len()) {
act, err := DecodeAction(data[n:])
if err != nil {
return errors.New("failed to decode actions")
}
a.actions = append(a.actions, act)
n += int(act.Len())
}
a.Length = uint16(n)
return err
}
func (a *NXActionConnTrack) Commit() *NXActionConnTrack {
a.Flags |= NX_CT_F_COMMIT
return a
}
func (a *NXActionConnTrack) Force() *NXActionConnTrack {
a.Flags |= NX_CT_F_FORCE
return a
}
func (a *NXActionConnTrack) Table(tableID uint8) *NXActionConnTrack {
a.RecircTable = tableID
return a
}
func (a *NXActionConnTrack) ZoneImm(zoneID uint16) *NXActionConnTrack {
a.ZoneSrc = 0
a.ZoneOfsNbits = zoneID
return a
}
func (a *NXActionConnTrack) ZoneRange(field *MatchField, rng *NXRange) *NXActionConnTrack {
a.ZoneSrc = field.MarshalHeader()
a.ZoneOfsNbits = rng.ToOfsBits()
return a
}
func (a *NXActionConnTrack) AddAction(actions ...Action) *NXActionConnTrack {
for _, act := range actions {
a.actions = append(a.actions, act)
a.Length += act.Len()
}
return a
}
func NewNXActionConnTrack() *NXActionConnTrack {
a := new(NXActionConnTrack)
a.NXActionHeader = NewNxActionHeader(NXAST_CT)
a.Length = a.NXActionHeader.Len() + 14
a.RecircTable = NX_CT_RECIRC_NONE
return a
}
// NXActionRegLoad is NX action to load data to a specified field.
type NXActionRegLoad struct {
*NXActionHeader
OfsNbits uint16
DstReg *MatchField
Value uint64
}
func NewNXActionRegLoad(ofsNbits uint16, dstField *MatchField, value uint64) *NXActionRegLoad {
a := new(NXActionRegLoad)
a.NXActionHeader = NewNxActionHeader(NXAST_REG_LOAD)
a.Length = a.NXActionHeader.Len() + 14
a.OfsNbits = ofsNbits
a.DstReg = dstField
a.Value = value
return a
}
func (a *NXActionRegLoad) Len() (n uint16) {
return a.Length
}
func (a *NXActionRegLoad) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(a.Len()))
var b []byte
n := 0
b, err = a.NXActionHeader.MarshalBinary()
copy(data[n:], b)
n += len(b)
binary.BigEndian.PutUint16(data[n:], a.OfsNbits)
n += 2
fieldHeaderData := a.DstReg.MarshalHeader()
binary.BigEndian.PutUint32(data[n:], fieldHeaderData)
n += 4
binary.BigEndian.PutUint64(data[n:], a.Value)
n += 8
return
}
func (a *NXActionRegLoad) UnmarshalBinary(data []byte) error {
n := 0
a.NXActionHeader = new(NXActionHeader)
if err := a.NXActionHeader.UnmarshalBinary(data[n:]); err != nil {
return err
}
n += int(a.NXActionHeader.Len())
if len(data) < int(a.Len()) {
return errors.New("the []byte is too short to unmarshal a full NXActionRegLoad message")
}
a.OfsNbits = binary.BigEndian.Uint16(data[n:])
n += 2
a.DstReg = new(MatchField)
if err := a.DstReg.UnmarshalHeader(data[n : n+4]); err != nil {
return err
}
n += 4
a.Value = binary.BigEndian.Uint64(data[n:])
return nil
}
// NXActionRegMove is NX action to move data from srcField to dstField.
type NXActionRegMove struct {
*NXActionHeader
Nbits uint16
SrcOfs uint16
DstOfs uint16
SrcField *MatchField
DstField *MatchField
}
func NewNXActionRegMove(nBits uint16, srcOfs uint16, dstOfs uint16, srcField *MatchField, dstField *MatchField) *NXActionRegMove {
a := new(NXActionRegMove)
a.NXActionHeader = NewNxActionHeader(NXAST_REG_MOVE)
a.Length = a.NXActionHeader.Len() + 14
a.Nbits = nBits
a.SrcOfs = srcOfs
a.DstOfs = dstOfs
a.SrcField = srcField
a.DstField = dstField
return a
}
func (a *NXActionRegMove) Len() (n uint16) {
return a.Length
}
func (a *NXActionRegMove) MarshalBinary() (data []byte, err error) {
data = make([]byte, a.Length)
var b []byte
n := 0
b, err = a.NXActionHeader.MarshalBinary()
copy(data[n:], b)
n += len(b)
binary.BigEndian.PutUint16(data[n:], a.Nbits)
n += 2
binary.BigEndian.PutUint16(data[n:], a.SrcOfs)
n += 2
binary.BigEndian.PutUint16(data[n:], a.DstOfs)
n += 2
srcFieldHeaderData := a.SrcField.MarshalHeader()
binary.BigEndian.PutUint32(data[n:], srcFieldHeaderData)
n += 4
dstFieldHeaderData := a.DstField.MarshalHeader()
binary.BigEndian.PutUint32(data[n:], dstFieldHeaderData)
n += 4
return
}
func (a *NXActionRegMove) UnmarshalBinary(data []byte) error {
n := 0
a.NXActionHeader = new(NXActionHeader)
if err := a.NXActionHeader.UnmarshalBinary(data[n:]); err != nil {
return err
}
n += int(a.NXActionHeader.Len())
if len(data) < int(a.Length) {
return errors.New("the []byte is too short to unmarshal a full NXActionRegMove message")
}
a.Nbits = binary.BigEndian.Uint16(data[n:])
n += 2
a.SrcOfs = binary.BigEndian.Uint16(data[n:])
n += 2
a.DstOfs = binary.BigEndian.Uint16(data[n:])
n += 2
a.SrcField = new(MatchField)
if err := a.SrcField.UnmarshalHeader(data[n:]); err != nil {
return err
}
n += 4
a.DstField = new(MatchField)
return a.DstField.UnmarshalHeader(data[n:])
}
// NXActionResubmit is NX action to resubmit packet to a specified in_port.
type NXActionResubmit struct {
*NXActionHeader
InPort uint16
TableID uint8
pad [3]byte // 3 bytes
}
func NewNXActionResubmit(inPort uint16) *NXActionResubmit {
a := new(NXActionResubmit)
a.NXActionHeader = NewNxActionHeader(NXAST_RESUBMIT)
a.Type = Type_Experimenter
a.Length = a.NXActionHeader.Len() + 6
a.InPort = inPort
a.pad = [3]byte{}
return a
}
func (a *NXActionResubmit) Len() (n uint16) {
return a.Length
}
func (a *NXActionResubmit) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(a.Len()))
var b []byte
n := 0
b, err = a.NXActionHeader.MarshalBinary()
copy(data[n:], b)
n += len(b)
binary.BigEndian.PutUint16(data[n:], a.InPort)
n += 2
a.TableID = OFPTT_ALL
n++
// Skip padding copy, move the index.
n += 3
return
}
func (a *NXActionResubmit) UnmarshalBinary(data []byte) error {
n := 0
a.NXActionHeader = new(NXActionHeader)
err := a.NXActionHeader.UnmarshalBinary(data[n:])
n += int(a.NXActionHeader.Len())
if len(data) < int(a.Len()) {
return errors.New("the []byte is too short to unmarshal a full NXActionConjunction message")
}
a.InPort = binary.BigEndian.Uint16(data[n:])
return err
}
// NXActionResubmitTable is NX action to resubmit packet to a specified table and in_port.
type NXActionResubmitTable struct {
*NXActionHeader
InPort uint16
TableID uint8
pad [3]byte // 3 bytes
withCT bool
}
func newNXActionResubmitTable() *NXActionResubmitTable {
a := &NXActionResubmitTable{
NXActionHeader: NewNxActionHeader(NXAST_RESUBMIT_TABLE),
withCT: false,
pad: [3]byte{},
}
a.Length = 16
return a
}
func newNXActionResubmitTableCT() *NXActionResubmitTable {
a := &NXActionResubmitTable{
NXActionHeader: NewNxActionHeader(NXAST_CT_RESUBMIT),
withCT: true,
pad: [3]byte{},
}
a.Length = 16
return a
}
func NewNXActionResubmitTableAction(inPort uint16, tableID uint8) *NXActionResubmitTable {
a := newNXActionResubmitTable()
a.InPort = inPort
a.TableID = tableID
return a
}
func (a *NXActionResubmitTable) IsCT() bool {
return a.withCT
}
func (a *NXActionResubmitTable) Len() (n uint16) {
return a.Length
}
func (a *NXActionResubmitTable) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(a.Len()))
var b []byte
n := 0
b, err = a.NXActionHeader.MarshalBinary()
copy(data[n:], b)
n += len(b)
binary.BigEndian.PutUint16(data[n:], a.InPort)
n += 2
data[n] = a.TableID
n++
// Skip padding copy, move the index.
n += 3
return
}
func (a *NXActionResubmitTable) UnmarshalBinary(data []byte) error {
n := 0
a.NXActionHeader = new(NXActionHeader)
err := a.NXActionHeader.UnmarshalBinary(data[n:])
n += int(a.NXActionHeader.Len())
if len(data) < int(a.Len()) {
return errors.New("the []byte is too short to unmarshal a full NXActionResubmitTable message")
}
a.InPort = binary.BigEndian.Uint16(data[n:])
n += 2
a.TableID = data[n]
n++
// Skip padding copy, move the index.
n += 3
return err
}
func NewNXActionResubmitTableCT(inPort uint16, tableID uint8) *NXActionResubmitTable {
a := newNXActionResubmitTableCT()
a.InPort = inPort
a.TableID = tableID
return a
}
func NewNXActionResubmitTableCTNoInPort(tableID uint8) *NXActionResubmitTable {
a := newNXActionResubmitTableCT()
a.InPort = OFPP_IN_PORT
a.TableID = tableID
return a
}
// NXActionCTNAT is NX action to set NAT in conntrack.
type NXActionCTNAT struct {
*NXActionHeader
pad []byte // 2 bytes
Flags uint16 // nat Flags to identify snat/dnat, and nat algorithms: random/protocolHash, connection persistent
rangePresent uint16 // mark if has set nat range, including ipv4/ipv6 range, port range
rangeIPv4Min net.IP
rangeIPv4Max net.IP
rangeIPv6Min net.IP
rangeIPv6Max net.IP
rangeProtoMin *uint16
rangeProtoMax *uint16
}
func NewNXActionCTNAT() *NXActionCTNAT {
a := new(NXActionCTNAT)
a.NXActionHeader = NewNxActionHeader(NXAST_NAT)
a.Length = 16
a.pad = make([]byte, 2)
return a
}
func (a *NXActionCTNAT) Len() (n uint16) {
a.Length = ((a.Length + 7) / 8) * 8
return a.Length
}
func (a *NXActionCTNAT) MarshalBinary() (data []byte, err error) {
data = make([]byte, a.Len())
var b []byte
n := 0
if b, err = a.NXActionHeader.MarshalBinary(); err != nil {
return
}
copy(data[n:], b)
n += len(b)
// Skip padding bytes
n += 2
binary.BigEndian.PutUint16(data[n:], a.Flags)
n += 2
binary.BigEndian.PutUint16(data[n:], a.rangePresent)
n += 2
if a.rangeIPv4Min != nil {
copy(data[n:], a.rangeIPv4Min.To4())
n += 4
}
if a.rangeIPv4Max != nil {
copy(data[n:], a.rangeIPv4Max.To4())
n += 4
}
if a.rangeIPv6Min != nil {
copy(data[n:], a.rangeIPv6Min.To16())
n += 16
}
if a.rangeIPv6Max != nil {
copy(data[n:], a.rangeIPv6Max.To16())
n += 16
}
if a.rangeProtoMin != nil {
binary.BigEndian.PutUint16(data[n:], *a.rangeProtoMin)
n += 2
}
if a.rangeProtoMin != nil {
binary.BigEndian.PutUint16(data[n:], *a.rangeProtoMax)
n += 2
}
return
}
func (a *NXActionCTNAT) SetSNAT() error {
if a.Flags&NX_NAT_F_DST != 0 {
return errors.New("the SNAT and DNAT actions should be mutually exclusive")
}
a.Flags |= NX_NAT_F_SRC
return nil
}
func (a *NXActionCTNAT) SetDNAT() error {
if a.Flags&NX_NAT_F_SRC != 0 {
return errors.New("the DNAT and SNAT actions should be mutually exclusive")
}
a.Flags |= NX_NAT_F_DST
return nil
}
func (a *NXActionCTNAT) SetProtoHash() error {
if a.Flags&NX_NAT_F_PROTO_RANDOM != 0 {
return errors.New("protocol hash and random should be mutually exclusive")
}
a.Flags |= NX_NAT_F_PROTO_HASH
return nil
}
func (a *NXActionCTNAT) SetRandom() error {
if a.Flags&NX_NAT_F_PROTO_HASH != 0 {
return errors.New("random and protocol hash should be mutually exclusive")
}
a.Flags |= NX_NAT_F_PROTO_RANDOM
return nil
}
func (a *NXActionCTNAT) SetPersistent() error {
a.Flags |= NX_NAT_F_PERSISTENT
return nil
}
func (a *NXActionCTNAT) SetRangeIPv4Min(ipMin net.IP) {
a.rangeIPv4Min = ipMin
a.rangePresent |= NX_NAT_RANGE_IPV4_MIN
a.Length += 4
}
func (a *NXActionCTNAT) SetRangeIPv4Max(ipMax net.IP) {
a.rangeIPv4Max = ipMax
a.rangePresent |= NX_NAT_RANGE_IPV4_MAX
a.Length += 4
}
func (a *NXActionCTNAT) SetRangeIPv6Min(ipMin net.IP) {
a.rangeIPv6Min = ipMin
a.rangePresent |= NX_NAT_RANGE_IPV6_MIN
a.Length += 16
}
func (a *NXActionCTNAT) SetRangeIPv6Max(ipMax net.IP) {
a.rangeIPv6Max = ipMax
a.rangePresent |= NX_NAT_RANGE_IPV6_MAX
a.Length += 16
}
func (a *NXActionCTNAT) SetRangeProtoMin(protoMin *uint16) {
a.rangeProtoMin = protoMin
a.rangePresent |= NX_NAT_RANGE_PROTO_MIN
a.Length += 2
}
func (a *NXActionCTNAT) SetRangeProtoMax(protoMax *uint16) {
a.rangeProtoMax = protoMax
a.rangePresent |= NX_NAT_RANGE_PROTO_MAX
a.Length += 2
}
func (a *NXActionCTNAT) UnmarshalBinary(data []byte) error {
n := 0
a.NXActionHeader = new(NXActionHeader)
err := a.NXActionHeader.UnmarshalBinary(data[n:])
n += int(a.NXActionHeader.Len())
if len(data) < int(a.Len()) {
return errors.New("the []byte is too short to unmarshal a full NXActionCTNAT message")
}
// Skip padding bytes
n += 2
a.Flags = binary.BigEndian.Uint16(data[n:])
n += 2
a.rangePresent = binary.BigEndian.Uint16(data[n:])
n += 2
if a.rangePresent&NX_NAT_RANGE_IPV4_MIN != 0 {
a.rangeIPv4Min = net.IPv4(data[n], data[n+1], data[n+2], data[n+3])
n += 4
}
if a.rangePresent&NX_NAT_RANGE_IPV4_MAX != 0 {
a.rangeIPv4Max = net.IPv4(data[n], data[n+1], data[n+2], data[n+3])
n += 4
}
if a.rangePresent&NX_NAT_RANGE_IPV6_MIN != 0 {
a.rangeIPv6Min = make([]byte, 16)
copy(a.rangeIPv6Min, data[n:n+16])
n += 16
}
if a.rangePresent&NX_NAT_RANGE_IPV6_MAX != 0 {
a.rangeIPv6Max = make([]byte, 16)
copy(a.rangeIPv6Max, data[n:n+16])
n += 16
}
if a.rangePresent&NX_NAT_RANGE_PROTO_MIN != 0 {
portMin := binary.BigEndian.Uint16(data[n:])
a.rangeProtoMin = &portMin
n += 2
}
if a.rangePresent&NX_NAT_RANGE_PROTO_MAX != 0 {
portMax := binary.BigEndian.Uint16(data[n:])
a.rangeProtoMax = &portMax
n += 2
}
return err
}
// NXActionOutputReg is NX action to output to a field with a specified range.
type NXActionOutputReg struct {
*NXActionHeader
OfsNbits uint16 // (ofs << 6 | (Nbits -1)
SrcField *MatchField // source nxm_nx_reg
MaxLen uint16 // Max length to send to controller if chosen port is OFPP_CONTROLLER
zero [6]uint8 // 6 uint8 with all Value as 0, reserved
}
func (a *NXActionOutputReg) Len() (n uint16) {
return a.Length
}
func (a *NXActionOutputReg) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(a.Len()))
var b []byte
n := 0
b, err = a.NXActionHeader.MarshalBinary()
copy(data[n:], b)
n += len(b)
binary.BigEndian.PutUint16(data[n:], a.OfsNbits)
n += 2
fieldHeaderData := a.SrcField.MarshalHeader()
binary.BigEndian.PutUint32(data[n:], fieldHeaderData)
n += 4
binary.BigEndian.PutUint16(data[n:], a.MaxLen)
n += 2
copy(data[n:], a.zero[0:])
n += 6
return
}
func (a *NXActionOutputReg) UnmarshalBinary(data []byte) error {
n := 0
a.NXActionHeader = new(NXActionHeader)
if err := a.NXActionHeader.UnmarshalBinary(data[n:]); err != nil {
return err
}
n += int(a.NXActionHeader.Len())
if len(data) < int(a.Len()) {
return errors.New("the []byte is too short to unmarshal a full NXActionOutputReg message")
}
a.OfsNbits = binary.BigEndian.Uint16(data[n:])
n += 2
a.SrcField = new(MatchField)
if err := a.SrcField.UnmarshalHeader(data[n : n+4]); err != nil {
return err
}
n += 4
a.MaxLen = binary.BigEndian.Uint16(data[n:])
return nil
}
func newNXActionOutputReg() *NXActionOutputReg {
a := &NXActionOutputReg{
NXActionHeader: NewNxActionHeader(NXAST_OUTPUT_REG),
zero: [6]uint8{},
}
a.Length = 24
return a
}
func NewOutputFromField(srcField *MatchField, ofsNbits uint16) *NXActionOutputReg {
a := newNXActionOutputReg()
a.SrcField = srcField
a.OfsNbits = ofsNbits
a.MaxLen = uint16(0xffff)
return a
}
func NewOutputFromFieldWithMaxLen(srcField *MatchField, ofsNbits uint16, maxLen uint16) *NXActionOutputReg {
a := newNXActionOutputReg()
a.SrcField = srcField
a.OfsNbits = ofsNbits
a.MaxLen = maxLen
return a
}
type NXActionDecTTL struct {
*NXActionHeader
controllers uint16 // number of controller
zeros [4]uint8 // 4 byte with zeros
}
func (a *NXActionDecTTL) Len() (n uint16) {
return a.Length
}
func (a *NXActionDecTTL) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(a.Len()))
var b []byte
n := 0
b, err = a.NXActionHeader.MarshalBinary()
copy(data[n:], b)
n += len(b)
binary.BigEndian.PutUint16(data[n:], a.controllers)
n += 2
copy(data[n:], a.zeros[0:])
return
}
func (a *NXActionDecTTL) UnmarshalBinary(data []byte) error {
n := 0
a.NXActionHeader = new(NXActionHeader)
err := a.NXActionHeader.UnmarshalBinary(data[n:])
n += int(a.NXActionHeader.Len())
if len(data) < int(a.Len()) {
return errors.New("the []byte is too short to unmarshal a full NXActionDecTTL message")
}
a.controllers = binary.BigEndian.Uint16(data[n:])
n += 2
a.zeros = [4]uint8{}
return err
}
func NewNXActionDecTTL() *NXActionDecTTL {
a := &NXActionDecTTL{
NXActionHeader: NewNxActionHeader(NXAST_DEC_TTL),
zeros: [4]uint8{},
}
a.Length = 16
return a
}
type NXActionDecTTLCntIDs struct {
*NXActionHeader
controllers uint16 // number of controller
zeros [4]uint8 // 4 byte with zeros
cntIDs []uint16 // controller IDs
}
func (a *NXActionDecTTLCntIDs) Len() (n uint16) {
return a.Length
}
func (a *NXActionDecTTLCntIDs) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(a.Len()))
var b []byte
n := 0
b, err = a.NXActionHeader.MarshalBinary()
copy(data[n:], b)
n += len(b)
binary.BigEndian.PutUint16(data[n:], a.controllers)
n += 2
copy(data[n:], a.zeros[0:])
n += 4
for _, id := range a.cntIDs {
binary.BigEndian.PutUint16(data[n:], id)
n += 2
}
return
}
func (a *NXActionDecTTLCntIDs) UnmarshalBinary(data []byte) error {
n := 0
a.NXActionHeader = new(NXActionHeader)
err := a.NXActionHeader.UnmarshalBinary(data[n:])
n += int(a.NXActionHeader.Len())
if len(data) < int(a.Len()) {
return errors.New("the []byte is too short to unmarshal a full NXActionDecTTLCntIDs message")