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

Changhoon Kim chang at barefootnetworks.com
Thu Aug 25 00:39:46 EDT 2016

Hi Chang,

As Vlad mentioned, one can't implement complex computing functions -- such
as encryption, decryption, pattern matching, or string manipulation -- in
P4, and that's by design. P4 is a language to describe packet-forwarding
logic, and we intend to keep it that way.

That said, we also understand P4 programmers sometimes will need to
"consume" complex computing functions if they are already made available in
the processing pipeline. The P4-design working group has been working to
offer such extensibility of P4 without bloating the P4 language. The extern
type -- introduced in P4_14 v1.1 spec (http://p4.org/spec/) -- offers such
extensibility. I believe some P4 target providers could expose a few
C-library functions at the P4 level as "extern" types. But such extern
types will most likely be supported only on those special types of targets.

-- Chang (Kim)

On Wed, Aug 24, 2016 at 2:55 AM, Vladimir Gurevich <
vladimir.gurevich at barefootnetworks.com> wrote:

> 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
> _______________________________________________
> P4-dev mailing list
> P4-dev at lists.p4.org
> http://lists.p4.org/mailman/listinfo/p4-dev_lists.p4.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.p4.org/pipermail/p4-dev_lists.p4.org/attachments/20160825/1ea2406f/attachment-0002.html>

More information about the P4-dev mailing list