A Discrete-Event Network Simulator
API
csma-bridge.py
Go to the documentation of this file.
1 # /*
2 # * This program is free software; you can redistribute it and/or modify
3 # * it under the terms of the GNU General Public License version 2 as
4 # * published by the Free Software Foundation
5 # *
6 # * This program is distributed in the hope that it will be useful,
7 # * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # * GNU General Public License for more details.
10 # *
11 # * You should have received a copy of the GNU General Public License
12 # * along with this program; if not, write to the Free Software
13 # * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 # */
15 
16 # Network topology
17 #
18 # n0 n1
19 # | |
20 # ----------
21 # | Switch |
22 # ----------
23 # | |
24 # n2 n3
25 #
26 #
27 # - CBR/UDP flows from n0 to n1 and from n3 to n0
28 # - DropTail queues
29 # - Tracing of queues and packet receptions to file "csma-bridge.tr"
30 
31 import ns.applications
32 import ns.bridge
33 import ns.core
34 import ns.csma
35 import ns.internet
36 import ns.network
37 
38 
39 def main(argv):
40 
41  #
42  # Allow the user to override any of the defaults and the above Bind() at
43  # run-time, via command-line arguments
44  #
45  cmd = ns.core.CommandLine()
46  cmd.Parse(argv)
47 
48  #
49  # Explicitly create the nodes required by the topology(shown above).
50  #
51  #print "Create nodes."
52  terminals = ns.network.NodeContainer()
53  terminals.Create(4)
54 
55  csmaSwitch = ns.network.NodeContainer()
56  csmaSwitch.Create(1)
57 
58  #print "Build Topology"
59  csma = ns.csma.CsmaHelper()
60  csma.SetChannelAttribute("DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
61  csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.MilliSeconds(2)))
62 
63  # Create the csma links, from each terminal to the switch
64 
65  terminalDevices = ns.network.NetDeviceContainer()
66  switchDevices = ns.network.NetDeviceContainer()
67 
68  for i in range(4):
69  link = csma.Install(ns.network.NodeContainer(ns.network.NodeContainer(terminals.Get(i)), csmaSwitch))
70  terminalDevices.Add(link.Get(0))
71  switchDevices.Add(link.Get(1))
72 
73  # Create the bridge netdevice, which will do the packet switching
74  switchNode = csmaSwitch.Get(0)
75  bridgeDevice = ns.bridge.BridgeNetDevice()
76  switchNode.AddDevice(bridgeDevice)
77 
78  for portIter in range(switchDevices.GetN()):
79  bridgeDevice.AddBridgePort(switchDevices.Get(portIter))
80 
81  # Add internet stack to the terminals
82  internet = ns.internet.InternetStackHelper()
83  internet.Install(terminals)
84 
85  # We've got the "hardware" in place. Now we need to add IP addresses.
86  #
87  #print "Assign IP Addresses."
88  ipv4 = ns.internet.Ipv4AddressHelper()
89  ipv4.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0"))
90  ipv4.Assign(terminalDevices)
91 
92  #
93  # Create an OnOff application to send UDP datagrams from node zero to node 1.
94  #
95  #print "Create Applications."
96  port = 9 # Discard port(RFC 863)
97 
98  onoff = ns.applications.OnOffHelper("ns3::UdpSocketFactory",
99  ns.network.Address(ns.network.InetSocketAddress(ns.network.Ipv4Address("10.1.1.2"), port)))
100  onoff.SetConstantRate (ns.network.DataRate ("500kb/s"))
101 
102  app = onoff.Install(ns.network.NodeContainer(terminals.Get(0)))
103  # Start the application
104  app.Start(ns.core.Seconds(1.0))
105  app.Stop(ns.core.Seconds(10.0))
106 
107  # Create an optional packet sink to receive these packets
108  sink = ns.applications.PacketSinkHelper("ns3::UdpSocketFactory",
109  ns.network.Address(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), port)))
110  app = sink.Install(ns.network.NodeContainer(terminals.Get(1)))
111  app.Start(ns.core.Seconds(0.0))
112 
113  #
114  # Create a similar flow from n3 to n0, starting at time 1.1 seconds
115  #
116  onoff.SetAttribute("Remote",
117  ns.network.AddressValue(ns.network.InetSocketAddress(ns.network.Ipv4Address("10.1.1.1"), port)))
118  app = onoff.Install(ns.network.NodeContainer(terminals.Get(3)))
119  app.Start(ns.core.Seconds(1.1))
120  app.Stop(ns.core.Seconds(10.0))
121 
122  app = sink.Install(ns.network.NodeContainer(terminals.Get(0)))
123  app.Start(ns.core.Seconds(0.0))
124 
125  #
126  # Configure tracing of all enqueue, dequeue, and NetDevice receive events.
127  # Trace output will be sent to the file "csma-bridge.tr"
128  #
129  #print "Configure Tracing."
130  #ascii = ns.network.AsciiTraceHelper();
131  #csma.EnableAsciiAll(ascii.CreateFileStream ("csma-bridge.tr"));
132 
133  #
134  # Also configure some tcpdump traces; each interface will be traced.
135  # The output files will be named:
136  # csma-bridge.pcap-<nodeId>-<interfaceId>
137  # and can be read by the "tcpdump -r" command(use "-tt" option to
138  # display timestamps correctly)
139  #
140  csma.EnablePcapAll("csma-bridge", False)
141 
142  #
143  # Now, do the actual simulation.
144  #
145  #print "Run Simulation."
146  ns.core.Simulator.Run()
147  ns.core.Simulator.Destroy()
148  #print "Done."
149 
150 
151 
152 if __name__ == '__main__':
153  import sys
154  main(sys.argv)
155