| A Discrete-Event Network Simulator | API |
- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
-
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 } ns3::ApplicationContainerholds a vector of ns3::Application pointers. Definition: application-container.h:41 ns3::Timekeep track of time values and allow control of global simulation resolution Definition: nstime.h:81 ns3::Ipv4InterfaceContainerholds a vector of std::pair of Ptr and interface index. Definition: ipv4-interface-container.h:32 ns3::StringValuehold variables of type string Definition: string.h:19 ns3::EmuHelperbuild a set of EmuNetDevice objects Definition: emu-helper.h:46 ns3::UdpEchoClientHelpercreate an application which sends a udp packet and waits for an echo of this packet ...Definition: udp-echo-helper.h:100 ns3::Simulator::Runstatic void Run(void)Definition: simulator.cc:157 ns3::InternetStackHelperaggregate IP/TCP/UDP functionality to existing Nodes. Definition: internet-stack-helper.h:64 ns3::EmuHelper::SetAttributevoid SetAttribute(std::string n1, const AttributeValue &v1)Definition: emu-helper.cc:61 NS_LOG_INFO#define NS_LOG_INFO(msg)Definition: log.h:264 ns3::UdpEchoServerHelper::InstallApplicationContainer Install(Ptr< Node > node) const Definition: udp-echo-helper.cc:43 ns3::UdpEchoServerHelperCreate a server application which waits for input udp packets and sends them back to the original sen...Definition: udp-echo-helper.h:36 ns3::PcapHelperForDevice::EnablePcapAllvoid 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 ...Definition: trace-helper.cc:432 ns3::TimeValuehold objects of type ns3::Time Definition: nstime.h:828 ns3::UintegerValueHold an unsigned integer type. Definition: uinteger.h:46 ns3::NetDeviceContainerholds a vector of ns3::NetDevice pointers Definition: net-device-container.h:41 ns3::GlobalValue::Bindstatic void Bind(std::string name, const AttributeValue &value)Definition: global-value.cc:149 ns3::ApplicationContainer::Startvoid Start(Time start)Arrange for all of the Applications in this container to Start() at the Time given as a parameter...Definition: application-container.cc:84 ns3::CommandLineParse command-line arguments. Definition: command-line.h:152 ns3::Simulator::Destroystatic void Destroy(void)Definition: simulator.cc:121 ns3::NodeContainerkeep track of a set of node pointers. Definition: node-container.h:38 ns3::InternetStackHelper::Installvoid Install(std::string nodeName) const Definition: internet-stack-helper.cc:487 ns3::ApplicationContainer::Stopvoid Stop(Time stop)Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...Definition: application-container.cc:93 ns3::Ipv4AddressHelper::AssignIpv4InterfaceContainer Assign(const NetDeviceContainer &c)Assign IP addresses to the net devices specified in the container based on the current network prefix...Definition: ipv4-address-helper.cc:131 ns3::NS_LOG_COMPONENT_DEFINENS_LOG_COMPONENT_DEFINE("PacketLossCounter") ns3::CommandLine::AddValuevoid AddValue(const std::string &name, const std::string &help, T &value)Definition: command-line.h:408 ns3::NodeContainer::GetPtr< Node > Get(uint32_t i) const Get the Ptr stored in this container at a given index. Definition: node-container.cc:88 ns3::EmuHelper::InstallNetDeviceContainer Install(Ptr< Node > node) const Definition: emu-helper.cc:207 ns3::CommandLine::Parsevoid Parse(int argc, char *argv[])Definition: command-line.cc:104 ns3::Ipv4AddressHelperA helper class to make life easier while doing simple IPv4 address assignment in scripts. Definition: ipv4-address-helper.h:45 ns3::NodeContainer::Createvoid Create(uint32_t n)Create n nodes and append pointers to them to the end of this NodeContainer. Definition: node-container.cc:93 ns3::UdpEchoClientHelper::SetAttributevoid SetAttribute(std::string name, const AttributeValue &value)Definition: udp-echo-helper.cc:98 ns3::Ipv4AddressHelper::SetBasevoid SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")Set the base network number, network mask and base address. Definition: ipv4-address-helper.cc:60 ns3::Ipv4InterfaceContainer::GetAddressIpv4Address GetAddress(uint32_t i, uint32_t j=0) const Definition: ipv4-interface-container.cc:39 mainint main(int argc, char *argv[])Definition: emu-udp-echo.cc:75
- src
- emu
- examples
- emu-udp-echo.cc
- Generated on Sun Apr 20 2014 11:14:49 for ns-3 by
1.8.6