forked from contiv/libOpenflow
/
match.go
1765 lines (1523 loc) · 42.7 KB
/
match.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"
"fmt"
"log"
"net"
"antrea.io/libOpenflow/util"
)
// ofp_match 1.3
type Match struct {
Type uint16
Length uint16
Fields []MatchField
}
// One match field TLV
type MatchField struct {
Class uint16
Field uint8
HasMask bool
Length uint8
ExperimenterID uint32
Value util.Message
Mask util.Message
}
func NewMatch() *Match {
m := new(Match)
m.Type = MatchType_OXM
m.Length = 4
m.Fields = make([]MatchField, 0)
return m
}
func (m *Match) Len() (n uint16) {
n = 4
for _, a := range m.Fields {
n += a.Len()
}
// Round it to closest multiple of 8
n = ((n + 7) / 8) * 8
return
}
func (m *Match) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(m.Len()))
n := 0
binary.BigEndian.PutUint16(data[n:], m.Type)
n += 2
binary.BigEndian.PutUint16(data[n:], m.Length)
n += 2
for _, a := range m.Fields {
b, err := a.MarshalBinary()
if err != nil {
return nil, err
}
copy(data[n:], b)
n += len(b)
}
/* See if we need to pad it to make it align to 64bit boundary
if ((n % 8) != 0) {
toPad := 8 - (n % 8)
b := make([]byte, toPad)
data = append(data, b...)
}
*/
return
}
func (m *Match) UnmarshalBinary(data []byte) error {
n := 0
m.Type = binary.BigEndian.Uint16(data[n:])
n += 2
m.Length = binary.BigEndian.Uint16(data[n:])
n += 2
for n < int(m.Length) {
field := new(MatchField)
if err := field.UnmarshalBinary(data[n:]); err != nil {
return err
}
m.Fields = append(m.Fields, *field)
n += int(field.Len())
}
return nil
}
func (m *Match) AddField(f MatchField) {
m.Fields = append(m.Fields, f)
m.Length += f.Len()
}
func (m *MatchField) Len() (n uint16) {
n = 4
if m.ExperimenterID != 0 {
n += 4
}
n += m.Value.Len()
if m.HasMask {
n += m.Mask.Len()
}
return
}
func (m *MatchField) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(m.Len()))
n := 0
binary.BigEndian.PutUint16(data[n:], m.Class)
n += 2
var fld uint8
if m.HasMask {
fld = (m.Field << 1) | 0x1
} else {
fld = m.Field << 1
}
data[n] = fld
n += 1
data[n] = m.Length
n += 1
b, err := m.Value.MarshalBinary()
copy(data[n:], b)
n += len(b)
if m.HasMask {
b, err = m.Mask.MarshalBinary()
copy(data[n:], b)
n += len(b)
}
return
}
func (m *MatchField) UnmarshalBinary(data []byte) error {
var n uint16
var err error
m.Class = binary.BigEndian.Uint16(data[n:])
n += 2
fld := data[n]
n += 1
if (fld & 0x1) == 1 {
m.HasMask = true
} else {
m.HasMask = false
}
m.Field = fld >> 1
m.Length = data[n]
n += 1
if m.Class == OXM_CLASS_EXPERIMENTER {
experimenterID := binary.BigEndian.Uint32(data[n:])
if experimenterID == ONF_EXPERIMENTER_ID {
n += 4
m.ExperimenterID = experimenterID
} else {
return fmt.Errorf("Unsupported experimenter id: %d in class: %d ", experimenterID, m.Class)
}
}
if m.Value, err = DecodeMatchField(m.Class, m.Field, m.Length, m.HasMask, data[n:]); err != nil {
return err
}
n += m.Value.Len()
if m.HasMask {
if m.Mask, err = DecodeMatchField(m.Class, m.Field, m.Length, m.HasMask, data[n:]); err != nil {
return err
}
n += m.Mask.Len()
}
return err
}
func (m *MatchField) MarshalHeader() uint32 {
var maskData uint32
if m.HasMask {
maskData = 1 << 8
} else {
maskData = 0 << 8
}
return uint32(m.Class)<<16 | uint32(m.Field)<<9 | maskData | uint32(m.Length)
}
func (m *MatchField) UnmarshalHeader(data []byte) error {
var err error
if len(data) < int(4) {
err = fmt.Errorf("the []byte is too short to unmarshal MatchField header")
return err
}
n := 0
m.Class = binary.BigEndian.Uint16(data[n:])
n += 2
fieldWithMask := data[n]
m.HasMask = fieldWithMask&1 == 1
m.Field = fieldWithMask >> 1
n += 1
m.Length = data[n] & 0xff
return err
}
func DecodeMatchField(class uint16, field uint8, length uint8, hasMask bool, data []byte) (util.Message, error) {
if class == OXM_CLASS_OPENFLOW_BASIC {
var val util.Message
val = nil
switch field {
case OXM_FIELD_IN_PORT:
val = new(InPortField)
case OXM_FIELD_IN_PHY_PORT:
case OXM_FIELD_METADATA:
val = new(MetadataField)
case OXM_FIELD_ETH_DST:
val = new(EthDstField)
case OXM_FIELD_ETH_SRC:
val = new(EthSrcField)
case OXM_FIELD_ETH_TYPE:
val = new(EthTypeField)
case OXM_FIELD_VLAN_VID:
val = new(VlanIdField)
case OXM_FIELD_VLAN_PCP:
case OXM_FIELD_IP_DSCP:
val = new(IpDscpField)
case OXM_FIELD_IP_ECN:
case OXM_FIELD_IP_PROTO:
val = new(IpProtoField)
case OXM_FIELD_IPV4_SRC:
val = new(Ipv4SrcField)
case OXM_FIELD_IPV4_DST:
val = new(Ipv4DstField)
case OXM_FIELD_TCP_SRC:
val = new(PortField)
case OXM_FIELD_TCP_DST:
val = new(PortField)
case OXM_FIELD_UDP_SRC:
val = new(PortField)
case OXM_FIELD_UDP_DST:
val = new(PortField)
case OXM_FIELD_SCTP_SRC:
val = new(PortField)
case OXM_FIELD_SCTP_DST:
val = new(PortField)
case OXM_FIELD_ICMPV4_TYPE:
val = new(IcmpTypeField)
case OXM_FIELD_ICMPV4_CODE:
val = new(IcmpCodeField)
case OXM_FIELD_ARP_OP:
val = new(ArpOperField)
case OXM_FIELD_ARP_SPA:
val = new(ArpXPaField)
case OXM_FIELD_ARP_TPA:
val = new(ArpXPaField)
case OXM_FIELD_ARP_SHA:
val = new(ArpXHaField)
case OXM_FIELD_ARP_THA:
val = new(ArpXHaField)
case OXM_FIELD_IPV6_SRC:
val = new(Ipv6SrcField)
case OXM_FIELD_IPV6_DST:
val = new(Ipv6DstField)
case OXM_FIELD_IPV6_FLABEL:
case OXM_FIELD_ICMPV6_TYPE:
val = new(IcmpTypeField)
case OXM_FIELD_ICMPV6_CODE:
val = new(IcmpCodeField)
case OXM_FIELD_IPV6_ND_TARGET:
val = new(Ipv6DstField)
case OXM_FIELD_IPV6_ND_SLL:
val = new(EthSrcField)
case OXM_FIELD_IPV6_ND_TLL:
val = new(EthDstField)
case OXM_FIELD_MPLS_LABEL:
val = new(MplsLabelField)
case OXM_FIELD_MPLS_TC:
val = new(MplsTcField)
case OXM_FIELD_MPLS_BOS:
val = new(MplsBosField)
case OXM_FIELD_PBB_ISID:
case OXM_FIELD_TUNNEL_ID:
val = new(TunnelIdField)
case OXM_FIELD_IPV6_EXTHDR:
case OXM_FIELD_TCP_FLAGS:
val = new(TcpFlagsField)
default:
log.Printf("Unhandled Field: %d in Class: %d", field, class)
}
if val == nil {
log.Printf("Bad pkt class: %v field: %v data: %v", class, field, data)
return nil, fmt.Errorf("Bad pkt class: %v field: %v data: %v", class, field, data)
}
err := val.UnmarshalBinary(data)
if err != nil {
return nil, err
}
return val, nil
} else if class == OXM_CLASS_NXM_1 {
var val util.Message
switch field {
case NXM_NX_REG0:
val = new(Uint32Message)
case NXM_NX_REG1:
val = new(Uint32Message)
case NXM_NX_REG2:
val = new(Uint32Message)
case NXM_NX_REG3:
val = new(Uint32Message)
case NXM_NX_REG4:
val = new(Uint32Message)
case NXM_NX_REG5:
val = new(Uint32Message)
case NXM_NX_REG6:
val = new(Uint32Message)
case NXM_NX_REG7:
val = new(Uint32Message)
case NXM_NX_REG8:
val = new(Uint32Message)
case NXM_NX_REG9:
val = new(Uint32Message)
case NXM_NX_REG10:
val = new(Uint32Message)
case NXM_NX_REG11:
val = new(Uint32Message)
case NXM_NX_REG12:
val = new(Uint32Message)
case NXM_NX_REG13:
val = new(Uint32Message)
case NXM_NX_REG14:
val = new(Uint32Message)
case NXM_NX_REG15:
val = new(Uint32Message)
case NXM_NX_TUN_ID:
case NXM_NX_ARP_SHA:
val = new(ArpXHaField)
case NXM_NX_ARP_THA:
val = new(ArpXHaField)
case NXM_NX_IPV6_SRC:
val = new(Ipv6SrcField)
case NXM_NX_IPV6_DST:
val = new(Ipv6DstField)
case NXM_NX_ICMPV6_TYPE:
val = new(IcmpTypeField)
case NXM_NX_ICMPV6_CODE:
val = new(IcmpCodeField)
case NXM_NX_ND_TARGET:
val = new(Ipv6DstField)
case NXM_NX_ND_SLL:
val = new(EthDstField)
case NXM_NX_ND_TLL:
val = new(EthSrcField)
case NXM_NX_IP_FRAG:
case NXM_NX_IPV6_LABEL:
case NXM_NX_IP_ECN:
case NXM_NX_IP_TTL:
val = new(TtlField)
case NXM_NX_MPLS_TTL:
case NXM_NX_TUN_IPV4_SRC:
val = new(TunnelIpv4SrcField)
case NXM_NX_TUN_IPV4_DST:
val = new(TunnelIpv4DstField)
case NXM_NX_PKT_MARK:
val = new(Uint32Message)
case NXM_NX_TCP_FLAGS:
case NXM_NX_DP_HASH:
case NXM_NX_RECIRC_ID:
case NXM_NX_CONJ_ID:
val = new(Uint32Message)
case NXM_NX_TUN_GBP_ID:
case NXM_NX_TUN_GBP_FLAGS:
case NXM_NX_TUN_METADATA0:
fallthrough
case NXM_NX_TUN_METADATA1:
fallthrough
case NXM_NX_TUN_METADATA2:
fallthrough
case NXM_NX_TUN_METADATA3:
fallthrough
case NXM_NX_TUN_METADATA4:
fallthrough
case NXM_NX_TUN_METADATA5:
fallthrough
case NXM_NX_TUN_METADATA6:
fallthrough
case NXM_NX_TUN_METADATA7:
msg := new(ByteArrayField)
if !hasMask {
msg.Length = length
} else {
msg.Length = length / 2
}
val = msg
case NXM_NX_TUN_FLAGS:
case NXM_NX_CT_STATE:
val = new(Uint32Message)
case NXM_NX_CT_ZONE:
val = new(Uint16Message)
case NXM_NX_CT_MARK:
val = new(Uint32Message)
case NXM_NX_CT_LABEL:
val = new(CTLabel)
case NXM_NX_TUN_IPV6_SRC:
val = new(Ipv6SrcField)
case NXM_NX_TUN_IPV6_DST:
val = new(Ipv6DstField)
case NXM_NX_CT_NW_PROTO:
val = new(IpProtoField)
case NXM_NX_CT_NW_SRC:
val = new(Ipv4SrcField)
case NXM_NX_CT_NW_DST:
val = new(Ipv4DstField)
case NXM_NX_CT_IPV6_SRC:
val = new(Ipv6SrcField)
case NXM_NX_CT_IPV6_DST:
val = new(Ipv6DstField)
case NXM_NX_CT_TP_DST:
val = new(PortField)
case NXM_NX_CT_TP_SRC:
val = new(PortField)
case NXM_NX_XXREG0:
fallthrough
case NXM_NX_XXREG1:
fallthrough
case NXM_NX_XXREG2:
fallthrough
case NXM_NX_XXREG3:
msg := new(ByteArrayField)
if !hasMask {
msg.Length = length
} else {
msg.Length = length / 2
}
val = msg
default:
log.Printf("Unhandled Field: %d in Class: %d", field, class)
return nil, fmt.Errorf("Bad pkt class: %v field: %v data: %v", class, field, data)
}
err := val.UnmarshalBinary(data)
if err != nil {
return nil, err
}
return val, nil
} else if class == OXM_CLASS_EXPERIMENTER {
var val util.Message
switch field {
case OXM_FIELD_TCP_FLAGS:
val = new(TcpFlagsField)
case OXM_FIELD_ACTSET_OUTPUT:
val = new(ActsetOutputField)
}
err := val.UnmarshalBinary(data)
if err != nil {
return nil, err
}
return val, nil
} else {
log.Panicf("Unsupported match field: %d in class: %d", field, class)
}
return nil, nil
}
// ofp_match_type 1.3
const (
MatchType_Standard = iota /* Deprecated. */
MatchType_OXM
)
// ofp_oxm_class 1.3
const (
OXM_CLASS_NXM_0 = 0x0000 /* Backward compatibility with NXM */
OXM_CLASS_NXM_1 = 0x0001 /* Backward compatibility with NXM */
OXM_CLASS_OPENFLOW_BASIC = 0x8000 /* Basic class for OpenFlow */
OXM_CLASS_EXPERIMENTER = 0xFFFF /* Experimenter class */
ONF_EXPERIMENTER_ID = 0x4f4e4600 /* ONF Experimenter ID */
)
const (
OXM_FIELD_IN_PORT = 0 /* Switch input port. */
OXM_FIELD_IN_PHY_PORT = 1 /* Switch physical input port. */
OXM_FIELD_METADATA = 2 /* Metadata passed between tables. */
OXM_FIELD_ETH_DST = 3 /* Ethernet destination address. */
OXM_FIELD_ETH_SRC = 4 /* Ethernet source address. */
OXM_FIELD_ETH_TYPE = 5 /* Ethernet frame type. */
OXM_FIELD_VLAN_VID = 6 /* VLAN id. */
OXM_FIELD_VLAN_PCP = 7 /* VLAN priority. */
OXM_FIELD_IP_DSCP = 8 /* IP DSCP (6 bits in ToS field). */
OXM_FIELD_IP_ECN = 9 /* IP ECN (2 bits in ToS field). */
OXM_FIELD_IP_PROTO = 10 /* IP protocol. */
OXM_FIELD_IPV4_SRC = 11 /* IPv4 source address. */
OXM_FIELD_IPV4_DST = 12 /* IPv4 destination address. */
OXM_FIELD_TCP_SRC = 13 /* TCP source port. */
OXM_FIELD_TCP_DST = 14 /* TCP destination port. */
OXM_FIELD_UDP_SRC = 15 /* UDP source port. */
OXM_FIELD_UDP_DST = 16 /* UDP destination port. */
OXM_FIELD_SCTP_SRC = 17 /* SCTP source port. */
OXM_FIELD_SCTP_DST = 18 /* SCTP destination port. */
OXM_FIELD_ICMPV4_TYPE = 19 /* ICMP type. */
OXM_FIELD_ICMPV4_CODE = 20 /* ICMP code. */
OXM_FIELD_ARP_OP = 21 /* ARP opcode. */
OXM_FIELD_ARP_SPA = 22 /* ARP source IPv4 address. */
OXM_FIELD_ARP_TPA = 23 /* ARP target IPv4 address. */
OXM_FIELD_ARP_SHA = 24 /* ARP source hardware address. */
OXM_FIELD_ARP_THA = 25 /* ARP target hardware address. */
OXM_FIELD_IPV6_SRC = 26 /* IPv6 source address. */
OXM_FIELD_IPV6_DST = 27 /* IPv6 destination address. */
OXM_FIELD_IPV6_FLABEL = 28 /* IPv6 Flow Label */
OXM_FIELD_ICMPV6_TYPE = 29 /* ICMPv6 type. */
OXM_FIELD_ICMPV6_CODE = 30 /* ICMPv6 code. */
OXM_FIELD_IPV6_ND_TARGET = 31 /* Target address for ND. */
OXM_FIELD_IPV6_ND_SLL = 32 /* Source link-layer for ND. */
OXM_FIELD_IPV6_ND_TLL = 33 /* Target link-layer for ND. */
OXM_FIELD_MPLS_LABEL = 34 /* MPLS label. */
OXM_FIELD_MPLS_TC = 35 /* MPLS TC. */
OXM_FIELD_MPLS_BOS = 36 /* MPLS BoS bit. */
OXM_FIELD_PBB_ISID = 37 /* PBB I-SID. */
OXM_FIELD_TUNNEL_ID = 38 /* Logical Port Metadata. */
OXM_FIELD_IPV6_EXTHDR = 39 /* IPv6 Extension Header pseudo-field */
OXM_FIELD_PBB_UCA = 41 /* PBB UCA header field (from OpenFlow 1.4) */
OXM_FIELD_TCP_FLAGS = 42 /* TCP flags (from OpenFlow 1.5) */
OXM_FIELD_ACTSET_OUTPUT = 43 /* actset output port number (from OpenFlow 1.5) */
)
const (
NXM_NX_REG0 = 0 /* nicira extension: reg0 */
NXM_NX_REG1 = 1 /* nicira extension: reg1 */
NXM_NX_REG2 = 2 /* nicira extension: reg2 */
NXM_NX_REG3 = 3 /* nicira extension: reg3 */
NXM_NX_REG4 = 4 /* nicira extension: reg4 */
NXM_NX_REG5 = 5 /* nicira extension: reg5 */
NXM_NX_REG6 = 6 /* nicira extension: reg6 */
NXM_NX_REG7 = 7 /* nicira extension: reg7 */
NXM_NX_REG8 = 8 /* nicira extension: reg8 */
NXM_NX_REG9 = 9 /* nicira extension: reg9 */
NXM_NX_REG10 = 10 /* nicira extension: reg10 */
NXM_NX_REG11 = 11 /* nicira extension: reg11 */
NXM_NX_REG12 = 12 /* nicira extension: reg12 */
NXM_NX_REG13 = 13 /* nicira extension: reg13 */
NXM_NX_REG14 = 14 /* nicira extension: reg14 */
NXM_NX_REG15 = 15 /* nicira extension: reg15 */
NXM_NX_TUN_ID = 16 /* nicira extension: tun_id, VNI */
NXM_NX_ARP_SHA = 17 /* nicira extension: arp_sha, ARP Source Ethernet Address */
NXM_NX_ARP_THA = 18 /* nicira extension: arp_tha, ARP Target Ethernet Address */
NXM_NX_IPV6_SRC = 19 /* nicira extension: tun_ipv6_src, IPv6 source address */
NXM_NX_IPV6_DST = 20 /* nicira extension: tun_ipv6_src, IPv6 destination address */
NXM_NX_ICMPV6_TYPE = 21 /* nicira extension: icmpv6_type, ICMPv6 type */
NXM_NX_ICMPV6_CODE = 22 /* nicira extension: icmpv6_code, ICMPv6 code */
NXM_NX_ND_TARGET = 23 /* nicira extension: nd_target, ICMPv6 neighbor discovery source ethernet address*/
NXM_NX_ND_SLL = 24 /* nicira extension: nd_sll, ICMPv6 neighbor discovery source ethernet address*/
NXM_NX_ND_TLL = 25 /* nicira extension: nd_tll, ICMPv6 neighbor discovery target ethernet address */
NXM_NX_IP_FRAG = 26 /* nicira extension: ip_frag, IP fragments */
NXM_NX_IPV6_LABEL = 27 /* nicira extension: ipv6_label, least 20 bits hold flow label from IPv6 header, others are zero*/
NXM_NX_IP_ECN = 28 /* nicira extension: nw_ecn, TOS byte with DSCP bits cleared to 0 */
NXM_NX_IP_TTL = 29 /* nicira extension: nw_ttl, time-to-live field */
NXM_NX_MPLS_TTL = 30 /* nicira extension: mpls_ttl, time-to-live field from MPLS label */
NXM_NX_TUN_IPV4_SRC = 31 /* nicira extension: tun_src, src IPv4 address of tunnel */
NXM_NX_TUN_IPV4_DST = 32 /* nicira extension: tun_dst, dst IPv4 address of tunnel */
NXM_NX_PKT_MARK = 33 /* nicira extension: pkg_mark, packet mark from Linux kernal */
NXM_NX_TCP_FLAGS = 34 /* nicira extension: tcp_flags */
NXM_NX_DP_HASH = 35
NXM_NX_RECIRC_ID = 36 /* nicira extension: recirc_id, used with ct */
NXM_NX_CONJ_ID = 37 /* nicira extension: conj_id, conjunction ID for conjunctive match */
NXM_NX_TUN_GBP_ID = 38 /* nicira extension: tun_gbp_id, GBP policy ID */
NXM_NX_TUN_GBP_FLAGS = 39 /* nicira extension: tun_gbp_flags, GBP policy Flags*/
NXM_NX_TUN_METADATA0 = 40 /* nicira extension: tun_metadata, for Geneve header variable data */
NXM_NX_TUN_METADATA1 = 41 /* nicira extension: tun_metadata, for Geneve header variable data */
NXM_NX_TUN_METADATA2 = 42 /* nicira extension: tun_metadata, for Geneve header variable data */
NXM_NX_TUN_METADATA3 = 43 /* nicira extension: tun_metadata, for Geneve header variable data */
NXM_NX_TUN_METADATA4 = 44 /* nicira extension: tun_metadata, for Geneve header variable data */
NXM_NX_TUN_METADATA5 = 45 /* nicira extension: tun_metadata, for Geneve header variable data */
NXM_NX_TUN_METADATA6 = 46 /* nicira extension: tun_metadata, for Geneve header variable data */
NXM_NX_TUN_METADATA7 = 47 /* nicira extension: tun_metadata, for Geneve header variable data */
NXM_NX_TUN_FLAGS = 104 /* nicira extension: tunnel Flags */
NXM_NX_CT_STATE = 105 /* nicira extension: ct_state for conn_track */
NXM_NX_CT_ZONE = 106 /* nicira extension: ct_zone for conn_track */
NXM_NX_CT_MARK = 107 /* nicira extension: ct_mark for conn_track */
NXM_NX_CT_LABEL = 108 /* nicira extension: ct_label for conn_track */
NXM_NX_TUN_IPV6_SRC = 109 /* nicira extension: tun_dst_ipv6, dst IPv6 address of tunnel */
NXM_NX_TUN_IPV6_DST = 110 /* nicira extension: tun_dst_ipv6, src IPv6 address of tunnel */
NXM_NX_XXREG0 = 111 /* nicira extension: xxreg0 */
NXM_NX_XXREG1 = 112 /* nicira extension: xxreg0 */
NXM_NX_XXREG2 = 113 /* nicira extension: xxreg0 */
NXM_NX_XXREG3 = 114 /* nicira extension: xxreg0 */
NXM_NX_CT_NW_PROTO = 119 /* nicira extension: ct_nw_proto, the protocol byte in the IPv4 or IPv6 header forthe original direction tuple of the conntrack entry */
NXM_NX_CT_NW_SRC = 120 /* nicira extension: ct_nw_src, source IPv4 address of the original direction tuple of the conntrack entry */
NXM_NX_CT_NW_DST = 121 /* nicira extension: ct_nw_dst, destination IPv4 address of the original direction tuple of the conntrack entry */
NXM_NX_CT_IPV6_SRC = 122 /* nicira extension: ct_ipv6_src, source IPv6 address of the original direction tuple of the conntrack entry */
NXM_NX_CT_IPV6_DST = 123 /* nicira extension: ct_ipv6_dst, destination IPv6 address of the original direction tuple of the conntrack entry */
NXM_NX_CT_TP_SRC = 124 /* nicira extension: ct_tp_src, transport layer source port of the original direction tuple of the conntrack entry */
NXM_NX_CT_TP_DST = 125 /* nicira extension: ct_tp_dst, transport layer destination port of the original direction tuple of the conntrack entry */
)
// IN_PORT field
type InPortField struct {
InPort uint32
}
func (m *InPortField) Len() uint16 {
return 4
}
func (m *InPortField) MarshalBinary() (data []byte, err error) {
data = make([]byte, 4)
binary.BigEndian.PutUint32(data, m.InPort)
return
}
func (m *InPortField) UnmarshalBinary(data []byte) error {
m.InPort = binary.BigEndian.Uint32(data)
return nil
}
// Return a MatchField for Input port matching
func NewInPortField(inPort uint32) *MatchField {
f := new(MatchField)
f.Class = OXM_CLASS_OPENFLOW_BASIC
f.Field = OXM_FIELD_IN_PORT
f.HasMask = false
inPortField := new(InPortField)
inPortField.InPort = inPort
f.Value = inPortField
f.Length = uint8(inPortField.Len())
return f
}
// ETH_DST field
type EthDstField struct {
EthDst net.HardwareAddr
}
func (m *EthDstField) Len() uint16 {
return 6
}
func (m *EthDstField) MarshalBinary() (data []byte, err error) {
data = make([]byte, 6)
copy(data, m.EthDst)
return
}
func (m *EthDstField) UnmarshalBinary(data []byte) error {
copy(m.EthDst, data)
return nil
}
// Return a MatchField for ethernet dest addr
func NewEthDstField(ethDst net.HardwareAddr, ethDstMask *net.HardwareAddr) *MatchField {
f := new(MatchField)
f.Class = OXM_CLASS_OPENFLOW_BASIC
f.Field = OXM_FIELD_ETH_DST
f.HasMask = false
ethDstField := new(EthDstField)
ethDstField.EthDst = ethDst
f.Value = ethDstField
f.Length = uint8(ethDstField.Len())
// Add the mask
if ethDstMask != nil {
mask := new(EthDstField)
mask.EthDst = *ethDstMask
f.Mask = mask
f.HasMask = true
f.Length += uint8(mask.Len())
}
return f
}
// ETH_SRC field
type EthSrcField struct {
EthSrc net.HardwareAddr
}
func (m *EthSrcField) Len() uint16 {
return 6
}
func (m *EthSrcField) MarshalBinary() (data []byte, err error) {
data = make([]byte, 6)
copy(data, m.EthSrc)
return
}
func (m *EthSrcField) UnmarshalBinary(data []byte) error {
copy(m.EthSrc, data)
return nil
}
// Return a MatchField for ethernet src addr
func NewEthSrcField(ethSrc net.HardwareAddr, ethSrcMask *net.HardwareAddr) *MatchField {
f := new(MatchField)
f.Class = OXM_CLASS_OPENFLOW_BASIC
f.Field = OXM_FIELD_ETH_SRC
f.HasMask = false
ethSrcField := new(EthSrcField)
ethSrcField.EthSrc = ethSrc
f.Value = ethSrcField
f.Length = uint8(ethSrcField.Len())
// Add the mask
if ethSrcMask != nil {
mask := new(EthSrcField)
mask.EthSrc = *ethSrcMask
f.Mask = mask
f.HasMask = true
f.Length += uint8(mask.Len())
}
return f
}
// ETH_TYPE field
type EthTypeField struct {
EthType uint16
}
func (m *EthTypeField) Len() uint16 {
return 2
}
func (m *EthTypeField) MarshalBinary() (data []byte, err error) {
data = make([]byte, 2)
binary.BigEndian.PutUint16(data, m.EthType)
return
}
func (m *EthTypeField) UnmarshalBinary(data []byte) error {
m.EthType = binary.BigEndian.Uint16(data)
return nil
}
// Return a MatchField for ethertype matching
func NewEthTypeField(ethType uint16) *MatchField {
f := new(MatchField)
f.Class = OXM_CLASS_OPENFLOW_BASIC
f.Field = OXM_FIELD_ETH_TYPE
f.HasMask = false
ethTypeField := new(EthTypeField)
ethTypeField.EthType = ethType
f.Value = ethTypeField
f.Length = uint8(ethTypeField.Len())
return f
}
const OFPVID_PRESENT = 0x1000 /* Bit that indicate that a VLAN id is set */
const OFPVID_NONE = 0x0000 /* No VLAN id was set. */
// VLAN_ID field
type VlanIdField struct {
VlanId uint16
}
func (m *VlanIdField) Len() uint16 {
return 2
}
func (m *VlanIdField) MarshalBinary() (data []byte, err error) {
data = make([]byte, 2)
binary.BigEndian.PutUint16(data, m.VlanId)
return
}
func (m *VlanIdField) UnmarshalBinary(data []byte) error {
m.VlanId = binary.BigEndian.Uint16(data)
return nil
}
// Return a MatchField for vlan id matching
func NewVlanIdField(vlanId uint16, vlanMask *uint16) *MatchField {
f := new(MatchField)
f.Class = OXM_CLASS_OPENFLOW_BASIC
f.Field = OXM_FIELD_VLAN_VID
f.HasMask = false
vlanIdField := new(VlanIdField)
vlanIdField.VlanId = vlanId | OFPVID_PRESENT
f.Value = vlanIdField
f.Length = uint8(vlanIdField.Len())
if vlanMask != nil {
mask := new(VlanIdField)
mask.VlanId = *vlanMask
f.Mask = mask
f.HasMask = true
f.Length += uint8(mask.Len())
}
return f
}
// MplsLabel field
type MplsLabelField struct {
MplsLabel uint32
}
func (m *MplsLabelField) Len() uint16 {
return 4
}
func (m *MplsLabelField) MarshalBinary() (data []byte, err error) {
data = make([]byte, 4)
binary.BigEndian.PutUint32(data, m.MplsLabel)
return
}
func (m *MplsLabelField) UnmarshalBinary(data []byte) error {
m.MplsLabel = binary.BigEndian.Uint32(data)
return nil
}
// Return a MatchField for mpls Label matching
func NewMplsLabelField(mplsLabel uint32) *MatchField {
f := new(MatchField)
f.Class = OXM_CLASS_OPENFLOW_BASIC
f.Field = OXM_FIELD_MPLS_LABEL
f.HasMask = false
mplsLabelField := new(MplsLabelField)
mplsLabelField.MplsLabel = mplsLabel
f.Value = mplsLabelField
f.Length = uint8(mplsLabelField.Len())
return f
}
// MplsBos field
type MplsBosField struct {
MplsBos uint8
}
func (m *MplsBosField) Len() uint16 {
return 1
}
func (m *MplsBosField) MarshalBinary() (data []byte, err error) {
data = make([]byte, 1)
data[0] = m.MplsBos
return
}
func (m *MplsBosField) UnmarshalBinary(data []byte) error {
m.MplsBos = data[0]
return nil
}
// Return a MatchField for mpls Bos matching
func NewMplsBosField(mplsBos uint8) *MatchField {
f := new(MatchField)
f.Class = OXM_CLASS_OPENFLOW_BASIC
f.Field = OXM_FIELD_MPLS_BOS
f.HasMask = false
mplsBosField := new(MplsBosField)
mplsBosField.MplsBos = mplsBos
f.Value = mplsBosField
f.Length = uint8(mplsBosField.Len())
return f
}
// MplsTc field
type MplsTcField struct {
MplsTc uint8
}
func (m *MplsTcField) Len() uint16 {
return 1
}
func (m *MplsTcField) MarshalBinary() (data []byte, err error) {
data = make([]byte, 1)
data[0] = m.MplsTc
return
}
func (m *MplsTcField) UnmarshalBinary(data []byte) error {
if len(data) < int(m.Len()) {
return fmt.Errorf("the []byte is too short to unmarshal a full MplsTcField message")
}
m.MplsTc = data[0]
return nil
}
// Return a MatchField for mpls Tc matching
func NewMplsTcField(mplsTc uint8) *MatchField {
f := new(MatchField)
f.Class = OXM_CLASS_OPENFLOW_BASIC
f.Field = OXM_FIELD_MPLS_TC
f.HasMask = false
mplsTcField := new(MplsTcField)
mplsTcField.MplsTc = mplsTc
f.Value = mplsTcField
f.Length = uint8(mplsTcField.Len())
return f
}
// IPV4_SRC field
type Ipv4SrcField struct {
Ipv4Src net.IP
}
func (m *Ipv4SrcField) Len() uint16 {
return 4
}
func (m *Ipv4SrcField) MarshalBinary() (data []byte, err error) {
data = make([]byte, 4)
copy(data, m.Ipv4Src.To4())
return
}
func (m *Ipv4SrcField) UnmarshalBinary(data []byte) error {
m.Ipv4Src = net.IPv4(data[0], data[1], data[2], data[3])
return nil
}
// Return a MatchField for ipv4 src addr
func NewIpv4SrcField(ipSrc net.IP, ipSrcMask *net.IP) *MatchField {
f := new(MatchField)
f.Class = OXM_CLASS_OPENFLOW_BASIC
f.Field = OXM_FIELD_IPV4_SRC
f.HasMask = false
ipSrcField := new(Ipv4SrcField)
ipSrcField.Ipv4Src = ipSrc
f.Value = ipSrcField
f.Length = uint8(ipSrcField.Len())
// Add the mask
if ipSrcMask != nil {
mask := new(Ipv4SrcField)
mask.Ipv4Src = *ipSrcMask
f.Mask = mask
f.HasMask = true
f.Length += uint8(mask.Len())
}
return f
}
// IPV4_DST field
type Ipv4DstField struct {
Ipv4Dst net.IP
}
func (m *Ipv4DstField) Len() uint16 {
return 4
}
func (m *Ipv4DstField) MarshalBinary() (data []byte, err error) {
data = make([]byte, 4)
copy(data, m.Ipv4Dst.To4())
return
}
func (m *Ipv4DstField) UnmarshalBinary(data []byte) error {
m.Ipv4Dst = net.IPv4(data[0], data[1], data[2], data[3])
return nil
}
// Return a MatchField for ipv4 dest addr
func NewIpv4DstField(ipDst net.IP, ipDstMask *net.IP) *MatchField {
f := new(MatchField)
f.Class = OXM_CLASS_OPENFLOW_BASIC
f.Field = OXM_FIELD_IPV4_DST
f.HasMask = false
ipDstField := new(Ipv4DstField)
ipDstField.Ipv4Dst = ipDst
f.Value = ipDstField
f.Length = uint8(ipDstField.Len())
// Add the mask
if ipDstMask != nil {
mask := new(Ipv4DstField)
mask.Ipv4Dst = *ipDstMask
f.Mask = mask
f.HasMask = true
f.Length += uint8(mask.Len())
}
return f
}