Skip to content
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
5 changes: 1 addition & 4 deletions source/device/switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ TimeNs Switch::process() {
return total_processing_time;
}
Packet packet = optional_packet.value();
if (packet.flow == nullptr) {
LOG_WARN("No flow in packet");
return total_processing_time;
}

std::shared_ptr<ILink> next_link = get_link_to_destination(packet);

Expand Down Expand Up @@ -65,6 +61,7 @@ TimeNs Switch::process() {
return total_processing_time;
}
packet.ttl--;
packet.path_hash ^= std::hash<Id>{}(get_id());

// TODO: increase total_processing_time correctly
next_link->schedule_arrival(packet);
Expand Down
3 changes: 3 additions & 0 deletions source/packet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace sim {

using PathHash = std::uint32_t;

struct Packet {
Packet(SizeByte a_size = SizeByte(0), IFlow* a_flow = nullptr,
Id a_source_id = "", Id a_dest_id = "",
Expand All @@ -30,6 +32,7 @@ struct Packet {
SizeByte delivered_data_size_at_origin; // For ACK this is inherited from
// data packet
TTL ttl = std::numeric_limits<TTL>::max();
PathHash path_hash = 0;
bool ecn_capable_transport;
bool congestion_experienced;
};
Expand Down
56 changes: 56 additions & 0 deletions test/switch/test_switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "../_mocks/flow_mock.hpp"
#include "../utils/fake_packet.hpp"
#include "device/host.hpp"
#include "device/switch.hpp"
#include "host_mock.hpp"
#include "link_mock.hpp"
Expand Down Expand Up @@ -164,4 +165,59 @@ TEST_F(TestSwitch, test_one_sender) { test_senders(1); }

TEST_F(TestSwitch, test_multiple_senders) { test_senders(5); }

Comment thread
PaulRalnikov marked this conversation as resolved.
// this test checks that packets passed throw same path have equal path hash
// and packets passed throw different pathes have different path hashes
TEST_F(TestSwitch, test_path_hash) {
// topology:
// sender --- switch_1
// | |
// | |
// switch_2 -- receiver
auto sender = std::make_shared<sim::Host>("sender");
auto switch_1 = std::make_shared<sim::Switch>("switch_1");
auto switch_2 = std::make_shared<sim::Switch>("switch_2");
auto receiver = std::make_shared<sim::Host>("receiver");

auto link_sender_to_switch_1 = std::make_shared<LinkMock>(sender, switch_1);
switch_1->add_inlink(link_sender_to_switch_1);

auto link_sender_to_switch_2 = std::make_shared<LinkMock>(sender, switch_2);
switch_2->add_inlink(link_sender_to_switch_2);

auto link_switch_1_to_receiver =
std::make_shared<LinkMock>(switch_1, receiver);
switch_1->update_routing_table(receiver->get_id(),
link_switch_1_to_receiver);

auto link_switch_2_to_receiver =
std::make_shared<LinkMock>(switch_2, receiver);
switch_2->update_routing_table(receiver->get_id(),
link_switch_2_to_receiver);

sim::Packet packet_template(SizeByte(1), nullptr, sender->get_id(),
receiver->get_id());
sim::Packet first_packet_route_1(packet_template);
sim::Packet second_packet_route_1(packet_template);
sim::Packet packet_route_2(packet_template);

link_sender_to_switch_1->set_ingress_packet(first_packet_route_1);
switch_1->process();
link_sender_to_switch_1->set_ingress_packet(second_packet_route_1);
switch_1->process();
auto arrived_packets_route_1 =
link_switch_1_to_receiver->get_arrived_packets();
ASSERT_EQ(arrived_packets_route_1.size(), 2);

ASSERT_EQ(arrived_packets_route_1[0].path_hash,
arrived_packets_route_1[1].path_hash);

link_sender_to_switch_2->set_ingress_packet(packet_route_2);
switch_2->process();
auto arrived_packets_route_2 =
link_switch_2_to_receiver->get_arrived_packets();
ASSERT_EQ(arrived_packets_route_2.size(), 1);
ASSERT_NE(arrived_packets_route_2[0].path_hash,
arrived_packets_route_1[0].path_hash);
}

} // namespace test