Skip to main content

vlib_node

Attribute Macro vlib_node 

Source
#[vlib_node]
Expand description

Registers a VPP node and associated function

This registers an internal VPP node, which is the most common type of node (as opposed to processing or input nodes).

In addition, node functions are also registered, compiled for multiple CPU architectures and target features to allow VPP to select the optimal implementation for the current CPU. This only has an effect for code inlined into the node functions. In other words, Node::function should be marked as #[inline(always)] and any other functions directly or indirectly called in performance-critical paths should be similarly marked.

§Attributes

The macro takes key-value attributes as follows:

  • name: (required, string literal) The name of the VPP node.
  • instance: (required, ident) The instance of the node.
  • runtime_data_default: (optional, ident) An identifier for a constant value of type Node::RuntimeData to use as the default runtime data for this node.
  • format_trace: (optional, ident) An identifier for a function with the signature fn(&mut MainRef, &mut NodeRef<...>, &TraceData) -> String to format trace data for this node.

§Examples

#[derive(Copy, Clone)]
struct ExampleTrace;

impl fmt::Display for ExampleTrace {
    fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Ok(())
    }
}

fn format_example_trace(
    _vm: &mut vlib::MainRef,
    _node: &mut vlib::NodeRef<ExampleNode>,
    t: &ExampleTrace,
) -> String {
    t.to_string()
}

static EXAMPLE_NODE: ExampleNode = ExampleNode::new();

#[vlib_node(
    name = "example",
    instance = EXAMPLE_NODE,
    format_trace = format_example_trace,
)]
struct ExampleNode;

impl ExampleNode {
    const fn new() -> Self {
        Self
    }
}

impl vlib::node::Node for ExampleNode {
    type Vector = ();
    type Scalar = ();
    type Aux = ();

    type NextNodes = ExampleNextNode;
    type RuntimeData = ();
    type TraceData = ExampleTrace;
    type Errors = ExampleErrorCounter;
    type FeatureData = ();

    #[inline(always)]
    unsafe fn function(
        &self,
        _vm: &mut vlib::MainRef,
        _node: &mut vlib::NodeRuntimeRef<ExampleNode>,
        _frame: &mut vlib::FrameRef<Self>,
    ) -> u16 {
        todo!()
    }
}