luci-base: consolidate network.js data acquisition
authorJo-Philipp Wich <jo@mein.io>
Thu, 31 Oct 2019 14:01:26 +0000 (15:01 +0100)
committerJo-Philipp Wich <jo@mein.io>
Fri, 1 Nov 2019 11:03:33 +0000 (12:03 +0100)
The new luci-rpc/getNetworkDevices procedure offers netdev enumeration
with included IPv4 and IPv6 address information as well as 64bit traffic
counters, so we can remove the calls to network.device/status and
luci/getIfaddrs now as we're able to obtain all info from a single source.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
modules/luci-base/htdocs/luci-static/resources/network.js
modules/luci-base/root/usr/libexec/rpcd/luci
modules/luci-base/root/usr/share/rpcd/acl.d/luci-base.json

index 728433bbe984d8d02f5a0e3b05794903acf5ffed..7c5d2b97435b5bd8e8c2261013a5d4e19616f2f9 100644 (file)
@@ -56,12 +56,6 @@ var callLuciWirelessDevices = rpc.declare({
        expect: { '': {} }
 });
 
-var callLuciIfaddrs = rpc.declare({
-       object: 'luci',
-       method: 'getIfaddrs',
-       expect: { result: [] }
-});
-
 var callLuciBoardJSON = rpc.declare({
        object: 'luci-rpc',
        method: 'getBoardJSON'
@@ -94,12 +88,6 @@ var callNetworkInterfaceDump = rpc.declare({
        expect: { 'interface': [] }
 });
 
-var callNetworkDeviceStatus = rpc.declare({
-       object: 'network.device',
-       method: 'status',
-       expect: { '': {} }
-});
-
 var callNetworkProtoHandlers = rpc.declare({
        object: 'network',
        method: 'get_proto_handlers',
@@ -361,9 +349,7 @@ function initNetworkState(refresh) {
        if (_state == null || refresh) {
                _init = _init || Promise.all([
                        L.resolveDefault(callNetworkInterfaceDump(), []),
-                       L.resolveDefault(callNetworkDeviceStatus(), {}),
                        L.resolveDefault(callLuciBoardJSON(), {}),
-                       L.resolveDefault(callLuciIfaddrs(), []),
                        L.resolveDefault(callLuciNetworkDevices(), {}),
                        L.resolveDefault(callLuciWirelessDevices(), {}),
                        L.resolveDefault(callLuciHostHints(), {}),
@@ -371,95 +357,72 @@ function initNetworkState(refresh) {
                        uci.load(['network', 'wireless', 'luci'])
                ]).then(function(data) {
                        var netifd_ifaces = data[0],
-                           netifd_devs   = data[1],
-                           board_json    = data[2],
-                           luci_ifaddrs  = data[3],
-                           luci_devs     = data[4];
+                           board_json    = data[1],
+                           luci_devs     = data[2];
 
                        var s = {
                                isTunnel: {}, isBridge: {}, isSwitch: {}, isWifi: {},
-                               ifaces: netifd_ifaces, radios: data[5], hosts: data[6],
+                               ifaces: netifd_ifaces, radios: data[3], hosts: data[4],
                                netdevs: {}, bridges: {}, switches: {}
                        };
 
-                       for (var i = 0, a; (a = luci_ifaddrs[i]) != null; i++) {
-                               var name = a.name.replace(/:.+$/, '');
+                       for (var name in luci_devs) {
+                               var dev = luci_devs[name];
 
                                if (isVirtualIfname(name))
                                        s.isTunnel[name] = true;
 
-                               if (s.isTunnel[name] || !(isIgnoredIfname(name) || isVirtualIfname(name))) {
-                                       s.netdevs[name] = s.netdevs[name] || {
-                                               idx:      a.ifindex || i,
-                                               name:     name,
-                                               rawname:  a.name,
-                                               flags:    [],
-                                               ipaddrs:  [],
-                                               ip6addrs: []
-                                       };
-
-                                       if (a.family == 'packet') {
-                                               s.netdevs[name].flags   = a.flags;
-                                               s.netdevs[name].stats   = a.data;
-
-                                               if (a.addr != null && a.addr != '00:00:00:00:00:00' && a.addr.length == 17)
-                                                       s.netdevs[name].macaddr = a.addr;
-                                       }
-                                       else if (a.family == 'inet') {
-                                               s.netdevs[name].ipaddrs.push(a.addr + '/' + a.netmask);
-                                       }
-                                       else if (a.family == 'inet6') {
-                                               s.netdevs[name].ip6addrs.push(a.addr + '/' + a.netmask);
-                                       }
-                               }
-                       }
-
-                       /* override getifaddr() stats with netifd device status stats as
-                          the former are limited to 32bit counters only */
-                       for (var devname in netifd_devs) {
-                               if (!s.netdevs.hasOwnProperty(devname))
-                                       continue;
-
-                               if (!L.isObject(netifd_devs[devname]))
+                               if (!s.isTunnel[name] && isIgnoredIfname(name))
                                        continue;
 
-                               s.netdevs[devname].stats = Object.assign({},
-                                       s.netdevs[devname].stats, netifd_devs[devname].statistics);
+                               s.netdevs[name] = s.netdevs[name] || {
+                                       idx:      dev.ifindex,
+                                       name:     name,
+                                       rawname:  name,
+                                       flags:    dev.flags,
+                                       stats:    dev.stats,
+                                       macaddr:  dev.mac,
+                                       type:     dev.type,
+                                       mtu:      dev.mtu,
+                                       qlen:     dev.qlen,
+                                       ipaddrs:  [],
+                                       ip6addrs: []
+                               };
+
+                               if (Array.isArray(dev.ipaddrs))
+                                       for (var i = 0; i < dev.ipaddrs.length; i++)
+                                               s.netdevs[name].ipaddrs.push(dev.ipaddrs[i].address + '/' + dev.ipaddrs[i].netmask);
+
+                               if (Array.isArray(dev.ip6addrs))
+                                       for (var i = 0; i < dev.ip6addrs.length; i++)
+                                               s.netdevs[name].ip6addrs.push(dev.ip6addrs[i].address + '/' + dev.ip6addrs[i].netmask);
                        }
 
-                       for (var devname in luci_devs) {
-                               var dev = luci_devs[devname];
+                       for (var name in luci_devs) {
+                               var dev = luci_devs[name];
 
-                               if (dev.bridge) {
-                                       var b = {
-                                               name:    devname,
-                                               id:      dev.id,
-                                               stp:     dev.stp,
-                                               ifnames: []
-                                       };
+                               if (!dev.bridge)
+                                       continue;
 
-                                       for (var i = 0; dev.ports && i < dev.ports.length; i++) {
-                                               var subdev = s.netdevs[dev.ports[i]];
+                               var b = {
+                                       name:    name,
+                                       id:      dev.id,
+                                       stp:     dev.stp,
+                                       ifnames: []
+                               };
 
-                                               if (subdev == null)
-                                                       continue;
+                               for (var i = 0; dev.ports && i < dev.ports.length; i++) {
+                                       var subdev = s.netdevs[dev.ports[i]];
 
-                                               b.ifnames.push(subdev);
-                                               subdev.bridge = b;
-                                       }
+                                       if (subdev == null)
+                                               continue;
 
-                                       s.bridges[devname] = b;
-                                       s.isBridge[devname] = true;
+                                       b.ifnames.push(subdev);
+                                       subdev.bridge = b;
                                }
 
-                               if (s.netdevs.hasOwnProperty(devname)) {
-                                       Object.assign(s.netdevs[devname], {
-                                               macaddr: dev.mac,
-                                               type:    dev.type,
-                                               mtu:     dev.mtu,
-                                               qlen:    dev.qlen
-                                       });
-                               }
+                               s.bridges[name] = b;
+                               s.isBridge[name] = true;
                        }
 
                        if (L.isObject(board_json.switch)) {
index ae504fd3e3a09763722da2b3d3481d4c24789756..4eb62d215229fcd4cc71e6b215e0cd18737f2c5d 100755 (executable)
@@ -160,12 +160,6 @@ local methods = {
                end
        },
 
-       getIfaddrs = {
-               call = function()
-                       return { result = nixio.getifaddrs() }
-               end
-       },
-
        getDUIDHints = {
                call = function()
                        local fp = io.open('/var/hosts/odhcpd')
index 8b47aa83f30fb359cb4c3467e418b7749eb89d72..d801f0168f8c8caa231bfdfbce39607f412373b6 100644 (file)
@@ -41,9 +41,8 @@
                        "ubus": {
                                "file": [ "list", "read", "stat" ],
                                "iwinfo": [ "assoclist", "freqlist", "txpowerlist", "countrylist" ],
-                               "luci": [ "getDUIDHints", "getIfaddrs", "getInitList", "getLocaltime", "getTimezones", "getLEDs", "getUSBDevices", "getSwconfigFeatures", "getSwconfigPortState", "getBlockDevices", "getMountPoints" ],
+                               "luci": [ "getDUIDHints", "getInitList", "getLocaltime", "getTimezones", "getLEDs", "getUSBDevices", "getSwconfigFeatures", "getSwconfigPortState", "getBlockDevices", "getMountPoints" ],
                                "luci-rpc": [ "getBoardJSON", "getDHCPLeases", "getHostHints", "getNetworkDevices", "getWirelessDevices" ],
-                               "network.device": [ "status" ],
                                "network.interface": [ "dump" ],
                                "network": [ "get_proto_handlers" ],
                                "system": [ "validate_firmware_image" ],