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