Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 1 | /* |
| 2 | * net/dccp/ccids/lib/loss_interval.c |
| 3 | * |
Ian McDonald | b2f41ff | 2007-05-28 12:23:29 -0300 | [diff] [blame] | 4 | * 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 Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 14 | #include <linux/module.h> |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 15 | #include <net/sock.h> |
Gerrit Renker | 59348b1 | 2006-11-20 18:39:23 -0200 | [diff] [blame] | 16 | #include "../../dccp.h" |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 17 | #include "loss_interval.h" |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame^] | 18 | #include "packet_history.h" |
| 19 | #include "tfrc.h" |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 20 | |
| 21 | struct dccp_li_hist *dccp_li_hist_new(const char *name) |
| 22 | { |
| 23 | struct dccp_li_hist *hist = kmalloc(sizeof(*hist), GFP_ATOMIC); |
| 24 | static const char dccp_li_hist_mask[] = "li_hist_%s"; |
| 25 | char *slab_name; |
| 26 | |
| 27 | if (hist == NULL) |
| 28 | goto out; |
| 29 | |
| 30 | slab_name = kmalloc(strlen(name) + sizeof(dccp_li_hist_mask) - 1, |
| 31 | GFP_ATOMIC); |
| 32 | if (slab_name == NULL) |
| 33 | goto out_free_hist; |
| 34 | |
| 35 | sprintf(slab_name, dccp_li_hist_mask, name); |
| 36 | hist->dccplih_slab = kmem_cache_create(slab_name, |
| 37 | sizeof(struct dccp_li_hist_entry), |
| 38 | 0, SLAB_HWCACHE_ALIGN, |
| 39 | NULL, NULL); |
| 40 | if (hist->dccplih_slab == NULL) |
| 41 | goto out_free_slab_name; |
| 42 | out: |
| 43 | return hist; |
| 44 | out_free_slab_name: |
| 45 | kfree(slab_name); |
| 46 | out_free_hist: |
| 47 | kfree(hist); |
| 48 | hist = NULL; |
| 49 | goto out; |
| 50 | } |
| 51 | |
| 52 | EXPORT_SYMBOL_GPL(dccp_li_hist_new); |
| 53 | |
| 54 | void dccp_li_hist_delete(struct dccp_li_hist *hist) |
| 55 | { |
| 56 | const char* name = kmem_cache_name(hist->dccplih_slab); |
| 57 | |
| 58 | kmem_cache_destroy(hist->dccplih_slab); |
| 59 | kfree(name); |
| 60 | kfree(hist); |
| 61 | } |
| 62 | |
| 63 | EXPORT_SYMBOL_GPL(dccp_li_hist_delete); |
| 64 | |
| 65 | void dccp_li_hist_purge(struct dccp_li_hist *hist, struct list_head *list) |
| 66 | { |
| 67 | struct dccp_li_hist_entry *entry, *next; |
| 68 | |
| 69 | list_for_each_entry_safe(entry, next, list, dccplih_node) { |
| 70 | list_del_init(&entry->dccplih_node); |
| 71 | kmem_cache_free(hist->dccplih_slab, entry); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | EXPORT_SYMBOL_GPL(dccp_li_hist_purge); |
| 76 | |
| 77 | /* Weights used to calculate loss event rate */ |
| 78 | /* |
| 79 | * These are integers as per section 8 of RFC3448. We can then divide by 4 * |
| 80 | * when we use it. |
| 81 | */ |
| 82 | static const int dccp_li_hist_w[DCCP_LI_HIST_IVAL_F_LENGTH] = { |
| 83 | 4, 4, 4, 4, 3, 2, 1, 1, |
| 84 | }; |
| 85 | |
| 86 | u32 dccp_li_hist_calc_i_mean(struct list_head *list) |
| 87 | { |
| 88 | struct dccp_li_hist_entry *li_entry, *li_next; |
| 89 | int i = 0; |
| 90 | u32 i_tot; |
| 91 | u32 i_tot0 = 0; |
| 92 | u32 i_tot1 = 0; |
| 93 | u32 w_tot = 0; |
| 94 | |
| 95 | list_for_each_entry_safe(li_entry, li_next, list, dccplih_node) { |
Ian McDonald | 551dc5f | 2007-03-20 14:46:52 -0300 | [diff] [blame] | 96 | if (li_entry->dccplih_interval != ~0U) { |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 97 | i_tot0 += li_entry->dccplih_interval * dccp_li_hist_w[i]; |
| 98 | w_tot += dccp_li_hist_w[i]; |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 99 | if (i != 0) |
| 100 | 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] | 101 | } |
| 102 | |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 103 | |
| 104 | if (++i > DCCP_LI_HIST_IVAL_F_LENGTH) |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | if (i != DCCP_LI_HIST_IVAL_F_LENGTH) |
| 109 | return 0; |
| 110 | |
| 111 | i_tot = max(i_tot0, i_tot1); |
| 112 | |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 113 | if (!w_tot) { |
Gerrit Renker | 59348b1 | 2006-11-20 18:39:23 -0200 | [diff] [blame] | 114 | DCCP_WARN("w_tot = 0\n"); |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 115 | return 1; |
| 116 | } |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 117 | |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 118 | return i_tot / w_tot; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | EXPORT_SYMBOL_GPL(dccp_li_hist_calc_i_mean); |
| 122 | |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 123 | int dccp_li_hist_interval_new(struct dccp_li_hist *hist, |
| 124 | struct list_head *list, const u64 seq_loss, const u8 win_loss) |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 125 | { |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 126 | struct dccp_li_hist_entry *entry; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 127 | int i; |
| 128 | |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 129 | for (i = 0; i < DCCP_LI_HIST_IVAL_F_LENGTH; i++) { |
Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame] | 130 | entry = dccp_li_hist_entry_new(hist, GFP_ATOMIC); |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 131 | if (entry == NULL) { |
| 132 | dccp_li_hist_purge(hist, list); |
Gerrit Renker | 59348b1 | 2006-11-20 18:39:23 -0200 | [diff] [blame] | 133 | DCCP_BUG("loss interval list entry is NULL"); |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 134 | return 0; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 135 | } |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 136 | entry->dccplih_interval = ~0; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 137 | list_add(&entry->dccplih_node, list); |
| 138 | } |
| 139 | |
| 140 | entry->dccplih_seqno = seq_loss; |
| 141 | entry->dccplih_win_count = win_loss; |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 142 | return 1; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | EXPORT_SYMBOL_GPL(dccp_li_hist_interval_new); |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame^] | 146 | |
| 147 | /* calculate first loss interval |
| 148 | * |
| 149 | * returns estimated loss interval in usecs */ |
| 150 | static u32 dccp_li_calc_first_li(struct sock *sk, |
| 151 | struct list_head *hist_list, |
| 152 | struct timeval *last_feedback, |
| 153 | u16 s, u32 bytes_recv, |
| 154 | u32 previous_x_recv) |
| 155 | { |
| 156 | struct dccp_rx_hist_entry *entry, *next, *tail = NULL; |
| 157 | u32 x_recv, p; |
| 158 | suseconds_t rtt, delta; |
| 159 | struct timeval tstamp = { 0, 0 }; |
| 160 | int interval = 0; |
| 161 | int win_count = 0; |
| 162 | int step = 0; |
| 163 | u64 fval; |
| 164 | |
| 165 | list_for_each_entry_safe(entry, next, hist_list, dccphrx_node) { |
| 166 | if (dccp_rx_hist_entry_data_packet(entry)) { |
| 167 | tail = entry; |
| 168 | |
| 169 | switch (step) { |
| 170 | case 0: |
| 171 | tstamp = entry->dccphrx_tstamp; |
| 172 | win_count = entry->dccphrx_ccval; |
| 173 | step = 1; |
| 174 | break; |
| 175 | case 1: |
| 176 | interval = win_count - entry->dccphrx_ccval; |
| 177 | if (interval < 0) |
| 178 | interval += TFRC_WIN_COUNT_LIMIT; |
| 179 | if (interval > 4) |
| 180 | goto found; |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | if (unlikely(step == 0)) { |
| 187 | DCCP_WARN("%s(%p), packet history has no data packets!\n", |
| 188 | dccp_role(sk), sk); |
| 189 | return ~0; |
| 190 | } |
| 191 | |
| 192 | if (unlikely(interval == 0)) { |
| 193 | DCCP_WARN("%s(%p), Could not find a win_count interval > 0." |
| 194 | "Defaulting to 1\n", dccp_role(sk), sk); |
| 195 | interval = 1; |
| 196 | } |
| 197 | found: |
| 198 | if (!tail) { |
| 199 | DCCP_CRIT("tail is null\n"); |
| 200 | return ~0; |
| 201 | } |
| 202 | |
| 203 | delta = timeval_delta(&tstamp, &tail->dccphrx_tstamp); |
| 204 | DCCP_BUG_ON(delta < 0); |
| 205 | |
| 206 | rtt = delta * 4 / interval; |
| 207 | dccp_pr_debug("%s(%p), approximated RTT to %dus\n", |
| 208 | dccp_role(sk), sk, (int)rtt); |
| 209 | |
| 210 | /* |
| 211 | * Determine the length of the first loss interval via inverse lookup. |
| 212 | * Assume that X_recv can be computed by the throughput equation |
| 213 | * s |
| 214 | * X_recv = -------- |
| 215 | * R * fval |
| 216 | * Find some p such that f(p) = fval; return 1/p [RFC 3448, 6.3.1]. |
| 217 | */ |
| 218 | if (rtt == 0) { /* would result in divide-by-zero */ |
| 219 | DCCP_WARN("RTT==0\n"); |
| 220 | return ~0; |
| 221 | } |
| 222 | |
| 223 | dccp_timestamp(sk, &tstamp); |
| 224 | delta = timeval_delta(&tstamp, last_feedback); |
| 225 | DCCP_BUG_ON(delta <= 0); |
| 226 | |
| 227 | x_recv = scaled_div32(bytes_recv, delta); |
| 228 | if (x_recv == 0) { /* would also trigger divide-by-zero */ |
| 229 | DCCP_WARN("X_recv==0\n"); |
| 230 | if (previous_x_recv == 0) { |
| 231 | DCCP_BUG("stored value of X_recv is zero"); |
| 232 | return ~0; |
| 233 | } |
| 234 | x_recv = previous_x_recv; |
| 235 | } |
| 236 | |
| 237 | fval = scaled_div(s, rtt); |
| 238 | fval = scaled_div32(fval, x_recv); |
| 239 | p = tfrc_calc_x_reverse_lookup(fval); |
| 240 | |
| 241 | dccp_pr_debug("%s(%p), receive rate=%u bytes/s, implied " |
| 242 | "loss rate=%u\n", dccp_role(sk), sk, x_recv, p); |
| 243 | |
| 244 | if (p == 0) |
| 245 | return ~0; |
| 246 | else |
| 247 | return 1000000 / p; |
| 248 | } |
| 249 | |
| 250 | void dccp_li_update_li(struct sock *sk, struct dccp_li_hist *li_hist, |
| 251 | struct list_head *li_hist_list, |
| 252 | struct list_head *hist_list, |
| 253 | struct timeval *last_feedback, u16 s, u32 bytes_recv, |
| 254 | u32 previous_x_recv, u64 seq_loss, u8 win_loss) |
| 255 | { |
| 256 | struct dccp_li_hist_entry *head; |
| 257 | u64 seq_temp; |
| 258 | |
| 259 | if (list_empty(li_hist_list)) { |
| 260 | if (!dccp_li_hist_interval_new(li_hist, li_hist_list, |
| 261 | seq_loss, win_loss)) |
| 262 | return; |
| 263 | |
| 264 | head = list_entry(li_hist_list->next, struct dccp_li_hist_entry, |
| 265 | dccplih_node); |
| 266 | head->dccplih_interval = dccp_li_calc_first_li(sk, hist_list, |
| 267 | last_feedback, |
| 268 | s, bytes_recv, |
| 269 | previous_x_recv); |
| 270 | } else { |
| 271 | struct dccp_li_hist_entry *entry; |
| 272 | struct list_head *tail; |
| 273 | |
| 274 | head = list_entry(li_hist_list->next, struct dccp_li_hist_entry, |
| 275 | dccplih_node); |
| 276 | /* FIXME win count check removed as was wrong */ |
| 277 | /* should make this check with receive history */ |
| 278 | /* and compare there as per section 10.2 of RFC4342 */ |
| 279 | |
| 280 | /* new loss event detected */ |
| 281 | /* calculate last interval length */ |
| 282 | seq_temp = dccp_delta_seqno(head->dccplih_seqno, seq_loss); |
| 283 | entry = dccp_li_hist_entry_new(li_hist, GFP_ATOMIC); |
| 284 | |
| 285 | if (entry == NULL) { |
| 286 | DCCP_BUG("out of memory - can not allocate entry"); |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | list_add(&entry->dccplih_node, li_hist_list); |
| 291 | |
| 292 | tail = li_hist_list->prev; |
| 293 | list_del(tail); |
| 294 | kmem_cache_free(li_hist->dccplih_slab, tail); |
| 295 | |
| 296 | /* Create the newest interval */ |
| 297 | entry->dccplih_seqno = seq_loss; |
| 298 | entry->dccplih_interval = seq_temp; |
| 299 | entry->dccplih_win_count = win_loss; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | EXPORT_SYMBOL_GPL(dccp_li_update_li); |