[P4-dev] Parse/process dynamic payload

cs.lev cs.lev at gmx.com
Tue Oct 9 08:26:18 EDT 2018

Just some small typo correction (first, I did not understand why your
copy-pasted code gave so interesting results :)):

hdr.tcp.dataOffset tells you the length of the TCP header in 32-bit
words, so to get in bytes one should multiply it by 32 and divide it by
In means, it should be multiplied by 32/8=4 (=pow(2,2), which is
shifting the value with 2 bits instead of 4 bits.
And getting it in bits then requires a shift of 5 bits (as

For the record, the following snippet is for storing this data in bits
and bytes:
meta.tcp_metadata.full_length = (hdr.ipv4.totalLen - IPV4_LEN) * 8;
        meta.tcp_metadata.header_length =
(((bit<16>)hdr.tcp.dataOffset) << 5);
        meta.tcp_metadata.payload_length =
meta.tcp_metadata.full_length - meta.tcp_metadata.header_length;

        meta.tcp_metadata.full_length_in_bytes =  (hdr.ipv4.totalLen -
        meta.tcp_metadata.header_length_in_bytes =
(bit<16>)hdr.tcp.dataOffset << 2;
        meta.tcp_metadata.payload_length_in_bytes = (hdr.ipv4.totalLen
- IPV4_LEN) - ((bit<16>)hdr.tcp.dataOffset << 2);

Thanks again Andy!


On Mon, 2018-10-08 at 08:47 -0700, Andy Fingerhut wrote:
> Change your parse state for parsing the TCP header so that after it
> extracts the TCP header, it calculates the length of the TCP payload
> in bytes, e.g. meta.tcp_payload_len = (hdr.ipv4.totalLen - IPV4_LEN -
> ((bit<16>) hdr.tcp.dataOffset << 4));

More information about the P4-dev mailing list