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

Update jsonconverter to use most recent format #223

Merged
merged 1 commit into from
Jan 10, 2017
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
36 changes: 30 additions & 6 deletions backends/bmv2/jsonconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1114,15 +1114,16 @@ Util::IJson* JsonConverter::convertIf(const CFG::IfNode* node, cstring) {

bool JsonConverter::handleTableImplementation(const IR::Property* implementation,
const IR::Key* key,
Util::JsonObject* table) {
Util::JsonObject* table,
Util::JsonArray* action_profiles) {
if (implementation == nullptr) {
table->emplace("type", "simple");
return true;
}

cstring name = refMap->newName("action_profile");
name = nameFromAnnotation(implementation->annotations, name);
table->emplace("act_prof_name", name);
table->emplace("action_profile", name);
if (!implementation->value->is<IR::ExpressionValue>()) {
::error("%1%: expected expression for property", implementation);
return false;
Expand All @@ -1139,14 +1140,33 @@ bool JsonConverter::handleTableImplementation(const IR::Property* implementation
return false;
}

auto action_profile = new Util::JsonObject();
action_profiles->append(action_profile);
action_profile->emplace("name", name);
action_profile->emplace("id", nextId("action_profiles"));

bool isSimpleTable = true;
auto ecc = cc->to<P4::ExternConstructorCall>();

auto add_size = [&action_profile, &ecc](size_t arg_index) {
auto size_expr = ecc->cce->arguments->at(arg_index);
int size;
if (!size_expr->is<IR::Constant>()) {
::error("%1% must be a constant", size_expr);
size = 0;
} else {
size = size_expr->to<IR::Constant>()->asInt();
}
action_profile->emplace("max_size", size);
};

if (ecc->type->name == v1model.action_selector.name) {
BUG_CHECK(ecc->cce->arguments->size() == 3, "%1%: expected 3 arguments", cc->cce);
isSimpleTable = false;
auto selector = new Util::JsonObject();
table->emplace("type", "indirect_ws");
table->emplace("selector", selector);
action_profile->emplace("selector", selector);
add_size(1);
auto hash = ecc->cce->arguments->at(0);
auto ei = P4::EnumInstance::resolve(hash, typeMap);
if (ei == nullptr) {
Expand All @@ -1169,6 +1189,7 @@ bool JsonConverter::handleTableImplementation(const IR::Property* implementation
} else if (ecc->type->name == v1model.action_profile.name) {
isSimpleTable = false;
table->emplace("type", "indirect");
add_size(0);
} else {
::error("%1%: unexpected value for property", propv);
}
Expand All @@ -1195,7 +1216,9 @@ cstring JsonConverter::convertHashAlgorithm(cstring algorithm) const {
}

Util::IJson*
JsonConverter::convertTable(const CFG::TableNode* node, Util::JsonArray* counters) {
JsonConverter::convertTable(const CFG::TableNode* node,
Util::JsonArray* counters,
Util::JsonArray* action_profiles) {
auto table = node->table;
LOG1("Processing " << table);
auto result = new Util::JsonObject();
Expand Down Expand Up @@ -1273,7 +1296,7 @@ JsonConverter::convertTable(const CFG::TableNode* node, Util::JsonArray* counter
conv->simpleExpressionsOnly = false;

auto impl = table->properties->getProperty(v1model.tableAttributes.tableImplementation.name);
bool simple = handleTableImplementation(impl, key, result);
bool simple = handleTableImplementation(impl, key, result, action_profiles);

unsigned size = 0;
auto sz = table->properties->getProperty(v1model.tableAttributes.size.name);
Expand Down Expand Up @@ -1504,11 +1527,12 @@ Util::IJson* JsonConverter::convertControl(const IR::ControlBlock* block, cstrin
result->emplace("init_table", start->name);
}
auto tables = mkArrayField(result, "tables");
auto action_profiles = mkArrayField(result, "action_profiles");
auto conditionals = mkArrayField(result, "conditionals");

for (auto node : cfg->allNodes) {
if (node->is<CFG::TableNode>()) {
auto j = convertTable(node->to<CFG::TableNode>(), counters);
auto j = convertTable(node->to<CFG::TableNode>(), counters, action_profiles);
tables->append(j);
} else if (node->is<CFG::IfNode>()) {
auto j = convertIf(node->to<CFG::IfNode>(), cont->name);
Expand Down
7 changes: 5 additions & 2 deletions backends/bmv2/jsonconverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ class JsonConverter final {
void convertActionBody(const IR::Vector<IR::StatOrDecl>* body,
Util::JsonArray* result, Util::JsonArray* fieldLists,
Util::JsonArray* calculations, Util::JsonArray* learn_lists);
Util::IJson* convertTable(const CFG::TableNode* node, Util::JsonArray* counters);
Util::IJson* convertTable(const CFG::TableNode* node,
Util::JsonArray* counters,
Util::JsonArray* action_profiles);
Util::IJson* convertIf(const CFG::IfNode* node, cstring parent);
Util::JsonArray* createActions(Util::JsonArray* fieldLists, Util::JsonArray* calculations,
Util::JsonArray* learn_lists);
Expand All @@ -117,7 +119,8 @@ class JsonConverter final {
// Return 'true' if the table is 'simple'
bool handleTableImplementation(const IR::Property* implementation,
const IR::Key* key,
Util::JsonObject* table);
Util::JsonObject* table,
Util::JsonArray* action_profiles);
void addToFieldList(const IR::Expression* expr, Util::JsonArray* fl);
// returns id of created field list
int createFieldList(const IR::Expression* expr, cstring group,
Expand Down
73 changes: 73 additions & 0 deletions testdata/p4_16_samples/action_profile-bmv2.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright 2013-present Barefoot Networks, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <core.p4>
#include <v1model.p4>

struct H { };
struct M {
bit<32> hash1;
}

parser ParserI(packet_in pk, out H hdr, inout M meta, inout standard_metadata_t smeta) {
state start { transition accept; }
}

action empty() { }

control IngressI(inout H hdr, inout M meta, inout standard_metadata_t smeta) {

action drop() { smeta.drop = 1; }

table indirect {
key = { }
actions = { drop; NoAction; }
const default_action = NoAction();
@name("ap") implementation = action_profile(32w128);
}

table indirect_ws {
key = { meta.hash1 : selector; }
actions = { drop; NoAction; }
const default_action = NoAction();
@name("ap_ws") implementation = action_selector(HashAlgorithm.identity, 32w1024, 32w10);
}

apply {
indirect.apply();
indirect_ws.apply();
}

};

control EgressI(inout H hdr, inout M meta, inout standard_metadata_t smeta) {
apply { }
};

control DeparserI(packet_out pk, in H hdr) {
apply { }
}

control VerifyChecksumI(in H hdr, inout M meta) {
apply { }
}

control ComputeChecksumI(inout H hdr, inout M meta) {
apply { }
}

V1Switch(ParserI(), VerifyChecksumI(), IngressI(), EgressI(),
ComputeChecksumI(), DeparserI()) main;
1 change: 1 addition & 0 deletions testdata/p4_16_samples/action_profile-bmv2.stf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# empty for now
70 changes: 70 additions & 0 deletions testdata/p4_16_samples_outputs/action_profile-bmv2-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <core.p4>
#include <v1model.p4>

struct H {
}

struct M {
bit<32> hash1;
}

parser ParserI(packet_in pk, out H hdr, inout M meta, inout standard_metadata_t smeta) {
state start {
transition accept;
}
}

action empty() {
}
control IngressI(inout H hdr, inout M meta, inout standard_metadata_t smeta) {
action drop() {
smeta.drop = 1w1;
}
table indirect() {
key = {
}
actions = {
drop();
NoAction();
}
const default_action = NoAction();
@name("ap") implementation = action_profile(32w128);
}
table indirect_ws() {
key = {
meta.hash1: selector;
}
actions = {
drop();
NoAction();
}
const default_action = NoAction();
@name("ap_ws") implementation = action_selector(HashAlgorithm.identity, 32w1024, 32w10);
}
apply {
indirect.apply();
indirect_ws.apply();
}
}

control EgressI(inout H hdr, inout M meta, inout standard_metadata_t smeta) {
apply {
}
}

control DeparserI(packet_out pk, in H hdr) {
apply {
}
}

control VerifyChecksumI(in H hdr, inout M meta) {
apply {
}
}

control ComputeChecksumI(inout H hdr, inout M meta) {
apply {
}
}

V1Switch<H, M>(ParserI(), VerifyChecksumI(), IngressI(), EgressI(), ComputeChecksumI(), DeparserI()) main;
68 changes: 68 additions & 0 deletions testdata/p4_16_samples_outputs/action_profile-bmv2-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <core.p4>
#include <v1model.p4>

struct H {
}

struct M {
bit<32> hash1;
}

parser ParserI(packet_in pk, out H hdr, inout M meta, inout standard_metadata_t smeta) {
state start {
transition accept;
}
}

control IngressI(inout H hdr, inout M meta, inout standard_metadata_t smeta) {
@name("drop") action drop_0() {
smeta.drop = 1w1;
}
@name("indirect") table indirect_0() {
key = {
}
actions = {
drop_0();
NoAction();
}
const default_action = NoAction();
@name("ap") implementation = action_profile(32w128);
}
@name("indirect_ws") table indirect_ws_0() {
key = {
meta.hash1: selector;
}
actions = {
drop_0();
NoAction();
}
const default_action = NoAction();
@name("ap_ws") implementation = action_selector(HashAlgorithm.identity, 32w1024, 32w10);
}
apply {
indirect_0.apply();
indirect_ws_0.apply();
}
}

control EgressI(inout H hdr, inout M meta, inout standard_metadata_t smeta) {
apply {
}
}

control DeparserI(packet_out pk, in H hdr) {
apply {
}
}

control VerifyChecksumI(in H hdr, inout M meta) {
apply {
}
}

control ComputeChecksumI(inout H hdr, inout M meta) {
apply {
}
}

V1Switch<H, M>(ParserI(), VerifyChecksumI(), IngressI(), EgressI(), ComputeChecksumI(), DeparserI()) main;
Loading