about summary refs log tree commit diff
path: root/usth/ICT3.16/labworks/3.md
blob: 8b849aaeaa9434c1da99ef966f020e8efc2a92f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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
```