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 sid = x['.name']
21 local switch_name = x.name or sid
24 local has_vlan4k = nil
25 local has_ptpvid = nil
26 local has_jumbo3 = nil
33 local enable_vlan4k = false
35 -- Parse some common switch properties from swconfig help output.
36 local swc = io.popen("swconfig dev %q help 2>/dev/null" % switch_name)
39 local is_port_attr = false
40 local is_vlan_attr = false
43 local line = swc:read("*l")
44 if not line then break end
46 if line:match("^%s+%-%-vlan") then
49 elseif line:match("^%s+%-%-port") then
53 elseif line:match("^Switch %d+:") then
54 num_ports, cpu_port, num_vlans =
55 line:match("ports: (%d+) %(cpu @ (%d+)%), vlans: (%d+)")
57 num_ports = tonumber(num_ports) or 6
58 num_vlans = tonumber(num_vlans) or 16
59 cpu_port = tonumber(cpu_port) or 5
62 elseif line:match(": pvid") or line:match(": tag") or line:match(": vid") then
63 if is_vlan_attr then has_vlan4k = line:match(": (%w+)") end
64 if is_port_attr then has_ptpvid = line:match(": (%w+)") end
66 elseif line:match(": enable_vlan4k") then
69 elseif line:match(": enable_vlan") then
70 has_vlan = "enable_vlan"
72 elseif line:match(": enable_learning") then
73 has_learn = "enable_learning"
75 elseif line:match(": max_length") then
76 has_jumbo3 = "max_length"
84 -- The PVID options (if any) are added to this table so that
85 -- section create below can add the just created vlan to the
86 -- choice list of the PVID options...
89 -- This function re-reads all existing vlan ids and populates
90 -- PVID options choice lists
91 local function populate_pvids()
93 m.uci:foreach("network", "switch_vlan",
95 if s.device == switch_name then
96 local vid = s[has_vlan4k or "vlan"] or s["vlan"]
98 vlan_ids[#vlan_ids+1] = vid
104 for _, opt in ipairs(pvid_opts) do
106 opt:value("", translate("none"))
107 for _, vid in luci.util.vspairs(vlan_ids) do
108 opt:value(vid, translatef("VLAN %d", tonumber(vid)))
114 s = m:section(NamedSection, x['.name'], "switch", translatef("Switch %q", switch_name))
118 s:option(Flag, has_vlan, translate("Enable VLAN functionality"))
121 if enable_vlan4k then
122 s:option(Flag, "enable_vlan4k", translate("Enable 4K VLANs"))
126 x = s:option(Flag, has_learn, translate("Enable learning and aging"))
127 x.default = x.enabled
131 x = s:option(Flag, has_jumbo3, translate("Enable Jumbo Frame passthrough"))
138 s = m:section(TypedSection, "switch_vlan", translatef("VLANs on %q", switch_name))
139 s.template = "cbi/tblsection"
144 s.filter = function(self, section)
145 local device = m:get(section, "device")
146 return (device and device == switch_name)
149 -- Override cfgsections callback to enforce row ordering by vlan id.
150 s.cfgsections = function(self)
151 local osections = TypedSection.cfgsections(self)
155 for _, section in luci.util.spairs(
158 return (tonumber(m:get(osections[a], has_vlan4k or "vlan")) or 9999)
159 < (tonumber(m:get(osections[b], has_vlan4k or "vlan")) or 9999)
162 sections[#sections+1] = section
168 -- When creating a new vlan, preset it with the highest found vid + 1.
169 -- Repopulate the PVID choice lists afterwards.
170 s.create = function(self, section, origin)
172 if m:get(origin, "device") ~= switch_name then
176 local sid = TypedSection.create(self, section)
181 m.uci:foreach("network", "switch_vlan",
183 if s.device == switch_name then
184 local nr = tonumber(s.vlan)
185 local id = has_vlan4k and tonumber(s[has_vlan4k])
186 if nr ~= nil and nr > max_nr then max_nr = nr end
187 if id ~= nil and id > max_id then max_id = id end
191 m.uci:set("network", sid, "device", switch_name)
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:get(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:get(section, has_vlan4k or "vlan")
284 or m:get(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"
321 function s.filter(self, section)
322 return (m:get(section, "name") == switch_name)
325 -- Build port list, store pointers to the option objects in the
326 -- pvid_opts array so that other callbacks can repopulate their
329 for pt = 0, num_ports - 1 do
330 local po = s:option(ListValue, tostring(pt),
331 (pt == cpu_port) and translate("CPU") or translatef("Port %d", (pt + 1)))
333 -- When cbi queries the current config value for this post,
334 -- lookup the associated switch_port section (if any) and
335 -- return its "pvid" or "vlan" option value.
336 po.cfgvalue = function(self, section)
338 m.uci:foreach("network", "switch_port",
340 if s.port == self.option then
348 -- On write, find the actual switch_port section associated
349 -- to this port and set the value there. Create a new
350 -- switch_port section for this port if there is none yet.
351 po.write = function(self, section, value)
354 m.uci:foreach("network", "switch_port",
356 if s.port == self.option then
357 m.uci:set("network", s['.name'], has_ptpvid, value)
364 m.uci:section("network", "switch_port", nil, {
365 ["port"] = self.option,
371 -- If the user cleared the PVID value on this port, find
372 -- the associated switch_port section and clear it.
373 -- If the section does not contain any other unrelated
374 -- options (like led or blinkrate) then remove it completely,
375 -- else just clear out the "pvid" option.
376 po.remove = function(self, section)
377 m.uci:foreach("network", "switch_port",
379 if s.port == self.option then
383 for k, _ in pairs(s) do
384 if k:sub(1,1) ~= "." and k ~= "port" and k ~= has_ptpvid then
391 m.uci:delete("network", s['.name'])
393 m.uci:delete("network", s['.name'], has_ptpvid)
401 -- The referenced VLAN might just have been removed, simply
402 -- return "" (none) in this case to avoid triggering a
404 po.validate = function(...)
405 return ListValue.validate(...) or ""
408 pvid_opts[#pvid_opts+1] = po