blob: c0a933a1d28eb8cbbbae20059b9bb9a3bdb20e59 [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 Melodd36a9a2007-05-28 18:56:44 -030021#define DCCP_LI_HIST_IVAL_F_LENGTH 8
22
23struct dccp_li_hist_entry {
24 struct list_head dccplih_node;
25 u64 dccplih_seqno:48,
26 dccplih_win_count:4;
27 u32 dccplih_interval;
28};
29
Adrian Bunk4fda25a2007-07-09 13:18:57 -070030static struct kmem_cache *dccp_li_cachep __read_mostly;
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -030031
32static inline struct dccp_li_hist_entry *dccp_li_hist_entry_new(const gfp_t prio)
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030033{
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -030034 return kmem_cache_alloc(dccp_li_cachep, prio);
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030035}
36
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -030037static inline void dccp_li_hist_entry_delete(struct dccp_li_hist_entry *entry)
Arnaldo Carvalho de Meloc70b7292007-05-28 18:25:12 -030038{
39 if (entry != NULL)
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -030040 kmem_cache_free(dccp_li_cachep, entry);
Arnaldo Carvalho de Meloc70b7292007-05-28 18:25:12 -030041}
42
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -030043void dccp_li_hist_purge(struct list_head *list)
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030044{
45 struct dccp_li_hist_entry *entry, *next;
46
47 list_for_each_entry_safe(entry, next, list, dccplih_node) {
48 list_del_init(&entry->dccplih_node);
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -030049 kmem_cache_free(dccp_li_cachep, entry);
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030050 }
51}
52
53EXPORT_SYMBOL_GPL(dccp_li_hist_purge);
54
55/* Weights used to calculate loss event rate */
56/*
57 * These are integers as per section 8 of RFC3448. We can then divide by 4 *
58 * when we use it.
59 */
60static const int dccp_li_hist_w[DCCP_LI_HIST_IVAL_F_LENGTH] = {
61 4, 4, 4, 4, 3, 2, 1, 1,
62};
63
64u32 dccp_li_hist_calc_i_mean(struct list_head *list)
65{
66 struct dccp_li_hist_entry *li_entry, *li_next;
67 int i = 0;
68 u32 i_tot;
69 u32 i_tot0 = 0;
70 u32 i_tot1 = 0;
71 u32 w_tot = 0;
72
73 list_for_each_entry_safe(li_entry, li_next, list, dccplih_node) {
Ian McDonald551dc5f2007-03-20 14:46:52 -030074 if (li_entry->dccplih_interval != ~0U) {
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030075 i_tot0 += li_entry->dccplih_interval * dccp_li_hist_w[i];
76 w_tot += dccp_li_hist_w[i];
Ian McDonald66a377c2006-08-26 23:40:50 -070077 if (i != 0)
78 i_tot1 += li_entry->dccplih_interval * dccp_li_hist_w[i - 1];
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030079 }
80
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030081
82 if (++i > DCCP_LI_HIST_IVAL_F_LENGTH)
83 break;
84 }
85
86 if (i != DCCP_LI_HIST_IVAL_F_LENGTH)
87 return 0;
88
89 i_tot = max(i_tot0, i_tot1);
90
Ian McDonald66a377c2006-08-26 23:40:50 -070091 if (!w_tot) {
Gerrit Renker59348b12006-11-20 18:39:23 -020092 DCCP_WARN("w_tot = 0\n");
Ian McDonald66a377c2006-08-26 23:40:50 -070093 return 1;
94 }
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030095
Ian McDonald66a377c2006-08-26 23:40:50 -070096 return i_tot / w_tot;
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030097}
98
99EXPORT_SYMBOL_GPL(dccp_li_hist_calc_i_mean);
100
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -0300101static int dccp_li_hist_interval_new(struct list_head *list,
Arnaldo Carvalho de Melo8c281782007-05-28 18:21:53 -0300102 const u64 seq_loss, const u8 win_loss)
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -0300103{
Ian McDonald66a377c2006-08-26 23:40:50 -0700104 struct dccp_li_hist_entry *entry;
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -0300105 int i;
106
Ian McDonald66a377c2006-08-26 23:40:50 -0700107 for (i = 0; i < DCCP_LI_HIST_IVAL_F_LENGTH; i++) {
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -0300108 entry = dccp_li_hist_entry_new(GFP_ATOMIC);
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -0300109 if (entry == NULL) {
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -0300110 dccp_li_hist_purge(list);
Gerrit Renker59348b12006-11-20 18:39:23 -0200111 DCCP_BUG("loss interval list entry is NULL");
Ian McDonald66a377c2006-08-26 23:40:50 -0700112 return 0;
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -0300113 }
Ian McDonald66a377c2006-08-26 23:40:50 -0700114 entry->dccplih_interval = ~0;
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -0300115 list_add(&entry->dccplih_node, list);
116 }
117
118 entry->dccplih_seqno = seq_loss;
119 entry->dccplih_win_count = win_loss;
Ian McDonald66a377c2006-08-26 23:40:50 -0700120 return 1;
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -0300121}
122
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300123/* calculate first loss interval
124 *
125 * returns estimated loss interval in usecs */
126static u32 dccp_li_calc_first_li(struct sock *sk,
127 struct list_head *hist_list,
Arnaldo Carvalho de Meloe7c23352007-08-19 17:17:51 -0700128 ktime_t last_feedback,
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300129 u16 s, u32 bytes_recv,
130 u32 previous_x_recv)
131{
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200132/*
133 * FIXME:
134 * Will be rewritten in the upcoming new loss intervals code.
135 * Has to be commented ou because it relies on the old rx history
136 * data structures
137 */
138#if 0
Arnaldo Carvalho de Melod58d1af2007-12-06 12:28:39 -0200139 struct tfrc_rx_hist_entry *entry, *next, *tail = NULL;
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300140 u32 x_recv, p;
141 suseconds_t rtt, delta;
Arnaldo Carvalho de Meloe7c23352007-08-19 17:17:51 -0700142 ktime_t tstamp = ktime_set(0, 0);
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300143 int interval = 0;
144 int win_count = 0;
145 int step = 0;
146 u64 fval;
147
Arnaldo Carvalho de Melod58d1af2007-12-06 12:28:39 -0200148 list_for_each_entry_safe(entry, next, hist_list, tfrchrx_node) {
149 if (tfrc_rx_hist_entry_data_packet(entry)) {
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300150 tail = entry;
151
152 switch (step) {
153 case 0:
Arnaldo Carvalho de Melod58d1af2007-12-06 12:28:39 -0200154 tstamp = entry->tfrchrx_tstamp;
155 win_count = entry->tfrchrx_ccval;
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300156 step = 1;
157 break;
158 case 1:
Arnaldo Carvalho de Melod58d1af2007-12-06 12:28:39 -0200159 interval = win_count - entry->tfrchrx_ccval;
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300160 if (interval < 0)
161 interval += TFRC_WIN_COUNT_LIMIT;
162 if (interval > 4)
163 goto found;
164 break;
165 }
166 }
167 }
168
169 if (unlikely(step == 0)) {
170 DCCP_WARN("%s(%p), packet history has no data packets!\n",
171 dccp_role(sk), sk);
172 return ~0;
173 }
174
175 if (unlikely(interval == 0)) {
Joe Perches4756daa2007-11-19 23:46:02 -0800176 DCCP_WARN("%s(%p), Could not find a win_count interval > 0. "
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300177 "Defaulting to 1\n", dccp_role(sk), sk);
178 interval = 1;
179 }
180found:
181 if (!tail) {
182 DCCP_CRIT("tail is null\n");
183 return ~0;
184 }
185
Arnaldo Carvalho de Melod58d1af2007-12-06 12:28:39 -0200186 delta = ktime_us_delta(tstamp, tail->tfrchrx_tstamp);
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300187 DCCP_BUG_ON(delta < 0);
188
189 rtt = delta * 4 / interval;
190 dccp_pr_debug("%s(%p), approximated RTT to %dus\n",
191 dccp_role(sk), sk, (int)rtt);
192
193 /*
194 * Determine the length of the first loss interval via inverse lookup.
195 * Assume that X_recv can be computed by the throughput equation
196 * s
197 * X_recv = --------
198 * R * fval
199 * Find some p such that f(p) = fval; return 1/p [RFC 3448, 6.3.1].
200 */
201 if (rtt == 0) { /* would result in divide-by-zero */
202 DCCP_WARN("RTT==0\n");
203 return ~0;
204 }
205
Arnaldo Carvalho de Meloe7c23352007-08-19 17:17:51 -0700206 delta = ktime_us_delta(ktime_get_real(), last_feedback);
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300207 DCCP_BUG_ON(delta <= 0);
208
209 x_recv = scaled_div32(bytes_recv, delta);
210 if (x_recv == 0) { /* would also trigger divide-by-zero */
211 DCCP_WARN("X_recv==0\n");
212 if (previous_x_recv == 0) {
213 DCCP_BUG("stored value of X_recv is zero");
214 return ~0;
215 }
216 x_recv = previous_x_recv;
217 }
218
219 fval = scaled_div(s, rtt);
220 fval = scaled_div32(fval, x_recv);
221 p = tfrc_calc_x_reverse_lookup(fval);
222
223 dccp_pr_debug("%s(%p), receive rate=%u bytes/s, implied "
224 "loss rate=%u\n", dccp_role(sk), sk, x_recv, p);
225
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200226 if (p != 0)
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300227 return 1000000 / p;
Arnaldo Carvalho de Melob84a2182007-12-06 13:18:11 -0200228#endif
229 return ~0;
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300230}
231
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -0300232void dccp_li_update_li(struct sock *sk,
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300233 struct list_head *li_hist_list,
234 struct list_head *hist_list,
Arnaldo Carvalho de Meloe7c23352007-08-19 17:17:51 -0700235 ktime_t last_feedback, u16 s, u32 bytes_recv,
YOSHIFUJI Hideaki23248002007-07-19 10:43:28 +0900236 u32 previous_x_recv, u64 seq_loss, u8 win_loss)
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300237{
238 struct dccp_li_hist_entry *head;
239 u64 seq_temp;
240
241 if (list_empty(li_hist_list)) {
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -0300242 if (!dccp_li_hist_interval_new(li_hist_list, seq_loss,
243 win_loss))
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300244 return;
245
246 head = list_entry(li_hist_list->next, struct dccp_li_hist_entry,
247 dccplih_node);
248 head->dccplih_interval = dccp_li_calc_first_li(sk, hist_list,
249 last_feedback,
250 s, bytes_recv,
251 previous_x_recv);
252 } else {
253 struct dccp_li_hist_entry *entry;
254 struct list_head *tail;
255
256 head = list_entry(li_hist_list->next, struct dccp_li_hist_entry,
257 dccplih_node);
258 /* FIXME win count check removed as was wrong */
259 /* should make this check with receive history */
260 /* and compare there as per section 10.2 of RFC4342 */
261
262 /* new loss event detected */
263 /* calculate last interval length */
264 seq_temp = dccp_delta_seqno(head->dccplih_seqno, seq_loss);
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -0300265 entry = dccp_li_hist_entry_new(GFP_ATOMIC);
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300266
267 if (entry == NULL) {
268 DCCP_BUG("out of memory - can not allocate entry");
269 return;
270 }
271
272 list_add(&entry->dccplih_node, li_hist_list);
273
274 tail = li_hist_list->prev;
275 list_del(tail);
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -0300276 kmem_cache_free(dccp_li_cachep, tail);
Arnaldo Carvalho de Melocc0a9102007-06-14 17:41:28 -0300277
278 /* Create the newest interval */
279 entry->dccplih_seqno = seq_loss;
280 entry->dccplih_interval = seq_temp;
281 entry->dccplih_win_count = win_loss;
282 }
283}
284
285EXPORT_SYMBOL_GPL(dccp_li_update_li);
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -0300286
Arnaldo Carvalho de Melo276f2ed2007-11-28 11:15:40 -0200287int __init dccp_li_init(void)
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -0300288{
289 dccp_li_cachep = kmem_cache_create("dccp_li_hist",
290 sizeof(struct dccp_li_hist_entry),
Paul Mundt20c2df82007-07-20 10:11:58 +0900291 0, SLAB_HWCACHE_ALIGN, NULL);
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -0300292 return dccp_li_cachep == NULL ? -ENOBUFS : 0;
293}
294
Arnaldo Carvalho de Melo276f2ed2007-11-28 11:15:40 -0200295void dccp_li_exit(void)
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -0300296{
Arnaldo Carvalho de Melo276f2ed2007-11-28 11:15:40 -0200297 if (dccp_li_cachep != NULL) {
298 kmem_cache_destroy(dccp_li_cachep);
299 dccp_li_cachep = NULL;
300 }
Arnaldo Carvalho de Melocc4d6a32007-05-28 18:53:08 -0300301}