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

DPDK Backend: Emit name information for externs in context json #3433

Merged
merged 5 commits into from
Jul 14, 2022
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
78 changes: 69 additions & 9 deletions backends/dpdk/DPDK_context_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
"type": "string",
"description": "Name of the referenced table as in the P4 file."
},
"target_name": {
"type": "string",
"description": "Name of the referenced table as in the spec file."
},
"handle": {
"type": "integer",
"description": "Handle of the referenced table."
Expand All @@ -30,10 +26,6 @@
"type": "string",
"description": "Name of the action as in P4 file"
},
"target_name": {
"type": "string",
"description": "Name of the action as in spec file"
},
"action_handle": {
"type": "integer",
"description": "Unique reference handle to this action"
Expand Down Expand Up @@ -111,6 +103,10 @@
"type": "string",
"description": "Name of this table"
},
"target_name": {
"type": "string",
"description": "Name of the table as in the spec file."
},
"handle": {
"type": "integer",
"description": "Unique reference ID for this table"
Expand Down Expand Up @@ -229,6 +225,10 @@
"type": "string",
"description": "The P4 name of the action."
},
"target_name": {
"type": "string",
"description": "Name of the action as in spec file"
},
"handle": {
"type": "integer",
"description": "A unique identifier for this action."
Expand Down Expand Up @@ -325,6 +325,10 @@
"type": "string",
"description": "Name of this table"
},
"target_name": {
"type": "string",
"description": "Name of the table as in the spec file."
},
"handle": {
"type": "integer",
"description": "Unique reference ID for this table"
Expand Down Expand Up @@ -378,6 +382,10 @@
"type": "string",
"description": "Name of this table"
},
"target_name": {
"type": "string",
"description": "Name of the table as in the spec file."
},
"handle": {
"type": "integer",
"description": "Unique reference ID for this table"
Expand Down Expand Up @@ -472,6 +480,50 @@
"actions"
],
"additionalProperties": false
},
"__main__.ExternAttributes": {
"type": "object",
"properties": {
"type": {
"type": "string",
"description": "Type of extern"
}
},
"additionalProperties": false
},
"__main__.Externs": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Name of the extern as in the P4 file."
},
"target_name": {
"type": "string",
"description": "Name of the extern as in the spec file."
},
"extern_type": {
"type": "string",
"description": "Extern type",
"enum": [
"Counter",
"Register",
"Meter",
"Hash",
"InternetChecksum"
]
},
"match_attributes": {
"$ref": "#/definitions/__main__.ExternAttributes"
}
},
"required": [
"name",
"target_name",
"extern_type",
"attributes"
],
"additionalProperties": false
}
},
"type": "object",
Expand Down Expand Up @@ -520,6 +572,13 @@
}
]
}
},
"externs": {
"type": "array",
"description": "List of externs in this P4 program",
"items": {
"$ref": "#/definitions/__main__.Externs"
}
}
},
"required": [
Expand All @@ -528,7 +587,8 @@
"compiler_version",
"schema_version",
"target",
"tables"
"tables",
"externs"
],
"additionalProperties": false
}
Expand Down
59 changes: 59 additions & 0 deletions backends/dpdk/dpdkContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,45 @@ void DpdkContextGenerator::CollectTablesAndSetAttributes() {
}
}

for (auto ed : structure->externDecls) {
if (auto type = ed->type->to<IR::Type_Specialized>()) {
auto externTypeName = type->baseType->path->name.name;
if (externTypeName == "Counter" ||
Copy link
Contributor

Choose a reason for hiding this comment

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

do you want to ignore silently all other externs?

externTypeName == "Register" ||
externTypeName == "Meter" ||
externTypeName == "Hash" ||
externTypeName == "InternetCheckSum") {
struct externAttributes externAttr;
externAttr.externalName = ed->controlPlaneName();
externAttr.externType = externTypeName;
if (externTypeName == "Counter") {
if (ed->arguments->size() != 2) {
::error(ErrorType::ERR_UNEXPECTED,
"%1%: expected 2 arguments, number of counters and type"
"of counter", ed);
}
auto counter_type = ed->arguments->at(1)->expression;
BUG_CHECK(counter_type->is<IR::Constant>(),
"Expected counter type to be constant");
auto value = counter_type->to<IR::Constant>()->asUnsigned();
switch (value) {
case 0:
externAttr.counterType = "packets";
break;
case 1:
externAttr.counterType = "bytes";
break;
case 2:
externAttr.counterType = "packets_and_bytes";
break;
}
}
externAttrMap.emplace(ed->name.name, externAttr);
externs.push_back(ed);
}
}
}

// Keep the compiler generated (hidden) tables at the end
for (auto d : selector_tables)
tables.push_back(d);
Expand Down Expand Up @@ -452,9 +491,27 @@ void DpdkContextGenerator::addMatchTables(Util::JsonArray* tablesJson) {
}
}

// Add extern information to the context json
void DpdkContextGenerator::addExternInfo(Util::JsonArray* externsJson) {
for (auto t : externs) {
auto externAttr = ::get(externAttrMap, t->name.name);
auto* externJson = new Util::JsonObject();
externJson->emplace("name", externAttr.externalName);
externJson->emplace("target_name", t->name.name);
externJson->emplace("type", externAttr.externType);
auto* attrJson = new Util::JsonObject();
if (externAttr.externType == "Counter") {
attrJson->emplace("type", externAttr.counterType);
}
externJson->emplace("attributes", attrJson);
externsJson->append(externJson);
}
}

const Util::JsonObject* DpdkContextGenerator::genContextJsonObject() {
auto* json = new Util::JsonObject();
auto* tablesJson = new Util::JsonArray();
auto* externsJson = new Util::JsonArray();
struct TopLevelCtxt tlinfo;
tlinfo.initTopLevelCtxt(options);
json->emplace("program_name", tlinfo.progName);
Expand All @@ -465,6 +522,8 @@ const Util::JsonObject* DpdkContextGenerator::genContextJsonObject() {
json->emplace("target", cstring("DPDK"));
json->emplace("tables", tablesJson);
addMatchTables(tablesJson);
json->emplace("externs", externsJson);
addExternInfo(externsJson);
return json;
}

Expand Down
11 changes: 10 additions & 1 deletion backends/dpdk/dpdkContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ struct actionAttributes {
IR::IndexedVector<IR::Parameter> *params;
};

struct externAttributes {
cstring externalName;
cstring externType;
cstring counterType;
};

/* Program level information for context json */
struct TopLevelCtxt{
cstring progName;
Expand Down Expand Up @@ -123,10 +129,12 @@ class DpdkContextGenerator : public Inspector {
DpdkOptions &options;
// All tables are collected into this vector
IR::IndexedVector<IR::Declaration> tables;
std::vector<const IR::Declaration_Instance*> externs;

// Maps holding table and action attributes needed for context JSON
// Maps holding table, extern and action attributes needed for context JSON
std::map<const cstring, struct TableAttributes> tableAttrmap;
std::map <cstring, struct actionAttributes> actionAttrMap;
std::map <cstring, struct externAttributes> externAttrMap;

// Running unique ID for tables and actions
static unsigned newTableHandle;
Expand All @@ -143,6 +151,7 @@ class DpdkContextGenerator : public Inspector {
void serializeContextJson(std::ostream* destination);
const Util::JsonObject* genContextJsonObject();
void addMatchTables(Util::JsonArray* tablesJson);
void addExternInfo(Util::JsonArray* externsJson);
Util::JsonObject* initTableCommonJson(const cstring name, const struct TableAttributes & attr);
void addKeyField(Util::JsonArray* keyJson, const cstring name, const cstring annon,
const IR::KeyElement *key, int position);
Expand Down