A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
socket-bound-tcp-static-routing.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 */
5
6/* Test program for multi-interface host, static routing
7
8 Destination host (10.20.1.2)
9 |
10 | 10.20.1.0/24
11 DSTRTR
12 10.10.1.0/24 / \ 10.10.2.0/24
13 / \
14 Rtr1 Rtr2
15 10.1.1.0/24 | | 10.1.2.0/24
16 | /
17 \ /
18 Source
19*/
20
21#include "ns3/applications-module.h"
22#include "ns3/core-module.h"
23#include "ns3/internet-module.h"
24#include "ns3/ipv4-list-routing-helper.h"
25#include "ns3/ipv4-static-routing-helper.h"
26#include "ns3/network-module.h"
27#include "ns3/point-to-point-module.h"
28
29#include <cassert>
30#include <fstream>
31#include <iostream>
32#include <string>
33
34using namespace ns3;
35
36NS_LOG_COMPONENT_DEFINE("SocketBoundTcpRoutingExample");
37
38static const uint32_t totalTxBytes = 20000;
40static const uint32_t writeSize = 1040;
41uint8_t data[writeSize];
42
43void StartFlow(Ptr<Socket>, Ipv4Address, uint16_t);
45
46void SendStuff(Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port);
47void BindSock(Ptr<Socket> sock, Ptr<NetDevice> netdev);
48void srcSocketRecv(Ptr<Socket> socket);
49void dstSocketRecv(Ptr<Socket> socket);
50
51int
52main(int argc, char* argv[])
53{
54 // Allow the user to override any of the defaults and the above
55 // DefaultValue::Bind ()s at run-time, via command-line arguments
56 CommandLine cmd(__FILE__);
57 cmd.Parse(argc, argv);
58
63 Ptr<Node> nDstRtr = CreateObject<Node>();
64
65 NodeContainer c = NodeContainer(nSrc, nDst, nRtr1, nRtr2, nDstRtr);
66
67 InternetStackHelper internet;
68 internet.Install(c);
69
70 // Point-to-point links
71 NodeContainer nSrcnRtr1 = NodeContainer(nSrc, nRtr1);
72 NodeContainer nSrcnRtr2 = NodeContainer(nSrc, nRtr2);
73 NodeContainer nRtr1nDstRtr = NodeContainer(nRtr1, nDstRtr);
74 NodeContainer nRtr2nDstRtr = NodeContainer(nRtr2, nDstRtr);
75 NodeContainer nDstRtrnDst = NodeContainer(nDstRtr, nDst);
76
77 // We create the channels first without any IP addressing information
79 p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
80 p2p.SetChannelAttribute("Delay", StringValue("2ms"));
81 NetDeviceContainer dSrcdRtr1 = p2p.Install(nSrcnRtr1);
82 NetDeviceContainer dSrcdRtr2 = p2p.Install(nSrcnRtr2);
83 NetDeviceContainer dRtr1dDstRtr = p2p.Install(nRtr1nDstRtr);
84 NetDeviceContainer dRtr2dDstRtr = p2p.Install(nRtr2nDstRtr);
85 NetDeviceContainer dDstRtrdDst = p2p.Install(nDstRtrnDst);
86
87 Ptr<NetDevice> SrcToRtr1 = dSrcdRtr1.Get(0);
88 Ptr<NetDevice> SrcToRtr2 = dSrcdRtr2.Get(0);
89
90 // Later, we add IP addresses.
92 ipv4.SetBase("10.1.1.0", "255.255.255.0");
93 Ipv4InterfaceContainer iSrciRtr1 = ipv4.Assign(dSrcdRtr1);
94 ipv4.SetBase("10.1.2.0", "255.255.255.0");
95 Ipv4InterfaceContainer iSrciRtr2 = ipv4.Assign(dSrcdRtr2);
96 ipv4.SetBase("10.10.1.0", "255.255.255.0");
97 Ipv4InterfaceContainer iRtr1iDstRtr = ipv4.Assign(dRtr1dDstRtr);
98 ipv4.SetBase("10.10.2.0", "255.255.255.0");
99 Ipv4InterfaceContainer iRtr2iDstRtr = ipv4.Assign(dRtr2dDstRtr);
100 ipv4.SetBase("10.20.1.0", "255.255.255.0");
101 Ipv4InterfaceContainer iDstRtrDst = ipv4.Assign(dDstRtrdDst);
102
103 Ptr<Ipv4> ipv4Src = nSrc->GetObject<Ipv4>();
104 Ptr<Ipv4> ipv4Rtr1 = nRtr1->GetObject<Ipv4>();
105 Ptr<Ipv4> ipv4Rtr2 = nRtr2->GetObject<Ipv4>();
106 Ptr<Ipv4> ipv4DstRtr = nDstRtr->GetObject<Ipv4>();
107 Ptr<Ipv4> ipv4Dst = nDst->GetObject<Ipv4>();
108
109 Ipv4StaticRoutingHelper ipv4RoutingHelper;
110 Ptr<Ipv4StaticRouting> staticRoutingSrc = ipv4RoutingHelper.GetStaticRouting(ipv4Src);
111 Ptr<Ipv4StaticRouting> staticRoutingRtr1 = ipv4RoutingHelper.GetStaticRouting(ipv4Rtr1);
112 Ptr<Ipv4StaticRouting> staticRoutingRtr2 = ipv4RoutingHelper.GetStaticRouting(ipv4Rtr2);
113 Ptr<Ipv4StaticRouting> staticRoutingDstRtr = ipv4RoutingHelper.GetStaticRouting(ipv4DstRtr);
114 Ptr<Ipv4StaticRouting> staticRoutingDst = ipv4RoutingHelper.GetStaticRouting(ipv4Dst);
115
116 // Create static routes from Src to Dst
117 staticRoutingRtr1->AddHostRouteTo(Ipv4Address("10.20.1.2"), Ipv4Address("10.10.1.2"), 2);
118 staticRoutingRtr2->AddHostRouteTo(Ipv4Address("10.20.1.2"), Ipv4Address("10.10.2.2"), 2);
119
120 // Two routes to same destination - setting separate metrics.
121 // You can switch these to see how traffic gets diverted via different routes
122 staticRoutingSrc->AddHostRouteTo(Ipv4Address("10.20.1.2"), Ipv4Address("10.1.1.2"), 1, 5);
123 staticRoutingSrc->AddHostRouteTo(Ipv4Address("10.20.1.2"), Ipv4Address("10.1.2.2"), 2, 10);
124
125 // Creating static routes from DST to Source pointing to Rtr1 VIA Rtr2(!)
126 staticRoutingDst->AddHostRouteTo(Ipv4Address("10.1.1.1"), Ipv4Address("10.20.1.1"), 1);
127 staticRoutingDstRtr->AddHostRouteTo(Ipv4Address("10.1.1.1"), Ipv4Address("10.10.2.1"), 2);
128 staticRoutingRtr2->AddHostRouteTo(Ipv4Address("10.1.1.1"), Ipv4Address("10.1.2.1"), 1);
129
130 staticRoutingDst->AddHostRouteTo(Ipv4Address("10.1.2.1"), Ipv4Address("10.20.1.1"), 1);
131 staticRoutingDstRtr->AddHostRouteTo(Ipv4Address("10.1.2.1"), Ipv4Address("10.10.2.1"), 2);
132 staticRoutingRtr2->AddHostRouteTo(Ipv4Address("10.1.2.1"), Ipv4Address("10.1.2.1"), 1);
133
134 // There are no apps that can utilize the Socket Option so doing the work directly..
135 // Taken from tcp-large-transfer example
136
137 Ptr<Socket> srcSocket1 =
138 Socket::CreateSocket(nSrc, TypeId::LookupByName("ns3::TcpSocketFactory"));
139 Ptr<Socket> srcSocket2 =
140 Socket::CreateSocket(nSrc, TypeId::LookupByName("ns3::TcpSocketFactory"));
141 Ptr<Socket> srcSocket3 =
142 Socket::CreateSocket(nSrc, TypeId::LookupByName("ns3::TcpSocketFactory"));
143 Ptr<Socket> srcSocket4 =
144 Socket::CreateSocket(nSrc, TypeId::LookupByName("ns3::TcpSocketFactory"));
145
146 uint16_t dstport = 12345;
147 Ipv4Address dstaddr("10.20.1.2");
148
149 PacketSinkHelper sink("ns3::TcpSocketFactory",
151 ApplicationContainer apps = sink.Install(nDst);
152 apps.Start(Seconds(0));
153 apps.Stop(Seconds(10));
154
155 AsciiTraceHelper ascii;
156 p2p.EnableAsciiAll(ascii.CreateFileStream("socket-bound-tcp-static-routing.tr"));
157 p2p.EnablePcapAll("socket-bound-tcp-static-routing");
158
160 LogComponentEnable("SocketBoundTcpRoutingExample", LOG_LEVEL_INFO);
161
162 // First packet as normal (goes via Rtr1)
163 Simulator::Schedule(Seconds(0.1), &StartFlow, srcSocket1, dstaddr, dstport);
164 // Second via Rtr1 explicitly
165 Simulator::Schedule(Seconds(1), &BindSock, srcSocket2, SrcToRtr1);
166 Simulator::Schedule(Seconds(1.1), &StartFlow, srcSocket2, dstaddr, dstport);
167 // Third via Rtr2 explicitly
168 Simulator::Schedule(Seconds(2), &BindSock, srcSocket3, SrcToRtr2);
169 Simulator::Schedule(Seconds(2.1), &StartFlow, srcSocket3, dstaddr, dstport);
170 // Fourth again as normal (goes via Rtr1)
171 Simulator::Schedule(Seconds(3), &BindSock, srcSocket4, Ptr<NetDevice>(nullptr));
172 Simulator::Schedule(Seconds(3.1), &StartFlow, srcSocket4, dstaddr, dstport);
173 // If you uncomment what's below, it results in ASSERT failing since you can't
174 // bind to a socket not existing on a node
175 // Simulator::Schedule(Seconds(4),&BindSock, srcSocket, dDstRtrdDst.Get(0));
178
179 return 0;
180}
181
182void
184{
185 sock->BindToNetDevice(netdev);
186}
187
188void
189StartFlow(Ptr<Socket> localSocket, Ipv4Address servAddress, uint16_t servPort)
190{
191 NS_LOG_INFO("Starting flow at time " << Simulator::Now().GetSeconds());
192 currentTxBytes = 0;
193 localSocket->Bind();
194 localSocket->Connect(InetSocketAddress(servAddress, servPort)); // connect
195
196 // tell the tcp implementation to call WriteUntilBufferFull again
197 // if we blocked and new tx buffer space becomes available
198 localSocket->SetSendCallback(MakeCallback(&WriteUntilBufferFull));
199 WriteUntilBufferFull(localSocket, localSocket->GetTxAvailable());
200}
201
202void
204{
205 while (currentTxBytes < totalTxBytes && localSocket->GetTxAvailable() > 0)
206 {
208 uint32_t dataOffset = currentTxBytes % writeSize;
209 uint32_t toWrite = writeSize - dataOffset;
210 toWrite = std::min(toWrite, left);
211 toWrite = std::min(toWrite, localSocket->GetTxAvailable());
212 int amountSent = localSocket->Send(&data[dataOffset], toWrite, 0);
213 if (amountSent < 0)
214 {
215 // we will be called again when new tx space becomes available.
216 return;
217 }
218 currentTxBytes += amountSent;
219 }
220 localSocket->Close();
221}
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Manage ASCII trace files for device models.
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.
Parse command-line arguments.
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
static Ipv4Address GetAny()
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Helper class that adds ns3::Ipv4StaticRouting objects.
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
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:61
Hold variables of type string.
Definition string.h:45
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
uint16_t port
Definition dsdv-manet.cc:33
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:291
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition log.h:108
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:93
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition log.cc:309
static const uint32_t totalTxBytes
void srcSocketRecv(Ptr< Socket > socket)
void dstSocketRecv(Ptr< Socket > socket)
void SendStuff(Ptr< Socket > sock, Ipv4Address dstaddr, uint16_t port)
static const uint32_t writeSize
void WriteUntilBufferFull(Ptr< Socket >, uint32_t)
static uint32_t currentTxBytes
uint8_t data[writeSize]
void BindSock(Ptr< Socket > sock, Ptr< NetDevice > netdev)
void StartFlow(Ptr< Socket >, Ipv4Address, uint16_t)
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44