modules/admin-full: readd jumbo frame passthrough support
[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 has_jumbo3  = nil
28                 local min_vid     = 0
29                 local max_vid     = 16
30                 local num_vlans   = 16
31                 local num_ports   = 6
32                 local cpu_port    = 5
33
34                 local enable_vlan4k = false
35
36                 -- Parse some common switch properties from swconfig help output.
37                 local swc = io.popen("swconfig dev %q help 2>/dev/null" % switch_name)
38                 if swc then
39
40                         local is_port_attr = false
41                         local is_vlan_attr = false
42
43                         while true do
44                                 local line = swc:read("*l")
45                                 if not line then break end
46
47                                 if line:match("^%s+%-%-vlan") then
48                                         is_vlan_attr = true
49
50                                 elseif line:match("^%s+%-%-port") then
51                                         is_vlan_attr = false
52                                         is_port_attr = true
53
54                                 elseif line:match("^Switch %d+:") then
55                                         num_ports, cpu_port, num_vlans =
56                                                 line:match("ports: (%d+) %(cpu @ (%d+)%), vlans: (%d+)")
57
58                                         num_ports  = tonumber(num_ports or  6)
59                                         num_vlans  = tonumber(num_vlans or 16)
60                                         cpu_port   = tonumber(cpu_port  or  5)
61                                         min_vid    = 1
62                                         has_enable = nil
63
64                                 elseif line:match(": pvid") or line:match(": tag") or line:match(": vid") then
65                                         if is_vlan_attr then has_vlan4k = line:match(": (%w+)") end
66                                         if is_port_attr then has_ptpvid = line:match(": (%w+)") end
67
68                                 elseif line:match(": enable_vlan") then
69                                         has_vlan = "enable_vlan"
70
71                                 elseif line:match(": enable_learning") then
72                                         has_learn = "enable_learning"
73
74                                 elseif line:match(": reset") then
75                                         has_reset = "reset"
76
77                                 elseif line:match(": enable_vlan4k") then
78                                         enable_vlan4k = true
79
80                                 elseif line:match(": max_length") then
81                                         has_jumbo3 = "max_length"
82                                 end
83                         end
84
85                         swc:close()
86                 end
87
88
89                 -- The PVID options (if any) are added to this table so that
90                 -- section create below can add the just created vlan to the
91                 -- choice list of the PVID options...
92                 local pvid_opts = { }
93
94                 -- This function re-reads all existing vlan ids and populates
95                 -- PVID options choice lists
96                 local function populate_pvids()
97                         local vlan_ids = { }
98                         m.uci:foreach("network", "switch_vlan",
99                                 function(s)
100                                         local vid = s[has_vlan4k or "vlan"] or s["vlan"]
101                                         if vid ~= nil then
102                                                 vlan_ids[#vlan_ids+1] = vid
103                                         end
104                                 end)
105
106                         local opt, vid
107                         for _, opt in ipairs(pvid_opts) do
108                                 opt:reset_values()
109                                 opt:value("", translate("none"))
110                                 for _, vid in luci.util.vspairs(vlan_ids) do
111                                         opt:value(vid, translatef("VLAN %d", tonumber(vid)))
112                                 end
113                         end
114                 end
115
116                 -- Switch properties
117                 s = m:section(NamedSection, x['.name'], "switch", translatef("Switch %q", switch_name))
118                 s.addremove = false
119
120                 if has_enable then
121                         x = s:option(Flag, has_enable, translate("Enable this switch"))
122                         x.default = x.enabled
123                 end
124
125                 if has_vlan then
126                         x = s:option(Flag, has_vlan, translate("Enable VLAN functionality"))
127                         x.default = x.enabled
128                 end
129
130                 if enable_vlan4k then
131                         s:option(Flag, "enable_vlan4k", translate("Enable 4K VLANs"))
132                 end
133
134                 if has_learn then
135                         x = s:option(Flag, has_learn, translate("Enable learning and aging"))
136                         x.default = x.enabled
137                 end
138
139                 if has_jumbo3 then
140                         x = s:option(Flag, has_jumbo3, translate("Enable Jumbo Frame passthrough"))
141                         x.enabled = "3"
142                         x.rmempty = true
143                 end
144
145                 if has_reset then
146                         x = s:option(Flag, has_reset, translate("Reset switch during setup"))
147                         x.default = x.enabled
148                 end
149
150
151                 -- VLAN table
152                 s = m:section(TypedSection, "switch_vlan", translatef("VLANs on %q", switch_name))
153                 s.template = "cbi/tblsection"
154                 s.addremove = true
155                 s.anonymous = true
156
157                 -- Override cfgsections callback to enforce row ordering by vlan id.
158                 s.cfgsections = function(self)
159                         local osections = TypedSection.cfgsections(self)
160                         local sections = { }
161                         local section
162
163                         for _, section in luci.util.spairs(
164                                 osections,
165                                 function(a, b)
166                                         return (tonumber(m.uci:get("network", osections[a], has_vlan4k or "vlan")) or 9999)
167                                                 <  (tonumber(m.uci:get("network", osections[b], has_vlan4k or "vlan")) or 9999)
168                                 end
169                         ) do
170                                 sections[#sections+1] = section
171                         end
172
173                         return sections
174                 end
175
176                 -- When creating a new vlan, preset it with the highest found vid + 1.
177                 -- Repopulate the PVID choice lists afterwards.
178                 s.create = function(self, section)
179                         local sid = TypedSection.create(self, section)
180
181                         local max_nr = 0
182                         local max_id = 0
183
184                         m.uci:foreach("network", "switch_vlan",
185                                 function(s)
186                                         local nr = tonumber(s.vlan)
187                                         local id = has_vlan4k and tonumber(s[has_vlan4k])
188                                         if nr ~= nil and nr > max_nr then max_nr = nr end
189                                         if id ~= nil and id > max_id then max_id = id end
190                                 end)
191
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.uci:get("network", 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.uci:get("network", section, has_vlan4k or "vlan")
284                                 or m.uci:get("network", 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                         -- Build port list, store pointers to the option objects in the
321                         -- pvid_opts array so that other callbacks can repopulate their
322                         -- choice lists.
323                         local pt
324                         for pt = 0, num_ports - 1 do
325                                 local po = s:option(ListValue, tostring(pt),
326                                         (pt == cpu_port) and translate("CPU") or translatef("Port %d", (pt + 1)))
327
328                                 -- When cbi queries the current config value for this post,
329                                 -- lookup the associated switch_port section (if any) and
330                                 -- return its "pvid" or "vlan" option value.
331                                 po.cfgvalue = function(self, section)
332                                         local val
333                                         m.uci:foreach("network", "switch_port",
334                                                 function(s)
335                                                         if s.port == self.option then
336                                                                 val = s[has_ptpvid]
337                                                                 return false
338                                                         end
339                                                 end)
340                                         return val
341                                 end
342
343                                 -- On write, find the actual switch_port section associated
344                                 -- to this port and set the value there. Create a new
345                                 -- switch_port section for this port if there is none yet.
346                                 po.write = function(self, section, value)
347                                         local found = false
348
349                                         m.uci:foreach("network", "switch_port",
350                                                 function(s)
351                                                         if s.port == self.option then
352                                                                 m.uci:set("network", s['.name'], has_ptpvid, value)
353                                                                 found = true
354                                                                 return false
355                                                         end
356                                                 end)
357
358                                         if not found then
359                                                 m.uci:section("network", "switch_port", nil, {
360                                                         ["port"]     = self.option,
361                                                         [has_ptpvid] = value
362                                                 })
363                                         end
364                                 end
365
366                                 -- If the user cleared the PVID value on this port, find
367                                 -- the associated switch_port section and clear it.
368                                 -- If the section does not contain any other unrelated
369                                 -- options (like led or blinkrate) then remove it completely,
370                                 -- else just clear out the "pvid" option.
371                                 po.remove = function(self, section)
372                                         m.uci:foreach("network", "switch_port",
373                                                 function(s)
374                                                         if s.port == self.option then
375                                                                 local k, found
376                                                                 local empty = true
377
378                                                                 for k, _ in pairs(s) do
379                                                                         if k:sub(1,1) ~= "." and k ~= "port" and k ~= has_ptpvid then
380                                                                                 empty = false
381                                                                                 break
382                                                                         end
383                                                                 end
384
385                                                                 if empty then
386                                                                         m.uci:delete("network", s['.name'])
387                                                                 else
388                                                                         m.uci:delete("network", s['.name'], has_ptpvid)
389                                                                 end
390
391                                                                 return false
392                                                         end
393                                                 end)
394                                 end
395
396                                 -- The referenced VLAN might just have been removed, simply
397                                 -- return "" (none) in this case to avoid triggering a
398                                 -- validation error.
399                                 po.validate = function(...)
400                                         return ListValue.validate(...) or ""
401                                 end
402
403                                 pvid_opts[#pvid_opts+1] = po
404                         end
405
406                         populate_pvids()
407                 end
408         end
409 )
410
411 return m