1 -- Copyright 2018 Rosy Song <rosysong@rosinson.com>
2 -- Licensed to the public under the Apache License 2.0.
4 local uci = require("luci.model.uci").cursor()
5 local wa = require("luci.tools.webadmin")
6 local fs = require("nixio.fs")
7 local ipc = require("luci.ip")
9 local def_rate_dl = uci:get("nft-qos", "default", "static_rate_dl")
10 local def_rate_ul = uci:get("nft-qos", "default", "static_rate_ul")
11 local def_unit_dl = uci:get("nft-qos", "default", "static_unit_dl")
12 local def_unit_ul = uci:get("nft-qos", "default", "static_unit_ul")
14 local def_up = uci:get("nft-qos", "default", "dynamic_bw_up")
15 local def_down = uci:get("nft-qos", "default", "dynamic_bw_down")
17 local limit_enable = uci:get("nft-qos", "default", "limit_enable")
18 local limit_mac_enable = uci:get("nft-qos", "default", "limit_mac_enable")
19 local limit_type = uci:get("nft-qos", "default", "limit_type")
20 local enable_priority = uci:get("nft-qos", "default", "priority_enable")
22 local has_ipv6 = fs.access("/proc/net/ipv6_route")
24 m = Map("nft-qos", translate("QoS over Nftables"))
29 s = m:section(TypedSection, "default", translate("NFT-QoS Settings"))
33 s:tab("limit", "Limit Rate by IP Address")
34 s:tab("limitmac", "Limit Rate by Mac Address")
35 s:tab("priority", "Traffic Priority")
40 o = s:taboption("limit", Flag, "limit_enable", translate("Limit Enable"), translate("Enable Limit Rate Feature"))
41 o.default = limit_enable or o.enabled
44 o = s:taboption("limit", ListValue, "limit_type", translate("Limit Type"), translate("Type of Limit Rate"))
45 o.default = limit_static or "static"
46 o:depends("limit_enable","1")
47 o:value("static", "Static")
48 o:value("dynamic", "Dynamic")
50 o = s:taboption("limit", Value, "static_rate_dl", translate("Default Download Rate"), translate("Default value for download rate"))
51 o.datatype = "uinteger"
52 o.default = def_rate_dl or '50'
53 o:depends("limit_type","static")
55 o = s:taboption("limit", ListValue, "static_unit_dl", translate("Default Download Unit"), translate("Default unit for download rate"))
56 o.default = def_unit_dl or "kbytes"
57 o:depends("limit_type","static")
58 o:value("bytes", "Bytes/s")
59 o:value("kbytes", "KBytes/s")
60 o:value("mbytes", "MBytes/s")
62 o = s:taboption("limit", Value, "static_rate_ul", translate("Default Upload Rate"), translate("Default value for upload rate"))
63 o.datatype = "uinteger"
64 o.default = def_rate_ul or '50'
65 o:depends("limit_type","static")
67 o = s:taboption("limit", ListValue, "static_unit_ul", translate("Default Upload Unit"), translate("Default unit for upload rate"))
68 o.default = def_unit_ul or "kbytes"
69 o:depends("limit_type","static")
70 o:value("bytes", "Bytes/s")
71 o:value("kbytes", "KBytes/s")
72 o:value("mbytes", "MBytes/s")
77 o = s:taboption("limit", Value, "dynamic_bw_down", translate("Download Bandwidth (Mbps)"), translate("Default value for download bandwidth"))
78 o.default = def_up or '100'
79 o.datatype = "uinteger"
80 o:depends("limit_type","dynamic")
82 o = s:taboption("limit", Value, "dynamic_bw_up", translate("Upload Bandwidth (Mbps)"), translate("Default value for upload bandwidth"))
83 o.default = def_down or '100'
84 o.datatype = "uinteger"
85 o:depends("limit_type","dynamic")
87 o = s:taboption("limit", Value, "dynamic_cidr", translate("Target Network (IPv4/MASK)"), translate("Network to be applied, e.g. 192.168.1.0/24, 10.2.0.0/16, etc."))
89 ipc.routes({ family = 4, type = 1 }, function(rt) o.default = rt.dest end)
90 o:depends("limit_type","dynamic")
93 o = s:taboption("limit", Value, "dynamic_cidr6", translate("Target Network6 (IPv6/MASK)"), translate("Network to be applied, e.g. AAAA::BBBB/64, CCCC::1/128, etc."))
95 o:depends("limit_type","dynamic")
98 o = s:taboption("limit", DynamicList, "limit_whitelist", translate("White List for Limit Rate"))
100 o:depends("limit_enable","1")
103 -- limit speed by mac address
105 o = s:taboption("limitmac", Flag, "limit_mac_enable", translate("Limit Enable"), translate("Enable Limit Rate Feature"))
106 o.default = limit_mac_enable or o.enabled
112 o = s:taboption("priority", Flag, "priority_enable", translate("Enable Traffic Priority"), translate("Enable this feature"))
113 o.default = enable_priority or o.enabled
116 o = s:taboption("priority", ListValue, "priority_netdev", translate("Default Network Interface"), translate("Network Interface for Traffic Shaping, e.g. br-lan, eth0.1, eth0, etc."))
117 o:depends("priority_enable", "1")
118 wa.cbi_add_networks(o)
121 -- Static Limit Rate - Download Rate
123 if limit_enable == "1" and limit_type == "static" then
125 x = m:section(TypedSection, "download", translate("Static QoS-Download Rate"))
128 x.template = "cbi/tblsection"
130 o = x:option(Value, "hostname", translate("Hostname"))
131 o.datatype = "hostname"
132 o.default = 'undefined'
135 o = x:option(Value, "ipaddr", translate("IP Address (v4 / v6)"))
137 o = x:option(Value, "ipaddr", translate("IP Address (v4 Only)"))
139 o.datatype = "ipaddr"
140 if nixio.fs.access("/tmp/dhcp.leases") or nixio.fs.access("/var/dhcp6.leases") then
141 o.titleref = luci.dispatcher.build_url("admin", "status", "overview")
144 o = x:option(Value, "rate", translate("Rate"))
145 o.default = def_rate_dl or '50'
147 o.datatype = "uinteger"
149 o = x:option(ListValue, "unit", translate("Unit"))
150 o.default = def_unit_dl or "kbytes"
151 o:value("bytes", "Bytes/s")
152 o:value("kbytes", "KBytes/s")
153 o:value("mbytes", "MBytes/s")
156 -- Static Limit Rate - Upload Rate
158 y = m:section(TypedSection, "upload", translate("Static QoS-Upload Rate"))
161 y.template = "cbi/tblsection"
163 o = y:option(Value, "hostname", translate("Hostname"))
164 o.datatype = "hostname"
165 o.default = 'undefined'
168 o = y:option(Value, "ipaddr", translate("IP Address (v4 / v6)"))
170 o = y:option(Value, "ipaddr", translate("IP Address (v4 Only)"))
172 o.datatype = "ipaddr"
173 if nixio.fs.access("/tmp/dhcp.leases") or nixio.fs.access("/var/dhcp6.leases") then
174 o.titleref = luci.dispatcher.build_url("admin", "status", "overview")
177 o = y:option(Value, "macaddr", translate("MAC (optional)"))
179 o.datatype = "macaddr"
181 o = y:option(Value, "rate", translate("Rate"))
182 o.default = def_rate_ul or '50'
184 o.datatype = "uinteger"
186 o = y:option(ListValue, "unit", translate("Unit"))
187 o.default = def_unit_ul or "kbytes"
188 o:value("bytes", "Bytes/s")
189 o:value("kbytes", "KBytes/s")
190 o:value("mbytes", "MBytes/s")
195 -- Traffic Priority Settings
197 if enable_priority == "1" then
199 s = m:section(TypedSection, "priority", translate("Traffic Priority Settings"))
202 s.template = "cbi/tblsection"
204 o = s:option(ListValue, "protocol", translate("Protocol"))
206 o:value("tcp", "TCP")
207 o:value("udp", "UDP")
208 o:value("udplite", "UDP-Lite")
209 o:value("sctp", "SCTP")
210 o:value("dccp", "DCCP")
212 o = s:option(ListValue, "priority", translate("Priority"))
226 o = s:option(Value, "service", translate("Service"), translate("e.g. https, 23, (separator is comma)"))
229 o = s:option(Value, "comment", translate("Comment"))
235 -- Static By Mac Address
237 if limit_mac_enable == "1" then
239 x = m:section(TypedSection, "client", translate("Limit Traffic Rate By Mac Address"))
242 x.template = "cbi/tblsection"
244 o = x:option(Value, "hostname", translate("Hostname"))
245 o.datatype = "hostname"
248 o = x:option(Value, "macaddr", translate("MAC Address"))
250 o.datatype = "macaddr"
252 o = x:option(Value, "drate", translate("Download Rate"))
253 o.default = def_rate_dl or '50'
255 o.datatype = "uinteger"
257 o = x:option(ListValue, "drunit", translate("Unit"))
258 o.default = def_unit_dl or "kbytes"
259 o:value("bytes", "Bytes/s")
260 o:value("kbytes", "KBytes/s")
261 o:value("mbytes", "MBytes/s")
263 o = x:option(Value, "urate", translate("Upload Rate"))
264 o.default = def_rate_ul or '50'
266 o.datatype = "uinteger"
268 o = x:option(ListValue, "urunit", translate("Unit"))
269 o.default = def_unit_ul or "kbytes"
270 o:value("bytes", "Bytes/s")
271 o:value("kbytes", "KBytes/s")
272 o:value("mbytes", "MBytes/s")