hqos:adjust smart qos to fair client mode

This commit is contained in:
padavanonly 2024-09-08 20:46:55 +08:00
parent 96d6459965
commit 77d57566c4
5 changed files with 129 additions and 51 deletions

View File

@ -33,7 +33,7 @@ return view.extend({
o.rmempty = false;
o = s.option(form.Flag, 'smarthqos', _('SMART_HWQOS'),
_('Enabling fair queue will automatically perform hardware offloading and forwarding for small packets, P2P downloads, and web browsing. After enabling, the maximum number available for IP speed limit will be limited to 25.'));
_('Enabling fair queue will automatically perform hardware offloading for every local host'));
o.default = o.disabled;
o.rmempty = false;

View File

@ -25,10 +25,8 @@ msgstr ""
#: package/mtk/applications/luci-app-eqos-mtk/htdocs/luci-static/resources/view/eqos.js:36
msgid ""
"Enabling fair queue will automatically perform hardware offloading and "
"forwarding for small packets, P2P downloads, and web browsing. After "
"enabling, the maximum number available for IP speed limit will be limited to "
"25."
"Enabling fair queue will automatically perform hardware offloading "
"forwarding for every local host "
msgstr ""
#: package/mtk/applications/luci-app-eqos-mtk/root/usr/share/rpcd/acl.d/luci-app-eqos.json:3

View File

@ -30,8 +30,8 @@ msgstr "启用"
msgid "SMART_HWQOS"
msgstr "智能硬件QOS"
msgid "Enabling fair queue will automatically perform hardware offloading and forwarding for small packets, P2P downloads, and web browsing. After enabling, the maximum number available for IP speed limit will be limited to 25."
msgstr "启用公平队列将自动对小包、P2P下载、网页浏览进行硬件分流转发.启用后IP限速可用的最大编号将限制为25"
msgid "Enabling fair queue will automatically perform hardware offloading for every local host"
msgstr "启用硬件公平队列,局域网的每台设备将拥有相同的转发优先级,保证设备最低网速(仅对dhcp获得ip地址的设备生效,不兼容固定限速)"
msgid "IPV6Enable"
msgstr "同时作用IPV6"

View File

@ -0,0 +1,103 @@
#!/bin/sh
ACTION=$2
MARK_FILE="/tmp/dhcp_mac_mark_mapping" # 存储MAC和MARK映射关系的文件
LEASE_FILE="/tmp/dhcp.leases" # DHCP leases 文件
MAX_MARK=31 # 限制mark值在1到31之间
# 定义哈希函数将MAC地址转化为一个数值限制在1到31之间
hash_mac() {
MAC=$1
# 将MAC地址中的冒号去掉转化为十六进制数值取模限制在1到31之间
MAC_HEX=$(echo "$MAC" | sed 's/://g')
echo $(( 0x$MAC_HEX % MAX_MARK + 1 ))
}
# 从文件中加载当前的MAC-MARK映射
load_mapping() {
if [ ! -f "$MARK_FILE" ]; then
touch "$MARK_FILE"
fi
cat "$MARK_FILE"
}
# 检查mark是否被占用
is_mark_in_use() {
MARK=$1
grep -q " $MARK$" "$MARK_FILE"
return $?
}
# 分配一个可用的mark
allocate_mark() {
MAC=$1
MARK=$(hash_mac $MAC)
retries=0 # 记录重试次数
max_retries=$((MAX_MARK - 1)) # 允许的最大重试次数
# 循环检查mark是否被占用直到找到可用的mark
while is_mark_in_use $MARK; do
MARK=$((MARK + 1))
retries=$((retries + 1))
if [ "$MARK" -gt $MAX_MARK ]; then
MARK=1
fi
# 如果重试次数达到最大值退出循环表示无法找到可用的MARK
if [ "$retries" -ge "$max_retries" ]; then
MARK=1
break
fi
done
# 返回可用的MARK
echo $MARK
}
# 保存MAC和MARK的映射
save_mapping() {
MAC=$1
MARK=$2
echo "$MAC $MARK" >> "$MARK_FILE"
}
# 删除MAC对应的MARK映射
delete_mapping() {
MAC=$1
sed -i "/^$MAC /d" "$MARK_FILE"
}
# 处理现有的DHCP记录确保已有设备保留其MARK
process_existing_leases() {
while read -r line; do
IP=$(echo "$line" | awk '{print $3}')
MAC=$(echo "$line" | awk '{print $2}')
EXISTING_MARK=$(grep "^$MAC " "$MARK_FILE" | awk '{print $2}')
if [ -z "$EXISTING_MARK" ]; then
# 如果没有记录分配新的MARK
MARK_VALUE=$(allocate_mark $MAC)
save_mapping $MAC $MARK_VALUE
else
# 使用现有的MARK
MARK_VALUE=$EXISTING_MARK
fi
idpair=$((MARK_VALUE+32))
# 添加iptables规则基于MAC和IP地址给这个设备打上MARK
iptables -t mangle -D eqos -s $IP -j DSCP --set-dscp ${MARK_VALUE}
iptables -t mangle -D eqos -d $IP -j DSCP --set-dscp ${idpair}
ip6tables -t mangle -D eqos -m mac --mac-source $MAC -j MARK --set-mark ${MARK_VALUE}
ebtables -t nat -D eqos -p ipv6 -d $MAC -j mark --mark-set ${idpair}
iptables -t mangle -A eqos -s $IP -j DSCP --set-dscp ${MARK_VALUE}
iptables -t mangle -A eqos -d $IP -j DSCP --set-dscp ${idpair}
ip6tables -t mangle -A eqos -m mac --mac-source $MAC -j MARK --set-mark ${MARK_VALUE}
ebtables -t nat -D eqos -A ipv6 -d $MAC -j mark --mark-set ${idpair}
done < "$LEASE_FILE"
}
if [ "$ACTION" = "init" ]; then
rm /tmp/dhcp_mac_mark_mapping
load_mapping
iptables -t mangle -F eqos
ip6tables -t mangle -F eqos
ebtables -t nat -F eqos
process_existing_leases
fi

View File

@ -50,7 +50,7 @@ start_service() {
ip6tables -t mangle -A FORWARD -j eqos
fi
if [ "$smarthqos" -eq "1" ]; then
if [ "$smarthqos" -eq "1" ]; then
for i in $(seq 1 31); do
if [ "$upload" -ne "0" ]; then
echo 2 1 $(($upload*225)) 1 $(($upload*1000)) 4 1 > /sys/kernel/debug/hnat/qdma_txq$i
@ -60,59 +60,36 @@ start_service() {
if [ "$download" -ne "0" ]; then
echo 3 1 $(($download*200)) 1 $(($download*1000)) 4 1 > /sys/kernel/debug/hnat/qdma_txq$i
fi
done
echo 2 1 $(($upload*100)) 1 $(($upload*1000)) 4 1 > /sys/kernel/debug/hnat/qdma_txq30
echo 3 1 $(($download*150)) 1 $(($download*1000)) 4 1 > /sys/kernel/debug/hnat/qdma_txq62
echo 2 1 $(($upload*100)) 1 $(($upload*1000)) 4 1 > /sys/kernel/debug/hnat/qdma_txq27
echo 3 1 $(($download*150)) 1 $(($download*1000)) 4 1 > /sys/kernel/debug/hnat/qdma_txq59
iptables -t mangle -A eqos -i br-lan -p tcp -j DSCP --set-dscp 31
iptables -t mangle -A eqos -o br-lan -p tcp -j DSCP --set-dscp 63
iptables -t mangle -A eqos -i br-lan -p udp -j DSCP --set-dscp 30
iptables -t mangle -A eqos -o br-lan -p udp -j DSCP --set-dscp 62
iptables -t mangle -A eqos -i br-lan -p tcp --dport 80 -j DSCP --set-dscp 29
iptables -t mangle -A eqos -o br-lan -p tcp --dport 80 -j DSCP --set-dscp 61
iptables -t mangle -A eqos -o br-lan -p tcp --dport 443 -j DSCP --set-dscp 29
iptables -t mangle -A eqos -o br-lan -p tcp --dport 443 -j DSCP --set-dscp 61
iptables -t mangle -A eqos -i br-lan -p tcp --dport 8080 -j DSCP --set-dscp 29
iptables -t mangle -A eqos -o br-lan -p tcp --dport 8080 -j DSCP --set-dscp 61
iptables -t mangle -A eqos -i br-lan -p tcp --dport 8081 -j DSCP --set-dscp 29
iptables -t mangle -A eqos -o br-lan -p tcp --dport 8081 -j DSCP --set-dscp 61
iptables -t mangle -A eqos -i br-lan -m length --length :256 -j DSCP --set-dscp 28
iptables -t mangle -A eqos -o br-lan -m length --length :256 -j DSCP --set-dscp 60
iptables -t mangle -A eqos -i br-lan -p udp -m hashlimit --hashlimit-mode srcip,srcport --hashlimit-name udpout --hashlimit-above 5/sec --hashlimit-burst 5 --hashlimit-htable-expire 10000 --hashlimit-htable-gcinterval 10000 -j DSCP --set-dscp 27
iptables -t mangle -A eqos -o br-lan -p udp -m hashlimit --hashlimit-mode dstip,dstport --hashlimit-name udpin --hashlimit-above 5/sec --hashlimit-burst 5 --hashlimit-htable-expire 10000 --hashlimit-htable-gcinterval 10000 -j DSCP --set-dscp 59
ip6tables -t mangle -A eqos -i br-lan -j MARK --set-mark 31
ip6tables -t mangle -A eqos -o br-lan -j MARK --set-mark 63
ip6tables -t mangle -A eqos -p udp -i br-lan -j MARK --set-mark 30
ip6tables -t mangle -A eqos -p udp -o br-lan -j MARK --set-mark 62
ip6tables -t mangle -A eqos -i br-lan -p tcp --dport 80 -j MARK --set-mark 29
ip6tables -t mangle -A eqos -o br-lan -p tcp --dport 80 -j MARK --set-mark 61
ip6tables -t mangle -A eqos -i br-lan -p tcp --dport 443 -j MARK --set-mark 29
ip6tables -t mangle -A eqos -o br-lan -p tcp --dport 443 -j MARK --set-mark 61
ip6tables -t mangle -A eqos -i br-lan -p tcp --dport 8080 -j MARK --set-mark 29
ip6tables -t mangle -A eqos -o br-lan -p tcp --dport 8080 -j MARK --set-mark 61
ip6tables -t mangle -A eqos -i br-lan -p tcp --dport 8081 -j MARK --set-mark 29
ip6tables -t mangle -A eqos -o br-lan -p tcp --dport 8081 -j MARK --set-mark 61
ip6tables -t mangle -A eqos -i br-lan -m length --length :256 -j MARK --set-mark 28
ip6tables -t mangle -A eqos -o br-lan -m length --length :256 -j MARK --set-mark 60
ip6tables -t mangle -A eqos -i br-lan -p udp -m hashlimit --hashlimit-mode srcip,srcport --hashlimit-name udp6out --hashlimit-above 5/sec --hashlimit-burst 5 --hashlimit-htable-expire 10000 --hashlimit-htable-gcinterval 10000 -j MARK --set-mark 27
ip6tables -t mangle -A eqos -o br-lan -p udp -m hashlimit --hashlimit-mode dstip,dstport --hashlimit-name udp6in --hashlimit-above 5/sec --hashlimit-burst 5 --hashlimit-htable-expire 10000 --hashlimit-htable-gcinterval 10000 -j MARK --set-mark 59
fi
done
fi
iptables -t mangle -F PREROUTING
if [ $interface ]; then
bash /usr/sbin/loadbalance "$interface"
fi
config_foreach parse_device "device"
if [ "$smarthqos" -eq "1" ]; then
rm /etc/hotplug.d/dhcp/99-eqos
cat << 'EOF' > /etc/hotplug.d/dhcp/99-eqos
#!/bin/sh
# 只有在新增 DHCP 租约时才执行操作
if [ "$ACTION" = "add" ]; then
# 执行指定的命令
/etc/init.d/dhcp_mark.sh eqos init
fi
EOF
chmod +x /etc/hotplug.d/dhcp/99-eqos
/etc/init.d/dnsmasq restart
/etc/init.d/dhcp_mark.sh eqos init
fi
}
stop_service() {
eqos stop
iptables -t mangle -F PREROUTING
rm /etc/hotplug.d/dhcp/99-eqos
}
reload_service() {