[P4-dev] BMv2 with double precision numbers
cs.lev at gmx.com
Fri Nov 9 10:45:31 EST 2018
With respect to my previous mail about 'how to implement an extern', I
have created some simple externs to handle double precision numbers in
P4 in a slightly interesting way. However, neither the implementation
nor the solution itself might be perfect:
My basic idea behind handling and making operations with double numbers
was to avoid adding complete set of double precision functions as
externs. Instead, I thought that an extern function that can convert a
double (represented in hexadecimal/binary format) to an integer number
(uint64) via multiplying it with a precision parameter as a power of 10
(e.g., number of digits after the comma), the switch would be able to
make the basic operations as it would do in case of the well-known
integer numbers. Once calculations are done (e.g., subtracting two
ineteger numbers), another extern function will convert the result back
to a double number by again setting a precision parameter. Finally, the
function pass back the hexadecimal/binary representation of the double
number (not the double-typed variable of course) so the P4 application
can easily deal with it later on (e.g., setting it as a header value).
It sounds like the switch becomes a little bit dumb as it only knows
the operator but not the real operands and just do the calculations
without knowing what “exactly is being done”. For many of the double
precision number related problems I needed so far, the approach seems
to be sufficient. On the other hand, since double operators are not
needed to be implemented, it might be easier to adapt such an approach
in future P4 switch releases.
Although, I know that there was a certain reason why double numbers
are not supported currently: add some extra functionality to the
dataplane but keep the line rate throughput.
The only drawback (at the moment) with my approach is that we cannot
define a double number without knowing its hexadecimal/binary
representation (not to mention the endianness :(). But so far, it
worked as it should even for negative numbers.
I have created a github repository, feel free to try, comment, advise,
An example code is also available.
More information about the P4-dev