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