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 import ns.internet_apps
31 import ns.core
32 import ns.csma
33 import ns.internet
34 import ns.network
35 
36 def main(argv):
37 
38  cmd = ns.core.CommandLine();
39 
40  cmd.Parse(argv);
41 
42  # Create nodes
43  print ("Create nodes")
44  n0 = ns.network.Node();
45  r = ns.network.Node();
46  n1 = ns.network.Node();
47 
48  net1 = ns.network.NodeContainer();
49  net1.Add(n0);
50  net1.Add(r);
51  net2 = ns.network.NodeContainer();
52  net2.Add(r);
53  net2.Add(n1);
54  all = ns.network.NodeContainer();
55  all.Add(n0);
56  all.Add(r);
57  all.Add(n1);
58 
59  # Create IPv6 Internet Stack
60  internetv6 = ns.internet.InternetStackHelper();
61  internetv6.Install(all);
62 
63  # Create channels
64  csma = ns.csma.CsmaHelper();
65  csma.SetChannelAttribute("DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)));
66  csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.MilliSeconds(2)));
67  d1 = csma.Install(net1);
68  d2 = csma.Install(net2);
69 
70  # Create networks and assign IPv6 Addresses
71  print ("Addressing")
72  ipv6 = ns.internet.Ipv6AddressHelper();
73  ipv6.SetBase(ns.network.Ipv6Address("2001:1::"), ns.network.Ipv6Prefix(64));
74  i1 = ipv6.Assign(d1);
75  i1.SetForwarding(1, True);
76  i1.SetDefaultRouteInAllNodes(1);
77  ipv6.SetBase(ns.network.Ipv6Address("2001:2::"), ns.network.Ipv6Prefix(64));
78  i2 = ipv6.Assign(d2);
79  i2.SetForwarding(0, True);
80  i2.SetDefaultRouteInAllNodes(0);
81 
82  # Create a Ping6 application to send ICMPv6 echo request from n0 to n1 via r
83  print ("Application")
84  packetSize = 1024;
85  maxPacketCount = 5;
86  interPacketInterval = ns.core.Seconds(1.);
87  ping6 = ns.internet_apps.Ping6Helper();
88 
89  ping6.SetLocal(i1.GetAddress(0, 1));
90  ping6.SetRemote(i2.GetAddress(1, 1));
91 
92  ping6.SetAttribute("MaxPackets", ns.core.UintegerValue(maxPacketCount));
93  ping6.SetAttribute("Interval", ns.core.TimeValue(interPacketInterval));
94  ping6.SetAttribute("PacketSize", ns.core.UintegerValue(packetSize));
95 
96  apps = ping6.Install(ns.network.NodeContainer(net1.Get(0)));
97  apps.Start(ns.core.Seconds(2.0));
98  apps.Stop(ns.core.Seconds(20.0));
99 
100  print ("Tracing")
101  ascii = ns.network.AsciiTraceHelper()
102  csma.EnableAsciiAll(ascii.CreateFileStream("simple-routing-ping6.tr"))
103  csma.EnablePcapAll("simple-routing-ping6", True)
104 
105  # Run Simulation
106  ns.core.Simulator.Run()
107  ns.core.Simulator.Destroy()
108 
109 if __name__ == '__main__':
110  import sys
111  main(sys.argv)
112