{"id":44,"date":"2014-04-06T16:56:23","date_gmt":"2014-04-06T08:56:23","guid":{"rendered":"http:\/\/blog.qiaogen.com\/?p=44"},"modified":"2014-04-06T16:56:23","modified_gmt":"2014-04-06T08:56:23","slug":"linux%e4%b8%8b%e4%bd%bf%e7%94%a8iptables%e5%92%8ctc%e9%99%90%e5%88%b6%e6%b5%81%e9%87%8f","status":"publish","type":"post","link":"https:\/\/www.sangqiao.com\/blog\/archives\/44","title":{"rendered":"linux\u4e0b\u4f7f\u7528iptables\u548ctc\u9650\u5236\u6d41\u91cf"},"content":{"rendered":"<p><a href=\"http:\/\/outofmemory.cn\/code-snippet\/90\/linux-usage-iptables-tc-limited-liuliang\">http:\/\/outofmemory.cn\/code-snippet\/90\/linux-usage-iptables-tc-limited-liuliang<\/a><\/p>\n<p>\u6700\u8fd1\u6211\u8fd9\u8fb9\u539f\u6765\u7684\u90a3\u4e2a\u8001\u7684\u8def\u7531\u5668\u51fa\u4e86\u70b9\u95ee\u9898\uff0c\u7ecf\u5e38\u62bd\u98ce\u3002\u6b63\u597d\u56e0\u4e3a\u4ee5\u524d\u5916\u7f51\u67096\u4e2aip\uff0c\u6240\u4ee5\u5f53\u65f6\u628a\u5916\u7f51\u7ebf\u76f4\u63a5\u63a5\u5728\u4e86\u4ea4\u6362\u673a\u4e0a\uff0c\u90a3\u4e48\u5c31\u60f3\u7740\u7528\u865a\u62df\u673a\u81ea\u5df1\u642d\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u8def\u7531\u5668\u7528\u7528\u3002\u73b0\u5728\u670d\u52a1\u5668\u8d44\u6e90\u8fc7\u5269\uff0c\u6709\u4ec0\u4e48\u65b9\u6848\u5c31\u5728\u4e0a\u9762\u5efa\u865a\u62df\u673a\u8bd5\u9a8c\u3002\u53bb\u7f51\u4e0a\u627e\u4e86\u5f88\u591a\u8d44\u6599\u7136\u540e\u81ea\u5df1\u6478\u7d22\u7740\u5728ubuntu\u4e0a\u642d\u4e86\u4e00\u4e2a\u8def\u7531\u5668\u3002<\/p>\n<h2>\u914d\u7f6e\u7f51\u5361<\/h2>\n<p>\u5efa\u7acb\u4e00\u53f0\u865a\u62df\u673a\uff0c\u5e76\u5b89\u88c5\u5b8c\u6210\u540e\u4ee5\u6865\u63a5\u7684\u65b9\u5f0f\u5728\u865a\u62df\u673a\u4e0a\u9762\u6dfb\u52a0\u4e24\u5f20\u7f51\u5361\u3002\u5206\u522b\u4e3aeth0\u548ceth1\u3002<\/p>\n<pre>eth0: a.b.c.d(\u5916\u7f51\u7684\u4e0a\u7f51\u5730\u5740)\r\neth1: 172.16.44.1(\u505a\u4e3a\u5185\u7f51\u7684\u7f51\u5173)<\/pre>\n<blockquote><p>Tip<\/p>\n<p>\u539f\u5148\u6211\u4f7f\u7528eth0:0\u7684\u8fd9\u79cd\u865a\u62df\u7f51\u5361\u7684\u5f62\u5f0f\u53bb\u914d\u7f6e\u4e00\u76f4\u4e0d\u6210\u529f\uff0c\u540e\u6765\u4f7f\u7528\u53cc\u7f51\u5361\u7684\u65f6\u5019\u4e00\u76f4\u5fd8\u4e86\u628aeth0:0\u8fd9\u5f20\u865a\u62df\u7f51\u5361\u5220\u6389\u5bfc\u81f4\u4e86\u9650\u901f\u914d\u7f6e\u4e00\u76f4\u4e0d\u6210\u529f\uff0c\u6d6a\u8d39\u4e86\u5927\u628a\u7684\u9752\u8471\u3002<\/p><\/blockquote>\n<h2>\u914d\u7f6eiptables nat<\/h2>\n<pre>#\u5f00\u542fip_forward\r\necho \"1\"&gt;\/proc\/sys\/net\/ipv4\/ip_forward\r\n\r\n#\u6e05\u9664\u539f\u6765\u7684\u9632\u706b\u5899\u89c4\u5219\r\niptables -F\r\niptables -t nat -F\r\niptables -t mangle -F\r\n\r\n#\u6dfb\u52a0nat\u8f6c\u53d1\r\niptables -t nat -A POSTROUTING -s 172.16.44.0\/24 -o eth0 -j MASQUERADE<\/pre>\n<p>\u901a\u8fc7\u6267\u884c\u4e0a\u9762\u7684\u4ee3\u7801\u540e\uff0c\u5c40\u57df\u7f51\u5185\u7684\u7535\u8111\u5c31\u53ef\u4ee5\u4e0a\u7f51\u4e86\u3002<\/p>\n<h2>\u7aef\u53e3\u8f6c\u53d1<\/h2>\n<p>\u7531\u4e8e\u6211\u7684\u5185\u7f51\u8fd8\u6302\u4e86\u7f51\u7ad9\uff0c\u6240\u4ee5\u8981\u5f00\u542f80\u7aef\u53e3\u7684\u8f6c\u53d1\u3002<\/p>\n<pre>iptables -t nat -I PREROUTING -p tcp -d a.b.c.d --dport 80 -j DNAT --to 172.16.44.210:80\r\niptables -t nat -I POSTROUTING -p tcp -d 172.16.44.210 --dport 80 -j SNAT --to 172.16.44.1<\/pre>\n<p>\u8fd9\u6761\u547d\u4ee4\u6307\u5b9a\u5916\u7f51\u5730\u5740a.b.c.d\u768480\u7aef\u53e3\u8f6c\u53d1\u5230172.16.44.210:80\u4e0a\u3002\u7531\u4e8e\u662f\u53cc\u7f51\u5361\uff0c\u6240\u4ee5\u9700\u8981\u505a\u4e00\u4e0b\u56de\u8def\u3002<\/p>\n<h2>\u4e0b\u8f7d\u9650\u901f<\/h2>\n<p>\u4e0b\u8f7d\u9650\u901f\u8981\u5728eth1\u4e0a\u9762\u505a\uff0c\u5224\u65ad\u6570\u636e\u5305\u7684\u76ee\u7684\u5730\u5740\u6765\u505a\u9650\u5236\u3002tc\u5305\u62ec\u4e09\u90e8\u5206\uff1a\u961f\u5217\u3001\u7c7b\u3001\u8fc7\u6ee4\u5668\u3002\u6211\u4f7f\u7528\u4e86htb\u65b9\u5f0f\u53bb\u9650\u5236\u901f\u5ea6\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528cbq\uff0c\u4f46cbq\u914d\u7f6e\u6bd4\u8f83\u590d\u6742\u4e00\u70b9\uff0c\u800c\u4e14\u636e\u8bf4\u6027\u80fd\u6ca1htb\u597d\u3002<\/p>\n<pre>#\u5220\u9664\u539f\u6765\u7684tc\u89c4\u5219\u961f\u5217\r\ntc qdisc del dev eth1 root\r\n#\u6dfb\u52a0tc\u89c4\u5219\u961f\u5217\r\ntc qdisc add dev eth1 root handle 10: htb default 256\r\n#\u751f\u6210\u6839\u7c7b\r\ntc class add dev eth1 parent 10: classid 10:1 htb rate 100mbit ceil 100mbit\r\n#\u652f\u7c7b\u5217\u8868\u7528\u4e8e\u9650\u5236\u901f\u5ea6\r\n#\u8fd9\u91cc\u7684rate\u6307\u7684\u662f\u4fdd\u8bc1\u5e26\u5bbd,ceil\u662f\u6700\u5927\u5e26\u5bbd\u3002\r\ntc class add dev eth1 parent 10:1 classid 10:10 htb rate 400kbps ceil 400kbps prio 1\r\n\r\n#\u6dfb\u52a0\u652f\u7c7b\u89c4\u5219\u961f\u5217\r\n#\u91c7\u7528sfq\u4f2a\u968f\u673a\u961f\u5217\uff0c\u5e76\u4e1410\u79d2\u91cd\u7f6e\u4e00\u6b21\u6563\u5217\u51fd\u6570\u3002\r\ntc qdisc add dev eth1 parent 10:10 handle 101: sfq perturb 10\r\n\r\n#\u5efa\u7acb\u7f51\u7edc\u5305\u8fc7\u6ee4\u5668\uff0c\u8bbe\u7f6efw\u3002\r\ntc filter add dev eth1 parent 10: protocol ip prio 10 handle 1 fw classid 10:10\r\n#\u5728iptables\u91cc\u9762\u8bbe\u5b9amark\u503c\uff0c\u4e0e\u4e0a\u9762\u7684handle\u503c\u5bf9\u5e94\u3002\r\niptables -t mangle -A POSTROUTING -d 172.16.44.130 -j MARK --set-mark 1<\/pre>\n<p>\u901a\u8fc7\u4e0a\u9762\u7684\u4ee3\u7801\u5c31\u53ef\u4ee5\u9650\u5236172.16.44.130\u8fd9\u53f0\u673a\u5b50\u7684\u4e0b\u8f7d\u901f\u5ea6\u5230400kbps\u3002<\/p>\n<blockquote><p>Tip<\/p>\n<p>\u7ecf\u8fc7\u5b9e\u9645\u6d4b\u8bd5\u8fd9\u91cc\u7684kbps\u5b9e\u9645\u4e0a\u5c31\u662fKB\/S\u6bcf\u79d2\u5343\u5b57\u8282\u3002\u53e6\u4e00\u4e2a\u5355\u4f4d\u662fkbit\uff0c\u8fd9\u4e2a\u624d\u662f\u6bcf\u79d2\u5343\u6bd4\u7279\u3002\u8fd9\u91cc\u7684172.16.44.130\u4e5f\u53ef\u4ee5\u5199\u6210\u4e00\u4e2a\u7f51\u6bb5\uff0c\u6bd4\u5982\uff1a172.16.44.0\/24<\/p><\/blockquote>\n<h2>\u4e0a\u4f20\u9650\u901f<\/h2>\n<p>\u4e0a\u4f20\u9650\u901f\u7684\u539f\u7406\u5176\u5b9e\u8ddf\u4e0b\u8f7d\u7684\u5dee\u4e0d\u591a\uff0c\u53ea\u4e0d\u8fc7\u9650\u5236\u7684\u7f51\u5361\u4e0d\u540c\uff0c\u8981\u5728eth0\u4e0a\u8fc7\u6ee4\u6765\u6e90\u5730\u5740\u53bb\u9650\u5236\u3002<\/p>\n<pre>#\u5220\u9664\u539f\u6765\u7684tc\u89c4\u5219\u961f\u5217\r\ntc qdisc del dev eth0 root\r\n\r\n#\u6dfb\u52a0tc\u89c4\u5219\u961f\u5217\r\ntc qdisc add dev eth0 root handle 20: htb default 256\r\n\r\n#\u751f\u6210\u6839\u7c7b\r\ntc class add dev eth0 parent 20: classid 20:1 htb rate 100mbit ceil 100mbit\r\n\r\n#\u652f\u7c7b\u5217\u8868\u7528\u4e8e\u9650\u5236\u901f\u5ea6\r\ntc class add dev eth0 parent 20:1 classid 20:10 htb rate 40kbps ceil 40kbps prio 1\r\n\r\n#\u6dfb\u52a0\u652f\u7c7b\u89c4\u5219\u961f\u5217\r\ntc qdisc add dev eth0 parent 20:10 handle 201: sfq perturb 10\r\n\r\n#\u5efa\u7acb\u7f51\u7edc\u5305\u8fc7\u6ee4\u5668\r\ntc filter add dev eth0 parent 20: protocol ip prio 100 handle 2 fw classid 20:10\r\niptables -t mangle -A PREROUTING -s 172.16.44.130 -j MARK --set-mark 2<\/pre>\n<blockquote><p>Tip<\/p>\n<p>\u8ddf\u4e0b\u8f7d\u4e0d\u540c\u7684\u662fPOSTROUTING\u8981\u6539\u6210PREROUTING\uff0c-d\u6539\u6210-s\u3002<\/p><\/blockquote>\n<h2>\u89c2\u5bdf\u8fde\u63a5\u6570<\/h2>\n<p>\u901a\u8fc7iptables\u7684nat\u8fde\u63a5\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u4ee3\u7801\u67e5\u770b\u3002\u81f3\u4e8e\u7edf\u8ba1\u8fde\u63a5\u6570\u53ef\u4ee5\u5199\u4ee3\u7801\u5b9e\u73b0\uff0c\u4e5f\u53ef\u4ee5\u5229\u7528awk,grep\u7b49\u5de5\u5177\u3002\u53cd\u6b63\u91cc\u9762\u7684\u5185\u5bb9\u5c31\u662f\u6587\u672c\uff0c\u5904\u7406\u8d77\u6765\u4e5f\u6bd4\u8f83\u7b80\u5355\u3002<\/p>\n<pre>cat \/proc\/net\/ip_conntrack<\/pre>\n<h2>\u5199\u5728\u7ed3\u5c3e<\/h2>\n<p>\u5230\u6b64\u4e0a\u7f51\u3001\u7aef\u53e3\u8f6c\u53d1\u548c\u6d41\u91cf\u9650\u5236\u90fd\u5df2\u7ecf\u5b9e\u73b0\u3002\u4e0b\u6b21\u518d\u8003\u8651\u914d\u7f6e\u4e2adhcp server\u548cdnsmasq\u3002\u81f3\u4e8e\u4e00\u4e9b\u8def\u7531\u5668\u5176\u5b83\u8bf8\u5982mac\u5730\u5740\u7ed1\u5b9a\uff0c\u9650\u5236\u4e0a\u7f51\u7b49\u7528\u5230\u7684\u65f6\u5019\u518d\u53bb\u7814\u7a76\u7814\u7a76\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/outofmemory.cn\/code-snippet\/90\/linux-usage-iptab [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.sangqiao.com\/blog\/wp-json\/wp\/v2\/posts\/44"}],"collection":[{"href":"https:\/\/www.sangqiao.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sangqiao.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sangqiao.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sangqiao.com\/blog\/wp-json\/wp\/v2\/comments?post=44"}],"version-history":[{"count":1,"href":"https:\/\/www.sangqiao.com\/blog\/wp-json\/wp\/v2\/posts\/44\/revisions"}],"predecessor-version":[{"id":45,"href":"https:\/\/www.sangqiao.com\/blog\/wp-json\/wp\/v2\/posts\/44\/revisions\/45"}],"wp:attachment":[{"href":"https:\/\/www.sangqiao.com\/blog\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sangqiao.com\/blog\/wp-json\/wp\/v2\/categories?post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sangqiao.com\/blog\/wp-json\/wp\/v2\/tags?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}