Skip to content

03 Lucid tofino compiler

John Sonchack edited this page Jun 17, 2024 · 2 revisions

Compiling to the Tofino

./dptc prog.dpt -o prog_build compiles the lucid program prog.dpt into a P4-tofino project in the prog_build directory. The three most important files in the build directory are:

  • lucid.p4 -- the P4 program.
  • lucid.py -- a python control script used to install compiler-generated multicast rules for event duplication (e.g., generate_ports). This script must be run from inside of the tofino's bfshell agent, e.g., with $SDE/run_bfshell.sh -b lucid.py.
  • globals.json -- a json file mapping each global defined in the Lucid program to an object in the resulting P4 program, along with additional type information. This file makes it easier to write custom control programs for Lucid data planes.

Additional options

The python control script generated by the compiler needs to know what ports you are using to correctly implement the "flood" builtin. Tell the compiler about a port with the --port <dpid>@<speed> argument. dpid is the port id and speed is the port's speed in Gb/s. Inputs are not checked and are assumed to be tofino compatible. The port argument can be used multiple times to add multiple ports. The compiler also generates code in the python control script to bring up all added ports. The default ports are 128@10, 129@10, 130@10, 131@10.

The compiler assumes that port 196 is the recirculation port. To change this, use --recirc_port <dpid>.

Tofino restrictions

The tofino backend has a few additional restrictions on Lucid programs.

  1. Each control flow of a handler may only call generate_port once and generate_ports once. This is necessary for the Lucid compiler to guarantee that generate_port and generate_ports can always be implemented without requiring recirculations. In P4, a generate_port(p, ...) compiles into setting the unicast output port of the current packet to p, while generate_ports({p1, p2, ..., pn}, ...); compiles into setting the multicast group of the current packet to a Lucid-created group containing ports p1, p2, ..., pn. Note that the base generate function, which always sends the event to the local recirculation port, may be used an unlimited number of times in a control flow.

  2. A handler is only allowed to generate one instance of each event in each control flow.

  3. Every event parameter must either start or end on a byte boundary.

Example programs

The programs in examples/tofino_apps/src are regularly compiled and tested on the Tofino's ASIC reference model.

Clone this wiki locally