[P4-dev] P4 Slice and EBPF backend

Mihai Budiu mbudiu at vmware.com
Wed Aug 30 12:07:29 EDT 2017


apply is the core method by which a visitor is applied to a rooted IR DAG.

newNode = node->apply(visitor)

runs a visitor on the IR DAG starting at 'node' and produces an new IR DAG whose root is 'newNode'

In this example codeGen is an Inspector, which does not modify the IR tree, but produces as a side-effect a textual representation of the program in C.

I will submit a PR which adds support for slices for some narrow data-types.

Mihai

From: hemant at mnkcg.com [mailto:hemant at mnkcg.com]
Sent: Wednesday, August 30, 2017 2:03 AM
To: Mihai Budiu <mbudiu at vmware.com>; 'p4-dev lists.p4.org' <p4-dev at lists.p4.org>
Subject: RE: [P4-dev] P4 Slice and EBPF backend

I understand C does not support the P4 Slice.  In order to debug specific backend ebpf code below, I had used toString.  Also one's goal may not be to output C.  As I said, I was merely using the ebpf backend as a simple backend to test the P4 Slice.  In ebpfControl.cpp, we have  "void EBPFControl::emit(CodeBuilder* builder)".

In this function there is this line of code:

controlBlock->container->body->apply(*codeGen);


The apply() refers to the Node and I wanted to see what is printed when the above code is used and saw interesting output.  I think we can Close the issue because my goal is not to port Slice from bmv2 to ebpf.  It's what's going on with the Node::apply() that I was more interested in.

Thanks,

Hemant


From: Mihai Budiu [mailto:mbudiu at vmware.com]
Sent: Tuesday, August 29, 2017 8:33 PM
To: hemant at mnkcg.com<mailto:hemant at mnkcg.com>; 'p4-dev lists.p4.org' <p4-dev at lists.p4.org<mailto:p4-dev at lists.p4.org>>
Subject: RE: [P4-dev] P4 Slice and EBPF backend

Currently the ebpf back-end has no support for the slice operator.
Using toString will not solve the problem, because C does not have a slice operator; one need to convert slices into shifts and masks.
Since you have filed an issue with the compiler we will treat that as a bug report requesting this feature to be implemented.

To handle slices on the left-hand side one could probably steal the corresponding pass from the bmv2 back-end.

Please note that none of the arithmetic operators in the ebpf back-end work on values wider than 64 bits, so probably even the slice operator will share this restriction in a first implementation.

Mihai

From: P4-dev [mailto:p4-dev-bounces at lists.p4.org] On Behalf Of hemant at mnkcg.com<mailto:hemant at mnkcg.com>
Sent: Saturday, August 26, 2017 4:46 PM
To: 'p4-dev lists.p4.org' <p4-dev at lists.p4.org<mailto:p4-dev at lists.p4.org>>
Subject: [P4-dev] P4 Slice and EBPF backend

I am using a modified EBPF backend to shoehorn P4 Slice code into this backend.  I have modified this file: "p4c/testdata/p4_16_samples/switch_ebpf.p4" as follows:

Added new struct below.

struct Token
{
    bit<64>    data;
};

Added the new struct as member of Headers_t

struct Headers_t
{
    Ethernet_h ethernet;
    IPv4_h     ipv4;
    Token      token;
}

Added one line to the control below for use of token.data.

control pipe(inout Headers_t headers, out bool pass)
{
    action Reject(IPv4Address add)
    {
        pass = false;
        headers.ipv4.srcAddr = add;
        headers.token.data[63:32] = add;
    }
...

I have modified p4c/backends/ebpf/codeGen.h and .cpp.  The .cpp has added this function and thus the modification to the .h file is obvious.

bool CodeGenInspector::preorder(const IR::Slice* expression) {
    int h = expression->getH();
    int l = expression->getL();

    std::cout << expression->toString() << h << " " << l  << "\n";
    builder->append(expression->toString());
    return true;
}

When I use p4c-ebpf on switch_ebpf.p4, I see the printed output from code in the preorder Slice and the preorder AssigmentStatement functions showing correct text for this line of P4 code:

headers.token.data[63:32] = add;

However, the output file from p4c-ebpf adds extra text after the sliced data.  The text is shown in highlight below.

headers.token.data[63:32]headers.token.data6332 = value->u.Reject.add;

I am missing something related to adding support for P4 Slice in this backend.  I know the bmv2 supports Slice but I wanted to experiment with the LHS and RHS of a Slice statement in the backend.  Since EBPF is a simpler backend, I have used it with my modifications.  I see EBPF backend uses an Inspector for a visitor.  I think, to work with a Slice, we would need a Modifier visitor so that one could modify the LHS or RHS of the Slice statement with a text of one's choice.  I don't want to use multiple inheritance for codeGen with Inspector and Modifier.

Thanks,

Hemant

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.p4.org/pipermail/p4-dev_lists.p4.org/attachments/20170830/578a40da/attachment-0002.html>


More information about the P4-dev mailing list