blob: b7785b3581ec2ee5a747ec3975d00496f6a42183 [file] [log] [blame]
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -03001/*
Ian McDonaldf3166c02006-08-26 19:01:03 -07002 * net/dccp/packet_history.c
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -03003 *
Arnaldo Carvalho de Melo276f2ed2007-11-28 11:15:40 -02004 * Copyright (c) 2007 The University of Aberdeen, Scotland, UK
5 * Copyright (c) 2005-7 The University of Waikato, Hamilton, New Zealand.
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -03006 *
7 * An implementation of the DCCP protocol
8 *
9 * This code has been developed by the University of Waikato WAND
10 * research group. For further information please see http://www.wand.net.nz/
Ian McDonalde6bccd32006-08-26 19:01:30 -070011 * or e-mail Ian McDonald - ian.mcdonald@jandi.co.nz
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -030012 *
13 * This code also uses code from Lulea University, rereleased as GPL by its
14 * authors:
15 * Copyright (c) 2003 Nils-Erik Mattsson, Joacim Haggmark, Magnus Erixzon
16 *
17 * Changes to meet Linux coding standards, to make it meet latest ccid3 draft
18 * and to make it work as a loadable module in the DCCP stack written by
19 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>.
20 *
21 * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
22 *
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 */
37
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -030038#include <linux/string.h>
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -020039#include <linux/slab.h>
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -030040#include "packet_history.h"
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -020041#include "../../dccp.h"
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -030042
Arnaldo Carvalho de Melo9108d5f2007-11-29 22:47:15 -020043/**
44 * tfrc_tx_hist_entry - Simple singly-linked TX history list
45 * @next: next oldest entry (LIFO order)
46 * @seqno: sequence number of this entry
47 * @stamp: send time of packet with sequence number @seqno
48 */
49struct tfrc_tx_hist_entry {
50 struct tfrc_tx_hist_entry *next;
51 u64 seqno;
52 ktime_t stamp;
53};
54
Gerrit Renker7af5af32006-12-10 00:24:33 -020055/*
Arnaldo Carvalho de Melo276f2ed2007-11-28 11:15:40 -020056 * Transmitter History Routines
Gerrit Renker7af5af32006-12-10 00:24:33 -020057 */
Arnaldo Carvalho de Meloe9c8b24a2007-12-06 12:27:49 -020058static struct kmem_cache *tfrc_tx_hist_slab;
Arnaldo Carvalho de Melo276f2ed2007-11-28 11:15:40 -020059
Gerrit Renkerdf8f83f2007-12-12 12:24:49 -020060int __init tfrc_tx_packet_history_init(void)
61{
62 tfrc_tx_hist_slab = kmem_cache_create("tfrc_tx_hist",
63 sizeof(struct tfrc_tx_hist_entry),
64 0, SLAB_HWCACHE_ALIGN, NULL);
65 return tfrc_tx_hist_slab == NULL ? -ENOBUFS : 0;
66}
67
68void tfrc_tx_packet_history_exit(void)
69{
70 if (tfrc_tx_hist_slab != NULL) {
71 kmem_cache_destroy(tfrc_tx_hist_slab);
72 tfrc_tx_hist_slab = NULL;
73 }
74}
75
Arnaldo Carvalho de Melo9108d5f2007-11-29 22:47:15 -020076static struct tfrc_tx_hist_entry *
Arnaldo Carvalho de Melo276f2ed2007-11-28 11:15:40 -020077 tfrc_tx_hist_find_entry(struct tfrc_tx_hist_entry *head, u64 seqno)
Gerrit Renker7af5af32006-12-10 00:24:33 -020078{
Arnaldo Carvalho de Melo276f2ed2007-11-28 11:15:40 -020079 while (head != NULL && head->seqno != seqno)
80 head = head->next;
Gerrit Renker7af5af32006-12-10 00:24:33 -020081
Arnaldo Carvalho de Melo276f2ed2007-11-28 11:15:40 -020082 return head;
Gerrit Renker7af5af32006-12-10 00:24:33 -020083}
84
Arnaldo Carvalho de Melo276f2ed2007-11-28 11:15:40 -020085int tfrc_tx_hist_add(struct tfrc_tx_hist_entry **headp, u64 seqno)
Gerrit Renker7af5af32006-12-10 00:24:33 -020086{
Arnaldo Carvalho de Meloe9c8b24a2007-12-06 12:27:49 -020087 struct tfrc_tx_hist_entry *entry = kmem_cache_alloc(tfrc_tx_hist_slab, gfp_any());
Gerrit Renker7af5af32006-12-10 00:24:33 -020088
Arnaldo Carvalho de Melo276f2ed2007-11-28 11:15:40 -020089 if (entry == NULL)
90 return -ENOBUFS;
91 entry->seqno = seqno;
92 entry->stamp = ktime_get_real();
93 entry->next = *headp;
94 *headp = entry;
95 return 0;
Gerrit Renker7af5af32006-12-10 00:24:33 -020096}
97
Arnaldo Carvalho de Melo276f2ed2007-11-28 11:15:40 -020098void tfrc_tx_hist_purge(struct tfrc_tx_hist_entry **headp)
Gerrit Renker7af5af32006-12-10 00:24:33 -020099{
Arnaldo Carvalho de Melo276f2ed2007-11-28 11:15:40 -0200100 struct tfrc_tx_hist_entry *head = *headp;
Gerrit Renker7af5af32006-12-10 00:24:33 -0200101
Arnaldo Carvalho de Melo276f2ed2007-11-28 11:15:40 -0200102 while (head != NULL) {
103 struct tfrc_tx_hist_entry *next = head->next;
Gerrit Renker7af5af32006-12-10 00:24:33 -0200104
Arnaldo Carvalho de Meloe9c8b24a2007-12-06 12:27:49 -0200105 kmem_cache_free(tfrc_tx_hist_slab, head);
Arnaldo Carvalho de Melo276f2ed2007-11-28 11:15:40 -0200106 head = next;
Gerrit Renker7af5af32006-12-10 00:24:33 -0200107 }
Arnaldo Carvalho de Melo276f2ed2007-11-28 11:15:40 -0200108
109 *headp = NULL;
Gerrit Renker7af5af32006-12-10 00:24:33 -0200110}
Gerrit Renker7af5af32006-12-10 00:24:33 -0200111
Arnaldo Carvalho de Melo9108d5f2007-11-29 22:47:15 -0200112u32 tfrc_tx_hist_rtt(struct tfrc_tx_hist_entry *head, const u64 seqno,
113 const ktime_t now)
114{
115 u32 rtt = 0;
116 struct tfrc_tx_hist_entry *packet = tfrc_tx_hist_find_entry(head, seqno);
117
118 if (packet != NULL) {
119 rtt = ktime_us_delta(now, packet->stamp);
120 /*
121 * Garbage-collect older (irrelevant) entries:
122 */
123 tfrc_tx_hist_purge(&packet->next);
124 }
125
126 return rtt;
127}
Arnaldo Carvalho de Melo9108d5f2007-11-29 22:47:15 -0200128
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200129
Gerrit Renker78282d22007-12-08 15:08:08 -0200130/*
131 * Receiver History Routines
132 */
133static struct kmem_cache *tfrc_rx_hist_slab;
134
Gerrit Renkerdf8f83f2007-12-12 12:24:49 -0200135int __init tfrc_rx_packet_history_init(void)
136{
137 tfrc_rx_hist_slab = kmem_cache_create("tfrc_rxh_cache",
138 sizeof(struct tfrc_rx_hist_entry),
139 0, SLAB_HWCACHE_ALIGN, NULL);
140 return tfrc_rx_hist_slab == NULL ? -ENOBUFS : 0;
141}
142
143void tfrc_rx_packet_history_exit(void)
144{
145 if (tfrc_rx_hist_slab != NULL) {
146 kmem_cache_destroy(tfrc_rx_hist_slab);
147 tfrc_rx_hist_slab = NULL;
148 }
149}
150
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200151static inline void tfrc_rx_hist_entry_from_skb(struct tfrc_rx_hist_entry *entry,
152 const struct sk_buff *skb,
Gerrit Renker5b5d0e72008-07-13 11:51:40 +0100153 const u64 ndp)
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300154{
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200155 const struct dccp_hdr *dh = dccp_hdr(skb);
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300156
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200157 entry->tfrchrx_seqno = DCCP_SKB_CB(skb)->dccpd_seq;
158 entry->tfrchrx_ccval = dh->dccph_ccval;
159 entry->tfrchrx_type = dh->dccph_type;
160 entry->tfrchrx_ndp = ndp;
161 entry->tfrchrx_tstamp = ktime_get_real();
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300162}
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200163
164void tfrc_rx_hist_add_packet(struct tfrc_rx_hist *h,
165 const struct sk_buff *skb,
Gerrit Renker5b5d0e72008-07-13 11:51:40 +0100166 const u64 ndp)
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200167{
168 struct tfrc_rx_hist_entry *entry = tfrc_rx_hist_last_rcv(h);
169
170 tfrc_rx_hist_entry_from_skb(entry, skb, ndp);
171}
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300172
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200173/* has the packet contained in skb been seen before? */
174int tfrc_rx_hist_duplicate(struct tfrc_rx_hist *h, struct sk_buff *skb)
Arnaldo Carvalho de Melo072ab6c2005-08-28 01:19:14 -0300175{
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200176 const u64 seq = DCCP_SKB_CB(skb)->dccpd_seq;
177 int i;
Arnaldo Carvalho de Melo072ab6c2005-08-28 01:19:14 -0300178
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200179 if (dccp_delta_seqno(tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno, seq) <= 0)
180 return 1;
Arnaldo Carvalho de Melo072ab6c2005-08-28 01:19:14 -0300181
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200182 for (i = 1; i <= h->loss_count; i++)
183 if (tfrc_rx_hist_entry(h, i)->tfrchrx_seqno == seq)
184 return 1;
Arnaldo Carvalho de Melo072ab6c2005-08-28 01:19:14 -0300185
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200186 return 0;
187}
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200188
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200189static void tfrc_rx_hist_swap(struct tfrc_rx_hist *h, const u8 a, const u8 b)
190{
191 const u8 idx_a = tfrc_rx_hist_index(h, a),
192 idx_b = tfrc_rx_hist_index(h, b);
193 struct tfrc_rx_hist_entry *tmp = h->ring[idx_a];
194
195 h->ring[idx_a] = h->ring[idx_b];
196 h->ring[idx_b] = tmp;
197}
198
199/*
200 * Private helper functions for loss detection.
201 *
202 * In the descriptions, `Si' refers to the sequence number of entry number i,
203 * whose NDP count is `Ni' (lower case is used for variables).
Gerrit Renkerb552c622008-07-13 11:51:40 +0100204 * Note: All __xxx_loss functions expect that a test against duplicates has been
205 * performed already: the seqno of the skb must not be less than the seqno
206 * of loss_prev; and it must not equal that of any valid history entry.
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200207 */
Gerrit Renkerb552c622008-07-13 11:51:40 +0100208static void __do_track_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u64 n1)
209{
210 u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,
211 s1 = DCCP_SKB_CB(skb)->dccpd_seq;
212
213 if (!dccp_loss_free(s0, s1, n1)) { /* gap between S0 and S1 */
214 h->loss_count = 1;
215 tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n1);
216 }
217}
218
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200219static void __one_after_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u32 n2)
220{
221 u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,
222 s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,
223 s2 = DCCP_SKB_CB(skb)->dccpd_seq;
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200224
Gerrit Renker2013c7e2008-07-13 11:51:40 +0100225 if (likely(dccp_delta_seqno(s1, s2) > 0)) { /* S1 < S2 */
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200226 h->loss_count = 2;
227 tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 2), skb, n2);
228 return;
229 }
230
231 /* S0 < S2 < S1 */
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200232
Gerrit Renker2013c7e2008-07-13 11:51:40 +0100233 if (dccp_loss_free(s0, s2, n2)) {
234 u64 n1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_ndp;
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200235
Gerrit Renker2013c7e2008-07-13 11:51:40 +0100236 if (dccp_loss_free(s2, s1, n1)) {
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200237 /* hole is filled: S0, S2, and S1 are consecutive */
238 h->loss_count = 0;
239 h->loss_start = tfrc_rx_hist_index(h, 1);
240 } else
241 /* gap between S2 and S1: just update loss_prev */
242 tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_loss_prev(h), skb, n2);
243
Gerrit Renker2013c7e2008-07-13 11:51:40 +0100244 } else { /* gap between S0 and S2 */
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200245 /*
Gerrit Renker2013c7e2008-07-13 11:51:40 +0100246 * Reorder history to insert S2 between S0 and S1
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200247 */
248 tfrc_rx_hist_swap(h, 0, 3);
249 h->loss_start = tfrc_rx_hist_index(h, 3);
250 tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n2);
251 h->loss_count = 2;
252 }
253}
254
255/* return 1 if a new loss event has been identified */
256static int __two_after_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u32 n3)
257{
258 u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,
259 s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,
260 s2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_seqno,
261 s3 = DCCP_SKB_CB(skb)->dccpd_seq;
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200262
Gerrit Renker2013c7e2008-07-13 11:51:40 +0100263 if (likely(dccp_delta_seqno(s2, s3) > 0)) { /* S2 < S3 */
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200264 h->loss_count = 3;
265 tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 3), skb, n3);
266 return 1;
267 }
268
269 /* S3 < S2 */
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200270
Gerrit Renker2013c7e2008-07-13 11:51:40 +0100271 if (dccp_delta_seqno(s1, s3) > 0) { /* S1 < S3 < S2 */
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200272 /*
Gerrit Renker2013c7e2008-07-13 11:51:40 +0100273 * Reorder history to insert S3 between S1 and S2
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200274 */
275 tfrc_rx_hist_swap(h, 2, 3);
276 tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 2), skb, n3);
277 h->loss_count = 3;
278 return 1;
279 }
280
281 /* S0 < S3 < S1 */
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200282
Gerrit Renker2013c7e2008-07-13 11:51:40 +0100283 if (dccp_loss_free(s0, s3, n3)) {
284 u64 n1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_ndp;
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200285
Gerrit Renker2013c7e2008-07-13 11:51:40 +0100286 if (dccp_loss_free(s3, s1, n1)) {
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200287 /* hole between S0 and S1 filled by S3 */
Gerrit Renker2013c7e2008-07-13 11:51:40 +0100288 u64 n2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_ndp;
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200289
Gerrit Renker2013c7e2008-07-13 11:51:40 +0100290 if (dccp_loss_free(s1, s2, n2)) {
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200291 /* entire hole filled by S0, S3, S1, S2 */
292 h->loss_start = tfrc_rx_hist_index(h, 2);
293 h->loss_count = 0;
294 } else {
295 /* gap remains between S1 and S2 */
296 h->loss_start = tfrc_rx_hist_index(h, 1);
297 h->loss_count = 1;
298 }
299
300 } else /* gap exists between S3 and S1, loss_count stays at 2 */
301 tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_loss_prev(h), skb, n3);
302
303 return 0;
304 }
305
306 /*
Gerrit Renker2013c7e2008-07-13 11:51:40 +0100307 * The remaining case: S0 < S3 < S1 < S2; gap between S0 and S3
308 * Reorder history to insert S3 between S0 and S1.
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200309 */
310 tfrc_rx_hist_swap(h, 0, 3);
311 h->loss_start = tfrc_rx_hist_index(h, 3);
312 tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n3);
313 h->loss_count = 3;
314
315 return 1;
316}
317
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200318/* recycle RX history records to continue loss detection if necessary */
319static void __three_after_loss(struct tfrc_rx_hist *h)
320{
321 /*
Gerrit Renker2013c7e2008-07-13 11:51:40 +0100322 * At this stage we know already that there is a gap between S0 and S1
323 * (since S0 was the highest sequence number received before detecting
324 * the loss). To recycle the loss record, it is thus only necessary to
325 * check for other possible gaps between S1/S2 and between S2/S3.
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200326 */
Gerrit Renker2013c7e2008-07-13 11:51:40 +0100327 u64 s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,
328 s2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_seqno,
329 s3 = tfrc_rx_hist_entry(h, 3)->tfrchrx_seqno;
330 u64 n2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_ndp,
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200331 n3 = tfrc_rx_hist_entry(h, 3)->tfrchrx_ndp;
332
Gerrit Renker2013c7e2008-07-13 11:51:40 +0100333 if (dccp_loss_free(s1, s2, n2)) {
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200334
Gerrit Renker2013c7e2008-07-13 11:51:40 +0100335 if (dccp_loss_free(s2, s3, n3)) {
336 /* no gap between S2 and S3: entire hole is filled */
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200337 h->loss_start = tfrc_rx_hist_index(h, 3);
338 h->loss_count = 0;
339 } else {
340 /* gap between S2 and S3 */
341 h->loss_start = tfrc_rx_hist_index(h, 2);
342 h->loss_count = 1;
343 }
344
Gerrit Renker2013c7e2008-07-13 11:51:40 +0100345 } else { /* gap between S1 and S2 */
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200346 h->loss_start = tfrc_rx_hist_index(h, 1);
347 h->loss_count = 2;
348 }
349}
350
351/**
352 * tfrc_rx_handle_loss - Loss detection and further processing
353 * @h: The non-empty RX history object
354 * @lh: Loss Intervals database to update
355 * @skb: Currently received packet
356 * @ndp: The NDP count belonging to @skb
357 * @calc_first_li: Caller-dependent computation of first loss interval in @lh
358 * @sk: Used by @calc_first_li (see tfrc_lh_interval_add)
359 * Chooses action according to pending loss, updates LI database when a new
360 * loss was detected, and does required post-processing. Returns 1 when caller
361 * should send feedback, 0 otherwise.
Gerrit Renkerb552c622008-07-13 11:51:40 +0100362 * Since it also takes care of reordering during loss detection and updates the
363 * records accordingly, the caller should not perform any more RX history
364 * operations when loss_count is greater than 0 after calling this function.
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200365 */
366int tfrc_rx_handle_loss(struct tfrc_rx_hist *h,
367 struct tfrc_loss_hist *lh,
Gerrit Renker5b5d0e72008-07-13 11:51:40 +0100368 struct sk_buff *skb, const u64 ndp,
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200369 u32 (*calc_first_li)(struct sock *), struct sock *sk)
370{
371 int is_new_loss = 0;
372
Gerrit Renkerb552c622008-07-13 11:51:40 +0100373 if (h->loss_count == 0) {
374 __do_track_loss(h, skb, ndp);
375 } else if (h->loss_count == 1) {
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200376 __one_after_loss(h, skb, ndp);
377 } else if (h->loss_count != 2) {
378 DCCP_BUG("invalid loss_count %d", h->loss_count);
379 } else if (__two_after_loss(h, skb, ndp)) {
380 /*
381 * Update Loss Interval database and recycle RX records
382 */
383 is_new_loss = tfrc_lh_interval_add(lh, h, calc_first_li, sk);
384 __three_after_loss(h);
385 }
386 return is_new_loss;
387}
Gerrit Renker8a9c7e92007-12-12 13:50:51 -0200388
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200389int tfrc_rx_hist_alloc(struct tfrc_rx_hist *h)
390{
391 int i;
392
393 for (i = 0; i <= TFRC_NDUPACK; i++) {
394 h->ring[i] = kmem_cache_alloc(tfrc_rx_hist_slab, GFP_ATOMIC);
395 if (h->ring[i] == NULL)
396 goto out_free;
Arnaldo Carvalho de Melo072ab6c2005-08-28 01:19:14 -0300397 }
Arnaldo Carvalho de Melo072ab6c2005-08-28 01:19:14 -0300398
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200399 h->loss_count = h->loss_start = 0;
400 return 0;
Arnaldo Carvalho de Melo072ab6c2005-08-28 01:19:14 -0300401
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200402out_free:
403 while (i-- != 0) {
404 kmem_cache_free(tfrc_rx_hist_slab, h->ring[i]);
405 h->ring[i] = NULL;
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300406 }
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200407 return -ENOBUFS;
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300408}
409
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200410void tfrc_rx_hist_purge(struct tfrc_rx_hist *h)
411{
412 int i;
413
414 for (i = 0; i <= TFRC_NDUPACK; ++i)
415 if (h->ring[i] != NULL) {
416 kmem_cache_free(tfrc_rx_hist_slab, h->ring[i]);
417 h->ring[i] = NULL;
418 }
419}
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300420
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200421/**
422 * tfrc_rx_hist_rtt_last_s - reference entry to compute RTT samples against
423 */
424static inline struct tfrc_rx_hist_entry *
425 tfrc_rx_hist_rtt_last_s(const struct tfrc_rx_hist *h)
426{
427 return h->ring[0];
428}
429
430/**
431 * tfrc_rx_hist_rtt_prev_s: previously suitable (wrt rtt_last_s) RTT-sampling entry
432 */
433static inline struct tfrc_rx_hist_entry *
434 tfrc_rx_hist_rtt_prev_s(const struct tfrc_rx_hist *h)
435{
436 return h->ring[h->rtt_sample_prev];
437}
438
439/**
440 * tfrc_rx_hist_sample_rtt - Sample RTT from timestamp / CCVal
441 * Based on ideas presented in RFC 4342, 8.1. Returns 0 if it was not able
442 * to compute a sample with given data - calling function should check this.
443 */
444u32 tfrc_rx_hist_sample_rtt(struct tfrc_rx_hist *h, const struct sk_buff *skb)
445{
446 u32 sample = 0,
447 delta_v = SUB16(dccp_hdr(skb)->dccph_ccval,
448 tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
449
450 if (delta_v < 1 || delta_v > 4) { /* unsuitable CCVal delta */
451 if (h->rtt_sample_prev == 2) { /* previous candidate stored */
452 sample = SUB16(tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_ccval,
453 tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
454 if (sample)
455 sample = 4 / sample *
456 ktime_us_delta(tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_tstamp,
457 tfrc_rx_hist_rtt_last_s(h)->tfrchrx_tstamp);
458 else /*
459 * FIXME: This condition is in principle not
460 * possible but occurs when CCID is used for
461 * two-way data traffic. I have tried to trace
462 * it, but the cause does not seem to be here.
463 */
464 DCCP_BUG("please report to dccp@vger.kernel.org"
465 " => prev = %u, last = %u",
466 tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_ccval,
467 tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
468 } else if (delta_v < 1) {
469 h->rtt_sample_prev = 1;
470 goto keep_ref_for_next_time;
471 }
472
473 } else if (delta_v == 4) /* optimal match */
474 sample = ktime_to_us(net_timedelta(tfrc_rx_hist_rtt_last_s(h)->tfrchrx_tstamp));
475 else { /* suboptimal match */
476 h->rtt_sample_prev = 2;
477 goto keep_ref_for_next_time;
478 }
479
480 if (unlikely(sample > DCCP_SANE_RTT_MAX)) {
481 DCCP_WARN("RTT sample %u too large, using max\n", sample);
482 sample = DCCP_SANE_RTT_MAX;
483 }
484
485 h->rtt_sample_prev = 0; /* use current entry as next reference */
486keep_ref_for_next_time:
487
488 return sample;
489}