Skip to content

Commit

Permalink
Merge pull request #2755 from robhoes/scan
Browse files Browse the repository at this point in the history
CA-220514: Protect PIF.scan with a mutex to avoid duplicate PIFs
  • Loading branch information
Jon Ludlam committed Sep 12, 2016
2 parents e7884e2 + f3d7ad7 commit a3df363
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions ocaml/xapi/xapi_pif.ml
Expand Up @@ -487,15 +487,11 @@ let forget ~__context ~self =
let t = make_tables ~__context ~host in
forget_internal ~t ~__context ~self

let scan_m = Mutex.create ()

let scan ~__context ~host =
let t = make_tables ~__context ~host in
let dbg = Context.string_of_task __context in

refresh_all ~__context ~host;
let devices_not_yet_represented_by_pifs =
List.set_difference
(List.map fst t.device_to_mac_table)
(List.map snd t.pif_to_device_table) in

let non_managed_devices, disallow_unplug_devices =
if Sys.file_exists !Xapi_globs.non_managed_pifs then
Expand All @@ -519,19 +515,27 @@ let scan ~__context ~host =
end
in

(* Create PIF records for the new interfaces *)
List.iter
(fun device ->
let mAC = List.assoc device t.device_to_mac_table in
let mTU = Int64.of_int (Net.Interface.get_mtu dbg ~name:device) in
let managed = not (List.mem device non_managed_devices) in
let disallow_unplug = (List.mem device disallow_unplug_devices) in
let (_: API.ref_PIF) =
introduce_internal
~t ~__context ~host ~mAC ~mTU ~vLAN:(-1L)
~vLAN_master_of:Ref.null ~device ~managed ~disallow_unplug () in
())
(devices_not_yet_represented_by_pifs);
Mutex.execute scan_m (fun () ->
let t = make_tables ~__context ~host in
let devices_not_yet_represented_by_pifs =
List.set_difference
(List.map fst t.device_to_mac_table)
(List.map snd t.pif_to_device_table) in

(* Create PIF records for the new interfaces *)
List.iter
(fun device ->
let mAC = List.assoc device t.device_to_mac_table in
let mTU = Int64.of_int (Net.Interface.get_mtu dbg ~name:device) in
let managed = not (List.mem device non_managed_devices) in
let disallow_unplug = (List.mem device disallow_unplug_devices) in
let (_: API.ref_PIF) =
introduce_internal
~t ~__context ~host ~mAC ~mTU ~vLAN:(-1L)
~vLAN_master_of:Ref.null ~device ~managed ~disallow_unplug () in
())
(devices_not_yet_represented_by_pifs)
);

(* Make sure the right PIF(s) are marked as management PIFs *)
update_management_flags ~__context ~host
Expand Down

0 comments on commit a3df363

Please sign in to comment.