Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 1 | /* |
| 2 | * net/dccp/ccids/lib/loss_interval.c |
| 3 | * |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame^] | 4 | * Copyright (c) 2007 The University of Aberdeen, Scotland, UK |
Ian McDonald | b2f41ff | 2007-05-28 12:23:29 -0300 | [diff] [blame] | 5 | * Copyright (c) 2005-7 The University of Waikato, Hamilton, New Zealand. |
| 6 | * Copyright (c) 2005-7 Ian McDonald <ian.mcdonald@jandi.co.nz> |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 7 | * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | */ |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 14 | #include <net/sock.h> |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 15 | #include "tfrc.h" |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 16 | |
Arnaldo Carvalho de Melo | dd36a9a | 2007-05-28 18:56:44 -0300 | [diff] [blame] | 17 | #define DCCP_LI_HIST_IVAL_F_LENGTH 8 |
| 18 | |
| 19 | struct dccp_li_hist_entry { |
| 20 | struct list_head dccplih_node; |
| 21 | u64 dccplih_seqno:48, |
| 22 | dccplih_win_count:4; |
| 23 | u32 dccplih_interval; |
| 24 | }; |
| 25 | |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame^] | 26 | static struct kmem_cache *tfrc_lh_slab __read_mostly; |
| 27 | /* Loss Interval weights from [RFC 3448, 5.4], scaled by 10 */ |
| 28 | static const int tfrc_lh_weights[NINTERVAL] = { 10, 10, 10, 10, 8, 6, 4, 2 }; |
| 29 | |
| 30 | /* implements LIFO semantics on the array */ |
| 31 | static inline u8 LIH_INDEX(const u8 ctr) |
| 32 | { |
| 33 | return (LIH_SIZE - 1 - (ctr % LIH_SIZE)); |
| 34 | } |
| 35 | |
| 36 | /* the `counter' index always points at the next entry to be populated */ |
| 37 | static inline struct tfrc_loss_interval *tfrc_lh_peek(struct tfrc_loss_hist *lh) |
| 38 | { |
| 39 | return lh->counter ? lh->ring[LIH_INDEX(lh->counter - 1)] : NULL; |
| 40 | } |
| 41 | |
| 42 | /* given i with 0 <= i <= k, return I_i as per the rfc3448bis notation */ |
| 43 | static inline u32 tfrc_lh_get_interval(struct tfrc_loss_hist *lh, const u8 i) |
| 44 | { |
| 45 | BUG_ON(i >= lh->counter); |
| 46 | return lh->ring[LIH_INDEX(lh->counter - i - 1)]->li_length; |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * On-demand allocation and de-allocation of entries |
| 51 | */ |
| 52 | static struct tfrc_loss_interval *tfrc_lh_demand_next(struct tfrc_loss_hist *lh) |
| 53 | { |
| 54 | if (lh->ring[LIH_INDEX(lh->counter)] == NULL) |
| 55 | lh->ring[LIH_INDEX(lh->counter)] = kmem_cache_alloc(tfrc_lh_slab, |
| 56 | GFP_ATOMIC); |
| 57 | return lh->ring[LIH_INDEX(lh->counter)]; |
| 58 | } |
| 59 | |
| 60 | void tfrc_lh_cleanup(struct tfrc_loss_hist *lh) |
| 61 | { |
| 62 | if (!tfrc_lh_is_initialised(lh)) |
| 63 | return; |
| 64 | |
| 65 | for (lh->counter = 0; lh->counter < LIH_SIZE; lh->counter++) |
| 66 | if (lh->ring[LIH_INDEX(lh->counter)] != NULL) { |
| 67 | kmem_cache_free(tfrc_lh_slab, |
| 68 | lh->ring[LIH_INDEX(lh->counter)]); |
| 69 | lh->ring[LIH_INDEX(lh->counter)] = NULL; |
| 70 | } |
| 71 | } |
| 72 | EXPORT_SYMBOL_GPL(tfrc_lh_cleanup); |
| 73 | |
Adrian Bunk | 4fda25a | 2007-07-09 13:18:57 -0700 | [diff] [blame] | 74 | static struct kmem_cache *dccp_li_cachep __read_mostly; |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame] | 75 | |
| 76 | static inline struct dccp_li_hist_entry *dccp_li_hist_entry_new(const gfp_t prio) |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 77 | { |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame] | 78 | return kmem_cache_alloc(dccp_li_cachep, prio); |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 79 | } |
| 80 | |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame] | 81 | static inline void dccp_li_hist_entry_delete(struct dccp_li_hist_entry *entry) |
Arnaldo Carvalho de Melo | c70b729 | 2007-05-28 18:25:12 -0300 | [diff] [blame] | 82 | { |
| 83 | if (entry != NULL) |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame] | 84 | kmem_cache_free(dccp_li_cachep, entry); |
Arnaldo Carvalho de Melo | c70b729 | 2007-05-28 18:25:12 -0300 | [diff] [blame] | 85 | } |
| 86 | |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame] | 87 | void dccp_li_hist_purge(struct list_head *list) |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 88 | { |
| 89 | struct dccp_li_hist_entry *entry, *next; |
| 90 | |
| 91 | list_for_each_entry_safe(entry, next, list, dccplih_node) { |
| 92 | list_del_init(&entry->dccplih_node); |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame] | 93 | kmem_cache_free(dccp_li_cachep, entry); |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | EXPORT_SYMBOL_GPL(dccp_li_hist_purge); |
| 98 | |
| 99 | /* Weights used to calculate loss event rate */ |
| 100 | /* |
| 101 | * These are integers as per section 8 of RFC3448. We can then divide by 4 * |
| 102 | * when we use it. |
| 103 | */ |
| 104 | static const int dccp_li_hist_w[DCCP_LI_HIST_IVAL_F_LENGTH] = { |
| 105 | 4, 4, 4, 4, 3, 2, 1, 1, |
| 106 | }; |
| 107 | |
| 108 | u32 dccp_li_hist_calc_i_mean(struct list_head *list) |
| 109 | { |
| 110 | struct dccp_li_hist_entry *li_entry, *li_next; |
| 111 | int i = 0; |
| 112 | u32 i_tot; |
| 113 | u32 i_tot0 = 0; |
| 114 | u32 i_tot1 = 0; |
| 115 | u32 w_tot = 0; |
| 116 | |
| 117 | list_for_each_entry_safe(li_entry, li_next, list, dccplih_node) { |
Ian McDonald | 551dc5f | 2007-03-20 14:46:52 -0300 | [diff] [blame] | 118 | if (li_entry->dccplih_interval != ~0U) { |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 119 | i_tot0 += li_entry->dccplih_interval * dccp_li_hist_w[i]; |
| 120 | w_tot += dccp_li_hist_w[i]; |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 121 | if (i != 0) |
| 122 | i_tot1 += li_entry->dccplih_interval * dccp_li_hist_w[i - 1]; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 123 | } |
| 124 | |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 125 | |
| 126 | if (++i > DCCP_LI_HIST_IVAL_F_LENGTH) |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | if (i != DCCP_LI_HIST_IVAL_F_LENGTH) |
| 131 | return 0; |
| 132 | |
| 133 | i_tot = max(i_tot0, i_tot1); |
| 134 | |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 135 | if (!w_tot) { |
Gerrit Renker | 59348b1 | 2006-11-20 18:39:23 -0200 | [diff] [blame] | 136 | DCCP_WARN("w_tot = 0\n"); |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 137 | return 1; |
| 138 | } |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 139 | |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 140 | return i_tot / w_tot; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | EXPORT_SYMBOL_GPL(dccp_li_hist_calc_i_mean); |
| 144 | |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame^] | 145 | static void tfrc_lh_calc_i_mean(struct tfrc_loss_hist *lh) |
| 146 | { |
| 147 | u32 i_i, i_tot0 = 0, i_tot1 = 0, w_tot = 0; |
| 148 | int i, k = tfrc_lh_length(lh) - 1; /* k is as in rfc3448bis, 5.4 */ |
| 149 | |
| 150 | for (i=0; i <= k; i++) { |
| 151 | i_i = tfrc_lh_get_interval(lh, i); |
| 152 | |
| 153 | if (i < k) { |
| 154 | i_tot0 += i_i * tfrc_lh_weights[i]; |
| 155 | w_tot += tfrc_lh_weights[i]; |
| 156 | } |
| 157 | if (i > 0) |
| 158 | i_tot1 += i_i * tfrc_lh_weights[i-1]; |
| 159 | } |
| 160 | |
| 161 | BUG_ON(w_tot == 0); |
| 162 | lh->i_mean = max(i_tot0, i_tot1) / w_tot; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * tfrc_lh_update_i_mean - Update the `open' loss interval I_0 |
| 167 | * For recomputing p: returns `true' if p > p_prev <=> 1/p < 1/p_prev |
| 168 | */ |
| 169 | u8 tfrc_lh_update_i_mean(struct tfrc_loss_hist *lh, struct sk_buff *skb) |
| 170 | { |
| 171 | struct tfrc_loss_interval *cur = tfrc_lh_peek(lh); |
| 172 | u32 old_i_mean = lh->i_mean; |
| 173 | s64 length; |
| 174 | |
| 175 | if (cur == NULL) /* not initialised */ |
| 176 | return 0; |
| 177 | |
| 178 | length = dccp_delta_seqno(cur->li_seqno, DCCP_SKB_CB(skb)->dccpd_seq); |
| 179 | |
| 180 | if (length - cur->li_length <= 0) /* duplicate or reordered */ |
| 181 | return 0; |
| 182 | |
| 183 | if (SUB16(dccp_hdr(skb)->dccph_ccval, cur->li_ccval) > 4) |
| 184 | /* |
| 185 | * Implements RFC 4342, 10.2: |
| 186 | * If a packet S (skb) exists whose seqno comes `after' the one |
| 187 | * starting the current loss interval (cur) and if the modulo-16 |
| 188 | * distance from C(cur) to C(S) is greater than 4, consider all |
| 189 | * subsequent packets as belonging to a new loss interval. This |
| 190 | * test is necessary since CCVal may wrap between intervals. |
| 191 | */ |
| 192 | cur->li_is_closed = 1; |
| 193 | |
| 194 | if (tfrc_lh_length(lh) == 1) /* due to RFC 3448, 6.3.1 */ |
| 195 | return 0; |
| 196 | |
| 197 | cur->li_length = length; |
| 198 | tfrc_lh_calc_i_mean(lh); |
| 199 | |
| 200 | return (lh->i_mean < old_i_mean); |
| 201 | } |
| 202 | EXPORT_SYMBOL_GPL(tfrc_lh_update_i_mean); |
| 203 | |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame] | 204 | static int dccp_li_hist_interval_new(struct list_head *list, |
Arnaldo Carvalho de Melo | 8c28178 | 2007-05-28 18:21:53 -0300 | [diff] [blame] | 205 | const u64 seq_loss, const u8 win_loss) |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 206 | { |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 207 | struct dccp_li_hist_entry *entry; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 208 | int i; |
| 209 | |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 210 | for (i = 0; i < DCCP_LI_HIST_IVAL_F_LENGTH; i++) { |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame] | 211 | entry = dccp_li_hist_entry_new(GFP_ATOMIC); |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 212 | if (entry == NULL) { |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame] | 213 | dccp_li_hist_purge(list); |
Gerrit Renker | 59348b1 | 2006-11-20 18:39:23 -0200 | [diff] [blame] | 214 | DCCP_BUG("loss interval list entry is NULL"); |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 215 | return 0; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 216 | } |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 217 | entry->dccplih_interval = ~0; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 218 | list_add(&entry->dccplih_node, list); |
| 219 | } |
| 220 | |
| 221 | entry->dccplih_seqno = seq_loss; |
| 222 | entry->dccplih_win_count = win_loss; |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 223 | return 1; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 224 | } |
| 225 | |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 226 | /* calculate first loss interval |
| 227 | * |
| 228 | * returns estimated loss interval in usecs */ |
| 229 | static u32 dccp_li_calc_first_li(struct sock *sk, |
| 230 | struct list_head *hist_list, |
Arnaldo Carvalho de Melo | e7c2335 | 2007-08-19 17:17:51 -0700 | [diff] [blame] | 231 | ktime_t last_feedback, |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 232 | u16 s, u32 bytes_recv, |
| 233 | u32 previous_x_recv) |
| 234 | { |
Arnaldo Carvalho de Melo | b84a218 | 2007-12-06 13:18:11 -0200 | [diff] [blame] | 235 | /* |
| 236 | * FIXME: |
| 237 | * Will be rewritten in the upcoming new loss intervals code. |
| 238 | * Has to be commented ou because it relies on the old rx history |
| 239 | * data structures |
| 240 | */ |
| 241 | #if 0 |
Arnaldo Carvalho de Melo | d58d1af | 2007-12-06 12:28:39 -0200 | [diff] [blame] | 242 | struct tfrc_rx_hist_entry *entry, *next, *tail = NULL; |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 243 | u32 x_recv, p; |
| 244 | suseconds_t rtt, delta; |
Arnaldo Carvalho de Melo | e7c2335 | 2007-08-19 17:17:51 -0700 | [diff] [blame] | 245 | ktime_t tstamp = ktime_set(0, 0); |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 246 | int interval = 0; |
| 247 | int win_count = 0; |
| 248 | int step = 0; |
| 249 | u64 fval; |
| 250 | |
Arnaldo Carvalho de Melo | d58d1af | 2007-12-06 12:28:39 -0200 | [diff] [blame] | 251 | list_for_each_entry_safe(entry, next, hist_list, tfrchrx_node) { |
| 252 | if (tfrc_rx_hist_entry_data_packet(entry)) { |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 253 | tail = entry; |
| 254 | |
| 255 | switch (step) { |
| 256 | case 0: |
Arnaldo Carvalho de Melo | d58d1af | 2007-12-06 12:28:39 -0200 | [diff] [blame] | 257 | tstamp = entry->tfrchrx_tstamp; |
| 258 | win_count = entry->tfrchrx_ccval; |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 259 | step = 1; |
| 260 | break; |
| 261 | case 1: |
Arnaldo Carvalho de Melo | d58d1af | 2007-12-06 12:28:39 -0200 | [diff] [blame] | 262 | interval = win_count - entry->tfrchrx_ccval; |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 263 | if (interval < 0) |
| 264 | interval += TFRC_WIN_COUNT_LIMIT; |
| 265 | if (interval > 4) |
| 266 | goto found; |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | if (unlikely(step == 0)) { |
| 273 | DCCP_WARN("%s(%p), packet history has no data packets!\n", |
| 274 | dccp_role(sk), sk); |
| 275 | return ~0; |
| 276 | } |
| 277 | |
| 278 | if (unlikely(interval == 0)) { |
Joe Perches | 4756daa | 2007-11-19 23:46:02 -0800 | [diff] [blame] | 279 | DCCP_WARN("%s(%p), Could not find a win_count interval > 0. " |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 280 | "Defaulting to 1\n", dccp_role(sk), sk); |
| 281 | interval = 1; |
| 282 | } |
| 283 | found: |
| 284 | if (!tail) { |
| 285 | DCCP_CRIT("tail is null\n"); |
| 286 | return ~0; |
| 287 | } |
| 288 | |
Arnaldo Carvalho de Melo | d58d1af | 2007-12-06 12:28:39 -0200 | [diff] [blame] | 289 | delta = ktime_us_delta(tstamp, tail->tfrchrx_tstamp); |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 290 | DCCP_BUG_ON(delta < 0); |
| 291 | |
| 292 | rtt = delta * 4 / interval; |
| 293 | dccp_pr_debug("%s(%p), approximated RTT to %dus\n", |
| 294 | dccp_role(sk), sk, (int)rtt); |
| 295 | |
| 296 | /* |
| 297 | * Determine the length of the first loss interval via inverse lookup. |
| 298 | * Assume that X_recv can be computed by the throughput equation |
| 299 | * s |
| 300 | * X_recv = -------- |
| 301 | * R * fval |
| 302 | * Find some p such that f(p) = fval; return 1/p [RFC 3448, 6.3.1]. |
| 303 | */ |
| 304 | if (rtt == 0) { /* would result in divide-by-zero */ |
| 305 | DCCP_WARN("RTT==0\n"); |
| 306 | return ~0; |
| 307 | } |
| 308 | |
Arnaldo Carvalho de Melo | e7c2335 | 2007-08-19 17:17:51 -0700 | [diff] [blame] | 309 | delta = ktime_us_delta(ktime_get_real(), last_feedback); |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 310 | DCCP_BUG_ON(delta <= 0); |
| 311 | |
| 312 | x_recv = scaled_div32(bytes_recv, delta); |
| 313 | if (x_recv == 0) { /* would also trigger divide-by-zero */ |
| 314 | DCCP_WARN("X_recv==0\n"); |
| 315 | if (previous_x_recv == 0) { |
| 316 | DCCP_BUG("stored value of X_recv is zero"); |
| 317 | return ~0; |
| 318 | } |
| 319 | x_recv = previous_x_recv; |
| 320 | } |
| 321 | |
| 322 | fval = scaled_div(s, rtt); |
| 323 | fval = scaled_div32(fval, x_recv); |
| 324 | p = tfrc_calc_x_reverse_lookup(fval); |
| 325 | |
| 326 | dccp_pr_debug("%s(%p), receive rate=%u bytes/s, implied " |
| 327 | "loss rate=%u\n", dccp_role(sk), sk, x_recv, p); |
| 328 | |
Arnaldo Carvalho de Melo | b84a218 | 2007-12-06 13:18:11 -0200 | [diff] [blame] | 329 | if (p != 0) |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 330 | return 1000000 / p; |
Arnaldo Carvalho de Melo | b84a218 | 2007-12-06 13:18:11 -0200 | [diff] [blame] | 331 | #endif |
| 332 | return ~0; |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 333 | } |
| 334 | |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame] | 335 | void dccp_li_update_li(struct sock *sk, |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 336 | struct list_head *li_hist_list, |
| 337 | struct list_head *hist_list, |
Arnaldo Carvalho de Melo | e7c2335 | 2007-08-19 17:17:51 -0700 | [diff] [blame] | 338 | ktime_t last_feedback, u16 s, u32 bytes_recv, |
YOSHIFUJI Hideaki | 2324800 | 2007-07-19 10:43:28 +0900 | [diff] [blame] | 339 | u32 previous_x_recv, u64 seq_loss, u8 win_loss) |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 340 | { |
| 341 | struct dccp_li_hist_entry *head; |
| 342 | u64 seq_temp; |
| 343 | |
| 344 | if (list_empty(li_hist_list)) { |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame] | 345 | if (!dccp_li_hist_interval_new(li_hist_list, seq_loss, |
| 346 | win_loss)) |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 347 | return; |
| 348 | |
| 349 | head = list_entry(li_hist_list->next, struct dccp_li_hist_entry, |
| 350 | dccplih_node); |
| 351 | head->dccplih_interval = dccp_li_calc_first_li(sk, hist_list, |
| 352 | last_feedback, |
| 353 | s, bytes_recv, |
| 354 | previous_x_recv); |
| 355 | } else { |
| 356 | struct dccp_li_hist_entry *entry; |
| 357 | struct list_head *tail; |
| 358 | |
| 359 | head = list_entry(li_hist_list->next, struct dccp_li_hist_entry, |
| 360 | dccplih_node); |
| 361 | /* FIXME win count check removed as was wrong */ |
| 362 | /* should make this check with receive history */ |
| 363 | /* and compare there as per section 10.2 of RFC4342 */ |
| 364 | |
| 365 | /* new loss event detected */ |
| 366 | /* calculate last interval length */ |
| 367 | seq_temp = dccp_delta_seqno(head->dccplih_seqno, seq_loss); |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame] | 368 | entry = dccp_li_hist_entry_new(GFP_ATOMIC); |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 369 | |
| 370 | if (entry == NULL) { |
| 371 | DCCP_BUG("out of memory - can not allocate entry"); |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | list_add(&entry->dccplih_node, li_hist_list); |
| 376 | |
| 377 | tail = li_hist_list->prev; |
| 378 | list_del(tail); |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame] | 379 | kmem_cache_free(dccp_li_cachep, tail); |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 380 | |
| 381 | /* Create the newest interval */ |
| 382 | entry->dccplih_seqno = seq_loss; |
| 383 | entry->dccplih_interval = seq_temp; |
| 384 | entry->dccplih_win_count = win_loss; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | EXPORT_SYMBOL_GPL(dccp_li_update_li); |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame] | 389 | |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame^] | 390 | /* Determine if `new_loss' does begin a new loss interval [RFC 4342, 10.2] */ |
| 391 | static inline u8 tfrc_lh_is_new_loss(struct tfrc_loss_interval *cur, |
| 392 | struct tfrc_rx_hist_entry *new_loss) |
| 393 | { |
| 394 | return dccp_delta_seqno(cur->li_seqno, new_loss->tfrchrx_seqno) > 0 && |
| 395 | (cur->li_is_closed || SUB16(new_loss->tfrchrx_ccval, cur->li_ccval) > 4); |
| 396 | } |
| 397 | |
| 398 | /** tfrc_lh_interval_add - Insert new record into the Loss Interval database |
| 399 | * @lh: Loss Interval database |
| 400 | * @rh: Receive history containing a fresh loss event |
| 401 | * @calc_first_li: Caller-dependent routine to compute length of first interval |
| 402 | * @sk: Used by @calc_first_li in caller-specific way (subtyping) |
| 403 | * Updates I_mean and returns 1 if a new interval has in fact been added to @lh. |
| 404 | */ |
| 405 | int tfrc_lh_interval_add(struct tfrc_loss_hist *lh, struct tfrc_rx_hist *rh, |
| 406 | u32 (*calc_first_li)(struct sock *), struct sock *sk) |
| 407 | { |
| 408 | struct tfrc_loss_interval *cur = tfrc_lh_peek(lh), *new; |
| 409 | |
| 410 | if (cur != NULL && !tfrc_lh_is_new_loss(cur, tfrc_rx_hist_loss_prev(rh))) |
| 411 | return 0; |
| 412 | |
| 413 | new = tfrc_lh_demand_next(lh); |
| 414 | if (unlikely(new == NULL)) { |
| 415 | DCCP_CRIT("Cannot allocate/add loss record."); |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | new->li_seqno = tfrc_rx_hist_loss_prev(rh)->tfrchrx_seqno; |
| 420 | new->li_ccval = tfrc_rx_hist_loss_prev(rh)->tfrchrx_ccval; |
| 421 | new->li_is_closed = 0; |
| 422 | |
| 423 | if (++lh->counter == 1) |
| 424 | lh->i_mean = new->li_length = (*calc_first_li)(sk); |
| 425 | else { |
| 426 | cur->li_length = dccp_delta_seqno(cur->li_seqno, new->li_seqno); |
| 427 | new->li_length = dccp_delta_seqno(new->li_seqno, |
| 428 | tfrc_rx_hist_last_rcv(rh)->tfrchrx_seqno); |
| 429 | if (lh->counter > (2*LIH_SIZE)) |
| 430 | lh->counter -= LIH_SIZE; |
| 431 | |
| 432 | tfrc_lh_calc_i_mean(lh); |
| 433 | } |
| 434 | return 1; |
| 435 | } |
| 436 | EXPORT_SYMBOL_GPL(tfrc_lh_interval_add); |
| 437 | |
Arnaldo Carvalho de Melo | 276f2ed | 2007-11-28 11:15:40 -0200 | [diff] [blame] | 438 | int __init dccp_li_init(void) |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame] | 439 | { |
| 440 | dccp_li_cachep = kmem_cache_create("dccp_li_hist", |
| 441 | sizeof(struct dccp_li_hist_entry), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 442 | 0, SLAB_HWCACHE_ALIGN, NULL); |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame] | 443 | return dccp_li_cachep == NULL ? -ENOBUFS : 0; |
| 444 | } |
| 445 | |
Arnaldo Carvalho de Melo | 276f2ed | 2007-11-28 11:15:40 -0200 | [diff] [blame] | 446 | void dccp_li_exit(void) |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame] | 447 | { |
Arnaldo Carvalho de Melo | 276f2ed | 2007-11-28 11:15:40 -0200 | [diff] [blame] | 448 | if (dccp_li_cachep != NULL) { |
| 449 | kmem_cache_destroy(dccp_li_cachep); |
| 450 | dccp_li_cachep = NULL; |
| 451 | } |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame] | 452 | } |