nullplan wrote:
On the send path, TCP and UDP send a packet to the appropriate IP layer (I think all OSes I have seen so far keep TCP over IPv4 and TCP over IPv6 separate). IP layer routes the packet, finds by the routing table what interface the packet is meant to go on and what the next hop's IP address is, checks if it knows the next hop's hardware address, if not it does the ARP thing if requested. And IPv6 would do the neighbor discovery. And then it calls down to the send routine of the interface.
To avoid scatter gather (which not all NICs support), I do the send process in two stages. First the TCP or UDP will request a buffer from the layer below (ipv4 or ipv6) with a given data size. Next, it will fill-out it's data part in the buffer and do a send request to the layer below. This will only require one allocate in the send process and doesn't need scatter-gather. The NIC can also decide to keep it's own physical buffers, and return one of those in response to the buffer allocation request.
Checking the route and generating ARPs will be done on buffer allocate, and then then the allocation will fail. So, the TCP/UDP never assembles the packet unless the route is clear.