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
fd-emu-udp-echo.cc
Go to the documentation of this file.
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."
);
117
EmuFdNetDeviceHelper
emu;
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
();
164
Simulator::Destroy
();
165
NS_LOG_INFO
(
"Done."
);
166
}
src
fd-net-device
examples
fd-emu-udp-echo.cc
Generated on Tue May 14 2013 11:08:20 for ns-3 by
1.8.1.2