Skip to content

Commit 846ce82

Browse files
committed
CP-54444: Return MAC addresses to host installer
The host installer uses this utility to get the management interface from the management bridge. Now it changes to use MAC address(es) to find out the management interface(s). This is because the interface-rename functionality will be deprecated and the names of the network interfaces are not guaranteed to be the same between dom0 and host installer's running environment. Note that this change must be delivered to a host before upgrading to a new version in which the interface-rename is deprecated because the host installer is built from the new version and it will not be able to find the management network interface by name if the networkd_db command returns only names generated by interface-rename. Specifically, the "interface_order" field is only available when the networkd takes place of interface-rename to generate order. Before that, only the "bridge_mac" can be used because at that time, the host installer only uses one interface to setup its own networking during installation and no MAC addresses are recorded in networkd.db for individual interfaces. The "bridge_mac" is just the MAC address of one of the interfaces which construct the management bridge. Signed-off-by: Ming Lu <[email protected]>
1 parent d248e2f commit 846ce82

File tree

1 file changed

+55
-14
lines changed

1 file changed

+55
-14
lines changed

ocaml/networkd/bin_db/networkd_db.ml

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

0 commit comments

Comments
 (0)