modules/admin-full: fix switch config page for models with multiple switches
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_network / vlan.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2010-2011 Jo-Philipp Wich <xm@subsignal.org>
6
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
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
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."))
17
18 m.uci:foreach("network", "switch",
19 function(x)
20 local sid = x['.name']
21 local switch_name = x.name or sid
22 local has_vlan = nil
23 local has_learn = nil
24 local has_vlan4k = nil
25 local has_ptpvid = nil
26 local has_jumbo3 = nil
27 local min_vid = 0
28 local max_vid = 16
29 local num_vlans = 16
30 local num_ports = 6
31 local cpu_port = 5
32
33 local enable_vlan4k = false
34
35 -- Parse some common switch properties from swconfig help output.
36 local swc = io.popen("swconfig dev %q help 2>/dev/null" % switch_name)
37 if swc then
38
39 local is_port_attr = false
40 local is_vlan_attr = false
41
42 while true do
43 local line = swc:read("*l")
44 if not line then break end
45
46 if line:match("^%s+%-%-vlan") then
47 is_vlan_attr = true
48
49 elseif line:match("^%s+%-%-port") then
50 is_vlan_attr = false
51 is_port_attr = true
52
53 elseif line:match("^Switch %d+:") then
54 num_ports, cpu_port, num_vlans =
55 line:match("ports: (%d+) %(cpu @ (%d+)%), vlans: (%d+)")
56
57 num_ports = tonumber(num_ports) or 6
58 num_vlans = tonumber(num_vlans) or 16
59 cpu_port = tonumber(cpu_port) or 5
60 min_vid = 1
61
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
65
66 elseif line:match(": enable_vlan4k") then
67 enable_vlan4k = true
68
69 elseif line:match(": enable_vlan") then
70 has_vlan = "enable_vlan"
71
72 elseif line:match(": enable_learning") then
73 has_learn = "enable_learning"
74
75 elseif line:match(": max_length") then
76 has_jumbo3 = "max_length"
77 end
78 end
79
80 swc:close()
81 end
82
83
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...
87 local pvid_opts = { }
88
89 -- This function re-reads all existing vlan ids and populates
90 -- PVID options choice lists
91 local function populate_pvids()
92 local vlan_ids = { }
93 m.uci:foreach("network", "switch_vlan",
94 function(s)
95 if s.device == switch_name then
96 local vid = s[has_vlan4k or "vlan"] or s["vlan"]
97 if vid ~= nil then
98 vlan_ids[#vlan_ids+1] = vid
99 end
100 end
101 end)
102
103 local opt, vid
104 for _, opt in ipairs(pvid_opts) do
105 opt:reset_values()
106 opt:value("", translate("none"))
107 for _, vid in luci.util.vspairs(vlan_ids) do
108 opt:value(vid, translatef("VLAN %d", tonumber(vid)))
109 end
110 end
111 end
112
113 -- Switch properties
114 s = m:section(NamedSection, x['.name'], "switch", translatef("Switch %q", switch_name))
115 s.addremove = false
116
117 if has_vlan then
118 s:option(Flag, has_vlan, translate("Enable VLAN functionality"))
119 end
120
121 if enable_vlan4k then
122 s:option(Flag, "enable_vlan4k", translate("Enable 4K VLANs"))
123 end
124
125 if has_learn then
126 x = s:option(Flag, has_learn, translate("Enable learning and aging"))
127 x.default = x.enabled
128 end
129
130 if has_jumbo3 then
131 x = s:option(Flag, has_jumbo3, translate("Enable Jumbo Frame passthrough"))
132 x.enabled = "3"
133 x.rmempty = true
134 end
135
136
137 -- VLAN table
138 s = m:section(TypedSection, "switch_vlan", translatef("VLANs on %q", switch_name))
139 s.template = "cbi/tblsection"
140 s.addremove = true
141 s.anonymous = true
142
143 -- Filter by switch
144 s.filter = function(self, section)
145 local device = m:get(section, "device")
146 return (device and device == switch_name)
147 end
148
149 -- Override cfgsections callback to enforce row ordering by vlan id.
150 s.cfgsections = function(self)
151 local osections = TypedSection.cfgsections(self)
152 local sections = { }
153 local section
154
155 for _, section in luci.util.spairs(
156 osections,
157 function(a, b)
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)
160 end
161 ) do
162 sections[#sections+1] = section
163 end
164
165 return sections
166 end
167
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)
171 -- Filter by switch
172 if m:get(origin, "device") ~= switch_name then
173 return
174 end
175
176 local sid = TypedSection.create(self, section)
177
178 local max_nr = 0
179 local max_id = 0
180
181 m.uci:foreach("network", "switch_vlan",
182 function(s)
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
188 end
189 end)
190
191 m.uci:set("network", sid, "device", switch_name)
192 m.uci:set("network", sid, "vlan", max_nr + 1)
193
194 if has_vlan4k then
195 m.uci:set("network", sid, has_vlan4k, max_id + 1)
196 end
197
198 -- add newly created vlan to the pvid choice list
199 populate_pvids()
200
201 return sid
202 end
203
204 -- Repopulate PVId choice lists if a vlan gets removed.
205 s.remove = function(self, section)
206 local rv = TypedSection.remove(self, section)
207
208 -- repopulate pvid choices
209 populate_pvids()
210
211 return rv
212 end
213
214
215 local port_opts = { }
216 local untagged = { }
217
218 -- Parse current tagging state from the "ports" option.
219 local portvalue = function(self, section)
220 local pt
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
224 end
225 return ""
226 end
227
228 -- Validate port tagging. Ensure that a port is only untagged once,
229 -- bail out if not.
230 local portvalidate = function(self, value, section)
231 -- ensure that the ports appears untagged only once
232 if value == "u" then
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
236 return nil,
237 translatef("Port %d is untagged in multiple VLANs!", tonumber(self.option) + 1)
238 end
239 end
240 return value
241 end
242
243
244 local vid = s:option(Value, has_vlan4k or "vlan", "VLAN ID")
245
246 vid.rmempty = false
247 vid.forcewrite = true
248
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
255 return value
256 else
257 return nil,
258 translatef("Invalid VLAN ID given! Only IDs between %d and %d are allowed.", min_vid, m)
259 end
260 end
261
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)
265 local o
266 local p = { }
267
268 for _, o in ipairs(port_opts) do
269 local v = o:formvalue(section)
270 if v == "t" then
271 p[#p+1] = o.option .. v
272 elseif v == "u" then
273 p[#p+1] = o.option
274 end
275 end
276
277 m.uci:set("network", section, "ports", table.concat(p, " "))
278 return Value.write(self, section, value)
279 end
280
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")
285 end
286
287 -- Build per-port off/untagged/tagged choice lists.
288 local pt
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)))
292
293 po:value("", translate("off"))
294 po:value("u" % pt, translate("untagged"))
295 po:value("t" % pt, translate("tagged"))
296
297 po.cfgvalue = portvalue
298 po.validate = portvalidate
299 po.write = function() end
300
301 port_opts[#port_opts+1] = po
302 end
303
304
305 -- Does this switch support PVIDs?
306 if has_ptpvid then
307
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."))
315
316 s.template = "cbi/tblsection"
317 s.addremove = false
318 s.anonymous = true
319
320 -- Filter by switch
321 function s.filter(self, section)
322 return (m:get(section, "name") == switch_name)
323 end
324
325 -- Build port list, store pointers to the option objects in the
326 -- pvid_opts array so that other callbacks can repopulate their
327 -- choice lists.
328 local pt
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)))
332
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)
337 local val
338 m.uci:foreach("network", "switch_port",
339 function(s)
340 if s.port == self.option then
341 val = s[has_ptpvid]
342 return false
343 end
344 end)
345 return val
346 end
347
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)
352 local found = false
353
354 m.uci:foreach("network", "switch_port",
355 function(s)
356 if s.port == self.option then
357 m.uci:set("network", s['.name'], has_ptpvid, value)
358 found = true
359 return false
360 end
361 end)
362
363 if not found then
364 m.uci:section("network", "switch_port", nil, {
365 ["port"] = self.option,
366 [has_ptpvid] = value
367 })
368 end
369 end
370
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",
378 function(s)
379 if s.port == self.option then
380 local k, found
381 local empty = true
382
383 for k, _ in pairs(s) do
384 if k:sub(1,1) ~= "." and k ~= "port" and k ~= has_ptpvid then
385 empty = false
386 break
387 end
388 end
389
390 if empty then
391 m.uci:delete("network", s['.name'])
392 else
393 m.uci:delete("network", s['.name'], has_ptpvid)
394 end
395
396 return false
397 end
398 end)
399 end
400
401 -- The referenced VLAN might just have been removed, simply
402 -- return "" (none) in this case to avoid triggering a
403 -- validation error.
404 po.validate = function(...)
405 return ListValue.validate(...) or ""
406 end
407
408 pvid_opts[#pvid_opts+1] = po
409 end
410
411 populate_pvids()
412 end
413 end
414 )
415
416 return m