@@ -13,26 +13,35 @@ type portId = int32 with sexp
13
13
type queueId = int32 with sexp
14
14
type bufferId = int32 with sexp
15
15
16
- let format_mac (fmt : Format.formatter ) (v :int48 ) =
17
- Format. pp_print_string fmt (Packet. string_of_mac v)
16
+ (* general formatters for numeric types *)
17
+ let format_int (fmt : Format.formatter ) (v :int ) =
18
+ Format. fprintf fmt " %u" v
18
19
19
- let format_ip (fmt : Format.formatter ) (v :int32 ) =
20
- Format. pp_print_string fmt ( Packet. string_of_ip v)
20
+ let format_int32 (fmt : Format.formatter ) (v :int32 ) =
21
+ Format. fprintf fmt " %lu " v
21
22
22
23
let format_hex (fmt : Format.formatter ) (v :int ) =
23
24
Format. fprintf fmt " 0x%x" v
24
25
25
- let format_int (fmt : Format.formatter ) (v :int ) =
26
- Format. fprintf fmt " %u" v
26
+ (* formatters for packet fields *)
27
+ let format_mac (fmt : Format.formatter ) (v :int48 ) =
28
+ Format. pp_print_string fmt (Packet. string_of_mac v)
27
29
28
- let format_int32 (fmt : Format.formatter ) (v :int32 ) =
29
- Format. fprintf fmt " %lu" v
30
+ let format_vlan (fmt : Format.formatter ) (v :int16 ) =
31
+ match v with
32
+ | 0xffff -> Format. pp_print_string fmt " <none>"
33
+ | _ -> format_int fmt v
34
+
35
+ let format_ip (fmt : Format.formatter ) (v :int32 ) =
36
+ Format. pp_print_string fmt (Packet. string_of_ip v)
30
37
31
- let format_ip_mask (fmt : Format.formatter ) ((p ,m ) : nwAddr * int32 ) =
32
- Format. fprintf fmt " %a%s"
33
- format_ip p
38
+ let format_ip_mask (fmt : Format.formatter ) ((p ,m ) : nwAddr * int32 ) =
39
+ Format. fprintf fmt " %a%s"
40
+ format_ip p
34
41
(if m = 32l then " " else Printf. sprintf " /%ld" m)
35
42
43
+ (* convert a formatter to a function that produces a string *)
44
+ (* TODO(jnf): we have this defined in several places. Consolidate. *)
36
45
let make_string_of formatter x =
37
46
let open Format in
38
47
let buf = Buffer. create 100 in
@@ -139,7 +148,7 @@ module Pattern = struct
139
148
begin match m1 with
140
149
| None -> false
141
150
| Some (v1 ) -> f v1 v2
142
- end in
151
+ end in
143
152
check (= ) p1.dlSrc p2.dlSrc
144
153
&& check (= ) p1.dlDst p2.dlDst
145
154
&& check (= ) p1.dlTyp p2.dlTyp
@@ -169,17 +178,17 @@ module Pattern = struct
169
178
&& check (= ) p1.tpSrc p2.tpSrc
170
179
&& check (= ) p1.tpDst p2.tpDst
171
180
&& check (= ) p1.inPort p2.inPort
172
-
181
+
173
182
let eq_join x1 x2 =
174
- if x1 = x2 then Some x1 else None
183
+ if x1 = x2 then Some x1 else None
175
184
176
185
let join p1 p2 =
177
186
let joiner m m1 m2 =
178
187
match m1, m2 with
179
- | Some v1 , Some v2 ->
188
+ | Some v1 , Some v2 ->
180
189
m v1 v2
181
- | _ ->
182
- None in
190
+ | _ ->
191
+ None in
183
192
{ dlSrc = joiner eq_join p1.dlSrc p2.dlSrc
184
193
; dlDst = joiner eq_join p1.dlDst p2.dlDst
185
194
; dlTyp = joiner eq_join p1.dlTyp p2.dlTyp
@@ -205,7 +214,7 @@ module Pattern = struct
205
214
format_field " ethSrc" format_mac p.dlSrc;
206
215
format_field " ethDst" format_mac p.dlDst;
207
216
format_field " ethTyp" format_hex p.dlTyp;
208
- format_field " vlanId" format_int p.dlVlan;
217
+ format_field " vlanId" format_vlan p.dlVlan;
209
218
format_field " vlanPcp" format_int p.dlVlanPcp;
210
219
format_field " nwProto" format_hex p.nwProto;
211
220
format_field " ipSrc" format_ip_mask p.nwSrc;
@@ -263,10 +272,10 @@ type flow = {
263
272
hard_timeout : timeout
264
273
}
265
274
266
- type flowTable = flow list
275
+ type flowTable = flow list
267
276
268
277
type payload =
269
- | Buffered of bufferId * bytes
278
+ | Buffered of bufferId * bytes
270
279
| NotBuffered of bytes
271
280
with sexp
272
281
@@ -307,11 +316,10 @@ let format_modify (fmt:Format.formatter) (m:modify) : unit =
307
316
Format. fprintf fmt " SetField(ethSrc, %a)" format_mac dlAddr
308
317
| SetEthDst (dlAddr ) ->
309
318
Format. fprintf fmt " SetField(ethDst, %a)" format_mac dlAddr
310
- | SetVlan (None )
311
- | SetVlan (Some(0xffff )) ->
312
- Format. fprintf fmt " SetField(vlan, <none>)"
313
- | SetVlan (Some(id )) ->
314
- Format. fprintf fmt " SetField(vlan, %u)" id
319
+ | SetVlan (None) ->
320
+ Format. fprintf fmt " SetField(vlan, %a)" format_vlan 0xffff
321
+ | SetVlan (Some(vlan_id )) ->
322
+ Format. fprintf fmt " SetField(vlan, %a)" format_vlan vlan_id
315
323
| SetVlanPcp (pcp ) ->
316
324
Format. fprintf fmt " SetField(vlanPcp, %u)" pcp
317
325
| SetEthTyp (dlTyp ) ->
@@ -339,10 +347,10 @@ let format_pseudoport (fmt:Format.formatter) (p:pseudoport) : unit =
339
347
| Local -> Format. fprintf fmt " Local"
340
348
341
349
let format_action (fmt :Format.formatter ) (a :action ) : unit =
342
- match a with
350
+ match a with
343
351
| Output (p ) ->
344
352
Format. fprintf fmt " Output(%a)" format_pseudoport p
345
- | Enqueue (m ,n ) ->
353
+ | Enqueue (m ,n ) ->
346
354
Format. fprintf fmt " Enqueue(%ld,%ld)" m n
347
355
| Modify (m ) ->
348
356
format_modify fmt m
@@ -367,27 +375,27 @@ let rec format_group (fmt : Format.formatter) (group : group) : unit =
367
375
| [par] -> format_par fmt par
368
376
| (par :: par' :: groups ) ->
369
377
Format. fprintf fmt " @[%a +@ %a@]" format_par par format_group (par' :: groups)
370
-
371
- let format_timeout (fmt :Format.formatter ) (t :timeout ) : unit =
372
- match t with
378
+
379
+ let format_timeout (fmt :Format.formatter ) (t :timeout ) : unit =
380
+ match t with
373
381
| Permanent -> Format. fprintf fmt " Permanent"
374
382
| ExpiresAfter (n ) -> Format. fprintf fmt " ExpiresAfter(%d)" n
375
383
376
- let format_flow (fmt : Format.formatter ) (f : flow ) : unit =
384
+ let format_flow (fmt : Format.formatter ) (f : flow ) : unit =
377
385
Format. fprintf fmt " @[{pattern=%a,@," Pattern. format f.pattern;
378
386
Format. fprintf fmt " action=%a,@," format_group f.action;
379
387
Format. fprintf fmt " cookie=%s,@," (Int64. to_string f.cookie);
380
388
Format. fprintf fmt " idle_timeout=%a,@," format_timeout f.idle_timeout;
381
389
Format. fprintf fmt " hard_timeout=%a}@]" format_timeout f.hard_timeout
382
-
383
- let format_flowTable (fmt :Format.formatter ) (l :flowTable ) : unit =
390
+
391
+ let format_flowTable (fmt :Format.formatter ) (l :flowTable ) : unit =
384
392
Format. fprintf fmt " @[[" ;
385
- let _ =
393
+ let _ =
386
394
List. fold_left
387
- (fun b f ->
395
+ (fun b f ->
388
396
if b then Format. fprintf fmt " @ " ;
389
397
format_flow fmt f;
390
- true ) false l in
398
+ true ) false l in
391
399
Format. fprintf fmt " ]@]"
392
400
393
401
let string_of_action = make_string_of format_action
0 commit comments