-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtcp-tx-buffer.cc
1463 lines (1268 loc) · 43.2 KB
/
tcp-tx-buffer.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2010-2015 Adrian Sai-wah Tam
* Copyright (c) 2016 Natale Patriciello <[email protected]>
*
* 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
*
* Original author: Adrian Sai-wah Tam <[email protected]>
*/
#include <algorithm>
#include <iostream>
#include "ns3/packet.h"
#include "ns3/log.h"
#include "ns3/abort.h"
#include "ns3/tcp-option-ts.h"
#include "tcp-tx-buffer.h"
namespace ns3 {
NS_LOG_COMPONENT_DEFINE ("TcpTxBuffer");
void
TcpTxItem::Print (std::ostream &os) const
{
bool comma = false;
os << "[" << m_startSeq << ";" << m_startSeq + GetSeqSize () << "|"
<< GetSeqSize () << "]";
if (m_lost)
{
os << "[lost]";
comma = true;
}
if (m_retrans)
{
if (comma)
{
os << ",";
}
os << "[retrans]";
comma = true;
}
if (m_sacked)
{
if (comma)
{
os << ",";
}
os << "[sacked]";
comma = true;
}
if (comma)
{
os << ",";
}
os << "[" << m_lastSent.GetSeconds () << "]";
}
NS_OBJECT_ENSURE_REGISTERED (TcpTxBuffer);
TypeId
TcpTxBuffer::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::TcpTxBuffer")
.SetParent<Object> ()
.SetGroupName ("Internet")
.AddConstructor<TcpTxBuffer> ()
.AddTraceSource ("UnackSequence",
"First unacknowledged sequence number (SND.UNA)",
MakeTraceSourceAccessor (&TcpTxBuffer::m_firstByteSeq),
"ns3::SequenceNumber32TracedValueCallback")
;
return tid;
}
/* A user is supposed to create a TcpSocket through a factory. In TcpSocket,
* there are attributes SndBufSize and RcvBufSize to control the default Tx and
* Rx window sizes respectively, with default of 128 KiByte. The attribute
* SndBufSize is passed to TcpTxBuffer by TcpSocketBase::SetSndBufSize() and in
* turn, TcpTxBuffer:SetMaxBufferSize(). Therefore, the m_maxBuffer value
* initialized below is insignificant.
*/
TcpTxBuffer::TcpTxBuffer (uint32_t n)
: m_maxBuffer (32768), m_size (0), m_sentSize (0), m_firstByteSeq (n)
{
}
TcpTxBuffer::~TcpTxBuffer (void)
{
PacketList::iterator it;
for (it = m_sentList.begin (); it != m_sentList.end (); ++it)
{
TcpTxItem *item = *it;
m_sentSize -= item->m_packet->GetSize ();
delete item;
}
for (it = m_appList.begin (); it != m_appList.end (); ++it)
{
TcpTxItem *item = *it;
m_size -= item->m_packet->GetSize ();
delete item;
}
}
SequenceNumber32
TcpTxBuffer::HeadSequence (void) const
{
return m_firstByteSeq;
}
SequenceNumber32
TcpTxBuffer::TailSequence (void) const
{
return m_firstByteSeq + SequenceNumber32 (m_size);
}
uint32_t
TcpTxBuffer::Size (void) const
{
return m_size;
}
uint32_t
TcpTxBuffer::MaxBufferSize (void) const
{
return m_maxBuffer;
}
void
TcpTxBuffer::SetMaxBufferSize (uint32_t n)
{
m_maxBuffer = n;
}
uint32_t
TcpTxBuffer::Available (void) const
{
return m_maxBuffer - m_size;
}
void
TcpTxBuffer::SetHeadSequence (const SequenceNumber32& seq)
{
NS_LOG_FUNCTION (this << seq);
m_firstByteSeq = seq;
if (m_sentList.size () > 0)
{
m_sentList.front ()->m_startSeq = seq;
}
// if you change the head with data already sent, something bad will happen
NS_ASSERT (m_sentList.size () == 0);
m_highestSack = std::make_pair (m_sentList.end (), SequenceNumber32 (0));
}
bool
TcpTxBuffer::Add (Ptr<Packet> p)
{
NS_LOG_FUNCTION (this << p);
NS_LOG_LOGIC ("Try to append " << p->GetSize () << " bytes to window starting at "
<< m_firstByteSeq << ", availSize=" << Available ());
if (p->GetSize () <= Available ())
{
if (p->GetSize () > 0)
{
TcpTxItem *item = new TcpTxItem ();
item->m_packet = p->Copy ();
m_appList.insert (m_appList.end (), item);
m_size += p->GetSize ();
NS_LOG_LOGIC ("Updated size=" << m_size << ", lastSeq=" <<
m_firstByteSeq + SequenceNumber32 (m_size));
}
return true;
}
NS_LOG_LOGIC ("Rejected. Not enough room to buffer packet.");
return false;
}
uint32_t
TcpTxBuffer::SizeFromSequence (const SequenceNumber32& seq) const
{
NS_LOG_FUNCTION (this << seq);
// Sequence of last byte in buffer
SequenceNumber32 lastSeq = TailSequence ();
if (lastSeq >= seq)
{
return static_cast<uint32_t> (lastSeq - seq);
}
NS_LOG_ERROR ("Requested a sequence beyond our space (" << seq << " > " << lastSeq <<
"). Returning 0 for convenience.");
return 0;
}
Ptr<Packet>
TcpTxBuffer::CopyFromSequence (uint32_t numBytes, const SequenceNumber32& seq)
{
NS_LOG_FUNCTION (this << numBytes << seq);
NS_ABORT_MSG_IF (m_firstByteSeq > seq,
"Requested a sequence number which is not in the buffer anymore");
ConsistencyCheck ();
// Real size to extract. Insure not beyond end of data
uint32_t s = std::min (numBytes, SizeFromSequence (seq));
if (s == 0)
{
return Create<Packet> ();
}
TcpTxItem *outItem = nullptr;
if (m_firstByteSeq + m_sentSize >= seq + s)
{
// already sent this block completely
outItem = GetTransmittedSegment (s, seq);
NS_ASSERT (outItem != nullptr);
NS_ASSERT (!outItem->m_sacked);
NS_LOG_DEBUG ("Returning already sent item " << *outItem << " from " << *this);
}
else if (m_firstByteSeq + m_sentSize <= seq)
{
NS_ABORT_MSG_UNLESS (m_firstByteSeq + m_sentSize == seq,
"Requesting a piece of new data with an hole");
// this is the first time we transmit this block
outItem = GetNewSegment (s);
NS_ASSERT (outItem != nullptr);
NS_ASSERT (outItem->m_retrans == false);
NS_LOG_DEBUG ("Returning new item " << *outItem << " from " << *this);
}
else if (m_firstByteSeq.Get ().GetValue () + m_sentSize > seq.GetValue ()
&& m_firstByteSeq.Get ().GetValue () + m_sentSize < seq.GetValue () + s)
{
// Partial: a part is retransmission, the remaining data is new
// Just return the old segment, without taking new data. Hopefully
// TcpSocketBase will request new data
uint32_t amount = (m_firstByteSeq.Get ().GetValue () + m_sentSize) - seq.GetValue ();
return CopyFromSequence (amount, seq);
}
outItem->m_lastSent = Simulator::Now ();
Ptr<Packet> toRet = outItem->m_packet->Copy ();
NS_ASSERT (toRet->GetSize () <= s);
NS_ASSERT_MSG (outItem->m_startSeq >= m_firstByteSeq,
"Returning an item " << *outItem << " with SND.UNA as " <<
m_firstByteSeq);
ConsistencyCheck ();
return toRet;
}
TcpTxItem*
TcpTxBuffer::GetNewSegment (uint32_t numBytes)
{
NS_LOG_FUNCTION (this << numBytes);
SequenceNumber32 startOfAppList = m_firstByteSeq + m_sentSize;
NS_LOG_INFO ("AppList start at " << startOfAppList << ", sentSize = " <<
m_sentSize << " firstByte: " << m_firstByteSeq);
TcpTxItem *item = GetPacketFromList (m_appList, startOfAppList,
numBytes, startOfAppList);
item->m_startSeq = startOfAppList;
// Move item from AppList to SentList (should be the first, not too complex)
auto it = std::find (m_appList.begin (), m_appList.end (), item);
NS_ASSERT (it != m_appList.end ());
m_appList.erase (it);
m_sentList.insert (m_sentList.end (), item);
m_sentSize += item->m_packet->GetSize ();
return item;
}
TcpTxItem*
TcpTxBuffer::GetTransmittedSegment (uint32_t numBytes, const SequenceNumber32 &seq)
{
NS_LOG_FUNCTION (this << numBytes << seq);
NS_ASSERT (seq >= m_firstByteSeq);
NS_ASSERT (numBytes <= m_sentSize);
NS_ASSERT (m_sentList.size () >= 1);
auto it = m_sentList.begin ();
bool listEdited = false;
uint32_t s = numBytes;
// Avoid to merge different packet for this retransmission if flags are
// different.
for (; it != m_sentList.end(); ++it)
{
if ((*it)->m_startSeq == seq)
{
auto next = it;
next++;
if (next != m_sentList.end ())
{
// Next is not sacked... there is the possibility to merge
if (! (*next)->m_sacked)
{
s = std::min(s, (*it)->m_packet->GetSize () + (*next)->m_packet->GetSize ());
}
else
{
// Next is sacked... better to retransmit only the first segment
s = std::min(s, (*it)->m_packet->GetSize ());
}
}
else
{
s = std::min(s, (*it)->m_packet->GetSize ());
}
break;
}
}
TcpTxItem *item = GetPacketFromList (m_sentList, m_firstByteSeq, s, seq, &listEdited);
if (! item->m_retrans)
{
m_retrans += item->m_packet->GetSize ();
item->m_retrans = true;
}
return item;
}
std::pair <TcpTxBuffer::PacketList::const_iterator, SequenceNumber32>
TcpTxBuffer::FindHighestSacked () const
{
NS_LOG_FUNCTION (this);
SequenceNumber32 beginOfCurrentPacket = m_firstByteSeq;
auto ret = std::make_pair (m_sentList.end (), SequenceNumber32 (0));
for (auto it = m_sentList.begin (); it != m_sentList.end (); ++it)
{
const TcpTxItem *item = *it;
if (item->m_sacked)
{
ret = std::make_pair (it, beginOfCurrentPacket);
}
beginOfCurrentPacket += item->m_packet->GetSize ();
}
return ret;
}
void
TcpTxBuffer::SplitItems (TcpTxItem *t1, TcpTxItem *t2, uint32_t size) const
{
NS_ASSERT (t1 != nullptr && t2 != nullptr);
NS_LOG_FUNCTION (this << *t2 << size);
t1->m_packet = t2->m_packet->CreateFragment (0, size);
t2->m_packet->RemoveAtStart (size);
t1->m_startSeq = t2->m_startSeq;
t1->m_sacked = t2->m_sacked;
t1->m_lastSent = t2->m_lastSent;
t1->m_retrans = t2->m_retrans;
t1->m_lost = t2->m_lost;
t2->m_startSeq += size;
NS_LOG_INFO ("Split of size " << size << " result: t1 " << *t1 << " t2 " << *t2);
}
TcpTxItem*
TcpTxBuffer::GetPacketFromList (PacketList &list, const SequenceNumber32 &listStartFrom,
uint32_t numBytes, const SequenceNumber32 &seq,
bool *listEdited) const
{
NS_LOG_FUNCTION (this << numBytes << seq);
/*
* Our possibilities are sketched out in the following:
*
* |------| |----| |----|
* GetList (m_data) = | | --> | | --> | |
* |------| |----| |----|
*
* ^ ^ ^ ^
* | | | | (1)
* seq | | numBytes
* | |
* | |
* seq numBytes (2)
*
* (1) seq and numBytes are the boundary of some packet
* (2) seq and numBytes are not the boundary of some packet
*
* We can have mixed case (e.g. seq over the boundary while numBytes not).
*
* If we discover that we are in (2) or in a mixed case, we split
* packets accordingly to the requested bounds and re-run the function.
*
* In (1), things are pretty easy, it's just a matter of walking the list and
* defragment packets, if needed (e.g. seq is the beginning of the first packet
* while maxBytes is the end of some packet next in the list).
*/
Ptr<Packet> currentPacket = nullptr;
TcpTxItem *currentItem = nullptr;
TcpTxItem *outItem = nullptr;
PacketList::iterator it = list.begin ();
SequenceNumber32 beginOfCurrentPacket = listStartFrom;
while (it != list.end ())
{
currentItem = *it;
currentPacket = currentItem->m_packet;
NS_ASSERT_MSG (list != m_sentList || currentItem->m_startSeq >= m_firstByteSeq,
"start: " << m_firstByteSeq << " currentItem start: " <<
currentItem->m_startSeq);
// The objective of this snippet is to find (or to create) the packet
// that begin with the sequence seq
if (seq < beginOfCurrentPacket + currentPacket->GetSize ())
{
// seq is inside the current packet
if (seq == beginOfCurrentPacket)
{
// seq is the beginning of the current packet. Hurray!
outItem = currentItem;
NS_LOG_INFO ("Current packet starts at seq " << seq <<
" ends at " << seq + currentPacket->GetSize ());
}
else if (seq > beginOfCurrentPacket)
{
// seq is inside the current packet but seq is not the beginning,
// it's somewhere in the middle. Just fragment the beginning and
// start again.
NS_LOG_INFO ("we are at " << beginOfCurrentPacket <<
" searching for " << seq <<
" and now we recurse because packet ends at "
<< beginOfCurrentPacket + currentPacket->GetSize ());
TcpTxItem *firstPart = new TcpTxItem ();
SplitItems (firstPart, currentItem, seq - beginOfCurrentPacket);
// insert firstPart before currentItem
list.insert (it, firstPart);
if (listEdited)
{
*listEdited = true;
}
return GetPacketFromList (list, listStartFrom, numBytes, seq, listEdited);
}
else
{
NS_FATAL_ERROR ("seq < beginOfCurrentPacket: our data is before");
}
}
else
{
// Walk the list, the current packet does not contain seq
beginOfCurrentPacket += currentPacket->GetSize ();
it++;
continue;
}
NS_ASSERT (outItem != nullptr);
// The objective of this snippet is to find (or to create) the packet
// that ends after numBytes bytes. We are sure that outPacket starts
// at seq.
if (seq + numBytes <= beginOfCurrentPacket + currentPacket->GetSize ())
{
// the end boundary is inside the current packet
if (numBytes == currentPacket->GetSize ())
{
// the end boundary is exactly the end of the current packet. Hurray!
if (currentItem->m_packet == outItem->m_packet)
{
// A perfect match!
return outItem;
}
else
{
// the end is exactly the end of current packet, but
// current > outPacket in the list. Merge current with the
// previous, and recurse.
NS_ASSERT (it != list.begin ());
TcpTxItem *previous = *(--it);
list.erase (it);
MergeItems (previous, currentItem);
delete currentItem;
if (listEdited)
{
*listEdited = true;
}
return GetPacketFromList (list, listStartFrom, numBytes, seq, listEdited);
}
}
else if (numBytes < currentPacket->GetSize ())
{
// the end is inside the current packet, but it isn't exactly
// the packet end. Just fragment, fix the list, and return.
TcpTxItem *firstPart = new TcpTxItem ();
SplitItems (firstPart, currentItem, numBytes);
// insert firstPart before currentItem
list.insert (it, firstPart);
if (listEdited)
{
*listEdited = true;
}
return firstPart;
}
}
else
{
// The end isn't inside current packet, but there is an exception for
// the merge and recurse strategy...
if (++it == list.end ())
{
// ...current is the last packet we sent. We have not more data;
// Go for this one.
NS_LOG_WARN ("Cannot reach the end, but this case is covered "
"with conditional statements inside CopyFromSequence."
"Something has gone wrong, report a bug");
return outItem;
}
// The current packet does not contain the requested end. Merge current
// with the packet that follows, and recurse
TcpTxItem *next = (*it); // Please remember we have incremented it
// in the previous if
MergeItems (currentItem, next);
list.erase (it);
delete next;
if (listEdited)
{
*listEdited = true;
}
return GetPacketFromList (list, listStartFrom, numBytes, seq, listEdited);
}
}
NS_FATAL_ERROR ("This point is not reachable");
}
static bool AreEquals (const bool &first, const bool &second)
{
return first ? second : !second;
}
void
TcpTxBuffer::MergeItems (TcpTxItem *t1, TcpTxItem *t2) const
{
NS_ASSERT (t1 != nullptr && t2 != nullptr);
NS_LOG_FUNCTION (this << *t1 << *t2);
NS_LOG_INFO ("Merging " << *t2 << " into " << *t1);
NS_ASSERT_MSG (AreEquals (t1->m_sacked, t2->m_sacked),
"Merging one sacked and another not sacked. Impossible");
NS_ASSERT_MSG (AreEquals (t1->m_lost, t2->m_lost),
"Merging one lost and another not lost. Impossible");
// If one is retrans and the other is not, cancel the retransmitted flag.
// We are merging this segment for the retransmit, so the count will
// be updated in GetTransmittedSegment.
if (! AreEquals (t1->m_retrans, t2->m_retrans))
{
if (t1->m_retrans)
{
TcpTxBuffer *self = const_cast<TcpTxBuffer*> (this);
self->m_retrans -= t1->m_packet->GetSize ();
t1->m_retrans = false;
}
else
{
NS_ASSERT (t2->m_retrans);
TcpTxBuffer *self = const_cast<TcpTxBuffer*> (this);
self->m_retrans -= t2->m_packet->GetSize ();
t2->m_retrans = false;
}
}
if (t1->m_lastSent < t2->m_lastSent)
{
t1->m_lastSent = t2->m_lastSent;
}
t1->m_packet->AddAtEnd (t2->m_packet);
NS_LOG_INFO ("Situation after the merge: " << *t1);
}
void
TcpTxBuffer::RemoveFromCounts (TcpTxItem *item, uint32_t size)
{
NS_LOG_FUNCTION (this << *item << size);
if (item->m_sacked)
{
NS_ASSERT (m_sackedOut >= size);
m_sackedOut -= size;
}
if (item->m_retrans)
{
NS_ASSERT (m_retrans >= size);
m_retrans -= size;
}
if (item->m_lost)
{
NS_ASSERT_MSG (m_lostOut >= size, "Trying to remove " << size <<
" bytes from " << m_lostOut);
m_lostOut -= size;
}
}
void
TcpTxBuffer::DiscardUpTo (const SequenceNumber32& seq)
{
NS_LOG_FUNCTION (this << seq);
// Cases do not need to scan the buffer
if (m_firstByteSeq >= seq)
{
NS_LOG_DEBUG ("Seq " << seq << " already discarded.");
return;
}
NS_LOG_DEBUG ("Remove up to " << seq << " lost: " << m_lostOut <<
" retrans: " << m_retrans << " sacked: " << m_sackedOut);
// Scan the buffer and discard packets
uint32_t offset = seq - m_firstByteSeq.Get (); // Number of bytes to remove
uint32_t pktSize;
PacketList::iterator i = m_sentList.begin ();
while (m_size > 0 && offset > 0)
{
if (i == m_sentList.end ())
{
// Move data from app list to sent list, so we can delete the item
Ptr<Packet> p = CopyFromSequence (offset, m_firstByteSeq);
NS_ASSERT (p != nullptr);
NS_UNUSED (p);
i = m_sentList.begin ();
NS_ASSERT (i != m_sentList.end ());
}
TcpTxItem *item = *i;
Ptr<Packet> p = item->m_packet;
pktSize = p->GetSize ();
NS_ASSERT_MSG (item->m_startSeq == m_firstByteSeq,
"Item starts at " << item->m_startSeq <<
" while SND.UNA is " << m_firstByteSeq << " from " << *this);
if (offset >= pktSize)
{ // This packet is behind the seqnum. Remove this packet from the buffer
m_size -= pktSize;
m_sentSize -= pktSize;
offset -= pktSize;
m_firstByteSeq += pktSize;
RemoveFromCounts (item, pktSize);
i = m_sentList.erase (i);
NS_LOG_INFO ("Removed " << *item << " lost: " << m_lostOut <<
" retrans: " << m_retrans << " sacked: " << m_sackedOut <<
". Remaining data " << m_size);
delete item;
}
else if (offset > 0)
{ // Part of the packet is behind the seqnum. Fragment
pktSize -= offset;
NS_LOG_INFO (*item);
// PacketTags are preserved when fragmenting
item->m_packet = item->m_packet->CreateFragment (offset, pktSize);
item->m_startSeq += offset;
m_size -= offset;
m_sentSize -= offset;
m_firstByteSeq += offset;
RemoveFromCounts (item, offset);
NS_LOG_INFO ("Fragmented one packet by size " << offset <<
", new size=" << pktSize << " resulting item is " <<
*item << " status: " << *this);
break;
}
}
// Catching the case of ACKing a FIN
if (m_size == 0)
{
m_firstByteSeq = seq;
}
if (!m_sentList.empty ())
{
TcpTxItem *head = m_sentList.front ();
if (head->m_sacked)
{
NS_ASSERT (!head->m_lost);
// It is not possible to have the UNA sacked; otherwise, it would
// have been ACKed. This is, most likely, our wrong guessing
// when adding Reno dupacks in the count.
head->m_sacked = false;
m_sackedOut -= head->m_packet->GetSize ();
NS_LOG_INFO ("Moving the SACK flag from the HEAD to another segment");
AddRenoSack ();
MarkHeadAsLost ();
}
NS_ASSERT_MSG (head->m_startSeq == seq,
"While removing up to " << seq << " we get SND.UNA to " <<
m_firstByteSeq << " this is the result: " << *this);
}
if (m_highestSack.second <= m_firstByteSeq)
{
m_highestSack = std::make_pair (m_sentList.end (), SequenceNumber32 (0));
}
NS_LOG_DEBUG ("Discarded up to " << seq << " lost: " << m_lostOut <<
" retrans: " << m_retrans << " sacked: " << m_sackedOut);
NS_LOG_LOGIC ("Buffer status after discarding data " << *this);
NS_ASSERT (m_firstByteSeq >= seq);
NS_ASSERT (m_sentSize >= m_sackedOut + m_lostOut);
ConsistencyCheck ();
}
bool
TcpTxBuffer::Update (const TcpOptionSack::SackList &list)
{
NS_LOG_FUNCTION (this);
NS_LOG_INFO ("Updating scoreboard, got " << list.size () << " blocks to analyze");
bool modified = false;
for (auto option_it = list.begin (); option_it != list.end (); ++option_it)
{
PacketList::iterator item_it = m_sentList.begin ();
SequenceNumber32 beginOfCurrentPacket = m_firstByteSeq;
if (m_firstByteSeq + m_sentSize < (*option_it).first && !modified)
{
NS_LOG_INFO ("Not updating scoreboard, the option block is outside the sent list");
return false;
}
while (item_it != m_sentList.end ())
{
uint32_t pktSize = (*item_it)->m_packet->GetSize ();
// Check the boundary of this packet ... only mark as sacked if
// it is precisely mapped over the option. It means that if the receiver
// is reporting as sacked single range bytes that are not mapped 1:1
// in what we have, the option is discarded. There's room for improvement
// here.
if (beginOfCurrentPacket >= (*option_it).first
&& beginOfCurrentPacket + pktSize <= (*option_it).second)
{
if ((*item_it)->m_sacked)
{
NS_ASSERT (!(*item_it)->m_lost);
NS_LOG_INFO ("Received block " << *option_it <<
", checking sentList for block " << *(*item_it) <<
", found in the sackboard already sacked");
}
else
{
if ((*item_it)->m_lost)
{
(*item_it)->m_lost = false;
m_lostOut -= (*item_it)->m_packet->GetSize ();
}
(*item_it)->m_sacked = true;
m_sackedOut += (*item_it)->m_packet->GetSize ();
if (m_highestSack.first == m_sentList.end()
|| m_highestSack.second <= beginOfCurrentPacket + pktSize)
{
m_highestSack = std::make_pair (item_it, beginOfCurrentPacket);
}
NS_LOG_INFO ("Received block " << *option_it <<
", checking sentList for block " << *(*item_it) <<
", found in the sackboard, sacking, current highSack: " <<
m_highestSack.second);
}
modified = true;
}
else if (beginOfCurrentPacket + pktSize > (*option_it).second)
{
// We already passed the received block end. Exit from the loop
NS_LOG_INFO ("Received block [" << *option_it <<
", checking sentList for block " << *(*item_it) <<
"], not found, breaking loop");
break;
}
beginOfCurrentPacket += pktSize;
++item_it;
}
}
if (modified)
{
NS_ASSERT_MSG (modified && m_highestSack.first != m_sentList.end(), "Buffer status: " << *this);
UpdateLostCount ();
}
NS_ASSERT ((*(m_sentList.begin ()))->m_sacked == false);
NS_ASSERT_MSG (m_sentSize >= m_sackedOut + m_lostOut, *this);
//NS_ASSERT (list.size () == 0 || modified); // Assert for duplicated SACK or
// impossiblity to map the option into the sent blocks
ConsistencyCheck ();
return modified;
}
void
TcpTxBuffer::UpdateLostCount ()
{
NS_LOG_FUNCTION (this);
uint32_t sacked = 0;
SequenceNumber32 beginOfCurrentPacket = m_highestSack.second;
if (m_highestSack.first == m_sentList.end ())
{
NS_LOG_INFO ("Status before the update: " << *this <<
", will start from the latest sent item");
}
else
{
NS_LOG_INFO ("Status before the update: " << *this <<
", will start from item " << *(*m_highestSack.first));
}
for (auto it = m_highestSack.first; it != m_sentList.begin(); --it)
{
TcpTxItem *item = *it;
if (item->m_sacked)
{
sacked++;
}
if (sacked >= m_dupAckThresh)
{
if (!item->m_sacked && !item->m_lost)
{
item->m_lost = true;
m_lostOut += item->m_packet->GetSize ();
}
}
beginOfCurrentPacket -= item->m_packet->GetSize ();
}
if (sacked >= m_dupAckThresh)
{
TcpTxItem *item = *m_sentList.begin ();
if (!item->m_lost)
{
item->m_lost = true;
m_lostOut += item->m_packet->GetSize ();
}
}
NS_LOG_INFO ("Status after the update: " << *this);
ConsistencyCheck ();
}
bool
TcpTxBuffer::IsLost (const SequenceNumber32 &seq) const
{
NS_LOG_FUNCTION (this << seq);
SequenceNumber32 beginOfCurrentPacket = m_firstByteSeq;
PacketList::const_iterator it;
if (seq >= m_highestSack.second)
{
return false;
}
// In theory, using a map and hints when inserting elements can improve
// performance
for (it = m_sentList.begin (); it != m_sentList.end (); ++it)
{
// Search for the right iterator before calling IsLost()
if (beginOfCurrentPacket >= seq)
{
if ((*it)->m_lost == true)
{
NS_LOG_INFO ("seq=" << seq << " is lost because of lost flag");
return true;
}
if ((*it)->m_sacked == true)
{
NS_LOG_INFO ("seq=" << seq << " is not lost because of sacked flag");
return false;
}
}
beginOfCurrentPacket += (*it)->m_packet->GetSize ();
}
return false;
}
bool
TcpTxBuffer::NextSeg (SequenceNumber32 *seq, bool isRecovery) const
{
NS_LOG_FUNCTION (this);
/* RFC 6675, NextSeg definition.
*
* (1) If there exists a smallest unSACKed sequence number 'S2' that
* meets the following three criteria for determining loss, the
* sequence range of one segment of up to SMSS octets starting
* with S2 MUST be returned.
*
* (1.a) S2 is greater than HighRxt.
*
* (1.b) S2 is less than the highest octet covered by any
* received SACK.
*
* (1.c) IsLost (S2) returns true.
*/
PacketList::const_iterator it;
TcpTxItem *item;
SequenceNumber32 seqPerRule3;
bool isSeqPerRule3Valid = false;
SequenceNumber32 beginOfCurrentPkt = m_firstByteSeq;
for (it = m_sentList.begin (); it != m_sentList.end (); ++it)
{
item = *it;
// Condition 1.a , 1.b , and 1.c
if (item->m_retrans == false && item->m_sacked == false)
{
if (item->m_lost)
{
NS_LOG_INFO("IsLost, returning" << beginOfCurrentPkt);
*seq = beginOfCurrentPkt;
return true;
}
else if (seqPerRule3.GetValue () == 0 && isRecovery)
{
NS_LOG_INFO ("Saving for rule 3 the seq " << beginOfCurrentPkt);
isSeqPerRule3Valid = true;
seqPerRule3 = beginOfCurrentPkt;
}
}
// Nothing found, iterate
beginOfCurrentPkt += item->m_packet->GetSize ();
}
/* (2) If no sequence number 'S2' per rule (1) exists but there
* exists available unsent data and the receiver's advertised
* window allows, the sequence range of one segment of up to SMSS
* octets of previously unsent data starting with sequence number
* HighData+1 MUST be returned.
*/
if (SizeFromSequence (m_firstByteSeq + m_sentSize) > 0)
{
NS_LOG_INFO ("There is unsent data. Send it");
*seq = m_firstByteSeq + m_sentSize;
return true;
}
else