Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Use device network section for STP on OpenWrt > 21 #123 #126

Merged
merged 2 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion openwisp-monitoring/files/lib/openwisp-monitoring/interfaces.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -166,7 +180,14 @@ 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'
-- On OpenWrt > 21, "stp" is present in the "device" section
local device_name = interface['device']
if device_name and network_devices[device_name] then
info.stp = network_devices[device_name]['stp']
else
info.stp = uci_cursor.get('network', interface['interface'], 'stp')
end
info.stp = info.stp == '1'
end
-- collect specialized info if available
local specialized_info = specialized_interfaces[interface.proto]
Expand Down
3 changes: 2 additions & 1 deletion openwisp-monitoring/tests/basic_env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 7 additions & 0 deletions openwisp-monitoring/tests/main_env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ 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[3] == 'device' then
args[4]({name = 'br-lan2', stp = '1'})
args[4]({name = 'br-lan3', stp = '0'})
end
end
}
end
Expand Down
3 changes: 2 additions & 1 deletion openwisp-monitoring/tests/test_dhcp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
102 changes: 102 additions & 0 deletions openwisp-monitoring/tests/test_files/interface_data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,84 @@ test_data.interface_data = {
up = true,
updated = {"addresses"},
uptime = 7738
}, {
autostart = true,
available = true,
data = {leasetime = 86400},
delegation = true,
device = "br-lan2",
["dns-search"] = {},
["dns-server"] = {"8.8.8.8", "8.8.4.4"},
dns_metric = 0,
dynamic = false,
inactive = {
["dns-search"] = {},
["dns-server"] = {},
["ipv4-address"] = {},
["ipv6-address"] = {},
neighbors = {},
route = {}
},
interface = "lan3",
["ipv4-address"] = {{address = "192.168.1.42", mask = 24}},
["ipv6-address"] = {},
["ipv6-prefix"] = {},
["ipv6-prefix-assignment"] = {{address = "fd78:adb4:afb3::", mask = 60}},
l3_device = "br-lan2",
metric = 0,
neighbors = {},
pending = false,
proto = "dhcp",
route = {
{
target = "0.0.0.0",
mask = 0,
nexthop = "192.168.1.1",
source = "192.168.1.41/32"
}
},
up = true,
updated = {"addresses"},
uptime = 773875
}, {
autostart = true,
available = true,
data = {leasetime = 86400},
delegation = true,
device = "br-lan3",
["dns-search"] = {},
["dns-server"] = {"8.8.8.8", "8.8.4.4"},
dns_metric = 0,
dynamic = false,
inactive = {
["dns-search"] = {},
["dns-server"] = {},
["ipv4-address"] = {},
["ipv6-address"] = {},
neighbors = {},
route = {}
},
interface = "lan3",
["ipv4-address"] = {{address = "192.168.1.42", mask = 24}},
["ipv6-address"] = {},
["ipv6-prefix"] = {},
["ipv6-prefix-assignment"] = {{address = "fd78:adb4:afb3::", mask = 60}},
l3_device = "br-lan3",
metric = 0,
neighbors = {},
pending = false,
proto = "dhcp",
route = {
{
target = "0.0.0.0",
mask = 0,
nexthop = "192.168.1.1",
source = "192.168.1.41/32"
}
},
up = true,
updated = {"addresses"},
uptime = 773875
}, {
autostart = true,
available = true,
Expand Down Expand Up @@ -304,6 +382,30 @@ 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
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the test expecting stp as true even though is not defined here?
Is the code assuming STP is turned on by default? That is not the case https://openwrt.org/docs/guide-user/base-system/basic-networking.

Copy link
Member Author

@pandafy pandafy Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was following the existing code here. I will update the test to check for multiple scenarios.

elseif arg[1] == 'network' and arg[3] == 'stp' then
return '1'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ubus:call('network.interface', 'dump', {}) command does not return the STP status.

It is fetched by another call uci_cursor.get('network', interface['interface'], 'stp') on OpenWrr < 21.

On OpenWrt >= 21, we loop over all the device sections and get the STP value for the bridge


test_data.br_lan3_interface = {
bridge_members = {"lan1", "lan2"},
mac = "00:00:00:00:00:00",
mtu = 1500,
multicast = true,
name = "br-lan3",
txqueuelen = 1000,
type = "bridge",
up = true
}

test_data.lan2_interface = {
link_supported = {
"10baseT-H", "10baseT-F", "100baseT-H", "100baseT-F", "1000baseT-F"
Expand Down
18 changes: 16 additions & 2 deletions openwisp-monitoring/tests/test_interfaces.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ TestNetJSON = {
'xhci-hcd.0.auto/usb2/2-1'
end
return nil
end
end,
foreach = function(...) return nil end
}
end
}
Expand Down Expand Up @@ -123,11 +124,24 @@ 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
-- Test STP is set to true
interface_info = interface_functions.get_interface_info('br-lan2',
interface_data.br_lan2_interface)
luaunit.assertEquals(interface_info,
{dns_servers = {"8.8.8.8", "8.8.4.4"}, stp = true})
-- Test STP is set false
interface_info = interface_functions.get_interface_info('br-lan3',
interface_data.br_lan3_interface)
luaunit.assertEquals(interface_info,
{dns_servers = {"8.8.8.8", "8.8.4.4"}, stp = false})

end

function TestInterface.test_specialized_info()
Expand Down Expand Up @@ -156,7 +170,7 @@ function TestNetJSON.test_interfaces()
local netjson = cjson.decode(netjson_file('*'))
luaunit.assertEquals(netjson["interfaces"][2]["mobile"]["signal"]["umts"], nil)
luaunit.assertEquals(netjson["interfaces"][3]["addresses"][1]["address"],
"192.168.1.41")
"192.168.1.41")
luaunit.assertEquals(netjson["interfaces"][3]["stp"], true)
luaunit.assertEquals(netjson["interfaces"][2]["mobile"]["signal"]["lte"]["snr"],
19.2)
Expand Down
7 changes: 6 additions & 1 deletion openwisp-monitoring/tests/test_ula_prefix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading