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 local enable_vlan4k = false
30 -- Parse some common switch properties from swconfig help output.
31 local swc = io.popen("swconfig dev %q help 2>/dev/null" % switch_name)
34 local is_port_attr = false
35 local is_vlan_attr = false
38 local line = swc:read("*l")
39 if not line then break end
41 if line:match("^%s+%-%-vlan") then
44 elseif line:match("^%s+%-%-port") then
48 elseif line:match("^Switch %d+:") then
49 num_ports, cpu_port, num_vlans =
50 line:match("ports: (%d+) %(cpu @ (%d+)%), vlans: (%d+)")
52 num_ports = tonumber(num_ports or 5)
53 num_vlans = tonumber(num_vlans or 16)
54 cpu_port = tonumber(cpu_port or 5)
56 elseif line:match(": pvid") or line:match(": tag") or line:match(": vid") then
57 if is_vlan_attr then has_vlan4k = line:match(": (%w+)") end
58 if is_port_attr then has_ptpvid = line:match(": (%w+)") end
60 elseif line:match(": enable_vlan4k") then
70 -- The PVID options (if any) are added to this table so that
71 -- section create below can add the just created vlan to the
72 -- choice list of the PVID options...
75 -- This function re-reads all existing vlan ids and populates
76 -- PVID options choice lists
77 local function populate_pvids()
79 m.uci:foreach("network", "switch_vlan",
81 local vid = s[has_vlan4k or "vlan"] or s["vlan"]
83 vlan_ids[#vlan_ids+1] = vid
88 for _, opt in ipairs(pvid_opts) do
90 opt:value("", translate("none"))
91 for _, vid in luci.util.vspairs(vlan_ids) do
92 opt:value(vid, translatef("VLAN %d", tonumber(vid)))
98 s = m:section(NamedSection, x['.name'], "switch", translatef("Switch %q", switch_name))
101 s:option(Flag, "enable", translate("Enable this switch")).default = "1"
102 s:option(Flag, "enable_vlan", translate("Enable VLAN functionality")).default = "1"
104 if enable_vlan4k then
105 s:option(Flag, "enable_vlan4k", translate("Enable 4K VLANs"))
108 s:option(Flag, "reset", translate("Reset switch during setup")).default = "1"
111 s = m:section(TypedSection, "switch_vlan", translatef("VLANs on %q", switch_name))
112 s.template = "cbi/tblsection"
116 -- Override cfgsections callback to enforce row ordering by vlan id.
117 s.cfgsections = function(self)
118 local osections = TypedSection.cfgsections(self)
122 for _, section in luci.util.spairs(
125 return (tonumber(m.uci:get("network", osections[a], has_vlan4k or "vlan")) or 9999)
126 < (tonumber(m.uci:get("network", osections[b], has_vlan4k or "vlan")) or 9999)
129 sections[#sections+1] = section
135 -- When creating a new vlan, preset it with the highest found vid + 1.
136 -- Repopulate the PVID choice lists afterwards.
137 s.create = function(self, section)
138 local sid = TypedSection.create(self, section)
143 m.uci:foreach("network", "switch_vlan",
145 local nr = tonumber(s.vlan)
146 local id = has_vlan4k and tonumber(s[has_vlan4k])
147 if nr ~= nil and nr > max_nr then max_nr = nr end
148 if id ~= nil and id > max_id then max_id = id end
151 m.uci:set("network", sid, "vlan", max_nr + 1)
154 m.uci:set("network", sid, has_vlan4k, max_id + 1)
157 -- add newly created vlan to the pvid choice list
163 -- Repopulate PVId choice lists if a vlan gets removed.
164 s.remove = function(self, section)
165 local rv = TypedSection.remove(self, section)
167 -- repopulate pvid choices
174 local port_opts = { }
177 -- Parse current tagging state from the "ports" option.
178 local portvalue = function(self, section)
180 for pt in (m.uci:get("network", section, "ports") or ""):gmatch("%w+") do
181 local pc, tu = pt:match("^(%d+)([tu]*)")
182 if pc == self.option then return (#tu > 0) and tu or "u" end
187 -- Validate port tagging. Ensure that a port is only untagged once,
189 local portvalidate = function(self, value, section)
190 -- ensure that the ports appears untagged only once
192 if not untagged[self.option] then
193 untagged[self.option] = true
196 translatef("Port %d is untagged in multiple VLANs!", tonumber(self.option) + 1)
203 local vid = s:option(Value, has_vlan4k or "vlan", "VLAN ID")
206 vid.forcewrite = true
208 -- Validate user provided VLAN ID, make sure its within the bounds
209 -- allowed by the switch.
210 vid.validate = function(self, value, section)
211 local v = tonumber(value)
212 local m = has_vlan4k and 4094 or (num_vlans - 1)
213 if v ~= nil and v > 0 and v <= m then
217 translatef("Invalid VLAN ID given! Only IDs between %d and %d are allowed.", 1, m)
221 -- When writing the "vid" or "vlan" option, serialize the port states
222 -- as well and write them as "ports" option to uci.
223 vid.write = function(self, section, value)
227 for _, o in ipairs(port_opts) do
228 local v = o:formvalue(section)
230 p[#p+1] = o.option .. v
236 m.uci:set("network", section, "ports", table.concat(p, " "))
237 return Value.write(self, section, value)
240 -- Fallback to "vlan" option if "vid" option is supported but unset.
241 vid.cfgvalue = function(self, section)
242 return m.uci:get("network", section, has_vlan4k or "vlan")
243 or m.uci:get("network", section, "vlan")
246 -- Build per-port off/untagged/tagged choice lists.
248 for pt = 0, num_ports - 1 do
249 local po = s:option(ListValue, tostring(pt),
250 (pt == cpu_port) and translate("CPU") or translatef("Port %d", (pt + 1)))
252 po:value("", translate("off"))
253 po:value("u" % pt, translate("untagged"))
254 po:value("t" % pt, translate("tagged"))
256 po.cfgvalue = portvalue
257 po.validate = portvalidate
258 po.write = function() end
260 port_opts[#port_opts+1] = po
264 -- Does this switch support PVIDs?
267 -- Spawn a "virtual" section. We just attach it to the global
268 -- switch section here, the overrides below take care of writing
269 -- the actual values to the correct uci sections.
270 s = m:section(TypedSection, "switch",
271 translatef("Port PVIDs on %q", switch_name),
272 translate("Port <abbr title=\"Primary VLAN IDs\">PVIDs</abbr> specify " ..
273 "the default VLAN ID added to received untagged frames."))
275 s.template = "cbi/tblsection"
279 -- Build port list, store pointers to the option objects in the
280 -- pvid_opts array so that other callbacks can repopulate their
283 for pt = 0, num_ports - 1 do
284 local po = s:option(ListValue, tostring(pt),
285 (pt == cpu_port) and translate("CPU") or translatef("Port %d", (pt + 1)))
287 -- When cbi queries the current config value for this post,
288 -- lookup the associated switch_port section (if any) and
289 -- return its "pvid" or "vlan" option value.
290 po.cfgvalue = function(self, section)
292 m.uci:foreach("network", "switch_port",
294 if s.port == self.option then
302 -- On write, find the actual switch_port section associated
303 -- to this port and set the value there. Create a new
304 -- switch_port section for this port if there is none yet.
305 po.write = function(self, section, value)
308 m.uci:foreach("network", "switch_port",
310 if s.port == self.option then
311 m.uci:set("network", s['.name'], has_ptpvid, value)
318 m.uci:section("network", "switch_port", nil, {
319 ["port"] = self.option,
325 -- If the user cleared the PVID value on this port, find
326 -- the associated switch_port section and clear it.
327 -- If the section does not contain any other unrelated
328 -- options (like led or blinkrate) then remove it completely,
329 -- else just clear out the "pvid" option.
330 po.remove = function(self, section)
331 m.uci:foreach("network", "switch_port",
333 if s.port == self.option then
337 for k, _ in pairs(s) do
338 if k:sub(1,1) ~= "." and k ~= "port" and k ~= has_ptpvid then
345 m.uci:delete("network", s['.name'])
347 m.uci:delete("network", s['.name'], has_ptpvid)
355 -- The referenced VLAN might just have been removed, simply
356 -- return "" (none) in this case to avoid triggering a
358 po.validate = function(...)
359 return ListValue.validate(...) or ""
362 pvid_opts[#pvid_opts+1] = po