Implementing Automatic Network Interface Switching Based on Data Cost and Signal Strength using OpenWRT

Hello,

I'm currently in the process of developing a router, but I'm facing challenges due to my limited networking experience.

The functionality I aim to develop is as follows:

I have multiple WAN interfaces (4 in total).

There is one LAN interface.

Each WAN interface is assigned a priority connection weight value.

Essentially, the WAN interface with both a strong signal strength and a high weight value should be selected.

For instance, if we have WAN1, WAN2, WAN3, and WAN4, I want a dynamic selection approach where the WAN with the highest weight and the strongest network signal is automatically chosen. This is particularly important because this router is installed on a ship, which constantly changes its location.

To achieve stable network connectivity, I need the router to intelligently switch to the WAN interface with the highest weight and the best network signal based on its current location.

I intend to set up this configuration using OpenWRT. Here's what I've done so far:

I've purchased a motherboard and installed OpenWRT 22.03 version for x86.

Following that, I've installed the mwan3 package through Luci because I've heard that mwan3 provides the functionality I need.

According to the mwan3 manual


in OpenWRT, the next step is to create an additional WAN interface.

However, I'm uncertain about how to proceed from this point. Below, you'll find a picture of my router.

Is there anyone who can provide me with guidance or assistance? I'm eagerly waiting for help.

[Picture of the Router]

Creating the additional interfaces is easy. Usually the interfaces are named wan1, wan2, etc... We can verify it though.

Please run the following commands (copy-paste the whole block) and paste the output here, using the "Preformatted text </> " button:
grafik
Remember to redact passwords, MAC addresses and any public IP addresses you may have

ubus call system board; \
uci export network; \
ip -4 addr ; ip link

As for the mwan3, it cannot decide based on signal strength, but it can avoid using an interface which has bad ping results.

1 Like

Thank you so much for your reply.
This is the value received by entering the given command.
What should I do now?
Please help. thank you.

///ubus call system board

root@OpenWrt:~# ubus call system board
{
"kernel": "5.10.176",
"hostname": "OpenWrt",
"system": "Intel(R) Celeron(R) CPU J1900 @ 1.99GHz",
"model": "NF533 NF533",
"board_name": "nf533-nf533",
"rootfs_type": "ext4",
"release": {
"distribution": "OpenWrt",
"version": "22.03.5",
"revision": "r20134-5f15225c1e",
"target": "x86/64",
"description": "OpenWrt 22.03.5 r20134-5f15225c1e"
}
}

///uci export network

root@OpenWrt:~# uci export network
package network

config interface 'loopback'
option device 'lo'
option proto 'static'
option ipaddr '127.0.0.1'
option netmask '255.0.0.0'

config globals 'globals'
option ula_prefix 'fd95:5739:bb5c::/48'

config device
option name 'br-lan'
option type 'bridge'
list ports 'eth0'

config interface 'lan'
option device 'br-lan'
option proto 'static'
option netmask '255.255.255.0'
option ip6assign '60'
option ipaddr '192.168.1.1'

config interface 'wan1'
option device 'eth1'
option proto 'dhcp'

config interface 'wan2'
option device 'eth2'
option proto 'static'
option ipaddr '192.168.0.129'
option netmask '255.255.255.0'

//ip -4 addr

root@OpenWrt:~# ip -4 addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
4: eth2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN group default qlen 1000
inet 192.168.0.129/24 brd 192.168.0.255 scope global eth2
valid_lft forever preferred_lft forever
7: br-lan: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
inet 192.168.1.1/24 brd 192.168.1.255 scope global br-lan
valid_lft forever preferred_lft forever

//ip link

root@OpenWrt:~# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master br-lan state UP mode DEFAULT group default qlen 1000
link/ether 00:30:18:09:ac:8c brd ff:ff:ff:ff:ff:ff
3: eth1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN mode DEFAULT group default qlen 1000
link/ether 00:30:18:09:ac:89 brd ff:ff:ff:ff:ff:ff
4: eth2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN mode DEFAULT group default qlen 1000
link/ether 00:30:18:09:ac:8a brd ff:ff:ff:ff:ff:ff
5: eth3: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether 00:30:18:09:ac:8b brd ff:ff:ff:ff:ff:ff
7: br-lan: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
link/ether 00:30:18:09:ac:8c brd ff:ff:ff:ff:ff:ff

since it interests me too

depends on what you use for the internet connection, I assume 4g connections on the ship, or other technologies?

If you have 4G connections, many modems/routers allow you to monitor their signal via script.

as regards your request, just set the metrics on all the wans you have, automatically the lowest metric is the one in use (even without mwan3).

wan1 metric 10
wan2 metric 20
wan3 metric 30
wan3 metric 40

example: a script that creates a metric 5 to replace wan1 with wan2

#!/bin/sh
#route -n
#Kernel IP routing table
#Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
#0.0.0.0         192.168.4.1     0.0.0.0         UG    10     0        0 wan
#0.0.0.0         192.168.5.1     0.0.0.0         UG    20     0        0 wanb

if [ ! -f /tmp/lock_gateway ]; then
logger "exec switch_gateway ip route add default via 192.168.5.1 metric 5"
ip route add default via 192.168.5.1 metric 5
touch /tmp/lock_gateway
else
rm /tmp/lock_gateway
logger "exec switch_gateway ip route del default metric 5"
ip route del default metric 5
fi
1 Like

Thank you very much for your response. I don't have much knowledge in this area, so all the answers are highly valuable, and I appreciate your assistance. However, in the OpenWrt structure you mentioned, where should I save the bash shell script file after writing it? I would like to understand how to configure settings through a script. (In LuCI, I noticed changes in Network > Advanced Settings.) The internet connection involves four wired Ethernet connections with different gateways.

the script would be saved to the partition

log in via ssh save the file for example (and chmod 700 on file)
/root/switch_gateway

my script is launched every day at 4:00
and allows you to change the current gateway
in order to distribute consumption
across two 4g sims (inserted into two 4g routers).

I launch it via:
/etc/crontab/root
#min hour day month day-week command
#0-59 0-23 1-31 1-12 0-6(0=Sunday) exec
0 4 * * * /root/switch_gateway

1 Like

Please use the "Preformatted text </>" button for logs, scripts, configs and general console output.
grafik
Please edit your post accordingly. Thank you! :slight_smile:

Other than that you have eth0 assigned to lan, which leaves you eth1-3 for wan. You could extend the amount of wan interfaces by using a managed switch and vlans.
As for how to create a new wan inteface, you can look how the existing are configured and apply it for the 3rd.

config interface 'wan1'
option device 'eth1'
option proto 'dhcp'

config interface 'wan2'
option device 'eth2'
option proto 'static'
option ipaddr '192.168.0.129'
option netmask '255.255.255.0'
2 Likes

Thank you for your assistance!
Thanks for the valuable advice.
Let's explore how to enhance functionality using shell scripts as mentioned above

Thank you for your valuable advice. I will set it up as you suggested!

Is there a way to know the strength of the network signal?

It depends on the modem/router connected to the ISP.

for example search:

https://www.google.com/search?q=huawei+router+4g+lte+signal+strength+script

view my another post:

1 Like

I'm building a router with 4 WAN and 2 LAN.
I would like to install openwrt and check the network signal strength and network connection status to select one of the four WANs.

  1. I would like to create a complete installation image file that includes this feature.
    Please provide advice on manuals and reference materials for creating openwrt image files with customization.

  2. Selecting a stable WAN depending on the connection status is being studied using mwan3.
    But does anyone know how to check the signal strength?
    If you look at the mwan3 manual on the openwrt homepage, it is mentioned that additional work can be done on the interface tracked by mwan3. Does anyone know in detail how to implement this part?

I'll be waiting for your help. Thank you so much.

https://firmware-selector.openwrt.org/, if not already supported, then the 1st link.

1 Like

Thank you for answer! It helps me a lot!

Hello. I am currently developing a router with OpenWRT that has the feature of automatic switching between multiple WANs. I do not have experience in this field, so I need assistance.

This router has a total of 4 wired Ethernet connections (wan1, wan2, wan3, wan4) and 1 LAN. The router is currently implemented following these steps:

1.Set up network configuration for the 4 WANs and 1 LAN.
2.Install the mwan3 package.
3.Configure mwan3 policies to automatically select the WAN with a signal using a ping test among the
WANs (wan1, wan2, wan3, wan4).
4.Choose the WAN with the highest signal strength if more than 1 WAN has a signal.
5.Log the process.
6. Customize Luci to show only the necessary menus.
Upon applying these steps, the router is completed. I need to create several of these routers. Therefore, what I am looking for is to create an installation image file where these steps are already applied. Please guide me on how to create a customized installation image file.

Thank you.

If you can provide advice on steps 4, 5, and 6 as well, I would greatly appreciate it. Thank you very much.

dd the finished image, from the source to a target ?

1 Like

sorry. I didn't understand the answer because I don't know much about Linux or openwrt. Could you explain it a little more simply? thank you.

In that case, boot clonezilla off an USB flash drive on the source, and save a disk image |somewhere],
then boot clonezilla again, on the target, and write the disk image you previously created to the boot drive.

1 Like

/etc/uci-defaults will execute your custom scripts to automatically setup your router.

1 Like

Moderator Note: I have merged multiple threads that are all about the same topic into this main thread. Feel free to continue using this thread, but please refrain from creating new threads that cover the same topics as previous threads.

3 Likes