You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.8 KiB

  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2018 rosysong@rosinson.com
  4. #
  5. . /lib/nft-qos/core.sh
  6. # append rule for static qos
  7. qosdef_append_rule_sta() { # <section> <operator> <default-unit> <default-rate>
  8. local ipaddr unit rate
  9. local operator=$2
  10. config_get ipaddr $1 ipaddr
  11. config_get unit $1 unit $3
  12. config_get rate $1 rate $4
  13. [ -z "$ipaddr" ] && return
  14. qosdef_append_rule_ip_limit $ipaddr $operator $unit $rate
  15. }
  16. # append chain for static qos
  17. qosdef_append_chain_sta() { # <hook> <name> <section> <unit> <rate>
  18. local hook=$1 name=$2
  19. local config=$3 operator
  20. case "$name" in
  21. download) operator="daddr";;
  22. upload) operator="saddr";;
  23. esac
  24. qosdef_appendx "\tchain $name {\n"
  25. qosdef_append_chain_def filter $hook 0 accept
  26. qosdef_append_rule_limit_whitelist $name
  27. config_foreach qosdef_append_rule_sta $config $operator $4 $5
  28. qosdef_appendx "\t}\n"
  29. }
  30. qosdef_flush_static() {
  31. qosdef_flush_table "$NFT_QOS_INET_FAMILY" nft-qos-static
  32. }
  33. # static limit rate init
  34. qosdef_init_static() {
  35. local unit_dl unit_ul rate_dl rate_ul
  36. local limit_enable limit_type hook_ul="prerouting" hook_dl="postrouting"
  37. uci_validate_section nft-qos default default \
  38. 'limit_enable:bool:0' \
  39. 'limit_type:maxlength(8)' \
  40. 'static_unit_dl:string:kbytes' \
  41. 'static_unit_ul:string:kbytes' \
  42. 'static_rate_dl:uinteger:50' \
  43. 'static_rate_ul:uinteger:50'
  44. [ $? -ne 0 ] && {
  45. logger -t nft-qos-static "validation failed"
  46. return 1
  47. }
  48. [ $limit_enable -eq 0 -o \
  49. $limit_type = "dynamic" ] && return 1
  50. [ -z "$NFT_QOS_HAS_BRIDGE" ] && {
  51. hook_ul="postrouting"
  52. hook_dl="prerouting"
  53. }
  54. qosdef_appendx "table $NFT_QOS_INET_FAMILY nft-qos-static {\n"
  55. qosdef_append_chain_sta $hook_ul upload upload $unit_ul $rate_ul
  56. qosdef_append_chain_sta $hook_dl download download $unit_dl $rate_dl
  57. qosdef_appendx "}\n"
  58. }