modules/admin-full: attempt to determine switch cpu port from /proc/switch/eth0/cpupo...
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_network / vlan.lua
index 80af2c8..8fcf9af 100644 (file)
@@ -10,10 +10,12 @@ You may obtain a copy of the License at
 
        http://www.apache.org/licenses/LICENSE-2.0
 
-$Id$
 ]]--
 
-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."))
+m = Map("network", translate("Switch"), translate("The network ports on this device 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."))
+
+local fs = require "nixio.fs"
+local switches = { }
 
 m.uci:foreach("network", "switch",
        function(x)
@@ -27,7 +29,7 @@ m.uci:foreach("network", "switch",
                local max_vid     = 16
                local num_vlans   = 16
                local num_ports   = 6
-               local cpu_port    = 5
+               local cpu_port    = tonumber(fs.readfile("/proc/switch/eth0/cpuport") or 5)
 
                local switch_title
                local enable_vlan4k = false
@@ -92,10 +94,6 @@ m.uci:foreach("network", "switch",
                        s:option(Flag, has_vlan, translate("Enable VLAN functionality"))
                end
 
-               if enable_vlan4k then
-                       s:option(Flag, "enable_vlan4k", translate("Enable 4K VLANs"))
-               end
-
                if has_learn then
                        x = s:option(Flag, has_learn, translate("Enable learning and aging"))
                        x.default = x.enabled
@@ -109,7 +107,10 @@ m.uci:foreach("network", "switch",
 
 
                -- VLAN table
-               s = m:section(TypedSection, "switch_vlan", translatef("VLANs on %q", switch_name))
+               s = m:section(TypedSection, "switch_vlan",
+                       switch_title and translatef("VLANs on %q (%s)", switch_name, switch_title)
+                                                 or translatef("VLANs on %q", switch_name))
+
                s.template = "cbi/tblsection"
                s.addremove = true
                s.anonymous = true
@@ -161,11 +162,11 @@ m.uci:foreach("network", "switch",
                                        end
                                end)
 
-                       m.uci:set("network", sid, "device", switch_name)
-                       m.uci:set("network", sid, "vlan", max_nr + 1)
+                       m:set(sid, "device", switch_name)
+                       m:set(sid, "vlan", max_nr + 1)
 
                        if has_vlan4k then
-                               m.uci:set("network", sid, has_vlan4k, max_id + 1)
+                               m:set(sid, has_vlan4k, max_id + 1)
                        end
 
                        return sid
@@ -201,10 +202,13 @@ m.uci:foreach("network", "switch",
                end
 
 
-               local vid = s:option(Value, has_vlan4k or "vlan", "VLAN ID")
+               local vid = s:option(Value, has_vlan4k or "vlan", "VLAN ID", "<div id='portstatus-%s'></div>" % switch_name)
+               local mx_vid = has_vlan4k and 4094 or (num_vlans - 1) 
 
                vid.rmempty = false
                vid.forcewrite = true
+               vid.vlan_used = { }
+               vid.datatype = "and(uinteger,range("..min_vid..","..mx_vid.."))"
 
                -- Validate user provided VLAN ID, make sure its within the bounds
                -- allowed by the switch.
@@ -212,7 +216,13 @@ m.uci:foreach("network", "switch",
                        local v = tonumber(value)
                        local m = has_vlan4k and 4094 or (num_vlans - 1)
                        if v ~= nil and v >= min_vid and v <= m then
-                               return value
+                               if not self.vlan_used[v] then
+                                       self.vlan_used[v] = true
+                                       return value
+                               else
+                                       return nil,
+                                               translatef("Invalid VLAN ID given! Only unique IDs are allowed")
+                               end
                        else
                                return nil,
                                        translatef("Invalid VLAN ID given! Only IDs between %d and %d are allowed.", min_vid, m)
@@ -234,7 +244,11 @@ m.uci:foreach("network", "switch",
                                end
                        end
 
-                       m.uci:set("network", section, "ports", table.concat(p, " "))
+                       if enable_vlan4k then
+                               m:set(sid, "enable_vlan4k", "1")
+                       end
+
+                       m:set(section, "ports", table.concat(p, " "))
                        return Value.write(self, section, value)
                end
 
@@ -247,12 +261,18 @@ m.uci:foreach("network", "switch",
                -- Build per-port off/untagged/tagged choice lists.
                local pt
                for pt = 0, num_ports - 1 do
-                       local po = s:option(ListValue, tostring(pt),
-                               (pt == cpu_port) and translate("CPU") or translatef("Port %d", (pt + 1)))
+                       local title
+                       if pt == cpu_port then
+                               title = translate("CPU")
+                       else
+                               title = translatef("Port %d", pt)
+                       end
 
-                       po:value("", translate("off"))
-                       po:value("u" % pt, translate("untagged"))
-                       po:value("t" % pt, translate("tagged"))
+                       local po = s:option(ListValue, tostring(pt), title)
+
+                       po:value("",  translate("off"))
+                       po:value("u", translate("untagged"))
+                       po:value("t", translate("tagged"))
 
                        po.cfgvalue = portvalue
                        po.validate = portvalidate
@@ -260,7 +280,14 @@ m.uci:foreach("network", "switch",
 
                        port_opts[#port_opts+1] = po
                end
+
+               switches[#switches+1] = switch_name
        end
 )
 
+-- Switch status template
+s = m:section(SimpleSection)
+s.template = "admin_network/switch_status"
+s.switches = switches
+
 return m