How to implement blockingusing p4?

I hope to use p4 to achieve the following functions: record the number and number of incoming data packets from a certain port, and rearrange the order of the data packets, for example, 5 data packets are incoming from port 1, and they are all sent from port 2 , numbered 1, 2, 3, 4, 5 respectively. I want to make packet 1 wait for a while, wait for packet 5 to be sent from port 2, and then send packet 1, so that the order in which they are sent becomes 2,3,4,5,1
I have implemented the counter function, but I don’t know how to block packet 1 and make it wait in the switch

@1181631986 – as with most “can I do X in P4” kind of questions, it is extremely important to mention the architecture you plan to use for your program.

For example, some P4_16 architectures (T2NA) do provide specialized mechanisms that allow the packets to be stored in manually controlled queues. By that I mean that individual packets are allowed to turn scheduling for these queues on and off (so-called XON/XOFF operations). Furthermore, the packets in the data plane can specify how many packets can leave the queue once it has been XON-ed. You can find the details in this presentation, slide 7.

In this case case you can use two queues: Q1 will be used to store the packet number 1 and Q2 will be used to store packets 2…5. Q1 will initially be XOFFed, meaning that packets sent to this queue will not be scheduled to be sent out. Q2 will work normally. Once packet number 5 arrives, it will signal Q1 that it can schedule one packet. As long as Q2 has scheduling priority higher than Q1, your packets will be sent out exactly in the order you like.

Please, see this post if you have further questions.

Happy hacking,
Vladimir