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

PSA/eBPF: use slice-by-8 algorithm for CRC-32 #3688

Merged
merged 5 commits into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
99 changes: 97 additions & 2 deletions backends/ebpf/psa/ebpfPsaGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,18 @@ void PSAEbpfGenerator::emitTypes(CodeBuilder *builder) const {
egress->parser->emitTypes(builder);
egress->control->emitTableTypes(builder);
builder->newline();
emitCRC32LookupTableTypes(builder);
builder->newline();
}
void PSAEbpfGenerator::emitCRC32LookupTableTypes(CodeBuilder *builder) const {
builder->append("struct lookup_tbl_val ");
builder->blockStart();
builder->emitIndent();
builder->append("u32 table[2048]");
builder->endOfStatement(true);
builder->blockEnd(true);
builder->endOfStatement(true);
}

void PSAEbpfGenerator::emitGlobalHeadersMetadata(CodeBuilder *builder) const {
builder->append("struct hdr_md ");
builder->blockStart();
Expand Down Expand Up @@ -197,6 +207,7 @@ void PSAEbpfGenerator::emitInitializer(CodeBuilder *builder) const {
ingress->control->emitTableInitializers(builder);
egress->control->emitTableInitializers(builder);
builder->newline();
emitCRC32LookupTableInitializer(builder);
builder->emitIndent();
builder->appendLine("return 0;");
builder->blockEnd(true);
Expand Down Expand Up @@ -404,10 +415,94 @@ void PSAArchTC::emitInstances(CodeBuilder *builder) const {

emitPacketReplicationTables(builder);
emitPipelineInstances(builder);

emitCRC32LookupTableInstance(builder);
builder->appendLine("REGISTER_END()");
builder->newline();
}
void PSAArchTC::emitCRC32LookupTableInstance(CodeBuilder *builder) const {
builder->target->emitTableDecl(builder, cstring("crc_lookup_tbl"), TableArray, "u32",
cstring("struct lookup_tbl_val"), 1);
}
void PSAEbpfGenerator::emitCRC32LookupTableInitializer(CodeBuilder *builder) const {
cstring keyName = "lookup_tbl_key";
cstring valueName = "lookup_tbl_value";
cstring instanceName = "crc_lookup_tbl";

builder->emitIndent();
builder->appendFormat("u32 %s = 0", keyName.c_str());
builder->endOfStatement(true);
builder->emitIndent();
builder->appendFormat("struct lookup_tbl_val* %s = BPF_MAP_LOOKUP_ELEM(%s, &%s)",
valueName.c_str(), instanceName.c_str(), keyName.c_str());
builder->endOfStatement(true);
builder->emitIndent();
builder->appendFormat("if (%s != NULL)", valueName.c_str());
builder->blockStart();
builder->emitIndent();
builder->appendFormat("for (u16 i = 0; i <= 255; i++)");
builder->blockStart();
builder->emitIndent();
builder->appendFormat("u32 crc = i");
builder->endOfStatement(true);
builder->emitIndent();
builder->appendFormat("for (u16 j = 0; j < 8; j++)");
builder->blockStart();
builder->emitIndent();
builder->appendFormat("crc = (crc >> 1) ^ ((crc & 1) * 3988292384)");
builder->endOfStatement(true);
builder->blockEnd(true);
builder->emitIndent();
builder->appendFormat("%s->table[i] = crc", valueName.c_str());
builder->endOfStatement(true);
builder->blockEnd(true);
builder->emitIndent();
builder->appendFormat("for (u16 i = 0; i <= 255; i++)");
builder->blockStart();
builder->emitIndent();
builder->appendFormat("%s->table[256+i] = "
"(%s->table[0+i] >> 8) ^ %s->table[(%s->table[0+i] & 0xFF)]",
valueName.c_str(), valueName.c_str(), valueName.c_str(),
valueName.c_str());
builder->endOfStatement(true);
builder->emitIndent();
builder->appendFormat("%s->table[512+i] = "
"(%s->table[256+i] >> 8) ^ %s->table[(%s->table[256+i] & 0xFF)]",
valueName.c_str(), valueName.c_str(), valueName.c_str(),
valueName.c_str());
builder->endOfStatement(true);
builder->emitIndent();
builder->appendFormat("%s->table[768+i] = "
"(%s->table[512+i] >> 8) ^ %s->table[(%s->table[512+i] & 0xFF)]",
valueName.c_str(), valueName.c_str(), valueName.c_str(),
valueName.c_str());
builder->endOfStatement(true);
builder->emitIndent();
builder->appendFormat("%s->table[1024+i] = "
"(%s->table[768+i] >> 8) ^ %s->table[(%s->table[768+i] & 0xFF)]",
valueName.c_str(), valueName.c_str(), valueName.c_str(),
valueName.c_str());
builder->endOfStatement(true);
builder->emitIndent();
builder->appendFormat("%s->table[1280+i] = "
"(%s->table[1024+i] >> 8) ^ %s->table[(%s->table[1024+i] & 0xFF)]",
valueName.c_str(), valueName.c_str(), valueName.c_str(),
valueName.c_str());
builder->endOfStatement(true);
builder->emitIndent();
builder->appendFormat("%s->table[1536+i] = "
"(%s->table[1280+i] >> 8) ^ %s->table[(%s->table[1280+i] & 0xFF)]",
valueName.c_str(), valueName.c_str(), valueName.c_str(),
valueName.c_str());
builder->endOfStatement(true);
builder->emitIndent();
builder->appendFormat("%s->table[1792+i] = "
"(%s->table[1536+i] >> 8) ^ %s->table[(%s->table[1536+i] & 0xFF)]",
valueName.c_str(), valueName.c_str(), valueName.c_str(),
valueName.c_str());
builder->endOfStatement(true);
builder->blockEnd(true);
builder->blockEnd(true);
}

void PSAArchTC::emitInitializerSection(CodeBuilder *builder) const {
builder->appendLine("SEC(\"classifier/map-initializer\")");
Expand Down
4 changes: 4 additions & 0 deletions backends/ebpf/psa/ebpfPsaGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class PSAEbpfGenerator {
void emitInitializer(CodeBuilder *builder) const;
virtual void emitInitializerSection(CodeBuilder *builder) const = 0;
void emitHelperFunctions(CodeBuilder *builder) const;
void emitCRC32LookupTableTypes(CodeBuilder *builder) const;
void emitCRC32LookupTableInitializer(CodeBuilder *builder) const;
};

class PSAArchTC : public PSAEbpfGenerator {
Expand All @@ -76,6 +78,8 @@ class PSAArchTC : public PSAEbpfGenerator {

void emitInstances(CodeBuilder *builder) const override;
void emitInitializerSection(CodeBuilder *builder) const override;
void emitCRC32LookupTableInstance(CodeBuilder *builder) const;

};

class ConvertToEbpfPSA : public Transform {
Expand Down
100 changes: 83 additions & 17 deletions backends/ebpf/psa/externs/ebpfPsaHashAlgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,90 @@ void EBPFHashAlgorithmPSA::emitAddData(CodeBuilder *builder, int dataPos,

void CRCChecksumAlgorithm::emitUpdateMethod(CodeBuilder* builder, int crcWidth) {
// Note that this update method is optimized for our CRC16 and CRC32, custom
// version may require other method of update. To deal with byte order, data
// version may require other method of update. To deal with byte order data
// is read from the end of buffer.
cstring code = "static __always_inline\n"
"void crc%w%_update(u%w% * reg, const u8 * data, "
"u16 data_size, const u%w% poly) {\n"
" data += data_size - 1;\n"
" #pragma clang loop unroll(full)\n"
" for (u16 i = 0; i < data_size; i++) {\n"
" bpf_trace_message(\"CRC%w%: data byte: %x\\n\", *data);\n"
" *reg ^= *data;\n"
" for (u8 bit = 0; bit < 8; bit++) {\n"
" *reg = (*reg) & 1 ? ((*reg) >> 1) ^ poly : (*reg) >> 1;\n"
" }\n"
" data--;\n"
" }\n"
"}";
code = code.replace("%w%", Util::printf_format("%d", crcWidth));
builder->appendLine(code);
if (crcWidth == 16){
cstring code = "static __always_inline\n"
"void crc%w%_update(u%w% * reg, const u8 * data, u16 data_size, const u%w% poly) {\n"
" data += data_size - 1;\n"
" for (u16 i = 0; i < data_size; i++) {\n"
" bpf_trace_message(\"CRC%w%: data byte: %x\\n\", *data);\n"
" *reg ^= *data;\n"
" for (u8 bit = 0; bit < 8; bit++) {\n"
" *reg = (*reg) & 1 ? ((*reg) >> 1) ^ poly : (*reg) >> 1;\n"
" }\n"
" data--;\n"
" }\n"
"}";
code = code.replace("%w%", Util::printf_format("%d", crcWidth));
builder->appendLine(code);
} else if (crcWidth == 32) {


cstring code = "static __always_inline\n"
"void crc%w%_update(u%w% * reg, const u8 * data, u16 data_size, "
"const u%w% poly) {\n"
" data += data_size - 4;\n"
" u32* current = (u32*) data;\n"
" struct lookup_tbl_val* lookup_table;\n"
" u32 index = 0;\n"
" lookup_table = BPF_MAP_LOOKUP_ELEM(crc_lookup_tbl, &index);\n"
" u32 lookup_key = 0;\n"
" u32 lookup_value = 0;\n"
" u32 lookup_value1 = 0;\n"
" u32 lookup_value2 = 0;\n"
" u32 lookup_value3 = 0;\n"
" u32 lookup_value4 = 0;\n"
" u32 lookup_value5 = 0;\n"
" u32 lookup_value6 = 0;\n"
" u32 lookup_value7 = 0;\n"
" u32 lookup_value8 = 0;\n"
" u16 tmp = 0;\n"
" if (lookup_table != NULL) {\n"
" for (u16 i = data_size; i >= 8; i -= 8) {\n"
" bpf_trace_message(\"CRC32: data byte: %x\", *current);\n"
" u32 one = __builtin_bswap32(*current--) ^ *reg;\n"
" bpf_trace_message(\"CRC32: data byte: %x\", *current);\n"
" u32 two = __builtin_bswap32(*current--);\n"
" lookup_key = (one & 0x000000FF);\n"
" lookup_value8 = lookup_table->table[(u16)(1792 + (u8)lookup_key)];\n"
" lookup_key = (one >> 8) & 0x000000FF;\n"
" lookup_value7 = lookup_table->table[(u16)(1536 + (u8)lookup_key)];\n"
" lookup_key = (one >> 16) & 0x000000FF;\n"
" lookup_value6 = lookup_table->table[(u16)(1280 + (u8)lookup_key)];\n"
" lookup_key = one >> 24;\n"
" lookup_value5 = "
"lookup_table->table[(u16)(1024 + (u8)(lookup_key))];\n"
" lookup_key = (two & 0x000000FF);\n"
" lookup_value4 = lookup_table->table[(u16)(768 + (u8)lookup_key)];\n"
" lookup_key = (two >> 8) & 0x000000FF;\n"
" lookup_value3 = lookup_table->table[(u16)(512 + (u8)lookup_key)];\n"
" lookup_key = (two >> 16) & 0x000000FF;\n"
" lookup_value2 = lookup_table->table[(u16)(256 + (u8)lookup_key)];\n"
" lookup_key = two >> 24;\n"
" lookup_value1 = lookup_table->table[(u8)(lookup_key)];\n"
" *reg = lookup_value8 ^ lookup_value7 ^ lookup_value6 ^ lookup_value5"
" ^ lookup_value4 ^\n"
" lookup_value3 ^ lookup_value2 ^ lookup_value1;\n"
" tmp += 8;\n"
" }\n"
" unsigned char *currentChar = (unsigned char *) current;\n"
" currentChar+= 3;\n"
" volatile int std_algo_lookup_key = 0;\n"
" for (u16 i = tmp; i < data_size; i++) {\n"
" bpf_trace_message(\"CRC32: data byte: %x\\n\", *current);\n"
" std_algo_lookup_key = (u32)(((*reg) & 0xFF) ^ *currentChar--);\n"
" if (std_algo_lookup_key >= 0) {\n"
" lookup_value = "
"lookup_table->table[(u8)(std_algo_lookup_key & 255)];\n"
" }\n"
" *reg = ((*reg) >> 8) ^ lookup_value;\n"
" }\n"
" }\n"
"}";
code = code.replace("%w%", Util::printf_format("%d", crcWidth));
builder->appendLine(code);
}
}

void CRCChecksumAlgorithm::emitVariables(CodeBuilder* builder,
Expand Down