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

Add support for new platform property annotations for P4Runtime. #4611

Merged
merged 11 commits into from
May 31, 2024
Merged
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ set(FETCHCONTENT_QUIET OFF)
FetchContent_Declare(
p4runtime
GIT_REPOSITORY https://github.com/p4lang/p4runtime.git
GIT_TAG 1e771c4e05c4e7e250df00212b3ca02ee3202d71
GIT_TAG 62a9bd60599b87497a15feb6c7893b7ec8ba461f
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(p4runtime)
Expand Down
9 changes: 3 additions & 6 deletions bazel/p4c_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,10 @@ filegroup(
git_repository(
name = "com_github_p4lang_p4runtime",
remote = "https://github.com/p4lang/p4runtime",
# Newest commit on main branch as of August 11, 2023.
commit = "1e771c4e05c4e7e250df00212b3ca02ee3202d71",
# Newest commit on main branch as of May 30, 2024.
commit = "62a9bd60599b87497a15feb6c7893b7ec8ba461f",
shallow_since = "1680213111 -0700",
# strip_prefix is broken; we use patch_cmds as a workaround,
# see https://github.com/bazelbuild/bazel/issues/10062.
# strip_prefix = "proto",
patch_cmds = ["mv proto/* ."],
strip_prefix = "proto",
)
if not native.existing_rule("com_google_googletest"):
# Cannot currently use local_repository due to Bazel limitation,
Expand Down
1 change: 1 addition & 0 deletions control-plane/p4RuntimeAnnotations.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ParseP4RuntimeAnnotations : public ParseAnnotations {
PARSE("id", Constant),
PARSE("brief", StringLiteral),
PARSE("description", StringLiteral),
PARSE_KV_LIST("platform_property"),
// These annotations are architecture-specific in theory, but
// given that they are "reserved" by the P4Runtime
// specification, I don't really have any qualms about adding
Expand Down
36 changes: 35 additions & 1 deletion control-plane/p4RuntimeSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -951,9 +951,43 @@ class P4RuntimeAnalyzer {
}
}

// Parse `@platform_property` annotation into the PkgInfo.
for (auto *annotation : decl->getAnnotations()->annotations) {
jonathan-dilorenzo marked this conversation as resolved.
Show resolved Hide resolved
if (annotation->name != "platform_property") continue;
auto *platform_properties = pkginfo->mutable_platform_properties();
for (auto *kv : annotation->kv) {
auto name = kv->name.name;
auto setInt32Field = [kv, &platform_properties](cstring fName) {
auto *v = kv->expression->to<IR::Constant>();
if (v == nullptr) {
::error(ErrorType::ERR_UNSUPPORTED,
"Value for '%1%' key in @platform_property annotation is not an "
"integer",
kv);
return;
}
// use Protobuf reflection library to minimize code duplication.
auto *descriptor = platform_properties->GetDescriptor();
auto *f = descriptor->FindFieldByName(static_cast<std::string>(fName));
platform_properties->GetReflection()->SetInt32(platform_properties, f,
static_cast<int32_t>(v->value));
};
if (name == "multicast_group_table_size" ||
name == "multicast_group_table_total_replicas" ||
name == "multicast_group_table_max_replicas_per_entry") {
setInt32Field(name);
} else {
::warning(ErrorType::WARN_UNKNOWN,
"Unknown key name '%1%' in @platform_property annotation", name);
}
}
}

// add other annotations on the P4 package to the message. @pkginfo is
// ignored using the unary predicate argument to addAnnotations.
addAnnotations(pkginfo, decl, [](cstring name) { return name == "pkginfo"; });
addAnnotations(pkginfo, decl, [](cstring name) {
return name == "pkginfo" || name == "platform_property";
});

addDocumentation(pkginfo, decl);
}
Expand Down
61 changes: 61 additions & 0 deletions testdata/p4_16_samples/pkginfo_annotation.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2019-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 { };

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) {
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(inout H hdr, inout M meta) {
apply { }
}

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


@pkginfo(
name = "some_name",
organization = "some_org",
version = "some_version",
contact = "some_contact",
url = "some_url"
)
@platform_property(
multicast_group_table_size = 10,
multicast_group_table_total_replicas = 20,
multicast_group_table_max_replicas_per_entry = 30
Copy link
Member

Choose a reason for hiding this comment

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

It would be really wonderful (but certainly not a blocker) if we could support optional, trailing commas here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, but I believe not currently how the KV parser works, which I just call without modification. @fruffy might know better though?

)
V1Switch(ParserI(), VerifyChecksumI(), IngressI(), EgressI(),
ComputeChecksumI(), DeparserI()) main;
42 changes: 42 additions & 0 deletions testdata/p4_16_samples_outputs/pkginfo_annotation-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <core.p4>
#define V1MODEL_VERSION 20180101
#include <v1model.p4>

struct H {
}

struct M {
}

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) {
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(inout H hdr, inout M meta) {
apply {
}
}

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

@pkginfo(name="some_name", organization="some_org", version="some_version", contact="some_contact", url="some_url") @platform_property(multicast_group_table_size = 10 , multicast_group_table_total_replicas = 20 , multicast_group_table_max_replicas_per_entry = 30) V1Switch<H, M>(ParserI(), VerifyChecksumI(), IngressI(), EgressI(), ComputeChecksumI(), DeparserI()) main;
42 changes: 42 additions & 0 deletions testdata/p4_16_samples_outputs/pkginfo_annotation-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <core.p4>
#define V1MODEL_VERSION 20180101
#include <v1model.p4>

struct H {
}

struct M {
}

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) {
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(inout H hdr, inout M meta) {
apply {
}
}

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

@pkginfo(name="some_name", organization="some_org", version="some_version", contact="some_contact", url="some_url") @platform_property(multicast_group_table_size = 10 , multicast_group_table_total_replicas = 20 , multicast_group_table_max_replicas_per_entry = 30) V1Switch<H, M>(ParserI(), VerifyChecksumI(), IngressI(), EgressI(), ComputeChecksumI(), DeparserI()) main;
42 changes: 42 additions & 0 deletions testdata/p4_16_samples_outputs/pkginfo_annotation-midend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <core.p4>
#define V1MODEL_VERSION 20180101
#include <v1model.p4>

struct H {
}

struct M {
}

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) {
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(inout H hdr, inout M meta) {
apply {
}
}

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

@pkginfo(name="some_name", organization="some_org", version="some_version", contact="some_contact", url="some_url") @platform_property(multicast_group_table_size = 10 , multicast_group_table_total_replicas = 20 , multicast_group_table_max_replicas_per_entry = 30) V1Switch<H, M>(ParserI(), VerifyChecksumI(), IngressI(), EgressI(), ComputeChecksumI(), DeparserI()) main;
42 changes: 42 additions & 0 deletions testdata/p4_16_samples_outputs/pkginfo_annotation.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <core.p4>
#define V1MODEL_VERSION 20180101
#include <v1model.p4>

struct H {
}

struct M {
}

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) {
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(inout H hdr, inout M meta) {
apply {
}
}

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

@pkginfo(name="some_name", organization="some_org", version="some_version", contact="some_contact", url="some_url") @platform_property(multicast_group_table_size = 10 , multicast_group_table_total_replicas = 20 , multicast_group_table_max_replicas_per_entry = 30) V1Switch(ParserI(), VerifyChecksumI(), IngressI(), EgressI(), ComputeChecksumI(), DeparserI()) main;
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# proto-file: p4/v1/p4runtime.proto
# proto-message: p4.v1.WriteRequest

16 changes: 16 additions & 0 deletions testdata/p4_16_samples_outputs/pkginfo_annotation.p4.p4info.txtpb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# proto-file: p4/config/v1/p4info.proto
# proto-message: p4.config.v1.P4Info

pkg_info {
name: "some_name"
version: "some_version"
arch: "v1model"
organization: "some_org"
contact: "some_contact"
url: "some_url"
platform_properties {
multicast_group_table_size: 10
multicast_group_table_total_replicas: 20
multicast_group_table_max_replicas_per_entry: 30
}
}
Loading