{"id":73,"date":"2011-08-11T10:15:51","date_gmt":"2011-08-11T02:15:51","guid":{"rendered":"http:\/\/www.sulabs.net\/?p=73"},"modified":"2017-01-09T10:33:32","modified_gmt":"2017-01-09T02:33:32","slug":"linux-%e4%b8%8a%e7%9a%84tc-%e6%b5%81%e9%87%8f%e6%8e%a7%e5%88%b6%e5%87%a0%e4%b8%aa%e4%be%8b%e5%ad%90","status":"publish","type":"post","link":"https:\/\/www.sulabs.net\/?p=73","title":{"rendered":"Linux \u4e0a\u7684TC \u6d41\u91cf\u63a7\u5236\u51e0\u4e2a\u4f8b\u5b50\uff0880\u7aef\u53e3\u6d41\u91cf\u9650\u5236\uff09"},"content":{"rendered":"<p>\u6587\u7ae0\u7ffb\u8bd1\u8f6c\u81ea<br \/>\n<a href=\"http:\/\/www.cyberciti.biz\/faq\/linux-traffic-shaping-using-tc-to-control-http-traffic\/\">http:\/\/www.cyberciti.biz\/faq\/linux-traffic-shaping-using-tc-to-control-http-traffic\/<\/a><\/p>\n<p>\u4e0d\u5c11\u4e2d\u5c0f\u4f01\u4e1a\u670d\u52a1\u5668\u63a5\u5165\u5e26\u5bbd\u5f80\u5f80\u53ea\u670910Mbps \u901a\u5e38\u670d\u52a1\u5668\u4e5f\u8fd0\u884c\u4e86\u5176\u5b83\u670d\u52a1\uff0c\u6bd4\u5982DNS\u670d\u52a1\uff0c\u5f53\u7f51\u7ad9\u8bbf\u95ee\u91cf\u8f83\u5927\u5e76\u4e14\u6709\u591a\u4eba\u540c\u65f6\u4e0b\u8f7d\u65f6\uff0c\u5e26\u5bbd\u5f88\u5bb9\u6613\u8017\u5c3d\uff0c\u5bfc\u81f4\u670d\u52a1\u5668\u5ef6\u8fdf\u589e\u52a0\u6216\u4e22\u5305\u3002\u5982\u4f55\u5bf980\u7aef\u53e3\u8fdb\u884c\u901f\u5ea6\u9650\u5236\uff0c\u800c\u4fdd\u969c\u5176\u5b83\u670d\u52a1\u80fd\u6b63\u5e38\u8fd0\u884c\uff1f<\/p>\n<div>\n<div><!--more--><\/div>\n<div>\u4f60\u53ef\u4ee5\u4f7f\u7528tc \u547d\u4ee4\uff0c\u5bf9\u670d\u52a1\u5668\u4e0a\u6307\u5b9a\u7684\u7aef\u53e3\u548c\u670d\u52a1\u5668\u8fdb\u884c\u8c03\u6574\u9650\u5236\u3002<\/div>\n<p>\u4ee4\u724c\u6876\u00a0(TB)<\/p>\n<p>\u4ee4\u724c\u6876\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u7b97\u6cd5\uff0c\u7528\u4e8e\u63a7\u5236\u8fdb\u5165\u5230\u7f51\u7edc\u7684\u6570\u636e\u91cf\uff0c\u5141\u8bb8\u5c06\u53d1\u9001\u6570\u636e\u7a81\u53d1\u3002\u5b83\u662f\u7528\u4e8e\u7f51\u7edc\u6d41\u91cf\u63a7\u5236\u6216\u901f\u7387\u9650\u5236\u3002\u968f\u7740\u4ee4\u724c\u6876\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u63a5\u53e3\u4e0a\u5141\u8bb8\u7684\u6700\u5927\u901f\u7387\u5728\u7ed9\u5b9a\u65f6\u95f4\u7684\u4ea4\u901a\u3002<\/p>\n<pre>                                      tokens\/sec\r\n                                   | \t\t|\r\n                                   |\t\t| Bucket to\r\n                                   |\t\t| to hold b tokens\r\n  \t                      \t   +======+=====+\r\n                                          |\r\n                                          |\r\n        |                                \\|\/\r\nPackets |      +============+\r\nstream  | ---&gt; | token wait | ---&gt;  Remove token  ---&gt;  eth0\r\n        |      +============+<\/pre>\n<ol>\n<li>The TB filter puts tokens into the bucket at a certain rate.<\/li>\n<li>Each token is permission for the source to send a specific number of bits into the network.<\/li>\n<li>Bucket can hold b tokens as per shaping rules.<\/li>\n<li>Kernel can send packet if you've a token else traffic need to wait.<\/li>\n<\/ol>\n<h2>\u6211\u5982\u4f55\u4f7f\u7528TC\u547d\u4ee4\uff1f<\/h2>\n<div><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-736\" src=\"https:\/\/www.sulabs.net\/wp-content\/uploads\/2017\/01\/warning-40px.png\" width=\"40\" height=\"33\" alt=\"\" title=\"Warning examples requires good understanding of TCP\/IP and other networking concepts\" \/>\u8b66\u544a\uff01\u8fd9\u4e9b\u4f8b\u5b50\uff0c\u9700\u8981\u5f88\u597d\u5730\u7406\u89e3TCP\/ IP\u548c\u5176\u5b83\u7f51\u7edc\u6982\u5ff5\u3002\u6240\u6709\u65b0\u7684\u7528\u6237\uff0c\u5e94\u5c3d\u91cf\u5728\u6d4b\u8bd5\u73af\u5883\u4e2d\u7684\u4f8b\u5b50\u3002<br \/>\nTC\u547d\u4ee4\u662f\u9ed8\u8ba4\u5b89\u88c5\u5728\u6211\u7684Linux\u53d1\u884c\u7248\u3002\u8981\u5217\u51fa\u73b0\u6709\u89c4\u5219\uff0c\u8bf7\u8f93\u5165\uff1a<strong><\/strong><\/div>\n<p><code># tc -s qdisc ls dev eth0<\/code><br \/>\n\u6837\u672c\u8f93\u51fa\uff1a<\/p>\n<pre>qdisc pfifo_fast 0: root bands 3 priomap  1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1\r\n Sent 2732108 bytes 10732 pkt (dropped 0, overlimits 0 requeues 0)\r\n rate 0bit 0pps backlog 0b 0p requeues 0<\/pre>\n<h3>\u4f60\u9996\u5148\u6d41\u91cf\u6574\u5f62\u89c4\u5219<br \/>\n\u9996\u5148\uff0c\u53d1\u9001ping\u8bf7\u6c42cyberciti.biz\u4f60\u672c\u5730\u7684Linux\u5de5\u4f5c\u7ad9\uff0c\u5e76\u6ce8\u610f\u4e0bping\u7684\u65f6\u95f4\uff0c\u8f93\u5165\uff1a<\/h3>\n<p><code># ping cyberciti.biz<\/code><br \/>\n\u6837\u672c\u8f93\u51fa\uff1a<\/p>\n<pre>PING cyberciti.biz (74.86.48.99) 56(84) bytes of data.\r\n64 bytes from txvip1.simplyguide.org (74.86.48.99): icmp_seq=1 ttl=47 time=304 ms\r\n64 bytes from txvip1.simplyguide.org (74.86.48.99): icmp_seq=2 ttl=47 time=304 ms\r\n64 bytes from txvip1.simplyguide.org (74.86.48.99): icmp_seq=3 ttl=47 time=304 ms\r\n64 bytes from txvip1.simplyguide.org (74.86.48.99): icmp_seq=4 ttl=47 time=304 ms\r\n64 bytes from txvip1.simplyguide.org (74.86.48.99): icmp_seq=5 ttl=47 time=304 ms\r\n64 bytes from txvip1.simplyguide.org (74.86.48.99): icmp_seq=6 ttl=47 time=304 ms<\/pre>\n<p>\u952e\u5165\u4e0b\u9762\u7684TC\u547d\u4ee4\u51cf\u6162200\u6beb\u79d2<\/p>\n<p><code># tc qdisc add dev eth0 root netem delay 200ms<\/code><br \/>\n\u73b0\u5728\uff0c\u518d\u6b21\u8fd0\u884cping \u8bf7\u6c42<br \/>\n<code># ping cyberciti.biz<\/code><br \/>\n\u8f93\u51fa\u6837\u672c:<\/p>\n<pre>PING cyberciti.biz (74.86.48.99) 56(84) bytes of data.\r\n64 bytes from txvip1.simplyguide.org (74.86.48.99): icmp_seq=1 ttl=47 time=505 ms\r\n64 bytes from txvip1.simplyguide.org (74.86.48.99): icmp_seq=2 ttl=47 time=505 ms\r\n64 bytes from txvip1.simplyguide.org (74.86.48.99): icmp_seq=3 ttl=47 time=505 ms\r\n64 bytes from txvip1.simplyguide.org (74.86.48.99): icmp_seq=4 ttl=47 time=505 ms\r\n64 bytes from txvip1.simplyguide.org (74.86.48.99): icmp_seq=5 ttl=47 time=505 ms\r\n64 bytes from txvip1.simplyguide.org (74.86.48.99): icmp_seq=6 ttl=47 time=505 ms\r\n64 bytes from txvip1.simplyguide.org (74.86.48.99): icmp_seq=7 ttl=47 time=505 ms\r\n64 bytes from txvip1.simplyguide.org (74.86.48.99): icmp_seq=8 ttl=47 time=505 ms\r\n^C\r\n--- cyberciti.biz ping statistics ---\r\n8 packets transmitted, 8 received, 0% packet loss, time 7006ms\r\nrtt min\/avg\/max\/mdev = 504.464\/505.303\/506.308\/0.949 ms<\/pre>\n<p>\u8981\u5217\u51fa\u5f53\u524d\u89c4\u5219\uff0c\u8bf7\u8f93\u5165<br \/>\n<code># tc -s qdisc ls dev eth0<\/code><br \/>\n\u8f93\u51fa\u6837\u672c:<\/p>\n<pre>qdisc netem 8001: root limit 1000 delay 200.0ms\r\n Sent 175545 bytes 540 pkt (dropped 0, overlimits 0 requeues 0)\r\n rate 0bit 0pps backlog 0b 0p requeues 0<\/pre>\n<p>\u60f3\u5220\u9664\u5168\u90e8\u89c4\u5219\uff0c\u8bf7\u8f93\u5165<br \/>\n<code># tc qdisc del dev eth0 root<br \/>\n# tc -s qdisc ls dev eth0<\/code><\/p>\n<h3>TBF\u7684\u8303\u4f8b<\/h3>\n<p>\u8981\u9644\u52a0\u4e00\u4e2a\u6301\u7eed\u7684\u6700\u5927\u901f\u73871Mbit \/ s\u7684\uff0cpeakrate2.0mbit\/ S\uff0c10kilobyte\u7f13\u51b2\u533a\u4e00\u4e2aTBF\u7684\u8ba1\u7b97\uff0c\u4f7fTBF\u7684\u539f\u56e0\u6700\u591a70ms\u7684\u5ef6\u8fdf\uff0c\u4e0e\u5b8c\u7f8e\u7684peakrate\u884c\u4e3a\uff0c\u4e0e\u524d\u6597\u961f\u5217\u5927\u5c0f\u9650\u5236\uff0c\uff0c\u8f93\u5165\uff1a<\/p>\n<p>&nbsp;<\/p>\n<p><code># tc qdisc add dev eth0 root tbf rate 1mbit burst 10kb latency 70ms peakrate 2mbit minburst 1540<\/code><\/p>\n<p>HTB - \u5c42\u6b21\u4ee4\u724c\u6876<\/p>\n<p>\u8981\u63a7\u5236\u5728\u4e00\u4e2a\u7ed9\u5b9a\u7684\u94fe\u63a5\u4f7f\u7528HTB\u7684\u51fa\u53e3\u5e26\u5bbd\u7684\u4f7f\u7528\uff1a<br \/>\n1.rate - \u60a8\u53ef\u4ee5\u8bbe\u7f6e\u5141\u8bb8\u7684\u5e26\u5bbd\u3002<br \/>\n2.ceil - \u60a8\u53ef\u4ee5\u8bbe\u7f6e\u7684\u7a81\u53d1\u5e26\u5bbd\u65f6\uff0c\u5141\u8bb8\u6876\u3002<br \/>\n3.prio - \u60a8\u53ef\u4ee5\u8bbe\u7f6e\u989d\u5916\u7684\u5e26\u5bbd\u4f18\u5148\u7ea7\u3002 prios\u8f83\u4f4e\u7684\u7c7b\u6240\u63d0\u4f9b\u7684\u5e26\u5bbd\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u7ed9DNS\u6d41\u91cf\u548cHTTP\u4e0b\u8f7dPRIO\u8f83\u4f4e\u3002<br \/>\n4.iptables\u548cTC\uff1a\u4f60\u9700\u8981\u4f7f\u7528iptables\u548cTC\u5982\u4e0b\uff1a\u63a7\u5236\u51fa\u7ad9HTTP\u6d41\u91cf\u3002<\/p>\n<h3>\u793a\u4f8b: HTTP \u51fa\u7ad9\u6d41\u91cf\u6574\u5f62<\/h3>\n<p>\u9996\u5148\uff0c\u5220\u9664eth1 \u7f51\u5361\u4e0a\u5df2\u5b58\u5728\u7684\u89c4\u5219:<br \/>\n<code># \/sbin\/tc qdisc del dev eth1 root<\/code><br \/>\n\u6253\u5f00\u961f\u5217\u89c4\u5219,\u8f93\u5165:<br \/>\n<code># \/sbin\/tc qdisc add dev eth1 root handle 1:0 htb default 10<\/code><br \/>\n\u5b9a\u4e49\u7c7b\u7684\u9650\u5236\uff0c\u5373\u5141\u8bb8\u7684\u5e26\u5bbd\u4e3a512\u5343\u5b57\u8282\u5230640\u5343\u5b57\u8282\u7684\u7aef\u53e380\u548c\u7a81\u53d1\u5e26\u5bbd\uff1a<\/p>\n<p><code># \/sbin\/tc class add dev eth1 parent 1:0 classid 1:10 htb rate 512kbps ceil 640kbps prio 0<\/code><br \/>\n\u8bf7\u6ce8\u610f\uff0c\u7aef\u53e380\u662f\u4e0d\u662f\u5728\u4e0a\u9762\u7684\u7c7b\u4e2d\u5b9a\u4e49\u7684\u4efb\u4f55\u5730\u65b9\u3002\u60a8\u5c06\u4f7f\u7528iptables\u88c2\u4f24\u89c4\u5219\u5982\u4e0b\uff1a<\/p>\n<p><code># \/sbin\/iptables -A OUTPUT -t mangle -p tcp --sport 80 -j MARK --set-mark 10<\/code><br \/>\n\u8981\u4fdd\u5b58iptables \u89c4\u5219\uff0c\u8bf7\u8f93\u5165\u4e0b\u5217\u547d\u4ee4\uff0c(RHEL \u4e3a\u4f8b):<br \/>\n<code># \/sbin\/service iptables save<\/code><br \/>\n\u6700\u540e\uff0c\u5c06\u5176\u5206\u914d\u7ed9 qdisc:<br \/>\n<code># tc filter add dev eth1 parent 1:0 prio 0 protocol ip handle 10 fw flowid 1:10<\/code><br \/>\n\u4e0b\u9762\u662f\u7aef\u53e380\u548c22\u7684\u53e6\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<p><code>\/sbin\/tc qdisc add dev eth0 root handle 1: htb<br \/>\n\/sbin\/tc class add dev eth0 parent 1: classid 1:1 htb rate 1024kbps<br \/>\n\/sbin\/tc class add dev eth0 parent 1:1 classid 1:5 htb rate 512kbps ceil 640kbps prio 1<br \/>\n\/sbin\/tc class add dev eth0 parent 1:1 classid 1:6 htb rate 100kbps ceil 160kbps prio 0<br \/>\n\/sbin\/tc filter add dev eth0 parent 1:0 prio 1 protocol ip handle 5 fw flowid 1:5<br \/>\n\/sbin\/tc filter add dev eth0 parent 1:0 prio 0 protocol ip handle 6 fw flowid 1:6<br \/>\n\/sbin\/iptables -A OUTPUT -t mangle -p tcp --sport 80 -j MARK --set-mark 5<br \/>\n\/sbin\/iptables -A OUTPUT -t mangle -p tcp --sport 22 -j MARK --set-mark 6<\/code><\/p>\n<h2>\u6211\u5982\u4f55\u76d1\u63a7\u548c\u6d4b\u8bd5\u901f\u5ea6\u5462\uff1f<\/h2>\n<p>\u4f7f\u7528\u4e00\u4e0b\u5de5\u5177<\/p>\n<p><code># \/sbin\/tc -s -d class show dev eth0<br \/>\n# \/sbin\/iptables -t mangle -n -v -L<br \/>\n# iptraf<br \/>\n# watch \/sbin\/tc -s -d class show dev eth0<\/code><br \/>\n\u8981\u6d4b\u8bd5\u4e0b\u8f7d\u901f\u5ea6\u4f7f\u7528LFTP\u6216\u8005wget\u547d\u4ee4\u884c\u5de5\u5177\u3002<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u7ffb\u8bd1\u8f6c\u81ea http:\/\/www.cyberciti.biz\/faq\/linu &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/www.sulabs.net\/?p=73\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":736,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[43],"class_list":["post-73","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","tag-tc"],"_links":{"self":[{"href":"https:\/\/www.sulabs.net\/index.php?rest_route=\/wp\/v2\/posts\/73","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sulabs.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sulabs.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sulabs.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sulabs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=73"}],"version-history":[{"count":6,"href":"https:\/\/www.sulabs.net\/index.php?rest_route=\/wp\/v2\/posts\/73\/revisions"}],"predecessor-version":[{"id":737,"href":"https:\/\/www.sulabs.net\/index.php?rest_route=\/wp\/v2\/posts\/73\/revisions\/737"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sulabs.net\/index.php?rest_route=\/wp\/v2\/media\/736"}],"wp:attachment":[{"href":"https:\/\/www.sulabs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sulabs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sulabs.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}