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.

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