How do I implement Direct Counter in v1model

How do I implement Direct Counter in v1model, I need to be able to read the value of the count during the ingress phase. I read that the documentation says that the Direct Counter value provided by itself can only be fetched at the control plane.
I am trying to implement weighted multipath routing and want to assign different outgoing ports based on the number of matches in the flow table

Hello @YPL,

As always, the best thing to start with is to specify which P4_16 architecture you want to use (as is explained in this post).

Happy hacking,

I’m very sorry, I’ve modified my question to make it more specific

Dear @YPL,

You are correct – the direct_counter extern, provided by the v1model architecture, can only be incremented in the data plane and cannot be read. It is also true, that if one needs to both count the number of packets or bytes in the data plane and read the value of such a counter in the data plane, they need to use a different extern, such as register.

Unfortunately, the v1model architecture does not provide a direct register extern, similar to direct_counter or direct_meter. Thus, your only option is to use the (indirect) register extern and manage the indexes manually.

Happy hacking,

I have another question, if I create a register like this and put it in an action to use, isn’t its common to the ingress stage, that is, all the flow tables that call that action use this one register instance. Instead of starting a counter for each flow table as in direct counter

In BMv2 there would be only one instance of a register shared by all packets performing ingress processing, yes.

In some physical switch ASICs, that would not be the case – e…g in Tofino with 4 pipes, there would be one independent instance of the register per pipe. This is something that you must account for in your implementation when running on such a device.

1 Like

In the case where a register instance is shared by all packets, but you want the entries of the register to be divided up in some way, for example, by input port, you can do this yourself by writing appropriate P4 code. For example, if you have 32 input ports, and you want to dedicate 128 register entries to each input port, you could have 32*128 = 4096 entries, and use a formula like (128*input_port + entry_number_idx) to address those 4096 entries.

1 Like

Thanks for your reply, it was very helpful!