# Network Simulation: Labwork 3 ## Bus Network Topology Build a bus network topology of three nodes (0, 1, 2) on a LAN using CSMA channel: ```c++ CsmaHelper csma; // Commonly found Ethernet data rate csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps")); // Speed of light, over maximum distance of 2000 meters csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560))); NodeContainer nodes; nodes.Create (3); NetDeviceContainer csmaDevices = csma.Install (nodes); InternetStackHelper stack; stack.Install (nodes); Ipv4AddressHelper address; address.SetBase ("10.1.1.0", "255.255.255.0"); Ipv4InterfaceContainer csmaInterfaces = address.Assign (csmaDevices); Ipv4GlobalRoutingHelper::PopulateRoutingTables (); Simulator::Run (); Simulator::Destroy (); ``` ## Packet Sniffing Implement scenario of node 0 is a client and node 2 is an echo server, which exchange 100 packets within 10 seconds: ```c++ // Set up ping server at port 9 UdpEchoServerHelper echoServer (9); ApplicationContainer serverApps = echoServer.Install (nodes.Get (2)); serverApps.Start (Seconds (1.0)); serverApps.Stop (Seconds (12.0)); UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (2), 9); echoClient.SetAttribute ("MaxPackets", UintegerValue (100)); // For the ease of comparison with the later section echoClient.SetAttribute ("Interval", TimeValue (MilliSeconds (100))); echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); ApplicationContainer clientApps = echoClient.Install (nodes.Get (0)); clientApps.Start (Seconds (2.0)); clientApps.Stop (Seconds (12.0)); Ipv4GlobalRoutingHelper::PopulateRoutingTables (); ``` Capture the PCAP trace from node 1: ```c++ csma.EnablePcap ("second", csmaDevices.Get (1), true); ``` ## Grow the Topology Implement the scenario of five nodes ```c++ nodes.Create (5); ``` on which there are 2 pairs of client-server (0-2) and (1-3) exchanging packets at the same time, by adding a server at node 3 and a client at node 1: ```c++ serverApps = echoServer.Install (nodes.Get (3)); serverApps.Start (Seconds (1.0)); serverApps.Stop (Seconds (12.0)); echoClient = UdpEchoClientHelper(csmaInterfaces.GetAddress (3), 9); echoClient.SetAttribute ("MaxPackets", UintegerValue (100)); echoClient.SetAttribute ("Interval", TimeValue (MilliSeconds (100))); echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); clientApps = echoClient.Install (nodes.Get (1)); clientApps.Start (Seconds (2.0)); clientApps.Stop (Seconds (12.0)); ``` The exchange rate is kept at 10 packets/s for 10s. Node 4 is used instead for sniffing: ```c++ csma.EnablePcap ("second", csmaDevices.Get (4), true); ``` At the interval of 100 ms (compared to the expected delivery delay of under 93 μm), it is unsurprising that for both cases of one and two echo pairs the packet delivery rate is perfectly 100 %. However, in the case with two pairs, both clients send at the same time and one server receives a few hundred microseconds later than the other due to queuing. To estimated by the average delay, we only need the time the clients receive the echo packet from the server: ```console $ tcpdump -nn -tt -r ../../second-4-0.pcap | > grep 'IP 10.1.1.[34]' | > awk 'match($1, /[0-9]+\.[0-9]/) {sum += $1 - substr($1, RSTART, RLENGTH)} > END {print sum/NR/2*1e6, "μs"}' 235.213 μs ``` In the original case, the average delay is almost a half: ```console $ tcpdump -nn -tt -r ../../second-1-0.pcap | > grep 'IP 10.1.1.3' | > awk 'match($1, /[0-9]+\.[0-9]/) {sum += $1 - substr($1, RSTART, RLENGTH)} > END {print sum/NR/2*1e6, "μs"}' 137.75 μs ```