Question about modify match-action table entry at runtime

I’m now looking for an opeation with the semantic value = hash(key) . In the P4 program, the match-action could be one solution after setting the table entries.
However, I want to change the table entries at runtime (dynamically add/remove those entries). The CLI could help (the table_add command) but I want to ask “is there a way to change entries in P4-16 ?”
Furthermore, if we cannot change entries at runtime in P4-16, is there exist the operations in P4-16 that has the same(or similar) semantic as hash ?

What P4 architecture / target are you working with? If the v1model architecture for the BMv2 software switch, there is a function called hash that is provided for you. Search for occurrences of hash in this file: p4c/v1model.p4 at main · p4lang/p4c · GitHub

That hash function is not configurable from the control plane – it calculates one of a selected few kinds of hash functions of its input value that you choose between at compile time.

If instead you want to use a P4 table, yes you can add/remove entries at run time. There are two control plane APIs supported by the BMv2 software switch:

  • a Thrift-based one that is custom to the BMv2 software switch, not used by any other P4-programmable devices that I am aware of.
  • the P4Runtime API, implemented by the simple_switch_grpc command name of the BMv2 software switch.

Both of these methods have an interactive CLI available, and both can be programmed from a control plane program that you may write yourself, e.g. in Python, C++, Java, etc.


I was writing an answer but it @andyfingerhut is always faster.

To define the semantically similar or “same” as hash functions in P4, and particularly about the V1Model, it is important to mention that no unkeyed cryptographic hash functions are currently supported (so you will not find MD5 or SHA). You will primarily find cyclic redundancy checks and checkums. I am not completely sure about the xor16, it might be universal hash function as listed here (correct me here if you feel it is mistake). Below list is extracted from v1model.p4.

enum HashAlgorithm {

While the supported algorithms do not cover cryptographic hash algorithms, any target might implement them. So you might have to check other targets, like hardware P4 targets.

You might want to check these papers for a more complete explanation: [1] for “Cryptographic Hashing in P4 Data Planes” and [2] (repo) since it is referred by [1].


1 Like

Thanks for your quick reply.

Yup currently I’m using the software switch (i.e. bmv2), and I’ve found one hash function in the v1model.p4 !!! Thanks for your replying :slight_smile:

However, I’ll move on my P4 programming on the HW switch. According to your description, the CLI could help. But there still remain several questions:

  1. Is there other ways to change the P4 table entries (apart from the RPC solution provided by the CLI) ?
    Yes I’ve found out the CLI command to change table entries, but I don’t want to loss the line rate property provided by the switch pipeline. Thus I’m finding out a way to
  • Change table entries in P4 program.
  1. Since I’ll move on HW switch afterwards, is those method(RPC CLI) still work on HW ?

Thanks for your reply !
The Cryptographic Hashing sounds quite appealing, and my case is simpler.
I’m just wanna to change the hash relations (e.g. cache existence). So I want to change match-action table entries at runtime. Furthermore, I wish to dynamically change them in p4 files.
But I suppose that maybe there’s noway to do it :rofl:

You might want to take a look at how the controller in Python install/removes rules from the switch. For hardware switch you might have to use another language and API, of course.

Actually this is not possible to do in P4 but there have been conversations about installing rules in the switch, such as in the “learning switch” use case. I don’t think this is possible yet though, who knows in the future…

Although it might not be helpful to you, you can create a table with fixed entries that is not possible to modify via P4runtime or CLI. I think this is possible in bmv2 Simple Switch but not sure if this is supported in
other targets. This is more like a static fixed-entry table (called const entries in P4).


I think @andyfingerhut 's first answer should be more of an overall correct answer for your particular question but let me try to help answer some of the rest of the questions you asked:

So, as far as I am concerned, the CLI tool uses a runtime protocol to communicate with the switch. It should be running P4Runtime or an equivalent protocol. Instead of using the CLI, you can automate this process by writing your own script that receives a packet from the switch (packet_in) and then installs a rule after (previously) having deleted it or just modify that rule. If the control plane runs physically close to the switch, you will experience less of a delay. If the control plane is centralized and physically “far” located from the switch, then the “latency” to modify a rule will higher. You can also be proactive and modify rules as you need without needing a packet from the switch (if that fits your use case). This can be achieved as shown in this or this examples. And if you are interested in Python and the P4runtime library, you might want to take a look on how to modify entries.

It all depends on the hw target you use. Some targets might be compatible with the CLI you are using now, and others will not. Some of the well known P4 hw targets (like TOfino-based switches) have a similar CLI tool and provide appropriate APIs for you to write your own control plane that deals with installing/modifying rules. This question, in particular, is better targeted to each of the ASIC (let’s say Intel), SmartNIC/DSC (like Pensando) or FPGA (like the NetFPGA Sume) -related forums since they must have a more specific answer about it.


Thanks! It really helps me a lot~