Skip to content

Commit

Permalink
Merge branch 'main' into symbolic_import_branch_97
Browse files Browse the repository at this point in the history
  • Loading branch information
kishanps authored Nov 19, 2024
2 parents 5fb2724 + 2bc31b5 commit a7ad365
Show file tree
Hide file tree
Showing 71 changed files with 3,567 additions and 1,996 deletions.
96 changes: 96 additions & 0 deletions dvaas/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ package(
licenses = ["notice"],
)

cc_library(
name = "validation_result",
srcs = ["validation_result.cc"],
hdrs = ["validation_result.h"],
deps = [
":test_run_validation",
":test_vector_cc_proto",
":test_vector_stats",
"//gutil:status",
"@com_github_google_glog//:glog",
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/status",
"@com_google_absl//absl/types:span",
"@com_google_protobuf//:protobuf",
],
)

cc_library(
name = "output_writer",
hdrs = ["output_writer.h"],
Expand Down Expand Up @@ -139,6 +157,43 @@ cc_library(
],
)

cc_library(
name = "test_vector_stats",
srcs = ["test_vector_stats.cc"],
hdrs = ["test_vector_stats.h"],
deps = [
":test_vector_cc_proto",
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
],
)

# go/golden-test-with-coverage
cc_test(
name = "test_vector_stats_test",
srcs = ["test_vector_stats_test.cc"],
linkstatic = True,
deps = [
":test_vector_cc_proto",
":test_vector_stats",
"//gutil:testing",
"@com_google_googletest//:gtest_main",
],
)

cmd_diff_test(
name = "test_vector_stats_diff_test",
actual_cmd = " | ".join([
"$(execpath :test_vector_stats_test)",
# Strip unnecessary lines for golden testing.
"sed '1,/^\\[ RUN/d'", # Strip everything up to a line beginning with '[ RUN'.
"sed '/^\\[/d'", # Strip every line beginning with '['.
]),
expected = "test_vector_stats_test.expected",
tools = [":test_vector_stats_test"],
)

cc_library(
name = "mirror_testbed_config",
testonly = True,
Expand All @@ -160,3 +215,44 @@ cc_library(
hdrs = ["switch_api.h"],
deps = ["//p4_pdpi:p4_runtime_session"],
)

cc_library(
name = "port_id_map",
srcs = ["port_id_map.cc"],
hdrs = ["port_id_map.h"],
deps = [
"//gutil:status",
"//lib/p4rt:p4rt_port",
"@com_github_gnmi//proto/gnmi:gnmi_cc_grpc_proto",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
],
)

cc_test(
name = "port_id_map_test",
srcs = ["port_id_map_test.cc"],
deps = [
":port_id_map",
"//gutil:status_matchers",
"//lib/p4rt:p4rt_port",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/status",
"@com_google_googletest//:gtest_main",
],
)


cc_test(
name = "test_vector_test",
srcs = ["test_vector_test.cc"],
deps = [
":test_vector",
"//gutil:status_matchers",
"//p4_pdpi/packetlib:packetlib_cc_proto",
"@com_google_googletest//:gtest_main",
],
)
85 changes: 85 additions & 0 deletions dvaas/port_id_map.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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 "dvaas/port_id_map.h"

#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/substitute.h"
#include "gutil/status.h"
#include "lib/p4rt/p4rt_port.h"

namespace dvaas {

using pins_test::P4rtPortId;

absl::StatusOr<MirrorTestbedP4rtPortIdMap>
MirrorTestbedP4rtPortIdMap::CreateFromSutToControlSwitchPortMap(
absl::flat_hash_map<P4rtPortId, pins_test::P4rtPortId>
sut_to_control_port_map) {
absl::flat_hash_map<pins_test::P4rtPortId, pins_test::P4rtPortId>
control_to_sut_port_map;
for (const auto& [sut_port, control_port] : sut_to_control_port_map) {
if (control_to_sut_port_map.contains(control_port)) {
return gutil::InvalidArgumentErrorBuilder() << absl::Substitute(
"Both SUT P4RT port IDs '$0' and '$1' are mapped to control "
"switch P4RT port ID '$2'",
sut_port, control_to_sut_port_map[control_port], control_port);
}
control_to_sut_port_map[control_port] = sut_port;
}

return MirrorTestbedP4rtPortIdMap(control_to_sut_port_map);
}

absl::StatusOr<MirrorTestbedP4rtPortIdMap>
MirrorTestbedP4rtPortIdMap::CreateFromControlSwitchToSutPortMap(
absl::flat_hash_map<pins_test::P4rtPortId, pins_test::P4rtPortId>
control_to_sut_port_map) {
absl::flat_hash_map<pins_test::P4rtPortId, pins_test::P4rtPortId>
sut_to_control_port_map;
for (const auto& [control_port, sut_port] : control_to_sut_port_map) {
if (sut_to_control_port_map.contains(sut_port)) {
return gutil::InvalidArgumentErrorBuilder() << absl::Substitute(
"Both control switch P4RT port IDs '$0' and '$1' are mapped "
"to SUT P4RT port ID '$2'",
control_port, sut_to_control_port_map[sut_port], sut_port);
}
sut_to_control_port_map[sut_port] = control_port;
}

return MirrorTestbedP4rtPortIdMap(control_to_sut_port_map);
}

absl::StatusOr<pins_test::P4rtPortId>
MirrorTestbedP4rtPortIdMap::GetSutPortConnectedToControlSwitchPort(
const pins_test::P4rtPortId& control_port) const {
// Handle implicit identity map.
if (!control_to_sut_port_map_.has_value()) return control_port;

// Handle explicit map.
const auto it = control_to_sut_port_map_->find(control_port);
if (it == control_to_sut_port_map_->end()) {
return absl::NotFoundError(
absl::Substitute("Control port '$0' was not found in control switch "
"to SUT P4RT port ID map.",
control_port));
} else {
return it->second;
}
}

} // namespace dvaas
83 changes: 83 additions & 0 deletions dvaas/port_id_map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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.

#ifndef PINS_DVAAS_PORT_ID_MAP_H_
#define PINS_DVAAS_PORT_ID_MAP_H_

#include <optional>
#include <utility>

#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "lib/p4rt/p4rt_port.h"
#include "proto/gnmi/gnmi.grpc.pb.h"

namespace dvaas {

// Keeps a map between the P4RT port IDs of connected interfaces between the
// control switch and the SUT in a mirror testbed.
class MirrorTestbedP4rtPortIdMap {
public:
// Creates a port mapping from SUT -> control switch port map.
static absl::StatusOr<MirrorTestbedP4rtPortIdMap>
CreateFromSutToControlSwitchPortMap(
absl::flat_hash_map<pins_test::P4rtPortId, pins_test::P4rtPortId>
sut_to_control_port_map);

// Creates a port mapping from control switch -> SUT port map.
static absl::StatusOr<MirrorTestbedP4rtPortIdMap>
CreateFromControlSwitchToSutPortMap(
absl::flat_hash_map<pins_test::P4rtPortId, pins_test::P4rtPortId>
control_to_sut_port_map);

// Creates an implicit map in which any port ID is mapped to itself.
static MirrorTestbedP4rtPortIdMap CreateIdentityMap() {
return MirrorTestbedP4rtPortIdMap();
}

// Creates a map in which the P4RT port IDs of interfaces of SUT and control
// switch with the same OpenConfig interface name are mapped to each other.
static absl::StatusOr<MirrorTestbedP4rtPortIdMap>
CreateFromMatchingInterfaceNames(gnmi::gNMI::StubInterface& sut,
gnmi::gNMI::StubInterface& control_switch) {
// TODO: Implement inferring port ID mapping from mirror
// testbed interface names.
return absl::UnimplementedError(
"Inferring port ID mapping from mirror testbed interface names is not "
"supported yet.");
}

// Returns the P4RT port ID of the SUT interface connected to the interface on
// the control switch with the given P4RT port ID according to the port
// mapping.
absl::StatusOr<pins_test::P4rtPortId> GetSutPortConnectedToControlSwitchPort(
const pins_test::P4rtPortId& control_port) const;

private:
MirrorTestbedP4rtPortIdMap(
absl::flat_hash_map<pins_test::P4rtPortId, pins_test::P4rtPortId>
control_to_sut_port_map)
: control_to_sut_port_map_(std::move(control_to_sut_port_map)) {}
MirrorTestbedP4rtPortIdMap() = default;

// If nullopt, an implicit identity map is assumed.
std::optional<
absl::flat_hash_map<pins_test::P4rtPortId, pins_test::P4rtPortId>>
control_to_sut_port_map_;
};

} // namespace dvaas

#endif // PINS_DVAAS_PORT_ID_MAP_H_
Loading

0 comments on commit a7ad365

Please sign in to comment.