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 -- Parse some common switch properties from swconfig help output.
29 local swc = io.popen("swconfig dev %q help 2>/dev/null" % switch_name)
32 local is_port_attr = false
33 local is_vlan_attr = false
36 local line = swc:read("*l")
37 if not line then break end
39 if line:match("^%s+%-%-vlan") then
42 elseif line:match("^%s+%-%-port") then
46 elseif line:match("^Switch %d+:") then
47 num_ports, cpu_port, num_vlans =
48 line:match("ports: (%d+) %(cpu @ (%d+)%), vlans: (%d+)")
50 num_ports = tonumber(num_ports or 5)
51 num_vlans = tonumber(num_vlans or 16)
52 cpu_port = tonumber(cpu_port or 5)
54 elseif line:match(": pvid") or line:match(": tag") or line:match(": vid") then
55 if is_vlan_attr then has_vlan4k = line:match(": (%w+)") end
56 if is_port_attr then has_ptpvid = line:match(": (%w+)") end
65 -- The PVID options (if any) are added to this table so that
66 -- section create below can add the just created vlan to the
67 -- choice list of the PVID options...
70 -- This function re-reads all existing vlan ids and populates
71 -- PVID options choice lists
72 local function populate_pvids()
74 m.uci:foreach("network", "switch_vlan",
76 local vid = s[has_vlan4k or "vlan"] or s["vlan"]
78 vlan_ids[#vlan_ids+1] = vid
83 for _, opt in ipairs(pvid_opts) do
85 opt:value("", translate("none"))
86 for _, vid in luci.util.vspairs(vlan_ids) do
87 opt:value(vid, translatef("VLAN %d", tonumber(vid)))
93 s = m:section(NamedSection, x['.name'], "switch", translatef("Switch %q", switch_name))
96 s:option(Flag, "enable", "Enable this switch")
97 .cfgvalue = function(self, section) return Flag.cfgvalue(self, section) or self.enabled end
99 s:option(Flag, "enable_vlan", "Enable VLAN functionality")
100 .cfgvalue = function(self, section) return Flag.cfgvalue(self, section) or self.enabled end
102 s:option(Flag, "reset", "Reset switch during setup")
103 .cfgvalue = function(self, section) return Flag.cfgvalue(self, section) or self.enabled end
107 s = m:section(TypedSection, "switch_vlan", translatef("VLANs on %q", switch_name))
108 s.template = "cbi/tblsection"
112 -- Override cfgsections callback to enforce row ordering by vlan id.
113 s.cfgsections = function(self)
114 local osections = TypedSection.cfgsections(self)
118 for _, section in luci.util.spairs(
121 return (tonumber(m.uci:get("network", osections[a], has_vlan4k or "vlan")) or 9999)
122 < (tonumber(m.uci:get("network", osections[b], has_vlan4k or "vlan")) or 9999)
125 sections[#sections+1] = section
131 -- When creating a new vlan, preset it with the highest found vid + 1.
132 -- Repopulate the PVID choice lists afterwards.
133 s.create = function(self, section)
134 local sid = TypedSection.create(self, section)
139 m.uci:foreach("network", "switch_vlan",
141 local nr = tonumber(s.vlan)
142 local id = has_vlan4k and tonumber(s[has_vlan4k])
143 if nr ~= nil and nr > max_nr then max_nr = nr end
144 if id ~= nil and id > max_id then max_id = id end
147 m.uci:set("network", sid, "vlan", max_nr + 1)
150 m.uci:set("network", sid, has_vlan4k, max_id + 1)
153 -- add newly created vlan to the pvid choice list
159 -- Repopulate PVId choice lists if a vlan gets removed.
160 s.remove = function(self, section)
161 local rv = TypedSection.remove(self, section)
163 -- repopulate pvid choices
170 local port_opts = { }
173 -- Parse current tagging state from the "ports" option.
174 local portvalue = function(self, section)
176 for pt in (m.uci:get("network", section, "ports") or ""):gmatch("%w+") do
177 local pc, tu = pt:match("^(%d+)([tu]*)")
178 if pc == self.option then return (#tu > 0) and tu or "u" end
183 -- Validate port tagging. Ensure that a port is only untagged once,
185 local portvalidate = function(self, value, section)
186 -- ensure that the ports appears untagged only once
188 if not untagged[self.option] then
189 untagged[self.option] = true
192 translatef("Port %d is untagged in multiple VLANs!", tonumber(self.option) + 1)
199 local vid = s:option(Value, has_vlan4k or "vlan", "VLAN ID")
203 -- Validate user provided VLAN ID, make sure its within the bounds
204 -- allowed by the switch.
205 vid.validate = function(self, value, section)
206 local v = tonumber(value)
207 local m = has_vlan4k and 4094 or (num_vlans - 1)
208 if v ~= nil and v > 0 and v <= m then
212 translatef("Invalid VLAN ID given! Only IDs between %d and %d are allowed.", 1, m)
216 -- When writing the "vid" or "vlan" option, serialize the port states
217 -- as well and write them as "ports" option to uci.
218 vid.write = function(self, section, value)
222 for _, o in ipairs(port_opts) do
223 local v = o:formvalue(section)
225 p[#p+1] = o.option .. v
231 m.uci:set("network", section, "ports", table.concat(p, " "))
232 return Value.write(self, section, value)
235 -- Fallback to "vlan" option if "vid" option is supported but unset.
236 vid.cfgvalue = function(self, section)
237 return m.uci:get("network", section, has_vlan4k or "vlan")
238 or m.uci:get("network", section, "vlan")
241 -- Build per-port off/untagged/tagged choice lists.
243 for pt = 0, num_ports - 1 do
244 local po = s:option(ListValue, tostring(pt),
245 (pt == cpu_port) and translate("CPU") or translatef("Port %d", (pt + 1)))
247 po:value("", translate("off"))
248 po:value("u" % pt, translate("untagged"))
249 po:value("t" % pt, translate("tagged"))
251 po.cfgvalue = portvalue
252 po.validate = portvalidate
254 port_opts[#port_opts+1] = po
258 -- Does this switch support PVIDs?
261 -- Spawn a "virtual" section. We just attach it to the global
262 -- switch section here, the overrides below take care of writing
263 -- the actual values to the correct uci sections.
264 s = m:section(TypedSection, "switch",
265 translatef("Port PVIDs on %q", switch_name),
266 translate("Port <abbr title=\"Primary VLAN IDs\">PVIDs</abbr> specify " ..
267 "the default VLAN ID added to received untagged frames.<br />" ..
268 "Leave the ID field empty to disable auto tagging on the associated port."))
270 s.template = "cbi/tblsection"
274 -- Build port list, store pointers to the option objects in the
275 -- pvid_opts array so that other callbacks can repopulate their
278 for pt = 0, num_ports - 1 do
279 local po = s:option(ListValue, tostring(pt),
280 (pt == cpu_port) and translate("CPU") or translatef("Port %d", (pt + 1)))
282 -- When cbi queries the current config value for this post,
283 -- lookup the associated switch_port section (if any) and
284 -- return its "pvid" or "vlan" option value.
285 po.cfgvalue = function(self, section)
287 m.uci:foreach("network", "switch_port",
289 if s.port == self.option then
297 -- On write, find the actual switch_port section associated
298 -- to this port and set the value there. Create a new
299 -- switch_port section for this port if there is none yet.
300 po.write = function(self, section, value)
303 m.uci:foreach("network", "switch_port",
305 if s.port == self.option then
306 m.uci:set("network", s['.name'], has_ptpvid, value)
313 m.uci:section("network", "switch_port", nil, {
314 ["port"] = self.option,
320 -- If the user cleared the PVID value on this port, find
321 -- the associated switch_port section and clear it.
322 -- If the section does not contain any other unrelated
323 -- options (like led or blinkrate) then remove it completely,
324 -- else just clear out the "pvid" option.
325 po.remove = function(self, section)
326 m.uci:foreach("network", "switch_port",
328 if s.port == self.option then
332 for k, _ in pairs(s) do
333 if k:sub(1,1) ~= "." and k ~= "port" and k ~= has_ptpvid then
340 m.uci:delete("network", s['.name'])
342 m.uci:delete("network", s['.name'], has_ptpvid)
350 -- The referenced VLAN might just have been removed, simply
351 -- return "" (none) in this case to avoid triggering a
353 po.validate = function(...)
354 return ListValue.validate(...) or ""
357 pvid_opts[#pvid_opts+1] = po