Dynamically Applying/Referencing a Match Action Table

The quick rundown on what I’m trying to do is to create a tree structure in P4 where each node maps 1-to-1 to its own distinct match-action table. Each node in the tree is assigned a unique global id where id 1 is the Root node. I’ve already created a Python script to automatically create the P4 source code for creating the tables with the following naming convention where i represents the current node’s id:

table node_i {
	key = {
		hdr.ipv4.srcAdd: lpm;
	}
	actions = {
		set_next_node_id;
	}
}

My original intention was for set_next_node_id to be an action that sets a temporary metadata variable next_id to the id of the chosen child node. Is there a way I can dynamically reference the next table to apply? For example, using Python f-strings as a reference, is there anything similar in P4 to call f’node_{next_id}'.apply() or would the only way be to brute force it by using chains of if statements hard coded to check for certain ids. Thank you!

There is nothing in P4 that lets you compute a table name to apply at packet processing time. if and switch statements are your main tool for directing flow of control there.

1 Like

Out of curiosity, and apologies if this comes across as a bit short-sighted/naïve, what is the pushback or reasons preventing such a feature from existing in P4? It seems to me (a P4 newbie) that the ability to compute table names at packet processing time would be a great tool for implementing match-action logic for complex data structures that follow some kind of fixed structure such as binary trees, decision trees, tree levels, pipeline stages, or any custom structure that can be broken down into subparts that follow a naming scheme. The table names could be node_id, tree_level, pipeline_stage, personal_id and the ability to resolve these table names at runtime would largely reduce the amount of repetitive source code.

There are obviously P4 programs running on general purpose CPU targets where it would be trivial to implement such a feature.

But the reason P4 exists is for target devices that can process packets for a much lower price & power per billion-of-packets-per-second than general purpose CPUs can, and at least some of their hardware designs physically locate tables within a pipeline, such that the packet must reach that stage of the pipeline before it is physically even possible to apply that table.

The kind of flexibility you are asking about could be restricted to be supportable on such targets, but it would need to be restricted in some way that your next question would likely be “why can’t I just pick any table id I want, in an arbitrary order, any time I want?”

And as a side note, I am a big fan of not typing repetitive code, but writing programs in Python (or your favorite general purpose programming language) that print out repetitive P4 code. e.g. see p4-guide/README.md at master · jafingerhut/p4-guide · GitHub