How to work with packet payload

Hello, I have a question regarding doing some action on packet payload like different CRC calculations over payload, for example, one CRC calculation would be done on every packet and another algorithm would run on each pair of packets (calculating CRC over two packets’ payload).

Hi @arashams

Welcome to the forum :slight_smile:

In general terms, P4 targets are not designed to work with the payload (application layer data). There are a few reasons that I come up with like the amount of resources necessary to do that or the variable size headers. I can imagine that many of the tasks involving packet payload processing take a long time, and I believe that the primary idea of P4 targets is to process packets as fast as possible. This is a task that, for instance, DPI devices would be in charge of. I still don’t think this is a task totally out of the context in P4 (because you have papers taking a look into the application layer), but definitely not the main focus at all.

Now, software targets (like bmv2) will always be much more flexible than hardware targets (an ASIC). For bmv2, you can create your own externs if you do not find the ones in v1model to be satisfactory. you can check some of the HashAlgorithm s:

enum HashAlgorithm {
    crc32,
    crc32_custom,
    crc16,
    crc16_custom,
    random,
    identity,
    csum16,
    xor16
}

I would recommend that you build a use case that extracts always the same size of payload, you will then overcome the use about variable size headers. I am not very sure that variable headers can be used for any other thing than parsing/deparsing. Maybe things have changed, so you might try anyway.

Then, use the stateful register to store the current packet’s (p) payload (pl) CRC (crc(pl)) in a register (r[ 0 ] and make your calculations with the CRC of packet p+1 payload pl+1, that is, crc(pl+1). Then store crc(pl+1) in same register position as crc(pl) in r[ 0 ] was saved at. Repeat for every packet.

PD: Of course, try to test if the existing hash functions accept your (maybe large) headers or else you need a different approach.

Hope it helps,