-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Random extern to PSA/eBPF backend (#3251)
Co-authored-by: Mateusz Kossakowski <[email protected]> Co-authored-by: Jan Palimąka <[email protected]>
- Loading branch information
1 parent
5754d6c
commit d7d6c29
Showing
9 changed files
with
310 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
Copyright 2022-present Orange | ||
Copyright 2022-present Open Networking Foundation | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
#include "ebpfPsaRandom.h" | ||
|
||
namespace EBPF { | ||
|
||
EBPFRandomPSA::EBPFRandomPSA(const IR::Declaration_Instance* di) : | ||
minValue(0), maxValue(0), range(0) { | ||
CHECK_NULL(di); | ||
|
||
// verify type | ||
if (!di->type->is<IR::Type_Specialized>()) { | ||
::error(ErrorType::ERR_MODEL, "Missing specialization: %1%", di); | ||
return; | ||
} | ||
auto ts = di->type->to<IR::Type_Specialized>(); | ||
BUG_CHECK(ts->arguments->size() == 1, "%1%, Lack of specialization argument", ts); | ||
auto type = ts->arguments->at(0); | ||
if (!type->is<IR::Type_Bits>()) { | ||
::error(ErrorType::ERR_UNSUPPORTED, "Must be bit or int type: %1%", ts); | ||
return; | ||
} | ||
if (type->width_bits() > 32) { | ||
::error(ErrorType::ERR_UNSUPPORTED, "%1%: up to 32 bits width is supported", ts); | ||
} | ||
|
||
if (di->arguments->size() != 2) { | ||
::error(ErrorType::ERR_MODEL, "Expected 2 arguments to: %1%", di); | ||
return; | ||
} | ||
|
||
unsigned tmp[2] = {0}; | ||
for (int i = 0; i < 2; ++i) { | ||
auto expr = di->arguments->at(i)->expression->to<IR::Constant>(); | ||
if (expr != nullptr) { | ||
if (expr->fitsUint()) { | ||
tmp[i] = expr->asUnsigned(); | ||
} else { | ||
::error(ErrorType::ERR_OVERLIMIT, "%1%: size too large", expr); | ||
} | ||
} else { | ||
::error(ErrorType::ERR_UNSUPPORTED, "Must be constant value: %1%", | ||
di->arguments->at(i)->expression); | ||
} | ||
} | ||
|
||
minValue = tmp[0]; | ||
maxValue = tmp[1]; | ||
range = (long) maxValue - minValue + 1; | ||
|
||
// verify constructor parameters | ||
if (minValue > maxValue) { | ||
::error(ErrorType::ERR_INVALID, "%1%: Max value lower than min value", di); | ||
} | ||
if (minValue == maxValue) { | ||
::warning(ErrorType::WARN_IGNORE, | ||
"%1%: No randomness, will always return the same value " | ||
"due to that the min value is equal to the max value", di); | ||
} | ||
} | ||
|
||
void EBPFRandomPSA::processMethod(CodeBuilder* builder, const P4::ExternMethod* method) const { | ||
if (method->method->type->name == "read") { | ||
emitRead(builder); | ||
} else { | ||
::error(ErrorType::ERR_UNSUPPORTED, "%1%: Method not implemented yet", method->expr); | ||
} | ||
} | ||
|
||
void EBPFRandomPSA::emitRead(CodeBuilder* builder) const { | ||
if (minValue == maxValue || range == 0) { | ||
builder->append(minValue); | ||
return; | ||
} | ||
|
||
bool rangeIsPowerOf2 = (range & (range - 1)) == 0; | ||
|
||
if (minValue != 0) | ||
builder->appendFormat("(%uu + ", minValue); | ||
|
||
builder->append("(bpf_get_prandom_u32() "); | ||
if (rangeIsPowerOf2) { | ||
builder->appendFormat("& 0x%llxu", range - 1); | ||
} else { | ||
builder->appendFormat("%% %lluu", range); | ||
} | ||
builder->append(")"); | ||
|
||
if (minValue != 0) | ||
builder->append(")"); | ||
} | ||
|
||
} // namespace EBPF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
Copyright 2022-present Orange | ||
Copyright 2022-present Open Networking Foundation | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
#ifndef BACKENDS_EBPF_PSA_EXTERNS_EBPFPSARANDOM_H_ | ||
#define BACKENDS_EBPF_PSA_EXTERNS_EBPFPSARANDOM_H_ | ||
|
||
#include "frontends/p4/methodInstance.h" | ||
#include "backends/ebpf/ebpfObject.h" | ||
|
||
namespace EBPF { | ||
|
||
class EBPFRandomPSA : public EBPFObject { | ||
unsigned int minValue, maxValue; | ||
long range; | ||
public: | ||
explicit EBPFRandomPSA(const IR::Declaration_Instance* di); | ||
|
||
void processMethod(CodeBuilder* builder, const P4::ExternMethod* method) const; | ||
void emitRead(CodeBuilder* builder) const; | ||
}; | ||
|
||
} // namespace EBPF | ||
|
||
#endif // BACKENDS_EBPF_PSA_EXTERNS_EBPFPSARANDOM_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
Copyright 2022-present Orange | ||
Copyright 2022-present Open Networking Foundation | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
#include <core.p4> | ||
#include <psa.p4> | ||
#include "common_headers.p4" | ||
|
||
header random_t { | ||
bit<32> f1; | ||
bit<16> f2; | ||
bit<16> f3; | ||
} | ||
|
||
struct headers { | ||
ethernet_t eth; | ||
random_t rand; | ||
} | ||
|
||
parser MyIP(packet_in buffer, out headers hdr, inout empty_t bp, | ||
in psa_ingress_parser_input_metadata_t c, in empty_t d, in empty_t e) { | ||
state start { | ||
buffer.extract(hdr.eth); | ||
buffer.extract(hdr.rand); | ||
transition accept; | ||
} | ||
} | ||
|
||
parser MyEP(packet_in buffer, out empty_t a, inout empty_t b, | ||
in psa_egress_parser_input_metadata_t c, in empty_t d, in empty_t e, in empty_t f) { | ||
state start { | ||
transition accept; | ||
} | ||
} | ||
|
||
control MyIC(inout headers a, inout empty_t bc, | ||
in psa_ingress_input_metadata_t istd, inout psa_ingress_output_metadata_t ostd) { | ||
Random(16w0, 16w127) r1; | ||
Random<bit<32>>(0x80_00_00_01, 0x80_00_00_05) r2; | ||
Random(16w256, 16w259) r3; | ||
|
||
action do_forward(PortId_t egress_port) { | ||
a.rand.f3 = r3.read(); | ||
send_to_port(ostd, egress_port); | ||
} | ||
|
||
table tbl_fwd { | ||
key = { | ||
istd.ingress_port : exact; | ||
} | ||
actions = { do_forward; NoAction; } | ||
default_action = do_forward((PortId_t) 5); | ||
size = 100; | ||
} | ||
|
||
apply { | ||
a.rand.f1 = r2.read(); | ||
a.rand.f2 = r1.read(); | ||
tbl_fwd.apply(); | ||
} | ||
} | ||
|
||
control MyEC(inout empty_t a, inout empty_t b, | ||
in psa_egress_input_metadata_t c, inout psa_egress_output_metadata_t d) { | ||
apply { } | ||
} | ||
|
||
control MyID(packet_out buffer, out empty_t a, out empty_t b, out empty_t c, | ||
inout headers d, in empty_t e, in psa_ingress_output_metadata_t f) { | ||
apply { | ||
buffer.emit(d.eth); | ||
buffer.emit(d.rand); | ||
} | ||
} | ||
|
||
control MyED(packet_out buffer, out empty_t a, out empty_t b, inout empty_t c, in empty_t d, | ||
in psa_egress_output_metadata_t e, in psa_egress_deparser_input_metadata_t f) { | ||
apply { } | ||
} | ||
|
||
IngressPipeline(MyIP(), MyIC(), MyID()) ip; | ||
EgressPipeline(MyEP(), MyEC(), MyED()) ep; | ||
|
||
PSA_Switch(ip, PacketReplicationEngine(), ep, BufferingQueueingEngine()) main; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters