记录一下linux 5.14 中 listen 的实现

概述

面试时候选人提到了半连接,全连接队列分别由内核中的常量,backlog参数决定。这个和之前的理解不符,于是看代码一探究竟。

源码剖析

源码版本:
https://github.com/torvalds/l...
d992fe5318d8d7af9510b879439a3c7f283da442

入口

搜索"sys_listen", 可以看到,backlog确实是取了用户传入和sysctl_somaxconn的最小值。

int __sys_listen(int fd, int backlog)
{
    struct socket *sock;
    int err, fput_needed;
    int somaxconn;

    sock = sockfd_lookup_light(fd, &err, &fput_needed);
    if (sock) {
        somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
        if ((unsigned int)backlog > somaxconn)
            backlog = somaxconn;

        err = security_socket_listen(sock, backlog);
        if (!err)
            err = sock->ops->listen(sock, backlog);

        fput_light(sock->file, fput_needed);
    }
    return err;
}

listen的实现

可以看到sock->ops是一个函数指针结构体,搜索"sock->ops = ",可以找到一系列proto_ops。挑base_sock_ops继续看。

static const struct proto_ops base_sock_ops = {
    .family        = PF_ISDN,
    .owner        = THIS_MODULE,
    .release    = base_sock_release,
    .ioctl        = base_sock_ioctl,
    .bind        = base_sock_bind,
    .getname    = sock_no_getname,
    .sendmsg    = sock_no_sendmsg,
    .recvmsg    = sock_no_recvmsg,
    .listen        = sock_no_listen,
    .shutdown    = sock_no_shutdown,
    .connect    = sock_no_connect,
    .socketpair    = sock_no_socketpair,
    .accept        = sock_no_accept,
    .mmap        = sock_no_mmap
};

发现大部分的proto_ops都没有定义listen的实现。
搜索".listen =",可以找到有 svc_listen, dn_listen, unix_listen, vsock_listen, x25_listen等. 从命名和文件头的描述,猜测inet_listen是实际用的listen,即socket_addr中指定的协议。继续看inet_listen。
可以看到backlog赋值给了sk->sk_max_ack_backlog,搜索发现其它listen函数也是如此。

int inet_listen(struct socket *sock, int backlog)
{
    struct sock *sk = sock->sk;
    unsigned char old_state;
    int err, tcp_fastopen;

    lock_sock(sk);

    err = -EINVAL;
    if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
        goto out;

    old_state = sk->sk_state;
    if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
        goto out;

    WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
    /* Really, if the socket is already in listen state
     * we can only allow the backlog to be adjusted.
     */

从inet_connection_sock.c:795 可以看到,这个值决定的icsk_accept_queue队列大小,并且做了一个新生代老年代的优化.

    icsk = inet_csk(sk_listener);
    net = sock_net(sk_listener);
    max_syn_ack_retries = icsk->icsk_syn_retries ? : net->ipv4.sysctl_tcp_synack_retries;
    /* Normally all the openreqs are young and become mature
     * (i.e. converted to established socket) for first timeout.
     * If synack was not acknowledged for 1 second, it means
     * one of the following things: synack was lost, ack was lost,
     * rtt is high or nobody planned to ack (i.e. synflood).
     * When server is a bit loaded, queue is populated with old
     * open requests, reducing effective size of queue.
     * When server is well loaded, queue size reduces to zero
     * after several minutes of work. It is not synflood,
     * it is normal operation. The solution is pruning
     * too old entries overriding normal timeout, when
     * situation becomes dangerous.
     *
     * Essentially, we reserve half of room for young
     * embrions; and abort old ones without pity, if old
     * ones are about to clog our table.
     */
    queue = &icsk->icsk_accept_queue;
    qlen = reqsk_queue_len(queue);
    if ((qlen << 1) > max(8U, READ_ONCE(sk_listener->sk_max_ack_backlog))) {
        int young = reqsk_queue_len_young(queue) << 1;

        while (max_syn_ack_retries > 2) {
            if (qlen < young)
                break;
            max_syn_ack_retries--;
            young <<= 1;
        }
    }

accept中有去find already established connection。

/*
 * This will accept the next outstanding connection.
 */
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);

    /* We need to make sure that this socket is listening,
     * and that it has something pending.
     */
    error = -EINVAL;
    if (sk->sk_state != TCP_LISTEN)
        goto out_err;

    /* Find already established connection */
    if (reqsk_queue_empty(queue)) {
        long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);

        /* If this is a non blocking socket don't sleep */
        error = -EAGAIN;
        if (!timeo)
            goto out_err;

        error = inet_csk_wait_for_connect(sk, timeo);
        if (error)
            goto out_err;
    }
    req = reqsk_queue_remove(queue, sk);
    newsk = req->sk;

结论

backlog决定icsk_accept_queue队列大小。该队列中为ESTABLISHTED状态的链接。至于SYNC_RECV的链接需要再继续看代码。

你可能感兴趣的:(记录一下linux 5.14 中 listen 的实现)