Skip to content

Commit

Permalink
P4TC - Support const entries (#4329)
Browse files Browse the repository at this point in the history
* Fix actionparams type field in introspection.json

* Support const_entries

* Update testcase

* Address new changes

* Address comments

* Addressed comments

* Fix issue when all entries are not emitted in case of multiple entry with same action
  • Loading branch information
komaljai authored Jan 30, 2024
1 parent 71e348e commit c43b24a
Show file tree
Hide file tree
Showing 20 changed files with 1,548 additions and 0 deletions.
60 changes: 60 additions & 0 deletions backends/tc/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,65 @@ void ConvertToBackendIR::postorder(const IR::P4Action *action) {
}
}

void ConvertToBackendIR::updateConstEntries(const IR::P4Table *t, IR::TCTable *tabledef) {
// Check if there are const entries.
auto entriesList = t->getEntries();
if (entriesList == nullptr) return;
auto keys = t->getKey();
if (keys == nullptr) {
return;
}
for (auto e : entriesList->entries) {
auto keyset = e->getKeys();
if (keyset->components.size() != keys->keyElements.size()) {
::error(ErrorType::ERR_INVALID,
"No of keys in const_entries should be same as no of keys in the table.");
return;
}
ordered_map<cstring, cstring> keyList;
for (size_t itr = 0; itr < keyset->components.size(); itr++) {
auto keyElement = keys->keyElements.at(itr);
auto keyString = keyElement->expression->toString();
auto annotations = keyElement->getAnnotations();
if (annotations) {
if (auto anno = annotations->getSingle("name")) {
keyString = anno->expr.at(0)->to<IR::StringLiteral>()->value;
}
}
auto keySetElement = keyset->components.at(itr);
auto key = keySetElement->toString();
if (keySetElement->is<IR::DefaultExpression>()) {
key = "default";
} else if (keySetElement->is<IR::Constant>()) {
big_int kValue = keySetElement->to<IR::Constant>()->value;
int kBase = keySetElement->to<IR::Constant>()->base;
std::stringstream value;
std::deque<char> buf;
do {
const int digit = static_cast<int>(static_cast<big_int>(kValue % kBase));
kValue = kValue / kBase;
buf.push_front(Util::DigitToChar(digit));
} while (kValue > 0);
for (auto ch : buf) value << ch;
key = value.str().c_str();
} else if (keySetElement->is<IR::Range>()) {
auto left = keySetElement->to<IR::Range>()->left;
auto right = keySetElement->to<IR::Range>()->right;
auto operand = keySetElement->to<IR::Range>()->getStringOp();
key = left->toString() + operand + right->toString();
} else if (keySetElement->is<IR::Mask>()) {
auto left = keySetElement->to<IR::Mask>()->left;
auto right = keySetElement->to<IR::Mask>()->right;
auto operand = keySetElement->to<IR::Mask>()->getStringOp();
key = left->toString() + operand + right->toString();
}
keyList.emplace(keyString, key);
}
IR::TCEntry *constEntry = new IR::TCEntry(e->action->toString(), keyList);
tabledef->addConstEntries(constEntry);
}
}

void ConvertToBackendIR::updateDefaultMissAction(const IR::P4Table *t, IR::TCTable *tabledef) {
auto defaultAction = t->getDefaultAction();
if (defaultAction == nullptr || !defaultAction->is<IR::MethodCallExpression>()) return;
Expand Down Expand Up @@ -450,6 +509,7 @@ void ConvertToBackendIR::postorder(const IR::P4Table *t) {
updateDefaultHitAction(t, tableDefinition);
updateDefaultMissAction(t, tableDefinition);
updateMatchType(t, tableDefinition);
updateConstEntries(t, tableDefinition);
tcPipeline->addTableDefinition(tableDefinition);
}
}
Expand Down
4 changes: 4 additions & 0 deletions backends/tc/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ and limitations under the License.
#ifndef BACKENDS_TC_BACKEND_H_
#define BACKENDS_TC_BACKEND_H_

#include <deque>

#include "backends/ebpf/psa/ebpfPsaGen.h"
#include "ebpfCodeGen.h"
#include "frontends/p4/evaluator/evaluator.h"
Expand All @@ -26,6 +28,7 @@ and limitations under the License.
#include "ir/ir.h"
#include "lib/error.h"
#include "lib/nullstream.h"
#include "lib/stringify.h"
#include "options.h"
#include "pnaProgramStructure.h"
#include "tcAnnotations.h"
Expand Down Expand Up @@ -73,6 +76,7 @@ class ConvertToBackendIR : public Inspector {
bool isDuplicateOrNoAction(const IR::P4Action *action);
void updateDefaultHitAction(const IR::P4Table *t, IR::TCTable *tdef);
void updateDefaultMissAction(const IR::P4Table *t, IR::TCTable *tdef);
void updateConstEntries(const IR::P4Table *t, IR::TCTable *tdef);
void updateMatchType(const IR::P4Table *t, IR::TCTable *tabledef);
bool isPnaParserMeta(const IR::Member *mem);
bool isPnaMainInputMeta(const IR::Member *mem);
Expand Down
31 changes: 31 additions & 0 deletions backends/tc/tc.def
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ class TCAction {
dbprint { out << toString(); }
}

class TCEntry {
cstring action;
ordered_map<cstring,cstring> keys;
cstring getActionName() const {
return action;
}
toString {
std::string tcEntry = "";
for(auto k : keys) {
tcEntry += " " + k.first + " " + k.second;
}
return tcEntry;
}
dbprint { out << toString(); }
}

class TCTable {
unsigned tableID;
cstring tableName;
Expand All @@ -167,6 +183,8 @@ class TCTable {
TCAction defaultMissAction;
bool isDefaultMissConst;
ordered_map<TCAction, unsigned> actionList;
safe_vector<TCEntry> const_entries;

void setKeySize(unsigned k) {
keySize = k;
}
Expand Down Expand Up @@ -200,6 +218,9 @@ class TCTable {
void addAction(TCAction action, unsigned flag) {
actionList.emplace(action, flag);
}
void addConstEntries(TCEntry entry) {
const_entries.push_back(entry);
}
cstring printMatchType(unsigned matchType) const {
cstring matchTypeString = "";
switch(matchType) {
Expand Down Expand Up @@ -288,6 +309,16 @@ class TCTable {
}
tcTable += " action " + defaultMissAction->getName();
}
if (const_entries.size() != 0) {
for (auto entry : const_entries) {
tcTable += "\n$TC p4template create table/" + pipelineName
+ "/" + controlName + "/" + tableName
+ " entry" + entry->toString();
tcTable += " action " + pipelineName
+ "/" + controlName + "/" + entry->getActionName();
tcTable += " permissions 0x1024";
}
}
return tcTable;
}
dbprint { out << toString(); }
Expand Down
1 change: 1 addition & 0 deletions lib/stringify.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,6 @@ cstring toString(const void *value);
cstring printf_format(const char *fmt_str, ...);
// vprintf into a string
cstring vprintf_format(const char *fmt_str, va_list ap);
char DigitToChar(int digit);
} // namespace Util
#endif /* LIB_STRINGIFY_H_ */
Loading

0 comments on commit c43b24a

Please sign in to comment.