p4-spec icon indicating copy to clipboard operation
p4-spec copied to clipboard

PSA (and PNA): what types are valid for the hash extern object type params

Open apinski-cavium opened this issue 3 years ago • 4 comments

Maybe this is just me being pedantic here but the two arch spec does not go into any details about this extern at all. (PNA spec not even have a section about it either). For an example is it valid to have:

struct hashreturn
{
  int<32> t;
  int<54> tt;
}

Hash<hashreturn>(TARGET_DEFAULT) h;

Or we leaving this to the target to define what is allowed or not? It seems like we should have at least a note about leaving it up to each target of what is allowed or not here. Even worse is allowing a header stack of a header union here even valid?

(as an aside, I didn't see any backend in p4c testing if that type is valid or not for that target).

apinski-cavium avatar Sep 23 '22 21:09 apinski-cavium

Pedantic folk are in very good company among implementers of P4 tools :-)

The P4 implementations I am most familiar with allow at least all the 'bit-vector-ish' types, i.e. bit<W>, int<W>, bit, serializable enums with one of those types as the underlying type (which I think are the only underlying types allowed by the language spec, IIRC), or list expressions where all of the elements of the list are one of those types.

I would not be surprised if some implementations support passing an expression with a header or struct type, too, but I have not checked whether BMv2 or DPDK support those any time recently, and do not recall off hand.

jfingerh avatar Sep 28 '22 08:09 jfingerh

I should add that IF an implementation supports an expression with a header type as an input to a hash function, I would GUESS that they probably behave as follows, but this would require confirmation with any particular P4 implementation that allows it, and documenting additional details like this in the PSA specification seems reasonable to me.

  • If the header is valid, then the hash function hashes the values of all fields of the header, but ignores the "valid bit"
  • If the header is invalid, then the hash function does exactly the same thing, although according the language spec reading any field of an invalid header results in an unspecified value, thus the hash function output would be unpredictable without knowing more about the particular P4 implementation's behavior.

jfingerh avatar Sep 28 '22 08:09 jfingerh

Another reason to have traits/type families...

jnfoster avatar Sep 28 '22 16:09 jnfoster

Good guidance exists in https://github.com/p4lang/p4c/blob/main/p4include/v1model.p4#L429

hesingh avatar Nov 30 '22 22:11 hesingh