-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtcp-rl.cc
364 lines (301 loc) · 7.91 KB
/
tcp-rl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2018 Technische Universität Berlin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Piotr Gawlowicz <gawlowicz@tkn.tu-berlin.de>
*/
#include "tcp-rl.h"
#include "tcp-rl-env.h"
#include "ns3/tcp-header.h"
#include "ns3/object.h"
#include "ns3/node-list.h"
#include "ns3/core-module.h"
#include "ns3/log.h"
#include "ns3/simulator.h"
#include "ns3/tcp-socket-base.h"
#include "ns3/tcp-l4-protocol.h"
namespace ns3 {
NS_OBJECT_ENSURE_REGISTERED (TcpSocketDerived);
TypeId
TcpSocketDerived::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::TcpSocketDerived")
.SetParent<TcpSocketBase> ()
.SetGroupName ("Internet")
.AddConstructor<TcpSocketDerived> ()
;
return tid;
}
TypeId
TcpSocketDerived::GetInstanceTypeId () const
{
return TcpSocketDerived::GetTypeId ();
}
TcpSocketDerived::TcpSocketDerived (void)
{
}
Ptr<TcpCongestionOps>
TcpSocketDerived::GetCongestionControlAlgorithm ()
{
return m_congestionControl;
}
TcpSocketDerived::~TcpSocketDerived (void)
{
}
NS_LOG_COMPONENT_DEFINE ("ns3::TcpRlBase");
NS_OBJECT_ENSURE_REGISTERED (TcpRlBase);
TypeId
TcpRlBase::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::TcpRlBase")
.SetParent<TcpCongestionOps> ()
.SetGroupName ("Internet")
.AddConstructor<TcpRlBase> ()
;
return tid;
}
TcpRlBase::TcpRlBase (void)
: TcpCongestionOps ()
{
NS_LOG_FUNCTION (this);
m_tcpSocket = 0;
m_tcpGymEnv = 0;
}
TcpRlBase::TcpRlBase (const TcpRlBase& sock)
: TcpCongestionOps (sock)
{
NS_LOG_FUNCTION (this);
m_tcpSocket = 0;
m_tcpGymEnv = 0;
}
TcpRlBase::~TcpRlBase (void)
{
m_tcpSocket = 0;
m_tcpGymEnv = 0;
}
uint64_t
TcpRlBase::GenerateUuid ()
{
static uint64_t uuid = 0;
uuid++;
return uuid;
}
void
TcpRlBase::CreateGymEnv()
{
NS_LOG_FUNCTION (this);
// should never be called, only child classes: TcpRl and TcpRlTimeBased
}
void
TcpRlBase::ConnectSocketCallbacks()
{
NS_LOG_FUNCTION (this);
bool foundSocket = false;
for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i) {
Ptr<Node> node = *i;
Ptr<TcpL4Protocol> tcp = node->GetObject<TcpL4Protocol> ();
ObjectVectorValue socketVec;
tcp->GetAttribute ("SocketList", socketVec);
NS_LOG_DEBUG("Node: " << node->GetId() << " TCP socket num: " << socketVec.GetN());
uint32_t sockNum = socketVec.GetN();
for (uint32_t j=0; j<sockNum; j++) {
Ptr<Object> sockObj = socketVec.Get(j);
Ptr<TcpSocketBase> tcpSocket = DynamicCast<TcpSocketBase> (sockObj);
NS_LOG_DEBUG("Node: " << node->GetId() << " TCP Socket: " << tcpSocket);
if(!tcpSocket) { continue; }
Ptr<TcpSocketDerived> dtcpSocket = StaticCast<TcpSocketDerived>(tcpSocket);
Ptr<TcpCongestionOps> ca = dtcpSocket->GetCongestionControlAlgorithm();
NS_LOG_DEBUG("CA name: " << ca->GetName());
Ptr<TcpRlBase> rlCa = DynamicCast<TcpRlBase>(ca);
if (rlCa == this) {
NS_LOG_DEBUG("Found TcpRl CA!");
foundSocket = true;
m_tcpSocket = tcpSocket;
break;
}
}
if (foundSocket) {
break;
}
}
NS_ASSERT_MSG(m_tcpSocket, "TCP socket was not found.");
if(m_tcpSocket) {
NS_LOG_DEBUG("Found TCP Socket: " << m_tcpSocket);
m_tcpSocket->TraceConnectWithoutContext ("Tx", MakeCallback (&TcpGymEnv::TxPktTrace, m_tcpGymEnv));
m_tcpSocket->TraceConnectWithoutContext ("Rx", MakeCallback (&TcpGymEnv::RxPktTrace, m_tcpGymEnv));
NS_LOG_DEBUG("Connect socket callbacks " << m_tcpSocket->GetNode()->GetId());
m_tcpGymEnv->SetNodeId(m_tcpSocket->GetNode()->GetId());
}
}
std::string
TcpRlBase::GetName () const
{
return "TcpRlBase";
}
uint32_t
TcpRlBase::GetSsThresh (Ptr<const TcpSocketState> state,
uint32_t bytesInFlight)
{
NS_LOG_FUNCTION (this << state << bytesInFlight);
if (!m_tcpGymEnv) {
CreateGymEnv();
}
uint32_t newSsThresh = 0;
if (m_tcpGymEnv) {
newSsThresh = m_tcpGymEnv->GetSsThresh(state, bytesInFlight);
}
return newSsThresh;
}
void
TcpRlBase::IncreaseWindow (Ptr<TcpSocketState> tcb, uint32_t segmentsAcked)
{
NS_LOG_FUNCTION (this << tcb << segmentsAcked);
if (!m_tcpGymEnv) {
CreateGymEnv();
}
if (m_tcpGymEnv) {
m_tcpGymEnv->IncreaseWindow(tcb, segmentsAcked);
}
}
void
TcpRlBase::PktsAcked (Ptr<TcpSocketState> tcb, uint32_t segmentsAcked, const Time& rtt)
{
NS_LOG_FUNCTION (this);
if (!m_tcpGymEnv) {
CreateGymEnv();
}
if (m_tcpGymEnv) {
m_tcpGymEnv->PktsAcked(tcb, segmentsAcked, rtt);
}
}
void
TcpRlBase::CongestionStateSet (Ptr<TcpSocketState> tcb, const TcpSocketState::TcpCongState_t newState)
{
NS_LOG_FUNCTION (this);
if (!m_tcpGymEnv) {
CreateGymEnv();
}
if (m_tcpGymEnv) {
m_tcpGymEnv->CongestionStateSet(tcb, newState);
}
}
void
TcpRlBase::CwndEvent (Ptr<TcpSocketState> tcb, const TcpSocketState::TcpCAEvent_t event)
{
NS_LOG_FUNCTION (this);
if (!m_tcpGymEnv) {
CreateGymEnv();
}
if (m_tcpGymEnv) {
m_tcpGymEnv->CwndEvent(tcb, event);
}
}
Ptr<TcpCongestionOps>
TcpRlBase::Fork ()
{
return CopyObject<TcpRlBase> (this);
}
NS_OBJECT_ENSURE_REGISTERED (TcpRl);
TypeId
TcpRl::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::TcpRl")
.SetParent<TcpRlBase> ()
.SetGroupName ("Internet")
.AddConstructor<TcpRl> ()
.AddAttribute ("Reward", "Reward when increasing congestion window.",
DoubleValue (4.0),
MakeDoubleAccessor (&TcpRl::m_reward),
MakeDoubleChecker<double> ())
.AddAttribute ("Penalty", "Reward when increasing congestion window.",
DoubleValue (-10.0),
MakeDoubleAccessor (&TcpRl::m_penalty),
MakeDoubleChecker<double> ())
;
return tid;
}
TcpRl::TcpRl (void)
: TcpRlBase ()
{
NS_LOG_FUNCTION (this);
}
TcpRl::TcpRl (const TcpRl& sock)
: TcpRlBase (sock)
{
NS_LOG_FUNCTION (this);
}
TcpRl::~TcpRl (void)
{
}
std::string
TcpRl::GetName () const
{
return "TcpRl";
}
void
TcpRl::CreateGymEnv()
{
NS_LOG_FUNCTION (this);
Ptr<TcpEventGymEnv> env = CreateObject<TcpEventGymEnv>();
env->SetSocketUuid(TcpRlBase::GenerateUuid());
env->SetReward(m_reward);
env->SetPenalty(m_penalty);
m_tcpGymEnv = env;
ConnectSocketCallbacks();
}
NS_OBJECT_ENSURE_REGISTERED (TcpRlTimeBased);
TypeId
TcpRlTimeBased::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::TcpRlTimeBased")
.SetParent<TcpRlBase> ()
.SetGroupName ("Internet")
.AddConstructor<TcpRlTimeBased> ()
.AddAttribute ("StepTime",
"Step interval used in TCP env. Default: 100ms",
TimeValue (MilliSeconds (100)),
MakeTimeAccessor (&TcpRlTimeBased::m_timeStep),
MakeTimeChecker ())
;
return tid;
}
TcpRlTimeBased::TcpRlTimeBased (void) : TcpRlBase ()
{
NS_LOG_FUNCTION (this);
}
TcpRlTimeBased::TcpRlTimeBased (const TcpRlTimeBased& sock)
: TcpRlBase (sock)
{
NS_LOG_FUNCTION (this);
}
TcpRlTimeBased::~TcpRlTimeBased (void)
{
}
std::string
TcpRlTimeBased::GetName () const
{
return "TcpRlTimeBased";
}
void
TcpRlTimeBased::CreateGymEnv()
{
NS_LOG_FUNCTION (this);
Ptr<TcpTimeStepGymEnv> env = CreateObject<TcpTimeStepGymEnv> (m_timeStep);
env->SetSocketUuid(TcpRlBase::GenerateUuid());
m_tcpGymEnv = env;
ConnectSocketCallbacks();
}
} // namespace ns3