1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2010-2011 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
5 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."))
7 local fs = require "nixio.fs"
10 m.uci:foreach("network", "switch",
12 local sid = x['.name']
13 local switch_name = x.name or sid
16 local has_vlan4k = nil
17 local has_jumbo3 = nil
18 local has_mirror = nil
22 local cpu_port = tonumber(fs.readfile("/proc/switch/eth0/cpuport") or 5)
23 local num_ports = cpu_port + 1
26 local enable_vlan4k = false
28 -- Parse some common switch properties from swconfig help output.
29 local swc = io.popen("swconfig dev %q help 2>/dev/null" % switch_name)
32 local is_port_attr = false
33 local is_vlan_attr = false
36 local line = swc:read("*l")
37 if not line then break end
39 if line:match("^%s+%-%-vlan") then
42 elseif line:match("^%s+%-%-port") then
46 elseif line:match("cpu @") then
47 switch_title = line:match("^switch%d: %w+%((.-)%)")
48 num_ports, cpu_port, num_vlans =
49 line:match("ports: (%d+) %(cpu @ (%d+)%), vlans: (%d+)")
51 num_ports = tonumber(num_ports) or 6
52 num_vlans = tonumber(num_vlans) or 16
53 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
59 elseif line:match(": enable_vlan4k") then
62 elseif line:match(": enable_vlan") then
63 has_vlan = "enable_vlan"
65 elseif line:match(": enable_learning") then
66 has_learn = "enable_learning"
68 elseif line:match(": enable_mirror_rx") then
69 has_mirror = "enable_mirror_rx"
71 elseif line:match(": max_length") then
72 has_jumbo3 = "max_length"
81 s = m:section(NamedSection, x['.name'], "switch",
82 switch_title and translatef("Switch %q (%s)", switch_name, switch_title)
83 or translatef("Switch %q", switch_name))
88 s:option(Flag, has_vlan, translate("Enable VLAN functionality"))
92 x = s:option(Flag, has_learn, translate("Enable learning and aging"))
97 x = s:option(Flag, has_jumbo3, translate("Enable Jumbo Frame passthrough"))
102 -- Does this switch support port mirroring?
104 s:option(Flag, "enable_mirror_rx", translate("Enable mirroring of incoming packets"))
105 s:option(Flag, "enable_mirror_tx", translate("Enable mirroring of outgoing packets"))
107 local sp = s:option(ListValue, "mirror_source_port", translate("Mirror source port"))
108 local mp = s:option(ListValue, "mirror_monitor_port", translate("Mirror monitor port"))
110 sp:depends("enable_mirror_tx", "1")
111 sp:depends("enable_mirror_rx", "1")
113 mp:depends("enable_mirror_tx", "1")
114 mp:depends("enable_mirror_rx", "1")
117 for pt = 0, num_ports - 1 do
120 name = (pt == cpu_port) and translate("CPU") or translatef("Port %d", pt)
128 s = m:section(TypedSection, "switch_vlan",
129 switch_title and translatef("VLANs on %q (%s)", switch_name, switch_title)
130 or translatef("VLANs on %q", switch_name))
132 s.template = "cbi/tblsection"
137 s.filter = function(self, section)
138 local device = m:get(section, "device")
139 return (device and device == switch_name)
142 -- Override cfgsections callback to enforce row ordering by vlan id.
143 s.cfgsections = function(self)
144 local osections = TypedSection.cfgsections(self)
148 for _, section in luci.util.spairs(
151 return (tonumber(m:get(osections[a], has_vlan4k or "vlan")) or 9999)
152 < (tonumber(m:get(osections[b], has_vlan4k or "vlan")) or 9999)
155 sections[#sections+1] = section
161 -- When creating a new vlan, preset it with the highest found vid + 1.
162 s.create = function(self, section, origin)
164 if m:get(origin, "device") ~= switch_name then
168 local sid = TypedSection.create(self, section)
173 m.uci:foreach("network", "switch_vlan",
175 if s.device == switch_name then
176 local nr = tonumber(s.vlan)
177 local id = has_vlan4k and tonumber(s[has_vlan4k])
178 if nr ~= nil and nr > max_nr then max_nr = nr end
179 if id ~= nil and id > max_id then max_id = id end
183 m:set(sid, "device", switch_name)
184 m:set(sid, "vlan", max_nr + 1)
187 m:set(sid, has_vlan4k, max_id + 1)
194 local port_opts = { }
197 -- Parse current tagging state from the "ports" option.
198 local portvalue = function(self, section)
200 for pt in (m:get(section, "ports") or ""):gmatch("%w+") do
201 local pc, tu = pt:match("^(%d+)([tu]*)")
202 if pc == self.option then return (#tu > 0) and tu or "u" end
207 -- Validate port tagging. Ensure that a port is only untagged once,
209 local portvalidate = function(self, value, section)
210 -- ensure that the ports appears untagged only once
212 if not untagged[self.option] then
213 untagged[self.option] = true
214 elseif min_vid > 0 or tonumber(self.option) ~= cpu_port then -- enable multiple untagged cpu ports due to weird broadcom default setup
216 translatef("Port %d is untagged in multiple VLANs!", tonumber(self.option) + 1)
223 local vid = s:option(Value, has_vlan4k or "vlan", "VLAN ID", "<div id='portstatus-%s'></div>" % switch_name)
224 local mx_vid = has_vlan4k and 4094 or (num_vlans - 1)
227 vid.forcewrite = true
229 vid.datatype = "and(uinteger,range("..min_vid..","..mx_vid.."))"
231 -- Validate user provided VLAN ID, make sure its within the bounds
232 -- allowed by the switch.
233 vid.validate = function(self, value, section)
234 local v = tonumber(value)
235 local m = has_vlan4k and 4094 or (num_vlans - 1)
236 if v ~= nil and v >= min_vid and v <= m then
237 if not self.vlan_used[v] then
238 self.vlan_used[v] = true
242 translatef("Invalid VLAN ID given! Only unique IDs are allowed")
246 translatef("Invalid VLAN ID given! Only IDs between %d and %d are allowed.", min_vid, m)
250 -- When writing the "vid" or "vlan" option, serialize the port states
251 -- as well and write them as "ports" option to uci.
252 vid.write = function(self, section, value)
256 for _, o in ipairs(port_opts) do
257 local v = o:formvalue(section)
259 p[#p+1] = o.option .. v
265 if enable_vlan4k then
266 m:set(sid, "enable_vlan4k", "1")
269 m:set(section, "ports", table.concat(p, " "))
270 return Value.write(self, section, value)
273 -- Fallback to "vlan" option if "vid" option is supported but unset.
274 vid.cfgvalue = function(self, section)
275 return m:get(section, has_vlan4k or "vlan")
276 or m:get(section, "vlan")
279 -- Build per-port off/untagged/tagged choice lists.
281 for pt = 0, num_ports - 1 do
283 if pt == cpu_port then
284 title = translate("CPU")
286 title = translatef("Port %d", pt)
289 local po = s:option(ListValue, tostring(pt), title)
291 po:value("", translate("off"))
292 po:value("u", translate("untagged"))
293 po:value("t", translate("tagged"))
295 po.cfgvalue = portvalue
296 po.validate = portvalidate
297 po.write = function() end
299 port_opts[#port_opts+1] = po
302 switches[#switches+1] = switch_name
306 -- Switch status template
307 s = m:section(SimpleSection)
308 s.template = "admin_network/switch_status"
309 s.switches = switches