Skip to content
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 Counter extern to PSA/eBPF backend #3165

Merged
merged 2 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion backends/ebpf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ set (P4C_EBPF_SRCS
psa/ebpfPipeline.cpp
psa/ebpfPsaParser.cpp
psa/ebpfPsaDeparser.cpp
psa/ebpfPsaControl.cpp
psa/ebpfPsaTable.cpp
psa/backend.cpp)
psa/backend.cpp
psa/externs/ebpfPsaCounter.cpp)

set (P4C_EBPF_HDRS
codeGen.h
Expand Down Expand Up @@ -70,6 +72,7 @@ set (P4C_EBPF_HDRS
psa/ebpfPsaDeparser.h
psa/ebpfPsaControl.h
psa/ebpfPsaTable.h
psa/externs/ebpfPsaCounter.h
)

add_cpplint_files(${CMAKE_CURRENT_SOURCE_DIR} "${P4C_EBPF_SRCS};${P4C_EBPF_HDRS}")
Expand Down
40 changes: 40 additions & 0 deletions backends/ebpf/psa/ebpfPsaControl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
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 "ebpfPsaControl.h"

namespace EBPF {

ControlBodyTranslatorPSA::ControlBodyTranslatorPSA(const EBPFControlPSA* control) :
CodeGenInspector(control->program->refMap, control->program->typeMap),
ControlBodyTranslator(control) {}

void ControlBodyTranslatorPSA::processMethod(const P4::ExternMethod* method) {
auto decl = method->object;
auto declType = method->originalExternType;
cstring name = EBPFObject::externalName(decl);

if (declType->name.name == "Counter") {
auto counterMap = control->getCounter(name);
counterMap->to<EBPFCounterPSA>()->emitMethodInvocation(builder, method, this);
return;
}

ControlBodyTranslator::processMethod(method);
}

}
18 changes: 18 additions & 0 deletions backends/ebpf/psa/ebpfPsaControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ namespace EBPF {

class EBPFControlPSA;

class ControlBodyTranslatorPSA : public ControlBodyTranslator {
public:
explicit ControlBodyTranslatorPSA(const EBPFControlPSA* control);

void processMethod(const P4::ExternMethod* method) override;
};

class ActionTranslationVisitorPSA : public ActionTranslationVisitor,
public ControlBodyTranslatorPSA {
public:
ActionTranslationVisitorPSA(const EBPFProgram* program, cstring valueName);

bool preorder(const IR::PathExpression* pe) override;
bool isActionParameter(const IR::Expression *expression) const;

void processMethod(const P4::ExternMethod* method) override;
};

class EBPFControlPSA : public EBPFControl {
public:
// Keeps track if ingress_timestamp or egress_timestamp is used within a control block.
Expand Down
21 changes: 20 additions & 1 deletion backends/ebpf/psa/ebpfPsaGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ limitations under the License.
#include "ebpfPsaTable.h"
#include "ebpfPsaControl.h"
#include "xdpHelpProgram.h"
#include "externs/ebpfPsaCounter.h"

namespace EBPF {

Expand Down Expand Up @@ -520,7 +521,7 @@ bool ConvertToEBPFControlPSA::preorder(const IR::ControlBlock *ctrl) {
control->inputStandardMetadata = *it; ++it;
control->outputStandardMetadata = *it;

auto codegen = new ControlBodyTranslator(control);
auto codegen = new ControlBodyTranslatorPSA(control);
codegen->substitute(control->headers, parserHeaders);

if (type != TC_EGRESS) {
Expand Down Expand Up @@ -608,6 +609,24 @@ bool ConvertToEBPFControlPSA::preorder(const IR::Declaration_Variable* decl) {
return true;
}

bool ConvertToEBPFControlPSA::preorder(const IR::ExternBlock* instance) {
auto di = instance->node->to<IR::Declaration_Instance>();
if (di == nullptr)
return false;
cstring name = EBPFObject::externalName(di);
cstring typeName = instance->type->getName().name;

if (typeName == "Counter") {
auto ctr = new EBPFCounterPSA(program, di, name, control->codeGen);
control->counters.emplace(name, ctr);
} else {
::error(ErrorType::ERR_UNEXPECTED, "Unexpected block %s nested within control",
instance);
}

return false;
}

// =====================EBPFDeparser=============================
bool ConvertToEBPFDeparserPSA::preorder(const IR::ControlBlock *ctrl) {
if (type == TC_INGRESS) {
Expand Down
1 change: 1 addition & 0 deletions backends/ebpf/psa/ebpfPsaGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class ConvertToEBPFControlPSA : public Inspector {
bool preorder(const IR::Declaration_Variable*) override;
bool preorder(const IR::Member *m) override;
bool preorder(const IR::IfStatement *a) override;
bool preorder(const IR::ExternBlock* instance) override;

EBPF::EBPFControlPSA *getEBPFControl() { return control; }
};
Expand Down
51 changes: 51 additions & 0 deletions backends/ebpf/psa/ebpfPsaTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,52 @@ limitations under the License.

namespace EBPF {

// =====================ActionTranslationVisitorPSA=============================
ActionTranslationVisitorPSA::ActionTranslationVisitorPSA(const EBPFProgram* program,
cstring valueName) :
CodeGenInspector(program->refMap, program->typeMap),
ActionTranslationVisitor(valueName, program),
ControlBodyTranslatorPSA(program->to<EBPFPipeline>()->control) {}

bool ActionTranslationVisitorPSA::preorder(const IR::PathExpression* pe) {
if (isActionParameter(pe)) {
return ActionTranslationVisitor::preorder(pe);
}
return ControlBodyTranslator::preorder(pe);
}

bool ActionTranslationVisitorPSA::isActionParameter(const IR::Expression *expression) const {
if (auto path = expression->to<IR::PathExpression>())
return ActionTranslationVisitor::isActionParameter(path);
else if (auto cast = expression->to<IR::Cast>())
return isActionParameter(cast->expr);
else
return false;
}

void ActionTranslationVisitorPSA::processMethod(const P4::ExternMethod* method) {
auto declType = method->originalExternType;
auto decl = method->object;
BUG_CHECK(decl->is<IR::Declaration_Instance>(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it useful to print something about the IR node that causes the failure, otherwise you make debugging a bit harder. But I understand that probably this never failed so far.
... declared: %1%", decl);

"Extern has not been declared");
auto di = decl->to<IR::Declaration_Instance>();
auto instanceName = EBPFObject::externalName(di);

if (declType->name.name == "Counter") {
auto ctr = control->to<EBPFControlPSA>()->getCounter(instanceName);
// Counter count() always has one argument/index
if (ctr != nullptr) {
ctr->to<EBPFCounterPSA>()->emitMethodInvocation(builder, method, this);
} else {
::error(ErrorType::ERR_NOT_FOUND,
"%1%: Counter named %2% not found",
method->expr, instanceName);
}
} else {
ControlBodyTranslatorPSA::processMethod(method);
}
}

// =====================EBPFTablePSA=============================
EBPFTablePSA::EBPFTablePSA(const EBPFProgram* program, const IR::TableBlock* table,
CodeGenInspector* codeGen) :
Expand All @@ -42,6 +88,11 @@ EBPFTablePSA::EBPFTablePSA(const EBPFProgram* program, const IR::TableBlock* tab
}
}

ActionTranslationVisitor* EBPFTablePSA::createActionTranslationVisitor(
cstring valueName, const EBPFProgram* program) const {
return new ActionTranslationVisitorPSA(program->to<EBPFPipeline>(), valueName);
}

void EBPFTablePSA::emitValueStructStructure(CodeBuilder* builder) {
// TODO: placeholder for handling psa_implementation
EBPFTable::emitValueStructStructure(builder);
Expand Down
5 changes: 4 additions & 1 deletion backends/ebpf/psa/ebpfPsaTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ limitations under the License.

#include "frontends/p4/methodInstance.h"
#include "backends/ebpf/ebpfTable.h"
#include "ebpfPsaControl.h"
#include "backends/ebpf/psa/externs/ebpfPsaCounter.h"

namespace EBPF {

Expand All @@ -33,6 +33,9 @@ class EBPFTablePSA : public EBPFTable {
size_t size) const;

protected:
ActionTranslationVisitor* createActionTranslationVisitor(
cstring valueName, const EBPFProgram* program) const override;

void emitTableValue(CodeBuilder* builder, const IR::MethodCallExpression* actionMce,
cstring valueName);
void emitDefaultActionInitializer(CodeBuilder *builder);
Expand Down
Loading