How would one go about implementing load balancing in a round robin manner in a fat-tree topology with 4 pods(k=4) using P4?
Let’s say we have 4 ports as the candidate output ports. Think about the most simple round-robin for every packet that arrives to the switch. You would have to send every packet to one of the 4 available ports (in a round-robin manner). If you want to do that, you have to ask yourself the way to keep track of the last port used. In other words, you need to statefully keep track of the last port (i.e.e, use a register). You have to be able to read the register (to check the last port used), change the value for every new packet (keep this value for yourself to later use as output port) and write it back to the register.
You can smartly use 2 bits (4 values) for the register value as the base of the output port. The index of the register can always be the same, not relevant for this naive example (read about registers here). If you have 4 possible ports, most likely, your candidate round-robin ports are going to be 1,2,3 and 4, so you will need some simple (+ 1) addition to adapt the register value to the output port to be assigned to
egress_spec. Remember that when the register value reaches the maximum, you have to force it to the 0 again (because you cannot store values > 3 in the register if you use 2 bits as register value size ).
It is important to mention that if the output port does not correlate with the register values, then you require another approach. Let’s say that instead of ports 1,2,3, and 4, now, ports are 4, 7, 8, and 16, or any port number that you cannot predict. At this point, you might want to use the register value as an index of a P4 table. The basic example from the P4 tutorial helps on this particular case. You only have to change the key of the table.
I guess my answer applies to your use case, let’s say, a core switch that has 4 possible pods in aggregation. However, let me know if my explanation does not fit your use case.
I hope you can write yourself the P4 code.
Thank you for your guidance. I ended up not needing to implement it on a fat-tree topology. I instead implemented it on a simpler network and it worked.