Skip to content

Commit 2feba20

Browse files
committed
Improve pretty printing of VlanId field.
1 parent 2629acf commit 2feba20

File tree

2 files changed

+46
-37
lines changed

2 files changed

+46
-37
lines changed

lib/OpenFlow0x01_Core.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ module Format = struct
205205
pktIn.total_len pktIn.port reason pktIn.reason
206206
payload pktIn.input_payload
207207

208+
(* TODO(jnf): we have this defined in several places. Consolidate. *)
208209
let string_of_mk formatter x =
209210
let buf = Buffer.create 100 in
210211
let fmt = formatter_of_buffer buf in

lib/SDN_Types.ml

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,40 @@ type portId = int32 with sexp
1313
type queueId = int32 with sexp
1414
type bufferId = int32 with sexp
1515

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
1819

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
2122

2223
let format_hex (fmt : Format.formatter) (v:int) =
2324
Format.fprintf fmt "0x%x" v
2425

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)
2729

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)
3037

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
3441
(if m = 32l then "" else Printf.sprintf "/%ld" m)
3542

43+
(* convert a formatter to a function that produces a string *)
44+
(* TODO(jnf): we have this defined in several places. Consolidate. *)
3645
let make_string_of formatter x =
3746
let open Format in
3847
let buf = Buffer.create 100 in
3948
let fmt = formatter_of_buffer buf in
40-
pp_set_margin fmt 80;
49+
pp_set_margin fmt 200;
4150
formatter fmt x;
4251
fprintf fmt "@?";
4352
Buffer.contents buf
@@ -139,7 +148,7 @@ module Pattern = struct
139148
begin match m1 with
140149
| None -> false
141150
| Some(v1) -> f v1 v2
142-
end in
151+
end in
143152
check (=) p1.dlSrc p2.dlSrc
144153
&& check (=) p1.dlDst p2.dlDst
145154
&& check (=) p1.dlTyp p2.dlTyp
@@ -169,17 +178,17 @@ module Pattern = struct
169178
&& check (=) p1.tpSrc p2.tpSrc
170179
&& check (=) p1.tpDst p2.tpDst
171180
&& check (=) p1.inPort p2.inPort
172-
181+
173182
let eq_join x1 x2 =
174-
if x1 = x2 then Some x1 else None
183+
if x1 = x2 then Some x1 else None
175184

176185
let join p1 p2 =
177186
let joiner m m1 m2 =
178187
match m1, m2 with
179-
| Some v1, Some v2 ->
188+
| Some v1, Some v2 ->
180189
m v1 v2
181-
| _ ->
182-
None in
190+
| _ ->
191+
None in
183192
{ dlSrc = joiner eq_join p1.dlSrc p2.dlSrc
184193
; dlDst = joiner eq_join p1.dlDst p2.dlDst
185194
; dlTyp = joiner eq_join p1.dlTyp p2.dlTyp
@@ -205,7 +214,7 @@ module Pattern = struct
205214
format_field "ethSrc" format_mac p.dlSrc;
206215
format_field "ethDst" format_mac p.dlDst;
207216
format_field "ethTyp" format_hex p.dlTyp;
208-
format_field "vlanId" format_int p.dlVlan;
217+
format_field "vlanId" format_vlan p.dlVlan;
209218
format_field "vlanPcp" format_int p.dlVlanPcp;
210219
format_field "nwProto" format_hex p.nwProto;
211220
format_field "ipSrc" format_ip_mask p.nwSrc;
@@ -263,10 +272,10 @@ type flow = {
263272
hard_timeout: timeout
264273
}
265274

266-
type flowTable = flow list
275+
type flowTable = flow list
267276

268277
type payload =
269-
| Buffered of bufferId * bytes
278+
| Buffered of bufferId * bytes
270279
| NotBuffered of bytes
271280
with sexp
272281

@@ -307,11 +316,10 @@ let format_modify (fmt:Format.formatter) (m:modify) : unit =
307316
Format.fprintf fmt "SetField(ethSrc, %a)" format_mac dlAddr
308317
| SetEthDst(dlAddr) ->
309318
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
315323
| SetVlanPcp(pcp) ->
316324
Format.fprintf fmt "SetField(vlanPcp, %u)" pcp
317325
| SetEthTyp(dlTyp) ->
@@ -339,10 +347,10 @@ let format_pseudoport (fmt:Format.formatter) (p:pseudoport) : unit =
339347
| Local -> Format.fprintf fmt "Local"
340348

341349
let format_action (fmt:Format.formatter) (a:action) : unit =
342-
match a with
350+
match a with
343351
| Output(p) ->
344352
Format.fprintf fmt "Output(%a)" format_pseudoport p
345-
| Enqueue(m,n) ->
353+
| Enqueue(m,n) ->
346354
Format.fprintf fmt "Enqueue(%ld,%ld)" m n
347355
| Modify(m) ->
348356
format_modify fmt m
@@ -367,27 +375,27 @@ let rec format_group (fmt : Format.formatter) (group : group) : unit =
367375
| [par] -> format_par fmt par
368376
| (par :: par' :: groups) ->
369377
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
373381
| Permanent -> Format.fprintf fmt "Permanent"
374382
| ExpiresAfter(n) -> Format.fprintf fmt "ExpiresAfter(%d)" n
375383

376-
let format_flow (fmt: Format.formatter) (f : flow) : unit =
384+
let format_flow (fmt: Format.formatter) (f : flow) : unit =
377385
Format.fprintf fmt "@[{pattern=%a,@," Pattern.format f.pattern;
378386
Format.fprintf fmt "action=%a,@," format_group f.action;
379387
Format.fprintf fmt "cookie=%s,@," (Int64.to_string f.cookie);
380388
Format.fprintf fmt "idle_timeout=%a,@," format_timeout f.idle_timeout;
381389
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 =
384392
Format.fprintf fmt "@[[";
385-
let _ =
393+
let _ =
386394
List.fold_left
387-
(fun b f ->
395+
(fun b f ->
388396
if b then Format.fprintf fmt "@ ";
389397
format_flow fmt f;
390-
true) false l in
398+
true) false l in
391399
Format.fprintf fmt "]@]"
392400

393401
let string_of_action = make_string_of format_action

0 commit comments

Comments
 (0)