A Discrete-Event Network Simulator
API
nsclick-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  * Authors: Lalith Suresh <suresh.lalith@gmail.com>
17  */
18 
19 // Network topology
20 //
21 //
22 // 172.16.1.0/24
23 // (1.1) (1.2) (2.1) (2.2)
24 //
25 // eth0 eth0 eth1 eth0
26 // n0 ========= n1 ========= n2
27 // LAN 1 LAN 2
28 //
29 // - UDP flows from n0 to n2 via n1.
30 // - All nodes are Click based.
31 //
32 
33 #include "ns3/core-module.h"
34 #include "ns3/network-module.h"
35 #include "ns3/internet-module.h"
36 #include "ns3/applications-module.h"
37 #include "ns3/csma-module.h"
38 #include "ns3/ipv4-click-routing.h"
39 #include "ns3/ipv4-l3-click-protocol.h"
40 #include "ns3/click-internet-stack-helper.h"
41 
42 using namespace ns3;
43 
44 NS_LOG_COMPONENT_DEFINE ("NsclickRouting");
45 
46 int
47 main (int argc, char *argv[])
48 {
49 #ifdef NS3_CLICK
50  std::string clickConfigFolder = "src/click/examples";
51 
53  cmd.AddValue ("clickConfigFolder", "Base folder for click configuration files", clickConfigFolder);
54  cmd.Parse (argc, argv);
55 
56 //
57 // Explicitly create the nodes required by the topology (shown above).
58 //
59  NS_LOG_INFO ("Create nodes.");
60  NodeContainer n;
61  n.Create (3);
62 
63 //
64 // Install Click on the nodes
65 //
66  ClickInternetStackHelper clickinternet;
67  clickinternet.SetClickFile (n.Get (0), clickConfigFolder + "/nsclick-routing-node0.click");
68  clickinternet.SetClickFile (n.Get (1), clickConfigFolder + "/nsclick-ip-router.click");
69  clickinternet.SetClickFile (n.Get (2), clickConfigFolder + "/nsclick-routing-node2.click");
70  clickinternet.SetRoutingTableElement (n.Get (0), "kernel/rt");
71  clickinternet.SetRoutingTableElement (n.Get (1), "u/rt");
72  clickinternet.SetRoutingTableElement (n.Get (2), "kernel/rt");
73  clickinternet.Install (n);
74 
75  NS_LOG_INFO ("Create channels.");
76 //
77 // Explicitly create the channels required by the topology (shown above).
78 //
80  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
81  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
82  csma.SetDeviceAttribute ("Mtu", UintegerValue (1400));
83  NetDeviceContainer d01 = csma.Install (NodeContainer (n.Get (0), n.Get (1)));
84  NetDeviceContainer d12 = csma.Install (NodeContainer (n.Get (1), n.Get (2)));
85 
86  Ipv4AddressHelper ipv4;
87 //
88 // We've got the "hardware" in place. Now we need to add IP addresses.
89 //
90  NS_LOG_INFO ("Assign IP Addresses.");
91  ipv4.SetBase ("172.16.1.0", "255.255.255.0");
92  Ipv4InterfaceContainer i01 = ipv4.Assign (d01);
93 
94  ipv4.SetBase ("172.16.2.0", "255.255.255.0");
95  Ipv4InterfaceContainer i12 = ipv4.Assign (d12);
96 
97  NS_LOG_INFO ("Create Applications.");
98 //
99 // Create one udpServer applications on node one.
100 //
101  uint16_t port = 4000;
102  UdpServerHelper server (port);
103  ApplicationContainer apps = server.Install (n.Get (2));
104  apps.Start (Seconds (1.0));
105  apps.Stop (Seconds (10.0));
106 
107 //
108 // Create one UdpClient application to send UDP datagrams from node zero to
109 // node one.
110 //
111  uint32_t MaxPacketSize = 1024;
112  Time interPacketInterval = Seconds (0.05);
113  uint32_t maxPacketCount = 320;
114  UdpClientHelper client (i12.GetAddress (1), port);
115  client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
116  client.SetAttribute ("Interval", TimeValue (interPacketInterval));
117  client.SetAttribute ("PacketSize", UintegerValue (MaxPacketSize));
118  apps = client.Install (NodeContainer (n.Get (0)));
119  apps.Start (Seconds (2.0));
120  apps.Stop (Seconds (10.0));
121 
122  csma.EnablePcap ("nsclick-routing", d01, false);
123  csma.EnablePcap ("nsclick-routing", d12, false);
124 
125 //
126 // Now, do the actual simulation.
127 //
128  NS_LOG_INFO ("Run Simulation.");
129  Simulator::Stop (Seconds (20.0));
130  Simulator::Run ();
132  NS_LOG_INFO ("Done.");
133 #else
134  NS_FATAL_ERROR ("Can't use ns-3-click without NSCLICK compiled in");
135 #endif
136 }
holds a vector of ns3::Application pointers.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:71
holds a vector of std::pair of Ptr and interface index.
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1001
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:217
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:277
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
tuple cmd
Definition: second.py:35
uint16_t port
Definition: dsdv-manet.cc:44
Class for representing data rates.
Definition: data-rate.h:88
Create a client application which sends UDP packets carrying a 32bit sequence number and a 64 bit tim...
AttributeValue implementation for Time.
Definition: nstime.h:1055
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
Create a server application which waits for input UDP packets and uses the information carried into t...
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:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
void SetAttribute(std::string name, const AttributeValue &value)
Record an attribute to be set in each Application after it is is created.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:65
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...
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:498
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:993
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 Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
tuple csma
Definition: second.py:63
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const