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
23 local has_jumbo3 = nil
29 local enable_vlan4k = false
31 -- Parse some common switch properties from swconfig help output.
32 local swc = io.popen("swconfig dev %q help 2>/dev/null" % switch_name)
35 local is_port_attr = false
36 local is_vlan_attr = false
39 local line = swc:read("*l")
40 if not line then break end
42 if line:match("^%s+%-%-vlan") then
45 elseif line:match("^%s+%-%-port") then
49 elseif line:match("^Switch %d+:") then
50 num_ports, cpu_port, num_vlans =
51 line:match("ports: (%d+) %(cpu @ (%d+)%), vlans: (%d+)")
53 num_ports = tonumber(num_ports) or 5
54 num_vlans = tonumber(num_vlans) or 16
55 cpu_port = tonumber(cpu_port) or 5
57 elseif line:match(": pvid") or line:match(": tag") or line:match(": vid") then
58 if is_vlan_attr then has_vlan4k = line:match(": (%w+)") end
59 if is_port_attr then has_ptpvid = line:match(": (%w+)") end
61 elseif line:match(": enable_vlan4k") then
64 elseif line:match(": max_length") then
73 -- The PVID options (if any) are added to this table so that
74 -- section create below can add the just created vlan to the
75 -- choice list of the PVID options...
78 -- This function re-reads all existing vlan ids and populates
79 -- PVID options choice lists
80 local function populate_pvids()
82 m.uci:foreach("network", "switch_vlan",
84 local vid = s[has_vlan4k or "vlan"] or s["vlan"]
86 vlan_ids[#vlan_ids+1] = vid
91 for _, opt in ipairs(pvid_opts) do
93 opt:value("", translate("none"))
94 for _, vid in luci.util.vspairs(vlan_ids) do
95 opt:value(vid, translatef("VLAN %d", tonumber(vid)))
101 s = m:section(NamedSection, x['.name'], "switch", translatef("Switch %q", switch_name))
104 s:option(Flag, "enable", translate("Enable this switch")).default = "1"
105 s:option(Flag, "enable_vlan", translate("Enable VLAN functionality")).default = "1"
107 if enable_vlan4k then
108 s:option(Flag, "enable_vlan4k", translate("Enable 4K VLANs"))
112 j = s:option(Flag, "max_length", translate("Enable Jumbo Frame passthrough"))
117 s:option(Flag, "reset", translate("Reset switch during setup")).default = "1"
120 s = m:section(TypedSection, "switch_vlan", translatef("VLANs on %q", switch_name))
121 s.template = "cbi/tblsection"
125 -- Override cfgsections callback to enforce row ordering by vlan id.
126 s.cfgsections = function(self)
127 local osections = TypedSection.cfgsections(self)
131 for _, section in luci.util.spairs(
134 return (tonumber(m.uci:get("network", osections[a], has_vlan4k or "vlan")) or 9999)
135 < (tonumber(m.uci:get("network", osections[b], has_vlan4k or "vlan")) or 9999)
138 sections[#sections+1] = section
144 -- When creating a new vlan, preset it with the highest found vid + 1.
145 -- Repopulate the PVID choice lists afterwards.
146 s.create = function(self, section)
147 local sid = TypedSection.create(self, section)
152 m.uci:foreach("network", "switch_vlan",
154 local nr = tonumber(s.vlan)
155 local id = has_vlan4k and tonumber(s[has_vlan4k])
156 if nr ~= nil and nr > max_nr then max_nr = nr end
157 if id ~= nil and id > max_id then max_id = id end
160 m.uci:set("network", sid, "vlan", max_nr + 1)
163 m.uci:set("network", sid, has_vlan4k, max_id + 1)
166 -- add newly created vlan to the pvid choice list
172 -- Repopulate PVId choice lists if a vlan gets removed.
173 s.remove = function(self, section)
174 local rv = TypedSection.remove(self, section)
176 -- repopulate pvid choices
183 local port_opts = { }
186 -- Parse current tagging state from the "ports" option.
187 local portvalue = function(self, section)
189 for pt in (m.uci:get("network", section, "ports") or ""):gmatch("%w+") do
190 local pc, tu = pt:match("^(%d+)([tu]*)")
191 if pc == self.option then return (#tu > 0) and tu or "u" end
196 -- Validate port tagging. Ensure that a port is only untagged once,
198 local portvalidate = function(self, value, section)
199 -- ensure that the ports appears untagged only once
201 if not untagged[self.option] then
202 untagged[self.option] = true
205 translatef("Port %d is untagged in multiple VLANs!", tonumber(self.option) + 1)
212 local vid = s:option(Value, has_vlan4k or "vlan", "VLAN ID")
215 vid.forcewrite = true
217 -- Validate user provided VLAN ID, make sure its within the bounds
218 -- allowed by the switch.
219 vid.validate = function(self, value, section)
220 local v = tonumber(value)
221 local m = has_vlan4k and 4094 or (num_vlans - 1)
222 if v ~= nil and v > 0 and v <= m then
226 translatef("Invalid VLAN ID given! Only IDs between %d and %d are allowed.", 1, m)
230 -- When writing the "vid" or "vlan" option, serialize the port states
231 -- as well and write them as "ports" option to uci.
232 vid.write = function(self, section, value)
236 for _, o in ipairs(port_opts) do
237 local v = o:formvalue(section)
239 p[#p+1] = o.option .. v
245 m.uci:set("network", section, "ports", table.concat(p, " "))
246 return Value.write(self, section, value)
249 -- Fallback to "vlan" option if "vid" option is supported but unset.
250 vid.cfgvalue = function(self, section)
251 return m.uci:get("network", section, has_vlan4k or "vlan")
252 or m.uci:get("network", section, "vlan")
255 -- Build per-port off/untagged/tagged choice lists.
257 for pt = 0, num_ports - 1 do
258 local po = s:option(ListValue, tostring(pt),
259 (pt == cpu_port) and translate("CPU") or translatef("Port %d", (pt + 1)))
261 po:value("", translate("off"))
262 po:value("u" % pt, translate("untagged"))
263 po:value("t" % pt, translate("tagged"))
265 po.cfgvalue = portvalue
266 po.validate = portvalidate
267 po.write = function() end
269 port_opts[#port_opts+1] = po
273 -- Does this switch support PVIDs?
276 -- Spawn a "virtual" section. We just attach it to the global
277 -- switch section here, the overrides below take care of writing
278 -- the actual values to the correct uci sections.
279 s = m:section(TypedSection, "switch",
280 translatef("Port PVIDs on %q", switch_name),
281 translate("Port <abbr title=\"Primary VLAN IDs\">PVIDs</abbr> specify " ..
282 "the default VLAN ID added to received untagged frames."))
284 s.template = "cbi/tblsection"
288 -- Build port list, store pointers to the option objects in the
289 -- pvid_opts array so that other callbacks can repopulate their
292 for pt = 0, num_ports - 1 do
293 local po = s:option(ListValue, tostring(pt),
294 (pt == cpu_port) and translate("CPU") or translatef("Port %d", (pt + 1)))
296 -- When cbi queries the current config value for this post,
297 -- lookup the associated switch_port section (if any) and
298 -- return its "pvid" or "vlan" option value.
299 po.cfgvalue = function(self, section)
301 m.uci:foreach("network", "switch_port",
303 if s.port == self.option then
311 -- On write, find the actual switch_port section associated
312 -- to this port and set the value there. Create a new
313 -- switch_port section for this port if there is none yet.
314 po.write = function(self, section, value)
317 m.uci:foreach("network", "switch_port",
319 if s.port == self.option then
320 m.uci:set("network", s['.name'], has_ptpvid, value)
327 m.uci:section("network", "switch_port", nil, {
328 ["port"] = self.option,
334 -- If the user cleared the PVID value on this port, find
335 -- the associated switch_port section and clear it.
336 -- If the section does not contain any other unrelated
337 -- options (like led or blinkrate) then remove it completely,
338 -- else just clear out the "pvid" option.
339 po.remove = function(self, section)
340 m.uci:foreach("network", "switch_port",
342 if s.port == self.option then
346 for k, _ in pairs(s) do
347 if k:sub(1,1) ~= "." and k ~= "port" and k ~= has_ptpvid then
354 m.uci:delete("network", s['.name'])
356 m.uci:delete("network", s['.name'], has_ptpvid)
364 -- The referenced VLAN might just have been removed, simply
365 -- return "" (none) in this case to avoid triggering a
367 po.validate = function(...)
368 return ListValue.validate(...) or ""
371 pvid_opts[#pvid_opts+1] = po