nl80211: don't guess if a name is an ifname
authorAndre Heider <a.heider@gmail.com>
Tue, 15 Nov 2022 13:12:41 +0000 (14:12 +0100)
committerJo-Philipp Wich <jo@mein.io>
Thu, 15 Dec 2022 20:22:18 +0000 (21:22 +0100)
It's too slow to do it all over again and again, especially with e.g.
luci constantly polling.

Before:
$ time iwinfo phy0 info
real 0m 0.54s
$ time iwinfo radio0 info
real 0m 0.70s
$ time ubus call luci-rpc getWirelessDevices
real 0m 0.67s

After:
$ time iwinfo phy0 info
real 0m 0.04s
$ time iwinfo radio0 info
real 0m 0.09s
$ time ubus call luci-rpc getWirelessDevices
real 0m 0.17s

Signed-off-by: Andre Heider <a.heider@gmail.com>
iwinfo_nl80211.c

index 50b1ab4085d9b86e66b3066180fa5b96488e03ef..545c56c5322d92317aa0ade3e70740f71608618a 100644 (file)
@@ -22,6 +22,7 @@
  * Parts of this code are derived from the Linux iw utility.
  */
 
+#include <sys/stat.h>
 #include <limits.h>
 #include <glob.h>
 #include <fnmatch.h>
@@ -411,6 +412,15 @@ out:
        return idx;
 }
 
+static bool nl80211_is_ifname(const char *name)
+{
+       struct stat st;
+       char buffer[PATH_MAX];
+
+       snprintf(buffer, sizeof(buffer), "/sys/class/net/%s", name);
+       return !lstat(buffer, &st);
+}
+
 static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname,
                                                  int cmd, int flags)
 {
@@ -426,10 +436,10 @@ static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname,
 
        if (!strncmp(ifname, "mon.", 4))
                ifidx = if_nametoindex(&ifname[4]);
-       else
+       else if (nl80211_is_ifname(ifname))
                ifidx = if_nametoindex(ifname);
-
-       if (!ifidx) {
+       else
+       {
                phyidx = nl80211_phy_idx_from_phy(ifname);
                if (phyidx < 0)
                        phyidx = nl80211_phy_idx_from_uci(ifname);