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

[Dvaas]: Make DVaaS API take P4RT port ID mapping between SUT and control switch for non-standard mirror testbeds. #714

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions dvaas/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,32 @@ 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",
],
)
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_
166 changes: 166 additions & 0 deletions dvaas/port_id_map_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// 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/status/status.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "gutil/status_matchers.h"
#include "lib/p4rt/p4rt_port.h"

namespace dvaas {
namespace {

using gutil::IsOkAndHolds;
using gutil::StatusIs;
using pins_test::P4rtPortId;
using testing::Eq;

TEST(MirrorTestbedP4rtPortIdMap,
ReturnsErrorWhenPortNotFoundGivenControlToSutMapping) {
ASSERT_OK_AND_ASSIGN(const auto port_1,
P4rtPortId::MakeFromP4rtEncoding("1"));
ASSERT_OK_AND_ASSIGN(const auto port_2,
P4rtPortId::MakeFromP4rtEncoding("2"));
ASSERT_OK_AND_ASSIGN(const auto port_3,
P4rtPortId::MakeFromP4rtEncoding("3"));

ASSERT_OK_AND_ASSIGN(
const auto port_id_map,
MirrorTestbedP4rtPortIdMap::CreateFromControlSwitchToSutPortMap(
{{port_1, port_2}}));

ASSERT_THAT(port_id_map.GetSutPortConnectedToControlSwitchPort(port_2),
StatusIs(absl::StatusCode::kNotFound));
ASSERT_THAT(port_id_map.GetSutPortConnectedToControlSwitchPort(port_3),
StatusIs(absl::StatusCode::kNotFound));
}

TEST(MirrorTestbedP4rtPortIdMap,
ReturnsErrorWhenGivenControlToSutMappingIsNotBijective) {
ASSERT_OK_AND_ASSIGN(const auto port_1,
P4rtPortId::MakeFromP4rtEncoding("1"));
ASSERT_OK_AND_ASSIGN(const auto port_2,
P4rtPortId::MakeFromP4rtEncoding("2"));
ASSERT_OK_AND_ASSIGN(const auto port_3,
P4rtPortId::MakeFromP4rtEncoding("3"));

ASSERT_THAT(MirrorTestbedP4rtPortIdMap::CreateFromControlSwitchToSutPortMap({
{port_1, port_3},
{port_2, port_3},
}),
StatusIs(absl::StatusCode::kInvalidArgument));
}

TEST(MirrorTestbedP4rtPortIdMap,
ReturnsErrorWhenPortNotFoundGivenSutToControlMapping) {
ASSERT_OK_AND_ASSIGN(const auto port_1,
P4rtPortId::MakeFromP4rtEncoding("1"));
ASSERT_OK_AND_ASSIGN(const auto port_2,
P4rtPortId::MakeFromP4rtEncoding("2"));
ASSERT_OK_AND_ASSIGN(const auto port_3,
P4rtPortId::MakeFromP4rtEncoding("3"));

ASSERT_OK_AND_ASSIGN(
const auto port_id_map,
MirrorTestbedP4rtPortIdMap::CreateFromSutToControlSwitchPortMap(
{{port_1, port_2}}));

ASSERT_THAT(port_id_map.GetSutPortConnectedToControlSwitchPort(port_1),
StatusIs(absl::StatusCode::kNotFound));
ASSERT_THAT(port_id_map.GetSutPortConnectedToControlSwitchPort(port_3),
StatusIs(absl::StatusCode::kNotFound));
}

TEST(MirrorTestbedP4rtPortIdMap,
ReturnsErrorWhenGivenSutToControlMappingIsNotBijective) {
ASSERT_OK_AND_ASSIGN(const auto port_1,
P4rtPortId::MakeFromP4rtEncoding("1"));
ASSERT_OK_AND_ASSIGN(const auto port_2,
P4rtPortId::MakeFromP4rtEncoding("2"));
ASSERT_OK_AND_ASSIGN(const auto port_3,
P4rtPortId::MakeFromP4rtEncoding("3"));

ASSERT_THAT(MirrorTestbedP4rtPortIdMap::CreateFromSutToControlSwitchPortMap({
{port_1, port_3},
{port_2, port_3},
}),
StatusIs(absl::StatusCode::kInvalidArgument));
}

TEST(MirrorTestbedP4rtPortIdMap,
RetrunsCorrectSutPortGivenControlPortGivenControlToSutMapping) {
ASSERT_OK_AND_ASSIGN(const auto port_1,
P4rtPortId::MakeFromP4rtEncoding("1"));
ASSERT_OK_AND_ASSIGN(const auto port_2,
P4rtPortId::MakeFromP4rtEncoding("2"));
ASSERT_OK_AND_ASSIGN(const auto port_3,
P4rtPortId::MakeFromP4rtEncoding("3"));
ASSERT_OK_AND_ASSIGN(const auto port_4,
P4rtPortId::MakeFromP4rtEncoding("4"));

ASSERT_OK_AND_ASSIGN(
const auto port_id_map,
MirrorTestbedP4rtPortIdMap::CreateFromControlSwitchToSutPortMap({
{port_1, port_2},
{port_3, port_4},
}));

ASSERT_THAT(port_id_map.GetSutPortConnectedToControlSwitchPort(port_1),
IsOkAndHolds(Eq(port_2)));
ASSERT_THAT(port_id_map.GetSutPortConnectedToControlSwitchPort(port_3),
IsOkAndHolds(Eq(port_4)));
}

TEST(MirrorTestbedP4rtPortIdMap,
RetrunsCorrectSutPortGivenControlPortGivenSutToControlMapping) {
ASSERT_OK_AND_ASSIGN(const auto port_1,
P4rtPortId::MakeFromP4rtEncoding("1"));
ASSERT_OK_AND_ASSIGN(const auto port_2,
P4rtPortId::MakeFromP4rtEncoding("2"));
ASSERT_OK_AND_ASSIGN(const auto port_3,
P4rtPortId::MakeFromP4rtEncoding("3"));
ASSERT_OK_AND_ASSIGN(const auto port_4,
P4rtPortId::MakeFromP4rtEncoding("4"));

ASSERT_OK_AND_ASSIGN(
const auto port_id_map,
MirrorTestbedP4rtPortIdMap::CreateFromSutToControlSwitchPortMap({
{port_1, port_2},
{port_3, port_4},
}));

ASSERT_THAT(port_id_map.GetSutPortConnectedToControlSwitchPort(port_2),
IsOkAndHolds(Eq(port_1)));
ASSERT_THAT(port_id_map.GetSutPortConnectedToControlSwitchPort(port_4),
IsOkAndHolds(Eq(port_3)));
}

TEST(MirrorTestbedP4rtPortIdMap, RetrunsCorrectSutPortWithImplicitIdentityMap) {
const auto port_id_map = MirrorTestbedP4rtPortIdMap::CreateIdentityMap();
ASSERT_OK_AND_ASSIGN(const auto port_1,
P4rtPortId::MakeFromP4rtEncoding("1"));
ASSERT_OK_AND_ASSIGN(const auto port_2,
P4rtPortId::MakeFromP4rtEncoding("2"));

ASSERT_THAT(port_id_map.GetSutPortConnectedToControlSwitchPort(port_1),
IsOkAndHolds(Eq(port_1)));
ASSERT_THAT(port_id_map.GetSutPortConnectedToControlSwitchPort(port_2),
IsOkAndHolds(Eq(port_2)));
}

} // namespace
} // namespace dvaas
Loading