A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
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
();
160
Simulator::Destroy
();
161
NS_LOG_INFO
(
"Done."
);
162
}
src
emu
examples
emu-udp-echo.cc
Generated on Tue May 14 2013 11:08:20 for ns-3 by
1.8.1.2