Skip to content

Commit

Permalink
[P4_Symbolic] Move IR parser to ir/. Simplify IR parser API. Remove…
Browse files Browse the repository at this point in the history
… unused IR pdpi_driver. Remove hard-coded SAI parser. Remove EvaluateSaiPipeline. Refactor SAI-specific code. (#917)



Co-authored-by: kishanps <kishanps@google.com>
  • Loading branch information
VSuryaprasad-HCL and kishanps authored Jan 4, 2025
1 parent e0c7281 commit ae9158f
Show file tree
Hide file tree
Showing 28 changed files with 313 additions and 613 deletions.
39 changes: 16 additions & 23 deletions p4_symbolic/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ cc_binary(
"main.cc",
],
deps = [
":parser",
":test_util",
"//gutil:io",
"//gutil:status",
"//p4_symbolic/bmv2",
"//p4_symbolic/sai:parser",
"//p4_symbolic/symbolic",
"@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/flags:parse",
"@com_google_absl//absl/flags:usage",
Expand All @@ -39,24 +37,6 @@ cc_binary(
],
)

cc_library(
name = "parser",
srcs = ["parser.cc"],
hdrs = ["parser.h"],
deps = [
"//gutil:io",
"//gutil:proto",
"//p4_pdpi:ir",
"//p4_pdpi:ir_cc_proto",
"//p4_symbolic/bmv2",
"//p4_symbolic/ir",
"//p4_symbolic/ir:ir_cc_proto",
"//p4_symbolic/ir:pdpi_driver",
"@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
"@com_google_absl//absl/types:span",
],
)

cc_library(
name = "z3_util",
srcs = ["z3_util.cc"],
Expand Down Expand Up @@ -88,6 +68,20 @@ cc_library(
],
)

cc_library(
name = "test_util",
testonly = False,
srcs = ["test_util.cc"],
hdrs = ["test_util.h"],
deps = [
"//gutil:io",
"//gutil:proto",
"@com_github_p4lang_p4runtime//:p4info_cc_proto",
"@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
"@com_google_absl//absl/status:statusor",
],
)

cc_test(
name = "deparser_test",
srcs = ["deparser_test.cc"],
Expand All @@ -97,7 +91,6 @@ cc_test(
],
deps = [
":deparser",
":parser",
":z3_util",
"//gutil:io",
"//gutil:proto",
Expand Down
6 changes: 2 additions & 4 deletions p4_symbolic/deparser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "p4/v1/p4runtime.pb.h"
#include "p4_pdpi/packetlib/packetlib.h"
#include "p4_pdpi/packetlib/packetlib.pb.h"
#include "p4_symbolic/parser.h"
#include "p4_symbolic/sai/fields.h"
#include "p4_symbolic/symbolic/symbolic.h"
#include "p4_symbolic/z3_util.h"
Expand All @@ -49,9 +48,8 @@ class SaiDeparserTest : public testing::Test {
gutil::ReadFile(std::string(p4_config_path)));

Z3Context(/*renew=*/true);
ASSERT_OK_AND_ASSIGN(ir::Dataplane dataplane,
ParseToIr(config, /*table_entries=*/{}));
ASSERT_OK_AND_ASSIGN(state_, symbolic::EvaluateP4Program(dataplane));
ASSERT_OK_AND_ASSIGN(state_,
symbolic::EvaluateP4Program(config, /*entries=*/{}));
}

protected:
Expand Down
34 changes: 17 additions & 17 deletions p4_symbolic/ir/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,6 @@ proto_library(
],
)

cc_library(
name = "pdpi_driver",
srcs = [
"pdpi_driver.cc",
],
hdrs = [
"pdpi_driver.h",
],
deps = [
"//gutil:proto",
"//gutil:status",
"//p4_pdpi:ir",
"//p4_pdpi:ir_cc_proto",
],
)

cc_library(
name = "table_entries",
srcs = [
Expand Down Expand Up @@ -116,6 +100,21 @@ cc_library(
],
)

cc_library(
name = "parser",
srcs = ["parser.cc"],
hdrs = ["parser.h"],
deps = [
":ir",
"//p4_pdpi:ir",
"//p4_pdpi:ir_cc_proto",
"//p4_symbolic/bmv2",
"@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/types:span",
],
)

cc_test(
name = "cfg_test",
srcs = ["cfg_test.cc"],
Expand All @@ -138,9 +137,10 @@ cc_binary(
"test.cc",
],
deps = [
":parser",
"//gutil:io",
"//gutil:proto",
"//p4_symbolic:parser",
"//p4_symbolic:test_util",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/flags:parse",
"@com_google_absl//absl/flags:usage",
Expand Down
39 changes: 39 additions & 0 deletions p4_symbolic/ir/parser.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2024 Google LLC
//
// 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 "p4_symbolic/ir/parser.h"

#include "p4/v1/p4runtime.pb.h"
#include "p4_pdpi/ir.h"
#include "p4_pdpi/ir.pb.h"
#include "p4_symbolic/bmv2/bmv2.h"
#include "p4_symbolic/ir/ir.h"

namespace p4_symbolic::ir {

absl::StatusOr<Dataplane> ParseToIr(
const p4::v1::ForwardingPipelineConfig &config,
absl::Span<const p4::v1::TableEntry> table_entries) {
ASSIGN_OR_RETURN(bmv2::P4Program bmv2,
bmv2::ParseBmv2JsonString(config.p4_device_config()),
_.SetPrepend() << "While trying to parse BMv2 JSON: ");
ASSIGN_OR_RETURN(const pdpi::IrP4Info ir_p4info,
pdpi::CreateIrP4Info(config.p4info()));
ASSIGN_OR_RETURN(P4Program program, Bmv2AndP4infoToIr(bmv2, ir_p4info));
ASSIGN_OR_RETURN(TableEntries ir_table_entries,
ParseTableEntries(ir_p4info, table_entries));
return Dataplane{program, ir_table_entries};
}

} // namespace p4_symbolic::ir
26 changes: 6 additions & 20 deletions p4_symbolic/parser.h → p4_symbolic/ir/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,20 @@
// This file defines the main API entry point for parsing input files
// into our IR representation.

#ifndef P4_SYMBOLIC_PARSER_H_
#define P4_SYMBOLIC_PARSER_H_

#include <string>
#ifndef P4_SYMBOLIC_IR_PARSER_H_
#define P4_SYMBOLIC_IR_PARSER_H_

#include "absl/status/statusor.h"
#include "absl/types/span.h"
#include "p4/v1/p4runtime.pb.h"
#include "p4_pdpi/ir.pb.h"
#include "p4_symbolic/ir/ir.h"

namespace p4_symbolic {

absl::StatusOr<ir::Dataplane> ParseToIr(const std::string &bmv2_json_path,
const std::string &p4info_path,
const std::string &table_entries_path);

absl::StatusOr<ir::Dataplane> ParseToIr(
const std::string &bmv2_json_path, const std::string &p4info_path,
absl::Span<const p4::v1::TableEntry> table_entries);

absl::StatusOr<ir::Dataplane> ParseToIr(
const std::string &bmv2_json, const pdpi::IrP4Info &p4info,
absl::Span<const p4::v1::TableEntry> table_entries);
namespace p4_symbolic::ir {

absl::StatusOr<ir::Dataplane> ParseToIr(
absl::StatusOr<Dataplane> ParseToIr(
const p4::v1::ForwardingPipelineConfig &config,
absl::Span<const p4::v1::TableEntry> table_entries);

} // namespace p4_symbolic
} // namespace p4_symbolic::ir

#endif // P4_SYMBOLIC_PARSER_H_
32 changes: 0 additions & 32 deletions p4_symbolic/ir/pdpi_driver.cc

This file was deleted.

35 changes: 0 additions & 35 deletions p4_symbolic/ir/pdpi_driver.h

This file was deleted.

11 changes: 8 additions & 3 deletions p4_symbolic/ir/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
#include "absl/strings/str_format.h"
#include "gutil/io.h"
#include "gutil/proto.h"
#include "p4_symbolic/parser.h"
#include "p4_symbolic/ir/parser.h"
#include "p4_symbolic/test_util.h"

ABSL_FLAG(std::string, p4info, "",
"The path to the p4info protobuf file (required)");
Expand All @@ -51,8 +52,12 @@ absl::Status Test() {

// Transform to IR and print.
ASSIGN_OR_RETURN(
p4_symbolic::ir::Dataplane dataplane,
p4_symbolic::ParseToIr(bmv2_path, p4info_path, entries_path));
p4::v1::ForwardingPipelineConfig config,
p4_symbolic::ParseToForwardingPipelineConfig(bmv2_path, p4info_path));
ASSIGN_OR_RETURN(std::vector<p4::v1::TableEntry> table_entries,
p4_symbolic::ParseToPiTableEntries(entries_path));
ASSIGN_OR_RETURN(p4_symbolic::ir::Dataplane dataplane,
p4_symbolic::ir::ParseToIr(config, table_entries));

// Dump string representation to the output file.
std::ostringstream output;
Expand Down
Loading

0 comments on commit ae9158f

Please sign in to comment.