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']
21 local has_enable = "enable"
25 local has_vlan4k = nil
26 local has_ptpvid = nil
27 local has_jumbo3 = nil
34 local enable_vlan4k = false
36 -- Parse some common switch properties from swconfig help output.
37 local swc = io.popen("swconfig dev %q help 2>/dev/null" % switch_name)
40 local is_port_attr = false
41 local is_vlan_attr = false
44 local line = swc:read("*l")
45 if not line then break end
47 if line:match("^%s+%-%-vlan") then
50 elseif line:match("^%s+%-%-port") then
54 elseif line:match("^Switch %d+:") then
55 num_ports, cpu_port, num_vlans =
56 line:match("ports: (%d+) %(cpu @ (%d+)%), vlans: (%d+)")
58 num_ports = tonumber(num_ports or 6)
59 num_vlans = tonumber(num_vlans or 16)
60 cpu_port = tonumber(cpu_port or 5)
64 elseif line:match(": pvid") or line:match(": tag") or line:match(": vid") then
65 if is_vlan_attr then has_vlan4k = line:match(": (%w+)") end
66 if is_port_attr then has_ptpvid = line:match(": (%w+)") end
68 elseif line:match(": enable_vlan") then
69 has_vlan = "enable_vlan"
71 elseif line:match(": enable_learning") then
72 has_learn = "enable_learning"
74 elseif line:match(": reset") then
77 elseif line:match(": enable_vlan4k") then
80 elseif line:match(": max_length") then
81 has_jumbo3 = "max_length"
89 -- The PVID options (if any) are added to this table so that
90 -- section create below can add the just created vlan to the
91 -- choice list of the PVID options...
94 -- This function re-reads all existing vlan ids and populates
95 -- PVID options choice lists
96 local function populate_pvids()
98 m.uci:foreach("network", "switch_vlan",
100 local vid = s[has_vlan4k or "vlan"] or s["vlan"]
102 vlan_ids[#vlan_ids+1] = vid
107 for _, opt in ipairs(pvid_opts) do
109 opt:value("", translate("none"))
110 for _, vid in luci.util.vspairs(vlan_ids) do
111 opt:value(vid, translatef("VLAN %d", tonumber(vid)))
117 s = m:section(NamedSection, x['.name'], "switch", translatef("Switch %q", switch_name))
121 x = s:option(Flag, has_enable, translate("Enable this switch"))
122 x.default = x.enabled
126 x = s:option(Flag, has_vlan, translate("Enable VLAN functionality"))
127 x.default = x.enabled
130 if enable_vlan4k then
131 s:option(Flag, "enable_vlan4k", translate("Enable 4K VLANs"))
135 x = s:option(Flag, has_learn, translate("Enable learning and aging"))
136 x.default = x.enabled
140 x = s:option(Flag, has_jumbo3, translate("Enable Jumbo Frame passthrough"))
146 x = s:option(Flag, has_reset, translate("Reset switch during setup"))
147 x.default = x.enabled
152 s = m:section(TypedSection, "switch_vlan", translatef("VLANs on %q", switch_name))
153 s.template = "cbi/tblsection"
157 -- Override cfgsections callback to enforce row ordering by vlan id.
158 s.cfgsections = function(self)
159 local osections = TypedSection.cfgsections(self)
163 for _, section in luci.util.spairs(
166 return (tonumber(m.uci:get("network", osections[a], has_vlan4k or "vlan")) or 9999)
167 < (tonumber(m.uci:get("network", osections[b], has_vlan4k or "vlan")) or 9999)
170 sections[#sections+1] = section
176 -- When creating a new vlan, preset it with the highest found vid + 1.
177 -- Repopulate the PVID choice lists afterwards.
178 s.create = function(self, section)
179 local sid = TypedSection.create(self, section)
184 m.uci:foreach("network", "switch_vlan",
186 local nr = tonumber(s.vlan)
187 local id = has_vlan4k and tonumber(s[has_vlan4k])
188 if nr ~= nil and nr > max_nr then max_nr = nr end
189 if id ~= nil and id > max_id then max_id = id end
192 m.uci:set("network", sid, "vlan", max_nr + 1)
195 m.uci:set("network", sid, has_vlan4k, max_id + 1)
198 -- add newly created vlan to the pvid choice list
204 -- Repopulate PVId choice lists if a vlan gets removed.
205 s.remove = function(self, section)
206 local rv = TypedSection.remove(self, section)
208 -- repopulate pvid choices
215 local port_opts = { }
218 -- Parse current tagging state from the "ports" option.
219 local portvalue = function(self, section)
221 for pt in (m.uci:get("network", section, "ports") or ""):gmatch("%w+") do
222 local pc, tu = pt:match("^(%d+)([tu]*)")
223 if pc == self.option then return (#tu > 0) and tu or "u" end
228 -- Validate port tagging. Ensure that a port is only untagged once,
230 local portvalidate = function(self, value, section)
231 -- ensure that the ports appears untagged only once
233 if not untagged[self.option] then
234 untagged[self.option] = true
235 elseif min_vid > 0 or tonumber(self.option) ~= cpu_port then -- enable multiple untagged cpu ports due to weird broadcom default setup
237 translatef("Port %d is untagged in multiple VLANs!", tonumber(self.option) + 1)
244 local vid = s:option(Value, has_vlan4k or "vlan", "VLAN ID")
247 vid.forcewrite = true
249 -- Validate user provided VLAN ID, make sure its within the bounds
250 -- allowed by the switch.
251 vid.validate = function(self, value, section)
252 local v = tonumber(value)
253 local m = has_vlan4k and 4094 or (num_vlans - 1)
254 if v ~= nil and v >= min_vid and v <= m then
258 translatef("Invalid VLAN ID given! Only IDs between %d and %d are allowed.", min_vid, m)
262 -- When writing the "vid" or "vlan" option, serialize the port states
263 -- as well and write them as "ports" option to uci.
264 vid.write = function(self, section, value)
268 for _, o in ipairs(port_opts) do
269 local v = o:formvalue(section)
271 p[#p+1] = o.option .. v
277 m.uci:set("network", section, "ports", table.concat(p, " "))
278 return Value.write(self, section, value)
281 -- Fallback to "vlan" option if "vid" option is supported but unset.
282 vid.cfgvalue = function(self, section)
283 return m.uci:get("network", section, has_vlan4k or "vlan")
284 or m.uci:get("network", section, "vlan")
287 -- Build per-port off/untagged/tagged choice lists.
289 for pt = 0, num_ports - 1 do
290 local po = s:option(ListValue, tostring(pt),
291 (pt == cpu_port) and translate("CPU") or translatef("Port %d", (pt + 1)))
293 po:value("", translate("off"))
294 po:value("u" % pt, translate("untagged"))
295 po:value("t" % pt, translate("tagged"))
297 po.cfgvalue = portvalue
298 po.validate = portvalidate
299 po.write = function() end
301 port_opts[#port_opts+1] = po
305 -- Does this switch support PVIDs?
308 -- Spawn a "virtual" section. We just attach it to the global
309 -- switch section here, the overrides below take care of writing
310 -- the actual values to the correct uci sections.
311 s = m:section(TypedSection, "switch",
312 translatef("Port PVIDs on %q", switch_name),
313 translate("Port <abbr title=\"Primary VLAN IDs\">PVIDs</abbr> specify " ..
314 "the default VLAN ID added to received untagged frames."))
316 s.template = "cbi/tblsection"
320 -- Build port list, store pointers to the option objects in the
321 -- pvid_opts array so that other callbacks can repopulate their
324 for pt = 0, num_ports - 1 do
325 local po = s:option(ListValue, tostring(pt),
326 (pt == cpu_port) and translate("CPU") or translatef("Port %d", (pt + 1)))
328 -- When cbi queries the current config value for this post,
329 -- lookup the associated switch_port section (if any) and
330 -- return its "pvid" or "vlan" option value.
331 po.cfgvalue = function(self, section)
333 m.uci:foreach("network", "switch_port",
335 if s.port == self.option then
343 -- On write, find the actual switch_port section associated
344 -- to this port and set the value there. Create a new
345 -- switch_port section for this port if there is none yet.
346 po.write = function(self, section, value)
349 m.uci:foreach("network", "switch_port",
351 if s.port == self.option then
352 m.uci:set("network", s['.name'], has_ptpvid, value)
359 m.uci:section("network", "switch_port", nil, {
360 ["port"] = self.option,
366 -- If the user cleared the PVID value on this port, find
367 -- the associated switch_port section and clear it.
368 -- If the section does not contain any other unrelated
369 -- options (like led or blinkrate) then remove it completely,
370 -- else just clear out the "pvid" option.
371 po.remove = function(self, section)
372 m.uci:foreach("network", "switch_port",
374 if s.port == self.option then
378 for k, _ in pairs(s) do
379 if k:sub(1,1) ~= "." and k ~= "port" and k ~= has_ptpvid then
386 m.uci:delete("network", s['.name'])
388 m.uci:delete("network", s['.name'], has_ptpvid)
396 -- The referenced VLAN might just have been removed, simply
397 -- return "" (none) in this case to avoid triggering a
399 po.validate = function(...)
400 return ListValue.validate(...) or ""
403 pvid_opts[#pvid_opts+1] = po