在下文中,我们将遵循最典型的代码路径,并将忽略丢包、重传以及使用 TCP 快速打开(代码 cmets 中的 TFO)等非典型功能所产生的问题。
accept 调用由intet_csk_accept 处理,它调用reqsk_queue_remove 从侦听套接字的接受队列&icsk->icsk_accept_queue 中取出一个套接字:
struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern)
{
struct inet_connection_sock *icsk = inet_csk(sk);
struct request_sock_queue *queue = &icsk->icsk_accept_queue;
struct request_sock *req;
struct sock *newsk;
int error;
lock_sock(sk);
[...]
req = reqsk_queue_remove(queue, sk);
newsk = req->sk;
[...]
return newsk;
[...]
}
在reqsk_queue_remove 中,它使用rskq_accept_head 和rskq_accept_tail 将套接字拉出队列并调用sk_acceptq_removed:
static inline struct request_sock *reqsk_queue_remove(struct request_sock_queue *queue,
struct sock *parent)
{
struct request_sock *req;
spin_lock_bh(&queue->rskq_lock);
req = queue->rskq_accept_head;
if (req) {
sk_acceptq_removed(parent);
WRITE_ONCE(queue->rskq_accept_head, req->dl_next);
if (queue->rskq_accept_head == NULL)
queue->rskq_accept_tail = NULL;
}
spin_unlock_bh(&queue->rskq_lock);
return req;
}
而sk_acceptq_removed 减少了sk_ack_backlog 中等待接受的套接字队列的长度:
static inline void sk_acceptq_removed(struct sock *sk)
{
WRITE_ONCE(sk->sk_ack_backlog, sk->sk_ack_backlog - 1);
}
我认为,提问者完全理解这一点。现在让我们看看收到 SYN 时会发生什么,以及 3WH 的最终 ACK 到达时会发生什么。
首先收到 SYN。同样,让我们假设 TFO 和 SYN cookie 没有发挥作用,并查看最常见的路径(至少在出现 SYN 洪水时不会)。
SYN 在tcp_conn_request 中处理,其中存储连接请求(不是完整的套接字)(我们很快就会看到),方法是调用inet_csk_reqsk_queue_hash_add,然后调用send_synack 来响应SYN:
int tcp_conn_request(struct request_sock_ops *rsk_ops,
const struct tcp_request_sock_ops *af_ops,
struct sock *sk, struct sk_buff *skb)
{
[...]
if (!want_cookie)
inet_csk_reqsk_queue_hash_add(sk, req,
tcp_timeout_init((struct sock *)req));
af_ops->send_synack(sk, dst, &fl, req, &foc,
!want_cookie ? TCP_SYNACK_NORMAL :
TCP_SYNACK_COOKIE);
[...]
return 0;
[...]
}
inet_csk_reqsk_queue_hash_add 调用 reqsk_queue_hash_req 和 inet_csk_reqsk_queue_added 来存储请求。
void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,
unsigned long timeout)
{
reqsk_queue_hash_req(req, timeout);
inet_csk_reqsk_queue_added(sk);
}
reqsk_queue_hash_req 将请求放入 ehash。
static void reqsk_queue_hash_req(struct request_sock *req,
unsigned long timeout)
{
[...]
inet_ehash_insert(req_to_sk(req), NULL);
[...]
}
然后inet_csk_reqsk_queue_added 使用icsk_accept_queue 调用reqsk_queue_added:
static inline void inet_csk_reqsk_queue_added(struct sock *sk)
{
reqsk_queue_added(&inet_csk(sk)->icsk_accept_queue);
}
增加qlen(不是sk_ack_backlog):
static inline void reqsk_queue_added(struct request_sock_queue *queue)
{
atomic_inc(&queue->young);
atomic_inc(&queue->qlen);
}
ehash 是所有 ESTABLISHED 和 TIMEWAIT 套接字的存储位置,以及最近存储 SYN“队列”的位置。
请注意,将到达的连接请求存储在适当的队列中实际上没有任何意义。它们的顺序无关紧要(最终的 ACK 可以按任何顺序到达),通过将它们移出侦听套接字,无需锁定侦听套接字来处理最终的 ACK。
请参阅this commit 了解影响此更改的代码。
最后,我们可以看到请求从 ehash 中移除,并作为一个完整的套接字添加到接受队列中。
3WH 的最终 ACK 由tcp_check_req 处理,它创建一个完整的子套接字,然后调用inet_csk_complete_hashdance:
struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,
struct request_sock *req,
bool fastopen, bool *req_stolen)
{
[...]
/* OK, ACK is valid, create big socket and
* feed this segment to it. It will repeat all
* the tests. THIS SEGMENT MUST MOVE SOCKET TO
* ESTABLISHED STATE. If it will be dropped after
* socket is created, wait for troubles.
*/
child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL,
req, &own_req);
[...]
return inet_csk_complete_hashdance(sk, child, req, own_req);
[...]
}
然后inet_csk_complete_hashdance 对请求调用inet_csk_reqsk_queue_drop 和reqsk_queue_removed,对子调用inet_csk_reqsk_queue_add:
struct sock *inet_csk_complete_hashdance(struct sock *sk, struct sock *child,
struct request_sock *req, bool own_req)
{
if (own_req) {
inet_csk_reqsk_queue_drop(sk, req);
reqsk_queue_removed(&inet_csk(sk)->icsk_accept_queue, req);
if (inet_csk_reqsk_queue_add(sk, req, child))
return child;
}
[...]
}
inet_csk_reqsk_queue_drop 调用 reqsk_queue_unlink,从 ehash 中删除请求,reqsk_queue_removed 减少 qlen:
void inet_csk_reqsk_queue_drop(struct sock *sk, struct request_sock *req)
{
if (reqsk_queue_unlink(req)) {
reqsk_queue_removed(&inet_csk(sk)->icsk_accept_queue, req);
reqsk_put(req);
}
}
最后,inet_csk_reqsk_queue_add 将完整的套接字添加到接受队列中。
struct sock *inet_csk_reqsk_queue_add(struct sock *sk,
struct request_sock *req,
struct sock *child)
{
struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
spin_lock(&queue->rskq_lock);
if (unlikely(sk->sk_state != TCP_LISTEN)) {
inet_child_forget(sk, req, child);
child = NULL;
} else {
req->sk = child;
req->dl_next = NULL;
if (queue->rskq_accept_head == NULL)
WRITE_ONCE(queue->rskq_accept_head, req);
else
queue->rskq_accept_tail->dl_next = req;
queue->rskq_accept_tail = req;
sk_acceptq_added(sk);
}
spin_unlock(&queue->rskq_lock);
return child;
}
TL;DR 它在 ehash 中,并且此类 SYN 的数量是 qlen(而不是 sk_ack_backlog,它保存了接受队列中的套接字数量)。