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