loss_interval.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. ktime_t last_feedback,
  106. u16 s, u32 bytes_recv,
  107. u32 previous_x_recv)
  108. {
  109. /*
  110. * FIXME:
  111. * Will be rewritten in the upcoming new loss intervals code.
  112. * Has to be commented ou because it relies on the old rx history
  113. * data structures
  114. */
  115. #if 0
  116. struct tfrc_rx_hist_entry *entry, *next, *tail = NULL;
  117. u32 x_recv, p;
  118. suseconds_t rtt, delta;
  119. ktime_t tstamp = ktime_set(0, 0);
  120. int interval = 0;
  121. int win_count = 0;
  122. int step = 0;
  123. u64 fval;
  124. list_for_each_entry_safe(entry, next, hist_list, tfrchrx_node) {
  125. if (tfrc_rx_hist_entry_data_packet(entry)) {
  126. tail = entry;
  127. switch (step) {
  128. case 0:
  129. tstamp = entry->tfrchrx_tstamp;
  130. win_count = entry->tfrchrx_ccval;
  131. step = 1;
  132. break;
  133. case 1:
  134. interval = win_count - entry->tfrchrx_ccval;
  135. if (interval < 0)
  136. interval += TFRC_WIN_COUNT_LIMIT;
  137. if (interval > 4)
  138. goto found;
  139. break;
  140. }
  141. }
  142. }
  143. if (unlikely(step == 0)) {
  144. DCCP_WARN("%s(%p), packet history has no data packets!\n",
  145. dccp_role(sk), sk);
  146. return ~0;
  147. }
  148. if (unlikely(interval == 0)) {
  149. DCCP_WARN("%s(%p), Could not find a win_count interval > 0. "
  150. "Defaulting to 1\n", dccp_role(sk), sk);
  151. interval = 1;
  152. }
  153. found:
  154. if (!tail) {
  155. DCCP_CRIT("tail is null\n");
  156. return ~0;
  157. }
  158. delta = ktime_us_delta(tstamp, tail->tfrchrx_tstamp);
  159. DCCP_BUG_ON(delta < 0);
  160. rtt = delta * 4 / interval;
  161. dccp_pr_debug("%s(%p), approximated RTT to %dus\n",
  162. dccp_role(sk), sk, (int)rtt);
  163. /*
  164. * Determine the length of the first loss interval via inverse lookup.
  165. * Assume that X_recv can be computed by the throughput equation
  166. * s
  167. * X_recv = --------
  168. * R * fval
  169. * Find some p such that f(p) = fval; return 1/p [RFC 3448, 6.3.1].
  170. */
  171. if (rtt == 0) { /* would result in divide-by-zero */
  172. DCCP_WARN("RTT==0\n");
  173. return ~0;
  174. }
  175. delta = ktime_us_delta(ktime_get_real(), last_feedback);
  176. DCCP_BUG_ON(delta <= 0);
  177. x_recv = scaled_div32(bytes_recv, delta);
  178. if (x_recv == 0) { /* would also trigger divide-by-zero */
  179. DCCP_WARN("X_recv==0\n");
  180. if (previous_x_recv == 0) {
  181. DCCP_BUG("stored value of X_recv is zero");
  182. return ~0;
  183. }
  184. x_recv = previous_x_recv;
  185. }
  186. fval = scaled_div(s, rtt);
  187. fval = scaled_div32(fval, x_recv);
  188. p = tfrc_calc_x_reverse_lookup(fval);
  189. dccp_pr_debug("%s(%p), receive rate=%u bytes/s, implied "
  190. "loss rate=%u\n", dccp_role(sk), sk, x_recv, p);
  191. if (p != 0)
  192. return 1000000 / p;
  193. #endif
  194. return ~0;
  195. }
  196. void dccp_li_update_li(struct sock *sk,
  197. struct list_head *li_hist_list,
  198. struct list_head *hist_list,
  199. ktime_t last_feedback, u16 s, u32 bytes_recv,
  200. u32 previous_x_recv, u64 seq_loss, u8 win_loss)
  201. {
  202. struct dccp_li_hist_entry *head;
  203. u64 seq_temp;
  204. if (list_empty(li_hist_list)) {
  205. if (!dccp_li_hist_interval_new(li_hist_list, seq_loss,
  206. win_loss))
  207. return;
  208. head = list_entry(li_hist_list->next, struct dccp_li_hist_entry,
  209. dccplih_node);
  210. head->dccplih_interval = dccp_li_calc_first_li(sk, hist_list,
  211. last_feedback,
  212. s, bytes_recv,
  213. previous_x_recv);
  214. } else {
  215. struct dccp_li_hist_entry *entry;
  216. struct list_head *tail;
  217. head = list_entry(li_hist_list->next, struct dccp_li_hist_entry,
  218. dccplih_node);
  219. /* FIXME win count check removed as was wrong */
  220. /* should make this check with receive history */
  221. /* and compare there as per section 10.2 of RFC4342 */
  222. /* new loss event detected */
  223. /* calculate last interval length */
  224. seq_temp = dccp_delta_seqno(head->dccplih_seqno, seq_loss);
  225. entry = dccp_li_hist_entry_new(GFP_ATOMIC);
  226. if (entry == NULL) {
  227. DCCP_BUG("out of memory - can not allocate entry");
  228. return;
  229. }
  230. list_add(&entry->dccplih_node, li_hist_list);
  231. tail = li_hist_list->prev;
  232. list_del(tail);
  233. kmem_cache_free(dccp_li_cachep, tail);
  234. /* Create the newest interval */
  235. entry->dccplih_seqno = seq_loss;
  236. entry->dccplih_interval = seq_temp;
  237. entry->dccplih_win_count = win_loss;
  238. }
  239. }
  240. EXPORT_SYMBOL_GPL(dccp_li_update_li);
  241. int __init dccp_li_init(void)
  242. {
  243. dccp_li_cachep = kmem_cache_create("dccp_li_hist",
  244. sizeof(struct dccp_li_hist_entry),
  245. 0, SLAB_HWCACHE_ALIGN, NULL);
  246. return dccp_li_cachep == NULL ? -ENOBUFS : 0;
  247. }
  248. void dccp_li_exit(void)
  249. {
  250. if (dccp_li_cachep != NULL) {
  251. kmem_cache_destroy(dccp_li_cachep);
  252. dccp_li_cachep = NULL;
  253. }
  254. }