File: simple-routing-ping6.py

package info (click to toggle)
ns3 3.26%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 117,520 kB
  • ctags: 72,063
  • sloc: cpp: 462,724; python: 364,339; perl: 8,720; ansic: 7,153; xml: 3,401; makefile: 1,981; sh: 628
file content (112 lines) | stat: -rw-r--r-- 3,376 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#
# Copyright (c) 2008-2009 Strasbourg University
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation;
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Author: David Gross <gdavid.devel@gmail.com>
#         Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
#

#
# Network topology:
# 
#             n0   r    n1
#             |    _    |
#             ====|_|====
#                router
#

import ns.internet_apps
import ns.core
import ns.csma
import ns.internet
import ns.network

def main(argv):

    cmd = ns.core.CommandLine();

    cmd.Parse(argv);

    # Create nodes
    print "Create nodes"
    n0 = ns.network.Node();
    r = ns.network.Node();
    n1 = ns.network.Node();

    net1 = ns.network.NodeContainer();
    net1.Add(n0);
    net1.Add(r);
    net2 = ns.network.NodeContainer();
    net2.Add(r);
    net2.Add(n1);
    all = ns.network.NodeContainer();
    all.Add(n0);
    all.Add(r);
    all.Add(n1);

    # Create IPv6 Internet Stack
    internetv6 = ns.internet.InternetStackHelper();
    internetv6.Install(all);

    # Create channels
    csma = ns.csma.CsmaHelper();
    csma.SetChannelAttribute("DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)));
    csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.MilliSeconds(2)));
    d1 = csma.Install(net1);
    d2 = csma.Install(net2);

    # Create networks and assign IPv6 Addresses
    print "Addressing"
    ipv6 = ns.internet.Ipv6AddressHelper();
    ipv6.SetBase(ns.network.Ipv6Address("2001:1::"), ns.network.Ipv6Prefix(64));
    i1 = ipv6.Assign(d1);
    i1.SetForwarding(1, True);
    i1.SetDefaultRouteInAllNodes(1);
    ipv6.SetBase(ns.network.Ipv6Address("2001:2::"), ns.network.Ipv6Prefix(64));
    i2 = ipv6.Assign(d2);
    i2.SetForwarding(0, True);
    i2.SetDefaultRouteInAllNodes(0);

    # Create a Ping6 application to send ICMPv6 echo request from n0 to n1 via r 
    print "Application"
    packetSize = 1024;
    maxPacketCount = 5;
    interPacketInterval = ns.core.Seconds(1.);
    ping6 = ns.internet_apps.Ping6Helper();

    ping6.SetLocal(i1.GetAddress(0, 1));
    ping6.SetRemote(i2.GetAddress(1, 1)); 

    ping6.SetAttribute("MaxPackets", ns.core.UintegerValue(maxPacketCount));
    ping6.SetAttribute("Interval", ns.core.TimeValue(interPacketInterval));
    ping6.SetAttribute("PacketSize", ns.core.UintegerValue(packetSize));

    apps = ping6.Install(ns.network.NodeContainer(net1.Get(0)));
    apps.Start(ns.core.Seconds(2.0));
    apps.Stop(ns.core.Seconds(20.0));

    print "Tracing"
    ascii = ns.network.AsciiTraceHelper()
    csma.EnableAsciiAll(ascii.CreateFileStream("simple-routing-ping6.tr"))
    csma.EnablePcapAll("simple-routing-ping6", True)

    # Run Simulation     
    ns.core.Simulator.Run()
    ns.core.Simulator.Destroy()

if __name__ == '__main__':
    import sys
    main(sys.argv)