Skip to content

Commit 2679dc3

Browse files
committed
fixup! fixup! fixup! CP-54444: Return MAC addresses to host installer
Signed-off-by: Ming Lu <[email protected]>
1 parent 470d6d1 commit 2679dc3

File tree

1 file changed

+58
-51
lines changed

1 file changed

+58
-51
lines changed

ocaml/networkd/bin_db/networkd_db.ml

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ open Network_interface
1616

1717
let name = "networkd_db"
1818

19+
type error = Skip | Msg of string
20+
21+
let ( let* ) = Result.bind
22+
1923
let _ =
2024
let bridge = ref "" in
2125
let iface = ref "" in
@@ -31,58 +35,61 @@ let _ =
3135
(Printf.sprintf "Usage: %s [-bridge <bridge> | -iface <interface>]" name) ;
3236
try
3337
let config = Network_config.read_config () in
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, _ :: _ ->
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-
| _, [] ->
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-
)
82-
)
38+
let r =
39+
let* bridge = if !bridge = "" then Error Skip else Ok !bridge in
40+
let* bridge_config =
41+
match List.assoc_opt bridge config.bridge_config with
42+
| Some bridge_config ->
43+
Ok bridge_config
8344
| None ->
84-
rc := 1 ;
85-
Printf.fprintf stderr "Could not find bridge %s\n" !bridge
45+
Error (Msg (Printf.sprintf "Could not find bridge %s\n" bridge))
46+
in
47+
let ifaces =
48+
List.concat_map (fun (_, port) -> port.interfaces) bridge_config.ports
49+
in
50+
let* macs =
51+
let to_mac ~order name =
52+
match List.find_opt (fun dev -> dev.name = name) order with
53+
| Some dev ->
54+
Either.Left (Macaddr.to_string dev.mac)
55+
| None ->
56+
Either.Right name
57+
in
58+
match (config.interface_order, ifaces) with
59+
| Some order, _ :: _ ->
60+
let oks, errs = List.partition_map (to_mac ~order) ifaces in
61+
if errs = [] then
62+
Ok oks
63+
else
64+
Error
65+
(Msg
66+
(Printf.sprintf "Could not find MAC address(es) for %s"
67+
(String.concat ", " errs)
68+
)
69+
)
70+
| _, [] ->
71+
(* No ifaces, no hwaddrs. *)
72+
Ok []
73+
| None, _ :: _ ->
74+
(* Fallback to use the bridge MAC address when the interface_order
75+
is not available. This can work only because the host installer
76+
requires only one network interface to setup its own networking so far. *)
77+
Ok (Option.to_list bridge_config.bridge_mac)
78+
in
79+
Printf.printf "interfaces=%s\n" (String.concat "," ifaces) ;
80+
Printf.printf "hwaddrs=%s\n" (String.concat "," macs) ;
81+
match bridge_config.vlan with
82+
| None ->
83+
Ok ()
84+
| Some (parent, id) ->
85+
Ok (Printf.printf "vlan=%d\nparent=%s\n" id parent)
86+
in
87+
( match r with
88+
| Ok () | Error Skip ->
89+
()
90+
| Error (Msg msg) ->
91+
rc := 1 ;
92+
Printf.fprintf stderr "%s" msg
8693
) ;
8794
if !iface <> "" then
8895
if List.mem_assoc !iface config.interface_config then

0 commit comments

Comments
 (0)