]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/sctp_cc_functions.c
sctp: Add macros to assert on inp info lock state
[FreeBSD/FreeBSD.git] / sys / netinet / sctp_cc_functions.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
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.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * a) Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  *
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.
17  *
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.
21  *
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.
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <netinet/sctp_os.h>
39 #include <netinet/sctp_var.h>
40 #include <netinet/sctp_sysctl.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctp_header.h>
43 #include <netinet/sctputil.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_input.h>
46 #include <netinet/sctp_indata.h>
47 #include <netinet/sctp_uio.h>
48 #include <netinet/sctp_timer.h>
49 #include <netinet/sctp_auth.h>
50 #include <netinet/sctp_asconf.h>
51 #include <netinet/sctp_kdtrace.h>
52
53 #define SHIFT_MPTCP_MULTI_N 40
54 #define SHIFT_MPTCP_MULTI_Z 16
55 #define SHIFT_MPTCP_MULTI 8
56
57 static void
58 sctp_enforce_cwnd_limit(struct sctp_association *assoc, struct sctp_nets *net)
59 {
60         if ((assoc->max_cwnd > 0) &&
61             (net->cwnd > assoc->max_cwnd) &&
62             (net->cwnd > (net->mtu - sizeof(struct sctphdr)))) {
63                 net->cwnd = assoc->max_cwnd;
64                 if (net->cwnd < (net->mtu - sizeof(struct sctphdr))) {
65                         net->cwnd = net->mtu - sizeof(struct sctphdr);
66                 }
67         }
68 }
69
70 static void
71 sctp_set_initial_cc_param(struct sctp_tcb *stcb, struct sctp_nets *net)
72 {
73         struct sctp_association *assoc;
74         uint32_t cwnd_in_mtu;
75
76         assoc = &stcb->asoc;
77         cwnd_in_mtu = SCTP_BASE_SYSCTL(sctp_initial_cwnd);
78         if (cwnd_in_mtu == 0) {
79                 /* Using 0 means that the value of RFC 4960 is used. */
80                 net->cwnd = min((net->mtu * 4), max((2 * net->mtu), SCTP_INITIAL_CWND));
81         } else {
82                 /*
83                  * We take the minimum of the burst limit and the initial
84                  * congestion window.
85                  */
86                 if ((assoc->max_burst > 0) && (cwnd_in_mtu > assoc->max_burst))
87                         cwnd_in_mtu = assoc->max_burst;
88                 net->cwnd = (net->mtu - sizeof(struct sctphdr)) * cwnd_in_mtu;
89         }
90         if ((stcb->asoc.sctp_cmt_on_off == SCTP_CMT_RPV1) ||
91             (stcb->asoc.sctp_cmt_on_off == SCTP_CMT_RPV2)) {
92                 /* In case of resource pooling initialize appropriately */
93                 net->cwnd /= assoc->numnets;
94                 if (net->cwnd < (net->mtu - sizeof(struct sctphdr))) {
95                         net->cwnd = net->mtu - sizeof(struct sctphdr);
96                 }
97         }
98         sctp_enforce_cwnd_limit(assoc, net);
99         net->ssthresh = assoc->peers_rwnd;
100         SDT_PROBE5(sctp, cwnd, net, init,
101             stcb->asoc.my_vtag, ((stcb->sctp_ep->sctp_lport << 16) | (stcb->rport)), net,
102             0, net->cwnd);
103         if (SCTP_BASE_SYSCTL(sctp_logging_level) &
104             (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
105                 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_INITIALIZATION);
106         }
107 }
108
109 static void
110 sctp_cwnd_update_after_fr(struct sctp_tcb *stcb,
111     struct sctp_association *asoc)
112 {
113         struct sctp_nets *net;
114         uint32_t t_ssthresh, t_cwnd;
115         uint64_t t_ucwnd_sbw;
116
117         /* MT FIXME: Don't compute this over and over again */
118         t_ssthresh = 0;
119         t_cwnd = 0;
120         t_ucwnd_sbw = 0;
121         if ((asoc->sctp_cmt_on_off == SCTP_CMT_RPV1) ||
122             (asoc->sctp_cmt_on_off == SCTP_CMT_RPV2)) {
123                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
124                         t_ssthresh += net->ssthresh;
125                         t_cwnd += net->cwnd;
126                         if (net->lastsa > 0) {
127                                 t_ucwnd_sbw += (uint64_t)net->cwnd / (uint64_t)net->lastsa;
128                         }
129                 }
130                 if (t_ucwnd_sbw == 0) {
131                         t_ucwnd_sbw = 1;
132                 }
133         }
134
135         /*-
136          * CMT fast recovery code. Need to debug. ((sctp_cmt_on_off > 0) &&
137          * (net->fast_retran_loss_recovery == 0)))
138          */
139         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
140                 if ((asoc->fast_retran_loss_recovery == 0) ||
141                     (asoc->sctp_cmt_on_off > 0)) {
142                         /* out of a RFC2582 Fast recovery window? */
143                         if (net->net_ack > 0) {
144                                 /*
145                                  * per section 7.2.3, are there any
146                                  * destinations that had a fast retransmit
147                                  * to them. If so what we need to do is
148                                  * adjust ssthresh and cwnd.
149                                  */
150                                 struct sctp_tmit_chunk *lchk;
151                                 int old_cwnd = net->cwnd;
152
153                                 if ((asoc->sctp_cmt_on_off == SCTP_CMT_RPV1) ||
154                                     (asoc->sctp_cmt_on_off == SCTP_CMT_RPV2)) {
155                                         if (asoc->sctp_cmt_on_off == SCTP_CMT_RPV1) {
156                                                 net->ssthresh = (uint32_t)(((uint64_t)4 *
157                                                     (uint64_t)net->mtu *
158                                                     (uint64_t)net->ssthresh) /
159                                                     (uint64_t)t_ssthresh);
160                                         }
161                                         if (asoc->sctp_cmt_on_off == SCTP_CMT_RPV2) {
162                                                 uint32_t srtt;
163
164                                                 srtt = net->lastsa;
165                                                 /*
166                                                  * lastsa>>3;  we don't need
167                                                  * to devide ...
168                                                  */
169                                                 if (srtt == 0) {
170                                                         srtt = 1;
171                                                 }
172                                                 /*
173                                                  * Short Version => Equal to
174                                                  * Contel Version MBe
175                                                  */
176                                                 net->ssthresh = (uint32_t)(((uint64_t)4 *
177                                                     (uint64_t)net->mtu *
178                                                     (uint64_t)net->cwnd) /
179                                                     ((uint64_t)srtt *
180                                                     t_ucwnd_sbw));
181                                                  /* INCREASE FACTOR */ ;
182                                         }
183                                         if ((net->cwnd > t_cwnd / 2) &&
184                                             (net->ssthresh < net->cwnd - t_cwnd / 2)) {
185                                                 net->ssthresh = net->cwnd - t_cwnd / 2;
186                                         }
187                                         if (net->ssthresh < net->mtu) {
188                                                 net->ssthresh = net->mtu;
189                                         }
190                                 } else {
191                                         net->ssthresh = net->cwnd / 2;
192                                         if (net->ssthresh < (net->mtu * 2)) {
193                                                 net->ssthresh = 2 * net->mtu;
194                                         }
195                                 }
196                                 net->cwnd = net->ssthresh;
197                                 sctp_enforce_cwnd_limit(asoc, net);
198                                 SDT_PROBE5(sctp, cwnd, net, fr,
199                                     stcb->asoc.my_vtag, ((stcb->sctp_ep->sctp_lport << 16) | (stcb->rport)), net,
200                                     old_cwnd, net->cwnd);
201                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
202                                         sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd),
203                                             SCTP_CWND_LOG_FROM_FR);
204                                 }
205                                 lchk = TAILQ_FIRST(&asoc->send_queue);
206
207                                 net->partial_bytes_acked = 0;
208                                 /* Turn on fast recovery window */
209                                 asoc->fast_retran_loss_recovery = 1;
210                                 if (lchk == NULL) {
211                                         /* Mark end of the window */
212                                         asoc->fast_recovery_tsn = asoc->sending_seq - 1;
213                                 } else {
214                                         asoc->fast_recovery_tsn = lchk->rec.data.tsn - 1;
215                                 }
216
217                                 /*
218                                  * CMT fast recovery -- per destination
219                                  * recovery variable.
220                                  */
221                                 net->fast_retran_loss_recovery = 1;
222
223                                 if (lchk == NULL) {
224                                         /* Mark end of the window */
225                                         net->fast_recovery_tsn = asoc->sending_seq - 1;
226                                 } else {
227                                         net->fast_recovery_tsn = lchk->rec.data.tsn - 1;
228                                 }
229
230                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND,
231                                     stcb->sctp_ep, stcb, net,
232                                     SCTP_FROM_SCTP_CC_FUNCTIONS + SCTP_LOC_1);
233                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
234                                     stcb->sctp_ep, stcb, net);
235                         }
236                 } else if (net->net_ack > 0) {
237                         /*
238                          * Mark a peg that we WOULD have done a cwnd
239                          * reduction but RFC2582 prevented this action.
240                          */
241                         SCTP_STAT_INCR(sctps_fastretransinrtt);
242                 }
243         }
244 }
245
246 /* Defines for instantaneous bw decisions */
247 #define SCTP_INST_LOOSING 1     /* Losing to other flows */
248 #define SCTP_INST_NEUTRAL 2     /* Neutral, no indication */
249 #define SCTP_INST_GAINING 3     /* Gaining, step down possible */
250
251 static int
252 cc_bw_same(struct sctp_tcb *stcb, struct sctp_nets *net, uint64_t nbw,
253     uint64_t rtt_offset, uint64_t vtag, uint8_t inst_ind)
254 {
255         uint64_t oth, probepoint;
256
257         probepoint = (((uint64_t)net->cwnd) << 32);
258         if (net->rtt > net->cc_mod.rtcc.lbw_rtt + rtt_offset) {
259                 /*
260                  * rtt increased we don't update bw.. so we don't update the
261                  * rtt either.
262                  */
263                 /* Probe point 5 */
264                 probepoint |= ((5 << 16) | 1);
265                 SDT_PROBE5(sctp, cwnd, net, rttvar,
266                     vtag,
267                     ((net->cc_mod.rtcc.lbw << 32) | nbw),
268                     ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
269                     net->flight_size,
270                     probepoint);
271                 if ((net->cc_mod.rtcc.steady_step) && (inst_ind != SCTP_INST_LOOSING)) {
272                         if (net->cc_mod.rtcc.last_step_state == 5)
273                                 net->cc_mod.rtcc.step_cnt++;
274                         else
275                                 net->cc_mod.rtcc.step_cnt = 1;
276                         net->cc_mod.rtcc.last_step_state = 5;
277                         if ((net->cc_mod.rtcc.step_cnt == net->cc_mod.rtcc.steady_step) ||
278                             ((net->cc_mod.rtcc.step_cnt > net->cc_mod.rtcc.steady_step) &&
279                             ((net->cc_mod.rtcc.step_cnt % net->cc_mod.rtcc.steady_step) == 0))) {
280                                 /* Try a step down */
281                                 oth = net->cc_mod.rtcc.vol_reduce;
282                                 oth <<= 16;
283                                 oth |= net->cc_mod.rtcc.step_cnt;
284                                 oth <<= 16;
285                                 oth |= net->cc_mod.rtcc.last_step_state;
286                                 SDT_PROBE5(sctp, cwnd, net, rttstep,
287                                     vtag,
288                                     ((net->cc_mod.rtcc.lbw << 32) | nbw),
289                                     ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
290                                     oth,
291                                     probepoint);
292                                 if (net->cwnd > (4 * net->mtu)) {
293                                         net->cwnd -= net->mtu;
294                                         net->cc_mod.rtcc.vol_reduce++;
295                                 } else {
296                                         net->cc_mod.rtcc.step_cnt = 0;
297                                 }
298                         }
299                 }
300                 return (1);
301         }
302         if (net->rtt < net->cc_mod.rtcc.lbw_rtt - rtt_offset) {
303                 /*
304                  * rtt decreased, there could be more room. we update both
305                  * the bw and the rtt here to lock this in as a good step
306                  * down.
307                  */
308                 /* Probe point 6 */
309                 probepoint |= ((6 << 16) | 0);
310                 SDT_PROBE5(sctp, cwnd, net, rttvar,
311                     vtag,
312                     ((net->cc_mod.rtcc.lbw << 32) | nbw),
313                     ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
314                     net->flight_size,
315                     probepoint);
316                 if (net->cc_mod.rtcc.steady_step) {
317                         oth = net->cc_mod.rtcc.vol_reduce;
318                         oth <<= 16;
319                         oth |= net->cc_mod.rtcc.step_cnt;
320                         oth <<= 16;
321                         oth |= net->cc_mod.rtcc.last_step_state;
322                         SDT_PROBE5(sctp, cwnd, net, rttstep,
323                             vtag,
324                             ((net->cc_mod.rtcc.lbw << 32) | nbw),
325                             ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
326                             oth,
327                             probepoint);
328                         if ((net->cc_mod.rtcc.last_step_state == 5) &&
329                             (net->cc_mod.rtcc.step_cnt > net->cc_mod.rtcc.steady_step)) {
330                                 /* Step down worked */
331                                 net->cc_mod.rtcc.step_cnt = 0;
332                                 return (1);
333                         } else {
334                                 net->cc_mod.rtcc.last_step_state = 6;
335                                 net->cc_mod.rtcc.step_cnt = 0;
336                         }
337                 }
338                 net->cc_mod.rtcc.lbw = nbw;
339                 net->cc_mod.rtcc.lbw_rtt = net->rtt;
340                 net->cc_mod.rtcc.cwnd_at_bw_set = net->cwnd;
341                 if (inst_ind == SCTP_INST_GAINING)
342                         return (1);
343                 else if (inst_ind == SCTP_INST_NEUTRAL)
344                         return (1);
345                 else
346                         return (0);
347         }
348         /*
349          * Ok bw and rtt remained the same .. no update to any
350          */
351         /* Probe point 7 */
352         probepoint |= ((7 << 16) | net->cc_mod.rtcc.ret_from_eq);
353         SDT_PROBE5(sctp, cwnd, net, rttvar,
354             vtag,
355             ((net->cc_mod.rtcc.lbw << 32) | nbw),
356             ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
357             net->flight_size,
358             probepoint);
359         if ((net->cc_mod.rtcc.steady_step) && (inst_ind != SCTP_INST_LOOSING)) {
360                 if (net->cc_mod.rtcc.last_step_state == 5)
361                         net->cc_mod.rtcc.step_cnt++;
362                 else
363                         net->cc_mod.rtcc.step_cnt = 1;
364                 net->cc_mod.rtcc.last_step_state = 5;
365                 if ((net->cc_mod.rtcc.step_cnt == net->cc_mod.rtcc.steady_step) ||
366                     ((net->cc_mod.rtcc.step_cnt > net->cc_mod.rtcc.steady_step) &&
367                     ((net->cc_mod.rtcc.step_cnt % net->cc_mod.rtcc.steady_step) == 0))) {
368                         /* Try a step down */
369                         if (net->cwnd > (4 * net->mtu)) {
370                                 net->cwnd -= net->mtu;
371                                 net->cc_mod.rtcc.vol_reduce++;
372                                 return (1);
373                         } else {
374                                 net->cc_mod.rtcc.step_cnt = 0;
375                         }
376                 }
377         }
378         if (inst_ind == SCTP_INST_GAINING)
379                 return (1);
380         else if (inst_ind == SCTP_INST_NEUTRAL)
381                 return (1);
382         else
383                 return ((int)net->cc_mod.rtcc.ret_from_eq);
384 }
385
386 static int
387 cc_bw_decrease(struct sctp_tcb *stcb, struct sctp_nets *net, uint64_t nbw, uint64_t rtt_offset,
388     uint64_t vtag, uint8_t inst_ind)
389 {
390         uint64_t oth, probepoint;
391
392         /* Bandwidth decreased. */
393         probepoint = (((uint64_t)net->cwnd) << 32);
394         if (net->rtt > net->cc_mod.rtcc.lbw_rtt + rtt_offset) {
395                 /* rtt increased */
396                 /* Did we add more */
397                 if ((net->cwnd > net->cc_mod.rtcc.cwnd_at_bw_set) &&
398                     (inst_ind != SCTP_INST_LOOSING)) {
399                         /* We caused it maybe.. back off? */
400                         /* PROBE POINT 1 */
401                         probepoint |= ((1 << 16) | 1);
402                         SDT_PROBE5(sctp, cwnd, net, rttvar,
403                             vtag,
404                             ((net->cc_mod.rtcc.lbw << 32) | nbw),
405                             ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
406                             net->flight_size,
407                             probepoint);
408                         if (net->cc_mod.rtcc.ret_from_eq) {
409                                 /*
410                                  * Switch over to CA if we are less
411                                  * aggressive
412                                  */
413                                 net->ssthresh = net->cwnd - 1;
414                                 net->partial_bytes_acked = 0;
415                         }
416                         return (1);
417                 }
418                 /* Probe point 2 */
419                 probepoint |= ((2 << 16) | 0);
420                 SDT_PROBE5(sctp, cwnd, net, rttvar,
421                     vtag,
422                     ((net->cc_mod.rtcc.lbw << 32) | nbw),
423                     ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
424                     net->flight_size,
425                     probepoint);
426                 /* Someone else - fight for more? */
427                 if (net->cc_mod.rtcc.steady_step) {
428                         oth = net->cc_mod.rtcc.vol_reduce;
429                         oth <<= 16;
430                         oth |= net->cc_mod.rtcc.step_cnt;
431                         oth <<= 16;
432                         oth |= net->cc_mod.rtcc.last_step_state;
433                         SDT_PROBE5(sctp, cwnd, net, rttstep,
434                             vtag,
435                             ((net->cc_mod.rtcc.lbw << 32) | nbw),
436                             ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
437                             oth,
438                             probepoint);
439                         /*
440                          * Did we voluntarily give up some? if so take one
441                          * back please
442                          */
443                         if ((net->cc_mod.rtcc.vol_reduce) &&
444                             (inst_ind != SCTP_INST_GAINING)) {
445                                 net->cwnd += net->mtu;
446                                 sctp_enforce_cwnd_limit(&stcb->asoc, net);
447                                 net->cc_mod.rtcc.vol_reduce--;
448                         }
449                         net->cc_mod.rtcc.last_step_state = 2;
450                         net->cc_mod.rtcc.step_cnt = 0;
451                 }
452                 goto out_decision;
453         } else if (net->rtt < net->cc_mod.rtcc.lbw_rtt - rtt_offset) {
454                 /* bw & rtt decreased */
455                 /* Probe point 3 */
456                 probepoint |= ((3 << 16) | 0);
457                 SDT_PROBE5(sctp, cwnd, net, rttvar,
458                     vtag,
459                     ((net->cc_mod.rtcc.lbw << 32) | nbw),
460                     ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
461                     net->flight_size,
462                     probepoint);
463                 if (net->cc_mod.rtcc.steady_step) {
464                         oth = net->cc_mod.rtcc.vol_reduce;
465                         oth <<= 16;
466                         oth |= net->cc_mod.rtcc.step_cnt;
467                         oth <<= 16;
468                         oth |= net->cc_mod.rtcc.last_step_state;
469                         SDT_PROBE5(sctp, cwnd, net, rttstep,
470                             vtag,
471                             ((net->cc_mod.rtcc.lbw << 32) | nbw),
472                             ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
473                             oth,
474                             probepoint);
475                         if ((net->cc_mod.rtcc.vol_reduce) &&
476                             (inst_ind != SCTP_INST_GAINING)) {
477                                 net->cwnd += net->mtu;
478                                 sctp_enforce_cwnd_limit(&stcb->asoc, net);
479                                 net->cc_mod.rtcc.vol_reduce--;
480                         }
481                         net->cc_mod.rtcc.last_step_state = 3;
482                         net->cc_mod.rtcc.step_cnt = 0;
483                 }
484                 goto out_decision;
485         }
486         /* The bw decreased but rtt stayed the same */
487         /* Probe point 4 */
488         probepoint |= ((4 << 16) | 0);
489         SDT_PROBE5(sctp, cwnd, net, rttvar,
490             vtag,
491             ((net->cc_mod.rtcc.lbw << 32) | nbw),
492             ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
493             net->flight_size,
494             probepoint);
495         if (net->cc_mod.rtcc.steady_step) {
496                 oth = net->cc_mod.rtcc.vol_reduce;
497                 oth <<= 16;
498                 oth |= net->cc_mod.rtcc.step_cnt;
499                 oth <<= 16;
500                 oth |= net->cc_mod.rtcc.last_step_state;
501                 SDT_PROBE5(sctp, cwnd, net, rttstep,
502                     vtag,
503                     ((net->cc_mod.rtcc.lbw << 32) | nbw),
504                     ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
505                     oth,
506                     probepoint);
507                 if ((net->cc_mod.rtcc.vol_reduce) &&
508                     (inst_ind != SCTP_INST_GAINING)) {
509                         net->cwnd += net->mtu;
510                         sctp_enforce_cwnd_limit(&stcb->asoc, net);
511                         net->cc_mod.rtcc.vol_reduce--;
512                 }
513                 net->cc_mod.rtcc.last_step_state = 4;
514                 net->cc_mod.rtcc.step_cnt = 0;
515         }
516 out_decision:
517         net->cc_mod.rtcc.lbw = nbw;
518         net->cc_mod.rtcc.lbw_rtt = net->rtt;
519         net->cc_mod.rtcc.cwnd_at_bw_set = net->cwnd;
520         if (inst_ind == SCTP_INST_GAINING) {
521                 return (1);
522         } else {
523                 return (0);
524         }
525 }
526
527 static int
528 cc_bw_increase(struct sctp_tcb *stcb, struct sctp_nets *net, uint64_t nbw, uint64_t vtag)
529 {
530         uint64_t oth, probepoint;
531
532         /*
533          * BW increased, so update and return 0, since all actions in our
534          * table say to do the normal CC update. Note that we pay no
535          * attention to the inst_ind since our overall sum is increasing.
536          */
537         /* PROBE POINT 0 */
538         probepoint = (((uint64_t)net->cwnd) << 32);
539         SDT_PROBE5(sctp, cwnd, net, rttvar,
540             vtag,
541             ((net->cc_mod.rtcc.lbw << 32) | nbw),
542             ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
543             net->flight_size,
544             probepoint);
545         if (net->cc_mod.rtcc.steady_step) {
546                 oth = net->cc_mod.rtcc.vol_reduce;
547                 oth <<= 16;
548                 oth |= net->cc_mod.rtcc.step_cnt;
549                 oth <<= 16;
550                 oth |= net->cc_mod.rtcc.last_step_state;
551                 SDT_PROBE5(sctp, cwnd, net, rttstep,
552                     vtag,
553                     ((net->cc_mod.rtcc.lbw << 32) | nbw),
554                     ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
555                     oth,
556                     probepoint);
557                 net->cc_mod.rtcc.last_step_state = 0;
558                 net->cc_mod.rtcc.step_cnt = 0;
559                 net->cc_mod.rtcc.vol_reduce = 0;
560         }
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         return (0);
565 }
566
567 /* RTCC Algorithm to limit growth of cwnd, return
568  * true if you want to NOT allow cwnd growth
569  */
570 static int
571 cc_bw_limit(struct sctp_tcb *stcb, struct sctp_nets *net, uint64_t nbw)
572 {
573         uint64_t bw_offset, rtt_offset;
574         uint64_t probepoint, rtt, vtag;
575         uint64_t bytes_for_this_rtt, inst_bw;
576         uint64_t div, inst_off;
577         int bw_shift;
578         uint8_t inst_ind;
579         int ret;
580
581         /*-
582          * Here we need to see if we want
583          * to limit cwnd growth due to increase
584          * in overall rtt but no increase in bw.
585          * We use the following table to figure
586          * out what we should do. When we return
587          * 0, cc update goes on as planned. If we
588          * return 1, then no cc update happens and cwnd
589          * stays where it is at.
590          * ----------------------------------
591          *   BW    |    RTT   | Action
592          * *********************************
593          *   INC   |    INC   | return 0
594          * ----------------------------------
595          *   INC   |    SAME  | return 0
596          * ----------------------------------
597          *   INC   |    DECR  | return 0
598          * ----------------------------------
599          *   SAME  |    INC   | return 1
600          * ----------------------------------
601          *   SAME  |    SAME  | return 1
602          * ----------------------------------
603          *   SAME  |    DECR  | return 0
604          * ----------------------------------
605          *   DECR  |    INC   | return 0 or 1 based on if we caused.
606          * ----------------------------------
607          *   DECR  |    SAME  | return 0
608          * ----------------------------------
609          *   DECR  |    DECR  | return 0
610          * ----------------------------------
611          *
612          * We are a bit fuzz on what an increase or
613          * decrease is. For BW it is the same if
614          * it did not change within 1/64th. For
615          * RTT it stayed the same if it did not
616          * change within 1/32nd
617          */
618         bw_shift = SCTP_BASE_SYSCTL(sctp_rttvar_bw);
619         rtt = stcb->asoc.my_vtag;
620         vtag = (rtt << 32) | (((uint32_t)(stcb->sctp_ep->sctp_lport)) << 16) | (stcb->rport);
621         probepoint = (((uint64_t)net->cwnd) << 32);
622         rtt = net->rtt;
623         if (net->cc_mod.rtcc.rtt_set_this_sack) {
624                 net->cc_mod.rtcc.rtt_set_this_sack = 0;
625                 bytes_for_this_rtt = net->cc_mod.rtcc.bw_bytes - net->cc_mod.rtcc.bw_bytes_at_last_rttc;
626                 net->cc_mod.rtcc.bw_bytes_at_last_rttc = net->cc_mod.rtcc.bw_bytes;
627                 if (net->rtt) {
628                         div = net->rtt / 1000;
629                         if (div) {
630                                 inst_bw = bytes_for_this_rtt / div;
631                                 inst_off = inst_bw >> bw_shift;
632                                 if (inst_bw > nbw)
633                                         inst_ind = SCTP_INST_GAINING;
634                                 else if ((inst_bw + inst_off) < nbw)
635                                         inst_ind = SCTP_INST_LOOSING;
636                                 else
637                                         inst_ind = SCTP_INST_NEUTRAL;
638                                 probepoint |= ((0xb << 16) | inst_ind);
639                         } else {
640                                 inst_ind = net->cc_mod.rtcc.last_inst_ind;
641                                 inst_bw = bytes_for_this_rtt / (uint64_t)(net->rtt);
642                                 /* Can't determine do not change */
643                                 probepoint |= ((0xc << 16) | inst_ind);
644                         }
645                 } else {
646                         inst_ind = net->cc_mod.rtcc.last_inst_ind;
647                         inst_bw = bytes_for_this_rtt;
648                         /* Can't determine do not change */
649                         probepoint |= ((0xd << 16) | inst_ind);
650                 }
651                 SDT_PROBE5(sctp, cwnd, net, rttvar,
652                     vtag,
653                     ((nbw << 32) | inst_bw),
654                     ((net->cc_mod.rtcc.lbw_rtt << 32) | rtt),
655                     net->flight_size,
656                     probepoint);
657         } else {
658                 /* No rtt measurement, use last one */
659                 inst_ind = net->cc_mod.rtcc.last_inst_ind;
660         }
661         bw_offset = net->cc_mod.rtcc.lbw >> bw_shift;
662         if (nbw > net->cc_mod.rtcc.lbw + bw_offset) {
663                 ret = cc_bw_increase(stcb, net, nbw, vtag);
664                 goto out;
665         }
666         rtt_offset = net->cc_mod.rtcc.lbw_rtt >> SCTP_BASE_SYSCTL(sctp_rttvar_rtt);
667         if (nbw < net->cc_mod.rtcc.lbw - bw_offset) {
668                 ret = cc_bw_decrease(stcb, net, nbw, rtt_offset, vtag, inst_ind);
669                 goto out;
670         }
671         /*
672          * If we reach here then we are in a situation where the bw stayed
673          * the same.
674          */
675         ret = cc_bw_same(stcb, net, nbw, rtt_offset, vtag, inst_ind);
676 out:
677         net->cc_mod.rtcc.last_inst_ind = inst_ind;
678         return (ret);
679 }
680
681 static void
682 sctp_cwnd_update_after_sack_common(struct sctp_tcb *stcb,
683     struct sctp_association *asoc,
684     int accum_moved, int reneged_all SCTP_UNUSED, int will_exit, int use_rtcc)
685 {
686         struct sctp_nets *net;
687         int old_cwnd;
688         uint32_t t_ssthresh, incr;
689         uint64_t t_ucwnd_sbw;
690         uint64_t t_path_mptcp;
691         uint64_t mptcp_like_alpha;
692         uint32_t srtt;
693         uint64_t max_path;
694
695         /* MT FIXME: Don't compute this over and over again */
696         t_ssthresh = 0;
697         t_ucwnd_sbw = 0;
698         t_path_mptcp = 0;
699         mptcp_like_alpha = 1;
700         if ((stcb->asoc.sctp_cmt_on_off == SCTP_CMT_RPV1) ||
701             (stcb->asoc.sctp_cmt_on_off == SCTP_CMT_RPV2) ||
702             (stcb->asoc.sctp_cmt_on_off == SCTP_CMT_MPTCP)) {
703                 max_path = 0;
704                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
705                         t_ssthresh += net->ssthresh;
706                         /* lastsa>>3;  we don't need to devide ... */
707                         srtt = net->lastsa;
708                         if (srtt > 0) {
709                                 uint64_t tmp;
710
711                                 t_ucwnd_sbw += (uint64_t)net->cwnd / (uint64_t)srtt;
712                                 t_path_mptcp += (((uint64_t)net->cwnd) << SHIFT_MPTCP_MULTI_Z) /
713                                     (((uint64_t)net->mtu) * (uint64_t)srtt);
714                                 tmp = (((uint64_t)net->cwnd) << SHIFT_MPTCP_MULTI_N) /
715                                     ((uint64_t)net->mtu * (uint64_t)(srtt * srtt));
716                                 if (tmp > max_path) {
717                                         max_path = tmp;
718                                 }
719                         }
720                 }
721                 if (t_path_mptcp > 0) {
722                         mptcp_like_alpha = max_path / (t_path_mptcp * t_path_mptcp);
723                 } else {
724                         mptcp_like_alpha = 1;
725                 }
726         }
727         if (t_ssthresh == 0) {
728                 t_ssthresh = 1;
729         }
730         if (t_ucwnd_sbw == 0) {
731                 t_ucwnd_sbw = 1;
732         }
733         /******************************/
734         /* update cwnd and Early FR   */
735         /******************************/
736         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
737 #ifdef JANA_CMT_FAST_RECOVERY
738                 /*
739                  * CMT fast recovery code. Need to debug.
740                  */
741                 if (net->fast_retran_loss_recovery && net->new_pseudo_cumack) {
742                         if (SCTP_TSN_GE(asoc->last_acked_seq, net->fast_recovery_tsn) ||
743                             SCTP_TSN_GE(net->pseudo_cumack, net->fast_recovery_tsn)) {
744                                 net->will_exit_fast_recovery = 1;
745                         }
746                 }
747 #endif
748                 /* if nothing was acked on this destination skip it */
749                 if (net->net_ack == 0) {
750                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
751                                 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FROM_SACK);
752                         }
753                         continue;
754                 }
755 #ifdef JANA_CMT_FAST_RECOVERY
756                 /*
757                  * CMT fast recovery code
758                  */
759                 /*
760                  * if (sctp_cmt_on_off > 0 && net->fast_retran_loss_recovery
761                  * && net->will_exit_fast_recovery == 0) { @@@ Do something
762                  * } else if (sctp_cmt_on_off == 0 &&
763                  * asoc->fast_retran_loss_recovery && will_exit == 0) {
764                  */
765 #endif
766
767                 if (asoc->fast_retran_loss_recovery &&
768                     (will_exit == 0) &&
769                     (asoc->sctp_cmt_on_off == 0)) {
770                         /*
771                          * If we are in loss recovery we skip any cwnd
772                          * update
773                          */
774                         return;
775                 }
776                 /*
777                  * Did any measurements go on for this network?
778                  */
779                 if (use_rtcc && (net->cc_mod.rtcc.tls_needs_set > 0)) {
780                         uint64_t nbw;
781
782                         /*
783                          * At this point our bw_bytes has been updated by
784                          * incoming sack information.
785                          *
786                          * But our bw may not yet be set.
787                          *
788                          */
789                         if ((net->cc_mod.rtcc.new_tot_time / 1000) > 0) {
790                                 nbw = net->cc_mod.rtcc.bw_bytes / (net->cc_mod.rtcc.new_tot_time / 1000);
791                         } else {
792                                 nbw = net->cc_mod.rtcc.bw_bytes;
793                         }
794                         if (net->cc_mod.rtcc.lbw) {
795                                 if (cc_bw_limit(stcb, net, nbw)) {
796                                         /* Hold here, no update */
797                                         continue;
798                                 }
799                         } else {
800                                 uint64_t vtag, probepoint;
801
802                                 probepoint = (((uint64_t)net->cwnd) << 32);
803                                 probepoint |= ((0xa << 16) | 0);
804                                 vtag = (net->rtt << 32) |
805                                     (((uint32_t)(stcb->sctp_ep->sctp_lport)) << 16) |
806                                     (stcb->rport);
807
808                                 SDT_PROBE5(sctp, cwnd, net, rttvar,
809                                     vtag,
810                                     nbw,
811                                     ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
812                                     net->flight_size,
813                                     probepoint);
814                                 net->cc_mod.rtcc.lbw = nbw;
815                                 net->cc_mod.rtcc.lbw_rtt = net->rtt;
816                                 if (net->cc_mod.rtcc.rtt_set_this_sack) {
817                                         net->cc_mod.rtcc.rtt_set_this_sack = 0;
818                                         net->cc_mod.rtcc.bw_bytes_at_last_rttc = net->cc_mod.rtcc.bw_bytes;
819                                 }
820                         }
821                 }
822                 /*
823                  * CMT: CUC algorithm. Update cwnd if pseudo-cumack has
824                  * moved.
825                  */
826                 if (accum_moved ||
827                     ((asoc->sctp_cmt_on_off > 0) && net->new_pseudo_cumack)) {
828                         /* If the cumulative ack moved we can proceed */
829                         if (net->cwnd <= net->ssthresh) {
830                                 /* We are in slow start */
831                                 if (net->flight_size + net->net_ack >= net->cwnd) {
832                                         uint32_t limit;
833
834                                         old_cwnd = net->cwnd;
835                                         switch (asoc->sctp_cmt_on_off) {
836                                         case SCTP_CMT_RPV1:
837                                                 limit = (uint32_t)(((uint64_t)net->mtu *
838                                                     (uint64_t)SCTP_BASE_SYSCTL(sctp_L2_abc_variable) *
839                                                     (uint64_t)net->ssthresh) /
840                                                     (uint64_t)t_ssthresh);
841                                                 incr = (uint32_t)(((uint64_t)net->net_ack *
842                                                     (uint64_t)net->ssthresh) /
843                                                     (uint64_t)t_ssthresh);
844                                                 if (incr > limit) {
845                                                         incr = limit;
846                                                 }
847                                                 if (incr == 0) {
848                                                         incr = 1;
849                                                 }
850                                                 break;
851                                         case SCTP_CMT_RPV2:
852                                                 /*
853                                                  * lastsa>>3;  we don't need
854                                                  * to divide ...
855                                                  */
856                                                 srtt = net->lastsa;
857                                                 if (srtt == 0) {
858                                                         srtt = 1;
859                                                 }
860                                                 limit = (uint32_t)(((uint64_t)net->mtu *
861                                                     (uint64_t)SCTP_BASE_SYSCTL(sctp_L2_abc_variable) *
862                                                     (uint64_t)net->cwnd) /
863                                                     ((uint64_t)srtt * t_ucwnd_sbw));
864                                                 /* INCREASE FACTOR */
865                                                 incr = (uint32_t)(((uint64_t)net->net_ack *
866                                                     (uint64_t)net->cwnd) /
867                                                     ((uint64_t)srtt * t_ucwnd_sbw));
868                                                 /* INCREASE FACTOR */
869                                                 if (incr > limit) {
870                                                         incr = limit;
871                                                 }
872                                                 if (incr == 0) {
873                                                         incr = 1;
874                                                 }
875                                                 break;
876                                         case SCTP_CMT_MPTCP:
877                                                 limit = (uint32_t)(((uint64_t)net->mtu *
878                                                     mptcp_like_alpha *
879                                                     (uint64_t)SCTP_BASE_SYSCTL(sctp_L2_abc_variable)) >>
880                                                     SHIFT_MPTCP_MULTI);
881                                                 incr = (uint32_t)(((uint64_t)net->net_ack *
882                                                     mptcp_like_alpha) >>
883                                                     SHIFT_MPTCP_MULTI);
884                                                 if (incr > limit) {
885                                                         incr = limit;
886                                                 }
887                                                 if (incr > net->net_ack) {
888                                                         incr = net->net_ack;
889                                                 }
890                                                 if (incr > net->mtu) {
891                                                         incr = net->mtu;
892                                                 }
893                                                 break;
894                                         default:
895                                                 incr = net->net_ack;
896                                                 if (incr > net->mtu * SCTP_BASE_SYSCTL(sctp_L2_abc_variable)) {
897                                                         incr = net->mtu * SCTP_BASE_SYSCTL(sctp_L2_abc_variable);
898                                                 }
899                                                 break;
900                                         }
901                                         net->cwnd += incr;
902                                         sctp_enforce_cwnd_limit(asoc, net);
903                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
904                                                 sctp_log_cwnd(stcb, net, incr,
905                                                     SCTP_CWND_LOG_FROM_SS);
906                                         }
907                                         SDT_PROBE5(sctp, cwnd, net, ack,
908                                             stcb->asoc.my_vtag,
909                                             ((stcb->sctp_ep->sctp_lport << 16) | (stcb->rport)),
910                                             net,
911                                             old_cwnd, net->cwnd);
912                                 } else {
913                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
914                                                 sctp_log_cwnd(stcb, net, net->net_ack,
915                                                     SCTP_CWND_LOG_NOADV_SS);
916                                         }
917                                 }
918                         } else {
919                                 /* We are in congestion avoidance */
920                                 /*
921                                  * Add to pba
922                                  */
923                                 net->partial_bytes_acked += net->net_ack;
924
925                                 if ((net->flight_size + net->net_ack >= net->cwnd) &&
926                                     (net->partial_bytes_acked >= net->cwnd)) {
927                                         net->partial_bytes_acked -= net->cwnd;
928                                         old_cwnd = net->cwnd;
929                                         switch (asoc->sctp_cmt_on_off) {
930                                         case SCTP_CMT_RPV1:
931                                                 incr = (uint32_t)(((uint64_t)net->mtu *
932                                                     (uint64_t)net->ssthresh) /
933                                                     (uint64_t)t_ssthresh);
934                                                 if (incr == 0) {
935                                                         incr = 1;
936                                                 }
937                                                 break;
938                                         case SCTP_CMT_RPV2:
939                                                 /*
940                                                  * lastsa>>3;  we don't need
941                                                  * to divide ...
942                                                  */
943                                                 srtt = net->lastsa;
944                                                 if (srtt == 0) {
945                                                         srtt = 1;
946                                                 }
947                                                 incr = (uint32_t)((uint64_t)net->mtu *
948                                                     (uint64_t)net->cwnd /
949                                                     ((uint64_t)srtt *
950                                                     t_ucwnd_sbw));
951                                                 /* INCREASE FACTOR */
952                                                 if (incr == 0) {
953                                                         incr = 1;
954                                                 }
955                                                 break;
956                                         case SCTP_CMT_MPTCP:
957                                                 incr = (uint32_t)((mptcp_like_alpha *
958                                                     (uint64_t)net->cwnd) >>
959                                                     SHIFT_MPTCP_MULTI);
960                                                 if (incr > net->mtu) {
961                                                         incr = net->mtu;
962                                                 }
963                                                 break;
964                                         default:
965                                                 incr = net->mtu;
966                                                 break;
967                                         }
968                                         net->cwnd += incr;
969                                         sctp_enforce_cwnd_limit(asoc, net);
970                                         SDT_PROBE5(sctp, cwnd, net, ack,
971                                             stcb->asoc.my_vtag,
972                                             ((stcb->sctp_ep->sctp_lport << 16) | (stcb->rport)),
973                                             net,
974                                             old_cwnd, net->cwnd);
975                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
976                                                 sctp_log_cwnd(stcb, net, net->mtu,
977                                                     SCTP_CWND_LOG_FROM_CA);
978                                         }
979                                 } else {
980                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
981                                                 sctp_log_cwnd(stcb, net, net->net_ack,
982                                                     SCTP_CWND_LOG_NOADV_CA);
983                                         }
984                                 }
985                         }
986                 } else {
987                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
988                                 sctp_log_cwnd(stcb, net, net->mtu,
989                                     SCTP_CWND_LOG_NO_CUMACK);
990                         }
991                 }
992         }
993 }
994
995 static void
996 sctp_cwnd_update_exit_pf_common(struct sctp_tcb *stcb, struct sctp_nets *net)
997 {
998         int old_cwnd;
999
1000         old_cwnd = net->cwnd;
1001         net->cwnd = net->mtu;
1002         SDT_PROBE5(sctp, cwnd, net, ack,
1003             stcb->asoc.my_vtag, ((stcb->sctp_ep->sctp_lport << 16) | (stcb->rport)), net,
1004             old_cwnd, net->cwnd);
1005         SCTPDBG(SCTP_DEBUG_INDATA1, "Destination %p moved from PF to reachable with cwnd %d.\n",
1006             (void *)net, net->cwnd);
1007 }
1008
1009 static void
1010 sctp_cwnd_update_after_timeout(struct sctp_tcb *stcb, struct sctp_nets *net)
1011 {
1012         int old_cwnd = net->cwnd;
1013         uint32_t t_ssthresh, t_cwnd;
1014         uint64_t t_ucwnd_sbw;
1015
1016         /* MT FIXME: Don't compute this over and over again */
1017         t_ssthresh = 0;
1018         t_cwnd = 0;
1019         if ((stcb->asoc.sctp_cmt_on_off == SCTP_CMT_RPV1) ||
1020             (stcb->asoc.sctp_cmt_on_off == SCTP_CMT_RPV2)) {
1021                 struct sctp_nets *lnet;
1022                 uint32_t srtt;
1023
1024                 t_ucwnd_sbw = 0;
1025                 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
1026                         t_ssthresh += lnet->ssthresh;
1027                         t_cwnd += lnet->cwnd;
1028                         srtt = lnet->lastsa;
1029                         /* lastsa>>3;  we don't need to divide ... */
1030                         if (srtt > 0) {
1031                                 t_ucwnd_sbw += (uint64_t)lnet->cwnd / (uint64_t)srtt;
1032                         }
1033                 }
1034                 if (t_ssthresh < 1) {
1035                         t_ssthresh = 1;
1036                 }
1037                 if (t_ucwnd_sbw < 1) {
1038                         t_ucwnd_sbw = 1;
1039                 }
1040                 if (stcb->asoc.sctp_cmt_on_off == SCTP_CMT_RPV1) {
1041                         net->ssthresh = (uint32_t)(((uint64_t)4 *
1042                             (uint64_t)net->mtu *
1043                             (uint64_t)net->ssthresh) /
1044                             (uint64_t)t_ssthresh);
1045                 } else {
1046                         uint64_t cc_delta;
1047
1048                         srtt = net->lastsa;
1049                         /* lastsa>>3;  we don't need to divide ... */
1050                         if (srtt == 0) {
1051                                 srtt = 1;
1052                         }
1053                         cc_delta = t_ucwnd_sbw * (uint64_t)srtt / 2;
1054                         if (cc_delta < t_cwnd) {
1055                                 net->ssthresh = (uint32_t)((uint64_t)t_cwnd - cc_delta);
1056                         } else {
1057                                 net->ssthresh = net->mtu;
1058                         }
1059                 }
1060                 if ((net->cwnd > t_cwnd / 2) &&
1061                     (net->ssthresh < net->cwnd - t_cwnd / 2)) {
1062                         net->ssthresh = net->cwnd - t_cwnd / 2;
1063                 }
1064                 if (net->ssthresh < net->mtu) {
1065                         net->ssthresh = net->mtu;
1066                 }
1067         } else {
1068                 net->ssthresh = max(net->cwnd / 2, 4 * net->mtu);
1069         }
1070         net->cwnd = net->mtu;
1071         net->partial_bytes_acked = 0;
1072         SDT_PROBE5(sctp, cwnd, net, to,
1073             stcb->asoc.my_vtag,
1074             ((stcb->sctp_ep->sctp_lport << 16) | (stcb->rport)),
1075             net,
1076             old_cwnd, net->cwnd);
1077         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1078                 sctp_log_cwnd(stcb, net, net->cwnd - old_cwnd, SCTP_CWND_LOG_FROM_RTX);
1079         }
1080 }
1081
1082 static void
1083 sctp_cwnd_update_after_ecn_echo_common(struct sctp_tcb *stcb, struct sctp_nets *net,
1084     int in_window, int num_pkt_lost, int use_rtcc)
1085 {
1086         int old_cwnd = net->cwnd;
1087
1088         if ((use_rtcc) && (net->lan_type == SCTP_LAN_LOCAL) && (net->cc_mod.rtcc.use_dccc_ecn)) {
1089                 /* Data center Congestion Control */
1090                 if (in_window == 0) {
1091                         /*
1092                          * Go to CA with the cwnd at the point we sent the
1093                          * TSN that was marked with a CE.
1094                          */
1095                         if (net->ecn_prev_cwnd < net->cwnd) {
1096                                 /* Restore to prev cwnd */
1097                                 net->cwnd = net->ecn_prev_cwnd - (net->mtu * num_pkt_lost);
1098                         } else {
1099                                 /* Just cut in 1/2 */
1100                                 net->cwnd /= 2;
1101                         }
1102                         /* Drop to CA */
1103                         net->ssthresh = net->cwnd - (num_pkt_lost * net->mtu);
1104                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1105                                 sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_SAT);
1106                         }
1107                 } else {
1108                         /*
1109                          * Further tuning down required over the drastic
1110                          * original cut
1111                          */
1112                         net->ssthresh -= (net->mtu * num_pkt_lost);
1113                         net->cwnd -= (net->mtu * num_pkt_lost);
1114                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1115                                 sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_SAT);
1116                         }
1117                 }
1118                 SCTP_STAT_INCR(sctps_ecnereducedcwnd);
1119         } else {
1120                 if (in_window == 0) {
1121                         SCTP_STAT_INCR(sctps_ecnereducedcwnd);
1122                         net->ssthresh = net->cwnd / 2;
1123                         if (net->ssthresh < net->mtu) {
1124                                 net->ssthresh = net->mtu;
1125                                 /*
1126                                  * here back off the timer as well, to slow
1127                                  * us down
1128                                  */
1129                                 net->RTO <<= 1;
1130                         }
1131                         net->cwnd = net->ssthresh;
1132                         SDT_PROBE5(sctp, cwnd, net, ecn,
1133                             stcb->asoc.my_vtag,
1134                             ((stcb->sctp_ep->sctp_lport << 16) | (stcb->rport)),
1135                             net,
1136                             old_cwnd, net->cwnd);
1137                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1138                                 sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_SAT);
1139                         }
1140                 }
1141         }
1142
1143 }
1144
1145 static void
1146 sctp_cwnd_update_after_packet_dropped(struct sctp_tcb *stcb,
1147     struct sctp_nets *net, struct sctp_pktdrop_chunk *cp,
1148     uint32_t *bottle_bw, uint32_t *on_queue)
1149 {
1150         uint32_t bw_avail;
1151         unsigned int incr;
1152         int old_cwnd = net->cwnd;
1153
1154         /* get bottle neck bw */
1155         *bottle_bw = ntohl(cp->bottle_bw);
1156         /* and whats on queue */
1157         *on_queue = ntohl(cp->current_onq);
1158         /*
1159          * adjust the on-queue if our flight is more it could be that the
1160          * router has not yet gotten data "in-flight" to it
1161          */
1162         if (*on_queue < net->flight_size) {
1163                 *on_queue = net->flight_size;
1164         }
1165         /* rtt is measured in micro seconds, bottle_bw in bytes per second */
1166         bw_avail = (uint32_t)(((uint64_t)(*bottle_bw) * net->rtt) / (uint64_t)1000000);
1167         if (bw_avail > *bottle_bw) {
1168                 /*
1169                  * Cap the growth to no more than the bottle neck. This can
1170                  * happen as RTT slides up due to queues. It also means if
1171                  * you have more than a 1 second RTT with a empty queue you
1172                  * will be limited to the bottle_bw per second no matter if
1173                  * other points have 1/2 the RTT and you could get more
1174                  * out...
1175                  */
1176                 bw_avail = *bottle_bw;
1177         }
1178         if (*on_queue > bw_avail) {
1179                 /*
1180                  * No room for anything else don't allow anything else to be
1181                  * "added to the fire".
1182                  */
1183                 int seg_inflight, seg_onqueue, my_portion;
1184
1185                 net->partial_bytes_acked = 0;
1186                 /* how much are we over queue size? */
1187                 incr = *on_queue - bw_avail;
1188                 if (stcb->asoc.seen_a_sack_this_pkt) {
1189                         /*
1190                          * undo any cwnd adjustment that the sack might have
1191                          * made
1192                          */
1193                         net->cwnd = net->prev_cwnd;
1194                 }
1195                 /* Now how much of that is mine? */
1196                 seg_inflight = net->flight_size / net->mtu;
1197                 seg_onqueue = *on_queue / net->mtu;
1198                 my_portion = (incr * seg_inflight) / seg_onqueue;
1199
1200                 /* Have I made an adjustment already */
1201                 if (net->cwnd > net->flight_size) {
1202                         /*
1203                          * for this flight I made an adjustment we need to
1204                          * decrease the portion by a share our previous
1205                          * adjustment.
1206                          */
1207                         int diff_adj;
1208
1209                         diff_adj = net->cwnd - net->flight_size;
1210                         if (diff_adj > my_portion)
1211                                 my_portion = 0;
1212                         else
1213                                 my_portion -= diff_adj;
1214                 }
1215                 /*
1216                  * back down to the previous cwnd (assume we have had a sack
1217                  * before this packet). minus what ever portion of the
1218                  * overage is my fault.
1219                  */
1220                 net->cwnd -= my_portion;
1221
1222                 /* we will NOT back down more than 1 MTU */
1223                 if (net->cwnd <= net->mtu) {
1224                         net->cwnd = net->mtu;
1225                 }
1226                 /* force into CA */
1227                 net->ssthresh = net->cwnd - 1;
1228         } else {
1229                 /*
1230                  * Take 1/4 of the space left or max burst up .. whichever
1231                  * is less.
1232                  */
1233                 incr = (bw_avail - *on_queue) >> 2;
1234                 if ((stcb->asoc.max_burst > 0) &&
1235                     (stcb->asoc.max_burst * net->mtu < incr)) {
1236                         incr = stcb->asoc.max_burst * net->mtu;
1237                 }
1238                 net->cwnd += incr;
1239         }
1240         if (net->cwnd > bw_avail) {
1241                 /* We can't exceed the pipe size */
1242                 net->cwnd = bw_avail;
1243         }
1244         if (net->cwnd < net->mtu) {
1245                 /* We always have 1 MTU */
1246                 net->cwnd = net->mtu;
1247         }
1248         sctp_enforce_cwnd_limit(&stcb->asoc, net);
1249         if (net->cwnd - old_cwnd != 0) {
1250                 /* log only changes */
1251                 SDT_PROBE5(sctp, cwnd, net, pd,
1252                     stcb->asoc.my_vtag,
1253                     ((stcb->sctp_ep->sctp_lport << 16) | (stcb->rport)),
1254                     net,
1255                     old_cwnd, net->cwnd);
1256                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1257                         sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd),
1258                             SCTP_CWND_LOG_FROM_SAT);
1259                 }
1260         }
1261 }
1262
1263 static void
1264 sctp_cwnd_update_after_output(struct sctp_tcb *stcb,
1265     struct sctp_nets *net, int burst_limit)
1266 {
1267         int old_cwnd = net->cwnd;
1268
1269         if (net->ssthresh < net->cwnd)
1270                 net->ssthresh = net->cwnd;
1271         if (burst_limit) {
1272                 net->cwnd = (net->flight_size + (burst_limit * net->mtu));
1273                 sctp_enforce_cwnd_limit(&stcb->asoc, net);
1274                 SDT_PROBE5(sctp, cwnd, net, bl,
1275                     stcb->asoc.my_vtag,
1276                     ((stcb->sctp_ep->sctp_lport << 16) | (stcb->rport)),
1277                     net,
1278                     old_cwnd, net->cwnd);
1279                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1280                         sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_BRST);
1281                 }
1282         }
1283 }
1284
1285 static void
1286 sctp_cwnd_update_after_sack(struct sctp_tcb *stcb,
1287     struct sctp_association *asoc,
1288     int accum_moved, int reneged_all, int will_exit)
1289 {
1290         /* Passing a zero argument in last disables the rtcc algorithm */
1291         sctp_cwnd_update_after_sack_common(stcb, asoc, accum_moved, reneged_all, will_exit, 0);
1292 }
1293
1294 static void
1295 sctp_cwnd_update_after_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
1296     int in_window, int num_pkt_lost)
1297 {
1298         /* Passing a zero argument in last disables the rtcc algorithm */
1299         sctp_cwnd_update_after_ecn_echo_common(stcb, net, in_window, num_pkt_lost, 0);
1300 }
1301
1302 /* Here starts the RTCCVAR type CC invented by RRS which
1303  * is a slight mod to RFC2581. We reuse a common routine or
1304  * two since these algorithms are so close and need to
1305  * remain the same.
1306  */
1307 static void
1308 sctp_cwnd_update_rtcc_after_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
1309     int in_window, int num_pkt_lost)
1310 {
1311         sctp_cwnd_update_after_ecn_echo_common(stcb, net, in_window, num_pkt_lost, 1);
1312 }
1313
1314 static void
1315 sctp_cwnd_update_rtcc_tsn_acknowledged(struct sctp_nets *net,
1316     struct sctp_tmit_chunk *tp1)
1317 {
1318         net->cc_mod.rtcc.bw_bytes += tp1->send_size;
1319 }
1320
1321 static void
1322 sctp_cwnd_prepare_rtcc_net_for_sack(struct sctp_tcb *stcb SCTP_UNUSED,
1323     struct sctp_nets *net)
1324 {
1325         if (net->cc_mod.rtcc.tls_needs_set > 0) {
1326                 /* We had a bw measurment going on */
1327                 struct timeval ltls;
1328
1329                 SCTP_GETPTIME_TIMEVAL(&ltls);
1330                 timevalsub(&ltls, &net->cc_mod.rtcc.tls);
1331                 net->cc_mod.rtcc.new_tot_time = (ltls.tv_sec * 1000000) + ltls.tv_usec;
1332         }
1333 }
1334
1335 static void
1336 sctp_cwnd_new_rtcc_transmission_begins(struct sctp_tcb *stcb,
1337     struct sctp_nets *net)
1338 {
1339         uint64_t vtag, probepoint;
1340
1341         if (net->cc_mod.rtcc.lbw) {
1342                 /* Clear the old bw.. we went to 0 in-flight */
1343                 vtag = (net->rtt << 32) | (((uint32_t)(stcb->sctp_ep->sctp_lport)) << 16) |
1344                     (stcb->rport);
1345                 probepoint = (((uint64_t)net->cwnd) << 32);
1346                 /* Probe point 8 */
1347                 probepoint |= ((8 << 16) | 0);
1348                 SDT_PROBE5(sctp, cwnd, net, rttvar,
1349                     vtag,
1350                     ((net->cc_mod.rtcc.lbw << 32) | 0),
1351                     ((net->cc_mod.rtcc.lbw_rtt << 32) | net->rtt),
1352                     net->flight_size,
1353                     probepoint);
1354                 net->cc_mod.rtcc.lbw_rtt = 0;
1355                 net->cc_mod.rtcc.cwnd_at_bw_set = 0;
1356                 net->cc_mod.rtcc.lbw = 0;
1357                 net->cc_mod.rtcc.bw_bytes_at_last_rttc = 0;
1358                 net->cc_mod.rtcc.vol_reduce = 0;
1359                 net->cc_mod.rtcc.bw_tot_time = 0;
1360                 net->cc_mod.rtcc.bw_bytes = 0;
1361                 net->cc_mod.rtcc.tls_needs_set = 0;
1362                 if (net->cc_mod.rtcc.steady_step) {
1363                         net->cc_mod.rtcc.vol_reduce = 0;
1364                         net->cc_mod.rtcc.step_cnt = 0;
1365                         net->cc_mod.rtcc.last_step_state = 0;
1366                 }
1367                 if (net->cc_mod.rtcc.ret_from_eq) {
1368                         /* less aggressive one - reset cwnd too */
1369                         uint32_t cwnd_in_mtu, cwnd;
1370
1371                         cwnd_in_mtu = SCTP_BASE_SYSCTL(sctp_initial_cwnd);
1372                         if (cwnd_in_mtu == 0) {
1373                                 /*
1374                                  * Using 0 means that the value of RFC 4960
1375                                  * is used.
1376                                  */
1377                                 cwnd = min((net->mtu * 4), max((2 * net->mtu), SCTP_INITIAL_CWND));
1378                         } else {
1379                                 /*
1380                                  * We take the minimum of the burst limit
1381                                  * and the initial congestion window.
1382                                  */
1383                                 if ((stcb->asoc.max_burst > 0) && (cwnd_in_mtu > stcb->asoc.max_burst))
1384                                         cwnd_in_mtu = stcb->asoc.max_burst;
1385                                 cwnd = (net->mtu - sizeof(struct sctphdr)) * cwnd_in_mtu;
1386                         }
1387                         if (net->cwnd > cwnd) {
1388                                 /*
1389                                  * Only set if we are not a timeout (i.e.
1390                                  * down to 1 mtu)
1391                                  */
1392                                 net->cwnd = cwnd;
1393                         }
1394                 }
1395         }
1396 }
1397
1398 static void
1399 sctp_set_rtcc_initial_cc_param(struct sctp_tcb *stcb,
1400     struct sctp_nets *net)
1401 {
1402         uint64_t vtag, probepoint;
1403
1404         sctp_set_initial_cc_param(stcb, net);
1405         stcb->asoc.use_precise_time = 1;
1406         probepoint = (((uint64_t)net->cwnd) << 32);
1407         probepoint |= ((9 << 16) | 0);
1408         vtag = (net->rtt << 32) |
1409             (((uint32_t)(stcb->sctp_ep->sctp_lport)) << 16) |
1410             (stcb->rport);
1411         SDT_PROBE5(sctp, cwnd, net, rttvar,
1412             vtag,
1413             0,
1414             0,
1415             0,
1416             probepoint);
1417         net->cc_mod.rtcc.lbw_rtt = 0;
1418         net->cc_mod.rtcc.cwnd_at_bw_set = 0;
1419         net->cc_mod.rtcc.vol_reduce = 0;
1420         net->cc_mod.rtcc.lbw = 0;
1421         net->cc_mod.rtcc.vol_reduce = 0;
1422         net->cc_mod.rtcc.bw_bytes_at_last_rttc = 0;
1423         net->cc_mod.rtcc.bw_tot_time = 0;
1424         net->cc_mod.rtcc.bw_bytes = 0;
1425         net->cc_mod.rtcc.tls_needs_set = 0;
1426         net->cc_mod.rtcc.ret_from_eq = SCTP_BASE_SYSCTL(sctp_rttvar_eqret);
1427         net->cc_mod.rtcc.steady_step = SCTP_BASE_SYSCTL(sctp_steady_step);
1428         net->cc_mod.rtcc.use_dccc_ecn = SCTP_BASE_SYSCTL(sctp_use_dccc_ecn);
1429         net->cc_mod.rtcc.step_cnt = 0;
1430         net->cc_mod.rtcc.last_step_state = 0;
1431 }
1432
1433 static int
1434 sctp_cwnd_rtcc_socket_option(struct sctp_tcb *stcb, int setorget,
1435     struct sctp_cc_option *cc_opt)
1436 {
1437         struct sctp_nets *net;
1438
1439         if (setorget == 1) {
1440                 /* a set */
1441                 if (cc_opt->option == SCTP_CC_OPT_RTCC_SETMODE) {
1442                         if ((cc_opt->aid_value.assoc_value != 0) &&
1443                             (cc_opt->aid_value.assoc_value != 1)) {
1444                                 return (EINVAL);
1445                         }
1446                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1447                                 net->cc_mod.rtcc.ret_from_eq = cc_opt->aid_value.assoc_value;
1448                         }
1449                 } else if (cc_opt->option == SCTP_CC_OPT_USE_DCCC_ECN) {
1450                         if ((cc_opt->aid_value.assoc_value != 0) &&
1451                             (cc_opt->aid_value.assoc_value != 1)) {
1452                                 return (EINVAL);
1453                         }
1454                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1455                                 net->cc_mod.rtcc.use_dccc_ecn = cc_opt->aid_value.assoc_value;
1456                         }
1457                 } else if (cc_opt->option == SCTP_CC_OPT_STEADY_STEP) {
1458                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1459                                 net->cc_mod.rtcc.steady_step = cc_opt->aid_value.assoc_value;
1460                         }
1461                 } else {
1462                         return (EINVAL);
1463                 }
1464         } else {
1465                 /* a get */
1466                 if (cc_opt->option == SCTP_CC_OPT_RTCC_SETMODE) {
1467                         net = TAILQ_FIRST(&stcb->asoc.nets);
1468                         if (net == NULL) {
1469                                 return (EFAULT);
1470                         }
1471                         cc_opt->aid_value.assoc_value = net->cc_mod.rtcc.ret_from_eq;
1472                 } else if (cc_opt->option == SCTP_CC_OPT_USE_DCCC_ECN) {
1473                         net = TAILQ_FIRST(&stcb->asoc.nets);
1474                         if (net == NULL) {
1475                                 return (EFAULT);
1476                         }
1477                         cc_opt->aid_value.assoc_value = net->cc_mod.rtcc.use_dccc_ecn;
1478                 } else if (cc_opt->option == SCTP_CC_OPT_STEADY_STEP) {
1479                         net = TAILQ_FIRST(&stcb->asoc.nets);
1480                         if (net == NULL) {
1481                                 return (EFAULT);
1482                         }
1483                         cc_opt->aid_value.assoc_value = net->cc_mod.rtcc.steady_step;
1484                 } else {
1485                         return (EINVAL);
1486                 }
1487         }
1488         return (0);
1489 }
1490
1491 static void
1492 sctp_cwnd_update_rtcc_packet_transmitted(struct sctp_tcb *stcb SCTP_UNUSED,
1493     struct sctp_nets *net)
1494 {
1495         if (net->cc_mod.rtcc.tls_needs_set == 0) {
1496                 SCTP_GETPTIME_TIMEVAL(&net->cc_mod.rtcc.tls);
1497                 net->cc_mod.rtcc.tls_needs_set = 2;
1498         }
1499 }
1500
1501 static void
1502 sctp_cwnd_update_rtcc_after_sack(struct sctp_tcb *stcb,
1503     struct sctp_association *asoc,
1504     int accum_moved, int reneged_all, int will_exit)
1505 {
1506         /* Passing a one argument at the last enables the rtcc algorithm */
1507         sctp_cwnd_update_after_sack_common(stcb, asoc, accum_moved, reneged_all, will_exit, 1);
1508 }
1509
1510 static void
1511 sctp_rtt_rtcc_calculated(struct sctp_tcb *stcb SCTP_UNUSED,
1512     struct sctp_nets *net,
1513     struct timeval *now SCTP_UNUSED)
1514 {
1515         net->cc_mod.rtcc.rtt_set_this_sack = 1;
1516 }
1517
1518 /* Here starts Sally Floyds HS-TCP */
1519
1520 struct sctp_hs_raise_drop {
1521         int32_t cwnd;
1522         int8_t increase;
1523         int8_t drop_percent;
1524 };
1525
1526 #define SCTP_HS_TABLE_SIZE 73
1527
1528 static const struct sctp_hs_raise_drop sctp_cwnd_adjust[SCTP_HS_TABLE_SIZE] = {
1529         {38, 1, 50},            /* 0   */
1530         {118, 2, 44},           /* 1   */
1531         {221, 3, 41},           /* 2   */
1532         {347, 4, 38},           /* 3   */
1533         {495, 5, 37},           /* 4   */
1534         {663, 6, 35},           /* 5   */
1535         {851, 7, 34},           /* 6   */
1536         {1058, 8, 33},          /* 7   */
1537         {1284, 9, 32},          /* 8   */
1538         {1529, 10, 31},         /* 9   */
1539         {1793, 11, 30},         /* 10  */
1540         {2076, 12, 29},         /* 11  */
1541         {2378, 13, 28},         /* 12  */
1542         {2699, 14, 28},         /* 13  */
1543         {3039, 15, 27},         /* 14  */
1544         {3399, 16, 27},         /* 15  */
1545         {3778, 17, 26},         /* 16  */
1546         {4177, 18, 26},         /* 17  */
1547         {4596, 19, 25},         /* 18  */
1548         {5036, 20, 25},         /* 19  */
1549         {5497, 21, 24},         /* 20  */
1550         {5979, 22, 24},         /* 21  */
1551         {6483, 23, 23},         /* 22  */
1552         {7009, 24, 23},         /* 23  */
1553         {7558, 25, 22},         /* 24  */
1554         {8130, 26, 22},         /* 25  */
1555         {8726, 27, 22},         /* 26  */
1556         {9346, 28, 21},         /* 27  */
1557         {9991, 29, 21},         /* 28  */
1558         {10661, 30, 21},        /* 29  */
1559         {11358, 31, 20},        /* 30  */
1560         {12082, 32, 20},        /* 31  */
1561         {12834, 33, 20},        /* 32  */
1562         {13614, 34, 19},        /* 33  */
1563         {14424, 35, 19},        /* 34  */
1564         {15265, 36, 19},        /* 35  */
1565         {16137, 37, 19},        /* 36  */
1566         {17042, 38, 18},        /* 37  */
1567         {17981, 39, 18},        /* 38  */
1568         {18955, 40, 18},        /* 39  */
1569         {19965, 41, 17},        /* 40  */
1570         {21013, 42, 17},        /* 41  */
1571         {22101, 43, 17},        /* 42  */
1572         {23230, 44, 17},        /* 43  */
1573         {24402, 45, 16},        /* 44  */
1574         {25618, 46, 16},        /* 45  */
1575         {26881, 47, 16},        /* 46  */
1576         {28193, 48, 16},        /* 47  */
1577         {29557, 49, 15},        /* 48  */
1578         {30975, 50, 15},        /* 49  */
1579         {32450, 51, 15},        /* 50  */
1580         {33986, 52, 15},        /* 51  */
1581         {35586, 53, 14},        /* 52  */
1582         {37253, 54, 14},        /* 53  */
1583         {38992, 55, 14},        /* 54  */
1584         {40808, 56, 14},        /* 55  */
1585         {42707, 57, 13},        /* 56  */
1586         {44694, 58, 13},        /* 57  */
1587         {46776, 59, 13},        /* 58  */
1588         {48961, 60, 13},        /* 59  */
1589         {51258, 61, 13},        /* 60  */
1590         {53677, 62, 12},        /* 61  */
1591         {56230, 63, 12},        /* 62  */
1592         {58932, 64, 12},        /* 63  */
1593         {61799, 65, 12},        /* 64  */
1594         {64851, 66, 11},        /* 65  */
1595         {68113, 67, 11},        /* 66  */
1596         {71617, 68, 11},        /* 67  */
1597         {75401, 69, 10},        /* 68  */
1598         {79517, 70, 10},        /* 69  */
1599         {84035, 71, 10},        /* 70  */
1600         {89053, 72, 10},        /* 71  */
1601         {94717, 73, 9}          /* 72  */
1602 };
1603
1604 static void
1605 sctp_hs_cwnd_increase(struct sctp_tcb *stcb, struct sctp_nets *net)
1606 {
1607         int cur_val, i, indx, incr;
1608         int old_cwnd = net->cwnd;
1609
1610         cur_val = net->cwnd >> 10;
1611         indx = SCTP_HS_TABLE_SIZE - 1;
1612
1613         if (cur_val < sctp_cwnd_adjust[0].cwnd) {
1614                 /* normal mode */
1615                 if (net->net_ack > net->mtu) {
1616                         net->cwnd += net->mtu;
1617                 } else {
1618                         net->cwnd += net->net_ack;
1619                 }
1620         } else {
1621                 for (i = net->last_hs_used; i < SCTP_HS_TABLE_SIZE; i++) {
1622                         if (cur_val < sctp_cwnd_adjust[i].cwnd) {
1623                                 indx = i;
1624                                 break;
1625                         }
1626                 }
1627                 net->last_hs_used = indx;
1628                 incr = (((int32_t)sctp_cwnd_adjust[indx].increase) << 10);
1629                 net->cwnd += incr;
1630         }
1631         sctp_enforce_cwnd_limit(&stcb->asoc, net);
1632         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1633                 sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_SS);
1634         }
1635 }
1636
1637 static void
1638 sctp_hs_cwnd_decrease(struct sctp_tcb *stcb, struct sctp_nets *net)
1639 {
1640         int cur_val, i, indx;
1641         int old_cwnd = net->cwnd;
1642
1643         cur_val = net->cwnd >> 10;
1644         if (cur_val < sctp_cwnd_adjust[0].cwnd) {
1645                 /* normal mode */
1646                 net->ssthresh = net->cwnd / 2;
1647                 if (net->ssthresh < (net->mtu * 2)) {
1648                         net->ssthresh = 2 * net->mtu;
1649                 }
1650                 net->cwnd = net->ssthresh;
1651         } else {
1652                 /* drop by the proper amount */
1653                 net->ssthresh = net->cwnd - (int)((net->cwnd / 100) *
1654                     (int32_t)sctp_cwnd_adjust[net->last_hs_used].drop_percent);
1655                 net->cwnd = net->ssthresh;
1656                 /* now where are we */
1657                 indx = net->last_hs_used;
1658                 cur_val = net->cwnd >> 10;
1659                 /* reset where we are in the table */
1660                 if (cur_val < sctp_cwnd_adjust[0].cwnd) {
1661                         /* feel out of hs */
1662                         net->last_hs_used = 0;
1663                 } else {
1664                         for (i = indx; i >= 1; i--) {
1665                                 if (cur_val > sctp_cwnd_adjust[i - 1].cwnd) {
1666                                         break;
1667                                 }
1668                         }
1669                         net->last_hs_used = indx;
1670                 }
1671         }
1672         sctp_enforce_cwnd_limit(&stcb->asoc, net);
1673         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1674                 sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_FR);
1675         }
1676 }
1677
1678 static void
1679 sctp_hs_cwnd_update_after_fr(struct sctp_tcb *stcb,
1680     struct sctp_association *asoc)
1681 {
1682         struct sctp_nets *net;
1683
1684         /*
1685          * CMT fast recovery code. Need to debug. ((sctp_cmt_on_off > 0) &&
1686          * (net->fast_retran_loss_recovery == 0)))
1687          */
1688         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
1689                 if ((asoc->fast_retran_loss_recovery == 0) ||
1690                     (asoc->sctp_cmt_on_off > 0)) {
1691                         /* out of a RFC2582 Fast recovery window? */
1692                         if (net->net_ack > 0) {
1693                                 /*
1694                                  * per section 7.2.3, are there any
1695                                  * destinations that had a fast retransmit
1696                                  * to them. If so what we need to do is
1697                                  * adjust ssthresh and cwnd.
1698                                  */
1699                                 struct sctp_tmit_chunk *lchk;
1700
1701                                 sctp_hs_cwnd_decrease(stcb, net);
1702
1703                                 lchk = TAILQ_FIRST(&asoc->send_queue);
1704
1705                                 net->partial_bytes_acked = 0;
1706                                 /* Turn on fast recovery window */
1707                                 asoc->fast_retran_loss_recovery = 1;
1708                                 if (lchk == NULL) {
1709                                         /* Mark end of the window */
1710                                         asoc->fast_recovery_tsn = asoc->sending_seq - 1;
1711                                 } else {
1712                                         asoc->fast_recovery_tsn = lchk->rec.data.tsn - 1;
1713                                 }
1714
1715                                 /*
1716                                  * CMT fast recovery -- per destination
1717                                  * recovery variable.
1718                                  */
1719                                 net->fast_retran_loss_recovery = 1;
1720
1721                                 if (lchk == NULL) {
1722                                         /* Mark end of the window */
1723                                         net->fast_recovery_tsn = asoc->sending_seq - 1;
1724                                 } else {
1725                                         net->fast_recovery_tsn = lchk->rec.data.tsn - 1;
1726                                 }
1727
1728                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND,
1729                                     stcb->sctp_ep, stcb, net,
1730                                     SCTP_FROM_SCTP_CC_FUNCTIONS + SCTP_LOC_2);
1731                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
1732                                     stcb->sctp_ep, stcb, net);
1733                         }
1734                 } else if (net->net_ack > 0) {
1735                         /*
1736                          * Mark a peg that we WOULD have done a cwnd
1737                          * reduction but RFC2582 prevented this action.
1738                          */
1739                         SCTP_STAT_INCR(sctps_fastretransinrtt);
1740                 }
1741         }
1742 }
1743
1744 static void
1745 sctp_hs_cwnd_update_after_sack(struct sctp_tcb *stcb,
1746     struct sctp_association *asoc,
1747     int accum_moved, int reneged_all SCTP_UNUSED, int will_exit)
1748 {
1749         struct sctp_nets *net;
1750
1751         /******************************/
1752         /* update cwnd and Early FR   */
1753         /******************************/
1754         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
1755 #ifdef JANA_CMT_FAST_RECOVERY
1756                 /*
1757                  * CMT fast recovery code. Need to debug.
1758                  */
1759                 if (net->fast_retran_loss_recovery && net->new_pseudo_cumack) {
1760                         if (SCTP_TSN_GE(asoc->last_acked_seq, net->fast_recovery_tsn) ||
1761                             SCTP_TSN_GE(net->pseudo_cumack, net->fast_recovery_tsn)) {
1762                                 net->will_exit_fast_recovery = 1;
1763                         }
1764                 }
1765 #endif
1766                 /* if nothing was acked on this destination skip it */
1767                 if (net->net_ack == 0) {
1768                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
1769                                 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FROM_SACK);
1770                         }
1771                         continue;
1772                 }
1773 #ifdef JANA_CMT_FAST_RECOVERY
1774                 /*
1775                  * CMT fast recovery code
1776                  */
1777                 /*
1778                  * if (sctp_cmt_on_off > 0 && net->fast_retran_loss_recovery
1779                  * && net->will_exit_fast_recovery == 0) { @@@ Do something
1780                  * } else if (sctp_cmt_on_off == 0 &&
1781                  * asoc->fast_retran_loss_recovery && will_exit == 0) {
1782                  */
1783 #endif
1784
1785                 if (asoc->fast_retran_loss_recovery &&
1786                     (will_exit == 0) &&
1787                     (asoc->sctp_cmt_on_off == 0)) {
1788                         /*
1789                          * If we are in loss recovery we skip any cwnd
1790                          * update
1791                          */
1792                         return;
1793                 }
1794                 /*
1795                  * CMT: CUC algorithm. Update cwnd if pseudo-cumack has
1796                  * moved.
1797                  */
1798                 if (accum_moved ||
1799                     ((asoc->sctp_cmt_on_off > 0) && net->new_pseudo_cumack)) {
1800                         /* If the cumulative ack moved we can proceed */
1801                         if (net->cwnd <= net->ssthresh) {
1802                                 /* We are in slow start */
1803                                 if (net->flight_size + net->net_ack >= net->cwnd) {
1804                                         sctp_hs_cwnd_increase(stcb, net);
1805                                 } else {
1806                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
1807                                                 sctp_log_cwnd(stcb, net, net->net_ack,
1808                                                     SCTP_CWND_LOG_NOADV_SS);
1809                                         }
1810                                 }
1811                         } else {
1812                                 /* We are in congestion avoidance */
1813                                 net->partial_bytes_acked += net->net_ack;
1814                                 if ((net->flight_size + net->net_ack >= net->cwnd) &&
1815                                     (net->partial_bytes_acked >= net->cwnd)) {
1816                                         net->partial_bytes_acked -= net->cwnd;
1817                                         net->cwnd += net->mtu;
1818                                         sctp_enforce_cwnd_limit(asoc, net);
1819                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1820                                                 sctp_log_cwnd(stcb, net, net->mtu,
1821                                                     SCTP_CWND_LOG_FROM_CA);
1822                                         }
1823                                 } else {
1824                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
1825                                                 sctp_log_cwnd(stcb, net, net->net_ack,
1826                                                     SCTP_CWND_LOG_NOADV_CA);
1827                                         }
1828                                 }
1829                         }
1830                 } else {
1831                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
1832                                 sctp_log_cwnd(stcb, net, net->mtu,
1833                                     SCTP_CWND_LOG_NO_CUMACK);
1834                         }
1835                 }
1836         }
1837 }
1838
1839 /*
1840  * H-TCP congestion control. The algorithm is detailed in:
1841  * R.N.Shorten, D.J.Leith:
1842  *   "H-TCP: TCP for high-speed and long-distance networks"
1843  *   Proc. PFLDnet, Argonne, 2004.
1844  * http://www.hamilton.ie/net/htcp3.pdf
1845  */
1846
1847 static int use_rtt_scaling = 1;
1848 static int use_bandwidth_switch = 1;
1849
1850 static inline int
1851 between(uint32_t seq1, uint32_t seq2, uint32_t seq3)
1852 {
1853         return (seq3 - seq2 >= seq1 - seq2);
1854 }
1855
1856 static inline uint32_t
1857 htcp_cong_time(struct htcp *ca)
1858 {
1859         return (sctp_get_tick_count() - ca->last_cong);
1860 }
1861
1862 static inline uint32_t
1863 htcp_ccount(struct htcp *ca)
1864 {
1865         return (ca->minRTT == 0 ? htcp_cong_time(ca) : htcp_cong_time(ca) / ca->minRTT);
1866 }
1867
1868 static inline void
1869 htcp_reset(struct htcp *ca)
1870 {
1871         ca->undo_last_cong = ca->last_cong;
1872         ca->undo_maxRTT = ca->maxRTT;
1873         ca->undo_old_maxB = ca->old_maxB;
1874         ca->last_cong = sctp_get_tick_count();
1875 }
1876
1877 #ifdef SCTP_NOT_USED
1878
1879 static uint32_t
1880 htcp_cwnd_undo(struct sctp_tcb *stcb, struct sctp_nets *net)
1881 {
1882         net->cc_mod.htcp_ca.last_cong = net->cc_mod.htcp_ca.undo_last_cong;
1883         net->cc_mod.htcp_ca.maxRTT = net->cc_mod.htcp_ca.undo_maxRTT;
1884         net->cc_mod.htcp_ca.old_maxB = net->cc_mod.htcp_ca.undo_old_maxB;
1885         return (max(net->cwnd, ((net->ssthresh / net->mtu << 7) / net->cc_mod.htcp_ca.beta) * net->mtu));
1886 }
1887
1888 #endif
1889
1890 static inline void
1891 measure_rtt(struct sctp_nets *net)
1892 {
1893         uint32_t srtt = net->lastsa >> SCTP_RTT_SHIFT;
1894
1895         /* keep track of minimum RTT seen so far, minRTT is zero at first */
1896         if (net->cc_mod.htcp_ca.minRTT > srtt || !net->cc_mod.htcp_ca.minRTT)
1897                 net->cc_mod.htcp_ca.minRTT = srtt;
1898
1899         /* max RTT */
1900         if (net->fast_retran_ip == 0 && net->ssthresh < 0xFFFF && htcp_ccount(&net->cc_mod.htcp_ca) > 3) {
1901                 if (net->cc_mod.htcp_ca.maxRTT < net->cc_mod.htcp_ca.minRTT)
1902                         net->cc_mod.htcp_ca.maxRTT = net->cc_mod.htcp_ca.minRTT;
1903                 if (net->cc_mod.htcp_ca.maxRTT < srtt && srtt <= net->cc_mod.htcp_ca.maxRTT + sctp_msecs_to_ticks(20))
1904                         net->cc_mod.htcp_ca.maxRTT = srtt;
1905         }
1906 }
1907
1908 static void
1909 measure_achieved_throughput(struct sctp_nets *net)
1910 {
1911         uint32_t now = sctp_get_tick_count();
1912
1913         if (net->fast_retran_ip == 0)
1914                 net->cc_mod.htcp_ca.bytes_acked = net->net_ack;
1915
1916         if (!use_bandwidth_switch)
1917                 return;
1918
1919         /* achieved throughput calculations */
1920         /* JRS - not 100% sure of this statement */
1921         if (net->fast_retran_ip == 1) {
1922                 net->cc_mod.htcp_ca.bytecount = 0;
1923                 net->cc_mod.htcp_ca.lasttime = now;
1924                 return;
1925         }
1926
1927         net->cc_mod.htcp_ca.bytecount += net->net_ack;
1928         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)) &&
1929             (now - net->cc_mod.htcp_ca.lasttime >= net->cc_mod.htcp_ca.minRTT) &&
1930             (net->cc_mod.htcp_ca.minRTT > 0)) {
1931                 uint32_t cur_Bi = net->cc_mod.htcp_ca.bytecount / net->mtu * hz / (now - net->cc_mod.htcp_ca.lasttime);
1932
1933                 if (htcp_ccount(&net->cc_mod.htcp_ca) <= 3) {
1934                         /* just after backoff */
1935                         net->cc_mod.htcp_ca.minB = net->cc_mod.htcp_ca.maxB = net->cc_mod.htcp_ca.Bi = cur_Bi;
1936                 } else {
1937                         net->cc_mod.htcp_ca.Bi = (3 * net->cc_mod.htcp_ca.Bi + cur_Bi) / 4;
1938                         if (net->cc_mod.htcp_ca.Bi > net->cc_mod.htcp_ca.maxB)
1939                                 net->cc_mod.htcp_ca.maxB = net->cc_mod.htcp_ca.Bi;
1940                         if (net->cc_mod.htcp_ca.minB > net->cc_mod.htcp_ca.maxB)
1941                                 net->cc_mod.htcp_ca.minB = net->cc_mod.htcp_ca.maxB;
1942                 }
1943                 net->cc_mod.htcp_ca.bytecount = 0;
1944                 net->cc_mod.htcp_ca.lasttime = now;
1945         }
1946 }
1947
1948 static inline void
1949 htcp_beta_update(struct htcp *ca, uint32_t minRTT, uint32_t maxRTT)
1950 {
1951         if (use_bandwidth_switch) {
1952                 uint32_t maxB = ca->maxB;
1953                 uint32_t old_maxB = ca->old_maxB;
1954
1955                 ca->old_maxB = ca->maxB;
1956
1957                 if (!between(5 * maxB, 4 * old_maxB, 6 * old_maxB)) {
1958                         ca->beta = BETA_MIN;
1959                         ca->modeswitch = 0;
1960                         return;
1961                 }
1962         }
1963
1964         if (ca->modeswitch && minRTT > sctp_msecs_to_ticks(10) && maxRTT) {
1965                 ca->beta = (minRTT << 7) / maxRTT;
1966                 if (ca->beta < BETA_MIN)
1967                         ca->beta = BETA_MIN;
1968                 else if (ca->beta > BETA_MAX)
1969                         ca->beta = BETA_MAX;
1970         } else {
1971                 ca->beta = BETA_MIN;
1972                 ca->modeswitch = 1;
1973         }
1974 }
1975
1976 static inline void
1977 htcp_alpha_update(struct htcp *ca)
1978 {
1979         uint32_t minRTT = ca->minRTT;
1980         uint32_t factor = 1;
1981         uint32_t diff = htcp_cong_time(ca);
1982
1983         if (diff > (uint32_t)hz) {
1984                 diff -= hz;
1985                 factor = 1 + (10 * diff + ((diff / 2) * (diff / 2) / hz)) / hz;
1986         }
1987
1988         if (use_rtt_scaling && minRTT) {
1989                 uint32_t scale = (hz << 3) / (10 * minRTT);
1990
1991                 scale = min(max(scale, 1U << 2), 10U << 3);     /* clamping ratio to
1992                                                                  * interval [0.5,10]<<3 */
1993                 factor = (factor << 3) / scale;
1994                 if (factor != 0)
1995                         factor = 1;
1996         }
1997
1998         ca->alpha = 2 * factor * ((1 << 7) - ca->beta);
1999         if (ca->alpha != 0)
2000                 ca->alpha = ALPHA_BASE;
2001 }
2002
2003 /* After we have the rtt data to calculate beta, we'd still prefer to wait one
2004  * rtt before we adjust our beta to ensure we are working from a consistent
2005  * data.
2006  *
2007  * This function should be called when we hit a congestion event since only at
2008  * that point do we really have a real sense of maxRTT (the queues en route
2009  * were getting just too full now).
2010  */
2011 static void
2012 htcp_param_update(struct sctp_nets *net)
2013 {
2014         uint32_t minRTT = net->cc_mod.htcp_ca.minRTT;
2015         uint32_t maxRTT = net->cc_mod.htcp_ca.maxRTT;
2016
2017         htcp_beta_update(&net->cc_mod.htcp_ca, minRTT, maxRTT);
2018         htcp_alpha_update(&net->cc_mod.htcp_ca);
2019
2020         /*
2021          * add slowly fading memory for maxRTT to accommodate routing
2022          * changes etc
2023          */
2024         if (minRTT > 0 && maxRTT > minRTT)
2025                 net->cc_mod.htcp_ca.maxRTT = minRTT + ((maxRTT - minRTT) * 95) / 100;
2026 }
2027
2028 static uint32_t
2029 htcp_recalc_ssthresh(struct sctp_nets *net)
2030 {
2031         htcp_param_update(net);
2032         return (max(((net->cwnd / net->mtu * net->cc_mod.htcp_ca.beta) >> 7) * net->mtu, 2U * net->mtu));
2033 }
2034
2035 static void
2036 htcp_cong_avoid(struct sctp_tcb *stcb, struct sctp_nets *net)
2037 {
2038         /*-
2039          * How to handle these functions?
2040          *      if (!tcp_is_cwnd_limited(sk, in_flight)) RRS - good question.
2041          *              return;
2042          */
2043         if (net->cwnd <= net->ssthresh) {
2044                 /* We are in slow start */
2045                 if (net->flight_size + net->net_ack >= net->cwnd) {
2046                         if (net->net_ack > (net->mtu * SCTP_BASE_SYSCTL(sctp_L2_abc_variable))) {
2047                                 net->cwnd += (net->mtu * SCTP_BASE_SYSCTL(sctp_L2_abc_variable));
2048                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
2049                                         sctp_log_cwnd(stcb, net, net->mtu,
2050                                             SCTP_CWND_LOG_FROM_SS);
2051                                 }
2052
2053                         } else {
2054                                 net->cwnd += net->net_ack;
2055                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
2056                                         sctp_log_cwnd(stcb, net, net->net_ack,
2057                                             SCTP_CWND_LOG_FROM_SS);
2058                                 }
2059                         }
2060                         sctp_enforce_cwnd_limit(&stcb->asoc, net);
2061                 } else {
2062                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
2063                                 sctp_log_cwnd(stcb, net, net->net_ack,
2064                                     SCTP_CWND_LOG_NOADV_SS);
2065                         }
2066                 }
2067         } else {
2068                 measure_rtt(net);
2069
2070                 /*
2071                  * In dangerous area, increase slowly. In theory this is
2072                  * net->cwnd += alpha / net->cwnd
2073                  */
2074                 /* What is snd_cwnd_cnt?? */
2075                 if (((net->partial_bytes_acked / net->mtu * net->cc_mod.htcp_ca.alpha) >> 7) * net->mtu >= net->cwnd) {
2076                         /*-
2077                          * Does SCTP have a cwnd clamp?
2078                          * if (net->snd_cwnd < net->snd_cwnd_clamp) - Nope (RRS).
2079                          */
2080                         net->cwnd += net->mtu;
2081                         net->partial_bytes_acked = 0;
2082                         sctp_enforce_cwnd_limit(&stcb->asoc, net);
2083                         htcp_alpha_update(&net->cc_mod.htcp_ca);
2084                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
2085                                 sctp_log_cwnd(stcb, net, net->mtu,
2086                                     SCTP_CWND_LOG_FROM_CA);
2087                         }
2088                 } else {
2089                         net->partial_bytes_acked += net->net_ack;
2090                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
2091                                 sctp_log_cwnd(stcb, net, net->net_ack,
2092                                     SCTP_CWND_LOG_NOADV_CA);
2093                         }
2094                 }
2095
2096                 net->cc_mod.htcp_ca.bytes_acked = net->mtu;
2097         }
2098 }
2099
2100 #ifdef SCTP_NOT_USED
2101 /* Lower bound on congestion window. */
2102 static uint32_t
2103 htcp_min_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net)
2104 {
2105         return (net->ssthresh);
2106 }
2107 #endif
2108
2109 static void
2110 htcp_init(struct sctp_nets *net)
2111 {
2112         memset(&net->cc_mod.htcp_ca, 0, sizeof(struct htcp));
2113         net->cc_mod.htcp_ca.alpha = ALPHA_BASE;
2114         net->cc_mod.htcp_ca.beta = BETA_MIN;
2115         net->cc_mod.htcp_ca.bytes_acked = net->mtu;
2116         net->cc_mod.htcp_ca.last_cong = sctp_get_tick_count();
2117 }
2118
2119 static void
2120 sctp_htcp_set_initial_cc_param(struct sctp_tcb *stcb, struct sctp_nets *net)
2121 {
2122         /*
2123          * We take the max of the burst limit times a MTU or the
2124          * INITIAL_CWND. We then limit this to 4 MTU's of sending.
2125          */
2126         net->cwnd = min((net->mtu * 4), max((2 * net->mtu), SCTP_INITIAL_CWND));
2127         net->ssthresh = stcb->asoc.peers_rwnd;
2128         sctp_enforce_cwnd_limit(&stcb->asoc, net);
2129         htcp_init(net);
2130
2131         if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
2132                 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_INITIALIZATION);
2133         }
2134 }
2135
2136 static void
2137 sctp_htcp_cwnd_update_after_sack(struct sctp_tcb *stcb,
2138     struct sctp_association *asoc,
2139     int accum_moved, int reneged_all SCTP_UNUSED, int will_exit)
2140 {
2141         struct sctp_nets *net;
2142
2143         /******************************/
2144         /* update cwnd and Early FR   */
2145         /******************************/
2146         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
2147 #ifdef JANA_CMT_FAST_RECOVERY
2148                 /*
2149                  * CMT fast recovery code. Need to debug.
2150                  */
2151                 if (net->fast_retran_loss_recovery && net->new_pseudo_cumack) {
2152                         if (SCTP_TSN_GE(asoc->last_acked_seq, net->fast_recovery_tsn) ||
2153                             SCTP_TSN_GE(net->pseudo_cumack, net->fast_recovery_tsn)) {
2154                                 net->will_exit_fast_recovery = 1;
2155                         }
2156                 }
2157 #endif
2158                 /* if nothing was acked on this destination skip it */
2159                 if (net->net_ack == 0) {
2160                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
2161                                 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FROM_SACK);
2162                         }
2163                         continue;
2164                 }
2165 #ifdef JANA_CMT_FAST_RECOVERY
2166                 /*
2167                  * CMT fast recovery code
2168                  */
2169                 /*
2170                  * if (sctp_cmt_on_off > 0 && net->fast_retran_loss_recovery
2171                  * && net->will_exit_fast_recovery == 0) { @@@ Do something
2172                  * } else if (sctp_cmt_on_off == 0 &&
2173                  * asoc->fast_retran_loss_recovery && will_exit == 0) {
2174                  */
2175 #endif
2176
2177                 if (asoc->fast_retran_loss_recovery &&
2178                     will_exit == 0 &&
2179                     (asoc->sctp_cmt_on_off == 0)) {
2180                         /*
2181                          * If we are in loss recovery we skip any cwnd
2182                          * update
2183                          */
2184                         return;
2185                 }
2186                 /*
2187                  * CMT: CUC algorithm. Update cwnd if pseudo-cumack has
2188                  * moved.
2189                  */
2190                 if (accum_moved ||
2191                     ((asoc->sctp_cmt_on_off > 0) && net->new_pseudo_cumack)) {
2192                         htcp_cong_avoid(stcb, net);
2193                         measure_achieved_throughput(net);
2194                 } else {
2195                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
2196                                 sctp_log_cwnd(stcb, net, net->mtu,
2197                                     SCTP_CWND_LOG_NO_CUMACK);
2198                         }
2199                 }
2200         }
2201 }
2202
2203 static void
2204 sctp_htcp_cwnd_update_after_fr(struct sctp_tcb *stcb,
2205     struct sctp_association *asoc)
2206 {
2207         struct sctp_nets *net;
2208
2209         /*
2210          * CMT fast recovery code. Need to debug. ((sctp_cmt_on_off > 0) &&
2211          * (net->fast_retran_loss_recovery == 0)))
2212          */
2213         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
2214                 if ((asoc->fast_retran_loss_recovery == 0) ||
2215                     (asoc->sctp_cmt_on_off > 0)) {
2216                         /* out of a RFC2582 Fast recovery window? */
2217                         if (net->net_ack > 0) {
2218                                 /*
2219                                  * per section 7.2.3, are there any
2220                                  * destinations that had a fast retransmit
2221                                  * to them. If so what we need to do is
2222                                  * adjust ssthresh and cwnd.
2223                                  */
2224                                 struct sctp_tmit_chunk *lchk;
2225                                 int old_cwnd = net->cwnd;
2226
2227                                 /* JRS - reset as if state were changed */
2228                                 htcp_reset(&net->cc_mod.htcp_ca);
2229                                 net->ssthresh = htcp_recalc_ssthresh(net);
2230                                 net->cwnd = net->ssthresh;
2231                                 sctp_enforce_cwnd_limit(asoc, net);
2232                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
2233                                         sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd),
2234                                             SCTP_CWND_LOG_FROM_FR);
2235                                 }
2236                                 lchk = TAILQ_FIRST(&asoc->send_queue);
2237
2238                                 net->partial_bytes_acked = 0;
2239                                 /* Turn on fast recovery window */
2240                                 asoc->fast_retran_loss_recovery = 1;
2241                                 if (lchk == NULL) {
2242                                         /* Mark end of the window */
2243                                         asoc->fast_recovery_tsn = asoc->sending_seq - 1;
2244                                 } else {
2245                                         asoc->fast_recovery_tsn = lchk->rec.data.tsn - 1;
2246                                 }
2247
2248                                 /*
2249                                  * CMT fast recovery -- per destination
2250                                  * recovery variable.
2251                                  */
2252                                 net->fast_retran_loss_recovery = 1;
2253
2254                                 if (lchk == NULL) {
2255                                         /* Mark end of the window */
2256                                         net->fast_recovery_tsn = asoc->sending_seq - 1;
2257                                 } else {
2258                                         net->fast_recovery_tsn = lchk->rec.data.tsn - 1;
2259                                 }
2260
2261                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND,
2262                                     stcb->sctp_ep, stcb, net,
2263                                     SCTP_FROM_SCTP_CC_FUNCTIONS + SCTP_LOC_3);
2264                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
2265                                     stcb->sctp_ep, stcb, net);
2266                         }
2267                 } else if (net->net_ack > 0) {
2268                         /*
2269                          * Mark a peg that we WOULD have done a cwnd
2270                          * reduction but RFC2582 prevented this action.
2271                          */
2272                         SCTP_STAT_INCR(sctps_fastretransinrtt);
2273                 }
2274         }
2275 }
2276
2277 static void
2278 sctp_htcp_cwnd_update_after_timeout(struct sctp_tcb *stcb,
2279     struct sctp_nets *net)
2280 {
2281         int old_cwnd = net->cwnd;
2282
2283         /* JRS - reset as if the state were being changed to timeout */
2284         htcp_reset(&net->cc_mod.htcp_ca);
2285         net->ssthresh = htcp_recalc_ssthresh(net);
2286         net->cwnd = net->mtu;
2287         net->partial_bytes_acked = 0;
2288         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
2289                 sctp_log_cwnd(stcb, net, net->cwnd - old_cwnd, SCTP_CWND_LOG_FROM_RTX);
2290         }
2291 }
2292
2293 static void
2294 sctp_htcp_cwnd_update_after_ecn_echo(struct sctp_tcb *stcb,
2295     struct sctp_nets *net, int in_window, int num_pkt_lost SCTP_UNUSED)
2296 {
2297         int old_cwnd;
2298
2299         old_cwnd = net->cwnd;
2300
2301         /* JRS - reset hctp as if state changed */
2302         if (in_window == 0) {
2303                 htcp_reset(&net->cc_mod.htcp_ca);
2304                 SCTP_STAT_INCR(sctps_ecnereducedcwnd);
2305                 net->ssthresh = htcp_recalc_ssthresh(net);
2306                 if (net->ssthresh < net->mtu) {
2307                         net->ssthresh = net->mtu;
2308                         /* here back off the timer as well, to slow us down */
2309                         net->RTO <<= 1;
2310                 }
2311                 net->cwnd = net->ssthresh;
2312                 sctp_enforce_cwnd_limit(&stcb->asoc, net);
2313                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
2314                         sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_SAT);
2315                 }
2316         }
2317 }
2318
2319 const struct sctp_cc_functions sctp_cc_functions[] = {
2320         {
2321                 .sctp_set_initial_cc_param = sctp_set_initial_cc_param,
2322                 .sctp_cwnd_update_after_sack = sctp_cwnd_update_after_sack,
2323                 .sctp_cwnd_update_exit_pf = sctp_cwnd_update_exit_pf_common,
2324                 .sctp_cwnd_update_after_fr = sctp_cwnd_update_after_fr,
2325                 .sctp_cwnd_update_after_timeout = sctp_cwnd_update_after_timeout,
2326                 .sctp_cwnd_update_after_ecn_echo = sctp_cwnd_update_after_ecn_echo,
2327                 .sctp_cwnd_update_after_packet_dropped = sctp_cwnd_update_after_packet_dropped,
2328                 .sctp_cwnd_update_after_output = sctp_cwnd_update_after_output,
2329         },
2330         {
2331                 .sctp_set_initial_cc_param = sctp_set_initial_cc_param,
2332                 .sctp_cwnd_update_after_sack = sctp_hs_cwnd_update_after_sack,
2333                 .sctp_cwnd_update_exit_pf = sctp_cwnd_update_exit_pf_common,
2334                 .sctp_cwnd_update_after_fr = sctp_hs_cwnd_update_after_fr,
2335                 .sctp_cwnd_update_after_timeout = sctp_cwnd_update_after_timeout,
2336                 .sctp_cwnd_update_after_ecn_echo = sctp_cwnd_update_after_ecn_echo,
2337                 .sctp_cwnd_update_after_packet_dropped = sctp_cwnd_update_after_packet_dropped,
2338                 .sctp_cwnd_update_after_output = sctp_cwnd_update_after_output,
2339         },
2340         {
2341                 .sctp_set_initial_cc_param = sctp_htcp_set_initial_cc_param,
2342                 .sctp_cwnd_update_after_sack = sctp_htcp_cwnd_update_after_sack,
2343                 .sctp_cwnd_update_exit_pf = sctp_cwnd_update_exit_pf_common,
2344                 .sctp_cwnd_update_after_fr = sctp_htcp_cwnd_update_after_fr,
2345                 .sctp_cwnd_update_after_timeout = sctp_htcp_cwnd_update_after_timeout,
2346                 .sctp_cwnd_update_after_ecn_echo = sctp_htcp_cwnd_update_after_ecn_echo,
2347                 .sctp_cwnd_update_after_packet_dropped = sctp_cwnd_update_after_packet_dropped,
2348                 .sctp_cwnd_update_after_output = sctp_cwnd_update_after_output,
2349         },
2350         {
2351                 .sctp_set_initial_cc_param = sctp_set_rtcc_initial_cc_param,
2352                 .sctp_cwnd_update_after_sack = sctp_cwnd_update_rtcc_after_sack,
2353                 .sctp_cwnd_update_exit_pf = sctp_cwnd_update_exit_pf_common,
2354                 .sctp_cwnd_update_after_fr = sctp_cwnd_update_after_fr,
2355                 .sctp_cwnd_update_after_timeout = sctp_cwnd_update_after_timeout,
2356                 .sctp_cwnd_update_after_ecn_echo = sctp_cwnd_update_rtcc_after_ecn_echo,
2357                 .sctp_cwnd_update_after_packet_dropped = sctp_cwnd_update_after_packet_dropped,
2358                 .sctp_cwnd_update_after_output = sctp_cwnd_update_after_output,
2359                 .sctp_cwnd_update_packet_transmitted = sctp_cwnd_update_rtcc_packet_transmitted,
2360                 .sctp_cwnd_update_tsn_acknowledged = sctp_cwnd_update_rtcc_tsn_acknowledged,
2361                 .sctp_cwnd_new_transmission_begins = sctp_cwnd_new_rtcc_transmission_begins,
2362                 .sctp_cwnd_prepare_net_for_sack = sctp_cwnd_prepare_rtcc_net_for_sack,
2363                 .sctp_cwnd_socket_option = sctp_cwnd_rtcc_socket_option,
2364                 .sctp_rtt_calculated = sctp_rtt_rtcc_calculated
2365         }
2366 };