Skip to content

Commit

Permalink
lime-hwd-ground-routing: there is no software way to associate switchX
Browse files Browse the repository at this point in the history
to ethY so read those informations from the configurations
lime-system: support passing informations about the device from scanning
to protos
  • Loading branch information
G10h4ck committed Dec 29, 2014
1 parent caad3ac commit 87381f5
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 45 deletions.
72 changes: 50 additions & 22 deletions packages/lime-hwd-ground-routing/src/ground_routing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,58 @@ end

function ground_routing.detect_hardware()
function parse_gr(section)
local link_name = section[".name"]
local net_dev = section["net_dev"]
local vlan = section["vlan"]
local physdev = section["physdev"]
local secname = ground_routing.sectionNamePrefix..section[".name"].."_"..physdev.."_"..vlan

local switch_ports = section["switch_ports"]
if switch_ports then
local ports = ""
for _,p in pairs(switch_ports) do ports = ports..p.."t " end

local uci = libuci:cursor()
uci:set("network", secname, "switch_vlan")
uci:set("network", secname, "device", physdev)
uci:set("network", secname, "vlan", vlan)
uci:set("network", secname, "ports", ports)
uci:save("network")
else
local uci = libuci:cursor()
uci:set("network", secname, "device")
uci:set("network", secname, "name", physdev.."."..vlan)
uci:set("network", secname, "ifname", physdev)
uci:set("network", secname, "type", "8021q")
uci:set("network", secname, "vid", vlan)
uci:save("network")

local uci = libuci:cursor()

function create_8021q_dev(vlan_p)
local dev_secname = ground_routing.sectionNamePrefix..link_name.."_"..net_dev.."_"..vlan_p
uci:set("network", dev_secname, "device")
uci:set("network", dev_secname, "name", net_dev.."."..vlan_p)
uci:set("network", dev_secname, "ifname", net_dev)
uci:set("network", dev_secname, "type", "8021q")
uci:set("network", dev_secname, "vid", vlan_p)
end

local switch_dev = section["switch_dev"]
if switch_dev then
local switch_cpu_port = section["switch_cpu_port"]
function tag_cpu_port(section)
if (section["device"] ~= switch_dev) then return end

local patterns = { "^"..switch_cpu_port.." ", " "..switch_cpu_port.."$", " "..switch_cpu_port.." " }
local substits = { switch_cpu_port.."t ", " "..switch_cpu_port.."t", " "..switch_cpu_port.."t " }
local matchCount = 0
local m = 0
for i,p in pairs(patterns) do
section["ports"], m = section["ports"]:gsub(p, substits[i])
matchCount = matchCount + m
end

if (matchCount > 0) then
create_8021q_dev(section["vlan"])
uci:set("network", section[".name"], "ports", section["ports"])
end
end

uci:foreach("network", "switch_vlan", tag_cpu_port)


local sw_secname = ground_routing.sectionNamePrefix..link_name.."_sw_"..switch_dev.."_"..vlan
local ports = switch_cpu_port.."t"
for _,p in pairs(section["switch_ports"]) do ports = ports.." "..p.."t" end

uci:set("network", sw_secname, "switch_vlan")
uci:set("network", sw_secname, "device", switch_dev)
uci:set("network", sw_secname, "vlan", vlan)
uci:set("network", sw_secname, "ports", ports)
end

create_8021q_dev(vlan)

uci:save("network")
end

config.foreach("hwd_gr", parse_gr)
Expand Down
9 changes: 5 additions & 4 deletions packages/lime-system/files/etc/config/lime
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
### One section for each ground routing link

#config hwd_gr link1
# option physdev 'switch0' # Can be a switch or a plain ethernet port, if it is a switch you must specify ports if not you must not specify them
# list switch_ports '0' # Refer to switch port map of your device on openwrt wiki to know port index
# list switch_ports '4'
# option vlan '173' # 802.1q vlan vid to use for this ground routing link
# option net_dev 'eth0' # Plain ethernet device on top of which 802.1q vlan will be constructed
# option vlan '275' # 802.1q vlan vid to use for this ground routing link
# option switch_dev 'switch0' # If your ethernet device is connected to a switch chip you must specify it
# option switch_cpu_port '0' # Refer to switch port map of your device on openwrt wiki to know CPU port index
# list switch_ports '4' # List switch ports on with you want the vlan being passed
38 changes: 20 additions & 18 deletions packages/lime-system/files/usr/lib/lua/lime/network.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ function network.clean()
local uci = libuci:cursor()

uci:delete("network", "globals", "ula_prefix")
uci:delete("network", "wan", "ifname")
uci:delete("network", "lan", "ifname")
uci:set("network", "wan", "proto", "none")
uci:set("network", "wan6", "proto", "none")

--! Delete interfaces and devices generated by LiMe
uci:foreach("network", "interface", function(s) if utils.stringStarts( s[".name"], network.limeIfNamePrefix ) then uci:delete("network", s[".name"]) end end)
Expand All @@ -123,22 +123,21 @@ end

function network.scandevices()
local devices = {}
local switch_vlan = {}

function dev_parser(dev)
if dev:match("^eth%d+$") then
local insert = true
for d,_ in pairs(devices) do if d:match("^"..dev..".%d+$") then insert = false ; break; end end
if insert then devices[dev] = dev end
devices[dev] = devices[dev] or {}
end

if dev:match("^eth%d+%.%d+$") then
local rawif = dev:match("^eth%d+")
devices[rawif] = nil
devices[dev] = dev
devices[rawif] = { nobridge = true }
devices[dev] = {}
end

if dev:match("^wlan%d+_%w+$") then
devices[dev] = dev
devices[dev] = {}
end
end

Expand All @@ -154,10 +153,8 @@ function network.scandevices()
end

function owrt_switch_vlan_parser(section)
local rawif_index = section["device"]:match("%d+$")
local vlan_index = section["vlan"]
local kernel_visible = section["ports"]:match("0t")
if kernel_visible then dev_parser("eth"..rawif_index.."."..vlan_index) end
local kernel_visible = section["ports"]:match("0t")
if kernel_visible then switch_vlan[section["vlan"]] = section["device"] end
end

--! Scrape from uci wireless
Expand All @@ -174,10 +171,18 @@ function network.scandevices()
for dev in stdOut:lines() do dev_parser(dev) end
stdOut:close()

--! Scrape switch_vlan devices from /sys/class/net/
local stdOut = io.popen("ls -1 /sys/class/net/ | grep -x 'eth[0-9][0-9]*\.[0-9][0-9]*'")
for dev in stdOut:lines() do if switch_vlan[dev:match("%d+$")] then dev_parser(dev) end end
stdOut:close()

return devices
end

function network.configure()
local specificIfaces = {}
config.foreach("net", function(iface) specificIfaces[iface["linux_name"]] = iface end)
local fisDevs = network.scandevices()

network.setup_rp_filter()

Expand All @@ -193,12 +198,8 @@ function network.configure()
end
end

local specificIfaces = {}
config.foreach("net", function(iface) specificIfaces[iface["linux_name"]] = iface end)

--! Scan for fisical devices, if there is a specific config apply that otherwise apply general config
local fisDevs = network.scandevices()
for _,device in pairs(fisDevs) do
--! For each scanned fisical device, if there is a specific config apply that one otherwise apply general config
for device,flags in pairs(fisDevs) do
local owrtIf = specificIfaces[device]
local deviceProtos = generalProtocols
if owrtIf then deviceProtos = owrtIf["protocols"] end
Expand All @@ -207,6 +208,7 @@ function network.configure()
local args = utils.split(protoParams, network.protoParamsSeparator)
if args[1] == "manual" then break end -- If manual is specified do not configure interface
local protoModule = "lime.proto."..args[1]
for k,v in pairs(flags) do args[k] = v end
if utils.isModuleAvailable(protoModule) then
local proto = require(protoModule)
xpcall(function() proto.setup_interface(device, args) end,
Expand Down
4 changes: 3 additions & 1 deletion packages/lime-system/files/usr/lib/lua/lime/proto/lan.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ local libuci = require("uci")

function lan.configure(args)
local ipv4, ipv6 = network.primary_address()

local uci = libuci:cursor()
uci:set("network", "lan", "ip6addr", ipv6:string())
uci:set("network", "lan", "ipaddr", ipv4:host():string())
uci:set("network", "lan", "netmask", ipv4:mask():string())
uci:set("network", "lan", "proto", "static")
uci:set("network", "lan", "mtu", "1500")
uci:delete("network", "lan", "ifname")
uci:save("network")
end

function lan.setup_interface(ifname, args)
if args and args["nobridge"] then return end
if ifname:match("^wlan") then return end
if ifname:match(network.protoVlanSeparator.."%d+$") then return end

Expand Down

0 comments on commit 87381f5

Please sign in to comment.