How to wait for a period in P4

Hello,everyone,

Recently, I have had some questions about the P4 .
I want to store the packet and wait one second, then forward the packet to a specific port. I find how to store packet data in the register. However, I don’t know how to wait for a period in P4.

Thanks in advance!

Hi and welcome @ckt620and,

I believe this is not a feature present in bmv2 simple switch, if you were asking about bmv2 (might be wrong though). I think I have seen other people requesting this feature or asking questions about it but not sure if anyone did any progress on their own. I also believe that this is quite an expensive feature to implement in P4 hardware switches, so I believe this has little to no sense in terms of the resources needed (in the medium/large scale of random time packet buffering). Of course, if the feature has a strong use case, I guess it could be present at some point in time.

When it comes to bmv2 or software switches, I think this feature is easier to implement and more cost effective. I think you have several options right now, not all focus on what you exactly need but they might work:

  • If you do not want to work a lot developing this feature, you might want to send the packet to the controller. Then keep it some time and send it back. Your precision is little in this case, you will probably never be able to be very precise us/ms if that is your time scale. If you are willing to allow a large error, this is your best bet. When I mean a large error, I mean that if you plan to keep the packet 500 ms, there might be 50 or 100 ms of innacuracy. I am not sure about the actual error offset, but I guess you understand what I mean.

  • Alternatively, you can try to create the “timers” at the control plane and then send a packet to the switch that functions as a trigger. Instead of buffering a packet, you make the control plane become the one that triggers sending the delayed packet. So the packet generated from the control plane will trigger capturing the values you need from the register and then this new packet is built and sent.

  • You can go all the way and develop a new pkt.emit_sleep(header.field, time). I have no idea how to exactly do this but I imagine it will not be an easy task. You have thoguh some nice people that have contributed to a guide a few days ago. Check this thread or PR. I guess this will also require a change into the P4 compiler.

  • You can try to imitate a cloning function like E2I that drops one of the packets and the cloned one sleeps until it is sent to ingress. Then you can try to check if it is cloned and use a particular port. This is not exactly what you asked but it is another possibility. This will require similar changes compared to the previous point.

Hope it helps :slight_smile: