Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | * |
| 15 | * TCP byte tracker that can generate delivery rate estimates. Based on |
| 16 | * draft-cheng-iccrg-delivery-rate-estimation-00 |
| 17 | */ |
| 18 | |
| 19 | #include <vnet/tcp/tcp.h> |
| 20 | |
| 21 | static tcp_bt_sample_t * |
| 22 | bt_get_sample (tcp_byte_tracker_t * bt, u32 bts_index) |
| 23 | { |
| 24 | if (pool_is_free_index (bt->samples, bts_index)) |
| 25 | return 0; |
| 26 | return pool_elt_at_index (bt->samples, bts_index); |
| 27 | } |
| 28 | |
| 29 | static tcp_bt_sample_t * |
| 30 | bt_next_sample (tcp_byte_tracker_t * bt, tcp_bt_sample_t * bts) |
| 31 | { |
| 32 | return bt_get_sample (bt, bts->next); |
| 33 | } |
| 34 | |
| 35 | static tcp_bt_sample_t * |
| 36 | bt_prev_sample (tcp_byte_tracker_t * bt, tcp_bt_sample_t * bts) |
| 37 | { |
| 38 | return bt_get_sample (bt, bts->prev); |
| 39 | } |
| 40 | |
| 41 | static u32 |
| 42 | bt_sample_index (tcp_byte_tracker_t * bt, tcp_bt_sample_t * bts) |
| 43 | { |
| 44 | if (!bts) |
| 45 | return TCP_BTS_INVALID_INDEX; |
| 46 | return bts - bt->samples; |
| 47 | } |
| 48 | |
| 49 | static inline int |
| 50 | bt_seq_lt (u32 a, u32 b) |
| 51 | { |
| 52 | return seq_lt (a, b); |
| 53 | } |
| 54 | |
| 55 | static tcp_bt_sample_t * |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 56 | bt_alloc_sample (tcp_byte_tracker_t * bt, u32 min_seq, u32 max_seq) |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 57 | { |
| 58 | tcp_bt_sample_t *bts; |
| 59 | |
| 60 | pool_get_zero (bt->samples, bts); |
| 61 | bts->next = bts->prev = TCP_BTS_INVALID_INDEX; |
| 62 | bts->min_seq = min_seq; |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 63 | bts->max_seq = max_seq; |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 64 | rb_tree_add_custom (&bt->sample_lookup, bts->min_seq, bts - bt->samples, |
| 65 | bt_seq_lt); |
| 66 | return bts; |
| 67 | } |
| 68 | |
| 69 | static void |
| 70 | bt_free_sample (tcp_byte_tracker_t * bt, tcp_bt_sample_t * bts) |
| 71 | { |
| 72 | if (bts->prev != TCP_BTS_INVALID_INDEX) |
| 73 | { |
| 74 | tcp_bt_sample_t *prev = bt_prev_sample (bt, bts); |
| 75 | prev->next = bts->next; |
| 76 | } |
| 77 | else |
| 78 | bt->head = bts->next; |
| 79 | |
| 80 | if (bts->next != TCP_BTS_INVALID_INDEX) |
| 81 | { |
| 82 | tcp_bt_sample_t *next = bt_next_sample (bt, bts); |
| 83 | next->prev = bts->prev; |
| 84 | } |
| 85 | else |
| 86 | bt->tail = bts->prev; |
| 87 | |
| 88 | rb_tree_del_custom (&bt->sample_lookup, bts->min_seq, bt_seq_lt); |
| 89 | if (CLIB_DEBUG) |
| 90 | memset (bts, 0xfc, sizeof (*bts)); |
| 91 | pool_put (bt->samples, bts); |
| 92 | } |
| 93 | |
| 94 | static tcp_bt_sample_t * |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 95 | bt_split_sample (tcp_byte_tracker_t * bt, tcp_bt_sample_t * bts, u32 seq) |
| 96 | { |
| 97 | tcp_bt_sample_t *ns, *next; |
| 98 | u32 bts_index; |
| 99 | |
| 100 | bts_index = bt_sample_index (bt, bts); |
| 101 | |
| 102 | ASSERT (seq_leq (bts->min_seq, seq) && seq_lt (seq, bts->max_seq)); |
| 103 | |
| 104 | ns = bt_alloc_sample (bt, seq, bts->max_seq); |
| 105 | bts = bt_get_sample (bt, bts_index); |
| 106 | |
| 107 | *ns = *bts; |
| 108 | ns->min_seq = seq; |
| 109 | bts->max_seq = seq; |
| 110 | |
| 111 | next = bt_next_sample (bt, bts); |
| 112 | if (next) |
| 113 | next->prev = bt_sample_index (bt, ns); |
| 114 | else |
| 115 | bt->tail = bt_sample_index (bt, ns); |
| 116 | |
| 117 | bts->next = bt_sample_index (bt, ns); |
| 118 | ns->prev = bt_sample_index (bt, bts); |
| 119 | |
| 120 | return ns; |
| 121 | } |
| 122 | |
| 123 | static tcp_bt_sample_t * |
| 124 | bt_merge_sample (tcp_byte_tracker_t * bt, tcp_bt_sample_t * prev, |
| 125 | tcp_bt_sample_t * cur) |
| 126 | { |
| 127 | ASSERT (prev->max_seq == cur->min_seq); |
| 128 | prev->max_seq = cur->max_seq; |
| 129 | if (bt_sample_index (bt, cur) == bt->tail) |
| 130 | bt->tail = bt_sample_index (bt, prev); |
| 131 | bt_free_sample (bt, cur); |
| 132 | return prev; |
| 133 | } |
| 134 | |
| 135 | static tcp_bt_sample_t * |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 136 | bt_lookup_seq (tcp_byte_tracker_t * bt, u32 seq) |
| 137 | { |
| 138 | rb_tree_t *rt = &bt->sample_lookup; |
| 139 | rb_node_t *cur, *prev; |
| 140 | tcp_bt_sample_t *bts; |
| 141 | |
| 142 | cur = rb_node (rt, rt->root); |
| 143 | if (rb_node_is_tnil (rt, cur)) |
| 144 | return 0; |
| 145 | |
| 146 | while (seq != cur->key) |
| 147 | { |
| 148 | prev = cur; |
| 149 | if (seq_lt (seq, cur->key)) |
| 150 | cur = rb_node_left (rt, cur); |
| 151 | else |
| 152 | cur = rb_node_right (rt, cur); |
| 153 | |
| 154 | if (rb_node_is_tnil (rt, cur)) |
| 155 | { |
| 156 | /* Hit tnil as a left child. Find predecessor */ |
| 157 | if (seq_lt (seq, prev->key)) |
| 158 | { |
| 159 | cur = rb_tree_predecessor (rt, prev); |
| 160 | if (rb_node_is_tnil (rt, cur)) |
| 161 | return 0; |
| 162 | bts = bt_get_sample (bt, cur->opaque); |
| 163 | } |
| 164 | /* Hit tnil as a right child */ |
| 165 | else |
| 166 | { |
| 167 | bts = bt_get_sample (bt, prev->opaque); |
| 168 | } |
| 169 | |
| 170 | if (seq_geq (seq, bts->min_seq)) |
| 171 | return bts; |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (!rb_node_is_tnil (rt, cur)) |
| 178 | return bt_get_sample (bt, cur->opaque); |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static void |
| 184 | bt_update_sample (tcp_byte_tracker_t * bt, tcp_bt_sample_t * bts, u32 seq) |
| 185 | { |
| 186 | rb_tree_del_custom (&bt->sample_lookup, bts->min_seq, bt_seq_lt); |
| 187 | bts->min_seq = seq; |
| 188 | rb_tree_add_custom (&bt->sample_lookup, bts->min_seq, |
| 189 | bt_sample_index (bt, bts), bt_seq_lt); |
| 190 | } |
| 191 | |
| 192 | static tcp_bt_sample_t * |
| 193 | bt_fix_overlapped (tcp_byte_tracker_t * bt, tcp_bt_sample_t * start, |
| 194 | u32 seq, u8 is_end) |
| 195 | { |
| 196 | tcp_bt_sample_t *cur, *next; |
| 197 | |
| 198 | cur = start; |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 199 | while (cur && seq_leq (cur->max_seq, seq)) |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 200 | { |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 201 | next = bt_next_sample (bt, cur); |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 202 | bt_free_sample (bt, cur); |
| 203 | cur = next; |
| 204 | } |
| 205 | |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 206 | if (cur && seq_lt (cur->min_seq, seq)) |
| 207 | bt_update_sample (bt, cur, seq); |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 208 | |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 209 | return cur; |
| 210 | } |
| 211 | |
| 212 | int |
| 213 | tcp_bt_is_sane (tcp_byte_tracker_t * bt) |
| 214 | { |
| 215 | tcp_bt_sample_t *bts, *tmp; |
| 216 | |
| 217 | if (pool_elts (bt->samples) != pool_elts (bt->sample_lookup.nodes) - 1) |
| 218 | return 0; |
| 219 | |
| 220 | if (bt->head == TCP_BTS_INVALID_INDEX) |
| 221 | { |
| 222 | if (bt->tail != TCP_BTS_INVALID_INDEX) |
| 223 | return 0; |
| 224 | if (pool_elts (bt->samples) != 0) |
| 225 | return 0; |
| 226 | return 1; |
| 227 | } |
| 228 | |
| 229 | bts = bt_get_sample (bt, bt->tail); |
| 230 | if (!bts) |
| 231 | return 0; |
| 232 | |
| 233 | bts = bt_get_sample (bt, bt->head); |
| 234 | if (!bts || bts->prev != TCP_BTS_INVALID_INDEX) |
| 235 | return 0; |
| 236 | |
| 237 | while (bts) |
| 238 | { |
| 239 | tmp = bt_lookup_seq (bt, bts->min_seq); |
| 240 | if (!tmp) |
| 241 | return 0; |
| 242 | if (tmp != bts) |
| 243 | return 0; |
| 244 | tmp = bt_next_sample (bt, bts); |
| 245 | if (tmp) |
| 246 | { |
| 247 | if (tmp->prev != bt_sample_index (bt, bts)) |
| 248 | { |
| 249 | clib_warning ("next %u thinks prev is %u should be %u", |
| 250 | bts->next, tmp->prev, bt_sample_index (bt, bts)); |
| 251 | return 0; |
| 252 | } |
| 253 | if (!seq_lt (bts->min_seq, tmp->min_seq)) |
| 254 | return 0; |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | if (bt->tail != bt_sample_index (bt, bts)) |
| 259 | return 0; |
| 260 | if (bts->next != TCP_BTS_INVALID_INDEX) |
| 261 | return 0; |
| 262 | } |
| 263 | bts = tmp; |
| 264 | } |
| 265 | return 1; |
| 266 | } |
| 267 | |
| 268 | static tcp_bt_sample_t * |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 269 | tcp_bt_alloc_tx_sample (tcp_connection_t * tc, u32 min_seq, u32 max_seq) |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 270 | { |
| 271 | tcp_bt_sample_t *bts; |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 272 | bts = bt_alloc_sample (tc->bt, min_seq, max_seq); |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 273 | bts->delivered = tc->delivered; |
| 274 | bts->delivered_time = tc->delivered_time; |
Florin Coras | 85fc130 | 2019-06-26 09:12:34 -0700 | [diff] [blame] | 275 | bts->tx_time = tcp_time_now_us (tc->c_thread_index); |
Florin Coras | 7436b43 | 2019-09-10 23:26:27 -0700 | [diff] [blame] | 276 | bts->first_tx_time = tc->first_tx_time; |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 277 | bts->flags |= tc->app_limited ? TCP_BTS_IS_APP_LIMITED : 0; |
| 278 | return bts; |
| 279 | } |
| 280 | |
| 281 | void |
| 282 | tcp_bt_check_app_limited (tcp_connection_t * tc) |
| 283 | { |
| 284 | u32 available_bytes, flight_size; |
| 285 | |
| 286 | available_bytes = transport_max_tx_dequeue (&tc->connection); |
| 287 | flight_size = tcp_flight_size (tc); |
| 288 | |
| 289 | /* Not enough bytes to fill the cwnd */ |
| 290 | if (available_bytes + flight_size + tc->snd_mss < tc->cwnd |
| 291 | /* Bytes considered lost have been retransmitted */ |
| 292 | && tc->sack_sb.lost_bytes <= tc->snd_rxt_bytes) |
| 293 | tc->app_limited = tc->delivered + flight_size ? : 1; |
| 294 | } |
| 295 | |
| 296 | void |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 297 | tcp_bt_track_tx (tcp_connection_t * tc, u32 len) |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 298 | { |
| 299 | tcp_byte_tracker_t *bt = tc->bt; |
| 300 | tcp_bt_sample_t *bts, *tail; |
| 301 | u32 bts_index; |
| 302 | |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 303 | tail = bt_get_sample (bt, bt->tail); |
| 304 | if (tail && tail->max_seq == tc->snd_nxt |
| 305 | && tail->tx_time == tcp_time_now_us (tc->c_thread_index)) |
| 306 | { |
| 307 | tail->max_seq += len; |
| 308 | return; |
| 309 | } |
| 310 | |
Florin Coras | 85fc130 | 2019-06-26 09:12:34 -0700 | [diff] [blame] | 311 | if (tc->snd_una == tc->snd_nxt) |
Florin Coras | 7436b43 | 2019-09-10 23:26:27 -0700 | [diff] [blame] | 312 | { |
| 313 | tc->delivered_time = tcp_time_now_us (tc->c_thread_index); |
| 314 | tc->first_tx_time = tc->delivered_time; |
| 315 | } |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 316 | |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 317 | bts = tcp_bt_alloc_tx_sample (tc, tc->snd_nxt, tc->snd_nxt + len); |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 318 | bts_index = bt_sample_index (bt, bts); |
| 319 | tail = bt_get_sample (bt, bt->tail); |
| 320 | if (tail) |
| 321 | { |
| 322 | tail->next = bts_index; |
| 323 | bts->prev = bt->tail; |
| 324 | bt->tail = bts_index; |
| 325 | } |
| 326 | else |
| 327 | { |
| 328 | bt->tail = bt->head = bts_index; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | void |
| 333 | tcp_bt_track_rxt (tcp_connection_t * tc, u32 start, u32 end) |
| 334 | { |
| 335 | tcp_byte_tracker_t *bt = tc->bt; |
| 336 | tcp_bt_sample_t *bts, *next, *cur, *prev, *nbts; |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 337 | u32 bts_index, cur_index, next_index, prev_index, max_seq; |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 338 | u8 is_end = end == tc->snd_nxt; |
| 339 | |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 340 | /* Contiguous blocks retransmitted at the same time */ |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 341 | bts = bt_get_sample (bt, bt->last_ooo); |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 342 | if (bts && bts->max_seq == start |
| 343 | && bts->tx_time == tcp_time_now_us (tc->c_thread_index)) |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 344 | { |
| 345 | bts->max_seq = end; |
| 346 | next = bt_next_sample (bt, bts); |
| 347 | if (next) |
| 348 | bt_fix_overlapped (bt, next, end, is_end); |
| 349 | |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | /* Find original tx sample */ |
| 354 | bts = bt_lookup_seq (bt, start); |
| 355 | |
| 356 | ASSERT (bts != 0 && seq_geq (start, bts->min_seq)); |
| 357 | |
| 358 | /* Head in the past */ |
| 359 | if (seq_lt (bts->min_seq, tc->snd_una)) |
| 360 | bt_update_sample (bt, bts, tc->snd_una); |
| 361 | |
| 362 | /* Head overlap */ |
| 363 | if (bts->min_seq == start) |
| 364 | { |
| 365 | prev_index = bts->prev; |
| 366 | next = bt_fix_overlapped (bt, bts, end, is_end); |
| 367 | next_index = bt_sample_index (bt, next); |
| 368 | |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 369 | cur = tcp_bt_alloc_tx_sample (tc, start, end); |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 370 | cur->flags |= TCP_BTS_IS_RXT; |
Florin Coras | 46ec6e0 | 2019-10-22 13:34:30 -0700 | [diff] [blame] | 371 | if (bts->flags & TCP_BTS_IS_RXT) |
| 372 | cur->flags |= TCP_BTS_IS_RXT_LOST; |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 373 | cur->next = next_index; |
| 374 | cur->prev = prev_index; |
| 375 | |
| 376 | cur_index = bt_sample_index (bt, cur); |
| 377 | |
| 378 | if (next_index != TCP_BTS_INVALID_INDEX) |
| 379 | { |
| 380 | next = bt_get_sample (bt, next_index); |
| 381 | next->prev = cur_index; |
| 382 | } |
| 383 | else |
| 384 | { |
| 385 | bt->tail = cur_index; |
| 386 | } |
| 387 | |
| 388 | if (prev_index != TCP_BTS_INVALID_INDEX) |
| 389 | { |
| 390 | prev = bt_get_sample (bt, prev_index); |
| 391 | prev->next = cur_index; |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | bt->head = cur_index; |
| 396 | } |
| 397 | |
| 398 | bt->last_ooo = cur_index; |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | bts_index = bt_sample_index (bt, bts); |
| 403 | next = bt_next_sample (bt, bts); |
| 404 | if (next) |
Florin Coras | 62a7fe2 | 2020-02-20 16:04:03 +0000 | [diff] [blame^] | 405 | bt_fix_overlapped (bt, next, end, is_end); |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 406 | |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 407 | max_seq = bts->max_seq; |
| 408 | ASSERT (seq_lt (start, max_seq)); |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 409 | |
| 410 | /* Have to split or tail overlap */ |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 411 | cur = tcp_bt_alloc_tx_sample (tc, start, end); |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 412 | cur->flags |= TCP_BTS_IS_RXT; |
Florin Coras | 46ec6e0 | 2019-10-22 13:34:30 -0700 | [diff] [blame] | 413 | if (bts->flags & TCP_BTS_IS_RXT) |
| 414 | cur->flags |= TCP_BTS_IS_RXT_LOST; |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 415 | cur->prev = bts_index; |
| 416 | cur_index = bt_sample_index (bt, cur); |
| 417 | |
| 418 | /* Split. Allocate another sample */ |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 419 | if (seq_lt (end, max_seq)) |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 420 | { |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 421 | nbts = tcp_bt_alloc_tx_sample (tc, end, bts->max_seq); |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 422 | cur = bt_get_sample (bt, cur_index); |
| 423 | bts = bt_get_sample (bt, bts_index); |
| 424 | |
| 425 | *nbts = *bts; |
| 426 | nbts->min_seq = end; |
| 427 | |
| 428 | if (nbts->next != TCP_BTS_INVALID_INDEX) |
| 429 | { |
| 430 | next = bt_get_sample (bt, nbts->next); |
| 431 | next->prev = bt_sample_index (bt, nbts); |
| 432 | } |
| 433 | else |
| 434 | bt->tail = bt_sample_index (bt, nbts); |
| 435 | |
| 436 | bts->next = nbts->prev = cur_index; |
| 437 | cur->next = bt_sample_index (bt, nbts); |
| 438 | |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 439 | bts->max_seq = start; |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 440 | bt->last_ooo = cur_index; |
| 441 | } |
| 442 | /* Tail completely overlapped */ |
| 443 | else |
| 444 | { |
| 445 | bts = bt_get_sample (bt, bts_index); |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 446 | bts->max_seq = start; |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 447 | |
| 448 | if (bts->next != TCP_BTS_INVALID_INDEX) |
| 449 | { |
| 450 | next = bt_get_sample (bt, bts->next); |
| 451 | next->prev = cur_index; |
| 452 | } |
| 453 | else |
| 454 | bt->tail = cur_index; |
| 455 | |
| 456 | cur->next = bts->next; |
| 457 | bts->next = cur_index; |
| 458 | |
| 459 | bt->last_ooo = cur_index; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | static void |
| 464 | tcp_bt_sample_to_rate_sample (tcp_connection_t * tc, tcp_bt_sample_t * bts, |
| 465 | tcp_rate_sample_t * rs) |
| 466 | { |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 467 | if (bts->flags & TCP_BTS_IS_SACKED) |
| 468 | return; |
| 469 | |
Florin Coras | 85fc130 | 2019-06-26 09:12:34 -0700 | [diff] [blame] | 470 | if (rs->prior_delivered && rs->prior_delivered >= bts->delivered) |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 471 | return; |
| 472 | |
Florin Coras | 85fc130 | 2019-06-26 09:12:34 -0700 | [diff] [blame] | 473 | rs->prior_delivered = bts->delivered; |
| 474 | rs->prior_time = bts->delivered_time; |
Florin Coras | 7436b43 | 2019-09-10 23:26:27 -0700 | [diff] [blame] | 475 | rs->interval_time = bts->tx_time - bts->first_tx_time; |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 476 | rs->rtt_time = tc->delivered_time - bts->tx_time; |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 477 | rs->flags = bts->flags; |
Florin Coras | 7436b43 | 2019-09-10 23:26:27 -0700 | [diff] [blame] | 478 | tc->first_tx_time = bts->tx_time; |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | static void |
| 482 | tcp_bt_walk_samples (tcp_connection_t * tc, tcp_rate_sample_t * rs) |
| 483 | { |
| 484 | tcp_byte_tracker_t *bt = tc->bt; |
| 485 | tcp_bt_sample_t *next, *cur; |
| 486 | |
| 487 | cur = bt_get_sample (bt, bt->head); |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 488 | while (cur && seq_leq (cur->max_seq, tc->snd_una)) |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 489 | { |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 490 | next = bt_next_sample (bt, cur); |
| 491 | tcp_bt_sample_to_rate_sample (tc, cur, rs); |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 492 | bt_free_sample (bt, cur); |
| 493 | cur = next; |
| 494 | } |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 495 | |
| 496 | if (cur && seq_lt (cur->min_seq, tc->snd_una)) |
| 497 | tcp_bt_sample_to_rate_sample (tc, cur, rs); |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static void |
| 501 | tcp_bt_walk_samples_ooo (tcp_connection_t * tc, tcp_rate_sample_t * rs) |
| 502 | { |
| 503 | sack_block_t *blks = tc->rcv_opts.sacks, *blk; |
| 504 | tcp_byte_tracker_t *bt = tc->bt; |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 505 | tcp_bt_sample_t *cur, *prev, *next; |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 506 | int i; |
| 507 | |
| 508 | for (i = 0; i < vec_len (blks); i++) |
| 509 | { |
| 510 | blk = &blks[i]; |
| 511 | |
| 512 | /* Ignore blocks that are already covered by snd_una */ |
| 513 | if (seq_lt (blk->end, tc->snd_una)) |
| 514 | continue; |
| 515 | |
| 516 | cur = bt_lookup_seq (bt, blk->start); |
| 517 | if (!cur) |
| 518 | continue; |
| 519 | |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 520 | ASSERT (seq_geq (blk->start, cur->min_seq) |
| 521 | && seq_lt (blk->start, cur->max_seq)); |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 522 | |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 523 | /* Current should be split. Second part will be consumed */ |
| 524 | if (PREDICT_FALSE (cur->min_seq != blk->start)) |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 525 | { |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 526 | cur = bt_split_sample (bt, cur, blk->start); |
| 527 | prev = bt_prev_sample (bt, cur); |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 528 | } |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 529 | else |
| 530 | prev = bt_prev_sample (bt, cur); |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 531 | |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 532 | while (cur && seq_leq (cur->max_seq, blk->end)) |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 533 | { |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 534 | if (!(cur->flags & TCP_BTS_IS_SACKED)) |
| 535 | { |
| 536 | tcp_bt_sample_to_rate_sample (tc, cur, rs); |
| 537 | cur->flags |= TCP_BTS_IS_SACKED; |
| 538 | if (prev && (prev->flags & TCP_BTS_IS_SACKED)) |
| 539 | { |
| 540 | cur = bt_merge_sample (bt, prev, cur); |
| 541 | next = bt_next_sample (bt, cur); |
| 542 | } |
| 543 | else |
| 544 | { |
| 545 | next = bt_next_sample (bt, cur); |
| 546 | if (next && (next->flags & TCP_BTS_IS_SACKED)) |
| 547 | { |
| 548 | cur = bt_merge_sample (bt, cur, next); |
| 549 | next = bt_next_sample (bt, cur); |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | else |
| 554 | next = bt_next_sample (bt, cur); |
| 555 | |
| 556 | prev = cur; |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 557 | cur = next; |
| 558 | } |
| 559 | |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 560 | if (cur && seq_lt (cur->min_seq, blk->end)) |
| 561 | { |
| 562 | tcp_bt_sample_to_rate_sample (tc, cur, rs); |
| 563 | prev = bt_prev_sample (bt, cur); |
| 564 | /* Extend previous to include the newly sacked bytes */ |
| 565 | if (prev && (prev->flags & TCP_BTS_IS_SACKED)) |
| 566 | { |
| 567 | prev->max_seq = blk->end; |
| 568 | bt_update_sample (bt, cur, blk->end); |
| 569 | } |
| 570 | /* Split sample into two. First part is consumed */ |
| 571 | else |
| 572 | { |
| 573 | next = bt_split_sample (bt, cur, blk->end); |
| 574 | cur = bt_prev_sample (bt, next); |
| 575 | cur->flags |= TCP_BTS_IS_SACKED; |
| 576 | } |
| 577 | } |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
| 581 | void |
| 582 | tcp_bt_sample_delivery_rate (tcp_connection_t * tc, tcp_rate_sample_t * rs) |
| 583 | { |
| 584 | u32 delivered; |
| 585 | |
| 586 | if (PREDICT_FALSE (tc->flags & TCP_CONN_FINSNT)) |
| 587 | return; |
| 588 | |
| 589 | delivered = tc->bytes_acked + tc->sack_sb.last_sacked_bytes; |
| 590 | if (!delivered || tc->bt->head == TCP_BTS_INVALID_INDEX) |
| 591 | return; |
| 592 | |
| 593 | /* Do not count bytes that were previously sacked again */ |
| 594 | tc->delivered += delivered - tc->sack_sb.last_bytes_delivered; |
| 595 | tc->delivered_time = tcp_time_now_us (tc->c_thread_index); |
| 596 | |
| 597 | if (tc->app_limited && tc->delivered > tc->app_limited) |
| 598 | tc->app_limited = 0; |
| 599 | |
| 600 | if (tc->bytes_acked) |
| 601 | tcp_bt_walk_samples (tc, rs); |
| 602 | |
| 603 | if (tc->sack_sb.last_sacked_bytes) |
| 604 | tcp_bt_walk_samples_ooo (tc, rs); |
Florin Coras | 85fc130 | 2019-06-26 09:12:34 -0700 | [diff] [blame] | 605 | |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 606 | rs->interval_time = clib_max ((tc->delivered_time - rs->prior_time), |
Florin Coras | 7436b43 | 2019-09-10 23:26:27 -0700 | [diff] [blame] | 607 | rs->interval_time); |
Florin Coras | 85fc130 | 2019-06-26 09:12:34 -0700 | [diff] [blame] | 608 | rs->delivered = tc->delivered - rs->prior_delivered; |
Florin Coras | 85fc130 | 2019-06-26 09:12:34 -0700 | [diff] [blame] | 609 | rs->acked_and_sacked = delivered; |
| 610 | rs->lost = tc->sack_sb.last_lost_bytes; |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | void |
| 614 | tcp_bt_flush_samples (tcp_connection_t * tc) |
| 615 | { |
| 616 | tcp_byte_tracker_t *bt = tc->bt; |
| 617 | tcp_bt_sample_t *bts; |
| 618 | u32 *samples = 0, *si; |
| 619 | |
| 620 | vec_validate (samples, pool_elts (bt->samples) - 1); |
Florin Coras | 92f190a | 2019-08-23 10:28:01 -0700 | [diff] [blame] | 621 | vec_reset_length (samples); |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 622 | |
| 623 | /* *INDENT-OFF* */ |
| 624 | pool_foreach (bts, bt->samples, ({ |
| 625 | vec_add1 (samples, bts - bt->samples); |
| 626 | })); |
| 627 | /* *INDENT-ON* */ |
| 628 | |
| 629 | vec_foreach (si, samples) |
| 630 | { |
| 631 | bts = bt_get_sample (bt, *si); |
| 632 | bt_free_sample (bt, bts); |
| 633 | } |
| 634 | |
| 635 | vec_free (samples); |
| 636 | } |
| 637 | |
| 638 | void |
| 639 | tcp_bt_cleanup (tcp_connection_t * tc) |
| 640 | { |
| 641 | tcp_byte_tracker_t *bt = tc->bt; |
| 642 | |
| 643 | rb_tree_free_nodes (&bt->sample_lookup); |
| 644 | pool_free (bt->samples); |
| 645 | clib_mem_free (bt); |
| 646 | tc->bt = 0; |
| 647 | } |
| 648 | |
| 649 | void |
| 650 | tcp_bt_init (tcp_connection_t * tc) |
| 651 | { |
| 652 | tcp_byte_tracker_t *bt; |
| 653 | |
| 654 | bt = clib_mem_alloc (sizeof (tcp_byte_tracker_t)); |
| 655 | clib_memset (bt, 0, sizeof (tcp_byte_tracker_t)); |
| 656 | |
| 657 | rb_tree_init (&bt->sample_lookup); |
| 658 | bt->head = bt->tail = TCP_BTS_INVALID_INDEX; |
| 659 | tc->bt = bt; |
| 660 | } |
| 661 | |
Florin Coras | d6ae4bf | 2019-10-12 18:10:20 -0700 | [diff] [blame] | 662 | u8 * |
| 663 | format_tcp_bt_sample (u8 * s, va_list * args) |
| 664 | { |
| 665 | tcp_connection_t *tc = va_arg (*args, tcp_connection_t *); |
| 666 | tcp_bt_sample_t *bts = va_arg (*args, tcp_bt_sample_t *); |
| 667 | f64 now = tcp_time_now_us (tc->c_thread_index); |
| 668 | s = format (s, "[%u, %u] d %u dt %.3f txt %.3f ftxt %.3f flags 0x%x", |
| 669 | bts->min_seq - tc->iss, bts->max_seq - tc->iss, bts->delivered, |
| 670 | now - bts->delivered_time, now - bts->tx_time, |
| 671 | now - bts->first_tx_time, bts->flags); |
| 672 | return s; |
| 673 | } |
| 674 | |
| 675 | u8 * |
| 676 | format_tcp_bt (u8 * s, va_list * args) |
| 677 | { |
| 678 | tcp_connection_t *tc = va_arg (*args, tcp_connection_t *); |
| 679 | tcp_byte_tracker_t *bt = tc->bt; |
| 680 | tcp_bt_sample_t *bts; |
| 681 | |
| 682 | bts = bt_get_sample (bt, bt->head); |
| 683 | while (bts) |
| 684 | { |
| 685 | s = format (s, "%U\n", format_tcp_bt_sample, tc, bts); |
| 686 | bts = bt_next_sample (bt, bts); |
| 687 | } |
| 688 | |
| 689 | return s; |
| 690 | } |
| 691 | |
Florin Coras | 5281473 | 2019-06-12 15:38:19 -0700 | [diff] [blame] | 692 | /* |
| 693 | * fd.io coding-style-patch-verification: ON |
| 694 | * |
| 695 | * Local Variables: |
| 696 | * eval: (c-set-style "gnu") |
| 697 | * End: |
| 698 | */ |