Examples to demonstrate how to generate UDP traffic in NeST

This directory contains the following examples to understand how UDP traffic can be generated in NeST. Flow API is used in these examples to configure flows between a pair of hosts. We recommend that you walk through these examples in the same order as they are presented.

1. udp-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. It is similar to the ah-point-to-point-3.py example in examples/address-helpers. Instead of ping, one UDP flow is configured from h1 to h2. The links between h1 to r1 and between r2 to h2 are edge links. The link between r1 and r2 is the bottleneck link with lesser bandwidth and higher propagation delays. Address helper is used in this program to assign IPv4 addresses.

This program runs for 50 seconds and creates a new directory called udp-point-to-point-3(date-timestamp)_dump. It contains a README which provides details about the sub-directories and files within this directory. For this program, see the plots in iperf3 and ping sub-directories.

Source code

 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.experiment import *
 9from nest.topology.network import Network
10from nest.topology.address_helper import AddressHelper
11
12# This program emulates point to point networks that connect two hosts `h1`
13# and `h2` via two routers `r1` and `r2`. It is similar to the
14# ah-point-to-point-3.py example in `examples/address-helpers`. Instead of
15# `ping`, one UDP flow is configured from `h1` to `h2`. The links between
16# `h1` to `r1` and between `r2` to `h2` are edge links. The link between `r1`
17# and `r2` is the bottleneck link with lesser bandwidth and higher propagation
18# delays.
19
20##############################################################################
21#                              Network Topology                              #
22#                                                                            #
23#      1000mbit, 1ms -->       10mbit, 10ms -->       1000mbit, 1ms -->      #
24# h1 -------------------- r1 -------------------- r2 -------------------- h2 #
25#     <-- 1000mbit, 1ms       <-- 10mbit, 10ms        <-- 1000mbit, 1ms      #
26#                                                                            #
27##############################################################################
28
29# This program runs for 50 seconds and creates a new directory called
30# `udp-point-to-point-3(date-timestamp)_dump`. It contains a `README` which
31# provides details about the sub-directories and files within this directory.
32# For this program, see the plots in `iperf3` and `ping` sub-directories.
33
34# Create two hosts `h1` and `h2`, and two routers `r1` and `r2`
35h1 = Node("h1")
36h2 = Node("h2")
37r1 = Router("r1")
38r2 = Router("r2")
39
40# Set the IPv4 address for the networks, and not the interfaces.
41# We will use the `AddressHelper` later to assign addresses to the interfaces.
42n1 = Network("192.168.1.0/24")  # network on the left of `r1`
43n2 = Network("192.168.2.0/24")  # network between two routers
44n3 = Network("192.168.3.0/24")  # network on the right of `r2`
45
46# Connect `h1` to `r1`, `r1` to `r2`, and then `r2` to `h2`
47# `eth1` and `eth2` are the interfaces at `h1` and `h2`, respectively.
48# `etr1a` is the first interface at `r1` which connects it with `h1`
49# `etr1b` is the second interface at `r1` which connects it with `r2`
50# `etr2a` is the first interface at `r2` which connects it with `r1`
51# `etr2b` is the second interface at `r2` which connects it with `h2`
52(eth1, etr1a) = connect(h1, r1, network=n1)
53(etr1b, etr2a) = connect(r1, r2, network=n2)
54(etr2b, eth2) = connect(r2, h2, network=n3)
55
56# Assign IPv4 addresses to all the interfaces in the network.
57AddressHelper.assign_addresses()
58
59# Set the link attributes: `h1` --> `r1` --> `r2` --> `h2`
60eth1.set_attributes("1000mbit", "1ms")  # from `h1` to `r1`
61etr1b.set_attributes("10mbit", "10ms")  # from `r1` to `r2`
62etr2b.set_attributes("1000mbit", "1ms")  # from `r2` to `h2`
63
64# Set the link attributes: `h2` --> `r2` --> `r1` --> `h1`
65eth2.set_attributes("1000mbit", "1ms")  # from `h2` to `r2`
66etr2a.set_attributes("10mbit", "10ms")  # from `r2` to `r1`
67etr1a.set_attributes("1000mbit", "1ms")  # from `r1` to `h1`
68
69# Set default routes in `h1` and `h2`. Additionally, set default routes in
70# `r1` and `r2` so that the packets that cannot be forwarded based on the
71# entries in their routing table are sent via a default interface.
72h1.add_route("DEFAULT", eth1)
73h2.add_route("DEFAULT", eth2)
74r1.add_route("DEFAULT", etr1b)
75r2.add_route("DEFAULT", etr2a)
76
77# Set up an Experiment. This API takes the name of the experiment as a string.
78exp = Experiment("udp-point-to-point-3")
79
80# Configure a flow from `h1` to `h2`. We do not use it as a UDP flow yet.
81# The `Flow` API takes in the source node, destination node, destination IP
82# address, start and stop time of the flow, and the total number of flows.
83# In this program, start time is 0 seconds, stop time is 50 seconds and the
84# number of flows is 1.
85flow1 = Flow(h1, h2, eth2.get_address(), 0, 50, 1)
86
87# Use `flow1` as a UDP flow, and set the rate at which this flow
88# would send UDP packets.
89exp.add_udp_flow(flow1, target_bandwidth="12mbit")
90
91# Run the experiment
92exp.run()

2. udp-simple-lan.py

This program emulates a Local Area Network (LAN). Four hosts: h1 to h4 are connected using a switch s1. It is similar to the ah-simple-lan.py example in examples/address-helpers. Instead of ping, one UDP flow is configured from h1 to h2 and another from h3 to h4. Address helper is used in this program to assign IPv4 addresses.

This program runs for 50 seconds and creates a new directory called udp-simple-lan(date-timestamp)_dump. It contains a README which provides details about the sub-directories and files within this directory. For this program, see the plots in iperf3 and ping sub-directories.

Source code

 1# SPDX-License-Identifier: GPL-2.0-only
 2# Copyright (c) 2019-2021 NITK Surathkal
 3
 4########################
 5# SHOULD BE RUN AS ROOT
 6########################
 7from nest.topology import *
 8from nest.experiment import *
 9from nest.topology.network import Network
10from nest.topology.address_helper import AddressHelper
11
12# This program emulates a Local Area Network (LAN). Four hosts: `h1` to `h4`
13# are connected using a switch `s1`. It is similar to the `ah-simple-lan.py`
14# example in `examples/address-helpers`. Instead of `ping`, one UDP
15# flow is configured from `h1` to `h2` and another from `h3` to `h4`.
16# Address helper is used in this program to assign IPv4 addresses.
17
18######################################################################
19#                          Network Topology                          #
20#                                                                    #
21#  h1               h2                          h3               h4  #
22#  |                |                           |                 |  #
23#  ----------------------------- s1 -------------------------------  #
24#                    <------ 100mbit, 1ms ------>                    #
25#                                                                    #
26######################################################################
27
28# This program runs for 50 seconds and creates a new directory called
29# `udp-simple-lan(date-timestamp)_dump`. It contains a `README` which
30# provides details about the sub-directories and files within this directory.
31# For this program, see the plots in `iperf3` and `ping` sub-directories.
32
33# Create four hosts `h1` to `h4`, and one switch `s1`
34h1 = Node("h1")
35h2 = Node("h2")
36h3 = Node("h3")
37h4 = Node("h4")
38s1 = Switch("s1")
39
40# Set the IPv4 address for the network, and not the interfaces.
41# We will use the `AddressHelper` later to assign addresses to the interfaces.
42n1 = Network("192.168.1.0/24")
43
44# Connect all the four hosts to the switch
45# `eth1` to `eth4` are the interfaces at `h1` to `h4`, respectively.
46# `ets1a` is the first interface at `s1` which connects it with `h1`
47# `ets1b` is the second interface at `s1` which connects it with `h2`
48# `ets1c` is the third interface at `s1` which connects it with `h3`
49# `ets1d` is the fourth interface at `s1` which connects it with `h4`
50with n1:
51    (eth1, ets1a) = connect(h1, s1)
52    (eth2, ets1b) = connect(h2, s1)
53    (eth3, ets1c) = connect(h3, s1)
54    (eth4, ets1d) = connect(h4, s1)
55
56# Assign IPv4 addresses to all the interfaces in the network.
57AddressHelper.assign_addresses()
58
59# Set the link attributes
60eth1.set_attributes("100mbit", "1ms")
61eth2.set_attributes("100mbit", "1ms")
62eth3.set_attributes("100mbit", "1ms")
63eth4.set_attributes("100mbit", "1ms")
64
65# Set up an Experiment. This API takes the name of the experiment as a string.
66exp = Experiment("udp-simple-lan")
67
68# Configure one flow from `h1` to `h2` and another from `h3` to `h4`. We do not
69# use it as a UDP flow yet.
70flow1 = Flow(h1, h2, eth2.get_address(), 0, 50, 1)
71flow2 = Flow(h3, h4, eth4.get_address(), 0, 50, 1)
72
73# Use `flow1` and `flow2` as UDP flows, and set the rate at which these flows
74# would send UDP packets.
75exp.add_udp_flow(flow1, target_bandwidth="12mbit")
76exp.add_udp_flow(flow2, target_bandwidth="12mbit")
77
78# Run the experiment
79exp.run()

3. udp-set-server-options.py

This program emulates configuration of iperf3 server on two Local Area Networks (LANs) connected directly to each other. LAN-1 consists three hosts h1 to h3 connected to switch s1, and LAN-2 consists three hosts h4 to h6 connected to switch s2. Switches s1 and s2 are connected to each other directly. Two UDP flows are created from h1 to h4 and one UDP flow is created from h2 to h4. Host h1 sends packets to h4 for 10 sec. At h4 output is reported at an interval of 1 sec. After 5 sec of the start of the program h2 starts sending packets to h4 for 10 sec which is reported at an interval of 0.5 sec.

Source code

 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.experiment import Experiment, Flow
 9from nest.experiment.tools import Iperf3
10
11# This program emulates configuration of iperf3 server on two Local Area
12# Networks (LANs) connected directly to each other. LAN-1 consists three hosts
13# `h1` to `h3` connected to switch `s1`, and LAN-2 consists three hosts `h4` to
14# `h6` connected to switch `s2`. Switches `s1` and `s2` are connected to each
15# other. Two UDP flows are created from `h1` to `h4` and one UDP flow is
16# created from `h2` to `h4`. Host `h1` sends packets to `h4` for 10 sec. At `h4`
17# output is reported at an interval of 1 sec. After 5 sec of the start of the
18# program `h2` starts sending packets to `h4` for 10 sec which is reported at an
19# interval of 0.5 sec.
20
21#########################################################
22#                    Network Topology                   #
23#           LAN-1                      LAN-2            #
24#   h1 ---------------            ---------------- h4   #
25#                     \         /                       #
26#   h2 --------------- s1 ---- s2 ---------------- h5   #
27#                     /         \                       #
28#   h3 ---------------            ---------------- h6   #
29#             <------ 100mbit, 1ms ------>              #
30#                                                       #
31#########################################################
32
33# Create six hosts `h1` to `h6`, and two switches `s1` and `s2`
34h1 = Node("h1")
35h2 = Node("h2")
36h3 = Node("h3")
37h4 = Node("h4")
38h5 = Node("h5")
39h6 = Node("h6")
40s1 = Switch("s1")
41s2 = Switch("s2")
42
43# Create LAN-1: Connect hosts `h1`, `h2` and `h3` to switch `s1`
44# `eth1` to `eth3` are the interfaces at `h1` to `h3`, respectively.
45# `ets1a` is the first interface at `s1` which connects it with `h1`
46# `ets1b` is the second interface at `s1` which connects it with `h2`
47# `ets1c` is the third interface at `s1` which connects it with `h3`
48(eth1, ets1a) = connect(h1, s1)
49(eth2, ets1b) = connect(h2, s1)
50(eth3, ets1c) = connect(h3, s1)
51
52# Create LAN-2: Connect hosts `h4`, `h5` and `h6` to switch `s2`
53# `eth4` to `eth6` are the interfaces at `h4` to `h6`, respectively.
54# `ets2a` is the first interface at `s2` which connects it with `h4`
55# `ets2b` is the second interface at `s2` which connects it with `h5`
56# `ets2c` is the third interface at `s2` which connects it with `h6`
57(eth4, ets2a) = connect(h4, s2)
58(eth5, ets2b) = connect(h5, s2)
59(eth6, ets2c) = connect(h6, s2)
60
61# Connect switches `s1` and `s2`
62# `ets1d` is the fourth interface at `s1` which connects it with `s2`
63# `ets2d` is the fourth interface at `s2` which connects it with `s1`
64(ets1d, ets2d) = connect(s1, s2)
65
66# Assign IPv4 addresses to all the interfaces.
67# We assume that the IPv4 address of this network is `192.168.1.0/24`.
68# Note: IP addresses should not be assigned to the interfaces on the switches.
69eth1.set_address("192.168.1.1/24")
70eth2.set_address("192.168.1.2/24")
71eth3.set_address("192.168.1.3/24")
72eth4.set_address("192.168.1.4/24")
73eth5.set_address("192.168.1.5/24")
74eth6.set_address("192.168.1.6/24")
75
76# Set the link attributes
77eth1.set_attributes("100mbit", "1ms")
78eth2.set_attributes("100mbit", "1ms")
79eth3.set_attributes("100mbit", "1ms")
80eth4.set_attributes("100mbit", "1ms")
81eth5.set_attributes("100mbit", "1ms")
82eth6.set_attributes("100mbit", "1ms")
83
84# Assign  source, Destination, start time, end time and number of
85# parallel flows to each udp flows
86flow_udp_1 = Flow(h1, h4, eth4.get_address(), 0, 10, 2)
87flow_udp_2 = Flow(h2, h4, eth4.get_address(), 5, 15, 1)
88
89# iperf3_server_options API is used to configure iperf3 server options
90
91exp = Experiment("udp_flow")
92exp.add_udp_flow(
93    flow_udp_1, server_options=Iperf3.server_option(s_interval=0.5, port_no=2345)
94)
95exp.add_udp_flow(
96    flow_udp_2, client_options=Iperf3.client_option(interval=0.3, cport=1234)
97)
98exp.run()