清空内核的TCP缓冲区以处理带有`MSG_MORE`标志的数据包。

7

send()函数的man page文档显示了MSG_MORE标志,它被断言为类似于TCP_CORK的作用。我有一个围绕send()的包装函数:

int SocketConnection_Write(SocketConnection *this, void *buf, int len) {
    errno = 0;

    int sent = send(this->fd, buf, len, MSG_NOSIGNAL);

    if (errno == EPIPE || errno == ENOTCONN) {
        throw(exc, &SocketConnection_NotConnectedException);
    } else if (errno == ECONNRESET) {
        throw(exc, &SocketConnection_ConnectionResetException);
    } else if (sent != len) {
        throw(exc, &SocketConnection_LengthMismatchException);
    }

    return sent;
}

假设我想使用内核缓冲区,我可以选择使用,在必要时启用它,然后禁用它以刷新缓冲区。但是另一方面,这样就需要进行额外的系统调用。因此,我认为使用更合适。我只需将上述send()行更改为:
int sent = send(this->fd, buf, len, MSG_NOSIGNAL | MSG_MORE);

根据lwm.net,如果数据包足够大,则会自动刷新:

如果应用程序在套接字上设置了该选项,则内核不会发送短数据包。相反,它将等待足够的数据出现以填充最大大小的数据包,然后发送。当关闭TCP_CORK时,剩余的数据将通过网络发送。

但是,此部分仅涉及。那么,如何刷新MSG_MORE数据包呢?
我只能想到两种可能性:
  1. 使用空缓冲区调用send()并且未设置MSG_MORE
  2. this页面所述重新应用TCP_CORK选项
不幸的是,整个主题的文档非常差,我在互联网上找不到太多信息。
我也想知道如何检查一切是否按预期工作?显然,通过strace运行服务器不是一个选项。因此,最简单的方法是使用netcat,然后查看其strace输出?或者内核会以不同方式处理通过环回接口传输的流量吗?

sendfile()保留“MSG_MORE”标志。当sendfile()返回时,缓存将被刷新。 - user206268
2个回答

12

我已经查看了内核源代码,两个假设似乎是正确的。以下代码是从 net/ipv4/tcp.c (2.6.33.1) 中提取的。

static inline void tcp_push(struct sock *sk, int flags, int mss_now,
                int nonagle)
{
    struct tcp_sock *tp = tcp_sk(sk);

    if (tcp_send_head(sk)) {
        struct sk_buff *skb = tcp_write_queue_tail(sk);
        if (!(flags & MSG_MORE) || forced_push(tp))
            tcp_mark_push(tp, skb);
        tcp_mark_urg(tp, flags, skb);
        __tcp_push_pending_frames(sk, mss_now,
                      (flags & MSG_MORE) ? TCP_NAGLE_CORK : nonagle);
    }
}
因此,如果未设置该标志,则挂起帧肯定会被刷新。但是,仅当缓冲区不为空时才会发生这种情况。
static ssize_t do_tcp_sendpages(struct sock *sk, struct page **pages, int poffset,
             size_t psize, int flags)
{
(...)
    ssize_t copied;
(...)
    copied = 0;

    while (psize > 0) {
(...)
        if (forced_push(tp)) {
            tcp_mark_push(tp, skb);
            __tcp_push_pending_frames(sk, mss_now, TCP_NAGLE_PUSH);
        } else if (skb == tcp_send_head(sk))
            tcp_push_one(sk, mss_now);
        continue;

wait_for_sndbuf:
        set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
wait_for_memory:
        if (copied)
            tcp_push(sk, flags & ~MSG_MORE, mss_now, TCP_NAGLE_PUSH);

        if ((err = sk_stream_wait_memory(sk, &timeo)) != 0)
            goto do_error;

        mss_now = tcp_send_mss(sk, &size_goal, flags);
    }

out:
    if (copied)
        tcp_push(sk, flags, mss_now, tp->nonagle);
    return copied;

do_error:
    if (copied)
        goto out;
out_err:
    return sk_stream_error(sk, flags, err);
}
while循环体将永远不会被执行,因为psize不大于0。然后,在out部分,有另一个机会来调用tcp_push(),但是因为copied仍然保持其默认值,它也会失败。

因此,发送长度为0的数据包永远不会导致刷新。

下一个可能的理论是重新应用TCP_CORK。让我们先看一下代码:

static int do_tcp_setsockopt(struct sock *sk, int level,
        int optname, char __user *optval, unsigned int optlen)
{

(...)

    switch (optname) {
(...)

    case TCP_NODELAY:
        if (val) {
            /* TCP_NODELAY is weaker than TCP_CORK, so that
             * this option on corked socket is remembered, but
             * it is not activated until cork is cleared.
             *
             * However, when TCP_NODELAY is set we make
             * an explicit push, which overrides even TCP_CORK
             * for currently queued segments.
             */
            tp->nonagle |= TCP_NAGLE_OFF|TCP_NAGLE_PUSH;
            tcp_push_pending_frames(sk);
        } else {
            tp->nonagle &= ~TCP_NAGLE_OFF;
        }
        break;

    case TCP_CORK:
        /* When set indicates to always queue non-full frames.
         * Later the user clears this option and we transmit
         * any pending partial frames in the queue.  This is
         * meant to be used alongside sendfile() to get properly
         * filled frames when the user (for example) must write
         * out headers with a write() call first and then use
         * sendfile to send out the data parts.
         *
         * TCP_CORK can be set together with TCP_NODELAY and it is
         * stronger than TCP_NODELAY.
         */
        if (val) {
            tp->nonagle |= TCP_NAGLE_CORK;
        } else {
            tp->nonagle &= ~TCP_NAGLE_CORK;
            if (tp->nonagle&TCP_NAGLE_OFF)
                tp->nonagle |= TCP_NAGLE_PUSH;
            tcp_push_pending_frames(sk);
        }
        break;
(...)

正如你所看到的,有两种刷新方式。 你可以将TCP_NODELAY设置为1或将TCP_CORK设置为0。幸运的是,两者都不会检查标志是否已经设置。因此,即使当前没有设置,我重新应用TCP_CORK标志的初始计划也可以优化为仅禁用它。

我希望这可以帮助遇到类似问题的人们。


3

这需要进行大量的研究...我能提供的是经验性的笔记:

发送一堆设置了MSG_MORE的数据包,然后跟随一个没有设置MSG_MORE的数据包,整套数据都会被发送出去。 对于像这样的事情,它起到了很好的作用:

  for (i=0; i<mg_live.length; i++) {
        // [...]
        if ((n = pth_send(sock, query, len, MSG_MORE | MSG_NOSIGNAL)) < len) {
           printf("error writing to socket (sent %i bytes of %i)\n", n, len);
           exit(1);
        }
     }
  }

  pth_send(sock, "END\n", 4, MSG_NOSIGNAL);

也就是说,当您一次性发送所有数据包并有明确定义的结束时... 并且您只使用一个套接字。

如果您在上述循环过程中尝试向另一个套接字写入数据,您可能会发现Linux释放了先前持有的数据包。至少这似乎是我现在遇到的问题。但对您来说可能是一个简单的解决方案。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接