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