nft-qos.lua 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. -- Copyright 2018 Rosy Song <rosysong@rosinson.com>
  2. -- Licensed to the public under the Apache License 2.0.
  3. local uci = require("luci.model.uci").cursor()
  4. local wa = require("luci.tools.webadmin")
  5. local fs = require("nixio.fs")
  6. local ipc = require("luci.ip")
  7. local def_rate_dl = uci:get("nft-qos", "default", "static_rate_dl")
  8. local def_rate_ul = uci:get("nft-qos", "default", "static_rate_ul")
  9. local def_unit_dl = uci:get("nft-qos", "default", "static_unit_dl")
  10. local def_unit_ul = uci:get("nft-qos", "default", "static_unit_ul")
  11. local def_up = uci:get("nft-qos", "default", "dynamic_bw_up")
  12. local def_down = uci:get("nft-qos", "default", "dynamic_bw_down")
  13. local limit_enable = uci:get("nft-qos", "default", "limit_enable")
  14. local limit_mac_enable = uci:get("nft-qos", "default", "limit_mac_enable")
  15. local limit_type = uci:get("nft-qos", "default", "limit_type")
  16. local enable_priority = uci:get("nft-qos", "default", "priority_enable")
  17. local has_ipv6 = fs.access("/proc/net/ipv6_route")
  18. m = Map("nft-qos", translate("QoS over Nftables"))
  19. --
  20. -- Taboptions
  21. --
  22. s = m:section(TypedSection, "default", translate("NFT-QoS Settings"))
  23. s.addremove = false
  24. s.anonymous = true
  25. s:tab("limit", translate("Limit Rate by IP Address"))
  26. s:tab("limitmac", translate("Limit Rate by Mac Address"))
  27. s:tab("priority", translate("Traffic Priority"))
  28. --
  29. -- Static
  30. --
  31. o = s:taboption("limit", Flag, "limit_enable", translate("Limit Enable"), translate("Enable Limit Rate Feature"))
  32. o.default = limit_enable or o.enabled
  33. o.rmempty = false
  34. o = s:taboption("limit", ListValue, "limit_type", translate("Limit Type"), translate("Type of Limit Rate"))
  35. o.default = limit_static or "static"
  36. o:depends("limit_enable","1")
  37. o:value("static", "Static")
  38. o:value("dynamic", "Dynamic")
  39. o = s:taboption("limit", Value, "static_rate_dl", translate("Default Download Rate"), translate("Default value for download rate"))
  40. o.datatype = "uinteger"
  41. o.default = def_rate_dl or '50'
  42. o:depends("limit_type","static")
  43. o = s:taboption("limit", ListValue, "static_unit_dl", translate("Default Download Unit"), translate("Default unit for download rate"))
  44. o.default = def_unit_dl or "kbytes"
  45. o:depends("limit_type","static")
  46. o:value("bytes", "Bytes/s")
  47. o:value("kbytes", "KBytes/s")
  48. o:value("mbytes", "MBytes/s")
  49. o = s:taboption("limit", Value, "static_rate_ul", translate("Default Upload Rate"), translate("Default value for upload rate"))
  50. o.datatype = "uinteger"
  51. o.default = def_rate_ul or '50'
  52. o:depends("limit_type","static")
  53. o = s:taboption("limit", ListValue, "static_unit_ul", translate("Default Upload Unit"), translate("Default unit for upload rate"))
  54. o.default = def_unit_ul or "kbytes"
  55. o:depends("limit_type","static")
  56. o:value("bytes", "Bytes/s")
  57. o:value("kbytes", "KBytes/s")
  58. o:value("mbytes", "MBytes/s")
  59. --
  60. -- Dynamic
  61. --
  62. o = s:taboption("limit", Value, "dynamic_bw_down", translate("Download Bandwidth (Mbps)"), translate("Default value for download bandwidth"))
  63. o.default = def_up or '100'
  64. o.datatype = "uinteger"
  65. o:depends("limit_type","dynamic")
  66. o = s:taboption("limit", Value, "dynamic_bw_up", translate("Upload Bandwidth (Mbps)"), translate("Default value for upload bandwidth"))
  67. o.default = def_down or '100'
  68. o.datatype = "uinteger"
  69. o:depends("limit_type","dynamic")
  70. 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."))
  71. o.datatype = "cidr4"
  72. ipc.routes({ family = 4, type = 1 }, function(rt) o.default = rt.dest end)
  73. o:depends("limit_type","dynamic")
  74. if has_ipv6 then
  75. 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."))
  76. o.datatype = "cidr6"
  77. o:depends("limit_type","dynamic")
  78. end
  79. o = s:taboption("limit", DynamicList, "limit_whitelist", translate("White List for Limit Rate"))
  80. o.datatype = "ipaddr"
  81. o:depends("limit_enable","1")
  82. --
  83. -- limit speed by mac address
  84. --
  85. o = s:taboption("limitmac", Flag, "limit_mac_enable", translate("Limit Enable"), translate("Enable Limit Rate Feature"))
  86. o.default = limit_mac_enable or o.enabled
  87. o.rmempty = false
  88. --
  89. -- Priority
  90. --
  91. o = s:taboption("priority", Flag, "priority_enable", translate("Enable Traffic Priority"), translate("Enable this feature"))
  92. o.default = enable_priority or o.enabled
  93. o.rmempty = false
  94. 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."))
  95. o:depends("priority_enable", "1")
  96. wa.cbi_add_networks(o)
  97. --
  98. -- Static Limit Rate - Download Rate
  99. --
  100. if limit_enable == "1" and limit_type == "static" then
  101. x = m:section(TypedSection, "download", translate("Static QoS-Download Rate"))
  102. x.anonymous = true
  103. x.addremove = true
  104. x.template = "cbi/tblsection"
  105. o = x:option(Value, "hostname", translate("Hostname"))
  106. o.datatype = "hostname"
  107. o.default = 'undefined'
  108. if has_ipv6 then
  109. o = x:option(Value, "ipaddr", translate("IP Address (v4 / v6)"))
  110. else
  111. o = x:option(Value, "ipaddr", translate("IP Address (v4 Only)"))
  112. end
  113. o.datatype = "ipaddr"
  114. if nixio.fs.access("/tmp/dhcp.leases") or nixio.fs.access("/var/dhcp6.leases") then
  115. o.titleref = luci.dispatcher.build_url("admin", "status", "overview")
  116. end
  117. o = x:option(Value, "rate", translate("Rate"))
  118. o.default = def_rate_dl or '50'
  119. o.size = 4
  120. o.datatype = "uinteger"
  121. o = x:option(ListValue, "unit", translate("Unit"))
  122. o.default = def_unit_dl or "kbytes"
  123. o:value("bytes", "Bytes/s")
  124. o:value("kbytes", "KBytes/s")
  125. o:value("mbytes", "MBytes/s")
  126. --
  127. -- Static Limit Rate - Upload Rate
  128. --
  129. y = m:section(TypedSection, "upload", translate("Static QoS-Upload Rate"))
  130. y.anonymous = true
  131. y.addremove = true
  132. y.template = "cbi/tblsection"
  133. o = y:option(Value, "hostname", translate("Hostname"))
  134. o.datatype = "hostname"
  135. o.default = 'undefined'
  136. if has_ipv6 then
  137. o = y:option(Value, "ipaddr", translate("IP Address (v4 / v6)"))
  138. else
  139. o = y:option(Value, "ipaddr", translate("IP Address (v4 Only)"))
  140. end
  141. o.datatype = "ipaddr"
  142. if nixio.fs.access("/tmp/dhcp.leases") or nixio.fs.access("/var/dhcp6.leases") then
  143. o.titleref = luci.dispatcher.build_url("admin", "status", "overview")
  144. end
  145. o = y:option(Value, "rate", translate("Rate"))
  146. o.default = def_rate_ul or '50'
  147. o.size = 4
  148. o.datatype = "uinteger"
  149. o = y:option(ListValue, "unit", translate("Unit"))
  150. o.default = def_unit_ul or "kbytes"
  151. o:value("bytes", "Bytes/s")
  152. o:value("kbytes", "KBytes/s")
  153. o:value("mbytes", "MBytes/s")
  154. end
  155. --
  156. -- Traffic Priority Settings
  157. --
  158. if enable_priority == "1" then
  159. s = m:section(TypedSection, "priority", translate("Traffic Priority Settings"))
  160. s.anonymous = true
  161. s.addremove = true
  162. s.template = "cbi/tblsection"
  163. o = s:option(ListValue, "protocol", translate("Protocol"))
  164. o.default = "tcp"
  165. o:value("tcp", "TCP")
  166. o:value("udp", "UDP")
  167. o:value("udplite", "UDP-Lite")
  168. o:value("sctp", "SCTP")
  169. o:value("dccp", "DCCP")
  170. o = s:option(ListValue, "priority", translate("Priority"))
  171. o.default = "1"
  172. o:value("-400", "1")
  173. o:value("-300", "2")
  174. o:value("-225", "3")
  175. o:value("-200", "4")
  176. o:value("-150", "5")
  177. o:value("-100", "6")
  178. o:value("0", "7")
  179. o:value("50", "8")
  180. o:value("100", "9")
  181. o:value("225", "10")
  182. o:value("300", "11")
  183. o = s:option(Value, "service", translate("Service"), translate("e.g. https, 23, (separator is comma)"))
  184. o.default = '?'
  185. o = s:option(Value, "comment", translate("Comment"))
  186. o.default = '?'
  187. end
  188. --
  189. -- Static By Mac Address
  190. --
  191. if limit_mac_enable == "1" then
  192. x = m:section(TypedSection, "client", translate("Limit Traffic Rate By Mac Address"))
  193. x.anonymous = true
  194. x.addremove = true
  195. x.template = "cbi/tblsection"
  196. o = x:option(Value, "hostname", translate("Hostname"))
  197. o.datatype = "hostname"
  198. o.default = ''
  199. o = x:option(Value, "macaddr", translate("MAC Address"))
  200. o.rmempty = true
  201. o.datatype = "macaddr"
  202. o = x:option(Value, "drate", translate("Download Rate"))
  203. o.default = def_rate_dl or '50'
  204. o.size = 4
  205. o.datatype = "uinteger"
  206. o = x:option(ListValue, "drunit", translate("Unit"))
  207. o.default = def_unit_dl or "kbytes"
  208. o:value("bytes", "Bytes/s")
  209. o:value("kbytes", "KBytes/s")
  210. o:value("mbytes", "MBytes/s")
  211. o = x:option(Value, "urate", translate("Upload Rate"))
  212. o.default = def_rate_ul or '50'
  213. o.size = 4
  214. o.datatype = "uinteger"
  215. o = x:option(ListValue, "urunit", translate("Unit"))
  216. o.default = def_unit_ul or "kbytes"
  217. o:value("bytes", "Bytes/s")
  218. o:value("kbytes", "KBytes/s")
  219. o:value("mbytes", "MBytes/s")
  220. end
  221. return m