bmv2
Designing your own switch target with bmv2
Classes | Macros
actions.h File Reference

Go to the source code of this file.

Classes

class  bm::ActionPrimitive< Args >
 

Macros

#define REGISTER_PRIMITIVE(primitive_name)
 
#define REGISTER_PRIMITIVE_W_NAME(primitive_name, primitive)
 

Detailed Description

Each action primitive supported by the target needs to be declared as a functor. This is how you declare one:

class SetField : public ActionPrimitive<Field &, const Data &> {
void operator ()(Field &f, const Data &d) {
f.set(d);
}
};
class Add : public ActionPrimitive<Field &, const Data &, const Data &> {
void operator ()(Field &f, const Data &d1, const Data &d2) {
f.add(d1, d2);
}
};

You can then register them like this:

And use them by their name in the P4 program / JSON file:

e.g. SetField(hdrA.f1, 99);

Valid functor parameters for an action primitive include:

You can declare and register primitives anywhere in your switch target C++ code.

Macro Definition Documentation

◆ REGISTER_PRIMITIVE

#define REGISTER_PRIMITIVE (   primitive_name)
Value:
bool primitive_name##_create_ = \
bm::ActionOpcodesMap::get_instance()->register_primitive( \
#primitive_name, \
[](){ return std::unique_ptr<::bm::ActionPrimitive_>( \
new primitive_name()); })

When implementing an action primitive for a target, this macro needs to be called to make this module aware of the primitive existence.

◆ REGISTER_PRIMITIVE_W_NAME

#define REGISTER_PRIMITIVE_W_NAME (   primitive_name,
  primitive 
)
Value:
bool primitive##_create_ = \
bm::ActionOpcodesMap::get_instance()->register_primitive( \
primitive_name, \
[](){ return std::unique_ptr<::bm::ActionPrimitive_>( \
new primitive()); })

This macro can also be called when registering a primitive for a target, in case you want to register the primitive under a different name than its functor name

REGISTER_PRIMITIVE
#define REGISTER_PRIMITIVE(primitive_name)
Definition: actions.h:128