-
Notifications
You must be signed in to change notification settings - Fork 4
/
OpenFlow0x04.ml
7036 lines (6272 loc) · 266 KB
/
OpenFlow0x04.ml
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
(* TODO(???): rename sizeof to size_of for consistency with 0x01 stuff. *)
(** OpenFlow 1.3 (protocol version 0x04) *)
open Printf
open Cstruct
open Cstruct.BE
open OpenFlow0x04_Core
open List
open Packet
exception Unparsable of string
let sym_num = ref 0
let sum (lst : int list) = List.fold_left (fun x y -> x + y) 0 lst
type uint128 = int64*int64
type uint48 = uint64
type uint24 = int32
type uint12 = uint16
type switchId = OpenFlow0x04_Core.switchId
(* OKAY *)
cenum msg_code {
HELLO;
ERROR;
ECHO_REQ;
ECHO_RESP;
VENDOR;
FEATURES_REQ;
FEATURES_RESP;
GET_CONFIG_REQ;
GET_CONFIG_RESP;
SET_CONFIG;
PACKET_IN;
FLOW_REMOVED;
PORT_STATUS;
PACKET_OUT;
FLOW_MOD;
GROUP_MOD;
PORT_MOD;
TABLE_MOD;
MULTIPART_REQ;
MULTIPART_RESP;
BARRIER_REQ;
BARRIER_RESP;
QUEUE_GET_CONFIG_REQ;
QUEUE_GET_CONFIG_RESP;
ROLE_REQ;
ROLE_RESP;
GET_ASYNC_REQ;
GET_ASYNC_REP;
SET_ASYNC;
METER_MOD
} as uint8_t
cstruct ofp_match {
uint16_t typ;
uint16_t length
} as big_endian
let ofpp_in_port = 0xfffffff8l
let ofpp_flood = 0xfffffffbl
let ofpp_all = 0xfffffffcl
let ofpp_controller = 0xfffffffdl
let ofpp_any = 0xffffffffl
let ofp_no_buffer = 0xffffffffl
(* Not in the spec, comes from C headers. :rolleyes: *)
let ofpg_all = 0xfffffffcl
let ofpg_any = 0xffffffffl
let ofp_eth_alen = 6 (* Bytes in an Ethernet address. *)
(* OKAY *)
cenum ofp_oxm_class {
OFPXMC_NXM_0 = 0x0000; (* Backward compatibility with NXM *)
OFPXMC_NXM_1 = 0x0001; (* Backward compatibility with NXM *)
OFPXMC_OPENFLOW_BASIC = 0x8000; (* Basic class for OpenFlow *)
OFPXMC_EXPERIMENTER = 0xFFFF (* Experimenter class *)
} as uint16_t
(* OKAY *)
cenum oxm_ofb_match_fields {
OFPXMT_OFB_IN_PORT = 0; (* Switch input port. *)
OFPXMT_OFB_IN_PHY_PORT = 1; (* Switch physical input port. *)
OFPXMT_OFB_METADATA = 2; (* Metadata passed between tables. *)
OFPXMT_OFB_ETH_DST = 3; (* Ethernet destination address. *)
OFPXMT_OFB_ETH_SRC = 4; (* Ethernet source address. *)
OFPXMT_OFB_ETH_TYPE = 5; (* Ethernet frame type. *)
OFPXMT_OFB_VLAN_VID = 6; (* VLAN id. *)
OFPXMT_OFB_VLAN_PCP = 7; (* VLAN priority. *)
OFPXMT_OFB_IP_DSCP = 8; (* IP DSCP (6 bits in ToS field). *)
OFPXMT_OFB_IP_ECN = 9; (* IP ECN (2 bits in ToS field). *)
OFPXMT_OFB_IP_PROTO = 10; (* IP protocol. *)
OFPXMT_OFB_IPV4_SRC = 11; (* IPv4 source address. *)
OFPXMT_OFB_IPV4_DST = 12; (* IPv4 destination address. *)
OFPXMT_OFB_TCP_SRC = 13; (* TCP source port. *)
OFPXMT_OFB_TCP_DST = 14; (* TCP destination port. *)
OFPXMT_OFB_UDP_SRC = 15; (* UDP source port. *)
OFPXMT_OFB_UDP_DST = 16; (* UDP destination port. *)
OFPXMT_OFB_SCTP_SRC = 17; (* SCTP source port. *)
OFPXMT_OFB_SCTP_DST = 18; (* SCTP destination port. *)
OFPXMT_OFB_ICMPV4_TYPE = 19; (* ICMP type. *)
OFPXMT_OFB_ICMPV4_CODE = 20; (* ICMP code. *)
OFPXMT_OFB_ARP_OP = 21; (* ARP opcode. *)
OFPXMT_OFB_ARP_SPA = 22; (* ARP source IPv4 address. *)
OFPXMT_OFB_ARP_TPA = 23; (* ARP target IPv4 address. *)
OFPXMT_OFB_ARP_SHA = 24; (* ARP source hardware address. *)
OFPXMT_OFB_ARP_THA = 25; (* ARP target hardware address. *)
OFPXMT_OFB_IPV6_SRC = 26; (* IPv6 source address. *)
OFPXMT_OFB_IPV6_DST = 27; (* IPv6 destination address. *)
OFPXMT_OFB_IPV6_FLABEL = 28; (* IPv6 Flow Label *)
OFPXMT_OFB_ICMPV6_TYPE = 29; (* ICMPv6 type. *)
OFPXMT_OFB_ICMPV6_CODE = 30; (* ICMPv6 code. *)
OFPXMT_OFB_IPV6_ND_TARGET = 31; (* Target address for ND. *)
OFPXMT_OFB_IPV6_ND_SLL = 32; (* Source link-layer for ND. *)
OFPXMT_OFB_IPV6_ND_TLL = 33; (* Target link-layer for ND. *)
OFPXMT_OFB_MPLS_LABEL = 34; (* MPLS label. *)
OFPXMT_OFB_MPLS_TC = 35; (* MPLS TC. *)
OFPXMT_OFP_MPLS_BOS = 36; (* MPLS BoS bit. *)
OFPXMT_OFB_PBB_ISID = 37; (* PBB I-SID. *)
OFPXMT_OFB_TUNNEL_ID = 38; (* Logical Port Metadata. *)
OFPXMT_OFB_IPV6_EXTHDR = 39 (* IPv6 Extension Header pseudo-field *)
} as uint8_t
cenum ofp_vlan_id {
OFPVID_PRESENT = 0x1000; (* Bit that indicate that a VLAN id is set *)
OFPVID_NONE = 0x0000 (* No VLAN id was set. *)
} as uint16_t
cstruct ofp_switch_features {
uint64_t datapath_id;
uint32_t n_buffers;
uint8_t n_tables;
uint8_t auxiliary_id;
uint8_t pad0;
uint8_t pad1;
uint32_t capabilities;
uint32_t reserved
} as big_endian
module PortConfig = struct
type t = portConfig
let config_to_int (config : t) : int32 =
Int32.logor (if config.port_down then (Int32.shift_left 1l 0) else 0l)
(Int32.logor (if config.no_recv then (Int32.shift_left 1l 2) else 0l)
(Int32.logor (if config.no_fwd then (Int32.shift_left 1l 5) else 0l)
(if config.no_packet_in then (Int32.shift_left 1l 6) else 0l)))
let marshal (pc : t) : int32 = config_to_int pc
let parse bits : t =
{ port_down = Bits.test_bit 0 bits;
no_recv = Bits.test_bit 2 bits;
no_fwd = Bits.test_bit 5 bits;
no_packet_in = Bits.test_bit 6 bits
}
let to_string (config : t) =
Format.sprintf "{ port_down = %b; no_recv = %b; no_fwd = %b; no_packet_in = %b }"
config.port_down
config.no_recv
config.no_fwd
config.no_packet_in
end
module PortFeatures = struct
type t = portFeatures
let marshal (t : t) : int32 =
Int32.logor (if t.rate_10mb_hd then (Int32.shift_left 1l 0) else 0l)
(Int32.logor (if t.rate_10mb_fd then (Int32.shift_left 1l 1) else 0l)
(Int32.logor (if t.rate_100mb_hd then (Int32.shift_left 1l 2) else 0l)
(Int32.logor (if t.rate_100mb_fd then (Int32.shift_left 1l 3) else 0l)
(Int32.logor (if t.rate_1gb_hd then (Int32.shift_left 1l 4) else 0l)
(Int32.logor (if t.rate_1gb_fd then (Int32.shift_left 1l 5) else 0l)
(Int32.logor (if t.rate_10gb_fd then (Int32.shift_left 1l 6) else 0l)
(Int32.logor (if t.rate_40gb_fd then (Int32.shift_left 1l 7) else 0l)
(Int32.logor (if t.rate_100gb_fd then (Int32.shift_left 1l 8) else 0l)
(Int32.logor (if t.rate_1tb_fd then (Int32.shift_left 1l 9) else 0l)
(Int32.logor (if t.other then (Int32.shift_left 1l 10) else 0l)
(Int32.logor (if t.copper then (Int32.shift_left 1l 11) else 0l)
(Int32.logor (if t.fiber then (Int32.shift_left 1l 12) else 0l)
(Int32.logor (if t.autoneg then (Int32.shift_left 1l 13) else 0l)
(Int32.logor (if t.pause then (Int32.shift_left 1l 14) else 0l)
(if t.pause_asym then (Int32.shift_left 1l 15) else 0l)))))))))))))))
let parse (bits : int32) : t =
{ rate_10mb_hd = Bits.test_bit 0 bits;
rate_10mb_fd = Bits.test_bit 1 bits;
rate_100mb_hd = Bits.test_bit 2 bits;
rate_100mb_fd = Bits.test_bit 3 bits;
rate_1gb_hd = Bits.test_bit 4 bits;
rate_1gb_fd = Bits.test_bit 5 bits;
rate_10gb_fd = Bits.test_bit 6 bits;
rate_40gb_fd = Bits.test_bit 7 bits;
rate_100gb_fd = Bits.test_bit 8 bits;
rate_1tb_fd = Bits.test_bit 9 bits;
other = Bits.test_bit 10 bits;
copper = Bits.test_bit 11 bits;
fiber = Bits.test_bit 12 bits;
autoneg = Bits.test_bit 13 bits;
pause = Bits.test_bit 14 bits;
pause_asym = Bits.test_bit 15 bits
}
let to_string (feat : t) =
Format.sprintf
"{ 10mhd = %B; 10mfd = %B; 100mhd = %B; 100mfd = %B; 1ghd%B\
1gfd = %B; 10gfd = %B; 40gfd = %B; 100gfd = %B; 1tfd = %B; \
other = %B; copper = %B; fiber = %B; autoneg = %B; pause = %B; \
pause_asym = %B }"
feat.rate_10mb_hd
feat.rate_10mb_fd
feat.rate_100mb_hd
feat.rate_100mb_fd
feat.rate_1gb_hd
feat.rate_1gb_fd
feat.rate_10gb_fd
feat.rate_40gb_fd
feat.rate_100gb_fd
feat.rate_1tb_fd
feat.other
feat.copper
feat.fiber
feat.autoneg
feat.pause
feat.pause_asym
end
module PortState = struct
type t = portState
let state_to_int (state : t) : int32 =
Int32.logor (if state.link_down then (Int32.shift_left 1l 0) else 0l)
(Int32.logor (if state.blocked then (Int32.shift_left 1l 1) else 0l)
(if state.live then (Int32.shift_left 1l 2) else 0l))
let marshal (ps : t) : int32 = state_to_int ps
let parse bits : t =
{ link_down = Bits.test_bit 0 bits;
blocked = Bits.test_bit 1 bits;
live = Bits.test_bit 2 bits
}
let to_string (state : t) =
Format.sprintf "{ link_down = %B; blocked = %B; live = %B }"
state.link_down
state.blocked
state.live
end
cstruct ofp_port_stats_request {
uint32_t port_no;
uint8_t pad[4]
} as big_endian
cstruct ofp_queue_stats_request {
uint32_t port_no;
uint32_t queue_id
} as big_endian
cstruct ofp_group_stats_request {
uint32_t group_id;
uint8_t pad[4]
} as big_endian
cstruct ofp_meter_multipart_request {
uint32_t meter_id;
uint8_t pad[4]
} as big_endian
cstruct ofp_table_features {
uint16_t length;
uint8_t table_id;
uint8_t pad[5];
uint8_t name[32];
uint64_t metadata_match;
uint64_t metadata_write;
uint32_t config;
uint32_t max_entries
} as big_endian
(* Body of reply to OFPMP_PORT request. If a counter is unsupported, set
* the field to all ones. *)
cstruct ofp_port_stats {
uint32_t port_no;
uint8_t pad[4]; (* Align to 64-bits. *)
uint64_t rx_packets; (* Number of received packets. *)
uint64_t tx_packets; (* Number of transmitted packets. *)
uint64_t rx_bytes; (* Number of received bytes. *)
uint64_t tx_bytes; (* Number of transmitted bytes. *)
uint64_t rx_dropped; (* Number of packets dropped by RX. *)
uint64_t tx_dropped; (* Number of packets dropped by TX. *)
uint64_t rx_errors; (* Number of receive errors. This is a super-set
of more specific receive errors and should be
greater than or equal to the sum of all
rx_*_err values. *)
uint64_t tx_errors; (* Number of transmit errors. This is a super-set
of more specific transmit errors and should be
greater than or equal to the sum of all
tx_*_err values (none currently defined.) *)
uint64_t rx_frame_err; (* Number of frame alignment errors. *)
uint64_t rx_over_err; (* Number of packets with RX overrun. *)
uint64_t rx_crc_err; (* Number of CRC errors. *)
uint64_t collisions; (* Number of collisions. *)
uint32_t duration_sec; (* Time port has been alive in seconds. *)
uint32_t duration_nsec (* Time port has been alive in nanoseconds beyond
duration_sec. *)
} as big_endian
cstruct ofp_port {
uint32_t port_no;
uint32_t pad;
uint8_t hw_addr[6];
uint8_t pad2;
uint8_t pad3;
uint8_t name[16]; (* OFP_MAX_PORT_NAME_LEN, Null-terminated *)
uint32_t config; (* Bitmap of OFPPC_* flags. *)
uint32_t state; (* Bitmap of OFPPS_* flags. *)
(* Bitmaps of OFPPF_* that describe features. All bits zeroed if
* unsupported or unavailable. *)
uint32_t curr; (* Current features. *)
uint32_t advertised; (* Features being advertised by the port. *)
uint32_t supported; (* Features supported by the port. *)
uint32_t peer; (* Features advertised by peer. *)
uint32_t curr_speed; (* Current port bitrate in kbps. *)
uint32_t max_speed (* Max port bitrate in kbps *)
} as big_endian
cenum ofp_port_reason {
OFPPR_ADD;
OFPPR_DELETE;
OFPPR_MODIFY
} as uint8_t
cstruct ofp_port_status {
uint8_t reason; (* One of OFPPR_* *)
uint8_t pad[7]
} as big_endian
cenum ofp_table_config {
OFPTC_DEPRECATED_MASK = 0x00000003l (* currently deprecated *)
} as uint32_t
cenum ofp_table_feature_prop_type {
OFPTFPT_INSTRUCTIONS = 0;
OFPTFPT_INSTRUCTIONS_MISS = 1;
OFPTFPT_NEXT_TABLES = 2;
OFPTFPT_NEXT_TABLES_MISS = 3;
OFPTFPT_WRITE_ACTIONS = 4;
OFPTFPT_WRITE_ACTIONS_MISS = 5;
OFPTFPT_APPLY_ACTIONS = 6;
OFPTFPT_APPLY_ACTIONS_MISS = 7;
OFPTFPT_MATCH = 8;
OFPTFPT_WILDCARDS = 10;
OFPTFPT_WRITE_SETFIELD = 12;
OFPTFPT_WRITE_SETFIELD_MISS = 13;
OFPTFPT_APPLY_SETFIELD = 14;
OFPTFPT_APPLY_SETFIELD_MISS = 15;
OFPTFPT_EXPERIMENTER = 0xFFFE;
OFPTFPT_EXPERIMENTER_MISS = 0xFFFF
} as uint16_t
cstruct ofp_table_feature_prop_header {
uint16_t typ;
uint16_t length
} as big_endian
cenum ofp_flow_mod_command {
OFPFC_ADD = 0; (* New flow. *)
OFPFC_MODIFY = 1; (* Modify all matching flows. *)
OFPFC_MODIFY_STRICT = 2; (* Modify entry strictly matching wildcards and
priority. *)
OFPFC_DELETE = 3; (* Delete all matching flows. *)
OFPFC_DELETE_STRICT = 4 (* Delete entry strictly matching wildcards and
priority. *)
} as uint8_t
cstruct ofp_flow_mod {
uint64_t cookie; (* Opaque controller-issued identifier. *)
uint64_t cookie_mask; (* Mask used to restrict the cookie bits
that must match when the command is
OFPFC_MODIFY* or OFPFC_DELETE*. A value
of 0 indicates no restriction. *)
(* Flow actions. *)
uint8_t table_id; (* ID of the table to put the flow in.
For OFPFC_DELETE_* commands, OFPTT_ALL
can also be used to delete matching
flows from all tables. *)
uint8_t command; (* One of OFPFC_*. *)
uint16_t idle_timeout; (* Idle time before discarding (seconds). *)
uint16_t hard_timeout; (* Max time before discarding (seconds). *)
uint16_t priority; (* Priority level of flow entry. *)
uint32_t buffer_id; (* Buffered packet to apply to, or
OFP_NO_BUFFER.
Not meaningful for OFPFC_DELETE*. *)
uint32_t out_port; (* For OFPFC_DELETE* commands, require
matching entries to include this as an
output port. A value of OFPP_ANY
indicates no restriction. *)
uint32_t out_group; (* For OFPFC_DELETE* commands, require
matching entries to include this as an
output group. A value of OFPG_ANY
indicates no restriction. *)
uint16_t flags; (* One of OFPFF_*. *)
uint8_t pad0;
uint8_t pad1
} as big_endian
(* OKAY *)
cenum ofp_action_type {
OFPAT_OUTPUT = 0; (* Output to switch port. *)
OFPAT_COPY_TTL_OUT = 11; (* Copy TTL "outwards" -- from next-to-outermost
to outermost *)
OFPAT_COPY_TTL_IN = 12; (* Copy TTL "inwards" -- from outermost to
next-to-outermost *)
OFPAT_SET_MPLS_TTL = 15; (* MPLS TTL *)
OFPAT_DEC_MPLS_TTL = 16; (* Decrement MPLS TTL *)
OFPAT_PUSH_VLAN = 17; (* Push a new VLAN tag *)
OFPAT_POP_VLAN = 18; (* Pop the outer VLAN tag *)
OFPAT_PUSH_MPLS = 19; (* Push a new MPLS tag *)
OFPAT_POP_MPLS = 20; (* Pop the outer MPLS tag *)
OFPAT_SET_QUEUE = 21; (* Set queue id when outputting to a port *)
OFPAT_GROUP = 22; (* Apply group. *)
OFPAT_SET_NW_TTL = 23; (* IP TTL. *)
OFPAT_DEC_NW_TTL = 24; (* Decrement IP TTL. *)
OFPAT_SET_FIELD = 25; (* Set a header field using OXM TLV format. *)
OFPAT_PUSH_PBB = 26; (* Push a new PBB service tag (I-TAG) *)
OFPAT_POP_PBB = 27; (* Pop the outer PBB service tag (I-TAG) *)
OFPAT_EXPERIMENTER = 0xffff
} as uint16_t
(* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
* When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
* number of bytes to send. A 'max_len' of zero means no bytes of the
* packet should be sent. A 'max_len' of OFPCML_NO_BUFFER means that
* the packet is not buffered and the complete packet is to be sent to
* the controller. *)
cstruct ofp_action_output {
uint16_t typ; (* OFPAT_OUTPUT. *)
uint16_t len; (* Length is 16. *)
uint32_t port; (* Output port. *)
uint16_t max_len; (* Max length to send to controller. *)
uint8_t pad0; (* Pad to 64 bits. *)
uint8_t pad1; (* Pad to 64 bits. *)
uint8_t pad2; (* Pad to 64 bits. *)
uint8_t pad3; (* Pad to 64 bits. *)
uint8_t pad4; (* Pad to 64 bits. *)
uint8_t pad5 (* Pad to 64 bits. *)
} as big_endian
(* Action structure for OFPAT_GROUP. *)
cstruct ofp_action_group {
uint16_t typ; (* OFPAT_GROUP. *)
uint16_t len; (* Length is 8. *)
uint32_t group_id (* Group identifier. *)
} as big_endian
(* Generic action header. Used for POP_VLAN *)
cstruct ofp_action_header {
uint16_t typ; (* POP_VLAN. *)
uint16_t len; (* Length is 8. *)
uint8_t pad;
uint8_t pad1;
uint8_t pad2;
uint8_t pad3
} as big_endian
(* Action structure for POP_MPLS *)
cstruct ofp_action_pop_mpls {
uint16_t typ; (* POP_VLAN. *)
uint16_t len; (* Length is 8. *)
uint16_t ethertype;
uint8_t pad0; (* Pad to 64 bits. *)
uint8_t pad1 (* Pad to 64 bits. *)
} as big_endian
(* Action structure for SET_NW_TTL *)
cstruct ofp_action_nw_ttl {
uint16_t typ; (* SET_NW_TTL. *)
uint16_t len; (* Length is 8. *)
uint8_t nw_ttl;
uint8_t pad;
uint8_t pad1;
uint8_t pad2
} as big_endian
(* Action structure for SET_MPLS_TTL *)
cstruct ofp_action_mpls_ttl {
uint16_t typ; (* SET_MPLS_TTL. *)
uint16_t len; (* Length is 8. *)
uint8_t mpls_ttl;
uint8_t pad[3];
} as big_endian
(* Action structure for *_PUSH *)
cstruct ofp_action_push {
uint16_t typ; (* OFPAT_PUSH_VLAN/MPLS/PBB *)
uint16_t len; (* Length is 8. *)
uint16_t ethertype; (* Pad to 64 bits. *)
uint8_t pad0; (* Pad to 64 bits. *)
uint8_t pad1 (* Pad to 64 bits. *)
} as big_endian
(* Action structure for OFPAT_SET_FIELD. *)
cstruct ofp_action_set_field {
uint16_t typ; (* OFPAT_SET_FIELD. *)
uint16_t len (* Length is padded to 64 bits. *)
(* Followed by:
* - Exactly oxm_len bytes containing a single OXM TLV, then
* - Exactly ((oxm_len + 4) + 7)/8*8 - (oxm_len + 4) (between 0 and 7)
* bytes of all-zero bytes
*)
} as big_endian
(* Action structure for SET_QUEUE *)
cstruct ofp_action_set_queue {
uint16_t typ; (* OFPAT_SET_QUEUE*)
uint16_t len; (* Length is 8. *)
uint32_t queue_id
} as big_endian
cstruct ofp_action_experimenter {
uint16_t typ;
uint16_t len;
uint32_t experimenter
} as big_endian
(* Instruction header that is common to all instructions. The length includes
* the header and any padding used to make the instruction 64-bit aligned.
* NB: The length of an instruction *must* always be a multiple of eight. *)
cstruct ofp_instruction {
uint16_t typ; (* Instruction type *)
uint16_t len (* Length of this struct in bytes. *)
} as big_endian
cenum ofp_instruction_type {
OFPIT_GOTO_TABLE = 1;
OFPIT_WRITE_METADATA = 2;
OFPIT_WRITE_ACTIONS = 3;
OFPIT_APPLY_ACTIONS = 4;
OFPIT_CLEAR_ACTIONS = 5;
OFPIT_METER = 6;
OFPIT_EXPERIMENTER = 0xFFFF;
} as uint16_t
(* Instruction structure for OFPIT_GOTO_TABLE *)
cstruct ofp_instruction_goto_table {
uint16_t typ; (* OFPIT_GOTO_TABLE *)
uint16_t len; (* Length of this struct in bytes. *)
uint8_t table_id; (* Set next table in the lookup pipeline *)
uint8_t pad0; (* Pad to 64 bits. *)
uint8_t pad1;
uint8_t pad2
} as big_endian
(* Instruction structure for OFPIT_WRITE_METADATA *)
cstruct ofp_instruction_write_metadata {
uint16_t typ; (* OFPIT_WRITE_METADATA *)
uint16_t len; (* Length of this struct in bytes. *)
uint8_t pad0; (* Align to 64-bits *)
uint8_t pad1;
uint8_t pad2;
uint8_t pad3;
uint64_t metadata; (* Metadata value to write *)
uint64_t metadata_mask (* Metadata write bitmask *)
} as big_endian
(* Instruction structure for OFPIT_WRITE/APPLY/CLEAR_ACTIONS *)
cstruct ofp_instruction_actions {
uint16_t typ; (* One of OFPIT_*_ACTIONS *)
uint16_t len; (* Length of this struct in bytes. *)
uint8_t pad0; (* Align to 64-bits *)
uint8_t pad1;
uint8_t pad2;
uint8_t pad3
} as big_endian
(* Instruction structure for OFPIT_METER *)
cstruct ofp_instruction_meter {
uint16_t typ; (* OFPIT_METER *)
uint16_t len; (* Length is 8. *)
uint32_t meter_id (* Meter instance. *)
} as big_endian
(* Instruction structure for experimental instructions *)
cstruct ofp_instruction_experimenter {
uint16_t typ; (* OFPIT_EXPERIMENTER *)
uint16_t len; (* Length of this struct in bytes *)
uint32_t experimenter (* Experimenter ID which takes the same form
as in struct ofp_experimenter_header. *)
(* Experimenter-defined arbitrary additional data. *)
} as big_endian
cenum ofp_group_type {
OFPGT_ALL = 0; (* All (multicast/broadcast) group. *)
OFPGT_SELECT = 1; (* Select group. *)
OFPGT_INDIRECT = 2; (* Indirect group. *)
OFPGT_FF = 3 (* Fast failover group. *)
} as uint16_t
(* Group setup and teardown (controller -> datapath). *)
cstruct ofp_group_mod {
uint16_t command; (* One of OFPGC_*. *)
uint8_t typ; (* One of OFPGT_*. *)
uint8_t pad; (* Pad to 64 bits. *)
uint32_t group_id (* Group identifier. *)
} as big_endian
(* Bucket for use in groups. *)
cstruct ofp_bucket {
uint16_t len; (* Length the bucket in bytes, including
this header and any padding to make it
64-bit aligned. *)
uint16_t weight; (* Relative weight of bucket. Only
defined for select groups. *)
uint32_t watch_port; (* Port whose state affects whether this
bucket is live. Only required for fast
failover groups. *)
uint32_t watch_group; (* Group whose state affects whether this
bucket is live. Only required for fast
failover groups. *)
uint8_t pad0;
uint8_t pad1;
uint8_t pad2;
uint8_t pad3
} as big_endian
cstruct ofp_oxm {
uint16_t oxm_class;
uint8_t oxm_field_and_hashmask;
uint8_t oxm_length
} as big_endian
cstruct ofp_meter_band_header {
uint16_t typ;
uint16_t len;
uint32_t rate;
uint32_t burst_size
} as big_endian
cstruct ofp_meter_band_drop {
uint16_t typ;
uint16_t len;
uint32_t rate;
uint32_t burst_size;
uint8_t pad[4]
} as big_endian
cstruct ofp_meter_band_dscp_remark {
uint16_t typ;
uint16_t len;
uint32_t rate;
uint32_t burst_size;
uint8_t prec_level;
uint8_t pad[3]
} as big_endian
cstruct ofp_meter_band_experimenter {
uint16_t typ;
uint16_t len;
uint32_t rate;
uint32_t burst_size;
uint32_t experimenter
} as big_endian
cenum ofp_meter_flags {
OFPMF_KBPS = 1;
OFPMF_PKTPS = 2;
OFPMF_BURST = 4;
OFPMF_STATS = 8;
} as uint32_t
cstruct ofp_multipart_request {
uint16_t typ; (* One of the OFPMP_* constants. *)
uint16_t flags; (* OFPMP_REQ_* flags (none yet defined). *)
uint8_t pad[4];
uint8_t body[0] (* Body of the request. *)
} as big_endian
cenum ofp_multipart_request_flags {
OFPMPF_REQ_MORE = 1 (* More requests to follow. *)
} as uint16_t
cenum ofp_multipart_reply_flags {
OFPMPF_REPLY_MORE = 1 (* More replies to follow. *)
} as uint16_t
cstruct ofp_multipart_reply {
uint16_t typ; (* One of the OFPMP_* constants. *)
uint16_t flags; (* OFPMP_REPLY_* flags. *)
uint8_t pad[4];
uint8_t body[0] (* Body of the reply. *)
} as big_endian
cenum ofp_multipart_types {
(* Description of this OpenFlow switch.
* The request body is empty.
* The reply body is struct ofp_desc. *)
OFPMP_DESC = 0;
(* Individual flow statistics.
* The request body is struct ofp_flow_multipart_request.
* The reply body is an array of struct ofp_flow_stats. *)
OFPMP_FLOW = 1;
(* Aggregate flow statistics.
* The request body is struct ofp_aggregate_stats_request.
* The reply body is struct ofp_aggregate_stats_reply. *)
OFPMP_AGGREGATE = 2;
(* Flow table statistics.
* The request body is empty.
* The reply body is an array of struct ofp_table_stats. *)
OFPMP_TABLE = 3;
(* Port statistics.
* The request body is struct ofp_port_stats_request.
* The reply body is an array of struct ofp_port_stats. *)
OFPMP_PORT_STATS = 4;
(* Queue statistics for a port
* The request body is struct ofp_queue_stats_request.
* The reply body is an array of struct ofp_queue_stats *)
OFPMP_QUEUE = 5;
(* Group counter statistics.
* The request body is struct ofp_group_stats_request.
* The reply is an array of struct ofp_group_stats. *)
OFPMP_GROUP = 6;
(* Group description statistics.
* The request body is empty.
* The reply body is an array of struct ofp_group_desc_stats. *)
OFPMP_GROUP_DESC = 7;
(* Group features.
* The request body is empty.
* The reply body is struct ofp_group_features_stats. *)
OFPMP_GROUP_FEATURES = 8;
(* Meter statistics.
* The request body is struct ofp_meter_multipart_requests.
* The reply body is an array of struct ofp_meter_stats. *)
OFPMP_METER = 9;
(* Meter configuration.
* The request body is struct ofp_meter_multipart_requests.
* The reply body is an array of struct ofp_meter_config. *)
OFPMP_METER_CONFIG = 10;
(* Meter features.
* The request body is empty.
* The reply body is struct ofp_meter_features. *)
OFPMP_METER_FEATURES = 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. *)
OFPMP_TABLE_FEATURES = 12;
(* Port description.
* The request body is empty.
* The reply body is an array of struct ofp_port. *)
OFPMP_PORT_DESC = 13;
(* Experimenter extension.
* The request and reply bodies begin with
* struct ofp_experimenter_stats_header.
* The request and reply bodies are otherwise experimenter-defined. *)
OFPMP_EXPERIMENTER = 0xffff
} as uint16_t
cstruct ofp_desc {
uint8_t mfr_desc[256];
uint8_t hw_desc[256];
uint8_t sw_desc[256];
uint8_t serial_num[32];
} as big_endian
cstruct ofp_uint8 {
uint8_t value
} as big_endian
cstruct ofp_uint16 {
uint16_t value
} as big_endian
cstruct ofp_uint24 {
uint16_t high;
uint8_t low
} as big_endian
cstruct ofp_uint32 {
uint32_t value
} as big_endian
cstruct ofp_uint48 {
uint32_t high;
uint16_t low
} as big_endian
cstruct ofp_uint64 {
uint64_t value
} as big_endian
cstruct ofp_uint128 {
uint64_t high;
uint64_t low
} as big_endian
let max_uint32 = 4294967296L (* = 2^32*)
let compare_uint32 a b =
(* val compare_uint32 : uint32 -> uint32 -> bool ; return a < b, for a, b uint32 *)
let a' = if a < 0l then
Int64.sub max_uint32 (Int64.of_int32 (Int32.abs a))
else Int64.of_int32 a in
let b' = if b < 0l then
Int64.sub max_uint32 (Int64.of_int32 (Int32.abs b))
else Int64.of_int32 b in
a' <= b'
let set_ofp_uint48_value (buf : Cstruct.t) (value : uint48) =
let high = Int64.to_int32 (Int64.shift_right_logical value 16) in
let low = ((Int64.to_int value) land 0xffff) in
set_ofp_uint48_high buf high;
set_ofp_uint48_low buf low
let get_ofp_uint48_value (buf : Cstruct.t) : uint48 =
let highBits = get_ofp_uint48_high buf in
let high = Int64.shift_left (
if highBits < 0l then
Int64.sub max_uint32 (Int64.of_int32 (Int32.abs highBits))
else
Int64.of_int32 highBits) 16 in
let low = Int64.of_int (get_ofp_uint48_low buf) in
Int64.logor low high
let get_ofp_uint24_value (buf : Cstruct.t) : uint24 =
let high = Int32.shift_left (Int32.of_int (get_ofp_uint24_high buf)) 8 in
let low = Int32.of_int (get_ofp_uint24_low buf )in
Int32.logor high low
let set_ofp_uint24_value (buf : Cstruct.t) (value : uint24) =
let high = (Int32.to_int value) lsr 8 in
let low = (Int32.to_int value) land 0xff in
set_ofp_uint24_high buf high;
set_ofp_uint24_low buf low
let set_ofp_uint128_value (buf : Cstruct.t) ((h,l) : uint128) =
set_ofp_uint128_high buf h;
set_ofp_uint128_low buf l
let get_ofp_uint128_value (buf : Cstruct.t) : uint128 =
(get_ofp_uint128_high buf, get_ofp_uint128_low buf)
let rec marshal_fields (buf: Cstruct.t) (fields : 'a list) (marshal_func : Cstruct.t -> 'a -> int ): int =
if (fields = []) then 0
else let size = marshal_func buf (List.hd fields) in
size + (marshal_fields (Cstruct.shift buf size) (List.tl fields) marshal_func)
let parse_fields (bits : Cstruct.t) (parse_func : Cstruct.t -> 'a) (length_func : Cstruct.t -> int option) :'a list =
let iter =
Cstruct.iter
length_func
parse_func
bits in
List.rev (Cstruct.fold (fun acc bits -> bits :: acc) iter [])
let pad_to_64bits (n : int) : int =
if n land 0x7 <> 0 then
n + (8 - (n land 0x7))
else
n
let rec pad_with_zeros (buf : Cstruct.t) (pad : int) : int =
if pad = 0 then 0
else begin set_ofp_uint8_value buf 0;
1 + pad_with_zeros (Cstruct.shift buf 1) (pad - 1) end
let test_bit16 (n:int) (x:int) : bool =
(x lsr n) land 1 = 1
module Oxm = struct
module IPv6ExtHdr = struct
type t = oxmIPv6ExtHdr
let marshal (hdr : t) : int16 =
(if hdr.noext then 1 lsl 0 else 0) lor
(if hdr.esp then 1 lsl 1 else 0) lor
(if hdr.auth then 1 lsl 2 else 0) lor
(if hdr.dest then 1 lsl 3 else 0) lor
(if hdr.frac then 1 lsl 4 else 0) lor
(if hdr.router then 1 lsl 5 else 0) lor
(if hdr.hop then 1 lsl 6 else 0) lor
(if hdr.unrep then 1 lsl 7 else 0) lor
(if hdr.unseq then 1 lsl 8 else 0)
let parse bits : t =
{ noext = test_bit16 0 bits
; esp = test_bit16 1 bits
; auth = test_bit16 2 bits
; dest = test_bit16 3 bits
; frac = test_bit16 4 bits
; router = test_bit16 5 bits
; hop = test_bit16 6 bits
; unrep = test_bit16 7 bits
; unseq = test_bit16 8 bits}
let to_string (t : t) : string =
Format.sprintf "{ noext = %B; esp = %B; auth = %B; dest = %B; frac = %B; router = %B; \
hop = %B; unrep = %B; unseq = %B }"
t.noext
t.esp
t.auth
t.dest
t.frac
t.router
t.hop
t.unrep
t.unseq
end
type t = oxm
let field_length (oxm : oxm) : int = match oxm with
| OxmInPort _ -> 4
| OxmInPhyPort _ -> 4
| OxmEthType _ -> 2
| OxmEthDst ethaddr ->
(match ethaddr.m_mask with
| None -> 6
| Some _ -> 12)
| OxmEthSrc ethaddr ->
(match ethaddr.m_mask with
| None -> 6
| Some _ -> 12)
| OxmVlanVId vid ->
(match vid.m_mask with
| None -> 2
| Some _ -> 4)
| OxmVlanPcp _ -> 1
| OxmIP4Src ipaddr ->
(match ipaddr.m_mask with
| None -> 4
| Some _ -> 8)
| OxmIP4Dst ipaddr ->
(match ipaddr.m_mask with
| None -> 4
| Some _ -> 8)
| OxmTCPSrc _ -> 2
| OxmTCPDst _ -> 2
| OxmARPOp _ -> 2
| OxmARPSpa t->
(match t.m_mask with
| None -> 4
| Some _ -> 8)
| OxmARPTpa t->
(match t.m_mask with
| None -> 4
| Some _ -> 8)
| OxmARPSha t->
(match t.m_mask with
| None -> 6
| Some _ -> 12)
| OxmARPTha t->
(match t.m_mask with
| None -> 6
| Some _ -> 12)
| OxmMPLSLabel _ -> 4
| OxmMPLSTc _ -> 1
| OxmMetadata t ->
(match t.m_mask with
| None -> 8
| Some _ -> 16)
| OxmIPProto _ -> 1
| OxmIPDscp _ -> 1
| OxmIPEcn _ -> 1
| OxmICMPType _ -> 1
| OxmICMPCode _ -> 1
| OxmTunnelId t ->
(match t.m_mask with
| None -> 8
| Some _ -> 16)
| OxmUDPSrc _ -> 2
| OxmUDPDst _ -> 2
| OxmSCTPSrc _ -> 2
| OxmSCTPDst _ -> 2
| OxmIPv6Src t ->
(match t.m_mask with
| None -> 16
| Some _ -> 32)
| OxmIPv6Dst t ->
(match t.m_mask with
| None -> 16
| Some _ -> 32)