Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 7c62863b04
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 286 lines (261 sloc) 10.104 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
(*
* Copyright (C) Citrix Systems Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; version 2.1 only. with the special
* exception on linking described in file LICENSE.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*)

open Network_utils

open Fun
open Stringext
open Listext
open Threadext

module D = Debug.Debugger(struct let name = "network_monitor_thread" end)
open D

(** Table for bonds status. *)
let bonds_status : (string, (int * int)) Hashtbl.t = Hashtbl.create 10

let xapi_rpc =
let open Xmlrpc_client in
XML_protocol.rpc ~http:(xmlrpc ~version:"1.0" "/")
~transport:(Unix (Filename.concat Fhs.vardir "xapi"))

let send_bond_change_alert dev interfaces message =
let ifaces = String.concat "+" (List.sort String.compare interfaces) in
let module XenAPI = Client.Client in
let session_id = XenAPI.Session.login_with_password
~rpc:xapi_rpc ~uname:"" ~pwd:"" ~version:"1.4" in
Pervasiveext.finally
(fun _ ->
let obj_uuid = Util_inventory.lookup Util_inventory._installation_uuid in
let body = Printf.sprintf "The status of the %s bond %s" ifaces message in
try
let (name, priority) = Api_messages.bond_status_changed in
let (_: 'a Ref.t) = XenAPI.Message.create ~rpc:xapi_rpc ~session_id
~name ~priority ~cls:`Host ~obj_uuid ~body in ()
with _ ->
warn "Exception sending a bond-status-change alert."
)
(fun _ -> XenAPI.Session.logout ~rpc:xapi_rpc ~session_id)

let check_for_changes ~(dev : string) ~(stat : Network_monitor.iface_stats) =
let open Network_monitor in
match String.startswith "vif" dev with true -> () | false ->
if stat.nb_links > 1 then ( (* It is a bond. *)
if Hashtbl.mem bonds_status dev then ( (* Seen before. *)
let nb_links_old, links_up_old = Hashtbl.find bonds_status dev in
if links_up_old <> stat.links_up then (
info "Bonds status changed: %s nb_links %d up %d up_old %d" dev stat.nb_links
stat.links_up links_up_old;
Hashtbl.replace bonds_status dev (stat.nb_links,stat.links_up);
let msg = Printf.sprintf "changed: %d/%d up (was %d/%d)" stat.links_up stat.nb_links
links_up_old nb_links_old in
try
send_bond_change_alert dev stat.interfaces msg
with e ->
debug "Error while sending alert BONDS_STATUS_CHANGED: %s\n%s"
(Printexc.to_string e) (Printexc.get_backtrace ())
)
) else ( (* Seen for the first time. *)
Hashtbl.add bonds_status dev (stat.nb_links,stat.links_up);
info "New bonds status: %s nb_links %d up %d" dev stat.nb_links stat.links_up;
if stat.links_up <> stat.nb_links then
(let msg = Printf.sprintf "is: %d/%d up" stat.links_up stat.nb_links in
try
send_bond_change_alert dev stat.interfaces msg
with e ->
debug "Error while sending alert BONDS_STATUS_CHANGED: %s\n%s"
(Printexc.to_string e) (Printexc.get_backtrace ()))
)
)

let failed_again = ref false

let rec monitor dbg () =
let open Network_interface in
let open Network_monitor in
(try
let devs = ref [] in

let standardise_name name =
try
let (d1,d2) = Scanf.sscanf name "tap%d.%d"
(fun d1 d2 -> d1,d2) in
let newname = Printf.sprintf "vif%d.%d" d1 d2 in
newname
with _ -> name
in

let f line =
if String.contains line ':' then (
let flds = String.split_f (fun c -> c = ' ' || c = ':') line in
let flds = List.filter (fun field -> field <> "") flds in
let name = standardise_name (List.nth flds 0) in
let vs = List.map (fun i ->
try Int64.of_string (List.nth flds i) with _ -> 0L)
[ 1; 2; 3; 9; 10; 11; ] in
let eth_stat = {default_stats with
rx_bytes = List.nth vs 0;
rx_pkts = List.nth vs 1;
rx_errors = List.nth vs 2;
tx_bytes = List.nth vs 3;
tx_pkts = List.nth vs 4;
tx_errors = List.nth vs 5;
} in
(* CA-23291: no good can come of recording 'dummy' device stats *)
if not(String.startswith "dummy" name) &&
not(String.startswith "xenbr" name) &&
not(String.startswith "xapi" name) &&
not(String.startswith "eth" name && String.contains name '.')
then devs := (name,eth_stat) :: (!devs)
)
in
Unixext.readfile_line f "/proc/net/dev";

let make_bond_info (name, interfaces) =
let devs = List.filter (fun (name', _) -> List.mem name' interfaces) !devs in
let eth_stat = {default_stats with
rx_bytes = List.fold_left (fun ac (_, stat) -> Int64.add ac stat.rx_bytes) 0L devs;
rx_pkts = List.fold_left (fun ac (_, stat) -> Int64.add ac stat.rx_pkts) 0L devs;
rx_errors = List.fold_left (fun ac (_, stat) -> Int64.add ac stat.rx_errors) 0L devs;
tx_bytes = List.fold_left (fun ac (_, stat) -> Int64.add ac stat.tx_bytes) 0L devs;
tx_pkts = List.fold_left (fun ac (_, stat) -> Int64.add ac stat.tx_pkts) 0L devs;
tx_errors = List.fold_left (fun ac (_, stat) -> Int64.add ac stat.tx_errors) 0L devs;
} in
name, eth_stat
in
let bonds : (string * string list) list = Network_server.Bridge.get_all_bonds () dbg ~from_cache:true () in
devs := (List.map make_bond_info bonds) @ !devs;

let transform_taps () =
let newdevnames = List.setify (List.map fst !devs) in
let newdevs = List.map (fun name ->
let devs = List.filter (fun (n,x) -> n=name) !devs in
let tot = List.fold_left (fun acc (_,b) ->
{default_stats with
rx_bytes = Int64.add acc.rx_bytes b.rx_bytes;
rx_pkts = Int64.add acc.rx_pkts b.rx_pkts;
rx_errors = Int64.add acc.rx_errors b.rx_errors;
tx_bytes = Int64.add acc.tx_bytes b.tx_bytes;
tx_pkts = Int64.add acc.tx_pkts b.tx_pkts;
tx_errors = Int64.add acc.tx_errors b.tx_errors}) default_stats devs
in
(name,tot)
) newdevnames
in
devs := newdevs
in

transform_taps ();

let bonds_list = ref [] in
devs := List.map (fun (dev, stat) ->
if not (String.startswith "vif" dev) then begin
let devs =
if List.mem_assoc dev bonds then
List.assoc dev bonds
else
[dev]
in
let vendor_id, device_id = if List.length devs = 1 then Sysfs.get_pci_ids dev else "", "" in
let carriers = List.map Sysfs.get_carrier devs in
let speed, duplex =
let int_of_duplex = function
| Duplex_half -> 1
| Duplex_full -> 2
| Duplex_unknown -> 0
in
let duplex_of_int = function
| 1 -> Duplex_half
| 2 -> Duplex_full
| _ -> Duplex_unknown
in
let statuses = List.map2 (fun dev carrier ->
let speed, duplex =
try
if not carrier then failwith "no carrier";
Bindings.get_status dev
with _ ->
0,
Duplex_unknown
in
speed, int_of_duplex duplex
) devs carriers in
let speed, duplex =
List.fold_left (fun (speed, duplex) (speed', duplex') -> (speed + speed'), (min duplex duplex')) (0, 2) statuses
in
speed, duplex_of_int duplex
in
let nb_links = List.length devs in
let carrier = List.mem true carriers in
let get_interfaces name =
let bonds = Network_server.Bridge.get_all_bonds () dbg ~from_cache:true () in
let interfaces = (try List.assoc dev bonds with _ -> []) in
interfaces in
let (links_up,interfaces) = (if nb_links > 1 then
(bonds_list := dev :: !bonds_list;
Network_server.Bridge.get_bond_links_up () dbg dev, get_interfaces dev)
else
((if carrier then 1 else 0), [dev]))
in
let pci_bus_path = if List.length devs = 1 then Sysfs.get_pcibuspath dev else "" in
let stat = {stat with carrier; speed; duplex; pci_bus_path; vendor_id;
device_id; nb_links; links_up; interfaces} in
check_for_changes ~dev ~stat;
dev, stat
end else
dev, stat
) (!devs);

if (List.length !bonds_list) <> (Hashtbl.length bonds_status) then begin
let dead_bonds = Hashtbl.fold (fun k _ acc -> if List.mem k !bonds_list then acc else k :: acc)
bonds_status [] in
List.iter (fun b -> info "Removing bond %s" b; Hashtbl.remove bonds_status b) dead_bonds
end;

write_stats !devs;
failed_again := false
with e ->
if not !failed_again then begin
failed_again := true;
debug "Error while collecting stats (suppressing further errors): %s\n%s"
(Printexc.to_string e) (Printexc.get_backtrace ())
end
);

Thread.delay interval;
monitor dbg ()

let watcher_m = Mutex.create ()
let watcher_pid = ref None

let signal_networking_change () =
let module XenAPI = Client.Client in
let session = XenAPI.Session.slave_local_login_with_password ~rpc:xapi_rpc ~uname:"" ~pwd:"" in
Pervasiveext.finally
(fun () -> XenAPI.Host.signal_networking_change xapi_rpc session)
(fun () -> XenAPI.Session.local_logout xapi_rpc session)

let ip_watcher () =
let cmd = Network_utils.iproute2 in
(* Only monitor IPv6 addresses at the moment *)
let args = ["-6"; "monitor"; "address"] in
let readme, writeme = Unix.pipe () in
Mutex.execute watcher_m (fun () ->
watcher_pid := Some (Forkhelpers.safe_close_and_exec ~env:(Unix.environment ()) None (Some writeme) None [] cmd args)
);
Unix.close writeme;
let in_channel = Unix.in_channel_of_descr readme in
debug "Started IPv6 watcher thread";
let rec loop () =
let line = input_line in_channel in
(* Do not send events for link-local IPv6 addresses *)
if String.has_substr line "inet" && not (String.has_substr line "inet6 fe80") then begin
signal_networking_change ()
end;
loop ()
in
loop ()

let start () =
let dbg = "monitor_thread" in
Debug.with_thread_associated dbg (fun () ->
debug "Starting network monitor";
let (_ : Thread.t) = Thread.create (monitor dbg) () in
let (_ : Thread.t) = Thread.create ip_watcher () in
()
) ()

let stop () =
Mutex.execute watcher_m (fun () ->
match !watcher_pid with
| None -> ()
| Some pid -> Unix.kill (Forkhelpers.getpid pid) Sys.sigterm
)

Something went wrong with that request. Please try again.