Openwrt QOS




Using the setup from my last two posts, let's play with some QoS parameters and observe the results.

The documentation for OpenWRT's QoS is rather lacking, please feel encouraged to improve it as you go!

Prerequisites

Documented here, the qos-scripts package offers a simple configuration that integrates well with the rest of OpenWRT's UCI (Unified Configuration Interface).

On the router:

opkg install qos-scripts

As an added side benefit, qos-scripts will pull in several dependencies that can be used to further tune our QoS.

Overview

You can look into /etc/config/qos to see the default configuration.

You'll see several config block types. Let's take a look at samples.

Interfaces

config interface wan
    option classgroup  "Default"
    option enabled      0
    option upload       128
    option download     1024

This is an interface definition for wanwan is defined in/etc/config/network.

  • option classgroup "Default" defines that we'll use the classes defined in the config classgroup "Default" block also in the configuration file.
  • option enabled 0 defines that QoS is currently not enabled for thewan interface. Meaning this configuration currently doesn't do anything. If it's value was 1 it would be enabled.
  • option upload 128 defines that this interface should only be able to upload at a rate of 128 kilobits/second. (Only for TCP)
  • option download 1024 defines that this interface should only be able to download at a rate of 1024 kilobits/second.

Rules

config classify
    option target       "Priority"
    option ports        "22,53"
    option comment      "ssh, dns"
config classify
    option target       "Normal"
    option proto        "tcp"
    option ports        "20,21,25,80,110,443,993,995"
    option comment      "ftp, smtp, http(s), imap"
config reclassify
    option target       "Priority"
    option proto        "icmp"
config default
    option target       "Bulk"
    option portrange    "1024-65535"

classify blocks are an initial, connection-tracked classification. They are only run on connections which have not been assigned a traffic class already.

reclassify blocks can override the class on a per packet basis without altering the connection's classification.

default blocks are fallbacks for everything that has not been marked by a classify or reclassify.

  • option target "Priority" means that any connections or packets under this block are placed in the specified class (in the interface's classgroup).
  • option ports "22,53" means that this classifier will only work for connections on port 22 and 53.
  • option comment "ssh,dns" is a comment about the purpose of the classifier.
  • option proto "tcp" defines the protocol (tcpudpicmp, etc) that this classifier matches.
  • option pktsize "-500" allows us to define a packetsize to match.

If you're wondering, the - in -500 operates I think, on the definition of the corresponding class.

Classgroups

config classgroup "Default"
    option classes      "Priority Express Normal Bulk"
    option default      "Normal"

classgroup blocks are used to define different class groupings. This is only really useful if you wish to have multiple interfaces with different class considerations, for example, you might want eth1 to have anultrapriority class or something.

Classes

config class "Priority"
    option packetsize  400
    option avgrate     10
    option priority    20
config class "Express"
    option packetsize  1000
    option avgrate     50
    option priority    10
config class "Normal"
    option packetsize  1500
    option packetdelay 100
    option avgrate     10
    option priority    5
config class "Bulk"
    option avgrate     1
    option packetdelay 200

class blocks are used to define packet classes. Each class is placed inside of a seperate bucket.

  • option packetsize 400 defines the size of packets within the bucket.
  • option maxsize 1000 defines the maximum size of the bucket.(Probably, this isn't specified.)
  • option packetdelay 100 defines the delay of the packet, in ms.
  • option avgrate 50 defines some undocumented parameter in %.
  • option priority 20 is a percentile value specifying the bucket's priority.

Initial Configuration

As a reminder, here is the configuration of our setup.

Our setup.Our setup.

First, we need to pull in xt_connmark, which is a depedency for some features of qos-scripts:

modprobe xt_connmark

Then, in /etc/config/qos replace the wan interface with our various defined interfaces with some modest and determinable differences to test that it works.

# INTERFACES:
# Fast client.
config interface lan1
        option classgroup  "Default"
        option enabled      1
        option upload       256
        option download     2048
# Slow client. (1/2 speed)
config interface lan2
        option classgroup  "Default"
        option enabled      1
        option upload       128
        option download     1024
# The Host. (Big pipes)
config interface lan3
        option classgroup  "Default"
        option enabled      1
        option upload       4096
        option download     4096

To enable QoS and start it:

/etc/init.d/qos enable
/etc/init.d/qos start

Later, to restart it:

/etc/init.d/qos restart
Aside: An Resolved Issue

When I attempted to use the pktsize option in a classifier I recieved yet unresolved errors.

The error was:

iptables: No chain/target/match by that name.

Inspecting the generated config:

/usr/lib/qos/generate.sh all

See the script executed along with output (for diagnosing errors):

/usr/lib/qos/generate.sh all | sh -x

Resulted in:

# ...
+ iptables -t mangle -A qos_Default -m mark --mark 0/0xf0 -p udp -m length --length 500 -j MARK --set-mark 34/0xff
iptables: No chain/target/match by that name.
# ...

I was only able to resolve this by removing the use of pktsize in classifiers then restarting the service.

Testing It

Open up a terminal to use vagrant ssh test1 and test2 then, run the following command at approximately the same time (remember to sub in $TEST3IP):

wget $TEST3IP/video.mp4

You should see something like the following:

In action.In action.

Feel free to cancel them at any time. We only care about the rate right now.

As you can see, test1 recieved approximately twice the bandwidth oftest2. Perfect, that's exactly what we wanted.

Things will not always be exact as there are protocol overheads and other factors in our simulation.

Before going on to more complicated experiments you may want to ensure that the rates are more fair.

Experimentation

With our QoS system in place, what kind of experiments can we do to the network?

Favoring Ports

Looking over the default configuration you can see the following config block:

config classify
    option target       "Priority"
    option ports        "22,53"
    option comment      "ssh, dns"

Which match to the following class:

config class "Priority"
    option packetsize  400
    option avgrate     10
    option priority    20

This suggests that all traffic over port 22 (The standard ssh default) will prioritized as it has the highest priority in the default configuration.

But what if you don't use port 22 for ssh? Then this is a silly rule. You can easily just remap the ports option to "2222,53" or something else.

Limiting a Class Rate

Say we'd like to gaurentee that one class of packets can only take up so much of the total limit of the connection.

First, lets get our test3 VM serving off multiple ports so we can classify them differently.

In /etc/nginx/nginx.conf modify your listen block:

    server {
      listen       80 default_server;
      listen       8080; # Add this.
      server_name  localhost;
      root /vagrant;
      # ...

Then run sudo systemctl restart nginx.

Back on the router, edit /etc/config/qos to add two new blocks, aclassify block and a class block. Also make sure that your lan1 andlan2 interfaces have the same upload and download so we can use them to test.

config classify
    option target  "httpdev"
    option ports   "8080"
    option comment "httpdev"
config class "httpdev"
    option packetsize  1500
    option packetdelay 100
    option limitrate   10
    option priority    5

Then in the classroup "Default" add httpdev to the classes.

Finally, restart the QoS:

/etc/init.d/qos reload

Then get test1 to download from port 80, and test2 to download from port 8080.

httpdevhttpdev

Attempting to download on port 8080 only resulted in recieving 10% of the available bandwidth, where on port 80 it was able to use the entire link.

Priorities

Instead of placing a hard limit on the rate a class can achieve, let's instead change the priority it recieves.

On the router, edit /etc/config/qos, change the Normal class to have a 50priority, while leaving httpdev on 5.

config class "Normal"
    option packetsize  1500
    option packetdelay 100
    option avgrate     10
    option priority    50

Then issue /etc/init.d/qos reload. To test this, on one of the test1 ortest2 open two connections and attempt to download from both ports.

Priorities.Priorities.

Here I ran two tests, one with downloads on different ports, one with them on the same. Notice how the 8080 download is much slower while there is a download on port 80 happening, but while both at on port 80 they're nearly the same (except that I couldn't stop both at the same time so the second one I stopped was reading higher then before, they were both in the teens).

A lone download on port 8080 (and thus of the httpdev class) would still be fast if there is no other traffic in the normal class, though.

Further Configuration

While qos-scripts provides many simplier facilities for configuring Quality-of-Service in OpenWRT, it is certainly not the be all, end all.

You can also take a much more low-level and fine grained control of how things behave using standard Linux tools. After all, everything inqos-scripts is built off iptablestc, and other standard tools.

Here are some links to explore further:

A special thanks to the Bergens Banen train for our test material. The cover image of this article is from the video.

小包优先+web优先+2千多条游戏服务器IP优先+游戏爆发+连接数限制 番茄概念QOS脚本 主要概念: 1.小包优先定义: 上传数据包中长度小于128Byte并且状态为ESTABLISHED的数据包 下载数据包中长度小于256Byte并且状态为ESTABLISHED的数据包 这个ESTABLISHED是什么东东呢? 解释:TCP数据包有INVALID,ESTABLISHED,NEW,RELATED,UNTRACKED等状态。 具体解释:http://man.chinaunix.net/network/iptables-tutorial-cn-1.1.19.html#USERLANDSTATES 这个ESTABLISHED指连接已经完全建立的数据包了,而NEW指新建一个连接所使用的第一个数据包。 这样,就排除了那些状态为NEW的小包。 好处:有些人总是热衷于“修改XP最大连接数”来提高BT下载速率。其实这个所谓的“最大连接数”是指“最大并发连接数”, 也就是XP每秒能够发送状态为NEW的数据包,默认值是10。有些垃圾的BT软件将其修改为1000,这样庞大的连接数将会导致 瞬间上传速率非常大,如果将其优先级设置太高,将会导致网络延迟的震荡(一会高一会低)。 PS:“修改XP最大连接数”是无法提高BT下载的速率的,最多可以提高达到最大速率的时间。比如说不修改30秒达到最大速率,修改 后可能10秒就达到最大速率。但是会带来操作系统不稳定,路由器压力增大,蠕虫攻击,网络延迟震荡等不良后果。 微软在IT业混了几十年,从来就不提倡所谓的“修改最大连接数”。 2.正常的web浏览相对于下载BT数据包具有较高的优先级。 3.游戏爆发的定义: 当某个内网IP的速率小于50KB/S的时候,那么该IP的数据包进入“游戏爆发队列”。时间为10秒。 “游戏爆发队列”的优先级仅仅低于“游戏队列” 对于那些只玩游戏不下载的IP有帮助。 4.连接数限制。PS:tcp连接数在ROS各版本均可用,UDP连接数则需在5.X以上支持! 每IP限制TCP连接数100,UDP连接数150,并且对DNS,WEB,QQ等端口例外 不过在此我并未加入在脚本里,大家可以自行添加。 ex: TCP限线程 /ip firewall filter add chain=forward protocol=tcp tcp-flags=syn connection-limit=100,32 action=drop 创奇高手 QQ:550453843 Q群:500人总群 ROS总基地 群号:7217877(拥挤) 新群 创奇网络社区ROS总基地 群号:143322592(推荐) 新群 创奇网络社区ROS总基地 群号:139532314 (推荐) 创奇网络社区|ROS总基地 专注于ROS软路由技术!专注于互联网IT前沿技术!http://bbs.chanki.net 提倡互助分享精神!打造一个集IT资讯,网吧技术,网络技术,资源共享,硬件数码等的一体化交流社区!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值