And what happens when a pkt that is too short, like it contains a valid ethernet header, but not enough bits to extract a ipv4 header. What are those ipv4 fields after the extraction process? Like srcAddr, dstAddr and, ihl? Are they all 0 if the packet is too short?
The P4 language specification says that when parsing such a packet, that contains enough bytes for a full Ethernet header, but not enough additional bytes to include a full IPv4 header, the parser you should do the following:
in state parse_ethernet, packet.extract(hdr.ethernet) will execute normally, leaving hdr.ethernet.isValid() equal to true, and all fields of hdr.ethernet equal to a copy of the corresponding bits from the parsed packet.
then assuming that hdr.ethernet.etherType is equal to TYPE_IPV4, the parser will transition to state parse_ipv4, where it will execute packet.extract(hdr.ipv4). Because there are not enough bytes to completely fill all of the fields of hdr.ipv4, parsing will immediately end, without executing any following statements, including not the “transition accept” line in state parse_ipv4. hdr.ipv4.isValid() will be false. The parser will transition to the special state “reject” and stop execution immediately.
Note that just because the parser transitions to state reject does not automatically mean that the packet will be dropped. In many P4 architectures, including v1model, PSA, and PNA, it is defined that the packet will be processed by the following control. If the developer wishes the packet to be dropped always if they experience a parse error, they should check the error status from the parser to see if it indicates a parsing error occurred, and drop the packet.
The P4 language spec says that if you attempt to use the value of a field within a header that is currently invalid, then an implementation is allowed to give back unpredictable values. e.g. hdr.ipv4.ihl might be 7 when you use it in one expression, and in the very next line of code if you use hdr.ipv4.ihl again it might be 1. A particular P4 implementation might behave in a more predictable way, if the implementers choose, but the language spec does not require them to be more predictable.
Thanks for your detailed explanation, that would answer my question. And also you mentioned that the answer is somehow mentioned in the p4 language specification,, but I was overwhelmed by its content and had no idea of how to find the solution. So do you have any suggestions based on your experience?
Just a small note: if you are really using Ethernet, then there is no way that the packet will be too short in your particular case. The minimum Ethernet frame size is 60 bytes plus 4 bytes of FCS, whereas the combined size of the Ethernet header plus IPv4 header is 34 bytes.