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 
34 
35 
36 import ns.applications
37 import ns.bridge
38 import ns.core
39 import ns.csma
40 import ns.internet
41 import ns.network
42 
43 
44 def main(argv):
45 
46  #
47  # Allow the user to override any of the defaults and the above Bind() at
48  # run-time, via command-line arguments
49  #
50  cmd = ns.core.CommandLine()
51  cmd.Parse(argv)
52 
53  #
54  # Explicitly create the nodes required by the topology(shown above).
55  #
56  #print "Create nodes."
57  terminals = ns.network.NodeContainer()
58  terminals.Create(4)
59 
60  csmaSwitch = ns.network.NodeContainer()
61  csmaSwitch.Create(1)
62 
63  #print "Build Topology"
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 
68  # Create the csma links, from each terminal to the switch
69 
70  terminalDevices = ns.network.NetDeviceContainer()
71  switchDevices = ns.network.NetDeviceContainer()
72 
73  for i in range(4):
74  link = csma.Install(ns.network.NodeContainer(ns.network.NodeContainer(terminals.Get(i)), csmaSwitch))
75  terminalDevices.Add(link.Get(0))
76  switchDevices.Add(link.Get(1))
77 
78  # Create the bridge netdevice, which will do the packet switching
79  switchNode = csmaSwitch.Get(0)
80  bridgeDevice = ns.bridge.BridgeNetDevice()
81  switchNode.AddDevice(bridgeDevice)
82 
83  for portIter in range(switchDevices.GetN()):
84  bridgeDevice.AddBridgePort(switchDevices.Get(portIter))
85 
86  # Add internet stack to the terminals
87  internet = ns.internet.InternetStackHelper()
88  internet.Install(terminals)
89 
90  # We've got the "hardware" in place. Now we need to add IP addresses.
91  #
92  #print "Assign IP Addresses."
93  ipv4 = ns.internet.Ipv4AddressHelper()
94  ipv4.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0"))
95  ipv4.Assign(terminalDevices)
96 
97  #
98  # Create an OnOff application to send UDP datagrams from node zero to node 1.
99  #
100  #print "Create Applications."
101  port = 9 # Discard port(RFC 863)
102 
103  onoff = ns.applications.OnOffHelper("ns3::UdpSocketFactory",
104  ns.network.Address(ns.network.InetSocketAddress(ns.network.Ipv4Address("10.1.1.2"), port)))
105  onoff.SetConstantRate (ns.network.DataRate ("500kb/s"))
106 
107  app = onoff.Install(ns.network.NodeContainer(terminals.Get(0)))
108  # Start the application
109  app.Start(ns.core.Seconds(1.0))
110  app.Stop(ns.core.Seconds(10.0))
111 
112  # Create an optional packet sink to receive these packets
113  sink = ns.applications.PacketSinkHelper("ns3::UdpSocketFactory",
114  ns.network.Address(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), port)))
115  app = sink.Install(ns.network.NodeContainer(terminals.Get(1)))
116  app.Start(ns.core.Seconds(0.0))
117 
118  #
119  # Create a similar flow from n3 to n0, starting at time 1.1 seconds
120  #
121  onoff.SetAttribute("Remote",
122  ns.network.AddressValue(ns.network.InetSocketAddress(ns.network.Ipv4Address("10.1.1.1"), port)))
123  app = onoff.Install(ns.network.NodeContainer(terminals.Get(3)))
124  app.Start(ns.core.Seconds(1.1))
125  app.Stop(ns.core.Seconds(10.0))
126 
127  app = sink.Install(ns.network.NodeContainer(terminals.Get(0)))
128  app.Start(ns.core.Seconds(0.0))
129 
130  #
131  # Configure tracing of all enqueue, dequeue, and NetDevice receive events.
132  # Trace output will be sent to the file "csma-bridge.tr"
133  #
134  #print "Configure Tracing."
135  #ascii = ns.network.AsciiTraceHelper();
136  #csma.EnableAsciiAll(ascii.CreateFileStream ("csma-bridge.tr"));
137 
138  #
139  # Also configure some tcpdump traces; each interface will be traced.
140  # The output files will be named:
141  # csma-bridge.pcap-<nodeId>-<interfaceId>
142  # and can be read by the "tcpdump -r" command(use "-tt" option to
143  # display timestamps correctly)
144  #
145  csma.EnablePcapAll("csma-bridge", False)
146 
147  #
148  # Now, do the actual simulation.
149  #
150  #print "Run Simulation."
151  ns.core.Simulator.Run()
152  ns.core.Simulator.Destroy()
153  #print "Done."
154 
155 
156 
157 if __name__ == '__main__':
158  import sys
159  main(sys.argv)
160