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