A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
mixed-global-routing.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 
18 // This script exercises global routing code in a mixed point-to-point
19 // and csma/cd environment
20 //
21 // Network topology
22 //
23 // n0
24 // \ p-p
25 // \ (shared csma/cd)
26 // n2 -------------------------n3
27 // / | |
28 // / p-p n4 n5 ---------- n6
29 // n1 p-p
30 //
31 // - CBR/UDP flows from n0 to n6
32 // - Tracing of queues and packet receptions to file "mixed-global-routing.tr"
33 
34 #include <iostream>
35 #include <fstream>
36 #include <string>
37 #include <cassert>
38 
39 #include "ns3/core-module.h"
40 #include "ns3/network-module.h"
41 #include "ns3/point-to-point-module.h"
42 #include "ns3/csma-module.h"
43 #include "ns3/applications-module.h"
44 #include "ns3/internet-module.h"
45 
46 using namespace ns3;
47 
48 NS_LOG_COMPONENT_DEFINE ("MixedGlobalRoutingExample");
49 
50 int
51 main (int argc, char *argv[])
52 {
53  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (210));
54  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("448kb/s"));
55 
56  // Allow the user to override any of the defaults and the above
57  // Bind ()s at run-time, via command-line arguments
58  CommandLine cmd;
59  cmd.Parse (argc, argv);
60 
61  NS_LOG_INFO ("Create nodes.");
62  NodeContainer c;
63  c.Create (7);
64  NodeContainer n0n2 = NodeContainer (c.Get (0), c.Get (2));
65  NodeContainer n1n2 = NodeContainer (c.Get (1), c.Get (2));
66  NodeContainer n5n6 = NodeContainer (c.Get (5), c.Get (6));
67  NodeContainer n2345 = NodeContainer (c.Get (2), c.Get (3), c.Get (4), c.Get (5));
68 
69  InternetStackHelper internet;
70  internet.Install (c);
71 
72  // We create the channels first without any IP addressing information
73  NS_LOG_INFO ("Create channels.");
75  p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
76  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
77  NetDeviceContainer d0d2 = p2p.Install (n0n2);
78 
79  NetDeviceContainer d1d2 = p2p.Install (n1n2);
80 
81  p2p.SetDeviceAttribute ("DataRate", StringValue ("1500kbps"));
82  p2p.SetChannelAttribute ("Delay", StringValue ("10ms"));
83  NetDeviceContainer d5d6 = p2p.Install (n5n6);
84 
85  // We create the channels first without any IP addressing information
86  CsmaHelper csma;
87  csma.SetChannelAttribute ("DataRate", StringValue ("5Mbps"));
88  csma.SetChannelAttribute ("Delay", StringValue ("2ms"));
89  NetDeviceContainer d2345 = csma.Install (n2345);
90 
91  // Later, we add IP addresses.
92  NS_LOG_INFO ("Assign IP Addresses.");
93  Ipv4AddressHelper ipv4;
94  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
95  ipv4.Assign (d0d2);
96 
97  ipv4.SetBase ("10.1.2.0", "255.255.255.0");
98  ipv4.Assign (d1d2);
99 
100  ipv4.SetBase ("10.1.3.0", "255.255.255.0");
101  Ipv4InterfaceContainer i5i6 = ipv4.Assign (d5d6);
102 
103  ipv4.SetBase ("10.250.1.0", "255.255.255.0");
104  ipv4.Assign (d2345);
105 
106  // Create router nodes, initialize routing database and set up the routing
107  // tables in the nodes.
109 
110  // Create the OnOff application to send UDP datagrams of size
111  // 210 bytes at a rate of 448 Kb/s
112  NS_LOG_INFO ("Create Applications.");
113  uint16_t port = 9; // Discard port (RFC 863)
114  OnOffHelper onoff ("ns3::UdpSocketFactory",
115  InetSocketAddress (i5i6.GetAddress (1), port));
116  onoff.SetConstantRate (DataRate ("300bps"));
117  onoff.SetAttribute ("PacketSize", UintegerValue (50));
118 
119  ApplicationContainer apps = onoff.Install (c.Get (0));
120  apps.Start (Seconds (1.0));
121  apps.Stop (Seconds (10.0));
122 
123  AsciiTraceHelper ascii;
124  Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream ("mixed-global-routing.tr");
125  p2p.EnableAsciiAll (stream);
126  csma.EnableAsciiAll (stream);
127 
128  p2p.EnablePcapAll ("mixed-global-routing");
129  csma.EnablePcapAll ("mixed-global-routing", false);
130 
131  NS_LOG_INFO ("Run Simulation.");
132  Simulator::Run ();
134  NS_LOG_INFO ("Done.");
135 }
holds a vector of ns3::Application pointers.
Manage ASCII trace files for device models.
Definition: trace-helper.h:141
an Inet address class
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:69
NodeContainer n1n2
Definition: red-tests.cc:63
holds a vector of std::pair of Ptr and interface index.
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
hold variables of type string
Definition: string.h:18
NetDeviceContainer Install(NodeContainer c)
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
aggregate IP/TCP/UDP functionality to existing Nodes.
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:215
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:223
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits. ...
Build a set of PointToPointNetDevice objects.
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
uint16_t port
Definition: dsdv-manet.cc:44
Class for representing data rates.
Definition: data-rate.h:71
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 an unsigned integer type.
Definition: uinteger.h:46
holds a vector of ns3::NetDevice pointers
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:177
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
void SetConstantRate(DataRate dataRate, uint32_t packetSize=512)
Helper function to set a constant rate source.
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...
NodeContainer n0n2
Definition: red-tests.cc:62
int main(int argc, char *argv[])
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
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...
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
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 EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
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