Crate p4rs

source ·
Expand description

This is the runtime support create for x4c generated programs.

The main abstraction in this crate is the Pipeline trait. Rust code that is generated by x4c implements this trait. A main_pipeline struct is exported by the generated code that implements Pipeline. Users can wrap the main_pipeline object in harness code to provide higher level interfaces for table manipulation and packet i/o.

use p4rs::{ packet_in, packet_out, Pipeline };
use std::net::Ipv6Addr;

struct Handler {
    pipe: Box<dyn Pipeline>
}

impl Handler {
    /// Create a new pipeline handler.
    fn new(pipe: Box<dyn Pipeline>) -> Self {
        Self{ pipe }
    }

    /// Handle a packet from the specified port. If the pipeline produces
    /// an output result, send the processed packet to the output port
    /// returned by the pipeline.
    fn handle_packet(&mut self, port: u16, pkt: &[u8]) {

        let mut input = packet_in::new(pkt);

        let output =  self.pipe.process_packet(port, &mut input);
        for (out_pkt, out_port) in &output {
            let mut out = out_pkt.header_data.clone();
            out.extend_from_slice(out_pkt.payload_data);
            self.send_packet(*out_port, &out);
        }

    }

    /// Add a routing table entry. Packets for the provided destination will
    /// be sent out the specified port.
    fn add_router_entry(&mut self, dest: Ipv6Addr, port: u16) {
        self.pipe.add_table_entry(
            "ingress.router.ipv6_routes", // qualified name of the table
            "forward_out_port",           // action to invoke on a hit
            &dest.octets(),
            &port.to_le_bytes(),
            0,
        );
    }

    /// Send a packet out the specified port.
    fn send_packet(&self, port: u16, pkt: &[u8]) {
        // send the packet ...
    }
}

Re-exports§

Modules§

Structs§

  • Every packet that goes through a P4 pipeline is represented as a packet_in instance. packet_in objects wrap an underlying mutable data reference that is ultimately rooted in a memory mapped region containing a ring of packets.

Traits§

Functions§