Improving MRI tutorial code

Hello,

I was trying to modify the MRI tutorial in P4tutorials to dock more headers and INT information, but after compiling and modifying send.py and receive.py as well, the receive.py couldn`t sniff nor show the packet with the new INT headers.

To exemplify:

I tried to include the packet_length metada in the switch trace header. I included the typedef in the mri.p4 code:

typedef bit<9> egressSpec_t;
typedef bit<48> macAddr_t;
typedef bit<32> ip4Addr_t;
typedef bit<32> switchID_t;
typedef bit<32> qdepth_t;
typedef bit<32> packet_length_t;

header switch_t {
switchID_t swid;
qdepth_t qdepth;
packet_length_t packet_length;
}

After that, on ***EGRESS PROCESSING **** I included the following code line:

hdr.swtraces[0].packet_length = (packet_length)standard_metadata.deq_qdepth;

Finally I modify the class SwitchTrace in receive.py and send.py

class SwitchTrace(Packet):
fields_desc = [ IntField(“swid”, 0),
IntField(“qdepth”, 0),
IntField(“packet_length”, 0)]
def extract_padding(self, p):
return “”, p

After I run make in mri directory, I run mininet and xterm h1 and h2. I run send.py in h1 and receive.py in h2. Unfortunately, h2 don`t sniff any package at all. Why this is happening?

It` seems I need to change this values:

    hdr.ipv4.ihl = hdr.ipv4.ihl + 2;
    hdr.ipv4_option.optionLength = hdr.ipv4_option.optionLength + 8;
    hdr.ipv4.totalLen = hdr.ipv4.totalLen + 8;

how do I recalculate these lengths when adding packet_lenght?

Regarding. your question “how do I recalculate these lengths when adding packet_lenght?”

I am not sure I understand your question.

P4 has no features or knowledge of particular header formats built into it. There is no auto-adjusting of packet lengths when you modify the packet in the BMv2 software switch.

All modifications to packet contents are done with assignment statements like the ones you show in your message, or adding headers by calling hdr.foo.setValid() and later in the deparser emit’ing that header, or removing headers by calling hdr.foo.setInvalid().