2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2010 Jo-Philipp Wich <xm@subsignal.org>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
16 m = Map("network", translate("Switch"), translate("The network ports on your router can be combined to several <abbr title=\"Virtual Local Area Network\">VLAN</abbr>s in which computers can communicate directly with each other. <abbr title=\"Virtual Local Area Network\">VLAN</abbr>s are often used to separate different network segments. Often there is by default one Uplink port for a connection to the next greater network like the internet and other ports for a local network."))
18 m.uci:foreach("network", "switch",
20 local switch_name = x.name or x['.name']
21 local has_vlan4k = nil
22 local has_ptpvid = nil
28 local swc = io.popen("swconfig dev %q help 2>/dev/null" % switch_name)
31 local is_port_attr = false
32 local is_vlan_attr = false
35 local line = swc:read("*l")
36 if not line then break end
38 if line:match("^%s+%-%-vlan") then
41 elseif line:match("^%s+%-%-port") then
45 elseif line:match("^Switch %d+:") then
46 num_ports, cpu_port, num_vlans =
47 line:match("ports: (%d+) %(cpu @ (%d+)%), vlans: (%d+)")
49 num_ports = tonumber(num_ports or 5)
50 num_vlans = tonumber(num_vlans or 16)
51 cpu_port = tonumber(cpu_port or 5)
53 elseif line:match("%-%-pvid") or line:match("%-%-tag") or line:match("%-%-vid") then
54 if is_vlan_attr then has_vlan4k = line:match("%-%-(%w+)") end
55 if is_port_attr then has_ptpvid = line:match("%-%-(%w+)") end
64 s = m:section(NamedSection, x['.name'], "switch", "Switch %q" % switch_name)
67 s:option(Flag, "enable", "Enable this switch")
68 .cfgvalue = function(self, section) return Flag.cfgvalue(self, section) or self.enabled end
70 s:option(Flag, "enable_vlan", "Enable VLAN functionality")
71 .cfgvalue = function(self, section) return Flag.cfgvalue(self, section) or self.enabled end
73 s:option(Flag, "reset", "Reset switch during setup")
74 .cfgvalue = function(self, section) return Flag.cfgvalue(self, section) or self.enabled end
78 s = m:section(TypedSection, "switch_vlan", "VLANs on %q" % switch_name)
79 s.template = "cbi/tblsection"
83 s.cfgsections = function(self)
84 local osections = TypedSection.cfgsections(self)
88 for _, section in luci.util.spairs(
91 return (tonumber(m.uci:get("network", osections[a], has_vlan4k or "vlan") or 9999) or 0)
92 < (tonumber(m.uci:get("network", osections[b], has_vlan4k or "vlan") or 9999) or 0)
95 sections[#sections+1] = section
101 s.create = function(self, section)
102 local sid = TypedSection.create(self, section)
107 m.uci:foreach("network", "switch_vlan",
109 local nr = tonumber(s.vlan)
110 local id = has_vlan4k and tonumber(s[has_vlan4k])
111 if nr ~= nil and nr > max_nr then max_nr = nr end
112 if id ~= nil and id > max_id then max_id = id end
115 m.uci:set("network", sid, "vlan", max_nr + 1)
118 m.uci:set("network", sid, has_vlan4k, max_id + 1)
125 local port_opts = { }
128 local portvalue = function(self, section)
130 for pt in (m.uci:get("network", section, "ports") or ""):gmatch("%w+") do
131 local pc, tu = pt:match("^(%d+)([tu]*)")
132 if pc == self.option then return (#tu > 0) and tu or "u" end
137 local portvalidate = function(self, value, section)
138 -- ensure that the ports appears untagged only once
140 if not untagged[self.option] then
141 untagged[self.option] = true
144 translatef("Port %d is untagged in multiple VLANs!", tonumber(self.option) + 1)
151 local vid = s:option(Value, has_vlan4k or "vlan", "VLAN ID")
155 vid.validate = function(self, value, section)
156 local v = tonumber(value)
157 local m = has_vlan4k and 4094 or (num_vlans - 1)
158 if v ~= nil and v > 0 and v <= m then
162 translatef("Invalid VLAN ID given! Only IDs between %d and %d are allowed.", 1, m)
166 vid.write = function(self, section, value)
170 for _, o in ipairs(port_opts) do
171 local v = o:formvalue(section)
173 p[#p+1] = o.option .. v
179 m.uci:set("network", section, "ports", table.concat(p, " "))
180 return Value.write(self, section, value)
185 for pt = 0, num_ports - 1 do
186 local po = s:option(ListValue, tostring(pt),
187 (pt == cpu_port) and "CPU" or "Port %d" % (pt + 1))
189 po:value("", translate("off"))
190 po:value("u" % pt, translate("untagged"))
191 po:value("t" % pt, translate("tagged"))
193 po.cfgvalue = portvalue
194 po.validate = portvalidate
196 port_opts[#port_opts+1] = po