Skip to content

Commit

Permalink
Create explicit count calls for direct counters in P4_14->16 translat…
Browse files Browse the repository at this point in the history
…ion.
  • Loading branch information
Chris Dodd committed Apr 7, 2017
1 parent 67cb53f commit baef6e4
Show file tree
Hide file tree
Showing 31 changed files with 840 additions and 421 deletions.
6 changes: 6 additions & 0 deletions backends/bmv2/jsonconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,12 @@ JsonConverter::convertActionBody(const IR::Vector<IR::StatOrDecl>* body,
// Do not generate any code for this operation
continue;
}
} else if (em->originalExternType->name == v1model.directCounter.name) {
if (em->method->name == v1model.directCounter.count.name) {
BUG_CHECK(mc->arguments->size() == 0, "Expected 0 argument for %1%", mc);
// Do not generate any code for this operation
continue;
}
}
} else if (mi->is<P4::ExternFunction>()) {
auto ef = mi->to<P4::ExternFunction>();
Expand Down
10 changes: 7 additions & 3 deletions frontends/p4/fromv1.0/converters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,14 @@ const IR::Node* StatementConverter::preorder(IR::Apply* apply) {
converted[a.second] = cases.size();
stat = conv.convert(a.second); }
const IR::Expression* destination;
if (a.first == "default")
if (a.first == "default") {
destination = new IR::DefaultExpression(Util::SourceInfo());
else
destination = new IR::PathExpression(IR::ID(a.first));
} else {
cstring act_name = a.first;
cstring full_name = table->name + '.' + act_name;
if (renameMap->count(full_name))
act_name = renameMap->at(full_name);
destination = new IR::PathExpression(IR::ID(act_name)); }
auto swcase = new IR::SwitchCase(a.second->srcInfo, destination, stat);
cases.insert(insert_at, swcase); }
auto check = new IR::Member(Util::SourceInfo(), call, IR::Type_Table::action_run);
Expand Down
64 changes: 52 additions & 12 deletions frontends/p4/fromv1.0/programStructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,8 @@ void ProgramStructure::createDeparser() {

const IR::P4Table*
ProgramStructure::convertTable(const IR::V1Table* table, cstring newName,
IR::IndexedVector<IR::Declaration>* stateful) {
IR::IndexedVector<IR::Declaration>* stateful,
std::map<cstring, cstring> &mapNames) {
ExpressionConverter conv(this);
auto propvec = new IR::IndexedVector<IR::Property>(*table->properties.properties);
auto actVect = new IR::IndexedVector<IR::ActionListElement>();
Expand All @@ -645,16 +646,19 @@ ProgramStructure::convertTable(const IR::V1Table* table, cstring newName,
}

auto mtr = get(directMeters, table->name);
auto ctr = get(directCounters, table->name);
const std::vector<IR::ID> &actionsToDo =
action_profile ? action_profile->actions : table->actions;
for (auto a : actionsToDo) {
auto action = actions.get(a);
cstring newname;
if (mtr != nullptr) {
// we must synthesize a new action, which has a writeback to the meter
if (mtr != nullptr || ctr != nullptr) {
// we must synthesize a new action, which has a writeback to
// the meter/counter
newname = makeUniqueName(a);
auto actCont = convertAction(action, newname, mtr);
auto actCont = convertAction(action, newname, mtr, ctr);
stateful->push_back(actCont);
mapNames[table->name + '.' + a] = newname;
} else {
newname = actions.get(action);
}
Expand Down Expand Up @@ -788,7 +792,6 @@ ProgramStructure::convertTable(const IR::V1Table* table, cstring newName,
propvec->push_back(prop);
}

auto ctr = get(directCounters, table->name);
if (!ctr.isNullOrEmpty()) {
auto counter = counters.get(ctr);
auto kindarg = counterType(counter);
Expand Down Expand Up @@ -1358,7 +1361,8 @@ const IR::Statement* ProgramStructure::convertPrimitive(const IR::Primitive* pri

const IR::P4Action*
ProgramStructure::convertAction(const IR::ActionFunction* action, cstring newName,
const IR::Meter* meterToAccess) {
const IR::Meter* meterToAccess,
cstring counterToAccess) {
LOG1("Converting action " << action->name);
auto body = new IR::IndexedVector<IR::StatOrDecl>();
auto params = new IR::IndexedVector<IR::Parameter>();
Expand Down Expand Up @@ -1405,7 +1409,20 @@ ProgramStructure::convertAction(const IR::ActionFunction* action, cstring newNam
body->push_back(stat);
}

// Save the original action name in an annotation.
if (counterToAccess != nullptr) {
ExpressionConverter conv(this);
auto decl = get(counterMap, counterToAccess);
CHECK_NULL(decl);
auto extObj = new IR::PathExpression(decl->name);
auto method = new IR::Member(Util::SourceInfo(), extObj, v1model.directCounter.count.Id());
auto args = new IR::Vector<IR::Expression>();
auto mc = new IR::MethodCallExpression(Util::SourceInfo(), method,
emptyTypeArguments, args);
auto stat = new IR::MethodCallStatement(mc->srcInfo, mc);
body->push_back(stat);
}

// Save the original action name in an annotation
// The leading dot indicates a global action
auto annos = addNameAnnotation(cstring(".") + action->name, action->annotations);
auto result = new IR::P4Action(
Expand Down Expand Up @@ -1560,6 +1577,22 @@ ProgramStructure::convertDirectMeter(const IR::Meter* m, cstring newName) {
return decl;
}

const IR::Declaration_Instance*
ProgramStructure::convertDirectCounter(const IR::Counter* c, cstring newName) {
LOG1("Synthesizing " << c);

IR::ID ext = v1model.directCounter.Id();
auto typepath = new IR::Path(ext);
auto type = new IR::Type_Name(typepath);
auto args = new IR::Vector<IR::Expression>();
auto kindarg = counterType(c);
args->push_back(kindarg);
auto annos = addNameAnnotation(c->name, c->annotations);
auto decl = new IR::Declaration_Instance(
Util::SourceInfo(), newName, annos, type, args, nullptr);
return decl;
}

const IR::P4Control*
ProgramStructure::convertControl(const IR::V1Control* control, cstring newName) {
IR::ID name = newName;
Expand Down Expand Up @@ -1598,8 +1631,15 @@ ProgramStructure::convertControl(const IR::V1Control* control, cstring newName)
::error("Cannot locate table %1%", c.first->table.name);
return nullptr;
}
if (std::find(usedTables.begin(), usedTables.end(), tbl) != usedTables.end())
directCounters.emplace(c.first->table.name, c.first->name);
if (std::find(usedTables.begin(), usedTables.end(), tbl) != usedTables.end()) {
auto counter = counters.get(c.second);
auto extcounter = convertDirectCounter(counter, c.second);
if (extcounter != nullptr) {
stateful->push_back(extcounter);
directCounters.emplace(c.first->table.name, c.first->name);
counterMap.emplace(c.first->name, extcounter);
}
}
}
}
for (auto c : countersToDo) {
Expand Down Expand Up @@ -1666,21 +1706,21 @@ ProgramStructure::convertControl(const IR::V1Control* control, cstring newName)
::error("Cannot locate action %1%", a);
return nullptr;
}
auto action = convertAction(act, actions.get(act), nullptr);
auto action = convertAction(act, actions.get(act), nullptr, nullptr);
stateful->push_back(action);
}

std::set<cstring> tablesDone;
std::map<cstring, cstring> instanceNames;
for (auto t : usedTables) {
if (tablesDone.find(t->name.name) != tablesDone.end())
continue;
auto tbl = convertTable(t, tables.get(t), stateful);
auto tbl = convertTable(t, tables.get(t), stateful, instanceNames);
if (tbl != nullptr)
stateful->push_back(tbl);
tablesDone.emplace(t->name.name);
}

std::map<cstring, cstring> instanceNames;
if (calledControls.isCaller(name.name)) {
for (auto cc : *calledControls.getCallees(name.name)) {
if (instanceNames.find(cc) != instanceNames.end()) continue;
Expand Down
6 changes: 4 additions & 2 deletions frontends/p4/fromv1.0/programStructure.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class ProgramStructure {
// map table name to direct meter
std::map<cstring, const IR::Meter*> directMeters;
std::map<const IR::Meter*, const IR::Declaration_Instance*> meterMap;
std::map<cstring, const IR::Declaration_Instance*> counterMap;

std::map<const IR::V1Table*, const IR::V1Control*> tableMapping;
std::map<const IR::V1Table*, const IR::Apply*> tableInvocation;
Expand Down Expand Up @@ -162,15 +163,16 @@ class ProgramStructure {
const IR::Statement* convertParserStatement(const IR::Expression* expr);
const IR::P4Control* convertControl(const IR::V1Control* control, cstring newName);
const IR::Declaration_Instance* convertDirectMeter(const IR::Meter* m, cstring newName);
const IR::Declaration_Instance* convertDirectCounter(const IR::Counter* m, cstring newName);
const IR::Declaration_Instance* convert(const IR::CounterOrMeter* cm, cstring newName);
const IR::Declaration_Instance* convert(const IR::Register* reg, cstring newName);
const IR::Declaration_Instance*
convertExtern(const IR::Declaration_Instance* ext, cstring newName);
const IR::P4Table*
convertTable(const IR::V1Table* table, cstring newName,
IR::IndexedVector<IR::Declaration>* stateful);
IR::IndexedVector<IR::Declaration>* stateful, std::map<cstring, cstring> &);
const IR::P4Action* convertAction(const IR::ActionFunction* action, cstring newName,
const IR::Meter* meterToAccess);
const IR::Meter* meterToAccess, cstring counterToAccess);
const IR::Type_Control* controlType(IR::ID name);
const IR::PathExpression* getState(IR::ID dest);
const IR::Declaration_Instance* checksumUnit(const IR::FieldListCalculation* flc);
Expand Down
11 changes: 8 additions & 3 deletions frontends/p4/fromv1.0/v1model.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ struct DirectMeter_Model : public CounterOrMeter_Model {
::Model::Elem read;
};

struct DirectCounter_Model : public CounterOrMeter_Model {
DirectCounter_Model() : CounterOrMeter_Model("direct_counter"), count("count") {}
::Model::Elem count;
};

struct StandardMetadataType_Model : public ::Model::Type_Model {
explicit StandardMetadataType_Model(cstring name) :
::Model::Type_Model(name), dropBit("drop"), recirculate("recirculate_port"),
Expand Down Expand Up @@ -254,8 +259,8 @@ class V1Model : public ::Model::Model {
tableAttributes(), rangeMatchType("range"), selectorMatchType("selector"),
verify("verifyChecksum", headersType), update("computeChecksum", headersType),
ck16(), digest_receiver(), hash(), algorithm(),
directCounter("direct_counter"), registers(), drop("mark_to_drop"),
recirculate("recirculate"), directMeter()
registers(), drop("mark_to_drop"),
recirculate("recirculate"), directMeter(), directCounter()
{}

public:
Expand Down Expand Up @@ -287,11 +292,11 @@ class V1Model : public ::Model::Model {
DigestReceiver_Model digest_receiver;
Hash_Model hash;
Algorithm_Model algorithm;
::Model::Elem directCounter;
Register_Model registers;
::Model::Elem drop;
::Model::Elem recirculate;
DirectMeter_Model directMeter;
DirectCounter_Model directCounter;

static V1Model instance;
};
Expand Down
1 change: 1 addition & 0 deletions p4include/v1model.p4
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ extern counter {

extern direct_counter {
direct_counter(CounterType type);
void count();
}

extern meter {
Expand Down
13 changes: 11 additions & 2 deletions testdata/p4_14_samples_outputs/counter-first.p4
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,26 @@ control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t
}

control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) {
@name("my_direct_counter") direct_counter(CounterType.bytes) my_direct_counter;
@name("my_indirect_counter") counter(32w16384, CounterType.packets) my_indirect_counter;
@name(".m_action") action m_action(bit<14> idx) {
my_indirect_counter.count((bit<32>)idx);
mark_to_drop();
}
@name("._nop") action _nop() {
}
@name(".m_action") action m_action_0(bit<14> idx) {
my_indirect_counter.count((bit<32>)idx);
mark_to_drop();
my_direct_counter.count();
}
@name("._nop") action _nop_0() {
my_direct_counter.count();
}
@name("m_table") table m_table {
actions = {
m_action();
_nop();
m_action_0();
_nop_0();
@default_only NoAction();
}
key = {
Expand Down
11 changes: 7 additions & 4 deletions testdata/p4_14_samples_outputs/counter-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,20 @@ control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t
}

control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) {
@name("my_direct_counter") direct_counter(CounterType.bytes) my_direct_counter_0;
@name("my_indirect_counter") counter(32w16384, CounterType.packets) my_indirect_counter_0;
@name(".m_action") action m_action_0(bit<14> idx) {
@name(".m_action") action m_action(bit<14> idx) {
my_indirect_counter_0.count((bit<32>)idx);
mark_to_drop();
my_direct_counter_0.count();
}
@name("._nop") action _nop_0() {
@name("._nop") action _nop() {
my_direct_counter_0.count();
}
@name("m_table") table m_table_0 {
actions = {
m_action_0();
_nop_0();
m_action();
_nop();
@default_only NoAction();
}
key = {
Expand Down
11 changes: 7 additions & 4 deletions testdata/p4_14_samples_outputs/counter-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,20 @@ control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t
control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) {
@name("NoAction") action NoAction_0() {
}
@name("my_direct_counter") direct_counter(CounterType.bytes) my_direct_counter;
@name("my_indirect_counter") counter(32w16384, CounterType.packets) my_indirect_counter;
@name(".m_action") action m_action_0(bit<14> idx) {
@name(".m_action") action m_action(bit<14> idx) {
my_indirect_counter.count((bit<32>)idx);
mark_to_drop();
my_direct_counter.count();
}
@name("._nop") action _nop_0() {
@name("._nop") action _nop() {
my_direct_counter.count();
}
@name("m_table") table m_table {
actions = {
m_action_0();
_nop_0();
m_action();
_nop();
@default_only NoAction_0();
}
key = {
Expand Down
13 changes: 11 additions & 2 deletions testdata/p4_14_samples_outputs/counter.p4
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,26 @@ control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t
}

control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) {
@name("my_direct_counter") direct_counter(CounterType.bytes) my_direct_counter;
@name("my_indirect_counter") counter(32w16384, CounterType.packets) my_indirect_counter;
@name(".m_action") action m_action(bit<14> idx) {
my_indirect_counter.count((bit<32>)idx);
mark_to_drop();
}
@name("._nop") action _nop() {
}
@name(".m_action") action m_action_0(bit<14> idx) {
my_indirect_counter.count((bit<32>)idx);
mark_to_drop();
my_direct_counter.count();
}
@name("._nop") action _nop_0() {
my_direct_counter.count();
}
@name("m_table") table m_table {
actions = {
m_action;
_nop;
m_action_0;
_nop_0;
@default_only NoAction;
}
key = {
Expand Down
7 changes: 6 additions & 1 deletion testdata/p4_14_samples_outputs/counter1-first.p4
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t
}

control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) {
@name("cnt") direct_counter(CounterType.packets) cnt;
@name(".act") action act(bit<9> port) {
standard_metadata.egress_spec = port;
}
@name(".act") action act_0(bit<9> port) {
standard_metadata.egress_spec = port;
cnt.count();
}
@name("tab1") table tab1 {
actions = {
act();
act_0();
@default_only NoAction();
}
key = {
Expand Down
6 changes: 4 additions & 2 deletions testdata/p4_14_samples_outputs/counter1-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t
}

control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) {
@name(".act") action act_0(bit<9> port) {
@name("cnt") direct_counter(CounterType.packets) cnt_0;
@name(".act") action act(bit<9> port) {
standard_metadata.egress_spec = port;
cnt_0.count();
}
@name("tab1") table tab1_0 {
actions = {
act_0();
act();
@default_only NoAction();
}
key = {
Expand Down
Loading

0 comments on commit baef6e4

Please sign in to comment.