packet_history.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * net/dccp/packet_history.c
  3. *
  4. * Copyright (c) 2007 The University of Aberdeen, Scotland, UK
  5. * Copyright (c) 2005-7 The University of Waikato, Hamilton, New Zealand.
  6. *
  7. * An implementation of the DCCP protocol
  8. *
  9. * This code has been developed by the University of Waikato WAND
  10. * research group. For further information please see http://www.wand.net.nz/
  11. * or e-mail Ian McDonald - ian.mcdonald@jandi.co.nz
  12. *
  13. * This code also uses code from Lulea University, rereleased as GPL by its
  14. * authors:
  15. * Copyright (c) 2003 Nils-Erik Mattsson, Joacim Haggmark, Magnus Erixzon
  16. *
  17. * Changes to meet Linux coding standards, to make it meet latest ccid3 draft
  18. * and to make it work as a loadable module in the DCCP stack written by
  19. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>.
  20. *
  21. * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  22. *
  23. * This program is free software; you can redistribute it and/or modify
  24. * it under the terms of the GNU General Public License as published by
  25. * the Free Software Foundation; either version 2 of the License, or
  26. * (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with this program; if not, write to the Free Software
  35. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  36. */
  37. #include <linux/string.h>
  38. #include "packet_history.h"
  39. /**
  40. * tfrc_tx_hist_entry - Simple singly-linked TX history list
  41. * @next: next oldest entry (LIFO order)
  42. * @seqno: sequence number of this entry
  43. * @stamp: send time of packet with sequence number @seqno
  44. */
  45. struct tfrc_tx_hist_entry {
  46. struct tfrc_tx_hist_entry *next;
  47. u64 seqno;
  48. ktime_t stamp;
  49. };
  50. /*
  51. * Transmitter History Routines
  52. */
  53. static struct kmem_cache *tfrc_tx_hist_slab;
  54. static struct tfrc_tx_hist_entry *
  55. tfrc_tx_hist_find_entry(struct tfrc_tx_hist_entry *head, u64 seqno)
  56. {
  57. while (head != NULL && head->seqno != seqno)
  58. head = head->next;
  59. return head;
  60. }
  61. int tfrc_tx_hist_add(struct tfrc_tx_hist_entry **headp, u64 seqno)
  62. {
  63. struct tfrc_tx_hist_entry *entry = kmem_cache_alloc(tfrc_tx_hist_slab, gfp_any());
  64. if (entry == NULL)
  65. return -ENOBUFS;
  66. entry->seqno = seqno;
  67. entry->stamp = ktime_get_real();
  68. entry->next = *headp;
  69. *headp = entry;
  70. return 0;
  71. }
  72. EXPORT_SYMBOL_GPL(tfrc_tx_hist_add);
  73. void tfrc_tx_hist_purge(struct tfrc_tx_hist_entry **headp)
  74. {
  75. struct tfrc_tx_hist_entry *head = *headp;
  76. while (head != NULL) {
  77. struct tfrc_tx_hist_entry *next = head->next;
  78. kmem_cache_free(tfrc_tx_hist_slab, head);
  79. head = next;
  80. }
  81. *headp = NULL;
  82. }
  83. EXPORT_SYMBOL_GPL(tfrc_tx_hist_purge);
  84. u32 tfrc_tx_hist_rtt(struct tfrc_tx_hist_entry *head, const u64 seqno,
  85. const ktime_t now)
  86. {
  87. u32 rtt = 0;
  88. struct tfrc_tx_hist_entry *packet = tfrc_tx_hist_find_entry(head, seqno);
  89. if (packet != NULL) {
  90. rtt = ktime_us_delta(now, packet->stamp);
  91. /*
  92. * Garbage-collect older (irrelevant) entries:
  93. */
  94. tfrc_tx_hist_purge(&packet->next);
  95. }
  96. return rtt;
  97. }
  98. EXPORT_SYMBOL_GPL(tfrc_tx_hist_rtt);
  99. /*
  100. * Receiver History Routines
  101. */
  102. struct dccp_rx_hist *dccp_rx_hist_new(const char *name)
  103. {
  104. struct dccp_rx_hist *hist = kmalloc(sizeof(*hist), GFP_ATOMIC);
  105. static const char dccp_rx_hist_mask[] = "rx_hist_%s";
  106. char *slab_name;
  107. if (hist == NULL)
  108. goto out;
  109. slab_name = kmalloc(strlen(name) + sizeof(dccp_rx_hist_mask) - 1,
  110. GFP_ATOMIC);
  111. if (slab_name == NULL)
  112. goto out_free_hist;
  113. sprintf(slab_name, dccp_rx_hist_mask, name);
  114. hist->dccprxh_slab = kmem_cache_create(slab_name,
  115. sizeof(struct dccp_rx_hist_entry),
  116. 0, SLAB_HWCACHE_ALIGN, NULL);
  117. if (hist->dccprxh_slab == NULL)
  118. goto out_free_slab_name;
  119. out:
  120. return hist;
  121. out_free_slab_name:
  122. kfree(slab_name);
  123. out_free_hist:
  124. kfree(hist);
  125. hist = NULL;
  126. goto out;
  127. }
  128. EXPORT_SYMBOL_GPL(dccp_rx_hist_new);
  129. void dccp_rx_hist_delete(struct dccp_rx_hist *hist)
  130. {
  131. const char* name = kmem_cache_name(hist->dccprxh_slab);
  132. kmem_cache_destroy(hist->dccprxh_slab);
  133. kfree(name);
  134. kfree(hist);
  135. }
  136. EXPORT_SYMBOL_GPL(dccp_rx_hist_delete);
  137. int dccp_rx_hist_find_entry(const struct list_head *list, const u64 seq,
  138. u8 *ccval)
  139. {
  140. struct dccp_rx_hist_entry *packet = NULL, *entry;
  141. list_for_each_entry(entry, list, dccphrx_node)
  142. if (entry->dccphrx_seqno == seq) {
  143. packet = entry;
  144. break;
  145. }
  146. if (packet)
  147. *ccval = packet->dccphrx_ccval;
  148. return packet != NULL;
  149. }
  150. EXPORT_SYMBOL_GPL(dccp_rx_hist_find_entry);
  151. struct dccp_rx_hist_entry *
  152. dccp_rx_hist_find_data_packet(const struct list_head *list)
  153. {
  154. struct dccp_rx_hist_entry *entry, *packet = NULL;
  155. list_for_each_entry(entry, list, dccphrx_node)
  156. if (entry->dccphrx_type == DCCP_PKT_DATA ||
  157. entry->dccphrx_type == DCCP_PKT_DATAACK) {
  158. packet = entry;
  159. break;
  160. }
  161. return packet;
  162. }
  163. EXPORT_SYMBOL_GPL(dccp_rx_hist_find_data_packet);
  164. void dccp_rx_hist_add_packet(struct dccp_rx_hist *hist,
  165. struct list_head *rx_list,
  166. struct list_head *li_list,
  167. struct dccp_rx_hist_entry *packet,
  168. u64 nonloss_seqno)
  169. {
  170. struct dccp_rx_hist_entry *entry, *next;
  171. u8 num_later = 0;
  172. list_add(&packet->dccphrx_node, rx_list);
  173. num_later = TFRC_RECV_NUM_LATE_LOSS + 1;
  174. if (!list_empty(li_list)) {
  175. list_for_each_entry_safe(entry, next, rx_list, dccphrx_node) {
  176. if (num_later == 0) {
  177. if (after48(nonloss_seqno,
  178. entry->dccphrx_seqno)) {
  179. list_del_init(&entry->dccphrx_node);
  180. dccp_rx_hist_entry_delete(hist, entry);
  181. }
  182. } else if (dccp_rx_hist_entry_data_packet(entry))
  183. --num_later;
  184. }
  185. } else {
  186. int step = 0;
  187. u8 win_count = 0; /* Not needed, but lets shut up gcc */
  188. int tmp;
  189. /*
  190. * We have no loss interval history so we need at least one
  191. * rtt:s of data packets to approximate rtt.
  192. */
  193. list_for_each_entry_safe(entry, next, rx_list, dccphrx_node) {
  194. if (num_later == 0) {
  195. switch (step) {
  196. case 0:
  197. step = 1;
  198. /* OK, find next data packet */
  199. num_later = 1;
  200. break;
  201. case 1:
  202. step = 2;
  203. /* OK, find next data packet */
  204. num_later = 1;
  205. win_count = entry->dccphrx_ccval;
  206. break;
  207. case 2:
  208. tmp = win_count - entry->dccphrx_ccval;
  209. if (tmp < 0)
  210. tmp += TFRC_WIN_COUNT_LIMIT;
  211. if (tmp > TFRC_WIN_COUNT_PER_RTT + 1) {
  212. /*
  213. * We have found a packet older
  214. * than one rtt remove the rest
  215. */
  216. step = 3;
  217. } else /* OK, find next data packet */
  218. num_later = 1;
  219. break;
  220. case 3:
  221. list_del_init(&entry->dccphrx_node);
  222. dccp_rx_hist_entry_delete(hist, entry);
  223. break;
  224. }
  225. } else if (dccp_rx_hist_entry_data_packet(entry))
  226. --num_later;
  227. }
  228. }
  229. }
  230. EXPORT_SYMBOL_GPL(dccp_rx_hist_add_packet);
  231. void dccp_rx_hist_purge(struct dccp_rx_hist *hist, struct list_head *list)
  232. {
  233. struct dccp_rx_hist_entry *entry, *next;
  234. list_for_each_entry_safe(entry, next, list, dccphrx_node) {
  235. list_del_init(&entry->dccphrx_node);
  236. kmem_cache_free(hist->dccprxh_slab, entry);
  237. }
  238. }
  239. EXPORT_SYMBOL_GPL(dccp_rx_hist_purge);
  240. __init int packet_history_init(void)
  241. {
  242. tfrc_tx_hist_slab = kmem_cache_create("tfrc_tx_hist",
  243. sizeof(struct tfrc_tx_hist_entry), 0,
  244. SLAB_HWCACHE_ALIGN, NULL);
  245. return tfrc_tx_hist_slab == NULL ? -ENOBUFS : 0;
  246. }
  247. void packet_history_exit(void)
  248. {
  249. if (tfrc_tx_hist_slab != NULL) {
  250. kmem_cache_destroy(tfrc_tx_hist_slab);
  251. tfrc_tx_hist_slab = NULL;
  252. }
  253. }