Commit Graph

290 Commits

Author SHA1 Message Date
Etienne Champetier
8ae00f92b3 mwan3: handle missing interface gracefully
`ubus.call(sprintf('network.interface.%s', ifname), 'status', {})`
returns null if the interface `ifname` doesn't exists (yet).

For pppoe interfaces using `option ipv6 auto`, a virtual interface suffixed `_6`
is automatically created once the connection is established,
but until then it doesn't exists.

Fixes: 6423781254 ("mwan3: reimplement rpcd plugin using ucode")

Signed-off-by: Etienne Champetier <champetier.etienne@gmail.com>
2025-08-03 14:24:52 -04:00
Etienne Champetier
6423781254 mwan3: reimplement rpcd plugin using ucode
On my "test" router (5 wans, 2 tracking ips per wan), before any rework,
prometheus-node-exporter-lua mwan3 average scraping time was 1230ms
(scraping only the interfaces), after optimizing the shell version,
average time was down to 485ms, with ucode we are now at 41ms.

Signed-off-by: Etienne Champetier <champetier.etienne@gmail.com>
2025-08-01 13:03:01 +02:00
Etienne Champetier
bfdbacca56 mwan3: remove call to network_is_up
It calls jsonfilter but can be avoided as we know
network is up if uptime is present.

Signed-off-by: Etienne Champetier <champetier.etienne@gmail.com>
2025-08-01 13:03:01 +02:00
Etienne Champetier
a6a7f0b21b mwan3: return via variable
Using $(...) to call an internal function causes a fork().
Pass the name of the return variable as first parameter,
and write to it using 'export -n'.

Signed-off-by: Etienne Champetier <champetier.etienne@gmail.com>
2025-08-01 13:03:01 +02:00
Etienne Champetier
ecd2470ddb mwan3: replace $(cat ..) with readfile
Remove a fork+exec

Signed-off-by: Etienne Champetier <champetier.etienne@gmail.com>
2025-08-01 13:03:01 +02:00
Etienne Champetier
de98fdebef mwan3: remove pgrep usage
pgrep is pretty slow as it needs to read all /proc/*/cmdline
or /proc/*/status, and it's called twice per wan.
Add STARTED and PID files to speedup mwan3_get_mwan3track_status().

Signed-off-by: Etienne Champetier <champetier.etienne@gmail.com>
2025-08-01 13:03:01 +02:00
Etienne Champetier
f93b00fa68 mwan3: add new readfile function to speedup variable read from file
This is a preparation for adding an utility function readfile() to read
directly to a variable. Using '$(cat ...)' causes a fork/execve which is
extremely slow just to read '~2' chars.

(commit message from Florian Eckert)
Signed-off-by: Etienne Champetier <champetier.etienne@gmail.com>
2025-08-01 13:03:01 +02:00
Etienne Champetier
50be11c618 mwan3: simplify how we kill subprocs
Just use 'job -p' to list the subprocesses currently running,
so we do not needed the global variables TRACK_PID and SLEEP_PID anymore

Signed-off-by: Etienne Champetier <champetier.etienne@gmail.com>
2025-08-01 13:03:01 +02:00
Philipp Schmitt
3f7c3ee7f7 mwan3: write mwan3 use error/debug messages to stderr instead of stdout
This redirects the debug output to stderr, allowing `mwan3 use` to be used in
scripts without polluting stdout.

Before:

mwan3 use wan curl -fsSL https://ifconfig.co/json | jq -er '.country_iso'
jq: parse error: Invalid numeric literal at line 1, column 8
curl: (23) Failure writing output to destination, passed 389 returned 0

After:

mwan3 use wan curl -fsSL https://ifconfig.co/json | jq -er '.country_iso'
Running 'curl -fsSL https://ifconfig.co/json' with DEVICE=eth2 SRCIP=192.168.0.1 FWMARK=0x3f00 FAMILY=ipv4
DE

Signed-off-by: Philipp Schmitt <philipp@schmitt.co>
2025-02-20 11:10:34 +01:00
Philipp Schmitt
0c9f4a8410 mwan3: propagate mwan3 use command exit code
This propagates the exit code of the command wrapped by `mwan3 use` and
allows for example to use `mwan3 use` in monitoring scripts.

Before change:
shell command:
  mwan3 use wan false >/dev/null && echo ok || echo fail
result:
  ok

After change:
shell command:
  mwan3 use wan false >/dev/null && echo ok || echo fail
result:
  fail

Signed-off-by: Philipp Schmitt <philipp@schmitt.co>
2025-02-20 07:59:20 +01:00
Brian J. Murrell
6f7562dee2 mwan3: Add support for nslookup track method
Signed-off-by: Brian J. Murrell <brian@interlinx.bc.ca>
2025-01-21 10:33:42 +01:00
Brian J. Murrell
c442892978 mwan3: Bug fixes with nping usage
The IP address family is a single dash option and one of 4 or 6, not tcp
or udp.

Signed-off-by: Brian J. Murrell <brian@interlinx.bc.ca>
2025-01-21 10:33:42 +01:00
Florian Eckert
619629ce85 mwan3: close flock fd when starting mwan3.user scripts
Fixes deadlock with multiple init script calls

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2024-10-29 08:44:44 +01:00
William Solichin
40b84a3ce7 mwan3: add check_quality for httping
Example output of TRACK_OUTPUT (httping -c 1 -t 1 'http://www.google.co.uk'):

PING www.google.co.uk:80 (/):
connected to 142.250.200.35:80 (654 bytes), seq=0 time=136.58 ms
--- http://www.google.co.uk/ ping statistics ---
1 connects, 1 ok, 0.00% failed, time 1137 ms
round-trip min/avg/max = 136.6/136.6/136.6 ms

Signed-off-by: William Solichin <william.r.solichin@protonmail.com>
2024-10-07 08:18:07 +02:00
Florian Eckert
1b9a6ae47a Revert "mwan3: use network_get_preferred_ipaddr6"
This reverts commit fc3cc2fe5c.

As long as the related pullrequest [1] is not merged, this change must be
revert.

[1] https://github.com/openwrt/openwrt/pull/14333

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2024-09-27 15:22:09 +02:00
Eduardo Aldaz-Carroll
6b2e5c71e3 mwan3: fix grep order for nping track method
Signed-off-by: Eduardo Aldaz-Carroll <ealdaz@sees.ai>
2024-09-05 14:38:33 +02:00
William Solichin
ebf5531b42 mwan3: fix sed regex for tracking output ping check_quality
Signed-off-by: William Solichin <william.r.solichin@protonmail.com>
2024-09-02 09:22:39 +02:00
Florian Eckert
4df02492c2 Merge pull request #23798 from gentoo-root/mwan3-fix-awk
mwan3: Fix awk expression in mwan3_delete_iface_rules
2024-08-09 07:53:45 +02:00
Maxim Mikityanskiy
4ab7d8b28a mwan3: Fix awk expression in mwan3_delete_iface_rules
The awk expression in mwan3_delete_iface_rules splits the `ip rule list`
output by spaces, therefore $1 contains the trailing colon (e.g., "1:",
"1000:"). The < and > operators compare such values as strings instead
of numbers, producing unexpected results (for example, "1:" > "1000").

Change the field separator to ":" for correct number comparison, so that
the right rules are removed.

An example error message that may appear before the fix:

Error: argument "1:" is wrong: preference value is invalid

It happens because `substr($1,0,4)` selects short numbers along with
the colon. In other cases wrong rules may be removed, for example, if
there is rule 10051, then rule 1005 will be removed.

Signed-off-by: Maxim Mikityanskiy <maxtram95@gmail.com>
2024-07-31 20:27:28 +03:00
Lars Kruse
d3c2f7625c mwan3: "use" action: run process via exec and handle whitespace
Previously the "use" command had the following shortcomings:
* a subprocess was created instead of replacing the shell process
* whitespace in arguments was not handled correctly

Implementation detail:
In shell context the `"$@"` expression should be used (instead of `$*`).
This allows the safe handling of arguments containing whitespace.

Closes: #20001

Signed-off-by: Lars Kruse <devel@sumpfralle.de>
2024-06-23 11:20:26 -07:00
Sergey Ponomarev
4c4a7bd31d treewide: use config_get_bool instead of config_get enabled
The config_get_bool function parses not just the 1 but on/yes, true/false or enabled/disabled.

Signed-off-by: Sergey Ponomarev <stokito@gmail.com>
2024-05-26 03:00:50 +08:00
Jonas Lochmann
e119a57e97 mwan3: check removed route before removal
This makes mwan3rtmon check if mwan3_get_routes returns a route
before removing it. This helps with IPv6 routes with source address
selector removal where multiple original routes are transformed to
the same mwan3 route if one of the source routes is removed while
the others are kept.

Signed-off-by: Jonas Lochmann <git@inkompetenz.org>
2024-02-20 21:53:22 +01:00
Jonas Lochmann
fc3cc2fe5c mwan3: use network_get_preferred_ipaddr6
This updates mwan3 to use network_get_preferred_ipaddr6 instead of
network_get_ipaddr6 if possible to determine a source ip for the
connectivity checks. This avoids issues where the first ip address
that is returned from network_get_ipaddr6 does not work anymore while
the preferred one returned from network_get_preferred_ipaddr6 works.

Signed-off-by: Jonas Lochmann <git@inkompetenz.org>
2024-02-04 16:39:13 -08:00
Florian Eckert
f021b9416b mwan3: refactoring mwan3_report_iface_status output
The tracking and interface status was mixed up in the report. To fix
this, the interface status and the tracking status are now used
directly. The online, uptime and error information are appended to the
status line if needed. If certain routing tables and routing rules are
missing, the error number is also given.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2023-09-20 16:10:34 +02:00
Florian Eckert
002439594c mwan3: also show tracker state via ubus
The tracker state is not shown via ubus. Only if the tracker was in
active state, then the boolean running was set or not. By adding the
tracking state to the ubus information we could also evaluate the state
of the tracker. To remain compatible, the runnig flag of the tracker is
not removed, which in fact displays the same information, but only if
the tracker is in state 'active' or not.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2023-09-20 16:10:06 +02:00
Florian Eckert
28e058e92f mwan3: rename tracking state from 'not enabled' to 'disabled'
The expression 'disabled' is more meaningful than 'not enabled' and can
therefore be better processed in the ubus output, since it is only one
word.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2023-09-20 16:05:13 +02:00
Florian Eckert
4f59c248e0 mwan3: remove notracking in mwan3track ubus status
The function 'get_mwan3_status' is reading the internal state from the
tracker via the status file. Do not use the state 'notracking' status
anymore. If the mwan3track is not running always return 'unknown'
and not 'notracking'. There is already an other function that evaluates
the external state of the tracker.

We have now the following states of the tracker:
internal (mwan3track):
- offline
- online
- diconnecting
- connecting
- disabled
- unknown

external (via pgrep and config):
- paused
- active
- down
- not enabled

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2023-09-20 16:05:08 +02:00
Florian Eckert
a56e111c49 mwan3: fix mwan3_connected ubus output
During the renameing of mwan3_connected_v4 to mwan3_connected_ipv4 and
mwan3_connected_v6 to mwan3_connected_ipv6 the adjustment in the ubus
call was forgotten. This commit fixes this.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2023-09-12 16:03:18 +02:00
Florian Eckert
5f0461be8b mwan3: reset score to up+down on connected
Set the score value to the maximum value when the connected function is
called. The same happens with a disconnected event, the score value is
there set to zero.

Suggested-by: Anna Tikhomirova <vamp@vampik.ru>
Suggested-by: Maxim Mikityanskiy <maxtram95@gmail.com>
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2023-05-08 09:47:08 +02:00
Florian Eckert
1f6bd672fe mwan3: refactoring mwan3track action handling
Refactoring the score handling, so that only one action could take place
during run. The behaviour should be more comprehensible, since several
score actions are not processed at the same time.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2023-05-08 09:47:08 +02:00
Anna Tikhomirova
84b3de9eda mwan3: fix addition of routes to mwan3_connected ipset
Addition of routes to mwan3_connected ipset is broken. The ipset name was
changed from mwan3_connected_v4/6 to mwan3_connected_ipv4/6, but this
change was not reflected in mwan3rtmon.

Signed-off-by: Anna Tikhomirova <vamp@vampik.ru>
* Update commit message
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2023-05-04 12:00:27 +02:00
Anna Tikhomirova
66257510d9 mwan3: fix addition of iptables rules for mwan3 sticky rules
Addition of iptables rules for mwan3 sticky rules is broken, resulting
in non-working sticky rules. The required parameters for the function
'mwan3_set_sticky_iptables' were passed in the wrong order.

Signed-off-by: Anna Tikhomirova <vamp@vampik.ru>
* Update commit message
* Quoting function arguments
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2023-05-04 11:49:56 +02:00
Florian Eckert
0434407428 mwan3: make mwan3.user executable
This is a fix for the the following change:
3d824ea288

Before the change, it was only possible to execute a shell script. To
remove this restriction, a binary or other script language can now also
be used for 'mwan3.user'. Unfortunately, the old shell script was not
executable for older mwan3 version. During a sysupgrade with config transfer,
this 'mwan3.user' script could not be executed for newer mwan3 versions.
To fix this, the calling script checks whether the 'mwan3.user' is executable,
and if not, this executable bit is now set.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-12-19 08:34:54 +01:00
Florian Eckert
997c0842ca mwan3: only send disconnected event if interface was connected before
Up to now on every interface down event a mwan3 disconnected event was
send. This is wrong because if the interface was never connected, then a
disconnected event should not get generated. This commit fixes this bug.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-10-17 15:17:31 +02:00
Florian Eckert
a6606bed17 mwan3: change log message on already added route
Only show message on already set route, if debugging is enabled.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-10-17 15:17:07 +02:00
Denys Yarkovyi
675ecfba89 mwan3: support offload routing modifier
Signed-off-by: Denys Yarkovyi <dyarkovoy@gmail.com>
2022-10-05 05:20:05 -07:00
James White
1a703ed6a6 mwan3: Switch default track_ip targets from Google DNS to Cloudflare DNS
Signed-off-by: James White <james@jmwhite.co.uk>
2022-09-25 09:06:05 +01:00
Florian Eckert
0ed8450d2f mwan3: fix balanced default config
The metrics and weight need to be the same. A 50% balanced would be
require member policies of the same metric and weight value.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-09-14 15:27:16 +02:00
Florian Eckert
87c611abf8 mwan3: fix sticky chain length
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-28 14:02:18 +02:00
Florian Eckert
0b5f09162b mwan3: dump iptables and ipset command for debugging
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-14 09:34:49 +01:00
Florian Eckert
5a80a5dbfe mwan3: unify error messages
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-14 09:34:49 +01:00
Florian Eckert
ed2e0b33ed mwan3: add sleep to release ipset reference
It turns out that under high system load, ipsets cannot be deleted. This
is because there is still a reference in iptables. A short sleep should
give the system time to clean this up.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-14 09:34:49 +01:00
Florian Eckert
13c2604eb1 mwan3: remove not needed ipset del cmd
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-14 09:34:49 +01:00
Florian Eckert
408458a72f mwan3: Split ipsets into separate ipv4 and ipv6 sets
Nft does not directly support ipsets, nft sets must be used instead.
The mwan3 uses ipsets for certain tasks. They can be combinded. So called
an ipset of ipsets. This list type is not available in nft. So that
mwan3 could be ported to nft in the feature, the ipset handling should be
split. So we have for each ipset an iptables rule.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-14 09:34:49 +01:00
Florian Eckert
502779755a mwan3: use mwan3_push_update function for mwan3_delete_iface_iptables
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-14 09:34:49 +01:00
Florian Eckert
b5c675392d mwan3: remove ipset generation from mwan3_set_general_iptables
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-14 09:34:49 +01:00
Florian Eckert
9c2abb19e2 mwan3: use sticky ipset generation mwan3_push_update pattern
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-14 09:34:49 +01:00
Florian Eckert
e985f0dcd1 mwan3: do not create dynamic ipset for IPv6 if not supported
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-14 09:34:49 +01:00
Florian Eckert
fedfbd5004 mwan3: do not add mwan3_custom_v6 set if ipv6 is not available
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-14 09:34:49 +01:00
Florian Eckert
54fad2326e mwan3: use also ipset restore for connected ipv4 sets
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-14 09:34:49 +01:00