+ -- read switch topology
+ local boardinfo = jsc.parse(nfs.readfile("/etc/board.json") or "")
+ if type(boardinfo) == "table" and type(boardinfo.switch) == "table" then
+ local switch, layout
+ for switch, layout in pairs(boardinfo.switch) do
+ if type(layout) == "table" and type(layout.ports) == "table" then
+ local _, port
+ local ports = { }
+ local nports = { }
+ local netdevs = { }
+
+ for _, port in ipairs(layout.ports) do
+ if type(port) == "table" and
+ type(port.num) == "number" and
+ (type(port.role) == "string" or
+ type(port.device) == "string")
+ then
+ local spec = {
+ num = port.num,
+ role = port.role or "cpu",
+ index = port.index or port.num
+ }
+
+ if port.device then
+ spec.device = port.device
+ spec.tagged = port.need_tag
+ netdevs[tostring(port.num)] = port.device
+ end
+
+ ports[#ports+1] = spec
+
+ if port.role then
+ nports[port.role] = (nports[port.role] or 0) + 1
+ end
+ end
+ end
+
+ table.sort(ports, function(a, b)
+ if a.role ~= b.role then
+ return (a.role < b.role)
+ end
+
+ return (a.index < b.index)
+ end)
+
+ local pnum, role
+ for _, port in ipairs(ports) do
+ if port.role ~= role then
+ role = port.role
+ pnum = 1
+ end
+
+ if role == "cpu" then
+ port.label = "CPU (%s)" % port.device
+ elseif nports[role] > 1 then
+ port.label = "%s %d" %{ role:upper(), pnum }
+ pnum = pnum + 1
+ else
+ port.label = role:upper()
+ end
+
+ port.role = nil
+ port.index = nil
+ end
+
+ _swtopo[switch] = {
+ ports = ports,
+ netdevs = netdevs
+ }
+ end
+ end
+ end
+