asap diablo
asap diablo

Reputation: 178

What are the differences between Kernel Buffer, TCP Socket Buffer and Sliding Window

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

Upvotes: 6

Views: 10576

Answers (1)

Creator
Creator

Reputation: 431

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.

Upvotes: 11

Related Questions