A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
emu-udp-echo.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  */
16 
17 // Network topology
18 //
19 // Normally, the use case for emulated net devices is in collections of
20 // small simulations that connect to the outside world through specific
21 // interfaces. For example, one could construct a number of virtual
22 // machines and connect them via a host-only network. To use the emulated
23 // net device, you would need to set all of the host-only interfaces in
24 // promiscuous mode and provide an appropriate device name (search for "eth1"
25 // below). One could also use the emulated net device in a testbed situation
26 // where the host on which the simulation is running has a specific interface
27 // of interested. You would also need to set this specific interface into
28 // promiscuous mode and provide an appropriate device name.
29 //
30 // This philosophy carries over to this simple example.
31 //
32 // We don't assume any special configuration and all of the ns-3 emulated net
33 // devices will actually talk to the same underlying OS device. We rely on
34 // the fact that the OS will deliver copies of our packets to the other ns-3
35 // net devices since we operate in promiscuous mode.
36 //
37 // Packets will be sent out over the device, but we use MAC spoofing. The
38 // MAC addresses will be generated using the Organizationally Unique Identifier
39 // (OUI) 00:00:00 as a base. This vendor code is not assigned to any
40 // organization and so should not conflict with any real hardware. We'll use
41 // the first n of these addresses, where n is the number of nodes, in this
42 // simualtion. It is up to you to determine that using these MAC addresses is
43 // okay on your network and won't conflict with anything else (including another
44 // simulation using emu devices) on your network. Once you have made this
45 // determination, you need to put the interface you chose into promiscuous mode.
46 // We don't do it for you since you need to think about it first.
47 //
48 // This simulation uses the real-time simulator and so will consume ten seconds
49 // of real time.
50 //
51 // By default, we create the following topology
52 //
53 // n0 n1 n2 n3
54 // | | | |
55 // -----------------
56 // "eth1"
57 //
58 // - UDP flows from n0 to n1 and back
59 // - DropTail queues
60 // - Tracing of queues and packet receptions to file "udp-echo.tr"
61 // - pcap tracing on all devices
62 //
63 
64 #include <fstream>
65 #include "ns3/core-module.h"
66 #include "ns3/internet-module.h"
67 #include "ns3/applications-module.h"
68 #include "ns3/emu-helper.h"
69 
70 using namespace ns3;
71 
72 NS_LOG_COMPONENT_DEFINE ("EmulatedUdpEchoExample");
73 
74 int
75 main (int argc, char *argv[])
76 {
77  std::string deviceName ("eth1");
78  std::string encapMode ("Dix");
79  uint32_t nNodes = 4;
80 
81  //
82  // Allow the user to override any of the defaults at run-time, via command-line
83  // arguments
84  //
85  CommandLine cmd;
86  cmd.AddValue ("deviceName", "device name", deviceName);
87  cmd.AddValue ("encapsulationMode", "encapsulation mode of emu device (\"Dix\" [default] or \"Llc\")", encapMode);
88  cmd.AddValue ("nNodes", "number of nodes to create (>= 2)", nNodes);
89 
90  cmd.Parse (argc, argv);
91 
92  GlobalValue::Bind ("SimulatorImplementationType",
93  StringValue ("ns3::RealtimeSimulatorImpl"));
94 
95  //
96  // need at least two nodes
97  //
98  nNodes = nNodes < 2 ? 2 : nNodes;
99 
100  //
101  // Explicitly create the nodes required by the topology (shown above).
102  //
103  NS_LOG_INFO ("Create nodes.");
104  NodeContainer n;
105  n.Create (nNodes);
106 
107  InternetStackHelper internet;
108  internet.Install (n);
109 
110  //
111  // Explicitly create the channels required by the topology (shown above).
112  //
113  NS_LOG_INFO ("Create channels.");
114  EmuHelper emu;
115  emu.SetAttribute ("DeviceName", StringValue (deviceName));
116  emu.SetAttribute ("EncapsulationMode", StringValue (encapMode));
117 
118  NetDeviceContainer d = emu.Install (n);
119 
120  //
121  // We've got the "hardware" in place. Now we need to add IP addresses.
122  //
123  Ipv4AddressHelper ipv4;
124  NS_LOG_INFO ("Assign IP Addresses.");
125  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
126  Ipv4InterfaceContainer i = ipv4.Assign (d);
127 
128  //
129  // Create a UdpEchoServer application on node one.
130  //
131  NS_LOG_INFO ("Create Applications.");
132  UdpEchoServerHelper server (9);
133  ApplicationContainer apps = server.Install (n.Get (1));
134  apps.Start (Seconds (1.0));
135  apps.Stop (Seconds (10.0));
136 
137  //
138  // Create a UdpEchoClient application to send UDP datagrams from node zero to node one.
139  //
140  uint32_t packetSize = 1024;
141  uint32_t maxPacketCount = 20;
142  Time interPacketInterval = Seconds (0.1);
143  UdpEchoClientHelper client (i.GetAddress (1), 9);
144  client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
145  client.SetAttribute ("Interval", TimeValue (interPacketInterval));
146  client.SetAttribute ("PacketSize", UintegerValue (packetSize));
147  apps = client.Install (n.Get (0));
148  apps.Start (Seconds (2.0));
149  apps.Stop (Seconds (10.0));
150 
151  std::ofstream ascii;
152  ascii.open ("emu-udp-echo.tr");
153  emu.EnablePcapAll ("emu-udp-echo", true);
154 
155  //
156  // Now, do the actual simulation.
157  //
158  NS_LOG_INFO ("Run Simulation.");
159  Simulator::Run ();
161  NS_LOG_INFO ("Done.");
162 }
holds a vector of ns3::Application pointers.
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
holds a vector of std::pair of Ptr and interface index.
hold variables of type string
Definition: string.h:19
build a set of EmuNetDevice objects
Definition: emu-helper.h:46
Create an application which sends a UDP packet and waits for an echo of this packet.
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
aggregate IP/TCP/UDP functionality to existing Nodes.
void SetAttribute(std::string n1, const AttributeValue &v1)
Definition: emu-helper.cc:61
#define NS_LOG_INFO(msg)
Definition: log.h:298
ApplicationContainer Install(Ptr< Node > node) const
Create a UdpEchoServerApplication on the specified Node.
Create a server application which waits for input UDP packets and sends them back to the original sen...
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 ...
hold objects of type ns3::Time
Definition: nstime.h:961
Hold an unsigned integer type.
Definition: uinteger.h:46
holds a vector of ns3::NetDevice pointers
static void Bind(std::string name, const AttributeValue &value)
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:152
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
keep track of a set of node pointers.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:408
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::EmuNetDevice with the attributes configured by EmuHelper::SetDeviceAttrib...
Definition: emu-helper.cc:207
void Parse(int argc, char *argv[])
Parse the program arguments.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void SetAttribute(std::string name, const AttributeValue &value)
Record an attribute to be set in each Application after it is is created.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
int main(int argc, char *argv[])
Definition: emu-udp-echo.cc:75