# How do I implement Direct Counter in v1model

**URL:** <https://forum.p4.org/t/how-do-i-implement-direct-counter-in-v1model/1185>\
**Category:** Getting Started with P4\
**Created:** [May 7, 2024, 4:48am UTC](https://forum.p4.org/t/how-do-i-implement-direct-counter-in-v1model/1185 "2024-05-07T04:48:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![YPL](https://avatars.discourse-cdn.com/v4/letter/y/4af34b/32.png) [@YPL](https://forum.p4.org/u/YPL)\
**Post date:** [May 7, 2024, 4:48am UTC](https://forum.p4.org/t/how-do-i-implement-direct-counter-in-v1model/1185/1 "2024-05-07T04:48:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![p4prof](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/p4prof/32/251_2.png) [@p4prof](https://forum.p4.org/u/p4prof)\
**Post date:** [May 7, 2024, 6:18am UTC](https://forum.p4.org/t/how-do-i-implement-direct-counter-in-v1model/1185/2 "2024-05-07T06:18:41Z")

</div>

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](https://forum.p4.org/t/use-these-three-magic-words-to-ask-your-p4-questions-like-a-pro/966)).

Happy hacking,  
Vladimir

---

<div class="post-metadata">

**Author:** ![YPL](https://avatars.discourse-cdn.com/v4/letter/y/4af34b/32.png) [@YPL](https://forum.p4.org/u/YPL)\
**Post date:** [May 7, 2024, 9:28am UTC](https://forum.p4.org/t/how-do-i-implement-direct-counter-in-v1model/1185/4 "2024-05-07T09:28:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![p4prof](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/p4prof/32/251_2.png) [@p4prof](https://forum.p4.org/u/p4prof)\
**Post date:** [May 7, 2024, 8:45pm UTC](https://forum.p4.org/t/how-do-i-implement-direct-counter-in-v1model/1185/5 "2024-05-07T20:45:26Z")

</div>

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,  
Vladimir

---

<div class="post-metadata">

**Author:** ![YPL](https://avatars.discourse-cdn.com/v4/letter/y/4af34b/32.png) [@YPL](https://forum.p4.org/u/YPL)\
**Post date:** [May 8, 2024, 2:18am UTC](https://forum.p4.org/t/how-do-i-implement-direct-counter-in-v1model/1185/7 "2024-05-08T02:18:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![andyfingerhut](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/andyfingerhut/32/17_2.png) [@andyfingerhut](https://forum.p4.org/u/andyfingerhut)\
**Post date:** [May 8, 2024, 1:49pm UTC](https://forum.p4.org/t/how-do-i-implement-direct-counter-in-v1model/1185/8 "2024-05-08T13:49:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![andyfingerhut](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/andyfingerhut/32/17_2.png) [@andyfingerhut](https://forum.p4.org/u/andyfingerhut)\
**Post date:** [May 8, 2024, 3:49pm UTC](https://forum.p4.org/t/how-do-i-implement-direct-counter-in-v1model/1185/9 "2024-05-08T15:49:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![YPL](https://avatars.discourse-cdn.com/v4/letter/y/4af34b/32.png) [@YPL](https://forum.p4.org/u/YPL)\
**Post date:** [May 15, 2024, 2:26am UTC](https://forum.p4.org/t/how-do-i-implement-direct-counter-in-v1model/1185/10 "2024-05-15T02:26:37Z")

</div>

Thanks for your reply, it was very helpful!
