loss_interval.c 7.1 KB

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