From 1f6b39524c21ec3884b4104f90c97ff9b6fa3472 Mon Sep 17 00:00:00 2001 From: tievolu <78606440+tievolu@users.noreply.github.com> Date: Fri, 8 Sep 2023 12:01:55 +0100 Subject: [PATCH] Improve error checking in get_current_bandwidth_from_tc() The error checking logic introduced in the previous commit was flawed. This commit fixes it. --- sqm-autorate.pl | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/sqm-autorate.pl b/sqm-autorate.pl index e1f31c1..7876587 100644 --- a/sqm-autorate.pl +++ b/sqm-autorate.pl @@ -3500,7 +3500,9 @@ sub get_current_bandwidth { # from the SQM config. We should only need to do this once during initialisation. sub get_current_bandwidth_from_tc { my ($direction) = @_; - + + &check_direction($direction); + my $interface = ""; if ($direction eq "download") { $interface = $dl_interfaces[0]; @@ -3511,20 +3513,25 @@ sub get_current_bandwidth_from_tc { my @qdiscs = split(/\n/, &run_sys_command("tc -d qdisc")); foreach my $qdisc (@qdiscs) { - if ($qdisc =~ / dev $interface .* bandwidth (\d+)(G|K|M)bit/) { - my $bw = $1; - my $bw_units = $2; - if ($bw_units eq "K") { - return $bw; - } elsif ($bw_units eq "M") { - return $bw * 1000; - } elsif ($bw_units eq "G") { - return $bw * 1000000; + if ($qdisc =~ / dev $interface .* bandwidth /) { + if ($qdisc =~ / dev $interface .* bandwidth (\d+)(G|K|M)bit/) { + my $bw = $1; + my $bw_units = $2; + if ($bw_units eq "K") { + return $bw; + } elsif ($bw_units eq "M") { + return $bw * 1000; + } elsif ($bw_units eq "G") { + return $bw * 1000000; + } } else { - &fatal_error("Unknown bandwidth unit \"$bw_units\" in tc output"); + &fatal_error("Failed to get bandwidth from tc output for interface \"$interface\":\n$qdisc"); } - } - } + } + } + + # If we reach here the first interface for the specified direction wasn't listed in the tc output + &fatal_error("Failed to get bandwidth from tc output for interface \"$interface\""); } # Get the maximum allowed latency for the specified direction when bandwidth usage is significant