Skip to content

Commit 8287fa1

Browse files
PaulRalnikovAz3git
andauthored
FEAT: path hash in packet (#473)
closes #474 --------- Co-authored-by: Alexey Zharkov <62723911+Az3git@users.noreply.github.com>
1 parent 30180fa commit 8287fa1

3 files changed

Lines changed: 60 additions & 4 deletions

File tree

source/device/switch.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ TimeNs Switch::process() {
3232
return total_processing_time;
3333
}
3434
Packet packet = optional_packet.value();
35-
if (packet.flow == nullptr) {
36-
LOG_WARN("No flow in packet");
37-
return total_processing_time;
38-
}
3935

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

@@ -65,6 +61,7 @@ TimeNs Switch::process() {
6561
return total_processing_time;
6662
}
6763
packet.ttl--;
64+
packet.path_hash ^= std::hash<Id>{}(get_id());
6865

6966
// TODO: increase total_processing_time correctly
7067
next_link->schedule_arrival(packet);

source/packet.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace sim {
99

10+
using PathHash = std::uint32_t;
11+
1012
struct Packet {
1113
Packet(SizeByte a_size = SizeByte(0), IFlow* a_flow = nullptr,
1214
Id a_source_id = "", Id a_dest_id = "",
@@ -30,6 +32,7 @@ struct Packet {
3032
SizeByte delivered_data_size_at_origin; // For ACK this is inherited from
3133
// data packet
3234
TTL ttl = std::numeric_limits<TTL>::max();
35+
PathHash path_hash = 0;
3336
bool ecn_capable_transport;
3437
bool congestion_experienced;
3538
};

test/switch/test_switch.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "../_mocks/flow_mock.hpp"
88
#include "../utils/fake_packet.hpp"
9+
#include "device/host.hpp"
910
#include "device/switch.hpp"
1011
#include "host_mock.hpp"
1112
#include "link_mock.hpp"
@@ -164,4 +165,59 @@ TEST_F(TestSwitch, test_one_sender) { test_senders(1); }
164165

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

168+
// this test checks that packets passed throw same path have equal path hash
169+
// and packets passed throw different pathes have different path hashes
170+
TEST_F(TestSwitch, test_path_hash) {
171+
// topology:
172+
// sender --- switch_1
173+
// | |
174+
// | |
175+
// switch_2 -- receiver
176+
auto sender = std::make_shared<sim::Host>("sender");
177+
auto switch_1 = std::make_shared<sim::Switch>("switch_1");
178+
auto switch_2 = std::make_shared<sim::Switch>("switch_2");
179+
auto receiver = std::make_shared<sim::Host>("receiver");
180+
181+
auto link_sender_to_switch_1 = std::make_shared<LinkMock>(sender, switch_1);
182+
switch_1->add_inlink(link_sender_to_switch_1);
183+
184+
auto link_sender_to_switch_2 = std::make_shared<LinkMock>(sender, switch_2);
185+
switch_2->add_inlink(link_sender_to_switch_2);
186+
187+
auto link_switch_1_to_receiver =
188+
std::make_shared<LinkMock>(switch_1, receiver);
189+
switch_1->update_routing_table(receiver->get_id(),
190+
link_switch_1_to_receiver);
191+
192+
auto link_switch_2_to_receiver =
193+
std::make_shared<LinkMock>(switch_2, receiver);
194+
switch_2->update_routing_table(receiver->get_id(),
195+
link_switch_2_to_receiver);
196+
197+
sim::Packet packet_template(SizeByte(1), nullptr, sender->get_id(),
198+
receiver->get_id());
199+
sim::Packet first_packet_route_1(packet_template);
200+
sim::Packet second_packet_route_1(packet_template);
201+
sim::Packet packet_route_2(packet_template);
202+
203+
link_sender_to_switch_1->set_ingress_packet(first_packet_route_1);
204+
switch_1->process();
205+
link_sender_to_switch_1->set_ingress_packet(second_packet_route_1);
206+
switch_1->process();
207+
auto arrived_packets_route_1 =
208+
link_switch_1_to_receiver->get_arrived_packets();
209+
ASSERT_EQ(arrived_packets_route_1.size(), 2);
210+
211+
ASSERT_EQ(arrived_packets_route_1[0].path_hash,
212+
arrived_packets_route_1[1].path_hash);
213+
214+
link_sender_to_switch_2->set_ingress_packet(packet_route_2);
215+
switch_2->process();
216+
auto arrived_packets_route_2 =
217+
link_switch_2_to_receiver->get_arrived_packets();
218+
ASSERT_EQ(arrived_packets_route_2.size(), 1);
219+
ASSERT_NE(arrived_packets_route_2[0].path_hash,
220+
arrived_packets_route_1[0].path_hash);
221+
}
222+
167223
} // namespace test

0 commit comments

Comments
 (0)