tc 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

tc 命令安装:


  1. -bash/zsh: tc: command not found
  2.  
  3. #Debian
  4. apt-get install iproute2
  5.  
  6. #Ubuntu
  7. apt-get install iproute2
  8.  
  9. #Alpine
  10. apk add iproute2
  11.  
  12. #Arch Linux
  13. pacman -S iproute2
  14.  
  15. #Kali Linux
  16. apt-get install iproute2
  17.  
  18. #CentOS
  19. yum install iproute-tc
  20.  
  21. #Fedora
  22. dnf install iproute-tc
  23.  
  24. #Raspbian
  25. apt-get install iproute2
  26.  
  27. #Docker
  28. docker run cmd.cat/tc tc

tc 命令补充说明:



tc

tc 用于在 Linux 内核中配置流量控制。tc 是 Linux 自带的模块,一般情况下不需要另行安装,可以用 man tc 查看 tc 相关命令细节,要求内核 2.4.18 以上。

tc 命令原理:


Linux 中的 QoS 分为入口 (Ingress) 部分和出口 (Egress) 部分,入口部分主要用于进行入口流量限速 (Policing),出口部分主要用于队列调度 (Queuing Scheduling)。大多数排队规则 (QDisc) 都是用于输出方向的,输入方向只有一个排队规则,即 Ingressqdisc。

Ingressqdisc 本身的功能很有限,输入方向只有一个排队规则,即 IngressqDisc(因为没有缓存只能实现流量的 Drop)但可用于重定向 Incomingpackets。通过 IngressqDisc 把输入方向的数据包重定向到虚拟设备 ifb,而 ifb 的输出方向可以配置多种 QDisc,就可以达到对输入方向的流量做队列调度的目的。

tc 命令语法:


  1. tc [ OPTIONS ] qdisc [ add | change | replace | link | delete ]
  2. dev DEV [ parent qdisc-id | root ] [ handle qdisc-id ] [
  3. ingress_block BLOCK_INDEX ] [ egress_block BLOCK_INDEX ] qdisc [
  4. qdisc specific parameters ]
  5.  
  6. tc [ OPTIONS ] class [ add | change | replace | delete ] dev DEV
  7. parent qdisc-id [ classid class-id ] qdisc [ qdisc specific
  8. parameters ]
  9.  
  10. tc [ OPTIONS ] filter [ add | change | replace | delete | get ]
  11. dev DEV [ parent qdisc-id | root ] [ handle filter-id ] protocol
  12. protocol prio priority filtertype [ filtertype specific
  13. parameters ] flowid flow-id
  14.  
  15. tc [ OPTIONS ] filter [ add | change | replace | delete | get ]
  16. block BLOCK_INDEX [ handle filter-id ] protocol protocol prio
  17. priority filtertype [ filtertype specific parameters ] flowid
  18. flow-id
  19.  
  20. tc [ OPTIONS ] chain [ add | delete | get ] dev DEV [ parent
  21. qdisc-id | root ] filtertype [ filtertype specific parameters ]
  22.  
  23. tc [ OPTIONS ] chain [ add | delete | get ] block BLOCK_INDEX
  24. filtertype [ filtertype specific parameters ]
  25.  
  26. tc [ OPTIONS ] [ FORMAT ] qdisc { show | list } [ dev DEV ] [
  27. root | ingress | handle QHANDLE | parent CLASSID ] [ invisible ]
  28.  
  29. tc [ OPTIONS ] [ FORMAT ] class show dev DEV
  30.  
  31. tc [ OPTIONS ] filter show dev DEV
  32.  
  33. tc [ OPTIONS ] filter show block BLOCK_INDEX
  34.  
  35. tc [ OPTIONS ] chain show dev DEV
  36.  
  37. tc [ OPTIONS ] chain show block BLOCK_INDEX
  38.  
  39. tc [ OPTIONS ] monitor [ file FILENAME ]

tc 命令选项:


  1. -b, -b filename, -batch, -batch filename
  2. read commands from provided file or standard input and
  3. invoke them. First failure will cause termination of tc.
  4.  
  5. -force don't terminate tc on errors in batch mode. If there were
  6. any errors during execution of the commands, the
  7. application return code will be non zero.
  8.  
  9. -o, -oneline
  10. output each record on a single line, replacing line feeds
  11. with the '\' character. This is convenient when you want
  12. to count records with wc(1) or to grep(1) the output.
  13.  
  14. -n, -net, -netns <NETNS>
  15. switches tc to the specified network namespace NETNS.
  16. Actually it just simplifies executing of:
  17.  
  18. ip netns exec NETNS tc [ OPTIONS ] OBJECT { COMMAND | help
  19. }
  20.  
  21. to
  22.  
  23. tc -n[etns] NETNS [ OPTIONS ] OBJECT { COMMAND | help }
  24.  
  25. -N, -Numeric
  26. Print the number of protocol, scope, dsfield, etc directly
  27. instead of converting it to human readable name.
  28.  
  29. -cf, -conf <FILENAME>
  30. specifies path to the config file. This option is used in
  31. conjunction with other options (e.g. -nm).
  32.  
  33. -t, -timestamp
  34. When tc monitor runs, print timestamp before the event
  35. message in format:
  36. Timestamp: <Day> <Month> <DD> <hh:mm:ss> <YYYY> <usecs>
  37. usec
  38.  
  39. -ts, -tshort
  40. When tc monitor runs, prints short timestamp before the
  41. event message in format:
  42. [<YYYY>-<MM>-<DD>T<hh:mm:ss>.<ms>]

tc 命令实例:


tc 查看包 drop 情况:

  1. tc -s -d qd

tc 列出目前所有的 Network Interface 的设定:

  1. $ tc qdisc ls
  2.  
  3. qdisc noqueue 0: dev lo root refcnt 2
  4. qdisc pfifo_fast 0: dev enp0s5 root refcnt 2 bands 3 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1composer require user/package_name

tc-prio 是预设的队列规则,如果有绑定新的队列规则会直接覆盖过去。预设prio 会有三个band,而priomap 则表示对应IP 封包中的4bits TOS 栏位,将封包透过该band 发送。

需注意 enp0s5 是我自己的 Network Interface,记得替换成自己装置上的 Network Interface

tc 预设将所有的流量导向 band 2,为了避免其他封包被影响,先将所有的封包都走band 2(指令计数从0开始)

  1. $ sudo tc qdisc add dev enp0s5 root handle 1: prio bands 10 priomap 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
  2.  
  3. -------------------------------
  4. handle 1: 指的是绑定到qdisc root
  5. bands 10: 可以创建10band

tc 设定封包延迟:

  1. $ sudo tc qdisc add dev enp0s5 parent 1:1 handle 10: netem delay 100ms 10ms
  2.  
  3. ---------------------
  4. 1. netem tc 的工具之一,可用来增加延迟、掉封包、重复封包等模拟工具,netem delay 100ms 10ms表示每个封包延迟100ms(+- 10ms)
  5. 2. parent 1:1 表示在class id 1 底下,建立一个id 1的子节点,因为当前的队列没有多层次的class设计,所以1:1 就对应到band 0
  6. 3. handle 10: 表示创建一个class id 10 的节点

tc 带宽限制,测试限制aws s3 上传的带宽:

  1. 删除旧的qdisc 设定
  2. $ sudo tc qdisc del dev enp0s5 root
  3.  
  4. 建立 htb
  5. $ sudo tc qdisc add dev enp0s5 root handle 1:0 htb
  6.  
  7. 加入bandwidth 限制
  8. $ sudo tc class add dev enp0s5 parent 1: classid 1:1 htb rate 100kbps ceil 100kbps

比对 s3 上传的前后,发现上传速度,被限制在 100 kbps。如果单纯想要限制上传速度,也可以用 tc-tbf,但 htb 优点是用层状架构限速,leaf 会受到 root 的限制,例如分配一条带宽 10 Mbps,可以在之下分配 4 Mbps 给某网域 6 Mbps 给其他网域等。

tc 扩展阅读:




tc 命令评论