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