@@ -22,6 +22,17 @@ module D = Debug.Make (struct let name = "monitor_dbcalls" end)
2222
2323open 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+
2536let 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
5880let 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 *)
72100let 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 _ ->
0 commit comments