Licklider Transmission Protocol (RFC 5326)
README.md

ltp #

Pure OCaml implementation of Licklider Transmission Protocol (RFC 5326) for reliable data delivery over high-delay and disruption-prone links.

Overview #

TCP assumes it can finish a handshake and see acknowledgements within seconds. On a link to Mars the round trip takes between about 6 and 44 minutes, and the link is only up during scheduled passes, so TCP never gets going. LTP sends a block as a session of segments without a handshake. The red part is delivered reliably, with checkpoints, reception reports and selective retransmission, and the green part is sent once, best effort. The sender marks some red segments as checkpoints, the last one at the end of the red part. The receiver answers each checkpoint with a report segment listing the byte ranges of the red part it holds, and the sender retransmits the missing ranges, with a new checkpoint, until a report covers the whole red part (RFC 5326, sections 6.2 and 6.3). Each retransmission round costs one round trip, and it resends only the bytes that were lost.

LTP is the convergence layer the Bundle Protocol commonly uses on deep-space links. The Bundle Protocol (RFC 9171) carries application data as bundles that nodes store and forward hop by hop, across links that are not all up at the same time, and a convergence layer is the protocol that carries bundles across one such hop.

ltp is I/O-free. Segments and their wire codec are plain values, and Ltp.Sender and Ltp.Receiver are the session state machines of RFC 5326: each maps a received segment, a timer expiry or a local request to a new state and a list of actions (segments to send, timers to arm, bytes to deliver). Time and the initial serial numbers are inputs, so the machines behave the same under Eio, in a unikernel and in the deterministic test suite.

Packages #

  • ltp holds the segments and their SDNV codec, and the sender and receiver session state machines.
  • ltp-eio carries LTP segments over UDP with Eio, one segment per datagram (RFC 7122, Section 3.3).

Installation #

Install with opam:

$ opam install ltp ltp-eio

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

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

Usage #

Deliver a block between two engines #

Ltp.Sender and Ltp.Receiver never touch a socket or a clock: each call to incoming names the time of its event and returns the segments to transmit, the timers to arm and the notices for the client service. Connecting the two machines directly is a lossless link, so no countdown timer fires and the exchange runs from the transmission request to the red-part reception notice, the report and its acknowledgment:

let link = Ltp.Session.limits ~light_time:(Ltp.Session.Duration.of_sec 1.) ()
let now = Ltp.Session.Time.zero

type hop = To_receiver of Ltp.segment | To_sender of Ltp.segment

let sender_out notices actions =
  List.fold_left
    (fun (notices, out) -> function
      | Ltp.Sender.Transmit seg -> (notices, out @ [ To_receiver seg ])
      | Ltp.Sender.Notify e -> (("sender", e) :: notices, out)
      | _ -> (notices, out))
    (notices, []) actions

let receiver_out notices actions =
  List.fold_left
    (fun (notices, out) -> function
      | Ltp.Receiver.Transmit seg -> (notices, out @ [ To_sender seg ])
      | Ltp.Receiver.Notify e -> (("receiver", e) :: notices, out)
      | _ -> (notices, out))
    (notices, []) actions

let rec exchange s r notices = function
  | [] -> List.rev notices
  | To_receiver seg :: rest ->
      let r, actions =
        Ltp.Receiver.incoming r ~now (Ltp.Receiver.Segment_received seg)
      in
      let notices, out = receiver_out notices actions in
      exchange s r notices (rest @ out)
  | To_sender seg :: rest ->
      let s, actions = Ltp.Sender.incoming s ~now (Ltp.Sender.Segment_received seg) in
      let notices, out = sender_out notices actions in
      exchange s r notices (rest @ out)

let deliver block =
  let request =
    Ltp.Session.transmission ~checkpoint_serial:42L
      ~session:{ Ltp.originator = 1L; number = 1L }
      ~client_service_id:1L ~segment_size:4 ~red_part:block ()
  in
  let s = Ltp.Sender.v ~limits:link 1L in
  let r =
    Ltp.Receiver.v ~limits:link ~first_report_serial:7L ~services:[ 1L ] 2L
  in
  let s, actions = Ltp.Sender.incoming s ~now (Ltp.Sender.Transmit_request request) in
  let notices, out = sender_out [] actions in
  exchange s r notices out

The serial numbers are inputs because RFC 5326 requires them to be random: draw them from a generator in a real engine.

# List.iter (fun (who, e) -> Fmt.pr "%s: %a@." who Ltp.Event.pp e)
    (deliver "hello, mars");;
sender: Session_started 1:1
receiver: Session_started 1:1
receiver: Red_part_received(1:1, len=11, eob=true)
sender: Transmission_completed 1:1
receiver: Reception_completed 1:1
- : unit = ()

On a real link, a driver arms each Start_timer action and feeds the expiry back as Timer_expired. The sender retransmits a lost segment when that expiry arrives.

Carry segments over UDP #

ltp-eio sends one segment per datagram (RFC 7122, Section 3.3), reads every segment of a datagram (RFC 5326, Section 5) and does no session logic. A receiving engine hands each segment it reads to the receiver machine and sends the segments the machine answers with:

let receive ~net ~port ~peer =
  Eio.Switch.run @@ fun sw ->
  let socket =
    Eio.Net.datagram_socket ~sw net (`Udp (Eio.Net.Ipaddr.V4.any, port))
  in
  let conn = Ltp_eio.of_socket ~peer socket in
  let rec loop r block =
    if Ltp.Receiver.phase r = Ltp.Receiver.Closed then block
    else
      match Ltp_eio.recv conn with
      | Error msg ->
          Fmt.epr "ltp: %s@." msg;
          loop r block
      | Ok seg ->
          let r, actions =
            Ltp.Receiver.incoming r ~now (Ltp.Receiver.Segment_received seg)
          in
          let block =
            List.fold_left
              (fun block -> function
                | Ltp.Receiver.Transmit seg ->
                    Ltp_eio.send conn seg;
                    block
                | Ltp.Receiver.Notify (Ltp.Event.Red_part_received p) ->
                    Some p.Ltp.Event.data
                | _ -> block)
              block actions
          in
          loop r block
  in
  loop (Ltp.Receiver.v ~limits:link ~first_report_serial:7L ~services:[ 1L ] 2L)
    None

let run () =
  Eio_main.run @@ fun env ->
  let peer = `Udp (Eio.Net.Ipaddr.V4.loopback, 1113) in
  match receive ~net:(Eio.Stdenv.net env) ~port:1114 ~peer with
  | Some block -> Fmt.pr "received %d bytes@." (String.length block)
  | None -> Fmt.pr "the session closed without its red-part@."

This loop arms no timer, so it relies on the sender to retransmit; a complete engine also drives the receiver's report timers.

Features #

The codec covers red and green data segments, report segments and their acknowledgements, and cancel segments and theirs. Integers on the wire are SDNVs (Self-Delimiting Numeric Values). A session is named by its originating engine and a session number. Checkpoints, reception reports and retransmission timers are handled by the sender and receiver state machines.

Segment-level API #

For protocol-level work (checkpoints, reports, cancel flow), construct and inspect segments directly.

Building and encoding #

open Ltp

let session = { originator = 1L; number = 42L }

let segment =
  data_segment
    ~session_id:session
    ~client_service_id:1L
    ~block_offset:0L
    "hello"

let wire = encode_segment segment

SDNV #

An SDNV (RFC 6256) writes an unsigned integer 7 bits per byte, most significant group first, with the top bit set on every byte but the last: 127 is the single byte 0x7F, and 128 is 0x81 0x00.

let encoded = Ltp.encode_sdnv 127L

let () =
  match Ltp.decode_sdnv encoded 0 with
  | Ok (value, _offset) -> Fmt.pr "Value: %Ld@." value
  | Error msg -> Fmt.pr "Error: %s@." msg

Predicates #

  • is_data_segment, is_red_segment, is_green_segment
  • is_checkpoint, is_eorp, is_eob

Report and cancel #

  • Ltp.report_segment ~session_id ~report_serial ~checkpoint_serial ~upper_bound claims
  • Ltp.report_ack ~session_id ~report_serial
  • Ltp.cancel ~session_id ~from_sender reason
  • Ltp.cancel_ack ~session_id ~to_sender
  • ION is NASA JPL's DTN implementation in C, with LTP.
  • HDTN is NASA Glenn's high-rate DTN implementation in C++, with LTP.

References #

Licence #

ISC License. See LICENSE.md for details.