A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
51 //
52 // Explicitly create the nodes required by the topology (shown above).
53 //
54  NS_LOG_INFO ("Create nodes.");
55  NodeContainer n;
56  n.Create (3);
57 
58 //
59 // Install Click on the nodes
60 //
61  ClickInternetStackHelper clickinternet;
62  clickinternet.SetClickFile (n.Get (0), "src/click/examples/nsclick-routing-node0.click");
63  clickinternet.SetClickFile (n.Get (1), "src/click/examples/nsclick-ip-router.click");
64  clickinternet.SetClickFile (n.Get (2), "src/click/examples/nsclick-routing-node2.click");
65  clickinternet.SetRoutingTableElement (n.Get (0), "kernel/rt");
66  clickinternet.SetRoutingTableElement (n.Get (1), "u/rt");
67  clickinternet.SetRoutingTableElement (n.Get (2), "kernel/rt");
68  clickinternet.Install (n);
69 
70  NS_LOG_INFO ("Create channels.");
71 //
72 // Explicitly create the channels required by the topology (shown above).
73 //
74  CsmaHelper csma;
75  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
76  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
77  csma.SetDeviceAttribute ("Mtu", UintegerValue (1400));
78  NetDeviceContainer d01 = csma.Install (NodeContainer (n.Get (0), n.Get (1)));
79  NetDeviceContainer d12 = csma.Install (NodeContainer (n.Get (1), n.Get (2)));
80 
81  Ipv4AddressHelper ipv4;
82 //
83 // We've got the "hardware" in place. Now we need to add IP addresses.
84 //
85  NS_LOG_INFO ("Assign IP Addresses.");
86  ipv4.SetBase ("172.16.1.0", "255.255.255.0");
87  Ipv4InterfaceContainer i01 = ipv4.Assign (d01);
88 
89  ipv4.SetBase ("172.16.2.0", "255.255.255.0");
90  Ipv4InterfaceContainer i12 = ipv4.Assign (d12);
91 
92  NS_LOG_INFO ("Create Applications.");
93 //
94 // Create one udpServer applications on node one.
95 //
96  uint16_t port = 4000;
97  UdpServerHelper server (port);
98  ApplicationContainer apps = server.Install (n.Get (2));
99  apps.Start (Seconds (1.0));
100  apps.Stop (Seconds (10.0));
101 
102 //
103 // Create one UdpClient application to send UDP datagrams from node zero to
104 // node one.
105 //
106  uint32_t MaxPacketSize = 1024;
107  Time interPacketInterval = Seconds (0.05);
108  uint32_t maxPacketCount = 320;
109  UdpClientHelper client (i12.GetAddress (1), port);
110  client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
111  client.SetAttribute ("Interval", TimeValue (interPacketInterval));
112  client.SetAttribute ("PacketSize", UintegerValue (MaxPacketSize));
113  apps = client.Install (NodeContainer (n.Get (0)));
114  apps.Start (Seconds (2.0));
115  apps.Stop (Seconds (10.0));
116 
117  csma.EnablePcap ("nsclick-routing", d01, false);
118  csma.EnablePcap ("nsclick-routing", d12, false);
119 
120 //
121 // Now, do the actual simulation.
122 //
123  NS_LOG_INFO ("Run Simulation.");
124  Simulator::Stop (Seconds (20.0));
125  Simulator::Run ();
127  NS_LOG_INFO ("Done.");
128 #else
129  NS_FATAL_ERROR ("Can't use ns-3-click without NSCLICK compiled in");
130 #endif
131 }