{"id":514,"date":"2025-04-09T23:50:26","date_gmt":"2025-04-09T15:50:26","guid":{"rendered":"https:\/\/cococat.top\/?p=514"},"modified":"2025-06-15T14:49:09","modified_gmt":"2025-06-15T06:49:09","slug":"syn-queue-accept-queue","status":"publish","type":"post","link":"https:\/\/cococat.top\/index.php\/2025\/04\/09\/syn-queue-accept-queue\/","title":{"rendered":"SYN Queue &#038; Accept Queue"},"content":{"rendered":"<p>\u5185\u6838\u4e2d\u7684\u4e24\u4e2a\u5b58\u653e\u8fde\u63a5\u4fe1\u606f\u7684\u961f\u5217\u3002\u5b58\u653e\u7684\u8fde\u63a5\u5143\u6570\u636e\uff0c\u56db\u5143\u7ec4\u3001MSS\u3001window scale \u7b49\u3002<br \/>\n\u5176\u7528\u9014\u7b80\u5355\u6765\u8bf4\uff1a<\/p>\n<ul>\n<li>\u6536\u5230 SYN \u4ee5\u540e\u653e\u8fdb SYN queue\uff08\u534a\u8fde\u63a5\u961f\u5217\uff09<\/li>\n<li>\u6536\u5230 ACK \u4ee5\u540e\u653e\u8fdb Accept Queue\uff08\u5168\u8fde\u63a5\u961f\u5217\uff09<\/li>\n<li>\u7528\u6237\u8fdb\u7a0b\u8c03\u7528 <code>accept()<\/code>  \u4ee5\u540e\uff0c\u4ece Accept Queue \u4e2d\u53d6\u51fa<br \/>\n![[synqueue.png]] <\/li>\n<\/ul>\n<h1>1. SYN Queue \u6ea2\u51fa\u65f6\u884c\u4e3a<\/h1>\n<h2>1.1. SYN Cookie \u673a\u5236<\/h2>\n<p>SYN Queue \u6ea2\u51fa\u65f6 linux \u7684\u884c\u4e3a\u4e0e SYN Cookie \u8fd9\u4e2a\u7528\u4e8e\u5bf9\u6297 SYN flood \u653b\u51fb\u7684\u673a\u5236\u6709\u5173\u3002<br \/>\nSYN Cookie \u539f\u7406\uff1a\u8bbe\u8ba1\u51fa\u4e00\u4e2a\u53ef\u9006\u7684 hash \u51fd\u6570\uff0c\u53ef\u4ee5\u57fa\u4e8e\u65f6\u95f4\u6233\u3001\u56db\u5143\u7ec4\u7b49 TCP \u8fde\u63a5\u4fe1\u606f\u7b97\u51fa hash \u503c\uff0c\u5e76\u4f5c\u4e3a SYN+ACK \u62a5\u6587\u7684\u5e8f\u5217\u53f7\u3002\u5982\u679c peer \u662f\u6b63\u5e38\u7684\u5ba2\u6237\u7aef\uff0c\u5219\u4e0b\u4e2a\u53d1\u9001\u8fc7\u6765\u7684 ACK \u662f\u8fd9\u4e2a\u5e8f\u5217\u53f7 +1\uff0c\u5c06\u5176\u51cf\u4e00\u5373\u53ef\u9006\u63a8\u51fa\u6240\u6709\u7684 TCP \u5143\u6570\u636e\uff0c\u76f4\u63a5\u653e\u5165 Accept Queue \u5373\u53ef\u3002<br \/>\n\u8fd9\u6837\u4e00\u6765\uff0cSYN_RCVD \u72b6\u6001\u7684 socket \u5c31\u53ef\u4ee5\u4e0d\u5fc5\u653e\u5165 SYN queue \u4e2d\uff0c\u9632\u6b62\u88ab\u6253\u7206\u3002<\/p>\n<h2>1.2. \u6ea2\u51fa\u884c\u4e3a<\/h2>\n<p>\u7531\u5185\u6838\u53c2\u6570\u63a7\u5236\u3002<\/p>\n<ul>\n<li><code>net.ipv4.tcp_syncookies = 0<\/code>\uff0c\u8868\u793a\u5173\u95ed SYN Cookie \u673a\u5236\uff0c\u5982\u679c SYN \u961f\u5217\u5df2\u6ee1\uff0c\u90a3\u4e48\u65b0\u5230\u7684 SYN \u62a5\u6587\u5c06\u88ab\u4e22\u5f03\u3002<\/li>\n<li><code>net.ipv4.tcp_syncookies = 1<\/code>\uff0c\u8868\u793a SYN Cookie \u673a\u5236\u4ec5\u5728 SYN \u961f\u5217\u6ee1\u65f6\u624d\u6b63\u5f0f\u542f\u7528\u3002<\/li>\n<li><code>net.ipv4.tcp_syncookies = 2<\/code>\uff0c\u8868\u793a\u65e0\u6761\u4ef6\u542f\u7528 SYN Cookie \u673a\u5236\u3002<\/li>\n<\/ul>\n<h2>1.3. \u67e5\u770b\u4e0e\u4fee\u6539 SYN Queue \u957f\u5ea6<\/h2>\n<pre><code>sysctl net.ipv4.tcp_max_syn_backlog\nsysctl -w net.ipv4.tcp_max_syn_backlog=2048<\/code><\/pre>\n<h2>1.4. \u76d1\u63a7\u5f53\u524d SYN Queue \u72b6\u6001<\/h2>\n<pre><code>netstat(\u6216 ss ) -tnap | grep SYN_RECV\n# \u4e5f\u53ef\u4ee5\u7528 ss \u652f\u6301\u7684\u66f4\u73b0\u4ee3\u65b9\u5f0f\uff1a\nss -n state syn-recv<\/code><\/pre>\n<h1>2. Accept Queue \u6ea2\u51fa\u65f6\u884c\u4e3a<\/h1>\n<h2>2.1. \u6ea2\u51fa\u884c\u4e3a<\/h2>\n<p>\u7531\u5185\u6838\u53c2\u6570 <code>net.ipv4.tcp_abort_on_overflow<\/code> \u63a7\u5236\u3002<\/p>\n<ul>\n<li><code>net.ipv4.tcp_abort_on_overflow = 0<\/code> \uff0c\uff08\u9ed8\u8ba4\uff09\uff0c\u4f1a\u4e22\u5f03 peer \u53d1\u6765\u7684\u7b2c\u4e09\u6b21\u63e1\u624b\u7684 ACK\uff0c\u800c\u91cd\u4f20\u7b2c\u4e8c\u6b21\u63e1\u624b\u65f6\u7684 SYN+ACK \u3002<\/li>\n<li><code>net.ipv4.tcp_abort_on_overflow = 1<\/code> \uff0c\u5219\u4f1a\u76f4\u63a5\u5411 peer \u53d1\u9001 RST\u3002<br \/>\n<h3>2.1.1. \u4e3a\u4f55\u8981\u91cd\u4f20 SYN+ACK\uff1f<\/h3>\n<\/li>\n<li>\u534f\u8bae\u89c4\u5b9a\uff1a\u7531\u4e8e Accept Queue \u6ee1\uff0c\u8fd9\u4e2a socket \u53ea\u80fd\u7ee7\u7eed\u5f85\u5728 SYN Queue \u4e2d\uff0c\u5373\u8fd8\u662f\u5904\u4e8e SYN_RECV \u72b6\u6001\uff0c\u6b64\u65f6\u53ea\u80fd\u56de\u590d SYN+ACK\u3002<\/li>\n<li>\u6709\u5229\u4e8e\u72b6\u6001\u673a\u6062\u590d\uff1a\u53e6\u4e00\u65b9\u9762\uff0cpeer \u6536\u5230\u91cd\u590d\u7684 SYN+ACK \u65f6\uff0c\u4f1a\u8ba4\u4e3a\u662f\u4e22\u5305\uff0c\u6545\u4f1a\u91cd\u4f20 ACK\u3002\u5982\u679c\u4e0b\u4e00\u4e2a ACK \u5230\u6765\u65f6\uff0cAccept Queue \u53c8\u6709\u7a7a\u95f4\u4e86\uff0c\u5219\u53ef\u4ee5\u6b63\u5e38\u5b8c\u6210\u63e1\u624b\u3001\u653e\u5165\u961f\u5217\u4e2d\u3002<\/li>\n<\/ul>\n<h2>2.2. \u5178\u578b\u6ea2\u51fa\u73b0\u8c61<\/h2>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5185\u6838\u53c2\u6570 <code>net.ipv4.tcp_abort_on_overflow<\/code> \u662f 0\u3002\u6b64\u65f6\u5982\u679c\u53d1\u751f SYN Queue \u6ea2\u51fa\uff0c\u5219 server \u4e00\u76f4\u5728\u53d1\u9001 SYN+ACK\uff0cclient \u4e00\u76f4\u5728\u53d1\u9001 ACK(+PSH)\u3002<\/p>\n<h2>2.3. \u4fee\u6539 Accept Queue \u5927\u5c0f<\/h2>\n<p>\u7531  <code>net.core.somaxconn<\/code> \u548c socket \u7684 <code>backlog<\/code> \u53c2\u6570\u4e2d\u7684\u6700\u5c0f\u503c\u51b3\u5b9a\u3002<\/p>\n<blockquote>\n<p>man listen:<br \/>\nIf  the  backlog argument is greater than the value in \/proc\/sys\/net\/core\/somaxconn, then it is silently truncated to that value.  Since Linux 5.4, the default in this file is 4096; in earlier kernels, the default value is 128.  In kernels before 2.4.25, this limit was a hard coded value, SOMAXCONN, with the value 128.<\/p>\n<\/blockquote>\n<h2>2.4. \u76d1\u63a7\u5f53\u524d\u5168\u8fde\u63a5\u961f\u5217\u72b6\u6001<\/h2>\n<pre><code>ss -lnt<\/code><\/pre>\n<p>\u5bf9\u4e8e <code>ss<\/code> \u7684\u8f93\u51fa\uff0c\u76d1\u542c\u72b6\u6001\u7684 socket \u7684 <code>Recv-Q<\/code> \u4ee3\u8868\u5168\u8fde\u63a5\u961f\u5217\u5927\u5c0f\uff0c<code>Send-Q<\/code> \u663e\u793a\u6700\u5927\u957f\u5ea6<\/p>\n<h1>3. \u53c2\u8003<\/h1>\n<ul>\n<li>\u6781\u597d: <a href=\"https:\/\/www.emqx.com\/zh\/blog\/emqx-performance-tuning-tcp-syn-queue-and-accept-queue\">https:\/\/www.emqx.com\/zh\/blog\/emqx-performance-tuning-tcp-syn-queue-and-accept-queue<\/a><\/li>\n<li><a href=\"https:\/\/www.alibabacloud.com\/blog\/tcp-syn-queue-and-accept-queue-overflow-explained_599203\">https:\/\/www.alibabacloud.com\/blog\/tcp-syn-queue-and-accept-queue-overflow-explained_599203<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5185\u6838\u4e2d\u7684\u4e24\u4e2a\u5b58\u653e\u8fde\u63a5\u4fe1\u606f\u7684\u961f\u5217\u3002\u5b58\u653e\u7684\u8fde\u63a5\u5143\u6570\u636e\uff0c\u56db\u5143\u7ec4\u3001MSS\u3001window scale \u7b49\u3002 \u5176\u7528\u9014\u7b80\u5355\u6765 [&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-514","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\/514","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=514"}],"version-history":[{"count":2,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/514\/revisions"}],"predecessor-version":[{"id":560,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/514\/revisions\/560"}],"wp:attachment":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/media?parent=514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/categories?post=514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/tags?post=514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}