Skip to content

Commit 27d5d7e

Browse files
committed
CP-53614: Update xapi pif introduce
Xapi get interface position from `get_interface_positions`, instead of getting position from "ethx" name. Signed-off-by: Changlei Li <[email protected]>
1 parent b2e7113 commit 27d5d7e

File tree

3 files changed

+50
-31
lines changed

3 files changed

+50
-31
lines changed

ocaml/xapi/helpers.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,11 @@ let call_script ?(log_output = Always) ?env ?stdin ?timeout script args =
129129
raise e
130130

131131
(** Construct a descriptive network name (used as name_label) for a give network interface. *)
132-
let choose_network_name_for_pif device =
133-
Printf.sprintf "Pool-wide network associated with %s" device
132+
let choose_network_name_for_pif device pos_opt =
133+
let pos_str =
134+
Option.fold ~none:"" ~some:(Printf.sprintf " (slot %d)") pos_opt
135+
in
136+
Printf.sprintf "Pool-wide network associated with %s%s" device pos_str
134137

135138
(* !! FIXME - trap proper MISSINGREFERENCE exception when this has been defined *)
136139
(* !! FIXME(2) - this code could be shared with the CLI? *)

ocaml/xapi/xapi_pif.ml

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,12 @@ let refresh_all ~__context ~host =
114114
in
115115
List.iter (fun self -> refresh_internal ~__context ~self) pifs
116116

117-
let bridge_naming_convention (device : string) =
118-
if String.starts_with ~prefix:"eth" device then
119-
"xenbr" ^ String.sub device 3 (String.length device - 3)
120-
else
121-
"br" ^ device
117+
let bridge_naming_convention (device : string) (pos_opt : int option) =
118+
match pos_opt with
119+
| Some index ->
120+
"xenbr" ^ string_of_int index
121+
| None ->
122+
"br" ^ device
122123

123124
let read_bridges_from_inventory () =
124125
try String.split ' ' (Xapi_inventory.lookup Xapi_inventory._current_interfaces)
@@ -337,8 +338,8 @@ let assert_fcoe_not_in_use ~__context ~self =
337338
()
338339
)
339340

340-
let find_or_create_network (bridge : string) (device : string) ~managed
341-
~__context =
341+
let find_or_create_network (bridge : string) (device : string)
342+
(pos_opt : int option) ~managed ~__context =
342343
let nets =
343344
Db.Network.get_refs_where ~__context
344345
~expr:(Eq (Field "bridge", Literal bridge))
@@ -352,41 +353,52 @@ let find_or_create_network (bridge : string) (device : string) ~managed
352353
let () =
353354
Db.Network.create ~__context ~ref:net_ref ~uuid:net_uuid
354355
~current_operations:[] ~allowed_operations:[]
355-
~name_label:(Helpers.choose_network_name_for_pif device)
356+
~name_label:(Helpers.choose_network_name_for_pif device pos_opt)
356357
~name_description:"" ~mTU:1500L ~purpose:[] ~bridge ~managed
357358
~other_config:[] ~blobs:[] ~tags:[] ~default_locking_mode:`unlocked
358359
~assigned_ips:[]
359360
in
360361
net_ref
361362

362363
type tables = {
363-
device_to_mac_table: (string * string) list
364+
device_to_position_table: (string * int) list
365+
; device_to_mac_table: (string * string) list
364366
; pif_to_device_table: (API.ref_PIF * string) list
365367
}
366368

367369
let make_tables ~__context ~host =
368370
let dbg = Context.string_of_task __context in
369-
let devices =
370-
List.filter
371-
(fun name -> Net.Interface.is_physical dbg name)
372-
(Net.Interface.get_all dbg ())
371+
let device_to_position_table = Net.Interface.get_interface_positions dbg () in
372+
let device_to_mac_table =
373+
List.map
374+
(fun (name, _) -> (name, Net.Interface.get_mac dbg name))
375+
device_to_position_table
373376
in
374-
let pifs =
377+
(* Get all PIFs on this host *)
378+
let pif_to_device_table =
375379
Db.PIF.get_records_where ~__context
376380
~expr:
377381
(And
378382
( Eq (Field "host", Literal (Ref.string_of host))
379383
, Eq (Field "physical", Literal "true")
380384
)
381385
)
386+
|> List.map (fun (pref, prec) -> (pref, prec.API.pIF_device))
382387
in
383-
{
384-
device_to_mac_table=
385-
List.combine devices
386-
(List.map (fun name -> Net.Interface.get_mac dbg name) devices)
387-
; pif_to_device_table=
388-
List.map (fun (pref, prec) -> (pref, prec.API.pIF_device)) pifs
389-
}
388+
debug "tables: device_to_position_table = %s"
389+
(String.concat "; "
390+
(List.map
391+
(fun (d, p) -> d ^ ":" ^ string_of_int p)
392+
device_to_position_table
393+
)
394+
) ;
395+
debug "tables: device_to_mac_table = %s"
396+
(String.concat "; "
397+
(List.map (fun (d, m) -> d ^ ":" ^ m) device_to_mac_table)
398+
) ;
399+
debug "tables: pif_to_device_table = %s"
400+
(String.concat "; " (List.map snd pif_to_device_table)) ;
401+
{device_to_position_table; device_to_mac_table; pif_to_device_table}
390402

391403
let is_my_management_pif ~__context ~self =
392404
let net = Db.PIF.get_network ~__context ~self in
@@ -445,16 +457,19 @@ let db_introduce = pool_introduce
445457
let db_forget ~__context ~self = Db.PIF.destroy ~__context ~self
446458

447459
(* Internal [introduce] is passed a pre-built table [t] *)
448-
let introduce_internal ?network ?(physical = true) ~t:_ ~__context ~host ~mAC
449-
~mTU ~device ~vLAN ~vLAN_master_of ?metrics ~managed
450-
?(disallow_unplug = false) () =
451-
let bridge = if managed then bridge_naming_convention device else "" in
460+
let introduce_internal ?network ?(physical = true) ~t ~__context ~host ~mAC ~mTU
461+
~device ~vLAN ~vLAN_master_of ?metrics ~managed ?(disallow_unplug = false)
462+
() =
463+
let pos_opt = List.assoc_opt device t.device_to_position_table in
464+
let bridge =
465+
if managed then bridge_naming_convention device pos_opt else ""
466+
in
452467
(* If we are not told which network to use,
453468
* apply the default convention *)
454469
let net_ref =
455470
match network with
456471
| None ->
457-
find_or_create_network bridge device ~managed ~__context
472+
find_or_create_network bridge device pos_opt ~managed ~__context
458473
| Some x ->
459474
x
460475
in

ocaml/xapi/xapi_pif.mli

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,10 @@ val plug : __context:Context.t -> self:[`PIF] Ref.t -> unit
160160

161161
(** {2 Miscellaneous Helper Functions} *)
162162

163-
val bridge_naming_convention : string -> string
164-
(** Constructs a bridge name from a device (network interface) name by replacing
165-
* [eth] by [xenbr], or prepending [br] if the device name does not start with [eth].
163+
val bridge_naming_convention : string -> int option -> string
164+
(** Constructs a bridge name from a [device] (network interface) name and an optional
165+
position [pos_opt]. If [pos_opt] is Some [pos], the bridge name will be
166+
"xenbr" ^ [pos], else "br" ^ [device].
166167
*)
167168

168169
val read_bridges_from_inventory : unit -> string list

0 commit comments

Comments
 (0)