libs/core: add "tunnel" interface type to network model, various optimizations

This commit is contained in:
Jo-Philipp Wich 2011-09-24 00:57:59 +00:00
parent baa1c0a365
commit 9b18001b9f

View file

@ -30,7 +30,7 @@ local uci = require "luci.model.uci"
module "luci.model.network" module "luci.model.network"
local ifs, brs, sws, uci_r, uci_s local ifs, brs, sws, tns, uci_r, uci_s
function _list_del(c, s, o, r) function _list_del(c, s, o, r)
local val = uci_r:get(c, s, o) local val = uci_r:get(c, s, o)
@ -174,6 +174,7 @@ function init(cursor)
ifs = { } ifs = { }
brs = { } brs = { }
sws = { } sws = { }
tns = { }
-- read interface information -- read interface information
local n, i local n, i
@ -181,7 +182,11 @@ function init(cursor)
local name = i.name:match("[^:]+") local name = i.name:match("[^:]+")
local prnt = name:match("^([^%.]+)%.") local prnt = name:match("^([^%.]+)%.")
if _iface_virtual(name) or not _iface_ignore(name) then if _iface_virtual(name) then
tns[name] = true
end
if tns[name] or not _iface_ignore(name) then
ifs[name] = ifs[name] or { ifs[name] = ifs[name] or {
idx = i.ifindex or n, idx = i.ifindex or n,
name = name, name = name,
@ -745,15 +750,17 @@ end
function network.get_interface(self) function network.get_interface(self)
if self:is_virtual() then if self:is_virtual() then
return interface(self:proto() .. "-" .. self.sid) tns[self:proto() .. "-" .. self.sid] = true
return interface(self:proto() .. "-" .. self.sid, self)
elseif self:is_bridge() then elseif self:is_bridge() then
return interface("br-" .. self.sid) brs["br-" .. self.sid] = true
return interface("br-" .. self.sid, self)
else else
local ifn = nil local ifn = nil
local num = { } local num = { }
for ifn in utl.imatch(uci_s:get("network", self.sid, "ifname")) do for ifn in utl.imatch(uci_s:get("network", self.sid, "ifname")) do
ifn = ifn:match("^[^:/]+") ifn = ifn:match("^[^:/]+")
return ifn and interface(ifn) return ifn and interface(ifn, self)
end end
ifn = nil ifn = nil
uci_s:foreach("wireless", "wifi-iface", uci_s:foreach("wireless", "wifi-iface",
@ -766,7 +773,7 @@ function network.get_interface(self)
end end
end end
end) end)
return ifn and interface(ifn) return ifn and interface(ifn, self)
end end
end end
@ -777,7 +784,7 @@ function network.get_interfaces(self)
local nfs = { } local nfs = { }
for ifn in utl.imatch(self:get("ifname")) do for ifn in utl.imatch(self:get("ifname")) do
ifn = ifn:match("^[^:/]+") ifn = ifn:match("^[^:/]+")
nfs[ifn] = interface(ifn) nfs[ifn] = interface(ifn, self)
end end
for ifn in utl.kspairs(nfs) do for ifn in utl.kspairs(nfs) do
@ -792,7 +799,7 @@ function network.get_interfaces(self)
num[s.device] = num[s.device] and num[s.device] + 1 or 1 num[s.device] = num[s.device] and num[s.device] + 1 or 1
if s.network == self.sid then if s.network == self.sid then
ifn = "%s.network%d" %{ s.device, num[s.device] } ifn = "%s.network%d" %{ s.device, num[s.device] }
wfs[ifn] = interface(ifn) wfs[ifn] = interface(ifn, self)
end end
end end
end) end)
@ -844,12 +851,13 @@ end
interface = utl.class() interface = utl.class()
function interface.__init__(self, ifname) function interface.__init__(self, ifname, network)
local wif = _wifi_lookup(ifname) local wif = _wifi_lookup(ifname)
if wif then self.wif = wifinet(wif) end if wif then self.wif = wifinet(wif) end
self.ifname = self.ifname or ifname self.ifname = self.ifname or ifname
self.dev = ifs[self.ifname] self.dev = ifs[self.ifname]
self.network = network
end end
function interface.name(self) function interface.name(self)
@ -857,7 +865,7 @@ function interface.name(self)
end end
function interface.mac(self) function interface.mac(self)
return self.dev and self.dev.macaddr or "00:00:00:00:00:00" return (self.dev and self.dev.macaddr or "00:00:00:00:00:00"):upper()
end end
function interface.ipaddrs(self) function interface.ipaddrs(self)
@ -873,6 +881,8 @@ function interface.type(self)
return "wifi" return "wifi"
elseif brs[self.ifname] then elseif brs[self.ifname] then
return "bridge" return "bridge"
elseif tns[self.ifname] then
return "tunnel"
elseif self.ifname:match("%.") then elseif self.ifname:match("%.") then
return "vlan" return "vlan"
elseif sws[self.ifname] then elseif sws[self.ifname] then
@ -915,6 +925,8 @@ function interface.get_type_i18n(self)
return i18n.translate("Ethernet Switch") return i18n.translate("Ethernet Switch")
elseif x == "vlan" then elseif x == "vlan" then
return i18n.translate("VLAN Interface") return i18n.translate("VLAN Interface")
elseif x == "tunnel" then
return i18n.translate("Tunnel Interface")
else else
return i18n.translate("Ethernet Adapter") return i18n.translate("Ethernet Adapter")
end end
@ -990,8 +1002,10 @@ function interface.rx_packets(self)
end end
function interface.get_network(self) function interface.get_network(self)
if self.dev and self.dev.network then if not self.network then
self.network = _M:get_network(self.dev.network) if self.dev and self.dev.network then
self.network = _M:get_network(self.dev.network)
end
end end
if not self.network then if not self.network then