Skip to content

Get size of header #660

@theojepsen

Description

@theojepsen

How do you get the size of a header in P4? Here is an example of what I'm trying to do:

action set_len() {
    hdr.ipv4.totalLen = len(hdr.ipv4) + len(hdr.udp) + len(hdr.my_proto);
}

len should return the size of the header (in bytes). In Python you can do this with Scapy. E.g. use len(scapy.UDP()) to get the size of a UDP header (i.e. 8 bytes).

The best workaround that I've found is to use a #define to set all the header sizes at the begging of my program, e.g.

#define IP_HDR_SIZE  20
#define MY_PROTO_HDR_SIZE 12

However, this has caused my lots of headaches. I can't tell you how many times I've changed the number/size of header fields, forgetting to update the constant, causing me to waste hours troubleshooting parser errors.

This seems like something straight-forward to implement in the compiler, possibly in the front-end. Since this can be calculated statically at compile time, it should be target-agnostic.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions