Examples to demonstrate the support of static routing in NeST

This directory contains the examples to demonstrate how to use static routing to populate routing tables for a network in NeST.

1. static-routing-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 ah-point-to-point-2.py available in examples/address-helpers, the only difference is that we populate the routing tables by constructing a spanning tree of the network using Depth First Search (DFS). A new package called RoutingHelper is 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
10from nest.routing.routing_helper import RoutingHelper
11
12# This program emulates point to point networks that connect two hosts `h1`
13# and `h2` via a router `r1`. Five ping packets are sent from `h1` to `h2`,
14# and the success/failure of these packets is reported. This program is
15# similar to `ah-point-to-point-2.py` available in `examples/address-helpers`,
16# the only difference is that we populate the routing tables by constructing a
17# spanning tree of the network using Depth First Search (DFS). A new package
18# called `RoutingHelper` is imported in this program (Line 10 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# Run a Depth First Search (DFS) to populate routing tables. These routes
59# remain fixed throughout the experiment.
60RoutingHelper(protocol="static").populate_routing_tables()
61
62# `Ping` from `h1` to `h2`.
63h1.ping(eth2.address)

2. static-routing-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 ah-point-to-point-3.py available in examples/address-helpers, the only difference is that we populate the routing tables by constructing a spanning tree of the network using Depth First Search (DFS). A new package called RoutingHelper is 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
10from nest.routing.routing_helper import RoutingHelper
11
12# This program emulates point to point networks that connect two hosts `h1` and
13# `h2` via two routers `r1` and `r2`. Five ping packets are sent from `h1` to
14# `h2`, and the success/failure of these packets is reported. It is similar to
15# `ah-point-to-point-3.py` available in `examples/address-helpers`, the only
16# difference is that we populate the routing tables by constructing a spanning
17# tree of the network using Depth First Search (DFS). A new package called
18# `RoutingHelper` is imported in this program (Line 10 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# Run a Depth First Search (DFS) to populate routing tables. These routes
67# remain fixed throughout the experiment.
68RoutingHelper(protocol="static").populate_routing_tables()
69
70# `Ping` from `h1` to `h2`.
71h1.ping(eth2.address)