xref: /freebsd/sys/netinet/sctp_cc_functions.c (revision 4f1f4356f3012928b463f9ef1710fb908e48b1e2)
1 /*-
2  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * a) Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  *
10  * b) Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *   the documentation and/or other materials provided with the distribution.
13  *
14  * c) Neither the name of Cisco Systems, Inc. nor the names of its
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <netinet/sctp_os.h>
32 #include <netinet/sctp_var.h>
33 #include <netinet/sctp_sysctl.h>
34 #include <netinet/sctp_pcb.h>
35 #include <netinet/sctp_header.h>
36 #include <netinet/sctputil.h>
37 #include <netinet/sctp_output.h>
38 #include <netinet/sctp_input.h>
39 #include <netinet/sctp_indata.h>
40 #include <netinet/sctp_uio.h>
41 #include <netinet/sctp_timer.h>
42 #include <netinet/sctp_auth.h>
43 #include <netinet/sctp_asconf.h>
44 #include <netinet/sctp_cc_functions.h>
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 void
48 sctp_set_initial_cc_param(struct sctp_tcb *stcb, struct sctp_nets *net)
49 {
50 	/*
51 	 * We take the max of the burst limit times a MTU or the
52 	 * INITIAL_CWND. We then limit this to 4 MTU's of sending. cwnd must
53 	 * be at least 2 MTU.
54 	 */
55 	net->cwnd = min((net->mtu * 4), max((2 * net->mtu), SCTP_INITIAL_CWND));
56 	net->ssthresh = stcb->asoc.peers_rwnd;
57 
58 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
59 		sctp_log_cwnd(stcb, net, 0, SCTP_CWND_INITIALIZATION);
60 	}
61 }
62 
63 void
64 sctp_cwnd_update_after_fr(struct sctp_tcb *stcb,
65     struct sctp_association *asoc)
66 {
67 	struct sctp_nets *net;
68 
69 	/*-
70 	 * CMT fast recovery code. Need to debug. ((sctp_cmt_on_off == 1) &&
71 	 * (net->fast_retran_loss_recovery == 0)))
72 	 */
73 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
74 		if ((asoc->fast_retran_loss_recovery == 0) ||
75 		    (asoc->sctp_cmt_on_off == 1)) {
76 			/* out of a RFC2582 Fast recovery window? */
77 			if (net->net_ack > 0) {
78 				/*
79 				 * per section 7.2.3, are there any
80 				 * destinations that had a fast retransmit
81 				 * to them. If so what we need to do is
82 				 * adjust ssthresh and cwnd.
83 				 */
84 				struct sctp_tmit_chunk *lchk;
85 				int old_cwnd = net->cwnd;
86 
87 				net->ssthresh = net->cwnd / 2;
88 				if (net->ssthresh < (net->mtu * 2)) {
89 					net->ssthresh = 2 * net->mtu;
90 				}
91 				net->cwnd = net->ssthresh;
92 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
93 					sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd),
94 					    SCTP_CWND_LOG_FROM_FR);
95 				}
96 				lchk = TAILQ_FIRST(&asoc->send_queue);
97 
98 				net->partial_bytes_acked = 0;
99 				/* Turn on fast recovery window */
100 				asoc->fast_retran_loss_recovery = 1;
101 				if (lchk == NULL) {
102 					/* Mark end of the window */
103 					asoc->fast_recovery_tsn = asoc->sending_seq - 1;
104 				} else {
105 					asoc->fast_recovery_tsn = lchk->rec.data.TSN_seq - 1;
106 				}
107 
108 				/*
109 				 * CMT fast recovery -- per destination
110 				 * recovery variable.
111 				 */
112 				net->fast_retran_loss_recovery = 1;
113 
114 				if (lchk == NULL) {
115 					/* Mark end of the window */
116 					net->fast_recovery_tsn = asoc->sending_seq - 1;
117 				} else {
118 					net->fast_recovery_tsn = lchk->rec.data.TSN_seq - 1;
119 				}
120 
121 				/*
122 				 * Disable Nonce Sum Checking and store the
123 				 * resync tsn
124 				 */
125 				asoc->nonce_sum_check = 0;
126 				asoc->nonce_resync_tsn = asoc->fast_recovery_tsn + 1;
127 
128 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND,
129 				    stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_32);
130 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
131 				    stcb->sctp_ep, stcb, net);
132 			}
133 		} else if (net->net_ack > 0) {
134 			/*
135 			 * Mark a peg that we WOULD have done a cwnd
136 			 * reduction but RFC2582 prevented this action.
137 			 */
138 			SCTP_STAT_INCR(sctps_fastretransinrtt);
139 		}
140 	}
141 }
142 
143 void
144 sctp_cwnd_update_after_sack(struct sctp_tcb *stcb,
145     struct sctp_association *asoc,
146     int accum_moved, int reneged_all, int will_exit)
147 {
148 	struct sctp_nets *net;
149 
150 	/******************************/
151 	/* update cwnd and Early FR   */
152 	/******************************/
153 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
154 
155 #ifdef JANA_CMT_FAST_RECOVERY
156 		/*
157 		 * CMT fast recovery code. Need to debug.
158 		 */
159 		if (net->fast_retran_loss_recovery && net->new_pseudo_cumack) {
160 			if (compare_with_wrap(asoc->last_acked_seq,
161 			    net->fast_recovery_tsn, MAX_TSN) ||
162 			    (asoc->last_acked_seq == net->fast_recovery_tsn) ||
163 			    compare_with_wrap(net->pseudo_cumack, net->fast_recovery_tsn, MAX_TSN) ||
164 			    (net->pseudo_cumack == net->fast_recovery_tsn)) {
165 				net->will_exit_fast_recovery = 1;
166 			}
167 		}
168 #endif
169 		if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
170 			/*
171 			 * So, first of all do we need to have a Early FR
172 			 * timer running?
173 			 */
174 			if (((TAILQ_FIRST(&asoc->sent_queue)) &&
175 			    (net->ref_count > 1) &&
176 			    (net->flight_size < net->cwnd)) ||
177 			    (reneged_all)) {
178 				/*
179 				 * yes, so in this case stop it if its
180 				 * running, and then restart it. Reneging
181 				 * all is a special case where we want to
182 				 * run the Early FR timer and then force the
183 				 * last few unacked to be sent, causing us
184 				 * to illicit a sack with gaps to force out
185 				 * the others.
186 				 */
187 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
188 					SCTP_STAT_INCR(sctps_earlyfrstpidsck2);
189 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
190 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_20);
191 				}
192 				SCTP_STAT_INCR(sctps_earlyfrstrid);
193 				sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net);
194 			} else {
195 				/* No, stop it if its running */
196 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
197 					SCTP_STAT_INCR(sctps_earlyfrstpidsck3);
198 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
199 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_21);
200 				}
201 			}
202 		}
203 		/* if nothing was acked on this destination skip it */
204 		if (net->net_ack == 0) {
205 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
206 				sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FROM_SACK);
207 			}
208 			continue;
209 		}
210 		if (net->net_ack2 > 0) {
211 			/*
212 			 * Karn's rule applies to clearing error count, this
213 			 * is optional.
214 			 */
215 			net->error_count = 0;
216 			if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
217 			    SCTP_ADDR_NOT_REACHABLE) {
218 				/* addr came good */
219 				net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE;
220 				net->dest_state |= SCTP_ADDR_REACHABLE;
221 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
222 				    SCTP_RECEIVED_SACK, (void *)net, SCTP_SO_NOT_LOCKED);
223 				/* now was it the primary? if so restore */
224 				if (net->dest_state & SCTP_ADDR_WAS_PRIMARY) {
225 					(void)sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net);
226 				}
227 			}
228 			/*
229 			 * JRS 5/14/07 - If CMT PF is on and the destination
230 			 * is in PF state, set the destination to active
231 			 * state and set the cwnd to one or two MTU's based
232 			 * on whether PF1 or PF2 is being used.
233 			 *
234 			 * Should we stop any running T3 timer here?
235 			 */
236 			if ((asoc->sctp_cmt_on_off == 1) &&
237 			    (asoc->sctp_cmt_pf > 0) &&
238 			    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
239 				net->dest_state &= ~SCTP_ADDR_PF;
240 				net->cwnd = net->mtu * asoc->sctp_cmt_pf;
241 				SCTPDBG(SCTP_DEBUG_INDATA1, "Destination %p moved from PF to reachable with cwnd %d.\n",
242 				    net, net->cwnd);
243 				/*
244 				 * Since the cwnd value is explicitly set,
245 				 * skip the code that updates the cwnd
246 				 * value.
247 				 */
248 				goto skip_cwnd_update;
249 			}
250 		}
251 #ifdef JANA_CMT_FAST_RECOVERY
252 		/*
253 		 * CMT fast recovery code
254 		 */
255 		/*
256 		 * if (sctp_cmt_on_off == 1 &&
257 		 * net->fast_retran_loss_recovery &&
258 		 * net->will_exit_fast_recovery == 0) { @@@ Do something }
259 		 * else if (sctp_cmt_on_off == 0 &&
260 		 * asoc->fast_retran_loss_recovery && will_exit == 0) {
261 		 */
262 #endif
263 
264 		if (asoc->fast_retran_loss_recovery &&
265 		    (will_exit == 0) &&
266 		    (asoc->sctp_cmt_on_off == 0)) {
267 			/*
268 			 * If we are in loss recovery we skip any cwnd
269 			 * update
270 			 */
271 			goto skip_cwnd_update;
272 		}
273 		/*
274 		 * CMT: CUC algorithm. Update cwnd if pseudo-cumack has
275 		 * moved.
276 		 */
277 		if (accum_moved ||
278 		    ((asoc->sctp_cmt_on_off == 1) && net->new_pseudo_cumack)) {
279 			/* If the cumulative ack moved we can proceed */
280 			if (net->cwnd <= net->ssthresh) {
281 				/* We are in slow start */
282 				if (net->flight_size + net->net_ack >= net->cwnd) {
283 					if (net->net_ack > (net->mtu * SCTP_BASE_SYSCTL(sctp_L2_abc_variable))) {
284 						net->cwnd += (net->mtu * SCTP_BASE_SYSCTL(sctp_L2_abc_variable));
285 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
286 							sctp_log_cwnd(stcb, net, net->mtu,
287 							    SCTP_CWND_LOG_FROM_SS);
288 						}
289 					} else {
290 						net->cwnd += net->net_ack;
291 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
292 							sctp_log_cwnd(stcb, net, net->net_ack,
293 							    SCTP_CWND_LOG_FROM_SS);
294 						}
295 					}
296 				} else {
297 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
298 						sctp_log_cwnd(stcb, net, net->net_ack,
299 						    SCTP_CWND_LOG_NOADV_SS);
300 					}
301 				}
302 			} else {
303 				/* We are in congestion avoidance */
304 				/*
305 				 * Add to pba
306 				 */
307 				net->partial_bytes_acked += net->net_ack;
308 
309 				if ((net->flight_size + net->net_ack >= net->cwnd) &&
310 				    (net->partial_bytes_acked >= net->cwnd)) {
311 					net->partial_bytes_acked -= net->cwnd;
312 					net->cwnd += net->mtu;
313 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
314 						sctp_log_cwnd(stcb, net, net->mtu,
315 						    SCTP_CWND_LOG_FROM_CA);
316 					}
317 				} else {
318 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
319 						sctp_log_cwnd(stcb, net, net->net_ack,
320 						    SCTP_CWND_LOG_NOADV_CA);
321 					}
322 				}
323 			}
324 		} else {
325 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
326 				sctp_log_cwnd(stcb, net, net->mtu,
327 				    SCTP_CWND_LOG_NO_CUMACK);
328 			}
329 		}
330 skip_cwnd_update:
331 		/*
332 		 * NOW, according to Karn's rule do we need to restore the
333 		 * RTO timer back? Check our net_ack2. If not set then we
334 		 * have a ambiguity.. i.e. all data ack'd was sent to more
335 		 * than one place.
336 		 */
337 		if (net->net_ack2) {
338 			/* restore any doubled timers */
339 			net->RTO = ((net->lastsa >> 2) + net->lastsv) >> 1;
340 			if (net->RTO < stcb->asoc.minrto) {
341 				net->RTO = stcb->asoc.minrto;
342 			}
343 			if (net->RTO > stcb->asoc.maxrto) {
344 				net->RTO = stcb->asoc.maxrto;
345 			}
346 		}
347 	}
348 }
349 
350 void
351 sctp_cwnd_update_after_timeout(struct sctp_tcb *stcb, struct sctp_nets *net)
352 {
353 	int old_cwnd = net->cwnd;
354 
355 	net->ssthresh = max(net->cwnd / 2, 4 * net->mtu);
356 	net->cwnd = net->mtu;
357 	net->partial_bytes_acked = 0;
358 
359 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
360 		sctp_log_cwnd(stcb, net, net->cwnd - old_cwnd, SCTP_CWND_LOG_FROM_RTX);
361 	}
362 }
363 
364 void
365 sctp_cwnd_update_after_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net)
366 {
367 	int old_cwnd = net->cwnd;
368 
369 	SCTP_STAT_INCR(sctps_ecnereducedcwnd);
370 	net->ssthresh = net->cwnd / 2;
371 	if (net->ssthresh < net->mtu) {
372 		net->ssthresh = net->mtu;
373 		/* here back off the timer as well, to slow us down */
374 		net->RTO <<= 1;
375 	}
376 	net->cwnd = net->ssthresh;
377 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
378 		sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_SAT);
379 	}
380 }
381 
382 void
383 sctp_cwnd_update_after_packet_dropped(struct sctp_tcb *stcb,
384     struct sctp_nets *net, struct sctp_pktdrop_chunk *cp,
385     uint32_t * bottle_bw, uint32_t * on_queue)
386 {
387 	uint32_t bw_avail;
388 	int rtt, incr;
389 	int old_cwnd = net->cwnd;
390 
391 	/* need real RTT for this calc */
392 	rtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
393 	/* get bottle neck bw */
394 	*bottle_bw = ntohl(cp->bottle_bw);
395 	/* and whats on queue */
396 	*on_queue = ntohl(cp->current_onq);
397 	/*
398 	 * adjust the on-queue if our flight is more it could be that the
399 	 * router has not yet gotten data "in-flight" to it
400 	 */
401 	if (*on_queue < net->flight_size)
402 		*on_queue = net->flight_size;
403 	/* calculate the available space */
404 	bw_avail = (*bottle_bw * rtt) / 1000;
405 	if (bw_avail > *bottle_bw) {
406 		/*
407 		 * Cap the growth to no more than the bottle neck. This can
408 		 * happen as RTT slides up due to queues. It also means if
409 		 * you have more than a 1 second RTT with a empty queue you
410 		 * will be limited to the bottle_bw per second no matter if
411 		 * other points have 1/2 the RTT and you could get more
412 		 * out...
413 		 */
414 		bw_avail = *bottle_bw;
415 	}
416 	if (*on_queue > bw_avail) {
417 		/*
418 		 * No room for anything else don't allow anything else to be
419 		 * "added to the fire".
420 		 */
421 		int seg_inflight, seg_onqueue, my_portion;
422 
423 		net->partial_bytes_acked = 0;
424 
425 		/* how much are we over queue size? */
426 		incr = *on_queue - bw_avail;
427 		if (stcb->asoc.seen_a_sack_this_pkt) {
428 			/*
429 			 * undo any cwnd adjustment that the sack might have
430 			 * made
431 			 */
432 			net->cwnd = net->prev_cwnd;
433 		}
434 		/* Now how much of that is mine? */
435 		seg_inflight = net->flight_size / net->mtu;
436 		seg_onqueue = *on_queue / net->mtu;
437 		my_portion = (incr * seg_inflight) / seg_onqueue;
438 
439 		/* Have I made an adjustment already */
440 		if (net->cwnd > net->flight_size) {
441 			/*
442 			 * for this flight I made an adjustment we need to
443 			 * decrease the portion by a share our previous
444 			 * adjustment.
445 			 */
446 			int diff_adj;
447 
448 			diff_adj = net->cwnd - net->flight_size;
449 			if (diff_adj > my_portion)
450 				my_portion = 0;
451 			else
452 				my_portion -= diff_adj;
453 		}
454 		/*
455 		 * back down to the previous cwnd (assume we have had a sack
456 		 * before this packet). minus what ever portion of the
457 		 * overage is my fault.
458 		 */
459 		net->cwnd -= my_portion;
460 
461 		/* we will NOT back down more than 1 MTU */
462 		if (net->cwnd <= net->mtu) {
463 			net->cwnd = net->mtu;
464 		}
465 		/* force into CA */
466 		net->ssthresh = net->cwnd - 1;
467 	} else {
468 		/*
469 		 * Take 1/4 of the space left or max burst up .. whichever
470 		 * is less.
471 		 */
472 		incr = min((bw_avail - *on_queue) >> 2,
473 		    stcb->asoc.max_burst * net->mtu);
474 		net->cwnd += incr;
475 	}
476 	if (net->cwnd > bw_avail) {
477 		/* We can't exceed the pipe size */
478 		net->cwnd = bw_avail;
479 	}
480 	if (net->cwnd < net->mtu) {
481 		/* We always have 1 MTU */
482 		net->cwnd = net->mtu;
483 	}
484 	if (net->cwnd - old_cwnd != 0) {
485 		/* log only changes */
486 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
487 			sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd),
488 			    SCTP_CWND_LOG_FROM_SAT);
489 		}
490 	}
491 }
492 
493 void
494 sctp_cwnd_update_after_output(struct sctp_tcb *stcb,
495     struct sctp_nets *net, int burst_limit)
496 {
497 	int old_cwnd = net->cwnd;
498 
499 	if (net->ssthresh < net->cwnd)
500 		net->ssthresh = net->cwnd;
501 	net->cwnd = (net->flight_size + (burst_limit * net->mtu));
502 
503 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
504 		sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_BRST);
505 	}
506 }
507 
508 void
509 sctp_cwnd_update_after_fr_timer(struct sctp_inpcb *inp,
510     struct sctp_tcb *stcb, struct sctp_nets *net)
511 {
512 	int old_cwnd = net->cwnd;
513 
514 	sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_EARLY_FR_TMR, SCTP_SO_NOT_LOCKED);
515 	/*
516 	 * make a small adjustment to cwnd and force to CA.
517 	 */
518 	if (net->cwnd > net->mtu)
519 		/* drop down one MTU after sending */
520 		net->cwnd -= net->mtu;
521 	if (net->cwnd < net->ssthresh)
522 		/* still in SS move to CA */
523 		net->ssthresh = net->cwnd - 1;
524 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
525 		sctp_log_cwnd(stcb, net, (old_cwnd - net->cwnd), SCTP_CWND_LOG_FROM_FR);
526 	}
527 }
528 
529 struct sctp_hs_raise_drop {
530 	int32_t cwnd;
531 	int32_t increase;
532 	int32_t drop_percent;
533 };
534 
535 #define SCTP_HS_TABLE_SIZE 73
536 
537 struct sctp_hs_raise_drop sctp_cwnd_adjust[SCTP_HS_TABLE_SIZE] = {
538 	{38, 1, 50},		/* 0   */
539 	{118, 2, 44},		/* 1   */
540 	{221, 3, 41},		/* 2   */
541 	{347, 4, 38},		/* 3   */
542 	{495, 5, 37},		/* 4   */
543 	{663, 6, 35},		/* 5   */
544 	{851, 7, 34},		/* 6   */
545 	{1058, 8, 33},		/* 7   */
546 	{1284, 9, 32},		/* 8   */
547 	{1529, 10, 31},		/* 9   */
548 	{1793, 11, 30},		/* 10  */
549 	{2076, 12, 29},		/* 11  */
550 	{2378, 13, 28},		/* 12  */
551 	{2699, 14, 28},		/* 13  */
552 	{3039, 15, 27},		/* 14  */
553 	{3399, 16, 27},		/* 15  */
554 	{3778, 17, 26},		/* 16  */
555 	{4177, 18, 26},		/* 17  */
556 	{4596, 19, 25},		/* 18  */
557 	{5036, 20, 25},		/* 19  */
558 	{5497, 21, 24},		/* 20  */
559 	{5979, 22, 24},		/* 21  */
560 	{6483, 23, 23},		/* 22  */
561 	{7009, 24, 23},		/* 23  */
562 	{7558, 25, 22},		/* 24  */
563 	{8130, 26, 22},		/* 25  */
564 	{8726, 27, 22},		/* 26  */
565 	{9346, 28, 21},		/* 27  */
566 	{9991, 29, 21},		/* 28  */
567 	{10661, 30, 21},	/* 29  */
568 	{11358, 31, 20},	/* 30  */
569 	{12082, 32, 20},	/* 31  */
570 	{12834, 33, 20},	/* 32  */
571 	{13614, 34, 19},	/* 33  */
572 	{14424, 35, 19},	/* 34  */
573 	{15265, 36, 19},	/* 35  */
574 	{16137, 37, 19},	/* 36  */
575 	{17042, 38, 18},	/* 37  */
576 	{17981, 39, 18},	/* 38  */
577 	{18955, 40, 18},	/* 39  */
578 	{19965, 41, 17},	/* 40  */
579 	{21013, 42, 17},	/* 41  */
580 	{22101, 43, 17},	/* 42  */
581 	{23230, 44, 17},	/* 43  */
582 	{24402, 45, 16},	/* 44  */
583 	{25618, 46, 16},	/* 45  */
584 	{26881, 47, 16},	/* 46  */
585 	{28193, 48, 16},	/* 47  */
586 	{29557, 49, 15},	/* 48  */
587 	{30975, 50, 15},	/* 49  */
588 	{32450, 51, 15},	/* 50  */
589 	{33986, 52, 15},	/* 51  */
590 	{35586, 53, 14},	/* 52  */
591 	{37253, 54, 14},	/* 53  */
592 	{38992, 55, 14},	/* 54  */
593 	{40808, 56, 14},	/* 55  */
594 	{42707, 57, 13},	/* 56  */
595 	{44694, 58, 13},	/* 57  */
596 	{46776, 59, 13},	/* 58  */
597 	{48961, 60, 13},	/* 59  */
598 	{51258, 61, 13},	/* 60  */
599 	{53677, 62, 12},	/* 61  */
600 	{56230, 63, 12},	/* 62  */
601 	{58932, 64, 12},	/* 63  */
602 	{61799, 65, 12},	/* 64  */
603 	{64851, 66, 11},	/* 65  */
604 	{68113, 67, 11},	/* 66  */
605 	{71617, 68, 11},	/* 67  */
606 	{75401, 69, 10},	/* 68  */
607 	{79517, 70, 10},	/* 69  */
608 	{84035, 71, 10},	/* 70  */
609 	{89053, 72, 10},	/* 71  */
610 	{94717, 73, 9}		/* 72  */
611 };
612 
613 static void
614 sctp_hs_cwnd_increase(struct sctp_tcb *stcb, struct sctp_nets *net)
615 {
616 	int cur_val, i, indx, incr;
617 
618 	cur_val = net->cwnd >> 10;
619 	indx = SCTP_HS_TABLE_SIZE - 1;
620 #ifdef SCTP_DEBUG
621 	printf("HS CC CAlled.\n");
622 #endif
623 	if (cur_val < sctp_cwnd_adjust[0].cwnd) {
624 		/* normal mode */
625 		if (net->net_ack > net->mtu) {
626 			net->cwnd += net->mtu;
627 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
628 				sctp_log_cwnd(stcb, net, net->mtu, SCTP_CWND_LOG_FROM_SS);
629 			}
630 		} else {
631 			net->cwnd += net->net_ack;
632 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
633 				sctp_log_cwnd(stcb, net, net->net_ack, SCTP_CWND_LOG_FROM_SS);
634 			}
635 		}
636 	} else {
637 		for (i = net->last_hs_used; i < SCTP_HS_TABLE_SIZE; i++) {
638 			if (cur_val < sctp_cwnd_adjust[i].cwnd) {
639 				indx = i;
640 				break;
641 			}
642 		}
643 		net->last_hs_used = indx;
644 		incr = ((sctp_cwnd_adjust[indx].increase) << 10);
645 		net->cwnd += incr;
646 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
647 			sctp_log_cwnd(stcb, net, incr, SCTP_CWND_LOG_FROM_SS);
648 		}
649 	}
650 }
651 
652 static void
653 sctp_hs_cwnd_decrease(struct sctp_tcb *stcb, struct sctp_nets *net)
654 {
655 	int cur_val, i, indx;
656 	int old_cwnd = net->cwnd;
657 
658 	cur_val = net->cwnd >> 10;
659 	indx = net->last_hs_used;
660 	if (cur_val < sctp_cwnd_adjust[0].cwnd) {
661 		/* normal mode */
662 		net->ssthresh = net->cwnd / 2;
663 		if (net->ssthresh < (net->mtu * 2)) {
664 			net->ssthresh = 2 * net->mtu;
665 		}
666 		net->cwnd = net->ssthresh;
667 	} else {
668 		/* drop by the proper amount */
669 		net->ssthresh = net->cwnd - (int)((net->cwnd / 100) *
670 		    sctp_cwnd_adjust[net->last_hs_used].drop_percent);
671 		net->cwnd = net->ssthresh;
672 		/* now where are we */
673 		indx = net->last_hs_used;
674 		cur_val = net->cwnd >> 10;
675 		/* reset where we are in the table */
676 		if (cur_val < sctp_cwnd_adjust[0].cwnd) {
677 			/* feel out of hs */
678 			net->last_hs_used = 0;
679 		} else {
680 			for (i = indx; i >= 1; i--) {
681 				if (cur_val > sctp_cwnd_adjust[i - 1].cwnd) {
682 					break;
683 				}
684 			}
685 			net->last_hs_used = indx;
686 		}
687 	}
688 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
689 		sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_FR);
690 	}
691 }
692 
693 void
694 sctp_hs_cwnd_update_after_fr(struct sctp_tcb *stcb,
695     struct sctp_association *asoc)
696 {
697 	struct sctp_nets *net;
698 
699 	/*
700 	 * CMT fast recovery code. Need to debug. ((sctp_cmt_on_off == 1) &&
701 	 * (net->fast_retran_loss_recovery == 0)))
702 	 */
703 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
704 		if ((asoc->fast_retran_loss_recovery == 0) ||
705 		    (asoc->sctp_cmt_on_off == 1)) {
706 			/* out of a RFC2582 Fast recovery window? */
707 			if (net->net_ack > 0) {
708 				/*
709 				 * per section 7.2.3, are there any
710 				 * destinations that had a fast retransmit
711 				 * to them. If so what we need to do is
712 				 * adjust ssthresh and cwnd.
713 				 */
714 				struct sctp_tmit_chunk *lchk;
715 
716 				sctp_hs_cwnd_decrease(stcb, net);
717 
718 				lchk = TAILQ_FIRST(&asoc->send_queue);
719 
720 				net->partial_bytes_acked = 0;
721 				/* Turn on fast recovery window */
722 				asoc->fast_retran_loss_recovery = 1;
723 				if (lchk == NULL) {
724 					/* Mark end of the window */
725 					asoc->fast_recovery_tsn = asoc->sending_seq - 1;
726 				} else {
727 					asoc->fast_recovery_tsn = lchk->rec.data.TSN_seq - 1;
728 				}
729 
730 				/*
731 				 * CMT fast recovery -- per destination
732 				 * recovery variable.
733 				 */
734 				net->fast_retran_loss_recovery = 1;
735 
736 				if (lchk == NULL) {
737 					/* Mark end of the window */
738 					net->fast_recovery_tsn = asoc->sending_seq - 1;
739 				} else {
740 					net->fast_recovery_tsn = lchk->rec.data.TSN_seq - 1;
741 				}
742 
743 				/*
744 				 * Disable Nonce Sum Checking and store the
745 				 * resync tsn
746 				 */
747 				asoc->nonce_sum_check = 0;
748 				asoc->nonce_resync_tsn = asoc->fast_recovery_tsn + 1;
749 
750 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND,
751 				    stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_32);
752 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
753 				    stcb->sctp_ep, stcb, net);
754 			}
755 		} else if (net->net_ack > 0) {
756 			/*
757 			 * Mark a peg that we WOULD have done a cwnd
758 			 * reduction but RFC2582 prevented this action.
759 			 */
760 			SCTP_STAT_INCR(sctps_fastretransinrtt);
761 		}
762 	}
763 }
764 
765 void
766 sctp_hs_cwnd_update_after_sack(struct sctp_tcb *stcb,
767     struct sctp_association *asoc,
768     int accum_moved, int reneged_all, int will_exit)
769 {
770 	struct sctp_nets *net;
771 
772 	/******************************/
773 	/* update cwnd and Early FR   */
774 	/******************************/
775 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
776 
777 #ifdef JANA_CMT_FAST_RECOVERY
778 		/*
779 		 * CMT fast recovery code. Need to debug.
780 		 */
781 		if (net->fast_retran_loss_recovery && net->new_pseudo_cumack) {
782 			if (compare_with_wrap(asoc->last_acked_seq,
783 			    net->fast_recovery_tsn, MAX_TSN) ||
784 			    (asoc->last_acked_seq == net->fast_recovery_tsn) ||
785 			    compare_with_wrap(net->pseudo_cumack, net->fast_recovery_tsn, MAX_TSN) ||
786 			    (net->pseudo_cumack == net->fast_recovery_tsn)) {
787 				net->will_exit_fast_recovery = 1;
788 			}
789 		}
790 #endif
791 		if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
792 			/*
793 			 * So, first of all do we need to have a Early FR
794 			 * timer running?
795 			 */
796 			if (((TAILQ_FIRST(&asoc->sent_queue)) &&
797 			    (net->ref_count > 1) &&
798 			    (net->flight_size < net->cwnd)) ||
799 			    (reneged_all)) {
800 				/*
801 				 * yes, so in this case stop it if its
802 				 * running, and then restart it. Reneging
803 				 * all is a special case where we want to
804 				 * run the Early FR timer and then force the
805 				 * last few unacked to be sent, causing us
806 				 * to illicit a sack with gaps to force out
807 				 * the others.
808 				 */
809 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
810 					SCTP_STAT_INCR(sctps_earlyfrstpidsck2);
811 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
812 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_20);
813 				}
814 				SCTP_STAT_INCR(sctps_earlyfrstrid);
815 				sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net);
816 			} else {
817 				/* No, stop it if its running */
818 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
819 					SCTP_STAT_INCR(sctps_earlyfrstpidsck3);
820 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
821 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_21);
822 				}
823 			}
824 		}
825 		/* if nothing was acked on this destination skip it */
826 		if (net->net_ack == 0) {
827 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
828 				sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FROM_SACK);
829 			}
830 			continue;
831 		}
832 		if (net->net_ack2 > 0) {
833 			/*
834 			 * Karn's rule applies to clearing error count, this
835 			 * is optional.
836 			 */
837 			net->error_count = 0;
838 			if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
839 			    SCTP_ADDR_NOT_REACHABLE) {
840 				/* addr came good */
841 				net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE;
842 				net->dest_state |= SCTP_ADDR_REACHABLE;
843 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
844 				    SCTP_RECEIVED_SACK, (void *)net, SCTP_SO_NOT_LOCKED);
845 				/* now was it the primary? if so restore */
846 				if (net->dest_state & SCTP_ADDR_WAS_PRIMARY) {
847 					(void)sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net);
848 				}
849 			}
850 			/*
851 			 * JRS 5/14/07 - If CMT PF is on and the destination
852 			 * is in PF state, set the destination to active
853 			 * state and set the cwnd to one or two MTU's based
854 			 * on whether PF1 or PF2 is being used.
855 			 *
856 			 * Should we stop any running T3 timer here?
857 			 */
858 			if ((asoc->sctp_cmt_on_off == 1) &&
859 			    (asoc->sctp_cmt_pf > 0) &&
860 			    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
861 				net->dest_state &= ~SCTP_ADDR_PF;
862 				net->cwnd = net->mtu * asoc->sctp_cmt_pf;
863 				SCTPDBG(SCTP_DEBUG_INDATA1, "Destination %p moved from PF to reachable with cwnd %d.\n",
864 				    net, net->cwnd);
865 				/*
866 				 * Since the cwnd value is explicitly set,
867 				 * skip the code that updates the cwnd
868 				 * value.
869 				 */
870 				goto skip_cwnd_update;
871 			}
872 		}
873 #ifdef JANA_CMT_FAST_RECOVERY
874 		/*
875 		 * CMT fast recovery code
876 		 */
877 		/*
878 		 * if (sctp_cmt_on_off == 1 &&
879 		 * net->fast_retran_loss_recovery &&
880 		 * net->will_exit_fast_recovery == 0) { @@@ Do something }
881 		 * else if (sctp_cmt_on_off == 0 &&
882 		 * asoc->fast_retran_loss_recovery && will_exit == 0) {
883 		 */
884 #endif
885 
886 		if (asoc->fast_retran_loss_recovery &&
887 		    (will_exit == 0) &&
888 		    (asoc->sctp_cmt_on_off == 0)) {
889 			/*
890 			 * If we are in loss recovery we skip any cwnd
891 			 * update
892 			 */
893 			goto skip_cwnd_update;
894 		}
895 		/*
896 		 * CMT: CUC algorithm. Update cwnd if pseudo-cumack has
897 		 * moved.
898 		 */
899 		if (accum_moved ||
900 		    ((asoc->sctp_cmt_on_off == 1) && net->new_pseudo_cumack)) {
901 			/* If the cumulative ack moved we can proceed */
902 			if (net->cwnd <= net->ssthresh) {
903 				/* We are in slow start */
904 				if (net->flight_size + net->net_ack >= net->cwnd) {
905 
906 					sctp_hs_cwnd_increase(stcb, net);
907 
908 				} else {
909 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
910 						sctp_log_cwnd(stcb, net, net->net_ack,
911 						    SCTP_CWND_LOG_NOADV_SS);
912 					}
913 				}
914 			} else {
915 				/* We are in congestion avoidance */
916 				net->partial_bytes_acked += net->net_ack;
917 				if ((net->flight_size + net->net_ack >= net->cwnd) &&
918 				    (net->partial_bytes_acked >= net->cwnd)) {
919 					net->partial_bytes_acked -= net->cwnd;
920 					net->cwnd += net->mtu;
921 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
922 						sctp_log_cwnd(stcb, net, net->mtu,
923 						    SCTP_CWND_LOG_FROM_CA);
924 					}
925 				} else {
926 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
927 						sctp_log_cwnd(stcb, net, net->net_ack,
928 						    SCTP_CWND_LOG_NOADV_CA);
929 					}
930 				}
931 			}
932 		} else {
933 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
934 				sctp_log_cwnd(stcb, net, net->mtu,
935 				    SCTP_CWND_LOG_NO_CUMACK);
936 			}
937 		}
938 skip_cwnd_update:
939 		/*
940 		 * NOW, according to Karn's rule do we need to restore the
941 		 * RTO timer back? Check our net_ack2. If not set then we
942 		 * have a ambiguity.. i.e. all data ack'd was sent to more
943 		 * than one place.
944 		 */
945 		if (net->net_ack2) {
946 			/* restore any doubled timers */
947 			net->RTO = ((net->lastsa >> 2) + net->lastsv) >> 1;
948 			if (net->RTO < stcb->asoc.minrto) {
949 				net->RTO = stcb->asoc.minrto;
950 			}
951 			if (net->RTO > stcb->asoc.maxrto) {
952 				net->RTO = stcb->asoc.maxrto;
953 			}
954 		}
955 	}
956 }
957 
958 
959 /*
960  * H-TCP congestion control. The algorithm is detailed in:
961  * R.N.Shorten, D.J.Leith:
962  *   "H-TCP: TCP for high-speed and long-distance networks"
963  *   Proc. PFLDnet, Argonne, 2004.
964  * http://www.hamilton.ie/net/htcp3.pdf
965  */
966 
967 
968 static int use_rtt_scaling = 1;
969 static int use_bandwidth_switch = 1;
970 
971 static inline int
972 between(uint32_t seq1, uint32_t seq2, uint32_t seq3)
973 {
974 	return seq3 - seq2 >= seq1 - seq2;
975 }
976 
977 static inline uint32_t
978 htcp_cong_time(struct htcp *ca)
979 {
980 	return sctp_get_tick_count() - ca->last_cong;
981 }
982 
983 static inline uint32_t
984 htcp_ccount(struct htcp *ca)
985 {
986 	return htcp_cong_time(ca) / ca->minRTT;
987 }
988 
989 static inline void
990 htcp_reset(struct htcp *ca)
991 {
992 	ca->undo_last_cong = ca->last_cong;
993 	ca->undo_maxRTT = ca->maxRTT;
994 	ca->undo_old_maxB = ca->old_maxB;
995 	ca->last_cong = sctp_get_tick_count();
996 }
997 
998 #ifdef SCTP_NOT_USED
999 
1000 static uint32_t
1001 htcp_cwnd_undo(struct sctp_tcb *stcb, struct sctp_nets *net)
1002 {
1003 	net->htcp_ca.last_cong = net->htcp_ca.undo_last_cong;
1004 	net->htcp_ca.maxRTT = net->htcp_ca.undo_maxRTT;
1005 	net->htcp_ca.old_maxB = net->htcp_ca.undo_old_maxB;
1006 	return max(net->cwnd, ((net->ssthresh / net->mtu << 7) / net->htcp_ca.beta) * net->mtu);
1007 }
1008 
1009 #endif
1010 
1011 static inline void
1012 measure_rtt(struct sctp_tcb *stcb, struct sctp_nets *net)
1013 {
1014 	uint32_t srtt = net->lastsa >> 3;
1015 
1016 	/* keep track of minimum RTT seen so far, minRTT is zero at first */
1017 	if (net->htcp_ca.minRTT > srtt || !net->htcp_ca.minRTT)
1018 		net->htcp_ca.minRTT = srtt;
1019 
1020 	/* max RTT */
1021 	if (net->fast_retran_ip == 0 && net->ssthresh < 0xFFFF && htcp_ccount(&net->htcp_ca) > 3) {
1022 		if (net->htcp_ca.maxRTT < net->htcp_ca.minRTT)
1023 			net->htcp_ca.maxRTT = net->htcp_ca.minRTT;
1024 		if (net->htcp_ca.maxRTT < srtt && srtt <= net->htcp_ca.maxRTT + MSEC_TO_TICKS(20))
1025 			net->htcp_ca.maxRTT = srtt;
1026 	}
1027 }
1028 
1029 static void
1030 measure_achieved_throughput(struct sctp_tcb *stcb, struct sctp_nets *net)
1031 {
1032 	uint32_t now = sctp_get_tick_count();
1033 
1034 	if (net->fast_retran_ip == 0)
1035 		net->htcp_ca.bytes_acked = net->net_ack;
1036 
1037 	if (!use_bandwidth_switch)
1038 		return;
1039 
1040 	/* achieved throughput calculations */
1041 	/* JRS - not 100% sure of this statement */
1042 	if (net->fast_retran_ip == 1) {
1043 		net->htcp_ca.bytecount = 0;
1044 		net->htcp_ca.lasttime = now;
1045 		return;
1046 	}
1047 	net->htcp_ca.bytecount += net->net_ack;
1048 
1049 	if (net->htcp_ca.bytecount >= net->cwnd - ((net->htcp_ca.alpha >> 7 ? : 1) * net->mtu)
1050 	    && now - net->htcp_ca.lasttime >= net->htcp_ca.minRTT
1051 	    && net->htcp_ca.minRTT > 0) {
1052 		uint32_t cur_Bi = net->htcp_ca.bytecount / net->mtu * hz / (now - net->htcp_ca.lasttime);
1053 
1054 		if (htcp_ccount(&net->htcp_ca) <= 3) {
1055 			/* just after backoff */
1056 			net->htcp_ca.minB = net->htcp_ca.maxB = net->htcp_ca.Bi = cur_Bi;
1057 		} else {
1058 			net->htcp_ca.Bi = (3 * net->htcp_ca.Bi + cur_Bi) / 4;
1059 			if (net->htcp_ca.Bi > net->htcp_ca.maxB)
1060 				net->htcp_ca.maxB = net->htcp_ca.Bi;
1061 			if (net->htcp_ca.minB > net->htcp_ca.maxB)
1062 				net->htcp_ca.minB = net->htcp_ca.maxB;
1063 		}
1064 		net->htcp_ca.bytecount = 0;
1065 		net->htcp_ca.lasttime = now;
1066 	}
1067 }
1068 
1069 static inline void
1070 htcp_beta_update(struct htcp *ca, uint32_t minRTT, uint32_t maxRTT)
1071 {
1072 	if (use_bandwidth_switch) {
1073 		uint32_t maxB = ca->maxB;
1074 		uint32_t old_maxB = ca->old_maxB;
1075 
1076 		ca->old_maxB = ca->maxB;
1077 
1078 		if (!between(5 * maxB, 4 * old_maxB, 6 * old_maxB)) {
1079 			ca->beta = BETA_MIN;
1080 			ca->modeswitch = 0;
1081 			return;
1082 		}
1083 	}
1084 	if (ca->modeswitch && minRTT > (uint32_t) MSEC_TO_TICKS(10) && maxRTT) {
1085 		ca->beta = (minRTT << 7) / maxRTT;
1086 		if (ca->beta < BETA_MIN)
1087 			ca->beta = BETA_MIN;
1088 		else if (ca->beta > BETA_MAX)
1089 			ca->beta = BETA_MAX;
1090 	} else {
1091 		ca->beta = BETA_MIN;
1092 		ca->modeswitch = 1;
1093 	}
1094 }
1095 
1096 static inline void
1097 htcp_alpha_update(struct htcp *ca)
1098 {
1099 	uint32_t minRTT = ca->minRTT;
1100 	uint32_t factor = 1;
1101 	uint32_t diff = htcp_cong_time(ca);
1102 
1103 	if (diff > (uint32_t) hz) {
1104 		diff -= hz;
1105 		factor = 1 + (10 * diff + ((diff / 2) * (diff / 2) / hz)) / hz;
1106 	}
1107 	if (use_rtt_scaling && minRTT) {
1108 		uint32_t scale = (hz << 3) / (10 * minRTT);
1109 
1110 		scale = min(max(scale, 1U << 2), 10U << 3);	/* clamping ratio to
1111 								 * interval [0.5,10]<<3 */
1112 		factor = (factor << 3) / scale;
1113 		if (!factor)
1114 			factor = 1;
1115 	}
1116 	ca->alpha = 2 * factor * ((1 << 7) - ca->beta);
1117 	if (!ca->alpha)
1118 		ca->alpha = ALPHA_BASE;
1119 }
1120 
1121 /* After we have the rtt data to calculate beta, we'd still prefer to wait one
1122  * rtt before we adjust our beta to ensure we are working from a consistent
1123  * data.
1124  *
1125  * This function should be called when we hit a congestion event since only at
1126  * that point do we really have a real sense of maxRTT (the queues en route
1127  * were getting just too full now).
1128  */
1129 static void
1130 htcp_param_update(struct sctp_tcb *stcb, struct sctp_nets *net)
1131 {
1132 	uint32_t minRTT = net->htcp_ca.minRTT;
1133 	uint32_t maxRTT = net->htcp_ca.maxRTT;
1134 
1135 	htcp_beta_update(&net->htcp_ca, minRTT, maxRTT);
1136 	htcp_alpha_update(&net->htcp_ca);
1137 
1138 	/*
1139 	 * add slowly fading memory for maxRTT to accommodate routing
1140 	 * changes etc
1141 	 */
1142 	if (minRTT > 0 && maxRTT > minRTT)
1143 		net->htcp_ca.maxRTT = minRTT + ((maxRTT - minRTT) * 95) / 100;
1144 }
1145 
1146 static uint32_t
1147 htcp_recalc_ssthresh(struct sctp_tcb *stcb, struct sctp_nets *net)
1148 {
1149 	htcp_param_update(stcb, net);
1150 	return max(((net->cwnd / net->mtu * net->htcp_ca.beta) >> 7) * net->mtu, 2U * net->mtu);
1151 }
1152 
1153 static void
1154 htcp_cong_avoid(struct sctp_tcb *stcb, struct sctp_nets *net)
1155 {
1156 	/*-
1157 	 * How to handle these functions?
1158          *	if (!tcp_is_cwnd_limited(sk, in_flight)) RRS - good question.
1159 	 *		return;
1160 	 */
1161 	if (net->cwnd <= net->ssthresh) {
1162 		/* We are in slow start */
1163 		if (net->flight_size + net->net_ack >= net->cwnd) {
1164 			if (net->net_ack > (net->mtu * SCTP_BASE_SYSCTL(sctp_L2_abc_variable))) {
1165 				net->cwnd += (net->mtu * SCTP_BASE_SYSCTL(sctp_L2_abc_variable));
1166 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1167 					sctp_log_cwnd(stcb, net, net->mtu,
1168 					    SCTP_CWND_LOG_FROM_SS);
1169 				}
1170 			} else {
1171 				net->cwnd += net->net_ack;
1172 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1173 					sctp_log_cwnd(stcb, net, net->net_ack,
1174 					    SCTP_CWND_LOG_FROM_SS);
1175 				}
1176 			}
1177 		} else {
1178 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
1179 				sctp_log_cwnd(stcb, net, net->net_ack,
1180 				    SCTP_CWND_LOG_NOADV_SS);
1181 			}
1182 		}
1183 	} else {
1184 		measure_rtt(stcb, net);
1185 
1186 		/*
1187 		 * In dangerous area, increase slowly. In theory this is
1188 		 * net->cwnd += alpha / net->cwnd
1189 		 */
1190 		/* What is snd_cwnd_cnt?? */
1191 		if (((net->partial_bytes_acked / net->mtu * net->htcp_ca.alpha) >> 7) * net->mtu >= net->cwnd) {
1192 			/*-
1193 			 * Does SCTP have a cwnd clamp?
1194 			 * if (net->snd_cwnd < net->snd_cwnd_clamp) - Nope (RRS).
1195 			 */
1196 			net->cwnd += net->mtu;
1197 			net->partial_bytes_acked = 0;
1198 			htcp_alpha_update(&net->htcp_ca);
1199 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1200 				sctp_log_cwnd(stcb, net, net->mtu,
1201 				    SCTP_CWND_LOG_FROM_CA);
1202 			}
1203 		} else {
1204 			net->partial_bytes_acked += net->net_ack;
1205 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
1206 				sctp_log_cwnd(stcb, net, net->net_ack,
1207 				    SCTP_CWND_LOG_NOADV_CA);
1208 			}
1209 		}
1210 
1211 		net->htcp_ca.bytes_acked = net->mtu;
1212 	}
1213 }
1214 
1215 #ifdef SCTP_NOT_USED
1216 /* Lower bound on congestion window. */
1217 static uint32_t
1218 htcp_min_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net)
1219 {
1220 	return net->ssthresh;
1221 }
1222 
1223 #endif
1224 
1225 static void
1226 htcp_init(struct sctp_tcb *stcb, struct sctp_nets *net)
1227 {
1228 	memset(&net->htcp_ca, 0, sizeof(struct htcp));
1229 	net->htcp_ca.alpha = ALPHA_BASE;
1230 	net->htcp_ca.beta = BETA_MIN;
1231 	net->htcp_ca.bytes_acked = net->mtu;
1232 	net->htcp_ca.last_cong = sctp_get_tick_count();
1233 }
1234 
1235 void
1236 sctp_htcp_set_initial_cc_param(struct sctp_tcb *stcb, struct sctp_nets *net)
1237 {
1238 	/*
1239 	 * We take the max of the burst limit times a MTU or the
1240 	 * INITIAL_CWND. We then limit this to 4 MTU's of sending.
1241 	 */
1242 	net->cwnd = min((net->mtu * 4), max((2 * net->mtu), SCTP_INITIAL_CWND));
1243 	net->ssthresh = stcb->asoc.peers_rwnd;
1244 	htcp_init(stcb, net);
1245 
1246 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
1247 		sctp_log_cwnd(stcb, net, 0, SCTP_CWND_INITIALIZATION);
1248 	}
1249 }
1250 
1251 void
1252 sctp_htcp_cwnd_update_after_sack(struct sctp_tcb *stcb,
1253     struct sctp_association *asoc,
1254     int accum_moved, int reneged_all, int will_exit)
1255 {
1256 	struct sctp_nets *net;
1257 
1258 	/******************************/
1259 	/* update cwnd and Early FR   */
1260 	/******************************/
1261 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
1262 
1263 #ifdef JANA_CMT_FAST_RECOVERY
1264 		/*
1265 		 * CMT fast recovery code. Need to debug.
1266 		 */
1267 		if (net->fast_retran_loss_recovery && net->new_pseudo_cumack) {
1268 			if (compare_with_wrap(asoc->last_acked_seq,
1269 			    net->fast_recovery_tsn, MAX_TSN) ||
1270 			    (asoc->last_acked_seq == net->fast_recovery_tsn) ||
1271 			    compare_with_wrap(net->pseudo_cumack, net->fast_recovery_tsn, MAX_TSN) ||
1272 			    (net->pseudo_cumack == net->fast_recovery_tsn)) {
1273 				net->will_exit_fast_recovery = 1;
1274 			}
1275 		}
1276 #endif
1277 		if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
1278 			/*
1279 			 * So, first of all do we need to have a Early FR
1280 			 * timer running?
1281 			 */
1282 			if (((TAILQ_FIRST(&asoc->sent_queue)) &&
1283 			    (net->ref_count > 1) &&
1284 			    (net->flight_size < net->cwnd)) ||
1285 			    (reneged_all)) {
1286 				/*
1287 				 * yes, so in this case stop it if its
1288 				 * running, and then restart it. Reneging
1289 				 * all is a special case where we want to
1290 				 * run the Early FR timer and then force the
1291 				 * last few unacked to be sent, causing us
1292 				 * to illicit a sack with gaps to force out
1293 				 * the others.
1294 				 */
1295 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
1296 					SCTP_STAT_INCR(sctps_earlyfrstpidsck2);
1297 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
1298 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_20);
1299 				}
1300 				SCTP_STAT_INCR(sctps_earlyfrstrid);
1301 				sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net);
1302 			} else {
1303 				/* No, stop it if its running */
1304 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
1305 					SCTP_STAT_INCR(sctps_earlyfrstpidsck3);
1306 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
1307 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_21);
1308 				}
1309 			}
1310 		}
1311 		/* if nothing was acked on this destination skip it */
1312 		if (net->net_ack == 0) {
1313 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
1314 				sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FROM_SACK);
1315 			}
1316 			continue;
1317 		}
1318 		if (net->net_ack2 > 0) {
1319 			/*
1320 			 * Karn's rule applies to clearing error count, this
1321 			 * is optional.
1322 			 */
1323 			net->error_count = 0;
1324 			if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
1325 			    SCTP_ADDR_NOT_REACHABLE) {
1326 				/* addr came good */
1327 				net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE;
1328 				net->dest_state |= SCTP_ADDR_REACHABLE;
1329 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
1330 				    SCTP_RECEIVED_SACK, (void *)net, SCTP_SO_NOT_LOCKED);
1331 				/* now was it the primary? if so restore */
1332 				if (net->dest_state & SCTP_ADDR_WAS_PRIMARY) {
1333 					(void)sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net);
1334 				}
1335 			}
1336 			/*
1337 			 * JRS 5/14/07 - If CMT PF is on and the destination
1338 			 * is in PF state, set the destination to active
1339 			 * state and set the cwnd to one or two MTU's based
1340 			 * on whether PF1 or PF2 is being used.
1341 			 *
1342 			 * Should we stop any running T3 timer here?
1343 			 */
1344 			if ((asoc->sctp_cmt_on_off == 1) &&
1345 			    (asoc->sctp_cmt_pf > 0) &&
1346 			    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
1347 				net->dest_state &= ~SCTP_ADDR_PF;
1348 				net->cwnd = net->mtu * asoc->sctp_cmt_pf;
1349 				SCTPDBG(SCTP_DEBUG_INDATA1, "Destination %p moved from PF to reachable with cwnd %d.\n",
1350 				    net, net->cwnd);
1351 				/*
1352 				 * Since the cwnd value is explicitly set,
1353 				 * skip the code that updates the cwnd
1354 				 * value.
1355 				 */
1356 				goto skip_cwnd_update;
1357 			}
1358 		}
1359 #ifdef JANA_CMT_FAST_RECOVERY
1360 		/*
1361 		 * CMT fast recovery code
1362 		 */
1363 		/*
1364 		 * if (sctp_cmt_on_off == 1 &&
1365 		 * net->fast_retran_loss_recovery &&
1366 		 * net->will_exit_fast_recovery == 0) { @@@ Do something }
1367 		 * else if (sctp_cmt_on_off == 0 &&
1368 		 * asoc->fast_retran_loss_recovery && will_exit == 0) {
1369 		 */
1370 #endif
1371 
1372 		if (asoc->fast_retran_loss_recovery &&
1373 		    will_exit == 0 &&
1374 		    (asoc->sctp_cmt_on_off == 0)) {
1375 			/*
1376 			 * If we are in loss recovery we skip any cwnd
1377 			 * update
1378 			 */
1379 			goto skip_cwnd_update;
1380 		}
1381 		/*
1382 		 * CMT: CUC algorithm. Update cwnd if pseudo-cumack has
1383 		 * moved.
1384 		 */
1385 		if (accum_moved ||
1386 		    ((asoc->sctp_cmt_on_off == 1) && net->new_pseudo_cumack)) {
1387 			htcp_cong_avoid(stcb, net);
1388 			measure_achieved_throughput(stcb, net);
1389 		} else {
1390 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
1391 				sctp_log_cwnd(stcb, net, net->mtu,
1392 				    SCTP_CWND_LOG_NO_CUMACK);
1393 			}
1394 		}
1395 skip_cwnd_update:
1396 		/*
1397 		 * NOW, according to Karn's rule do we need to restore the
1398 		 * RTO timer back? Check our net_ack2. If not set then we
1399 		 * have a ambiguity.. i.e. all data ack'd was sent to more
1400 		 * than one place.
1401 		 */
1402 		if (net->net_ack2) {
1403 			/* restore any doubled timers */
1404 			net->RTO = ((net->lastsa >> 2) + net->lastsv) >> 1;
1405 			if (net->RTO < stcb->asoc.minrto) {
1406 				net->RTO = stcb->asoc.minrto;
1407 			}
1408 			if (net->RTO > stcb->asoc.maxrto) {
1409 				net->RTO = stcb->asoc.maxrto;
1410 			}
1411 		}
1412 	}
1413 }
1414 
1415 void
1416 sctp_htcp_cwnd_update_after_fr(struct sctp_tcb *stcb,
1417     struct sctp_association *asoc)
1418 {
1419 	struct sctp_nets *net;
1420 
1421 	/*
1422 	 * CMT fast recovery code. Need to debug. ((sctp_cmt_on_off == 1) &&
1423 	 * (net->fast_retran_loss_recovery == 0)))
1424 	 */
1425 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
1426 		if ((asoc->fast_retran_loss_recovery == 0) ||
1427 		    (asoc->sctp_cmt_on_off == 1)) {
1428 			/* out of a RFC2582 Fast recovery window? */
1429 			if (net->net_ack > 0) {
1430 				/*
1431 				 * per section 7.2.3, are there any
1432 				 * destinations that had a fast retransmit
1433 				 * to them. If so what we need to do is
1434 				 * adjust ssthresh and cwnd.
1435 				 */
1436 				struct sctp_tmit_chunk *lchk;
1437 				int old_cwnd = net->cwnd;
1438 
1439 				/* JRS - reset as if state were changed */
1440 				htcp_reset(&net->htcp_ca);
1441 				net->ssthresh = htcp_recalc_ssthresh(stcb, net);
1442 				net->cwnd = net->ssthresh;
1443 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1444 					sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd),
1445 					    SCTP_CWND_LOG_FROM_FR);
1446 				}
1447 				lchk = TAILQ_FIRST(&asoc->send_queue);
1448 
1449 				net->partial_bytes_acked = 0;
1450 				/* Turn on fast recovery window */
1451 				asoc->fast_retran_loss_recovery = 1;
1452 				if (lchk == NULL) {
1453 					/* Mark end of the window */
1454 					asoc->fast_recovery_tsn = asoc->sending_seq - 1;
1455 				} else {
1456 					asoc->fast_recovery_tsn = lchk->rec.data.TSN_seq - 1;
1457 				}
1458 
1459 				/*
1460 				 * CMT fast recovery -- per destination
1461 				 * recovery variable.
1462 				 */
1463 				net->fast_retran_loss_recovery = 1;
1464 
1465 				if (lchk == NULL) {
1466 					/* Mark end of the window */
1467 					net->fast_recovery_tsn = asoc->sending_seq - 1;
1468 				} else {
1469 					net->fast_recovery_tsn = lchk->rec.data.TSN_seq - 1;
1470 				}
1471 
1472 				/*
1473 				 * Disable Nonce Sum Checking and store the
1474 				 * resync tsn
1475 				 */
1476 				asoc->nonce_sum_check = 0;
1477 				asoc->nonce_resync_tsn = asoc->fast_recovery_tsn + 1;
1478 
1479 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND,
1480 				    stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_32);
1481 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
1482 				    stcb->sctp_ep, stcb, net);
1483 			}
1484 		} else if (net->net_ack > 0) {
1485 			/*
1486 			 * Mark a peg that we WOULD have done a cwnd
1487 			 * reduction but RFC2582 prevented this action.
1488 			 */
1489 			SCTP_STAT_INCR(sctps_fastretransinrtt);
1490 		}
1491 	}
1492 }
1493 
1494 void
1495 sctp_htcp_cwnd_update_after_timeout(struct sctp_tcb *stcb,
1496     struct sctp_nets *net)
1497 {
1498 	int old_cwnd = net->cwnd;
1499 
1500 	/* JRS - reset as if the state were being changed to timeout */
1501 	htcp_reset(&net->htcp_ca);
1502 	net->ssthresh = htcp_recalc_ssthresh(stcb, net);
1503 	net->cwnd = net->mtu;
1504 	net->partial_bytes_acked = 0;
1505 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1506 		sctp_log_cwnd(stcb, net, net->cwnd - old_cwnd, SCTP_CWND_LOG_FROM_RTX);
1507 	}
1508 }
1509 
1510 void
1511 sctp_htcp_cwnd_update_after_fr_timer(struct sctp_inpcb *inp,
1512     struct sctp_tcb *stcb, struct sctp_nets *net)
1513 {
1514 	int old_cwnd;
1515 
1516 	old_cwnd = net->cwnd;
1517 
1518 	sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_EARLY_FR_TMR, SCTP_SO_NOT_LOCKED);
1519 	net->htcp_ca.last_cong = sctp_get_tick_count();
1520 	/*
1521 	 * make a small adjustment to cwnd and force to CA.
1522 	 */
1523 	if (net->cwnd > net->mtu)
1524 		/* drop down one MTU after sending */
1525 		net->cwnd -= net->mtu;
1526 	if (net->cwnd < net->ssthresh)
1527 		/* still in SS move to CA */
1528 		net->ssthresh = net->cwnd - 1;
1529 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1530 		sctp_log_cwnd(stcb, net, (old_cwnd - net->cwnd), SCTP_CWND_LOG_FROM_FR);
1531 	}
1532 }
1533 
1534 void
1535 sctp_htcp_cwnd_update_after_ecn_echo(struct sctp_tcb *stcb,
1536     struct sctp_nets *net)
1537 {
1538 	int old_cwnd;
1539 
1540 	old_cwnd = net->cwnd;
1541 
1542 	/* JRS - reset hctp as if state changed */
1543 	htcp_reset(&net->htcp_ca);
1544 	SCTP_STAT_INCR(sctps_ecnereducedcwnd);
1545 	net->ssthresh = htcp_recalc_ssthresh(stcb, net);
1546 	if (net->ssthresh < net->mtu) {
1547 		net->ssthresh = net->mtu;
1548 		/* here back off the timer as well, to slow us down */
1549 		net->RTO <<= 1;
1550 	}
1551 	net->cwnd = net->ssthresh;
1552 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_MONITOR_ENABLE) {
1553 		sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_SAT);
1554 	}
1555 }
1556