2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * a) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
14 * b) Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the distribution.
18 * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
35 #if defined(__FreeBSD__) && !defined(__Userspace__)
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: head/sys/netinet/sctp_cc_functions.c 359405 2020-03-28 20:25:45Z tuexen $");
40 #include <netinet/sctp_os.h>
41 #include <netinet/sctp_var.h>
42 #include <netinet/sctp_sysctl.h>
43 #include <netinet/sctp_pcb.h>
44 #include <netinet/sctp_header.h>
45 #include <netinet/sctputil.h>
46 #include <netinet/sctp_output.h>
47 #include <netinet/sctp_input.h>
48 #include <netinet/sctp_indata.h>
49 #include <netinet/sctp_uio.h>
50 #include <netinet/sctp_timer.h>
51 #include <netinet/sctp_auth.h>
52 #include <netinet/sctp_asconf.h>
53 #if defined(__FreeBSD__) && !defined(__Userspace__)
54 #include <netinet/sctp_kdtrace.h>
57 #define SHIFT_MPTCP_MULTI_N 40
58 #define SHIFT_MPTCP_MULTI_Z 16
59 #define SHIFT_MPTCP_MULTI 8
62 sctp_enforce_cwnd_limit(struct sctp_association *assoc, struct sctp_nets *net)
64 if ((assoc->max_cwnd > 0) &&
65 (net->cwnd > assoc->max_cwnd) &&
66 (net->cwnd > (net->mtu - sizeof(struct sctphdr)))) {
67 net->cwnd = assoc->max_cwnd ;
68 if (net->cwnd < (net->mtu - sizeof(struct sctphdr))) {
69 net->cwnd = net->mtu - sizeof(struct sctphdr);
75 sctp_set_initial_cc_param(struct sctp_tcb *stcb, struct sctp_nets *net)
77 struct sctp_association *assoc;
81 cwnd_in_mtu = SCTP_BASE_SYSCTL(sctp_initial_cwnd);
82 if (cwnd_in_mtu == 0) {
83 /* Using 0 means that the value of RFC 4960 is used. */
84 net->cwnd = min((net->mtu * 4), max((2 * net->mtu), SCTP_INITIAL_CWND));
87 * We take the minimum of the burst limit and the
88 * initial congestion window.
90 if ((assoc->max_burst > 0) && (cwnd_in_mtu > assoc->max_burst))
91 cwnd_in_mtu = assoc->max_burst;
92 net->cwnd = (net->mtu - sizeof(struct sctphdr)) * cwnd_in_mtu;
94 if ((stcb->asoc.sctp_cmt_on_off == SCTP_CMT_RPV1) ||
95 (stcb->asoc.sctp_cmt_on_off == SCTP_CMT_RPV2)) {
96 /* In case of resource pooling initialize appropriately */
97 net->cwnd /= assoc->numnets;
98 if (net->cwnd < (net->mtu - sizeof(struct sctphdr))) {
99 net->cwnd = net->mtu - sizeof(struct sctphdr);
102 sctp_enforce_cwnd_limit(assoc, net);
103 net->ssthresh = assoc->peers_rwnd;
104 #if defined(__FreeBSD__) && !defined(__Userspace__)
105 SDT_PROBE5(sctp, cwnd, net, init,
106 stcb->asoc.my_vtag, ((stcb->sctp_ep->sctp_lport << 16) | (stcb->rport)), net,
109 if (SCTP_BASE_SYSCTL(sctp_logging_level) &
110 (SCTP_CWND_MONITOR_ENABLE|SCTP_CWND_LOGGING_ENABLE)) {
111 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_INITIALIZATION);
116 sctp_cwnd_update_after_fr(struct sctp_tcb *stcb,
117 struct sctp_association *asoc)
119 struct sctp_nets *net;
120 uint32_t t_ssthresh, t_cwnd;
121 uint64_t t_ucwnd_sbw;
123 /* MT FIXME: Don't compute this over and over again */
127 if ((asoc->sctp_cmt_on_off == SCTP_CMT_RPV1) ||
128 (asoc->sctp_cmt_on_off == SCTP_CMT_RPV2)) {
129 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
130 t_ssthresh += net->ssthresh;
132 if (net->lastsa > 0) {
133 t_ucwnd_sbw += (uint64_t)net->cwnd / (uint64_t)net->lastsa;
136 if (t_ucwnd_sbw == 0) {
142 * CMT fast recovery code. Need to debug. ((sctp_cmt_on_off > 0) &&
143 * (net->fast_retran_loss_recovery == 0)))
145 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
146 if ((asoc->fast_retran_loss_recovery == 0) ||
147 (asoc->sctp_cmt_on_off > 0)) {
148 /* out of a RFC2582 Fast recovery window? */
149 if (net->net_ack > 0) {
151 * per section 7.2.3, are there any
152 * destinations that had a fast retransmit
153 * to them. If so what we need to do is
154 * adjust ssthresh and cwnd.
156 struct sctp_tmit_chunk *lchk;
157 int old_cwnd = net->cwnd;
159 if ((asoc->sctp_cmt_on_off == SCTP_CMT_RPV1) ||
160 (asoc->sctp_cmt_on_off == SCTP_CMT_RPV2)) {
161 if (asoc->sctp_cmt_on_off == SCTP_CMT_RPV1) {
162 net->ssthresh = (uint32_t)(((uint64_t)4 *
164 (uint64_t)net->ssthresh) /
165 (uint64_t)t_ssthresh);
168 if (asoc->sctp_cmt_on_off == SCTP_CMT_RPV2) {
172 /* lastsa>>3; we don't need to devide ...*/
176 /* Short Version => Equal to Contel Version MBe */
177 net->ssthresh = (uint32_t) (((uint64_t)4 *
179 (uint64_t)net->cwnd) /
182 /* INCREASE FACTOR */;
184 if ((net->cwnd > t_cwnd / 2) &&
185 (net->ssthresh < net->cwnd - t_cwnd / 2)) {
186 net->ssthresh = net->cwnd - t_cwnd / 2;
188 if (net->ssthresh < net->mtu) {
189 net->ssthresh = net->mtu;
192 net->ssthresh = net->cwnd / 2;
193 if (net->ssthresh < (net->mtu * 2)) {
194 net->ssthresh = 2 * net->mtu;
197 net->cwnd = net->ssthresh;
198 sctp_enforce_cwnd_limit(asoc, net);
199 #if defined(__FreeBSD__) && !defined(__Userspace__)
200 SDT_PROBE5(sctp, cwnd, net, fr,
201 stcb->asoc.my_vtag, ((stcb->sctp_ep->sctp_lport << 16) | (stcb->rport)), net,
202 old_cwnd, net->cwnd);
204 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
205 sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd),
206 SCTP_CWND_LOG_FROM_FR);
208 lchk = TAILQ_FIRST(&asoc->send_queue);
210 net->partial_bytes_acked = 0;
211 /* Turn on fast recovery window */
212 asoc->fast_retran_loss_recovery = 1;
214 /* Mark end of the window */
215 asoc->fast_recovery_tsn = asoc->sending_seq - 1;
217 asoc->fast_recovery_tsn = lchk->rec.data.tsn - 1;
221 * CMT fast recovery -- per destination
224 net->fast_retran_loss_recovery = 1;
227 /* Mark end of the window */
228 net->fast_recovery_tsn = asoc->sending_seq - 1;
230 net->fast_recovery_tsn = lchk->rec.data.tsn - 1;
233 sctp_timer_stop(SCTP_TIMER_TYPE_SEND,
234 stcb->sctp_ep, stcb, net,
235 SCTP_FROM_SCTP_CC_FUNCTIONS + SCTP_LOC_1);
236 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
237 stcb->sctp_ep, stcb, net);
239 } else if (net->net_ack > 0) {
241 * Mark a peg that we WOULD have done a cwnd
242 * reduction but RFC2582 prevented this action.
244 SCTP_STAT_INCR(sctps_fastretransinrtt);
249 /* Defines for instantaneous bw decisions */
250 #define SCTP_INST_LOOSING 1 /* Losing to other flows */
251 #define SCTP_INST_NEUTRAL 2 /* Neutral, no indication */
252 #define SCTP_INST_GAINING 3 /* Gaining, step down possible */
255 #if defined(__FreeBSD__) && !defined(__Userspace__)
257 cc_bw_same(struct sctp_tcb *stcb, struct sctp_nets *net, uint64_t nbw,
258 uint64_t rtt_offset, uint64_t vtag, uint8_t inst_ind)
261 cc_bw_same(struct sctp_tcb *stcb SCTP_UNUSED, struct sctp_nets *net, uint64_t nbw,
262 uint64_t rtt_offset, uint8_t inst_ind)
265 #if defined(__FreeBSD__) && !defined(__Userspace__)
266 uint64_t oth, probepoint;
269 #if defined(__FreeBSD__) && !defined(__Userspace__)
270 probepoint = (((uint64_t)net->cwnd) << 32);
272 if (net->rtt > net->cc_mod.rtcc.lbw_rtt + rtt_offset) {
275 * we don't update bw.. so we don't
276 * update the rtt either.
278 #if defined(__FreeBSD__) && !defined(__Userspace__)
280 probepoint |= ((5 << 16) | 1);
281 SDT_PROBE5(sctp, cwnd, net, rttvar,
283 ((net->cc_mod.rtcc.lbw << 32) | nbw),
284 ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
288 if ((net->cc_mod.rtcc.steady_step) && (inst_ind != SCTP_INST_LOOSING)) {
289 if (net->cc_mod.rtcc.last_step_state == 5)
290 net->cc_mod.rtcc.step_cnt++;
292 net->cc_mod.rtcc.step_cnt = 1;
293 net->cc_mod.rtcc.last_step_state = 5;
294 if ((net->cc_mod.rtcc.step_cnt == net->cc_mod.rtcc.steady_step) ||
295 ((net->cc_mod.rtcc.step_cnt > net->cc_mod.rtcc.steady_step) &&
296 ((net->cc_mod.rtcc.step_cnt % net->cc_mod.rtcc.steady_step) == 0))) {
297 /* Try a step down */
298 #if defined(__FreeBSD__) && !defined(__Userspace__)
299 oth = net->cc_mod.rtcc.vol_reduce;
301 oth |= net->cc_mod.rtcc.step_cnt;
303 oth |= net->cc_mod.rtcc.last_step_state;
304 SDT_PROBE5(sctp, cwnd, net, rttstep,
306 ((net->cc_mod.rtcc.lbw << 32) | nbw),
307 ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
311 if (net->cwnd > (4 * net->mtu)) {
312 net->cwnd -= net->mtu;
313 net->cc_mod.rtcc.vol_reduce++;
315 net->cc_mod.rtcc.step_cnt = 0;
321 if (net->rtt < net->cc_mod.rtcc.lbw_rtt-rtt_offset) {
323 * rtt decreased, there could be more room.
324 * we update both the bw and the rtt here to
325 * lock this in as a good step down.
327 #if defined(__FreeBSD__) && !defined(__Userspace__)
329 probepoint |= ((6 << 16) | 0);
330 SDT_PROBE5(sctp, cwnd, net, rttvar,
332 ((net->cc_mod.rtcc.lbw << 32) | nbw),
333 ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
337 if (net->cc_mod.rtcc.steady_step) {
338 #if defined(__FreeBSD__) && !defined(__Userspace__)
339 oth = net->cc_mod.rtcc.vol_reduce;
341 oth |= net->cc_mod.rtcc.step_cnt;
343 oth |= net->cc_mod.rtcc.last_step_state;
344 SDT_PROBE5(sctp, cwnd, net, rttstep,
346 ((net->cc_mod.rtcc.lbw << 32) | nbw),
347 ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
351 if ((net->cc_mod.rtcc.last_step_state == 5) &&
352 (net->cc_mod.rtcc.step_cnt > net->cc_mod.rtcc.steady_step)) {
353 /* Step down worked */
354 net->cc_mod.rtcc.step_cnt = 0;
357 net->cc_mod.rtcc.last_step_state = 6;
358 net->cc_mod.rtcc.step_cnt = 0;
361 net->cc_mod.rtcc.lbw = nbw;
362 net->cc_mod.rtcc.lbw_rtt = net->rtt;
363 net->cc_mod.rtcc.cwnd_at_bw_set = net->cwnd;
364 if (inst_ind == SCTP_INST_GAINING)
366 else if (inst_ind == SCTP_INST_NEUTRAL)
371 /* Ok bw and rtt remained the same .. no update to any
373 #if defined(__FreeBSD__) && !defined(__Userspace__)
375 probepoint |= ((7 << 16) | net->cc_mod.rtcc.ret_from_eq);
376 SDT_PROBE5(sctp, cwnd, net, rttvar,
378 ((net->cc_mod.rtcc.lbw << 32) | nbw),
379 ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
383 if ((net->cc_mod.rtcc.steady_step) && (inst_ind != SCTP_INST_LOOSING)) {
384 if (net->cc_mod.rtcc.last_step_state == 5)
385 net->cc_mod.rtcc.step_cnt++;
387 net->cc_mod.rtcc.step_cnt = 1;
388 net->cc_mod.rtcc.last_step_state = 5;
389 if ((net->cc_mod.rtcc.step_cnt == net->cc_mod.rtcc.steady_step) ||
390 ((net->cc_mod.rtcc.step_cnt > net->cc_mod.rtcc.steady_step) &&
391 ((net->cc_mod.rtcc.step_cnt % net->cc_mod.rtcc.steady_step) == 0))) {
392 /* Try a step down */
393 if (net->cwnd > (4 * net->mtu)) {
394 net->cwnd -= net->mtu;
395 net->cc_mod.rtcc.vol_reduce++;
398 net->cc_mod.rtcc.step_cnt = 0;
402 if (inst_ind == SCTP_INST_GAINING)
404 else if (inst_ind == SCTP_INST_NEUTRAL)
407 return ((int)net->cc_mod.rtcc.ret_from_eq);
410 #if defined(__FreeBSD__) && !defined(__Userspace__)
412 cc_bw_decrease(struct sctp_tcb *stcb, struct sctp_nets *net, uint64_t nbw, uint64_t rtt_offset,
413 uint64_t vtag, uint8_t inst_ind)
416 cc_bw_decrease(struct sctp_tcb *stcb SCTP_UNUSED, struct sctp_nets *net, uint64_t nbw, uint64_t rtt_offset,
420 #if defined(__FreeBSD__) && !defined(__Userspace__)
421 uint64_t oth, probepoint;
424 /* Bandwidth decreased.*/
425 #if defined(__FreeBSD__) && !defined(__Userspace__)
426 probepoint = (((uint64_t)net->cwnd) << 32);
428 if (net->rtt > net->cc_mod.rtcc.lbw_rtt+rtt_offset) {
430 /* Did we add more */
431 if ((net->cwnd > net->cc_mod.rtcc.cwnd_at_bw_set) &&
432 (inst_ind != SCTP_INST_LOOSING)) {
433 /* We caused it maybe.. back off? */
434 #if defined(__FreeBSD__) && !defined(__Userspace__)
436 probepoint |= ((1 << 16) | 1);
437 SDT_PROBE5(sctp, cwnd, net, rttvar,
439 ((net->cc_mod.rtcc.lbw << 32) | nbw),
440 ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
444 if (net->cc_mod.rtcc.ret_from_eq) {
445 /* Switch over to CA if we are less aggressive */
446 net->ssthresh = net->cwnd-1;
447 net->partial_bytes_acked = 0;
451 #if defined(__FreeBSD__) && !defined(__Userspace__)
453 probepoint |= ((2 << 16) | 0);
454 SDT_PROBE5(sctp, cwnd, net, rttvar,
456 ((net->cc_mod.rtcc.lbw << 32) | nbw),
457 ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
461 /* Someone else - fight for more? */
462 if (net->cc_mod.rtcc.steady_step) {
463 #if defined(__FreeBSD__) && !defined(__Userspace__)
464 oth = net->cc_mod.rtcc.vol_reduce;
466 oth |= net->cc_mod.rtcc.step_cnt;
468 oth |= net->cc_mod.rtcc.last_step_state;
469 SDT_PROBE5(sctp, cwnd, net, rttstep,
471 ((net->cc_mod.rtcc.lbw << 32) | nbw),
472 ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
476 /* Did we voluntarily give up some? if so take
479 if ((net->cc_mod.rtcc.vol_reduce) &&
480 (inst_ind != SCTP_INST_GAINING)) {
481 net->cwnd += net->mtu;
482 sctp_enforce_cwnd_limit(&stcb->asoc, net);
483 net->cc_mod.rtcc.vol_reduce--;
485 net->cc_mod.rtcc.last_step_state = 2;
486 net->cc_mod.rtcc.step_cnt = 0;
489 } else if (net->rtt < net->cc_mod.rtcc.lbw_rtt-rtt_offset) {
490 /* bw & rtt decreased */
491 #if defined(__FreeBSD__) && !defined(__Userspace__)
493 probepoint |= ((3 << 16) | 0);
494 SDT_PROBE5(sctp, cwnd, net, rttvar,
496 ((net->cc_mod.rtcc.lbw << 32) | nbw),
497 ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
501 if (net->cc_mod.rtcc.steady_step) {
502 #if defined(__FreeBSD__) && !defined(__Userspace__)
503 oth = net->cc_mod.rtcc.vol_reduce;
505 oth |= net->cc_mod.rtcc.step_cnt;
507 oth |= net->cc_mod.rtcc.last_step_state;
508 SDT_PROBE5(sctp, cwnd, net, rttstep,
510 ((net->cc_mod.rtcc.lbw << 32) | nbw),
511 ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
515 if ((net->cc_mod.rtcc.vol_reduce) &&
516 (inst_ind != SCTP_INST_GAINING)) {
517 net->cwnd += net->mtu;
518 sctp_enforce_cwnd_limit(&stcb->asoc, net);
519 net->cc_mod.rtcc.vol_reduce--;
521 net->cc_mod.rtcc.last_step_state = 3;
522 net->cc_mod.rtcc.step_cnt = 0;
526 /* The bw decreased but rtt stayed the same */
527 #if defined(__FreeBSD__) && !defined(__Userspace__)
529 probepoint |= ((4 << 16) | 0);
530 SDT_PROBE5(sctp, cwnd, net, rttvar,
532 ((net->cc_mod.rtcc.lbw << 32) | nbw),
533 ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
537 if (net->cc_mod.rtcc.steady_step) {
538 #if defined(__FreeBSD__) && !defined(__Userspace__)
539 oth = net->cc_mod.rtcc.vol_reduce;
541 oth |= net->cc_mod.rtcc.step_cnt;
543 oth |= net->cc_mod.rtcc.last_step_state;
544 SDT_PROBE5(sctp, cwnd, net, rttstep,
546 ((net->cc_mod.rtcc.lbw << 32) | nbw),
547 ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
551 if ((net->cc_mod.rtcc.vol_reduce) &&
552 (inst_ind != SCTP_INST_GAINING)) {
553 net->cwnd += net->mtu;
554 sctp_enforce_cwnd_limit(&stcb->asoc, net);
555 net->cc_mod.rtcc.vol_reduce--;
557 net->cc_mod.rtcc.last_step_state = 4;
558 net->cc_mod.rtcc.step_cnt = 0;
561 net->cc_mod.rtcc.lbw = nbw;
562 net->cc_mod.rtcc.lbw_rtt = net->rtt;
563 net->cc_mod.rtcc.cwnd_at_bw_set = net->cwnd;
564 if (inst_ind == SCTP_INST_GAINING) {
571 #if defined(__FreeBSD__) && !defined(__Userspace__)
573 cc_bw_increase(struct sctp_tcb *stcb, struct sctp_nets *net, uint64_t nbw, uint64_t vtag)
576 cc_bw_increase(struct sctp_tcb *stcb SCTP_UNUSED, struct sctp_nets *net, uint64_t nbw)
579 #if defined(__FreeBSD__) && !defined(__Userspace__)
580 uint64_t oth, probepoint;
583 /* BW increased, so update and
584 * return 0, since all actions in
585 * our table say to do the normal CC
586 * update. Note that we pay no attention to
587 * the inst_ind since our overall sum is increasing.
589 #if defined(__FreeBSD__) && !defined(__Userspace__)
591 probepoint = (((uint64_t)net->cwnd) << 32);
592 SDT_PROBE5(sctp, cwnd, net, rttvar,
594 ((net->cc_mod.rtcc.lbw << 32) | nbw),
595 ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
599 if (net->cc_mod.rtcc.steady_step) {
600 #if defined(__FreeBSD__) && !defined(__Userspace__)
601 oth = net->cc_mod.rtcc.vol_reduce;
603 oth |= net->cc_mod.rtcc.step_cnt;
605 oth |= net->cc_mod.rtcc.last_step_state;
606 SDT_PROBE5(sctp, cwnd, net, rttstep,
608 ((net->cc_mod.rtcc.lbw << 32) | nbw),
609 ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
613 net->cc_mod.rtcc.last_step_state = 0;
614 net->cc_mod.rtcc.step_cnt = 0;
615 net->cc_mod.rtcc.vol_reduce = 0;
617 net->cc_mod.rtcc.lbw = nbw;
618 net->cc_mod.rtcc.lbw_rtt = net->rtt;
619 net->cc_mod.rtcc.cwnd_at_bw_set = net->cwnd;
623 /* RTCC Algorithm to limit growth of cwnd, return
624 * true if you want to NOT allow cwnd growth
627 cc_bw_limit(struct sctp_tcb *stcb, struct sctp_nets *net, uint64_t nbw)
629 uint64_t bw_offset, rtt_offset;
630 #if defined(__FreeBSD__) && !defined(__Userspace__)
631 uint64_t probepoint, rtt, vtag;
633 uint64_t bytes_for_this_rtt, inst_bw;
634 uint64_t div, inst_off;
639 * Here we need to see if we want
640 * to limit cwnd growth due to increase
641 * in overall rtt but no increase in bw.
642 * We use the following table to figure
643 * out what we should do. When we return
644 * 0, cc update goes on as planned. If we
645 * return 1, then no cc update happens and cwnd
646 * stays where it is at.
647 * ----------------------------------
649 * *********************************
650 * INC | INC | return 0
651 * ----------------------------------
652 * INC | SAME | return 0
653 * ----------------------------------
654 * INC | DECR | return 0
655 * ----------------------------------
656 * SAME | INC | return 1
657 * ----------------------------------
658 * SAME | SAME | return 1
659 * ----------------------------------
660 * SAME | DECR | return 0
661 * ----------------------------------
662 * DECR | INC | return 0 or 1 based on if we caused.
663 * ----------------------------------
664 * DECR | SAME | return 0
665 * ----------------------------------
666 * DECR | DECR | return 0
667 * ----------------------------------
669 * We are a bit fuzz on what an increase or
670 * decrease is. For BW it is the same if
671 * it did not change within 1/64th. For
672 * RTT it stayed the same if it did not
673 * change within 1/32nd
675 bw_shift = SCTP_BASE_SYSCTL(sctp_rttvar_bw);
676 #if defined(__FreeBSD__) && !defined(__Userspace__)
677 rtt = stcb->asoc.my_vtag;
678 vtag = (rtt << 32) | (((uint32_t)(stcb->sctp_ep->sctp_lport)) << 16) | (stcb->rport);
679 probepoint = (((uint64_t)net->cwnd) << 32);
682 if (net->cc_mod.rtcc.rtt_set_this_sack) {
683 net->cc_mod.rtcc.rtt_set_this_sack = 0;
684 bytes_for_this_rtt = net->cc_mod.rtcc.bw_bytes - net->cc_mod.rtcc.bw_bytes_at_last_rttc;
685 net->cc_mod.rtcc.bw_bytes_at_last_rttc = net->cc_mod.rtcc.bw_bytes;
687 div = net->rtt / 1000;
689 inst_bw = bytes_for_this_rtt / div;
690 inst_off = inst_bw >> bw_shift;
692 inst_ind = SCTP_INST_GAINING;
693 else if ((inst_bw+inst_off) < nbw)
694 inst_ind = SCTP_INST_LOOSING;
696 inst_ind = SCTP_INST_NEUTRAL;
697 #if defined(__FreeBSD__) && !defined(__Userspace__)
698 probepoint |= ((0xb << 16) | inst_ind);
701 inst_ind = net->cc_mod.rtcc.last_inst_ind;
702 #if defined(__FreeBSD__) && !defined(__Userspace__)
703 inst_bw = bytes_for_this_rtt / (uint64_t)(net->rtt);
704 /* Can't determine do not change */
705 probepoint |= ((0xc << 16) | inst_ind);
709 inst_ind = net->cc_mod.rtcc.last_inst_ind;
710 #if defined(__FreeBSD__) && !defined(__Userspace__)
711 inst_bw = bytes_for_this_rtt;
712 /* Can't determine do not change */
713 probepoint |= ((0xd << 16) | inst_ind);
716 #if defined(__FreeBSD__) && !defined(__Userspace__)
717 SDT_PROBE5(sctp, cwnd, net, rttvar,
719 ((nbw << 32) | inst_bw),
720 ((net->cc_mod.rtcc.lbw_rtt << 32) | rtt),
725 /* No rtt measurement, use last one */
726 inst_ind = net->cc_mod.rtcc.last_inst_ind;
728 bw_offset = net->cc_mod.rtcc.lbw >> bw_shift;
729 if (nbw > net->cc_mod.rtcc.lbw + bw_offset) {
730 #if defined(__FreeBSD__) && !defined(__Userspace__)
731 ret = cc_bw_increase(stcb, net, nbw, vtag);
733 ret = cc_bw_increase(stcb, net, nbw);
737 rtt_offset = net->cc_mod.rtcc.lbw_rtt >> SCTP_BASE_SYSCTL(sctp_rttvar_rtt);
738 if (nbw < net->cc_mod.rtcc.lbw - bw_offset) {
739 #if defined(__FreeBSD__) && !defined(__Userspace__)
740 ret = cc_bw_decrease(stcb, net, nbw, rtt_offset, vtag, inst_ind);
742 ret = cc_bw_decrease(stcb, net, nbw, rtt_offset, inst_ind);
746 /* If we reach here then
747 * we are in a situation where
748 * the bw stayed the same.
750 #if defined(__FreeBSD__) && !defined(__Userspace__)
751 ret = cc_bw_same(stcb, net, nbw, rtt_offset, vtag, inst_ind);
753 ret = cc_bw_same(stcb, net, nbw, rtt_offset, inst_ind);
756 net->cc_mod.rtcc.last_inst_ind = inst_ind;
761 sctp_cwnd_update_after_sack_common(struct sctp_tcb *stcb,
762 struct sctp_association *asoc,
763 int accum_moved, int reneged_all SCTP_UNUSED, int will_exit, int use_rtcc)
765 struct sctp_nets *net;
766 #if defined(__FreeBSD__) && !defined(__Userspace__)
769 uint32_t t_ssthresh, t_cwnd, incr;
770 uint64_t t_ucwnd_sbw;
771 uint64_t t_path_mptcp;
772 uint64_t mptcp_like_alpha;
776 /* MT FIXME: Don't compute this over and over again */
781 mptcp_like_alpha = 1;
782 if ((stcb->asoc.sctp_cmt_on_off == SCTP_CMT_RPV1) ||
783 (stcb->asoc.sctp_cmt_on_off == SCTP_CMT_RPV2) ||
784 (stcb->asoc.sctp_cmt_on_off == SCTP_CMT_MPTCP)) {
786 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
787 t_ssthresh += net->ssthresh;
789 /* lastsa>>3; we don't need to devide ...*/
794 t_ucwnd_sbw += (uint64_t)net->cwnd / (uint64_t)srtt;
795 t_path_mptcp += (((uint64_t)net->cwnd) << SHIFT_MPTCP_MULTI_Z) /
796 (((uint64_t)net->mtu) * (uint64_t)srtt);
797 tmp = (((uint64_t)net->cwnd) << SHIFT_MPTCP_MULTI_N) /
798 ((uint64_t)net->mtu * (uint64_t)(srtt * srtt));
799 if (tmp > max_path) {
804 if (t_path_mptcp > 0) {
805 mptcp_like_alpha = max_path / (t_path_mptcp * t_path_mptcp);
807 mptcp_like_alpha = 1;
810 if (t_ssthresh == 0) {
813 if (t_ucwnd_sbw == 0) {
816 /******************************/
817 /* update cwnd and Early FR */
818 /******************************/
819 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
821 #ifdef JANA_CMT_FAST_RECOVERY
823 * CMT fast recovery code. Need to debug.
825 if (net->fast_retran_loss_recovery && net->new_pseudo_cumack) {
826 if (SCTP_TSN_GE(asoc->last_acked_seq, net->fast_recovery_tsn) ||
827 SCTP_TSN_GE(net->pseudo_cumack,net->fast_recovery_tsn)) {
828 net->will_exit_fast_recovery = 1;
832 /* if nothing was acked on this destination skip it */
833 if (net->net_ack == 0) {
834 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
835 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FROM_SACK);
839 #ifdef JANA_CMT_FAST_RECOVERY
840 /* CMT fast recovery code
843 if (sctp_cmt_on_off > 0 && net->fast_retran_loss_recovery && net->will_exit_fast_recovery == 0) {
846 else if (sctp_cmt_on_off == 0 && asoc->fast_retran_loss_recovery && will_exit == 0) {
850 if (asoc->fast_retran_loss_recovery &&
852 (asoc->sctp_cmt_on_off == 0)) {
854 * If we are in loss recovery we skip any cwnd
860 * Did any measurements go on for this network?
862 if (use_rtcc && (net->cc_mod.rtcc.tls_needs_set > 0)) {
865 * At this point our bw_bytes has been updated
866 * by incoming sack information.
868 * But our bw may not yet be set.
871 if ((net->cc_mod.rtcc.new_tot_time/1000) > 0) {
872 nbw = net->cc_mod.rtcc.bw_bytes/(net->cc_mod.rtcc.new_tot_time/1000);
874 nbw = net->cc_mod.rtcc.bw_bytes;
876 if (net->cc_mod.rtcc.lbw) {
877 if (cc_bw_limit(stcb, net, nbw)) {
878 /* Hold here, no update */
882 #if defined(__FreeBSD__) && !defined(__Userspace__)
883 uint64_t vtag, probepoint;
885 probepoint = (((uint64_t)net->cwnd) << 32);
886 probepoint |= ((0xa << 16) | 0);
887 vtag = (net->rtt << 32) |
888 (((uint32_t)(stcb->sctp_ep->sctp_lport)) << 16) |
891 SDT_PROBE5(sctp, cwnd, net, rttvar,
894 ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
898 net->cc_mod.rtcc.lbw = nbw;
899 net->cc_mod.rtcc.lbw_rtt = net->rtt;
900 if (net->cc_mod.rtcc.rtt_set_this_sack) {
901 net->cc_mod.rtcc.rtt_set_this_sack = 0;
902 net->cc_mod.rtcc.bw_bytes_at_last_rttc = net->cc_mod.rtcc.bw_bytes;
907 * CMT: CUC algorithm. Update cwnd if pseudo-cumack has
911 ((asoc->sctp_cmt_on_off > 0) && net->new_pseudo_cumack)) {
912 /* If the cumulative ack moved we can proceed */
913 if (net->cwnd <= net->ssthresh) {
914 /* We are in slow start */
915 if (net->flight_size + net->net_ack >= net->cwnd) {
918 #if defined(__FreeBSD__) && !defined(__Userspace__)
919 old_cwnd = net->cwnd;
921 switch (asoc->sctp_cmt_on_off) {
923 limit = (uint32_t)(((uint64_t)net->mtu *
924 (uint64_t)SCTP_BASE_SYSCTL(sctp_L2_abc_variable) *
925 (uint64_t)net->ssthresh) /
926 (uint64_t)t_ssthresh);
927 incr = (uint32_t)(((uint64_t)net->net_ack *
928 (uint64_t)net->ssthresh) /
929 (uint64_t)t_ssthresh);
938 /* lastsa>>3; we don't need to divide ...*/
943 limit = (uint32_t)(((uint64_t)net->mtu *
944 (uint64_t)SCTP_BASE_SYSCTL(sctp_L2_abc_variable) *
945 (uint64_t)net->cwnd) /
946 ((uint64_t)srtt * t_ucwnd_sbw));
947 /* INCREASE FACTOR */
948 incr = (uint32_t)(((uint64_t)net->net_ack *
949 (uint64_t)net->cwnd) /
950 ((uint64_t)srtt * t_ucwnd_sbw));
951 /* INCREASE FACTOR */
960 limit = (uint32_t)(((uint64_t)net->mtu *
962 (uint64_t)SCTP_BASE_SYSCTL(sctp_L2_abc_variable)) >>
964 incr = (uint32_t)(((uint64_t)net->net_ack *
970 if (incr > net->net_ack) {
973 if (incr > net->mtu) {
979 if (incr > net->mtu * SCTP_BASE_SYSCTL(sctp_L2_abc_variable)) {
980 incr = net->mtu * SCTP_BASE_SYSCTL(sctp_L2_abc_variable);
985 sctp_enforce_cwnd_limit(asoc, net);
986 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
987 sctp_log_cwnd(stcb, net, incr,
988 SCTP_CWND_LOG_FROM_SS);
990 #if defined(__FreeBSD__) && !defined(__Userspace__)
991 SDT_PROBE5(sctp, cwnd, net, ack,
993 ((stcb->sctp_ep->sctp_lport << 16) | (stcb->rport)),
995 old_cwnd, net->cwnd);
998 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
999 sctp_log_cwnd(stcb, net, net->net_ack,
1000 SCTP_CWND_LOG_NOADV_SS);
1004 /* We are in congestion avoidance */
1008 net->partial_bytes_acked += net->net_ack;
1010 if ((net->flight_size + net->net_ack >= net->cwnd) &&
1011 (net->partial_bytes_acked >= net->cwnd)) {
1012 net->partial_bytes_acked -= net->cwnd;
1013 #if defined(__FreeBSD__) && !defined(__Userspace__)
1014 old_cwnd = net->cwnd;
1016 switch (asoc->sctp_cmt_on_off) {
1018 incr = (uint32_t)(((uint64_t)net->mtu *
1019 (uint64_t)net->ssthresh) /
1020 (uint64_t)t_ssthresh);
1026 /* lastsa>>3; we don't need to divide ... */
1031 incr = (uint32_t)((uint64_t)net->mtu *
1032 (uint64_t)net->cwnd /
1035 /* INCREASE FACTOR */
1040 case SCTP_CMT_MPTCP:
1041 incr = (uint32_t)((mptcp_like_alpha *
1042 (uint64_t) net->cwnd) >>
1044 if (incr > net->mtu) {
1053 sctp_enforce_cwnd_limit(asoc, net);
1054 #if defined(__FreeBSD__) && !defined(__Userspace__)
1055 SDT_PROBE5(sctp, cwnd, net, ack,
1057 ((stcb->sctp_ep->sctp_lport << 16) | (stcb->rport)),
1059 old_cwnd, net->cwnd);
1061 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1062 sctp_log_cwnd(stcb, net, net->mtu,
1063 SCTP_CWND_LOG_FROM_CA);
1066 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
1067 sctp_log_cwnd(stcb, net, net->net_ack,
1068 SCTP_CWND_LOG_NOADV_CA);
1073 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
1074 sctp_log_cwnd(stcb, net, net->mtu,
1075 SCTP_CWND_LOG_NO_CUMACK);
1081 #if defined(__FreeBSD__) && !defined(__Userspace__)
1083 sctp_cwnd_update_exit_pf_common(struct sctp_tcb *stcb, struct sctp_nets *net)
1086 sctp_cwnd_update_exit_pf_common(struct sctp_tcb *stcb SCTP_UNUSED, struct sctp_nets *net)
1089 #if defined(__FreeBSD__) && !defined(__Userspace__)
1092 old_cwnd = net->cwnd;
1094 net->cwnd = net->mtu;
1095 #if defined(__FreeBSD__) && !defined(__Userspace__)
1096 SDT_PROBE5(sctp, cwnd, net, ack,
1097 stcb->asoc.my_vtag, ((stcb->sctp_ep->sctp_lport << 16) | (stcb->rport)), net,
1098 old_cwnd, net->cwnd);
1100 SCTPDBG(SCTP_DEBUG_INDATA1, "Destination %p moved from PF to reachable with cwnd %d.\n",
1101 (void *)net, net->cwnd);
1106 sctp_cwnd_update_after_timeout(struct sctp_tcb *stcb, struct sctp_nets *net)
1108 int old_cwnd = net->cwnd;
1109 uint32_t t_ssthresh, t_cwnd;
1110 uint64_t t_ucwnd_sbw;
1112 /* MT FIXME: Don't compute this over and over again */
1115 if ((stcb->asoc.sctp_cmt_on_off == SCTP_CMT_RPV1) ||
1116 (stcb->asoc.sctp_cmt_on_off == SCTP_CMT_RPV2)) {
1117 struct sctp_nets *lnet;
1121 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
1122 t_ssthresh += lnet->ssthresh;
1123 t_cwnd += lnet->cwnd;
1124 srtt = lnet->lastsa;
1125 /* lastsa>>3; we don't need to divide ... */
1127 t_ucwnd_sbw += (uint64_t)lnet->cwnd / (uint64_t)srtt;
1130 if (t_ssthresh < 1) {
1133 if (t_ucwnd_sbw < 1) {
1136 if (stcb->asoc.sctp_cmt_on_off == SCTP_CMT_RPV1) {
1137 net->ssthresh = (uint32_t)(((uint64_t)4 *
1138 (uint64_t)net->mtu *
1139 (uint64_t)net->ssthresh) /
1140 (uint64_t)t_ssthresh);
1145 /* lastsa>>3; we don't need to divide ... */
1149 cc_delta = t_ucwnd_sbw * (uint64_t)srtt / 2;
1150 if (cc_delta < t_cwnd) {
1151 net->ssthresh = (uint32_t)((uint64_t)t_cwnd - cc_delta);
1153 net->ssthresh = net->mtu;
1156 if ((net->cwnd > t_cwnd / 2) &&
1157 (net->ssthresh < net->cwnd - t_cwnd / 2)) {
1158 net->ssthresh = net->cwnd - t_cwnd / 2;
1160 if (net->ssthresh < net->mtu) {
1161 net->ssthresh = net->mtu;
1164 net->ssthresh = max(net->cwnd / 2, 4 * net->mtu);
1166 net->cwnd = net->mtu;
1167 net->partial_bytes_acked = 0;
1168 #if defined(__FreeBSD__) && !defined(__Userspace__)
1169 SDT_PROBE5(sctp, cwnd, net, to,
1171 ((stcb->sctp_ep->sctp_lport << 16) | (stcb->rport)),
1173 old_cwnd, net->cwnd);
1175 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1176 sctp_log_cwnd(stcb, net, net->cwnd - old_cwnd, SCTP_CWND_LOG_FROM_RTX);
1181 sctp_cwnd_update_after_ecn_echo_common(struct sctp_tcb *stcb, struct sctp_nets *net,
1182 int in_window, int num_pkt_lost, int use_rtcc)
1184 int old_cwnd = net->cwnd;
1185 if ((use_rtcc) && (net->lan_type == SCTP_LAN_LOCAL) && (net->cc_mod.rtcc.use_dccc_ecn)) {
1186 /* Data center Congestion Control */
1187 if (in_window == 0) {
1188 /* Go to CA with the cwnd at the point we sent
1189 * the TSN that was marked with a CE.
1191 if (net->ecn_prev_cwnd < net->cwnd) {
1192 /* Restore to prev cwnd */
1193 net->cwnd = net->ecn_prev_cwnd - (net->mtu * num_pkt_lost);
1195 /* Just cut in 1/2 */
1199 net->ssthresh = net->cwnd - (num_pkt_lost * net->mtu);
1200 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1201 sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_SAT);
1204 /* Further tuning down required over the drastic original cut */
1205 net->ssthresh -= (net->mtu * num_pkt_lost);
1206 net->cwnd -= (net->mtu * num_pkt_lost);
1207 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1208 sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_SAT);
1212 SCTP_STAT_INCR(sctps_ecnereducedcwnd);
1214 if (in_window == 0) {
1215 SCTP_STAT_INCR(sctps_ecnereducedcwnd);
1216 net->ssthresh = net->cwnd / 2;
1217 if (net->ssthresh < net->mtu) {
1218 net->ssthresh = net->mtu;
1219 /* here back off the timer as well, to slow us down */
1222 net->cwnd = net->ssthresh;
1223 #if defined(__FreeBSD__) && !defined(__Userspace__)
1224 SDT_PROBE5(sctp, cwnd, net, ecn,
1226 ((stcb->sctp_ep->sctp_lport << 16) | (stcb->rport)),
1228 old_cwnd, net->cwnd);
1230 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1231 sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_SAT);
1239 sctp_cwnd_update_after_packet_dropped(struct sctp_tcb *stcb,
1240 struct sctp_nets *net, struct sctp_pktdrop_chunk *cp,
1241 uint32_t *bottle_bw, uint32_t *on_queue)
1245 int old_cwnd = net->cwnd;
1247 /* get bottle neck bw */
1248 *bottle_bw = ntohl(cp->bottle_bw);
1249 /* and whats on queue */
1250 *on_queue = ntohl(cp->current_onq);
1252 * adjust the on-queue if our flight is more it could be
1253 * that the router has not yet gotten data "in-flight" to it
1255 if (*on_queue < net->flight_size) {
1256 *on_queue = net->flight_size;
1258 /* rtt is measured in micro seconds, bottle_bw in bytes per second */
1259 bw_avail = (uint32_t)(((uint64_t)(*bottle_bw) * net->rtt) / (uint64_t)1000000);
1260 if (bw_avail > *bottle_bw) {
1262 * Cap the growth to no more than the bottle neck.
1263 * This can happen as RTT slides up due to queues.
1264 * It also means if you have more than a 1 second
1265 * RTT with a empty queue you will be limited to the
1266 * bottle_bw per second no matter if other points
1267 * have 1/2 the RTT and you could get more out...
1269 bw_avail = *bottle_bw;
1271 if (*on_queue > bw_avail) {
1273 * No room for anything else don't allow anything
1274 * else to be "added to the fire".
1276 int seg_inflight, seg_onqueue, my_portion;
1278 net->partial_bytes_acked = 0;
1279 /* how much are we over queue size? */
1280 incr = *on_queue - bw_avail;
1281 if (stcb->asoc.seen_a_sack_this_pkt) {
1283 * undo any cwnd adjustment that the sack
1286 net->cwnd = net->prev_cwnd;
1288 /* Now how much of that is mine? */
1289 seg_inflight = net->flight_size / net->mtu;
1290 seg_onqueue = *on_queue / net->mtu;
1291 my_portion = (incr * seg_inflight) / seg_onqueue;
1293 /* Have I made an adjustment already */
1294 if (net->cwnd > net->flight_size) {
1296 * for this flight I made an adjustment we
1297 * need to decrease the portion by a share
1298 * our previous adjustment.
1302 diff_adj = net->cwnd - net->flight_size;
1303 if (diff_adj > my_portion)
1306 my_portion -= diff_adj;
1309 * back down to the previous cwnd (assume we have
1310 * had a sack before this packet). minus what ever
1311 * portion of the overage is my fault.
1313 net->cwnd -= my_portion;
1315 /* we will NOT back down more than 1 MTU */
1316 if (net->cwnd <= net->mtu) {
1317 net->cwnd = net->mtu;
1320 net->ssthresh = net->cwnd - 1;
1323 * Take 1/4 of the space left or max burst up ..
1324 * whichever is less.
1326 incr = (bw_avail - *on_queue) >> 2;
1327 if ((stcb->asoc.max_burst > 0) &&
1328 (stcb->asoc.max_burst * net->mtu < incr)) {
1329 incr = stcb->asoc.max_burst * net->mtu;
1333 if (net->cwnd > bw_avail) {
1334 /* We can't exceed the pipe size */
1335 net->cwnd = bw_avail;
1337 if (net->cwnd < net->mtu) {
1338 /* We always have 1 MTU */
1339 net->cwnd = net->mtu;
1341 sctp_enforce_cwnd_limit(&stcb->asoc, net);
1342 if (net->cwnd - old_cwnd != 0) {
1343 /* log only changes */
1344 #if defined(__FreeBSD__) && !defined(__Userspace__)
1345 SDT_PROBE5(sctp, cwnd, net, pd,
1347 ((stcb->sctp_ep->sctp_lport << 16) | (stcb->rport)),
1349 old_cwnd, net->cwnd);
1351 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1352 sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd),
1353 SCTP_CWND_LOG_FROM_SAT);
1359 sctp_cwnd_update_after_output(struct sctp_tcb *stcb,
1360 struct sctp_nets *net, int burst_limit)
1362 int old_cwnd = net->cwnd;
1364 if (net->ssthresh < net->cwnd)
1365 net->ssthresh = net->cwnd;
1367 net->cwnd = (net->flight_size + (burst_limit * net->mtu));
1368 sctp_enforce_cwnd_limit(&stcb->asoc, net);
1369 #if defined(__FreeBSD__) && !defined(__Userspace__)
1370 SDT_PROBE5(sctp, cwnd, net, bl,
1372 ((stcb->sctp_ep->sctp_lport << 16) | (stcb->rport)),
1374 old_cwnd, net->cwnd);
1376 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1377 sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_BRST);
1383 sctp_cwnd_update_after_sack(struct sctp_tcb *stcb,
1384 struct sctp_association *asoc,
1385 int accum_moved, int reneged_all, int will_exit)
1387 /* Passing a zero argument in last disables the rtcc algorithm */
1388 sctp_cwnd_update_after_sack_common(stcb, asoc, accum_moved, reneged_all, will_exit, 0);
1392 sctp_cwnd_update_after_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
1393 int in_window, int num_pkt_lost)
1395 /* Passing a zero argument in last disables the rtcc algorithm */
1396 sctp_cwnd_update_after_ecn_echo_common(stcb, net, in_window, num_pkt_lost, 0);
1399 /* Here starts the RTCCVAR type CC invented by RRS which
1400 * is a slight mod to RFC2581. We reuse a common routine or
1401 * two since these algorithms are so close and need to
1405 sctp_cwnd_update_rtcc_after_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
1406 int in_window, int num_pkt_lost)
1408 sctp_cwnd_update_after_ecn_echo_common(stcb, net, in_window, num_pkt_lost, 1);
1413 void sctp_cwnd_update_rtcc_tsn_acknowledged(struct sctp_nets *net,
1414 struct sctp_tmit_chunk *tp1)
1416 net->cc_mod.rtcc.bw_bytes += tp1->send_size;
1420 sctp_cwnd_prepare_rtcc_net_for_sack(struct sctp_tcb *stcb SCTP_UNUSED,
1421 struct sctp_nets *net)
1423 if (net->cc_mod.rtcc.tls_needs_set > 0) {
1424 /* We had a bw measurment going on */
1425 struct timeval ltls;
1426 SCTP_GETPTIME_TIMEVAL(<ls);
1427 timevalsub(<ls, &net->cc_mod.rtcc.tls);
1428 net->cc_mod.rtcc.new_tot_time = (ltls.tv_sec * 1000000) + ltls.tv_usec;
1433 sctp_cwnd_new_rtcc_transmission_begins(struct sctp_tcb *stcb,
1434 struct sctp_nets *net)
1436 #if defined(__FreeBSD__) && !defined(__Userspace__)
1437 uint64_t vtag, probepoint;
1440 if (net->cc_mod.rtcc.lbw) {
1441 #if defined(__FreeBSD__) && !defined(__Userspace__)
1442 /* Clear the old bw.. we went to 0 in-flight */
1443 vtag = (net->rtt << 32) | (((uint32_t)(stcb->sctp_ep->sctp_lport)) << 16) |
1445 probepoint = (((uint64_t)net->cwnd) << 32);
1447 probepoint |= ((8 << 16) | 0);
1448 SDT_PROBE5(sctp, cwnd, net, rttvar,
1450 ((net->cc_mod.rtcc.lbw << 32) | 0),
1451 ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
1455 net->cc_mod.rtcc.lbw_rtt = 0;
1456 net->cc_mod.rtcc.cwnd_at_bw_set = 0;
1457 net->cc_mod.rtcc.lbw = 0;
1458 net->cc_mod.rtcc.bw_bytes_at_last_rttc = 0;
1459 net->cc_mod.rtcc.vol_reduce = 0;
1460 net->cc_mod.rtcc.bw_tot_time = 0;
1461 net->cc_mod.rtcc.bw_bytes = 0;
1462 net->cc_mod.rtcc.tls_needs_set = 0;
1463 if (net->cc_mod.rtcc.steady_step) {
1464 net->cc_mod.rtcc.vol_reduce = 0;
1465 net->cc_mod.rtcc.step_cnt = 0;
1466 net->cc_mod.rtcc.last_step_state = 0;
1468 if (net->cc_mod.rtcc.ret_from_eq) {
1469 /* less aggressive one - reset cwnd too */
1470 uint32_t cwnd_in_mtu, cwnd;
1472 cwnd_in_mtu = SCTP_BASE_SYSCTL(sctp_initial_cwnd);
1473 if (cwnd_in_mtu == 0) {
1474 /* Using 0 means that the value of RFC 4960 is used. */
1475 cwnd = min((net->mtu * 4), max((2 * net->mtu), SCTP_INITIAL_CWND));
1478 * We take the minimum of the burst limit and the
1479 * initial congestion window.
1481 if ((stcb->asoc.max_burst > 0) && (cwnd_in_mtu > stcb->asoc.max_burst))
1482 cwnd_in_mtu = stcb->asoc.max_burst;
1483 cwnd = (net->mtu - sizeof(struct sctphdr)) * cwnd_in_mtu;
1485 if (net->cwnd > cwnd) {
1486 /* Only set if we are not a timeout (i.e. down to 1 mtu) */
1494 sctp_set_rtcc_initial_cc_param(struct sctp_tcb *stcb,
1495 struct sctp_nets *net)
1497 #if defined(__FreeBSD__) && !defined(__Userspace__)
1498 uint64_t vtag, probepoint;
1501 sctp_set_initial_cc_param(stcb, net);
1502 stcb->asoc.use_precise_time = 1;
1503 #if defined(__FreeBSD__) && !defined(__Userspace__)
1504 probepoint = (((uint64_t)net->cwnd) << 32);
1505 probepoint |= ((9 << 16) | 0);
1506 vtag = (net->rtt << 32) |
1507 (((uint32_t)(stcb->sctp_ep->sctp_lport)) << 16) |
1509 SDT_PROBE5(sctp, cwnd, net, rttvar,
1516 net->cc_mod.rtcc.lbw_rtt = 0;
1517 net->cc_mod.rtcc.cwnd_at_bw_set = 0;
1518 net->cc_mod.rtcc.vol_reduce = 0;
1519 net->cc_mod.rtcc.lbw = 0;
1520 net->cc_mod.rtcc.vol_reduce = 0;
1521 net->cc_mod.rtcc.bw_bytes_at_last_rttc = 0;
1522 net->cc_mod.rtcc.bw_tot_time = 0;
1523 net->cc_mod.rtcc.bw_bytes = 0;
1524 net->cc_mod.rtcc.tls_needs_set = 0;
1525 net->cc_mod.rtcc.ret_from_eq = SCTP_BASE_SYSCTL(sctp_rttvar_eqret);
1526 net->cc_mod.rtcc.steady_step = SCTP_BASE_SYSCTL(sctp_steady_step);
1527 net->cc_mod.rtcc.use_dccc_ecn = SCTP_BASE_SYSCTL(sctp_use_dccc_ecn);
1528 net->cc_mod.rtcc.step_cnt = 0;
1529 net->cc_mod.rtcc.last_step_state = 0;
1535 sctp_cwnd_rtcc_socket_option(struct sctp_tcb *stcb, int setorget,
1536 struct sctp_cc_option *cc_opt)
1538 struct sctp_nets *net;
1539 if (setorget == 1) {
1541 if (cc_opt->option == SCTP_CC_OPT_RTCC_SETMODE) {
1542 if ((cc_opt->aid_value.assoc_value != 0) &&
1543 (cc_opt->aid_value.assoc_value != 1)) {
1546 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1547 net->cc_mod.rtcc.ret_from_eq = cc_opt->aid_value.assoc_value;
1549 } else if (cc_opt->option == SCTP_CC_OPT_USE_DCCC_ECN) {
1550 if ((cc_opt->aid_value.assoc_value != 0) &&
1551 (cc_opt->aid_value.assoc_value != 1)) {
1554 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1555 net->cc_mod.rtcc.use_dccc_ecn = cc_opt->aid_value.assoc_value;
1557 } else if (cc_opt->option == SCTP_CC_OPT_STEADY_STEP) {
1558 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1559 net->cc_mod.rtcc.steady_step = cc_opt->aid_value.assoc_value;
1566 if (cc_opt->option == SCTP_CC_OPT_RTCC_SETMODE) {
1567 net = TAILQ_FIRST(&stcb->asoc.nets);
1571 cc_opt->aid_value.assoc_value = net->cc_mod.rtcc.ret_from_eq;
1572 } else if (cc_opt->option == SCTP_CC_OPT_USE_DCCC_ECN) {
1573 net = TAILQ_FIRST(&stcb->asoc.nets);
1577 cc_opt->aid_value.assoc_value = net->cc_mod.rtcc.use_dccc_ecn;
1578 } else if (cc_opt->option == SCTP_CC_OPT_STEADY_STEP) {
1579 net = TAILQ_FIRST(&stcb->asoc.nets);
1583 cc_opt->aid_value.assoc_value = net->cc_mod.rtcc.steady_step;
1592 sctp_cwnd_update_rtcc_packet_transmitted(struct sctp_tcb *stcb SCTP_UNUSED,
1593 struct sctp_nets *net)
1595 if (net->cc_mod.rtcc.tls_needs_set == 0) {
1596 SCTP_GETPTIME_TIMEVAL(&net->cc_mod.rtcc.tls);
1597 net->cc_mod.rtcc.tls_needs_set = 2;
1602 sctp_cwnd_update_rtcc_after_sack(struct sctp_tcb *stcb,
1603 struct sctp_association *asoc,
1604 int accum_moved, int reneged_all, int will_exit)
1606 /* Passing a one argument at the last enables the rtcc algorithm */
1607 sctp_cwnd_update_after_sack_common(stcb, asoc, accum_moved, reneged_all, will_exit, 1);
1611 sctp_rtt_rtcc_calculated(struct sctp_tcb *stcb SCTP_UNUSED,
1612 struct sctp_nets *net,
1613 struct timeval *now SCTP_UNUSED)
1615 net->cc_mod.rtcc.rtt_set_this_sack = 1;
1618 /* Here starts Sally Floyds HS-TCP */
1620 struct sctp_hs_raise_drop {
1623 int8_t drop_percent;
1626 #define SCTP_HS_TABLE_SIZE 73
1628 static const struct sctp_hs_raise_drop sctp_cwnd_adjust[SCTP_HS_TABLE_SIZE] = {
1629 {38, 1, 50}, /* 0 */
1630 {118, 2, 44}, /* 1 */
1631 {221, 3, 41}, /* 2 */
1632 {347, 4, 38}, /* 3 */
1633 {495, 5, 37}, /* 4 */
1634 {663, 6, 35}, /* 5 */
1635 {851, 7, 34}, /* 6 */
1636 {1058, 8, 33}, /* 7 */
1637 {1284, 9, 32}, /* 8 */
1638 {1529, 10, 31}, /* 9 */
1639 {1793, 11, 30}, /* 10 */
1640 {2076, 12, 29}, /* 11 */
1641 {2378, 13, 28}, /* 12 */
1642 {2699, 14, 28}, /* 13 */
1643 {3039, 15, 27}, /* 14 */
1644 {3399, 16, 27}, /* 15 */
1645 {3778, 17, 26}, /* 16 */
1646 {4177, 18, 26}, /* 17 */
1647 {4596, 19, 25}, /* 18 */
1648 {5036, 20, 25}, /* 19 */
1649 {5497, 21, 24}, /* 20 */
1650 {5979, 22, 24}, /* 21 */
1651 {6483, 23, 23}, /* 22 */
1652 {7009, 24, 23}, /* 23 */
1653 {7558, 25, 22}, /* 24 */
1654 {8130, 26, 22}, /* 25 */
1655 {8726, 27, 22}, /* 26 */
1656 {9346, 28, 21}, /* 27 */
1657 {9991, 29, 21}, /* 28 */
1658 {10661, 30, 21}, /* 29 */
1659 {11358, 31, 20}, /* 30 */
1660 {12082, 32, 20}, /* 31 */
1661 {12834, 33, 20}, /* 32 */
1662 {13614, 34, 19}, /* 33 */
1663 {14424, 35, 19}, /* 34 */
1664 {15265, 36, 19}, /* 35 */
1665 {16137, 37, 19}, /* 36 */
1666 {17042, 38, 18}, /* 37 */
1667 {17981, 39, 18}, /* 38 */
1668 {18955, 40, 18}, /* 39 */
1669 {19965, 41, 17}, /* 40 */
1670 {21013, 42, 17}, /* 41 */
1671 {22101, 43, 17}, /* 42 */
1672 {23230, 44, 17}, /* 43 */
1673 {24402, 45, 16}, /* 44 */
1674 {25618, 46, 16}, /* 45 */
1675 {26881, 47, 16}, /* 46 */
1676 {28193, 48, 16}, /* 47 */
1677 {29557, 49, 15}, /* 48 */
1678 {30975, 50, 15}, /* 49 */
1679 {32450, 51, 15}, /* 50 */
1680 {33986, 52, 15}, /* 51 */
1681 {35586, 53, 14}, /* 52 */
1682 {37253, 54, 14}, /* 53 */
1683 {38992, 55, 14}, /* 54 */
1684 {40808, 56, 14}, /* 55 */
1685 {42707, 57, 13}, /* 56 */
1686 {44694, 58, 13}, /* 57 */
1687 {46776, 59, 13}, /* 58 */
1688 {48961, 60, 13}, /* 59 */
1689 {51258, 61, 13}, /* 60 */
1690 {53677, 62, 12}, /* 61 */
1691 {56230, 63, 12}, /* 62 */
1692 {58932, 64, 12}, /* 63 */
1693 {61799, 65, 12}, /* 64 */
1694 {64851, 66, 11}, /* 65 */
1695 {68113, 67, 11}, /* 66 */
1696 {71617, 68, 11}, /* 67 */
1697 {75401, 69, 10}, /* 68 */
1698 {79517, 70, 10}, /* 69 */
1699 {84035, 71, 10}, /* 70 */
1700 {89053, 72, 10}, /* 71 */
1701 {94717, 73, 9} /* 72 */
1705 sctp_hs_cwnd_increase(struct sctp_tcb *stcb, struct sctp_nets *net)
1707 int cur_val, i, indx, incr;
1708 int old_cwnd = net->cwnd;
1710 cur_val = net->cwnd >> 10;
1711 indx = SCTP_HS_TABLE_SIZE - 1;
1713 if (cur_val < sctp_cwnd_adjust[0].cwnd) {
1715 if (net->net_ack > net->mtu) {
1716 net->cwnd += net->mtu;
1718 net->cwnd += net->net_ack;
1721 for (i = net->last_hs_used; i < SCTP_HS_TABLE_SIZE; i++) {
1722 if (cur_val < sctp_cwnd_adjust[i].cwnd) {
1727 net->last_hs_used = indx;
1728 incr = (((int32_t)sctp_cwnd_adjust[indx].increase) << 10);
1731 sctp_enforce_cwnd_limit(&stcb->asoc, net);
1732 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1733 sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_SS);
1738 sctp_hs_cwnd_decrease(struct sctp_tcb *stcb, struct sctp_nets *net)
1740 int cur_val, i, indx;
1741 int old_cwnd = net->cwnd;
1743 cur_val = net->cwnd >> 10;
1744 if (cur_val < sctp_cwnd_adjust[0].cwnd) {
1746 net->ssthresh = net->cwnd / 2;
1747 if (net->ssthresh < (net->mtu * 2)) {
1748 net->ssthresh = 2 * net->mtu;
1750 net->cwnd = net->ssthresh;
1752 /* drop by the proper amount */
1753 net->ssthresh = net->cwnd - (int)((net->cwnd / 100) *
1754 (int32_t)sctp_cwnd_adjust[net->last_hs_used].drop_percent);
1755 net->cwnd = net->ssthresh;
1756 /* now where are we */
1757 indx = net->last_hs_used;
1758 cur_val = net->cwnd >> 10;
1759 /* reset where we are in the table */
1760 if (cur_val < sctp_cwnd_adjust[0].cwnd) {
1761 /* feel out of hs */
1762 net->last_hs_used = 0;
1764 for (i = indx; i >= 1; i--) {
1765 if (cur_val > sctp_cwnd_adjust[i - 1].cwnd) {
1769 net->last_hs_used = indx;
1772 sctp_enforce_cwnd_limit(&stcb->asoc, net);
1773 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1774 sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_FR);
1779 sctp_hs_cwnd_update_after_fr(struct sctp_tcb *stcb,
1780 struct sctp_association *asoc)
1782 struct sctp_nets *net;
1784 * CMT fast recovery code. Need to debug. ((sctp_cmt_on_off > 0) &&
1785 * (net->fast_retran_loss_recovery == 0)))
1787 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
1788 if ((asoc->fast_retran_loss_recovery == 0) ||
1789 (asoc->sctp_cmt_on_off > 0)) {
1790 /* out of a RFC2582 Fast recovery window? */
1791 if (net->net_ack > 0) {
1793 * per section 7.2.3, are there any
1794 * destinations that had a fast retransmit
1795 * to them. If so what we need to do is
1796 * adjust ssthresh and cwnd.
1798 struct sctp_tmit_chunk *lchk;
1800 sctp_hs_cwnd_decrease(stcb, net);
1802 lchk = TAILQ_FIRST(&asoc->send_queue);
1804 net->partial_bytes_acked = 0;
1805 /* Turn on fast recovery window */
1806 asoc->fast_retran_loss_recovery = 1;
1808 /* Mark end of the window */
1809 asoc->fast_recovery_tsn = asoc->sending_seq - 1;
1811 asoc->fast_recovery_tsn = lchk->rec.data.tsn - 1;
1815 * CMT fast recovery -- per destination
1816 * recovery variable.
1818 net->fast_retran_loss_recovery = 1;
1821 /* Mark end of the window */
1822 net->fast_recovery_tsn = asoc->sending_seq - 1;
1824 net->fast_recovery_tsn = lchk->rec.data.tsn - 1;
1827 sctp_timer_stop(SCTP_TIMER_TYPE_SEND,
1828 stcb->sctp_ep, stcb, net,
1829 SCTP_FROM_SCTP_CC_FUNCTIONS + SCTP_LOC_2);
1830 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
1831 stcb->sctp_ep, stcb, net);
1833 } else if (net->net_ack > 0) {
1835 * Mark a peg that we WOULD have done a cwnd
1836 * reduction but RFC2582 prevented this action.
1838 SCTP_STAT_INCR(sctps_fastretransinrtt);
1844 sctp_hs_cwnd_update_after_sack(struct sctp_tcb *stcb,
1845 struct sctp_association *asoc,
1846 int accum_moved, int reneged_all SCTP_UNUSED, int will_exit)
1848 struct sctp_nets *net;
1849 /******************************/
1850 /* update cwnd and Early FR */
1851 /******************************/
1852 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
1854 #ifdef JANA_CMT_FAST_RECOVERY
1856 * CMT fast recovery code. Need to debug.
1858 if (net->fast_retran_loss_recovery && net->new_pseudo_cumack) {
1859 if (SCTP_TSN_GE(asoc->last_acked_seq, net->fast_recovery_tsn) ||
1860 SCTP_TSN_GE(net->pseudo_cumack,net->fast_recovery_tsn)) {
1861 net->will_exit_fast_recovery = 1;
1865 /* if nothing was acked on this destination skip it */
1866 if (net->net_ack == 0) {
1867 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
1868 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FROM_SACK);
1872 #ifdef JANA_CMT_FAST_RECOVERY
1873 /* CMT fast recovery code
1876 if (sctp_cmt_on_off > 0 && net->fast_retran_loss_recovery && net->will_exit_fast_recovery == 0) {
1879 else if (sctp_cmt_on_off == 0 && asoc->fast_retran_loss_recovery && will_exit == 0) {
1883 if (asoc->fast_retran_loss_recovery &&
1885 (asoc->sctp_cmt_on_off == 0)) {
1887 * If we are in loss recovery we skip any cwnd
1893 * CMT: CUC algorithm. Update cwnd if pseudo-cumack has
1897 ((asoc->sctp_cmt_on_off > 0) && net->new_pseudo_cumack)) {
1898 /* If the cumulative ack moved we can proceed */
1899 if (net->cwnd <= net->ssthresh) {
1900 /* We are in slow start */
1901 if (net->flight_size + net->net_ack >= net->cwnd) {
1902 sctp_hs_cwnd_increase(stcb, net);
1904 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
1905 sctp_log_cwnd(stcb, net, net->net_ack,
1906 SCTP_CWND_LOG_NOADV_SS);
1910 /* We are in congestion avoidance */
1911 net->partial_bytes_acked += net->net_ack;
1912 if ((net->flight_size + net->net_ack >= net->cwnd) &&
1913 (net->partial_bytes_acked >= net->cwnd)) {
1914 net->partial_bytes_acked -= net->cwnd;
1915 net->cwnd += net->mtu;
1916 sctp_enforce_cwnd_limit(asoc, net);
1917 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1918 sctp_log_cwnd(stcb, net, net->mtu,
1919 SCTP_CWND_LOG_FROM_CA);
1922 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
1923 sctp_log_cwnd(stcb, net, net->net_ack,
1924 SCTP_CWND_LOG_NOADV_CA);
1929 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
1930 sctp_log_cwnd(stcb, net, net->mtu,
1931 SCTP_CWND_LOG_NO_CUMACK);
1939 * H-TCP congestion control. The algorithm is detailed in:
1940 * R.N.Shorten, D.J.Leith:
1941 * "H-TCP: TCP for high-speed and long-distance networks"
1942 * Proc. PFLDnet, Argonne, 2004.
1943 * http://www.hamilton.ie/net/htcp3.pdf
1947 static int use_rtt_scaling = 1;
1948 static int use_bandwidth_switch = 1;
1951 between(uint32_t seq1, uint32_t seq2, uint32_t seq3)
1953 return (seq3 - seq2 >= seq1 - seq2);
1956 static inline uint32_t
1957 htcp_cong_time(struct htcp *ca)
1959 return (sctp_get_tick_count() - ca->last_cong);
1962 static inline uint32_t
1963 htcp_ccount(struct htcp *ca)
1965 return (ca->minRTT == 0 ? htcp_cong_time(ca) : htcp_cong_time(ca)/ca->minRTT);
1969 htcp_reset(struct htcp *ca)
1971 ca->undo_last_cong = ca->last_cong;
1972 ca->undo_maxRTT = ca->maxRTT;
1973 ca->undo_old_maxB = ca->old_maxB;
1974 ca->last_cong = sctp_get_tick_count();
1977 #ifdef SCTP_NOT_USED
1980 htcp_cwnd_undo(struct sctp_tcb *stcb, struct sctp_nets *net)
1982 net->cc_mod.htcp_ca.last_cong = net->cc_mod.htcp_ca.undo_last_cong;
1983 net->cc_mod.htcp_ca.maxRTT = net->cc_mod.htcp_ca.undo_maxRTT;
1984 net->cc_mod.htcp_ca.old_maxB = net->cc_mod.htcp_ca.undo_old_maxB;
1985 return (max(net->cwnd, ((net->ssthresh/net->mtu<<7)/net->cc_mod.htcp_ca.beta)*net->mtu));
1991 measure_rtt(struct sctp_nets *net)
1993 uint32_t srtt = net->lastsa>>SCTP_RTT_SHIFT;
1995 /* keep track of minimum RTT seen so far, minRTT is zero at first */
1996 if (net->cc_mod.htcp_ca.minRTT > srtt || !net->cc_mod.htcp_ca.minRTT)
1997 net->cc_mod.htcp_ca.minRTT = srtt;
2000 if (net->fast_retran_ip == 0 && net->ssthresh < 0xFFFF && htcp_ccount(&net->cc_mod.htcp_ca) > 3) {
2001 if (net->cc_mod.htcp_ca.maxRTT < net->cc_mod.htcp_ca.minRTT)
2002 net->cc_mod.htcp_ca.maxRTT = net->cc_mod.htcp_ca.minRTT;
2003 if (net->cc_mod.htcp_ca.maxRTT < srtt && srtt <= net->cc_mod.htcp_ca.maxRTT+sctp_msecs_to_ticks(20))
2004 net->cc_mod.htcp_ca.maxRTT = srtt;
2009 measure_achieved_throughput(struct sctp_nets *net)
2011 uint32_t now = sctp_get_tick_count();
2013 if (net->fast_retran_ip == 0)
2014 net->cc_mod.htcp_ca.bytes_acked = net->net_ack;
2016 if (!use_bandwidth_switch)
2019 /* achieved throughput calculations */
2020 /* JRS - not 100% sure of this statement */
2021 if (net->fast_retran_ip == 1) {
2022 net->cc_mod.htcp_ca.bytecount = 0;
2023 net->cc_mod.htcp_ca.lasttime = now;
2027 net->cc_mod.htcp_ca.bytecount += net->net_ack;
2028 if ((net->cc_mod.htcp_ca.bytecount >= net->cwnd - (((net->cc_mod.htcp_ca.alpha >> 7) ? (net->cc_mod.htcp_ca.alpha >> 7) : 1) * net->mtu)) &&
2029 (now - net->cc_mod.htcp_ca.lasttime >= net->cc_mod.htcp_ca.minRTT) &&
2030 (net->cc_mod.htcp_ca.minRTT > 0)) {
2031 uint32_t cur_Bi = net->cc_mod.htcp_ca.bytecount/net->mtu*hz/(now - net->cc_mod.htcp_ca.lasttime);
2033 if (htcp_ccount(&net->cc_mod.htcp_ca) <= 3) {
2034 /* just after backoff */
2035 net->cc_mod.htcp_ca.minB = net->cc_mod.htcp_ca.maxB = net->cc_mod.htcp_ca.Bi = cur_Bi;
2037 net->cc_mod.htcp_ca.Bi = (3*net->cc_mod.htcp_ca.Bi + cur_Bi)/4;
2038 if (net->cc_mod.htcp_ca.Bi > net->cc_mod.htcp_ca.maxB)
2039 net->cc_mod.htcp_ca.maxB = net->cc_mod.htcp_ca.Bi;
2040 if (net->cc_mod.htcp_ca.minB > net->cc_mod.htcp_ca.maxB)
2041 net->cc_mod.htcp_ca.minB = net->cc_mod.htcp_ca.maxB;
2043 net->cc_mod.htcp_ca.bytecount = 0;
2044 net->cc_mod.htcp_ca.lasttime = now;
2049 htcp_beta_update(struct htcp *ca, uint32_t minRTT, uint32_t maxRTT)
2051 if (use_bandwidth_switch) {
2052 uint32_t maxB = ca->maxB;
2053 uint32_t old_maxB = ca->old_maxB;
2054 ca->old_maxB = ca->maxB;
2056 if (!between(5*maxB, 4*old_maxB, 6*old_maxB)) {
2057 ca->beta = BETA_MIN;
2063 if (ca->modeswitch && minRTT > sctp_msecs_to_ticks(10) && maxRTT) {
2064 ca->beta = (minRTT<<7)/maxRTT;
2065 if (ca->beta < BETA_MIN)
2066 ca->beta = BETA_MIN;
2067 else if (ca->beta > BETA_MAX)
2068 ca->beta = BETA_MAX;
2070 ca->beta = BETA_MIN;
2076 htcp_alpha_update(struct htcp *ca)
2078 uint32_t minRTT = ca->minRTT;
2079 uint32_t factor = 1;
2080 uint32_t diff = htcp_cong_time(ca);
2082 if (diff > (uint32_t)hz) {
2084 factor = 1+ ( 10*diff + ((diff/2)*(diff/2)/hz))/hz;
2087 if (use_rtt_scaling && minRTT) {
2088 uint32_t scale = (hz<<3)/(10*minRTT);
2089 scale = min(max(scale, 1U<<2), 10U<<3); /* clamping ratio to interval [0.5,10]<<3 */
2090 factor = (factor<<3)/scale;
2095 ca->alpha = 2*factor*((1<<7)-ca->beta);
2097 ca->alpha = ALPHA_BASE;
2100 /* After we have the rtt data to calculate beta, we'd still prefer to wait one
2101 * rtt before we adjust our beta to ensure we are working from a consistent
2104 * This function should be called when we hit a congestion event since only at
2105 * that point do we really have a real sense of maxRTT (the queues en route
2106 * were getting just too full now).
2109 htcp_param_update(struct sctp_nets *net)
2111 uint32_t minRTT = net->cc_mod.htcp_ca.minRTT;
2112 uint32_t maxRTT = net->cc_mod.htcp_ca.maxRTT;
2114 htcp_beta_update(&net->cc_mod.htcp_ca, minRTT, maxRTT);
2115 htcp_alpha_update(&net->cc_mod.htcp_ca);
2117 /* add slowly fading memory for maxRTT to accommodate routing changes etc */
2118 if (minRTT > 0 && maxRTT > minRTT)
2119 net->cc_mod.htcp_ca.maxRTT = minRTT + ((maxRTT-minRTT)*95)/100;
2123 htcp_recalc_ssthresh(struct sctp_nets *net)
2125 htcp_param_update(net);
2126 return (max(((net->cwnd/net->mtu * net->cc_mod.htcp_ca.beta) >> 7)*net->mtu, 2U*net->mtu));
2130 htcp_cong_avoid(struct sctp_tcb *stcb, struct sctp_nets *net)
2133 * How to handle these functions?
2134 * if (!tcp_is_cwnd_limited(sk, in_flight)) RRS - good question.
2137 if (net->cwnd <= net->ssthresh) {
2138 /* We are in slow start */
2139 if (net->flight_size + net->net_ack >= net->cwnd) {
2140 if (net->net_ack > (net->mtu * SCTP_BASE_SYSCTL(sctp_L2_abc_variable))) {
2141 net->cwnd += (net->mtu * SCTP_BASE_SYSCTL(sctp_L2_abc_variable));
2142 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
2143 sctp_log_cwnd(stcb, net, net->mtu,
2144 SCTP_CWND_LOG_FROM_SS);
2148 net->cwnd += net->net_ack;
2149 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
2150 sctp_log_cwnd(stcb, net, net->net_ack,
2151 SCTP_CWND_LOG_FROM_SS);
2155 sctp_enforce_cwnd_limit(&stcb->asoc, net);
2157 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
2158 sctp_log_cwnd(stcb, net, net->net_ack,
2159 SCTP_CWND_LOG_NOADV_SS);
2165 /* In dangerous area, increase slowly.
2166 * In theory this is net->cwnd += alpha / net->cwnd
2168 /* What is snd_cwnd_cnt?? */
2169 if (((net->partial_bytes_acked/net->mtu * net->cc_mod.htcp_ca.alpha) >> 7)*net->mtu >= net->cwnd) {
2171 * Does SCTP have a cwnd clamp?
2172 * if (net->snd_cwnd < net->snd_cwnd_clamp) - Nope (RRS).
2174 net->cwnd += net->mtu;
2175 net->partial_bytes_acked = 0;
2176 sctp_enforce_cwnd_limit(&stcb->asoc, net);
2177 htcp_alpha_update(&net->cc_mod.htcp_ca);
2178 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
2179 sctp_log_cwnd(stcb, net, net->mtu,
2180 SCTP_CWND_LOG_FROM_CA);
2183 net->partial_bytes_acked += net->net_ack;
2184 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
2185 sctp_log_cwnd(stcb, net, net->net_ack,
2186 SCTP_CWND_LOG_NOADV_CA);
2190 net->cc_mod.htcp_ca.bytes_acked = net->mtu;
2194 #ifdef SCTP_NOT_USED
2195 /* Lower bound on congestion window. */
2197 htcp_min_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net)
2199 return (net->ssthresh);
2204 htcp_init(struct sctp_nets *net)
2206 memset(&net->cc_mod.htcp_ca, 0, sizeof(struct htcp));
2207 net->cc_mod.htcp_ca.alpha = ALPHA_BASE;
2208 net->cc_mod.htcp_ca.beta = BETA_MIN;
2209 net->cc_mod.htcp_ca.bytes_acked = net->mtu;
2210 net->cc_mod.htcp_ca.last_cong = sctp_get_tick_count();
2214 sctp_htcp_set_initial_cc_param(struct sctp_tcb *stcb, struct sctp_nets *net)
2217 * We take the max of the burst limit times a MTU or the
2218 * INITIAL_CWND. We then limit this to 4 MTU's of sending.
2220 net->cwnd = min((net->mtu * 4), max((2 * net->mtu), SCTP_INITIAL_CWND));
2221 net->ssthresh = stcb->asoc.peers_rwnd;
2222 sctp_enforce_cwnd_limit(&stcb->asoc, net);
2225 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE|SCTP_CWND_LOGGING_ENABLE)) {
2226 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_INITIALIZATION);
2231 sctp_htcp_cwnd_update_after_sack(struct sctp_tcb *stcb,
2232 struct sctp_association *asoc,
2233 int accum_moved, int reneged_all SCTP_UNUSED, int will_exit)
2235 struct sctp_nets *net;
2237 /******************************/
2238 /* update cwnd and Early FR */
2239 /******************************/
2240 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
2242 #ifdef JANA_CMT_FAST_RECOVERY
2244 * CMT fast recovery code. Need to debug.
2246 if (net->fast_retran_loss_recovery && net->new_pseudo_cumack) {
2247 if (SCTP_TSN_GE(asoc->last_acked_seq, net->fast_recovery_tsn) ||
2248 SCTP_TSN_GE(net->pseudo_cumack,net->fast_recovery_tsn)) {
2249 net->will_exit_fast_recovery = 1;
2253 /* if nothing was acked on this destination skip it */
2254 if (net->net_ack == 0) {
2255 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
2256 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FROM_SACK);
2260 #ifdef JANA_CMT_FAST_RECOVERY
2261 /* CMT fast recovery code
2264 if (sctp_cmt_on_off > 0 && net->fast_retran_loss_recovery && net->will_exit_fast_recovery == 0) {
2267 else if (sctp_cmt_on_off == 0 && asoc->fast_retran_loss_recovery && will_exit == 0) {
2271 if (asoc->fast_retran_loss_recovery &&
2273 (asoc->sctp_cmt_on_off == 0)) {
2275 * If we are in loss recovery we skip any cwnd
2281 * CMT: CUC algorithm. Update cwnd if pseudo-cumack has
2285 ((asoc->sctp_cmt_on_off > 0) && net->new_pseudo_cumack)) {
2286 htcp_cong_avoid(stcb, net);
2287 measure_achieved_throughput(net);
2289 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
2290 sctp_log_cwnd(stcb, net, net->mtu,
2291 SCTP_CWND_LOG_NO_CUMACK);
2298 sctp_htcp_cwnd_update_after_fr(struct sctp_tcb *stcb,
2299 struct sctp_association *asoc)
2301 struct sctp_nets *net;
2303 * CMT fast recovery code. Need to debug. ((sctp_cmt_on_off > 0) &&
2304 * (net->fast_retran_loss_recovery == 0)))
2306 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
2307 if ((asoc->fast_retran_loss_recovery == 0) ||
2308 (asoc->sctp_cmt_on_off > 0)) {
2309 /* out of a RFC2582 Fast recovery window? */
2310 if (net->net_ack > 0) {
2312 * per section 7.2.3, are there any
2313 * destinations that had a fast retransmit
2314 * to them. If so what we need to do is
2315 * adjust ssthresh and cwnd.
2317 struct sctp_tmit_chunk *lchk;
2318 int old_cwnd = net->cwnd;
2320 /* JRS - reset as if state were changed */
2321 htcp_reset(&net->cc_mod.htcp_ca);
2322 net->ssthresh = htcp_recalc_ssthresh(net);
2323 net->cwnd = net->ssthresh;
2324 sctp_enforce_cwnd_limit(asoc, net);
2325 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
2326 sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd),
2327 SCTP_CWND_LOG_FROM_FR);
2329 lchk = TAILQ_FIRST(&asoc->send_queue);
2331 net->partial_bytes_acked = 0;
2332 /* Turn on fast recovery window */
2333 asoc->fast_retran_loss_recovery = 1;
2335 /* Mark end of the window */
2336 asoc->fast_recovery_tsn = asoc->sending_seq - 1;
2338 asoc->fast_recovery_tsn = lchk->rec.data.tsn - 1;
2342 * CMT fast recovery -- per destination
2343 * recovery variable.
2345 net->fast_retran_loss_recovery = 1;
2348 /* Mark end of the window */
2349 net->fast_recovery_tsn = asoc->sending_seq - 1;
2351 net->fast_recovery_tsn = lchk->rec.data.tsn - 1;
2354 sctp_timer_stop(SCTP_TIMER_TYPE_SEND,
2355 stcb->sctp_ep, stcb, net,
2356 SCTP_FROM_SCTP_CC_FUNCTIONS + SCTP_LOC_3);
2357 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
2358 stcb->sctp_ep, stcb, net);
2360 } else if (net->net_ack > 0) {
2362 * Mark a peg that we WOULD have done a cwnd
2363 * reduction but RFC2582 prevented this action.
2365 SCTP_STAT_INCR(sctps_fastretransinrtt);
2371 sctp_htcp_cwnd_update_after_timeout(struct sctp_tcb *stcb,
2372 struct sctp_nets *net)
2374 int old_cwnd = net->cwnd;
2376 /* JRS - reset as if the state were being changed to timeout */
2377 htcp_reset(&net->cc_mod.htcp_ca);
2378 net->ssthresh = htcp_recalc_ssthresh(net);
2379 net->cwnd = net->mtu;
2380 net->partial_bytes_acked = 0;
2381 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
2382 sctp_log_cwnd(stcb, net, net->cwnd - old_cwnd, SCTP_CWND_LOG_FROM_RTX);
2387 sctp_htcp_cwnd_update_after_ecn_echo(struct sctp_tcb *stcb,
2388 struct sctp_nets *net, int in_window, int num_pkt_lost SCTP_UNUSED)
2391 old_cwnd = net->cwnd;
2393 /* JRS - reset hctp as if state changed */
2394 if (in_window == 0) {
2395 htcp_reset(&net->cc_mod.htcp_ca);
2396 SCTP_STAT_INCR(sctps_ecnereducedcwnd);
2397 net->ssthresh = htcp_recalc_ssthresh(net);
2398 if (net->ssthresh < net->mtu) {
2399 net->ssthresh = net->mtu;
2400 /* here back off the timer as well, to slow us down */
2403 net->cwnd = net->ssthresh;
2404 sctp_enforce_cwnd_limit(&stcb->asoc, net);
2405 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
2406 sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_SAT);
2411 const struct sctp_cc_functions sctp_cc_functions[] = {
2413 #if defined(_WIN32) && !defined(__MINGW32__)
2414 sctp_set_initial_cc_param,
2415 sctp_cwnd_update_after_sack,
2416 sctp_cwnd_update_exit_pf_common,
2417 sctp_cwnd_update_after_fr,
2418 sctp_cwnd_update_after_timeout,
2419 sctp_cwnd_update_after_ecn_echo,
2420 sctp_cwnd_update_after_packet_dropped,
2421 sctp_cwnd_update_after_output,
2423 .sctp_set_initial_cc_param = sctp_set_initial_cc_param,
2424 .sctp_cwnd_update_after_sack = sctp_cwnd_update_after_sack,
2425 .sctp_cwnd_update_exit_pf = sctp_cwnd_update_exit_pf_common,
2426 .sctp_cwnd_update_after_fr = sctp_cwnd_update_after_fr,
2427 .sctp_cwnd_update_after_timeout = sctp_cwnd_update_after_timeout,
2428 .sctp_cwnd_update_after_ecn_echo = sctp_cwnd_update_after_ecn_echo,
2429 .sctp_cwnd_update_after_packet_dropped = sctp_cwnd_update_after_packet_dropped,
2430 .sctp_cwnd_update_after_output = sctp_cwnd_update_after_output,
2434 #if defined(_WIN32) && !defined(__MINGW32__)
2435 sctp_set_initial_cc_param,
2436 sctp_hs_cwnd_update_after_sack,
2437 sctp_cwnd_update_exit_pf_common,
2438 sctp_hs_cwnd_update_after_fr,
2439 sctp_cwnd_update_after_timeout,
2440 sctp_cwnd_update_after_ecn_echo,
2441 sctp_cwnd_update_after_packet_dropped,
2442 sctp_cwnd_update_after_output,
2444 .sctp_set_initial_cc_param = sctp_set_initial_cc_param,
2445 .sctp_cwnd_update_after_sack = sctp_hs_cwnd_update_after_sack,
2446 .sctp_cwnd_update_exit_pf = sctp_cwnd_update_exit_pf_common,
2447 .sctp_cwnd_update_after_fr = sctp_hs_cwnd_update_after_fr,
2448 .sctp_cwnd_update_after_timeout = sctp_cwnd_update_after_timeout,
2449 .sctp_cwnd_update_after_ecn_echo = sctp_cwnd_update_after_ecn_echo,
2450 .sctp_cwnd_update_after_packet_dropped = sctp_cwnd_update_after_packet_dropped,
2451 .sctp_cwnd_update_after_output = sctp_cwnd_update_after_output,
2455 #if defined(_WIN32) && !defined(__MINGW32__)
2456 sctp_htcp_set_initial_cc_param,
2457 sctp_htcp_cwnd_update_after_sack,
2458 sctp_cwnd_update_exit_pf_common,
2459 sctp_htcp_cwnd_update_after_fr,
2460 sctp_htcp_cwnd_update_after_timeout,
2461 sctp_htcp_cwnd_update_after_ecn_echo,
2462 sctp_cwnd_update_after_packet_dropped,
2463 sctp_cwnd_update_after_output,
2465 .sctp_set_initial_cc_param = sctp_htcp_set_initial_cc_param,
2466 .sctp_cwnd_update_after_sack = sctp_htcp_cwnd_update_after_sack,
2467 .sctp_cwnd_update_exit_pf = sctp_cwnd_update_exit_pf_common,
2468 .sctp_cwnd_update_after_fr = sctp_htcp_cwnd_update_after_fr,
2469 .sctp_cwnd_update_after_timeout = sctp_htcp_cwnd_update_after_timeout,
2470 .sctp_cwnd_update_after_ecn_echo = sctp_htcp_cwnd_update_after_ecn_echo,
2471 .sctp_cwnd_update_after_packet_dropped = sctp_cwnd_update_after_packet_dropped,
2472 .sctp_cwnd_update_after_output = sctp_cwnd_update_after_output,
2476 #if defined(_WIN32) && !defined(__MINGW32__)
2477 sctp_set_rtcc_initial_cc_param,
2478 sctp_cwnd_update_rtcc_after_sack,
2479 sctp_cwnd_update_exit_pf_common,
2480 sctp_cwnd_update_after_fr,
2481 sctp_cwnd_update_after_timeout,
2482 sctp_cwnd_update_rtcc_after_ecn_echo,
2483 sctp_cwnd_update_after_packet_dropped,
2484 sctp_cwnd_update_after_output,
2485 sctp_cwnd_update_rtcc_packet_transmitted,
2486 sctp_cwnd_update_rtcc_tsn_acknowledged,
2487 sctp_cwnd_new_rtcc_transmission_begins,
2488 sctp_cwnd_prepare_rtcc_net_for_sack,
2489 sctp_cwnd_rtcc_socket_option,
2490 sctp_rtt_rtcc_calculated
2492 .sctp_set_initial_cc_param = sctp_set_rtcc_initial_cc_param,
2493 .sctp_cwnd_update_after_sack = sctp_cwnd_update_rtcc_after_sack,
2494 .sctp_cwnd_update_exit_pf = sctp_cwnd_update_exit_pf_common,
2495 .sctp_cwnd_update_after_fr = sctp_cwnd_update_after_fr,
2496 .sctp_cwnd_update_after_timeout = sctp_cwnd_update_after_timeout,
2497 .sctp_cwnd_update_after_ecn_echo = sctp_cwnd_update_rtcc_after_ecn_echo,
2498 .sctp_cwnd_update_after_packet_dropped = sctp_cwnd_update_after_packet_dropped,
2499 .sctp_cwnd_update_after_output = sctp_cwnd_update_after_output,
2500 .sctp_cwnd_update_packet_transmitted = sctp_cwnd_update_rtcc_packet_transmitted,
2501 .sctp_cwnd_update_tsn_acknowledged = sctp_cwnd_update_rtcc_tsn_acknowledged,
2502 .sctp_cwnd_new_transmission_begins = sctp_cwnd_new_rtcc_transmission_begins,
2503 .sctp_cwnd_prepare_net_for_sack = sctp_cwnd_prepare_rtcc_net_for_sack,
2504 .sctp_cwnd_socket_option = sctp_cwnd_rtcc_socket_option,
2505 .sctp_rtt_calculated = sctp_rtt_rtcc_calculated