6

Here's my understanding of incoming data flow in TCP/IP

  1. Kernel reads data to its buffer from network interface
  2. Kernel copy data from its buffer to TCP Socket Buffer, where Sliding Window works
  3. 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

asap diablo
  • 178
  • 2
  • 13
  • I get to know that sliding window is a part of TCP Socket Buffer, which means that the size of sliding window can never be greater than the size of TCP Buffer. – asap diablo Feb 21 '19 at 09:24

1 Answers1

8

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:

  1. 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.
  2. 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.
  3. 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.
  4. See struct tcp_sock member u32 rcv_wnd which is then used in tp->rcvq_space.space as the per-connection space left in window.
  5. 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.

Armen Michaeli
  • 8,625
  • 8
  • 58
  • 95
Creator
  • 401
  • 2
  • 9
  • In step 2 by stating "the buffers are moved to recv queue for the network stack", is it implying that for network stack, there's a memory area holding the packets coming from socket buffer? @Knightingale – asap diablo Dec 10 '18 at 02:15
  • 1
    It's not a "memory area", rather it's a linked list pointing to packets that arrived and are linked as a queue for message handling. – Creator Dec 10 '18 at 11:04