[P4-dev] Where to implement if-else apart from control flow to set value

LJ Wobker ljw at barefootnetworks.com
Tue Aug 18 12:58:23 EDT 2015

One of the other (smarter) guys will correct me if I’m wrong, but given
that you can’t do conditionals in actions, and you’re limited to doing a
comparison per stage when you have dependent lookups, I think you can solve
this in two ways (perhaps more that I’m not thinking of)

Method one is effectively a bubble sort – in stage N you do a compare of
your first two possible items and get the lowest, then store it.  Then in a
subsequent stage, you do a comparison of that value and the max (or min)
floats/bubbles to the top.  This becomes inefficient for large numbers of

If you’re willing to burn extra metadata, I think you’d be able to store
all of your elements and then do a binary search style comparison… if you
had for example 7 elements, you should be able to do 3 comparisons in the
first stage, eliminating 3 non-minimum elements.  In stage N+1, you’d do
two comparisons on the remaining 4 elements, eliminating 2 more.  Then in
stage N+2 you’d choose between the last two.

All of that said, I don’t think the language has a particular elegant way
of doing min/max searching or sorting.  I know that this capability has
been discussed, but given that the various implementations (targets) is
very wide, some of those targets are unlikely to be able to efficiently
sort/select values… so I’m not yet convinced that having this primitive
makes sense.

I’m sure others will also have thoughts on this…


*From:* P4-dev [mailto:p4-dev-bounces at p4.org] *On Behalf Of *Swaroop Thool
*Sent:* Tuesday, August 18, 2015 5:41 PM
*To:* p4-dev at p4.org
*Subject:* [P4-dev] Where to implement if-else apart from control flow to
set value

Hi All,

I am learning p4. I want to know where can steps be written to implement
simple operations.

1)  Where can I put snippet shown below which can set a field of
metadata/register means, in control flow or action only? for example: If I
want to use if-else statement where comparison between metadata fields are
considered and output of it should set one field from that metadata.
I have seen if-else are only used to call "apply(table)" in control part
and I also cannot carry out if-else in action definition of that table.

Above example: I didnt find syntax to directly find minimum among three.
This is simple snippet to find minimum of three. I get syntax error when I
tried to use in control or action part. So I want to use like this without
creating new table.

*      if ((local_metadata.val1 < local_metadata.val2) and
(local_metadata.val1 < local_metadata.val3)){
modify_field(local_metadata.minimum, local_metadata.val1)       }      else
if ((local_metadata.val2 < local_metadata.val3) and (local_metadata.val2 <
local_metadata.val1)){                modify_field(local_metadata.minimum,
local_metadata.val2)      }      else
modify_field(local_metadata.minimum, local_metadata.val3) How can this be
implemented in p4 terms and inside what?2) I came across serial and
parallel semantics. What if I want use value of 1st modify_field() into
next modify_field() under same action definition. Is parallel behaviour can
be changed?3) I am unclear with Action Profile part of specification to
implement it. Can anyone explain it?*Thanks,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.p4.org/pipermail/p4-dev_lists.p4.org/attachments/20150818/e38857b7/attachment-0001.html>

More information about the P4-dev mailing list