# Error in parsing

**URL:** <https://forum.p4.org/t/error-in-parsing/293>\
**Category:** Uncategorized\
**Created:** [March 31, 2022, 6:56pm UTC](https://forum.p4.org/t/error-in-parsing/293 "2022-03-31T18:56:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Parardha](https://avatars.discourse-cdn.com/v4/letter/p/8797f3/32.png) [@Parardha](https://forum.p4.org/u/Parardha)\
**Post date:** [March 31, 2022, 6:56pm UTC](https://forum.p4.org/t/error-in-parsing/293/1 "2022-03-31T18:56:46Z")

</div>

Hi.  
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 :

```auto

[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

```auto
state parse_ipv4 {
        packet.extract(hdr.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.

---

<div class="post-metadata">

**Author:** ![andyfingerhut](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/andyfingerhut/32/17_2.png) [@andyfingerhut](https://forum.p4.org/u/andyfingerhut)\
**Post date:** [April 1, 2022, 6:47am UTC](https://forum.p4.org/t/error-in-parsing/293/2 "2022-04-01T06:47:18Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Parardha](https://avatars.discourse-cdn.com/v4/letter/p/8797f3/32.png) [@Parardha](https://forum.p4.org/u/Parardha)\
**Post date:** [April 1, 2022, 8:19am UTC](https://forum.p4.org/t/error-in-parsing/293/3 "2022-04-01T08:19:27Z")

</div>

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”.

---

<div class="post-metadata">

**Author:** ![andyfingerhut](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/andyfingerhut/32/17_2.png) [@andyfingerhut](https://forum.p4.org/u/andyfingerhut)\
**Post date:** [April 1, 2022, 3:47pm UTC](https://forum.p4.org/t/error-in-parsing/293/4 "2022-04-01T15:47:34Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Parardha](https://avatars.discourse-cdn.com/v4/letter/p/8797f3/32.png) [@Parardha](https://forum.p4.org/u/Parardha)\
**Post date:** [April 1, 2022, 4:22pm UTC](https://forum.p4.org/t/error-in-parsing/293/5 "2022-04-01T16:22:23Z")

</div>

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. 🙂
