1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#include <linux/string.h>
37#include <linux/slab.h>
38#include "packet_history.h"
39#include "../../dccp.h"
40
41
42
43
44static struct kmem_cache *tfrc_tx_hist_slab;
45
46int __init tfrc_tx_packet_history_init(void)
47{
48 tfrc_tx_hist_slab = kmem_cache_create("tfrc_tx_hist",
49 sizeof(struct tfrc_tx_hist_entry),
50 0, SLAB_HWCACHE_ALIGN, NULL);
51 return tfrc_tx_hist_slab == NULL ? -ENOBUFS : 0;
52}
53
54void tfrc_tx_packet_history_exit(void)
55{
56 if (tfrc_tx_hist_slab != NULL) {
57 kmem_cache_destroy(tfrc_tx_hist_slab);
58 tfrc_tx_hist_slab = NULL;
59 }
60}
61
62int tfrc_tx_hist_add(struct tfrc_tx_hist_entry **headp, u64 seqno)
63{
64 struct tfrc_tx_hist_entry *entry = kmem_cache_alloc(tfrc_tx_hist_slab, gfp_any());
65
66 if (entry == NULL)
67 return -ENOBUFS;
68 entry->seqno = seqno;
69 entry->stamp = ktime_get_real();
70 entry->next = *headp;
71 *headp = entry;
72 return 0;
73}
74
75void tfrc_tx_hist_purge(struct tfrc_tx_hist_entry **headp)
76{
77 struct tfrc_tx_hist_entry *head = *headp;
78
79 while (head != NULL) {
80 struct tfrc_tx_hist_entry *next = head->next;
81
82 kmem_cache_free(tfrc_tx_hist_slab, head);
83 head = next;
84 }
85
86 *headp = NULL;
87}
88
89
90
91
92static struct kmem_cache *tfrc_rx_hist_slab;
93
94int __init tfrc_rx_packet_history_init(void)
95{
96 tfrc_rx_hist_slab = kmem_cache_create("tfrc_rxh_cache",
97 sizeof(struct tfrc_rx_hist_entry),
98 0, SLAB_HWCACHE_ALIGN, NULL);
99 return tfrc_rx_hist_slab == NULL ? -ENOBUFS : 0;
100}
101
102void tfrc_rx_packet_history_exit(void)
103{
104 if (tfrc_rx_hist_slab != NULL) {
105 kmem_cache_destroy(tfrc_rx_hist_slab);
106 tfrc_rx_hist_slab = NULL;
107 }
108}
109
110static inline void tfrc_rx_hist_entry_from_skb(struct tfrc_rx_hist_entry *entry,
111 const struct sk_buff *skb,
112 const u64 ndp)
113{
114 const struct dccp_hdr *dh = dccp_hdr(skb);
115
116 entry->tfrchrx_seqno = DCCP_SKB_CB(skb)->dccpd_seq;
117 entry->tfrchrx_ccval = dh->dccph_ccval;
118 entry->tfrchrx_type = dh->dccph_type;
119 entry->tfrchrx_ndp = ndp;
120 entry->tfrchrx_tstamp = ktime_get_real();
121}
122
123void tfrc_rx_hist_add_packet(struct tfrc_rx_hist *h,
124 const struct sk_buff *skb,
125 const u64 ndp)
126{
127 struct tfrc_rx_hist_entry *entry = tfrc_rx_hist_last_rcv(h);
128
129 tfrc_rx_hist_entry_from_skb(entry, skb, ndp);
130}
131
132
133int tfrc_rx_hist_duplicate(struct tfrc_rx_hist *h, struct sk_buff *skb)
134{
135 const u64 seq = DCCP_SKB_CB(skb)->dccpd_seq;
136 int i;
137
138 if (dccp_delta_seqno(tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno, seq) <= 0)
139 return 1;
140
141 for (i = 1; i <= h->loss_count; i++)
142 if (tfrc_rx_hist_entry(h, i)->tfrchrx_seqno == seq)
143 return 1;
144
145 return 0;
146}
147
148static void tfrc_rx_hist_swap(struct tfrc_rx_hist *h, const u8 a, const u8 b)
149{
150 const u8 idx_a = tfrc_rx_hist_index(h, a),
151 idx_b = tfrc_rx_hist_index(h, b);
152 struct tfrc_rx_hist_entry *tmp = h->ring[idx_a];
153
154 h->ring[idx_a] = h->ring[idx_b];
155 h->ring[idx_b] = tmp;
156}
157
158
159
160
161
162
163
164
165
166
167static void __do_track_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u64 n1)
168{
169 u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,
170 s1 = DCCP_SKB_CB(skb)->dccpd_seq;
171
172 if (!dccp_loss_free(s0, s1, n1)) {
173 h->loss_count = 1;
174 tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n1);
175 }
176}
177
178static void __one_after_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u32 n2)
179{
180 u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,
181 s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,
182 s2 = DCCP_SKB_CB(skb)->dccpd_seq;
183
184 if (likely(dccp_delta_seqno(s1, s2) > 0)) {
185 h->loss_count = 2;
186 tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 2), skb, n2);
187 return;
188 }
189
190
191
192 if (dccp_loss_free(s0, s2, n2)) {
193 u64 n1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_ndp;
194
195 if (dccp_loss_free(s2, s1, n1)) {
196
197 h->loss_count = 0;
198 h->loss_start = tfrc_rx_hist_index(h, 1);
199 } else
200
201 tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_loss_prev(h), skb, n2);
202
203 } else {
204
205
206
207 tfrc_rx_hist_swap(h, 0, 3);
208 h->loss_start = tfrc_rx_hist_index(h, 3);
209 tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n2);
210 h->loss_count = 2;
211 }
212}
213
214
215static int __two_after_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u32 n3)
216{
217 u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,
218 s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,
219 s2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_seqno,
220 s3 = DCCP_SKB_CB(skb)->dccpd_seq;
221
222 if (likely(dccp_delta_seqno(s2, s3) > 0)) {
223 h->loss_count = 3;
224 tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 3), skb, n3);
225 return 1;
226 }
227
228
229
230 if (dccp_delta_seqno(s1, s3) > 0) {
231
232
233
234 tfrc_rx_hist_swap(h, 2, 3);
235 tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 2), skb, n3);
236 h->loss_count = 3;
237 return 1;
238 }
239
240
241
242 if (dccp_loss_free(s0, s3, n3)) {
243 u64 n1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_ndp;
244
245 if (dccp_loss_free(s3, s1, n1)) {
246
247 u64 n2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_ndp;
248
249 if (dccp_loss_free(s1, s2, n2)) {
250
251 h->loss_start = tfrc_rx_hist_index(h, 2);
252 h->loss_count = 0;
253 } else {
254
255 h->loss_start = tfrc_rx_hist_index(h, 1);
256 h->loss_count = 1;
257 }
258
259 } else
260 tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_loss_prev(h), skb, n3);
261
262 return 0;
263 }
264
265
266
267
268
269 tfrc_rx_hist_swap(h, 0, 3);
270 h->loss_start = tfrc_rx_hist_index(h, 3);
271 tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n3);
272 h->loss_count = 3;
273
274 return 1;
275}
276
277
278static void __three_after_loss(struct tfrc_rx_hist *h)
279{
280
281
282
283
284
285
286 u64 s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,
287 s2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_seqno,
288 s3 = tfrc_rx_hist_entry(h, 3)->tfrchrx_seqno;
289 u64 n2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_ndp,
290 n3 = tfrc_rx_hist_entry(h, 3)->tfrchrx_ndp;
291
292 if (dccp_loss_free(s1, s2, n2)) {
293
294 if (dccp_loss_free(s2, s3, n3)) {
295
296 h->loss_start = tfrc_rx_hist_index(h, 3);
297 h->loss_count = 0;
298 } else {
299
300 h->loss_start = tfrc_rx_hist_index(h, 2);
301 h->loss_count = 1;
302 }
303
304 } else {
305 h->loss_start = tfrc_rx_hist_index(h, 1);
306 h->loss_count = 2;
307 }
308}
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326int tfrc_rx_handle_loss(struct tfrc_rx_hist *h,
327 struct tfrc_loss_hist *lh,
328 struct sk_buff *skb, const u64 ndp,
329 u32 (*calc_first_li)(struct sock *), struct sock *sk)
330{
331 int is_new_loss = 0;
332
333 if (h->loss_count == 0) {
334 __do_track_loss(h, skb, ndp);
335 } else if (h->loss_count == 1) {
336 __one_after_loss(h, skb, ndp);
337 } else if (h->loss_count != 2) {
338 DCCP_BUG("invalid loss_count %d", h->loss_count);
339 } else if (__two_after_loss(h, skb, ndp)) {
340
341
342
343 is_new_loss = tfrc_lh_interval_add(lh, h, calc_first_li, sk);
344 __three_after_loss(h);
345 }
346 return is_new_loss;
347}
348
349int tfrc_rx_hist_alloc(struct tfrc_rx_hist *h)
350{
351 int i;
352
353 for (i = 0; i <= TFRC_NDUPACK; i++) {
354 h->ring[i] = kmem_cache_alloc(tfrc_rx_hist_slab, GFP_ATOMIC);
355 if (h->ring[i] == NULL)
356 goto out_free;
357 }
358
359 h->loss_count = h->loss_start = 0;
360 return 0;
361
362out_free:
363 while (i-- != 0) {
364 kmem_cache_free(tfrc_rx_hist_slab, h->ring[i]);
365 h->ring[i] = NULL;
366 }
367 return -ENOBUFS;
368}
369
370void tfrc_rx_hist_purge(struct tfrc_rx_hist *h)
371{
372 int i;
373
374 for (i = 0; i <= TFRC_NDUPACK; ++i)
375 if (h->ring[i] != NULL) {
376 kmem_cache_free(tfrc_rx_hist_slab, h->ring[i]);
377 h->ring[i] = NULL;
378 }
379}
380
381
382
383
384static inline struct tfrc_rx_hist_entry *
385 tfrc_rx_hist_rtt_last_s(const struct tfrc_rx_hist *h)
386{
387 return h->ring[0];
388}
389
390
391
392
393static inline struct tfrc_rx_hist_entry *
394 tfrc_rx_hist_rtt_prev_s(const struct tfrc_rx_hist *h)
395{
396 return h->ring[h->rtt_sample_prev];
397}
398
399
400
401
402
403
404u32 tfrc_rx_hist_sample_rtt(struct tfrc_rx_hist *h, const struct sk_buff *skb)
405{
406 u32 sample = 0,
407 delta_v = SUB16(dccp_hdr(skb)->dccph_ccval,
408 tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
409
410 if (delta_v < 1 || delta_v > 4) {
411 if (h->rtt_sample_prev == 2) {
412 sample = SUB16(tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_ccval,
413 tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
414 if (sample)
415 sample = 4 / sample *
416 ktime_us_delta(tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_tstamp,
417 tfrc_rx_hist_rtt_last_s(h)->tfrchrx_tstamp);
418 else
419
420
421
422
423
424 DCCP_BUG("please report to dccp@vger.kernel.org"
425 " => prev = %u, last = %u",
426 tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_ccval,
427 tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
428 } else if (delta_v < 1) {
429 h->rtt_sample_prev = 1;
430 goto keep_ref_for_next_time;
431 }
432
433 } else if (delta_v == 4)
434 sample = ktime_to_us(net_timedelta(tfrc_rx_hist_rtt_last_s(h)->tfrchrx_tstamp));
435 else {
436 h->rtt_sample_prev = 2;
437 goto keep_ref_for_next_time;
438 }
439
440 if (unlikely(sample > DCCP_SANE_RTT_MAX)) {
441 DCCP_WARN("RTT sample %u too large, using max\n", sample);
442 sample = DCCP_SANE_RTT_MAX;
443 }
444
445 h->rtt_sample_prev = 0;
446keep_ref_for_next_time:
447
448 return sample;
449}
450