bmv2
Designing your own switch target with bmv2
|
Go to the source code of this file.
Classes | |
class | bm::SwitchWContexts |
class | bm::Switch |
This file contains 2 classes: bm::SwitchWContexts and bm::Switch. When implementing your target, you need to subclass one of them. By subclassing bm::SwitchWContexts, you will be able to write a target containing an arbitary number of bm::Context objects. For a detailed description of what a bm::Context is, please read the context.h documentation. However, many targets don't require the notion of bm::Context, which is why we also provide the bm::Switch class. The bm::Switch class inherits from bm::SwitchWContexts. Because it assumes that your switch will only use a single bm::Context, the very notion of context can be removed from the bm::Switch class and its dataplane APIs. However, because we offer unified runtime APIs, you will have to use a context id of 0
when programming the tables, even when your switch class inherits from bm::Switch and not bm::SwitchWContexts. The simple switch target only supports one bm::Context and inherits from bm::Switch.
When subclassing on of these two classes, you need to remember to implement the two pure virtual functions: bm::SwitchWContexts::receive_(int port_num, const char *buffer, int len) and bm::SwitchWContexts::start_and_return_(). Your receive() implementation will be called for you every time a new packet is received by the device. In your start_and_return() function, you are supposed to start the different processing threads of your target switch and return immediately. Note that start_and_return() should not be mandatory per se (the target designer could do the initialization any way he wants, even potentially in the constructor). However, we have decided to keep it around for now.
Both switch classes support live swapping of P4-JSON configurations. To enable it you need to provide the correct flag to the constructor (see bm::SwitchWContexts::SwitchWContexts()). Swaps are ordered through the runtime interfaces. We ensure that during the actual swap operation (bm::SwitchWContexts::do_swap() method), there is no Packet instance inflight, which we achieve using the process_packet_mutex mutex). The final step of the swap is to call bm::SwitchWContexts::swap_notify_(), which targets can override if they need to perform some operations as part of the swap. Targets are guaranteed that no Packet instances exist as that time. Note that swapping configurations may invalidate pointers that you are still using, and it is your responsibility to refresh them.