modules/admin-full: throw more work-arounds, exceptions, hacks and detections at...
[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 switch_name = x.name or x['.name']
21                 local has_enable  = "enable"
22                 local has_reset   = nil
23                 local has_vlan    = nil
24                 local has_learn   = nil 
25                 local has_vlan4k  = nil
26                 local has_ptpvid  = 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                                         has_enable = nil
62
63                                 elseif line:match(": pvid") or line:match(": tag") or line:match(": vid") then
64                                         if is_vlan_attr then has_vlan4k = line:match(": (%w+)") end
65                                         if is_port_attr then has_ptpvid = line:match(": (%w+)") end
66
67                                 elseif line:match(": enable_vlan") then
68                                         has_vlan = "enable_vlan"
69
70                                 elseif line:match(": enable_learning") then
71                                         has_learn = "enable_learning"
72
73                                 elseif line:match(": reset") then
74                                         has_reset = "reset"
75
76                                 elseif line:match(": enable_vlan4k") then
77                                         enable_vlan4k = true
78
79                                 end
80                         end
81
82                         swc:close()
83                 end
84
85
86                 -- The PVID options (if any) are added to this table so that
87                 -- section create below can add the just created vlan to the
88                 -- choice list of the PVID options...
89                 local pvid_opts = { }
90
91                 -- This function re-reads all existing vlan ids and populates
92                 -- PVID options choice lists
93                 local function populate_pvids()
94                         local vlan_ids = { }
95                         m.uci:foreach("network", "switch_vlan",
96                                 function(s)
97                                         local vid = s[has_vlan4k or "vlan"] or s["vlan"]
98                                         if vid ~= nil then
99                                                 vlan_ids[#vlan_ids+1] = vid
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_enable then
118                         x = s:option(Flag, has_enable, translate("Enable this switch"))
119                         x.default = x.enabled
120                 end
121
122                 if has_vlan then
123                         x = s:option(Flag, has_vlan, translate("Enable VLAN functionality"))
124                         x.default = x.enabled
125                 end
126
127                 if enable_vlan4k then
128                         s:option(Flag, "enable_vlan4k", translate("Enable 4K VLANs"))
129                 end
130
131                 if has_learn then
132                         x = s:option(Flag, has_learn, translate("Enable learning and aging"))
133                         x.default = x.enabled
134                 end
135
136                 if has_reset then
137                         x = s:option(Flag, has_reset, translate("Reset switch during setup"))
138                         x.default = x.enabled
139                 end
140
141
142                 -- VLAN table
143                 s = m:section(TypedSection, "switch_vlan", translatef("VLANs on %q", switch_name))
144                 s.template = "cbi/tblsection"
145                 s.addremove = true
146                 s.anonymous = true
147
148                 -- Override cfgsections callback to enforce row ordering by vlan id.
149                 s.cfgsections = function(self)
150                         local osections = TypedSection.cfgsections(self)
151                         local sections = { }
152                         local section
153
154                         for _, section in luci.util.spairs(
155                                 osections,
156                                 function(a, b)
157                                         return (tonumber(m.uci:get("network", osections[a], has_vlan4k or "vlan")) or 9999)
158                                                 <  (tonumber(m.uci:get("network", osections[b], has_vlan4k or "vlan")) or 9999)
159                                 end
160                         ) do
161                                 sections[#sections+1] = section
162                         end
163
164                         return sections
165                 end
166
167                 -- When creating a new vlan, preset it with the highest found vid + 1.
168                 -- Repopulate the PVID choice lists afterwards.
169                 s.create = function(self, section)
170                         local sid = TypedSection.create(self, section)
171
172                         local max_nr = 0
173                         local max_id = 0
174
175                         m.uci:foreach("network", "switch_vlan",
176                                 function(s)
177                                         local nr = tonumber(s.vlan)
178                                         local id = has_vlan4k and tonumber(s[has_vlan4k])
179                                         if nr ~= nil and nr > max_nr then max_nr = nr end
180                                         if id ~= nil and id > max_id then max_id = id end
181                                 end)
182
183                         m.uci:set("network", sid, "vlan", max_nr + 1)
184
185                         if has_vlan4k then
186                                 m.uci:set("network", sid, has_vlan4k, max_id + 1)
187                         end
188
189                         -- add newly created vlan to the pvid choice list
190                         populate_pvids()
191
192                         return sid
193                 end
194
195                 -- Repopulate PVId choice lists if a vlan gets removed.
196                 s.remove = function(self, section)
197                         local rv = TypedSection.remove(self, section)
198
199                         -- repopulate pvid choices
200                         populate_pvids()
201
202                         return rv
203                 end
204
205
206                 local port_opts = { }
207                 local untagged  = { }
208
209                 -- Parse current tagging state from the "ports" option.
210                 local portvalue = function(self, section)
211                         local pt
212                         for pt in (m.uci:get("network", section, "ports") or ""):gmatch("%w+") do
213                                 local pc, tu = pt:match("^(%d+)([tu]*)")
214                                 if pc == self.option then return (#tu > 0) and tu or "u" end
215                         end
216                         return ""
217                 end
218
219                 -- Validate port tagging. Ensure that a port is only untagged once,
220                 -- bail out if not.
221                 local portvalidate = function(self, value, section)
222                         -- ensure that the ports appears untagged only once
223                         if value == "u" then
224                                 if not untagged[self.option] then
225                                         untagged[self.option] = true
226                                 elseif min_vid > 0 or tonumber(self.option) ~= cpu_port then -- enable multiple untagged cpu ports due to weird broadcom default setup
227                                         return nil,
228                                                 translatef("Port %d is untagged in multiple VLANs!", tonumber(self.option) + 1)
229                                 end
230                         end
231                         return value
232                 end
233
234
235                 local vid = s:option(Value, has_vlan4k or "vlan", "VLAN ID")
236
237                 vid.rmempty = false
238                 vid.forcewrite = true
239
240                 -- Validate user provided VLAN ID, make sure its within the bounds
241                 -- allowed by the switch.
242                 vid.validate = function(self, value, section)
243                         local v = tonumber(value)
244                         local m = has_vlan4k and 4094 or (num_vlans - 1)
245                         if v ~= nil and v >= min_vid and v <= m then
246                                 return value
247                         else
248                                 return nil,
249                                         translatef("Invalid VLAN ID given! Only IDs between %d and %d are allowed.", min_vid, m)
250                         end
251                 end
252
253                 -- When writing the "vid" or "vlan" option, serialize the port states
254                 -- as well and write them as "ports" option to uci.
255                 vid.write = function(self, section, value)
256                         local o
257                         local p = { }
258
259                         for _, o in ipairs(port_opts) do
260                                 local v = o:formvalue(section)
261                                 if v == "t" then
262                                         p[#p+1] = o.option .. v
263                                 elseif v == "u" then
264                                         p[#p+1] = o.option
265                                 end
266                         end
267
268                         m.uci:set("network", section, "ports", table.concat(p, " "))
269                         return Value.write(self, section, value)
270                 end
271
272                 -- Fallback to "vlan" option if "vid" option is supported but unset.
273                 vid.cfgvalue = function(self, section)
274                         return m.uci:get("network", section, has_vlan4k or "vlan")
275                                 or m.uci:get("network", section, "vlan")
276                 end
277
278                 -- Build per-port off/untagged/tagged choice lists.
279                 local pt
280                 for pt = 0, num_ports - 1 do
281                         local po = s:option(ListValue, tostring(pt),
282                                 (pt == cpu_port) and translate("CPU") or translatef("Port %d", (pt + 1)))
283
284                         po:value("", translate("off"))
285                         po:value("u" % pt, translate("untagged"))
286                         po:value("t" % pt, translate("tagged"))
287
288                         po.cfgvalue = portvalue
289                         po.validate = portvalidate
290                         po.write    = function() end
291
292                         port_opts[#port_opts+1] = po
293                 end
294
295
296                 -- Does this switch support PVIDs?
297                 if has_ptpvid then
298
299                         -- Spawn a "virtual" section. We just attach it to the global
300                         -- switch section here, the overrides below take care of writing
301                         -- the actual values to the correct uci sections.
302                         s = m:section(TypedSection, "switch",
303                                 translatef("Port PVIDs on %q", switch_name),
304                                 translate("Port <abbr title=\"Primary VLAN IDs\">PVIDs</abbr> specify " ..
305                                         "the default VLAN ID added to received untagged frames."))
306
307                         s.template  = "cbi/tblsection"
308                         s.addremove = false
309                         s.anonymous = true
310
311                         -- Build port list, store pointers to the option objects in the
312                         -- pvid_opts array so that other callbacks can repopulate their
313                         -- choice lists.
314                         local pt
315                         for pt = 0, num_ports - 1 do
316                                 local po = s:option(ListValue, tostring(pt),
317                                         (pt == cpu_port) and translate("CPU") or translatef("Port %d", (pt + 1)))
318
319                                 -- When cbi queries the current config value for this post,
320                                 -- lookup the associated switch_port section (if any) and
321                                 -- return its "pvid" or "vlan" option value.
322                                 po.cfgvalue = function(self, section)
323                                         local val
324                                         m.uci:foreach("network", "switch_port",
325                                                 function(s)
326                                                         if s.port == self.option then
327                                                                 val = s[has_ptpvid]
328                                                                 return false
329                                                         end
330                                                 end)
331                                         return val
332                                 end
333
334                                 -- On write, find the actual switch_port section associated
335                                 -- to this port and set the value there. Create a new
336                                 -- switch_port section for this port if there is none yet.
337                                 po.write = function(self, section, value)
338                                         local found = false
339
340                                         m.uci:foreach("network", "switch_port",
341                                                 function(s)
342                                                         if s.port == self.option then
343                                                                 m.uci:set("network", s['.name'], has_ptpvid, value)
344                                                                 found = true
345                                                                 return false
346                                                         end
347                                                 end)
348
349                                         if not found then
350                                                 m.uci:section("network", "switch_port", nil, {
351                                                         ["port"]     = self.option,
352                                                         [has_ptpvid] = value
353                                                 })
354                                         end
355                                 end
356
357                                 -- If the user cleared the PVID value on this port, find
358                                 -- the associated switch_port section and clear it.
359                                 -- If the section does not contain any other unrelated
360                                 -- options (like led or blinkrate) then remove it completely,
361                                 -- else just clear out the "pvid" option.
362                                 po.remove = function(self, section)
363                                         m.uci:foreach("network", "switch_port",
364                                                 function(s)
365                                                         if s.port == self.option then
366                                                                 local k, found
367                                                                 local empty = true
368
369                                                                 for k, _ in pairs(s) do
370                                                                         if k:sub(1,1) ~= "." and k ~= "port" and k ~= has_ptpvid then
371                                                                                 empty = false
372                                                                                 break
373                                                                         end
374                                                                 end
375
376                                                                 if empty then
377                                                                         m.uci:delete("network", s['.name'])
378                                                                 else
379                                                                         m.uci:delete("network", s['.name'], has_ptpvid)
380                                                                 end
381
382                                                                 return false
383                                                         end
384                                                 end)
385                                 end
386
387                                 -- The referenced VLAN might just have been removed, simply
388                                 -- return "" (none) in this case to avoid triggering a
389                                 -- validation error.
390                                 po.validate = function(...)
391                                         return ListValue.validate(...) or ""
392                                 end
393
394                                 pvid_opts[#pvid_opts+1] = po
395                         end
396
397                         populate_pvids()
398                 end
399         end
400 )
401
402 return m