Appendix D P4 core library
The P4 core library contains declarations that are useful to most programs.
For example, the core library includes the declarations of the
predefined packet_in and packet_out extern objects, used in parsers
and deparsers to access packet data.
\~ Begin P4Example /// Standard error codes. New error codes can be
declared by users. error { NoError, /// No error. PacketTooShort, ///
Not enough bits in packet for ‘extract’. NoMatch, /// ‘select’
expression has no matches. StackOutOfBounds, /// Reference to invalid
element of a header stack. HeaderTooShort, /// Extracting too many bits
into a varbit field. ParserTimeout, /// Parser execution time limit
exceeded. ParserInvalidArgument /// Parser operation was called with a
value /// not supported by the implementation. } extern packet_in { ///
Read a header from the packet into a fixed-sized header @hdr /// and
advance the cursor. /// May trigger error PacketTooShort or
StackOutOfBounds. /// @T must be a fixed-size header type void
extract
/// Static assert evaluates a boolean expression /// at compilation time. If the expression evaluates to /// false, compilation is stopped and the corresponding message is printed. /// The function returns a boolean, so that it can be used /// as a global constant value in a program, e.g.: /// const version = static_assert(V1MODEL_VERSION > 20180000, “Expected a v1 model version >= 20180000”); extern bool static_assert(bool check, string message);
/// Like the above but using a default message. extern bool static_assert(bool check);
\~ End P4Example