Skip to content

Commit ff82426

Browse files
committed
CP-312083: [xapi] Expose received LLDP neighbour on PIF_metrics
Monitor_dbcalls now reads iface_stats.lldp_neighbor from /dev/shm/network_stats, encodes it as a (system-name, port-id, port-description) map, and writes it to PIF_metrics.lldp_neighbor for the matching PIF on the host. A per-device cache (mirroring the pif/bond caches) ensures the database is only updated when the neighbour changes; an empty map is written when no neighbour is seen.
1 parent af53918 commit ff82426

2 files changed

Lines changed: 73 additions & 3 deletions

File tree

ocaml/xapi/monitor_dbcalls.ml

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ module D = Debug.Make (struct let name = "monitor_dbcalls" end)
2222

2323
open D
2424

25+
(* Encode a received LLDP neighbour as a (key, value) map for
26+
PIF_metrics.lldp_neighbor, omitting fields that were not advertised. *)
27+
let lldp_map_of_rx (rx : Network_stats.lldp_rx) : (string * string) list =
28+
List.filter_map
29+
(fun (k, v) -> Option.map (fun x -> (k, x)) v)
30+
[
31+
("system-name", rx.Network_stats.system_name)
32+
; ("port-id", rx.Network_stats.port_id)
33+
; ("port-description", rx.Network_stats.port_description)
34+
]
35+
2536
let get_pif_and_bond_changes () =
2637
(* Read fresh PIF information from networkd. *)
2738
let open Network_stats in
@@ -42,7 +53,14 @@ let get_pif_and_bond_changes () =
4253
; pif_device_id= stat.device_id
4354
}
4455
in
45-
Hashtbl.add pifs_tmp pif.pif_name pif
56+
Hashtbl.add pifs_tmp pif.pif_name pif ;
57+
Hashtbl.replace lldp_neighbor_tmp dev
58+
(match stat.lldp_neighbor with
59+
| Some rx ->
60+
lldp_map_of_rx rx
61+
| None ->
62+
[]
63+
)
4664
)
4765
)
4866
stats ;
@@ -52,8 +70,12 @@ let get_pif_and_bond_changes () =
5270
let bond_changes =
5371
get_updates_map ~before:bonds_links_up_cached ~after:bonds_links_up_tmp
5472
in
73+
(* Check if any received LLDP neighbour has changed since our last reading. *)
74+
let lldp_changes =
75+
get_updates_map ~before:lldp_neighbor_cached ~after:lldp_neighbor_tmp
76+
in
5577
(* Return lists of changes. *)
56-
(pif_changes, bond_changes)
78+
(pif_changes, bond_changes, lldp_changes)
5779

5880
let set_pif_changes ?except () =
5981
with_lock pifs_cached_m (fun _ ->
@@ -66,15 +88,22 @@ let set_bond_changes ?except () =
6688
~target:bonds_links_up_cached ()
6789
)
6890

91+
let set_lldp_changes ?except () =
92+
with_lock lldp_neighbor_cached_m (fun _ ->
93+
transfer_map ?except ~source:lldp_neighbor_tmp
94+
~target:lldp_neighbor_cached ()
95+
)
96+
6997
(* This function updates the database for all the slowly changing properties
7098
* of host memory, VM memory, PIFs, and bonds.
7199
*)
72100
let pifs_update_fn () =
73-
let pif_changes, bond_changes = get_pif_and_bond_changes () in
101+
let pif_changes, bond_changes, lldp_changes = get_pif_and_bond_changes () in
74102
Server_helpers.exec_with_new_task "updating PIFs" (fun __context ->
75103
let host = Helpers.get_localhost ~__context in
76104
let issues = ref [] in
77105
let keeps = ref [] in
106+
let keeps_lldp = ref [] in
78107
List.iter
79108
(fun (bond, links_up) ->
80109
try
@@ -112,6 +141,31 @@ let pifs_update_fn () =
112141
set_pif_changes ()
113142
with e -> issues := e :: !issues
114143
) ;
144+
List.iter
145+
(fun (dev, neighbor) ->
146+
try
147+
match
148+
Db.PIF.get_records_where ~__context
149+
~expr:
150+
(And
151+
( Eq (Field "host", Literal (Ref.string_of host))
152+
, Eq (Field "device", Literal dev)
153+
)
154+
)
155+
with
156+
| (_, pif_rec) :: _ ->
157+
let metrics = pif_rec.API.pIF_metrics in
158+
if Db.is_valid_ref __context metrics then
159+
Db.PIF_metrics.set_lldp_neighbor ~__context ~self:metrics
160+
~value:neighbor
161+
| [] ->
162+
()
163+
with e ->
164+
issues := e :: !issues ;
165+
keeps_lldp := dev :: !keeps_lldp
166+
)
167+
lldp_changes ;
168+
set_lldp_changes ~except:!keeps_lldp () ;
115169
List.iter
116170
(function
117171
| Db_exn.Read_missing_uuid _ ->

ocaml/xapi/monitor_dbcalls_cache.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ let bonds_links_up_cached : (string, int) Hashtbl.t = Hashtbl.create 10
3030

3131
let bonds_links_up_tmp : (string, int) Hashtbl.t = Hashtbl.create 10
3232

33+
(* A cache mapping PIF (device) names to their received LLDP neighbour, encoded
34+
as a (key, value) map. *)
35+
let lldp_neighbor_cached_m : Mutex.t = Mutex.create ()
36+
37+
let lldp_neighbor_cached : (string, (string * string) list) Hashtbl.t =
38+
Hashtbl.create 10
39+
40+
let lldp_neighbor_tmp : (string, (string * string) list) Hashtbl.t =
41+
Hashtbl.create 10
42+
3343
(* A cache mapping vm_uuids to actual memory. *)
3444
let vm_memory_cached_m : Mutex.t = Mutex.create ()
3545

@@ -60,6 +70,10 @@ let clear_cache_for_pif ~pif_name =
6070
with_lock pifs_cached_m (fun _ ->
6171
Hashtbl.remove pifs_cached pif_name ;
6272
Hashtbl.remove pifs_tmp pif_name
73+
) ;
74+
with_lock lldp_neighbor_cached_m (fun _ ->
75+
Hashtbl.remove lldp_neighbor_cached pif_name ;
76+
Hashtbl.remove lldp_neighbor_tmp pif_name
6377
)
6478

6579
(** [clear_cache_for_vm] removes any current cache for VM with [vm_uuid],
@@ -86,6 +100,8 @@ let clear_cache () =
86100
safe_clear ~cache:pifs_cached ~tmp:pifs_tmp ~lock:pifs_cached_m ;
87101
safe_clear ~cache:bonds_links_up_cached ~tmp:bonds_links_up_tmp
88102
~lock:bonds_links_up_cached_m ;
103+
safe_clear ~cache:lldp_neighbor_cached ~tmp:lldp_neighbor_tmp
104+
~lock:lldp_neighbor_cached_m ;
89105
safe_clear ~cache:vm_memory_cached ~tmp:vm_memory_tmp ~lock:vm_memory_cached_m ;
90106
with_lock host_memory_m (fun _ ->
91107
host_memory_free_cached := Int64.zero ;

0 commit comments

Comments
 (0)