A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
csma-bridge.cc
Go to the documentation of this file.
1/*
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#include "ns3/applications-module.h"
21#include "ns3/bridge-module.h"
22#include "ns3/core-module.h"
23#include "ns3/csma-module.h"
24#include "ns3/internet-module.h"
25#include "ns3/network-module.h"
26
27#include <fstream>
28#include <iostream>
29
30/**
31 * \file
32 * \ingroup bridge
33 * Bridge example connecting four nodes,
34 * with the bridge acting as a simple switch.
35 */
36
37using namespace ns3;
38
39NS_LOG_COMPONENT_DEFINE("CsmaBridgeExample");
40
41int
42main(int argc, char* argv[])
43{
44 //
45 // Users may find it convenient to turn on explicit debugging
46 // for selected modules; the below lines suggest how to do this
47 //
48#if 0
49 LogComponentEnable ("CsmaBridgeExample", LOG_LEVEL_INFO);
50#endif
51
52 //
53 // Allow the user to override any of the defaults and the above Bind() at
54 // run-time, via command-line arguments
55 //
56 CommandLine cmd(__FILE__);
57 cmd.Parse(argc, argv);
58
59 //
60 // Explicitly create the nodes required by the topology (shown above).
61 //
62 NS_LOG_INFO("Create nodes.");
64 terminals.Create(4);
65
67 csmaSwitch.Create(1);
68
69 NS_LOG_INFO("Build Topology");
71 csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
72 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
73
74 // Create the csma links, from each terminal to the switch
75
78
79 for (int i = 0; i < 4; i++)
80 {
81 NetDeviceContainer link = csma.Install(NodeContainer(terminals.Get(i), csmaSwitch));
82 terminalDevices.Add(link.Get(0));
83 switchDevices.Add(link.Get(1));
84 }
85
86 // Create the bridge netdevice, which will do the packet switching
88 BridgeHelper bridge;
89 bridge.Install(switchNode, switchDevices);
90
91 // Add internet stack to the terminals
93 internet.Install(terminals);
94
95 // We've got the "hardware" in place. Now we need to add IP addresses.
96 //
97 NS_LOG_INFO("Assign IP Addresses.");
99 ipv4.SetBase("10.1.1.0", "255.255.255.0");
100 ipv4.Assign(terminalDevices);
101
102 //
103 // Create an OnOff application to send UDP datagrams from node zero to node 1.
104 //
105 NS_LOG_INFO("Create Applications.");
106 uint16_t port = 9; // Discard port (RFC 863)
107
108 OnOffHelper onoff("ns3::UdpSocketFactory",
110 onoff.SetConstantRate(DataRate("500kb/s"));
111
112 ApplicationContainer app = onoff.Install(terminals.Get(0));
113 // Start the application
114 app.Start(Seconds(1.0));
115 app.Stop(Seconds(10.0));
116
117 // Create an optional packet sink to receive these packets
118 PacketSinkHelper sink("ns3::UdpSocketFactory",
120 app = sink.Install(terminals.Get(1));
121 app.Start(Seconds(0.0));
122
123 //
124 // Create a similar flow from n3 to n0, starting at time 1.1 seconds
125 //
126 onoff.SetAttribute("Remote", AddressValue(InetSocketAddress(Ipv4Address("10.1.1.1"), port)));
127 app = onoff.Install(terminals.Get(3));
128 app.Start(Seconds(1.1));
129 app.Stop(Seconds(10.0));
130
131 app = sink.Install(terminals.Get(0));
132 app.Start(Seconds(0.0));
133
134 NS_LOG_INFO("Configure Tracing.");
135
136 //
137 // Configure tracing of all enqueue, dequeue, and NetDevice receive events.
138 // Trace output will be sent to the file "csma-bridge.tr"
139 //
140 AsciiTraceHelper ascii;
141 csma.EnableAsciiAll(ascii.CreateFileStream("csma-bridge.tr"));
142
143 //
144 // Also configure some tcpdump traces; each interface will be traced.
145 // The output files will be named:
146 // csma-bridge-<nodeId>-<interfaceId>.pcap
147 // and can be read by the "tcpdump -r" command (use "-tt" option to
148 // display timestamps correctly)
149 //
150 csma.EnablePcapAll("csma-bridge", false);
151
152 //
153 // Now, do the actual simulation.
154 //
155 NS_LOG_INFO("Run Simulation.");
158 NS_LOG_INFO("Done.");
159
160 return 0;
161}
a polymophic address class
Definition address.h:90
holds a vector of ns3::Application pointers.
Manage ASCII trace files for device models.
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Add capability to bridge multiple LAN segments (IEEE 802.1D bridging)
NetDeviceContainer Install(Ptr< Node > node, NetDeviceContainer c)
This method creates an ns3::BridgeNetDevice with the attributes configured by BridgeHelper::SetDevice...
Parse command-line arguments.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
Class for representing data rates.
Definition data-rate.h:78
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
static Ipv4Address GetAny()
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
uint16_t port
Definition dsdv-manet.cc:33
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:291
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:93
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44