Deleted Added
full compact
tcp_sack.c (196019) tcp_sack.c (207369)
1/*-
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
3 * The Regents of the University of California.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * @(#)tcp_sack.c 8.12 (Berkeley) 5/24/95
31 */
32
33/*-
34 * @@(#)COPYRIGHT 1.1 (NRL) 17 January 1995
35 *
36 * NRL grants permission for redistribution and use in source and binary
37 * forms, with or without modification, of the software and documentation
38 * created at NRL provided that the following conditions are met:
39 *
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgements:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * This product includes software developed at the Information
50 * Technology Division, US Naval Research Laboratory.
51 * 4. Neither the name of the NRL nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
56 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
57 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
58 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR
59 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
60 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
61 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
62 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
63 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
64 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
65 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66 *
67 * The views and conclusions contained in the software and documentation
68 * are those of the authors and should not be interpreted as representing
69 * official policies, either expressed or implied, of the US Naval
70 * Research Laboratory (NRL).
71 */
72
73#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
3 * The Regents of the University of California.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * @(#)tcp_sack.c 8.12 (Berkeley) 5/24/95
31 */
32
33/*-
34 * @@(#)COPYRIGHT 1.1 (NRL) 17 January 1995
35 *
36 * NRL grants permission for redistribution and use in source and binary
37 * forms, with or without modification, of the software and documentation
38 * created at NRL provided that the following conditions are met:
39 *
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgements:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * This product includes software developed at the Information
50 * Technology Division, US Naval Research Laboratory.
51 * 4. Neither the name of the NRL nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
56 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
57 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
58 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR
59 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
60 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
61 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
62 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
63 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
64 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
65 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66 *
67 * The views and conclusions contained in the software and documentation
68 * are those of the authors and should not be interpreted as representing
69 * official policies, either expressed or implied, of the US Naval
70 * Research Laboratory (NRL).
71 */
72
73#include <sys/cdefs.h>
74__FBSDID("$FreeBSD: head/sys/netinet/tcp_sack.c 196019 2009-08-01 19:26:27Z rwatson $");
74__FBSDID("$FreeBSD: head/sys/netinet/tcp_sack.c 207369 2010-04-29 11:52:42Z bz $");
75
76#include "opt_inet.h"
77#include "opt_inet6.h"
78#include "opt_tcpdebug.h"
79
80#include <sys/param.h>
81#include <sys/systm.h>
82#include <sys/kernel.h>
83#include <sys/sysctl.h>
84#include <sys/malloc.h>
85#include <sys/mbuf.h>
86#include <sys/proc.h> /* for proc0 declaration */
87#include <sys/protosw.h>
88#include <sys/socket.h>
89#include <sys/socketvar.h>
90#include <sys/syslog.h>
91#include <sys/systm.h>
92
93#include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */
94
95#include <vm/uma.h>
96
97#include <net/if.h>
98#include <net/route.h>
99#include <net/vnet.h>
100
101#include <netinet/in.h>
102#include <netinet/in_systm.h>
103#include <netinet/ip.h>
104#include <netinet/in_var.h>
105#include <netinet/in_pcb.h>
106#include <netinet/ip_var.h>
107#include <netinet/ip6.h>
108#include <netinet/icmp6.h>
109#include <netinet6/nd6.h>
110#include <netinet6/ip6_var.h>
111#include <netinet6/in6_pcb.h>
112#include <netinet/tcp.h>
113#include <netinet/tcp_fsm.h>
114#include <netinet/tcp_seq.h>
115#include <netinet/tcp_timer.h>
116#include <netinet/tcp_var.h>
117#include <netinet6/tcp6_var.h>
118#include <netinet/tcpip.h>
119#ifdef TCPDEBUG
120#include <netinet/tcp_debug.h>
121#endif /* TCPDEBUG */
122
123#include <machine/in_cksum.h>
124
125VNET_DECLARE(struct uma_zone *, sack_hole_zone);
75
76#include "opt_inet.h"
77#include "opt_inet6.h"
78#include "opt_tcpdebug.h"
79
80#include <sys/param.h>
81#include <sys/systm.h>
82#include <sys/kernel.h>
83#include <sys/sysctl.h>
84#include <sys/malloc.h>
85#include <sys/mbuf.h>
86#include <sys/proc.h> /* for proc0 declaration */
87#include <sys/protosw.h>
88#include <sys/socket.h>
89#include <sys/socketvar.h>
90#include <sys/syslog.h>
91#include <sys/systm.h>
92
93#include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */
94
95#include <vm/uma.h>
96
97#include <net/if.h>
98#include <net/route.h>
99#include <net/vnet.h>
100
101#include <netinet/in.h>
102#include <netinet/in_systm.h>
103#include <netinet/ip.h>
104#include <netinet/in_var.h>
105#include <netinet/in_pcb.h>
106#include <netinet/ip_var.h>
107#include <netinet/ip6.h>
108#include <netinet/icmp6.h>
109#include <netinet6/nd6.h>
110#include <netinet6/ip6_var.h>
111#include <netinet6/in6_pcb.h>
112#include <netinet/tcp.h>
113#include <netinet/tcp_fsm.h>
114#include <netinet/tcp_seq.h>
115#include <netinet/tcp_timer.h>
116#include <netinet/tcp_var.h>
117#include <netinet6/tcp6_var.h>
118#include <netinet/tcpip.h>
119#ifdef TCPDEBUG
120#include <netinet/tcp_debug.h>
121#endif /* TCPDEBUG */
122
123#include <machine/in_cksum.h>
124
125VNET_DECLARE(struct uma_zone *, sack_hole_zone);
126VNET_DEFINE(int, tcp_do_sack);
127VNET_DEFINE(int, tcp_sack_maxholes);
128VNET_DEFINE(int, tcp_sack_globalmaxholes);
129VNET_DEFINE(int, tcp_sack_globalholes);
130
131#define V_sack_hole_zone VNET(sack_hole_zone)
126#define V_sack_hole_zone VNET(sack_hole_zone)
132#define V_tcp_do_sack VNET(tcp_do_sack)
133#define V_tcp_sack_maxholes VNET(tcp_sack_maxholes)
134#define V_tcp_sack_globalmaxholes VNET(tcp_sack_globalmaxholes)
135#define V_tcp_sack_globalholes VNET(tcp_sack_globalholes)
136
137SYSCTL_NODE(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW, 0, "TCP SACK");
127
128SYSCTL_NODE(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW, 0, "TCP SACK");
129VNET_DEFINE(int, tcp_do_sack) = 1;
130#define V_tcp_do_sack VNET(tcp_do_sack)
138SYSCTL_VNET_INT(_net_inet_tcp_sack, OID_AUTO, enable, CTLFLAG_RW,
139 &VNET_NAME(tcp_do_sack), 0, "Enable/Disable TCP SACK support");
140
131SYSCTL_VNET_INT(_net_inet_tcp_sack, OID_AUTO, enable, CTLFLAG_RW,
132 &VNET_NAME(tcp_do_sack), 0, "Enable/Disable TCP SACK support");
133
134VNET_DEFINE(int, tcp_sack_maxholes) = 128;
135#define V_tcp_sack_maxholes VNET(tcp_sack_maxholes)
141SYSCTL_VNET_INT(_net_inet_tcp_sack, OID_AUTO, maxholes, CTLFLAG_RW,
142 &VNET_NAME(tcp_sack_maxholes), 0,
143 "Maximum number of TCP SACK holes allowed per connection");
144
136SYSCTL_VNET_INT(_net_inet_tcp_sack, OID_AUTO, maxholes, CTLFLAG_RW,
137 &VNET_NAME(tcp_sack_maxholes), 0,
138 "Maximum number of TCP SACK holes allowed per connection");
139
140VNET_DEFINE(int, tcp_sack_globalmaxholes) = 65536;
141#define V_tcp_sack_globalmaxholes VNET(tcp_sack_globalmaxholes)
145SYSCTL_VNET_INT(_net_inet_tcp_sack, OID_AUTO, globalmaxholes, CTLFLAG_RW,
146 &VNET_NAME(tcp_sack_globalmaxholes), 0,
147 "Global maximum number of TCP SACK holes");
148
142SYSCTL_VNET_INT(_net_inet_tcp_sack, OID_AUTO, globalmaxholes, CTLFLAG_RW,
143 &VNET_NAME(tcp_sack_globalmaxholes), 0,
144 "Global maximum number of TCP SACK holes");
145
146VNET_DEFINE(int, tcp_sack_globalholes) = 0;
147#define V_tcp_sack_globalholes VNET(tcp_sack_globalholes)
149SYSCTL_VNET_INT(_net_inet_tcp_sack, OID_AUTO, globalholes, CTLFLAG_RD,
150 &VNET_NAME(tcp_sack_globalholes), 0,
151 "Global number of TCP SACK holes currently allocated");
152
153/*
154 * This function is called upon receipt of new valid data (while not in
155 * header prediction mode), and it updates the ordered list of sacks.
156 */
157void
158tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end)
159{
160 /*
161 * First reported block MUST be the most recent one. Subsequent
162 * blocks SHOULD be in the order in which they arrived at the
163 * receiver. These two conditions make the implementation fully
164 * compliant with RFC 2018.
165 */
166 struct sackblk head_blk, saved_blks[MAX_SACK_BLKS];
167 int num_head, num_saved, i;
168
169 INP_WLOCK_ASSERT(tp->t_inpcb);
170
171 /* Check arguments. */
172 KASSERT(SEQ_LT(rcv_start, rcv_end), ("rcv_start < rcv_end"));
173
174 /* SACK block for the received segment. */
175 head_blk.start = rcv_start;
176 head_blk.end = rcv_end;
177
178 /*
179 * Merge updated SACK blocks into head_blk, and save unchanged SACK
180 * blocks into saved_blks[]. num_saved will have the number of the
181 * saved SACK blocks.
182 */
183 num_saved = 0;
184 for (i = 0; i < tp->rcv_numsacks; i++) {
185 tcp_seq start = tp->sackblks[i].start;
186 tcp_seq end = tp->sackblks[i].end;
187 if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) {
188 /*
189 * Discard this SACK block.
190 */
191 } else if (SEQ_LEQ(head_blk.start, end) &&
192 SEQ_GEQ(head_blk.end, start)) {
193 /*
194 * Merge this SACK block into head_blk. This SACK
195 * block itself will be discarded.
196 */
197 if (SEQ_GT(head_blk.start, start))
198 head_blk.start = start;
199 if (SEQ_LT(head_blk.end, end))
200 head_blk.end = end;
201 } else {
202 /*
203 * Save this SACK block.
204 */
205 saved_blks[num_saved].start = start;
206 saved_blks[num_saved].end = end;
207 num_saved++;
208 }
209 }
210
211 /*
212 * Update SACK list in tp->sackblks[].
213 */
214 num_head = 0;
215 if (SEQ_GT(head_blk.start, tp->rcv_nxt)) {
216 /*
217 * The received data segment is an out-of-order segment. Put
218 * head_blk at the top of SACK list.
219 */
220 tp->sackblks[0] = head_blk;
221 num_head = 1;
222 /*
223 * If the number of saved SACK blocks exceeds its limit,
224 * discard the last SACK block.
225 */
226 if (num_saved >= MAX_SACK_BLKS)
227 num_saved--;
228 }
229 if (num_saved > 0) {
230 /*
231 * Copy the saved SACK blocks back.
232 */
233 bcopy(saved_blks, &tp->sackblks[num_head],
234 sizeof(struct sackblk) * num_saved);
235 }
236
237 /* Save the number of SACK blocks. */
238 tp->rcv_numsacks = num_head + num_saved;
239}
240
241/*
242 * Delete all receiver-side SACK information.
243 */
244void
245tcp_clean_sackreport(struct tcpcb *tp)
246{
247 int i;
248
249 INP_WLOCK_ASSERT(tp->t_inpcb);
250 tp->rcv_numsacks = 0;
251 for (i = 0; i < MAX_SACK_BLKS; i++)
252 tp->sackblks[i].start = tp->sackblks[i].end=0;
253}
254
255/*
256 * Allocate struct sackhole.
257 */
258static struct sackhole *
259tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end)
260{
261 struct sackhole *hole;
262
263 if (tp->snd_numholes >= V_tcp_sack_maxholes ||
264 V_tcp_sack_globalholes >= V_tcp_sack_globalmaxholes) {
265 TCPSTAT_INC(tcps_sack_sboverflow);
266 return NULL;
267 }
268
269 hole = (struct sackhole *)uma_zalloc(V_sack_hole_zone, M_NOWAIT);
270 if (hole == NULL)
271 return NULL;
272
273 hole->start = start;
274 hole->end = end;
275 hole->rxmit = start;
276
277 tp->snd_numholes++;
278 atomic_add_int(&V_tcp_sack_globalholes, 1);
279
280 return hole;
281}
282
283/*
284 * Free struct sackhole.
285 */
286static void
287tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole)
288{
289
290 uma_zfree(V_sack_hole_zone, hole);
291
292 tp->snd_numholes--;
293 atomic_subtract_int(&V_tcp_sack_globalholes, 1);
294
295 KASSERT(tp->snd_numholes >= 0, ("tp->snd_numholes >= 0"));
296 KASSERT(V_tcp_sack_globalholes >= 0, ("tcp_sack_globalholes >= 0"));
297}
298
299/*
300 * Insert new SACK hole into scoreboard.
301 */
302static struct sackhole *
303tcp_sackhole_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end,
304 struct sackhole *after)
305{
306 struct sackhole *hole;
307
308 /* Allocate a new SACK hole. */
309 hole = tcp_sackhole_alloc(tp, start, end);
310 if (hole == NULL)
311 return NULL;
312
313 /* Insert the new SACK hole into scoreboard. */
314 if (after != NULL)
315 TAILQ_INSERT_AFTER(&tp->snd_holes, after, hole, scblink);
316 else
317 TAILQ_INSERT_TAIL(&tp->snd_holes, hole, scblink);
318
319 /* Update SACK hint. */
320 if (tp->sackhint.nexthole == NULL)
321 tp->sackhint.nexthole = hole;
322
323 return hole;
324}
325
326/*
327 * Remove SACK hole from scoreboard.
328 */
329static void
330tcp_sackhole_remove(struct tcpcb *tp, struct sackhole *hole)
331{
332
333 /* Update SACK hint. */
334 if (tp->sackhint.nexthole == hole)
335 tp->sackhint.nexthole = TAILQ_NEXT(hole, scblink);
336
337 /* Remove this SACK hole. */
338 TAILQ_REMOVE(&tp->snd_holes, hole, scblink);
339
340 /* Free this SACK hole. */
341 tcp_sackhole_free(tp, hole);
342}
343
344/*
345 * Process cumulative ACK and the TCP SACK option to update the scoreboard.
346 * tp->snd_holes is an ordered list of holes (oldest to newest, in terms of
347 * the sequence space).
348 */
349void
350tcp_sack_doack(struct tcpcb *tp, struct tcpopt *to, tcp_seq th_ack)
351{
352 struct sackhole *cur, *temp;
353 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1], *sblkp;
354 int i, j, num_sack_blks;
355
356 INP_WLOCK_ASSERT(tp->t_inpcb);
357
358 num_sack_blks = 0;
359 /*
360 * If SND.UNA will be advanced by SEG.ACK, and if SACK holes exist,
361 * treat [SND.UNA, SEG.ACK) as if it is a SACK block.
362 */
363 if (SEQ_LT(tp->snd_una, th_ack) && !TAILQ_EMPTY(&tp->snd_holes)) {
364 sack_blocks[num_sack_blks].start = tp->snd_una;
365 sack_blocks[num_sack_blks++].end = th_ack;
366 }
367 /*
368 * Append received valid SACK blocks to sack_blocks[], but only if we
369 * received new blocks from the other side.
370 */
371 if (to->to_flags & TOF_SACK) {
372 for (i = 0; i < to->to_nsacks; i++) {
373 bcopy((to->to_sacks + i * TCPOLEN_SACK),
374 &sack, sizeof(sack));
375 sack.start = ntohl(sack.start);
376 sack.end = ntohl(sack.end);
377 if (SEQ_GT(sack.end, sack.start) &&
378 SEQ_GT(sack.start, tp->snd_una) &&
379 SEQ_GT(sack.start, th_ack) &&
380 SEQ_LT(sack.start, tp->snd_max) &&
381 SEQ_GT(sack.end, tp->snd_una) &&
382 SEQ_LEQ(sack.end, tp->snd_max))
383 sack_blocks[num_sack_blks++] = sack;
384 }
385 }
386 /*
387 * Return if SND.UNA is not advanced and no valid SACK block is
388 * received.
389 */
390 if (num_sack_blks == 0)
391 return;
392
393 /*
394 * Sort the SACK blocks so we can update the scoreboard with just one
395 * pass. The overhead of sorting upto 4+1 elements is less than
396 * making upto 4+1 passes over the scoreboard.
397 */
398 for (i = 0; i < num_sack_blks; i++) {
399 for (j = i + 1; j < num_sack_blks; j++) {
400 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
401 sack = sack_blocks[i];
402 sack_blocks[i] = sack_blocks[j];
403 sack_blocks[j] = sack;
404 }
405 }
406 }
407 if (TAILQ_EMPTY(&tp->snd_holes))
408 /*
409 * Empty scoreboard. Need to initialize snd_fack (it may be
410 * uninitialized or have a bogus value). Scoreboard holes
411 * (from the sack blocks received) are created later below
412 * (in the logic that adds holes to the tail of the
413 * scoreboard).
414 */
415 tp->snd_fack = SEQ_MAX(tp->snd_una, th_ack);
416 /*
417 * In the while-loop below, incoming SACK blocks (sack_blocks[]) and
418 * SACK holes (snd_holes) are traversed from their tails with just
419 * one pass in order to reduce the number of compares especially when
420 * the bandwidth-delay product is large.
421 *
422 * Note: Typically, in the first RTT of SACK recovery, the highest
423 * three or four SACK blocks with the same ack number are received.
424 * In the second RTT, if retransmitted data segments are not lost,
425 * the highest three or four SACK blocks with ack number advancing
426 * are received.
427 */
428 sblkp = &sack_blocks[num_sack_blks - 1]; /* Last SACK block */
429 if (SEQ_LT(tp->snd_fack, sblkp->start)) {
430 /*
431 * The highest SACK block is beyond fack. Append new SACK
432 * hole at the tail. If the second or later highest SACK
433 * blocks are also beyond the current fack, they will be
434 * inserted by way of hole splitting in the while-loop below.
435 */
436 temp = tcp_sackhole_insert(tp, tp->snd_fack,sblkp->start,NULL);
437 if (temp != NULL) {
438 tp->snd_fack = sblkp->end;
439 /* Go to the previous sack block. */
440 sblkp--;
441 } else {
442 /*
443 * We failed to add a new hole based on the current
444 * sack block. Skip over all the sack blocks that
445 * fall completely to the right of snd_fack and
446 * proceed to trim the scoreboard based on the
447 * remaining sack blocks. This also trims the
448 * scoreboard for th_ack (which is sack_blocks[0]).
449 */
450 while (sblkp >= sack_blocks &&
451 SEQ_LT(tp->snd_fack, sblkp->start))
452 sblkp--;
453 if (sblkp >= sack_blocks &&
454 SEQ_LT(tp->snd_fack, sblkp->end))
455 tp->snd_fack = sblkp->end;
456 }
457 } else if (SEQ_LT(tp->snd_fack, sblkp->end))
458 /* fack is advanced. */
459 tp->snd_fack = sblkp->end;
460 /* We must have at least one SACK hole in scoreboard. */
461 KASSERT(!TAILQ_EMPTY(&tp->snd_holes),
462 ("SACK scoreboard must not be empty"));
463 cur = TAILQ_LAST(&tp->snd_holes, sackhole_head); /* Last SACK hole. */
464 /*
465 * Since the incoming sack blocks are sorted, we can process them
466 * making one sweep of the scoreboard.
467 */
468 while (sblkp >= sack_blocks && cur != NULL) {
469 if (SEQ_GEQ(sblkp->start, cur->end)) {
470 /*
471 * SACKs data beyond the current hole. Go to the
472 * previous sack block.
473 */
474 sblkp--;
475 continue;
476 }
477 if (SEQ_LEQ(sblkp->end, cur->start)) {
478 /*
479 * SACKs data before the current hole. Go to the
480 * previous hole.
481 */
482 cur = TAILQ_PREV(cur, sackhole_head, scblink);
483 continue;
484 }
485 tp->sackhint.sack_bytes_rexmit -= (cur->rxmit - cur->start);
486 KASSERT(tp->sackhint.sack_bytes_rexmit >= 0,
487 ("sackhint bytes rtx >= 0"));
488 if (SEQ_LEQ(sblkp->start, cur->start)) {
489 /* Data acks at least the beginning of hole. */
490 if (SEQ_GEQ(sblkp->end, cur->end)) {
491 /* Acks entire hole, so delete hole. */
492 temp = cur;
493 cur = TAILQ_PREV(cur, sackhole_head, scblink);
494 tcp_sackhole_remove(tp, temp);
495 /*
496 * The sack block may ack all or part of the
497 * next hole too, so continue onto the next
498 * hole.
499 */
500 continue;
501 } else {
502 /* Move start of hole forward. */
503 cur->start = sblkp->end;
504 cur->rxmit = SEQ_MAX(cur->rxmit, cur->start);
505 }
506 } else {
507 /* Data acks at least the end of hole. */
508 if (SEQ_GEQ(sblkp->end, cur->end)) {
509 /* Move end of hole backward. */
510 cur->end = sblkp->start;
511 cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
512 } else {
513 /*
514 * ACKs some data in middle of a hole; need
515 * to split current hole
516 */
517 temp = tcp_sackhole_insert(tp, sblkp->end,
518 cur->end, cur);
519 if (temp != NULL) {
520 if (SEQ_GT(cur->rxmit, temp->rxmit)) {
521 temp->rxmit = cur->rxmit;
522 tp->sackhint.sack_bytes_rexmit
523 += (temp->rxmit
524 - temp->start);
525 }
526 cur->end = sblkp->start;
527 cur->rxmit = SEQ_MIN(cur->rxmit,
528 cur->end);
529 }
530 }
531 }
532 tp->sackhint.sack_bytes_rexmit += (cur->rxmit - cur->start);
533 /*
534 * Testing sblkp->start against cur->start tells us whether
535 * we're done with the sack block or the sack hole.
536 * Accordingly, we advance one or the other.
537 */
538 if (SEQ_LEQ(sblkp->start, cur->start))
539 cur = TAILQ_PREV(cur, sackhole_head, scblink);
540 else
541 sblkp--;
542 }
543}
544
545/*
546 * Free all SACK holes to clear the scoreboard.
547 */
548void
549tcp_free_sackholes(struct tcpcb *tp)
550{
551 struct sackhole *q;
552
553 INP_WLOCK_ASSERT(tp->t_inpcb);
554 while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL)
555 tcp_sackhole_remove(tp, q);
556 tp->sackhint.sack_bytes_rexmit = 0;
557
558 KASSERT(tp->snd_numholes == 0, ("tp->snd_numholes == 0"));
559 KASSERT(tp->sackhint.nexthole == NULL,
560 ("tp->sackhint.nexthole == NULL"));
561}
562
563/*
564 * Partial ack handling within a sack recovery episode. Keeping this very
565 * simple for now. When a partial ack is received, force snd_cwnd to a value
566 * that will allow the sender to transmit no more than 2 segments. If
567 * necessary, a better scheme can be adopted at a later point, but for now,
568 * the goal is to prevent the sender from bursting a large amount of data in
569 * the midst of sack recovery.
570 */
571void
572tcp_sack_partialack(struct tcpcb *tp, struct tcphdr *th)
573{
574 int num_segs = 1;
575
576 INP_WLOCK_ASSERT(tp->t_inpcb);
577 tcp_timer_activate(tp, TT_REXMT, 0);
578 tp->t_rtttime = 0;
579 /* Send one or 2 segments based on how much new data was acked. */
580 if (((th->th_ack - tp->snd_una) / tp->t_maxseg) > 2)
581 num_segs = 2;
582 tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit +
583 (tp->snd_nxt - tp->sack_newdata) + num_segs * tp->t_maxseg);
584 if (tp->snd_cwnd > tp->snd_ssthresh)
585 tp->snd_cwnd = tp->snd_ssthresh;
586 tp->t_flags |= TF_ACKNOW;
587 (void) tcp_output(tp);
588}
589
590#if 0
591/*
592 * Debug version of tcp_sack_output() that walks the scoreboard. Used for
593 * now to sanity check the hint.
594 */
595static struct sackhole *
596tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt)
597{
598 struct sackhole *p;
599
600 INP_WLOCK_ASSERT(tp->t_inpcb);
601 *sack_bytes_rexmt = 0;
602 TAILQ_FOREACH(p, &tp->snd_holes, scblink) {
603 if (SEQ_LT(p->rxmit, p->end)) {
604 if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */
605 continue;
606 }
607 *sack_bytes_rexmt += (p->rxmit - p->start);
608 break;
609 }
610 *sack_bytes_rexmt += (p->rxmit - p->start);
611 }
612 return (p);
613}
614#endif
615
616/*
617 * Returns the next hole to retransmit and the number of retransmitted bytes
618 * from the scoreboard. We store both the next hole and the number of
619 * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK
620 * reception). This avoids scoreboard traversals completely.
621 *
622 * The loop here will traverse *at most* one link. Here's the argument. For
623 * the loop to traverse more than 1 link before finding the next hole to
624 * retransmit, we would need to have at least 1 node following the current
625 * hint with (rxmit == end). But, for all holes following the current hint,
626 * (start == rxmit), since we have not yet retransmitted from them.
627 * Therefore, in order to traverse more 1 link in the loop below, we need to
628 * have at least one node following the current hint with (start == rxmit ==
629 * end). But that can't happen, (start == end) means that all the data in
630 * that hole has been sacked, in which case, the hole would have been removed
631 * from the scoreboard.
632 */
633struct sackhole *
634tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt)
635{
636 struct sackhole *hole = NULL;
637
638 INP_WLOCK_ASSERT(tp->t_inpcb);
639 *sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit;
640 hole = tp->sackhint.nexthole;
641 if (hole == NULL || SEQ_LT(hole->rxmit, hole->end))
642 goto out;
643 while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) {
644 if (SEQ_LT(hole->rxmit, hole->end)) {
645 tp->sackhint.nexthole = hole;
646 break;
647 }
648 }
649out:
650 return (hole);
651}
652
653/*
654 * After a timeout, the SACK list may be rebuilt. This SACK information
655 * should be used to avoid retransmitting SACKed data. This function
656 * traverses the SACK list to see if snd_nxt should be moved forward.
657 */
658void
659tcp_sack_adjust(struct tcpcb *tp)
660{
661 struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes);
662
663 INP_WLOCK_ASSERT(tp->t_inpcb);
664 if (cur == NULL)
665 return; /* No holes */
666 if (SEQ_GEQ(tp->snd_nxt, tp->snd_fack))
667 return; /* We're already beyond any SACKed blocks */
668 /*-
669 * Two cases for which we want to advance snd_nxt:
670 * i) snd_nxt lies between end of one hole and beginning of another
671 * ii) snd_nxt lies between end of last hole and snd_fack
672 */
673 while ((p = TAILQ_NEXT(cur, scblink)) != NULL) {
674 if (SEQ_LT(tp->snd_nxt, cur->end))
675 return;
676 if (SEQ_GEQ(tp->snd_nxt, p->start))
677 cur = p;
678 else {
679 tp->snd_nxt = p->start;
680 return;
681 }
682 }
683 if (SEQ_LT(tp->snd_nxt, cur->end))
684 return;
685 tp->snd_nxt = tp->snd_fack;
686}
148SYSCTL_VNET_INT(_net_inet_tcp_sack, OID_AUTO, globalholes, CTLFLAG_RD,
149 &VNET_NAME(tcp_sack_globalholes), 0,
150 "Global number of TCP SACK holes currently allocated");
151
152/*
153 * This function is called upon receipt of new valid data (while not in
154 * header prediction mode), and it updates the ordered list of sacks.
155 */
156void
157tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end)
158{
159 /*
160 * First reported block MUST be the most recent one. Subsequent
161 * blocks SHOULD be in the order in which they arrived at the
162 * receiver. These two conditions make the implementation fully
163 * compliant with RFC 2018.
164 */
165 struct sackblk head_blk, saved_blks[MAX_SACK_BLKS];
166 int num_head, num_saved, i;
167
168 INP_WLOCK_ASSERT(tp->t_inpcb);
169
170 /* Check arguments. */
171 KASSERT(SEQ_LT(rcv_start, rcv_end), ("rcv_start < rcv_end"));
172
173 /* SACK block for the received segment. */
174 head_blk.start = rcv_start;
175 head_blk.end = rcv_end;
176
177 /*
178 * Merge updated SACK blocks into head_blk, and save unchanged SACK
179 * blocks into saved_blks[]. num_saved will have the number of the
180 * saved SACK blocks.
181 */
182 num_saved = 0;
183 for (i = 0; i < tp->rcv_numsacks; i++) {
184 tcp_seq start = tp->sackblks[i].start;
185 tcp_seq end = tp->sackblks[i].end;
186 if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) {
187 /*
188 * Discard this SACK block.
189 */
190 } else if (SEQ_LEQ(head_blk.start, end) &&
191 SEQ_GEQ(head_blk.end, start)) {
192 /*
193 * Merge this SACK block into head_blk. This SACK
194 * block itself will be discarded.
195 */
196 if (SEQ_GT(head_blk.start, start))
197 head_blk.start = start;
198 if (SEQ_LT(head_blk.end, end))
199 head_blk.end = end;
200 } else {
201 /*
202 * Save this SACK block.
203 */
204 saved_blks[num_saved].start = start;
205 saved_blks[num_saved].end = end;
206 num_saved++;
207 }
208 }
209
210 /*
211 * Update SACK list in tp->sackblks[].
212 */
213 num_head = 0;
214 if (SEQ_GT(head_blk.start, tp->rcv_nxt)) {
215 /*
216 * The received data segment is an out-of-order segment. Put
217 * head_blk at the top of SACK list.
218 */
219 tp->sackblks[0] = head_blk;
220 num_head = 1;
221 /*
222 * If the number of saved SACK blocks exceeds its limit,
223 * discard the last SACK block.
224 */
225 if (num_saved >= MAX_SACK_BLKS)
226 num_saved--;
227 }
228 if (num_saved > 0) {
229 /*
230 * Copy the saved SACK blocks back.
231 */
232 bcopy(saved_blks, &tp->sackblks[num_head],
233 sizeof(struct sackblk) * num_saved);
234 }
235
236 /* Save the number of SACK blocks. */
237 tp->rcv_numsacks = num_head + num_saved;
238}
239
240/*
241 * Delete all receiver-side SACK information.
242 */
243void
244tcp_clean_sackreport(struct tcpcb *tp)
245{
246 int i;
247
248 INP_WLOCK_ASSERT(tp->t_inpcb);
249 tp->rcv_numsacks = 0;
250 for (i = 0; i < MAX_SACK_BLKS; i++)
251 tp->sackblks[i].start = tp->sackblks[i].end=0;
252}
253
254/*
255 * Allocate struct sackhole.
256 */
257static struct sackhole *
258tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end)
259{
260 struct sackhole *hole;
261
262 if (tp->snd_numholes >= V_tcp_sack_maxholes ||
263 V_tcp_sack_globalholes >= V_tcp_sack_globalmaxholes) {
264 TCPSTAT_INC(tcps_sack_sboverflow);
265 return NULL;
266 }
267
268 hole = (struct sackhole *)uma_zalloc(V_sack_hole_zone, M_NOWAIT);
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 atomic_add_int(&V_tcp_sack_globalholes, 1);
278
279 return hole;
280}
281
282/*
283 * Free struct sackhole.
284 */
285static void
286tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole)
287{
288
289 uma_zfree(V_sack_hole_zone, hole);
290
291 tp->snd_numholes--;
292 atomic_subtract_int(&V_tcp_sack_globalholes, 1);
293
294 KASSERT(tp->snd_numholes >= 0, ("tp->snd_numholes >= 0"));
295 KASSERT(V_tcp_sack_globalholes >= 0, ("tcp_sack_globalholes >= 0"));
296}
297
298/*
299 * Insert new SACK hole into scoreboard.
300 */
301static struct sackhole *
302tcp_sackhole_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end,
303 struct sackhole *after)
304{
305 struct sackhole *hole;
306
307 /* Allocate a new SACK hole. */
308 hole = tcp_sackhole_alloc(tp, start, end);
309 if (hole == NULL)
310 return NULL;
311
312 /* Insert the new SACK hole into scoreboard. */
313 if (after != NULL)
314 TAILQ_INSERT_AFTER(&tp->snd_holes, after, hole, scblink);
315 else
316 TAILQ_INSERT_TAIL(&tp->snd_holes, hole, scblink);
317
318 /* Update SACK hint. */
319 if (tp->sackhint.nexthole == NULL)
320 tp->sackhint.nexthole = hole;
321
322 return hole;
323}
324
325/*
326 * Remove SACK hole from scoreboard.
327 */
328static void
329tcp_sackhole_remove(struct tcpcb *tp, struct sackhole *hole)
330{
331
332 /* Update SACK hint. */
333 if (tp->sackhint.nexthole == hole)
334 tp->sackhint.nexthole = TAILQ_NEXT(hole, scblink);
335
336 /* Remove this SACK hole. */
337 TAILQ_REMOVE(&tp->snd_holes, hole, scblink);
338
339 /* Free this SACK hole. */
340 tcp_sackhole_free(tp, hole);
341}
342
343/*
344 * Process cumulative ACK and the TCP SACK option to update the scoreboard.
345 * tp->snd_holes is an ordered list of holes (oldest to newest, in terms of
346 * the sequence space).
347 */
348void
349tcp_sack_doack(struct tcpcb *tp, struct tcpopt *to, tcp_seq th_ack)
350{
351 struct sackhole *cur, *temp;
352 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1], *sblkp;
353 int i, j, num_sack_blks;
354
355 INP_WLOCK_ASSERT(tp->t_inpcb);
356
357 num_sack_blks = 0;
358 /*
359 * If SND.UNA will be advanced by SEG.ACK, and if SACK holes exist,
360 * treat [SND.UNA, SEG.ACK) as if it is a SACK block.
361 */
362 if (SEQ_LT(tp->snd_una, th_ack) && !TAILQ_EMPTY(&tp->snd_holes)) {
363 sack_blocks[num_sack_blks].start = tp->snd_una;
364 sack_blocks[num_sack_blks++].end = th_ack;
365 }
366 /*
367 * Append received valid SACK blocks to sack_blocks[], but only if we
368 * received new blocks from the other side.
369 */
370 if (to->to_flags & TOF_SACK) {
371 for (i = 0; i < to->to_nsacks; i++) {
372 bcopy((to->to_sacks + i * TCPOLEN_SACK),
373 &sack, sizeof(sack));
374 sack.start = ntohl(sack.start);
375 sack.end = ntohl(sack.end);
376 if (SEQ_GT(sack.end, sack.start) &&
377 SEQ_GT(sack.start, tp->snd_una) &&
378 SEQ_GT(sack.start, th_ack) &&
379 SEQ_LT(sack.start, tp->snd_max) &&
380 SEQ_GT(sack.end, tp->snd_una) &&
381 SEQ_LEQ(sack.end, tp->snd_max))
382 sack_blocks[num_sack_blks++] = sack;
383 }
384 }
385 /*
386 * Return if SND.UNA is not advanced and no valid SACK block is
387 * received.
388 */
389 if (num_sack_blks == 0)
390 return;
391
392 /*
393 * Sort the SACK blocks so we can update the scoreboard with just one
394 * pass. The overhead of sorting upto 4+1 elements is less than
395 * making upto 4+1 passes over the scoreboard.
396 */
397 for (i = 0; i < num_sack_blks; i++) {
398 for (j = i + 1; j < num_sack_blks; j++) {
399 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
400 sack = sack_blocks[i];
401 sack_blocks[i] = sack_blocks[j];
402 sack_blocks[j] = sack;
403 }
404 }
405 }
406 if (TAILQ_EMPTY(&tp->snd_holes))
407 /*
408 * Empty scoreboard. Need to initialize snd_fack (it may be
409 * uninitialized or have a bogus value). Scoreboard holes
410 * (from the sack blocks received) are created later below
411 * (in the logic that adds holes to the tail of the
412 * scoreboard).
413 */
414 tp->snd_fack = SEQ_MAX(tp->snd_una, th_ack);
415 /*
416 * In the while-loop below, incoming SACK blocks (sack_blocks[]) and
417 * SACK holes (snd_holes) are traversed from their tails with just
418 * one pass in order to reduce the number of compares especially when
419 * the bandwidth-delay product is large.
420 *
421 * Note: Typically, in the first RTT of SACK recovery, the highest
422 * three or four SACK blocks with the same ack number are received.
423 * In the second RTT, if retransmitted data segments are not lost,
424 * the highest three or four SACK blocks with ack number advancing
425 * are received.
426 */
427 sblkp = &sack_blocks[num_sack_blks - 1]; /* Last SACK block */
428 if (SEQ_LT(tp->snd_fack, sblkp->start)) {
429 /*
430 * The highest SACK block is beyond fack. Append new SACK
431 * hole at the tail. If the second or later highest SACK
432 * blocks are also beyond the current fack, they will be
433 * inserted by way of hole splitting in the while-loop below.
434 */
435 temp = tcp_sackhole_insert(tp, tp->snd_fack,sblkp->start,NULL);
436 if (temp != NULL) {
437 tp->snd_fack = sblkp->end;
438 /* Go to the previous sack block. */
439 sblkp--;
440 } else {
441 /*
442 * We failed to add a new hole based on the current
443 * sack block. Skip over all the sack blocks that
444 * fall completely to the right of snd_fack and
445 * proceed to trim the scoreboard based on the
446 * remaining sack blocks. This also trims the
447 * scoreboard for th_ack (which is sack_blocks[0]).
448 */
449 while (sblkp >= sack_blocks &&
450 SEQ_LT(tp->snd_fack, sblkp->start))
451 sblkp--;
452 if (sblkp >= sack_blocks &&
453 SEQ_LT(tp->snd_fack, sblkp->end))
454 tp->snd_fack = sblkp->end;
455 }
456 } else if (SEQ_LT(tp->snd_fack, sblkp->end))
457 /* fack is advanced. */
458 tp->snd_fack = sblkp->end;
459 /* We must have at least one SACK hole in scoreboard. */
460 KASSERT(!TAILQ_EMPTY(&tp->snd_holes),
461 ("SACK scoreboard must not be empty"));
462 cur = TAILQ_LAST(&tp->snd_holes, sackhole_head); /* Last SACK hole. */
463 /*
464 * Since the incoming sack blocks are sorted, we can process them
465 * making one sweep of the scoreboard.
466 */
467 while (sblkp >= sack_blocks && cur != NULL) {
468 if (SEQ_GEQ(sblkp->start, cur->end)) {
469 /*
470 * SACKs data beyond the current hole. Go to the
471 * previous sack block.
472 */
473 sblkp--;
474 continue;
475 }
476 if (SEQ_LEQ(sblkp->end, cur->start)) {
477 /*
478 * SACKs data before the current hole. Go to the
479 * previous hole.
480 */
481 cur = TAILQ_PREV(cur, sackhole_head, scblink);
482 continue;
483 }
484 tp->sackhint.sack_bytes_rexmit -= (cur->rxmit - cur->start);
485 KASSERT(tp->sackhint.sack_bytes_rexmit >= 0,
486 ("sackhint bytes rtx >= 0"));
487 if (SEQ_LEQ(sblkp->start, cur->start)) {
488 /* Data acks at least the beginning of hole. */
489 if (SEQ_GEQ(sblkp->end, cur->end)) {
490 /* Acks entire hole, so delete hole. */
491 temp = cur;
492 cur = TAILQ_PREV(cur, sackhole_head, scblink);
493 tcp_sackhole_remove(tp, temp);
494 /*
495 * The sack block may ack all or part of the
496 * next hole too, so continue onto the next
497 * hole.
498 */
499 continue;
500 } else {
501 /* Move start of hole forward. */
502 cur->start = sblkp->end;
503 cur->rxmit = SEQ_MAX(cur->rxmit, cur->start);
504 }
505 } else {
506 /* Data acks at least the end of hole. */
507 if (SEQ_GEQ(sblkp->end, cur->end)) {
508 /* Move end of hole backward. */
509 cur->end = sblkp->start;
510 cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
511 } else {
512 /*
513 * ACKs some data in middle of a hole; need
514 * to split current hole
515 */
516 temp = tcp_sackhole_insert(tp, sblkp->end,
517 cur->end, cur);
518 if (temp != NULL) {
519 if (SEQ_GT(cur->rxmit, temp->rxmit)) {
520 temp->rxmit = cur->rxmit;
521 tp->sackhint.sack_bytes_rexmit
522 += (temp->rxmit
523 - temp->start);
524 }
525 cur->end = sblkp->start;
526 cur->rxmit = SEQ_MIN(cur->rxmit,
527 cur->end);
528 }
529 }
530 }
531 tp->sackhint.sack_bytes_rexmit += (cur->rxmit - cur->start);
532 /*
533 * Testing sblkp->start against cur->start tells us whether
534 * we're done with the sack block or the sack hole.
535 * Accordingly, we advance one or the other.
536 */
537 if (SEQ_LEQ(sblkp->start, cur->start))
538 cur = TAILQ_PREV(cur, sackhole_head, scblink);
539 else
540 sblkp--;
541 }
542}
543
544/*
545 * Free all SACK holes to clear the scoreboard.
546 */
547void
548tcp_free_sackholes(struct tcpcb *tp)
549{
550 struct sackhole *q;
551
552 INP_WLOCK_ASSERT(tp->t_inpcb);
553 while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL)
554 tcp_sackhole_remove(tp, q);
555 tp->sackhint.sack_bytes_rexmit = 0;
556
557 KASSERT(tp->snd_numholes == 0, ("tp->snd_numholes == 0"));
558 KASSERT(tp->sackhint.nexthole == NULL,
559 ("tp->sackhint.nexthole == NULL"));
560}
561
562/*
563 * Partial ack handling within a sack recovery episode. Keeping this very
564 * simple for now. When a partial ack is received, force snd_cwnd to a value
565 * that will allow the sender to transmit no more than 2 segments. If
566 * necessary, a better scheme can be adopted at a later point, but for now,
567 * the goal is to prevent the sender from bursting a large amount of data in
568 * the midst of sack recovery.
569 */
570void
571tcp_sack_partialack(struct tcpcb *tp, struct tcphdr *th)
572{
573 int num_segs = 1;
574
575 INP_WLOCK_ASSERT(tp->t_inpcb);
576 tcp_timer_activate(tp, TT_REXMT, 0);
577 tp->t_rtttime = 0;
578 /* Send one or 2 segments based on how much new data was acked. */
579 if (((th->th_ack - tp->snd_una) / tp->t_maxseg) > 2)
580 num_segs = 2;
581 tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit +
582 (tp->snd_nxt - tp->sack_newdata) + num_segs * tp->t_maxseg);
583 if (tp->snd_cwnd > tp->snd_ssthresh)
584 tp->snd_cwnd = tp->snd_ssthresh;
585 tp->t_flags |= TF_ACKNOW;
586 (void) tcp_output(tp);
587}
588
589#if 0
590/*
591 * Debug version of tcp_sack_output() that walks the scoreboard. Used for
592 * now to sanity check the hint.
593 */
594static struct sackhole *
595tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt)
596{
597 struct sackhole *p;
598
599 INP_WLOCK_ASSERT(tp->t_inpcb);
600 *sack_bytes_rexmt = 0;
601 TAILQ_FOREACH(p, &tp->snd_holes, scblink) {
602 if (SEQ_LT(p->rxmit, p->end)) {
603 if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */
604 continue;
605 }
606 *sack_bytes_rexmt += (p->rxmit - p->start);
607 break;
608 }
609 *sack_bytes_rexmt += (p->rxmit - p->start);
610 }
611 return (p);
612}
613#endif
614
615/*
616 * Returns the next hole to retransmit and the number of retransmitted bytes
617 * from the scoreboard. We store both the next hole and the number of
618 * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK
619 * reception). This avoids scoreboard traversals completely.
620 *
621 * The loop here will traverse *at most* one link. Here's the argument. For
622 * the loop to traverse more than 1 link before finding the next hole to
623 * retransmit, we would need to have at least 1 node following the current
624 * hint with (rxmit == end). But, for all holes following the current hint,
625 * (start == rxmit), since we have not yet retransmitted from them.
626 * Therefore, in order to traverse more 1 link in the loop below, we need to
627 * have at least one node following the current hint with (start == rxmit ==
628 * end). But that can't happen, (start == end) means that all the data in
629 * that hole has been sacked, in which case, the hole would have been removed
630 * from the scoreboard.
631 */
632struct sackhole *
633tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt)
634{
635 struct sackhole *hole = NULL;
636
637 INP_WLOCK_ASSERT(tp->t_inpcb);
638 *sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit;
639 hole = tp->sackhint.nexthole;
640 if (hole == NULL || SEQ_LT(hole->rxmit, hole->end))
641 goto out;
642 while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) {
643 if (SEQ_LT(hole->rxmit, hole->end)) {
644 tp->sackhint.nexthole = hole;
645 break;
646 }
647 }
648out:
649 return (hole);
650}
651
652/*
653 * After a timeout, the SACK list may be rebuilt. This SACK information
654 * should be used to avoid retransmitting SACKed data. This function
655 * traverses the SACK list to see if snd_nxt should be moved forward.
656 */
657void
658tcp_sack_adjust(struct tcpcb *tp)
659{
660 struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes);
661
662 INP_WLOCK_ASSERT(tp->t_inpcb);
663 if (cur == NULL)
664 return; /* No holes */
665 if (SEQ_GEQ(tp->snd_nxt, tp->snd_fack))
666 return; /* We're already beyond any SACKed blocks */
667 /*-
668 * Two cases for which we want to advance snd_nxt:
669 * i) snd_nxt lies between end of one hole and beginning of another
670 * ii) snd_nxt lies between end of last hole and snd_fack
671 */
672 while ((p = TAILQ_NEXT(cur, scblink)) != NULL) {
673 if (SEQ_LT(tp->snd_nxt, cur->end))
674 return;
675 if (SEQ_GEQ(tp->snd_nxt, p->start))
676 cur = p;
677 else {
678 tp->snd_nxt = p->start;
679 return;
680 }
681 }
682 if (SEQ_LT(tp->snd_nxt, cur->end))
683 return;
684 tp->snd_nxt = tp->snd_fack;
685}