区分Internet和Local数据流的Qos实现

区分Internet和Local数据流的Qos实现

需求

区分广域网和局域网的流量控制

实现分析

假设sta1链接在ath00接口

可能的上行流量

可能的下行流量

需要给ath00, ath10, eth0 TX方向设置QoS

为了做到基于用户的权重,需要放在同一级子类别中共享带宽。默认每个接口上创建一个根类别,用于子类别共享带宽,并创建一个默认子类,挂载sfq qdisc做均衡

为ath00/ath10/eth0创建静态队列,类别和过滤器

  1. 为ath00创建HTB队列,默认类别1:30,为来自WAN的流量。
tc qdisc add dev ath00 root handle 1: htb default 30 r2q 1
  1. 为根队列1:,创建一个根类,和一个默认类别1:30
tc class add dev ath00 parent 1:0 classid 1:1 htb rate 12kbit ceil 500mbit burst 150k
tc class add dev ath00 parent 1:1 classid 1:30 htb rate 12kbit ceil 500mbit burst 150k
tc qdisc add dev ath00 parent 1:30 handle 30: sfq perturb 10

为sta1创建上行WAN流量限速,在eth0 1:1下子类1:100上

  1. 在eth0的1:1类别下,建立子类1:100 限速(src是sta1)1mbit, 权重wt
tc class add dev eth0 parent 1:1 classid 1:100 htb rate $((12*${wt}))kbit ceil 1mbit burst 15k
  1. 基于basic match 和u32创建过滤器, src:sta1 dst:!192.168.0.0/16
local wan_uplink_ematch="not u32(u32 0xc0a80000 0xffff0000 at 16) and not u32(u32 0xac100000 0xfff00000 at 16) and not u32(u32 0x0a000000 0xff000000 at 16)"
local wan_uplink_ip_ematch="u32(u32 0x${ip_int} 0xfffffffe at 12)"
local ematch="handle $(printf %x ${id}) protocol ip prio 3 basic match '${wan_uplink_ip_ematch} and ${wan_uplink_ematch}'"
run "tc filter add dev eth0 parent 1:0 ${ematch} flowid 1:${id}"

为sta1创建下行WAN流量限速,在ath00 1:1下子类1:100上

  1. 在ath00的1:1类别下创建子类别1:100,限速(dst是sta1)1mbit, 权重wt
tc class add dev ath00 parent 1:1 classid 1:100 htb rate $((12*${wt}))kbit ceil 1mbit burst 15k
  1. 创建过滤器, dst:sta1, src:!192.168.0.0/16
local wan_downlink_ematch="not u32(u32 0xc0a80000 0xffff0000 at 12) and not u32(u32 0xac100000 0xfff00000 at 12) and not u32(u32 0x0a000000 0xff000000 at 12)"
local wan_downlink_ip_ematch="u32(u32 0x${ip_int} 0xfffffffe at 16)"
local ematch="handle $(printf %x ${id}) protocol ip prio 3 basic match '${wan_downlink_ip_ematch} and ${wan_downlink_ematch}'"
run "tc filter add dev $iface parent 1:0 ${ematch} flowid 1:${id}"

为sta1创建上行LAN流量限速,在eth0 1:1100, ath00/ath10 1:2100

  1. 在eth0的1:1类别下创建子类别1:2100,限速(src是sta1的)1mbit,权重wt
tc class add dev eth0 parent 1:1 classid 1:2100 htb rate $((12*${wt}))kbit ceil 1mbit burst 15k
  1. 创建过滤器, str:sta1, dst:192.168.0.0/16
local lan_uplink_ematch="(u32(u32 0xc0a80000 0xffff0000 at 16) or u32(u32 0xac100000 0xfff00000 at 16) or u32(u32        0x0a000000 0xff000000 at 16))"
local lan_uplink_ip_ematch="u32(u32 0x${ip_int} 0xfffffffe at 12)"
local ematch="handle $(printf %x 2${id}) protocol ip prio 2 basic match '${lan_uplink_ip_ematch} and                     ${lan_uplink_ematch}'"
run "tc filter add dev eth0 parent 1:0 ${ematch} flowid 1:2${id}"
  1. 在ath00/ath10的1:1类别下创建子类别1:2100,限速(src是sta1的)1mbit,权重wt
tc class add dev ath00 parent 1:1 classid 1:2100 htb rate $((12*${wt}))kbit ceil 1mbit burst 15k
  1. 创建过滤器, src:sta1, dst192.168.0.0/16
local lan_uplink_ematch="(u32(u32 0xc0a80000 0xffff0000 at 16) or u32(u32 0xac100000 0xfff00000 at 16) or u32(u32        0x0a000000 0xff000000 at 16))"
local lan_uplink_ip_ematch="u32(u32 0x${ip_int} 0xfffffffe at 12)"
local ematch="handle $(printf %x 2${id}) protocol ip prio 2 basic match '${lan_uplink_ip_ematch} and                     ${lan_uplink_ematch}'"
run "tc filter add dev $all_ifaces parent 1:0 ${ematch} flowid 1:2${id}"

为sta1创建下行LAN流量限速,在ath00 1:1100上

  1. 在ath00的1:1类别下创建子类别1:1100,限速(dst是sta1的)1mbit,权重wt
tc class add dev ath00 parent 1:1 classid 1:1100 htb rate $((12*${wt}))kbit ceil 1mbit burst 15k
  1. 创建过滤器
local lan_downlink_ematch="(u32(u32 0xc0a80000 0xffff0000 at 12) or u32(u32 0xac100000 0xfff00000 at 12) or u32(u32      0x0a000000 0xff000000 at 12))"
local lan_downlink_ip_ematch="u32(u32 0x${ip_int} 0xfffffffe at 16)"
local ematch="handle $(printf %x 1${id}) protocol ip prio 1 basic match '${lan_downlink_ip_ematch} and                   ${lan_downlink_ematch}'"
run "tc filter add dev $iface parent 1:0 ${ematch} flowid 1:1${id}"

汇总后类别树如下

Reference

  1. TC
  2. Openwrt QoS
  3. HTB
  4. Linux Advanced Routing and Traffic Control

nicephil@gmail.com – 2017-12-6

posted on 2017-12-21 20:52  nicephil  阅读(249)  评论(0编辑  收藏  举报

导航