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

allow json output format to be modified #4407

Merged
merged 3 commits into from
Feb 16, 2024
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
1 change: 1 addition & 0 deletions backends/p4tools/modules/testgen/targets/bmv2/bmv2.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "backends/p4tools/common/compiler/compiler_target.h"
#include "backends/p4tools/common/compiler/midend.h"
#include "backends/p4tools/common/lib/variables.h"
#include "control-plane/p4RuntimeSerializer.h"
#include "frontends/common/options.h"

#include "backends/p4tools/modules/testgen/core/compiler_target.h"
Expand Down
1 change: 1 addition & 0 deletions control-plane/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ set (CONTROLPLANE_HDRS
p4RuntimeArchStandard.h
p4RuntimeSerializer.h
p4RuntimeSymbolTable.h
p4RuntimeTypes.h
typeSpecConverter.h
bfruntime.h
)
Expand Down
9 changes: 9 additions & 0 deletions control-plane/p4RuntimeArchHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ limitations under the License.
#ifndef CONTROL_PLANE_P4RUNTIMEARCHHANDLER_H_
#define CONTROL_PLANE_P4RUNTIMEARCHHANDLER_H_

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wpedantic"
#include <google/protobuf/util/json_util.h>
#pragma GCC diagnostic pop

#include <optional>
#include <set>

Expand Down Expand Up @@ -184,6 +190,9 @@ class P4RuntimeArchHandlerIface {
const IR::ExternBlock *externBlock) = 0;
/// called when processing annotations via setPreamble
virtual bool filterAnnotations(cstring anno) = 0;

/// Control how JSON is output
virtual google::protobuf::util::JsonPrintOptions getJsonPrintOptions() = 0;
};

/// A functor interface that needs to be implemented for each
Expand Down
11 changes: 10 additions & 1 deletion control-plane/p4RuntimeArchStandard.h
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,9 @@ class P4RuntimeArchHandlerCommon : public P4RuntimeArchHandlerIface {

P4RuntimeArchHandlerCommon(ReferenceMap *refMap, TypeMap *typeMap,
const IR::ToplevelBlock *evaluatedProgram)
: refMap(refMap), typeMap(typeMap), evaluatedProgram(evaluatedProgram) {}
: refMap(refMap), typeMap(typeMap), evaluatedProgram(evaluatedProgram) {
jsonPrintOptions.add_whitespace = true;
}

void collectTableProperties(P4RuntimeSymbolTableIface *symbols,
const IR::TableBlock *tableBlock) override {
Expand Down Expand Up @@ -708,6 +710,10 @@ class P4RuntimeArchHandlerCommon : public P4RuntimeArchHandlerIface {
const IR::ExternBlock *) override {}
bool filterAnnotations(cstring) override { return false; }

google::protobuf::util::JsonPrintOptions getJsonPrintOptions() override {
return jsonPrintOptions;
}

static std::optional<ActionProfile> getActionProfile(cstring name, const IR::Type_Extern *type,
int64_t size,
const IR::IAnnotated *annotations) {
Expand Down Expand Up @@ -924,6 +930,9 @@ class P4RuntimeArchHandlerCommon : public P4RuntimeArchHandlerIface {

/// The extern instances we've serialized so far. Used for deduplication.
std::set<p4rt_id_t> serializedInstances;

// JSON printing options for serialization
google::protobuf::util::JsonPrintOptions jsonPrintOptions;
};

/// Implements a common @ref P4RuntimeArchHandlerIface for the PSA and PNA architecture. The
Expand Down
14 changes: 6 additions & 8 deletions control-plane/p4RuntimeSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ static std::optional<cstring> explicitNameAnnotation(const IR::IAnnotated *item)
namespace writers {

using google::protobuf::Message;
using google::protobuf::util::JsonPrintOptions;

/// Serialize the protobuf @message to @destination in the binary protocol
/// buffers format.
Expand All @@ -113,14 +114,11 @@ static bool writeTo(const Message &message, std::ostream *destination) {

/// Serialize the protobuf @message to @destination in the JSON protocol buffers
/// format. This is intended for debugging and testing.
static bool writeJsonTo(const Message &message, std::ostream *destination) {
static bool writeJsonTo(const Message &message, std::ostream *destination,
const JsonPrintOptions &options) {
using namespace google::protobuf::util;
CHECK_NULL(destination);

// Serialize the JSON in a human-readable format.
JsonPrintOptions options;
options.add_whitespace = true;

std::string output;
if (!MessageToJsonString(message, &output, options).ok()) {
::error(ErrorType::ERR_IO, "Failed to serialize protobuf message to JSON");
Expand Down Expand Up @@ -1413,7 +1411,7 @@ class P4RuntimeEntriesConverter {

auto *p4Info = analyzer.getP4Info();
auto *p4Entries = entriesConverter.getEntries();
return P4RuntimeAPI{p4Info, p4Entries};
return P4RuntimeAPI{p4Info, p4Entries, archHandler->getJsonPrintOptions()};
}

} // namespace ControlPlaneAPI
Expand Down Expand Up @@ -1464,7 +1462,7 @@ void P4RuntimeAPI::serializeP4InfoTo(std::ostream *destination, P4RuntimeFormat
success = writers::writeTo(*p4Info, destination);
break;
case P4RuntimeFormat::JSON:
success = writers::writeJsonTo(*p4Info, destination);
success = writers::writeJsonTo(*p4Info, destination, jsonPrintOptions);
break;
case P4RuntimeFormat::TEXT_PROTOBUF:
case P4RuntimeFormat::TEXT:
Expand All @@ -1484,7 +1482,7 @@ void P4RuntimeAPI::serializeEntriesTo(std::ostream *destination, P4RuntimeFormat
success = writers::writeTo(*entries, destination);
break;
case P4RuntimeFormat::JSON:
success = writers::writeJsonTo(*entries, destination);
success = writers::writeJsonTo(*entries, destination, jsonPrintOptions);
break;
case P4RuntimeFormat::TEXT_PROTOBUF:
case P4RuntimeFormat::TEXT:
Expand Down
22 changes: 19 additions & 3 deletions control-plane/p4RuntimeSerializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ limitations under the License.
#ifndef CONTROL_PLANE_P4RUNTIMESERIALIZER_H_
#define CONTROL_PLANE_P4RUNTIMESERIALIZER_H_

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wpedantic"
#include <google/protobuf/util/json_util.h>
#pragma GCC diagnostic pop

#include <iosfwd>
#include <unordered_map>

#include "lib/cstring.h"
#include "p4RuntimeTypes.h"

namespace p4 {
namespace config {
Expand All @@ -41,9 +48,6 @@ class CompilerOptions;

namespace P4 {

/// P4Runtime serialization formats.
enum class P4RuntimeFormat { BINARY, JSON, TEXT, TEXT_PROTOBUF };

/// A P4 program's control-plane API, represented in terms of P4Runtime's data
/// structures. Can be inspected or serialized.
struct P4RuntimeAPI {
Expand All @@ -60,6 +64,18 @@ struct P4RuntimeAPI {
/// All static table entries as one P4Runtime WriteRequest object. Never
/// null.
const ::p4::v1::WriteRequest *entries;

// Print options to use while outputting JSON.
google::protobuf::util::JsonPrintOptions jsonPrintOptions;

P4RuntimeAPI(const ::p4::config::v1::P4Info *p4Info, const ::p4::v1::WriteRequest *entries)
: p4Info(p4Info), entries(entries) {
jsonPrintOptions.add_whitespace = true;
}

P4RuntimeAPI(const ::p4::config::v1::P4Info *p4Info, const ::p4::v1::WriteRequest *entries,
google::protobuf::util::JsonPrintOptions jsonPrintOptions)
: p4Info(p4Info), entries(entries), jsonPrintOptions(jsonPrintOptions) {}
};

namespace ControlPlaneAPI {
Expand Down
27 changes: 27 additions & 0 deletions control-plane/p4RuntimeTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2024-present Intel Corporation

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.
*/

#ifndef CONTROL_PLANE_P4RUNTIMETYPES_H_
#define CONTROL_PLANE_P4RUNTIMETYPES_H_

namespace P4 {

/// P4Runtime serialization formats.
enum class P4RuntimeFormat { BINARY, JSON, TEXT, TEXT_PROTOBUF };

} // namespace P4

#endif /* CONTROL_PLANE_P4RUNTIMETYPES_H_ */
2 changes: 1 addition & 1 deletion frontends/common/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ limitations under the License.

#include "parser_options.h"
// for p4::P4RuntimeFormat definition
#include "control-plane/p4RuntimeSerializer.h"
#include "control-plane/p4RuntimeTypes.h"

class CompilerOptions : public ParserOptions {
protected:
Expand Down
53 changes: 53 additions & 0 deletions test/gtest/p4runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,59 @@ TEST_F(P4Runtime, Documentation) {
}
}

TEST_F(P4Runtime, JsonSerializationPrintOptions) {
auto test = createP4RuntimeTestCase(P4_SOURCE(P4Headers::V1MODEL, R"(
struct Headers { }
struct Metadata { }
parser parse(packet_in p, out Headers h, inout Metadata m,
inout standard_metadata_t sm) {
state start { transition accept; } }
control verifyChecksum(inout Headers h, inout Metadata m) { apply { } }
control egress(inout Headers h, inout Metadata m,
inout standard_metadata_t sm) { apply { } }
control computeChecksum(inout Headers h, inout Metadata m) { apply { } }
control deparse(packet_out p, in Headers h) { apply { } }

control ingress(inout Headers h, inout Metadata m,
inout standard_metadata_t sm) {
action noop() { }

action drop() { mark_to_drop(sm); }

table t {
key = { sm.ingress_port : exact; }
actions = { noop; drop; }
default_action = noop;
}

apply {
t.apply();
}
}

V1Switch(parse(), verifyChecksum(), ingress(), egress(),
computeChecksum(), deparse()) main;
)"));

ASSERT_TRUE(test);
EXPECT_EQ(0U, ::diagnosticCount());

{
// Default options: expect whitespace
std::ostringstream json_output;
test->serializeP4InfoTo(&json_output, P4::P4RuntimeFormat::JSON);
EXPECT_NE(json_output.str().find(' '), std::string::npos);
}

{
// Disable adding whitespace: json should not contain whitespace
std::ostringstream json_output;
test->jsonPrintOptions.add_whitespace = false;
test->serializeP4InfoTo(&json_output, P4::P4RuntimeFormat::JSON);
EXPECT_EQ(json_output.str().find(' '), std::string::npos);
}
}

class P4RuntimePkgInfo : public P4CTest {
protected:
static std::optional<P4::P4RuntimeAPI> createTestCase(const char *annotations);
Expand Down
Loading