Externs in PSA Switch are different than those in Simple Switch.
Simple Switch uses externs baked into the BMV2 model where as PSA Switch
uses externs specific to its own architecture. To understand what this
translates to in code, look at the counter used in
Simple Switch
and the one in
PSA Switch.
The Simple Switch counter lives inside of src/bm_sim where as the
counter for PSA Switch lives inside of the target specific directory.
This doc is meant to provide guidance for developers on how to develop
new externs in PSA Switch.
For those who are familiar with the internals of BMV2 feel free
to skip this section. For those who are new/less familiar with how BMV2
operates, consider reading P4Objects.cpp as a starting point
as this is where externs will be created. From here you can also see
what the expected JSON for externs should be. Next, read the changes
in the following commit.
It details all the changes made to support the counter extern in PSA.
It will also be useful to understand on a high level how P4 Runtime
interacts with PSA Switch. Briefly skimming runtime_CLI.py will do.
The following process is meant to serve as a general guide on how to develop new externs in PSA Switch.
- Provide support for emitting the extern in p4c. Currently, only counters declared in P4 are translated to JSON in the p4c for PSA Switch. This commit is an example for how to emit an extern object to JSON, and this commit is an example for how to emit extern methods to JSON. If the examples aren't clear, one can always refer back to the JSON spec defined here. As for internals of how to the compiler works, check out its docs here.
- Create the extern type in PSA Switch. There are several important
things to note.
- The extern type must sub-class
bm::ExternType. - The extern should live within the
bm::psanamespace. - You must register the newly created extern type to the
extern factory used by BMV2 to create externs from JSON
using
BM_REGISTER_EXTERN_W_NAME. - You must also register any extern methods used by P4 using
BM_REGISTER_EXTERN_W_NAME_METHOD. - Create an
import_yourExternNameHeredummy method and call it withinpsa_switch.cpp.
- The extern type must sub-class
- Provide P4 Runtime support if necessary. This means overriding parts of the PSA Switch/P4 Runtime interface as necessary so that P4 Runtime can configure/monitor the newly developed extern.
Refer to this as a complete example.
Here is a list of all the bugs encountered during the development of counters and might serve as a helpful resource for others.
- The extern can't be created.
- Most likely a mis-match between the supplied and expected JSON format.
- Did you remember to register the extern into the extern factory?
- The extern method is not being called inside of an action.
- Did you remember to register the extern method? These must be done by the programmer.
- P4 Runtime can't interact with my extern.
- Did you remember to override necessary methods in the P4 Runtime interface?
- I created a new extern, added it to the Makefile, but it isn't
being linked.
- Dont forget step 2.5 from above.
- Everything in BMV2 looks good, but it seems that P4c is
failing.
- Supporting additional externs might have allowed previous tests marked as XFAIL to pass which will cause P4c to fail. Double check the test cases that have failed and see if this was the issue.