Issue
Here's my understanding of incoming data flow in TCP/IP
- Kernel reads data to its buffer from network interface
- Kernel copy data from its buffer to TCP Socket Buffer, where Sliding Window works
- The program that is blocked by read() wakes up and copy data from socket buffer.
I'm a little bit confused about where does the sliding window locate, or is it the same as socket buffer
Solution
Linux does not handle TCP's sliding window as a separate buffer, rather as several indices indicating how much has already been received / read. The Linux kernel packet handling process can be described in many ways and can be divided to small parts as yo go deeper, but the general flow is as follows:
- The kernel prepares to receive data over a network interface, it prepares SKB (Socket Buffer) data structures and map them to the interface Rx DMA buffer ring.
- When packets arrive, they fill these preconfigured buffers and notify the kernel in an interrupt context of the packets arrival. In this context, the buffers are moved to a recv queue for the network stack to handle them out of an interrupt context.
- The network stack retrieves these packets and handles them accordingly, eventually arriving to the TCP layer (if they are indeed TCP packets) which in turn handles the window.
- See
struct tcp_sock
memberu32 rcv_wnd
which is then used intp->rcvq_space.space
as the per-connection space left in window. - The buffer is added to socket receive queue and is read accordingly as stream data in
tcp_recvmsg()
The important thing to remember here is that copies is the worst thing regarding performance. Therefore, the kernel will always (unless absolutely necessary) will avoid copies and use pointers instead.
Answered By - Creator Answer Checked By - Willingham (WPSolving Volunteer)