UDP datagram header codec
README.md

nox-udp #

Warning. This is a work-in-progress experimental fork of mirage-tcpip. Do not use it in production; use mirage-tcpip instead.

UDP (RFC 768) is the transport under DNS, DHCP, NTP and most tunnels, and its header is eight bytes: two ports, a length and a checksum computed over an IP pseudo-header. nox-udp encodes and decodes that header in OCaml for IPv4 and IPv6, and Udp.Ports tracks which local ports are bound and allocates ephemeral ones from the dynamic range 49152 to 65535 (RFC 6335). Neither does any I/O.

nox-udp-eio (module Udp_eio) is the datagram layer over Eio. One layer carries UDP over the IPv4 and IPv6 layers of nox-ip-eio, sends each datagram by its destination's address family, and queues received datagrams per bound port. Udp_eio.over_netif builds the whole stack (Ethernet, ARP, IP and UDP) over a raw Ethernet device, and Udp_eio.Socket and Udp_eio.Forward wrap host datagram sockets.

Installation #

Install with opam:

$ opam install nox-udp

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install nox-udp

Credits #

The header codec is forked from mirage/mirage-tcpip, by Anil Madhavapeddy, Mindy Preston and the MirageOS team, under the ISC licence. The cstruct codec is replaced by the wire library, the checksum by nox-crc, and the pseudo-header by nox-ip. The original copyright notices are preserved in LICENSE.md.

Bounded receive state #

Udp.Receiver owns local ports, checksum admission, FIFO buffering and capacity refusal in the pure core. Udp_eio installs that state under a mutex and waits on the destination named by the core. Configure a pure receiver with port_capacity, unbound_capacity, max_datagrams and max_bytes, then pass it as Udp_eio.v ~receiver. queued reports retained datagrams and payload bytes; dropped counts capacity refusal. The aggregate limits bound a flood across many ports, including zero-length datagrams.

let receiver = Udp.Receiver.v ~port_capacity:32 ~unbound_capacity:4
  ~max_datagrams:256 ~max_bytes:1048576 ()

On exhaustion the arriving datagram is discarded; earlier datagrams keep FIFO order. A full port never blocks ingress processing on another port. Admission still requires available aggregate capacity. A finite admitted backlog drains when its receivers continue running; the transport provides no network delivery guarantee. Unbinding releases that port's retained payloads, and pending Eio receives recheck the pure ownership decision after a binding change. Retaining an old pure snapshot also retains its payloads in memory.

Checksums follow RFC 1122 section 4.1.3.4 and RFC 8200 section 8.1: invalid checksums are discarded; normal IPv6 UDP input requires a checksum. Multicast and IPv4 limited-broadcast sources are refused before buffering, per RFC 1122 section 4.1.3.6 and RFC 4291 section 2.7. Buffer bounds do not provide congestion control. Applications using UDP over the Internet must provide the controls described by RFC 8085.

Checksum validation and admitted payload materialization cost O(B) for B input bytes, with O(log P) port lookup for P caller-bound ports. Queues use amortized constant work along one progressing history. The total payload bytes and retained datagrams are bounded independently; the caller controls port registration. The reproduction below measures looped send/receive allocation at 100 and 1000 byte payloads:

$ dune exec bench/receiver.exe