Merge pull request #17303 from CarlosDerSeher/feature_bt_agent
[feed/packages.git] / net / nft-qos / files / lib / monitor.sh
1 #!/bin/sh
2 #
3 # Copyright (C) 2018 rosysong@rosinson.com
4 #
5
6 . /lib/nft-qos/core.sh
7
8 qosdef_monitor_get_ip_handle() { # <family> <chain> <ip>
9 echo $(nft -a list chain $1 nft-qos-monitor $2 2>/dev/null | grep $3 | awk '{print $11}')
10 }
11
12 qosdef_monitor_add() { # <mac> <ip> <hostname>
13 handle_dl=$(qosdef_monitor_get_ip_handle $NFT_QOS_INET_FAMILY download $2)
14 [ -z "$handle_dl" ] && nft add rule $NFT_QOS_INET_FAMILY nft-qos-monitor download ip daddr $2 counter
15 handle_ul=$(qosdef_monitor_get_ip_handle $NFT_QOS_INET_FAMILY upload $2)
16 [ -z "$handle_ul" ] && nft add rule $NFT_QOS_INET_FAMILY nft-qos-monitor upload ip saddr $2 counter
17 }
18
19 qosdef_monitor_del() { # <mac> <ip> <hostname>
20 local handle_dl handle_ul
21 handle_dl=$(qosdef_monitor_get_ip_handle $NFT_QOS_INET_FAMILY download $2)
22 handle_ul=$(qosdef_monitor_get_ip_handle $NFT_QOS_INET_FAMILY upload $2)
23 [ -n "$handle_dl" ] && nft delete handle $handle_dl
24 [ -n "$handle_ul" ] && nft delete handle $handle_ul
25 }
26
27 # init qos monitor
28 qosdef_init_monitor() {
29 local hook_ul="prerouting" hook_dl="postrouting"
30
31 [ -z "$NFT_QOS_HAS_BRIDGE" ] && {
32 hook_ul="postrouting"
33 hook_dl="prerouting"
34 }
35
36 nft add table $NFT_QOS_INET_FAMILY nft-qos-monitor
37 nft add chain $NFT_QOS_INET_FAMILY nft-qos-monitor upload { type filter hook $hook_ul priority 0\; }
38 nft add chain $NFT_QOS_INET_FAMILY nft-qos-monitor download { type filter hook $hook_dl priority 0\; }
39 }