blob: e6b1f0c913eca900338947a6b3c214c546bcee36 [file] [log] [blame]
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -03001/*
2 * net/dccp/ccids/lib/loss_interval.c
3 *
Ian McDonaldb2f41ff2007-05-28 12:23:29 -03004 * Copyright (c) 2005-7 The University of Waikato, Hamilton, New Zealand.
5 * Copyright (c) 2005-7 Ian McDonald <ian.mcdonald@jandi.co.nz>
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -03006 * 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
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030014#include <linux/module.h>
Ian McDonald66a377c2006-08-26 23:40:50 -070015#include <net/sock.h>
Gerrit Renker59348b12006-11-20 18:39:23 -020016#include "../../dccp.h"
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030017#include "loss_interval.h"
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -030018#include "packet_history.h"
19#include "tfrc.h"
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030020
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -030021struct kmem_cache *dccp_li_cachep __read_mostly;
22
23static inline struct dccp_li_hist_entry *dccp_li_hist_entry_new(const gfp_t prio)
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030024{
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -030025 return kmem_cache_alloc(dccp_li_cachep, prio);
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030026}
27
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -030028static inline void dccp_li_hist_entry_delete(struct dccp_li_hist_entry *entry)
Arnaldo Carvalho de Meloc70b7292007-05-28 18:25:12 -030029{
30 if (entry != NULL)
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -030031 kmem_cache_free(dccp_li_cachep, entry);
Arnaldo Carvalho de Meloc70b7292007-05-28 18:25:12 -030032}
33
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -030034void dccp_li_hist_purge(struct list_head *list)
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030035{
36 struct dccp_li_hist_entry *entry, *next;
37
38 list_for_each_entry_safe(entry, next, list, dccplih_node) {
39 list_del_init(&entry->dccplih_node);
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -030040 kmem_cache_free(dccp_li_cachep, entry);
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030041 }
42}
43
44EXPORT_SYMBOL_GPL(dccp_li_hist_purge);
45
46/* Weights used to calculate loss event rate */
47/*
48 * These are integers as per section 8 of RFC3448. We can then divide by 4 *
49 * when we use it.
50 */
51static const int dccp_li_hist_w[DCCP_LI_HIST_IVAL_F_LENGTH] = {
52 4, 4, 4, 4, 3, 2, 1, 1,
53};
54
55u32 dccp_li_hist_calc_i_mean(struct list_head *list)
56{
57 struct dccp_li_hist_entry *li_entry, *li_next;
58 int i = 0;
59 u32 i_tot;
60 u32 i_tot0 = 0;
61 u32 i_tot1 = 0;
62 u32 w_tot = 0;
63
64 list_for_each_entry_safe(li_entry, li_next, list, dccplih_node) {
Ian McDonald551dc5f2007-03-20 14:46:52 -030065 if (li_entry->dccplih_interval != ~0U) {
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030066 i_tot0 += li_entry->dccplih_interval * dccp_li_hist_w[i];
67 w_tot += dccp_li_hist_w[i];
Ian McDonald66a377c2006-08-26 23:40:50 -070068 if (i != 0)
69 i_tot1 += li_entry->dccplih_interval * dccp_li_hist_w[i - 1];
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030070 }
71
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030072
73 if (++i > DCCP_LI_HIST_IVAL_F_LENGTH)
74 break;
75 }
76
77 if (i != DCCP_LI_HIST_IVAL_F_LENGTH)
78 return 0;
79
80 i_tot = max(i_tot0, i_tot1);
81
Ian McDonald66a377c2006-08-26 23:40:50 -070082 if (!w_tot) {
Gerrit Renker59348b12006-11-20 18:39:23 -020083 DCCP_WARN("w_tot = 0\n");
Ian McDonald66a377c2006-08-26 23:40:50 -070084 return 1;
85 }
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030086
Ian McDonald66a377c2006-08-26 23:40:50 -070087 return i_tot / w_tot;
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030088}
89
90EXPORT_SYMBOL_GPL(dccp_li_hist_calc_i_mean);
91
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -030092static int dccp_li_hist_interval_new(struct list_head *list,
Arnaldo Carvalho de Melo8c281782007-05-28 18:21:53 -030093 const u64 seq_loss, const u8 win_loss)
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030094{
Ian McDonald66a377c2006-08-26 23:40:50 -070095 struct dccp_li_hist_entry *entry;
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030096 int i;
97
Ian McDonald66a377c2006-08-26 23:40:50 -070098 for (i = 0; i < DCCP_LI_HIST_IVAL_F_LENGTH; i++) {
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -030099 entry = dccp_li_hist_entry_new(GFP_ATOMIC);
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -0300100 if (entry == NULL) {
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -0300101 dccp_li_hist_purge(list);
Gerrit Renker59348b12006-11-20 18:39:23 -0200102 DCCP_BUG("loss interval list entry is NULL");
Ian McDonald66a377c2006-08-26 23:40:50 -0700103 return 0;
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -0300104 }
Ian McDonald66a377c2006-08-26 23:40:50 -0700105 entry->dccplih_interval = ~0;
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -0300106 list_add(&entry->dccplih_node, list);
107 }
108
109 entry->dccplih_seqno = seq_loss;
110 entry->dccplih_win_count = win_loss;
Ian McDonald66a377c2006-08-26 23:40:50 -0700111 return 1;
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -0300112}
113
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300114/* calculate first loss interval
115 *
116 * returns estimated loss interval in usecs */
117static u32 dccp_li_calc_first_li(struct sock *sk,
118 struct list_head *hist_list,
119 struct timeval *last_feedback,
120 u16 s, u32 bytes_recv,
121 u32 previous_x_recv)
122{
123 struct dccp_rx_hist_entry *entry, *next, *tail = NULL;
124 u32 x_recv, p;
125 suseconds_t rtt, delta;
126 struct timeval tstamp = { 0, 0 };
127 int interval = 0;
128 int win_count = 0;
129 int step = 0;
130 u64 fval;
131
132 list_for_each_entry_safe(entry, next, hist_list, dccphrx_node) {
133 if (dccp_rx_hist_entry_data_packet(entry)) {
134 tail = entry;
135
136 switch (step) {
137 case 0:
138 tstamp = entry->dccphrx_tstamp;
139 win_count = entry->dccphrx_ccval;
140 step = 1;
141 break;
142 case 1:
143 interval = win_count - entry->dccphrx_ccval;
144 if (interval < 0)
145 interval += TFRC_WIN_COUNT_LIMIT;
146 if (interval > 4)
147 goto found;
148 break;
149 }
150 }
151 }
152
153 if (unlikely(step == 0)) {
154 DCCP_WARN("%s(%p), packet history has no data packets!\n",
155 dccp_role(sk), sk);
156 return ~0;
157 }
158
159 if (unlikely(interval == 0)) {
160 DCCP_WARN("%s(%p), Could not find a win_count interval > 0."
161 "Defaulting to 1\n", dccp_role(sk), sk);
162 interval = 1;
163 }
164found:
165 if (!tail) {
166 DCCP_CRIT("tail is null\n");
167 return ~0;
168 }
169
170 delta = timeval_delta(&tstamp, &tail->dccphrx_tstamp);
171 DCCP_BUG_ON(delta < 0);
172
173 rtt = delta * 4 / interval;
174 dccp_pr_debug("%s(%p), approximated RTT to %dus\n",
175 dccp_role(sk), sk, (int)rtt);
176
177 /*
178 * Determine the length of the first loss interval via inverse lookup.
179 * Assume that X_recv can be computed by the throughput equation
180 * s
181 * X_recv = --------
182 * R * fval
183 * Find some p such that f(p) = fval; return 1/p [RFC 3448, 6.3.1].
184 */
185 if (rtt == 0) { /* would result in divide-by-zero */
186 DCCP_WARN("RTT==0\n");
187 return ~0;
188 }
189
190 dccp_timestamp(sk, &tstamp);
191 delta = timeval_delta(&tstamp, last_feedback);
192 DCCP_BUG_ON(delta <= 0);
193
194 x_recv = scaled_div32(bytes_recv, delta);
195 if (x_recv == 0) { /* would also trigger divide-by-zero */
196 DCCP_WARN("X_recv==0\n");
197 if (previous_x_recv == 0) {
198 DCCP_BUG("stored value of X_recv is zero");
199 return ~0;
200 }
201 x_recv = previous_x_recv;
202 }
203
204 fval = scaled_div(s, rtt);
205 fval = scaled_div32(fval, x_recv);
206 p = tfrc_calc_x_reverse_lookup(fval);
207
208 dccp_pr_debug("%s(%p), receive rate=%u bytes/s, implied "
209 "loss rate=%u\n", dccp_role(sk), sk, x_recv, p);
210
211 if (p == 0)
212 return ~0;
213 else
214 return 1000000 / p;
215}
216
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -0300217void dccp_li_update_li(struct sock *sk,
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300218 struct list_head *li_hist_list,
219 struct list_head *hist_list,
220 struct timeval *last_feedback, u16 s, u32 bytes_recv,
221 u32 previous_x_recv, u64 seq_loss, u8 win_loss)
222{
223 struct dccp_li_hist_entry *head;
224 u64 seq_temp;
225
226 if (list_empty(li_hist_list)) {
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -0300227 if (!dccp_li_hist_interval_new(li_hist_list, seq_loss,
228 win_loss))
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300229 return;
230
231 head = list_entry(li_hist_list->next, struct dccp_li_hist_entry,
232 dccplih_node);
233 head->dccplih_interval = dccp_li_calc_first_li(sk, hist_list,
234 last_feedback,
235 s, bytes_recv,
236 previous_x_recv);
237 } else {
238 struct dccp_li_hist_entry *entry;
239 struct list_head *tail;
240
241 head = list_entry(li_hist_list->next, struct dccp_li_hist_entry,
242 dccplih_node);
243 /* FIXME win count check removed as was wrong */
244 /* should make this check with receive history */
245 /* and compare there as per section 10.2 of RFC4342 */
246
247 /* new loss event detected */
248 /* calculate last interval length */
249 seq_temp = dccp_delta_seqno(head->dccplih_seqno, seq_loss);
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -0300250 entry = dccp_li_hist_entry_new(GFP_ATOMIC);
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300251
252 if (entry == NULL) {
253 DCCP_BUG("out of memory - can not allocate entry");
254 return;
255 }
256
257 list_add(&entry->dccplih_node, li_hist_list);
258
259 tail = li_hist_list->prev;
260 list_del(tail);
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -0300261 kmem_cache_free(dccp_li_cachep, tail);
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300262
263 /* Create the newest interval */
264 entry->dccplih_seqno = seq_loss;
265 entry->dccplih_interval = seq_temp;
266 entry->dccplih_win_count = win_loss;
267 }
268}
269
270EXPORT_SYMBOL_GPL(dccp_li_update_li);
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -0300271
272static __init int dccp_li_init(void)
273{
274 dccp_li_cachep = kmem_cache_create("dccp_li_hist",
275 sizeof(struct dccp_li_hist_entry),
276 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
277 return dccp_li_cachep == NULL ? -ENOBUFS : 0;
278}
279
280static __exit void dccp_li_exit(void)
281{
282 kmem_cache_destroy(dccp_li_cachep);
283}
284
285module_init(dccp_li_init);
286module_exit(dccp_li_exit);