1 -- Copyright 2014 Aedan Renner <chipdankly@gmail.com
2 -- Copyright 2018 Florian Eckert <fe@dev.tdt.de>
3 -- Licensed to the public under the GNU General Public License v2.
5 dsp = require "luci.dispatcher"
9 function interfaceWarnings(overview, count, iface_max)
11 if count <= iface_max then
12 warnings = string.format("<strong>%s</strong><br />",
13 translatef("There are currently %d of %d supported interfaces configured", count, iface_max)
16 warnings = string.format("<strong>%s</strong><br />",
17 translatef("WARNING: %d interfaces are configured exceeding the maximum of %d!", count, iface_max)
21 for i, k in pairs(overview) do
22 if overview[i]["network"] == false then
23 warnings = warnings .. string.format("<strong>%s</strong><br />",
24 translatef("WARNING: Interface %s are not found in /etc/config/network", i)
28 if overview[i]["default_route"] == false then
29 warnings = warnings .. string.format("<strong>%s</strong><br />",
30 translatef("WARNING: Interface %s has no default route in the main routing table", i)
34 if overview[i]["reliability"] == false then
35 warnings = warnings .. string.format("<strong>%s</strong><br />",
36 translatef("WARNING: Interface %s has a higher reliability " ..
37 "requirement than tracking hosts (%d)", i, overview[i]["tracking"])
41 if overview[i]["duplicate_metric"] == true then
42 warnings = warnings .. string.format("<strong>%s</strong><br />",
43 translatef("WARNING: Interface %s has a duplicate metric %s configured", i, overview[i]["metric"])
51 function configCheck()
54 local duplicate_metric = {}
55 uci.cursor():foreach("mwan3", "interface",
57 local uci = uci.cursor(nil, "/var/state")
58 local iface = section[".name"]
61 local network = uci:get("network", iface)
62 overview[iface]["network"] = false
63 if network ~= nil then
64 overview[iface]["network"] = true
66 local device = uci:get("network", iface, "ifname")
68 overview[iface]["device"] = device
71 local metric = uci:get("network", iface, "metric")
73 overview[iface]["metric"] = metric
74 overview[iface]["duplicate_metric"] = false
75 for _, m in ipairs(duplicate_metric) do
77 overview[iface]["duplicate_metric"] = true
80 table.insert(duplicate_metric, metric)
83 local dump = require("luci.util").ubus("network.interface.%s" % iface, "status", {})
84 overview[iface]["default_route"] = false
85 if dump and dump.route then
87 for _, route in ipairs(dump.route) do
88 if dump.route[_].target == "0.0.0.0" then
89 overview[iface]["default_route"] = true
95 local trackingNumber = uci:get("mwan3", iface, "track_ip")
96 overview[iface]["tracking"] = 0
97 if trackingNumber and #trackingNumber > 0 then
98 overview[iface]["tracking"] = #trackingNumber
99 overview[iface]["reliability"] = false
100 local reliabilityNumber = tonumber(uci:get("mwan3", iface, "reliability"))
101 if reliabilityNumber and reliabilityNumber <= #trackingNumber then
102 overview[iface]["reliability"] = true
108 -- calculate iface_max usage from firewall mmx_mask
112 function hasbit(x, p)
113 return x % (p + p) >= p
115 function setbit(x, p)
116 return hasbit(x, p) and x or x + p
119 local uci = require("uci").cursor(nil, "/var/state")
120 local mmx_mask = uci:get("mwan3", "globals", "mmx_mask") or "0x3F00"
121 local number = tonumber(mmx_mask, 16)
125 if hasbit(number, bit(i)) then
127 iface_max = setbit( iface_max, bit(bits))
131 -- subtract blackhole, unreachable and default table from iface_max
132 iface_max = iface_max - 3
134 return overview, count, iface_max
137 m5 = Map("mwan3", translate("MWAN - Interfaces"),
138 interfaceWarnings(configCheck()))
140 mwan_interface = m5:section(TypedSection, "interface", nil,
141 translate("MWAN supports up to 250 physical and/or logical interfaces<br />" ..
142 "MWAN requires that all interfaces have a unique metric configured in /etc/config/network<br />" ..
143 "Names must match the interface name found in /etc/config/network (see advanced tab)<br />" ..
144 "Names may contain characters A-Z, a-z, 0-9, _ and no spaces<br />" ..
145 "Interfaces may not share the same name as configured members, policies or rules"))
146 mwan_interface.addremove = true
147 mwan_interface.dynamic = false
148 mwan_interface.sectionhead = translate("Interface")
149 mwan_interface.sortable = false
150 mwan_interface.template = "cbi/tblsection"
151 mwan_interface.extedit = dsp.build_url("admin", "network", "mwan", "interface", "%s")
152 function mwan_interface.create(self, section)
153 TypedSection.create(self, section)
155 luci.http.redirect(dsp.build_url("admin", "network", "mwan", "interface", section))
158 enabled = mwan_interface:option(DummyValue, "enabled", translate("Enabled"))
159 enabled.rawhtml = true
160 function enabled.cfgvalue(self, s)
161 if self.map:get(s, "enabled") == "1" then
162 return translate("Yes")
164 return translate("No")
168 track_method = mwan_interface:option(DummyValue, "track_method", translate("Tracking method"))
169 track_method.rawhtml = true
170 function track_method.cfgvalue(self, s)
171 local tracked = self.map:get(s, "track_ip")
173 return self.map:get(s, "track_method") or "—"
179 reliability = mwan_interface:option(DummyValue, "reliability", translate("Tracking reliability"))
180 reliability.rawhtml = true
181 function reliability.cfgvalue(self, s)
182 local tracked = self.map:get(s, "track_ip")
184 return self.map:get(s, "reliability") or "—"
190 interval = mwan_interface:option(DummyValue, "interval", translate("Ping interval"))
191 interval.rawhtml = true
192 function interval.cfgvalue(self, s)
193 local tracked = self.map:get(s, "track_ip")
195 local intervalValue = self.map:get(s, "interval")
196 if intervalValue then
197 return intervalValue .. "s"
206 down = mwan_interface:option(DummyValue, "down", translate("Interface down"))
208 function down.cfgvalue(self, s)
209 local tracked = self.map:get(s, "track_ip")
211 return self.map:get(s, "down") or "—"
217 up = mwan_interface:option(DummyValue, "up", translate("Interface up"))
219 function up.cfgvalue(self, s)
220 local tracked = self.map:get(s, "track_ip")
222 return self.map:get(s, "up") or "—"
228 metric = mwan_interface:option(DummyValue, "metric", translate("Metric"))
229 metric.rawhtml = true
230 function metric.cfgvalue(self, s)
231 local uci = uci.cursor(nil, "/var/state")
232 local metric = uci:get("network", s, "metric")