A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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", inet_sock_address.ConvertTo())
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 inet_address = ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), port)
109 sink = ns.applications.PacketSinkHelper("ns3::UdpSocketFactory", inet_address.ConvertTo())
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 inet_address = ns.network.InetSocketAddress(ns.network.Ipv4Address("10.1.1.1"), port)
117 onoff.SetAttribute("Remote",
118 ns.network.AddressValue(inet_address.ConvertTo()))
119 app = onoff.Install(ns.network.NodeContainer(terminals.Get(3)))
120 app.Start(ns.core.Seconds(1.1))
121 app.Stop(ns.core.Seconds(10.0))
122
123 app = sink.Install(ns.network.NodeContainer(terminals.Get(0)))
124 app.Start(ns.core.Seconds(0.0))
125
126 #
127 # Configure tracing of all enqueue, dequeue, and NetDevice receive events.
128 # Trace output will be sent to the file "csma-bridge.tr"
129 #
130 #print "Configure Tracing."
131 #ascii = ns.network.AsciiTraceHelper();
132 #csma.EnableAsciiAll(ascii.CreateFileStream ("csma-bridge.tr"));
133
134 #
135 # Also configure some tcpdump traces; each interface will be traced.
136 # The output files will be named:
137 # csma-bridge.pcap-<nodeId>-<interfaceId>
138 # and can be read by the "tcpdump -r" command(use "-tt" option to
139 # display timestamps correctly)
140 #
141 csma.EnablePcapAll("csma-bridge", False)
142
143 #
144 # Now, do the actual simulation.
145 #
146 #print "Run Simulation."
147 ns.core.Simulator.Run()
148 ns.core.Simulator.Destroy()
149 #print "Done."
150
151
152
153if __name__ == '__main__':
154 import sys
155 main(sys.argv)
156