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