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 | |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame^] | 21 | struct kmem_cache *dccp_li_cachep __read_mostly; |
| 22 | |
| 23 | 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] | 24 | { |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame^] | 25 | return kmem_cache_alloc(dccp_li_cachep, prio); |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 26 | } |
| 27 | |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame^] | 28 | 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] | 29 | { |
| 30 | if (entry != NULL) |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame^] | 31 | kmem_cache_free(dccp_li_cachep, entry); |
Arnaldo Carvalho de Melo | c70b729 | 2007-05-28 18:25:12 -0300 | [diff] [blame] | 32 | } |
| 33 | |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame^] | 34 | void dccp_li_hist_purge(struct list_head *list) |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 35 | { |
| 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 Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame^] | 40 | kmem_cache_free(dccp_li_cachep, entry); |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
| 44 | EXPORT_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 | */ |
| 51 | static const int dccp_li_hist_w[DCCP_LI_HIST_IVAL_F_LENGTH] = { |
| 52 | 4, 4, 4, 4, 3, 2, 1, 1, |
| 53 | }; |
| 54 | |
| 55 | u32 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 McDonald | 551dc5f | 2007-03-20 14:46:52 -0300 | [diff] [blame] | 65 | if (li_entry->dccplih_interval != ~0U) { |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 66 | i_tot0 += li_entry->dccplih_interval * dccp_li_hist_w[i]; |
| 67 | w_tot += dccp_li_hist_w[i]; |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 68 | if (i != 0) |
| 69 | 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] | 70 | } |
| 71 | |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 72 | |
| 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 McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 82 | if (!w_tot) { |
Gerrit Renker | 59348b1 | 2006-11-20 18:39:23 -0200 | [diff] [blame] | 83 | DCCP_WARN("w_tot = 0\n"); |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 84 | return 1; |
| 85 | } |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 86 | |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 87 | return i_tot / w_tot; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | EXPORT_SYMBOL_GPL(dccp_li_hist_calc_i_mean); |
| 91 | |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame^] | 92 | 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] | 93 | const u64 seq_loss, const u8 win_loss) |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 94 | { |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 95 | struct dccp_li_hist_entry *entry; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 96 | int i; |
| 97 | |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 98 | 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^] | 99 | entry = dccp_li_hist_entry_new(GFP_ATOMIC); |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 100 | if (entry == NULL) { |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame^] | 101 | dccp_li_hist_purge(list); |
Gerrit Renker | 59348b1 | 2006-11-20 18:39:23 -0200 | [diff] [blame] | 102 | DCCP_BUG("loss interval list entry is NULL"); |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 103 | return 0; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 104 | } |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 105 | entry->dccplih_interval = ~0; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 106 | list_add(&entry->dccplih_node, list); |
| 107 | } |
| 108 | |
| 109 | entry->dccplih_seqno = seq_loss; |
| 110 | entry->dccplih_win_count = win_loss; |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 111 | return 1; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 112 | } |
| 113 | |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 114 | /* calculate first loss interval |
| 115 | * |
| 116 | * returns estimated loss interval in usecs */ |
| 117 | static 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 | } |
| 164 | found: |
| 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 Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame^] | 217 | void dccp_li_update_li(struct sock *sk, |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 218 | 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 Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame^] | 227 | if (!dccp_li_hist_interval_new(li_hist_list, seq_loss, |
| 228 | win_loss)) |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 229 | 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 Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame^] | 250 | entry = dccp_li_hist_entry_new(GFP_ATOMIC); |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 251 | |
| 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 Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame^] | 261 | kmem_cache_free(dccp_li_cachep, tail); |
Arnaldo Carvalho de Melo | cc0a910 | 2007-06-14 17:41:28 -0300 | [diff] [blame] | 262 | |
| 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 | |
| 270 | EXPORT_SYMBOL_GPL(dccp_li_update_li); |
Arnaldo Carvalho de Melo | cc4d6a3 | 2007-05-28 18:53:08 -0300 | [diff] [blame^] | 271 | |
| 272 | static __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 | |
| 280 | static __exit void dccp_li_exit(void) |
| 281 | { |
| 282 | kmem_cache_destroy(dccp_li_cachep); |
| 283 | } |
| 284 | |
| 285 | module_init(dccp_li_init); |
| 286 | module_exit(dccp_li_exit); |