Skip to content

Commit

Permalink
Added helper to save P4 flow snapshots Fields such as counter_data
Browse files Browse the repository at this point in the history
…and `meter_counter_data` are expected to change. Added them to exception while comparing the snapshots.Add helper to stop the OTG trafficAdd support for NSF ACL flow coverage test. The goal of the test is to make sure the ACL flows installed prior to NSF reboot, converge after the NSF reboot.
  • Loading branch information
kishanps authored and VSuryaprasad-HCL committed Feb 12, 2025
1 parent e99497e commit af0fad3
Show file tree
Hide file tree
Showing 7 changed files with 372 additions and 36 deletions.
30 changes: 30 additions & 0 deletions tests/integration/system/nsf/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,33 @@ cc_library(
"@com_google_absl//absl/strings:string_view",
],
)

cc_library(
name = "nsf_acl_flow_coverage_test",
testonly = True,
srcs = ["nsf_acl_flow_coverage_test.cc"],
hdrs = ["nsf_acl_flow_coverage_test.h"],
deps = [
":util",
"//gutil:status_matchers",
"//gutil:testing",
"//p4_pdpi:ir",
"//p4_pdpi:p4_runtime_session",
"//p4_pdpi:pd",
"//sai_p4/instantiations/google:sai_pd_cc_proto",
"//tests/integration/system/nsf/interfaces:component_validator",
"//tests/integration/system/nsf/interfaces:flow_programmer",
"//tests/integration/system/nsf/interfaces:test_params",
"//tests/integration/system/nsf/interfaces:testbed",
"//tests/integration/system/nsf/interfaces:traffic_helper",
"//thinkit:ssh_client",
"//thinkit:switch",
"@com_github_google_glog//:glog",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/time",
"@com_google_googletest//:gtest_main",
],
alwayslink = True,
)
202 changes: 202 additions & 0 deletions tests/integration/system/nsf/nsf_acl_flow_coverage_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
// Copyright 2025 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 "tests/integration/system/nsf/nsf_acl_flow_coverage_test.h"

#include <memory>
#include <vector>

#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "glog/logging.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "gutil/status.h"
#include "gutil/status_matchers.h" // NOLINT: Need to add status_matchers.h for using `ASSERT_OK` in upstream code.
#include "gutil/testing.h"
#include "p4_pdpi/ir.h"
#include "p4_pdpi/p4_runtime_session.h"
#include "p4_pdpi/pd.h"
#include "sai_p4/instantiations/google/sai_pd.pb.h"
#include "tests/integration/system/nsf/interfaces/test_params.h"
#include "tests/integration/system/nsf/interfaces/testbed.h"
#include "tests/integration/system/nsf/util.h"
#include "thinkit/switch.h"

namespace pins_test {
using ::p4::v1::Entity;
using ::p4::v1::ReadResponse;

// Since the validation is while the traffic is in progress, error margin needs
// to be defined.
constexpr int kErrorPercentage = 1;
constexpr absl::Duration kTrafficRunDuration = absl::Minutes(15);

void NsfAclFlowCoverageTestFixture::SetUp() {
flow_programmer_ = GetParam().create_flow_programmer();
traffic_helper_ = GetParam().create_traffic_helper();
testbed_interface_ = GetParam().create_testbed_interface();
component_validators_ = GetParam().create_component_validators();
ssh_client_ = GetParam().create_ssh_client();
SetupTestbed(testbed_interface_);
ASSERT_OK_AND_ASSIGN(testbed_, GetTestbed(testbed_interface_));
}
void NsfAclFlowCoverageTestFixture::TearDown() {
TearDownTestbed(testbed_interface_);
}

namespace {

absl::StatusOr<sai::TableEntries> GetAclFlowEntries() {
return gutil::ParseProtoOrDie<sai::TableEntries>(
R"pb(
entries {
acl_ingress_table_entry {
match { ether_type { value: "0x0806" mask: "0xffff" } }
action { acl_trap { qos_queue: "INBAND_PRIORITY_3" } }
priority: 2031
}
}
entries {
acl_ingress_qos_table_entry {
match { ether_type { value: "0x0806" mask: "0xffff" } }
action {
set_qos_queue_and_cancel_copy_above_rate_limit {
qos_queue: "INBAND_PRIORITY_3"
}
}
priority: 4600
meter_config { bytes_per_second: 32000 burst_bytes: 8000 }
controller_metadata: "orion_type: ARP_PUNTFLOW orion_cookie: 9223372036856217610"
}
}
)pb");
}

absl::Status ProgramAclFlows(thinkit::Switch& thinkit_switch,
const p4::config::v1::P4Info& p4_info) {
ASSIGN_OR_RETURN(sai::TableEntries kTableEntries, GetAclFlowEntries());
ASSIGN_OR_RETURN(std::unique_ptr<pdpi::P4RuntimeSession> sut_p4rt,
pdpi::P4RuntimeSession::Create(thinkit_switch));
ASSIGN_OR_RETURN(pdpi::IrP4Info ir_p4info, pdpi::CreateIrP4Info(p4_info));
std::vector<p4::v1::Entity> pi_entities;
pi_entities.reserve(kTableEntries.entries_size());
for (const sai::TableEntry& entry : kTableEntries.entries()) {
ASSIGN_OR_RETURN(pi_entities.emplace_back(),
pdpi::PdTableEntryToPiEntity(ir_p4info, entry));
}
LOG(INFO) << "Installing PI table entries on "
<< thinkit_switch.ChassisName();
return pdpi::InstallPiEntities(sut_p4rt.get(), ir_p4info, pi_entities);
}

} // namespace

TEST_P(NsfAclFlowCoverageTestFixture, NsfAclFlowCoverageTest) {
// The test needs at least 1 image_config_param to run.
if (GetParam().image_config_params.empty()) {
GTEST_SKIP() << "No image config params provided";
}
// Pick the first image config param.
ImageConfigParams image_config_param = GetParam().image_config_params[0];
thinkit::Switch& sut = GetSut(testbed_);

ASSERT_OK(ValidateTestbedState(image_config_param.image_label, testbed_,
*ssh_client_, image_config_param.gnmi_config));
ASSERT_OK(StoreSutDebugArtifacts(
absl::StrCat(image_config_param.image_label, "_before_nsf_reboot"),
testbed_));

// P4 snapshot before programming flows and starting the traffic.
LOG(INFO) << "Capturing P4 snapshot before programming flows and starting "
"the traffic";
ASSERT_OK_AND_ASSIGN(ReadResponse p4flow_snapshot1,
TakeP4FlowSnapshot(testbed_));
ASSERT_OK(SaveP4FlowSnapshot(
testbed_, p4flow_snapshot1,
absl::StrCat(sut.ChassisName(),
"p4flow_snapshot1_before_programming_flows.txt")));

// Program all the flows.
LOG(INFO) << "Programming L3 flows before starting the traffic";
ASSERT_OK(
flow_programmer_->ProgramFlows(image_config_param.p4_info, testbed_));
LOG(INFO) << "Programming ACL flows";
ASSERT_OK(ProgramAclFlows(sut, image_config_param.p4_info));

LOG(INFO) << "Starting the traffic";
ASSERT_OK(traffic_helper_->StartTraffic(testbed_));

// P4 snapshot before NSF reboot.
LOG(INFO) << "Capturing P4 snapshot before NSF reboot";
ASSERT_OK_AND_ASSIGN(ReadResponse p4flow_snapshot2,
TakeP4FlowSnapshot(testbed_));
ASSERT_OK(SaveP4FlowSnapshot(
testbed_, p4flow_snapshot2,
absl::StrCat(sut.ChassisName(), "p4flow_snapshot2_before_nsf.txt")));

LOG(INFO) << "Initiating NSF reboot";
ASSERT_OK(pins_test::NsfReboot(testbed_));
ASSERT_OK(WaitForNsfReboot(testbed_, *ssh_client_));

ASSERT_OK(ValidateTestbedState(image_config_param.image_label, testbed_,
*ssh_client_, image_config_param.gnmi_config));
ASSERT_OK(StoreSutDebugArtifacts(
absl::StrCat(image_config_param.image_label, "_after_nsf_reboot"),
testbed_));

// P4 snapshot after upgrade and NSF reboot.
LOG(INFO) << "Capturing P4 snapshot after NSF reboot";
ASSERT_OK_AND_ASSIGN(ReadResponse p4flow_snapshot3,
TakeP4FlowSnapshot(testbed_));
ASSERT_OK(SaveP4FlowSnapshot(
testbed_, p4flow_snapshot3,
absl::StrCat(sut.ChassisName(), "p4flow_snapshot3_after_nsf.txt")));

// Stop and validate traffic
LOG(INFO) << "Stopping the traffic";
ASSERT_OK(traffic_helper_->StopTraffic(testbed_));

// TODO: b/319088305 - For now, we validate traffic only after stopping
// traffic. Ideally we would want to validate traffic while injection is in
// progress to narrow down when the traffic loss occurred (i.e. before
// reboot, during reboot or after reconciliation).
LOG(INFO) << "Validating the traffic";
ASSERT_OK(traffic_helper_->ValidateTraffic(testbed_, kErrorPercentage));

// Selectively clear flows (eg. not clearing nexthop entries for host
// testbeds).
LOG(INFO) << "Clearing the flows";
ASSERT_OK(flow_programmer_->ClearFlows(testbed_));

// P4 snapshot after cleaning up flows.
LOG(INFO) << "Capturing P4 snapshot after cleaning up flows";
ASSERT_OK_AND_ASSIGN(ReadResponse p4flow_snapshot4,
TakeP4FlowSnapshot(testbed_));
ASSERT_OK(SaveP4FlowSnapshot(
testbed_, p4flow_snapshot4,
absl::StrCat(sut.ChassisName(),
"p4flow_snapshot4_after_clearing_flows.txt")));

LOG(INFO) << "Comparing P4 snapshots - Before Programming Flows Vs After "
"Clearing Flows";
ASSERT_OK(CompareP4FlowSnapshots(p4flow_snapshot1, p4flow_snapshot4));

LOG(INFO)
<< "Comparing P4 snapshots - Before NSF Reboot Vs. After NSF Reboot";
ASSERT_OK(CompareP4FlowSnapshots(p4flow_snapshot2, p4flow_snapshot3));
}
} // namespace pins_test
48 changes: 48 additions & 0 deletions tests/integration/system/nsf/nsf_acl_flow_coverage_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2025 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_TESTS_INTEGRATION_SYSTEM_NSF_NSF_ACL_FLOW_COVERAGE_TEST_H_
#define PINS_TESTS_INTEGRATION_SYSTEM_NSF_NSF_ACL_FLOW_COVERAGE_TEST_H_

#include <memory>
#include <string>
#include <vector>

#include "absl/status/status.h"
#include "gtest/gtest.h"
#include "tests/integration/system/nsf/interfaces/component_validator.h"
#include "tests/integration/system/nsf/interfaces/flow_programmer.h"
#include "tests/integration/system/nsf/interfaces/test_params.h"
#include "tests/integration/system/nsf/interfaces/testbed.h"
#include "tests/integration/system/nsf/interfaces/traffic_helper.h"
#include "thinkit/ssh_client.h"

namespace pins_test {

class NsfAclFlowCoverageTestFixture
: public testing::TestWithParam<NsfTestParams> {
protected:
void SetUp() override;
void TearDown() override;

std::unique_ptr<FlowProgrammer> flow_programmer_;
std::unique_ptr<TrafficHelper> traffic_helper_;
TestbedInterface testbed_interface_;
Testbed testbed_;
std::vector<std::unique_ptr<ComponentValidator>> component_validators_;
std::unique_ptr<thinkit::SSHClient> ssh_client_;
};

} // namespace pins_test

#endif // PINS_TESTS_INTEGRATION_SYSTEM_NSF_NSF_ACL_FLOW_COVERAGE_TEST_H_
24 changes: 23 additions & 1 deletion tests/integration/system/nsf/traffic_helpers/otg_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,29 @@ absl::Status OtgHelper::StartTraffic(Testbed& testbed) {
}

absl::Status OtgHelper::StopTraffic(Testbed& testbed) {
return absl::UnimplementedError("Stopping traffic is not implemented.");
return std::visit(
gutil::Overload{
[&](std::unique_ptr<thinkit::GenericTestbed>& testbed)
-> absl::Status {
otg::Openapi::StubInterface* stub = testbed->GetTrafficClient();
otg::SetControlStateRequest request;
otg::SetControlStateResponse response;
::grpc::ClientContext context;
request.mutable_control_state()->set_choice(
otg::ControlState::Choice::traffic);
request.mutable_control_state()->mutable_traffic()->set_choice(
otg::StateTraffic::Choice::flow_transmit);
request.mutable_control_state()
->mutable_traffic()
->mutable_flow_transmit()
->set_state(otg::StateTrafficFlowTransmit::State::stop);
return gutil::GrpcStatusToAbslStatus(
stub->SetControlState(&context, request, &response));
},
[&](thinkit::MirrorTestbed* testbed) {
return absl::UnimplementedError("MirrorTestbed not implemented");
}},
testbed);
}

absl::Status OtgHelper::ValidateTraffic(Testbed& testbed,
Expand Down
29 changes: 18 additions & 11 deletions tests/integration/system/nsf/upgrade_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ ABSL_FLAG(pins_test::NsfMilestone, milestone, pins_test::NsfMilestone::kAll,

namespace pins_test {

using ::p4::v1::Entity;
using ::p4::v1::ReadResponse;

// Since the validation is while the traffic is in progress, error margin needs
// to be defined.
Expand Down Expand Up @@ -82,8 +82,10 @@ absl::Status NsfUpgradeTest::NsfUpgradeOrReboot(
// P4 snapshot before programming flows and starting the traffic.
LOG(INFO) << "Capturing P4 snapshot before programming flows and starting "
"the traffic";
ASSIGN_OR_RETURN(std::vector<Entity> p4flow_snapshot1,
TakeP4FlowSnapshot(testbed_));
ASSIGN_OR_RETURN(ReadResponse p4flow_snapshot1, TakeP4FlowSnapshot(testbed_));
RETURN_IF_ERROR(
SaveP4FlowSnapshot(testbed_, p4flow_snapshot1,
"p4flow_snapshot1_before_programming_flows.txt"));

// Program all the flows.
LOG(INFO) << "Programming flows before starting the traffic";
Expand All @@ -101,8 +103,10 @@ absl::Status NsfUpgradeTest::NsfUpgradeOrReboot(

// P4 snapshot before Upgrade and NSF reboot.
LOG(INFO) << "Capturing P4 snapshot before Upgrade and NSF reboot";
ASSIGN_OR_RETURN(std::vector<Entity> p4flow_snapshot2,
TakeP4FlowSnapshot(testbed_));
ASSIGN_OR_RETURN(ReadResponse p4flow_snapshot2, TakeP4FlowSnapshot(testbed_));
RETURN_IF_ERROR(
SaveP4FlowSnapshot(testbed_, p4flow_snapshot2,
"p4flow_snapshot2_before_upgrade_and_nsf.txt"));

LOG(INFO) << "Starting NSF Upgrade";
// Copy image to the switch for installation.
Expand Down Expand Up @@ -130,8 +134,10 @@ absl::Status NsfUpgradeTest::NsfUpgradeOrReboot(

// P4 snapshot after upgrade and NSF reboot.
LOG(INFO) << "Capturing P4 snapshot after Upgrade and NSF reboot";
ASSIGN_OR_RETURN(std::vector<Entity> p4flow_snapshot3,
TakeP4FlowSnapshot(testbed_));
ASSIGN_OR_RETURN(ReadResponse p4flow_snapshot3, TakeP4FlowSnapshot(testbed_));
RETURN_IF_ERROR(
SaveP4FlowSnapshot(testbed_, p4flow_snapshot3,
"p4flow_snapshot3_after_upgrade_and_nsf.txt"));

// Push the new config and validate.
RETURN_IF_ERROR(PushConfig(next_image_config, testbed_, *ssh_client_));
Expand Down Expand Up @@ -167,15 +173,16 @@ absl::Status NsfUpgradeTest::NsfUpgradeOrReboot(

// P4 snapshot after cleaning up flows.
LOG(INFO) << "Capturing P4 snapshot after cleaning up flows";
ASSIGN_OR_RETURN(std::vector<Entity> p4flow_snapshot4,
TakeP4FlowSnapshot(testbed_));
ASSIGN_OR_RETURN(ReadResponse p4flow_snapshot4, TakeP4FlowSnapshot(testbed_));
RETURN_IF_ERROR(SaveP4FlowSnapshot(
testbed_, p4flow_snapshot4, "p4flow_snapshot4_after_clearing_flows.txt"));

LOG(INFO) << "Comparing P4 snapshots - Before Programming Flows Vs After "
"Clearing Flows";
RETURN_IF_ERROR(CompareP4FlowSnapshots(p4flow_snapshot2, p4flow_snapshot3));
RETURN_IF_ERROR(CompareP4FlowSnapshots(p4flow_snapshot1, p4flow_snapshot4));
LOG(INFO) << "Comparing P4 snapshots - Before Upgrade + NSF Reboot Vs. After "
"Upgrade + NSF Reboot";
RETURN_IF_ERROR(CompareP4FlowSnapshots(p4flow_snapshot1, p4flow_snapshot4));
RETURN_IF_ERROR(CompareP4FlowSnapshots(p4flow_snapshot2, p4flow_snapshot3));
LOG(INFO) << "NSF Upgrade from: " << curr_image_config.image_label
<< " to: " << next_image_config.image_label << " is complete.";
return absl::OkStatus();
Expand Down
Loading

0 comments on commit af0fad3

Please sign in to comment.