Skip to content

Commit 2eeb50c

Browse files
committed
CP-22034: read PIF speed & duplex from sysfs
Sisyphus can tell us these details nowadays, so we can read them from the relevant files rather than going through the C bindings. The existing C bindings cannot cope with speeds such as 50 Gbits/second and higher. Therefore this commit removes the get_status functions from the C stub and removes the Bindings module that uses it, and adds a replacement get_status function in the Sysfs module; it returns the same (speed, duplex) pair as the old function. Signed-off-by: Thomas Sanders <[email protected]>
1 parent d3e7379 commit 2eeb50c

File tree

3 files changed

+15
-74
lines changed

3 files changed

+15
-74
lines changed

lib/link_stubs.c

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -137,43 +137,6 @@ struct ethtool_cmd {
137137
uint32_t reserved[4];
138138
};
139139

140-
value stub_link_get_status(value fd, value dev)
141-
{
142-
CAMLparam2(fd, dev);
143-
CAMLlocal1(ret);
144-
struct ifreq ifr;
145-
struct ethtool_cmd ecmd;
146-
int err, speed, duplex;
147-
148-
SET_IFREQ(ifr, String_val(dev));
149-
ecmd.cmd = ETHTOOL_GSET;
150-
ifr.ifr_data = (caddr_t) &ecmd;
151-
err = ioctl(Int_val(fd), SIOCETHTOOL, &ifr);
152-
CHECK_IOCTL(err, "get ethtool");
153-
154-
/* CA-24610: apparently speeds can be other values eg 2500 */
155-
speed = ecmd.speed;
156-
157-
switch (ecmd.duplex) {
158-
case 0: duplex = 1; break;
159-
case 1: duplex = 2; break;
160-
default: duplex = 0;
161-
}
162-
163-
ret = caml_alloc_tuple(2);
164-
Store_field(ret, 0, Val_int(speed));
165-
Store_field(ret, 1, Val_int(duplex));
166-
167-
CAMLreturn(ret);
168-
}
169140
#else
170-
value stub_link_get_status(value fd, value dev)
171-
{
172-
CAMLparam2(fd, dev);
173-
CAMLlocal1(ret);
174-
ret = caml_alloc_tuple(2);
175-
Store_field(ret, 0, Val_int(0)); /* unknown speed */
176-
Store_field(ret, 1, Val_int(0)); /* unknown duplex */
177-
CAMLreturn(ret);
178-
}
141+
/* nothing here at present */
179142
#endif

lib/network_utils.ml

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,18 @@ module Sysfs = struct
179179
let get_all_bridges () =
180180
let ifaces = list () in
181181
List.filter (fun name -> Sys.file_exists (getpath name "bridge")) ifaces
182+
183+
(** Returns (speed, duplex) for a given network interface: int megabits/s, Duplex.
184+
* The units of speed are specified in pif_record in xen-api/xapi/records.ml.
185+
* Note: these data are present in sysfs from kernel 2.6.33. *)
186+
let get_status name =
187+
let speed = getpath name "speed"
188+
|> (fun p -> try (read_one_line p |> int_of_string) with _ -> 0)
189+
in
190+
let duplex = getpath name "duplex"
191+
|> (fun p -> try read_one_line p |> duplex_of_string with _ -> Duplex_unknown)
192+
in (speed, duplex)
193+
182194
end
183195

184196
module Ip = struct
@@ -1093,37 +1105,3 @@ module Ethtool = struct
10931105
if options <> [] then
10941106
ignore (call ~log:true ("-K" :: name :: (List.concat (List.map (fun (k, v) -> [k; v]) options))))
10951107
end
1096-
1097-
module Bindings = struct
1098-
let control_socket () =
1099-
try
1100-
Unix.socket Unix.PF_INET Unix.SOCK_DGRAM 0
1101-
with
1102-
exn ->
1103-
try
1104-
Unix.socket Unix.PF_UNIX Unix.SOCK_DGRAM 0
1105-
with
1106-
exn ->
1107-
Unix.socket Unix.PF_INET6 Unix.SOCK_DGRAM 0
1108-
1109-
let with_fd f =
1110-
let fd = control_socket () in
1111-
let r = begin try
1112-
f fd
1113-
with
1114-
exn ->
1115-
Unix.close fd;
1116-
raise exn
1117-
end in
1118-
Unix.close fd;
1119-
r
1120-
1121-
external _get_status : Unix.file_descr -> string -> int * duplex = "stub_link_get_status"
1122-
1123-
(** Returns speed and duplex for a given network interface.
1124-
* Note: from kernel 2.6.33, this information is also present in sysfs. *)
1125-
let get_status name =
1126-
try
1127-
with_fd (fun fd -> _get_status fd name)
1128-
with _ -> raise (Read_error "stub_link_get_status")
1129-
end

networkd/network_monitor_thread.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ let rec monitor dbg () =
188188
let carrier = Sysfs.get_carrier dev in
189189
let speed, duplex =
190190
if carrier then
191-
try Bindings.get_status dev with _ -> (0, Duplex_unknown)
191+
Sysfs.get_status dev
192192
else
193193
(0, Duplex_unknown)
194194
in
@@ -209,7 +209,7 @@ let rec monitor dbg () =
209209
List.fold_left (fun (speed, duplex) info ->
210210
try
211211
if info.active then
212-
let speed', duplex' = Bindings.get_status info.slave in
212+
let speed', duplex' = Sysfs.get_status info.slave in
213213
speed + speed', combine_duplex (duplex, duplex')
214214
else
215215
speed, duplex

0 commit comments

Comments
 (0)