Hardware interview practice
Locate the payload in an Ethernet frame
Firmware receives a complete Ethernet frame without FCS. It supports either an untagged header or one 802.1Q tag with TPID 0x8100. Write a bounded C parser that returns EtherType and payload offset.
Starting point
Question code
int eth_payload(const uint8_t *f, size_t n,
uint16_t *type, size_t *offset);Reviewed example
Work through one case
Input
An untagged frame of at least 14 bytes has f[12]=8'h08 and f[13]=8'h00.Expected output
Return 0 with type=16'h0800 and offset=14.The big-endian field is not the VLAN TPID 16'h8100, so it is the EtherType and the payload begins after the base Ethernet header.
What to cover
Requirements
- Require f, type, and offset and require n>=14; return -1 and preserve outputs otherwise.
- Read the big-endian field at bytes 12 and 13. If it is not 0x8100, return it as type and set offset=14.
- If it is 0x8100, require n>=18, read big-endian inner EtherType at bytes 16 and 17, and set offset=18.
- On success write both outputs and return 0; support exactly zero or one VLAN tag.
