[P4-dev] P4 Slice and EBPF backend

Mihai Budiu mbudiu at vmware.com
Tue Aug 29 20:33:21 EDT 2017

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.


From: P4-dev [mailto:p4-dev-bounces at lists.p4.org] On Behalf Of hemant at mnkcg.com
Sent: Saturday, August 26, 2017 4:46 PM
To: 'p4-dev lists.p4.org' <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";
    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.



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

More information about the P4-dev mailing list