loss_interval.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * net/dccp/ccids/lib/loss_interval.c
  3. *
  4. * Copyright (c) 2005-7 The University of Waikato, Hamilton, New Zealand.
  5. * Copyright (c) 2005-7 Ian McDonald <ian.mcdonald@jandi.co.nz>
  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. #include <linux/module.h>
  14. #include <net/sock.h>
  15. #include "../../dccp.h"
  16. #include "loss_interval.h"
  17. #include "packet_history.h"
  18. #include "tfrc.h"
  19. #define DCCP_LI_HIST_IVAL_F_LENGTH 8
  20. struct dccp_li_hist_entry {
  21. struct list_head dccplih_node;
  22. u64 dccplih_seqno:48,
  23. dccplih_win_count:4;
  24. u32 dccplih_interval;
  25. };
  26. static struct kmem_cache *dccp_li_cachep __read_mostly;
  27. static inline struct dccp_li_hist_entry *dccp_li_hist_entry_new(const gfp_t prio)
  28. {
  29. return kmem_cache_alloc(dccp_li_cachep, prio);
  30. }
  31. static inline void dccp_li_hist_entry_delete(struct dccp_li_hist_entry *entry)
  32. {
  33. if (entry != NULL)
  34. kmem_cache_free(dccp_li_cachep, entry);
  35. }
  36. void dccp_li_hist_purge(struct list_head *list)
  37. {
  38. struct dccp_li_hist_entry *entry, *next;
  39. list_for_each_entry_safe(entry, next, list, dccplih_node) {
  40. list_del_init(&entry->dccplih_node);
  41. kmem_cache_free(dccp_li_cachep, entry);
  42. }
  43. }
  44. EXPORT_SYMBOL_GPL(dccp_li_hist_purge);
  45. /* Weights used to calculate loss event rate */
  46. /*
  47. * These are integers as per section 8 of RFC3448. We can then divide by 4 *
  48. * when we use it.
  49. */
  50. static const int dccp_li_hist_w[DCCP_LI_HIST_IVAL_F_LENGTH] = {
  51. 4, 4, 4, 4, 3, 2, 1, 1,
  52. };
  53. u32 dccp_li_hist_calc_i_mean(struct list_head *list)
  54. {
  55. struct dccp_li_hist_entry *li_entry, *li_next;
  56. int i = 0;
  57. u32 i_tot;
  58. u32 i_tot0 = 0;
  59. u32 i_tot1 = 0;
  60. u32 w_tot = 0;
  61. list_for_each_entry_safe(li_entry, li_next, list, dccplih_node) {
  62. if (li_entry->dccplih_interval != ~0U) {
  63. i_tot0 += li_entry->dccplih_interval * dccp_li_hist_w[i];
  64. w_tot += dccp_li_hist_w[i];
  65. if (i != 0)
  66. i_tot1 += li_entry->dccplih_interval * dccp_li_hist_w[i - 1];
  67. }
  68. if (++i > DCCP_LI_HIST_IVAL_F_LENGTH)
  69. break;
  70. }
  71. if (i != DCCP_LI_HIST_IVAL_F_LENGTH)
  72. return 0;
  73. i_tot = max(i_tot0, i_tot1);
  74. if (!w_tot) {
  75. DCCP_WARN("w_tot = 0\n");
  76. return 1;
  77. }
  78. return i_tot / w_tot;
  79. }
  80. EXPORT_SYMBOL_GPL(dccp_li_hist_calc_i_mean);
  81. static int dccp_li_hist_interval_new(struct list_head *list,
  82. const u64 seq_loss, const u8 win_loss)
  83. {
  84. struct dccp_li_hist_entry *entry;
  85. int i;
  86. for (i = 0; i < DCCP_LI_HIST_IVAL_F_LENGTH; i++) {
  87. entry = dccp_li_hist_entry_new(GFP_ATOMIC);
  88. if (entry == NULL) {
  89. dccp_li_hist_purge(list);
  90. DCCP_BUG("loss interval list entry is NULL");
  91. return 0;
  92. }
  93. entry->dccplih_interval = ~0;
  94. list_add(&entry->dccplih_node, list);
  95. }
  96. entry->dccplih_seqno = seq_loss;
  97. entry->dccplih_win_count = win_loss;
  98. return 1;
  99. }
  100. /* calculate first loss interval
  101. *
  102. * returns estimated loss interval in usecs */
  103. static u32 dccp_li_calc_first_li(struct sock *sk,
  104. struct list_head *hist_list,
  105. struct timeval *last_feedback,
  106. u16 s, u32 bytes_recv,
  107. u32 previous_x_recv)
  108. {
  109. struct dccp_rx_hist_entry *entry, *next, *tail = NULL;
  110. u32 x_recv, p;
  111. suseconds_t rtt, delta;
  112. struct timeval tstamp = { 0, 0 };
  113. int interval = 0;
  114. int win_count = 0;
  115. int step = 0;
  116. u64 fval;
  117. list_for_each_entry_safe(entry, next, hist_list, dccphrx_node) {
  118. if (dccp_rx_hist_entry_data_packet(entry)) {
  119. tail = entry;
  120. switch (step) {
  121. case 0:
  122. tstamp = entry->dccphrx_tstamp;
  123. win_count = entry->dccphrx_ccval;
  124. step = 1;
  125. break;
  126. case 1:
  127. interval = win_count - entry->dccphrx_ccval;
  128. if (interval < 0)
  129. interval += TFRC_WIN_COUNT_LIMIT;
  130. if (interval > 4)
  131. goto found;
  132. break;
  133. }
  134. }
  135. }
  136. if (unlikely(step == 0)) {
  137. DCCP_WARN("%s(%p), packet history has no data packets!\n",
  138. dccp_role(sk), sk);
  139. return ~0;
  140. }
  141. if (unlikely(interval == 0)) {
  142. DCCP_WARN("%s(%p), Could not find a win_count interval > 0."
  143. "Defaulting to 1\n", dccp_role(sk), sk);
  144. interval = 1;
  145. }
  146. found:
  147. if (!tail) {
  148. DCCP_CRIT("tail is null\n");
  149. return ~0;
  150. }
  151. delta = timeval_delta(&tstamp, &tail->dccphrx_tstamp);
  152. DCCP_BUG_ON(delta < 0);
  153. rtt = delta * 4 / interval;
  154. dccp_pr_debug("%s(%p), approximated RTT to %dus\n",
  155. dccp_role(sk), sk, (int)rtt);
  156. /*
  157. * Determine the length of the first loss interval via inverse lookup.
  158. * Assume that X_recv can be computed by the throughput equation
  159. * s
  160. * X_recv = --------
  161. * R * fval
  162. * Find some p such that f(p) = fval; return 1/p [RFC 3448, 6.3.1].
  163. */
  164. if (rtt == 0) { /* would result in divide-by-zero */
  165. DCCP_WARN("RTT==0\n");
  166. return ~0;
  167. }
  168. dccp_timestamp(sk, &tstamp);
  169. delta = timeval_delta(&tstamp, last_feedback);
  170. DCCP_BUG_ON(delta <= 0);
  171. x_recv = scaled_div32(bytes_recv, delta);
  172. if (x_recv == 0) { /* would also trigger divide-by-zero */
  173. DCCP_WARN("X_recv==0\n");
  174. if (previous_x_recv == 0) {
  175. DCCP_BUG("stored value of X_recv is zero");
  176. return ~0;
  177. }
  178. x_recv = previous_x_recv;
  179. }
  180. fval = scaled_div(s, rtt);
  181. fval = scaled_div32(fval, x_recv);
  182. p = tfrc_calc_x_reverse_lookup(fval);
  183. dccp_pr_debug("%s(%p), receive rate=%u bytes/s, implied "
  184. "loss rate=%u\n", dccp_role(sk), sk, x_recv, p);
  185. if (p == 0)
  186. return ~0;
  187. else
  188. return 1000000 / p;
  189. }
  190. void dccp_li_update_li(struct sock *sk,
  191. struct list_head *li_hist_list,
  192. struct list_head *hist_list,
  193. struct timeval *last_feedback, u16 s, u32 bytes_recv,
  194. u32 previous_x_recv, u64 seq_loss, u8 win_loss)
  195. {
  196. struct dccp_li_hist_entry *head;
  197. u64 seq_temp;
  198. if (list_empty(li_hist_list)) {
  199. if (!dccp_li_hist_interval_new(li_hist_list, seq_loss,
  200. win_loss))
  201. return;
  202. head = list_entry(li_hist_list->next, struct dccp_li_hist_entry,
  203. dccplih_node);
  204. head->dccplih_interval = dccp_li_calc_first_li(sk, hist_list,
  205. last_feedback,
  206. s, bytes_recv,
  207. previous_x_recv);
  208. } else {
  209. struct dccp_li_hist_entry *entry;
  210. struct list_head *tail;
  211. head = list_entry(li_hist_list->next, struct dccp_li_hist_entry,
  212. dccplih_node);
  213. /* FIXME win count check removed as was wrong */
  214. /* should make this check with receive history */
  215. /* and compare there as per section 10.2 of RFC4342 */
  216. /* new loss event detected */
  217. /* calculate last interval length */
  218. seq_temp = dccp_delta_seqno(head->dccplih_seqno, seq_loss);
  219. entry = dccp_li_hist_entry_new(GFP_ATOMIC);
  220. if (entry == NULL) {
  221. DCCP_BUG("out of memory - can not allocate entry");
  222. return;
  223. }
  224. list_add(&entry->dccplih_node, li_hist_list);
  225. tail = li_hist_list->prev;
  226. list_del(tail);
  227. kmem_cache_free(dccp_li_cachep, tail);
  228. /* Create the newest interval */
  229. entry->dccplih_seqno = seq_loss;
  230. entry->dccplih_interval = seq_temp;
  231. entry->dccplih_win_count = win_loss;
  232. }
  233. }
  234. EXPORT_SYMBOL_GPL(dccp_li_update_li);
  235. static __init int dccp_li_init(void)
  236. {
  237. dccp_li_cachep = kmem_cache_create("dccp_li_hist",
  238. sizeof(struct dccp_li_hist_entry),
  239. 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
  240. return dccp_li_cachep == NULL ? -ENOBUFS : 0;
  241. }
  242. static __exit void dccp_li_exit(void)
  243. {
  244. kmem_cache_destroy(dccp_li_cachep);
  245. }
  246. module_init(dccp_li_init);
  247. module_exit(dccp_li_exit);