From 93d0ea52aec49e6d3c01b39f09a860f34f89e232 Mon Sep 17 00:00:00 2001 From: Gagan Deep Date: Thu, 21 Sep 2023 19:37:49 +0530 Subject: [PATCH] [fix] Use device network section for STP on OpenWrt > 21 #123 Fixes #123 --- .../lib/openwisp-monitoring/interfaces.lua | 21 ++++++++++++++++++- openwisp-monitoring/tests/basic_env.lua | 3 ++- openwisp-monitoring/tests/main_env.lua | 6 ++++++ openwisp-monitoring/tests/test_dhcp.lua | 3 ++- .../tests/test_files/interface_data.lua | 13 ++++++++++++ openwisp-monitoring/tests/test_interfaces.lua | 10 ++++++++- openwisp-monitoring/tests/test_ula_prefix.lua | 7 ++++++- 7 files changed, 58 insertions(+), 5 deletions(-) diff --git a/openwisp-monitoring/files/lib/openwisp-monitoring/interfaces.lua b/openwisp-monitoring/files/lib/openwisp-monitoring/interfaces.lua index 30f12fec..06787828 100644 --- a/openwisp-monitoring/files/lib/openwisp-monitoring/interfaces.lua +++ b/openwisp-monitoring/files/lib/openwisp-monitoring/interfaces.lua @@ -155,6 +155,20 @@ function interfaces.get_addresses(name) return addresses end +function interfaces.get_network_devices() + local devices = {} + uci_cursor:foreach('network', 'device', function(uci_device) + local device = {} + for key, value in pairs(uci_device) do + if not string.match(key, '^%.') then device[key] = value end + end + devices[uci_device['name']] = device + end) + return devices +end + +local network_devices = interfaces.get_network_devices() + function interfaces.get_interface_info(name, netjson_interface) local info = {dns_search = nil, dns_servers = nil} for _, interface in pairs(interface_data['interface']) do @@ -166,7 +180,12 @@ function interfaces.get_interface_info(name, netjson_interface) info.dns_servers = interface['dns-server'] end if netjson_interface.type == 'bridge' then - info.stp = uci_cursor.get('network', interface['interface'], 'stp') == '1' + local device_name = interface['device'] + if device_name and network_devices[device_name] then + info.stp = network_devices[device_name]['stp'] == '1' or '0' + else + info.stp = uci_cursor.get('network', interface['interface'], 'stp') == '1' + end end -- collect specialized info if available local specialized_info = specialized_interfaces[interface.proto] diff --git a/openwisp-monitoring/tests/basic_env.lua b/openwisp-monitoring/tests/basic_env.lua index 648d0017..bf0a5ba3 100644 --- a/openwisp-monitoring/tests/basic_env.lua +++ b/openwisp-monitoring/tests/basic_env.lua @@ -37,7 +37,8 @@ env.uci = { cursor = function() return { get_all = function(...) return nil end, - get = function(...) return nil end + get = function(...) return nil end, + foreach = function(...) return nil end } end } diff --git a/openwisp-monitoring/tests/main_env.lua b/openwisp-monitoring/tests/main_env.lua index 45ae7837..0e4546f3 100644 --- a/openwisp-monitoring/tests/main_env.lua +++ b/openwisp-monitoring/tests/main_env.lua @@ -25,6 +25,12 @@ env.uci = { return '/sys/devices/platform/soc/8af8800.usb3/8a00000.dwc3/' .. 'xhci-hcd.0.auto/usb2/2-1' end + end, + foreach = function(...) + local args = {...} + if args[2] == 'network' and args[1] == 'device' then + args[4]({name = 'br-lan2', stp = '1'}) + end end } end diff --git a/openwisp-monitoring/tests/test_dhcp.lua b/openwisp-monitoring/tests/test_dhcp.lua index 6c6cabe6..10c76c53 100644 --- a/openwisp-monitoring/tests/test_dhcp.lua +++ b/openwisp-monitoring/tests/test_dhcp.lua @@ -50,7 +50,8 @@ TestNetJSON = { return nil end end, - get = function(...) return nil end + get = function(...) return nil end, + foreach = function(...) return nil end } end } diff --git a/openwisp-monitoring/tests/test_files/interface_data.lua b/openwisp-monitoring/tests/test_files/interface_data.lua index b3489fdf..303f484e 100644 --- a/openwisp-monitoring/tests/test_files/interface_data.lua +++ b/openwisp-monitoring/tests/test_files/interface_data.lua @@ -304,6 +304,19 @@ test_data.br_lan_interface = { up = true } +test_data.br_lan2_interface = { + bridge_members = { + "lan1", "lan2", "mesh0", "mesh1", "wan", "wlan0", "wlan1", "wlan2" + }, + mac = "00:00:00:00:00:00", + mtu = 1500, + multicast = true, + name = "br-lan2", + txqueuelen = 1000, + type = "bridge", + up = true +} + test_data.lan2_interface = { link_supported = { "10baseT-H", "10baseT-F", "100baseT-H", "100baseT-F", "1000baseT-F" diff --git a/openwisp-monitoring/tests/test_interfaces.lua b/openwisp-monitoring/tests/test_interfaces.lua index 1e556e94..968be654 100644 --- a/openwisp-monitoring/tests/test_interfaces.lua +++ b/openwisp-monitoring/tests/test_interfaces.lua @@ -59,7 +59,8 @@ TestNetJSON = { 'xhci-hcd.0.auto/usb2/2-1' end return nil - end + end, + foreach = function(...) return nil end } end } @@ -123,11 +124,18 @@ function TestInterface.test_get_addresses() end function TestInterface.test_get_interface_info() + -- For OpenWrt < 21 local interface_functions = require('interfaces') local interface_info = interface_functions.get_interface_info('br-lan', interface_data.br_lan_interface) luaunit.assertEquals(interface_info, {dns_servers = {"8.8.8.8", "8.8.4.4"}, stp = true}) + -- For OpenWrt >= 21 + -- local interface_info = interface_functions.get_interface_info('br-lan2', + -- interface_data.br_lan2_interface) + -- luaunit.assertEquals(interface_info, + -- {stp = true}) + end function TestInterface.test_specialized_info() diff --git a/openwisp-monitoring/tests/test_ula_prefix.lua b/openwisp-monitoring/tests/test_ula_prefix.lua index 1724de62..d522760e 100644 --- a/openwisp-monitoring/tests/test_ula_prefix.lua +++ b/openwisp-monitoring/tests/test_ula_prefix.lua @@ -10,7 +10,12 @@ TestUla = { local env = require('main_env') package.loaded.ubus = env.ubus package.loaded.uci = { - cursor = function() return {get = function(...) return nil end} end + cursor = function() + return { + get = function(...) return nil end, + foreach = function(...) return nil end + } + end } package.loaded.nixio = { getifaddrs = function() return require('test_files/nixio_data') end