A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
fd-emu-udp-echo.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 University of Washington, 2012 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
7// Network topology
8//
9// Normally, the use case for emulated net devices is in collections of
10// small simulations that connect to the outside world through specific
11// interfaces. For example, one could construct a number of virtual
12// machines and connect them via a host-only network. To use the emulated
13// net device, you would need to set all of the host-only interfaces in
14// promiscuous mode and provide an appropriate device name (search for "eth1"
15// below). One could also use the emulated net device in a testbed situation
16// where the host on which the simulation is running has a specific interface
17// of interested. You would also need to set this specific interface into
18// promiscuous mode and provide an appropriate device name.
19//
20// This philosophy carries over to this simple example.
21//
22// We don't assume any special configuration and all of the ns-3 emulated net
23// devices will actually talk to the same underlying OS device. We rely on
24// the fact that the OS will deliver copies of our packets to the other ns-3
25// net devices since we operate in promiscuous mode.
26//
27// Packets will be sent out over the device, but we use MAC spoofing. The
28// MAC addresses will be generated using the Organizationally Unique Identifier
29// (OUI) 00:00:00 as a base. This vendor code is not assigned to any
30// organization and so should not conflict with any real hardware. We'll use
31// the first n of these addresses, where n is the number of nodes, in this
32// simulation. It is up to you to determine that using these MAC addresses is
33// okay on your network and won't conflict with anything else (including another
34// simulation using emu devices) on your network. Once you have made this
35// determination, you need to put the interface you chose into promiscuous mode.
36// We don't do it for you since you need to think about it first.
37//
38// This simulation uses the real-time simulator and so will consume ten seconds
39// of real time.
40//
41// By default, we create the following topology
42//
43// n0 n1
44// | |
45// -------
46// "eth1"
47//
48// - UDP flows from n0 to n1 and back
49// - DropTail queues
50// - Tracing of queues and packet receptions to file "udp-echo.tr"
51// - pcap tracing on all devices
52//
53// Another mode of operation corresponds to the wiki HOWTO
54// 'HOWTO use ns-3 scripts to drive real hardware'
55//
56// If the --client mode is specified, only one ns-3 node is created
57// on the specified device name, assuming that a server node is
58// on another virtual machine. The client node will use 10.1.1.2
59//
60// If the --server mode is specified, only one ns-3 node is created
61// on the specified device name, assuming that a client node is
62// on another virtual machine. The server node will use 10.1.1.1
63
64#include "ns3/applications-module.h"
65#include "ns3/core-module.h"
66#include "ns3/fd-net-device-module.h"
67#include "ns3/internet-module.h"
68
69#include <fstream>
70
71using namespace ns3;
72
73NS_LOG_COMPONENT_DEFINE("EmulatedUdpEchoExample");
74
75int
76main(int argc, char* argv[])
77{
78 std::string deviceName("eth1");
79 std::string encapMode("Dix");
80 bool clientMode = false;
81 bool serverMode = false;
82 double stopTime = 10;
83 uint32_t nNodes = 2;
84
85 //
86 // Allow the user to override any of the defaults at run-time, via command-line
87 // arguments
88 //
89 CommandLine cmd(__FILE__);
90 cmd.AddValue("client", "client mode", clientMode);
91 cmd.AddValue("server", "server mode", serverMode);
92 cmd.AddValue("deviceName", "device name", deviceName);
93 cmd.AddValue("stopTime", "stop time (seconds)", stopTime);
94 cmd.AddValue("encapsulationMode",
95 "encapsulation mode of emu device (\"Dix\" [default] or \"Llc\")",
96 encapMode);
97 cmd.AddValue("nNodes", "number of nodes to create (>= 2)", nNodes);
98
99 cmd.Parse(argc, argv);
100
101 GlobalValue::Bind("SimulatorImplementationType", StringValue("ns3::RealtimeSimulatorImpl"));
102
103 GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
104
105 if (clientMode && serverMode)
106 {
107 NS_FATAL_ERROR("Error, both client and server options cannot be enabled.");
108 }
109 //
110 // need at least two nodes
111 //
112 nNodes = nNodes < 2 ? 2 : nNodes;
113
114 //
115 // Explicitly create the nodes required by the topology (shown above).
116 //
117 NS_LOG_INFO("Create nodes.");
119 n.Create(nNodes);
120
122 internet.Install(n);
123
124 //
125 // Explicitly create the channels required by the topology (shown above).
126 //
127 NS_LOG_INFO("Create channels.");
129 emu.SetDeviceName(deviceName);
130 emu.SetAttribute("EncapsulationMode", StringValue(encapMode));
131
136
137 ipv4.SetBase("10.1.1.0", "255.255.255.0");
138 if (clientMode)
139 {
140 d = emu.Install(n.Get(0));
141 // Note: incorrect MAC address assignments are one of the confounding
142 // aspects of network emulation experiments. Here, we assume that there
143 // will be a server mode taking the first MAC address, so we need to
144 // force the MAC address to be one higher (just like IP address below)
145 Ptr<FdNetDevice> dev = d.Get(0)->GetObject<FdNetDevice>();
146 dev->SetAddress(Mac48Address("00:00:00:00:00:02"));
147 NS_LOG_INFO("Assign IP Addresses.");
148 ipv4.NewAddress(); // burn the 10.1.1.1 address so that 10.1.1.2 is next
149 i = ipv4.Assign(d);
150 }
151 else if (serverMode)
152 {
153 d = emu.Install(n.Get(0));
154 NS_LOG_INFO("Assign IP Addresses.");
155 i = ipv4.Assign(d);
156 }
157 else
158 {
159 d = emu.Install(n);
160 NS_LOG_INFO("Assign IP Addresses.");
161 i = ipv4.Assign(d);
162 }
163
164 if (serverMode)
165 {
166 //
167 // Create a UdpEchoServer application
168 //
169 NS_LOG_INFO("Create Applications.");
171 apps = server.Install(n.Get(0));
172 apps.Start(Seconds(1.0));
173 apps.Stop(Seconds(stopTime));
174 }
175 else if (clientMode)
176 {
177 //
178 // Create a UdpEchoClient application to send UDP datagrams
179 //
180 uint32_t packetSize = 1024;
181 uint32_t maxPacketCount = 20;
182 Time interPacketInterval = Seconds(0.1);
183 UdpEchoClientHelper client(Ipv4Address("10.1.1.1"), 9);
184 client.SetAttribute("MaxPackets", UintegerValue(maxPacketCount));
185 client.SetAttribute("Interval", TimeValue(interPacketInterval));
186 client.SetAttribute("PacketSize", UintegerValue(packetSize));
187 apps = client.Install(n.Get(0));
188 apps.Start(Seconds(2.0));
189 apps.Stop(Seconds(stopTime));
190 }
191 else
192 {
193 //
194 // Create a UdpEchoServer application on node one.
195 //
196 NS_LOG_INFO("Create Applications.");
198 apps = server.Install(n.Get(1));
199 apps.Start(Seconds(1.0));
200 apps.Stop(Seconds(stopTime));
201
202 //
203 // Create a UdpEchoClient application to send UDP datagrams from node zero to node one.
204 //
205 uint32_t packetSize = 1024;
206 uint32_t maxPacketCount = 20;
207 Time interPacketInterval = Seconds(0.1);
209 client.SetAttribute("MaxPackets", UintegerValue(maxPacketCount));
210 client.SetAttribute("Interval", TimeValue(interPacketInterval));
211 client.SetAttribute("PacketSize", UintegerValue(packetSize));
212 apps = client.Install(n.Get(0));
213 apps.Start(Seconds(2.0));
214 apps.Stop(Seconds(stopTime));
215 }
216
217 emu.EnablePcapAll("fd-emu-udp-echo", true);
218 emu.EnableAsciiAll("fd-emu-udp-echo.tr");
219
220 //
221 // Now, do the actual simulation.
222 //
223 NS_LOG_INFO("Run Simulation.");
227 NS_LOG_INFO("Done.");
228
229 return 0;
230}
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
Parse command-line arguments.
build a set of FdNetDevice objects attached to a physical network interface
void SetDeviceName(std::string deviceName)
Set the device name of this device.
void SetAttribute(std::string n1, const AttributeValue &v1)
virtual NetDeviceContainer Install(Ptr< Node > node) const
This method creates a FdNetDevice and associates it to a node.
a NetDevice to read/write network traffic from/into a file descriptor.
static void Bind(std::string name, const AttributeValue &value)
Iterate over the set of GlobalValues until a matching name is found and then set its value with Globa...
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.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
an EUI-48 address
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
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.
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
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
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
Create an application which sends a UDP packet and waits for an echo of this packet.
Create a server application which waits for input UDP packets and sends them back to the original sen...
Hold an unsigned integer type.
Definition uinteger.h:34
Time stopTime
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#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
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static const uint32_t packetSize
Packet size generated at the AP.