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
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
57 cmd.Parse(argc, argv);
58
64
66
67 InternetStackHelper internet;
68 internet.Install(c);
69
70 // Point-to-point links
76
77 // We create the channels first without any IP addressing information
79 p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
80 p2p.SetChannelAttribute("Delay", StringValue("2ms"));
86
89
90 // Later, we add IP addresses.
92 ipv4.SetBase("10.1.1.0", "255.255.255.0");
94 ipv4.SetBase("10.1.2.0", "255.255.255.0");
96 ipv4.SetBase("10.10.1.0", "255.255.255.0");
98 ipv4.SetBase("10.10.2.0", "255.255.255.0");
100 ipv4.SetBase("10.20.1.0", "255.255.255.0");
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
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
138 Socket::CreateSocket(nSrc, TypeId::LookupByName("ns3::TcpSocketFactory"));
140 Socket::CreateSocket(nSrc, TypeId::LookupByName("ns3::TcpSocketFactory"));
142 Socket::CreateSocket(nSrc, TypeId::LookupByName("ns3::TcpSocketFactory"));
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",
152 apps.Start(Seconds(0));
153 apps.Stop(Seconds(10));
154
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)
164 // Second via Rtr1 explicitly
167 // Third via Rtr2 explicitly
170 // Fourth again as normal (goes via Rtr1)
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
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
199 WriteUntilBufferFull(localSocket, localSocket->GetTxAvailable());
200}
201
202void
204{
205 while (currentTxBytes < totalTxBytes && localSocket->GetTxAvailable() > 0)
206 {
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 }
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.
Manage ASCII trace files for device models.
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
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.
Definition ptr.h:66
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 > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
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