关于linux:记录一下linux-514-中-listen-的实现

43次阅读

共计 3645 个字符,预计需要花费 10 分钟才能阅读完成。

概述

面试时候选人提到了半连贯,全连贯队列别离由内核中的常量,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 队列大小,从代码上来看,并没有辨别所谓的半,全链接队列。并且做了一个新生代老年代的优化.
一个是 reqsk_timer_handler 中有解决 syn_ack 的重发。

    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 队列大小。该队列中存在 SYNC_RECV/ESTABLISHTED 状态的链接。

正文完
 0