p4lang / p4c

P4_16 reference compiler
https://p4.org/
Apache License 2.0
642 stars 430 forks source link

Mixed compiling with C code? #2066

Closed BDHU closed 4 years ago

BDHU commented 4 years ago

I noticed the the P4 specification mentioned there's a way to include extern libraries. Is there any instruction to compile my P4 program with C code together or is there a way to compile P4 source code such that the output file could be incorporated with C? I haven't found much instructions regarding how to achieve that. Any help will be appreciated!

Edit: To be more specific, I have a software TCP acceleration stack implemented in C running on a CPU core. I want to be able to express simple data path logic in P4 and all the other control tasks such as buffer allocation still in C since P4 is quite limited in that sense. Is there any way to achieve that?

jnfoster commented 4 years ago

The P4 language does not specify the structure of the architecture, or which externs are supported. So your question would be best asked in the context of a specific architecture.

If the architecture is being deployed on a switch ASIC like Barefoot Tofino, then there is no general way to link arbitrary C code with the Tofino binary and have something sensible happen.

If you are compiling to a target like eBPF or XDP, then it may be possible to extend the compiler with new externs backed by a C implementation, or even link the generated code with your own. However, the details of how to do this would be best answered by the developers of those platforms. The p4c compiler itself does not facilitate this behavior.

Finally, the compilers for flexible devices like Netronome SmartNICs and Xilinx SD-NET may support embedding C source with P4 source code. Again, the details of how to do that would be best asked to the developers of those targets.