Skip to content

Commit

Permalink
Add library functions instead of checking for file names.
Browse files Browse the repository at this point in the history
  • Loading branch information
fruffy committed Apr 30, 2024
1 parent 54d0981 commit 4986811
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
21 changes: 16 additions & 5 deletions backends/ebpf/ebpfProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,13 @@ void EBPFProgram::emitTypes(CodeBuilder *builder) {
builder->newline();
}
if (const auto *method = d->to<IR::Method>()) {
if (method->srcInfo.isValid()) {
if (!method->srcInfo.isValid()) {
continue;
}
// Ignore methods originating from core.p4 and ubpf_model.p4 because they are already
// defined.
// TODO: Is there a more portable way to do this? Currently we check for a specific
// filename as the source of a method.
auto sourceName = std::filesystem::path(method->srcInfo.getSourceFile().c_str());
if (sourceName.filename() == "core.p4" || sourceName.filename() == "ubpf_model.p4") {
// TODO: Maybe we should still generate declarations for these methods?
if (isLibraryMethod(method->controlPlaneName())) {
continue;
}
EBPFMethodDeclaration methodInstance(method);
Expand Down Expand Up @@ -348,4 +346,17 @@ void EBPFProgram::emitPipeline(CodeBuilder *builder) {
}
}

bool EBPFProgram::isLibraryMethod(cstring methodName) {
static std::set<cstring> DEFAULT_METHODS = {"static_assert", "verify"};
if (DEFAULT_METHODS.find(methodName) != DEFAULT_METHODS.end() && options.target != "xdp") {
return true;
}

static std::set<cstring> XDP_METHODS = {
"ebpf_ipv4_checksum", "csum_replace2", "csum_replace4",
"BPF_PERF_EVENT_OUTPUT", "BPF_KTIME_GET_NS",
};
return XDP_METHODS.find(methodName) != XDP_METHODS.end();
}

} // namespace EBPF
5 changes: 5 additions & 0 deletions backends/ebpf/ebpfProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ class EBPFProgram : public EBPFObject {
virtual void emitLocalVariables(CodeBuilder *builder);
virtual void emitPipeline(CodeBuilder *builder);

/// Checks whether a method name is considered to be part of the standard library, e.g., defined
/// in core.p4 or ebpf_model.p4.
/// TODO: Should we also distinguish overloaded methods?
virtual bool isLibraryMethod(cstring methodName);

public:
virtual void emitCommonPreamble(CodeBuilder *builder);
virtual void emitGeneratedComment(CodeBuilder *builder);
Expand Down
6 changes: 2 additions & 4 deletions backends/ubpf/ubpfProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,8 @@ void UBPFProgram::emitTypes(EBPF::CodeBuilder *builder) {
}
// Ignore methods originating from core.p4 and ubpf_model.p4 because they are already
// defined.
// TODO: Is there a more portable way to do this? Currently we check for a specific
// filename as the source of a method.
auto sourceName = std::filesystem::path(method->srcInfo.getSourceFile().c_str());
if (sourceName.filename() == "core.p4" || sourceName.filename() == "ubpf_model.p4") {
// TODO: Maybe we should still generate declarations for these methods?
if (isLibraryMethod(method->controlPlaneName())) {
continue;
}
EBPF::EBPFMethodDeclaration methodInstance(method);
Expand Down
9 changes: 9 additions & 0 deletions backends/ubpf/ubpfProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ class UBPFProgram : public EBPF::EBPFProgram {
void emitMetadataInstance(EBPF::CodeBuilder *builder) const;
void emitLocalVariables(EBPF::CodeBuilder *builder) override;
void emitPipeline(EBPF::CodeBuilder *builder) override;

bool isLibraryMethod(cstring methodName) override {
static std::set<cstring> DEFAULT_METHODS = {
"mark_to_drop", "mark_to_pass", "ubpf_time_get_ns", "truncate",
"hash", "csum_replace2", "csum_replace4",
};
return DEFAULT_METHODS.find(methodName) != DEFAULT_METHODS.end() ||
EBPFProgram::isLibraryMethod(methodName);
}
};

} // namespace UBPF
Expand Down

0 comments on commit 4986811

Please sign in to comment.