Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting an error while trying to implement a simple P4 PNA program. #4985

Open
rupesh-chiluka-marvell opened this issue Oct 28, 2024 · 19 comments
Labels
bug This behavior is unintended and should be fixed. pna-bmv2 Topics related to the BMv2 PNA back end.

Comments

@rupesh-chiluka-marvell
Copy link
Contributor

Program:

#include <core.p4>
#include <bmv2/pna.p4>

typedef bit<48>  EthernetAddress;
// const PortId_t dest_port = (PortId_t) 1;

header ethernet_t {
    EthernetAddress dstAddr;
    EthernetAddress srcAddr;
    bit<16>         etherType;
}

struct metadata_t {
}

struct headers_t {
    ethernet_t eth;
}

parser MainParserImpl(
    packet_in pkt,
    out   headers_t  hdr,
    inout metadata_t meta,
    in    pna_main_parser_input_metadata_t istd)
{
    state start {
        pkt.extract(hdr.eth);
        transition accept;
    }
}

control MainControlImpl(
    inout headers_t  hdr,
    inout metadata_t meta,
    in    pna_main_input_metadata_t  istd,
    inout pna_main_output_metadata_t ostd)
{
    action dummy() {
        // do nothing
    }

    table dummy_table {
        key = {}
        actions = { dummy; }
        default_action = dummy;
    }

    action send_packet() {
        // do nothing
    }

    table send_packet_table {
        key = {
            hdr.eth.dstAddr: exact;
        }
        actions = { send_packet; }
        default_action = send_packet;
    }

    apply {
        bool is_recirculated = istd.recirculated;
        dummy_table.apply();
        if (is_recirculated) {
            send_packet_table.apply();
        }
    }
}

control MainDeparserImpl(
    packet_out pkt,
    inout headers_t hdr,
    in    metadata_t meta,
    in    pna_main_output_metadata_t ostd)
{
    apply {
        pkt.emit(hdr.eth);
    }
}

PNA_NIC(
    MainParserImpl(),
    MainControlImpl(),
    MainDeparserImpl()
    ) main;

sudo gdb --args pna_nic  --interface 0@veth0 --interface 1@veth2 --interface 2@veth4 pna-sample-program.json  --l
og-console

Ouptut:

[18:07:04.786] [bmv2] [D] [thread 609629] [0.0] [cxt 0] Processing packet received on port 0
[18:07:04.786] [bmv2] [D] [thread 609629] [0.0] [cxt 0] Parser 'main_parser': start
[18:07:04.786] [bmv2] [D] [thread 609629] [0.0] [cxt 0] Parser 'main_parser' entering state 'start'
[18:07:04.786] [bmv2] [D] [thread 609629] [0.0] [cxt 0] Extracting header 'eth'
[18:07:04.786] [bmv2] [D] [thread 609629] [0.0] [cxt 0] Parser state 'start' has no switch, going to default next state
[18:07:04.786] [bmv2] [T] [thread 609629] [0.0] [cxt 0] Bytes parsed: 14
[18:07:04.786] [bmv2] [D] [thread 609629] [0.0] [cxt 0] Parser 'main_parser': end
[18:07:04.786] [bmv2] [D] [thread 609629] [0.0] [cxt 0] Pipeline 'main_control': start
[18:07:04.786] [bmv2] [T] [thread 609629] [0.0] [cxt 0] Applying table 'MainControlImpl.dummy_table'
[18:07:04.786] [bmv2] [D] [thread 609629] [0.0] [cxt 0] Looking up key:

[18:07:04.786] [bmv2] [D] [thread 609629] [0.0] [cxt 0] Table 'MainControlImpl.dummy_table': miss
[18:07:04.786] [bmv2] [D] [thread 609629] [0.0] [cxt 0] Action entry is MainControlImpl.dummy - 
[18:07:04.786] [bmv2] [T] [thread 609629] [0.0] [cxt 0] Action MainControlImpl.dummy

Thread 7 "pna_nic" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffef7fe640 (LWP 609629)]
0x00005555556633aa in std::_Bit_reference::operator bool (this=<synthetic pointer>) at /usr/include/c++/11/bits/stl_bvector.h:87
87          { return !!(*_M_p & _M_mask); }

(gdb) bt
#0  0x00005555556633aa in std::_Bit_reference::operator bool (this=<synthetic pointer>) at /usr/include/c++/11/bits/stl_bvector.h:87
p4lang/behavioral-model#1  bm::ExpressionTemps::pop_bool (this=<optimized out>) at expressions.cpp:425
p4lang/behavioral-model#2  bm::Expression::eval_bool (this=this@entry=0x5555557cf658, phv=..., locals=std::vector of length 0, capacity 0) at expressions.cpp:832
p4lang/behavioral-model#3  0x000055555564373a in bm::Conditional::eval (phv=..., this=0x5555557cf620) at ../../include/bm/bm_sim/conditionals.h:44
p4lang/behavioral-model#4  bm::Conditional::operator() (this=0x5555557cf620, pkt=0x7fffe0000e00) at conditionals.cpp:38
p4lang/behavioral-model#5  0x00005555556ba999 in bm::Pipeline::apply (this=this@entry=0x5555557dc160, pkt=0x7fffe0000e00) at pipeline.cpp:44
p4lang/behavioral-model#6  0x00005555555f1463 in bm::pna::PnaNic::main_thread (this=0x5555557ca2e0) at /usr/include/c++/11/bits/unique_ptr.h:173
p4lang/behavioral-model#7  0x00007ffff72dc253 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
p4lang/behavioral-model#8  0x00007ffff6e94ac3 in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:442
p4lang/behavioral-model#9  0x00007ffff6f26850 in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:81
@jafingerhut
Copy link
Contributor

Rupesh, is this a new PNA BMv2 test program you are working on, hoping to modify the code so that it works? If so, great!

Are you looking for help on why this error occurs, or do you already know the root cause?

@rupesh-chiluka-marvell
Copy link
Contributor Author

rupesh-chiluka-marvell commented Oct 28, 2024

Rupesh, is this a new PNA BMv2 test program you are working on, hoping to modify the code so that it works? If so, great!

Yes

Are you looking for help on why this error occurs, or do you already know the root cause?

Looking for the help

@jafingerhut
Copy link
Contributor

Is the JSON file output by the compiler something anyone should be able to reproduce from the source code and latest version of p4c source code? And the error occurs with latest behavioral-model code published? Or does the JSON file require local changes to p4c that only you have, and/or the error only occur with locally modified behavioral-model code? In either case, attaching the file pna-sample-program.json to a comment might make it easier for someone else to reproduce the issue.

@antoninbas
Copy link
Member

In either case, attaching the file pna-sample-program.json to a comment might make it easier for someone else to reproduce the issue.

Not to mention that there is a pretty strong chance that this is caused by an invalid / unsupported JSON file.

@rupesh-chiluka-marvell
Copy link
Contributor Author

Is the JSON file output by the compiler something anyone should be able to reproduce from the source code and latest version of p4c source code?

Yes

And the error occurs with latest behavioral-model code published?

Yes

Or does the JSON file require local changes to p4c that only you have, and/or the error only occur with locally modified behavioral-model code?

No local changes are made in this case

@rupesh-chiluka-marvell
Copy link
Contributor Author

JSON file:
pna_example_program.json

@antoninbas
Copy link
Member

The expression for the conditional present in the JSON is not supported:

      "conditionals" : [
        {
          "name" : "node_3",
          "id" : 0,
          "source_info" : {
            "filename" : "pna-demo-recirculation.p4",
            "line" : 96,
            "column" : 12,
            "source_fragment" : "is_recirculated"
          },
          "expression" : {
            "type" : "field",
            "value" : ["pna_main_input_metadata", "recirculated"]
          },
          "false_next" : null,
          "true_next" : "MainControlImpl.send_packet_table"
        }
      ]

The pna_main_input_metadata.recirculated field cannot be used directly as a boolean value. There are 2 possibilities: either the expression is changed to use an explicit comparison (pna_main_input_metadata.recirculated == 1) or the d2b (data-to-bool conversion) unary operator is used.

This seems to be an issue in the PNA p4c backend, as I would expect such a simple program to work well for simple_switch / v1model. You can probably write the same program for v1model, and check what the JSON looks like (and make sure it is accepted by simple_switch).

I think this issue should be transferred to p4c.

@rupesh-chiluka-marvell
Copy link
Contributor Author

rupesh-chiluka-marvell commented Oct 28, 2024

(pna_main_input_metadata.recirculated == (bool) 1) didn't work.

This seems to be an issue in the PNA p4c backend, as I would expect such a simple program to work well for simple_switch / v1model.

Yes.

I think this issue should be transferred to p4c.

Ok

@fruffy fruffy transferred this issue from p4lang/behavioral-model Oct 28, 2024
@fruffy fruffy added pna-bmv2 Topics related to the BMv2 PNA back end. bug This behavior is unintended and should be fixed. labels Oct 28, 2024
@antoninbas
Copy link
Member

(pna_main_input_metadata.recirculated == (bool) 1) didn't work.

I didn't suggest modifying the P4 program, that was very unlikely to work. JSON generation needs to be fixed.

@rupesh-chiluka-marvell
Copy link
Contributor Author

rupesh-chiluka-marvell commented Oct 28, 2024

Yes, after changing to explicit conversion in the JSON, it is working fine.

"expression" : {
  "type" : "expression",
  "value" : {
    "op" : "==",
    "left" : {
      "type" : "field",
      "value" : ["pna_main_input_metadata", "recirculated"]
    },
    "right" : {
      "type" : "hexstr",
      "value" : 1
    }
  }
},

@rupesh-chiluka-marvell
Copy link
Contributor Author

I am having the same problem with the below PSA program, and it is working after changing the JSON as per the above suggestion.

psa-example-program.p4.txt

@jafingerhut
Copy link
Contributor

The PSA back end for BMv2 is probably in a similarly unfinished state as the PNA back end for BMv2. There are many, many small simple PSA programs that either do not compile using p4c-bm2-psa, or they compile but do not execute correctly. I would recommend_not_ using the PSA back end for BMv2 as a reference for how to do things.

The v1model back end for BMv2 is by (I believe) a wide margin the most mature, featureful, and well tested open source p4c back end.

@jafingerhut
Copy link
Contributor

I guess now that the Tofino back end is open source, it might beat the v1model back end now, by those measures. I would still recommend referring to how the v1model back end does things when trying to learn reasonable ways to work with BMv2 as the target.

@rupesh-chiluka-marvell
Copy link
Contributor Author

Ok. Thanks everyone for very quick response :)

@rupesh-chiluka-marvell
Copy link
Contributor Author

Should I close this issue or leave it open till the bug is fixed?

@jafingerhut
Copy link
Contributor

I think it is fine to leave the issue open until the bug is fixed.

@fruffy
Copy link
Collaborator

fruffy commented Oct 29, 2024

@rupesh-chiluka-marvell Are you interested in implementing a fix for this in the PNA back end?

@rupesh-chiluka-marvell
Copy link
Contributor Author

Yes. I would like to 👍.

@fruffy
Copy link
Collaborator

fruffy commented Oct 29, 2024

Great! Feel free to make a PR at any time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This behavior is unintended and should be fixed. pna-bmv2 Topics related to the BMv2 PNA back end.
Projects
None yet
Development

No branches or pull requests

4 participants