[P4-dev] A beginner's question about p4 calling C library functions

Vladimir Gurevich vladimir.gurevich at barefootnetworks.com
Wed Aug 24 01:55:29 EDT 2016


Hello Chang,

In terms of regular arithmetic, most implementations do support more
primitives than just add() and add_to_field(), mandated by the spec. For
example, subtraction is also supported as well as a normal set of bitwise
logical operations, such as bit_and(), bit_or() , etc. The current
specification draft (v.1.1) also allows using expressions in
modify_field(). The freely available compiler (p4c-bm) also supports
expressions.

In terms of more complex/specialized functions (such as string compare you
mentioned in your email), you have to be more careful. I'll start by
reminding you that P4 is not a general-purpose programming language. It was
specifically designed for describing packet processing in high-speed
pipelines and thus has a fairly limited number of primitives that we know
can be efficiently implemented (particularly in hardware). For that same
reason, interfacing with C is not a goal of the language -- in the majority
of real-world situations, P4 programs are not going to be run on
general-purpose CPU to begin with and thus there will be no C libraries to
link to.

Having said that, there are software implementations (such as the
BMv2-simple_switch behavioral model) that can be relatively easily extended
by adding new P4 primitives, implemented as C functions. Note, though, that
programs using them will not be portable to other systems unless they
somehow implement these primitives. Ideally, you should create a separate
target (i.e. my_switch instead of simple_switch) to avoid confusion and
incompatibilities, though it is not strictly required.

If you search this list, you'll see a couple of threads that explain how
this can be done.

Happy hacking,
Vladimir

On Mon, Aug 22, 2016 at 10:36 PM, Chang Liu <gaustin909 at gmail.com> wrote:

> Hi, everybody,
>
> I am a P4 newbie and I have a simple question about P4: It seems to me
> that doing arbitrary arithmetic operations or string comparisons is not
> well supported in P4 (correct me if I am wrong). So is it possible in P4 to
> call external C library functions?
>
> Thanks for your help in advance.
>
> Best
> Chang
>
>
>
> _______________________________________________
> P4-dev mailing list
> P4-dev at lists.p4.org
> http://lists.p4.org/mailman/listinfo/p4-dev_lists.p4.org
>



-- 
*Vladimir Gurevich*

*Barefoot Networks*
*Technical Lead, Customer Engineering*
Email: vag at barefootnetworks.com
Phone: (408) 833-4505
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.p4.org/pipermail/p4-dev_lists.p4.org/attachments/20160823/6dde322b/attachment-0002.html>


More information about the P4-dev mailing list