]> err.no Git - linux-2.6/blob - net/dccp/ccids/lib/packet_history.c
[TFRC]: Put RX/TX initialisation into tfrc.c
[linux-2.6] / net / dccp / ccids / lib / packet_history.c
1 /*
2  *  net/dccp/packet_history.c
3  *
4  *  Copyright (c) 2007   The University of Aberdeen, Scotland, UK
5  *  Copyright (c) 2005-7 The University of Waikato, Hamilton, New Zealand.
6  *
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/
11  *  or e-mail Ian McDonald - ian.mcdonald@jandi.co.nz
12  *
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
38 #include <linux/string.h>
39 #include <linux/slab.h>
40 #include "packet_history.h"
41 #include "../../dccp.h"
42
43 /**
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  */
49 struct tfrc_tx_hist_entry {
50         struct tfrc_tx_hist_entry *next;
51         u64                       seqno;
52         ktime_t                   stamp;
53 };
54
55 /*
56  * Transmitter History Routines
57  */
58 static struct kmem_cache *tfrc_tx_hist_slab;
59
60 int __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
68 void 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
76 static struct tfrc_tx_hist_entry *
77         tfrc_tx_hist_find_entry(struct tfrc_tx_hist_entry *head, u64 seqno)
78 {
79         while (head != NULL && head->seqno != seqno)
80                 head = head->next;
81
82         return head;
83 }
84
85 int tfrc_tx_hist_add(struct tfrc_tx_hist_entry **headp, u64 seqno)
86 {
87         struct tfrc_tx_hist_entry *entry = kmem_cache_alloc(tfrc_tx_hist_slab, gfp_any());
88
89         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;
96 }
97 EXPORT_SYMBOL_GPL(tfrc_tx_hist_add);
98
99 void tfrc_tx_hist_purge(struct tfrc_tx_hist_entry **headp)
100 {
101         struct tfrc_tx_hist_entry *head = *headp;
102
103         while (head != NULL) {
104                 struct tfrc_tx_hist_entry *next = head->next;
105
106                 kmem_cache_free(tfrc_tx_hist_slab, head);
107                 head = next;
108         }
109
110         *headp = NULL;
111 }
112 EXPORT_SYMBOL_GPL(tfrc_tx_hist_purge);
113
114 u32 tfrc_tx_hist_rtt(struct tfrc_tx_hist_entry *head, const u64 seqno,
115                      const ktime_t now)
116 {
117         u32 rtt = 0;
118         struct tfrc_tx_hist_entry *packet = tfrc_tx_hist_find_entry(head, seqno);
119
120         if (packet != NULL) {
121                 rtt = ktime_us_delta(now, packet->stamp);
122                 /*
123                  * Garbage-collect older (irrelevant) entries:
124                  */
125                 tfrc_tx_hist_purge(&packet->next);
126         }
127
128         return rtt;
129 }
130 EXPORT_SYMBOL_GPL(tfrc_tx_hist_rtt);
131
132
133 /*
134  *      Receiver History Routines
135  */
136 static struct kmem_cache *tfrc_rx_hist_slab;
137
138 int __init tfrc_rx_packet_history_init(void)
139 {
140         tfrc_rx_hist_slab = kmem_cache_create("tfrc_rxh_cache",
141                                               sizeof(struct tfrc_rx_hist_entry),
142                                               0, SLAB_HWCACHE_ALIGN, NULL);
143         return tfrc_rx_hist_slab == NULL ? -ENOBUFS : 0;
144 }
145
146 void tfrc_rx_packet_history_exit(void)
147 {
148         if (tfrc_rx_hist_slab != NULL) {
149                 kmem_cache_destroy(tfrc_rx_hist_slab);
150                 tfrc_rx_hist_slab = NULL;
151         }
152 }
153
154 /**
155  * tfrc_rx_hist_index - index to reach n-th entry after loss_start
156  */
157 static inline u8 tfrc_rx_hist_index(const struct tfrc_rx_hist *h, const u8 n)
158 {
159         return (h->loss_start + n) & TFRC_NDUPACK;
160 }
161
162 /**
163  * tfrc_rx_hist_last_rcv - entry with highest-received-seqno so far
164  */
165 static inline struct tfrc_rx_hist_entry *
166                         tfrc_rx_hist_last_rcv(const struct tfrc_rx_hist *h)
167 {
168         return h->ring[tfrc_rx_hist_index(h, h->loss_count)];
169 }
170
171 void tfrc_rx_hist_add_packet(struct tfrc_rx_hist *h,
172                              const struct sk_buff *skb,
173                              const u32 ndp)
174 {
175         struct tfrc_rx_hist_entry *entry = tfrc_rx_hist_last_rcv(h);
176         const struct dccp_hdr *dh = dccp_hdr(skb);
177
178         entry->tfrchrx_seqno = DCCP_SKB_CB(skb)->dccpd_seq;
179         entry->tfrchrx_ccval = dh->dccph_ccval;
180         entry->tfrchrx_type  = dh->dccph_type;
181         entry->tfrchrx_ndp   = ndp;
182         entry->tfrchrx_tstamp = ktime_get_real();
183 }
184 EXPORT_SYMBOL_GPL(tfrc_rx_hist_add_packet);
185
186 /**
187  * tfrc_rx_hist_entry - return the n-th history entry after loss_start
188  */
189 static inline struct tfrc_rx_hist_entry *
190                 tfrc_rx_hist_entry(const struct tfrc_rx_hist *h, const u8 n)
191 {
192         return h->ring[tfrc_rx_hist_index(h, n)];
193 }
194
195 /**
196  * tfrc_rx_hist_loss_prev - entry with highest-received-seqno before loss was detected
197  */
198 static inline struct tfrc_rx_hist_entry *
199                         tfrc_rx_hist_loss_prev(const struct tfrc_rx_hist *h)
200 {
201         return h->ring[h->loss_start];
202 }
203
204 /* has the packet contained in skb been seen before? */
205 int tfrc_rx_hist_duplicate(struct tfrc_rx_hist *h, struct sk_buff *skb)
206 {
207         const u64 seq = DCCP_SKB_CB(skb)->dccpd_seq;
208         int i;
209
210         if (dccp_delta_seqno(tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno, seq) <= 0)
211                 return 1;
212
213         for (i = 1; i <= h->loss_count; i++)
214                 if (tfrc_rx_hist_entry(h, i)->tfrchrx_seqno == seq)
215                         return 1;
216
217         return 0;
218 }
219 EXPORT_SYMBOL_GPL(tfrc_rx_hist_duplicate);
220
221 /* initialise loss detection and disable RTT sampling */
222 static inline void tfrc_rx_hist_loss_indicated(struct tfrc_rx_hist *h)
223 {
224         h->loss_count = 1;
225 }
226
227 /* indicate whether previously a packet was detected missing */
228 static inline int tfrc_rx_hist_loss_pending(const struct tfrc_rx_hist *h)
229 {
230         return h->loss_count;
231 }
232
233 /* any data packets missing between last reception and skb ? */
234 int tfrc_rx_hist_new_loss_indicated(struct tfrc_rx_hist *h,
235                                     const struct sk_buff *skb, u32 ndp)
236 {
237         int delta = dccp_delta_seqno(tfrc_rx_hist_last_rcv(h)->tfrchrx_seqno,
238                                      DCCP_SKB_CB(skb)->dccpd_seq);
239
240         if (delta > 1 && ndp < delta)
241                 tfrc_rx_hist_loss_indicated(h);
242
243         return tfrc_rx_hist_loss_pending(h);
244 }
245 EXPORT_SYMBOL_GPL(tfrc_rx_hist_new_loss_indicated);
246
247 int tfrc_rx_hist_alloc(struct tfrc_rx_hist *h)
248 {
249         int i;
250
251         for (i = 0; i <= TFRC_NDUPACK; i++) {
252                 h->ring[i] = kmem_cache_alloc(tfrc_rx_hist_slab, GFP_ATOMIC);
253                 if (h->ring[i] == NULL)
254                         goto out_free;
255         }
256
257         h->loss_count = h->loss_start = 0;
258         return 0;
259
260 out_free:
261         while (i-- != 0) {
262                 kmem_cache_free(tfrc_rx_hist_slab, h->ring[i]);
263                 h->ring[i] = NULL;
264         }
265         return -ENOBUFS;
266 }
267 EXPORT_SYMBOL_GPL(tfrc_rx_hist_alloc);
268
269 void tfrc_rx_hist_purge(struct tfrc_rx_hist *h)
270 {
271         int i;
272
273         for (i = 0; i <= TFRC_NDUPACK; ++i)
274                 if (h->ring[i] != NULL) {
275                         kmem_cache_free(tfrc_rx_hist_slab, h->ring[i]);
276                         h->ring[i] = NULL;
277                 }
278 }
279 EXPORT_SYMBOL_GPL(tfrc_rx_hist_purge);
280
281 /**
282  * tfrc_rx_hist_rtt_last_s - reference entry to compute RTT samples against
283  */
284 static inline struct tfrc_rx_hist_entry *
285                         tfrc_rx_hist_rtt_last_s(const struct tfrc_rx_hist *h)
286 {
287         return h->ring[0];
288 }
289
290 /**
291  * tfrc_rx_hist_rtt_prev_s: previously suitable (wrt rtt_last_s) RTT-sampling entry
292  */
293 static inline struct tfrc_rx_hist_entry *
294                         tfrc_rx_hist_rtt_prev_s(const struct tfrc_rx_hist *h)
295 {
296         return h->ring[h->rtt_sample_prev];
297 }
298
299 /**
300  * tfrc_rx_hist_sample_rtt  -  Sample RTT from timestamp / CCVal
301  * Based on ideas presented in RFC 4342, 8.1. Returns 0 if it was not able
302  * to compute a sample with given data - calling function should check this.
303  */
304 u32 tfrc_rx_hist_sample_rtt(struct tfrc_rx_hist *h, const struct sk_buff *skb)
305 {
306         u32 sample = 0,
307             delta_v = SUB16(dccp_hdr(skb)->dccph_ccval,
308                             tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
309
310         if (delta_v < 1 || delta_v > 4) {       /* unsuitable CCVal delta */
311                 if (h->rtt_sample_prev == 2) {  /* previous candidate stored */
312                         sample = SUB16(tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_ccval,
313                                        tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
314                         if (sample)
315                                 sample = 4 / sample *
316                                          ktime_us_delta(tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_tstamp,
317                                                         tfrc_rx_hist_rtt_last_s(h)->tfrchrx_tstamp);
318                         else    /*
319                                  * FIXME: This condition is in principle not
320                                  * possible but occurs when CCID is used for
321                                  * two-way data traffic. I have tried to trace
322                                  * it, but the cause does not seem to be here.
323                                  */
324                                 DCCP_BUG("please report to dccp@vger.kernel.org"
325                                          " => prev = %u, last = %u",
326                                          tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_ccval,
327                                          tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
328                 } else if (delta_v < 1) {
329                         h->rtt_sample_prev = 1;
330                         goto keep_ref_for_next_time;
331                 }
332
333         } else if (delta_v == 4) /* optimal match */
334                 sample = ktime_to_us(net_timedelta(tfrc_rx_hist_rtt_last_s(h)->tfrchrx_tstamp));
335         else {                   /* suboptimal match */
336                 h->rtt_sample_prev = 2;
337                 goto keep_ref_for_next_time;
338         }
339
340         if (unlikely(sample > DCCP_SANE_RTT_MAX)) {
341                 DCCP_WARN("RTT sample %u too large, using max\n", sample);
342                 sample = DCCP_SANE_RTT_MAX;
343         }
344
345         h->rtt_sample_prev = 0;        /* use current entry as next reference */
346 keep_ref_for_next_time:
347
348         return sample;
349 }
350 EXPORT_SYMBOL_GPL(tfrc_rx_hist_sample_rtt);