Assigning value to ipv4.options

Hello everyone,
I have a header named ipv4_options_t and inside it is a single variable: bit<128> options;
I want it to carry the standard_metadata.ingress_global_timestamp, so I assigned the timestamp to ipv4.options by:
hdr.ipv4Options.options = (bit<80>)0 ++ standard_metadata.ingress_global_timestamp;
However, this did not really work. As you can see in the log below, it is strange how the last 32 bits are assigned, but not the whole options field (still 0).
To make things worse, if I set the options field to be valid by:
hdr.ipv4Options.setValid();
This will even make the last 32 bits to be 0 as well. And I have tried to capture the packet using Wireshark, the raw bytes are 0.
Could someone please tell me why this is happening?
Just so you know, I did not parse the options field in the parser.
Thanks a lot!

Did you do hdr.ipv4Options.setValid(); before or after the assignments to the fields of hdr.ipv4Options? If before, good. If after, note that the language spec says the assignments to fields of an invalid header are a no-op. Then later doing the setValid() will leave the fields with undefined values – at least, they are not defined by the language spec. I would not be surprised if the BMv2 implementation would consistently initialize them with 0.

1 Like

What I did was setting setValid() after the assignment. Now I have fixed this by doing so before the assignment.
Thank you so much for this!