The network: TCP as a library in the process
The first layer of the network is the base: the frame service, the network service, endpoints of datagrams. Glossary defines the terms.
The idea in plain words. No process opens a connection because it runs. The root service gives a process a grant that names what it can reach: one remote (a client grant) or one port on which it answers whoever comes (a listen grant). The network service checks each datagram and segment against that grant; the protocol itself, TCP or QUIC, is a library inside the process, so a fault in it harms only that process.
For whom. A reader who writes a process that uses the network, and a reader who changes the network service or the TCP library.
Read before. Kernel properties: Kernel off the data path and "20. No ambient access: the other holes to close" (line 3). Architecture: Network steering service. After this guide, QUIC.
The map of this guide. "The principles" and "The parts" give the model and the processes. "The path of a datagram and of a segment" says which process touches the bytes, hop by hop, and who checks what. "The segment endpoint" is the grant of TCP and its checks. "The TCP library" is the protocol in the process. "The tests" and "The HTTP server" show it at work. "The listen grant of UDP" and "QUIC: what it reuses, and what it needs" lead to QUIC.

The principles
The kernel carries no network data and holds no network protocol. A process that talks over the network runs the protocol itself, as a library, and one service of user space only checks each datagram or segment against the grant of the process. Three rules follow:
- The network stack is a library in the process that uses the network (property 5). A fault in TCP or QUIC harms only the process that runs it.
- The network service is on the control path of each datagram and each segment: it reads the headers and checks them against the grant. It is on the data path for one copy of what arrives, and for nothing else: on send, the device reads the data from the memory of the process, the service copies only 8 bytes of the header for its check, and it computes no checksum of the bytes and writes no header of UDP or TCP. The reason is the device of the first machine, which has one pair of queues (section "The path of a datagram and of a segment").
- The network has UDP, TCP and QUIC, and a server as a scenario:
serverspeaks HTTP over TCP, andquic_echoandremotespeak QUIC over UDP.
The parts
Each part of the network is a process or a library of user space. The table names each one, where its code is, and what it does.
| Part | Where | What it does |
|---|---|---|
| TCP library | src/library/modus/tcp | The state machine, the windows, the timers, the reassembly, the congestion control. Pure code over memory of the caller: no kernel operation, no allocation. The modus tool uses the same package on the host |
| Network service | src/modules/system/network | Gives each process with the grant one endpoint. It checks the header of each datagram and each segment against the grant, writes and checks the IPv4 header, and copies a datagram or a segment that arrives one time. On send it copies 8 bytes of the header, and the device reads the rest from the datagram region. It computes no checksum of UDP or TCP and keeps no state of a connection |
| Protocol library | src/library/modus/network | For the network service: Ethernet, ARP, IPv4, ICMP echo, and the checks of the header of UDP and TCP (parse). For a process with a datagram endpoint: the UDP header and its checksum (udp_header_write, udp_data_read) |
| Server | src/modules/demonstrations/server | A process with a listen grant on the TCP port 80. One thread serves all its connections from one connection table. It speaks a small HTTP/1.1 |
| Client | src/modules/demonstrations/fetch | A process with a segment grant of one remote. It fetches pages and measures |
| Module file service | src/modules/system/module_files | A client with the same grant: it fetches a module file and stores it in the store module_files of its principal (Module contract: A module from a disk) |
| QUIC server | src/modules/demonstrations/quic_echo | A process with a listen grant of UDP on the port 4433: QUIC in the process (QUIC: QUIC on the network (the fourth layer, built, with Retry and the budgets of work)) |
| Remote service | src/modules/system/remote | The calls of the host: a QUIC server with a listen grant of UDP on the port 7100 that admits the address of the host (10.0.2.2) only. Each call is one QUIC stream (QUIC: remote over QUIC (built)) |
| Peer | tools/modustool/machine/peer.odin | The gateway of the test machine. It is also a TCP client that the tool drives, with loss, reorder and duplicates that the tool chooses, and the QUIC client of the remote service |
The kernel has no network code.
The network service has one static address, 10.0.2.15/24, and the gateway 10.0.2.2 (the network of QEMU). It has one thread, which waits on the frame ring, on the control ring of the root service and on the command queue of each endpoint. Its first line is "network: ok, the address 10.0.2.15/24 ..., the gateway 10.0.2.2, the link is up, ...", then one line for each endpoint, such as "network: an endpoint for server: TCP, the listen port 80, the remotes that send to it". Each line of an endpoint is a log fact of network_service with the process that owns the endpoint as the field owner: Endpoint_Listens_Tcp and the other forms of a grant, Endpoint_Grant_Changed_To_None, Endpoint_Ended with the datagrams sent and received. So log.records(0) |> facts(network_service.Endpoint_Ended) |> where(owner = "server") gives the end of each endpoint of server (Developer tools: The log). A frame that it refuses becomes a problem row (datagram.Problem_Row) of its endpoint, or of the machine, with the layer, the field and the rule.
It keeps the hardware address of each neighbor in a table of 16 (ARP_CAPACITY of src/library/modus/network). The table learns only an address that a request of the service asked, so an ARP frame that nobody asked for changes nothing, also not the entry of the gateway.
The network service holds the frame service with the right Read. When the frame service stops, and when it does not answer a command of the start in 60 s, the network service writes "network: the frame service stopped: the network device moves no frame, and this service stops" (or the command that had no answer) and stops. The root service then writes "root: the network service stopped: no process with an endpoint of the network starts until the machine starts again; the other processes start", or "did not start" when the frame service cannot start, as on a machine with no network device. Each later process with the grant Network is refused, with no new start of the two services (A missing or silent part of the machine in the repository). The refusal follows any stop of the network service, also a fault of the network service itself, and lasts until the next start of the machine. A start that fails for a full table, a short budget or short memory is not refused for ever: only an absent network device is.
The path of a datagram and of a segment
A datagram or a segment passes through several processes between the device and the process that uses it. This section names each hop, the bytes that the hop reads or writes, and the party that checks each field. Two terms sort the hops (Glossary): the data path is the code that reads or writes the bytes of the data; the control path of a packet is the code that reads the headers and decides where the bytes go.
The payload of a frame is what follows the IPv4 header: a whole datagram with its UDP header, or a whole segment with its TCP header. UDP and TCP take the same path through the network service: the process writes and reads the whole payload, and the network service treats the two alike.
A datagram or a segment that arrives
| Hop | What runs | Bytes of the payload that it touches |
|---|---|---|
| The device | It writes the frame into a receive place of the frame region | Written one time by the device |
| The frame service | It reads the used ring and gives the length of the frame to the network service | None: it reads no frame |
| The network service | network.parse checks the headers of Ethernet and IPv4 (with the IPv4 checksum, 20 bytes), then the lengths of the header of UDP or TCP | The header only: the two ports, the length field of UDP, the data offset and the flags of TCP |
| The network service | It finds the endpoint by the local port, checks the remote against the grant, and admits a remote of a listen grant | None |
| The network service | deliver copies the payload into the place that the process gave | One pass: the copy |
| The process | TCP: tcp.step checks the TCP checksum, then reads the header and copies the data into the receive buffer of the connection. UDP: network.udp_data_read checks the UDP checksum, when the process asks for it | The passes of the process itself |
A datagram or a segment that leaves
| Hop | What runs | Bytes of the payload that it touches |
|---|---|---|
| The process | It writes the payload in a place of its datagram region: the header of UDP or TCP, the data, and the checksum over both (tcp.header_write, network.udp_header_write) | The passes of the process itself |
| The network service | It copies the first 8 bytes of the payload from the region to a send place of the frame region | The header only: 8 bytes |
| The network service | header_granted reads the two ports, and the length field of UDP, in its own copy, and checks them against the endpoint and the grant | The header only, in the copy |
| The network service | network.ipv4_frame writes the headers of IPv4 and Ethernet before the copy | None |
| The frame service | It gives the device a chain of three parts: the virtio header, the headers in the frame region, the rest of the payload in the datagram region, which it holds as a read region | None |
| The device | It reads the frame region and the datagram region | Read one time by the device |
| The frame service | When the device used the frame, it completes the frame; the network service then completes the Send of the process, which can write its place again | None |
The passes of the network service
A pass is one read or one write of each byte of a payload by one process. The table gives the passes of the network service for each byte, in this form and in the form with the checksums of the transport in the network service.
| Direction | Protocol | With the checksums in the network service | This form |
|---|---|---|---|
| Arrives | TCP | 2: the TCP checksum, then the copy | 1: the copy |
| Arrives | UDP | 2: the UDP checksum, then the copy | 1: the copy |
| Leaves | TCP | 1: the copy | 0 over the data: a copy of 8 bytes of the header |
| Leaves | UDP | 2: the copy, then the UDP checksum | 0 over the data: a copy of the 8 bytes of the header |
The network service counts its passes. For each endpoint and each direction it counts the datagrams or the segments, their bytes, the bytes of each pass, and the bytes of the data among them (data_pass in src/modules/system/network, called at each place that reads the bytes). When an endpoint ends, the log has two lines: a measure, "network: a measure: the endpoint of server, TCP: the passes of this service over the data for each payload byte: 1.00 on receive (...), a fraction on send (...); the bytes of the data that this service read on send: 0", and the structural fact, "network: the endpoint of server, TCP: this service read each payload byte one time on receive, for the copy, and on send only the 8 bytes of each header that it checks, and 0 bytes of the data; it computed no checksum: as it must". The machine tests of net, server and quic_echo, and the test iommu for net, wait for the second line. It is a count of passes, not a speed.
The one copy on receive cannot leave with this device. The device chooses the place of a frame before any process knows whose frame it is, so one process that is not the receiver must read the headers, and the bytes must then move to the receiver. A device that gives each endpoint its own receive queue, and steers frames by the destination port, removes the copy on receive too; the device of the test machine has no such queues.
The send with no copy
The device reads the data of a datagram or a segment where the process wrote it. The rules:
- When the remapping unit of the network device gives device windows (
Device_Createof its registers says so: ), the datagram region of each endpoint is not contiguous: the kernel gives it a device window where its pages follow each other, so the start of a process does not need 512 KiB of contiguous memory. Else (no IOMMU, a device that no unit covers, a unit of fewer than 39 bits of address), the root service makes the region contiguous: the device needs one address for it. The network service gives the region to the frame service with the right Read only, as a read region at the place of the endpoint (Read_Region_Addof the frame contract). The frame service refuses a read region with the right Write. - On a machine with the IOMMU, the frame service declares the read region with
Memory_Physicalthrough a handle with Read only: the network device reads the region and cannot write it. - The
Sendof a process completes when the device read the frame, not before. A process writes a place again only after the completion of itsSend. A process that writes it before changes what goes on the wire, and harms only its own packet. - The process writes the checksum, as before. The device computes none.
- When an endpoint ends, the frame service keeps its handle of the region until the last frame that reads it is complete, then closes it. The memory of the region stays while the device reads it, whatever the process and the network service close.
The design: The network service and the data path: The send with no copy in the repository.
Who checks what
| Field | Who checks it | Where |
|---|---|---|
| The headers of Ethernet and IPv4 of a frame that arrives, with the IPv4 checksum | The network service | network.parse |
| The lengths of the header of UDP or TCP of a payload that arrives | The network service | network.parse |
| The local port and the remote of a payload that arrives, against the endpoints and their grants | The network service | arrived in src/modules/system/network |
| The checksum of a segment that arrives | The process | tcp.step: a wrong one adds to Table.bad_checksums, changes no connection and gets no answer |
| The checksum of a datagram that arrives | The process | network.udp_data_read with the rule Verify. A QUIC process gives the rule Do_Not_Verify: QUIC authenticates each packet, so the checksum adds nothing (QUIC: The form of the library) |
| The source port of a payload that leaves: a local port of the endpoint | The network service, in its copy | header_granted |
| The remote of a payload that leaves: the address of the command and the destination port of the header, against the grant or the admitted remotes | The network service, in its copy | header_granted |
| The length field of a datagram that leaves | The network service, in its copy | header_granted |
| The source address of a payload that leaves | Nobody: the network service writes it | network.ipv4_frame |
| The checksum of a payload that leaves | Nobody on this machine: the remote does | — |
Three rules follow from the table.
- A process cannot send as another process. The network service writes the source address. It reads the source port in its own copy of the first 8 bytes of the header, after the copy, and never reads the region of the process a second time: the process cannot change what was checked. The device reads the rest from the region, and the process can change it until the device read it. A process controls the data and the checksum of its own datagrams and segments, and nothing else.
- A wrong checksum harms its writer only. A process that sends a wrong checksum loses its own datagram or segment: the remote drops it. A process that does not check the checksum of what it receives takes damaged bytes as data: the harm stays in that process. The TCP library always checks. A process with a datagram endpoint chooses the rule in its call of
network.udp_data_read. - The checksum comes after the choice of the endpoint. The network service chooses the endpoint by a port that it did not check with the checksum. A frame whose port the wire damaged, and that the check of Ethernet let pass, can reach the endpoint of another port. The process of that endpoint then refuses it by its checksum, after it saw the bytes. The grant, not the checksum, is the guard against a hostile sender: a hostile sender writes a correct checksum.
net shows the two sides in the machine test: the peer sends one datagram with a wrong UDP checksum to the port of net ("net: UDP from 10.0.2.2 port 7 with a wrong checksum: dropped by this process: 1 datagram; ..."), and net writes one datagram with another source port, which the network service refuses ("net: UDP from the source port 1234: refused: ..."). net also writes a datagram whose length field says one byte more than its Send gives ("net: UDP with a wrong length field: refused: ...") and sends 7 bytes, fewer than a UDP header ("net: UDP of 7 bytes: refused: ..."): the network service refuses both. On receive, network.udp_data_read refuses a datagram under 8 bytes and a datagram whose length field is not its count, with each rule.
The segment endpoint
A process reaches the network through an endpoint: a ring of commands and a region of memory that it shares with the network service. A segment endpoint is the form for TCP: the process writes whole TCP segments, and the network service checks them and sends them. The grant of the endpoint says which remotes the process can reach.
A segment endpoint is an endpoint (src/library/modus/contract/datagram) whose grant has the protocol TCP. The ring, the region, the commands and the error records are the ones of a datagram endpoint. Two things differ:
- 1. A Send names a whole segment in the region: the TCP header, its options and its data, where a datagram endpoint names a whole datagram: the UDP header and its data. In both, the process writes the header and its checksum, and a Receive gives the whole segment or datagram.
- 2. The grant names TCP ports, not UDP ports, and a client grant has a block of local ports.
With the right Inspect of its grant, a process also reads the facts of the machine: the counts and the problem rows of the frames that no endpoint owns, and the kernel entries of the network service and of the frame service. Without it, those values are 0: they tell of the traffic of other processes.
The library writes the segments directly in the places of the datagram region. A segment that arrives is copied one time on its path: by the network service, from the frame to the region. A segment that leaves is not copied: the device reads it from the region, after the headers that the network service wrote.
The grant of a segment endpoint
The root service names the grant by the name of the process, as for UDP (endpoint_of). The principal reads it with grants and revokes it with revoke. The grant is one record, datagram.Grant: a remote address, a port, and the flags tcp, listen and inspect. So an endpoint has one of four forms, two for TCP and two for UDP:
| Form | Local ports | Remotes | Use |
|---|---|---|---|
| TCP client grant | A block of 64 TCP ports (datagram.PORT_BLOCK) that the network service chooses | One remote address and one remote TCP port | A process that connects to one server: fetch, and the module file service module_files |
| TCP listen grant | One TCP port that the root service names | Each remote that sent a SYN to that port | A server: server, on the port 80 |
| UDP client grant | One UDP port that the network service gives (Info); the process writes it as the source port of each datagram | One remote address and one remote UDP port | A process that talks to one remote over datagrams |
| UDP listen grant | One UDP port that the root service names | Each remote that sent a first datagram of 1200 bytes at least, and with a named address, that address only | A QUIC server: quic_echo on the port 4433, remote on the port 7100 (section "The listen grant of UDP") |
The grant crosses the control ring and the completion of Info as one word (datagram.grant_pack): the port in bits 0 to 15, the address in bits 16 to 47, and the flags inspect, tcp and listen in bits 48, 49 and 50. datagram.grant_unpack is the one border of that word. It refuses a word with a bit from 51 to 63: it gives no grant and false. The network service then refuses Endpoint_Add and Endpoint_Grant with Bad_Command and the record Bad_Value of the grant argument, whose value is the unknown bits shifted down by datagram.GRANT_KNOWN_BIT_COUNT. It maps no region, opens no ring, and makes or changes no endpoint (network.endpoint_add_grant, network.endpoint_grant_change). A process whose Info gives such a word stops at its start, or, for the test modules net and fetch, fails its first case. The design: The grant word of an endpoint in the repository.
The form and the local ports of an endpoint come from its creation, and stay until the endpoint is removed. A revocation sets the grant to none and keeps them:
- A listen port has one holder. The port of an endpoint whose grant was revoked is still taken: no second endpoint gets it (
network.listen_port_taken, one rule for UDP and TCP). - A new grant on an endpoint must have the form of its creation: the same protocol, listen or client, and for a listen grant the same port. An endpoint that was made for a listen port never gets a client grant, which would send from that port.
- A Send that waits for a hardware address (the answer of ARP) was checked against the grant of its time. A change of the grant ends each such Send of the endpoint with the refusal
Not_Granted: no frame leaves after a revocation.
A client grant of TCP has 64 local ports, because a closed connection stays in TIME-WAIT (below) and one local port serves one connection at a time to one remote. The process chooses a port of its block for each connection. The network service writes the block on the log, as "network: an endpoint for fetch: TCP, the local ports <first> to <last>, the grant 10.0.2.100 port 80".
A listen grant is narrow in another way: the process answers, but it does not reach. The network service keeps one table of admitted remotes for each listen grant: an address and a TCP port that sent a SYN (no ACK, no RST) to the listen port, when a Receive of the endpoint waited for it, from an address that can answer (not 0, a broadcast or multicast address, or the local address). A segment of a remote that is not admitted does not reach the process. A segment to send goes out only to an admitted remote. So a process with a listen grant cannot open a connection to a machine that did not come to it, and cannot send a segment to it. The table has 64 places, each with the time of its last segment in or out. The process releases a remote (command Release) when its connection ended, after its last segment. When the table is full, the oldest place with no segment for more than 120 s serves again (twice the longest gap between two probes of a zero window in src/library/modus/tcp, so that a live remote keeps its place); with none, a SYN from a new remote is dropped and counted in the problems of the endpoint (kind Full). A process that forgets a Release loses only places with no traffic.
This table is the one state of TCP in the network service: addresses and ports, with no sequence number and no window. It is the port table of the network service, with the remotes of a server.
What the network service does for each segment
| Direction | Work | Why |
|---|---|---|
| Arrives | Checks the Ethernet and IPv4 headers, and the length and the data offset of the TCP header (network.parse). It does not check the TCP checksum | The service reads the ports to choose the endpoint; it reads no byte of the data. A segment with a wrong checksum reaches the endpoint of its port, and the TCP library drops it |
| Arrives | Finds the endpoint by the local port, checks the remote against the grant, admits a remote of a listen grant | Property 20, line 3 |
| Arrives | Copies the segment from the frame to the region | The device writes the frame region of the service, not the region of the process |
| Sent | Copies the first 8 bytes of the segment from the region to the frame, then reads its ports from the copy. The device reads the rest from the region | A port is checked in bytes that the process cannot change after the check, and the service reads no byte of the data |
| Sent | Checks the local port against the block or the listen port, and the remote against the grant or the table of admitted remotes | Property 20, lines 3 and 11 |
| Sent | Writes the IPv4 and Ethernet headers | It must check them anyway |
| Sent | Does not check the TCP checksum | A wrong checksum harms only the process that wrote it: the remote drops the segment |
The work for a datagram is the same, with the UDP header in the place of the TCP header (section "The path of a datagram and of a segment").
The library computes the TCP checksum of each segment that it sends, and checks the checksum of each segment that arrives, in tcp.step, before any other read of the segment. The checksum covers the pseudo-header: the remote address that the network service gives with the segment, and the local address of the table. A segment with a wrong checksum adds one to Table.bad_checksums; it changes no connection and gets no answer, because its ports and its sequence numbers are not to be trusted. A SYN with a wrong checksum takes a place of the admitted remotes until the process releases it: server releases the remote of each segment that has no connection.
A process decides nothing from the bytes of a segment that the library dropped, with one exception that is safe for any bytes: the release of a remote that no connection has. tcp.connection_of_segment answers "does a connection have the remote of this segment" from the remote address and the two ports at their fixed place, bytes 0 to 3, and reads nothing else. server releases the remote of a segment only when the answer is no. So a segment from the remote of a live connection, damaged by the wire or with a malformed option, makes the network service forget nothing: the connection goes on. A release for a segment of no connection frees at most a place that a SYN took; it harms no connection, whatever the segment holds.
Property 5 as it is built
Property 5 says that the kernel is off the data path, and that the network stack is a library in the process. Its cost line names what one device with one pair of queues needs: a multiplexer in user space. The network service is that multiplexer. This section says what it does, and what it does not do.
- The kernel carries no data and has no network code. No kernel entry carries a datagram or a segment: an entry is a wait or a wake, and one entry serves a batch.
- The transport is in the process: the whole of TCP (
src/library/modus/tcp), the whole of QUIC (src/library/modus/quic), and the header and the checksum of UDP (src/library/modus/network,udp_header_writeandudp_data_read). The process can read and change each field of its connections. - The network service is on the control path of each datagram and each segment, in both directions: it reads the headers and checks them against the grant.
- The network service is on the data path for one copy of what arrives. On send, it copies 8 bytes of the header for its check, and the device reads the data from the datagram region of the process. It computes no checksum of the data and writes no header of the transport.
- IPv4, ARP and ICMP echo stay in the network service: they are the shared identity of the machine.
- The network service keeps only the ports and, for a listen grant, the remotes that came to it: no sequence number, no window and no key.
One change takes the network service further off the data path. It needs a device with a receive queue for each endpoint and an exact filter by destination port that only the frame service programs; then the copy on receive leaves too. It is not built.
The TCP library
src/library/modus/tcp is the whole protocol of TCP as pure code: it reads segments and the time from memory of the caller, and writes segments and events back. It makes no kernel operation, so the same package runs in a process of the system and in the tool on the host. A process calls it in a loop: wait on the rings, call tcp.step, send what it wrote, read the events.
The form of the interface
The interface follows the rule of the system on data-oriented interfaces: many records in one call, in memory of the caller, never a list of pointers.
- A connection is one record of fixed size (
tcp.Connection, 424 bytes). The caller gives the table of records and one block of memory for the buffers (property 14): each connection has a send buffer and a receive buffer at a place that its index gives. The library allocates nothing. - One call does the work of a batch:
tcp.steptakes all the segments that arrived, the time, a table of places for the segments to send, and a table of events. It reads all the segments, runs the timers, and writes all the segments to send. - An event is a record of fixed size: the index of a connection and a set of kinds (Opened, Accepted, Readable, Writable, Peer_Closed, Closed, Reset, Timed_Out, Refused). One record for each connection that has news, so a table with one place for each connection is enough.
tcp.stepgives the deadline of the next timer. The process waits on its rings with that deadline. No call blocks and no call has a callback.- A call that fails gives an error record (, Module contract: Errors that diagnose): a full table, a local port out of the block, a write on a connection that is not open.
connect,write,read,closeandabortchange the record of one connection. The segments that follow come at the nextstep.
The table also counts the records in TIME-WAIT that a SYN took when no record was free (reclaimed). The process owns each field. It reads the state, the windows, the round trip estimate and the counts of each connection in its own memory, with no call; server draws them.
What the library does
| Subject | Rule | Source |
|---|---|---|
| Open | Active and passive open; a simultaneous open | RFC 9293 3.5 |
| Sequence numbers | Modulo 2^32: a is before b when i32(a - b) < 0 | RFC 9293 3.4 |
| Initial sequence number | M + F(addresses, ports, secret): M counts 4 microseconds, F is the first 4 bytes of BLAKE3 of the secret and the four values | RFC 6528 |
| MSS option | Sent with each SYN (1460); a SYN with no option gives 536 | RFC 9293 3.7.1 |
| Receive window | The free room of the receive buffer, 65 535 at most. The right edge never moves left. It grows only by one MSS or half the buffer (avoidance of the silly window) | RFC 9293 3.8.6 |
| Send window | The segment that carries the newest window updates it (SND.WL1, SND.WL2) | RFC 9293 3.10.7.4 |
| Out of order | A segment after a hole goes to its place in the receive buffer; 8 ranges remember what arrived. An ACK goes at once | RFC 9293 3.10.7.4, RFC 5681 4.2 |
| Delayed ACK | One ACK for each second full segment, or after 40 ms | RFC 9293 3.8.6.3, RFC 5681 4.2 |
| Retransmission timer | SRTT and RTTVAR from the round trips, in microseconds; one segment timed at a time; no sample from a segment sent two times (Karn); the timer doubles after each timeout | RFC 6298 |
| Least RTO | 200 ms, not the 1 s of RFC 6298 2.4 | A choice of this library: the test link has round trips under 1 ms |
| Retries | 8 timeouts in a row end the connection (event Timed_Out, a RST) | RFC 9293 3.8.3 |
| Congestion control | Slow start, congestion avoidance, fast retransmit after 3 duplicate ACKs, fast recovery: Reno, with the partial ACK of NewReno. The congestion window stops at the largest window of the remote | RFC 5681, RFC 6582 |
| Limited Transmit | The first two duplicate ACKs each send one new segment | RFC 3042 |
| Duplicate ACKs | One ACK at once for each segment after a hole, also when a batch has several | RFC 5681 4.2 |
| Initial window | 3 segments of 1460 bytes | RFC 5681 3.1 |
| Zero window | A persist timer sends a probe (one byte before SND.UNA, with no data) until the window opens; it doubles as the retransmission timer does | RFC 9293 3.8.6.1 |
| Close | FIN in both directions: FIN-WAIT-1, FIN-WAIT-2, CLOSING, TIME-WAIT, CLOSE-WAIT, LAST-ACK. A FIN is taken only before the FIN of the remote. FIN-WAIT-2 ends after 20 s with a RST | RFC 9293 3.6 |
| Limits against a remote that does not answer | A passive open ends after 3 timeouts (15 s); 8 probes of a zero window with no answer end a connection; the ACKs that answer segments out of the window, and the challenge ACKs, are 10 in one second for each connection | RFC 5961 7, CVE-2016-5696 |
| TIME-WAIT | 10 s: 2 MSL with an MSL of 5 s | A choice of this library, see below |
| RST | Sent for a segment of no connection, for a SYN when the table is full, and by abort. A RST received ends the connection only if its sequence is exactly RCV.NXT | RFC 9293 3.10.7.1, RFC 5961 3 |
| Challenge ACK | A RST in the window but not at RCV.NXT, a SYN in a synchronized state, and an ACK after SND.NXT get an ACK and change nothing | RFC 5961 3, 4, 5 |
The MSL of RFC 9293 is 2 minutes. The library takes 5 s, as an MSL for one link: the TIME-WAIT of 10 s keeps a connection record for 10 s, not for 4 minutes. A process that serves many short connections needs records for them. The initial sequence numbers of RFC 6528 protect a new connection on the same ports.
What it does not do
- Window scaling (RFC 7323): a window is 65 535 bytes at most. On a link with a round trip of 1 ms, that is 65 MB/s at most for one connection.
- Selective acknowledgments (SACK, RFC 2018), timestamps (RFC 7323), TCP Fast Open (RFC 7413), ECN (RFC 3168), urgent data (the field is read and ignored).
- The Nagle rule (RFC 9293 3.7.4): the library sends a small segment at once.
- Keepalive (RFC 9293 3.8.4).
- SYN cookies: a SYN takes a connection record. A table that is full ends its oldest record in TIME-WAIT and drops the SYN (the remote sends it again), else refuses the SYN with a RST.
Table.remote_limitlimits the open connections of one remote address. - Path MTU discovery: the MSS is 1460 at most, for one frame of Ethernet.
- IPv6.
- A RST from the network service for a segment to a port with no endpoint: the service drops it and counts it.
Randomness
RFC 6528 needs a secret that an attacker cannot guess: with it, a remote cannot predict the first sequence number of a new connection and inject a segment into it. server and fetch take the secret from their random generator (random.generator_from_context): each process has one, seeded by its starter (Randomness). A process with no seed in its context stops: server writes "server: no random seed in the context" and stops with the stop outcome Refused.
The tests
TCP is tested at three levels: the library alone on the host, the system against a TCP stack that this project did not write, and the system against a peer that damages the traffic on purpose. modustool test tcp runs the two machines (Developer tools: The parts of the machine test).
The library on the host
odin test src/library/modus/tcp (in modustool check): two connection tables on one link that the test controls, with loss, reorder and duplicates; the sequence numbers at the wrap of 2^32; the round trip estimate and the timer of RFC 6298, with its doubling; a zero window that opens; a RST in the window and out of it; a SYN in a synchronized state. The checksum has its own cases: a segment with one changed bit, in its data or in its header, changes no field of the connection and adds one to the count; a segment whose checksum is right for another pair of addresses is dropped; a segment from the remote of a live connection with a malformed option, with and without a right checksum, still names its connection for the process (connection_of_segment), and data on the connection is taken after it.
odin test src/library/modus/network checks the other half: parse gives a datagram or a segment with a wrong checksum to its port, and udp_data_read refuses the datagram with the rule Verify and takes it with the rule Do_Not_Verify. A datagram with an odd count of bytes has its own case: the test sums the words by hand, and one changed bit of the last byte is refused. The rule of a taken listen port has its case too: the port of an endpoint with no grant stays taken until the endpoint is removed.
odin test src/library/modus/contract/datagram gives grant_unpack each word that it must refuse first: each bit from 51 to 63 alone and beside a whole grant, and the largest word. Then the edges: 0, each flag alone, the largest address and port. The grants of the processes come last. odin test src/library/modus/network gives the decisions of the network service (endpoint_add_grant, endpoint_grant_change) the words with bit 51 and with bit 63, alone and beside a whole grant, and a grant of another form: each gives its status and its record, with a positive value, and leaves the grant of the endpoint unchanged.
odin test src/library/modus/contract/datagram gives command_of_ring_word each word that names no command: None, the word one past the last command and the largest word. On the machine of modustool test network, net sends the same three words on its datagram ring. The network service answers each one with Bad_Command and the record Unsupported of the word, and serves the Info after it: "net: the command word 10 of the datagram ring: refused with Bad_Command and the record Unsupported of the word, and the next Info is served: as it must".
modustool test network then starts a machine with no network device. The first start of net gives the line of the root service for the absent device and the line of the refused network; a second start of net gives no second line of the device, and the console still answers 1 + 2 * 3. QEMU has no network device that the root service starts and that the frame service then refuses, so the wait of the network service for a frame service that stops is tested on the host only: odin test src/library/modus/system gives the decision of the wait (answer_wait_of) a process that stopped, a handle that names no process, a process past its deadline and the largest times.
odin test src/library/modus/contract/byte_order checks the reads and the writes of a number in each order: 0, 1, the largest value, the place of each byte, a write that leaves its neighbors unchanged. The checksum has its edges: no byte, one byte, the fold of a carry that makes a new carry, the largest sum, and the example of RFC 1071, whose sum before its fold (0x2DDF0) shows that the sum keeps its carries.
One place for the byte order and the checksum
The network packages, the network service and the tool read and write each number of a header, and compute each checksum of the Internet, with one package: src/library/modus/contract/byte_order. The TPM client, the boot program, the history file of the line editor, the files of the principals, the disk grant table and the manual pages use it too. It holds the reads and writes of 16, 32 and 64 bits in network order and in the other order, the sum of RFC 1071 with its fold, and the sum of the pseudo-header of UDP and TCP over IPv4. The design: The byte order and the checksum of the Internet in the repository.
Against an independent implementation
The peer uses src/library/modus/tcp too: it tests the library against itself. So the test also runs the system's TCP against a stack that this project did not write:
- A second network machine (
scenario_tcp_real) has QEMU user networking (-netdev user). Its TCP is the one of libslirp, which comes from the TCP of 4.4BSD. hostfwdgives a port of the host loopback to the port 80 of the machine: the tool connects withcore:net(the TCP of the host kernel to libslirp, then libslirp to the machine) and fetches the pages ofserver. The tool checks each answer itself. The access records ofserverare read through its record outputrequests: the console reads them with a condition and a known joined record, then the test modulerequests_pacestarts a secondserverwhoserequestshas a slow reader of each pace and a fast reader, and the tool sends the 80 requests that their exact counts name, in three parts, each after the line of the slow reader that drained its ring (Record outputs and record inputs of a module: typed streams of records between processes: The steps in the repository, 5 and 6).guestfwdgives the address 10.0.2.100 port 80 of the network of the machine to a port that the tool listens on withcore:net:fetchconnects to it. The first machine of the partdisk_processeshas the same network: its module file service fetches a module file that the listener serves (Developer tools: The disk of the disk processes).
With the controlled peer
QEMU gives the peer each frame of the network device as one UDP datagram on the loopback of the host (-netdev dgram). The peer reads each frame with its own code, not with src/library/modus/network. It answers ARP, an echo request and a datagram to port 7, and it carries each call of the tool to the remote service on a call stream, and the call answer back (tools/modustool/machine/peer.odin, peer_remote.odin).
A third network machine (scenario_tcp) has the peer of the tool on -netdev dgram, as scenario_network. The tool tells the peer to run one case at a time. The peer is a TCP client of server, and it changes the frames between its library and the machine with a generator of numbers that the case seeds, so each run makes the same changes: 5 % loss, a reorder, duplicates, a zero window of its receive buffer.
The case "damaged" sends two segments by hand on a live connection, after a first answer: an ACK with an option of length 0 and one bit of its checksum changed, then the same ACK with the checksum of its bytes. The library of server drops both; the log has "server: 1 segments dropped for a wrong checksum". The client then sends two more requests on the same connection, and the case passes when both are answered: "closed, the remote first: 3 requests". The second request is the one that shows the rule: the server can answer the first one before its network service acts on a release.
What the tests found
- With batches of segments, a receiver must still send one duplicate ACK for each segment after a hole; with one ACK for a batch, no fast retransmit starts. The record counts the ACKs that it owes.
recoverof NewReno starts at the initial sequence number, not at 0: with 0 and an initial number above 2^31, no fast retransmit can start.- Avoidance of the silly window can stop a sender with a small window and nothing in flight: the persist timer ends that case too.
- The congestion window stops at the largest window that the remote gave: without that limit, it grows with no end when the window of the remote limits the sender.
- 1 MiB with 5 % loss in each direction, on the host link of the tests: 5 to 10 s with Reno and NewReno, 1.5 to 2.7 s with Limited Transmit. The timeouts that remain come mostly from a retransmission that the link dropped too: SACK would remove more of them.
- libslirp acknowledges the FIN of a connection of
guestfwdand does not send its own: the connection offetchstays in FIN-WAIT-2. - The measures (the machine test, QEMU with KVM, 4 processors, while other machine tests ran on the host; one run unless said):
- Between the peer and
server: 1 MiB from the server in 52.9 ms (19.8 MB/s), 1 MiB to the server at 20.2 MB/s. - With 5 % loss in each direction: 0.8 and 0.7 MB/s (48 segments sent again; 2.2 and 0.8 MB/s in another run). With 10 % reordered: 16.6 and 15.3 MB/s. With 10 % repeated: 16.1 and 14.9 MB/s. A zero window of 1.5 s: 4 probes.
- The handshake seen by
server(SYN to the ACK of its SYN, ACK): 61 to 135 us at least.GET /answered and acknowledged in 157 to 224 us. - With libslirp: the host fetched 1 MiB at 58.6 to 64.9 MB/s and sent 1 MiB at 57.6 to 61.0 MB/s (the clock of the host).
servermeasured 92.9 to 98.4 MB/s out and 81.9 to 90.4 MB/s in. fetchmeasured the handshake in 261 to 266 us, 1 MiB in at 105.3 to 140.7 MB/s, 1 MiB out at 11.4 to 11.9 MB/s, andGET /helloin 42.6 to 43.5 ms. The cause of that time is not known: the tool answers at once, so the time is in QEMU, libslirp or the wait offetch.- With 16 connections, the answers were acknowledged 0.2 to 30 ms after their request, in the order of the connections: the peer runs one step for each frame.
- The window of a connection is 32 KiB (the receive buffer), so one connection cannot exceed 32 KiB for each round trip. The test asserts no speed.
- Between the peer and
The HTTP server
server shows the TCP library at work: it serves web pages over HTTP to each client that comes to its port 80. It reads hostile input with fixed bounds, and its pages come from its own private store, which the principal fills from the console.
server is a simple HTTP/1.1 server (RFC 9110, RFC 9112), in the process as its TCP is: one thread, one table of 48 connections (CAPACITY, with the ones in TIME-WAIT), one session of fixed size for each (its input of 8192 bytes, web.REQUEST_HEAD_LIMIT, and its answer), and no allocation. Each connection has a send buffer of 64 KiB and a receive buffer of 32 KiB.
- The reading of a head is
request_readofsrc/library/modus/web, for hostile input: a bound for each part, and a problem with its status (400, 405, 408, 413, 414, 431, 501, 505) for each rule that a head breaks. - Connections stay open after an answer; "Connection: close" or HTTP/1.0 ends them. The bytes after a head stay in the input: they are the body, or the next request (pipelining). The answers go in the order of the requests.
- What it serves comes from its grants (property 20): its own counts (
/,/counts.json), and the pages of its private store (property 9). The principal changes the pages from the console through its exports (property 19):server.put,server.remove,server.pages,server.served. PUT and DELETE over HTTP stay off: without TLS the network is not authenticated. - Limits against a client that makes no progress (without them, one host could keep each record): 10 s with no request, 408 for a head after 10 s (also a head that starts after an answer), no byte of a body in 10 s, a body and the bytes to send under 4096 bytes in one second over a period of 10 s (also after this server closed), 24 connections for each remote address.
- The pages and their origin: an answer of a page keeps the generation of its place, and a page that changes during it ends the connection with a RST.
putandremovechange the store first. The pages,/and/counts.jsonshare one origin, and a page of the store runs script on it by design: the origin holds no secret, no cookie and no authority. Each answer saysX-Content-Type-Options: nosniff. - The Date field: an answer has a field
Datewhen the date page of the process (src/library/modus/contract/date) knows the date. With no date known, the answer has noDate: RFC 9110 6.6.1 asks a server with no clock to send none. - The pages and the requests that it keeps: at most 16 pages (
PAGE_CAPACITY), and the last 18 access records in its window (ACCESS_HISTORY), each with its time in microseconds. - The requests as a record output:
serverwrites the access record of each request into its record outputrequests(Record outputs and record inputs of a module: typed streams of records between processes in the repository), not on the log. Its fields arestatus,answer_size,answer_time,method,targetandpeer; the time runs from the first byte of the head to the last byte of the answer in the send buffer. An example row ofserver.requests |> first(1):200 12 bytes 157 us GET /hello.html 10.0.2.2 port 49200. A reader wires it and receives each request from its joined record on, in order:server.requests |> where(status = 404) |> first(5)in the console, afterconnect("server"). With no reader, a write costs one load and the record is counted as discarded.serverwrites with no wait: a full ring of a reader that the console or a starter chose refuses the record withFull, andstream_counts(server.requests)says how many. Only a ring of the paceAlways_Holds_Writer, as an audit asks, holds the server until it has room. - No TLS over TCP: the TLS 1.3 of the system is the one of QUIC (QUIC).
The listen grant of UDP
A QUIC server must answer clients that it does not know in advance, over UDP. The listen grant of UDP lets it answer each client that came to its port, and never reach a machine that did not come. It is the listen grant of TCP, with one rule changed for datagrams.
Written for QUIC (QUIC: QUIC on the network (the fourth layer, built, with Retry and the budgets of work)); modustool test quic and modustool test quic-real run it. A datagram endpoint can have a listen grant: one local UDP port that the root service names, and the remotes that came to it. Its rules are the ones of the listen grant of TCP above, with one change: a new remote is admitted on a datagram of 1200 bytes at least (datagram.LISTEN_FIRST_LEAST), where TCP admits it on a SYN. 1200 bytes is the least first datagram of a QUIC client (RFC 9000, section 14.1), so a stray small datagram of an unknown remote takes no place.
- The table of admitted remotes is the one of TCP (
network.Admitted): 64 places, the oldest place with no traffic for 120 s serves again,Releaseforgets a remote. - A UDP listen grant can name one remote address (
datagram.Grant.address): then a datagram of another address is never admitted, whatever its size, and the endpoint counts it (Not_Granted). The port of the remote is not named: a QUIC client takes a new port for each connection. The grant ofremotenames the address of the host, 10.0.2.2: that address is the rule of who may call (QUIC: remote over QUIC (built)). A TCP listen grant names no address. - With QEMU user networking, each datagram that a
hostfwdof UDP gives to the machine comes from 10.0.2.2. A forward of a UDP port of the host to the port 7100 would then let each sender that reaches that port callremoteas the principal. The tool refuses a machine with such a forward (network_device_problemintools/modustool/machine/machine_command.odin). - A UDP listen port is not a port PORT_FIRST + n of a client grant, and no other UDP listen grant has it.
- A Send goes only to an admitted remote. To a remote that the process did not validate, the network service sends at most 3 times the bytes that came from it (status
Amplification, error kindOver_Limit); the commandValidatelifts that limit for a remote that proved its address to the process (QUIC: QUIC on the network (the fourth layer, built, with Retry and the budgets of work), says what a process that validates every remote gains). A send does not keep a place alive: only a datagram of the remote does. ResolveandEchoare refused, as for each listen grant: the process answers, but it does not reach.- The process releases a remote whose datagram it refused (a forged Initial, a datagram of no connection), so that a flood of many addresses does not hold the table.
The kernel has no code for it. The network service does for a datagram of a listen grant what it does for each datagram: it checks the headers, finds the endpoint by the local port, checks the remote, and copies the datagram one time. The 1200 bytes of a first datagram and the 3 times of the limit count the data, with no UDP header.
QUIC: what it reuses, and what it needs
The design of QUIC is in QUIC. QUIC runs over UDP (RFC 9000), so it is a library in the process over a datagram endpoint (src/library/modus/quic). This section says what QUIC took from the work on TCP, and what it needed in addition.
It reuses from TCP:
- The endpoint and its grant: a client grant of one remote, and the listen grant with its admitted remotes, for UDP in place of TCP. The table of admitted remotes is the one of the datagram endpoint, unchanged (the section above).
- The form of the interface: a table of connection records and buffers of the caller, one
stepfor a batch of datagrams, a table of events, the deadline of the next timer. - The ideas of loss and congestion: RFC 9002 takes the round trip estimate of RFC 6298, and its congestion control is NewReno. The code is not shared:
src/library/modus/quic/recovery.odinhas its own estimate and congestion window, andsrc/library/modus/quicimports nothing ofsrc/library/modus/tcp. - The tests: the peer with loss, reorder and duplicates (
tools/modustool/machine/peer_quic.odin). The test against a stack that this project did not write uses the QUIC client of OpenSSL through the UDP relay of the peer, not libslirp: withrestrict=on, libslirp drops each UDP datagram that the machine sends.
It needs more than TCP gives:
- TLS 1.3 (RFC 8446, RFC 9001): the key exchange (X25519), AEAD (AES-GCM or ChaCha20-Poly1305), HKDF. Cryptography has the primitives.
- Randomness: keys need a secret that an attacker cannot guess. Each process takes it from its random generator, seeded by its starter (Randomness).
- A check of the peer. A certificate needs a time of the day that an attacker cannot set: the date page has the date of the real-time clock, read one time, and the clock of the machine is not such a time. So QUIC uses raw public keys in place of certificates and a date (QUIC: TLS 1.3 for QUIC (the second layer, built)).
