Examples to demonstrate the usage of Address Helpers in NeST

This directory contains the following examples to understand how address helpers can be used in NeST to automatically assign IPv4/IPv6 addresses. These helpers can avoid the overhead of manually assigning the addresses. We recommend that you walk through these examples in the same order as they are presented.

1. ah-point-to-point-1.py

This program emulates a point to point network between two hosts h1 and h2. Five ping packets are sent from h1 to h2, and the success/failure of these packets is reported. This program is similar to point-to-point-1.py available in examples/basic-examples, the only difference is that we use an address helper in this program to assign IPv4 addresses to interfaces instead of manually assigning them. Note that two packages: Network and AddressHelper are imported in this program.

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.topology.network import Network
 9from nest.topology.address_helper import AddressHelper
10
11# This program emulates a point to point network between two hosts `h1` and
12# `h2`. Five ping packets are sent from `h1` to `h2`, and the success/failure
13# of these packets is reported. This program is similar to `point-to-point-1.py`
14# available in `examples/basic-examples`, the only difference is that we use an
15# address helper in this program to assign IPv4 addresses to interfaces instead
16# of manually assigning them. Note that two packages: `Network` and
17# `AddressHelper` are imported in this program (Lines 8-9 above).
18
19#################################
20#       Network Topology        #
21#                               #
22#          5mbit, 5ms -->       #
23#   h1 ------------------- h2   #
24#       <-- 10mbit, 100ms       #
25#                               #
26#################################
27
28# Create two hosts `h1` and `h2`
29h1 = Node("h1")
30h2 = Node("h2")
31
32# Set the IPv4 address for the network, and not the interfaces.
33# We will use the `AddressHelper` later to assign addresses to the interfaces.
34n1 = Network("192.168.1.0/24")
35
36# Connect the above two hosts using a veth pair. Note that `connect` API in
37# this program takes `network` as an additional parameter. The following line
38# implies that `eth1` and `eth2` interfaces on `h1` and `h2`, respectively are
39# in the same network `n1`.
40(eth1, eth2) = connect(h1, h2, network=n1)
41
42# Assign IPv4 addresses to all the interfaces in the network.
43AddressHelper.assign_addresses()
44
45# Set the link attributes: `h1` --> `h2` and `h2` --> `h1`
46eth1.set_attributes("5mbit", "5ms")
47eth2.set_attributes("10mbit", "100ms")
48
49# `Ping` from `h1` to `h2`.
50h1.ping(eth2.address)

2. ah-point-to-point-2.py

This program emulates point to point networks that connect two hosts h1 and h2 via a router r1. Five ping packets are sent from h1 to h2, and the success/failure of these packets is reported. This program is similar to point-to-point-2.py available in examples/basic-examples, the only difference is that we use an address helper in this program to assign IPv4 addresses to interfaces instead of manually assigning them. Note that two packages: Network and AddressHelper are imported in this program.

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.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`
12# and `h2` via a router `r1`. Five ping packets are sent from `h1` to `h2`, and
13# the success/failure of these packets is reported. This program is similar to
14# `point-to-point-2.py` available in `examples/basic-examples`, the only
15# difference is that we use an address helper in this program to assign IPv4
16# addresses to interfaces instead of manually assigning them. Note that two
17# packages: `Network` and `AddressHelper` are imported in this program
18# (Lines 8-9 above).
19
20##########################################################
21#                   Network Topology                     #
22#                                                        #
23#          5mbit, 5ms -->          5mbit, 5ms -->        #
24#   h1 -------------------- r1 -------------------- h2   #
25#       <-- 10mbit, 100ms        <-- 10mbit, 100ms       #
26#                                                        #
27##########################################################
28
29# Create two hosts `h1` and `h2`, and one router `r1`
30h1 = Node("h1")
31h2 = Node("h2")
32r1 = Router("r1")  # Internally, `Router` API enables IP forwarding in `r1`
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 two networks, one each on either side of `r1`.
37n1 = Network("192.168.1.0/24")  # network on the left side of `r1`
38n2 = Network("192.168.2.0/24")  # network on the right side of `r1`
39
40# Connect `h1` to `r1` (left side), and then `r1` (right side) to `h2`.
41# `eth1` and `eth2` are the interfaces at `h1` and `h2`, respectively.
42# `etr1a` is the first interface at `r1` which connects it with `h1`
43# `etr1b` is the second interface at `r1` which connects it with `h2`
44(eth1, etr1a) = connect(h1, r1, network=n1)
45(etr1b, eth2) = connect(r1, h2, network=n2)
46
47# Assign IPv4 addresses to all the interfaces in the network.
48AddressHelper.assign_addresses()
49
50# Set the link attributes: `h1` --> `r1` --> `h2`
51eth1.set_attributes("5mbit", "5ms")  # from `h1` to `r1`
52etr1b.set_attributes("5mbit", "5ms")  # from `r1` to `h2`
53
54# Set the link attributes: `h2` --> `r1` --> `h1`
55eth2.set_attributes("10mbit", "100ms")  # from `h2` to `r1`
56etr1a.set_attributes("10mbit", "100ms")  # from `r1` to `h1`
57
58# Set default routes in `h1` and `h2` so that the packets that cannot be
59# forwarded based on the entries in their routing table are sent via a
60# default interface.
61h1.add_route("DEFAULT", eth1)
62h2.add_route("DEFAULT", eth2)
63
64# `Ping` from `h1` to `h2`.
65h1.ping(eth2.address)

3. ah-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. Five ping packets are sent from h1 to h2, and the success/failure of these packets is reported. It is similar to point-to-point-3.py available in examples/basic-examples, the only difference is that we use an address helper in this program to assign IPv4 addresses to interfaces instead of manually assigning them. Note that two packages: Network and AddressHelper are imported in this program.

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.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`. Five ping packets are sent from `h1` to
13# `h2`, and the success/failure of these packets is reported. It is similar to
14# `point-to-point-3.py` available in `examples/basic-examples`, the only
15# difference is that we use an address helper in this program to assign IPv4
16# addresses to interfaces instead of manually assigning them. Note that two
17# packages: `Network` and `AddressHelper` are imported in this program
18# (Lines 8-9 above).
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`.
75h1.ping(eth2.address)

4. ah-simple-lan.py

This program emulates a Local Area Network (LAN). Four hosts: h1 to h4 are connected using a switch s1. Five ping packets are sent from h1 to h2, and five ping packets from h3 to h4. The success/failure of these packets is reported. It is similar to simple-lan.py available in examples/basic-examples, the only difference is that we use an address helper in this program to assign IPv4 addresses to interfaces instead of manually assigning them. Note that two packages: Network and AddressHelper are imported in this program. Since all the interfaces in this example belong to the same network, we demonstrate a simpler approach to use the Network API.

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.topology.network import Network
 9from nest.topology.address_helper import AddressHelper
10
11# This program emulates a Local Area Network (LAN). Four hosts: `h1` to `h4`
12# are connected using a switch `s1`. Five ping packets are sent from `h1` to
13# `h2`, and five ping packets from `h3` to `h4`. The success/failure of these
14# packets are reported. It is similar to `simple-lan.py` available in
15# `examples/basic-examples`, the only difference is that we use an address
16# helper in this program to assign IPv4 addresses to interfaces instead of
17# manually assigning them. Note that two packages: `Network` and
18# `AddressHelper` are imported in this program (Lines 8-9 above).
19
20######################################################################
21#                          Network Topology                          #
22#                                                                    #
23#  h1               h2                          h3               h4  #
24#  |                |                           |                 |  #
25#  ----------------------------- s1 -------------------------------  #
26#                    <------ 100mbit, 1ms ------>                    #
27#                                                                    #
28######################################################################
29
30# Create four hosts `h1` to `h4`, and one switch `s1`
31h1 = Node("h1")
32h2 = Node("h2")
33h3 = Node("h3")
34h4 = Node("h4")
35s1 = Switch("s1")
36
37# Set the IPv4 address for the network, and not the interfaces.
38# We will use the `AddressHelper` later to assign addresses to the interfaces.
39n1 = Network("192.168.1.0/24")
40
41# Connect all the four hosts to the switch
42# `eth1` to `eth4` are the interfaces at `h1` to `h4`, respectively.
43# `ets1a` is the first interface at `s1` which connects it with `h1`
44# `ets1b` is the second interface at `s1` which connects it with `h2`
45# `ets1c` is the third interface at `s1` which connects it with `h3`
46# `ets1d` is the fourth interface at `s1` which connects it with `h4`
47#
48# Note: Since all the interfaces in this example belong to the same
49# network, we demonstrate a simpler approach to use the `Network` API.
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# `Ping` from `h1` to `h2`, and `h3` to `h4`.
66h1.ping(eth2.address)
67h3.ping(eth4.address)

5. ah-two-lans-connected-directly.py

This program emulates 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. Five ping packets are sent from h1 to h4, five from h2 to h5, and lastly, five from h3 to h6. The success/failure of these packets is reported. It is similar to two-lans-directly-connected.py available in examples/basic-examples, the only difference is that we use an address helper in this program to assign IPv4 addresses to interfaces instead of manually assigning them. Note that two packages: Network and AddressHelper are imported in this program. Since all the interfaces in this example belong to the same network, we demonstrate a simpler approach to use the Network API.

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.topology.network import Network
 9from nest.topology.address_helper import AddressHelper
10
11# This program emulates two Local Area Networks (LANs) connected directly to
12# each other. LAN-1 consists three hosts `h1` to `h3` connected to switch `s1`,
13# and LAN-2 consists three hosts `h4` to `h6` connected to switch `s2`.
14# Switches `s1` and `s2` are connected to each other. Five ping packets are
15# sent from `h1` to `h4`, five from `h2` to `h5`, and lastly, five from `h3` to
16# `h6`. The success/failure of these packets is reported. It is similar to
17# `two-lans-directly-connected.py` available in `examples/basic-examples`, the
18# only difference is that we use an address helper in this program to assign
19# IPv4 addresses to interfaces instead of manually assigning them. Note that
20# two packages: `Network` and `AddressHelper` are imported in this program
21# (Lines 8-9 above).
22
23#########################################################
24#                    Network Topology                   #
25#           LAN-1                      LAN-2            #
26#   h1 ---------------            ---------------- h4   #
27#                     \         /                       #
28#   h2 --------------- s1 ---- s2 ---------------- h5   #
29#                     /         \                       #
30#   h3 ---------------            ---------------- h6   #
31#             <------ 100mbit, 1ms ------>              #
32#                                                       #
33#########################################################
34
35# Create six hosts `h1` to `h6`, and two switches `s1` and `s2`
36h1 = Node("h1")
37h2 = Node("h2")
38h3 = Node("h3")
39h4 = Node("h4")
40h5 = Node("h5")
41h6 = Node("h6")
42s1 = Switch("s1")
43s2 = Switch("s2")
44
45# Set the IPv4 address for the network, and not the interfaces.
46# We will use the `AddressHelper` later to assign addresses to the interfaces.
47n1 = Network("192.168.1.0/24")
48
49# LAN-1 connects hosts `h1`, `h2` and `h3` to switch `s1`
50# `eth1` to `eth3` are the interfaces at `h1` to `h3`, respectively.
51# `ets1a` is the first interface at `s1` which connects it with `h1`
52# `ets1b` is the second interface at `s1` which connects it with `h2`
53# `ets1c` is the third interface at `s1` which connects it with `h3`
54#
55# LAN-2 connects hosts `h4`, `h5` and `h6` to switch `s2`
56# `eth4` to `eth6` are the interfaces at `h4` to `h6`, respectively.
57# `ets2a` is the first interface at `s2` which connects it with `h4`
58# `ets2b` is the second interface at `s2` which connects it with `h5`
59# `ets2c` is the third interface at `s2` which connects it with `h6`
60#
61# LAN-1 and LAN-2 are connected via 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#
65# Note: Since all the interfaces in this example belong to the same
66# network, we demonstrate a simpler approach to use the `Network` API.
67with n1:
68    (eth1, ets1a) = connect(h1, s1)
69    (eth2, ets1b) = connect(h2, s1)
70    (eth3, ets1c) = connect(h3, s1)
71    (eth4, ets2a) = connect(h4, s2)
72    (eth5, ets2b) = connect(h5, s2)
73    (eth6, ets2c) = connect(h6, s2)
74    (ets1d, ets2d) = connect(s1, s2)
75
76# Assign IPv4 addresses to all the interfaces in the network.
77AddressHelper.assign_addresses()
78
79# Set the link attributes
80eth1.set_attributes("100mbit", "1ms")
81eth2.set_attributes("100mbit", "1ms")
82eth3.set_attributes("100mbit", "1ms")
83eth4.set_attributes("100mbit", "1ms")
84eth5.set_attributes("100mbit", "1ms")
85eth6.set_attributes("100mbit", "1ms")
86
87# `Ping` from `h1` to `h4`, `h2` to `h5`, and `h3` to `h6`.
88h1.ping(eth4.address)
89h2.ping(eth5.address)
90h3.ping(eth6.address)

6. ah-two-lans-connected-via-router.py

This program emulates two Local Area Networks (LANs) connected via a router. 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 via a router r1. Five ping packets are sent from h1 to h4, five from h2 to h5 and lastly, five from h3 to h6. The success/failure of these packets is reported. It is similar to two-lans-connected-via-router.py available in examples/basic-examples, the only difference is that we use an address helper in this program to assign IPv4 addresses to interfaces instead of manually assigning them. Note that two packages: Network and AddressHelper are imported in this program.

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.topology.network import Network
 9from nest.topology.address_helper import AddressHelper
10
11# This program emulates two Local Area Networks (LANs) connected via a router.
12# LAN-1 consists three hosts `h1` to `h3` connected to switch `s1`, and LAN-2
13# consists three hosts `h4` to `h6` connected to switch `s2`. Switches `s1`
14# and `s2` are connected to each other via a router `r1`. Five ping packets are
15# sent from `h1` to `h4`, five from `h2` to `h5` and lastly, five from `h3` to
16# `h6`. The success/failure of these packets is reported. It is similar to
17# `two-lans-connected-via-router.py` available in `examples/basic-examples`,
18# the only difference is that we use an address helper in this program to
19# assign IPv4 addresses to interfaces instead of manually assigning them. Note
20# that two packages: `Network` and `AddressHelper` are imported in this program
21# (Lines 8-9 above).
22
23##################################################################
24#                       Network Topology                         #
25#           LAN-1                           LAN-2                #
26#   h1 ---------------                      --------------- h4   #
27#                     \                    /                     #
28#   h2 --------------- s1 ----- r1 ----- s2 --------------- h5   #
29#                     /                    \                     #
30#   h3 ---------------                      --------------- h6   #
31#   <- 100mbit, 1ms ->  <- 10mbit, 10ms ->  <- 100mbit, 1ms ->   #
32#                                                                #
33##################################################################
34
35# Create six hosts 'h1' to 'h6'
36h1 = Node("h1")
37h2 = Node("h2")
38h3 = Node("h3")
39h4 = Node("h4")
40h5 = Node("h5")
41h6 = Node("h6")
42
43# Create two switches 's1' and 's2'
44s1 = Switch("s1")
45s2 = Switch("s2")
46
47# Create a router 'r1'
48r1 = Router("r1")
49
50# Create two networks
51n1 = Network("192.168.1.0/24")  # network on the left of `r1`
52n2 = Network("192.168.2.0/24")  # network on the right of `r1`
53
54# Create LAN-1: Connect hosts `h1`, `h2` and `h3` to switch `s1`
55(eth1, ets1a) = connect(h1, s1, network=n1)
56(eth2, ets1b) = connect(h2, s1, network=n1)
57(eth3, ets1c) = connect(h3, s1, network=n1)
58
59# Create LAN-2: Connect hosts `h4`, `h5` and `h6` to switch `s2`
60(eth4, ets2a) = connect(h4, s2, network=n2)
61(eth5, ets2b) = connect(h5, s2, network=n2)
62(eth6, ets2c) = connect(h6, s2, network=n2)
63
64# Connect switches `s1` and `s2` to router `r1`
65(ets1d, etr1a) = connect(s1, r1, network=n1)
66(ets2d, etr1b) = connect(s2, r1, network=n2)
67
68# Assign IPv4 addresses to all the interfaces and switches in the network.
69AddressHelper.assign_addresses()
70
71# Set the attributes of the links between hosts and switches
72eth1.set_attributes("100mbit", "1ms")
73eth2.set_attributes("100mbit", "1ms")
74eth3.set_attributes("100mbit", "1ms")
75eth4.set_attributes("100mbit", "1ms")
76eth5.set_attributes("100mbit", "1ms")
77eth6.set_attributes("100mbit", "1ms")
78
79# Set the attributes of the links between `r1` and switches
80etr1a.set_attributes("10mbit", "10ms")
81etr1b.set_attributes("10mbit", "10ms")
82
83# Set the default routes for hosts `h1`, `h2` and `h3` via `etr1a`
84h1.add_route("DEFAULT", eth1, etr1a.address)
85h2.add_route("DEFAULT", eth2, etr1a.address)
86h3.add_route("DEFAULT", eth3, etr1a.address)
87
88# Set the default routes for hosts `h4`, `h5` and `h6` via `etr1b`
89h4.add_route("DEFAULT", eth4, etr1b.address)
90h5.add_route("DEFAULT", eth5, etr1b.address)
91h6.add_route("DEFAULT", eth6, etr1b.address)
92
93# `Ping` from `h1` to `h4`, `h2` to `h5`, and `h3` to `h6`.
94h1.ping(eth4.address)
95h2.ping(eth5.address)
96h3.ping(eth6.address)

7. ipv6-ah-point-to-point-1.py

This program emulates a point to point network between two hosts h1 and h2. Five ping packets are sent from h1 to h2, and the success/failure of these packets is reported. It is similar to ipv6-point-to-point-1.py available in examples/ipv6, the only difference is that we use an address helper in this program to assign IPv6 addresses to interfaces instead of manually assigning them. Note that two packages: Network and AddressHelper are imported in this program.

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.topology.network import Network
 9from nest.topology.address_helper import AddressHelper
10
11# This program emulates a point to point network between two hosts `h1` and
12# `h2`. Five ping packets are sent from `h1` to `h2`, and the success/failure
13# of these packets is reported. It is similar to `ipv6-point-to-point-1.py`
14# available in `examples/ipv6`, the only difference is that we use an address
15# helper in this program to assign IPv6 addresses to interfaces instead of
16# manually assigning them. Note that two packages: `Network` and
17# `AddressHelper` are imported in this program (Lines 8-9 above).
18
19#################################
20#       Network Topology        #
21#                               #
22#          5mbit, 5ms -->       #
23#   h1 ------------------- h2   #
24#       <-- 10mbit, 100ms       #
25#                               #
26#################################
27
28# Create two hosts `h1` and `h2`
29h1 = Node("h1")
30h2 = Node("h2")
31
32# Set the IPv6 address for the network, and not the interfaces.
33# We will use the `AddressHelper` later to assign addresses to the interfaces.
34n1 = Network("2001:1::/122")
35
36# Connect the above two hosts using a veth pair. Note that `connect` API in
37# this program takes `network` as an additional parameter. The following line
38# implies that `eth1` and `eth2` interfaces on `h1` and `h2`, respectively are
39# in the same network `n1`.
40(eth1, eth2) = connect(h1, h2, network=n1)
41
42# Assign IPv6 addresses to all the interfaces in the network.
43AddressHelper.assign_addresses()
44
45# Set the link attributes: `h1` --> `h2` and `h2` --> `h1`
46eth1.set_attributes("5mbit", "5ms")
47eth2.set_attributes("10mbit", "100ms")
48
49# `Ping` from `h1` to `h2`.
50h1.ping(eth2.address)

8. ipv6-ah-point-to-point-2.py

This program emulates point to point networks that connect two hosts h1 and h2 via a router r1. Five ping packets are sent from h1 to h2, and the success/failure of these packets is reported. This program is similar to ipv6-point-to-point-2.py available in examples/ipv6, the only difference is that we use an address helper in this program to assign IPv6 addresses to interfaces instead of manually assigning them. Note that two packages: Network and AddressHelper are imported in this program.

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.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`
12# and `h2` via a router `r1`. Five ping packets are sent from `h1` to `h2`, and
13# the success/failure of these packets is reported. This program is similar to
14# `ipv6-point-to-point-2.py` available in `examples/ipv6`, the only difference
15# is that we use an address helper in this program to assign IPv6 addresses to
16# interfaces instead of manually assigning them. Note that two packages:
17# `Network` and `AddressHelper` are imported in this program (Lines 8-9 above).
18
19##########################################################
20#                   Network Topology                     #
21#                                                        #
22#          5mbit, 5ms -->          5mbit, 5ms -->        #
23#   h1 -------------------- r1 -------------------- h2   #
24#       <-- 10mbit, 100ms        <-- 10mbit, 100ms       #
25#                                                        #
26##########################################################
27
28# Create two hosts `h1` and `h2`, and one router `r1`
29h1 = Node("h1")
30h2 = Node("h2")
31r1 = Router("r1")  # Internally, `Router` API enables IP forwarding in `r1`
32
33# Set the IPv6 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 two networks, one each on either side of `r1`.
36n1 = Network("2001:1::/122")  # network on the left side of `r1`
37n2 = Network("2001:2::/122")  # network on the right side of `r1`
38
39# Connect `h1` to `r1` (left side), and then `r1` (right side) to `h2`.
40# `eth1` and `eth2` are the interfaces at `h1` and `h2`, respectively.
41# `etr1a` is the first interface at `r1` which connects it with `h1`
42# `etr1b` is the second interface at `r1` which connects it with `h2`
43(eth1, etr1a) = connect(h1, r1, network=n1)
44(etr1b, eth2) = connect(r1, h2, network=n2)
45
46# Assign IPv6 addresses to all the interfaces in the network.
47AddressHelper.assign_addresses()
48
49# Set the link attributes: `h1` --> `r1` --> `h2`
50eth1.set_attributes("5mbit", "5ms")  # from `h1` to `r1`
51etr1b.set_attributes("5mbit", "5ms")  # from `r1` to `h2`
52
53# Set the link attributes: `h2` --> `r1` --> `h1`
54eth2.set_attributes("10mbit", "100ms")  # from `h2` to `r1`
55etr1a.set_attributes("10mbit", "100ms")  # from `r1` to `h1`
56
57# Set default routes in `h1` and `h2` so that the packets that cannot be
58# forwarded based on the entries in their routing table are sent via a
59# default interface.
60h1.add_route("DEFAULT", eth1)
61h2.add_route("DEFAULT", eth2)
62
63# `Ping` from `h1` to `h2`.
64h1.ping(eth2.address)

9. ipv6-ah-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. Five ping packets are sent from h1 to h2, and the success/failure of these packets is reported. It is similar to ipv6-point-to-point-3.py available in examples/ipv6, the only difference is that we use an address helper in this program to assign IPv6 addresses to interfaces instead of manually assigning them. Note that two packages: Network and AddressHelper are imported in this program.

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.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`. Five ping packets are sent from `h1` to
13# `h2`, and the success/failure of these packets is reported. It is similar to
14# `ipv6-point-to-point-3.py` available in `examples/ipv6`, the only difference
15# is that we use an address helper in this program to assign IPv6 addresses to
16# interfaces instead of manually assigning them. Note that two packages:
17# `Network` and `AddressHelper` are imported in this program (Lines 8-9 above).
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 IPv6 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("2001:1::/122")  # network on the left of `r1`
39n2 = Network("2001:2::/122")  # network between two routers
40n3 = Network("2001:3::/122")  # 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 IPv6 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`.
74h1.ping(eth2.address)

10. ipv6-ah-simple-lan.py

This program emulates a Local Area Network (LAN). Four hosts: h1 to h4 are connected using a switch s1. Five ping packets are sent from h1 to h2, and five ping packets from h3 to h4. The success/failure of these packets is reported. It is similar to ipv6-simple-lan.py available in examples/ipv6, the only difference is that we use an address helper in this program to assign IPv6 addresses to interfaces instead of manually assigning them. Note that two packages: Network and AddressHelper are imported in this program. Since all the interfaces in this example belong to the same network, we demonstrate a simpler approach to use the Network API.

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.topology.network import Network
 9from nest.topology.address_helper import AddressHelper
10
11# This program emulates a Local Area Network (LAN). Four hosts: `h1` to `h4`
12# are connected using a switch `s1`. Five ping packets are sent from `h1` to
13# `h2`, and another from `h3` to `h4`. The success/failure of these packets is
14# reported. It is similar to `ipv6-simple-lan.py` available in `examples/ipv6`,
15# the only difference is that we use an address helper in this program to
16# assign IPv6 addresses to interfaces instead of manually assigning them. Note
17# that two packages: `Network` and `AddressHelper` are imported in this program
18# (Lines 8-9 above).
19
20######################################################################
21#                          Network Topology                          #
22#                                                                    #
23#  h1               h2                          h3               h4  #
24#  |                |                           |                 |  #
25#  ----------------------------- s1 -------------------------------  #
26#                    <------ 100mbit, 1ms ------>                    #
27#                                                                    #
28######################################################################
29
30# Create four hosts `h1` to `h4`, and one switch `s1`
31h1 = Node("h1")
32h2 = Node("h2")
33h3 = Node("h3")
34h4 = Node("h4")
35s1 = Switch("s1")
36
37# Set the IPv6 address for the network, and not the interfaces.
38# We will use the `AddressHelper` later to assign addresses to the interfaces.
39n1 = Network("2001:1::/122")
40
41# Connect all the four hosts to the switch
42# `eth1` to `eth4` are the interfaces at `h1` to `h4`, respectively.
43# `ets1a` is the first interface at `s1` which connects it with `h1`
44# `ets1b` is the second interface at `s1` which connects it with `h2`
45# `ets1c` is the third interface at `s1` which connects it with `h3`
46# `ets1d` is the fourth interface at `s1` which connects it with `h4`
47#
48# Note: Since all the interfaces in this example belong to the same
49# network, we demonstrate a simpler approach to use the `Network` API.
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 IPv6 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# `Ping` from `h1` to `h2`, and `h3` to `h4`.
66h1.ping(eth2.address)
67h3.ping(eth4.address)

11. ipv6-ah-two-lans-connected-directly.py

This program emulates 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. Five ping packets are sent from h1 to h4, five from h2 to h5, and lastly, five from h3 to h6. The success/failure of these packets is reported. It is similar to ipv6-two-lans-connected-directly.py available in examples/ipv6, the only difference is that we use an address helper in this program to assign IPv6 addresses to interfaces instead of manually assigning them. Note that two packages: Network and AddressHelper are imported in this program. Since all the interfaces in this example belong to the same network, we demonstrate a simpler approach to use the Network API.

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.topology.network import Network
 9from nest.topology.address_helper import AddressHelper
10
11# This program emulates two Local Area Networks (LANs) connected directly to
12# each other. LAN-1 consists three hosts `h1` to `h3` connected to switch `s1`,
13# and LAN-2 consists three hosts `h4` to `h6` connected to switch `s2`.
14# Switches `s1` and `s2` are connected to each other. Five ping packets are
15# sent from `h1` to `h4`, five from `h2` to `h5`, and lastly, five from `h3` to
16# `h6`. The success/failure of these packets is reported. It is similar to
17# `ipv6-two-lans-connected-directly.py` available in `examples/ipv6`, the only
18# difference is that we use an address helper in this program to assign IPv6
19# addresses to interfaces instead of manually assigning them. Note that two
20# packages: `Network` and `AddressHelper` are imported in this program (Lines
21# 8-9 above).
22
23#########################################################
24#                    Network Topology                   #
25#           LAN-1                      LAN-2            #
26#   h1 ---------------            ---------------- h4   #
27#                     \         /                       #
28#   h2 --------------- s1 ---- s2 ---------------- h5   #
29#                     /         \                       #
30#   h3 ---------------            ---------------- h6   #
31#             <------ 100mbit, 1ms ------>              #
32#                                                       #
33#########################################################
34
35# Create six hosts `h1` to `h6`, and two switches `s1` and `s2`
36h1 = Node("h1")
37h2 = Node("h2")
38h3 = Node("h3")
39h4 = Node("h4")
40h5 = Node("h5")
41h6 = Node("h6")
42s1 = Switch("s1")
43s2 = Switch("s2")
44
45# Set the IPv6 address for the network, and not the interfaces.
46# We will use the `AddressHelper` later to assign addresses to the interfaces.
47n1 = Network("2001:1::/122")
48
49# LAN-1 connects hosts `h1`, `h2` and `h3` to switch `s1`
50# `eth1` to `eth3` are the interfaces at `h1` to `h3`, respectively.
51# `ets1a` is the first interface at `s1` which connects it with `h1`
52# `ets1b` is the second interface at `s1` which connects it with `h2`
53# `ets1c` is the third interface at `s1` which connects it with `h3`
54#
55# LAN-2 connects hosts `h4`, `h5` and `h6` to switch `s2`
56# `eth4` to `eth6` are the interfaces at `h4` to `h6`, respectively.
57# `ets2a` is the first interface at `s2` which connects it with `h4`
58# `ets2b` is the second interface at `s2` which connects it with `h5`
59# `ets2c` is the third interface at `s2` which connects it with `h6`
60#
61# LAN-1 and LAN-2 are connected via 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#
65# Note: Since all the interfaces in this example belong to the same
66# network, we demonstrate a simpler approach to use the `Network` API.
67with n1:
68    (eth1, ets1a) = connect(h1, s1)
69    (eth2, ets1b) = connect(h2, s1)
70    (eth3, ets1c) = connect(h3, s1)
71    (eth4, ets2a) = connect(h4, s2)
72    (eth5, ets2b) = connect(h5, s2)
73    (eth6, ets2c) = connect(h6, s2)
74    (ets1d, ets2d) = connect(s1, s2)
75
76# Assign IPv6 addresses to all the interfaces in the network.
77AddressHelper.assign_addresses()
78
79# Set the link attributes
80eth1.set_attributes("100mbit", "1ms")
81eth2.set_attributes("100mbit", "1ms")
82eth3.set_attributes("100mbit", "1ms")
83eth4.set_attributes("100mbit", "1ms")
84eth5.set_attributes("100mbit", "1ms")
85eth6.set_attributes("100mbit", "1ms")
86
87# `Ping` from `h1` to `h4`, `h2` to `h5`, and `h3` to `h6`.
88h1.ping(eth4.address)
89h2.ping(eth5.address)
90h3.ping(eth6.address)