-
Notifications
You must be signed in to change notification settings - Fork 528
/
path.c
259 lines (227 loc) · 6.9 KB
/
path.c
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
/*++
Copyright (c) Microsoft Corporation.
Licensed under the MIT License.
Abstract:
Per path functionality for the connection.
TODO:
Make Path ETW events.
--*/
#include "precomp.h"
#ifdef QUIC_CLOG
#include "path.c.clog.h"
#endif
_IRQL_requires_max_(PASSIVE_LEVEL)
void
QuicPathInitialize(
_In_ QUIC_CONNECTION* Connection,
_In_ QUIC_PATH* Path
)
{
QuicZeroMemory(Path, sizeof(QUIC_PATH));
Path->ID = Connection->NextPathId++; // TODO - Check for duplicates after wrap around?
Path->MinRtt = UINT32_MAX;
Path->Mtu = QUIC_DEFAULT_PATH_MTU;
Path->SmoothedRtt = MS_TO_US(Connection->Settings.InitialRttMs);
Path->RttVariance = Path->SmoothedRtt / 2;
QuicTraceLogConnInfo(
PathInitialized,
Connection,
"Path[%hhu] Initialized",
Path->ID);
}
_IRQL_requires_max_(PASSIVE_LEVEL)
void
QuicPathRemove(
_In_ QUIC_CONNECTION* Connection,
_In_ uint8_t Index
)
{
QUIC_DBG_ASSERT(Index < Connection->PathsCount);
const QUIC_PATH* Path = &Connection->Paths[Index];
QuicTraceLogConnInfo(
PathRemoved,
Connection,
"Path[%hhu] Removed",
Path->ID);
if (Index + 1 < Connection->PathsCount) {
QuicMoveMemory(
Connection->Paths + Index,
Connection->Paths + Index + 1,
(Connection->PathsCount - Index - 1) * sizeof(QUIC_PATH));
}
Connection->PathsCount--;
}
_IRQL_requires_max_(PASSIVE_LEVEL)
void
QuicPathSetAllowance(
_In_ QUIC_CONNECTION* Connection,
_In_ QUIC_PATH* Path,
_In_ uint32_t NewAllowance
)
{
BOOLEAN WasBlocked = Path->Allowance < QUIC_MIN_SEND_ALLOWANCE;
Path->Allowance = NewAllowance;
if (!Path->IsPeerValidated &&
(Path->Allowance < QUIC_MIN_SEND_ALLOWANCE) != WasBlocked) {
if (WasBlocked) {
QuicConnRemoveOutFlowBlockedReason(
Connection, QUIC_FLOW_BLOCKED_AMPLIFICATION_PROT);
if (Connection->Send.SendFlags != 0) {
//
// We were blocked by amplification protection (no allowance
// left) and we have stuff to send, so flush the send now.
//
QuicSendQueueFlush(&Connection->Send, REASON_AMP_PROTECTION);
}
//
// Now that we are no longer blocked by amplification protection
// we need to re-enable the loss detection timers. This call may
// even cause the loss timer to fire (be queued) immediately
// because packets were already lost, but we didn't know it.
//
QuicLossDetectionUpdateTimer(&Connection->LossDetection);
} else {
QuicConnAddOutFlowBlockedReason(
Connection, QUIC_FLOW_BLOCKED_AMPLIFICATION_PROT);
}
}
}
_IRQL_requires_max_(PASSIVE_LEVEL)
void
QuicPathSetValid(
_In_ QUIC_CONNECTION* Connection,
_In_ QUIC_PATH* Path,
_In_ QUIC_PATH_VALID_REASON Reason
)
{
if (Path->IsPeerValidated) {
return;
}
const char* ReasonStrings[] = {
"Initial Token",
"Handshake Packet",
"Path Response"
};
QuicTraceLogConnInfo(
PathValidated,
Connection,
"Path[%hhu] Validated (%s)",
Path->ID,
ReasonStrings[Reason]);
Path->IsPeerValidated = TRUE;
QuicPathSetAllowance(Connection, Path, UINT32_MAX);
if (Path->IsPeerValidated && Reason == QUIC_PATH_VALID_PATH_RESPONSE) {
//
// If the active path was just validated, then let's queue up a PMTUD
// packet.
//
// TODO - If minimum MTU was not validated, we might want to validate
// that first instead.
//
QuicSendSetSendFlag(&Connection->Send, QUIC_CONN_SEND_FLAG_PMTUD);
}
}
_IRQL_requires_max_(PASSIVE_LEVEL)
_Ret_maybenull_
_Success_(return != NULL)
QUIC_PATH*
QuicConnGetPathByID(
_In_ QUIC_CONNECTION* Connection,
_In_ uint8_t ID,
_Out_ uint8_t* Index
)
{
for (uint8_t i = 0; i < Connection->PathsCount; ++i) {
if (Connection->Paths[i].ID == ID) {
*Index = i;
return &Connection->Paths[i];
}
}
return NULL;
}
_IRQL_requires_max_(PASSIVE_LEVEL)
_Ret_maybenull_
QUIC_PATH*
QuicConnGetPathForDatagram(
_In_ QUIC_CONNECTION* Connection,
_In_ const QUIC_RECV_DATAGRAM* Datagram
)
{
for (uint8_t i = 0; i < Connection->PathsCount; ++i) {
if (!QuicAddrCompare(
&Datagram->Tuple->LocalAddress,
&Connection->Paths[i].LocalAddress) ||
!QuicAddrCompare(
&Datagram->Tuple->RemoteAddress,
&Connection->Paths[i].RemoteAddress)) {
if (!Connection->State.HandshakeConfirmed) {
//
// Ignore packets on any other paths until connected/confirmed.
//
return NULL;
}
continue;
}
return &Connection->Paths[i];
}
if (Connection->PathsCount == QUIC_MAX_PATH_COUNT) {
//
// Already tracking the maximum number of paths.
//
return NULL;
}
if (Connection->PathsCount > 1) {
//
// Make room for the new path (at index 1).
//
QuicMoveMemory(
&Connection->Paths[2],
&Connection->Paths[1],
(Connection->PathsCount - 1) * sizeof(QUIC_PATH));
}
QUIC_PATH* Path = &Connection->Paths[1];
QuicPathInitialize(Connection, Path);
Connection->PathsCount++;
Path->DestCid = Connection->Paths[0].DestCid;
Path->Binding = Connection->Paths[0].Binding;
Path->LocalAddress = Datagram->Tuple->LocalAddress;
Path->RemoteAddress = Datagram->Tuple->RemoteAddress;
return Path;
}
_IRQL_requires_max_(PASSIVE_LEVEL)
void
QuicPathSetActive(
_In_ QUIC_CONNECTION* Connection,
_In_ QUIC_PATH* Path
)
{
BOOLEAN UdpPortChangeOnly = FALSE;
if (Path == &Connection->Paths[0]) {
QUIC_DBG_ASSERT(!Path->IsActive);
Path->IsActive = TRUE;
} else {
UdpPortChangeOnly =
QuicAddrGetFamily(&Path->RemoteAddress) == QuicAddrGetFamily(&Connection->Paths[0].RemoteAddress) &&
QuicAddrCompareIp(&Path->RemoteAddress, &Connection->Paths[0].RemoteAddress);
QUIC_PATH PrevActivePath = Connection->Paths[0];
PrevActivePath.IsActive = FALSE;
Path->IsActive = TRUE;
if (UdpPortChangeOnly) {
//
// We assume port only changes don't change the PMTU.
//
Path->IsMinMtuValidated = PrevActivePath.IsMinMtuValidated;
}
Connection->Paths[0] = *Path;
*Path = PrevActivePath;
}
QuicTraceLogConnInfo(
PathActive,
Connection,
"Path[%hhu] Set active (rebind=%hhu)",
Connection->Paths[0].ID,
UdpPortChangeOnly);
if (!UdpPortChangeOnly) {
QuicCongestionControlReset(&Connection->CongestionControl);
}
}