A Discrete-Event Network Simulator
API
socket-bound-tcp-static-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 /* Test program for multi-interface host, static routing
19 
20  Destination host (10.20.1.2)
21  |
22  | 10.20.1.0/24
23  DSTRTR
24  10.10.1.0/24 / \ 10.10.2.0/24
25  / \
26  Rtr1 Rtr2
27  10.1.1.0/24 | | 10.1.2.0/24
28  | /
29  \ /
30  Source
31 */
32 
33 #include <iostream>
34 #include <fstream>
35 #include <string>
36 #include <cassert>
37 
38 #include "ns3/core-module.h"
39 #include "ns3/network-module.h"
40 #include "ns3/internet-module.h"
41 #include "ns3/point-to-point-module.h"
42 #include "ns3/applications-module.h"
43 #include "ns3/ipv4-static-routing-helper.h"
44 #include "ns3/ipv4-list-routing-helper.h"
45 
46 using namespace ns3;
47 
48 NS_LOG_COMPONENT_DEFINE ("SocketBoundTcpRoutingExample");
49 
50 static const uint32_t totalTxBytes = 20000;
51 static uint32_t currentTxBytes = 0;
52 static const uint32_t writeSize = 1040;
53 uint8_t data[writeSize];
54 
55 
56 void StartFlow (Ptr<Socket>, Ipv4Address, uint16_t);
57 void WriteUntilBufferFull (Ptr<Socket>, uint32_t);
58 
59 void SendStuff (Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port);
60 void BindSock (Ptr<Socket> sock, Ptr<NetDevice> netdev);
61 void srcSocketRecv (Ptr<Socket> socket);
62 void dstSocketRecv (Ptr<Socket> socket);
63 
64 int
65 main (int argc, char *argv[])
66 {
67 
68  // Allow the user to override any of the defaults and the above
69  // DefaultValue::Bind ()s at run-time, via command-line arguments
71  cmd.Parse (argc, argv);
72 
73  Ptr<Node> nSrc = CreateObject<Node> ();
74  Ptr<Node> nDst = CreateObject<Node> ();
75  Ptr<Node> nRtr1 = CreateObject<Node> ();
76  Ptr<Node> nRtr2 = CreateObject<Node> ();
77  Ptr<Node> nDstRtr = CreateObject<Node> ();
78 
79  NodeContainer c = NodeContainer (nSrc, nDst, nRtr1, nRtr2, nDstRtr);
80 
81  InternetStackHelper internet;
82  internet.Install (c);
83 
84  // Point-to-point links
85  NodeContainer nSrcnRtr1 = NodeContainer (nSrc, nRtr1);
86  NodeContainer nSrcnRtr2 = NodeContainer (nSrc, nRtr2);
87  NodeContainer nRtr1nDstRtr = NodeContainer (nRtr1, nDstRtr);
88  NodeContainer nRtr2nDstRtr = NodeContainer (nRtr2, nDstRtr);
89  NodeContainer nDstRtrnDst = NodeContainer (nDstRtr, nDst);
90 
91  // We create the channels first without any IP addressing information
93  p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
94  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
95  NetDeviceContainer dSrcdRtr1 = p2p.Install (nSrcnRtr1);
96  NetDeviceContainer dSrcdRtr2 = p2p.Install (nSrcnRtr2);
97  NetDeviceContainer dRtr1dDstRtr = p2p.Install (nRtr1nDstRtr);
98  NetDeviceContainer dRtr2dDstRtr = p2p.Install (nRtr2nDstRtr);
99  NetDeviceContainer dDstRtrdDst = p2p.Install (nDstRtrnDst);
100 
101  Ptr<NetDevice> SrcToRtr1=dSrcdRtr1.Get (0);
102  Ptr<NetDevice> SrcToRtr2=dSrcdRtr2.Get (0);
103 
104  // Later, we add IP addresses.
105  Ipv4AddressHelper ipv4;
106  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
107  Ipv4InterfaceContainer iSrciRtr1 = ipv4.Assign (dSrcdRtr1);
108  ipv4.SetBase ("10.1.2.0", "255.255.255.0");
109  Ipv4InterfaceContainer iSrciRtr2 = ipv4.Assign (dSrcdRtr2);
110  ipv4.SetBase ("10.10.1.0", "255.255.255.0");
111  Ipv4InterfaceContainer iRtr1iDstRtr = ipv4.Assign (dRtr1dDstRtr);
112  ipv4.SetBase ("10.10.2.0", "255.255.255.0");
113  Ipv4InterfaceContainer iRtr2iDstRtr = ipv4.Assign (dRtr2dDstRtr);
114  ipv4.SetBase ("10.20.1.0", "255.255.255.0");
115  Ipv4InterfaceContainer iDstRtrDst = ipv4.Assign (dDstRtrdDst);
116 
117  Ptr<Ipv4> ipv4Src = nSrc->GetObject<Ipv4> ();
118  Ptr<Ipv4> ipv4Rtr1 = nRtr1->GetObject<Ipv4> ();
119  Ptr<Ipv4> ipv4Rtr2 = nRtr2->GetObject<Ipv4> ();
120  Ptr<Ipv4> ipv4DstRtr = nDstRtr->GetObject<Ipv4> ();
121  Ptr<Ipv4> ipv4Dst = nDst->GetObject<Ipv4> ();
122 
123  Ipv4StaticRoutingHelper ipv4RoutingHelper;
124  Ptr<Ipv4StaticRouting> staticRoutingSrc = ipv4RoutingHelper.GetStaticRouting (ipv4Src);
125  Ptr<Ipv4StaticRouting> staticRoutingRtr1 = ipv4RoutingHelper.GetStaticRouting (ipv4Rtr1);
126  Ptr<Ipv4StaticRouting> staticRoutingRtr2 = ipv4RoutingHelper.GetStaticRouting (ipv4Rtr2);
127  Ptr<Ipv4StaticRouting> staticRoutingDstRtr = ipv4RoutingHelper.GetStaticRouting (ipv4DstRtr);
128  Ptr<Ipv4StaticRouting> staticRoutingDst = ipv4RoutingHelper.GetStaticRouting (ipv4Dst);
129 
130  // Create static routes from Src to Dst
131  staticRoutingRtr1->AddHostRouteTo (Ipv4Address ("10.20.1.2"), Ipv4Address ("10.10.1.2"), 2);
132  staticRoutingRtr2->AddHostRouteTo (Ipv4Address ("10.20.1.2"), Ipv4Address ("10.10.2.2"), 2);
133 
134  // Two routes to same destination - setting separate metrics.
135  // You can switch these to see how traffic gets diverted via different routes
136  staticRoutingSrc->AddHostRouteTo (Ipv4Address ("10.20.1.2"), Ipv4Address ("10.1.1.2"), 1,5);
137  staticRoutingSrc->AddHostRouteTo (Ipv4Address ("10.20.1.2"), Ipv4Address ("10.1.2.2"), 2,10);
138 
139  // Creating static routes from DST to Source pointing to Rtr1 VIA Rtr2(!)
140  staticRoutingDst->AddHostRouteTo (Ipv4Address ("10.1.1.1"), Ipv4Address ("10.20.1.1"), 1);
141  staticRoutingDstRtr->AddHostRouteTo (Ipv4Address ("10.1.1.1"), Ipv4Address ("10.10.2.1"), 2);
142  staticRoutingRtr2->AddHostRouteTo (Ipv4Address ("10.1.1.1"), Ipv4Address ("10.1.2.1"), 1);
143 
144  staticRoutingDst->AddHostRouteTo (Ipv4Address ("10.1.2.1"), Ipv4Address ("10.20.1.1"), 1);
145  staticRoutingDstRtr->AddHostRouteTo (Ipv4Address ("10.1.2.1"), Ipv4Address ("10.10.2.1"), 2);
146  staticRoutingRtr2->AddHostRouteTo (Ipv4Address ("10.1.2.1"), Ipv4Address ("10.1.2.1"), 1);
147 
148  // There are no apps that can utilize the Socket Option so doing the work directly..
149  // Taken from tcp-large-transfer example
150 
151  Ptr<Socket> srcSocket1 = Socket::CreateSocket (nSrc, TypeId::LookupByName ("ns3::TcpSocketFactory"));
152  Ptr<Socket> srcSocket2 = Socket::CreateSocket (nSrc, TypeId::LookupByName ("ns3::TcpSocketFactory"));
153  Ptr<Socket> srcSocket3 = Socket::CreateSocket (nSrc, TypeId::LookupByName ("ns3::TcpSocketFactory"));
154  Ptr<Socket> srcSocket4 = Socket::CreateSocket (nSrc, TypeId::LookupByName ("ns3::TcpSocketFactory"));
155 
156 
157  uint16_t dstport = 12345;
158  Ipv4Address dstaddr ("10.20.1.2");
159 
160  PacketSinkHelper sink ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), dstport));
161  ApplicationContainer apps = sink.Install (nDst);
162  apps.Start (Seconds (0.0));
163  apps.Stop (Seconds (10.0));
164 
165  AsciiTraceHelper ascii;
166  p2p.EnableAsciiAll (ascii.CreateFileStream ("socket-bound-tcp-static-routing.tr"));
167  p2p.EnablePcapAll ("socket-bound-tcp-static-routing");
168 
170  LogComponentEnable ("SocketBoundTcpRoutingExample", LOG_LEVEL_INFO);
171 
172  // First packet as normal (goes via Rtr1)
173  Simulator::Schedule (Seconds (0.1),&StartFlow, srcSocket1, dstaddr, dstport);
174  // Second via Rtr1 explicitly
175  Simulator::Schedule (Seconds (1.0),&BindSock, srcSocket2, SrcToRtr1);
176  Simulator::Schedule (Seconds (1.1),&StartFlow, srcSocket2, dstaddr, dstport);
177  // Third via Rtr2 explicitly
178  Simulator::Schedule (Seconds (2.0),&BindSock, srcSocket3, SrcToRtr2);
179  Simulator::Schedule (Seconds (2.1),&StartFlow, srcSocket3, dstaddr, dstport);
180  // Fourth again as normal (goes via Rtr1)
181  Simulator::Schedule (Seconds (3.0),&BindSock, srcSocket4, Ptr<NetDevice>(0));
182  Simulator::Schedule (Seconds (3.1),&StartFlow, srcSocket4, dstaddr, dstport);
183  // If you uncomment what's below, it results in ASSERT failing since you can't
184  // bind to a socket not existing on a node
185  // Simulator::Schedule(Seconds(4.0),&BindSock, srcSocket, dDstRtrdDst.Get(0));
186  Simulator::Run ();
188 
189  return 0;
190 }
191 
193 {
194  sock->BindToNetDevice (netdev);
195  return;
196 }
197 
198 void StartFlow (Ptr<Socket> localSocket,
199  Ipv4Address servAddress,
200  uint16_t servPort)
201 {
202  NS_LOG_INFO ("Starting flow at time " << Simulator::Now ().GetSeconds ());
203  currentTxBytes = 0;
204  localSocket->Bind ();
205  localSocket->Connect (InetSocketAddress (servAddress, servPort)); //connect
206 
207  // tell the tcp implementation to call WriteUntilBufferFull again
208  // if we blocked and new tx buffer space becomes available
210  WriteUntilBufferFull (localSocket, localSocket->GetTxAvailable ());
211 }
212 
213 void WriteUntilBufferFull (Ptr<Socket> localSocket, uint32_t txSpace)
214 {
215  while (currentTxBytes < totalTxBytes && localSocket->GetTxAvailable () > 0)
216  {
217  uint32_t left = totalTxBytes - currentTxBytes;
218  uint32_t dataOffset = currentTxBytes % writeSize;
219  uint32_t toWrite = writeSize - dataOffset;
220  toWrite = std::min (toWrite, left);
221  toWrite = std::min (toWrite, localSocket->GetTxAvailable ());
222  int amountSent = localSocket->Send (&data[dataOffset], toWrite, 0);
223  if(amountSent < 0)
224  {
225  // we will be called again when new tx space becomes available.
226  return;
227  }
228  currentTxBytes += amountSent;
229  }
230  localSocket->Close ();
231 }
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56
holds a vector of ns3::Application pointers.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
an Inet address class
static Ipv4Address GetAny(void)
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Hold variables of type string.
Definition: string.h:41
#define min(a, b)
Definition: 80211b.c:42
NetDeviceContainer Install(NodeContainer c)
static uint32_t currentTxBytes
static void Run(void)
Run the simulation.
Definition: simulator.cc:170
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
aggregate IP/TCP/UDP functionality to existing Nodes.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
cmd
Definition: second.py:35
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we&#39;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.
static const uint32_t totalTxBytes
void AddHostRouteTo(Ipv4Address dest, Ipv4Address nextHop, uint32_t interface, uint32_t metric=0)
Add a host route to the static routing table.
uint16_t port
Definition: dsdv-manet.cc:45
void StartFlow(Ptr< Socket >, Ipv4Address, uint16_t)
void dstSocketRecv(Ptr< Socket > socket)
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:370
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 ...
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1390
uint8_t data[writeSize]
LOG_INFO and above.
Definition: log.h:107
holds a vector of ns3::NetDevice pointers
static const uint32_t writeSize
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1610
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:71
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:212
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:134
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:461
void LogComponentEnableAll(enum LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:394
Prefix all trace prints with simulation time.
Definition: log.h:119
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
void WriteUntilBufferFull(Ptr< Socket >, uint32_t)
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:193
void SetSendCallback(Callback< void, Ptr< Socket >, uint32_t > sendCb)
Notify application when space in transmit buffer is added.
Definition: socket.cc:121
virtual void BindToNetDevice(Ptr< NetDevice > netdevice)
Bind a socket to specific device.
Definition: socket.cc:330
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void srcSocketRecv(Ptr< Socket > socket)
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
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...
Helper class that adds ns3::Ipv4StaticRouting objects.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1064
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...
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
void SendStuff(Ptr< Socket > sock, Ipv4Address dstaddr, uint16_t port)
virtual int Close(void)=0
Close a socket.
virtual uint32_t GetTxAvailable(void) const =0
Returns the number of bytes which can be sent in a single call to Send.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:829
void BindSock(Ptr< Socket > sock, Ptr< NetDevice > netdev)