From 529c7f17f65d63e8ac675be699dccbcd0a9368ff Mon Sep 17 00:00:00 2001 From: Ming Lu Date: Mon, 12 May 2025 17:44:08 +0800 Subject: [PATCH 1/3] 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 --- ocaml/networkd/bin_db/networkd_db.ml | 54 +++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/ocaml/networkd/bin_db/networkd_db.ml b/ocaml/networkd/bin_db/networkd_db.ml index f62021828fa..1f8a7afc321 100644 --- a/ocaml/networkd/bin_db/networkd_db.ml +++ b/ocaml/networkd/bin_db/networkd_db.ml @@ -32,18 +32,56 @@ let _ = try let config = Network_config.read_config () in if !bridge <> "" then - if List.mem_assoc !bridge config.bridge_config then ( + if List.mem_assoc !bridge config.bridge_config then let bridge_config = List.assoc !bridge config.bridge_config in let ifaces = List.concat_map (fun (_, port) -> port.interfaces) bridge_config.ports in - Printf.printf "interfaces=%s\n" (String.concat "," ifaces) ; - match bridge_config.vlan with - | None -> - () - | Some (parent, id) -> - Printf.printf "vlan=%d\nparent=%s\n" id parent - ) else ( + let mac_of_iface ~order name = + match List.find_opt (fun dev -> dev.name = name) order with + | Some dev -> + Ok (Macaddr.to_string dev.mac) + | None -> + Error (Printf.sprintf "Could not find MAC address of %s\n" name) + in + let macs = + match config.interface_order with + | Some order -> + List.map (mac_of_iface ~order) ifaces + |> List.fold_left + (fun acc mac -> + match (acc, mac) with + | Ok acc, Ok mac -> + Ok (mac :: acc) + | Ok _, Error msg -> + Error msg + | Error msg, _ -> + Error msg + ) + (Ok []) + | None -> + if ifaces <> [] then + (* Fallback to use the bridge MAC address when the interface_order + is not available. This can work only because the host installer + requires only one network interface to setup its own networking so far. *) + Ok (Option.to_list bridge_config.bridge_mac) + else (* No ifaces, no hwaddrs. *) + Ok [] + in + match macs with + | Error msg -> + rc := 1 ; + Printf.fprintf stderr "%s\n" msg + | Ok macs -> ( + Printf.printf "interfaces=%s\n" (String.concat "," ifaces) ; + Printf.printf "hwaddrs=%s\n" (String.concat "," (List.rev macs)) ; + match bridge_config.vlan with + | None -> + () + | Some (parent, id) -> + Printf.printf "vlan=%d\nparent=%s\n" id parent + ) + else ( rc := 1 ; Printf.fprintf stderr "Could not find bridge %s\n" !bridge ) ; From 6c56907556c61f408ea8e0c6b6ada16827f534a1 Mon Sep 17 00:00:00 2001 From: Ming Lu Date: Fri, 16 May 2025 09:47:35 +0800 Subject: [PATCH 2/3] fixup! CP-54444: Return MAC addresses to host installer Signed-off-by: Ming Lu --- ocaml/networkd/bin_db/networkd_db.ml | 105 +++++++++++++-------------- 1 file changed, 52 insertions(+), 53 deletions(-) diff --git a/ocaml/networkd/bin_db/networkd_db.ml b/ocaml/networkd/bin_db/networkd_db.ml index 1f8a7afc321..4c454aebf98 100644 --- a/ocaml/networkd/bin_db/networkd_db.ml +++ b/ocaml/networkd/bin_db/networkd_db.ml @@ -31,60 +31,59 @@ let _ = (Printf.sprintf "Usage: %s [-bridge | -iface ]" name) ; try let config = Network_config.read_config () in - if !bridge <> "" then - if List.mem_assoc !bridge config.bridge_config then - let bridge_config = List.assoc !bridge config.bridge_config in - let ifaces = - List.concat_map (fun (_, port) -> port.interfaces) bridge_config.ports - in - let mac_of_iface ~order name = - match List.find_opt (fun dev -> dev.name = name) order with - | Some dev -> - Ok (Macaddr.to_string dev.mac) - | None -> - Error (Printf.sprintf "Could not find MAC address of %s\n" name) - in - let macs = - match config.interface_order with - | Some order -> - List.map (mac_of_iface ~order) ifaces - |> List.fold_left - (fun acc mac -> - match (acc, mac) with - | Ok acc, Ok mac -> - Ok (mac :: acc) - | Ok _, Error msg -> - Error msg - | Error msg, _ -> - Error msg - ) - (Ok []) - | None -> - if ifaces <> [] then - (* Fallback to use the bridge MAC address when the interface_order - is not available. This can work only because the host installer - requires only one network interface to setup its own networking so far. *) - Ok (Option.to_list bridge_config.bridge_mac) - else (* No ifaces, no hwaddrs. *) - Ok [] - in - match macs with - | Error msg -> - rc := 1 ; - Printf.fprintf stderr "%s\n" msg - | Ok macs -> ( - Printf.printf "interfaces=%s\n" (String.concat "," ifaces) ; - Printf.printf "hwaddrs=%s\n" (String.concat "," (List.rev macs)) ; - match bridge_config.vlan with - | None -> - () - | Some (parent, id) -> - Printf.printf "vlan=%d\nparent=%s\n" id parent + ( if !bridge <> "" then + match List.assoc_opt !bridge config.bridge_config with + | Some bridge_config -> ( + let ifaces = + List.concat_map + (fun (_, port) -> port.interfaces) + bridge_config.ports + in + let macs = + let to_mac ~order name = + match List.find_opt (fun dev -> dev.name = name) order with + | Some dev -> + Either.Left (Macaddr.to_string dev.mac) + | None -> + Either.Right name + in + match (config.interface_order, ifaces) with + | Some order, ifaces -> + let oks, errs = List.partition_map (to_mac ~order) ifaces in + if errs = [] then + Ok oks + else + Error + (Printf.sprintf "Could not find MAC address(es) for %s" + (String.concat ", " errs) + ) + | None, [] -> + (* No ifaces, no hwaddrs. *) + Ok [] + | None, _ :: _ -> + (* Fallback to use the bridge MAC address when the interface_order + is not available. This can work only because the host installer + requires only one network interface to setup its own networking so far. *) + Ok (Option.to_list bridge_config.bridge_mac) + in + match macs with + | Error msg -> + rc := 1 ; + Printf.fprintf stderr "%s\n" msg + | Ok macs -> ( + Printf.printf "interfaces=%s\n" (String.concat "," ifaces) ; + Printf.printf "hwaddrs=%s\n" (String.concat "," macs) ; + match bridge_config.vlan with + | None -> + () + | Some (parent, id) -> + Printf.printf "vlan=%d\nparent=%s\n" id parent + ) ) - else ( - rc := 1 ; - Printf.fprintf stderr "Could not find bridge %s\n" !bridge - ) ; + | None -> + rc := 1 ; + Printf.fprintf stderr "Could not find bridge %s\n" !bridge + ) ; if !iface <> "" then if List.mem_assoc !iface config.interface_config then let interface_config = List.assoc !iface config.interface_config in From 470d6d1afa929a5048fa670d55908e4ec176f7cf Mon Sep 17 00:00:00 2001 From: Ming Lu Date: Fri, 16 May 2025 18:07:07 +0800 Subject: [PATCH 3/3] fixup! fixup! CP-54444: Return MAC addresses to host installer Signed-off-by: Ming Lu --- ocaml/networkd/bin_db/networkd_db.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ocaml/networkd/bin_db/networkd_db.ml b/ocaml/networkd/bin_db/networkd_db.ml index 4c454aebf98..94e5e4a77e3 100644 --- a/ocaml/networkd/bin_db/networkd_db.ml +++ b/ocaml/networkd/bin_db/networkd_db.ml @@ -48,7 +48,7 @@ let _ = Either.Right name in match (config.interface_order, ifaces) with - | Some order, ifaces -> + | Some order, _ :: _ -> let oks, errs = List.partition_map (to_mac ~order) ifaces in if errs = [] then Ok oks @@ -57,7 +57,7 @@ let _ = (Printf.sprintf "Could not find MAC address(es) for %s" (String.concat ", " errs) ) - | None, [] -> + | _, [] -> (* No ifaces, no hwaddrs. *) Ok [] | None, _ :: _ ->