Can we use extract() function to write varbit data in other data types such as bit<32>? If yes, how?

Basically, the title. I read somewhere that we could use extract() to write varbit data into bit type. Is it possible? If yes, how?

There is no feature in any P4.org specification document, nor in any P4 implementation I have personally used (which is admittedly less than half of the variety that are out there), that enables one to copy data from a varbit type field into a field with non-varbit type, like bit<32>.

There are workarounds to this limitation, e.g. handle variable-length headers without ever using type varbit, by defining multiple different fixed-length headers, that include a definition for each possible header length you want to support. There are some ideas along those lines written up here, but I have not reviewed this article in the last couple of years to see if there might be better ideas available: https://github.com/jafingerhut/p4-guide/tree/master/variable-length-header

1 Like

Hello Sandeep,

There is an enhancement request for the P4 specification, but it has not been approved or implemented.

Obviously, this does not prohibit individual architectures to define their own extern methods that would do the same thing (and in certain cases they would be relatively straightforward to implement), but I haven’t heard of any that would actually implement such a functionality.

As of today, the rule is very simple: use varbit only if you intend to carry a variable-length field from ingress to egress without doing any “real” processing. If you want to do any processing (lookup, modification, etc, etc.) use multiple fixed-length chunks instead, as described in @andyfingerhut’s guide or as was clearly suggested in my previous post.

Happy hacking,
Vladimir

1 Like

Thank you for all your wonderful insights. I finally decided to ditch the varbits and it worked wonderfully, thanks to this forum and the knowledge catered here.

I look forward to learning more from here.

Thanks and regards,