Skip to content

Commit 575b4ee

Browse files
committed
Confirm that the interface exists before calling ip
The logs are full of errors from commands such as `ip addr show` complaining that the given interface does not exist. A Script_error exception would be raised in such cases, which is usually caught and thrown away higher up the stack. Still, the alarming log message is left behind, which often confuses people. Instead of calling out to the `ip` command and hoping for the best, we now first check whether the interface actually exists, and raise the new Interface_does_not_exist error if not. Signed-off-by: Rob Hoes <[email protected]>
1 parent 15fca74 commit 575b4ee

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

lib/network_utils.ml

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ module Sysfs = struct
106106
let all = Array.to_list (Sys.readdir "/sys/class/net") in
107107
List.filter (fun name -> Sys.is_directory ("/sys/class/net/" ^ name)) all
108108

109+
let exists dev =
110+
List.mem dev @@ list ()
111+
112+
let assert_exists dev =
113+
if not @@ exists dev then
114+
raise (Network_error (Interface_does_not_exist dev))
115+
109116
let list_drivers () =
110117
try
111118
Array.to_list (Sys.readdir "/sys/bus/pci/drivers")
@@ -357,6 +364,7 @@ module Ip = struct
357364
List.map (fun i -> List.nth args (succ i)) indices
358365

359366
let get_link_flags dev =
367+
Sysfs.assert_exists dev;
360368
let output = call ["link"; "show"; "dev"; dev] in
361369
let i = String.index output '<' in
362370
let j = String.index output '>' in
@@ -369,11 +377,13 @@ module Ip = struct
369377
with _ -> false
370378

371379
let link_set dev args =
380+
Sysfs.assert_exists dev;
372381
ignore (call ~log:true ("link" :: "set" :: dev :: args))
373382

374383
let link_set_mtu dev mtu =
375384
try ignore (link_set dev ["mtu"; string_of_int mtu])
376-
with e -> error "MTU size is not supported: %s" (string_of_int mtu)
385+
with Network_error (Script_error _) ->
386+
error "MTU size is not supported: %d" mtu
377387

378388
let link_set_up dev =
379389
link_set dev ["up"]
@@ -390,11 +400,13 @@ module Ip = struct
390400
(fun () -> List.iter link_set_up up_links)
391401

392402
let link ?(version=V46) dev attr =
403+
Sysfs.assert_exists dev;
393404
let v = string_of_version version in
394405
let output = call (v @ ["link"; "show"; "dev"; dev]) in
395406
find output attr
396407

397408
let addr ?(version=V46) dev attr =
409+
Sysfs.assert_exists dev;
398410
let v = string_of_version version in
399411
let output = call (v @ ["addr"; "show"; "dev"; dev]) in
400412
find output attr
@@ -487,13 +499,15 @@ module Ip = struct
487499

488500
let flush_ip_addr ?(ipv6=false) dev =
489501
try
502+
Sysfs.assert_exists dev;
490503
let mode = if ipv6 then "-6" else "-4" in
491504
ignore (call ~log:true [mode; "addr"; "flush"; "dev"; dev])
492505
with _ -> ()
493506

494507
let del_ip_addr dev (ip, prefixlen) =
495508
let addr = Printf.sprintf "%s/%d" (Unix.string_of_inet_addr ip) prefixlen in
496509
try
510+
Sysfs.assert_exists dev;
497511
ignore (call ~log:true ["addr"; "del"; addr; "dev"; dev])
498512
with _ -> ()
499513

@@ -503,6 +517,7 @@ module Ip = struct
503517

504518
let set_route ?network dev gateway =
505519
try
520+
Sysfs.assert_exists dev;
506521
match network with
507522
| None ->
508523
ignore (call ~log:true ["route"; "replace"; "default"; "via"; Unix.string_of_inet_addr gateway; "dev"; dev])
@@ -517,12 +532,12 @@ module Ip = struct
517532
Printf.sprintf "%s.%d" interface vlan
518533

519534
let create_vlan interface vlan =
520-
if not (List.mem (vlan_name interface vlan) (Sysfs.list ())) then
535+
if not (Sysfs.exists (vlan_name interface vlan)) then
521536
ignore (call ~log:true ["link"; "add"; "link"; interface; "name"; vlan_name interface vlan;
522537
"type"; "vlan"; "id"; string_of_int vlan])
523538

524539
let destroy_vlan name =
525-
if List.mem name (Sysfs.list ()) then
540+
if Sysfs.exists name then
526541
ignore (call ~log:true ["link"; "delete"; name])
527542

528543
let set_vf_mac dev index mac =

0 commit comments

Comments
 (0)