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