packet_history.c 10 KB

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