+ -
当前位置:首页 → 问答吧 → 关于ip_append_data函数的问题

关于ip_append_data函数的问题

时间:2010-11-29

来源:互联网

大家好,在阅读《understanding linux network internals 》一书中第21章时,遇到如下描述:

If the L4 layer wants fast response time, it might call ip_push_pending_frames after each call to ip_append_data. But the two functions are provided so that the L4 layer can buffer as much data as possible (up to the size of the PMTU) and then send it at once to be efficient.

现在我有点不解的问题是,该文中所说的“ If the L4 layer wants fast response time” 是什么意思?换句话讲,L4层什么时候认为需要快速响应,什么时候又不需要呢?决定权在哪里?

作者: jiufei19   发布时间: 2010-11-29

我记得 socket 有个选项是 NODELAY,应该是告诉传输层尽快发送数据,就是不需要等待数据达到 PMTU 的时候,才发送

作者: Godbach   发布时间: 2010-11-29

回复 Godbach


    谢谢斑竹!

   斑竹说的是TCP_NODELAY 这个选项吗?查了下资料,该选项的用法如下:

   If set, this option disables TCP's Nagle algorithm (Section 19.4 of TCPv1 and pp. 858–859 of TCPv2). By default, this algorithm is enabled

    显然,对于ip_build_and_send_pkt和ip_send_reply这两个函数而言,正好符合这个要求,因为这两个函数是TCP用于发送SYN ACKs、RST分段的,因此这两个函数都不会和应用层数据有关。那么是否能这样理解,如果L4数据是来自应用层的数据,那么是不会有所谓L4调用ip_append_data函数缓存该应用层待发送数据的现象?

作者: jiufei19   发布时间: 2010-11-29

ip_append_data 实际上主要的任务就是处理应用层的数据,并将其缓存,但是不负责数据的发送。数据的发送由 ip_push_pending_frames 负责

作者: Godbach   发布时间: 2010-11-29

ip_append_data 这个函数 LZ 理解的怎么样了。可以交流一下,我最近也在看这些东西

作者: Godbach   发布时间: 2010-11-29

回复 Godbach


    谢谢!

   我感觉我的表述没有清楚,我想表达的意思好像不是我的本意,晕了,我再整理下思路后,再来请教。

作者: jiufei19   发布时间: 2010-11-29



QUOTE:
If the L4 layer wants fast response time, it might call ip_push_pending_frames after each call to ip_append_data



其实建议接着在后面看一些内核你就明白了。
传输层发送数据,涉及到高效率和高响应率。ip_push_pending_frames 这个函数被调用之前,可能有到多次调用 ip_append_data。

如果你想要高响应率,那么就调用一次  ip_append_data 之后,尽快执行 ip_push_pending_frames 。
如果你想高效率,一次尽可能多的发数据,那就可以多次调用 ip_append_data,知道缓冲的数据达到 PMTU,才调用 ip_push_pending_frames。这样一次发送的数据量比较大。

作者: Godbach   发布时间: 2010-11-29

本帖最后由 jiufei19 于 2010-11-29 11:33 编辑

回复 Godbach


谢谢斑竹的热情!

ip_append_data和ip_push_pending_frames这两个函数我已经全部阅读完了,基本上其内部的原理有一个大概的了解。我今天的疑问是发生在回头阅读原书的这几段话时,突然发现自己没有很好理解ip_append_data函数的应用场景了,尤其是对其缓存发送数据的应用场景的全面了解上。

我整理好思路后,再和斑竹交流!


-----------------------------
如果你想要高响应率,那么就调用一次  ip_append_data 之后,尽快执行 ip_push_pending_frames 。
如果你想高效率,一次尽可能多的发数据,那就可以多次调用 ip_append_data,知道缓冲的数据达到 PMTU,才调用 ip_push_pending_frames。这样一次发送的数据量比较大。
----------------------------
这段话的意思我是理解的,我现在的疑问不是指这段话所描述的本身原理,而是------我现在还不知道咋个清晰描述我的问题,呵呵!

作者: jiufei19   发布时间: 2010-11-29

好的,整理好了发上来一起交流一下。我这块也有不少疑问。疑义相与析啊。

作者: Godbach   发布时间: 2010-11-29

回复 Godbach


  斑竹,因为好像问题有点复杂,我先将问题分解下,逐个进行表述,看看能否理清问题思路。

1、首先我想知道的是,ip_append_data被L4层协议调用时,假定L4层协议是UDP协议,那么是否有什么机制进行这里所谓的缓存数据的发送?如果有这个机制是什么?应用层能控制吗?

作者: jiufei19   发布时间: 2010-11-29

udp_sendmsg 调用这个函数

作者: Godbach   发布时间: 2010-11-29

另外,在 udp_sendmsg 中,调用的 ip_append_data 中,是要判断 MSG_MORE 标志位的,这个标志位代表着应用空间很快就用更多的数据下来了,这样分配缓存的时候就会考虑分配一个比较大的

作者: Godbach   发布时间: 2010-11-29

看一下 sendmsg 调用的入口参数


QUOTE:
       ssize_t sendmsg(int s, const struct msghdr *msg, int flags);



最后一个参数 flags 的解释中谈到了  MSG_MORE



QUOTE:
       MSG_MORE (Since Linux 2.4.4)
              The caller has more data to send.  This flag is used with TCP sockets to obtain the same effect as the TCP_CORK socket option (see tcp(7)),  with
              the difference that this flag can be set on a per-call basis.

              Since  Linux 2.6, this flag is also supported for UDP sockets, and informs the kernel to package all of the data sent in calls with this flag set
              into a single datagram which is only transmitted when a call is performed that does not specify this flag.  (See also the UDP_CORK socket  option
              described in udp(7).)

作者: Godbach   发布时间: 2010-11-29