1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2007 The University of Aberdeen, Scotland, UK
4*4882a593Smuzhiyun * Copyright (c) 2005-7 The University of Waikato, Hamilton, New Zealand.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * An implementation of the DCCP protocol
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * This code has been developed by the University of Waikato WAND
9*4882a593Smuzhiyun * research group. For further information please see https://www.wand.net.nz/
10*4882a593Smuzhiyun * or e-mail Ian McDonald - ian.mcdonald@jandi.co.nz
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * This code also uses code from Lulea University, rereleased as GPL by its
13*4882a593Smuzhiyun * authors:
14*4882a593Smuzhiyun * Copyright (c) 2003 Nils-Erik Mattsson, Joacim Haggmark, Magnus Erixzon
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * Changes to meet Linux coding standards, to make it meet latest ccid3 draft
17*4882a593Smuzhiyun * and to make it work as a loadable module in the DCCP stack written by
18*4882a593Smuzhiyun * Arnaldo Carvalho de Melo <acme@conectiva.com.br>.
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
21*4882a593Smuzhiyun */
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #include <linux/string.h>
24*4882a593Smuzhiyun #include <linux/slab.h>
25*4882a593Smuzhiyun #include "packet_history.h"
26*4882a593Smuzhiyun #include "../../dccp.h"
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /*
29*4882a593Smuzhiyun * Transmitter History Routines
30*4882a593Smuzhiyun */
31*4882a593Smuzhiyun static struct kmem_cache *tfrc_tx_hist_slab;
32*4882a593Smuzhiyun
tfrc_tx_packet_history_init(void)33*4882a593Smuzhiyun int __init tfrc_tx_packet_history_init(void)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun tfrc_tx_hist_slab = kmem_cache_create("tfrc_tx_hist",
36*4882a593Smuzhiyun sizeof(struct tfrc_tx_hist_entry),
37*4882a593Smuzhiyun 0, SLAB_HWCACHE_ALIGN, NULL);
38*4882a593Smuzhiyun return tfrc_tx_hist_slab == NULL ? -ENOBUFS : 0;
39*4882a593Smuzhiyun }
40*4882a593Smuzhiyun
tfrc_tx_packet_history_exit(void)41*4882a593Smuzhiyun void tfrc_tx_packet_history_exit(void)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun if (tfrc_tx_hist_slab != NULL) {
44*4882a593Smuzhiyun kmem_cache_destroy(tfrc_tx_hist_slab);
45*4882a593Smuzhiyun tfrc_tx_hist_slab = NULL;
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun
tfrc_tx_hist_add(struct tfrc_tx_hist_entry ** headp,u64 seqno)49*4882a593Smuzhiyun int tfrc_tx_hist_add(struct tfrc_tx_hist_entry **headp, u64 seqno)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun struct tfrc_tx_hist_entry *entry = kmem_cache_alloc(tfrc_tx_hist_slab, gfp_any());
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun if (entry == NULL)
54*4882a593Smuzhiyun return -ENOBUFS;
55*4882a593Smuzhiyun entry->seqno = seqno;
56*4882a593Smuzhiyun entry->stamp = ktime_get_real();
57*4882a593Smuzhiyun entry->next = *headp;
58*4882a593Smuzhiyun *headp = entry;
59*4882a593Smuzhiyun return 0;
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun
tfrc_tx_hist_purge(struct tfrc_tx_hist_entry ** headp)62*4882a593Smuzhiyun void tfrc_tx_hist_purge(struct tfrc_tx_hist_entry **headp)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun struct tfrc_tx_hist_entry *head = *headp;
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun while (head != NULL) {
67*4882a593Smuzhiyun struct tfrc_tx_hist_entry *next = head->next;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun kmem_cache_free(tfrc_tx_hist_slab, head);
70*4882a593Smuzhiyun head = next;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun *headp = NULL;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun /*
77*4882a593Smuzhiyun * Receiver History Routines
78*4882a593Smuzhiyun */
79*4882a593Smuzhiyun static struct kmem_cache *tfrc_rx_hist_slab;
80*4882a593Smuzhiyun
tfrc_rx_packet_history_init(void)81*4882a593Smuzhiyun int __init tfrc_rx_packet_history_init(void)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun tfrc_rx_hist_slab = kmem_cache_create("tfrc_rxh_cache",
84*4882a593Smuzhiyun sizeof(struct tfrc_rx_hist_entry),
85*4882a593Smuzhiyun 0, SLAB_HWCACHE_ALIGN, NULL);
86*4882a593Smuzhiyun return tfrc_rx_hist_slab == NULL ? -ENOBUFS : 0;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun
tfrc_rx_packet_history_exit(void)89*4882a593Smuzhiyun void tfrc_rx_packet_history_exit(void)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun if (tfrc_rx_hist_slab != NULL) {
92*4882a593Smuzhiyun kmem_cache_destroy(tfrc_rx_hist_slab);
93*4882a593Smuzhiyun tfrc_rx_hist_slab = NULL;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun
tfrc_rx_hist_entry_from_skb(struct tfrc_rx_hist_entry * entry,const struct sk_buff * skb,const u64 ndp)97*4882a593Smuzhiyun static inline void tfrc_rx_hist_entry_from_skb(struct tfrc_rx_hist_entry *entry,
98*4882a593Smuzhiyun const struct sk_buff *skb,
99*4882a593Smuzhiyun const u64 ndp)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun const struct dccp_hdr *dh = dccp_hdr(skb);
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun entry->tfrchrx_seqno = DCCP_SKB_CB(skb)->dccpd_seq;
104*4882a593Smuzhiyun entry->tfrchrx_ccval = dh->dccph_ccval;
105*4882a593Smuzhiyun entry->tfrchrx_type = dh->dccph_type;
106*4882a593Smuzhiyun entry->tfrchrx_ndp = ndp;
107*4882a593Smuzhiyun entry->tfrchrx_tstamp = ktime_get_real();
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun
tfrc_rx_hist_add_packet(struct tfrc_rx_hist * h,const struct sk_buff * skb,const u64 ndp)110*4882a593Smuzhiyun void tfrc_rx_hist_add_packet(struct tfrc_rx_hist *h,
111*4882a593Smuzhiyun const struct sk_buff *skb,
112*4882a593Smuzhiyun const u64 ndp)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun struct tfrc_rx_hist_entry *entry = tfrc_rx_hist_last_rcv(h);
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun tfrc_rx_hist_entry_from_skb(entry, skb, ndp);
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /* has the packet contained in skb been seen before? */
tfrc_rx_hist_duplicate(struct tfrc_rx_hist * h,struct sk_buff * skb)120*4882a593Smuzhiyun int tfrc_rx_hist_duplicate(struct tfrc_rx_hist *h, struct sk_buff *skb)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun const u64 seq = DCCP_SKB_CB(skb)->dccpd_seq;
123*4882a593Smuzhiyun int i;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun if (dccp_delta_seqno(tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno, seq) <= 0)
126*4882a593Smuzhiyun return 1;
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun for (i = 1; i <= h->loss_count; i++)
129*4882a593Smuzhiyun if (tfrc_rx_hist_entry(h, i)->tfrchrx_seqno == seq)
130*4882a593Smuzhiyun return 1;
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun return 0;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun
tfrc_rx_hist_swap(struct tfrc_rx_hist * h,const u8 a,const u8 b)135*4882a593Smuzhiyun static void tfrc_rx_hist_swap(struct tfrc_rx_hist *h, const u8 a, const u8 b)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun const u8 idx_a = tfrc_rx_hist_index(h, a),
138*4882a593Smuzhiyun idx_b = tfrc_rx_hist_index(h, b);
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun swap(h->ring[idx_a], h->ring[idx_b]);
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /*
144*4882a593Smuzhiyun * Private helper functions for loss detection.
145*4882a593Smuzhiyun *
146*4882a593Smuzhiyun * In the descriptions, `Si' refers to the sequence number of entry number i,
147*4882a593Smuzhiyun * whose NDP count is `Ni' (lower case is used for variables).
148*4882a593Smuzhiyun * Note: All __xxx_loss functions expect that a test against duplicates has been
149*4882a593Smuzhiyun * performed already: the seqno of the skb must not be less than the seqno
150*4882a593Smuzhiyun * of loss_prev; and it must not equal that of any valid history entry.
151*4882a593Smuzhiyun */
__do_track_loss(struct tfrc_rx_hist * h,struct sk_buff * skb,u64 n1)152*4882a593Smuzhiyun static void __do_track_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u64 n1)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,
155*4882a593Smuzhiyun s1 = DCCP_SKB_CB(skb)->dccpd_seq;
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun if (!dccp_loss_free(s0, s1, n1)) { /* gap between S0 and S1 */
158*4882a593Smuzhiyun h->loss_count = 1;
159*4882a593Smuzhiyun tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n1);
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun
__one_after_loss(struct tfrc_rx_hist * h,struct sk_buff * skb,u32 n2)163*4882a593Smuzhiyun static void __one_after_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u32 n2)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,
166*4882a593Smuzhiyun s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,
167*4882a593Smuzhiyun s2 = DCCP_SKB_CB(skb)->dccpd_seq;
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun if (likely(dccp_delta_seqno(s1, s2) > 0)) { /* S1 < S2 */
170*4882a593Smuzhiyun h->loss_count = 2;
171*4882a593Smuzhiyun tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 2), skb, n2);
172*4882a593Smuzhiyun return;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun /* S0 < S2 < S1 */
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun if (dccp_loss_free(s0, s2, n2)) {
178*4882a593Smuzhiyun u64 n1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_ndp;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun if (dccp_loss_free(s2, s1, n1)) {
181*4882a593Smuzhiyun /* hole is filled: S0, S2, and S1 are consecutive */
182*4882a593Smuzhiyun h->loss_count = 0;
183*4882a593Smuzhiyun h->loss_start = tfrc_rx_hist_index(h, 1);
184*4882a593Smuzhiyun } else
185*4882a593Smuzhiyun /* gap between S2 and S1: just update loss_prev */
186*4882a593Smuzhiyun tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_loss_prev(h), skb, n2);
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun } else { /* gap between S0 and S2 */
189*4882a593Smuzhiyun /*
190*4882a593Smuzhiyun * Reorder history to insert S2 between S0 and S1
191*4882a593Smuzhiyun */
192*4882a593Smuzhiyun tfrc_rx_hist_swap(h, 0, 3);
193*4882a593Smuzhiyun h->loss_start = tfrc_rx_hist_index(h, 3);
194*4882a593Smuzhiyun tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n2);
195*4882a593Smuzhiyun h->loss_count = 2;
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun /* return 1 if a new loss event has been identified */
__two_after_loss(struct tfrc_rx_hist * h,struct sk_buff * skb,u32 n3)200*4882a593Smuzhiyun static int __two_after_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u32 n3)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,
203*4882a593Smuzhiyun s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,
204*4882a593Smuzhiyun s2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_seqno,
205*4882a593Smuzhiyun s3 = DCCP_SKB_CB(skb)->dccpd_seq;
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun if (likely(dccp_delta_seqno(s2, s3) > 0)) { /* S2 < S3 */
208*4882a593Smuzhiyun h->loss_count = 3;
209*4882a593Smuzhiyun tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 3), skb, n3);
210*4882a593Smuzhiyun return 1;
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun /* S3 < S2 */
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun if (dccp_delta_seqno(s1, s3) > 0) { /* S1 < S3 < S2 */
216*4882a593Smuzhiyun /*
217*4882a593Smuzhiyun * Reorder history to insert S3 between S1 and S2
218*4882a593Smuzhiyun */
219*4882a593Smuzhiyun tfrc_rx_hist_swap(h, 2, 3);
220*4882a593Smuzhiyun tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 2), skb, n3);
221*4882a593Smuzhiyun h->loss_count = 3;
222*4882a593Smuzhiyun return 1;
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun /* S0 < S3 < S1 */
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun if (dccp_loss_free(s0, s3, n3)) {
228*4882a593Smuzhiyun u64 n1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_ndp;
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun if (dccp_loss_free(s3, s1, n1)) {
231*4882a593Smuzhiyun /* hole between S0 and S1 filled by S3 */
232*4882a593Smuzhiyun u64 n2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_ndp;
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun if (dccp_loss_free(s1, s2, n2)) {
235*4882a593Smuzhiyun /* entire hole filled by S0, S3, S1, S2 */
236*4882a593Smuzhiyun h->loss_start = tfrc_rx_hist_index(h, 2);
237*4882a593Smuzhiyun h->loss_count = 0;
238*4882a593Smuzhiyun } else {
239*4882a593Smuzhiyun /* gap remains between S1 and S2 */
240*4882a593Smuzhiyun h->loss_start = tfrc_rx_hist_index(h, 1);
241*4882a593Smuzhiyun h->loss_count = 1;
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun } else /* gap exists between S3 and S1, loss_count stays at 2 */
245*4882a593Smuzhiyun tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_loss_prev(h), skb, n3);
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun return 0;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun /*
251*4882a593Smuzhiyun * The remaining case: S0 < S3 < S1 < S2; gap between S0 and S3
252*4882a593Smuzhiyun * Reorder history to insert S3 between S0 and S1.
253*4882a593Smuzhiyun */
254*4882a593Smuzhiyun tfrc_rx_hist_swap(h, 0, 3);
255*4882a593Smuzhiyun h->loss_start = tfrc_rx_hist_index(h, 3);
256*4882a593Smuzhiyun tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n3);
257*4882a593Smuzhiyun h->loss_count = 3;
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun return 1;
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun /* recycle RX history records to continue loss detection if necessary */
__three_after_loss(struct tfrc_rx_hist * h)263*4882a593Smuzhiyun static void __three_after_loss(struct tfrc_rx_hist *h)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun /*
266*4882a593Smuzhiyun * At this stage we know already that there is a gap between S0 and S1
267*4882a593Smuzhiyun * (since S0 was the highest sequence number received before detecting
268*4882a593Smuzhiyun * the loss). To recycle the loss record, it is thus only necessary to
269*4882a593Smuzhiyun * check for other possible gaps between S1/S2 and between S2/S3.
270*4882a593Smuzhiyun */
271*4882a593Smuzhiyun u64 s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,
272*4882a593Smuzhiyun s2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_seqno,
273*4882a593Smuzhiyun s3 = tfrc_rx_hist_entry(h, 3)->tfrchrx_seqno;
274*4882a593Smuzhiyun u64 n2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_ndp,
275*4882a593Smuzhiyun n3 = tfrc_rx_hist_entry(h, 3)->tfrchrx_ndp;
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun if (dccp_loss_free(s1, s2, n2)) {
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun if (dccp_loss_free(s2, s3, n3)) {
280*4882a593Smuzhiyun /* no gap between S2 and S3: entire hole is filled */
281*4882a593Smuzhiyun h->loss_start = tfrc_rx_hist_index(h, 3);
282*4882a593Smuzhiyun h->loss_count = 0;
283*4882a593Smuzhiyun } else {
284*4882a593Smuzhiyun /* gap between S2 and S3 */
285*4882a593Smuzhiyun h->loss_start = tfrc_rx_hist_index(h, 2);
286*4882a593Smuzhiyun h->loss_count = 1;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun } else { /* gap between S1 and S2 */
290*4882a593Smuzhiyun h->loss_start = tfrc_rx_hist_index(h, 1);
291*4882a593Smuzhiyun h->loss_count = 2;
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /**
296*4882a593Smuzhiyun * tfrc_rx_handle_loss - Loss detection and further processing
297*4882a593Smuzhiyun * @h: The non-empty RX history object
298*4882a593Smuzhiyun * @lh: Loss Intervals database to update
299*4882a593Smuzhiyun * @skb: Currently received packet
300*4882a593Smuzhiyun * @ndp: The NDP count belonging to @skb
301*4882a593Smuzhiyun * @calc_first_li: Caller-dependent computation of first loss interval in @lh
302*4882a593Smuzhiyun * @sk: Used by @calc_first_li (see tfrc_lh_interval_add)
303*4882a593Smuzhiyun *
304*4882a593Smuzhiyun * Chooses action according to pending loss, updates LI database when a new
305*4882a593Smuzhiyun * loss was detected, and does required post-processing. Returns 1 when caller
306*4882a593Smuzhiyun * should send feedback, 0 otherwise.
307*4882a593Smuzhiyun * Since it also takes care of reordering during loss detection and updates the
308*4882a593Smuzhiyun * records accordingly, the caller should not perform any more RX history
309*4882a593Smuzhiyun * operations when loss_count is greater than 0 after calling this function.
310*4882a593Smuzhiyun */
tfrc_rx_handle_loss(struct tfrc_rx_hist * h,struct tfrc_loss_hist * lh,struct sk_buff * skb,const u64 ndp,u32 (* calc_first_li)(struct sock *),struct sock * sk)311*4882a593Smuzhiyun int tfrc_rx_handle_loss(struct tfrc_rx_hist *h,
312*4882a593Smuzhiyun struct tfrc_loss_hist *lh,
313*4882a593Smuzhiyun struct sk_buff *skb, const u64 ndp,
314*4882a593Smuzhiyun u32 (*calc_first_li)(struct sock *), struct sock *sk)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun int is_new_loss = 0;
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun if (h->loss_count == 0) {
319*4882a593Smuzhiyun __do_track_loss(h, skb, ndp);
320*4882a593Smuzhiyun } else if (h->loss_count == 1) {
321*4882a593Smuzhiyun __one_after_loss(h, skb, ndp);
322*4882a593Smuzhiyun } else if (h->loss_count != 2) {
323*4882a593Smuzhiyun DCCP_BUG("invalid loss_count %d", h->loss_count);
324*4882a593Smuzhiyun } else if (__two_after_loss(h, skb, ndp)) {
325*4882a593Smuzhiyun /*
326*4882a593Smuzhiyun * Update Loss Interval database and recycle RX records
327*4882a593Smuzhiyun */
328*4882a593Smuzhiyun is_new_loss = tfrc_lh_interval_add(lh, h, calc_first_li, sk);
329*4882a593Smuzhiyun __three_after_loss(h);
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun return is_new_loss;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun
tfrc_rx_hist_alloc(struct tfrc_rx_hist * h)334*4882a593Smuzhiyun int tfrc_rx_hist_alloc(struct tfrc_rx_hist *h)
335*4882a593Smuzhiyun {
336*4882a593Smuzhiyun int i;
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun for (i = 0; i <= TFRC_NDUPACK; i++) {
339*4882a593Smuzhiyun h->ring[i] = kmem_cache_alloc(tfrc_rx_hist_slab, GFP_ATOMIC);
340*4882a593Smuzhiyun if (h->ring[i] == NULL)
341*4882a593Smuzhiyun goto out_free;
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun h->loss_count = h->loss_start = 0;
345*4882a593Smuzhiyun return 0;
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun out_free:
348*4882a593Smuzhiyun while (i-- != 0) {
349*4882a593Smuzhiyun kmem_cache_free(tfrc_rx_hist_slab, h->ring[i]);
350*4882a593Smuzhiyun h->ring[i] = NULL;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun return -ENOBUFS;
353*4882a593Smuzhiyun }
354*4882a593Smuzhiyun
tfrc_rx_hist_purge(struct tfrc_rx_hist * h)355*4882a593Smuzhiyun void tfrc_rx_hist_purge(struct tfrc_rx_hist *h)
356*4882a593Smuzhiyun {
357*4882a593Smuzhiyun int i;
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun for (i = 0; i <= TFRC_NDUPACK; ++i)
360*4882a593Smuzhiyun if (h->ring[i] != NULL) {
361*4882a593Smuzhiyun kmem_cache_free(tfrc_rx_hist_slab, h->ring[i]);
362*4882a593Smuzhiyun h->ring[i] = NULL;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun }
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun /**
367*4882a593Smuzhiyun * tfrc_rx_hist_rtt_last_s - reference entry to compute RTT samples against
368*4882a593Smuzhiyun * @h: The non-empty RX history object
369*4882a593Smuzhiyun */
370*4882a593Smuzhiyun static inline struct tfrc_rx_hist_entry *
tfrc_rx_hist_rtt_last_s(const struct tfrc_rx_hist * h)371*4882a593Smuzhiyun tfrc_rx_hist_rtt_last_s(const struct tfrc_rx_hist *h)
372*4882a593Smuzhiyun {
373*4882a593Smuzhiyun return h->ring[0];
374*4882a593Smuzhiyun }
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun /**
377*4882a593Smuzhiyun * tfrc_rx_hist_rtt_prev_s - previously suitable (wrt rtt_last_s) RTT-sampling entry
378*4882a593Smuzhiyun * @h: The non-empty RX history object
379*4882a593Smuzhiyun */
380*4882a593Smuzhiyun static inline struct tfrc_rx_hist_entry *
tfrc_rx_hist_rtt_prev_s(const struct tfrc_rx_hist * h)381*4882a593Smuzhiyun tfrc_rx_hist_rtt_prev_s(const struct tfrc_rx_hist *h)
382*4882a593Smuzhiyun {
383*4882a593Smuzhiyun return h->ring[h->rtt_sample_prev];
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun /**
387*4882a593Smuzhiyun * tfrc_rx_hist_sample_rtt - Sample RTT from timestamp / CCVal
388*4882a593Smuzhiyun * Based on ideas presented in RFC 4342, 8.1. Returns 0 if it was not able
389*4882a593Smuzhiyun * to compute a sample with given data - calling function should check this.
390*4882a593Smuzhiyun */
tfrc_rx_hist_sample_rtt(struct tfrc_rx_hist * h,const struct sk_buff * skb)391*4882a593Smuzhiyun u32 tfrc_rx_hist_sample_rtt(struct tfrc_rx_hist *h, const struct sk_buff *skb)
392*4882a593Smuzhiyun {
393*4882a593Smuzhiyun u32 sample = 0,
394*4882a593Smuzhiyun delta_v = SUB16(dccp_hdr(skb)->dccph_ccval,
395*4882a593Smuzhiyun tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun if (delta_v < 1 || delta_v > 4) { /* unsuitable CCVal delta */
398*4882a593Smuzhiyun if (h->rtt_sample_prev == 2) { /* previous candidate stored */
399*4882a593Smuzhiyun sample = SUB16(tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_ccval,
400*4882a593Smuzhiyun tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
401*4882a593Smuzhiyun if (sample)
402*4882a593Smuzhiyun sample = 4 / sample *
403*4882a593Smuzhiyun ktime_us_delta(tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_tstamp,
404*4882a593Smuzhiyun tfrc_rx_hist_rtt_last_s(h)->tfrchrx_tstamp);
405*4882a593Smuzhiyun else /*
406*4882a593Smuzhiyun * FIXME: This condition is in principle not
407*4882a593Smuzhiyun * possible but occurs when CCID is used for
408*4882a593Smuzhiyun * two-way data traffic. I have tried to trace
409*4882a593Smuzhiyun * it, but the cause does not seem to be here.
410*4882a593Smuzhiyun */
411*4882a593Smuzhiyun DCCP_BUG("please report to dccp@vger.kernel.org"
412*4882a593Smuzhiyun " => prev = %u, last = %u",
413*4882a593Smuzhiyun tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_ccval,
414*4882a593Smuzhiyun tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
415*4882a593Smuzhiyun } else if (delta_v < 1) {
416*4882a593Smuzhiyun h->rtt_sample_prev = 1;
417*4882a593Smuzhiyun goto keep_ref_for_next_time;
418*4882a593Smuzhiyun }
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun } else if (delta_v == 4) /* optimal match */
421*4882a593Smuzhiyun sample = ktime_to_us(net_timedelta(tfrc_rx_hist_rtt_last_s(h)->tfrchrx_tstamp));
422*4882a593Smuzhiyun else { /* suboptimal match */
423*4882a593Smuzhiyun h->rtt_sample_prev = 2;
424*4882a593Smuzhiyun goto keep_ref_for_next_time;
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun if (unlikely(sample > DCCP_SANE_RTT_MAX)) {
428*4882a593Smuzhiyun DCCP_WARN("RTT sample %u too large, using max\n", sample);
429*4882a593Smuzhiyun sample = DCCP_SANE_RTT_MAX;
430*4882a593Smuzhiyun }
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun h->rtt_sample_prev = 0; /* use current entry as next reference */
433*4882a593Smuzhiyun keep_ref_for_next_time:
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun return sample;
436*4882a593Smuzhiyun }
437