Skip to content

Commit

Permalink
backends/ebpf: Output an error if a header is not byte-aligned (p4lan…
Browse files Browse the repository at this point in the history
…g#4176).

Although P4 allows headers to be an arbitrary number of bits, the
eBPF backend does not support it properly. Specifically, the code
in compileExtractField() assumes that the header starts at a byte
boundary. In any case, there is no realistic use case for headers
which aren't a whole number of bytes. Output an error rather than
fail silently. The BMv2 backend has a similar limitation already.
  • Loading branch information
thomascalvert-xlnx committed Oct 9, 2023
1 parent 43a00bf commit 115dd15
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
7 changes: 7 additions & 0 deletions backends/ebpf/ebpfParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,14 @@ void StateTranslationVisitor::compileExtract(const IR::Expression *destination)
return;
}

// We expect all headers to start on a byte boundary.
unsigned width = ht->width_bits();
if ((width % 8) != 0) {
::error(ErrorType::ERR_UNSUPPORTED_ON_TARGET,
"Header %1% size %2% is not a multiple of 8 bits.", destination, width);
return;
}

auto program = state->parser->program;

cstring offsetStr =
Expand Down
28 changes: 28 additions & 0 deletions testdata/p4_16_ebpf_errors/header_unaligned.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <ebpf_model.p4>
#include <core.p4>

header ThreeBit_h {
bit<3> x;
}

struct Headers_t {
ThreeBit_h foo;
ThreeBit_h bar;
}

parser prs(packet_in p, out Headers_t headers) {
state start {
p.extract(headers.foo);
p.extract(headers.bar);
transition accept;
}
}

control pipe(inout Headers_t headers, out bool pass) {

apply {
pass = true;
}
}

ebpfFilter(prs(), pipe()) main;
Empty file.
6 changes: 6 additions & 0 deletions testdata/p4_16_ebpf_errors_outputs/header_unaligned.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
header_unaligned.p4(15): [--Werror=target-error] error: Header headers.foo size 3 is not a multiple of 8 bits.
p.extract(headers.foo);
^^^^^^^^^^^
header_unaligned.p4(16): [--Werror=target-error] error: Header headers.bar size 3 is not a multiple of 8 bits.
p.extract(headers.bar);
^^^^^^^^^^^

0 comments on commit 115dd15

Please sign in to comment.