packet_history.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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/module.h>
  38. #include <linux/string.h>
  39. #include "packet_history.h"
  40. /**
  41. * tfrc_tx_hist_entry - Simple singly-linked TX history list
  42. * @next: next oldest entry (LIFO order)
  43. * @seqno: sequence number of this entry
  44. * @stamp: send time of packet with sequence number @seqno
  45. */
  46. struct tfrc_tx_hist_entry {
  47. struct tfrc_tx_hist_entry *next;
  48. u64 seqno;
  49. ktime_t stamp;
  50. };
  51. /*
  52. * Transmitter History Routines
  53. */
  54. static struct kmem_cache *tfrc_tx_hist;
  55. static struct tfrc_tx_hist_entry *
  56. tfrc_tx_hist_find_entry(struct tfrc_tx_hist_entry *head, u64 seqno)
  57. {
  58. while (head != NULL && head->seqno != seqno)
  59. head = head->next;
  60. return head;
  61. }
  62. int tfrc_tx_hist_add(struct tfrc_tx_hist_entry **headp, u64 seqno)
  63. {
  64. struct tfrc_tx_hist_entry *entry = kmem_cache_alloc(tfrc_tx_hist, gfp_any());
  65. if (entry == NULL)
  66. return -ENOBUFS;
  67. entry->seqno = seqno;
  68. entry->stamp = ktime_get_real();
  69. entry->next = *headp;
  70. *headp = entry;
  71. return 0;
  72. }
  73. EXPORT_SYMBOL_GPL(tfrc_tx_hist_add);
  74. void tfrc_tx_hist_purge(struct tfrc_tx_hist_entry **headp)
  75. {
  76. struct tfrc_tx_hist_entry *head = *headp;
  77. while (head != NULL) {
  78. struct tfrc_tx_hist_entry *next = head->next;
  79. kmem_cache_free(tfrc_tx_hist, head);
  80. head = next;
  81. }
  82. *headp = NULL;
  83. }
  84. EXPORT_SYMBOL_GPL(tfrc_tx_hist_purge);
  85. u32 tfrc_tx_hist_rtt(struct tfrc_tx_hist_entry *head, const u64 seqno,
  86. const ktime_t now)
  87. {
  88. u32 rtt = 0;
  89. struct tfrc_tx_hist_entry *packet = tfrc_tx_hist_find_entry(head, seqno);
  90. if (packet != NULL) {
  91. rtt = ktime_us_delta(now, packet->stamp);
  92. /*
  93. * Garbage-collect older (irrelevant) entries:
  94. */
  95. tfrc_tx_hist_purge(&packet->next);
  96. }
  97. return rtt;
  98. }
  99. EXPORT_SYMBOL_GPL(tfrc_tx_hist_rtt);
  100. /*
  101. * Receiver History Routines
  102. */
  103. struct dccp_rx_hist *dccp_rx_hist_new(const char *name)
  104. {
  105. struct dccp_rx_hist *hist = kmalloc(sizeof(*hist), GFP_ATOMIC);
  106. static const char dccp_rx_hist_mask[] = "rx_hist_%s";
  107. char *slab_name;
  108. if (hist == NULL)
  109. goto out;
  110. slab_name = kmalloc(strlen(name) + sizeof(dccp_rx_hist_mask) - 1,
  111. GFP_ATOMIC);
  112. if (slab_name == NULL)
  113. goto out_free_hist;
  114. sprintf(slab_name, dccp_rx_hist_mask, name);
  115. hist->dccprxh_slab = kmem_cache_create(slab_name,
  116. sizeof(struct dccp_rx_hist_entry),
  117. 0, SLAB_HWCACHE_ALIGN, NULL);
  118. if (hist->dccprxh_slab == NULL)
  119. goto out_free_slab_name;
  120. out:
  121. return hist;
  122. out_free_slab_name:
  123. kfree(slab_name);
  124. out_free_hist:
  125. kfree(hist);
  126. hist = NULL;
  127. goto out;
  128. }
  129. EXPORT_SYMBOL_GPL(dccp_rx_hist_new);
  130. void dccp_rx_hist_delete(struct dccp_rx_hist *hist)
  131. {
  132. const char* name = kmem_cache_name(hist->dccprxh_slab);
  133. kmem_cache_destroy(hist->dccprxh_slab);
  134. kfree(name);
  135. kfree(hist);
  136. }
  137. EXPORT_SYMBOL_GPL(dccp_rx_hist_delete);
  138. int dccp_rx_hist_find_entry(const struct list_head *list, const u64 seq,
  139. u8 *ccval)
  140. {
  141. struct dccp_rx_hist_entry *packet = NULL, *entry;
  142. list_for_each_entry(entry, list, dccphrx_node)
  143. if (entry->dccphrx_seqno == seq) {
  144. packet = entry;
  145. break;
  146. }
  147. if (packet)
  148. *ccval = packet->dccphrx_ccval;
  149. return packet != NULL;
  150. }
  151. EXPORT_SYMBOL_GPL(dccp_rx_hist_find_entry);
  152. struct dccp_rx_hist_entry *
  153. dccp_rx_hist_find_data_packet(const struct list_head *list)
  154. {
  155. struct dccp_rx_hist_entry *entry, *packet = NULL;
  156. list_for_each_entry(entry, list, dccphrx_node)
  157. if (entry->dccphrx_type == DCCP_PKT_DATA ||
  158. entry->dccphrx_type == DCCP_PKT_DATAACK) {
  159. packet = entry;
  160. break;
  161. }
  162. return packet;
  163. }
  164. EXPORT_SYMBOL_GPL(dccp_rx_hist_find_data_packet);
  165. void dccp_rx_hist_add_packet(struct dccp_rx_hist *hist,
  166. struct list_head *rx_list,
  167. struct list_head *li_list,
  168. struct dccp_rx_hist_entry *packet,
  169. u64 nonloss_seqno)
  170. {
  171. struct dccp_rx_hist_entry *entry, *next;
  172. u8 num_later = 0;
  173. list_add(&packet->dccphrx_node, rx_list);
  174. num_later = TFRC_RECV_NUM_LATE_LOSS + 1;
  175. if (!list_empty(li_list)) {
  176. list_for_each_entry_safe(entry, next, rx_list, dccphrx_node) {
  177. if (num_later == 0) {
  178. if (after48(nonloss_seqno,
  179. entry->dccphrx_seqno)) {
  180. list_del_init(&entry->dccphrx_node);
  181. dccp_rx_hist_entry_delete(hist, entry);
  182. }
  183. } else if (dccp_rx_hist_entry_data_packet(entry))
  184. --num_later;
  185. }
  186. } else {
  187. int step = 0;
  188. u8 win_count = 0; /* Not needed, but lets shut up gcc */
  189. int tmp;
  190. /*
  191. * We have no loss interval history so we need at least one
  192. * rtt:s of data packets to approximate rtt.
  193. */
  194. list_for_each_entry_safe(entry, next, rx_list, dccphrx_node) {
  195. if (num_later == 0) {
  196. switch (step) {
  197. case 0:
  198. step = 1;
  199. /* OK, find next data packet */
  200. num_later = 1;
  201. break;
  202. case 1:
  203. step = 2;
  204. /* OK, find next data packet */
  205. num_later = 1;
  206. win_count = entry->dccphrx_ccval;
  207. break;
  208. case 2:
  209. tmp = win_count - entry->dccphrx_ccval;
  210. if (tmp < 0)
  211. tmp += TFRC_WIN_COUNT_LIMIT;
  212. if (tmp > TFRC_WIN_COUNT_PER_RTT + 1) {
  213. /*
  214. * We have found a packet older
  215. * than one rtt remove the rest
  216. */
  217. step = 3;
  218. } else /* OK, find next data packet */
  219. num_later = 1;
  220. break;
  221. case 3:
  222. list_del_init(&entry->dccphrx_node);
  223. dccp_rx_hist_entry_delete(hist, entry);
  224. break;
  225. }
  226. } else if (dccp_rx_hist_entry_data_packet(entry))
  227. --num_later;
  228. }
  229. }
  230. }
  231. EXPORT_SYMBOL_GPL(dccp_rx_hist_add_packet);
  232. void dccp_rx_hist_purge(struct dccp_rx_hist *hist, struct list_head *list)
  233. {
  234. struct dccp_rx_hist_entry *entry, *next;
  235. list_for_each_entry_safe(entry, next, list, dccphrx_node) {
  236. list_del_init(&entry->dccphrx_node);
  237. kmem_cache_free(hist->dccprxh_slab, entry);
  238. }
  239. }
  240. EXPORT_SYMBOL_GPL(dccp_rx_hist_purge);
  241. extern int __init dccp_li_init(void);
  242. extern void dccp_li_exit(void);
  243. static __init int packet_history_init(void)
  244. {
  245. if (dccp_li_init() != 0)
  246. goto out;
  247. tfrc_tx_hist = kmem_cache_create("tfrc_tx_hist",
  248. sizeof(struct tfrc_tx_hist_entry), 0,
  249. SLAB_HWCACHE_ALIGN, NULL);
  250. if (tfrc_tx_hist == NULL)
  251. goto out_li_exit;
  252. return 0;
  253. out_li_exit:
  254. dccp_li_exit();
  255. out:
  256. return -ENOBUFS;
  257. }
  258. module_init(packet_history_init);
  259. static __exit void packet_history_exit(void)
  260. {
  261. if (tfrc_tx_hist != NULL) {
  262. kmem_cache_destroy(tfrc_tx_hist);
  263. tfrc_tx_hist = NULL;
  264. }
  265. dccp_li_exit();
  266. }
  267. module_exit(packet_history_exit);
  268. MODULE_AUTHOR("Ian McDonald <ian.mcdonald@jandi.co.nz>, "
  269. "Arnaldo Carvalho de Melo <acme@ghostprotocols.net>");
  270. MODULE_DESCRIPTION("DCCP TFRC library");
  271. MODULE_LICENSE("GPL");