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