]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/tcp_sack.c
xnu-3248.30.4.tar.gz
[apple/xnu.git] / bsd / netinet / tcp_sack.c
1 /*
2 * Copyright (c) 2004-2015 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 */
61
62 #define _IP_VHL
63
64
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
68 #include <sys/sysctl.h>
69 #include <sys/mbuf.h>
70 #include <sys/domain.h>
71 #include <sys/protosw.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
74
75 #include <kern/zalloc.h>
76
77 #include <net/route.h>
78
79 #include <netinet/in.h>
80 #include <netinet/in_systm.h>
81 #include <netinet/ip.h>
82 #include <netinet/in_pcb.h>
83 #include <netinet/ip_var.h>
84 #if INET6
85 #include <netinet6/in6_pcb.h>
86 #include <netinet/ip6.h>
87 #include <netinet6/ip6_var.h>
88 #endif
89 #include <netinet/tcp.h>
90 //#define TCPOUTFLAGS
91 #include <netinet/tcp_fsm.h>
92 #include <netinet/tcp_seq.h>
93 #include <netinet/tcp_timer.h>
94 #include <netinet/tcp_var.h>
95 #include <netinet/tcpip.h>
96 #if TCPDEBUG
97 #include <netinet/tcp_debug.h>
98 #endif
99 #include <sys/kdebug.h>
100
101 #if IPSEC
102 #include <netinet6/ipsec.h>
103 #endif /*IPSEC*/
104
105 #include <libkern/OSAtomic.h>
106
107 int tcp_do_sack = 1;
108 SYSCTL_INT(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_do_sack, 0,
109 "Enable/Disable TCP SACK support");
110 static int tcp_sack_maxholes = 128;
111 SYSCTL_INT(_net_inet_tcp, OID_AUTO, sack_maxholes, CTLFLAG_RW | CTLFLAG_LOCKED,
112 &tcp_sack_maxholes, 0,
113 "Maximum number of TCP SACK holes allowed per connection");
114
115 static int tcp_sack_globalmaxholes = 65536;
116 SYSCTL_INT(_net_inet_tcp, OID_AUTO, sack_globalmaxholes, CTLFLAG_RW | CTLFLAG_LOCKED,
117 &tcp_sack_globalmaxholes, 0,
118 "Global maximum number of TCP SACK holes");
119
120 static SInt32 tcp_sack_globalholes = 0;
121 SYSCTL_INT(_net_inet_tcp, OID_AUTO, sack_globalholes, CTLFLAG_RD | CTLFLAG_LOCKED,
122 &tcp_sack_globalholes, 0,
123 "Global number of TCP SACK holes currently allocated");
124
125 static int tcp_detect_reordering = 1;
126 static int tcp_dsack_ignore_hw_duplicates = 0;
127
128 #if (DEVELOPMENT || DEBUG)
129 SYSCTL_INT(_net_inet_tcp, OID_AUTO, detect_reordering,
130 CTLFLAG_RW | CTLFLAG_LOCKED,
131 &tcp_detect_reordering, 0, "");
132
133 SYSCTL_INT(_net_inet_tcp, OID_AUTO, ignore_hw_duplicates,
134 CTLFLAG_RW | CTLFLAG_LOCKED,
135 &tcp_dsack_ignore_hw_duplicates, 0, "");
136 #endif /* (DEVELOPMENT || DEBUG) */
137
138 extern struct zone *sack_hole_zone;
139
140 #define TCP_VALIDATE_SACK_SEQ_NUMBERS(_tp_, _sb_, _ack_) \
141 (SEQ_GT((_sb_)->end, (_sb_)->start) && \
142 SEQ_GT((_sb_)->start, (_tp_)->snd_una) && \
143 SEQ_GT((_sb_)->start, (_ack_)) && \
144 SEQ_LT((_sb_)->start, (_tp_)->snd_max) && \
145 SEQ_GT((_sb_)->end, (_tp_)->snd_una) && \
146 SEQ_LEQ((_sb_)->end, (_tp_)->snd_max))
147
148 /*
149 * This function is called upon receipt of new valid data (while not in header
150 * prediction mode), and it updates the ordered list of sacks.
151 */
152 void
153 tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end)
154 {
155 /*
156 * First reported block MUST be the most recent one. Subsequent
157 * blocks SHOULD be in the order in which they arrived at the
158 * receiver. These two conditions make the implementation fully
159 * compliant with RFC 2018.
160 */
161 struct sackblk head_blk, saved_blks[MAX_SACK_BLKS];
162 int num_head, num_saved, i;
163
164 /* SACK block for the received segment. */
165 head_blk.start = rcv_start;
166 head_blk.end = rcv_end;
167
168 /*
169 * Merge updated SACK blocks into head_blk, and
170 * save unchanged SACK blocks into saved_blks[].
171 * num_saved will have the number of the saved SACK blocks.
172 */
173 num_saved = 0;
174 for (i = 0; i < tp->rcv_numsacks; i++) {
175 tcp_seq start = tp->sackblks[i].start;
176 tcp_seq end = tp->sackblks[i].end;
177 if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) {
178 /*
179 * Discard this SACK block.
180 */
181 } else if (SEQ_LEQ(head_blk.start, end) &&
182 SEQ_GEQ(head_blk.end, start)) {
183 /*
184 * Merge this SACK block into head_blk.
185 * This SACK block itself will be discarded.
186 */
187 if (SEQ_GT(head_blk.start, start))
188 head_blk.start = start;
189 if (SEQ_LT(head_blk.end, end))
190 head_blk.end = end;
191 } else {
192 /*
193 * Save this SACK block.
194 */
195 saved_blks[num_saved].start = start;
196 saved_blks[num_saved].end = end;
197 num_saved++;
198 }
199 }
200
201 /*
202 * Update SACK list in tp->sackblks[].
203 */
204 num_head = 0;
205 if (SEQ_GT(head_blk.start, tp->rcv_nxt)) {
206 /*
207 * The received data segment is an out-of-order segment.
208 * Put head_blk at the top of SACK list.
209 */
210 tp->sackblks[0] = head_blk;
211 num_head = 1;
212 /*
213 * If the number of saved SACK blocks exceeds its limit,
214 * discard the last SACK block.
215 */
216 if (num_saved >= MAX_SACK_BLKS)
217 num_saved--;
218 }
219 if (num_saved > 0) {
220 /*
221 * Copy the saved SACK blocks back.
222 */
223 bcopy(saved_blks, &tp->sackblks[num_head],
224 sizeof(struct sackblk) * num_saved);
225 }
226
227 /* Save the number of SACK blocks. */
228 tp->rcv_numsacks = num_head + num_saved;
229
230 /* If we are requesting SACK recovery, reset the stretch-ack state
231 * so that connection will generate more acks after recovery and
232 * sender's cwnd will open.
233 */
234 if ((tp->t_flags & TF_STRETCHACK) != 0 && tp->rcv_numsacks > 0)
235 tcp_reset_stretch_ack(tp);
236
237 #if TRAFFIC_MGT
238 if (tp->acc_iaj > 0 && tp->rcv_numsacks > 0)
239 reset_acc_iaj(tp);
240 #endif /* TRAFFIC_MGT */
241 }
242
243 /*
244 * Delete all receiver-side SACK information.
245 */
246 void
247 tcp_clean_sackreport( struct tcpcb *tp)
248 {
249
250 tp->rcv_numsacks = 0;
251 bzero(&tp->sackblks[0], sizeof (struct sackblk) * MAX_SACK_BLKS);
252 }
253
254 /*
255 * Allocate struct sackhole.
256 */
257 static struct sackhole *
258 tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end)
259 {
260 struct sackhole *hole;
261
262 if (tp->snd_numholes >= tcp_sack_maxholes ||
263 tcp_sack_globalholes >= tcp_sack_globalmaxholes) {
264 tcpstat.tcps_sack_sboverflow++;
265 return NULL;
266 }
267
268 hole = (struct sackhole *)zalloc(sack_hole_zone);
269 if (hole == NULL)
270 return NULL;
271
272 hole->start = start;
273 hole->end = end;
274 hole->rxmit = start;
275
276 tp->snd_numholes++;
277 OSIncrementAtomic(&tcp_sack_globalholes);
278
279 return hole;
280 }
281
282 /*
283 * Free struct sackhole.
284 */
285 static void
286 tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole)
287 {
288 zfree(sack_hole_zone, hole);
289
290 tp->snd_numholes--;
291 OSDecrementAtomic(&tcp_sack_globalholes);
292 }
293
294 /*
295 * Insert new SACK hole into scoreboard.
296 */
297 static struct sackhole *
298 tcp_sackhole_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end,
299 struct sackhole *after)
300 {
301 struct sackhole *hole;
302
303 /* Allocate a new SACK hole. */
304 hole = tcp_sackhole_alloc(tp, start, end);
305 if (hole == NULL)
306 return NULL;
307 hole->rxmit_start = tcp_now;
308 /* Insert the new SACK hole into scoreboard */
309 if (after != NULL)
310 TAILQ_INSERT_AFTER(&tp->snd_holes, after, hole, scblink);
311 else
312 TAILQ_INSERT_TAIL(&tp->snd_holes, hole, scblink);
313
314 /* Update SACK hint. */
315 if (tp->sackhint.nexthole == NULL)
316 tp->sackhint.nexthole = hole;
317
318 return(hole);
319 }
320
321 /*
322 * Remove SACK hole from scoreboard.
323 */
324 static void
325 tcp_sackhole_remove(struct tcpcb *tp, struct sackhole *hole)
326 {
327 /* Update SACK hint. */
328 if (tp->sackhint.nexthole == hole)
329 tp->sackhint.nexthole = TAILQ_NEXT(hole, scblink);
330
331 /* Remove this SACK hole. */
332 TAILQ_REMOVE(&tp->snd_holes, hole, scblink);
333
334 /* Free this SACK hole. */
335 tcp_sackhole_free(tp, hole);
336 }
337 /*
338 * When a new ack with SACK is received, check if it indicates packet
339 * reordering. If there is packet reordering, the socket is marked and
340 * the late time offset by which the packet was reordered with
341 * respect to its closest neighboring packets is computed.
342 */
343 static void
344 tcp_sack_detect_reordering(struct tcpcb *tp, struct sackhole *s,
345 tcp_seq sacked_seq, tcp_seq snd_fack)
346 {
347 int32_t rext = 0, reordered = 0;
348
349 /*
350 * If the SACK hole is past snd_fack, this is from new SACK
351 * information, so we can ignore it.
352 */
353 if (SEQ_GT(s->end, snd_fack))
354 return;
355 /*
356 * If there has been a retransmit timeout, then the timestamp on
357 * the SACK segment will be newer. This might lead to a
358 * false-positive. Avoid re-ordering detection in this case.
359 */
360 if (tp->t_rxtshift > 0)
361 return;
362
363 /*
364 * Detect reordering from SACK information by checking
365 * if recently sacked data was never retransmitted from this hole.
366 */
367 if (SEQ_LT(s->rxmit, sacked_seq)) {
368 reordered = 1;
369 tcpstat.tcps_avoid_rxmt++;
370 }
371
372 if (reordered) {
373 if (tcp_detect_reordering == 1 &&
374 !(tp->t_flagsext & TF_PKTS_REORDERED)) {
375 tp->t_flagsext |= TF_PKTS_REORDERED;
376 tcpstat.tcps_detect_reordering++;
377 }
378
379 tcpstat.tcps_reordered_pkts++;
380 tp->t_reordered_pkts++;
381
382 VERIFY(SEQ_GEQ(snd_fack, s->rxmit));
383
384 if (s->rxmit_start > 0) {
385 rext = timer_diff(tcp_now, 0, s->rxmit_start, 0);
386 if (rext < 0)
387 return;
388
389 /*
390 * We take the maximum reorder window to schedule
391 * DELAYFR timer as that will take care of jitter
392 * on the network path.
393 *
394 * Computing average and standard deviation seems
395 * to cause unnecessary retransmissions when there
396 * is high jitter.
397 *
398 * We set a maximum of SRTT/2 and a minimum of
399 * 10 ms on the reorder window.
400 */
401 tp->t_reorderwin = max(tp->t_reorderwin, rext);
402 tp->t_reorderwin = min(tp->t_reorderwin,
403 (tp->t_srtt >> (TCP_RTT_SHIFT - 1)));
404 tp->t_reorderwin = max(tp->t_reorderwin, 10);
405 }
406 }
407 }
408
409 /*
410 * Process cumulative ACK and the TCP SACK option to update the scoreboard.
411 * tp->snd_holes is an ordered list of holes (oldest to newest, in terms of
412 * the sequence space).
413 */
414 void
415 tcp_sack_doack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th,
416 u_int32_t *newbytes_acked)
417 {
418 struct sackhole *cur, *temp;
419 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1], *sblkp;
420 int i, j, num_sack_blks;
421 tcp_seq old_snd_fack = 0, th_ack = th->th_ack;
422
423 num_sack_blks = 0;
424 /*
425 * If SND.UNA will be advanced by SEG.ACK, and if SACK holes exist,
426 * treat [SND.UNA, SEG.ACK) as if it is a SACK block.
427 */
428 if (SEQ_LT(tp->snd_una, th_ack) && !TAILQ_EMPTY(&tp->snd_holes)) {
429 sack_blocks[num_sack_blks].start = tp->snd_una;
430 sack_blocks[num_sack_blks++].end = th_ack;
431 }
432 /*
433 * Append received valid SACK blocks to sack_blocks[].
434 * Check that the SACK block range is valid.
435 */
436 for (i = 0; i < to->to_nsacks; i++) {
437 bcopy((to->to_sacks + i * TCPOLEN_SACK),
438 &sack, sizeof(sack));
439 sack.start = ntohl(sack.start);
440 sack.end = ntohl(sack.end);
441 if (TCP_VALIDATE_SACK_SEQ_NUMBERS(tp, &sack, th_ack))
442 sack_blocks[num_sack_blks++] = sack;
443 }
444
445 /*
446 * Return if SND.UNA is not advanced and no valid SACK block
447 * is received.
448 */
449 if (num_sack_blks == 0)
450 return;
451
452 VERIFY(num_sack_blks <= (TCP_MAX_SACK + 1));
453 /*
454 * Sort the SACK blocks so we can update the scoreboard
455 * with just one pass. The overhead of sorting upto 4+1 elements
456 * is less than making upto 4+1 passes over the scoreboard.
457 */
458 for (i = 0; i < num_sack_blks; i++) {
459 for (j = i + 1; j < num_sack_blks; j++) {
460 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
461 sack = sack_blocks[i];
462 sack_blocks[i] = sack_blocks[j];
463 sack_blocks[j] = sack;
464 }
465 }
466 }
467 if (TAILQ_EMPTY(&tp->snd_holes)) {
468 /*
469 * Empty scoreboard. Need to initialize snd_fack (it may be
470 * uninitialized or have a bogus value). Scoreboard holes
471 * (from the sack blocks received) are created later below (in
472 * the logic that adds holes to the tail of the scoreboard).
473 */
474 tp->snd_fack = SEQ_MAX(tp->snd_una, th_ack);
475 *newbytes_acked += (tp->snd_fack - tp->snd_una);
476 }
477
478 old_snd_fack = tp->snd_fack;
479 /*
480 * In the while-loop below, incoming SACK blocks (sack_blocks[])
481 * and SACK holes (snd_holes) are traversed from their tails with
482 * just one pass in order to reduce the number of compares especially
483 * when the bandwidth-delay product is large.
484 * Note: Typically, in the first RTT of SACK recovery, the highest
485 * three or four SACK blocks with the same ack number are received.
486 * In the second RTT, if retransmitted data segments are not lost,
487 * the highest three or four SACK blocks with ack number advancing
488 * are received.
489 */
490 sblkp = &sack_blocks[num_sack_blks - 1]; /* Last SACK block */
491 if (SEQ_LT(tp->snd_fack, sblkp->start)) {
492 /*
493 * The highest SACK block is beyond fack.
494 * Append new SACK hole at the tail.
495 * If the second or later highest SACK blocks are also
496 * beyond the current fack, they will be inserted by
497 * way of hole splitting in the while-loop below.
498 */
499 temp = tcp_sackhole_insert(tp, tp->snd_fack,sblkp->start,NULL);
500 if (temp != NULL) {
501 tp->snd_fack = sblkp->end;
502 *newbytes_acked += (sblkp->end - sblkp->start);
503
504 /* Go to the previous sack block. */
505 sblkp--;
506 } else {
507 /*
508 * We failed to add a new hole based on the current
509 * sack block. Skip over all the sack blocks that
510 * fall completely to the right of snd_fack and proceed
511 * to trim the scoreboard based on the remaining sack
512 * blocks. This also trims the scoreboard for th_ack
513 * (which is sack_blocks[0]).
514 */
515 while (sblkp >= sack_blocks &&
516 SEQ_LT(tp->snd_fack, sblkp->start))
517 sblkp--;
518 if (sblkp >= sack_blocks &&
519 SEQ_LT(tp->snd_fack, sblkp->end)) {
520 *newbytes_acked += (sblkp->end - tp->snd_fack);
521 tp->snd_fack = sblkp->end;
522 }
523 }
524 } else if (SEQ_LT(tp->snd_fack, sblkp->end)) {
525 /* fack is advanced. */
526 *newbytes_acked += (sblkp->end - tp->snd_fack);
527 tp->snd_fack = sblkp->end;
528 }
529 /* We must have at least one SACK hole in scoreboard */
530 cur = TAILQ_LAST(&tp->snd_holes, sackhole_head); /* Last SACK hole */
531 /*
532 * Since the incoming sack blocks are sorted, we can process them
533 * making one sweep of the scoreboard.
534 */
535 while (sblkp >= sack_blocks && cur != NULL) {
536 if (SEQ_GEQ(sblkp->start, cur->end)) {
537 /*
538 * SACKs data beyond the current hole.
539 * Go to the previous sack block.
540 */
541 sblkp--;
542 continue;
543 }
544 if (SEQ_LEQ(sblkp->end, cur->start)) {
545 /*
546 * SACKs data before the current hole.
547 * Go to the previous hole.
548 */
549 cur = TAILQ_PREV(cur, sackhole_head, scblink);
550 continue;
551 }
552 tp->sackhint.sack_bytes_rexmit -= (cur->rxmit - cur->start);
553 if (SEQ_LEQ(sblkp->start, cur->start)) {
554 /* Data acks at least the beginning of hole */
555 if (SEQ_GEQ(sblkp->end, cur->end)) {
556 /* Acks entire hole, so delete hole */
557 *newbytes_acked += (cur->end - cur->start);
558
559 tcp_sack_detect_reordering(tp, cur,
560 cur->end, old_snd_fack);
561 temp = cur;
562 cur = TAILQ_PREV(cur, sackhole_head, scblink);
563 tcp_sackhole_remove(tp, temp);
564 /*
565 * The sack block may ack all or part of the next
566 * hole too, so continue onto the next hole.
567 */
568 continue;
569 } else {
570 /* Move start of hole forward */
571 *newbytes_acked += (sblkp->end - cur->start);
572 tcp_sack_detect_reordering(tp, cur,
573 sblkp->end, old_snd_fack);
574 cur->start = sblkp->end;
575 cur->rxmit = SEQ_MAX(cur->rxmit, cur->start);
576 }
577 } else {
578 /* Data acks at least the end of hole */
579 if (SEQ_GEQ(sblkp->end, cur->end)) {
580 /* Move end of hole backward */
581 *newbytes_acked += (cur->end - sblkp->start);
582 tcp_sack_detect_reordering(tp, cur,
583 cur->end, old_snd_fack);
584 cur->end = sblkp->start;
585 cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
586 } else {
587 /*
588 * ACKs some data in the middle of a hole;
589 * need to split current hole
590 */
591 *newbytes_acked += (sblkp->end - sblkp->start);
592 tcp_sack_detect_reordering(tp, cur,
593 sblkp->end, old_snd_fack);
594 temp = tcp_sackhole_insert(tp, sblkp->end,
595 cur->end, cur);
596 if (temp != NULL) {
597 if (SEQ_GT(cur->rxmit, temp->rxmit)) {
598 temp->rxmit = cur->rxmit;
599 tp->sackhint.sack_bytes_rexmit
600 += (temp->rxmit
601 - temp->start);
602 }
603 cur->end = sblkp->start;
604 cur->rxmit = SEQ_MIN(cur->rxmit,
605 cur->end);
606 /*
607 * Reset the rxmit_start to that of
608 * the current hole as that will
609 * help to compute the reorder
610 * window correctly
611 */
612 temp->rxmit_start = cur->rxmit_start;
613 }
614 }
615 }
616 tp->sackhint.sack_bytes_rexmit += (cur->rxmit - cur->start);
617 /*
618 * Testing sblkp->start against cur->start tells us whether
619 * we're done with the sack block or the sack hole.
620 * Accordingly, we advance one or the other.
621 */
622 if (SEQ_LEQ(sblkp->start, cur->start))
623 cur = TAILQ_PREV(cur, sackhole_head, scblink);
624 else
625 sblkp--;
626 }
627 }
628
629 /*
630 * Free all SACK holes to clear the scoreboard.
631 */
632 void
633 tcp_free_sackholes(struct tcpcb *tp)
634 {
635 struct sackhole *q;
636
637 while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL)
638 tcp_sackhole_remove(tp, q);
639 tp->sackhint.sack_bytes_rexmit = 0;
640 tp->sackhint.nexthole = NULL;
641 tp->sack_newdata = 0;
642
643 }
644
645 /*
646 * Partial ack handling within a sack recovery episode.
647 * Keeping this very simple for now. When a partial ack
648 * is received, force snd_cwnd to a value that will allow
649 * the sender to transmit no more than 2 segments.
650 * If necessary, a better scheme can be adopted at a
651 * later point, but for now, the goal is to prevent the
652 * sender from bursting a large amount of data in the midst
653 * of sack recovery.
654 */
655 void
656 tcp_sack_partialack(tp, th)
657 struct tcpcb *tp;
658 struct tcphdr *th;
659 {
660 int num_segs = 1;
661
662 tp->t_timer[TCPT_REXMT] = 0;
663 tp->t_rtttime = 0;
664 /* send one or 2 segments based on how much new data was acked */
665 if (((BYTES_ACKED(th, tp)) / tp->t_maxseg) > 2)
666 num_segs = 2;
667 tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit +
668 (tp->snd_nxt - tp->sack_newdata) +
669 num_segs * tp->t_maxseg);
670 if (tp->snd_cwnd > tp->snd_ssthresh)
671 tp->snd_cwnd = tp->snd_ssthresh;
672 if (SEQ_LT(tp->snd_fack, tp->snd_recover) &&
673 tp->snd_fack == th->th_ack && TAILQ_EMPTY(&tp->snd_holes)) {
674 struct sackhole *temp;
675 /*
676 * we received a partial ack but there is no sack_hole
677 * that will cover the remaining seq space. In this case,
678 * create a hole from snd_fack to snd_recover so that
679 * the sack recovery will continue.
680 */
681 temp = tcp_sackhole_insert(tp, tp->snd_fack,
682 tp->snd_recover, NULL);
683 if (temp != NULL)
684 tp->snd_fack = tp->snd_recover;
685 }
686 (void) tcp_output(tp);
687 }
688
689 /*
690 * Debug version of tcp_sack_output() that walks the scoreboard. Used for
691 * now to sanity check the hint.
692 */
693 static struct sackhole *
694 tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt)
695 {
696 struct sackhole *p;
697
698 *sack_bytes_rexmt = 0;
699 TAILQ_FOREACH(p, &tp->snd_holes, scblink) {
700 if (SEQ_LT(p->rxmit, p->end)) {
701 if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */
702 continue;
703 }
704 *sack_bytes_rexmt += (p->rxmit - p->start);
705 break;
706 }
707 *sack_bytes_rexmt += (p->rxmit - p->start);
708 }
709 return (p);
710 }
711
712 /*
713 * Returns the next hole to retransmit and the number of retransmitted bytes
714 * from the scoreboard. We store both the next hole and the number of
715 * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK
716 * reception). This avoids scoreboard traversals completely.
717 *
718 * The loop here will traverse *at most* one link. Here's the argument.
719 * For the loop to traverse more than 1 link before finding the next hole to
720 * retransmit, we would need to have at least 1 node following the current hint
721 * with (rxmit == end). But, for all holes following the current hint,
722 * (start == rxmit), since we have not yet retransmitted from them. Therefore,
723 * in order to traverse more 1 link in the loop below, we need to have at least
724 * one node following the current hint with (start == rxmit == end).
725 * But that can't happen, (start == end) means that all the data in that hole
726 * has been sacked, in which case, the hole would have been removed from the
727 * scoreboard.
728 */
729 struct sackhole *
730 tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt)
731 {
732 struct sackhole *hole = NULL, *dbg_hole = NULL;
733 int dbg_bytes_rexmt;
734
735 dbg_hole = tcp_sack_output_debug(tp, &dbg_bytes_rexmt);
736 *sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit;
737 hole = tp->sackhint.nexthole;
738 if (hole == NULL || SEQ_LT(hole->rxmit, hole->end))
739 goto out;
740 while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) {
741 if (SEQ_LT(hole->rxmit, hole->end)) {
742 tp->sackhint.nexthole = hole;
743 break;
744 }
745 }
746 out:
747 if (dbg_hole != hole) {
748 printf("%s: Computed sack hole not the same as cached value\n", __func__);
749 hole = dbg_hole;
750 }
751 if (*sack_bytes_rexmt != dbg_bytes_rexmt) {
752 printf("%s: Computed sack_bytes_retransmitted (%d) not "
753 "the same as cached value (%d)\n",
754 __func__, dbg_bytes_rexmt, *sack_bytes_rexmt);
755 *sack_bytes_rexmt = dbg_bytes_rexmt;
756 }
757 return (hole);
758 }
759
760 /*
761 * After a timeout, the SACK list may be rebuilt. This SACK information
762 * should be used to avoid retransmitting SACKed data. This function
763 * traverses the SACK list to see if snd_nxt should be moved forward.
764 */
765 void
766 tcp_sack_adjust(struct tcpcb *tp)
767 {
768 struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes);
769
770 if (cur == NULL)
771 return; /* No holes */
772 if (SEQ_GEQ(tp->snd_nxt, tp->snd_fack))
773 return; /* We're already beyond any SACKed blocks */
774 /*
775 * Two cases for which we want to advance snd_nxt:
776 * i) snd_nxt lies between end of one hole and beginning of another
777 * ii) snd_nxt lies between end of last hole and snd_fack
778 */
779 while ((p = TAILQ_NEXT(cur, scblink)) != NULL) {
780 if (SEQ_LT(tp->snd_nxt, cur->end))
781 return;
782 if (SEQ_GEQ(tp->snd_nxt, p->start))
783 cur = p;
784 else {
785 tp->snd_nxt = p->start;
786 return;
787 }
788 }
789 if (SEQ_LT(tp->snd_nxt, cur->end))
790 return;
791 tp->snd_nxt = tp->snd_fack;
792 return;
793 }
794
795 /*
796 * This function returns TRUE if more than (tcprexmtthresh - 1) * SMSS
797 * bytes with sequence numbers greater than snd_una have been SACKed.
798 */
799 boolean_t
800 tcp_sack_byte_islost(struct tcpcb *tp)
801 {
802 u_int32_t unacked_bytes, sndhole_bytes = 0;
803 struct sackhole *sndhole;
804 if (!SACK_ENABLED(tp) || IN_FASTRECOVERY(tp) ||
805 TAILQ_EMPTY(&tp->snd_holes) ||
806 (tp->t_flagsext & TF_PKTS_REORDERED))
807 return (FALSE);
808
809 unacked_bytes = tp->snd_max - tp->snd_una;
810
811 TAILQ_FOREACH(sndhole, &tp->snd_holes, scblink) {
812 sndhole_bytes += (sndhole->end - sndhole->start);
813 }
814
815 VERIFY(unacked_bytes >= sndhole_bytes);
816 return ((unacked_bytes - sndhole_bytes) >
817 ((tcprexmtthresh - 1) * tp->t_maxseg));
818 }
819
820 /*
821 * Process any DSACK options that might be present on an input packet
822 */
823
824 boolean_t
825 tcp_sack_process_dsack(struct tcpcb *tp, struct tcpopt *to,
826 struct tcphdr *th)
827 {
828 struct sackblk first_sack, second_sack;
829 struct tcp_rxt_seg *rxseg;
830
831 bcopy(to->to_sacks, &first_sack, sizeof(first_sack));
832 first_sack.start = ntohl(first_sack.start);
833 first_sack.end = ntohl(first_sack.end);
834
835 if (to->to_nsacks > 1) {
836 bcopy((to->to_sacks + TCPOLEN_SACK), &second_sack,
837 sizeof(second_sack));
838 second_sack.start = ntohl(second_sack.start);
839 second_sack.end = ntohl(second_sack.end);
840 }
841
842 if (SEQ_LT(first_sack.start, th->th_ack) &&
843 SEQ_LEQ(first_sack.end, th->th_ack)) {
844 /*
845 * There is a dsack option reporting a duplicate segment
846 * also covered by cumulative acknowledgement.
847 *
848 * Validate the sequence numbers before looking at dsack
849 * option. The duplicate notification can come after
850 * snd_una moves forward. In order to set a window of valid
851 * sequence numbers to look for, we set a maximum send
852 * window within which the DSACK option will be processed.
853 */
854 if (!(TCP_DSACK_SEQ_IN_WINDOW(tp, first_sack.start, th->th_ack) &&
855 TCP_DSACK_SEQ_IN_WINDOW(tp, first_sack.end, th->th_ack))) {
856 to->to_nsacks--;
857 to->to_sacks += TCPOLEN_SACK;
858 tcpstat.tcps_dsack_recvd_old++;
859
860 /*
861 * returning true here so that the ack will not be
862 * treated as duplicate ack.
863 */
864 return (TRUE);
865 }
866 } else if (to->to_nsacks > 1 &&
867 SEQ_LEQ(second_sack.start, first_sack.start) &&
868 SEQ_GEQ(second_sack.end, first_sack.end)) {
869 /*
870 * there is a dsack option in the first block not
871 * covered by the cumulative acknowledgement but covered
872 * by the second sack block.
873 *
874 * verify the sequence numbes on the second sack block
875 * before processing the DSACK option. Returning false
876 * here will treat the ack as a duplicate ack.
877 */
878 if (!TCP_VALIDATE_SACK_SEQ_NUMBERS(tp, &second_sack,
879 th->th_ack)) {
880 to->to_nsacks--;
881 to->to_sacks += TCPOLEN_SACK;
882 tcpstat.tcps_dsack_recvd_old++;
883 return (TRUE);
884 }
885 } else {
886 /* no dsack options, proceed with processing the sack */
887 return (FALSE);
888 }
889
890 /* Update the tcpopt pointer to exclude dsack block */
891 to->to_nsacks--;
892 to->to_sacks += TCPOLEN_SACK;
893 tcpstat.tcps_dsack_recvd++;
894 tp->t_dsack_recvd++;
895
896 /* ignore DSACK option, if DSACK is disabled */
897 if (tp->t_flagsext & TF_DISABLE_DSACK)
898 return (TRUE);
899
900 /* If the DSACK is for TLP mark it as such */
901 if ((tp->t_flagsext & TF_SENT_TLPROBE) &&
902 first_sack.end == tp->t_tlphighrxt) {
903 if ((rxseg = tcp_rxtseg_find(tp, first_sack.start,
904 (first_sack.end - 1))) != NULL)
905 rxseg->rx_flags |= TCP_RXT_DSACK_FOR_TLP;
906 }
907 /* Update the sender's retransmit segment state */
908 if (((tp->t_rxtshift == 1 && first_sack.start == tp->snd_una) ||
909 ((tp->t_flagsext & TF_SENT_TLPROBE) &&
910 first_sack.end == tp->t_tlphighrxt)) &&
911 TAILQ_EMPTY(&tp->snd_holes) &&
912 SEQ_GT(th->th_ack, tp->snd_una)) {
913 /*
914 * If the dsack is for a retransmitted packet and one of
915 * the two cases is true, it indicates ack loss:
916 * - retransmit timeout and first_sack.start == snd_una
917 * - TLP probe and first_sack.end == tlphighrxt
918 *
919 * Ignore dsack and do not update state when there is
920 * ack loss
921 */
922 tcpstat.tcps_dsack_ackloss++;
923
924 return (TRUE);
925 } else if ((rxseg = tcp_rxtseg_find(tp, first_sack.start,
926 (first_sack.end - 1))) == NULL) {
927 /*
928 * Duplicate notification was not triggered by a
929 * retransmission. This might be due to network duplication,
930 * disable further DSACK processing.
931 */
932 if (!tcp_dsack_ignore_hw_duplicates) {
933 tp->t_flagsext |= TF_DISABLE_DSACK;
934 tcpstat.tcps_dsack_disable++;
935 }
936 } else {
937 /*
938 * If the segment was retransmitted only once, mark it as
939 * spurious. Otherwise ignore the duplicate notification.
940 */
941 if (rxseg->rx_count == 1)
942 rxseg->rx_flags |= TCP_RXT_SPURIOUS;
943 else
944 rxseg->rx_flags &= ~TCP_RXT_SPURIOUS;
945 }
946 return (TRUE);
947 }