# Counting up to n packets and using that as trigger

**URL:** https://forum.p4.org/t/counting-up-to-n-packets-and-using-that-as-trigger/467
**Category:** Getting Started with P4
**Created:** [September 13, 2022, 8:26am UTC](https://forum.p4.org/t/counting-up-to-n-packets-and-using-that-as-trigger/467 "2022-09-13T08:26:35Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![yiwen](https://avatars.discourse-cdn.com/v4/letter/y/a9a28c/32.png) [@yiwen](https://forum.p4.org/u/yiwen)
#### Post date: [September 13, 2022, 8:26am UTC](https://forum.p4.org/t/counting-up-to-n-packets-and-using-that-as-trigger/467/1 "2022-09-13T08:26:35Z")

</div>

I would like to implement a way for the P4 switch to count to n ingress packets, and at that time, to trigger a set of actions for sampling a packet (like gather information about it and send it to an external controller) but nevermind the latter part for now.  
First to count to n packets, would this best be implemented using a counter or register? And then how do I read that value within the P4 program and put it in an if statement as a trigger?

Thank you

---

<div class="post-metadata">

### Author: ![DavideS](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/davides/32/120_2.png) [@DavideS](https://forum.p4.org/u/DavideS)
#### Post date: [September 13, 2022, 9:43am UTC](https://forum.p4.org/t/counting-up-to-n-packets-and-using-that-as-trigger/467/2 "2022-09-13T09:43:37Z")

</div>

Hi @yiwen,

As you can find in the P4\_16 spec [https://p4.org/p4-spec/docs/P4-16-v1.2.3.pdf](https://p4.org/p4-spec/docs/P4-16-v1.2.3.pdf), you can associate a direct/indirect counter to a table. So, you can count the packets that match a certain entry. Unfortunately, till now does not exist an extern/ “function” that allow you to read counter in the P4 program, you can read it via P4Runtime ([https://p4.org/p4-spec/p4runtime/main/P4Runtime-Spec.pdf](https://p4.org/p4-spec/p4runtime/main/P4Runtime-Spec.pdf)). While, you can read an write a register in the P4 program and via P4Runtime, but I think that are not the right structure. You could try to use Meter (they should be similar to counters), the have 3 different leve of color “metadata” and you can use them as a trigger in your P4 code.

---

<div class="post-metadata">

### Author: ![yiwen](https://avatars.discourse-cdn.com/v4/letter/y/a9a28c/32.png) [@yiwen](https://forum.p4.org/u/yiwen)
#### Post date: [September 14, 2022, 8:53am UTC](https://forum.p4.org/t/counting-up-to-n-packets-and-using-that-as-trigger/467/3 "2022-09-14T08:53:49Z")

</div>

Thank you for the reply. I’m not sure about using meters, because for meters you set a rate, but in this case I’m trying to do an action every n packets, whatever the rate is. I thought about using counters in the beginning but as you say counters can only be read by an external controller via P4Runtime. And I’m not really sure about registers

---

<div class="post-metadata">

### Author: ![DavideS](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/davides/32/120_2.png) [@DavideS](https://forum.p4.org/u/DavideS)
#### Post date: [September 14, 2022, 9:28am UTC](https://forum.p4.org/t/counting-up-to-n-packets-and-using-that-as-trigger/467/4 "2022-09-14T09:28:10Z")

</div>

Hi,

Maybe my comment is useless, but you can define Meter in packest or bytes. So, you can counting the packets and apply an action/do a manipulation of the packets. You do not have a fine count, but perhaps it is the right trade-off compared to using registers, in terms of memory occupancy, latency, computation resources, etc.

---

<div class="post-metadata">

### Author: ![ederollora](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/ederollora/32/21_2.png) [@ederollora](https://forum.p4.org/u/ederollora)
#### Post date: [September 14, 2022, 11:03am UTC](https://forum.p4.org/t/counting-up-to-n-packets-and-using-that-as-trigger/467/5 "2022-09-14T11:03:28Z")

</div>

There are no useless comments 🙂

---

<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: [September 14, 2022, 7:29pm UTC](https://forum.p4.org/t/counting-up-to-n-packets-and-using-that-as-trigger/467/6 "2022-09-14T19:29:39Z")

</div>

As DavideS mentioned, counters can be used to count events, but almost every P4 implementation I am aware of does NOT allow the data plane to read the values of counters. This is by design, because in a high speed implementation, there are tricks you can take advantage of in the hardware that make such unreadable-from-the-data-plane counters cheaper to implement than readable-from-the-data-plane counters, and such unreadable-from-the-data-plane counters are very very frequently used in production data planes.

I would recommend looking into P4 register arrays. They can be used to implement readable-from-the-data-plane counters, as well as fancier things than counters that can be read from the data plane.
