加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 如何使用tc和cgroup来预防数据包

发布时间:2020-12-13 23:02:49 所属栏目:Linux 来源:网络整理
导读:我正在尝试优先处理从某个进程组生成的数据包,以便首先从PC发送它们.我的目标是通过使用cgroups和tc来做到这一点,但似乎没有用. 首先,我在ubuntu上设置cgroup如下, modprobe cls_cgroup # load this module to get net_clsmkdir /sys/fs/cgroup/net_cls # mo
我正在尝试优先处理从某个进程组生成的数据包,以便首先从PC发送它们.我的目标是通过使用cgroups和tc来做到这一点,但似乎没有用.

首先,我在ubuntu上设置cgroup如下,

modprobe cls_cgroup # load this module to get net_cls

mkdir /sys/fs/cgroup/net_cls  # mount point

mount -t cgroup net_cls -onet_cls /sys/fs/cgroup/net_cls/

mkdir /sys/fs/cgroup/net_cls/foo # new cgroup

echo 0x00010001 > /sys/fs/cgroup/foo/net_cls.classid  # echo in a class id

echo 2348 > /sys/fs/cgroup/net_cls/foo/tasks # echo in pid of firefox

tc qdisc add dev eth0 root handle 1: pri

tc qdisc add dev eth0 parent 1:1 handle 10: sfq

tc qdisc add dev eth0 parent 1:2 handle 20: sfq

tc qdisc add dev eth0 parent 1:3 handle 30: sfq

在浏览firefox并运行之后,

tc -s qdisc ls dev eth0

我明白了

qdisc prio 1: root refcnt 2 bands 3 priomap  1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
 Sent 29351 bytes 154 pkt (dropped 0,overlimits 0 requeues 0) 
 backlog 0b 0p requeues 0 
qdisc sfq 10: parent 1:1 limit 127p quantum 1514b divisor 1024 
 Sent 0 bytes 0 pkt (dropped 0,overlimits 0 requeues 0) 
 backlog 0b 0p requeues 0 
qdisc sfq 20: parent 1:2 limit 127p quantum 1514b divisor 1024 
 Sent 27873 bytes 143 pkt (dropped 0,overlimits 0 requeues 0) 
 backlog 0b 0p requeues 0 
qdisc sfq 30: parent 1:3 limit 127p quantum 1514b divisor 1024 
 Sent 0 bytes 0 pkt (dropped 0,overlimits 0 requeues 0) 
 backlog 0b 0p requeues 0

相反,我希望流量在句柄10中流动,我做错了什么?

解决方法

执行此操作的正确方法需要通知tc您将使用cgroup.这已在Ubuntu 12.04上通过3.10内核进行了验证.

确保您拥有net_cls支持

$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

如果不,

使用net_cls支持编译内核

只需将所有这些选项放在.config中.这些似乎不存在于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

然后制作并安装.

确保您有/ etc / fstab条目

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

创建测试cgroup并进行设置

如果未设置cpuset,某些cgroup设置会抱怨通用错误.您还必须将主要和次要tc类ID转换为0xAAAABBBB的十六进制,其中AAAA是主要的,BBBB是次要的.

# 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

配置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

将任务回送到cgroup

(但一次只有一个)

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

修改tc类

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

编辑:

我一直无法使用ingress进行这项工作.只有出口(上传)似乎有效. tc似乎没有带入口的cgroup选项.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读