当前位置:网站首页>TCP_ Nodelay and TCP_ CORK
TCP_ Nodelay and TCP_ CORK
2022-06-24 21:24:00 【already_ skb】
Nagle Algorithm
It's to reduce the number of small packets in the wan , So as to reduce the emergence of network congestion .
The algorithm requires a tcp There can only be at most one unconfirmed unfinished small group on the connection , In this group ack No other packets can be sent until they arrive ,tcp You need to collect these small groups , And in ack When it comes, it's sent out as a packet ; The definition of small group is less than MSS Any grouping of .( Coming network )
Cork Algorithm
Cork Packets in the link are not allowed in the calculation , The currently sent message is a packet directly hold live , Wait for a certain time to send .
Nagle Algorithm on
TCP_NODELAY Support for sock Set up , The specific code is in the following function :
static int do_tcp_setsockopt(struct sock *sk, int level,
int optname, char __user *optval, unsigned int optlen)
among optval Not 0 Express , close TCP_NODELAY, Otherwise, it means open ,TCP_NODELAY It is enabled by default .
Nagle Open code snippet
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;1. TCP_NODELAY Function is weaker than TCP_CORK, If in TCP_CORK Is set on the connection of TCP_NODELAY, It will not actually take effect , We have to wait until TCP_CORK Function off .
2. TCP_NODELAY In the configuration ( That is, trigger the call do_tcp_setsockopt function ) Shut down yes , It will trigger once PSH Flag message sending , This mechanism does not accept TCP_CORK control .
TCP_NODELAY Message sending relation
Obviously TCP_NODELAY It must restrict the sending behavior of the message , Ahead [Nagle Algorithm ] It has been explained TCP Connection Association NAGLE Sending behavior of , Now let's hand over the documents and look at the code .
The functions associated with the sending path are as follows :
static inline void tcp_push_pending_frames(struct sock *sk)
It's starting to introduce nonagle The factor is , above do_tcp_setsockopt Variable flag set in function : tp->nonagle
void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
int nonagle)
static bool tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle,
int push_one, gfp_t gfp)
stay tcp_write_ximit The code snippet in is as follows
if (tso_segs == 1) {
if (unlikely(!tcp_nagle_test(tp, skb, mss_now,
(tcp_skb_is_last(sk, skb) ?
nonagle : TCP_NAGLE_PUSH))))
break;
} else {
if (!push_one &&
tcp_tso_should_defer(sk, skb, &is_cwnd_limited,
&is_rwnd_limited, max_segs))
break;
}tcp_nagle_test Namely TCP_NODELAY Check function for , Whether the message is sent or not is decided again .
The fourth parameter is ( the last one ) yes nagle Control variables , If there is only one message in the current sending queue ( consider TSO, Send the last message in the queue ), Give it to TCP_NODELAY The mechanism decides whether to send a message , Otherwise, send the message directly .
Concrete nagle Judge behavior
/* Return true if the Nagle test allows this packet to be
* sent now.
*/
static inline bool tcp_nagle_test(const struct tcp_sock *tp, const struct sk_buff *skb,
unsigned int cur_mss, int nonagle)
{
/* Nagle rule does not apply to frames, which sit in the middle of the
* write_queue (they have no chances to get new data).
*
* This is implemented in the callers, where they modify the 'nonagle'
* argument based upon the location of SKB in the send queue.
*/
if (nonagle & TCP_NAGLE_PUSH)
return true;
/* Don't use the nagle rule for urgent data (or for the final FIN). */
if (tcp_urg_mode(tp) || (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
return true;
if (!tcp_nagle_check(skb->len < cur_mss, tp, nonagle))
return true;
return false;
}1. If you hit TCP_NAGLE_PUSH sign , return true, The outer logic is to send messages directly .
2. If the message FIN Message or urg The message is sent directly , That is to say TCP_NODELAY Yes FIN Message and URG Message invalid .
TCP_NODELAY Main decision logic
/* Return false, if packet can be sent now without violation Nagle's rules:
* 1. It is full sized. (provided by caller in %partial bool)
* 2. Or it contains FIN. (already checked by caller)
* 3. Or TCP_CORK is not set, and TCP_NODELAY is set.
* 4. Or TCP_CORK is not set, and all sent packets are ACKed.
* With Minshall's modification: all sent small packets are ACKed.
*/
static bool tcp_nagle_check(bool partial, const struct tcp_sock *tp,
int nonagle)
{
return partial &&
((nonagle & TCP_NAGLE_CORK) ||
(!nonagle && tp->packets_out && tcp_minshall_check(tp)));
}
/* Minshall's variant of the Nagle send check. */
static bool tcp_minshall_check(const struct tcp_sock *tp)
{
return after(tp->snd_sml, tp->snd_una) &&
!after(tp->snd_sml, tp->snd_nxt);
}tcp_nagle_check Reading :
1. Parameters 1- partial, Packet is true, Big bag false.
2. Parameters 3- nonagle, At the top do_tcp_setsockopt Set parameters .
3. Main decision logic :
1) It is obvious that the big bag returns false,tcp_nagle_test The function returns true, stay tcp_write_xmit Continue with the rest of the sending process , That is to say TCP_NONAGLE Do not interfere with the sending of large packets .
2) If it's time to sock Open the TCP_NAGLE_CORK, Plug directly , Don't let send .
3) If nagle by 0( Indicates that... Is enabled by default NAGLE), There are packets in the link that have sent unanswered packets , Block up , Don't let send .
CORK and NAGLE The difference between
1. From the above 2) and 3) It can be seen that CORK and NAGLE The difference between ,CORK Packets in the link are not allowed , and NAGLE The link is allowed to have only one unacknowledged packet .
2. Mechanism implementation CORK Priority over NAGLE, If set at the same time CORK and NAGLE, that CORK take effect ,NAGLE invalid .
CORK The configuration of the algorithm is also in do_tcp_setsockopt Realized , No more analysis .
Legacy 2 A question
1. NAGLE perhaps CORK After opening , The data was extended to send , So what are the trigger logics to be sent next time ? They need delay How long to send ?
2. NAGLE perhaps CORK In what business or scenario is it beneficial to start ?
边栏推荐
- 123. 买卖股票的最佳时机 III
- More than ten years' work experience is recommended at the bottom of the box: how much does it cost to find a job? See here! Brothers and sisters are recommended to collect and pay attention
- Create a multithreaded thread class
- Physical layer introduction
- Golang reflection operation collation
- 网络安全审查办公室对知网启动网络安全审查
- Go coding specification
- VirtualBox虚拟机安装Win10企业版
- Address mapping of virtual memory paging mechanism
- Distributed basic concepts
猜你喜欢

Rip/ospf protocol notes sorting

OSI notes sorting

188. 买卖股票的最佳时机 IV
Talking about the range of data that MySQL update will lock

Microsoft Certification (dynamic 365) test

Hongxiang Yunteng is compatible with dragon lizard operating system, and the product runs stably

What does CTO (technical director) usually do?

Appium desktop introduction

Station B takes goods to learn from New Oriental

yeb_ Back first day
随机推荐
Static routing job
虚拟货币7个月蒸发2万亿美元,“马斯克们”终结15万人暴富梦
Haitai Advanced Technology | application of privacy computing technology in medical data protection
Mr. Hu Bo, CIO of weiduomei, a scientific innovator: digitalization is a bloodless revolution, and the correct answer lies in the field of business
B站带货当学新东方
Debugging Analysis of Kernel panics and Kernel oopses using System Map
Php-pdo parameter binding problem
Read all text from stdin to a string
memcached全面剖析–3. memcached的删除机制和发展方向
Use of kubernetes storage volumes
Foundations of Cryptography
Simpledateformat thread unsafe
Alibaba cloud lightweight servers open designated ports
TCP Jprobe utilization problem location
Shrimp skin test surface treated
Am, FM, PM modulation technology
Web automation: web control interaction / multi window processing / Web page frame
Selenium crawl notes
[cloud native learning notes] learn about kubernetes' pod
What does CTO (technical director) usually do?