Skip to content

Commit

Permalink
[Comb] Generate IrTableEntryReferences in create IrP4info. (sonic-net…
Browse files Browse the repository at this point in the history
…#292)

* [pdpi] Add function to create IrTableEntryReferences from IrP4info

PiperOrigin-RevId: 577009820

* [pdpi] Generate IrTableEntryReferences in create IrP4info.

---------

Co-authored-by: Srikishen Pondicherry Shanmugam <[email protected]>
Co-authored-by: bhagatgit <[email protected]>
  • Loading branch information
3 people authored Jul 8, 2024
1 parent 2fcc6e5 commit cff1b38
Show file tree
Hide file tree
Showing 5 changed files with 2,162 additions and 208 deletions.
2 changes: 2 additions & 0 deletions p4_pdpi/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ cc_library(
# Disable default arguments internally. Using them in PDPI itself is very likely a bug.
local_defines = ["PDPI_DISABLE_TRANSLATION_OPTIONS_DEFAULT"],
deps = [
":built_ins",
":ir_cc_proto",
":reference_annotations",
":translation_options",
Expand All @@ -278,6 +279,7 @@ cc_library(
"@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
"@com_github_p4lang_p4runtime//:p4types_cc_proto",
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/status",
Expand Down
111 changes: 111 additions & 0 deletions p4_pdpi/ir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
#include <ctype.h>
#include <stdint.h>

#include <algorithm>
#include <cctype>
#include <string>
#include <utility>
#include <vector>

#include "absl/algorithm/container.h"
#include "absl/base/attributes.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/status/status.h"
Expand All @@ -43,6 +47,7 @@
#include "p4/config/v1/p4info.pb.h"
#include "p4/config/v1/p4types.pb.h"
#include "p4/v1/p4runtime.pb.h"
#include "p4_pdpi/built_ins.h"
#include "p4_pdpi/ir.pb.h"
#include "p4_pdpi/reference_annotations.h"
#include "p4_pdpi/translation_options.h"
Expand All @@ -63,6 +68,7 @@ using ::pdpi::IrActionInvocation;
using ::pdpi::IrMatchFieldDefinition;
using ::pdpi::IrP4Info;
using ::pdpi::IrTableDefinition;
using ::pdpi::IrTableReference;
using ::pdpi::ParsedRefersToAnnotation;

namespace {
Expand Down Expand Up @@ -227,6 +233,9 @@ absl::StatusOr<uint32_t> MatchFieldNameToId(

// Returns the set of references for a given set of annotations. Does not
// validate the table or match field yet.
// TODO: b/306016407 - Remove this function once all p4 infrastructure has been
// moved to IrTableEntryReference.
ABSL_DEPRECATED("Use ParseRefersToAnnotations instead")
absl::StatusOr<std::vector<IrMatchFieldReference>> GetRefersToAnnotations(
const p4::config::v1::P4Info &p4info,
const ::google::protobuf::RepeatedPtrField<std::string> &annotations) {
Expand All @@ -239,6 +248,9 @@ absl::StatusOr<std::vector<IrMatchFieldReference>> GetRefersToAnnotations(
absl::string_view table = refers_to_annotation.table;
absl::string_view match_field = refers_to_annotation.field;

// Deprecated function should ignore new functionality.
if (IsBuiltInTable(table)) continue;

ASSIGN_OR_RETURN(uint32_t table_id, TableAliasToId(p4info, table));
ASSIGN_OR_RETURN(uint32_t match_field_id,
MatchFieldNameToId(p4info, table_id, match_field));
Expand Down Expand Up @@ -1149,6 +1161,98 @@ bool ExpensiveIsElementUnsupported(
});
}

absl::Status InsertOutgoingReferenceInfo(const IrTableReference &reference,
IrP4Info &info) {
const IrTable &source = reference.source_table();
switch (source.table_case()) {
case IrTable::kP4Table: {
auto it_name =
info.mutable_tables_by_name()->find(source.p4_table().table_name());
if (it_name == info.mutable_tables_by_name()->end()) {
return gutil::InternalErrorBuilder()
<< "Generated IrTableEntryReference contains unknown source p4 "
"table name '"
<< source.p4_table().table_name() << "'.";
}
auto it_id =
info.mutable_tables_by_id()->find(source.p4_table().table_id());
if (it_id == info.mutable_tables_by_id()->end()) {
return gutil::InternalErrorBuilder()
<< "Generated IrTableEntryReference contains unknown source p4 "
"table id '"
<< source.p4_table().table_name() << "'.";
}
*(it_name->second.add_outgoing_references()) = reference;
*(it_id->second.add_outgoing_references()) = reference;
return absl::OkStatus();
}
case IrTable::kBuiltInTable: {
ASSIGN_OR_RETURN(const std::string built_in_table_name,
IrBuiltInTableToString(source.built_in_table()));
auto [it, fresh] = info.mutable_built_in_tables()->insert(
{built_in_table_name, IrBuiltInTableDefinition()});
if (fresh) {
it->second.set_built_in_table(source.built_in_table());
}
*info.mutable_built_in_tables()
->at(built_in_table_name)
.add_outgoing_references() = reference;

return absl::OkStatus();
}
case IrTable::TABLE_NOT_SET: {
return gutil::InternalErrorBuilder()
<< "Source IrTable oneof not set." << reference.DebugString();
}
}
}

absl::Status InsertIncomingReferenceInfo(const IrTableReference &reference,
IrP4Info &info) {
const IrTable &destination = reference.destination_table();
switch (destination.table_case()) {
case IrTable::kP4Table: {
auto it_name = info.mutable_tables_by_name()->find(
destination.p4_table().table_name());
if (it_name == info.mutable_tables_by_name()->end()) {
return gutil::InternalErrorBuilder()
<< "Generated IrTableEntryReference contains unknown "
"destination p4 table name '"
<< destination.p4_table().table_name() << "'.";
}
auto it_id =
info.mutable_tables_by_id()->find(destination.p4_table().table_id());
if (it_id == info.mutable_tables_by_id()->end()) {
return gutil::InternalErrorBuilder()
<< "Generated IrTableEntryReference contains unknown "
"destination p4 table id '"
<< destination.p4_table().table_name() << "'.";
}
*(it_name->second.add_incoming_references()) = reference;
*(it_id->second.add_incoming_references()) = reference;
return absl::OkStatus();
}
case IrTable::kBuiltInTable: {
ASSIGN_OR_RETURN(const std::string built_in_table_name,
IrBuiltInTableToString(destination.built_in_table()));
auto [it, fresh] = info.mutable_built_in_tables()->insert(
{built_in_table_name, IrBuiltInTableDefinition()});
if (fresh) {
it->second.set_built_in_table(destination.built_in_table());
}
*info.mutable_built_in_tables()
->at(built_in_table_name)
.add_incoming_references() = reference;

return absl::OkStatus();
}
case IrTable::TABLE_NOT_SET: {
return gutil::InternalErrorBuilder()
<< "Destination IrTable oneof not set." << reference.DebugString();
}
}
}

} // namespace

StatusOr<IrP4Info> CreateIrP4Info(const p4::config::v1::P4Info &p4_info) {
Expand Down Expand Up @@ -1488,6 +1592,13 @@ StatusOr<IrP4Info> CreateIrP4Info(const p4::config::v1::P4Info &p4_info) {
}
}

ASSIGN_OR_RETURN(std::vector<IrTableReference> references,
ParseIrTableReferences(info));
for (const auto &reference : references) {
RETURN_IF_ERROR(InsertOutgoingReferenceInfo(reference, info));
RETURN_IF_ERROR(InsertIncomingReferenceInfo(reference, info));
}

return info;
}

Expand Down
Loading

0 comments on commit cff1b38

Please sign in to comment.