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]: Create function to retag packets and update the tag for entire PacketTestVector. #739

Merged
merged 3 commits into from
Nov 21, 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
12 changes: 6 additions & 6 deletions dvaas/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,12 @@ cc_library(
"//gutil:proto",
"//gutil:status",
"//p4_pdpi:ir_cc_proto",
"//p4_pdpi/packetlib",
"//p4_pdpi/packetlib:packetlib_cc_proto",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/types:optional",
"@com_google_absl//absl/types:span",
"@com_google_googletest//:gtest",
"@com_google_protobuf//:protobuf",
"@com_googlesource_code_re2//:re2",
],
)
Expand Down Expand Up @@ -251,8 +247,12 @@ cc_test(
srcs = ["test_vector_test.cc"],
deps = [
":test_vector",
":test_vector_cc_proto",
"//gutil:status_matchers",
"//gutil:testing",
"//p4_pdpi/packetlib",
"//p4_pdpi/packetlib:packetlib_cc_proto",
"@com_google_absl//absl/status",
"@com_google_googletest//:gtest_main",
],
)
Expand Down
64 changes: 64 additions & 0 deletions dvaas/test_vector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
#include <ostream>
#include <string>

#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_cat.h"
#include "dvaas/test_vector.pb.h"
#include "gutil/proto.h"
#include "gutil/status.h"
#include "p4_pdpi/packetlib/packetlib.h"
#include "p4_pdpi/packetlib/packetlib.pb.h"
#include "re2/re2.h"

Expand Down Expand Up @@ -53,4 +57,64 @@ std::ostream& operator<<(std::ostream& os, const SwitchOutput& output) {
return os << output.DebugString();
}

absl::Status UpdateTestTag(packetlib::Packet& packet, int new_tag) {
// Make a new input packet with updated payload.
std::string new_payload = packet.payload();
if (!RE2::Replace(&new_payload, *kTestPacketIdRegexp,
MakeTestPacketTagFromUniqueId(new_tag))) {
return gutil::InvalidArgumentErrorBuilder()
<< "Test packets must contain a tag of the form '"
<< kTestPacketIdRegexp->pattern()
<< "' in their payload, but the given packet with payload '"
<< packet.payload() << "' does not:\n"
<< gutil::PrintTextProto(packet);
}
packet.set_payload(new_payload);
bool status;
ASSIGN_OR_RETURN(status, PadPacketToMinimumSize(packet),
_.LogError() << "Failed to pad packet for tag: " << new_tag);
ASSIGN_OR_RETURN(status, UpdateAllComputedFields(packet),
_.LogError()
<< "Failed to update payload for tag: " << new_tag);

return absl::OkStatus();
}

// Returns a serialization of the given `packet` as a hexstring.
absl::StatusOr<std::string> SerializeAsHexString(
const packetlib::Packet& packet) {
ASSIGN_OR_RETURN(std::string serialized_packet,
packetlib::RawSerializePacket(packet),
_ << " where packet = " << packet.DebugString());
return absl::BytesToHexString(serialized_packet);
}

absl::Status UpdateTestTag(PacketTestVector& packet_test_vector, int new_tag) {
// Updates the payload of the SwitchInput.
dvaas::Packet& input_packet =
*packet_test_vector.mutable_input()->mutable_packet();
RETURN_IF_ERROR(UpdateTestTag(*input_packet.mutable_parsed(), new_tag));
ASSIGN_OR_RETURN(const std::string input_packet_updated_hexstr,
SerializeAsHexString(input_packet.parsed()));
input_packet.set_hex(input_packet_updated_hexstr);

// Update the payload of the SwitchOutput.
for (SwitchOutput& output_packet :
*packet_test_vector.mutable_acceptable_outputs()) {
for (dvaas::Packet& packet_out : *output_packet.mutable_packets()) {
RETURN_IF_ERROR(UpdateTestTag(*packet_out.mutable_parsed(), new_tag));
ASSIGN_OR_RETURN(const std::string packet_out_updated_hexstr,
SerializeAsHexString(packet_out.parsed()));
packet_out.set_hex(packet_out_updated_hexstr);
}
for (dvaas::PacketIn& packet_in : *output_packet.mutable_packet_ins()) {
RETURN_IF_ERROR(UpdateTestTag(*packet_in.mutable_parsed(), new_tag));
ASSIGN_OR_RETURN(const std::string packet_in_updated_hexstr,
SerializeAsHexString(packet_in.parsed()));
packet_in.set_hex(packet_in_updated_hexstr);
}
}
return absl::OkStatus();
}

} // namespace dvaas
11 changes: 6 additions & 5 deletions dvaas/test_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@

#include <ostream>
#include <string>
#include <vector>

#include "absl/container/btree_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/types/optional.h"
#include "dvaas/test_vector.pb.h"
#include "google/protobuf/descriptor.h"
#include "p4_pdpi/ir.pb.h"
#include "re2/re2.h"

namespace dvaas {

Expand All @@ -47,6 +43,11 @@ absl::StatusOr<int> ExtractTestPacketTag(const packetlib::Packet& packet);
// Needed to make gUnit produce human-readable output in open source.
std::ostream& operator<<(std::ostream& os, const SwitchOutput& output);

// Updates the test tag (to `new_tag`) and all computed fields of all packets
// (input, acceptable outputs) in the given `packet_test_vectr`. Returns an
// error if the packets are not already tagged.
absl::Status UpdateTestTag(PacketTestVector& packet_test_vector, int new_tag);

using PacketTestVectorById = absl::btree_map<int, PacketTestVector>;

} // namespace dvaas
Expand Down
Loading
Loading