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