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