Linux2.6 Core Stack Series--TCP Protocol 2. Receive

Source: Internet
Author: User

1. Queuing mechanism

When receiving an input TCP message, there are three queues:

Pending queue

Pre-queued Queue

Receive Queue

The Receive queue contains processed TCP data segments, which means that all of the protocol headers are removed and the data is being copied to the user application. The Receive queue contains all of the data segments received sequentially, and TCP data segments in the other two queues require further processing.

TCP messages are first processed by TCP_V4_RCV (). This function determines whether the message needs to be processed or queued in the pending queue and the pre-queued queue.

/* Transfer beginning import */int TCP_V4_RCV (struct Sk_buff *skb) {.../* first obtain a socket rotation lock, and when entering the routine, disable the lower half function because the routine is called from the net SOFTIRQ interrupt. */bh_lock_sock (SK);/* Lock socket in soft interrupt */ret = 0;/* If the process does not have access to the transport control block, the normal receive *//* then checks whether the socket is in use. When someone is using the socket, the (SK)->sk_lock.owner is 1. When you perform a read, write, or modify operation on a socket, the socket is in use. */if (!sock_owned_by_user (SK)) {/* * calls Tcp_prequeue to send the TCP message to the pre-queued queue. */if (!tcp_prequeue (SK, SKB)/* * If TCP cannot be queued, the data segment is processed directly. */ret = TCP_V4_DO_RCV (SK, SKB);} Else/* adds the message to the fallback queue, processing */sk_add_backlog (SK, SKB) when the user process unlocks the control block, Bh_unlock_sock (SK); ...}

Treatment of 2.tcp_rcv_established ()

The

does not cover all processing details here, only the processing and queuing mechanisms. The possibility of copying data directly to the user buffer is discussed first. If it is not possible, remove the TCP header and send the data segment to the receive queue for queuing.

/* When the connection is already established, process the received TCP message */int tcp_rcv_established (struct sock *sk, struct sk_buff *skb,struct tcphdr *th, unsigned len) { struct Tcp_sock *TP = Tcp_sk (SK); ...} else {/* has a data payload */int eaten = 0;/* Determines whether the segment being received can be copied directly to the user space *//* the ordinal of the segment being received is equal to the first ordinal of the segment that has not been copied from the kernel space to the user space, that is, the receive queue should be empty in the *//* TCP segment  The user data length is less than the remaining available amount of */if (tp->ucopy.task = = Current && Tp->copied_seq = = tp->rcv_nxt && len -Tcp_header_len <= tp->ucopy.len && sock_owned_by_user (SK)) {/* locks are held by the current process */__set_current_state (Task_run NING);/* Copies the SKB data to the user buffer */if (!tcp_copy_to_iovec (SK, SKB, Tcp_header_len)) {if (Tcp_header_len = = (sizeof (struct TCPHD R) + tcpolen_tstamp_aligned) && tp->rcv_nxt = = tp->rcv_wup)/* update timestamp */tcp_store_ts_recent (TP); TCP_RCV _RTT_MEASURE_TS (TP, SKB);/* Update round-trip time */__skb_pull (SKB, Tcp_header_len);/* The next expected segment sequence number *//* will tp->rcv_ The NXT is updated to the end sequence number of the processed message. */TP-&GT;RCV_NXT = TCP_SKB_CB (SKB)->end_seq;net_inc_stats_bh (linux_mib_tcphphitstouser); eaten = 1;}} if (!eaten{.../* move the pointer, skip the TCP header, i.e. remove the TCP headers */__skb_pull (Skb,tcp_header_len);/* Add the packet to the receive queue and cache it, waiting for the process to actively read the */__skb_queue_tail ( &sk->sk_receive_queue, SKB);/* Set the owner of the SKB as the current socket, update the total number of receive buffers used and the pre-allocated cache length */sk_stream_set_owner_r (SKB, SK);/* Update rcv_ NXT is the end sequence number for the segment */TP-&GT;RCV_NXT = TCP_SKB_CB (SKB)->end_seq;} ... return 0;}

3.

Too much content to be continued

Linux2.6 Core Stack Series--TCP Protocol 2. Receive

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.