[P4-dev] Simulate FOR and/or WHILE cycles (p4-14)

Andy Fingerhut andy.fingerhut at gmail.com
Thu Apr 13 11:49:34 EDT 2017

Arrays of structs would be nice for some things, for sure, as well as
loops, but given the design of some of the targets P4 is capable of writing
programs for (i.e. single chips forwarding packets at billions of packets
per second), the language has avoided constructs that would lead to
unbounded computation per packet.

Some hack-ish things you may consider trying:

Use a little Perl or Python program that has a loop, and use it to generate
repetitive P4 code.  For example, if you wish you had P4 code that looked
like this:

    for (i = 0; i < hdr.foo.some_field; i++) {
        meta.tmp = some expression;
        store meta.tmp in a P4 register at index base_value+i

you could 'unroll' the loop like this:

    i = 0;
    if (0 < hdr.foo.some_field) {
        meta.tmp = some expression, where i is replaced with constant 0;
        store meta.tmp in a P4 register at index base_value+0
    if (1 < hdr.foo.some_field) {
        meta.tmp = some expression, where i is replaced with constant 1;
        store meta.tmp in a P4 register at index base_value+1

etc. repeating until i is some constant that you know is the largest
possible value for hdr.foo.some_field.

registers are the main construct that P4_14 has, and the open source
compiler and behavioral model for P4_16 has, for making storage that can be
both read and written while forwarding packets, but if you want to do that
at billions of packets per second, you better ask the maker of your chip
first before relying on those.  If you just want to do it in software
forwarding, the open source tools probably do what you need.


On Thu, Apr 13, 2017 at 6:17 AM, Diogo Pinto <diogo.f1.p2 at gmail.com> wrote:

> Hello,
> I need to perform several multiplications and additions over a subset of
> bits and packets.
> For instance:
> 1. Grab X bits of packet Y and perform several additions/multiplications
> over them
> 2. Repeat 1 for several other packets Z
> 3. Store all the results for later use
> The main issue is that Z can reach hundreds, and so the problem is:
>    - The quantity of registers I need to store all the bits,
>    - All the metadata required, to copy the values from registers, in
>    order to be able to use it as a parameter to multiplications and additions,
>    - And all the metadata required to store intermediate results.
> Also, I cant even have stacks of metadata headers. You can see the problem
> of manually store all values and perform all operations. It will take ages
> to write the code. Not to say, the complexity of it.
> As far as I know P4 doesnt implement cycles. Is there any way of
> simulating one ?
> Also, is there any strategy to organize my metadata, so that I can use
> some kind of index to access some field, instead of manually specify the
> field I want to manipulate ?? That way, maybe I could write some generic
> actions, to be used in several contexts.
> Thank you,
> Diogo.
> _______________________________________________
> 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/20170413/8e15777c/attachment-0002.html>

More information about the P4-dev mailing list