A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
csma-one-subnet.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 n2 n3
8// | | | |
9// =================
10// LAN
11//
12// - CBR/UDP flows from n0 to n1 and from n3 to n0
13// - DropTail queues
14// - Tracing of queues and packet receptions to file "csma-one-subnet.tr"
15
16#include "ns3/applications-module.h"
17#include "ns3/core-module.h"
18#include "ns3/csma-module.h"
19#include "ns3/internet-module.h"
20#include "ns3/network-module.h"
21
22#include <fstream>
23#include <iostream>
24
25using namespace ns3;
26
27NS_LOG_COMPONENT_DEFINE("CsmaOneSubnetExample");
28
29int
30main(int argc, char* argv[])
31{
32//
33// Users may find it convenient to turn on explicit debugging
34// for selected modules; the below lines suggest how to do this
35//
36#if 0
37 LogComponentEnable ("CsmaOneSubnetExample", LOG_LEVEL_INFO);
38#endif
39 //
40 // Allow the user to override any of the defaults and the above Bind() at
41 // run-time, via command-line arguments
42 //
43 CommandLine cmd(__FILE__);
44 cmd.Parse(argc, argv);
45 //
46 // Explicitly create the nodes required by the topology (shown above).
47 //
48 NS_LOG_INFO("Create nodes.");
50 nodes.Create(4);
51
52 NS_LOG_INFO("Build Topology");
54 csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
55 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
56 //
57 // Now fill out the topology by creating the net devices required to connect
58 // the nodes to the channels and hooking them up.
59 //
61
63 internet.Install(nodes);
64
65 // We've got the "hardware" in place. Now we need to add IP addresses.
66 //
67 NS_LOG_INFO("Assign IP Addresses.");
69 ipv4.SetBase("10.1.1.0", "255.255.255.0");
70 Ipv4InterfaceContainer interfaces = ipv4.Assign(devices);
71
72 //
73 // Create an OnOff application to send UDP datagrams from node zero to node 1.
74 //
75 NS_LOG_INFO("Create Applications.");
76 uint16_t port = 9; // Discard port (RFC 863)
77
78 OnOffHelper onoff("ns3::UdpSocketFactory",
79 Address(InetSocketAddress(interfaces.GetAddress(1), port)));
80 onoff.SetConstantRate(DataRate("500kb/s"));
81
83 // Start the application
84 app.Start(Seconds(1.0));
85 app.Stop(Seconds(10.0));
86
87 // Create an optional packet sink to receive these packets
88 PacketSinkHelper sink("ns3::UdpSocketFactory",
90 app = sink.Install(nodes.Get(1));
91 app.Start(Seconds(0.0));
92
93 //
94 // Create a similar flow from n3 to n0, starting at time 1.1 seconds
95 //
96 onoff.SetAttribute("Remote", AddressValue(InetSocketAddress(interfaces.GetAddress(0), port)));
97 app = onoff.Install(nodes.Get(3));
98 app.Start(Seconds(1.1));
99 app.Stop(Seconds(10.0));
100
101 app = sink.Install(nodes.Get(0));
102 app.Start(Seconds(0.0));
103
104 NS_LOG_INFO("Configure Tracing.");
105 //
106 // Configure ascii tracing of all enqueue, dequeue, and NetDevice receive
107 // events on all devices. Trace output will be sent to the file
108 // "csma-one-subnet.tr"
109 //
110 AsciiTraceHelper ascii;
111 csma.EnableAsciiAll(ascii.CreateFileStream("csma-one-subnet.tr"));
112
113 //
114 // Also configure some tcpdump traces; each interface will be traced.
115 // The output files will be named:
116 //
117 // csma-one-subnet-<node ID>-<device's interface index>.pcap
118 //
119 // and can be read by the "tcpdump -r" command (use "-tt" option to
120 // display timestamps correctly)
121 //
122 csma.EnablePcapAll("csma-one-subnet", false);
123 //
124 // Now, do the actual simulation.
125 //
126 NS_LOG_INFO("Run Simulation.");
129 NS_LOG_INFO("Done.");
130
131 return 0;
132}
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.
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.
static Ipv4Address GetAny()
holds a vector of std::pair of Ptr<Ipv4> and interface index.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
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.
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
NodeContainer nodes
devices
Definition first.py:31
interfaces
Definition first.py:39
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