Exporting extracted payload to a C program

Hello, i am trying to figure out how to extract a layer 3 packet, encrypt the data part, repackage it then forward the packet as normal. The packet extraction and parsing are being done with P4, but it would be VERY NICE to be able to offload the packet encryption to a C written program.
I imagine it be:
parsing() → pass the parsed data to the C written process → wait to receive back the new data → continue with the pipeline.
Is there a way to do this? I am quite new to the P4 language, so thanks in advance for any input.

If you are writing P4 programs that run on a software switch like BMv2 or DPDK, then yes, it is possible to write C or C++ code that can be invoked from your P4 program. However, there is no tutorial document describing step by step how to do it.

About the closest thing I know of that exists for the BMv2 software switch is this example of a BMv2 pull request that adds a new P4 extern definition: Demonstration of adding a new extern to BMv2 / behavioral-model

Note that the act of implementing an extern is different from P4 target to P4 target. If you do this for BMv2, and then want to use that extern on an FPGA target device that is P4-programmable, you would need to reimplement the extern for that FPGA target.

For some ASIC target devices, it is not possible to add new functionality in this way. That is, unless you consider the approach of “send the packet to a general purpose CPU nearby, do the desired processing there, and send the packet back to the P4-programmable device”, but then you have the price/power/performance ratios that can be achieved on that general purpose CPU, which are often quite different than the kinds of devices that inspired P4.