2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2010-2011 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']
23 local has_vlan4k = nil
24 local has_ptpvid = nil
25 local has_jumbo3 = nil
32 local enable_vlan4k = false
34 -- Parse some common switch properties from swconfig help output.
35 local swc = io.popen("swconfig dev %q help 2>/dev/null" % switch_name)
38 local is_port_attr = false
39 local is_vlan_attr = false
42 local line = swc:read("*l")
43 if not line then break end
45 if line:match("^%s+%-%-vlan") then
48 elseif line:match("^%s+%-%-port") then
52 elseif line:match("^Switch %d+:") then
53 num_ports, cpu_port, num_vlans =
54 line:match("ports: (%d+) %(cpu @ (%d+)%), vlans: (%d+)")
56 num_ports = tonumber(num_ports) or 6
57 num_vlans = tonumber(num_vlans) or 16
58 cpu_port = tonumber(cpu_port) or 5
61 elseif line:match(": pvid") or line:match(": tag") or line:match(": vid") then
62 if is_vlan_attr then has_vlan4k = line:match(": (%w+)") end
63 if is_port_attr then has_ptpvid = line:match(": (%w+)") end
65 elseif line:match(": enable_vlan") then
66 has_vlan = "enable_vlan"
68 elseif line:match(": enable_learning") then
69 has_learn = "enable_learning"
71 elseif line:match(": enable_vlan4k") then
74 elseif line:match(": max_length") then
75 has_jumbo3 = "max_length"
83 -- The PVID options (if any) are added to this table so that
84 -- section create below can add the just created vlan to the
85 -- choice list of the PVID options...
88 -- This function re-reads all existing vlan ids and populates
89 -- PVID options choice lists
90 local function populate_pvids()
92 m.uci:foreach("network", "switch_vlan",
94 local vid = s[has_vlan4k or "vlan"] or s["vlan"]
96 vlan_ids[#vlan_ids+1] = vid
101 for _, opt in ipairs(pvid_opts) do
103 opt:value("", translate("none"))
104 for _, vid in luci.util.vspairs(vlan_ids) do
105 opt:value(vid, translatef("VLAN %d", tonumber(vid)))
111 s = m:section(NamedSection, x['.name'], "switch", translatef("Switch %q", switch_name))
115 s:option(Flag, has_vlan, translate("Enable VLAN functionality"))
118 if enable_vlan4k then
119 s:option(Flag, "enable_vlan4k", translate("Enable 4K VLANs"))
123 x = s:option(Flag, has_learn, translate("Enable learning and aging"))
124 x.default = x.enabled
128 x = s:option(Flag, has_jumbo3, translate("Enable Jumbo Frame passthrough"))
135 s = m:section(TypedSection, "switch_vlan", translatef("VLANs on %q", switch_name))
136 s.template = "cbi/tblsection"
140 -- Override cfgsections callback to enforce row ordering by vlan id.
141 s.cfgsections = function(self)
142 local osections = TypedSection.cfgsections(self)
146 for _, section in luci.util.spairs(
149 return (tonumber(m.uci:get("network", osections[a], has_vlan4k or "vlan")) or 9999)
150 < (tonumber(m.uci:get("network", osections[b], has_vlan4k or "vlan")) or 9999)
153 sections[#sections+1] = section
159 -- When creating a new vlan, preset it with the highest found vid + 1.
160 -- Repopulate the PVID choice lists afterwards.
161 s.create = function(self, section)
162 local sid = TypedSection.create(self, section)
167 m.uci:foreach("network", "switch_vlan",
169 local nr = tonumber(s.vlan)
170 local id = has_vlan4k and tonumber(s[has_vlan4k])
171 if nr ~= nil and nr > max_nr then max_nr = nr end
172 if id ~= nil and id > max_id then max_id = id end
175 m.uci:set("network", sid, "vlan", max_nr + 1)
178 m.uci:set("network", sid, has_vlan4k, max_id + 1)
181 -- add newly created vlan to the pvid choice list
187 -- Repopulate PVId choice lists if a vlan gets removed.
188 s.remove = function(self, section)
189 local rv = TypedSection.remove(self, section)
191 -- repopulate pvid choices
198 local port_opts = { }
201 -- Parse current tagging state from the "ports" option.
202 local portvalue = function(self, section)
204 for pt in (m.uci:get("network", section, "ports") or ""):gmatch("%w+") do
205 local pc, tu = pt:match("^(%d+)([tu]*)")
206 if pc == self.option then return (#tu > 0) and tu or "u" end
211 -- Validate port tagging. Ensure that a port is only untagged once,
213 local portvalidate = function(self, value, section)
214 -- ensure that the ports appears untagged only once
216 if not untagged[self.option] then
217 untagged[self.option] = true
218 elseif min_vid > 0 or tonumber(self.option) ~= cpu_port then -- enable multiple untagged cpu ports due to weird broadcom default setup
220 translatef("Port %d is untagged in multiple VLANs!", tonumber(self.option) + 1)
227 local vid = s:option(Value, has_vlan4k or "vlan", "VLAN ID")
230 vid.forcewrite = true
232 -- Validate user provided VLAN ID, make sure its within the bounds
233 -- allowed by the switch.
234 vid.validate = function(self, value, section)
235 local v = tonumber(value)
236 local m = has_vlan4k and 4094 or (num_vlans - 1)
237 if v ~= nil and v >= min_vid and v <= m then
241 translatef("Invalid VLAN ID given! Only IDs between %d and %d are allowed.", min_vid, m)
245 -- When writing the "vid" or "vlan" option, serialize the port states
246 -- as well and write them as "ports" option to uci.
247 vid.write = function(self, section, value)
251 for _, o in ipairs(port_opts) do
252 local v = o:formvalue(section)
254 p[#p+1] = o.option .. v
260 m.uci:set("network", section, "ports", table.concat(p, " "))
261 return Value.write(self, section, value)
264 -- Fallback to "vlan" option if "vid" option is supported but unset.
265 vid.cfgvalue = function(self, section)
266 return m.uci:get("network", section, has_vlan4k or "vlan")
267 or m.uci:get("network", section, "vlan")
270 -- Build per-port off/untagged/tagged choice lists.
272 for pt = 0, num_ports - 1 do
273 local po = s:option(ListValue, tostring(pt),
274 (pt == cpu_port) and translate("CPU") or translatef("Port %d", (pt + 1)))
276 po:value("", translate("off"))
277 po:value("u" % pt, translate("untagged"))
278 po:value("t" % pt, translate("tagged"))
280 po.cfgvalue = portvalue
281 po.validate = portvalidate
282 po.write = function() end
284 port_opts[#port_opts+1] = po
288 -- Does this switch support PVIDs?
291 -- Spawn a "virtual" section. We just attach it to the global
292 -- switch section here, the overrides below take care of writing
293 -- the actual values to the correct uci sections.
294 s = m:section(TypedSection, "switch",
295 translatef("Port PVIDs on %q", switch_name),
296 translate("Port <abbr title=\"Primary VLAN IDs\">PVIDs</abbr> specify " ..
297 "the default VLAN ID added to received untagged frames."))
299 s.template = "cbi/tblsection"
303 -- Build port list, store pointers to the option objects in the
304 -- pvid_opts array so that other callbacks can repopulate their
307 for pt = 0, num_ports - 1 do
308 local po = s:option(ListValue, tostring(pt),
309 (pt == cpu_port) and translate("CPU") or translatef("Port %d", (pt + 1)))
311 -- When cbi queries the current config value for this post,
312 -- lookup the associated switch_port section (if any) and
313 -- return its "pvid" or "vlan" option value.
314 po.cfgvalue = function(self, section)
316 m.uci:foreach("network", "switch_port",
318 if s.port == self.option then
326 -- On write, find the actual switch_port section associated
327 -- to this port and set the value there. Create a new
328 -- switch_port section for this port if there is none yet.
329 po.write = function(self, section, value)
332 m.uci:foreach("network", "switch_port",
334 if s.port == self.option then
335 m.uci:set("network", s['.name'], has_ptpvid, value)
342 m.uci:section("network", "switch_port", nil, {
343 ["port"] = self.option,
349 -- If the user cleared the PVID value on this port, find
350 -- the associated switch_port section and clear it.
351 -- If the section does not contain any other unrelated
352 -- options (like led or blinkrate) then remove it completely,
353 -- else just clear out the "pvid" option.
354 po.remove = function(self, section)
355 m.uci:foreach("network", "switch_port",
357 if s.port == self.option then
361 for k, _ in pairs(s) do
362 if k:sub(1,1) ~= "." and k ~= "port" and k ~= has_ptpvid then
369 m.uci:delete("network", s['.name'])
371 m.uci:delete("network", s['.name'], has_ptpvid)
379 -- The referenced VLAN might just have been removed, simply
380 -- return "" (none) in this case to avoid triggering a
382 po.validate = function(...)
383 return ListValue.validate(...) or ""
386 pvid_opts[#pvid_opts+1] = po