Examples to understand the support of IPv6 addressing in NeST

This directory contains the following examples to understand how IPv6 addresses can be used in NeST. We recommend that you walk through these examples in the same order as they are presented.

NOTE
Duplicate Address Detection (DAD) feature of IPv6 in Linux is disabled by default in NeST. It can be enabled by using the config as shown here. However, you might have to manually add delays during the IPv6 address assignment in NeST if you enable the DAD feature. Hence, enabling the DAD feature in NeST is recommended only for those users who are familiar about the functionality of DAD and can tweak the network experiment as required.

1. ipv6-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 point-to-point-1.py available in examples/basic-examples, the only difference is that IPv6 addresses are used 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 *
 8
 9# This program emulates a point to point network between two hosts `h1` and
10# `h2`. Five ping packets are sent from `h1` to `h2`, and the success/failure
11# of these packets is reported. It is similar to `point-to-point-1.py`
12# available in `examples/basic-examples`, the only difference is that IPv6
13# addresses are used in this program.
14
15#################################
16#       Network Topology        #
17#                               #
18#          5mbit, 5ms -->       #
19#   h1 ------------------- h2   #
20#       <-- 10mbit, 100ms       #
21#                               #
22#################################
23
24# Create two hosts `h1` and `h2`
25h1 = Node("h1")
26h2 = Node("h2")
27
28# Connect the above two hosts using a veth (virtual Ethernet) pair
29(eth1, eth2) = connect(h1, h2)
30
31# Assign IPv6 address to both the interfaces.
32# We assume that the IPv6 address of this network is `2001::1/122`
33eth1.set_address("2001::1:1/122")
34eth2.set_address("2001::1:2/122")
35
36# Set the link attributes: `h1` --> `h2` and `h2` --> `h1`
37eth1.set_attributes("5mbit", "5ms")
38eth2.set_attributes("10mbit", "100ms")
39
40# `Ping` from `h1` to `h2`.
41h1.ping(eth2.address)

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

3. ipv6-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. This example extends the ipv6-point-to-point-2.py example. It is similar to point-to-point-3.py available in examples/basic-examples, the only difference is that IPv6 addresses are used 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 *
 8
 9# This program emulates point to point networks that connect two hosts `h1`
10# and `h2` via two routers `r1` and `r2`. Five ping packets are sent from `h1`
11# to `h2`, and the success/failure of these packets is reported. It is similar
12# to `point-to-point-3.py` available in `examples/basic-examples`, the only
13# difference is that IPv6 addresses are used in this program.
14
15##############################################################################
16#                              Network Topology                              #
17#                                                                            #
18#        5mbit, 5ms -->          5mbit, 5ms -->          5mbit, 5ms -->      #
19# h1 -------------------- r1 -------------------- r2 -------------------- h2 #
20#     <-- 10mbit, 100ms       <-- 10mbit, 100ms       <-- 10mbit, 100ms      #
21#                                                                            #
22##############################################################################
23
24# Create two hosts `h1` and `h2`, and two routers `r1` and `r2`
25h1 = Node("h1")
26h2 = Node("h2")
27r1 = Router("r1")
28r2 = Router("r2")
29
30# Connect `h1` to `r1`, `r1` to `r2`, and then `r2` to `h2`
31# `eth1` and `eth2` are the interfaces at `h1` and `h2`, respectively.
32# `etr1a` is the first interface at `r1` which connects it with `h1`
33# `etr1b` is the second interface at `r1` which connects it with `r2`
34# `etr2a` is the first interface at `r2` which connects it with `r1`
35# `etr2b` is the second interface at `r2` which connects it with `h2`
36(eth1, etr1a) = connect(h1, r1)
37(etr1b, etr2a) = connect(r1, r2)
38(etr2b, eth2) = connect(r2, h2)
39
40# Assign IPv6 addresses to all the interfaces.
41# Note: this example has three networks: one on the left of `r1`, second
42# between the two routers, and third on the right of `r2`.
43# Assign IPv6 addresses to interfaces in the network which is on the left of
44# `r1`. We assume that the IPv6 address of this network is `2001::1/122`
45eth1.set_address("2001::1:1/122")
46etr1a.set_address("2001::1:2/122")
47
48# Assign IPv6 addresses to interfaces in the network which is between the two
49# routers. We assume that the IPv6 address of this network is `2001::2/122`
50etr1b.set_address("2001::2:1/122")
51etr2a.set_address("2001::2:2/122")
52
53# Assign IPv6 addresses to interfaces in the network which is on the right of
54# `r2`. We assume that the IPv6 address of this network is `2001::3/122`
55etr2b.set_address("2001::3:1/122")
56eth2.set_address("2001::3:2/122")
57
58# Set the link attributes: `h1` --> `r1` --> `r2` --> `h2`
59eth1.set_attributes("5mbit", "5ms")  # from `h1` to `r1`
60etr1b.set_attributes("5mbit", "5ms")  # from `r1` to `r2`
61etr2b.set_attributes("5mbit", "5ms")  # from `r2` to `h2`
62
63# Set the link attributes: `h2` --> `r2` --> `r1` --> `h1`
64eth2.set_attributes("10mbit", "100ms")  # from `h2` to `r2`
65etr2a.set_attributes("10mbit", "100ms")  # from `r2` to `r1`
66etr1a.set_attributes("10mbit", "100ms")  # from `r1` to `h1`
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)

4. ipv6-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 IPv6 addresses are used 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 *
 8
 9# This program emulates a Local Area Network (LAN). Four hosts: `h1` to `h4`
10# are connected using a switch `s1`. Five ping packets are sent from `h1` to
11# `h2` and five ping packets from `h3` to `h4`. The success/failure of these
12# packets is reported. It is similar to `simple-lan.py` available in
13# `examples/basic-examples`, the only difference is that IPv6 addresses are
14# used in this program.
15
16######################################################################
17#                          Network Topology                          #
18#                                                                    #
19#  h1               h2                          h3               h4  #
20#  |                |                           |                 |  #
21#  ----------------------------- s1 -------------------------------  #
22#                    <------ 100mbit, 1ms ------>                    #
23#                                                                    #
24######################################################################
25
26# Create four hosts `h1` to `h4`, and one switch `s1`
27h1 = Node("h1")
28h2 = Node("h2")
29h3 = Node("h3")
30h4 = Node("h4")
31s1 = Switch("s1")
32
33# Connect all the four hosts to the switch
34# `eth1` to `eth4` are the interfaces at `h1` to `h4`, respectively.
35# `ets1a` is the first interface at `s1` which connects it with `h1`
36# `ets1b` is the second interface at `s1` which connects it with `h2`
37# `ets1c` is the third interface at `s1` which connects it with `h3`
38# `ets1d` is the fourth interface at `s1` which connects it with `h4`
39(eth1, ets1a) = connect(h1, s1)
40(eth2, ets1b) = connect(h2, s1)
41(eth3, ets1c) = connect(h3, s1)
42(eth4, ets1d) = connect(h4, s1)
43
44# Assign IPv6 addresses to all the interfaces.
45# We assume that the IPv6 address of this network is `2001::1/122`.
46# Note: IP addresses should not be assigned to the interfaces on the switch.
47eth1.set_address("2001::1:1/122")
48eth2.set_address("2001::1:2/122")
49eth3.set_address("2001::1:3/122")
50eth4.set_address("2001::1:4/122")
51
52# Set the link attributes
53eth1.set_attributes("100mbit", "1ms")
54eth2.set_attributes("100mbit", "1ms")
55eth3.set_attributes("100mbit", "1ms")
56eth4.set_attributes("100mbit", "1ms")
57
58# `Ping` from `h1` to `h2`, and `h3` to `h4`.
59h1.ping(eth2.address)
60h3.ping(eth4.address)

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

This program emulates two Local Area Networks (LANs) connected directly to each other. LAN-1 consists of 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. This program extends ipv6-simple-lan.py. It is similar to two-lans-connected-directly.py available in examples/basic-examples, the only difference is that IPv6 addresses are used 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 *
 8
 9# This program emulates two Local Area Networks (LANs) connected directly to
10# each other. LAN-1 consists three hosts `h1` to `h3` connected to switch `s1`,
11# and LAN-2 consists three hosts `h4` to `h6` connected to switch `s2`.
12# Switches `s1` and `s2` are connected to each other. Five ping packets are
13# sent from `h1` to `h4`, five from `h2` to `h5`, and lastly, five `h3` to
14# `h6`. The success/failure of these packets is reported. It is similar to
15# `two-lans-connected-directly.py` available in `examples/basic-examples`, the
16# only difference is that IPv6 addresses are used in this program.
17
18#########################################################
19#                    Network Topology                   #
20#           LAN-1                      LAN-2            #
21#   h1 ---------------            ---------------- h4   #
22#                     \         /                       #
23#   h2 --------------- s1 ---- s2 ---------------- h5   #
24#                     /         \                       #
25#   h3 ---------------            ---------------- h6   #
26#             <------ 100mbit, 1ms ------>              #
27#                                                       #
28#########################################################
29
30# Create six hosts `h1` to `h6`, and two switches `s1` and `s2`
31h1 = Node("h1")
32h2 = Node("h2")
33h3 = Node("h3")
34h4 = Node("h4")
35h5 = Node("h5")
36h6 = Node("h6")
37s1 = Switch("s1")
38s2 = Switch("s2")
39
40# Create LAN-1: Connect hosts `h1`, `h2` and `h3` to switch `s1`
41# `eth1` to `eth3` are the interfaces at `h1` to `h3`, respectively.
42# `ets1a` is the first interface at `s1` which connects it with `h1`
43# `ets1b` is the second interface at `s1` which connects it with `h2`
44# `ets1c` is the third interface at `s1` which connects it with `h3`
45(eth1, ets1a) = connect(h1, s1)
46(eth2, ets1b) = connect(h2, s1)
47(eth3, ets1c) = connect(h3, s1)
48
49# Create LAN-2: Connect hosts `h4`, `h5` and `h6` to switch `s2`
50# `eth4` to `eth6` are the interfaces at `h4` to `h6`, respectively.
51# `ets2a` is the first interface at `s2` which connects it with `h4`
52# `ets2b` is the second interface at `s2` which connects it with `h5`
53# `ets2c` is the third interface at `s2` which connects it with `h6`
54(eth4, ets2a) = connect(h4, s2)
55(eth5, ets2b) = connect(h5, s2)
56(eth6, ets2c) = connect(h6, s2)
57
58# Connect switches `s1` and `s2`
59# `ets1d` is the fourth interface at `s1` which connects it with `s2`
60# `ets2d` is the fourth interface at `s2` which connects it with `s1`
61(ets1d, ets2d) = connect(s1, s2)
62
63# Assign IPv6 addresses to all the interfaces.
64# We assume that the IPv6 address of this network is `2001::1/122`.
65# Note: IP addresses should not be assigned to the interfaces on the switches.
66eth1.set_address("2001::1:1/122")
67eth2.set_address("2001::1:2/122")
68eth3.set_address("2001::1:3/122")
69eth4.set_address("2001::1:4/122")
70eth5.set_address("2001::1:5/122")
71eth6.set_address("2001::1:6/122")
72
73# Set the link attributes
74eth1.set_attributes("100mbit", "1ms")
75eth2.set_attributes("100mbit", "1ms")
76eth3.set_attributes("100mbit", "1ms")
77eth4.set_attributes("100mbit", "1ms")
78eth5.set_attributes("100mbit", "1ms")
79eth6.set_attributes("100mbit", "1ms")
80
81# `Ping` from `h1` to `h4`, `h2` to `h5`, and `h3` to `h6`.
82h1.ping(eth4.address)
83h2.ping(eth5.address)
84h3.ping(eth6.address)

6. ipv6-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 IPv6 addresses are used 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 *
  8
  9# This program emulates two Local Area Networks (LANs) connected via a router.
 10# LAN-1 consists three hosts `h1` to `h3` connected to switch `s1`, and
 11# LAN-2 consists three hosts `h4` to `h6` connected to switch `s2`. Switches
 12# `s1` and `s2` are connected to each other via a router `r1`. Five ping
 13# packets are sent from `h1` to `h4`, five from `h2` to `h5` and lastly, five
 14# from `h3` to `h6`. The success/failure of these packets is reported. It is
 15# similar to `two-lans-connected-via-router.py` available in
 16# `examples/basic-examples`, the only difference is that IPv6 addresses are
 17# used in this program.
 18
 19##################################################################
 20#                       Network Topology                         #
 21#           LAN-1                           LAN-2                #
 22#   h1 ---------------                      --------------- h4   #
 23#                     \                    /                     #
 24#   h2 --------------- s1 ----- r1 ----- s2 --------------- h5   #
 25#                     /                    \                     #
 26#   h3 ---------------                      --------------- h6   #
 27#   <- 100mbit, 1ms ->  <- 10mbit, 10ms ->  <- 100mbit, 1ms ->   #
 28#                                                                #
 29##################################################################
 30
 31# Create six hosts 'h1' to 'h6'
 32h1 = Node("h1")
 33h2 = Node("h2")
 34h3 = Node("h3")
 35h4 = Node("h4")
 36h5 = Node("h5")
 37h6 = Node("h6")
 38
 39# Create two switches 's1' and 's2'
 40s1 = Switch("s1")
 41s2 = Switch("s2")
 42
 43# Create a router 'r1'
 44r1 = Router("r1")
 45
 46# Create LAN-1: Connect hosts `h1`, `h2` and `h3` to switch `s1`
 47# `eth1` to `eth3` are the interfaces at `h1` to `h3`, respectively.
 48# `ets1a` is the first interface at `s1` which connects it with `h1`
 49# `ets1b` is the second interface at `s1` which connects it with `h2`
 50# `ets1c` is the third interface at `s1` which connects it with `h3`
 51(eth1, ets1a) = connect(h1, s1)
 52(eth2, ets1b) = connect(h2, s1)
 53(eth3, ets1c) = connect(h3, s1)
 54
 55# Create LAN-2: Connect 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(eth4, ets2a) = connect(h4, s2)
 61(eth5, ets2b) = connect(h5, s2)
 62(eth6, ets2c) = connect(h6, s2)
 63
 64# Connect switches `s1` and `s2` to router `r1`
 65# `ets1d` is the fourth interface at `s1` which connects it with `r1`
 66# `ets2d` is the fourth interface at `s2` which connects it with `r1`
 67# `etr1a` is the first interface at `r1` which connects it with `s1`
 68# `etr1b` is the second interface at `r1` which connects it with `s2`
 69(ets1d, etr1a) = connect(s1, r1)
 70(ets2d, etr1b) = connect(s2, r1)
 71
 72# Assign IPv6 addresses to all the interfaces of network on the left of `r1`
 73# We assume that the IPv6 address of this network is `2001::1/122`.
 74# Assign IPv6 addresses to the hosts
 75eth1.set_address("2001::1:1/122")
 76eth2.set_address("2001::1:2/122")
 77eth3.set_address("2001::1:3/122")
 78
 79# Assign IPv6 address to the switch `s1` on the left of `r1`
 80s1.set_address("2001::1:4/122")
 81
 82# Assign IPv6 address to the left interface of `r1`
 83etr1a.set_address("2001::1:5/122")
 84
 85# Assign IPv6 addresses to all the interfaces of network on the right of `r1`
 86# We assume that the IPv6 address of this network is `2001::2/122`.
 87# Assign IPv6 addresses to the hosts
 88eth4.set_address("2001::2:1/122")
 89eth5.set_address("2001::2:2/122")
 90eth6.set_address("2001::2:3/122")
 91
 92# Assign IPv6 address to the switch `s2` on the right of `r1`
 93s2.set_address("2001::2:4/122")
 94
 95# Assign IPv6 address to the right interface of `r1`
 96etr1b.set_address("2001::2:5/122")
 97
 98# Set the attributes of the links between hosts and switches
 99eth1.set_attributes("100mbit", "1ms")
100eth2.set_attributes("100mbit", "1ms")
101eth3.set_attributes("100mbit", "1ms")
102eth4.set_attributes("100mbit", "1ms")
103eth5.set_attributes("100mbit", "1ms")
104eth6.set_attributes("100mbit", "1ms")
105
106# Set the attributes of the links between `r1` and switches
107etr1a.set_attributes("10mbit", "10ms")
108etr1b.set_attributes("10mbit", "10ms")
109
110# Set the default routes for hosts `h1`, `h2` and `h3` via `etr1a`
111h1.add_route("DEFAULT", eth1, etr1a.address)
112h2.add_route("DEFAULT", eth2, etr1a.address)
113h3.add_route("DEFAULT", eth3, etr1a.address)
114
115# Set the default routes for hosts `h4`, `h5` and `h6` via `etr1b`
116h4.add_route("DEFAULT", eth4, etr1b.address)
117h5.add_route("DEFAULT", eth5, etr1b.address)
118h6.add_route("DEFAULT", eth6, etr1b.address)
119
120# `Ping` from `h1` to `h4`, `h2` to `h5`, and `h3` to `h6`.
121h1.ping(eth4.address)
122h2.ping(eth5.address)
123h3.ping(eth6.address)

7. ipv6-v4-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 ipv6-point-to-point-3.py available in examples/ipv6, the only difference is that both IPv4 and IPv6 addresses are assigned to hosts and routers. Five ping packets are sent from h1 to h2, first with IPv4 addresses and then with IPv6 addresses. The success/failure of these packets is reported.

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 *
 8
 9# This program emulates point to point networks that connect two hosts `h1`
10# and `h2` via two routers `r1` and `r2`. It is similar to
11# `ipv6-point-to-point-3.py` available in `examples/ipv6`, the only difference
12# is that both IPv4 and IPv6 addresses are assigned to hosts and routers. Five
13# ping packets are sent from `h1` to `h2`, first with IPv4 addresses and then
14# with IPv6 addresses. The success/failure of these packets is reported.
15
16##############################################################################
17#                              Network Topology                              #
18#                                                                            #
19#        5mbit, 5ms -->          5mbit, 5ms -->          5mbit, 5ms -->      #
20# h1 -------------------- r1 -------------------- r2 -------------------- h2 #
21#     <-- 10mbit, 100ms       <-- 10mbit, 100ms       <-- 10mbit, 100ms      #
22#                                                                            #
23##############################################################################
24
25# Create two hosts `h1` and `h2`, and two routers `r1` and `r2`
26h1 = Node("h1")
27h2 = Node("h2")
28r1 = Router("r1")
29r2 = Router("r2")
30
31# Connect `h1` to `r1`, `r1` to `r2`, and then `r2` to `h2`
32# `eth1` and `eth2` are the interfaces at `h1` and `h2`, respectively.
33# `etr1a` is the first interface at `r1` which connects it with `h1`
34# `etr1b` is the second interface at `r1` which connects it with `r2`
35# `etr2a` is the first interface at `r2` which connects it with `r1`
36# `etr2b` is the second interface at `r2` which connects it with `h2`
37(eth1, etr1a) = connect(h1, r1)
38(etr1b, etr2a) = connect(r1, r2)
39(etr2b, eth2) = connect(r2, h2)
40
41# Assign IPv4 and IPv6 addresses to all the interfaces.
42# Note: this example has three networks: one on the left of `r1`, second
43# between the two routers, and third on the right of `r2`.
44# Assign IPv4 and IPv6 addresses to interfaces in the network which is on the
45# left of `r1`. We assume that the IPv4 and IPv6 address of this network is
46# `192.168.1.0/24` and `2001::1/122`, respectively.
47# Note: `set_address` API accepts a list of IP addresses, passed as strings.
48# IPv4 and IPv6 addresses can be passed in any order to `set_address` API.
49eth1.set_address(["192.168.1.1/24", "2001::1:1/122"])
50etr1a.set_address(["192.168.1.2/24", "2001::1:2/122"])
51
52# Assign IPv4 and IPv6 addresses to interfaces in the network which is between
53# the two routers. We assume that the IPv4 and IPv6 address of this network is
54# `192.168.2.0/24` and `2001::2/122`, respectively.
55etr1b.set_address(["192.168.2.1/24", "2001::2:1/122"])
56etr2a.set_address(["192.168.2.2/24", "2001::2:2/122"])
57
58# Assign IPv4 and IPv6 addresses to interfaces in the network which is on the
59# right of `r2`. We assume that the IPv4 and IPv6 address of this network is
60# `192.168.3.0/24` and `2001::3/122`, respectively.
61etr2b.set_address(["192.168.3.1/24", "2001::3:1/122"])
62eth2.set_address(["192.168.3.2/24", "2001::3:2/122"])
63
64# Set the link attributes: `h1` --> `r1` --> `r2` --> `h2`
65eth1.set_attributes("5mbit", "5ms")  # from `h1` to `r1`
66etr1b.set_attributes("5mbit", "5ms")  # from `r1` to `r2`
67etr2b.set_attributes("5mbit", "5ms")  # from `r2` to `h2`
68
69# Set the link attributes: `h2` --> `r2` --> `r1` --> `h1`
70eth2.set_attributes("10mbit", "100ms")  # from `h2` to `r2`
71etr2a.set_attributes("10mbit", "100ms")  # from `r2` to `r1`
72etr1a.set_attributes("10mbit", "100ms")  # from `r1` to `h1`
73
74# Set default routes in `h1` and `h2`. Additionally, set default routes in
75# `r1` and `r2` so that the packets that cannot be forwarded based on the
76# entries in their routing table are sent via next hop addresses specified.
77# Note: the first parameter passed to the `get_address` API indicates whether
78# IPv4 address should be used or not, and the second parameter indicates
79# whether IPv6 address should be used or not.
80# Default routes for IPv4:
81h1.add_route("DEFAULT", eth1, eth1.pair.get_address(True, False))
82h2.add_route("DEFAULT", eth2, eth2.pair.get_address(True, False))
83r1.add_route("DEFAULT", etr1b, etr1b.pair.get_address(True, False))
84r2.add_route("DEFAULT", etr2a, etr2a.pair.get_address(True, False))
85
86# Default routes for IPv6:
87h1.add_route("DEFAULT", eth1, eth1.pair.get_address(False, True))
88h2.add_route("DEFAULT", eth2, eth2.pair.get_address(False, True))
89r1.add_route("DEFAULT", etr1b, etr1b.pair.get_address(False, True))
90r2.add_route("DEFAULT", etr2a, etr2a.pair.get_address(False, True))
91
92# `Ping` from `h1` to `h2` twice, first with IPv4 and then with IPv6
93h1.ping(eth2.get_address(True, False))  # Use IPv4 address
94h1.ping(eth2.get_address(False, True))  # Use IPv6 address