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.

93 lines
2.0 KiB

  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2018 rosysong@rosinson.com
  4. #
  5. . /lib/functions/network.sh
  6. . /lib/nft-qos/core.sh
  7. P1=""; P2=""; P3=""; P4=""; P5=""; P6="";
  8. P7=""; P8=""; P9=""; P10=""; P11="";
  9. qosdef_validate_priority() {
  10. uci_load_validate nft-qos default "$1" "$2" \
  11. 'priority_enable:bool:0' \
  12. 'priority_netdev:maxlength(8)'
  13. }
  14. _qosdef_handle_protox() { # <priority> <rule>
  15. case "$1" in
  16. -400) P1="$P1""$2";;
  17. -300) P2="$P2""$2";;
  18. -225) P3="$P3""$2";;
  19. -200) P4="$P4""$2";;
  20. -150) P5="$P5""$2";;
  21. -100) P6="$P6""$2";;
  22. 0) P7="$P7""$2";;
  23. 50) P8="$P8""$2";;
  24. 100) P9="$P9""$2";;
  25. 225) P10="$P10""$2";;
  26. 300) P11="$P11""$2";;
  27. esac
  28. }
  29. qosdef_handle_protox() { # <section>
  30. local proto prio srv
  31. config_get proto $1 'protocol'
  32. config_get prio $1 'priority'
  33. config_get srv $1 'service'
  34. [ -z "$proto" -o \
  35. -z "$prio" -o \
  36. -z "$srv" ] && return
  37. _qosdef_handle_protox $prio \
  38. "\t\t$proto dport { $srv } accept\n"
  39. }
  40. qosdef_append_rule_protox() { # <section>
  41. config_foreach qosdef_handle_protox $1
  42. qosdef_appendx \
  43. "${P1}${P2}${P3}${P4}${P5}${P6}${P7}${P8}${P9}${P10}${P11}"
  44. }
  45. qosdef_append_chain_priority() { # <name> <section> <device>
  46. local name=$1 device=$3
  47. qosdef_appendx "\tchain $name {\n"
  48. qosdef_append_chain_ingress filter $device 0 accept
  49. qosdef_append_rule_protox $2
  50. qosdef_appendx "\t}\n"
  51. }
  52. qosdef_remove_priority() {
  53. qosdef_remove_table netdev nft-qos-priority
  54. }
  55. # init traffic priority
  56. qosdef_init_priority() {
  57. local ifname="br-lan"
  58. [ "$2" = 0 ] || {
  59. logger -t nft-qos-priority "validation failed"
  60. return 1
  61. }
  62. [ $priority_enable -eq 0 ] && return 1
  63. case "$priority_netdev" in
  64. lan) [ "$(uci_get network.lan.type)" != "bridge" ] && {
  65. network_get_device ifname "$priority_netdev" || \
  66. ifname="$(uci_get network.lan.ifname)"
  67. }
  68. ;;
  69. wan*) network_get_device ifname "$priority_netdev" || \
  70. ifname="$(uci_get network.$priority_netdev.ifname)"
  71. ;;
  72. esac
  73. qosdef_appendx "table netdev nft-qos-priority {\n"
  74. qosdef_append_chain_priority filter priority $ifname
  75. qosdef_appendx "}\n"
  76. }