How to priotize packets using tc and cgroups

大憨熊 提交于 2019-12-03 16:36:06

The correct way to do this requires informing tc that you are to be using cgroups. This has been verified on Ubuntu 12.04 with a 3.10 kernel.

Ensure you have net_cls support

$ cat /proc/cgroups 
#subsys_name    hierarchy   num_cgroups enabled
cpuset  1   2   1
cpu 1   2   1
cpuacct 1   2   1
memory  1   2   1
net_cls 1   2   1
blkio   1   2   1

if not,

Compile a kernel with net_cls support

Just put all these options in your .config. These appear not to exist in menuconfig.

CONFIG_NET_CLS=y
CONFIG_NET_CLS_BASIC=m
CONFIG_NET_CLS_TCINDEX=m
CONFIG_NET_CLS_ROUTE4=m
CONFIG_NET_CLS_FW=m
CONFIG_NET_CLS_U32=m
CONFIG_NET_CLS_RSVP=m
CONFIG_NET_CLS_RSVP6=m
CONFIG_NET_CLS_FLOW=m
CONFIG_NET_CLS_CGROUP=y
CONFIG_NET_CLS_ACT=y
CONFIG_NET_CLS_IND=y

then make and install.

Ensure you have an /etc/fstab entry

# echo "cgroup /sys/fs/cgroup cgroup defaults 0 0" >> /etc/fstab
# reboot

Create the test cgroup and set it up

Some cgroup setups complain with generic errors if cpuset is not set. You also must convert your major and minor tc class ID into hex of 0xAAAABBBB where AAAA is major and BBBB is minor.

# mkdir /sys/fs/cgroup/clstest
# /bin/echo 0 > /sys/fs/cgroup/clstest/cpuset.mems
# /bin/echo 0 > /sys/fs/cgroup/clstest/cpuset.cpus
# /bin/echo 0x100001 > /sys/fs/cgroup/clstest/net_cls.classid

Configure tc

# tc qdisc add dev eth2 root handle 10: htb
# tc class add dev eth2 parent 10: classid 10:1 htb rate 10mbit
# tc filter add dev eth2 parent 10: protocol ip prio 10 handle 1: cgroup

Echo tasks into cgroup

(but only one at a time)

# echo $FIREFOX_PID > /sys/fs/cgroup/clstest/tasks

Modify tc class

# tc class change dev eth2 parent 10: classid 10:1 htb rate 40mbit

EDIT:

I have been unable to make this work with ingress. Only egress (upload) appears to be working. tc does not appear to take the cgroup option with ingress.

You should add class and filter to the traffic you want to control (assume you want to control HTTP traffic).

# tc class add dev eth0 parent 1:1 classid 1:1 htb rate 500mbit ceil 800mbit burst 10k prio 10
# tc filter add dev eth0 parent 1:1 protocol ip prio 10 u32 match ip dport 80 0xffff flowid 1:1

Then you can use iptraf to verify the connection rate.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!