Skip to content

Commit b2e7113

Browse files
committed
CP-44107: Add Interface.get_interface_positions
If networkd config.interface_order is None, then sort based on the renamed interfaces name "ethx" to indicate its position, else get positions from config.interface_order. Signed-off-by: Changlei Li <[email protected]>
1 parent 61deaa3 commit b2e7113

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

ocaml/networkd/bin/network_server.ml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ let read_config () =
9797
error "Could not interpret the configuration in management.conf"
9898
)
9999

100+
let get_index_from_ethx name =
101+
if String.starts_with ~prefix:"eth" name then
102+
let index = String.sub name 3 (String.length name - 3) in
103+
int_of_string_opt index
104+
else
105+
None
106+
107+
let sort_based_on_ethx () =
108+
Sysfs.list ()
109+
|> List.filter_map (fun name ->
110+
if Sysfs.is_physical name then
111+
get_index_from_ethx name |> Option.map (fun i -> (name, i))
112+
else
113+
None
114+
)
115+
100116
let on_shutdown signal =
101117
let dbg = "shutdown" in
102118
Debug.with_thread_associated dbg
@@ -321,6 +337,24 @@ module Interface = struct
321337
let get_all dbg () =
322338
Debug.with_thread_associated dbg (fun () -> Sysfs.list ()) ()
323339

340+
let get_interface_positions dbg () =
341+
Debug.with_thread_associated dbg
342+
(fun () ->
343+
match !config.interface_order with
344+
| Some order ->
345+
List.filter_map
346+
(fun dev ->
347+
if dev.present then
348+
Some (dev.name, dev.position)
349+
else
350+
None
351+
)
352+
order
353+
| None ->
354+
sort_based_on_ethx ()
355+
)
356+
()
357+
324358
let exists dbg name =
325359
Debug.with_thread_associated dbg
326360
(fun () -> List.mem name (Sysfs.list ()))

ocaml/networkd/bin/networkd.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ let bind () =
183183
S.set_gateway_interface set_gateway_interface ;
184184
S.set_dns_interface set_dns_interface ;
185185
S.Interface.get_all Interface.get_all ;
186+
S.Interface.get_interface_positions Interface.get_interface_positions ;
186187
S.Interface.exists Interface.exists ;
187188
S.Interface.get_mac Interface.get_mac ;
188189
S.Interface.get_pci_bus_path Interface.get_pci_bus_path ;

ocaml/xapi-idl/network/network_interface.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,18 @@ module Interface_API (R : RPC) = struct
420420
["Get list of all interface names"]
421421
(debug_info_p @-> unit_p @-> returning iface_list_p err)
422422

423+
let get_interface_positions =
424+
let module T = struct
425+
type _iface_position_list_t = (iface * int) list [@@deriving rpcty]
426+
end in
427+
let iface_position_list_p =
428+
Param.mk ~description:["interface postion list"]
429+
T._iface_position_list_t
430+
in
431+
declare "Interface.get_interface_positions"
432+
["Get list of interface names and their positions"]
433+
(debug_info_p @-> unit_p @-> returning iface_position_list_p err)
434+
423435
let exists =
424436
let result = Param.mk ~description:["existence"] Types.bool in
425437
declare "Interface.exists"

0 commit comments

Comments
 (0)