接下来,我们将遵循最典型的代码路径,并忽略丢包、重传以及使用 TCP 快速打开(代码注释中的 TFO)等非典型功能所引起的问题。
接受调用的处理由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;
[...]
}
In reqsk_queue_remove
, 它用rskq_accept_head
and 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;
}
And 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
calls reqsk_queue_hash_req
and 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
calls reqsk_queue_added
与icsk_accept_queue
:
static inline void inet_csk_reqsk_queue_added(struct sock *sk)
{
reqsk_queue_added(&inet_csk(sk)->icsk_accept_queue);
}
这增加了qlen
(not sk_ack_backlog
):
static inline void reqsk_queue_added(struct request_sock_queue *queue)
{
atomic_inc(&queue->young);
atomic_inc(&queue->qlen);
}
哈希是存储所有 ESTABLISHED 和 TIME_WAIT 套接字的位置,最近也是存储 SYN“队列”的位置。
请注意,将到达的连接请求存储在适当的队列中实际上没有任何意义。它们的顺序无关(最终的 ACK 可以按任何顺序到达),并且通过将它们移出侦听套接字,无需锁定侦听套接字来处理最终的 ACK。
See 这次提交 https://git.amelchem.com/amel/linux/commit/079096f103faca2dd87342cca6f23d4b34da8871影响此更改的代码。
最后,我们可以看到请求从 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);
[...]
}
Then inet_csk_complete_hashdance
calls inet_csk_reqsk_queue_drop
and 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
calls 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
,它保存接受队列中的套接字数量)。