Error in parsing

I am new to P4 and am using the tutorial VM to try to extract and parse some MQTT headers.
The log file for a switch in the logs folder had the following entries :

[18:11:40.129] [bmv2] [D] [thread 3539] [39.0] [cxt 0] Parser 'parser' entering state 'parse_ipv4'
[18:11:40.129] [bmv2] [D] [thread 3539] [39.0] [cxt 0] Extracting header 'ipv4'
[18:11:40.129] [bmv2] [D] [thread 3539] [39.0] [cxt 0] Extracting header 'mqtt_fixed'
[18:11:40.129] [bmv2] [D] [thread 3539] [39.0] [cxt 0] Parser state 'parse_ipv4' has no switch, going to default next state
[18:11:40.129] [bmv2] [T] [thread 3539] [39.0] [cxt 0] Bytes parsed: 35

The definition of the parse_ipv4 state is

state parse_ipv4 {
        transition parse_mqtt;

My question is how is the extraction of the ‘mqtt_fixed’ header occuring before entering the state ‘parse_mqtt’ when the extraction statement is declared in the parse_mqtt state?
I think this might have something to do with the incorrect data I was getting during the parsing phase.
Appreciate any help.

Sometimes the p4c compiler changes the contents of parser states from the original source code that you write. This might explain the BMv2 log statement order you are seeing. One way to try to determine whether this is true is to compile your P4 program with some additional command line options that cause it to print out modified P4_16 source files after the front and mid-end of the P4 compiler has finished processing your program, to see how it differs from your original program.

If your original P4 program is foo.p4, you can run a command like this:

mkdir tmp
p4c --target bmv2 --arch v1model --dump tmp --top4 MidEndLast foo.p4

Then look in the directory tmp for a P4_16 source file and read the source code of its parser.

I tried to run the command but it retuned “p4c: error: unrecognized arguments: --dump --top4 MidEndLast foo.p4”.
I did look into my output json file and yes, the “parsers” list did not have any entry for the state “parse_mqtt” also, the extract in the beginning of “parse_mqtt” was moved to the “parse_ipv4” state and the output for the “transition select …” statement that was in the “parse_mqtt” state seem to be missing even from the entry for “parse_ipv4”.

The log statements during packet parsing printed by BMv2 match the transformed/optimized parser code, not your original source file, then.

If you are willing to publish your P4 source code, and a description of some sample packet that you do not understand why it is behaving the way it is, that might be enough information for someone else to see what is going on here, but so far everything you described sounds like normal behavior given the way p4c and BMv2 work (admittedly it can be confusing to read and understand the BMv2 logs when things like this happen, but it is normal).

Thank you for your explanation. I was actually having some issues with the variable header extraction which is why i went looking at the logs. I have since figured out the problem’s solutions. :slight_smile: