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
15 m = Map("network", translate("Switch"), translate("The network ports on this device 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."))
17 m.uci:foreach("network", "switch",
19 local sid = x['.name']
20 local switch_name = x.name or sid
23 local has_vlan4k = nil
24 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("cpu @") then
53 switch_title = line:match("^switch%d: %w+%((.-)%)")
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
65 elseif line:match(": enable_vlan4k") then
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(": max_length") then
75 has_jumbo3 = "max_length"
84 s = m:section(NamedSection, x['.name'], "switch",
85 switch_title and translatef("Switch %q (%s)", switch_name, switch_title)
86 or translatef("Switch %q", switch_name))
91 s:option(Flag, has_vlan, translate("Enable VLAN functionality"))
95 x = s:option(Flag, has_learn, translate("Enable learning and aging"))
100 x = s:option(Flag, has_jumbo3, translate("Enable Jumbo Frame passthrough"))
107 s = m:section(TypedSection, "switch_vlan",
108 switch_title and translatef("VLANs on %q (%s)", switch_name, switch_title)
109 or translatef("VLANs on %q", switch_name))
111 s.template = "cbi/tblsection"
116 s.filter = function(self, section)
117 local device = m:get(section, "device")
118 return (device and device == switch_name)
121 -- Override cfgsections callback to enforce row ordering by vlan id.
122 s.cfgsections = function(self)
123 local osections = TypedSection.cfgsections(self)
127 for _, section in luci.util.spairs(
130 return (tonumber(m:get(osections[a], has_vlan4k or "vlan")) or 9999)
131 < (tonumber(m:get(osections[b], has_vlan4k or "vlan")) or 9999)
134 sections[#sections+1] = section
140 -- When creating a new vlan, preset it with the highest found vid + 1.
141 s.create = function(self, section, origin)
143 if m:get(origin, "device") ~= switch_name then
147 local sid = TypedSection.create(self, section)
152 m.uci:foreach("network", "switch_vlan",
154 if s.device == switch_name then
155 local nr = tonumber(s.vlan)
156 local id = has_vlan4k and tonumber(s[has_vlan4k])
157 if nr ~= nil and nr > max_nr then max_nr = nr end
158 if id ~= nil and id > max_id then max_id = id end
162 m:set(sid, "device", switch_name)
163 m:set(sid, "vlan", max_nr + 1)
166 m:set(sid, has_vlan4k, max_id + 1)
173 local port_opts = { }
176 -- Parse current tagging state from the "ports" option.
177 local portvalue = function(self, section)
179 for pt in (m:get(section, "ports") or ""):gmatch("%w+") do
180 local pc, tu = pt:match("^(%d+)([tu]*)")
181 if pc == self.option then return (#tu > 0) and tu or "u" end
186 -- Validate port tagging. Ensure that a port is only untagged once,
188 local portvalidate = function(self, value, section)
189 -- ensure that the ports appears untagged only once
191 if not untagged[self.option] then
192 untagged[self.option] = true
193 elseif min_vid > 0 or tonumber(self.option) ~= cpu_port then -- enable multiple untagged cpu ports due to weird broadcom default setup
195 translatef("Port %d is untagged in multiple VLANs!", tonumber(self.option) + 1)
202 local vid = s:option(Value, has_vlan4k or "vlan", "VLAN ID")
205 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 >= min_vid and v <= m then
214 if not self.vlan_used[v] then
215 self.vlan_used[v] = true
219 translatef("Invalid VLAN ID given! Only unique IDs are allowed")
223 translatef("Invalid VLAN ID given! Only IDs between %d and %d are allowed.", min_vid, m)
227 -- When writing the "vid" or "vlan" option, serialize the port states
228 -- as well and write them as "ports" option to uci.
229 vid.write = function(self, section, value)
233 for _, o in ipairs(port_opts) do
234 local v = o:formvalue(section)
236 p[#p+1] = o.option .. v
242 if enable_vlan4k then
243 m:set(sid, "enable_vlan4k", "1")
246 m:set(section, "ports", table.concat(p, " "))
247 return Value.write(self, section, value)
250 -- Fallback to "vlan" option if "vid" option is supported but unset.
251 vid.cfgvalue = function(self, section)
252 return m:get(section, has_vlan4k or "vlan")
253 or m:get(section, "vlan")
256 -- Build per-port off/untagged/tagged choice lists.
258 for pt = 0, num_ports - 1 do
260 if pt == cpu_port then
261 title = translate("CPU")
263 title = translatef("Port %d", pt)
266 local po = s:option(ListValue, tostring(pt), title)
268 po:value("", translate("off"))
269 po:value("u", translate("untagged"))
270 po:value("t", translate("tagged"))
272 po.cfgvalue = portvalue
273 po.validate = portvalidate
274 po.write = function() end
276 port_opts[#port_opts+1] = po
280 -- Switch status template
281 s = m:section(SimpleSection)
282 s.template = "admin_network/switch_status"
283 s.switch = switch_name