-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAck.cpp
453 lines (397 loc) · 15.5 KB
/
Ack.cpp
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "Logging.h"
#include "MozQuic.h"
#include "MozQuicInternal.h"
#include "Sender.h"
#include "Streams.h"
#include "TransportExtension.h"
#include <array>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
namespace mozquic {
static const uint32_t kReorderingThreshold = 3;
// a request to acknowledge a packetnumber
void
MozQuic::AckScoreboard(uint64_t packetNumber, enum keyPhase kp)
{
if (mStreamState->mAckList.empty()) {
mStreamState->mAckList.emplace_front(packetNumber, Timestamp(), kp);
return;
}
auto iter = mStreamState->mAckList.begin();
for (; iter != mStreamState->mAckList.end(); ++iter) {
if ((iter->mPhase == kp) &&
((iter->mPacketNumber + 1) == packetNumber)) {
// the common case is to just adjust this counter
// in the first element.. but you can't do that if it has
// already been transmitted. (that needs a new node)
if (iter->Transmitted()) {
break;
}
iter->mPacketNumber++;
iter->mExtra++;
iter->mReceiveTime.push_front(Timestamp());
return;
}
if (iter->mPacketNumber >= packetNumber &&
packetNumber >= (iter->mPacketNumber - iter->mExtra)) {
return; // dup
}
if (iter->mPacketNumber < packetNumber) {
break;
}
}
mStreamState->mAckList.emplace(iter, packetNumber, Timestamp(), kp);
}
int
MozQuic::MaybeSendAck(bool delAckOK)
{
if (mStreamState->mAckList.empty()) {
return MOZQUIC_OK;
}
// if we aren't in connected we will only piggyback
if (mConnectionState != CLIENT_STATE_CONNECTED &&
mConnectionState != SERVER_STATE_CONNECTED) {
return MOZQUIC_OK;
}
if (delAckOK &&
mDelAckTimer->Armed() && !mDelAckTimer->Expired()) {
AckLog5("bare ack delayed due to existing delAckTimer\n");
return MOZQUIC_OK;
}
if (delAckOK && !mDelAckTimer->Armed()) {
uint64_t timerVal = mSendState->SmoothedRTT() >> 2;
// todo min and max filters
// todo consider how 1/4 addition to measured rtt loop impacts srtt based things
AckLog5("bare ack arm and delay delAckTimer %d\n", timerVal);
mDelAckTimer->Arm(timerVal);
return MOZQUIC_OK;
}
if (mDelAckTimer->Armed()) {
assert(mDelAckTimer->Expired());
AckLog5("bare ack timer expired\n");
mDelAckTimer->Cancel();
}
for (auto iter = mStreamState->mAckList.begin();
iter != mStreamState->mAckList.end(); ++iter) {
if (iter->Transmitted()) {
continue;
}
AckLog7("Trigger Ack based on %lX (extra %d) kp=%d\n",
iter->mPacketNumber, iter->mExtra, iter->mPhase);
mStreamState->Flush(true);
break;
}
return MOZQUIC_OK;
}
// To clarify.. an ack frame for 15,14,13,11,10,8,2,1
// ackblockcount=3, largest=15, 1stAckBlock= 2 // 15,14,13
// #1 gap=0, additional ack block=1 // 11,10
// #2 gap=0, additional ack block=0 // 8
// #3 gap=4, additional ack block=1 // 2, 1
uint32_t
MozQuic::AckPiggyBack(unsigned char *pkt, uint64_t packetNumberOfAck, uint32_t avail, keyPhase kp,
bool bareAck, uint32_t &used)
{
used = 0;
// build as many ack frames as will fit
uint64_t lowAcked = 0;
bool newFrame = true;
uint32_t outputSize = 0;
unsigned char *ackBlockLocation = NULL;
uint32_t ackBlockCounter = 0;
uint32_t offsetRollbackExtra = 0;
uint32_t offsetCheckpoint = 0;
for (auto iter = mStreamState->mAckList.begin(); iter != mStreamState->mAckList.end(); iter++) {
// list ordered as 7/2, 2/1.. (ack 7,6,5.. 2,1 but not 4,3 or 0) i.e. highest num first
if ((kp <= keyPhaseUnprotected) && (iter->mPhase >= keyPhase0Rtt)) {
AckLog7("skip ack generation of %lX wrong kp need %d the phase is %d\n",
iter->mPacketNumber, iter->mPhase, kp);
break;
}
if (newFrame) {
newFrame = false;
assert(!used);
if (avail < 5) {
return MOZQUIC_OK;
}
pkt[0] = FRAME_TYPE_ACK;
used = 1;
avail -= 1;
if (EncodeVarint(iter->mPacketNumber, pkt + used, avail, outputSize) != MOZQUIC_OK) {
AckLog7("Cannot create new ack frame due to lack of space in packet\n");
used = 0;
return MOZQUIC_OK; // ok to return as we haven't written any of the frame
}
used += outputSize;
avail -= outputSize;
assert(iter->mReceiveTime.size());
uint64_t delay64 = 0;
if (!bareAck) {
delay64 = Timestamp() - *(iter->mReceiveTime.begin());
delay64 *= 1000; // wire encoding is microseconds
if (kp != keyPhaseUnprotected) {
delay64 /= (1ULL << mLocalAckDelayExponent);
} else {
delay64 /= (1ULL << kDefaultAckDelayExponent);
}
}
if (EncodeVarint(delay64, pkt + used, avail, outputSize) != MOZQUIC_OK) {
AckLog7("Cannot create new ack frame due to lack of space in packet\n");
used = 0;
return MOZQUIC_OK; // ok to return as we haven't written any of the frame
}
used += outputSize;
avail -= outputSize;
// we will fill in ackBlockLength at the end, until then reserve 2 bytes for it
if (avail < 2) {
AckLog7("Cannot create new ack frame due to lack of space in packet\n");
used = 0;
return MOZQUIC_OK; // ok to return as we haven't written any of the frame
}
ackBlockLocation = pkt + used;
used += 2;
avail -= 2;
offsetRollbackExtra = used;
if (EncodeVarint(iter->mExtra, pkt + used, avail, outputSize) != MOZQUIC_OK) {
used = 0;
return MOZQUIC_OK; // ok to return as we haven't written any of the frame
}
used += outputSize;
avail -= outputSize;
lowAcked = iter->mPacketNumber - iter->mExtra;
offsetCheckpoint = used;
} else {
assert(lowAcked > iter->mPacketNumber);
if ((lowAcked - iter->mPacketNumber) == 1) {
if (avail < 7) {
// this expands the last entry by an unknown amount
// and we don't have a checkpoint to pop back to when running out of
// room - so just don't start this operation if the worst case cannot
// be accommodated.
break;
}
// crud. There is no gap here which is not allowed by the packet format.
// so we need to logically coalesce the last list entry with this one
assert(offsetRollbackExtra);
assert(used > offsetRollbackExtra);
avail += (used - offsetRollbackExtra);
used = offsetRollbackExtra;
uint64_t lastExtra;
uint32_t lenExtra;
DecodeVarint(pkt + offsetRollbackExtra, avail, lastExtra, lenExtra);
uint64_t newExtra = lastExtra + 1 + iter->mExtra;
lowAcked -= 1 + iter->mExtra;
assert (lowAcked == iter->mPacketNumber - iter->mExtra);
if (EncodeVarint(newExtra, pkt + used, avail, outputSize) != MOZQUIC_OK) {
used = offsetCheckpoint;
break;
}
used += outputSize;
avail -= outputSize;
offsetCheckpoint = used;
} else {
uint64_t gap = lowAcked - iter->mPacketNumber - 2;
if (EncodeVarint(gap, pkt + used, avail, outputSize) != MOZQUIC_OK) {
used = offsetCheckpoint;
break;
}
used += outputSize;
avail -= outputSize;
offsetRollbackExtra = used;
if (EncodeVarint(iter->mExtra, pkt + used, avail, outputSize) != MOZQUIC_OK) {
used = offsetCheckpoint;
break;
}
used += outputSize;
avail -= outputSize;
lowAcked -= iter->mExtra + gap + 2;
assert (lowAcked == iter->mPacketNumber - iter->mExtra);
ackBlockCounter++;
offsetCheckpoint = used;
}
}
AckLog6("created ack of %lX (%d extra) into pn=%lX @ block %d [%d prev transmits]\n",
iter->mPacketNumber, iter->mExtra, packetNumberOfAck, ackBlockCounter, iter->mTransmits.size());
iter->mTransmits.push_back(std::pair<uint64_t, uint64_t>(packetNumberOfAck, Timestamp()));
}
if (ackBlockLocation) {
// this does not impact used or avail as we are just filling in a hole that
// has already been accounted for
EncodeVarintAs2(ackBlockCounter, ackBlockLocation);
}
return MOZQUIC_OK;
}
void
MozQuic::Acknowledge(uint64_t packetNumber, keyPhase kp)
{
assert(mIsChild || mIsClient);
if (packetNumber >= mNextRecvPacketNumber) {
mNextRecvPacketNumber = packetNumber + 1;
}
AckLog6("%p REQUEST TO GEN ACK FOR %lX kp=%d\n", this, packetNumber, kp);
AckScoreboard(packetNumber, kp);
}
void
MozQuic::ProcessAck(FrameHeaderData *ackMetaInfo, const unsigned char *framePtr,
const unsigned char *endOfPacket, bool fromCleartext,
uint32_t &outUsedByAckFrame)
{
// frameptr points to the beginning of the ackblock section
assert (ackMetaInfo->mType == FRAME_TYPE_ACK);
assert (ackMetaInfo->u.mAck.mAckBlocks <= 4096);
const unsigned char *originalFramePtr = framePtr;
outUsedByAckFrame = 0;
uint16_t numRanges = 0;
std::array<std::pair<uint64_t, uint64_t>, 4096> ackStack;
uint64_t largestAcked = ackMetaInfo->u.mAck.mLargestAcked;
for (uint32_t idx = 0; idx < ackMetaInfo->u.mAck.mAckBlocks; idx++) {
if (largestAcked == 0ULL - 1) {
RaiseError(MOZQUIC_ERR_GENERAL, (char *) "invalid ack encoding");
return;
}
uint32_t amtParsed;
if (idx != 0) { // mind the gap
uint64_t gap;
if (DecodeVarint(framePtr, endOfPacket - framePtr, gap, amtParsed) != MOZQUIC_OK) {
RaiseError(MOZQUIC_ERR_GENERAL, (char *) "ack frame header short");
return;
}
framePtr += amtParsed;
if (largestAcked < (gap + 1)) {
RaiseError(MOZQUIC_ERR_GENERAL, (char *) "invalid ack encoding");
return;
}
largestAcked -= gap + 1;
}
uint64_t extra = 0;
if (DecodeVarint(framePtr, endOfPacket - framePtr, extra, amtParsed) != MOZQUIC_OK) {
RaiseError(MOZQUIC_ERR_GENERAL, (char *) "ack frame header short");
return;
}
framePtr += amtParsed;
AckLog5("ACK RECVD (%s) FOR %lX -> %lX\n",
fromCleartext ? "cleartext" : "protected",
largestAcked - extra, largestAcked);
// form a stack here so we can process them starting at the
// lowest packet number, which is how mStreamState->mUnAckedPackets is ordered and
// do it all in one pass
if (numRanges >= 4096) {
RaiseError(MOZQUIC_ERR_GENERAL, (char *) "ack frame too long to handle");
return;
}
ackStack[numRanges++] =
std::pair<uint64_t, uint64_t>(largestAcked - extra, extra + 1);
if (largestAcked < (extra)) {
RaiseError(MOZQUIC_ERR_GENERAL, (char *) "invalid ack encoding");
return;
}
largestAcked -= extra + 1;
}
outUsedByAckFrame = framePtr - originalFramePtr;
bool maybeDoEarlyRetransmit = false;
uint64_t reportLossLessThan = 0;
auto dataIter = mStreamState->mUnAckedPackets.cbegin();
for (auto iters = numRanges; iters > 0; --iters) {
uint64_t haveAckFor = ackStack[iters - 1].first;
uint64_t haveAckForEnd = haveAckFor + ackStack[iters - 1].second;
if (mPMTUD1PacketNumber &&
(mPMTUD1PacketNumber >= haveAckFor) &&
(mPMTUD1PacketNumber < haveAckForEnd)) {
CompletePMTUD1();
}
for (; haveAckFor < haveAckForEnd; haveAckFor++) {
// skip over stuff that is too low
for (; (dataIter != mStreamState->mUnAckedPackets.cend()) && ((*dataIter)->mPacketNumber < haveAckFor); dataIter++);
if ((dataIter == mStreamState->mUnAckedPackets.cend()) || ((*dataIter)->mPacketNumber > haveAckFor)) {
AckLog9("haveAckFor %lX did not find matching unacked data\n", haveAckFor)
} else {
assert ((*dataIter)->mPacketNumber == haveAckFor);
if ((haveAckFor == mHighestTransmittedAckable) && haveAckFor) {
maybeDoEarlyRetransmit = true;
}
AckLog5("haveAckFor %lX found unacked data. packet size %d fromrto=%d\n", haveAckFor,
(*dataIter)->mPacketLen, (*dataIter)->mFromRTO);
if ((*dataIter)->mFromRTO) {
reportLossLessThan = std::max(reportLossLessThan, (*dataIter)->mPacketNumber);
}
if (ackMetaInfo->u.mAck.mLargestAcked == haveAckFor) {
uint64_t xmit = (*dataIter)->mTransmitTime;
mSendState->RTTSample(xmit,
ackMetaInfo->u.mAck.mAckDelay *
(1ULL << (fromCleartext ? kDefaultAckDelayExponent : mPeerAckDelayExponent)) /
1000ULL);
}
mSendState->Ack((*dataIter)->mPacketNumber, (*dataIter)->mPacketLen);
dataIter = mStreamState->mUnAckedPackets.erase(dataIter);
}
}
// fast retransmit.. may be a nop
if (haveAckForEnd > kReorderingThreshold) {
reportLossLessThan = std::max(reportLossLessThan, haveAckForEnd - kReorderingThreshold);
}
}
if (maybeDoEarlyRetransmit && !mStreamState->mUnAckedPackets.empty()) {
// this means we processed an ack for the highest unacked packet
// but some things still are not acked. That means early retransmit.
// todo this should really have a delay
reportLossLessThan = std::max(reportLossLessThan, mHighestTransmittedAckable);
}
if (reportLossLessThan) {
mStreamState->ReportLossLessThan(reportLossLessThan);
}
// obv unacked lists should be combined (data, other frames, acks)
for (auto iters = numRanges; iters > 0; --iters) {
uint64_t haveAckFor = ackStack[iters - 1].first;
uint64_t haveAckForEnd = haveAckFor + ackStack[iters - 1].second;
for (; haveAckFor < haveAckForEnd; haveAckFor++) {
bool foundHaveAckFor = false;
for (auto acklistIter = mStreamState->mAckList.cbegin(); acklistIter != mStreamState->mAckList.cend(); ) {
bool foundAckFor = false;
for (auto vectorIter = acklistIter->mTransmits.cbegin();
vectorIter != acklistIter->mTransmits.cend(); vectorIter++ ) {
if ((*vectorIter).first == haveAckFor) {
AckLog5("haveAckFor %lX found unacked ack of %lX (+%d) transmitted %d times\n",
haveAckFor, acklistIter->mPacketNumber, acklistIter->mExtra,
acklistIter->mTransmits.size());
foundAckFor = true;
break; // vector iteration
// need to keep looking at the rest of mStreamState->mAckList. Todo this is terribly wasteful.
}
} // vector iteration
if (!foundAckFor) {
acklistIter++;
} else {
acklistIter = mStreamState->mAckList.erase(acklistIter);
foundHaveAckFor = true;
}
} // macklist iteration
if (!foundHaveAckFor) {
AckLog9("haveAckFor %lX did not find matching unacked ack\n", haveAckFor)
}
} // haveackfor iteration
} //ranges iteration
// cong control limits are better now
mSendState->Flush();
}
uint32_t
MozQuic::HandleAckFrame(FrameHeaderData *result, bool fromCleartext,
const unsigned char *pkt, const unsigned char *endpkt,
uint32_t &_ptr)
{
if (fromCleartext && (mConnectionState == SERVER_STATE_LISTEN)) {
// acks are not allowed processing client_initial
RaiseError(MOZQUIC_ERR_GENERAL, (char *) "acks are not allowed in client initial\n");
return MOZQUIC_ERR_GENERAL;
}
// pkt + _ptr now points at ack blocks
uint32_t used;
ProcessAck(result, pkt + _ptr, endpkt, fromCleartext, used);
_ptr += used;
return MOZQUIC_OK;
}
}