Examples to show different packet level operations supported in NeST¶
This directory contains the examples to show different packet level operations
supported in NeST
.
IMPORTANT
The example packet-capture-point-to-point-3.py
requires tcpdump
to be pre-
installed. It can be installed on debian based systems as:
sudo apt install tcpdump
1. packet-capture-point-to-point-3-tcpdump.py¶
This program emulates point to point networks that connect two hosts h1
and
h2
via two routers r1
and r2
. 10 ping packets are sent from h1
to
h2
. It is similar to ah-point-to-point-3.py
in examples/address-helpers
.
This program shows how to capture packets at h2
by using tcpdump
. Output
of the details of the packets captured at the eth2
interface of h2
is then
displayed on the console.
1# SPDX-License-Identifier: GPL-2.0-only
2# Copyright (c) 2019-2022 NITK Surathkal
3
4########################
5# SHOULD BE RUN AS ROOT
6########################
7from nest.topology import *
8from nest.topology.network import Network
9from nest.topology.address_helper import AddressHelper
10import subprocess
11import multiprocessing
12
13# This program emulates point to point networks that connect two hosts `h1` and
14# `h2` via two routers `r1` and `r2`. 10 ping packets are sent from `h1` to
15# `h2`. It is similar to `ah-point-to-point-3.py` in `examples/address-helpers`
16# . This program shows how to capture packets at `h2` by using `tcpdump`.
17# Output of the details of the packets captured at the `eth2` interface of `h2`
18# is then displayed on the console.
19
20##############################################################################
21# Network Topology #
22# #
23# 5mbit, 5ms --> 5mbit, 5ms --> 5mbit, 5ms --> #
24# h1 -------------------- r1 -------------------- r2 -------------------- h2 #
25# <-- 10mbit, 100ms <-- 10mbit, 100ms <-- 10mbit, 100ms #
26# #
27##############################################################################
28
29# Create two hosts `h1` and `h2`, and two routers `r1` and `r2`
30h1 = Node("h1")
31h2 = Node("h2")
32r1 = Router("r1")
33r2 = Router("r2")
34
35# Set the IPv4 address for the networks, and not the interfaces.
36# We will use the `AddressHelper` later to assign addresses to the interfaces.
37# Note: this example has three networks: one on the left of `r1`, second
38# between the two routers, and third on the right of `r2`.
39n1 = Network("192.168.1.0/24") # network on the left of `r1`
40n2 = Network("192.168.2.0/24") # network between two routers
41n3 = Network("192.168.3.0/24") # network on the right of `r2`
42
43# Connect `h1` to `r1`, `r1` to `r2`, and then `r2` to `h2`
44# `eth1` and `eth2` are the interfaces at `h1` and `h2`, respectively.
45# `etr1a` is the first interface at `r1` which connects it with `h1`
46# `etr1b` is the second interface at `r1` which connects it with `r2`
47# `etr2a` is the first interface at `r2` which connects it with `r1`
48# `etr2b` is the second interface at `r2` which connects it with `h2`
49(eth1, etr1a) = connect(h1, r1, network=n1)
50(etr1b, etr2a) = connect(r1, r2, network=n2)
51(etr2b, eth2) = connect(r2, h2, network=n3)
52
53# Assign IPv4 addresses to all the interfaces in the network.
54AddressHelper.assign_addresses()
55
56# Set the link attributes: `h1` --> `r1` --> `r2` --> `h2`
57eth1.set_attributes("5mbit", "5ms") # from `h1` to `r1`
58etr1b.set_attributes("5mbit", "5ms") # from `r1` to `r2`
59etr2b.set_attributes("5mbit", "5ms") # from `r2` to `h2`
60
61# Set the link attributes: `h2` --> `r2` --> `r1` --> `h1`
62eth2.set_attributes("10mbit", "100ms") # from `h2` to `r2`
63etr2a.set_attributes("10mbit", "100ms") # from `r2` to `r1`
64etr1a.set_attributes("10mbit", "100ms") # from `r1` to `h1`
65
66# Set default routes in `h1` and `h2`. Additionally, set default routes in
67# `r1` and `r2` so that the packets that cannot be forwarded based on the
68# entries in their routing table are sent via a default interface.
69h1.add_route("DEFAULT", eth1)
70h2.add_route("DEFAULT", eth2)
71r1.add_route("DEFAULT", etr1b)
72r2.add_route("DEFAULT", etr2a)
73
74# `Ping` from `h1` to `h2` as a separate process. Send 10 ping packets.
75process = multiprocessing.Process(target=h1.ping, args=(eth2.address, 10, False))
76process.start()
77
78# Capture a maximum of 30 packets on `eth2` interface of `h2`. Although we
79# send just 10 ping packets, there are ping replies and other packets (such
80# as ARP) to be captured. Hence, we capture a maximum of 30 packets.
81with h2:
82 print("Running tcpdump in h2 to capture ping packets")
83 proc = subprocess.Popen(
84 ["tcpdump", "-i", eth2.id, f"-c {30}"],
85 stdout=subprocess.PIPE,
86 stderr=subprocess.PIPE,
87 )
88 (stdout, _) = proc.communicate()
89
90# Output the details of the packets captured after ping process completes.
91process.join()
92print(f"\nPackets captured at h2 by tcpdump (max: {30} packets):\n")
93print(stdout.decode())
2. packet-capture-point-to-point-3-tshark.py¶
This program emulates point to point networks that connect two hosts h1
and
h2
via two routers r1
and r2
. 10 ping packets are sent from h1
to
h2
. It is similar to ah-point-to-point-3.py
in examples/address-helpers
.
This program shows how to capture packets at h2
by using tshark
. The
details of the packets captured at the eth2
interface of h2
are then
written into packet_capture.pcap
file.
1# SPDX-License-Identifier: GPL-2.0-only
2# Copyright (c) 2019-2022 NITK Surathkal
3
4########################
5# SHOULD BE RUN AS ROOT
6########################
7from nest.topology import *
8from nest.topology.network import Network
9from nest.topology.address_helper import AddressHelper
10
11# This program emulates point to point networks that connect two hosts `h1` and
12# `h2` via two routers `r1` and `r2`. 10 ping packets are sent from `h1` to
13# `h2`. It is similar to `ah-point-to-point-3.py` in `examples/address-helpers`.
14# This program shows how to capture packets at `h2` by using `tcpdump`.
15# The details of the packets captured at the `eth2` interface of `h2` are then
16# written into `packet_capture.pcap` file.
17
18##############################################################################
19# Network Topology #
20# #
21# 5mbit, 5ms --> 5mbit, 5ms --> 5mbit, 5ms --> #
22# h1 -------------------- r1 -------------------- r2 -------------------- h2 #
23# <-- 10mbit, 100ms <-- 10mbit, 100ms <-- 10mbit, 100ms #
24# #
25##############################################################################
26
27# Create two hosts `h1` and `h2`, and two routers `r1` and `r2`
28h1 = Node("h1")
29h2 = Node("h2")
30r1 = Router("r1")
31r2 = Router("r2")
32
33# Set the IPv4 address for the networks, and not the interfaces.
34# We will use the `AddressHelper` later to assign addresses to the interfaces.
35# Note: this example has three networks: one on the left of `r1`, second
36# between the two routers, and third on the right of `r2`.
37n1 = Network("192.168.1.0/24") # network on the left of `r1`
38n2 = Network("192.168.2.0/24") # network between two routers
39n3 = Network("192.168.3.0/24") # network on the right of `r2`
40
41# Connect `h1` to `r1`, `r1` to `r2`, and then `r2` to `h2`
42# `eth1` and `eth2` are the interfaces at `h1` and `h2`, respectively.
43# `etr1a` is the first interface at `r1` which connects it with `h1`
44# `etr1b` is the second interface at `r1` which connects it with `r2`
45# `etr2a` is the first interface at `r2` which connects it with `r1`
46# `etr2b` is the second interface at `r2` which connects it with `h2`
47(eth1, etr1a) = connect(h1, r1, network=n1)
48(etr1b, etr2a) = connect(r1, r2, network=n2)
49(etr2b, eth2) = connect(r2, h2, network=n3)
50
51# Assign IPv4 addresses to all the interfaces in the network.
52AddressHelper.assign_addresses()
53
54# Set the link attributes: `h1` --> `r1` --> `r2` --> `h2`
55eth1.set_attributes("5mbit", "5ms") # from `h1` to `r1`
56etr1b.set_attributes("5mbit", "5ms") # from `r1` to `r2`
57etr2b.set_attributes("5mbit", "5ms") # from `r2` to `h2`
58
59# Set the link attributes: `h2` --> `r2` --> `r1` --> `h1`
60eth2.set_attributes("10mbit", "100ms") # from `h2` to `r2`
61etr2a.set_attributes("10mbit", "100ms") # from `r2` to `r1`
62etr1a.set_attributes("10mbit", "100ms") # from `r1` to `h1`
63
64# Set default routes in `h1` and `h2`. Additionally, set default routes in
65# `r1` and `r2` so that the packets that cannot be forwarded based on the
66# entries in their routing table are sent via a default interface.
67h1.add_route("DEFAULT", eth1)
68h2.add_route("DEFAULT", eth2)
69r1.add_route("DEFAULT", etr1b)
70r2.add_route("DEFAULT", etr2a)
71
72# Capture a maximum of 30 packets on `eth2` interface of `h2`. Although we
73# send just 10 ping packets, there are ping replies and other packets (such
74# as ARP) to be captured. Hence, we capture a maximum of 30 packets.
75# Output the details of the packets captured after 30 packets are captured,
76# into `packet_capture.pcap` file.
77h2.capture_packets(interface=eth2, packet_count=30, output_file="packet_capture.pcap")
78
79# `Ping` from `h1` to `h2`. Send 10 ping packets.
80h1.ping(eth2.address, packets=10)
3. packet-corruption-point-to-point-3.py¶
This program emulates point to point networks that connect two hosts h1
and
h2
via two routers r1
and r2
. 20 ping packets are sent from h1
to
h2
, and the success/failure of these packets is reported. It is similar to
ah-point-to-point-3.py
in examples/address-helpers
. This program shows
the emulation of random noise to introduce an error at a random position
for a chosen percentage of packets.
1# SPDX-License-Identifier: GPL-2.0-only
2# Copyright (c) 2019-2022 NITK Surathkal
3
4########################
5# SHOULD BE RUN AS ROOT
6########################
7from nest.topology import *
8from nest.topology.network import Network
9from nest.topology.address_helper import AddressHelper
10
11# This program emulates point to point networks that connect two hosts `h1` and
12# `h2` via two routers `r1` and `r2`. 20 ping packets are sent from `h1` to
13# `h2`, and the success/failure of these packets is reported. It is similar to
14# `ah-point-to-point-3.py` in `examples/address-helpers`. This program shows
15# the emulation of random noise to introduce an error at a random position
16# for a chosen percentage of packets.
17
18##############################################################################
19# Network Topology #
20# #
21# Error rate: 20% #
22# Correlation: 0.5% #
23# 5mbit, 5ms --> 5mbit, 5ms --> 5mbit, 5ms --> #
24# h1 -------------------- r1 -------------------- r2 -------------------- h2 #
25# <-- 10mbit, 100ms <-- 10mbit, 100ms <-- 10mbit, 100ms #
26# #
27##############################################################################
28
29# Create two hosts `h1` and `h2`, and two routers `r1` and `r2`
30h1 = Node("h1")
31h2 = Node("h2")
32r1 = Router("r1")
33r2 = Router("r2")
34
35# Set the IPv4 address for the networks, and not the interfaces.
36# We will use the `AddressHelper` later to assign addresses to the interfaces.
37# Note: this example has three networks: one on the left of `r1`, second
38# between the two routers, and third on the right of `r2`.
39n1 = Network("192.168.1.0/24") # network on the left of `r1`
40n2 = Network("192.168.2.0/24") # network between two routers
41n3 = Network("192.168.3.0/24") # network on the right of `r2`
42
43# Connect `h1` to `r1`, `r1` to `r2`, and then `r2` to `h2`
44# `eth1` and `eth2` are the interfaces at `h1` and `h2`, respectively.
45# `etr1a` is the first interface at `r1` which connects it with `h1`
46# `etr1b` is the second interface at `r1` which connects it with `r2`
47# `etr2a` is the first interface at `r2` which connects it with `r1`
48# `etr2b` is the second interface at `r2` which connects it with `h2`
49(eth1, etr1a) = connect(h1, r1, network=n1)
50(etr1b, etr2a) = connect(r1, r2, network=n2)
51(etr2b, eth2) = connect(r2, h2, network=n3)
52
53# Assign IPv4 addresses to all the interfaces in the network.
54AddressHelper.assign_addresses()
55
56# Set the link attributes: `h1` --> `r1` --> `r2` --> `h2`
57eth1.set_attributes("5mbit", "5ms") # from `h1` to `r1`
58etr1b.set_attributes("5mbit", "5ms") # from `r1` to `r2`
59etr2b.set_attributes("5mbit", "5ms") # from `r2` to `h2`
60
61# Set the link attributes: `h2` --> `r2` --> `r1` --> `h1`
62eth2.set_attributes("10mbit", "100ms") # from `h2` to `r2`
63etr2a.set_attributes("10mbit", "100ms") # from `r2` to `r1`
64etr1a.set_attributes("10mbit", "100ms") # from `r1` to `h1`
65
66# Set a packet error rate of 10% with 0.5% correlation rate between two packets
67# on the link from `r1` to `r2`.
68etr1b.set_packet_corruption("10%", "0.5%")
69
70# Set default routes in `h1` and `h2`. Additionally, set default routes in
71# `r1` and `r2` so that the packets that cannot be forwarded based on the
72# entries in their routing table are sent via a default interface.
73h1.add_route("DEFAULT", eth1)
74h2.add_route("DEFAULT", eth2)
75r1.add_route("DEFAULT", etr1b)
76r2.add_route("DEFAULT", etr2a)
77
78# `Ping` from `h1` to `h2`.
79# Note: You may not see a packet error rate of 10% because we transmit only 20
80# ping packets in this example. If we transmit large number of ping packets,
81# the average packet error rate observed would be 10%.
82h1.ping(eth2.address, packets=20)
4. packet-delay-distribution-point-to-point-3.py¶
This program emulates point to point networks that connect two hosts h1
and
h2
via two routers r1
and r2
. 20 ping packets are sent from h1
to
h2
, and the success/failure of these packets is reported. It is similar to
ah-point-to-point-3.py
in examples/address-helpers
. This program shows how
to use different delay distribution options using NeST.
1# SPDX-License-Identifier: GPL-2.0-only
2# Copyright (c) 2019-2022 NITK Surathkal
3
4########################
5# SHOULD BE RUN AS ROOT
6########################
7from nest.topology import *
8from nest.topology.network import Network
9from nest.topology.address_helper import AddressHelper
10
11# This program emulates point to point networks that connect two hosts `h1` and
12# `h2` via two routers `r1` and `r2`. 20 ping packets are sent from `h1` to
13# `h2`, and the success/failure of these packets is reported. It is similar to
14# `ah-point-to-point-3.py` in `examples/address-helpers`. This program shows how
15# to use different delay distribution options using NeST.
16
17##############################################################################
18# Network Topology #
19# #
20# Delay: 100ms #
21# Jitter: 10ms #
22# Distribution: normal #
23# 5mbit, 5ms --> 5mbit, 5ms --> 5mbit, 5ms --> #
24# h1 -------------------- r1 -------------------- r2 -------------------- h2 #
25# <-- 10mbit, 100ms <-- 10mbit, 100ms <-- 10mbit, 100ms #
26# #
27##############################################################################
28
29# Create two hosts `h1` and `h2`, and two routers `r1` and `r2`
30h1 = Node("h1")
31h2 = Node("h2")
32r1 = Router("r1")
33r2 = Router("r2")
34
35# Set the IPv4 address for the networks, and not the interfaces.
36# We will use the `AddressHelper` later to assign addresses to the interfaces.
37# Note: this example has three networks: one on the left of `r1`, second
38# between the two routers, and third on the right of `r2`.
39n1 = Network("192.168.1.0/24") # network on the left of `r1`
40n2 = Network("192.168.2.0/24") # network between two routers
41n3 = Network("192.168.3.0/24") # network on the right of `r2`
42
43# Connect `h1` to `r1`, `r1` to `r2`, and then `r2` to `h2`
44# `eth1` and `eth2` are the interfaces at `h1` and `h2`, respectively.
45# `etr1a` is the first interface at `r1` which connects it with `h1`
46# `etr1b` is the second interface at `r1` which connects it with `r2`
47# `etr2a` is the first interface at `r2` which connects it with `r1`
48# `etr2b` is the second interface at `r2` which connects it with `h2`
49(eth1, etr1a) = connect(h1, r1, network=n1)
50(etr1b, etr2a) = connect(r1, r2, network=n2)
51(etr2b, eth2) = connect(r2, h2, network=n3)
52
53# Assign IPv4 addresses to all the interfaces in the network.
54AddressHelper.assign_addresses()
55
56# Set the link attributes: `h1` --> `r1` --> `r2` --> `h2`
57eth1.set_attributes("5mbit", "5ms") # from `h1` to `r1`
58etr1b.set_attributes("5mbit", "5ms") # from `r1` to `r2`
59etr2b.set_attributes("5mbit", "5ms") # from `r2` to `h2`
60
61# Set the link attributes: `h2` --> `r2` --> `r1` --> `h1`
62eth2.set_attributes("10mbit", "100ms") # from `h2` to `r2`
63etr2a.set_attributes("10mbit", "100ms") # from `r2` to `r1`
64etr1a.set_attributes("10mbit", "100ms") # from `r1` to `h1`
65
66# Set a delay of 100ms, jitter 10ms and distribution to normal
67# on the link from `h1` to `r1`.
68eth1.set_delay_distribution("100ms", "10ms", "normal")
69
70# Set default routes in `h1` and `h2`. Additionally, set default routes in
71# `r1` and `r2` so that the packets that cannot be forwarded based on the
72# entries in their routing table are sent via a default interface.
73h1.add_route("DEFAULT", eth1)
74h2.add_route("DEFAULT", eth2)
75r1.add_route("DEFAULT", etr1b)
76r2.add_route("DEFAULT", etr2a)
77
78# `Ping` from `h1` to `h2`.
79# Note: Delays will be distributed normally with a variation (jitter) of 10ms
80h1.ping(eth2.address, packets=20)
5. packet-duplication-point-to-point-3.py¶
This program emulates point to point networks that connect two hosts h1
and
h2
via two routers r1
and r2
. 20 ping packets are sent from h1
to
h2
, and the success/failure of these packets is reported. It is similar to
ah-point-to-point-3.py
in examples/address-helpers
. This program shows
the emulation of packet duplication for a chosen percentage of packets.
1# SPDX-License-Identifier: GPL-2.0-only
2# Copyright (c) 2019-2022 NITK Surathkal
3
4########################
5# SHOULD BE RUN AS ROOT
6########################
7from nest.topology import *
8from nest.topology.network import Network
9from nest.topology.address_helper import AddressHelper
10
11# This program emulates point to point networks that connect two hosts `h1` and
12# `h2` via two routers `r1` and `r2`. 20 ping packets are sent from `h1` to
13# `h2`, and the success/failure of these packets is reported. It is similar to
14# `ah-point-to-point-3.py` in `examples/address-helpers`. This program shows
15# the emulation of packet duplication for a chosen percentage of packets.
16
17##############################################################################
18# Network Topology #
19# #
20# Duplication rate: 20% #
21# #
22# 5mbit, 5ms --> 5mbit, 5ms --> 5mbit, 5ms --> #
23# h1 -------------------- r1 -------------------- r2 -------------------- h2 #
24# <-- 10mbit, 100ms <-- 10mbit, 100ms <-- 10mbit, 100ms #
25# #
26##############################################################################
27
28# Create two hosts `h1` and `h2`, and two routers `r1` and `r2`
29h1 = Node("h1")
30h2 = Node("h2")
31r1 = Router("r1")
32r2 = Router("r2")
33
34# Set the IPv4 address for the networks, and not the interfaces.
35# We will use the `AddressHelper` later to assign addresses to the interfaces.
36# Note: this example has three networks: one on the left of `r1`, second
37# between the two routers, and third on the right of `r2`.
38n1 = Network("192.168.1.0/24") # network on the left of `r1`
39n2 = Network("192.168.2.0/24") # network between two routers
40n3 = Network("192.168.3.0/24") # network on the right of `r2`
41
42# Connect `h1` to `r1`, `r1` to `r2`, and then `r2` to `h2`
43# `eth1` and `eth2` are the interfaces at `h1` and `h2`, respectively.
44# `etr1a` is the first interface at `r1` which connects it with `h1`
45# `etr1b` is the second interface at `r1` which connects it with `r2`
46# `etr2a` is the first interface at `r2` which connects it with `r1`
47# `etr2b` is the second interface at `r2` which connects it with `h2`
48(eth1, etr1a) = connect(h1, r1, network=n1)
49(etr1b, etr2a) = connect(r1, r2, network=n2)
50(etr2b, eth2) = connect(r2, h2, network=n3)
51
52# Assign IPv4 addresses to all the interfaces in the network.
53AddressHelper.assign_addresses()
54
55# Set the link attributes: `h1` --> `r1` --> `r2` --> `h2`
56eth1.set_attributes("5mbit", "5ms") # from `h1` to `r1`
57etr1b.set_attributes("5mbit", "5ms") # from `r1` to `r2`
58etr2b.set_attributes("5mbit", "5ms") # from `r2` to `h2`
59
60# Set the link attributes: `h2` --> `r2` --> `r1` --> `h1`
61eth2.set_attributes("10mbit", "100ms") # from `h2` to `r2`
62etr2a.set_attributes("10mbit", "100ms") # from `r2` to `r1`
63etr1a.set_attributes("10mbit", "100ms") # from `r1` to `h1`
64
65# Set the packet duplication rate to 20% on the link from `r1` to `r2`.
66etr1b.set_packet_duplication("20%")
67
68# Set default routes in `h1` and `h2`. Additionally, set default routes in
69# `r1` and `r2` so that the packets that cannot be forwarded based on the
70# entries in their routing table are sent via a default interface.
71h1.add_route("DEFAULT", eth1)
72h2.add_route("DEFAULT", eth2)
73r1.add_route("DEFAULT", etr1b)
74r2.add_route("DEFAULT", etr2a)
75
76# `Ping` from `h1` to `h2`.
77# Note: We may not see a packet duplication rate of 20% because we transmit
78# only 20 ping packets in this example. If we transmit large number of ping
79# packets, the average packet duplication rate observed would be 20%.
80h1.ping(eth2.address, packets=20)
6. packet-loss-gemodel-point-to-point-3.py¶
This program emulates point to point networks that connect two hosts h1
and
h2
via two routers r1
and r2
. 20 ping packets are sent from h1
to
h2
, and the success/failure of these packets is reported. It is similar to
ah-point-to-point-3.py
in examples/address-helpers
. This program shows
how to add an loss probability according to the Gilbert-Elliot loss model
or its special cases (Gilbert, Simple Gilbert and Bernoulli) to the
packets outgoing from the chosen network interface.
1# SPDX-License-Identifier: GPL-2.0-only
2# Copyright (c) 2019-2022 NITK Surathkal
3
4########################
5# SHOULD BE RUN AS ROOT
6########################
7from nest.topology import *
8from nest.topology.network import Network
9from nest.topology.address_helper import AddressHelper
10
11# This program emulates point to point networks that connect two hosts `h1` and
12# `h2` via two routers `r1` and `r2`. 20 ping packets are sent from `h1` to
13# `h2`, and the success/failure of these packets is reported. It is similar to
14# `ah-point-to-point-3.py` in `examples/address-helpers`. This program shows
15# how to add an loss probability according to the Gilbert-Elliot loss model
16# or its special cases (Gilbert, Simple Gilbert and Bernoulli) to the packets
17# outgoing from the chosen network interface.
18
19##############################################################################
20# Network Topology #
21# #
22# #
23# 5mbit, 5ms --> 5mbit, 5ms --> 5mbit, 5ms --> #
24# h1 -------------------- r1 -------------------- r2 -------------------- h2 #
25# <-- 10mbit, 100ms <-- 10mbit, 100ms <-- 10mbit, 100ms #
26# #
27##############################################################################
28
29# Create two hosts `h1` and `h2`, and two routers `r1` and `r2`
30h1 = Node("h1")
31h2 = Node("h2")
32r1 = Router("r1")
33r2 = Router("r2")
34
35# Set the IPv4 address for the networks, and not the interfaces.
36# We will use the `AddressHelper` later to assign addresses to the interfaces.
37# Note: this example has three networks: one on the left of `r1`, second
38# between the two routers, and third on the right of `r2`.
39n1 = Network("192.168.1.0/24") # network on the left of `r1`
40n2 = Network("192.168.2.0/24") # network between two routers
41n3 = Network("192.168.3.0/24") # network on the right of `r2`
42
43# Connect `h1` to `r1`, `r1` to `r2`, and then `r2` to `h2`
44# `eth1` and `eth2` are the interfaces at `h1` and `h2`, respectively.
45# `etr1a` is the first interface at `r1` which connects it with `h1`
46# `etr1b` is the second interface at `r1` which connects it with `r2`
47# `etr2a` is the first interface at `r2` which connects it with `r1`
48# `etr2b` is the second interface at `r2` which connects it with `h2`
49(eth1, etr1a) = connect(h1, r1, network=n1)
50(etr1b, etr2a) = connect(r1, r2, network=n2)
51(etr2b, eth2) = connect(r2, h2, network=n3)
52
53# Assign IPv4 addresses to all the interfaces in the network.
54AddressHelper.assign_addresses()
55
56# Set the link attributes: `h1` --> `r1` --> `r2` --> `h2`
57eth1.set_attributes("5mbit", "5ms") # from `h1` to `r1`
58etr1b.set_attributes("5mbit", "5ms") # from `r1` to `r2`
59etr2b.set_attributes("5mbit", "5ms") # from `r2` to `h2`
60
61# Set the link attributes: `h2` --> `r2` --> `r1` --> `h1`
62eth2.set_attributes("10mbit", "100ms") # from `h2` to `r2`
63etr2a.set_attributes("10mbit", "100ms") # from `r2` to `r1`
64etr1a.set_attributes("10mbit", "100ms") # from `r1` to `h1`
65
66# Set the probability of packet loss to p, r, 1-h and 1-k and enable ecn on
67# the link from `r1` to `r2`.
68etr1b.set_packet_loss_gemodel("10%", "4%", "25%", "4%", True)
69
70# Set default routes in `h1` and `h2`. Additionally, set default routes in
71# `r1` and `r2` so that the packets that cannot be forwarded based on the
72# entries in their routing table are sent via a default interface.
73h1.add_route("DEFAULT", eth1)
74h2.add_route("DEFAULT", eth2)
75r1.add_route("DEFAULT", etr1b)
76r2.add_route("DEFAULT", etr2a)
77
78# `Ping` from `h1` to `h2`.
79h1.ping(eth2.address, packets=20)
7. packet-ping-preload-point-to-point-3.py¶
This program emulates a point to point network that connects two hosts h1
and h2
via two routers r1
and r2
. 20 ping packets are sent from h1
to
h2
. It is similar to examples/address-helpers/ah-point-to-point-3.py
.
Out of 20 ping packets that are sent, the first 10 ping packets are sent
simultaneously without waiting for a reply from h2
, and the remaining 10
ping packets are sent one by one. The success/failure of all ping packets is
reported. The ping preload option is used for sending the first 10 packets.
1# SPDX-License-Identifier: GPL-2.0-only
2# Copyright (c) 2019-2022 NITK Surathkal
3
4########################
5# SHOULD BE RUN AS ROOT
6########################
7from nest.topology import *
8from nest.topology.network import Network
9from nest.topology.address_helper import AddressHelper
10
11# This program emulates a point to point network that connects two hosts `h1`
12# and `h2` via two routers `r1` and `r2`. 20 ping packets are sent from `h1` to
13# `h2`. It is similar to `examples/address-helpers/ah-point-to-point-3.py`.
14# Out of 20 ping packets that are sent, the first 10 ping packets are sent
15# simultaneously without waiting for a reply from `h2`, and the remaining 10
16# ping packets are sent one by one. The success/failure of all ping packets is
17# reported. The ping preload option is used for sending the first 10 packets.
18
19##############################################################################
20# Network Topology #
21# #
22# 5mbit, 5ms --> 5mbit, 5ms --> 5mbit, 5ms --> #
23# h1 -------------------- r1 -------------------- r2 -------------------- h2 #
24# <-- 10mbit, 100ms <-- 10mbit, 100ms <-- 10mbit, 100ms #
25# #
26##############################################################################
27
28# Create two hosts `h1` and `h2`, and two routers `r1` and `r2`
29h1 = Node("h1")
30h2 = Node("h2")
31r1 = Router("r1")
32r2 = Router("r2")
33
34# Set the IPv4 address for the networks, and not the interfaces.
35# We will use the `AddressHelper` later to assign addresses to the interfaces.
36# Note: this example has three networks: one on the left of `r1`, second
37# between the two routers, and third on the right of `r2`.
38n1 = Network("192.168.1.0/24") # network on the left of `r1`
39n2 = Network("192.168.2.0/24") # network between two routers
40n3 = Network("192.168.3.0/24") # network on the right of `r2`
41
42# Connect `h1` to `r1`, `r1` to `r2`, and then `r2` to `h2`
43# `eth1` and `eth2` are the interfaces at `h1` and `h2`, respectively.
44# `etr1a` is the first interface at `r1` which connects it with `h1`
45# `etr1b` is the second interface at `r1` which connects it with `r2`
46# `etr2a` is the first interface at `r2` which connects it with `r1`
47# `etr2b` is the second interface at `r2` which connects it with `h2`
48(eth1, etr1a) = connect(h1, r1, network=n1)
49(etr1b, etr2a) = connect(r1, r2, network=n2)
50(etr2b, eth2) = connect(r2, h2, network=n3)
51
52# Assign IPv4 addresses to all the interfaces in the network.
53AddressHelper.assign_addresses()
54
55# Set the link attributes: `h1` --> `r1` --> `r2` --> `h2`
56eth1.set_attributes("5mbit", "5ms") # from `h1` to `r1`
57etr1b.set_attributes("5mbit", "5ms") # from `r1` to `r2`
58etr2b.set_attributes("5mbit", "5ms") # from `r2` to `h2`
59
60# Set the link attributes: `h2` --> `r2` --> `r1` --> `h1`
61eth2.set_attributes("10mbit", "100ms") # from `h2` to `r2`
62etr2a.set_attributes("10mbit", "100ms") # from `r2` to `r1`
63etr1a.set_attributes("10mbit", "100ms") # from `r1` to `h1`
64
65# Set default routes in `h1` and `h2`. Additionally, set default routes in
66# `r1` and `r2` so that the packets that cannot be forwarded based on the
67# entries in their routing table are sent via a default interface.
68h1.add_route("DEFAULT", eth1)
69h2.add_route("DEFAULT", eth2)
70r1.add_route("DEFAULT", etr1b)
71r2.add_route("DEFAULT", etr2a)
72
73# `Ping` from `h1` to `h2`.
74# Send 20 ping packets from h1 to h2, but send 10 of them with the preload
75# option set so that they are sent out as fast as possible, without waiting
76# for a ping response.
77h1.ping(eth2.address, preload=10, packets=20)
8. packet-loss-state-point-to-point-3.py¶
This program emulates point to point networks that connect two hosts h1
and
h2
via two routers r1
and r2
. 20 ping packets are sent from h1
to
h2
, and the success/failure of these packets is reported. It is similar to
ah-point-to-point-3.py
in examples/address-helpers
. This program shows
how to add an loss probability using Markov model having four states to the
packets outgoing from the chosen network interface.
1# SPDX-License-Identifier: GPL-2.0-only
2# Copyright (c) 2019-2022 NITK Surathkal
3
4########################
5# SHOULD BE RUN AS ROOT
6########################
7from nest.topology import *
8from nest.topology.network import Network
9from nest.topology.address_helper import AddressHelper
10
11# This program emulates point to point networks that connect two hosts `h1` and
12# `h2` via two routers `r1` and `r2`. 20 ping packets are sent from `h1` to
13# `h2`, and the success/failure of these packets is reported. It is similar to
14# `ah-point-to-point-3.py` in `examples/address-helpers`. This program shows
15# how to add an loss probability using Markov model having four states to the packets outgoing from the
16# chosen network interface.
17
18##############################################################################
19# Network Topology #
20# #
21# #
22# 5mbit, 5ms --> 5mbit, 5ms --> 5mbit, 5ms --> #
23# h1 -------------------- r1 -------------------- r2 -------------------- h2 #
24# <-- 10mbit, 100ms <-- 10mbit, 100ms <-- 10mbit, 100ms #
25# #
26##############################################################################
27
28# Create two hosts `h1` and `h2`, and two routers `r1` and `r2`
29h1 = Node("h1")
30h2 = Node("h2")
31r1 = Router("r1")
32r2 = Router("r2")
33
34# Set the IPv4 address for the networks, and not the interfaces.
35# We will use the `AddressHelper` later to assign addresses to the interfaces.
36# Note: this example has three networks: one on the left of `r1`, second
37# between the two routers, and third on the right of `r2`.
38n1 = Network("192.168.1.0/24") # network on the left of `r1`
39n2 = Network("192.168.2.0/24") # network between two routers
40n3 = Network("192.168.3.0/24") # network on the right of `r2`
41
42# Connect `h1` to `r1`, `r1` to `r2`, and then `r2` to `h2`
43# `eth1` and `eth2` are the interfaces at `h1` and `h2`, respectively.
44# `etr1a` is the first interface at `r1` which connects it with `h1`
45# `etr1b` is the second interface at `r1` which connects it with `r2`
46# `etr2a` is the first interface at `r2` which connects it with `r1`
47# `etr2b` is the second interface at `r2` which connects it with `h2`
48(eth1, etr1a) = connect(h1, r1, network=n1)
49(etr1b, etr2a) = connect(r1, r2, network=n2)
50(etr2b, eth2) = connect(r2, h2, network=n3)
51
52# Assign IPv4 addresses to all the interfaces in the network.
53AddressHelper.assign_addresses()
54
55# Set the link attributes: `h1` --> `r1` --> `r2` --> `h2`
56eth1.set_attributes("5mbit", "5ms") # from `h1` to `r1`
57etr1b.set_attributes("5mbit", "5ms") # from `r1` to `r2`
58etr2b.set_attributes("5mbit", "5ms") # from `r2` to `h2`
59
60# Set the link attributes: `h2` --> `r2` --> `r1` --> `h1`
61eth2.set_attributes("10mbit", "100ms") # from `h2` to `r2`
62etr2a.set_attributes("10mbit", "100ms") # from `r2` to `r1`
63etr1a.set_attributes("10mbit", "100ms") # from `r1` to `h1`
64
65# Set the packet loss to p13, p31, p32, p23 and p14 on the link from `r1` to `r2`.
66etr1b.set_packet_loss_state("10%", "3%", "2%", "5%", "4%", True)
67
68# Set default routes in `h1` and `h2`. Additionally, set default routes in
69# `r1` and `r2` so that the packets that cannot be forwarded based on the
70# entries in their routing table are sent via a default interface.
71h1.add_route("DEFAULT", eth1)
72h2.add_route("DEFAULT", eth2)
73r1.add_route("DEFAULT", etr1b)
74r2.add_route("DEFAULT", etr2a)
75
76# `Ping` from `h1` to `h2`.
77h1.ping(eth2.address, packets=20)
9. packet-ping-preload-point-to-point-3.py¶
This program emulates a point to point network that connects two hosts h1
and h2
via two routers r1
and r2
. 20 ping packets are sent from h1
to
h2
. It is similar to examples/address-helpers/ah-point-to-point-3.py
.
Out of 20 ping packets that are sent, the first 10 ping packets are sent
simultaneously without waiting for a reply from h2
, and the remaining 10
ping packets are sent one by one. The success/failure of all ping packets is
reported. The ping preload option is used for sending the first 10 packets.
1# SPDX-License-Identifier: GPL-2.0-only
2# Copyright (c) 2019-2022 NITK Surathkal
3
4########################
5# SHOULD BE RUN AS ROOT
6########################
7from nest.topology import *
8from nest.topology.network import Network
9from nest.topology.address_helper import AddressHelper
10
11# This program emulates a point to point network that connects two hosts `h1`
12# and `h2` via two routers `r1` and `r2`. 20 ping packets are sent from `h1` to
13# `h2`. It is similar to `examples/address-helpers/ah-point-to-point-3.py`.
14# Out of 20 ping packets that are sent, the first 10 ping packets are sent
15# simultaneously without waiting for a reply from `h2`, and the remaining 10
16# ping packets are sent one by one. The success/failure of all ping packets is
17# reported. The ping preload option is used for sending the first 10 packets.
18
19##############################################################################
20# Network Topology #
21# #
22# 5mbit, 5ms --> 5mbit, 5ms --> 5mbit, 5ms --> #
23# h1 -------------------- r1 -------------------- r2 -------------------- h2 #
24# <-- 10mbit, 100ms <-- 10mbit, 100ms <-- 10mbit, 100ms #
25# #
26##############################################################################
27
28# Create two hosts `h1` and `h2`, and two routers `r1` and `r2`
29h1 = Node("h1")
30h2 = Node("h2")
31r1 = Router("r1")
32r2 = Router("r2")
33
34# Set the IPv4 address for the networks, and not the interfaces.
35# We will use the `AddressHelper` later to assign addresses to the interfaces.
36# Note: this example has three networks: one on the left of `r1`, second
37# between the two routers, and third on the right of `r2`.
38n1 = Network("192.168.1.0/24") # network on the left of `r1`
39n2 = Network("192.168.2.0/24") # network between two routers
40n3 = Network("192.168.3.0/24") # network on the right of `r2`
41
42# Connect `h1` to `r1`, `r1` to `r2`, and then `r2` to `h2`
43# `eth1` and `eth2` are the interfaces at `h1` and `h2`, respectively.
44# `etr1a` is the first interface at `r1` which connects it with `h1`
45# `etr1b` is the second interface at `r1` which connects it with `r2`
46# `etr2a` is the first interface at `r2` which connects it with `r1`
47# `etr2b` is the second interface at `r2` which connects it with `h2`
48(eth1, etr1a) = connect(h1, r1, network=n1)
49(etr1b, etr2a) = connect(r1, r2, network=n2)
50(etr2b, eth2) = connect(r2, h2, network=n3)
51
52# Assign IPv4 addresses to all the interfaces in the network.
53AddressHelper.assign_addresses()
54
55# Set the link attributes: `h1` --> `r1` --> `r2` --> `h2`
56eth1.set_attributes("5mbit", "5ms") # from `h1` to `r1`
57etr1b.set_attributes("5mbit", "5ms") # from `r1` to `r2`
58etr2b.set_attributes("5mbit", "5ms") # from `r2` to `h2`
59
60# Set the link attributes: `h2` --> `r2` --> `r1` --> `h1`
61eth2.set_attributes("10mbit", "100ms") # from `h2` to `r2`
62etr2a.set_attributes("10mbit", "100ms") # from `r2` to `r1`
63etr1a.set_attributes("10mbit", "100ms") # from `r1` to `h1`
64
65# Set default routes in `h1` and `h2`. Additionally, set default routes in
66# `r1` and `r2` so that the packets that cannot be forwarded based on the
67# entries in their routing table are sent via a default interface.
68h1.add_route("DEFAULT", eth1)
69h2.add_route("DEFAULT", eth2)
70r1.add_route("DEFAULT", etr1b)
71r2.add_route("DEFAULT", etr2a)
72
73# `Ping` from `h1` to `h2`.
74# Send 20 ping packets from h1 to h2, but send 10 of them with the preload
75# option set so that they are sent out as fast as possible, without waiting
76# for a ping response.
77h1.ping(eth2.address, preload=10, packets=20)
10. packet-reordering-point-to-point-3.py¶
This program emulates point to point networks that connect two hosts h1
and
h2
via two routers r1
and r2
. 20 ping packets are sent from h1
to
h2
, and the success/failure of these packets is reported. It is similar to
ah-point-to-point-3.py
in examples/address-helpers
. This program shows the
emulation of packet reordering for a chosen percentage of packets.
1# SPDX-License-Identifier: GPL-2.0-only
2# Copyright (c) 2019-2022 NITK Surathkal
3
4########################
5# SHOULD BE RUN AS ROOT
6########################
7from nest.topology import *
8from nest.topology.network import Network
9from nest.topology.address_helper import AddressHelper
10
11# This program emulates point to point networks that connect two hosts `h1` and
12# `h2` via two routers `r1` and `r2`. 20 ping packets are sent from `h1` to
13# `h2`, and the success/failure of these packets is reported. It is similar to
14# `ah-point-to-point-3.py` in `examples/address-helpers`. This program shows the
15# emulation of packet reordering for a chosen percentage of packets.
16
17##############################################################################
18# Network Topology #
19# #
20# #
21# Delay : 20ms #
22# Reorder rate : 25% #
23# gap : 5 #
24# 5mbit, 5ms --> 5mbit, 5ms --> 5mbit, 5ms --> #
25# h1 -------------------- r1 -------------------- r2 -------------------- h2 #
26# <-- 10mbit, 100ms <-- 10mbit, 100ms <-- 10mbit, 100ms #
27# #
28##############################################################################
29
30# Create two hosts `h1` and `h2`, and two routers `r1` and `r2`
31h1 = Node("h1")
32h2 = Node("h2")
33r1 = Router("r1")
34r2 = Router("r2")
35
36# Set the IPv4 address for the networks, and not the interfaces.
37# We will use the `AddressHelper` later to assign addresses to the interfaces.
38# Note: this example has three networks: one on the left of `r1`, second
39# between the two routers, and third on the right of `r2`.
40n1 = Network("192.168.1.0/24") # network on the left of `r1`
41n2 = Network("192.168.2.0/24") # network between two routers
42n3 = Network("192.168.3.0/24") # network on the right of `r2`
43
44# Connect `h1` to `r1`, `r1` to `r2`, and then `r2` to `h2`
45# `eth1` and `eth2` are the interfaces at `h1` and `h2`, respectively.
46# `etr1a` is the first interface at `r1` which connects it with `h1`
47# `etr1b` is the second interface at `r1` which connects it with `r2`
48# `etr2a` is the first interface at `r2` which connects it with `r1`
49# `etr2b` is the second interface at `r2` which connects it with `h2`
50(eth1, etr1a) = connect(h1, r1, network=n1)
51(etr1b, etr2a) = connect(r1, r2, network=n2)
52(etr2b, eth2) = connect(r2, h2, network=n3)
53
54# Assign IPv4 addresses to all the interfaces in the network.
55AddressHelper.assign_addresses()
56
57# Set the link attributes: `h1` --> `r1` --> `r2` --> `h2`
58eth1.set_attributes("5mbit", "5ms") # from `h1` to `r1`
59etr1b.set_attributes("5mbit", "5ms") # from `r1` to `r2`
60etr2b.set_attributes("5mbit", "5ms") # from `r2` to `h2`
61
62# Set the link attributes: `h2` --> `r2` --> `r1` --> `h1`
63eth2.set_attributes("10mbit", "100ms") # from `h2` to `r2`
64etr2a.set_attributes("10mbit", "100ms") # from `r2` to `r1`
65etr1a.set_attributes("10mbit", "100ms") # from `r1` to `h1`
66
67# Set a delay of 20ms and a packet reorder rate of 25% and gap 5
68# on the link from `r1` to `r2`.
69# Note: Delay must be specified to emulate packet reordering.
70eth1.set_packet_reordering("20ms", "25%", gap=5) # Gap is optional
71
72# Set default routes in `h1` and `h2`. Additionally, set default routes in
73# `r1` and `r2` so that the packets that cannot be forwarded based on the
74# entries in their routing table are sent via a default interface.
75h1.add_route("DEFAULT", eth1)
76h2.add_route("DEFAULT", eth2)
77r1.add_route("DEFAULT", etr1b)
78r2.add_route("DEFAULT", etr2a)
79
80# `Ping` from `h1` to `h2`.
81# Note: ping preload option must be used to see the reordered packet sequence
82# in the output.
83h1.ping(eth2.address, packets=1) # build ARP table before using preload.
84h1.ping(eth2.address, preload=10, packets=15)