By default the wlanX device name is derived from the radio phy. The first netdev on phy0 will be called wlan0, the first netdev on phy1 will be called wlan1 and so on.
The first spontaneous idea would be to simply swap the phy names from a hotplug script using a command sequence like:
iw phy phy0 set name phy_tmp
iw phy phy1 set name phy0
iw phy phy_tmp set name phy1
Unfortunately this does not work since the kernel explicitly forbids assigning phy<digit>
names:
https://elixir.bootlin.com/linux/latest/source/net/wireless/core.c#L102
The code above allows names like phy<digit><digit>
names though so I tried the following hotplug script:
root@OpenWrt:~# cat /etc/hotplug.d/ieee80211/09-swap-phy-names
#!/bin/sh
[ $ACTION = add ] || exit 0
if iw phy phy0 info 2>/dev/null | grep -q "5... MHz" && \
iw phy phy1 info 2>/dev/null | grep -q "2... MHz";
then
iw phy phy0 set name phy01
iw phy phy1 set name phy00
fi
The script is prefixed with 09-
to precede the default 10-wifi-detect
script which is responsible for enumerating the phys and generating the initial wireless config.
After staging this script and deleting /etc/config/wireless, the radio0 and radio1 identifiers will be swapped on the next boot (radio0 = 2.4GHz, radio1 = 5GHz).
Only downside is that the resulting wlan interfaces will be called wlan00
and wlan01
now instead of wlan0
and wlan1
respectively. If this is an issue, you can fix the names using the ifname
option in the corresponding wifi-iface
sections:
root@OpenWrt:~# iw list | grep Wiphy
Wiphy phy00
Wiphy phy01
root@OpenWrt:~# ip link | grep wlan
1020: wlan00: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-lan state UP qlen 1000
1021: wlan01: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-lan state UP qlen 1000
root@OpenWrt:~# uci set wireless.@wifi-iface[0].ifname=wlan0
root@OpenWrt:~# uci set wireless.@wifi-iface[1].ifname=wlan1
root@OpenWrt:~# uci commit wireless
root@OpenWrt:~# wifi
root@OpenWrt:~# ip link | grep wlan
1029: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-lan state UP qlen 1000
1030: wlan1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-lan state UP qlen 1000
root@OpenWrt:~#
However, when you change the names anyway, you can as well assign descriptive names to them, like phy_n
and phy_ac
which will yield interface names like wlan_n
or wlan_ac
(the wireless setup will strip the leading phy
prefix and simply replace it with wlan
as can be seen here: https://git.openwrt.org/?p=openwrt/openwrt.git;a=blob;f=package/kernel/mac80211/files/lib/netifd/wireless/mac80211.sh;h=e7d442f8ef1a0efb8087be90a740b4cd6348337f;hb=9b8ea3623b22509b7c5090dc50e18e9af1f13405#l451)