packet_history.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * net/dccp/packet_history.h
  3. *
  4. * Copyright (c) 2005 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 - iam4@cs.waikato.ac.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. int 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. {
  102. struct dccp_rx_hist_entry *entry, *next, *iter;
  103. u8 num_later = 0;
  104. iter = dccp_rx_hist_head(rx_list);
  105. if (iter == NULL)
  106. dccp_rx_hist_add_entry(rx_list, packet);
  107. else {
  108. const u64 seqno = packet->dccphrx_seqno;
  109. if (after48(seqno, iter->dccphrx_seqno))
  110. dccp_rx_hist_add_entry(rx_list, packet);
  111. else {
  112. if (dccp_rx_hist_entry_data_packet(iter))
  113. num_later = 1;
  114. list_for_each_entry_continue(iter, rx_list,
  115. dccphrx_node) {
  116. if (after48(seqno, iter->dccphrx_seqno)) {
  117. dccp_rx_hist_add_entry(&iter->dccphrx_node,
  118. packet);
  119. goto trim_history;
  120. }
  121. if (dccp_rx_hist_entry_data_packet(iter))
  122. num_later++;
  123. if (num_later == TFRC_RECV_NUM_LATE_LOSS) {
  124. dccp_rx_hist_entry_delete(hist, packet);
  125. return 1;
  126. }
  127. }
  128. if (num_later < TFRC_RECV_NUM_LATE_LOSS)
  129. dccp_rx_hist_add_entry(rx_list, packet);
  130. /*
  131. * FIXME: else what? should we destroy the packet
  132. * like above?
  133. */
  134. }
  135. }
  136. trim_history:
  137. /*
  138. * Trim history (remove all packets after the NUM_LATE_LOSS + 1
  139. * data packets)
  140. */
  141. num_later = TFRC_RECV_NUM_LATE_LOSS + 1;
  142. if (!list_empty(li_list)) {
  143. list_for_each_entry_safe(entry, next, rx_list, dccphrx_node) {
  144. if (num_later == 0) {
  145. list_del_init(&entry->dccphrx_node);
  146. dccp_rx_hist_entry_delete(hist, entry);
  147. } else if (dccp_rx_hist_entry_data_packet(entry))
  148. --num_later;
  149. }
  150. } else {
  151. int step = 0;
  152. u8 win_count = 0; /* Not needed, but lets shut up gcc */
  153. int tmp;
  154. /*
  155. * We have no loss interval history so we need at least one
  156. * rtt:s of data packets to approximate rtt.
  157. */
  158. list_for_each_entry_safe(entry, next, rx_list, dccphrx_node) {
  159. if (num_later == 0) {
  160. switch (step) {
  161. case 0:
  162. step = 1;
  163. /* OK, find next data packet */
  164. num_later = 1;
  165. break;
  166. case 1:
  167. step = 2;
  168. /* OK, find next data packet */
  169. num_later = 1;
  170. win_count = entry->dccphrx_ccval;
  171. break;
  172. case 2:
  173. tmp = win_count - entry->dccphrx_ccval;
  174. if (tmp < 0)
  175. tmp += TFRC_WIN_COUNT_LIMIT;
  176. if (tmp > TFRC_WIN_COUNT_PER_RTT + 1) {
  177. /*
  178. * We have found a packet older
  179. * than one rtt remove the rest
  180. */
  181. step = 3;
  182. } else /* OK, find next data packet */
  183. num_later = 1;
  184. break;
  185. case 3:
  186. list_del_init(&entry->dccphrx_node);
  187. dccp_rx_hist_entry_delete(hist, entry);
  188. break;
  189. }
  190. } else if (dccp_rx_hist_entry_data_packet(entry))
  191. --num_later;
  192. }
  193. }
  194. return 0;
  195. }
  196. EXPORT_SYMBOL_GPL(dccp_rx_hist_add_packet);
  197. u64 dccp_rx_hist_detect_loss(struct list_head *rx_list,
  198. struct list_head *li_list, u8 *win_loss)
  199. {
  200. struct dccp_rx_hist_entry *entry, *next, *packet;
  201. struct dccp_rx_hist_entry *a_loss = NULL;
  202. struct dccp_rx_hist_entry *b_loss = NULL;
  203. u64 seq_loss = DCCP_MAX_SEQNO + 1;
  204. u8 num_later = TFRC_RECV_NUM_LATE_LOSS;
  205. list_for_each_entry_safe(entry, next, rx_list, dccphrx_node) {
  206. if (num_later == 0) {
  207. b_loss = entry;
  208. break;
  209. } else if (dccp_rx_hist_entry_data_packet(entry))
  210. --num_later;
  211. }
  212. if (b_loss == NULL)
  213. goto out;
  214. num_later = 1;
  215. list_for_each_entry_safe_continue(entry, next, rx_list, dccphrx_node) {
  216. if (num_later == 0) {
  217. a_loss = entry;
  218. break;
  219. } else if (dccp_rx_hist_entry_data_packet(entry))
  220. --num_later;
  221. }
  222. if (a_loss == NULL) {
  223. if (list_empty(li_list)) {
  224. /* no loss event have occured yet */
  225. LIMIT_NETDEBUG("%s: TODO: find a lost data packet by "
  226. "comparing to initial seqno\n",
  227. __FUNCTION__);
  228. goto out;
  229. } else {
  230. LIMIT_NETDEBUG("%s: Less than 4 data pkts in history!",
  231. __FUNCTION__);
  232. goto out;
  233. }
  234. }
  235. /* Locate a lost data packet */
  236. entry = packet = b_loss;
  237. list_for_each_entry_safe_continue(entry, next, rx_list, dccphrx_node) {
  238. u64 delta = dccp_delta_seqno(entry->dccphrx_seqno,
  239. packet->dccphrx_seqno);
  240. if (delta != 0) {
  241. if (dccp_rx_hist_entry_data_packet(packet))
  242. --delta;
  243. /*
  244. * FIXME: check this, probably this % usage is because
  245. * in earlier drafts the ndp count was just 8 bits
  246. * long, but now it cam be up to 24 bits long.
  247. */
  248. #if 0
  249. if (delta % DCCP_NDP_LIMIT !=
  250. (packet->dccphrx_ndp -
  251. entry->dccphrx_ndp) % DCCP_NDP_LIMIT)
  252. #endif
  253. if (delta != packet->dccphrx_ndp - entry->dccphrx_ndp) {
  254. seq_loss = entry->dccphrx_seqno;
  255. dccp_inc_seqno(&seq_loss);
  256. }
  257. }
  258. packet = entry;
  259. if (packet == a_loss)
  260. break;
  261. }
  262. out:
  263. if (seq_loss != DCCP_MAX_SEQNO + 1)
  264. *win_loss = a_loss->dccphrx_ccval;
  265. else
  266. *win_loss = 0; /* Paranoia */
  267. return seq_loss;
  268. }
  269. EXPORT_SYMBOL_GPL(dccp_rx_hist_detect_loss);
  270. struct dccp_tx_hist *dccp_tx_hist_new(const char *name)
  271. {
  272. struct dccp_tx_hist *hist = kmalloc(sizeof(*hist), GFP_ATOMIC);
  273. static const char dccp_tx_hist_mask[] = "tx_hist_%s";
  274. char *slab_name;
  275. if (hist == NULL)
  276. goto out;
  277. slab_name = kmalloc(strlen(name) + sizeof(dccp_tx_hist_mask) - 1,
  278. GFP_ATOMIC);
  279. if (slab_name == NULL)
  280. goto out_free_hist;
  281. sprintf(slab_name, dccp_tx_hist_mask, name);
  282. hist->dccptxh_slab = kmem_cache_create(slab_name,
  283. sizeof(struct dccp_tx_hist_entry),
  284. 0, SLAB_HWCACHE_ALIGN,
  285. NULL, NULL);
  286. if (hist->dccptxh_slab == NULL)
  287. goto out_free_slab_name;
  288. out:
  289. return hist;
  290. out_free_slab_name:
  291. kfree(slab_name);
  292. out_free_hist:
  293. kfree(hist);
  294. hist = NULL;
  295. goto out;
  296. }
  297. EXPORT_SYMBOL_GPL(dccp_tx_hist_new);
  298. void dccp_tx_hist_delete(struct dccp_tx_hist *hist)
  299. {
  300. const char* name = kmem_cache_name(hist->dccptxh_slab);
  301. kmem_cache_destroy(hist->dccptxh_slab);
  302. kfree(name);
  303. kfree(hist);
  304. }
  305. EXPORT_SYMBOL_GPL(dccp_tx_hist_delete);
  306. struct dccp_tx_hist_entry *
  307. dccp_tx_hist_find_entry(const struct list_head *list, const u64 seq)
  308. {
  309. struct dccp_tx_hist_entry *packet = NULL, *entry;
  310. list_for_each_entry(entry, list, dccphtx_node)
  311. if (entry->dccphtx_seqno == seq) {
  312. packet = entry;
  313. break;
  314. }
  315. return packet;
  316. }
  317. EXPORT_SYMBOL_GPL(dccp_tx_hist_find_entry);
  318. void dccp_tx_hist_purge_older(struct dccp_tx_hist *hist,
  319. struct list_head *list,
  320. struct dccp_tx_hist_entry *packet)
  321. {
  322. struct dccp_tx_hist_entry *next;
  323. list_for_each_entry_safe_continue(packet, next, list, dccphtx_node) {
  324. list_del_init(&packet->dccphtx_node);
  325. dccp_tx_hist_entry_delete(hist, packet);
  326. }
  327. }
  328. EXPORT_SYMBOL_GPL(dccp_tx_hist_purge_older);
  329. void dccp_tx_hist_purge(struct dccp_tx_hist *hist, struct list_head *list)
  330. {
  331. struct dccp_tx_hist_entry *entry, *next;
  332. list_for_each_entry_safe(entry, next, list, dccphtx_node) {
  333. list_del_init(&entry->dccphtx_node);
  334. dccp_tx_hist_entry_delete(hist, entry);
  335. }
  336. }
  337. EXPORT_SYMBOL_GPL(dccp_tx_hist_purge);
  338. MODULE_AUTHOR("Ian McDonald <iam4@cs.waikato.ac.nz>, "
  339. "Arnaldo Carvalho de Melo <acme@ghostprotocols.net>");
  340. MODULE_DESCRIPTION("DCCP TFRC library");
  341. MODULE_LICENSE("GPL");