Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux-2.6] / net / dccp / ccids / lib / loss_interval.c
1 /*
2  *  net/dccp/ccids/lib/loss_interval.c
3  *
4  *  Copyright (c) 2005 The University of Waikato, Hamilton, New Zealand.
5  *  Copyright (c) 2005 Ian McDonald <iam4@cs.waikato.ac.nz>
6  *  Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  */
13
14 #include <linux/module.h>
15
16 #include "loss_interval.h"
17
18 struct dccp_li_hist *dccp_li_hist_new(const char *name)
19 {
20         struct dccp_li_hist *hist = kmalloc(sizeof(*hist), GFP_ATOMIC);
21         static const char dccp_li_hist_mask[] = "li_hist_%s";
22         char *slab_name;
23
24         if (hist == NULL)
25                 goto out;
26
27         slab_name = kmalloc(strlen(name) + sizeof(dccp_li_hist_mask) - 1,
28                             GFP_ATOMIC);
29         if (slab_name == NULL)
30                 goto out_free_hist;
31
32         sprintf(slab_name, dccp_li_hist_mask, name);
33         hist->dccplih_slab = kmem_cache_create(slab_name,
34                                              sizeof(struct dccp_li_hist_entry),
35                                                0, SLAB_HWCACHE_ALIGN,
36                                                NULL, NULL);
37         if (hist->dccplih_slab == NULL)
38                 goto out_free_slab_name;
39 out:
40         return hist;
41 out_free_slab_name:
42         kfree(slab_name);
43 out_free_hist:
44         kfree(hist);
45         hist = NULL;
46         goto out;
47 }
48
49 EXPORT_SYMBOL_GPL(dccp_li_hist_new);
50
51 void dccp_li_hist_delete(struct dccp_li_hist *hist)
52 {
53         const char* name = kmem_cache_name(hist->dccplih_slab);
54
55         kmem_cache_destroy(hist->dccplih_slab);
56         kfree(name);
57         kfree(hist);
58 }
59
60 EXPORT_SYMBOL_GPL(dccp_li_hist_delete);
61
62 void dccp_li_hist_purge(struct dccp_li_hist *hist, struct list_head *list)
63 {
64         struct dccp_li_hist_entry *entry, *next;
65
66         list_for_each_entry_safe(entry, next, list, dccplih_node) {
67                 list_del_init(&entry->dccplih_node);
68                 kmem_cache_free(hist->dccplih_slab, entry);
69         }
70 }
71
72 EXPORT_SYMBOL_GPL(dccp_li_hist_purge);
73
74 /* Weights used to calculate loss event rate */
75 /*
76  * These are integers as per section 8 of RFC3448. We can then divide by 4 *
77  * when we use it.
78  */
79 static const int dccp_li_hist_w[DCCP_LI_HIST_IVAL_F_LENGTH] = {
80         4, 4, 4, 4, 3, 2, 1, 1,
81 };
82
83 u32 dccp_li_hist_calc_i_mean(struct list_head *list)
84 {
85         struct dccp_li_hist_entry *li_entry, *li_next;
86         int i = 0;
87         u32 i_tot;
88         u32 i_tot0 = 0;
89         u32 i_tot1 = 0;
90         u32 w_tot  = 0;
91
92         list_for_each_entry_safe(li_entry, li_next, list, dccplih_node) {
93                 if (i < DCCP_LI_HIST_IVAL_F_LENGTH) {
94                         i_tot0 += li_entry->dccplih_interval * dccp_li_hist_w[i];
95                         w_tot  += dccp_li_hist_w[i];
96                 }
97
98                 if (i != 0)
99                         i_tot1 += li_entry->dccplih_interval * dccp_li_hist_w[i - 1];
100
101                 if (++i > DCCP_LI_HIST_IVAL_F_LENGTH)
102                         break;
103         }
104
105         if (i != DCCP_LI_HIST_IVAL_F_LENGTH)
106                 return 0;
107
108         i_tot = max(i_tot0, i_tot1);
109
110         /* FIXME: Why do we do this? -Ian McDonald */
111         if (i_tot * 4 < w_tot)
112                 i_tot = w_tot * 4;
113
114         return i_tot * 4 / w_tot;
115 }
116
117 EXPORT_SYMBOL_GPL(dccp_li_hist_calc_i_mean);
118
119 struct dccp_li_hist_entry *dccp_li_hist_interval_new(struct dccp_li_hist *hist,
120                                                      struct list_head *list,
121                                                      const u64 seq_loss,
122                                                      const u8 win_loss)
123 {
124         struct dccp_li_hist_entry *tail = NULL, *entry;
125         int i;
126
127         for (i = 0; i <= DCCP_LI_HIST_IVAL_F_LENGTH; ++i) {
128                 entry = dccp_li_hist_entry_new(hist, SLAB_ATOMIC);
129                 if (entry == NULL) {
130                         dccp_li_hist_purge(hist, list);
131                         return NULL;
132                 }
133                 if (tail == NULL)
134                         tail = entry;
135                 list_add(&entry->dccplih_node, list);
136         }
137
138         entry->dccplih_seqno     = seq_loss;
139         entry->dccplih_win_count = win_loss;
140         return tail;
141 }
142
143 EXPORT_SYMBOL_GPL(dccp_li_hist_interval_new);