{"id":430,"date":"2024-11-23T11:37:13","date_gmt":"2024-11-23T03:37:13","guid":{"rendered":"https:\/\/cococat.top\/?p=430"},"modified":"2024-11-23T11:37:13","modified_gmt":"2024-11-23T03:37:13","slug":"tcp-note","status":"publish","type":"post","link":"https:\/\/cococat.top\/index.php\/2024\/11\/23\/tcp-note\/","title":{"rendered":"TCP \u6742\u8bb0"},"content":{"rendered":"<h2>1. TCP_NODELAY \u548c TCP_QUICKACK<\/h2>\n<h3>1.1. Nagle's Algorithm<\/h3>\n<p>\u73b0\u4ee3\u7684 Linux kernel \u7684\u7f51\u7edc\u534f\u8bae\u6808\u4e2d\u9ed8\u8ba4\u542f\u7528\u7684\u7b97\u6cd5\uff0c\u4e5f\u662f\u7c98\u5305\u4ea7\u751f\u7684\u539f\u56e0\u4e4b\u4e00\u3002<br \/>\n\u6839\u636e<a href=\"https:\/\/www.ietf.org\/archive\/id\/draft-minshall-nagle-01.txt\">\u7b97\u6cd5\u6587\u6863<\/a>\uff0c\u539f\u7248\u7684 Nagle \u7b97\u6cd5\u53ef\u4ee5\u7b80\u8981\u8868\u8fbe\u4e3a\uff1a<\/p>\n<pre><code>If a TCP has less than a full-sized packet to transmit,\nand if any previous packet has not yet been acknowledged,\ndo not transmit a packet<\/code><\/pre>\n<p>\u4f2a\u7801\u5f62\u5f0f\u662f\uff1a<\/p>\n<pre><code>if ((packet.size &lt; Eff.snd.MSS) &amp;&amp; (snd.nxt &gt; snd.una)) {\n    do not send the packet;\n}<\/code><\/pre>\n<p>\u4f2a\u7801\u4e2d\u7684 MSS \u987e\u540d\u601d\u4e49\uff0c<code>snd.nxt<\/code> \u6307\u4e0b\u4e00\u4e2a\u9700\u8981\u53d1\u9001\u7684 TCP \u62a5\u6587\u7684\u5e8f\u5217\u53f7\uff1b<code>snd.una<\/code> \u6307\u4e0b\u4e00\u4e2a\u9700\u8981ACK \u7684 TCP \u62a5\u6587\u5e8f\u5217\u53f7\u3002\u5982\u679c <code>snd.nxt<\/code> \u7b49\u4e8e <code>snd.una<\/code>\uff0c\u5219\u8bf4\u660e\u6240\u6709\u53d1\u51fa\u7684\u62a5\u6587\u90fd\u5df2\u7ecf\u88ab ACK \u4e86\u3002<\/p>\n<p>\u4ece\u8fd9\u4e2a\u5173\u7cfb\u4e2d\u53ef\u5f97\u77e5\uff1a<\/p>\n<ul>\n<li>\u63e1\u624b\u5b8c\u6210\u540e\u7684\u7b2c\u4e00\u4e2a\u5305\uff0c\u4e0d\u8bba\u5927\u5c0f\u90fd\u4f1a\u88ab\u7acb\u523b\u53d1\u9001\uff0c\u56e0\u4e3a\u5e76\u6ca1\u6709\u9700\u8981\u7b49\u5f85 ACK \u7684\u5305\uff1b<\/li>\n<li>\u6b64\u540e\uff0c\u6240\u6709\u7684\u5e94\u7528\u5c42\u6570\u636e\uff0c\u5982\u679c\u7531\u4e8e\u8f83\u5927\u800c\u51fa\u73b0\u62c6\u5305\u3001\u4e14\u5176\u4e2d\u6700\u540e\u4e00\u4e2a\u5305\u5c0f\u4e8e MSS \u7684\u8bdd\uff0c\u5219\u8fd9\u6700\u540e\u4e00\u4e2a\u5305\u9700\u8981\u7b49\u5f85\u524d\u9762\u5df2\u7ecf\u53d1\u51fa\u7684\u6240\u6709\u5305\u90fd\u88ab ACK \u4ee5\u540e\uff0c\u624d\u4f1a\u88ab\u53d1\u51fa\u3002<\/li>\n<\/ul>\n<p>\u6b64\u5916\u8fd8\u6709\uff1a<\/p>\n<ul>\n<li>\u5f53\u5c1a\u5b58\u5728\u672a\u88ab ACK \u7684\u524d\u5e8f\u5305\u65f6\uff0c\u5982\u679c\u5e94\u7528\u5c42\u8fde\u7eed\u53d1\u51fa\u8bb8\u591a\u5c0f\u6570\u636e\uff0c\u5219\u76f4\u5230\u5b83\u4eec\u80fd\u591f\u586b\u6ee1\u4e00\u4e2a MSS \u4e4b\u524d\uff0c\u90fd\u4f1a\u88ab\u653e\u5728\u7f13\u51b2\u533a\u91cc\u7f13\u5b58\uff0c\u5e76\u4e0d\u4f1a\u53d1\u51fa\u3002<\/li>\n<\/ul>\n<h3>1.2 Nagle \u7b97\u6cd5\u7684\u8bbe\u8ba1\u80cc\u666f\u4e0e\u521d\u8877<\/h3>\n<p>TLDR\uff1aNagle \u7b97\u6cd5\u7684\u672c\u610f\u662f\u5c3d\u91cf\u51cf\u5c11\u5c0f\u5305\u6570\u91cf\uff0c\u4ece\u800c\u51cf\u5c11\u5e26\u5bbd\u6d6a\u8d39\u3001\u907f\u514d\u7f51\u7edc\u62e5\u585e\u3002<\/p>\n<p>\u8be6\u7ec6\u6765\u8bb2\uff0c\u4e3e\u4e2a\ud83c\udf30\uff1a\u5728\u901a\u8fc7 telnet \u4f20\u8f93\u952e\u76d8\u64cd\u4f5c\u7684\u4fe1\u4ee4\u65f6\uff0c\u6bcf\u6b21\u6572\u51fb\u952e\u76d8\u5f80\u5f80\u53ea\u4f1a\u4ea7\u751f\u53ea\u6709 1 \u4e2a\u5b57\u8282\u7684\u5e94\u7528\u5c42\u6570\u636e\u3002\u5982\u679c\u6ca1\u6709 Nagle \u7b97\u6cd5\uff0c\u5219\u5f53\u6211\u4eec\u64cd\u4f5c telnet \u65f6\uff0c\u4f1a\u5728\u77ed\u65f6\u95f4\u5185\u5927\u91cf\u751f\u6210 payload \u53ea\u6709 1 \u4e2a\u5b57\u8282\u7684\u5c0f\u5305\uff0c\u5e76\u5168\u90fd\u53d1\u9001\u51fa\u53bb\u3002<\/p>\n<p>\u8fd9\u4f1a\u5e26\u6765\u4e24\u4e2a\u95ee\u9898\uff1a<\/p>\n<ol>\n<li>\n<p>\u5bf9\u4e8e\u8fd9\u4e9b\u4f20\u8f93\u4fe1\u4ee4\u7684\u5305\uff0c\u5149\u662f TCP header\uff0820 \u5b57\u8282\uff09 + \u7f51\u7edc\u5c42 header\uff0820\/40 \u5b57\u8282\uff09\u5c31\u662f\u5176\u672c\u8eab payload \u7684 40 \u500d\u3002\u5360\u7528\u5e26\u5bbd\u7684\u7edd\u5927\u90e8\u5206\u90fd\u7528\u6765\u4f20\u8f93 header \u4e86\uff0c\u9020\u6210\u5de8\u5927\u7684\u6d6a\u8d39\u3002<\/p>\n<\/li>\n<li>\n<p>\u5bf9\u4e8e\u65e9\u671f\u4e92\u8054\u7f51\u7684\u4f4e\u901f\u7f51\u7edc\uff0c\u77ed\u65f6\u95f4\u5185\u5927\u91cf\u751f\u6210\u5c0f\u5305\u5e76\u5168\u90fd\u53d1\u9001\u51fa\u53bb\u7684\u884c\u4e3a\u5bb9\u6613\u9020\u6210\u7f51\u7edc\u62e5\u585e\u3002<\/p>\n<\/li>\n<\/ol>\n<h3>1.3 \u4e0e\u5ef6\u8fdf\u786e\u8ba4(Delayed ACK)\u7684\u76f8\u4e92\u4f5c\u7528<\/h3>\n<p>\u5ef6\u8fdf\u786e\u8ba4 (Delayed ACK) \u6280\u672f\u662f\u53e6\u4e00\u79cd TCP \u4f18\u5316\u65b9\u6848\uff0c\u5176\u53d1\u660e\u65f6\u95f4\u4e0e\u5728 Nagle \u7b97\u6cd5\u51e0\u4e4e\u76f8\u540c\u65f6\u95f4\u3002\u5b83\u8bbe\u8ba1\u7684\u601d\u8def\u5f88\u7b80\u5355\uff1a\u5229\u7528 ACK \u7684\u7d2f\u8ba1\u786e\u8ba4\u7279\u6027\uff0c\u9002\u5f53\u5730\u5ef6\u540e ACK \u62a5\u6587\u7684\u53d1\u9001\uff0c\u4ece\u800c\u80fd\u7528\u4e00\u4e2a\u7d2f\u8ba1\u7684 ACK \u66ff\u4ee3\u591a\u4e2a\u5355\u72ec\u7684 ACK\uff0c\u51cf\u5c11\u6d6a\u8d39\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u4e8c\u8005\u540c\u65f6\u88ab\u5f15\u5165 TCP \u534f\u8bae\u540e\uff0c\u5c31\u9020\u6210\u4e00\u4e2a\u6076\u6027\u7684\u95ee\u9898\u3002\u5982\u679c\u4e0b\u9762\u4e09\u4e2a\u6761\u4ef6\u540c\u65f6\u6ee1\u8db3\uff1a<\/p>\n<ol>\n<li>\u53d1\u9001\u65b9\u5f00\u542f Nagle \u7b97\u6cd5<\/li>\n<li>\u63a5\u53d7\u65b9\u5f00\u542f Delayed ACK<\/li>\n<li>\u53d1\u9001\u7684\u6570\u636e\u51fa\u73b0\u4e86\u62c6\u5305\u3001\u4e14\u6700\u540e\u4e00\u4e2a\u5305\u5c0f\u4e8e MSS<\/li>\n<\/ol>\n<p>\u5219\u4f1a\u51fa\u73b0\u4e00\u4e2a\u76f8\u5f53\u7cdf\u7cd5\u7684\u60c5\u51b5\uff1a\u53d1\u9001\u65b9\u5728\u7b49\u5f85\u63a5\u6536\u65b9\u53d1\u6765\u4e0a\u4e2a\u5305\u7684 ACK \uff0c\u5728\u6536\u5230\u4e4b\u524d\u5e76\u4e0d\u4f1a\u53d1\u9001\u6700\u540e\u7684\u5c0f\u5305\uff1b\u4f46\u63a5\u6536\u65b9\u53c8\u5ef6\u8fdf\u4e86 ACK \u7684\u53d1\u9001\uff0c\u8fd9\u6837\u4e00\u6765\uff0c\u4e24\u8fb9\u5c31\u540c\u65f6\u9677\u5165\u4e86\u505c\u6ede\u72b6\u6001\uff0c\u76f4\u5230 TCP Delayed ACK \u7684\u8d85\u65f6\u88ab\u89e6\u53d1\uff08\u9ed8\u8ba4 40ms\uff09\u3002<\/p>\n<h3>1.4 \u89e3\u51b3\u65b9\u6848<\/h3>\n<p>\u73b0\u4ee3\u7684 Linux \u5f15\u5165\u4e86\u4e24\u4e2a TCP options \u6765\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\uff1a<\/p>\n<ul>\n<li><code>TCP_NODELAY<\/code> \u7981\u7528 Nagle \u7b97\u6cd5<\/li>\n<li><code>TCP_QUICKACK<\/code> \u7981\u7528 Delayed ACK<\/li>\n<\/ul>\n<p>\u5f53\u6784\u5efa\u4e00\u4e2a\u9ad8\u6027\u80fd PROXY \u7684\u65f6\u5019\uff0c\u6211\u4eec\u5e94\u8be5\u540c\u65f6\u542f\u7528\u8fd9\u4e24\u4e2a Option \uff0c\u4ece\u800c\u6392\u9664\u8fd9\u4e9b\u7b97\u6cd5\u53ef\u80fd\u5f15\u5165\u7684\u5e72\u6270\uff0c\u8fbe\u5230\u6539\u5584\u7f51\u7edc\u6027\u80fd\u7684\u76ee\u7684\u3002<\/p>\n<h3>1.5 ref<\/h3>\n<p><a href=\"https:\/\/cloud.tencent.com\/developer\/article\/1648761\">https:\/\/cloud.tencent.com\/developer\/article\/1648761<\/a><br \/>\n<a href=\"https:\/\/medium.com\/fcamels-notes\/nagles-algorithm-%E5%92%8C-delayed-ack-%E4%BB%A5%E5%8F%8A-minshall-%E7%9A%84%E5%8A%A0%E5%BC%B7%E7%89%88-8fadcb84d96f\">https:\/\/medium.com\/fcamels-notes\/nagles-algorithm-%E5%92%8C-delayed-ack-%E4%BB%A5%E5%8F%8A-minshall-%E7%9A%84%E5%8A%A0%E5%BC%B7%E7%89%88-8fadcb84d96f<\/a><br \/>\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Nagle%27s_algorithm\">https:\/\/en.wikipedia.org\/wiki\/Nagle%27s_algorithm<\/a><br \/>\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/TCP_delayed_acknowledgment\">https:\/\/en.wikipedia.org\/wiki\/TCP_delayed_acknowledgment<\/a><\/p>\n<h2>2. TCP keepalive \u592a\u957f\u5bfc\u81f4\u88ab\u5bb9\u5668\u7f51\u7edc NAT \u5e72\u6389<\/h2>\n<h3>2.1 TCP keepalive \u7684\u4e09\u4e2a\u53c2\u6570<\/h3>\n<p>TCP KeepAlive\u673a\u5236\u4e3b\u8981\u6d89\u53ca3\u4e2a\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>\n<p><code>tcp_keepalive_time<\/code> (integer; default: 7200; since Linux 2.2)<br \/>\nThe number of seconds a connection needs to be idle before TCP begins sending out keep-alive probes. Keep-alives are sent only when the SO_KEEPALIVE socket option is enabled. The default value is 7200 seconds (2 hours). An idle connection is terminated after approximately an additional 11 minutes (9 probes an interval of 75 seconds apart) when keep-alive is enabled.<br \/>\n\u5728 TCP \u4fdd\u6d3b\u542f\u7528\u7684\u60c5\u51b5\u4e0b\uff0c\u4ece\u6700\u540e\u4e00\u6b21\u6570\u636e\u4ea4\u6362\u5230 TCP \u53d1\u9001\u7b2c\u4e00\u4e2a\u4fdd\u6d3b\u63a2\u6d4b\u5305\u7684\u95f4\u9694\uff0c\u5373\u5141\u8bb8\u7684\u6301\u7eed\u7a7a\u95f2\u65f6\u957f\uff0c\u6216\u8005\u8bf4\u6bcf\u6b21\u6b63\u5e38\u53d1\u9001\u5fc3\u8df3\u7684\u5468\u671f\uff0c\u9ed8\u8ba4\u503c\u4e3a7200s\uff082h\uff09\u3002<\/p>\n<\/li>\n<li>\n<p><code>tcp_keepalive_intvl<\/code> (integer; default: 75; since Linux 2.4)<br \/>\nThe number of seconds between TCP keep-alive probes.<br \/>\n\u5728 <code>tcp_keepalive_time<\/code> \u4e4b\u540e\uff0c\u6ca1\u6709\u63a5\u6536\u5230\u5bf9\u65b9\u786e\u8ba4\uff0c\u7ee7\u7eed\u53d1\u9001\u4fdd\u6d3b\u63a2\u6d4b\u5305\u7684\u53d1\u9001\u9891\u7387\uff0c\u9ed8\u8ba4\u503c\u4e3a 75s\u3002<\/p>\n<\/li>\n<li>\n<p><code>tcp_keepalive_probes<\/code> (integer; default: 9; since Linux 2.2)<br \/>\nThe maximum number of TCP keep-alive probes to send before giving up and killing the connection if no response is obtained from the other end.<br \/>\n\u5728 <code>tcp_keepalive_time<\/code> \u4e4b\u540e\uff0c\u5141\u8bb8\u53d1\u9001\u4fdd\u6d3b\u63a2\u6d4b\u5305\u7684\u6700\u5927\u6b21\u6570\uff0c\u5230\u8fbe\u6b64\u6b21\u6570\u540e\u76f4\u63a5\u653e\u5f03\u5c1d\u8bd5\u5e76\u5173\u95ed\u8fde\u63a5\uff0c\u9ed8\u8ba4\u503c\u4e3a 9 \u6b21\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>2.2 \u5728\u5bb9\u5668\u7f51\u7edc NAT \u73af\u5883\u4e2d\u7684\u95ee\u9898<\/h3>\n<p>\u9ed8\u8ba4\u7684 2 \u5c0f\u65f6\u592a\u957f\u4e86\u3002\u53ef\u80fd\u8fd8\u6ca1\u6765\u5f97\u53ca\u53d1\u51fa\u7b2c\u4e00\u4e2a\u4fdd\u6d3b\u5305\uff0c\u5bb9\u5668\u7f51\u7edc NAT \u5c31\u8ba4\u4e3a\u8fd9\u6761 TCP \u8fde\u63a5\u4e0a\u957f\u671f\u6ca1\u6709\u6570\u636e\u4ea4\u4e92\uff0c\u76f4\u63a5\u7ed9\u5e72\u6389\u4e86\u3002\u6b64\u540e\uff0c\u5f53\u5bb9\u5668\u5185\u5e94\u7528\u518d\u6b21\u5c1d\u8bd5\u4f7f\u7528\u8fd9\u4e2a Socket \u65f6\uff0c\u4f1a\u53d1\u73b0\u5df2\u7ecf\u88ab\u5f02\u5e38\u5173\u95ed\uff08EOF\uff09\u3002<\/p>\n<h3>2.3 ref<\/h3>\n<p><a href=\"https:\/\/www.cnblogs.com\/hueyxu\/p\/15759819.html\">https:\/\/www.cnblogs.com\/hueyxu\/p\/15759819.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. TCP_NODELAY \u548c TCP_QUICKACK 1.1. Nagle&#8217;s Algorithm \u73b0\u4ee3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,11],"tags":[20,31],"class_list":["post-430","post","type-post","status-publish","format-standard","hentry","category-linux","category-11","tag-linux","tag-31"],"_links":{"self":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/430","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/comments?post=430"}],"version-history":[{"count":0,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/430\/revisions"}],"wp:attachment":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/media?parent=430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/categories?post=430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/tags?post=430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}