packet_history.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. struct dccp_rx_hist *dccp_rx_hist_new(const char *name)
  40. {
  41. struct dccp_rx_hist *hist = kmalloc(sizeof(*hist), GFP_ATOMIC);
  42. static const char dccp_rx_hist_mask[] = "rx_hist_%s";
  43. char *slab_name;
  44. if (hist == NULL)
  45. goto out;
  46. slab_name = kmalloc(strlen(name) + sizeof(dccp_rx_hist_mask) - 1,
  47. GFP_ATOMIC);
  48. if (slab_name == NULL)
  49. goto out_free_hist;
  50. sprintf(slab_name, dccp_rx_hist_mask, name);
  51. hist->dccprxh_slab = kmem_cache_create(slab_name,
  52. sizeof(struct dccp_rx_hist_entry),
  53. 0, SLAB_HWCACHE_ALIGN,
  54. NULL, NULL);
  55. if (hist->dccprxh_slab == NULL)
  56. goto out_free_slab_name;
  57. out:
  58. return hist;
  59. out_free_slab_name:
  60. kfree(slab_name);
  61. out_free_hist:
  62. kfree(hist);
  63. hist = NULL;
  64. goto out;
  65. }
  66. EXPORT_SYMBOL_GPL(dccp_rx_hist_new);
  67. void dccp_rx_hist_delete(struct dccp_rx_hist *hist)
  68. {
  69. const char* name = kmem_cache_name(hist->dccprxh_slab);
  70. kmem_cache_destroy(hist->dccprxh_slab);
  71. kfree(name);
  72. kfree(hist);
  73. }
  74. EXPORT_SYMBOL_GPL(dccp_rx_hist_delete);
  75. void dccp_rx_hist_purge(struct dccp_rx_hist *hist, struct list_head *list)
  76. {
  77. struct dccp_rx_hist_entry *entry, *next;
  78. list_for_each_entry_safe(entry, next, list, dccphrx_node) {
  79. list_del_init(&entry->dccphrx_node);
  80. kmem_cache_free(hist->dccprxh_slab, entry);
  81. }
  82. }
  83. EXPORT_SYMBOL_GPL(dccp_rx_hist_purge);
  84. struct dccp_rx_hist_entry *
  85. dccp_rx_hist_find_data_packet(const struct list_head *list)
  86. {
  87. struct dccp_rx_hist_entry *entry, *packet = NULL;
  88. list_for_each_entry(entry, list, dccphrx_node)
  89. if (entry->dccphrx_type == DCCP_PKT_DATA ||
  90. entry->dccphrx_type == DCCP_PKT_DATAACK) {
  91. packet = entry;
  92. break;
  93. }
  94. return packet;
  95. }
  96. EXPORT_SYMBOL_GPL(dccp_rx_hist_find_data_packet);
  97. void dccp_rx_hist_add_packet(struct dccp_rx_hist *hist,
  98. struct list_head *rx_list,
  99. struct list_head *li_list,
  100. struct dccp_rx_hist_entry *packet,
  101. u64 nonloss_seqno)
  102. {
  103. struct dccp_rx_hist_entry *entry, *next;
  104. u8 num_later = 0;
  105. list_add(&packet->dccphrx_node, rx_list);
  106. num_later = TFRC_RECV_NUM_LATE_LOSS + 1;
  107. if (!list_empty(li_list)) {
  108. list_for_each_entry_safe(entry, next, rx_list, dccphrx_node) {
  109. if (num_later == 0) {
  110. if (after48(nonloss_seqno,
  111. entry->dccphrx_seqno)) {
  112. list_del_init(&entry->dccphrx_node);
  113. dccp_rx_hist_entry_delete(hist, entry);
  114. }
  115. } else if (dccp_rx_hist_entry_data_packet(entry))
  116. --num_later;
  117. }
  118. } else {
  119. int step = 0;
  120. u8 win_count = 0; /* Not needed, but lets shut up gcc */
  121. int tmp;
  122. /*
  123. * We have no loss interval history so we need at least one
  124. * rtt:s of data packets to approximate rtt.
  125. */
  126. list_for_each_entry_safe(entry, next, rx_list, dccphrx_node) {
  127. if (num_later == 0) {
  128. switch (step) {
  129. case 0:
  130. step = 1;
  131. /* OK, find next data packet */
  132. num_later = 1;
  133. break;
  134. case 1:
  135. step = 2;
  136. /* OK, find next data packet */
  137. num_later = 1;
  138. win_count = entry->dccphrx_ccval;
  139. break;
  140. case 2:
  141. tmp = win_count - entry->dccphrx_ccval;
  142. if (tmp < 0)
  143. tmp += TFRC_WIN_COUNT_LIMIT;
  144. if (tmp > TFRC_WIN_COUNT_PER_RTT + 1) {
  145. /*
  146. * We have found a packet older
  147. * than one rtt remove the rest
  148. */
  149. step = 3;
  150. } else /* OK, find next data packet */
  151. num_later = 1;
  152. break;
  153. case 3:
  154. list_del_init(&entry->dccphrx_node);
  155. dccp_rx_hist_entry_delete(hist, entry);
  156. break;
  157. }
  158. } else if (dccp_rx_hist_entry_data_packet(entry))
  159. --num_later;
  160. }
  161. }
  162. }
  163. EXPORT_SYMBOL_GPL(dccp_rx_hist_add_packet);
  164. struct dccp_tx_hist *dccp_tx_hist_new(const char *name)
  165. {
  166. struct dccp_tx_hist *hist = kmalloc(sizeof(*hist), GFP_ATOMIC);
  167. static const char dccp_tx_hist_mask[] = "tx_hist_%s";
  168. char *slab_name;
  169. if (hist == NULL)
  170. goto out;
  171. slab_name = kmalloc(strlen(name) + sizeof(dccp_tx_hist_mask) - 1,
  172. GFP_ATOMIC);
  173. if (slab_name == NULL)
  174. goto out_free_hist;
  175. sprintf(slab_name, dccp_tx_hist_mask, name);
  176. hist->dccptxh_slab = kmem_cache_create(slab_name,
  177. sizeof(struct dccp_tx_hist_entry),
  178. 0, SLAB_HWCACHE_ALIGN,
  179. NULL, NULL);
  180. if (hist->dccptxh_slab == NULL)
  181. goto out_free_slab_name;
  182. out:
  183. return hist;
  184. out_free_slab_name:
  185. kfree(slab_name);
  186. out_free_hist:
  187. kfree(hist);
  188. hist = NULL;
  189. goto out;
  190. }
  191. EXPORT_SYMBOL_GPL(dccp_tx_hist_new);
  192. void dccp_tx_hist_delete(struct dccp_tx_hist *hist)
  193. {
  194. const char* name = kmem_cache_name(hist->dccptxh_slab);
  195. kmem_cache_destroy(hist->dccptxh_slab);
  196. kfree(name);
  197. kfree(hist);
  198. }
  199. EXPORT_SYMBOL_GPL(dccp_tx_hist_delete);
  200. struct dccp_tx_hist_entry *
  201. dccp_tx_hist_find_entry(const struct list_head *list, const u64 seq)
  202. {
  203. struct dccp_tx_hist_entry *packet = NULL, *entry;
  204. list_for_each_entry(entry, list, dccphtx_node)
  205. if (entry->dccphtx_seqno == seq) {
  206. packet = entry;
  207. break;
  208. }
  209. return packet;
  210. }
  211. EXPORT_SYMBOL_GPL(dccp_tx_hist_find_entry);
  212. int dccp_rx_hist_find_entry(const struct list_head *list, const u64 seq,
  213. u8 *ccval)
  214. {
  215. struct dccp_rx_hist_entry *packet = NULL, *entry;
  216. list_for_each_entry(entry, list, dccphrx_node)
  217. if (entry->dccphrx_seqno == seq) {
  218. packet = entry;
  219. break;
  220. }
  221. if (packet)
  222. *ccval = packet->dccphrx_ccval;
  223. return packet != NULL;
  224. }
  225. EXPORT_SYMBOL_GPL(dccp_rx_hist_find_entry);
  226. void dccp_tx_hist_purge_older(struct dccp_tx_hist *hist,
  227. struct list_head *list,
  228. struct dccp_tx_hist_entry *packet)
  229. {
  230. struct dccp_tx_hist_entry *next;
  231. list_for_each_entry_safe_continue(packet, next, list, dccphtx_node) {
  232. list_del_init(&packet->dccphtx_node);
  233. dccp_tx_hist_entry_delete(hist, packet);
  234. }
  235. }
  236. EXPORT_SYMBOL_GPL(dccp_tx_hist_purge_older);
  237. void dccp_tx_hist_purge(struct dccp_tx_hist *hist, struct list_head *list)
  238. {
  239. struct dccp_tx_hist_entry *entry, *next;
  240. list_for_each_entry_safe(entry, next, list, dccphtx_node) {
  241. list_del_init(&entry->dccphtx_node);
  242. dccp_tx_hist_entry_delete(hist, entry);
  243. }
  244. }
  245. EXPORT_SYMBOL_GPL(dccp_tx_hist_purge);
  246. MODULE_AUTHOR("Ian McDonald <ian.mcdonald@jandi.co.nz>, "
  247. "Arnaldo Carvalho de Melo <acme@ghostprotocols.net>");
  248. MODULE_DESCRIPTION("DCCP TFRC library");
  249. MODULE_LICENSE("GPL");