packet_history.c 7.6 KB

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