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

backends/ebpf: Fix #4098 by renaming conflicting write_partial macro. #4134

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion backends/ebpf/ebpfControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void ControlBodyTranslator::compileEmitField(const IR::Expression *expr, cstring
builder->appendFormat("write_byte(%s, BYTES(%s) + %d, (%s) << %d)",
program->packetStartVar.c_str(), program->offsetVar.c_str(), i,
program->byteVar.c_str(), 8 - bitsToWrite);
else
else // FIXME change to use write_partial_ex
builder->appendFormat("write_partial(%s + BYTES(%s) + %d, %d, (%s) << %d)",
program->packetStartVar.c_str(), program->offsetVar.c_str(), i,
alignment, program->byteVar.c_str(), 8 - bitsToWrite);
Expand Down
4 changes: 2 additions & 2 deletions backends/ebpf/ebpfDeparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ void DeparserHdrEmitTranslator::emitField(CodeBuilder *builder, cstring field,
program->byteVar.c_str());
} else { // write partial
shift = (8 - alignment - bitsToWrite);
builder->appendFormat("write_partial(%s + BYTES(%s) + %d, %d, %d, (%s >> %d))",
builder->appendFormat("write_partial_ex(%s + BYTES(%s) + %d, %d, %d, (%s >> %d))",
program->packetStartVar.c_str(), program->offsetVar.c_str(),
i, // do not reverse byte order
bitsToWrite, shift, program->byteVar.c_str(),
Expand All @@ -266,7 +266,7 @@ void DeparserHdrEmitTranslator::emitField(CodeBuilder *builder, cstring field,
i, // do not reverse byte order
program->byteVar.c_str(), 8 - alignment % 8);
} else {
builder->appendFormat("write_partial(%s + BYTES(%s) + %d + 1, %d, %d, (%s))",
builder->appendFormat("write_partial_ex(%s + BYTES(%s) + %d + 1, %d, %d, (%s))",
program->packetStartVar.c_str(), program->offsetVar.c_str(),
i, // do not reverse byte order
bitsToWrite, 8 + alignment - bitsToWrite,
Expand Down
29 changes: 19 additions & 10 deletions backends/ebpf/ebpfProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,7 @@ class ErrorCodesVisitor : public Inspector {
};
} // namespace

void EBPFProgram::emitPreamble(CodeBuilder *builder) {
builder->emitIndent();
builder->appendFormat("enum %s ", errorEnum.c_str());
builder->blockStart();

ErrorCodesVisitor visitor(builder);
program->apply(visitor);

builder->blockEnd(false);
builder->endOfStatement(true);
void EBPFProgram::emitCommonPreamble(CodeBuilder *builder) {
builder->newline();
builder->appendLine("#define EBPF_MASK(t, w) ((((t)(1)) << (w)) - (t)1)");
builder->appendLine("#define BYTES(w) ((w) / 8)");
Expand All @@ -198,10 +189,28 @@ void EBPFProgram::emitPreamble(CodeBuilder *builder) {
"{ u8 mask = EBPF_MASK(u8, s); "
"*((u8*)a) = ((*((u8*)a)) & ~mask) | (((v) >> (8 - (s))) & mask); "
"} while (0)");
builder->appendLine(
"#define write_partial_ex(a, w, s, v) do { *((u8*)a) = ((*((u8*)a)) "
"& ~(EBPF_MASK(u8, w) << s)) | (v << s) ; } while (0)");
tatry marked this conversation as resolved.
Show resolved Hide resolved
builder->appendLine(
"#define write_byte(base, offset, v) do { "
"*(u8*)((base) + (offset)) = (v); "
"} while (0)");
}

void EBPFProgram::emitPreamble(CodeBuilder *builder) {
builder->emitIndent();
builder->appendFormat("enum %s ", errorEnum.c_str());
builder->blockStart();

ErrorCodesVisitor visitor(builder);
program->apply(visitor);

builder->blockEnd(false);
builder->endOfStatement(true);

emitCommonPreamble(builder);

builder->newline();
builder->appendLine("void* memcpy(void* dest, const void* src, size_t num);");
builder->newline();
Expand Down
1 change: 1 addition & 0 deletions backends/ebpf/ebpfProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class EBPFProgram : public EBPFObject {
virtual void emitPipeline(CodeBuilder *builder);

public:
virtual void emitCommonPreamble(CodeBuilder *builder);
virtual void emitGeneratedComment(CodeBuilder *builder);
virtual void emitH(CodeBuilder *builder, cstring headerFile); // emits C headers
virtual void emitC(CodeBuilder *builder, cstring headerFile); // emits C program
Expand Down
11 changes: 1 addition & 10 deletions backends/ebpf/psa/ebpfPsaGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,7 @@ void PSAEbpfGenerator::emitPreamble(CodeBuilder *builder) const {
}

void PSAEbpfGenerator::emitCommonPreamble(CodeBuilder *builder) const {
builder->newline();
builder->appendLine("#define EBPF_MASK(t, w) ((((t)(1)) << (w)) - (t)1)");
builder->appendLine("#define BYTES(w) ((w) / 8)");
builder->appendLine(
"#define write_partial(a, w, s, v) do { *((u8*)a) = ((*((u8*)a)) "
"& ~(EBPF_MASK(u8, w) << s)) | (v << s) ; } while (0)");
builder->appendLine(
"#define write_byte(base, offset, v) do { "
"*(u8*)((base) + (offset)) = (v); "
"} while (0)");
ingress->emitCommonPreamble(builder); // from ebpfProgram
builder->target->emitPreamble(builder);
}

Expand Down