-
Notifications
You must be signed in to change notification settings - Fork 444
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
Add support for Random extern to PSA/eBPF backend #3251
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but this whole sequence could be a utility function; you do this a lot in this backend.