From 98e37433e7f33b3e2ee751250e04b194a0aa6a0b Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Thu, 24 Aug 2023 16:44:40 +0200 Subject: [PATCH] luci-base: rpc: add call to enumerate builtin ethernet ports Add a new luci/getBuiltinEthernetPorts RPC call which returns a consolidated list of known ethernet ports found in `/etc/board.json`. Add an x86/64 specific workaround which attempts to enumerate missing ethernet devices too. Ref: #6534, #6538 Signed-off-by: Jo-Philipp Wich --- .../luci-base/root/usr/share/rpcd/ucode/luci | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/modules/luci-base/root/usr/share/rpcd/ucode/luci b/modules/luci-base/root/usr/share/rpcd/ucode/luci index 35f592578b..3c4fea4691 100644 --- a/modules/luci-base/root/usr/share/rpcd/ucode/luci +++ b/modules/luci-base/root/usr/share/rpcd/ucode/luci @@ -8,7 +8,7 @@ import { cursor } from 'uci'; import { init_list, init_index, init_enabled, init_action, conntrack_list, process_list } from 'luci.sys'; import { revision, branch } from 'luci.version'; -import { statvfs } from 'luci.core'; +import { statvfs, uname } from 'luci.core'; import timezones from 'luci.zoneinfo'; @@ -538,6 +538,49 @@ const methods = { call: function() { return { result: process_list() }; } + }, + + getBuiltinEthernetPorts: { + call: function() { + let fd = open('/etc/board.json', 'r'); + let board = fd ? json(fd) : {}; + let ports = []; + + for (let k in [ 'lan', 'wan' ]) { + if (!board?.network?.[k]) + continue; + + if (type(board.network[k].ports) == 'array') { + for (let ifname in board.network[k].ports) { + push(ports, { role: k, device: ifname }); + } + } + else if (type(board.network[k].device) == 'string') { + push(ports, { role: k, device: board.network[k].device }); + } + } + + /* Workaround for targets that do not enumerate all netdevs in board.json */ + if (uname().machine in [ 'x86_64' ] && + match(ports[0]?.device, /^eth\d+$/)) { + let bus = readlink(`/sys/class/net/${ports[0].device}/device/subsystem`); + + for (let netdev in lsdir('/sys/class/net')) { + if (!match(netdev, /^eth\d+$/)) + continue; + + if (length(filter(ports, port => port.device == netdev))) + continue; + + if (readlink(`/sys/class/net/${netdev}/device/subsystem`) != bus) + continue; + + push(ports, { role: 'unknown', device: netdev }); + } + } + + return { result: ports }; + } } }; -- 2.30.2