+static void update_base_rtt(struct tcpcb *tp, uint32_t rtt);
+uint32_t get_base_rtt(struct tcpcb *tp);
+void tcp_set_background_cc(struct socket *so);
+void tcp_set_foreground_cc(struct socket *so);
+static void tcp_set_new_cc(struct socket *so, uint16_t cc_index);
+
+#if TRAFFIC_MGT
+void
+reset_acc_iaj(struct tcpcb *tp)
+{
+ tp->acc_iaj = 0;
+ tp->iaj_rwintop = 0;
+ clear_iaj_state(tp);
+}
+
+static inline void
+update_iaj_state(struct tcpcb *tp, uint32_t size, int rst_size)
+{
+ if (rst_size > 0)
+ tp->iaj_size = 0;
+ if (tp->iaj_size == 0 || size >= tp->iaj_size) {
+ tp->iaj_size = size;
+ tp->iaj_rcv_ts = tcp_now;
+ tp->iaj_small_pkt = 0;
+ }
+}
+
+static inline void
+clear_iaj_state(struct tcpcb *tp)
+{
+ tp->iaj_rcv_ts = 0;
+}
+
+/* For every 32 bit unsigned integer(v), this function will find the
+ * largest integer n such that (n*n <= v). This takes at most 16 iterations
+ * irrespective of the value of v and does not involve multiplications.
+ */
+static inline int
+isqrt(unsigned int val) {
+ unsigned int sqrt_cache[11] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100};
+ unsigned int temp, g=0, b=0x8000, bshft=15;
+ if ( val <= 100) {
+ for (g = 0; g <= 10; ++g) {
+ if (sqrt_cache[g] > val) {
+ g--;
+ break;
+ } else if (sqrt_cache[g] == val) {
+ break;
+ }
+ }
+ } else {
+ do {
+ temp = (((g << 1) + b) << (bshft--));
+ if (val >= temp) {
+ g += b;
+ val -= temp;
+ }
+ b >>= 1;
+ } while ( b > 0 && val > 0);
+ }
+ return(g);
+}
+
+void
+compute_iaj(struct tcpcb *tp)
+{
+ /* When accumulated IAJ reaches MAX_ACC_IAJ in milliseconds, throttle the
+ * receive window to a minimum of MIN_IAJ_WIN packets
+ */
+#define MAX_ACC_IAJ (tcp_acc_iaj_high_thresh + tcp_acc_iaj_react_limit)
+
+ uint32_t allowed_iaj, acc_iaj = 0;
+ uint32_t cur_iaj = tcp_now - tp->iaj_rcv_ts;
+
+ uint32_t mean, temp;
+ int32_t cur_iaj_dev;
+ cur_iaj_dev = (cur_iaj - tp->avg_iaj);
+
+ /* Allow a jitter of "allowed_iaj" milliseconds. Some connections may have a
+ * constant jitter more than that. We detect this by using
+ * standard deviation.
+ */
+ allowed_iaj = tp->avg_iaj + tp->std_dev_iaj;
+ if (allowed_iaj < tcp_allowed_iaj)
+ allowed_iaj = tcp_allowed_iaj;
+
+ /* Initially when the connection starts, the senders congestion window
+ * is small. During this period we avoid throttling a connection because
+ * we do not have a good starting point for allowed_iaj. IAJ_IGNORE_PKTCNT
+ * is used to quietly gloss over the first few packets.
+ */
+ if (tp->iaj_pktcnt > IAJ_IGNORE_PKTCNT) {
+ if ( cur_iaj <= allowed_iaj ) {
+ if (tp->acc_iaj >= 2)
+ acc_iaj = tp->acc_iaj - 2;
+ else
+ acc_iaj = 0;
+ } else {
+ acc_iaj = tp->acc_iaj + (cur_iaj - allowed_iaj);
+ }
+
+ if (acc_iaj > MAX_ACC_IAJ)
+ acc_iaj = MAX_ACC_IAJ;
+ tp->acc_iaj = acc_iaj;
+ }
+
+ /* Compute weighted average where the history has a weight of
+ * 15 out of 16 and the current value has a weight of 1 out of 16.
+ * This will make the short-term measurements have more weight.
+ */
+ tp->avg_iaj = (((tp->avg_iaj << 4) - tp->avg_iaj) + cur_iaj) >> 4;
+
+ /* Compute Root-mean-square of deviation where mean is a weighted
+ * average as described above
+ */
+ temp = tp->std_dev_iaj * tp->std_dev_iaj;
+ mean = (((temp << 4) - temp) + (cur_iaj_dev * cur_iaj_dev)) >> 4;
+
+ tp->std_dev_iaj = isqrt(mean);
+
+ DTRACE_TCP3(iaj, struct tcpcb *, tp, uint32_t, cur_iaj, uint32_t, allowed_iaj);
+
+ return;
+}
+#endif /* TRAFFIC_MGT */