xref: /openbmc/linux/net/dccp/ccids/lib/loss_interval.c (revision cdd38c5f1ce4398ec58fec95904b75824daab7b5)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ae6706f0SArnaldo Carvalho de Melo /*
38a9c7e92SGerrit Renker  *  Copyright (c) 2007   The University of Aberdeen, Scotland, UK
4b2f41ff4SIan McDonald  *  Copyright (c) 2005-7 The University of Waikato, Hamilton, New Zealand.
5b2f41ff4SIan McDonald  *  Copyright (c) 2005-7 Ian McDonald <ian.mcdonald@jandi.co.nz>
6ae6706f0SArnaldo Carvalho de Melo  *  Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
7ae6706f0SArnaldo Carvalho de Melo  */
866a377c5SIan McDonald #include <net/sock.h>
9cc0a910bSArnaldo Carvalho de Melo #include "tfrc.h"
10ae6706f0SArnaldo Carvalho de Melo 
118a9c7e92SGerrit Renker static struct kmem_cache  *tfrc_lh_slab  __read_mostly;
128a9c7e92SGerrit Renker /* Loss Interval weights from [RFC 3448, 5.4], scaled by 10 */
138a9c7e92SGerrit Renker static const int tfrc_lh_weights[NINTERVAL] = { 10, 10, 10, 10, 8, 6, 4, 2 };
148a9c7e92SGerrit Renker 
158a9c7e92SGerrit Renker /* implements LIFO semantics on the array */
LIH_INDEX(const u8 ctr)168a9c7e92SGerrit Renker static inline u8 LIH_INDEX(const u8 ctr)
178a9c7e92SGerrit Renker {
18aa1b1ff0SGerrit Renker 	return LIH_SIZE - 1 - (ctr % LIH_SIZE);
198a9c7e92SGerrit Renker }
208a9c7e92SGerrit Renker 
218a9c7e92SGerrit Renker /* the `counter' index always points at the next entry to be populated */
tfrc_lh_peek(struct tfrc_loss_hist * lh)228a9c7e92SGerrit Renker static inline struct tfrc_loss_interval *tfrc_lh_peek(struct tfrc_loss_hist *lh)
238a9c7e92SGerrit Renker {
248a9c7e92SGerrit Renker 	return lh->counter ? lh->ring[LIH_INDEX(lh->counter - 1)] : NULL;
258a9c7e92SGerrit Renker }
268a9c7e92SGerrit Renker 
278a9c7e92SGerrit Renker /* given i with 0 <= i <= k, return I_i as per the rfc3448bis notation */
tfrc_lh_get_interval(struct tfrc_loss_hist * lh,const u8 i)288a9c7e92SGerrit Renker static inline u32 tfrc_lh_get_interval(struct tfrc_loss_hist *lh, const u8 i)
298a9c7e92SGerrit Renker {
308a9c7e92SGerrit Renker 	BUG_ON(i >= lh->counter);
318a9c7e92SGerrit Renker 	return lh->ring[LIH_INDEX(lh->counter - i - 1)]->li_length;
328a9c7e92SGerrit Renker }
338a9c7e92SGerrit Renker 
348a9c7e92SGerrit Renker /*
358a9c7e92SGerrit Renker  *	On-demand allocation and de-allocation of entries
368a9c7e92SGerrit Renker  */
tfrc_lh_demand_next(struct tfrc_loss_hist * lh)378a9c7e92SGerrit Renker static struct tfrc_loss_interval *tfrc_lh_demand_next(struct tfrc_loss_hist *lh)
388a9c7e92SGerrit Renker {
398a9c7e92SGerrit Renker 	if (lh->ring[LIH_INDEX(lh->counter)] == NULL)
408a9c7e92SGerrit Renker 		lh->ring[LIH_INDEX(lh->counter)] = kmem_cache_alloc(tfrc_lh_slab,
418a9c7e92SGerrit Renker 								    GFP_ATOMIC);
428a9c7e92SGerrit Renker 	return lh->ring[LIH_INDEX(lh->counter)];
438a9c7e92SGerrit Renker }
448a9c7e92SGerrit Renker 
tfrc_lh_cleanup(struct tfrc_loss_hist * lh)458a9c7e92SGerrit Renker void tfrc_lh_cleanup(struct tfrc_loss_hist *lh)
468a9c7e92SGerrit Renker {
478a9c7e92SGerrit Renker 	if (!tfrc_lh_is_initialised(lh))
488a9c7e92SGerrit Renker 		return;
498a9c7e92SGerrit Renker 
508a9c7e92SGerrit Renker 	for (lh->counter = 0; lh->counter < LIH_SIZE; lh->counter++)
518a9c7e92SGerrit Renker 		if (lh->ring[LIH_INDEX(lh->counter)] != NULL) {
528a9c7e92SGerrit Renker 			kmem_cache_free(tfrc_lh_slab,
538a9c7e92SGerrit Renker 					lh->ring[LIH_INDEX(lh->counter)]);
548a9c7e92SGerrit Renker 			lh->ring[LIH_INDEX(lh->counter)] = NULL;
558a9c7e92SGerrit Renker 		}
568a9c7e92SGerrit Renker }
578a9c7e92SGerrit Renker 
tfrc_lh_calc_i_mean(struct tfrc_loss_hist * lh)588a9c7e92SGerrit Renker static void tfrc_lh_calc_i_mean(struct tfrc_loss_hist *lh)
598a9c7e92SGerrit Renker {
608a9c7e92SGerrit Renker 	u32 i_i, i_tot0 = 0, i_tot1 = 0, w_tot = 0;
618a9c7e92SGerrit Renker 	int i, k = tfrc_lh_length(lh) - 1; /* k is as in rfc3448bis, 5.4 */
628a9c7e92SGerrit Renker 
63959fd992SGerrit Renker 	if (k <= 0)
64959fd992SGerrit Renker 		return;
65959fd992SGerrit Renker 
668a9c7e92SGerrit Renker 	for (i = 0; i <= k; i++) {
678a9c7e92SGerrit Renker 		i_i = tfrc_lh_get_interval(lh, i);
688a9c7e92SGerrit Renker 
698a9c7e92SGerrit Renker 		if (i < k) {
708a9c7e92SGerrit Renker 			i_tot0 += i_i * tfrc_lh_weights[i];
718a9c7e92SGerrit Renker 			w_tot  += tfrc_lh_weights[i];
728a9c7e92SGerrit Renker 		}
738a9c7e92SGerrit Renker 		if (i > 0)
748a9c7e92SGerrit Renker 			i_tot1 += i_i * tfrc_lh_weights[i-1];
758a9c7e92SGerrit Renker 	}
768a9c7e92SGerrit Renker 
778a9c7e92SGerrit Renker 	lh->i_mean = max(i_tot0, i_tot1) / w_tot;
788a9c7e92SGerrit Renker }
798a9c7e92SGerrit Renker 
808a9c7e92SGerrit Renker /**
818a9c7e92SGerrit Renker  * tfrc_lh_update_i_mean  -  Update the `open' loss interval I_0
82*0b609b55SAndrew Lunn  * @lh: histogram to update
83*0b609b55SAndrew Lunn  * @skb: received socket triggering loss interval update
84*0b609b55SAndrew Lunn  *
85410e27a4SGerrit Renker  * For recomputing p: returns `true' if p > p_prev  <=>  1/p < 1/p_prev
868a9c7e92SGerrit Renker  */
tfrc_lh_update_i_mean(struct tfrc_loss_hist * lh,struct sk_buff * skb)87410e27a4SGerrit Renker u8 tfrc_lh_update_i_mean(struct tfrc_loss_hist *lh, struct sk_buff *skb)
888a9c7e92SGerrit Renker {
898a9c7e92SGerrit Renker 	struct tfrc_loss_interval *cur = tfrc_lh_peek(lh);
90410e27a4SGerrit Renker 	u32 old_i_mean = lh->i_mean;
912eeea7baSGerrit Renker 	s64 len;
928a9c7e92SGerrit Renker 
938a9c7e92SGerrit Renker 	if (cur == NULL)			/* not initialised */
94410e27a4SGerrit Renker 		return 0;
958a9c7e92SGerrit Renker 
962eeea7baSGerrit Renker 	len = dccp_delta_seqno(cur->li_seqno, DCCP_SKB_CB(skb)->dccpd_seq) + 1;
978a9c7e92SGerrit Renker 
982eeea7baSGerrit Renker 	if (len - (s64)cur->li_length <= 0)	/* duplicate or reordered */
99410e27a4SGerrit Renker 		return 0;
1008a9c7e92SGerrit Renker 
1018a9c7e92SGerrit Renker 	if (SUB16(dccp_hdr(skb)->dccph_ccval, cur->li_ccval) > 4)
1028a9c7e92SGerrit Renker 		/*
1038a9c7e92SGerrit Renker 		 * Implements RFC 4342, 10.2:
1048a9c7e92SGerrit Renker 		 * If a packet S (skb) exists whose seqno comes `after' the one
1058a9c7e92SGerrit Renker 		 * starting the current loss interval (cur) and if the modulo-16
1068a9c7e92SGerrit Renker 		 * distance from C(cur) to C(S) is greater than 4, consider all
1078a9c7e92SGerrit Renker 		 * subsequent packets as belonging to a new loss interval. This
1088a9c7e92SGerrit Renker 		 * test is necessary since CCVal may wrap between intervals.
1098a9c7e92SGerrit Renker 		 */
1108a9c7e92SGerrit Renker 		cur->li_is_closed = 1;
1118a9c7e92SGerrit Renker 
1128a9c7e92SGerrit Renker 	if (tfrc_lh_length(lh) == 1)		/* due to RFC 3448, 6.3.1 */
113410e27a4SGerrit Renker 		return 0;
1148a9c7e92SGerrit Renker 
1152eeea7baSGerrit Renker 	cur->li_length = len;
1168a9c7e92SGerrit Renker 	tfrc_lh_calc_i_mean(lh);
117410e27a4SGerrit Renker 
118a02cec21SEric Dumazet 	return lh->i_mean < old_i_mean;
1198a9c7e92SGerrit Renker }
1208a9c7e92SGerrit Renker 
1218a9c7e92SGerrit Renker /* Determine if `new_loss' does begin a new loss interval [RFC 4342, 10.2] */
tfrc_lh_is_new_loss(struct tfrc_loss_interval * cur,struct tfrc_rx_hist_entry * new_loss)1228a9c7e92SGerrit Renker static inline u8 tfrc_lh_is_new_loss(struct tfrc_loss_interval *cur,
1238a9c7e92SGerrit Renker 				     struct tfrc_rx_hist_entry *new_loss)
1248a9c7e92SGerrit Renker {
1258a9c7e92SGerrit Renker 	return	dccp_delta_seqno(cur->li_seqno, new_loss->tfrchrx_seqno) > 0 &&
1268a9c7e92SGerrit Renker 		(cur->li_is_closed || SUB16(new_loss->tfrchrx_ccval, cur->li_ccval) > 4);
1278a9c7e92SGerrit Renker }
1288a9c7e92SGerrit Renker 
129aa1b1ff0SGerrit Renker /**
130aa1b1ff0SGerrit Renker  * tfrc_lh_interval_add  -  Insert new record into the Loss Interval database
1318a9c7e92SGerrit Renker  * @lh:		   Loss Interval database
1328a9c7e92SGerrit Renker  * @rh:		   Receive history containing a fresh loss event
1338a9c7e92SGerrit Renker  * @calc_first_li: Caller-dependent routine to compute length of first interval
1348a9c7e92SGerrit Renker  * @sk:		   Used by @calc_first_li in caller-specific way (subtyping)
1352c53040fSBen Hutchings  *
1368a9c7e92SGerrit Renker  * Updates I_mean and returns 1 if a new interval has in fact been added to @lh.
1378a9c7e92SGerrit Renker  */
tfrc_lh_interval_add(struct tfrc_loss_hist * lh,struct tfrc_rx_hist * rh,u32 (* calc_first_li)(struct sock *),struct sock * sk)138410e27a4SGerrit Renker int tfrc_lh_interval_add(struct tfrc_loss_hist *lh, struct tfrc_rx_hist *rh,
1398a9c7e92SGerrit Renker 			 u32 (*calc_first_li)(struct sock *), struct sock *sk)
1408a9c7e92SGerrit Renker {
1418a9c7e92SGerrit Renker 	struct tfrc_loss_interval *cur = tfrc_lh_peek(lh), *new;
1428a9c7e92SGerrit Renker 
1438a9c7e92SGerrit Renker 	if (cur != NULL && !tfrc_lh_is_new_loss(cur, tfrc_rx_hist_loss_prev(rh)))
144410e27a4SGerrit Renker 		return 0;
1458a9c7e92SGerrit Renker 
1468a9c7e92SGerrit Renker 	new = tfrc_lh_demand_next(lh);
1478a9c7e92SGerrit Renker 	if (unlikely(new == NULL)) {
1488a9c7e92SGerrit Renker 		DCCP_CRIT("Cannot allocate/add loss record.");
149410e27a4SGerrit Renker 		return 0;
1508a9c7e92SGerrit Renker 	}
1518a9c7e92SGerrit Renker 
1528a9c7e92SGerrit Renker 	new->li_seqno	  = tfrc_rx_hist_loss_prev(rh)->tfrchrx_seqno;
1538a9c7e92SGerrit Renker 	new->li_ccval	  = tfrc_rx_hist_loss_prev(rh)->tfrchrx_ccval;
1548a9c7e92SGerrit Renker 	new->li_is_closed = 0;
1558a9c7e92SGerrit Renker 
1568a9c7e92SGerrit Renker 	if (++lh->counter == 1)
1578a9c7e92SGerrit Renker 		lh->i_mean = new->li_length = (*calc_first_li)(sk);
1588a9c7e92SGerrit Renker 	else {
1598a9c7e92SGerrit Renker 		cur->li_length = dccp_delta_seqno(cur->li_seqno, new->li_seqno);
1608a9c7e92SGerrit Renker 		new->li_length = dccp_delta_seqno(new->li_seqno,
1612eeea7baSGerrit Renker 				  tfrc_rx_hist_last_rcv(rh)->tfrchrx_seqno) + 1;
1628a9c7e92SGerrit Renker 		if (lh->counter > (2*LIH_SIZE))
1638a9c7e92SGerrit Renker 			lh->counter -= LIH_SIZE;
1648a9c7e92SGerrit Renker 
1658a9c7e92SGerrit Renker 		tfrc_lh_calc_i_mean(lh);
1668a9c7e92SGerrit Renker 	}
167410e27a4SGerrit Renker 	return 1;
1688a9c7e92SGerrit Renker }
1698a9c7e92SGerrit Renker 
tfrc_li_init(void)170954c2db8SGerrit Renker int __init tfrc_li_init(void)
171cc4d6a3aSArnaldo Carvalho de Melo {
172954c2db8SGerrit Renker 	tfrc_lh_slab = kmem_cache_create("tfrc_li_hist",
173954c2db8SGerrit Renker 					 sizeof(struct tfrc_loss_interval), 0,
174954c2db8SGerrit Renker 					 SLAB_HWCACHE_ALIGN, NULL);
175954c2db8SGerrit Renker 	return tfrc_lh_slab == NULL ? -ENOBUFS : 0;
176cc4d6a3aSArnaldo Carvalho de Melo }
177cc4d6a3aSArnaldo Carvalho de Melo 
tfrc_li_exit(void)178954c2db8SGerrit Renker void tfrc_li_exit(void)
179cc4d6a3aSArnaldo Carvalho de Melo {
180954c2db8SGerrit Renker 	if (tfrc_lh_slab != NULL) {
181954c2db8SGerrit Renker 		kmem_cache_destroy(tfrc_lh_slab);
182954c2db8SGerrit Renker 		tfrc_lh_slab = NULL;
183cc4d6a3aSArnaldo Carvalho de Melo 	}
184276f2edcSArnaldo Carvalho de Melo }
185