p4lang / p4c

P4_16 reference compiler
https://p4.org/
Apache License 2.0
665 stars 441 forks source link

Generated JSON file with variable length fields violates assertion in simple_switch #746

Closed 4tXJ7f closed 7 years ago

4tXJ7f commented 7 years ago

P4 programs with variable length fields violate an assertion when compiled with p4c and run on simple_switch:

simple_switch: parser.cpp:278: virtual void bm::ParserOpExtractVL::operator()(bm::Packet*, const char*, size_t*) const: Assertion `hdr.is_VL_header()' failed.
Aborted (core dumped)

This might be due to a bug in p4c: for variable length fields, it generates JSON with concrete lengths, e.g.:

   {
      "name" : "ipv4_t_2",
      "id" : 2,
      "fields" : [
        ["vlf", 504]
      ]
    },

But the documentation of the BMv2 JSON input format states that:

For variable length fields, the field width is "*".

The assertion failure can be a reproduced as follows:

  1. Compile the following P4 program:
    
    $ cat vl_field_test.p4 
    header_type ipv4_t {
    fields {
    ihl : 8;
    vlf : *; // Variable length field
    }
    length : ihl * 8;
    max_length: 64;
    }

header ipv4_t ipv4;

parser start { extract(ipv4); return ingress; }

control ingress { }

control egress { }

$ p4c-bm2-ss --p4v 14 -o vl_field_test.json vl_field_test.p4


2. Run the program with `simple_switch`:

$ sudo simple_switch --log-console -i 0@veth2 -i 1@veth4 vl_field_test.json


3. Send a simple test packet:

$ tcpdump -XX -r test.pcap reading from file test.pcap, link-type EN10MB (Ethernet) 14:58:46.579815 00:00:00:00:00:00 (oui Ethernet) > 04:00:00:00:00:00 (oui Unknown) Null Information, send seq 0, rcv seq 0, Flags [Command], length 490 0x0000: 0400 0000 0000 0000 0000 0000 0000 0000 ................ 0x0010: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x0020: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x0030: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x0040: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x0050: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x0060: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x0070: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x0080: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x0090: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x00a0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x00b0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x00c0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x00d0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x00e0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x00f0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x0100: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x0110: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x0120: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x0130: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x0140: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x0150: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x0160: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x0170: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x0180: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x0190: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x01a0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x01b0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x01c0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x01d0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x01e0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 0x01f0: 0000 0000 0000 0000 ........ $ sudo scapy

sendp(rdpcap('test.pcap'), iface='veth2') . Sent 1 packets.

Sending the packet will make simple_switch crash with the assertion failure mentioned in the beginning.

I'd be happy to help fixing the issue but I am not quite sure who's at fault here (I am suspecting p4c because of the JSON documentation) and whether this is expected or not.

Thanks!

antoninbas commented 7 years ago

Hi Andres, We were not able to reproduce this issue, the JSON file we generate with the latest compiler version uses "*" as expected. Are you using an old version of p4c? I think the handling of variable-length fields was fixed recently (within the last month or so).

4tXJ7f commented 7 years ago

Hmm, it looks like it works with the newest version. I thought that I had updated to the newest version before reporting the issue, sorry about that and thanks a lot for the reply.