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.

74 lines
1.8 KiB

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