Can we use the P4 program to turn a switch equipped with Tofino 2 into an Infiniband switch?
If you want to know what Tofino 1 or 2 are capable of in detail, it is better to ask on Intel support forums about that. See this other question and its answers: Intel(r) Tofino(tm) Family, TNA, and P4Studio questions
That said, I know that Tofino 1 and 2 have Ethernet ports with Ethernet MAC logic on the chip for receiving and sending Ethernet frames on those ports. If by “Infiniband switch” you mean a switch with Infiniband links, then my belief is that Infiniband links and Ethernet links are enough different that you cannot implement Infiniband links with Ethernet MAC logic.
Now if you mean “Can you implement an Ethernet switch that can parse RoCEv2 headers inside of Ethernet frames?”, then you can definitely do that: RDMA over Converged Ethernet - Wikipedia