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
36from ns import ns
37
38
39def 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 inet_sock_address = ns.network.InetSocketAddress(ns.network.Ipv4Address("10.1.1.2"), port)
99 onoff = ns.applications.OnOffHelper("ns3::UdpSocketFactory",
100 ns.network.Address(ns.addressFromInetSocketAddress(inet_sock_address)))
101 onoff.SetConstantRate (ns.network.DataRate ("500kb/s"))
102
103 app = onoff.Install(ns.network.NodeContainer(terminals.Get(0)))
104 # Start the application
105 app.Start(ns.core.Seconds(1.0))
106 app.Stop(ns.core.Seconds(10.0))
107
108 # Create an optional packet sink to receive these packets
109 inet_address = ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), port)
110 sink = ns.applications.PacketSinkHelper("ns3::UdpSocketFactory",
111 ns.network.Address(ns.addressFromInetSocketAddress(inet_address)))
112 app = sink.Install(ns.network.NodeContainer(terminals.Get(1)))
113 app.Start(ns.core.Seconds(0.0))
114
115 #
116 # Create a similar flow from n3 to n0, starting at time 1.1 seconds
117 #
118 inet_address = ns.network.InetSocketAddress(ns.network.Ipv4Address("10.1.1.1"), port)
119 onoff.SetAttribute("Remote",
120 ns.network.AddressValue(ns.addressFromInetSocketAddress(inet_address)))
121 app = onoff.Install(ns.network.NodeContainer(terminals.Get(3)))
122 app.Start(ns.core.Seconds(1.1))
123 app.Stop(ns.core.Seconds(10.0))
124
125 app = sink.Install(ns.network.NodeContainer(terminals.Get(0)))
126 app.Start(ns.core.Seconds(0.0))
127
128 #
129 # Configure tracing of all enqueue, dequeue, and NetDevice receive events.
130 # Trace output will be sent to the file "csma-bridge.tr"
131 #
132 #print "Configure Tracing."
133 #ascii = ns.network.AsciiTraceHelper();
134 #csma.EnableAsciiAll(ascii.CreateFileStream ("csma-bridge.tr"));
135
136 #
137 # Also configure some tcpdump traces; each interface will be traced.
138 # The output files will be named:
139 # csma-bridge.pcap-<nodeId>-<interfaceId>
140 # and can be read by the "tcpdump -r" command(use "-tt" option to
141 # display timestamps correctly)
142 #
143 csma.EnablePcapAll("csma-bridge", False)
144
145 #
146 # Now, do the actual simulation.
147 #
148 #print "Run Simulation."
149 ns.core.Simulator.Run()
150 ns.core.Simulator.Destroy()
151 #print "Done."
152
153
154
155if __name__ == '__main__':
156 import sys
157 main(sys.argv)
158