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

Swaroop Thool swaroopthool1991 at outlook.com
Wed Feb 10 11:38:45 EST 2016

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

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.


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



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

More information about the P4-dev mailing list