Skip to content

Commit 6c56907

Browse files
committed
fixup! CP-54444: Return MAC addresses to host installer
Signed-off-by: Ming Lu <[email protected]>
1 parent 529c7f1 commit 6c56907

File tree

1 file changed

+52
-53
lines changed

1 file changed

+52
-53
lines changed

ocaml/networkd/bin_db/networkd_db.ml

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -31,60 +31,59 @@ let _ =
3131
(Printf.sprintf "Usage: %s [-bridge <bridge> | -iface <interface>]" name) ;
3232
try
3333
let config = Network_config.read_config () in
34-
if !bridge <> "" then
35-
if List.mem_assoc !bridge config.bridge_config then
36-
let bridge_config = List.assoc !bridge config.bridge_config in
37-
let ifaces =
38-
List.concat_map (fun (_, port) -> port.interfaces) bridge_config.ports
39-
in
40-
let mac_of_iface ~order name =
41-
match List.find_opt (fun dev -> dev.name = name) order with
42-
| Some dev ->
43-
Ok (Macaddr.to_string dev.mac)
44-
| None ->
45-
Error (Printf.sprintf "Could not find MAC address of %s\n" name)
46-
in
47-
let macs =
48-
match config.interface_order with
49-
| Some order ->
50-
List.map (mac_of_iface ~order) ifaces
51-
|> List.fold_left
52-
(fun acc mac ->
53-
match (acc, mac) with
54-
| Ok acc, Ok mac ->
55-
Ok (mac :: acc)
56-
| Ok _, Error msg ->
57-
Error msg
58-
| Error msg, _ ->
59-
Error msg
60-
)
61-
(Ok [])
62-
| None ->
63-
if ifaces <> [] then
64-
(* Fallback to use the bridge MAC address when the interface_order
65-
is not available. This can work only because the host installer
66-
requires only one network interface to setup its own networking so far. *)
67-
Ok (Option.to_list bridge_config.bridge_mac)
68-
else (* No ifaces, no hwaddrs. *)
69-
Ok []
70-
in
71-
match macs with
72-
| Error msg ->
73-
rc := 1 ;
74-
Printf.fprintf stderr "%s\n" msg
75-
| Ok macs -> (
76-
Printf.printf "interfaces=%s\n" (String.concat "," ifaces) ;
77-
Printf.printf "hwaddrs=%s\n" (String.concat "," (List.rev macs)) ;
78-
match bridge_config.vlan with
79-
| None ->
80-
()
81-
| Some (parent, id) ->
82-
Printf.printf "vlan=%d\nparent=%s\n" id parent
34+
( if !bridge <> "" then
35+
match List.assoc_opt !bridge config.bridge_config with
36+
| Some bridge_config -> (
37+
let ifaces =
38+
List.concat_map
39+
(fun (_, port) -> port.interfaces)
40+
bridge_config.ports
41+
in
42+
let macs =
43+
let to_mac ~order name =
44+
match List.find_opt (fun dev -> dev.name = name) order with
45+
| Some dev ->
46+
Either.Left (Macaddr.to_string dev.mac)
47+
| None ->
48+
Either.Right name
49+
in
50+
match (config.interface_order, ifaces) with
51+
| Some order, ifaces ->
52+
let oks, errs = List.partition_map (to_mac ~order) ifaces in
53+
if errs = [] then
54+
Ok oks
55+
else
56+
Error
57+
(Printf.sprintf "Could not find MAC address(es) for %s"
58+
(String.concat ", " errs)
59+
)
60+
| None, [] ->
61+
(* No ifaces, no hwaddrs. *)
62+
Ok []
63+
| None, _ :: _ ->
64+
(* Fallback to use the bridge MAC address when the interface_order
65+
is not available. This can work only because the host installer
66+
requires only one network interface to setup its own networking so far. *)
67+
Ok (Option.to_list bridge_config.bridge_mac)
68+
in
69+
match macs with
70+
| Error msg ->
71+
rc := 1 ;
72+
Printf.fprintf stderr "%s\n" msg
73+
| Ok macs -> (
74+
Printf.printf "interfaces=%s\n" (String.concat "," ifaces) ;
75+
Printf.printf "hwaddrs=%s\n" (String.concat "," macs) ;
76+
match bridge_config.vlan with
77+
| None ->
78+
()
79+
| Some (parent, id) ->
80+
Printf.printf "vlan=%d\nparent=%s\n" id parent
81+
)
8382
)
84-
else (
85-
rc := 1 ;
86-
Printf.fprintf stderr "Could not find bridge %s\n" !bridge
87-
) ;
83+
| None ->
84+
rc := 1 ;
85+
Printf.fprintf stderr "Could not find bridge %s\n" !bridge
86+
) ;
8887
if !iface <> "" then
8988
if List.mem_assoc !iface config.interface_config then
9089
let interface_config = List.assoc !iface config.interface_config in

0 commit comments

Comments
 (0)