[P4-dev] Help to writing functor using available C++ functions/library

Antonin Bas antonin at barefootnetworks.com
Wed Feb 10 16:44:18 EST 2016


The simple_switch target has the modify_field_rng_uniform() primitive which
generates a random integer uniformly between 2 (positive) integers:
https://github.com/p4lang/behavioral-model/blob/master/targets/simple_switch/primitives.cpp#L43
You can also write your own action primitives (just like you can write your
own hash functions), so bmv2 pretty much lets you do whatever you want in
actions. Although bmv2 gives you great freedom, programmers writing P4
programs for specific targets will probably not have that level of
freedom... Also, regarding the last part of your email, P4 programmers
usually try to do all packet processing in the data plane, without having
to send the packet to the controller. However, at the moment, P4 does not
offer a way to do learning (e.g. mac address learning) directly in the P4
program, which is one example for which packets have to be sent to the
controller; more generally there is no way to update P4 tables directly
from the P4 program.

On Wed, Feb 10, 2016 at 8:38 AM, Swaroop Thool <swaroopthool1991 at outlook.com
> wrote:

> Hi Antonin and All,
>
> Thank you for your answer. But is there any way to implement such random
> number generator function apart from hash function format?
> My second thought of implementing this operation and any other arithmetic
> which are not supported by P4 yet on any other x86 (partly) for computing
> by sending packets and then again giving back to switch to carry out its
> work. However, if this is possible then, how can I send and receive the
> 'same packet' bytes to and from x86 when packets comes to switch. This can
> cause significant overhead by waiting for computation and get that packet
> back on switch
>
> Thanks,
> Swaroop
>
> ------------------------------
> Date: Tue, 9 Feb 2016 23:09:05 -0800
> Subject: Re: [P4-dev] Help to writing functor using available C++
> functions/library
> From: antonin at barefootnetworks.com
> To: swaroopthool1991 at outlook.com
> CC: p4-dev at lists.p4.org
>
>
> Hi Swaroop,
>
> The return type of your functor is uin16_t, so trying to return -1 is
> probably not a good idea. Note that according to bmv2 design you cannot
> define a hash function with a signed return type.
> Now I am pretty sure that if you return 1 or 2 based on a random output,
> you will observe the correct behavior.
>
> Antonin
>
> On Tue, Feb 9, 2016 at 10:33 AM, Swaroop Thool <
> swaroopthool1991 at outlook.com> wrote:
>
> Hi Everyone,
>
> I want to write functor for generating random values of +1/-1 to implement
> one of my interested algorithm in p4. So referring Antonin's post on
> writing own hash functions I have written something like this:
>
> *struct my_hash {*
> *     uint16_t operator()(const char *buf, size_t s) const {   *
> * return (random() & 1 ? 1 : -1);*
> *       }*
> *  };*
>
> *REGISTER_HASH(my_hash);  *
>
> I can get access for it by writing:
> *field_list_calculation myhash  {*
> *    input {*
> *        hashvalue_list;                               * // *I dont
> require this , is this necessary?*
> *    }*
> *    algorithm : my_hash; *
> *    output_width : 16;*
> *}*
>
> Then calling in some action :
> * modify_field_with_hash_based_offset(mymetadata.val,myhash,0,1000);*
>
> When I add this in my previous program which implements copy_to_cpu , I
> can see only 00 00 for both +1 and -1? Is this because negative value
> support explanation of my previous post applies here? If yes, then for both
> +1 and -1  it should have come as 01 01.
> OR I am writing above functor in wrong way? Please help.
>
> Thanks,
> Swaroop
>
> _______________________________________________
> P4-dev mailing list
> P4-dev at lists.p4.org
> http://lists.p4.org/mailman/listinfo/p4-dev_lists.p4.org
>
>
>
>
> --
> Antonin
>
> _______________________________________________
> P4-dev mailing list
> P4-dev at lists.p4.org
> http://lists.p4.org/mailman/listinfo/p4-dev_lists.p4.org
>



-- 
Antonin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.p4.org/pipermail/p4-dev_lists.p4.org/attachments/20160210/7e931507/attachment-0002.html>


More information about the P4-dev mailing list