A Discrete-Event Network Simulator
API
simple-routing-ping6.py
Go to the documentation of this file.
1 #
2 # Copyright (c) 2008-2009 Strasbourg University
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 2 as
6 # published by the Free Software Foundation;
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 #
17 # Author: David Gross <gdavid.devel@gmail.com>
18 # Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
19 #
20 
21 #
22 # Network topology:
23 #
24 # n0 r n1
25 # | _ |
26 # ====|_|====
27 # router
28 #
29 
30 try:
31  from ns import ns
32 except ModuleNotFoundError:
33  raise SystemExit(
34  "Error: ns3 Python module not found;"
35  " Python bindings may not be enabled"
36  " or your PYTHONPATH might not be properly configured"
37  )
38 
39 
40 def main(argv):
41  cmd = ns.CommandLine()
42 
43  cmd.Parse(argv)
44 
45  # Create nodes
46  print("Create nodes")
47 
48  all = ns.NodeContainer(3)
49  net1 = ns.NodeContainer()
50  net1.Add(all.Get(0))
51  net1.Add(all.Get(1))
52  net2 = ns.NodeContainer()
53  net2.Add(all.Get(1))
54  net2.Add(all.Get(2))
55 
56  # Create IPv6 Internet Stack
57  internetv6 = ns.InternetStackHelper()
58  internetv6.Install(all)
59 
60  # Create channels
61  csma = ns.csma.CsmaHelper()
62  csma.SetChannelAttribute("DataRate", ns.DataRateValue(ns.DataRate(5000000)))
63  csma.SetChannelAttribute("Delay", ns.TimeValue(ns.MilliSeconds(2)))
64  d1 = csma.Install(net1)
65  d2 = csma.Install(net2)
66 
67  # Create networks and assign IPv6 Addresses
68  print("Addressing")
69  ipv6 = ns.Ipv6AddressHelper()
70  ipv6.SetBase(ns.Ipv6Address("2001:1::"), ns.Ipv6Prefix(64))
71  i1 = ipv6.Assign(d1)
72  i1.SetForwarding(1, True)
73  i1.SetDefaultRouteInAllNodes(1)
74  ipv6.SetBase(ns.Ipv6Address("2001:2::"), ns.Ipv6Prefix(64))
75  i2 = ipv6.Assign(d2)
76  i2.SetForwarding(0, True)
77  i2.SetDefaultRouteInAllNodes(0)
78 
79  # Create a Ping6 application to send ICMPv6 echo request from n0 to n1 via r
80  print("Application")
81  packetSize = 1024
82  maxPacketCount = 5
83  interPacketInterval = ns.Seconds(1.0)
84  # ping = ns.PingHelper(i2.GetAddress(1, 1).ConvertTo())
85  ping = ns.PingHelper(i2.GetAddress(1, 1).ConvertTo())
86 
87  # ping6.SetLocal(i1.GetAddress(0, 1))
88  # ping6.SetRemote(i2.GetAddress(1, 1))
89 
90  ping.SetAttribute("Count", ns.UintegerValue(maxPacketCount))
91  ping.SetAttribute("Interval", ns.TimeValue(interPacketInterval))
92  ping.SetAttribute("Size", ns.UintegerValue(packetSize))
93 
94  apps = ping.Install(ns.NodeContainer(net1.Get(0)))
95  apps.Start(ns.Seconds(2.0))
96  apps.Stop(ns.Seconds(20.0))
97 
98  print("Tracing")
99  ascii = ns.AsciiTraceHelper()
100  csma.EnableAsciiAll(ascii.CreateFileStream("simple-routing-ping6.tr"))
101  csma.EnablePcapAll("simple-routing-ping6", True)
102 
103  # Run Simulation
104  ns.Simulator.Run()
105  ns.Simulator.Destroy()
106 
107 
108 if __name__ == "__main__":
109  import sys
110 
111  main(sys.argv)