Skip to content

Commit

Permalink
Fix control-plane generation for headers containing structs (p4lang#3331
Browse files Browse the repository at this point in the history
)

Signed-off-by: Mihai Budiu <mbudiu@vmware.com>
  • Loading branch information
Mihai Budiu authored and github-sajan committed May 26, 2022
1 parent 3c44be5 commit 03cdd04
Show file tree
Hide file tree
Showing 16 changed files with 349 additions and 39 deletions.
9 changes: 6 additions & 3 deletions control-plane/flattenHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace P4 {

namespace ControlPlaneAPI {

FlattenHeader::FlattenHeader(const P4::TypeMap* typeMap, IR::Type_Header* flattenedHeader)
FlattenHeader::FlattenHeader(P4::TypeMap* typeMap, IR::Type_Header* flattenedHeader)
: typeMap(typeMap), flattenedHeader(flattenedHeader) { }

void FlattenHeader::doFlatten(const IR::Type* type) {
Expand All @@ -42,7 +42,10 @@ void FlattenHeader::doFlatten(const IR::Type* type) {
auto annotations = mergeAnnotations();
annotations = annotations->addOrReplace(
IR::Annotation::nameAnnotation, new IR::StringLiteral(originalName));
newFields.push_back(new IR::StructField(IR::ID(newName), annotations, type));
auto field = new IR::StructField(IR::ID(newName), annotations, type);
newFields.push_back(field);
auto ftype = typeMap->getTypeType(type, true);
typeMap->setType(field, ftype);
}
}

Expand All @@ -69,7 +72,7 @@ const IR::Annotations* FlattenHeader::mergeAnnotations() const {

/* static */
const IR::Type_Header* FlattenHeader::flatten(
const P4::TypeMap* typeMap, const IR::Type_Header* headerType) {
P4::TypeMap* typeMap, const IR::Type_Header* headerType) {
auto flattenedHeader = headerType->clone();
flattenedHeader->fields.clear();
FlattenHeader flattener(typeMap, flattenedHeader);
Expand Down
6 changes: 3 additions & 3 deletions control-plane/flattenHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ namespace ControlPlaneAPI {
/// Flattens a header type "locally", without modifying the IR.
class FlattenHeader {
private:
const P4::TypeMap* typeMap;
P4::TypeMap* typeMap;
IR::Type_Header* flattenedHeader;
std::vector<cstring> nameSegments{};
std::vector<const IR::Annotations*> allAnnotations{};
bool needsFlattening{false};

FlattenHeader(const P4::TypeMap* typeMap, IR::Type_Header* flattenedHeader);
FlattenHeader(P4::TypeMap* typeMap, IR::Type_Header* flattenedHeader);

void doFlatten(const IR::Type* type);

Expand All @@ -48,7 +48,7 @@ class FlattenHeader {
/// a new flattened field list. Otherwise returns @headerType. This does not
/// modify the IR.
static const IR::Type_Header* flatten(
const P4::TypeMap* typeMap, const IR::Type_Header* headerType);
P4::TypeMap* typeMap, const IR::Type_Header* headerType);
};

} // namespace ControlPlaneAPI
Expand Down
2 changes: 1 addition & 1 deletion control-plane/p4RuntimeArchHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ struct Counterlike {
static boost::optional<Counterlike<Kind>>
from(const IR::ExternBlock* instance,
const ReferenceMap* refMap,
const P4::TypeMap* typeMap,
P4::TypeMap* typeMap,
::p4::config::v1::P4TypeInfo* p4RtTypeInfo) {
CHECK_NULL(instance);
auto declaration = instance->node->to<IR::Declaration_Instance>();
Expand Down
2 changes: 1 addition & 1 deletion control-plane/p4RuntimeArchStandard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class P4RuntimeArchHandlerV1Model final : public P4RuntimeArchHandlerCommon<Arch
static boost::optional<Digest>
getDigestCall(const P4::ExternFunction* function,
ReferenceMap* refMap,
const P4::TypeMap* typeMap,
P4::TypeMap* typeMap,
p4configv1::P4TypeInfo* p4RtTypeInfo) {
if (function->method->name != P4V1::V1Model::instance.digest_receiver.name)
return boost::none;
Expand Down
2 changes: 1 addition & 1 deletion control-plane/p4RuntimeArchStandard.h
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ struct Register {
static boost::optional<Register>
from(const IR::ExternBlock* instance,
const ReferenceMap* refMap,
const P4::TypeMap* typeMap,
P4::TypeMap* typeMap,
p4configv1::P4TypeInfo* p4RtTypeInfo) {
CHECK_NULL(instance);
auto declaration = instance->node->to<IR::Declaration_Instance>();
Expand Down
6 changes: 3 additions & 3 deletions control-plane/typeSpecConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ bool hasTranslationAnnotation(const IR::Type* type,
return false;
}

cstring getTypeName(const IR::Type* type, const TypeMap* typeMap) {
cstring getTypeName(const IR::Type* type, TypeMap* typeMap) {
CHECK_NULL(type);

auto t = typeMap->getTypeType(type, true);
Expand All @@ -93,7 +93,7 @@ cstring getTypeName(const IR::Type* type, const TypeMap* typeMap) {
}

TypeSpecConverter::TypeSpecConverter(
const P4::ReferenceMap* refMap, const P4::TypeMap* typeMap, P4TypeInfo* p4RtTypeInfo)
const P4::ReferenceMap* refMap, P4::TypeMap* typeMap, P4TypeInfo* p4RtTypeInfo)
: refMap(refMap), typeMap(typeMap), p4RtTypeInfo(p4RtTypeInfo) {
CHECK_NULL(refMap);
CHECK_NULL(typeMap);
Expand Down Expand Up @@ -388,7 +388,7 @@ bool TypeSpecConverter::preorder(const IR::Type_Error* type) {

const P4DataTypeSpec* TypeSpecConverter::convert(
const P4::ReferenceMap* refMap,
const P4::TypeMap* typeMap,
P4::TypeMap* typeMap,
const IR::Type* type, P4TypeInfo* typeInfo) {
TypeSpecConverter typeSpecConverter(refMap, typeMap, typeInfo);
type->apply(typeSpecConverter);
Expand Down
8 changes: 4 additions & 4 deletions control-plane/typeSpecConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace ControlPlaneAPI {
class TypeSpecConverter : public Inspector {
private:
const P4::ReferenceMap* refMap;
const P4::TypeMap* typeMap;
P4::TypeMap* typeMap;
/// type_info field of the P4Info message: includes information about P4
/// named types (struct, header, header union, enum, error).
::p4::config::v1::P4TypeInfo* p4RtTypeInfo;
Expand All @@ -52,7 +52,7 @@ class TypeSpecConverter : public Inspector {
std::map<const IR::Type*, ::p4::config::v1::P4DataTypeSpec*> map;

TypeSpecConverter(const P4::ReferenceMap* refMap,
const P4::TypeMap* typeMap,
P4::TypeMap* typeMap,
::p4::config::v1::P4TypeInfo* p4RtTypeInfo);

// fallback for unsupported types, should be unreachable
Expand Down Expand Up @@ -82,7 +82,7 @@ class TypeSpecConverter : public Inspector {
/// @typeInfo is nullptr, then the relevant information is not generated for
/// named types.
static const ::p4::config::v1::P4DataTypeSpec* convert(
const P4::ReferenceMap* refMap, const P4::TypeMap* typeMap,
const P4::ReferenceMap* refMap, P4::TypeMap* typeMap,
const IR::Type* type, ::p4::config::v1::P4TypeInfo* typeInfo);
};

Expand All @@ -107,7 +107,7 @@ bool hasTranslationAnnotation(const IR::Type* type,

/// getTypeName returns a cstring for use as type_name for a Type_Newtype. It
/// returns nullptr if @type is not a Type_Newtype.
cstring getTypeName(const IR::Type* type, const TypeMap* typeMap);
cstring getTypeName(const IR::Type* type, TypeMap* typeMap);

} // namespace ControlPlaneAPI

Expand Down
81 changes: 81 additions & 0 deletions testdata/p4_16_samples/issue3329-bmv2.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
Copyright 2019 Cisco Systems, 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 s {
bit<32> b;
}

@controller_header("packet_out")
header packet_out_header_t {
s b;
}

struct headers_t {
packet_out_header_t packet_out;
}

struct metadata_t {
}

parser ParserImpl(packet_in packet,
out headers_t hdr,
inout metadata_t meta,
inout standard_metadata_t stdmeta)
{
state start {
transition accept;
}
}

control ingress(inout headers_t hdr,
inout metadata_t meta,
inout standard_metadata_t stdmeta)
{
apply {
}
}

control egress(inout headers_t hdr,
inout metadata_t meta,
inout standard_metadata_t stdmeta)
{
apply { }
}

control DeparserImpl(packet_out packet, in headers_t hdr) {
apply {
}
}

control verifyChecksum(inout headers_t hdr, inout metadata_t meta) {
apply {
}
}

control computeChecksum(inout headers_t hdr, inout metadata_t meta) {
apply {
}
}

V1Switch(ParserImpl(),
verifyChecksum(),
ingress(),
egress(),
computeChecksum(),
DeparserImpl()) main;
45 changes: 22 additions & 23 deletions testdata/p4_16_samples/v1model-p4runtime-most-types1.p4
Original file line number Diff line number Diff line change
Expand Up @@ -354,34 +354,34 @@ parser ParserImpl(packet_in packet,
, hdr.packet_out.addr2
#endif // PROBLEM_VALUE_SET_FIELD_TYPE
, hdr.packet_out.e

, hdr.packet_out.e0
#ifdef PROBLEM_VALUE_SET_FIELD_TYPE
, hdr.packet_out.e1
, hdr.packet_out.e2
#endif // PROBLEM_VALUE_SET_FIELD_TYPE

, hdr.packet_out.e00
#ifdef PROBLEM_VALUE_SET_FIELD_TYPE
, hdr.packet_out.e01
, hdr.packet_out.e02

, hdr.packet_out.e10
, hdr.packet_out.e11
, hdr.packet_out.e12

, hdr.packet_out.e20
, hdr.packet_out.e21
, hdr.packet_out.e22

, hdr.packet_out.e001
, hdr.packet_out.e002
, hdr.packet_out.e101
, hdr.packet_out.e102
, hdr.packet_out.e201
, hdr.packet_out.e202
, hdr.packet_out.e220

, hdr.packet_out.e0020010
, hdr.packet_out.e0020020
#endif // PROBLEM_VALUE_SET_FIELD_TYPE
Expand Down Expand Up @@ -443,7 +443,7 @@ control ingress(inout headers_t hdr,
Custom201_t e201,
Custom202_t e202,
Custom220_t e220,

Custom0020010_t e0020010,
Custom0020020_t e0020020,

Expand Down Expand Up @@ -491,23 +491,23 @@ control ingress(inout headers_t hdr,
hdr.custom.addr1 : exact;
hdr.custom.addr2 : exact;
hdr.custom.e : exact;

hdr.custom.e0 : exact;
hdr.custom.e1 : exact;
hdr.custom.e2 : exact;

hdr.custom.e00 : exact;
hdr.custom.e01 : exact;
hdr.custom.e02 : exact;

hdr.custom.e10 : exact;
hdr.custom.e11 : exact;
hdr.custom.e12 : exact;

hdr.custom.e20 : exact;
hdr.custom.e21 : exact;
hdr.custom.e22 : exact;

hdr.custom.e001 : exact;
hdr.custom.e002 : exact;
hdr.custom.e101 : exact;
Expand Down Expand Up @@ -558,23 +558,23 @@ control verifyChecksum(inout headers_t hdr, inout metadata_t meta) {
hdr.custom.addr1,
hdr.custom.addr2,
hdr.custom.e,

hdr.custom.e0,
hdr.custom.e1,
hdr.custom.e2,

hdr.custom.e00,
hdr.custom.e01,
hdr.custom.e02,

hdr.custom.e10,
hdr.custom.e11,
hdr.custom.e12,

hdr.custom.e20,
hdr.custom.e21,
hdr.custom.e22,

hdr.custom.e001,
hdr.custom.e002,
hdr.custom.e101,
Expand All @@ -600,23 +600,23 @@ control computeChecksum(inout headers_t hdr, inout metadata_t meta) {
hdr.custom.addr1,
hdr.custom.addr2,
hdr.custom.e,

hdr.custom.e0,
hdr.custom.e1,
hdr.custom.e2,

hdr.custom.e00,
hdr.custom.e01,
hdr.custom.e02,

hdr.custom.e10,
hdr.custom.e11,
hdr.custom.e12,

hdr.custom.e20,
hdr.custom.e21,
hdr.custom.e22,

hdr.custom.e001,
hdr.custom.e002,
hdr.custom.e101,
Expand All @@ -640,4 +640,3 @@ V1Switch(ParserImpl(),
egress(),
computeChecksum(),
DeparserImpl()) main;

Loading

0 comments on commit 03cdd04

Please sign in to comment.