Course contents 29 / 32

29 / 32 · Networking

Computer networks

How bytes travel between machines: protocol layers and encapsulation, Ethernet and MAC addresses, IP addresses with subnets and routing, ports, UDP and TCP with its handshake and its flow and congestion control, DNS, and why latency and bandwidth limit transfers differently.

On this page 9

The previous note gave a container its own view of the machine, down to its own network interfaces, and left open what those interfaces connect to. Every channel the course has built so far, pipes, shared memory and local sockets, joins processes that share one kernel. A network joins processes on different machines, which share nothing but a medium that carries bits: a cable, a fibre, a radio channel. This note follows one message from a process on one machine to a process on another: the layers it passes through, the address each layer uses, the two transport protocols, how a name becomes an address, and what limits how fast the message arrives. The next note writes the programs at both ends.

Notation. A message has SS bytes. A link has bandwidth BB in bits per second and one-way latency LL in seconds; the round-trip time RTT\mathrm{RTT} is the time for a message to go and an answer to come back, about 2L2L. An IPv4 address with a prefix length nn is written a.b.c.d/n. 1 kB=1031\ \text{kB} = 10^3 bytes, 1 MB=1061\ \text{MB} = 10^6 bytes, and 1 Mbit/s=1061\ \text{Mbit/s} = 10^6 bits per second.

Baseline: one wire between two machines#

The simplest network is two computers joined by one cable. One side drives a signal, the other samples it, and bits cross. For two machines and one program on each, this works. It breaks in six ways as soon as the network grows:

  1. More than two machines on one medium. Every machine sees every signal. A frame needs the address of the machine it is for.
  2. Errors. Noise flips bits. The receiver needs a way to notice that a frame arrived damaged.
  3. Many networks. No cable reaches the whole world. A message must cross intermediate machines that decide where to send it next, and it needs an address that makes sense beyond the local cable.
  4. Many programs per machine. A machine runs a web server and a mail server; the message must say which program it is for.
  5. Loss and reordering. Across a path of many links, messages get dropped, duplicated and overtaken. Many programs need every byte, in order.
  6. Names. People remember example.org, not a 32-bit number.

Each problem is solved once, by one layer of software, and the layers are stacked.

Layers: who solves which problem#

Two layer models are in use. The OSI reference model of ISO and the ITU (X.200) has seven layers; the Internet model of RFC 1122 has four. The Internet’s protocols won, but OSI’s numbers survived in everyday speech: a “layer 2 switch” works with Ethernet frames, a “layer 7 load balancer” with HTTP requests.

OSI layerInternet layerUnit of dataAddressExamples
7 application, 6 presentation, 5 sessionapplicationmessagename, URLHTTP, DNS, SSH
4 transporttransportsegment (TCP), datagram (UDP)portTCP, UDP
3 networkinternetpacketIP addressIPv4, IPv6, ICMP
2 data linklinkframeMAC addressEthernet, Wi-Fi
1 physicalbelow the link layerbitsnonecable, fibre, radio

The layers also map onto the parts of a computer that the course has met. The link layer lives in the network adapter, an I/O device that moves frames to and from memory by DMA and raises an interrupt when one arrives, and in its driver. The internet and transport layers live in the kernel. The application layer is ordinary user code in a process. The boundary between the transport layer in the kernel and the application is a system-call interface, the socket API of the next note.

Encapsulation: headers around a payload#

Worked example. An application sends 1,460 bytes over TCP on Ethernet.

Figure

Encapsulation: each layer wraps the data in its own header

The headers of four layers around the application dataFour rows, top to bottom: application, TCP, IP, Ethernet. The first row holds only the data, 1,460 bytes. Each row below adds a header on the left: TCP 20 bytes, IP 20 bytes, Ethernet 14 bytes, and the last row also has a 4-byte FCS on the right. Sizes are written at the right: segment 1,480, packet 1,500, frame 1,518 bytes. Below: with the preamble and gap the frame occupies 1,538 bytes on the wire, 94.9 percent of it data.application1,460 Bdata1,460 B1transport (TCP)segment 1,480 BTCP20 Bdata1,460 B2internet (IP)packet 1,500 BIP20 BTCP20 Bdata1,460 B3link (Ethernet)frame 1,518 BEthernet14 BIP20 BTCP20 Bdata1,460 BFCS4 B4on the wire also an 8 B preamble and a 12 B gap: 1,538 B, of which 94.9 % is datareceiving: each layer removes its own header, in reverse order, and passes the rest up5The headers of four layers around the application dataFour rows, top to bottom: application, TCP, IP, Ethernet. The first row holds only the data, 1,460 bytes. Each row below adds a header on the left: TCP 20 bytes, IP 20 bytes, Ethernet 14 bytes, and the last row also has a 4-byte FCS on the right. Sizes are written at the right: segment 1,480, packet 1,500, frame 1,518 bytes. Below: with the preamble and gap the frame occupies 1,538 bytes on the wire, 94.9 percent of it data.application1,460 Bdata1,460 B1transport (TCP)segment 1,480 BTCP20 Bdata1,460 B2internet (IP)packet 1,500 BIP20 BTCP20 Bdata1,460 B3link (Ethernet)frame 1,518 BEth14 BIP20 BTCP20 Bdata1,460 BFCS4 B4+ 8 B preamble + 12 B gap:1,538 B on the wire, 94.9 % datareceiving: each layer removesits own header, in reverse order5
One full TCP segment on Ethernet. The application hands over 1,460 bytes (1); TCP adds a 20-byte header, giving a 1,480-byte segment (2); IP adds another 20 bytes, and the 1,500-byte packet exactly fills the Ethernet MTU (3); Ethernet adds a 14-byte header in front and a 4-byte checksum behind, giving a 1,518-byte frame (4). The receiver strips the headers in reverse order (5). The boxes are not drawn to scale: the data is 73 times as long as the TCP header.
  1. The application hands 1,460 bytes to TCP.
  2. TCP adds a 20-byte header (without options): source and destination ports, a sequence number, an acknowledgement number, a window and a checksum. The result is a 1,480-byte segment.
  3. IP adds a 20-byte header: source and destination IP addresses, a time-to-live counter, the number of the protocol inside (6 for TCP) and a header checksum. The result is a 1,500-byte packet, exactly the largest payload an Ethernet frame may carry, its MTU (maximum transmission unit).
  4. Ethernet adds a 14-byte header (destination MAC address, source MAC address, and a 2-byte type, 0x0800 for IPv4) and a 4-byte frame check sequence, a CRC-32 of the frame. The result is a 1,518-byte frame.
  5. The receiver checks the CRC and strips the Ethernet header, then the IP header, then the TCP header, and hands the 1,460 bytes to the receiving process.

On the wire, each frame is also preceded by an 8-byte preamble and start delimiter and followed by a 12-byte gap of silence, so it occupies 1,5381{,}538 byte times, of which 1,460/1,538=94.9 %1{,}460 / 1{,}538 = 94.9\,\% is data. Headers are cheap for large messages and expensive for small ones. A single byte of data gives a 41-byte IP packet; Ethernet pads its payload to the 46-byte minimum, so the frame has the minimum size of 64 bytes and occupies 84 byte times, of which 1/84=1.2 %1/84 = 1.2\,\% is data.

Every Ethernet or Wi-Fi adapter has a 48-bit MAC address (medium access control), written as six bytes in hexadecimal, for example 02:1a:2b:3c:4d:5e. Manufacturers get blocks of addresses from the IEEE, whose first three bytes identify the manufacturer, so every adapter leaves the factory with a unique address. Two bits of the first byte have meanings of their own: bit 0 marks a group (multicast) address, and bit 1 a locally administered one, set by software rather than burned in at the factory; the 02 of the example has bit 1 set. The address ff:ff:ff:ff:ff:ff is the broadcast address: a frame sent to it goes to every machine on the local network.

A switch joins the cables of a local network. It reads the source address of every frame it receives and remembers which port that address came from; it then forwards each frame only out of the port where its destination lives, and floods a frame to all ports while the destination is still unknown. A receiver that finds a CRC mismatch drops the frame silently. Ethernet detects errors but does not repair them: retransmission, when a program needs it, is the job of a higher layer.

MAC addresses work only on one local network. To send an IP packet to a neighbour, a host must first learn the neighbour’s MAC address, and the Address Resolution Protocol (ARP, RFC 826) does that: the host broadcasts “who has 192.168.10.1? tell 192.168.10.77”, the owner of that IP address answers with its MAC address, and the answer is cached for a while.

The network layer: IP addresses, subnets and routing#

An IPv4 address is 32 bits, written as four bytes in decimal: 192.168.10.77 is 0xC0A80A4D. Every IP header stores it, like every multi-byte number in a protocol header, in big-endian network byte order, whatever the byte order of the machine (the byte-order note explains the two orders). 2322^{32} is about 4.3 billion addresses, fewer than the devices that want one; two answers to the shortage are NAT, below, and IPv6, whose addresses have 128 bits (RFC 8200). This note uses IPv4 throughout.

Subnets and CIDR#

An IP address has two parts: a network prefix, shared by every host on one local network, and a host part that tells the hosts apart. Since CIDR (classless inter-domain routing, RFC 4632) the split can fall after any bit. The prefix length nn is written after a slash, and the netmask is nn ones followed by 32−n32 - n zeros.

Worked example. Take 192.168.10.77/26. The first three bytes are all network bits; the split falls inside the last byte.

  • The mask is 26 ones and 6 zeros: 255.255.255.192, since the last byte is 110000002=19211000000_2 = 192.
  • The last byte of the address is 77=01001101277 = 01001101_2. The network address is the bitwise AND of address and mask: 01001101&11000000=01000000=6401001101 \mathbin{\&} 11000000 = 01000000 = 64, so the network is 192.168.10.64/26.
  • The broadcast address sets every host bit to one: 011111112=12701111111_2 = 127, so 192.168.10.127.
  • The host part has 26=642^6 = 64 values. The all-zeros one names the network and the all-ones one is the broadcast address, which leaves 64−2=6264 - 2 = 62 hosts, 192.168.10.65 to 192.168.10.126.
Figure

The subnet 192.168.10.77/26, bit by bit

Address, mask, network address and broadcast address in binaryFour rows of 32 bit cells. The first 26 cells are blue, the last 6 amber. The address 192.168.10.77, the mask 255.255.255.192, the network address 192.168.10.64 and the broadcast address 192.168.10.127. The rows differ only in the last six bits.network: 26 bitshost: 6 bitsaddress192.168.10.77110000001010100000001010010011011mask /26255.255.255.192111111111111111111111111110000002network192.168.10.64110000001010100000001010010000003broadcast192.168.10.127110000001010100000001010011111114hosts: .65 to .126, 2⁶ − 2 = 62 addresses192.168.10.130: last octet 10000010 starts with 10, not 01, so it is on another subnet5Address, mask, network address and broadcast address: the last octet in binaryFour rows. The first three octets are written in decimal and the last octet as eight bit cells: the first two blue (network), the last six amber (host). Address 01001101, mask 11000000, network address 01000000, broadcast address 01111111.networkhost: 6 bitsaddress192.168.10.77192.168.10.010011011mask /26255.255.255.192255.255.255.110000002network address192.168.10.64192.168.10.010000003broadcast address192.168.10.127192.168.10.011111114hosts: .65 to .126,2⁶ − 2 = 62 addresses192.168.10.130: octet 10000010 startswith 10, not 01: another subnet5
The prefix /26 splits the 32 bits of the address into 26 network bits (blue) and 6 host bits (amber) (1). The mask is 26 ones and 6 zeros (2). The bitwise AND of address and mask is the network address 192.168.10.64 (3), and setting every host bit to one gives the broadcast address 192.168.10.127 (4). Between them lie 62 host addresses, .65 to .126; the address 192.168.10.130 differs already in the network bits and is on another subnet (5).

The mask decides how a host sends a packet. If the destination has the same network address as the host, it is on the same local network: the host finds its MAC address by ARP and sends the frame directly. Otherwise the host sends the frame to its default gateway, a router on the local network. From 192.168.10.77/26, the destination 192.168.10.130 has last byte 10000010210000010_2, network address 100000002=128≠6410000000_2 = 128 \ne 64, so the packet goes to the router even though the first three bytes match.

Routing: longest-prefix match#

A router is a machine with interfaces on several networks. For each packet it looks up the destination address in a forwarding table of prefixes, picks the entry with the longest prefix that matches, and sends the packet to that entry’s next hop.

Worked example. A router has three entries (the address 203.0.113.1 is from a block reserved for documentation, RFC 5737):

PrefixNext hop
0.0.0.0/0upstream router 203.0.113.1
192.168.10.0/24interface eth0
192.168.10.64/26interface eth1

The destination 192.168.10.77 matches all three entries, and /26 is the longest, so the packet leaves through eth1. The destination 192.168.10.5 matches /24 and /0 and leaves through eth0. The destination 198.51.100.7 matches only /0, the default route, and goes upstream. Each router on the way repeats the lookup with its own table; none of them knows the whole path.

IP delivery is best effort: a router whose queue is full drops packets, two packets can take different paths and arrive out of order, and nothing tells the sender. Each router decrements the header’s time-to-live (TTL) counter and drops a packet whose counter reaches zero, sending an ICMP “time exceeded” message back; this ends routing loops, and the traceroute tool uses it to list the routers on a path.

Two ranges are special. The private ranges 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 (RFC 1918) are reused inside many homes and companies and are never routed on the Internet; a NAT router rewrites their addresses and ports into its own public address on the way out. The loopback range 127.0.0.0/8 never leaves the machine: a packet to 127.0.0.1 is delivered by the kernel to a process on the same host, which is how the next note tests a client and a server on one computer.

Ports: which process on the host#

An IP address names a host. A port, a 16-bit number in the TCP and UDP headers, names an endpoint inside the host, and the kernel delivers each arriving segment to the socket bound to its destination port. A TCP connection is identified by five values, the protocol and both address–port pairs, so a server on port 443 can hold thousands of connections at once: they differ in the client’s address or port.

RFC 6335 divides the range into three parts:

  • system (well-known) ports, 0 to 1023, assigned to standard services: 22 for SSH, 53 for DNS, 80 for HTTP, 443 for HTTPS;
  • user (registered) ports, 1024 to 49151, assigned on request, such as 5432 for PostgreSQL;
  • dynamic ports, 49152 to 65535, never assigned, for temporary use.

A client rarely chooses its port: the kernel gives it an ephemeral port from a configured range (on Linux 32768 to 60999 by default). On Linux, binding a port below 1024 needs root or a capability, a privilege check of the kind described in the note on users and permissions: an ordinary user should not be able to pose as a system service by taking its port.

The transport layer: UDP and TCP#

IP moves packets between hosts, unreliably. The transport layer turns that into a service for processes, and the Internet offers two.

UDP: one datagram, no promises#

UDP (RFC 768) adds an 8-byte header to IP: source port, destination port, length and checksum. Each send produces one datagram, delivered whole or not at all; nothing is retransmitted, ordered or acknowledged, and there is no connection to set up. A UDP header for 12 bytes of data from port 5000 to port 53 is the eight bytes 13 88 00 35 00 14 00 00: the two ports and the length 8+12=208 + 12 = 20 in big-endian, and a checksum left at zero, which IPv4 allows. UDP suits short request–reply exchanges where a lost message can simply be resent (DNS), and real-time traffic where a late packet is useless (voice, video, games). QUIC, which carries HTTP/3, builds its own reliability on top of UDP in user space.

TCP: a reliable byte stream#

TCP (RFC 9293) gives two processes a connection: a reliable, ordered, two-way stream of bytes. It has no message boundaries: two send calls of 100 bytes may arrive as one read of 200 bytes, or as three reads. It builds this on top of IP with four mechanisms.

The three-way handshake. Each side picks an initial sequence number (ISN), random to make old or forged segments unlikely to fit, and must learn the other’s.

  1. The client sends a segment with the SYN flag and its ISN, say x=1000x = 1000.
  2. The server answers with SYN and ACK set, its own ISN y=5000y = 5000, and the acknowledgement x+1=1001x + 1 = 1001, the next byte it expects.
  3. The client acknowledges with ACK y+1=5001y + 1 = 5001.

A SYN counts as one byte of sequence space, which is why the acknowledgements are x+1x + 1 and y+1y + 1. If the client now sends 100 bytes, the segment carries sequence number 1001, and the server’s acknowledgement is 1101. Two messages would not be enough: after a two-way exchange the server would not know whether the client received its ISN. The handshake costs one round trip before any data can flow. Closing takes a FIN from each side, each acknowledged; the next note draws the whole exchange next to the system calls that cause it.

Reliability. Sequence numbers count bytes. The receiver acknowledges cumulatively, “everything up to byte kk has arrived”, puts out-of-order segments back in order, and discards duplicates. The sender keeps every unacknowledged byte and retransmits it when a timer expires; the timeout follows a smoothed estimate of the round-trip time, and RFC 6298 recommends that it never fall below one second. Three duplicate acknowledgements of the same byte also trigger a retransmission, without waiting for the timer (fast retransmit). The checksum covers header and data.

Flow control. The receiver advertises a window: how many more bytes its buffer can hold. The sender keeps at most that many unacknowledged bytes in flight, so a fast sender cannot overrun a slow reader. The window field has 16 bits, so it says at most 65,535 bytes, unless both sides agree on a window scale option that multiplies it by up to 2142^{14} (RFC 7323).

Congestion control. The bottleneck may be neither the sender nor the receiver but a router in between. Each sender keeps a congestion window and sends at most the smaller of the two windows. It starts with 10 segments (RFC 6928) and doubles the congestion window every round trip (slow start), then grows it by one segment per round trip (congestion avoidance); a loss is read as a sign of congestion, and the window is cut, by half in the classic scheme (Jacobson, 1988; RFC 5681). Additive increase and multiplicative decrease make competing connections converge to a fair share. Linux uses CUBIC by default, which grows the window as a cubic function of the time since the last loss but keeps the same structure.

Worked example. From 10 segments of 1,460 bytes, 14,600 bytes, slow start reaches a window of 1 MB after ⌈log⁡2(106/14,600)⌉=⌈6.1⌉=7\lceil \log_2(10^6 / 14{,}600) \rceil = \lceil 6.1 \rceil = 7 round trips, if no loss interrupts it. On a path with an RTT of 80 ms that is more than half a second before the connection can use its full speed.

UDPTCP
Connectionnonethree-way handshake, one RTT
Unitdatagram, boundaries keptbyte stream, no boundaries
Deliverymaybe, in any orderevery byte, in order, or an error
Rateas fast as the sender sendslimited by the receiver’s window and the congestion window
Header8 bytes20 bytes and more
Typical useDNS, voice and video, games, QUICweb, mail, SSH, file transfer

DNS: from names to addresses#

The Domain Name System (RFC 1034 and 1035) is a distributed database that maps names to records. Names form a tree read from right to left: the root, a top-level domain such as org, a domain such as example.org, a host such as www.example.org. Each zone of the tree is served by its own authoritative servers, which know its records: A for an IPv4 address, AAAA for IPv6, CNAME for an alias, MX for a mail server, NS for the servers of a sub-zone.

A program asks through the C library, by calling getaddrinfo. The library first reads the local hosts file, where localhost is defined as 127.0.0.1, and otherwise sends a query, usually one UDP datagram to port 53, to a recursive resolver configured on the machine, often run by the network provider. The resolver walks the tree on the program’s behalf: it asks a root server where the org servers are, asks an org server where the example.org servers are, asks one of those for the address, and returns it. Every answer carries a time to live, and the resolver caches it for that long, so popular names are answered without walking the tree at all.

Worked example. Let the program reach its resolver in an RTT of 2 ms, and the resolver reach the root, the org server and the authoritative server in RTTs of 20, 30 and 40 ms. With an empty cache, the lookup takes 2+20+30+40=922 + 20 + 30 + 40 = 92 ms, longer than the transfer of a small web page; with the answer cached at the resolver, it takes 2 ms. This is why DNS answers are cached at every level, in the resolver, the operating system and the browser.

Latency versus bandwidth#

How long does a message take? A simple model, for one message on an open path with no loss, is

T=L+8SB,\boxed{T = L + \frac{8S}{B}} ,

the latency before the first bit arrives plus the time to push all 8S8S bits through the link. Latency itself is a sum: propagation (light in fibre covers about 200 km per millisecond, so 2,000 km cost 10 ms each way), plus time spent waiting in router queues, plus processing. Bandwidth can be bought; propagation cannot (Patterson, 2004).

Worked example. Link A is near and slow: L=2L = 2 ms, B=100B = 100 Mbit/s. Link B is far and fast: L=38L = 38 ms, B=1B = 1 Gbit/s.

  • A 1 kB message: TA=2+0.08=2.08T_A = 2 + 0.08 = 2.08 ms and TB=38+0.008=38.008T_B = 38 + 0.008 = 38.008 ms. Latency dominates, and the slow link is 18 times faster.
  • A 1 GB message: TA=0.002+80=80.002T_A = 0.002 + 80 = 80.002 s and TB=0.038+8=8.038T_B = 0.038 + 8 = 8.038 s. Bandwidth dominates, and the fast link is 10 times faster.
  • The two times are equal when LA+8S/BA=LB+8S/BBL_A + 8S/B_A = L_B + 8S/B_B, that is, at 8S=(LB−LA)/(1/BA−1/BB)=0.036/(9×10−9)=4×1068S = (L_B - L_A) / (1/B_A - 1/B_B) = 0.036 / (9 \times 10^{-9}) = 4 \times 10^6 bits, S=500S = 500 kB, where both take 42 ms.
Figure

Transfer time: latency against bandwidth

Transfer time over two links against message sizeA log-log plot: message size from 100 bytes to 10 gigabytes, time from 0.1 millisecond to 1000 seconds. The curve of link A starts at 2 ms and rises more steeply; the curve of link B starts at 38 ms and rises less steeply. They cross at 500 kilobytes and 42 ms; to the left A is faster, to the right B.0.1 ms1 ms10 ms100 ms1 s10 s100 s1000 s100 B10 kB1 MB100 MB10 GBmessage size500 kB, 42 ms← latency dominatesbandwidth dominates →A: 2 ms, 100 Mbit/sB: 38 ms, 1 Gbit/sTransfer time over two links against message sizeA log-log plot: message size from 100 bytes to 10 gigabytes, time from 0.1 millisecond to 1000 seconds. The curve of link A starts at 2 ms and rises more steeply; the curve of link B starts at 38 ms and rises less steeply. They cross at 500 kilobytes and 42 ms; to the left A is faster, to the right B.0.1 ms1 ms10 ms100 ms1 s10 s100 s1000 s100 B10 kB1 MB100 MB10 GBmessage size500 kB, 42 ms← latencybandwidth →A: 2 ms, 100 Mbit/sB: 38 ms, 1 Gbit/s
The time to deliver one message of S bytes, T = L + 8S/B, over two links: A is near and slow (latency L = 2 ms, bandwidth B = 100 Mbit/s), B far and fast (38 ms, 1 Gbit/s). For small messages the time is almost the latency, and A wins by a factor of 18 at 1 kB; for large ones it is almost 8S/B, and B wins by a factor of 10 at 1 GB. The curves cross at S = 500 kB, T = 42 ms. The model leaves out the TCP handshake, the window and losses. Both axes are logarithmic.

TCP adds round trips to the model. The handshake costs one before the first byte; slow start costs several before the window is large; and the window must be large enough to fill the path. The number of bytes that are on their way at any moment on a full path is the bandwidth–delay product B⋅RTTB \cdot \mathrm{RTT}.

Worked example. At 100 Mbit/s and an RTT of 80 ms, B⋅RTT=108×0.08=8×106B \cdot \mathrm{RTT} = 10^8 \times 0.08 = 8 \times 10^6 bits, 1 MB. A sender limited to the unscaled maximum window of 65,535 bytes sends one window per round trip, so its throughput is at most 65,535×8/0.08=6.5565{,}535 \times 8 / 0.08 = 6.55 Mbit/s, 6.6 % of the link, however fast the link is. Window scaling exists for exactly this case.

For small transfers, then, the number of round trips matters more than the bandwidth. This is why browsers reuse connections, why TLS 1.3 shortened its handshake, and why content is served from machines near the reader.

What changed#

Problem on one wireSolved byMechanism
Which machine on the medium?link layer48-bit MAC address, switch learning, ARP
Damaged bitslink layer, then TCPCRC drops bad frames; TCP retransmits
Many networksinternet layerIP addresses, prefixes, longest-prefix routing
Which program on the machine?transport layer16-bit ports
Loss, duplication, reorderingTCPsequence numbers, acknowledgements, timers
A fast sender, a slow reader or a full routerTCPreceive window, congestion window
Human namesapplication layerDNS, cached by time to live

Limitations#

  • The four layers are cleaner on paper than in practice. NAT rewrites transport ports inside a network-layer device; TLS sits between TCP and the application; QUIC moves reliability and congestion control into a user-space library over UDP; tunnels and VPNs carry whole packets, even whole frames, inside other packets.
  • T=L+8S/BT = L + 8S/B ignores queues, losses, handshakes and windows. Real throughput is measured, not computed, and it changes from minute to minute.
  • The note uses IPv4. IPv6 has 128-bit addresses, no broadcast, and the Neighbor Discovery Protocol in place of ARP; its ideas of prefixes and routing are the same.
  • None of IP, UDP, TCP or classic DNS authenticates anything: a machine on the path can read, change or forge packets. TLS, and DNSSEC for names, add that protection above.
  • Wireless links lose packets for reasons other than congestion, which misleads loss-based congestion control; this is one motivation for newer schemes such as BBR.

What you should remember#

  • Layers solve one problem each and talk only to their neighbours; on the way out each wraps the data in its header, on the way in each strips its own. A full Ethernet frame carries 1,460 bytes of TCP data in 1,518 bytes.
  • A MAC address names an adapter on one local network; an IP address names a host anywhere; a port names an endpoint inside the host. ARP maps IP to MAC on the local network.
  • A prefix /n splits an address into network and host bits. Network address = address AND mask; a /n network has 232−n−22^{32-n} - 2 hosts. Routers forward by the longest matching prefix.
  • UDP: datagrams, no connection, no guarantees. TCP: a connection opened by a three-way handshake, a reliable ordered byte stream with no message boundaries, a receive window for flow control and a congestion window for the network.
  • DNS maps names to addresses through a tree of servers, walked by a recursive resolver and cached by time to live.
  • T=L+8S/BT = L + 8S/B: small messages are limited by latency, large ones by bandwidth. Filling a path needs a window of B⋅RTTB \cdot \mathrm{RTT} bytes in flight.

Code#

The numbers of the note, recomputed with the Python standard library. Every output is printed by the code and copied into the comments.

import ipaddress, math, socket, struct

# 1. Encapsulation: a full 1,460-byte segment and a 1-byte one
def frame(payload):
    packet = payload + 20 + 20                  # TCP header + IPv4 header, no options
    frame = 14 + max(packet, 46) + 4            # Ethernet header, payload padded to 46, FCS
    return packet, frame, frame + 8 + 12        # + preamble and start delimiter, + interframe gap
for p in (1460, 1):
    packet, f, wire = frame(p)
    print(p, packet, f, wire, f'{p / wire:.1%}')
# 1460 1500 1518 1538 94.9% | 1 41 64 84 1.2%

# 2. A /26 subnet, by hand and with ipaddress
addr = int(ipaddress.IPv4Address('192.168.10.77'))
mask = (0xFFFFFFFF << (32 - 26)) & 0xFFFFFFFF
net, bcast = addr & mask, addr | (~mask & 0xFFFFFFFF)
print(hex(addr), ipaddress.IPv4Address(mask), ipaddress.IPv4Address(net), ipaddress.IPv4Address(bcast))
# 0xc0a80a4d 255.255.255.192 192.168.10.64 192.168.10.127
n = ipaddress.ip_network('192.168.10.64/26')
print(n.num_addresses, n.num_addresses - 2, ipaddress.ip_address('192.168.10.130') in n)   # 64 62 False

# 3. Longest-prefix match
table = {'0.0.0.0/0': 'upstream 203.0.113.1', '192.168.10.0/24': 'eth0', '192.168.10.64/26': 'eth1'}
def route(dst):
    hits = [ipaddress.ip_network(p) for p in table if ipaddress.ip_address(dst) in ipaddress.ip_network(p)]
    return table[str(max(hits, key=lambda h: h.prefixlen))]
print([route(d) for d in ('192.168.10.77', '192.168.10.5', '198.51.100.7')])
# ['eth1', 'eth0', 'upstream 203.0.113.1']

# 4. A UDP header in network byte order: ports 5000 -> 53, 12 bytes of data
print(struct.pack('!HHHH', 5000, 53, 8 + 12, 0).hex(' '))   # 13 88 00 35 00 14 00 00

# 5. Transfer time T = L + 8S/B on two links, and where they cross
links = {'A': (0.002, 100e6), 'B': (0.038, 1e9)}       # one-way latency in s, bandwidth in bit/s
for size in (1e3, 1e9):                                  # bytes
    print(size, {k: round(L + 8 * size / B, 6) for k, (L, B) in links.items()})
# 1000.0 {'A': 0.00208, 'B': 0.038008} | 1000000000.0 {'A': 80.002, 'B': 8.038}
(LA, BA), (LB, BB) = links['A'], links['B']
print(round((LB - LA) / (1 / BA - 1 / BB) / 8))          # 500000 bytes: below this, A is faster

# 6. Bandwidth-delay product and the 64 KiB window, then slow start from 10 segments
B, rtt = 100e6, 0.080
print(B * rtt / 8, round(65535 * 8 / rtt / 1e6, 2))     # 1000000.0 bytes in flight; 6.55 Mbit/s at most
print(math.ceil(math.log2(1e6 / (10 * 1460))))           # 7 round trips to grow a window of 14,600 B to 1 MB

# 7. The name localhost resolves to the loopback address without asking any server
print('127.0.0.1' in {ai[4][0] for ai in socket.getaddrinfo('localhost', 80, socket.AF_INET)})   # True

Next#

The kernel now knows how to carry bytes to a process on another machine: it builds the headers, runs the handshake, retransmits what is lost and slows down when the network is full. What a program sees of all this is a file descriptor. The next note opens that descriptor with the socket API, writes a TCP server and client in C, and shows how one server handles many clients at once.

References#