当前位置:网站首页>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 ?
边栏推荐
- [cloud native learning notes] learn about kubernetes' pod
- 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
- memcached全面剖析–5. memcached的应用和兼容程序
- JMeter installation plug-in, adding [email protected] -Perfmon metric collector listener steps
- Record a deletion bash_ Profile file
- Rename and delete files
- Handwritten RPC the next day -- review of some knowledge
- Realization of truth table assignment by discrete mathematical programming
- Is the waiting insurance record a waiting insurance evaluation? What is the relationship between the two?
- Alibaba cloud schedules tasks and automatically releases them
猜你喜欢

I feel that I am bald again when I help my children with their homework. I feel pity for my parents all over the world

Handling of garbled JMeter response data - three solutions

After screwing the screws in the factory for two years, I earned more than 10000 yuan a month by "testing" and counterattacked

Failed to open after installing Charles without any prompt

Page replacement of virtual memory paging mechanism

Oauth1.0 introduction

JMeter parameterization

Read all text from stdin to a string

Pytest test framework II

Basic database syntax learning
随机推荐
After a few years in the testing industry, do you still know a little?
Subnet partition operation
Variable setting in postman
Alibaba cloud schedules tasks and automatically releases them
Handwritten RPC the next day -- review of some knowledge
Self signed certificate generation
After 5 months' test, it took 15K to come for an interview. When I asked, it was not worth even 5K. It was really
Hongxiang Yunteng is compatible with dragon lizard operating system, and the product runs stably
DHCP operation
PIXIV Gizmo
Background operation retry gave up; KeeperErrorCode = ConnectionLoss
Analysis of errors in JSON conversion using objectmapper
Format method and parse method of dateformat class
Go coding specification
JMeter response assertion
Selenium crawl notes
[cloud native learning notes] learn about kubernetes' pod
Network flow 24 questions (round table questions)
Concepts of kubernetes components
Nifi fast authentication configuration