packet_history.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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/string.h>
  38. #include <linux/slab.h>
  39. #include "packet_history.h"
  40. #include "../../dccp.h"
  41. /**
  42. * tfrc_tx_hist_entry - Simple singly-linked TX history list
  43. * @next: next oldest entry (LIFO order)
  44. * @seqno: sequence number of this entry
  45. * @stamp: send time of packet with sequence number @seqno
  46. */
  47. struct tfrc_tx_hist_entry {
  48. struct tfrc_tx_hist_entry *next;
  49. u64 seqno;
  50. ktime_t stamp;
  51. };
  52. /*
  53. * Transmitter History Routines
  54. */
  55. static struct kmem_cache *tfrc_tx_hist_slab;
  56. static struct tfrc_tx_hist_entry *
  57. tfrc_tx_hist_find_entry(struct tfrc_tx_hist_entry *head, u64 seqno)
  58. {
  59. while (head != NULL && head->seqno != seqno)
  60. head = head->next;
  61. return head;
  62. }
  63. int tfrc_tx_hist_add(struct tfrc_tx_hist_entry **headp, u64 seqno)
  64. {
  65. struct tfrc_tx_hist_entry *entry = kmem_cache_alloc(tfrc_tx_hist_slab, gfp_any());
  66. if (entry == NULL)
  67. return -ENOBUFS;
  68. entry->seqno = seqno;
  69. entry->stamp = ktime_get_real();
  70. entry->next = *headp;
  71. *headp = entry;
  72. return 0;
  73. }
  74. EXPORT_SYMBOL_GPL(tfrc_tx_hist_add);
  75. void tfrc_tx_hist_purge(struct tfrc_tx_hist_entry **headp)
  76. {
  77. struct tfrc_tx_hist_entry *head = *headp;
  78. while (head != NULL) {
  79. struct tfrc_tx_hist_entry *next = head->next;
  80. kmem_cache_free(tfrc_tx_hist_slab, head);
  81. head = next;
  82. }
  83. *headp = NULL;
  84. }
  85. EXPORT_SYMBOL_GPL(tfrc_tx_hist_purge);
  86. u32 tfrc_tx_hist_rtt(struct tfrc_tx_hist_entry *head, const u64 seqno,
  87. const ktime_t now)
  88. {
  89. u32 rtt = 0;
  90. struct tfrc_tx_hist_entry *packet = tfrc_tx_hist_find_entry(head, seqno);
  91. if (packet != NULL) {
  92. rtt = ktime_us_delta(now, packet->stamp);
  93. /*
  94. * Garbage-collect older (irrelevant) entries:
  95. */
  96. tfrc_tx_hist_purge(&packet->next);
  97. }
  98. return rtt;
  99. }
  100. EXPORT_SYMBOL_GPL(tfrc_tx_hist_rtt);
  101. /**
  102. * tfrc_rx_hist_index - index to reach n-th entry after loss_start
  103. */
  104. static inline u8 tfrc_rx_hist_index(const struct tfrc_rx_hist *h, const u8 n)
  105. {
  106. return (h->loss_start + n) & TFRC_NDUPACK;
  107. }
  108. /**
  109. * tfrc_rx_hist_last_rcv - entry with highest-received-seqno so far
  110. */
  111. static inline struct tfrc_rx_hist_entry *
  112. tfrc_rx_hist_last_rcv(const struct tfrc_rx_hist *h)
  113. {
  114. return h->ring[tfrc_rx_hist_index(h, h->loss_count)];
  115. }
  116. /*
  117. * Receiver History Routines
  118. */
  119. static struct kmem_cache *tfrc_rx_hist_slab;
  120. void tfrc_rx_hist_add_packet(struct tfrc_rx_hist *h,
  121. const struct sk_buff *skb,
  122. const u32 ndp)
  123. {
  124. struct tfrc_rx_hist_entry *entry = tfrc_rx_hist_last_rcv(h);
  125. const struct dccp_hdr *dh = dccp_hdr(skb);
  126. entry->tfrchrx_seqno = DCCP_SKB_CB(skb)->dccpd_seq;
  127. entry->tfrchrx_ccval = dh->dccph_ccval;
  128. entry->tfrchrx_type = dh->dccph_type;
  129. entry->tfrchrx_ndp = ndp;
  130. entry->tfrchrx_tstamp = ktime_get_real();
  131. }
  132. EXPORT_SYMBOL_GPL(tfrc_rx_hist_add_packet);
  133. static inline void tfrc_rx_hist_entry_delete(struct tfrc_rx_hist_entry *entry)
  134. {
  135. kmem_cache_free(tfrc_rx_hist_slab, entry);
  136. }
  137. /**
  138. * tfrc_rx_hist_entry - return the n-th history entry after loss_start
  139. */
  140. static inline struct tfrc_rx_hist_entry *
  141. tfrc_rx_hist_entry(const struct tfrc_rx_hist *h, const u8 n)
  142. {
  143. return h->ring[tfrc_rx_hist_index(h, n)];
  144. }
  145. /**
  146. * tfrc_rx_hist_loss_prev - entry with highest-received-seqno before loss was detected
  147. */
  148. static inline struct tfrc_rx_hist_entry *
  149. tfrc_rx_hist_loss_prev(const struct tfrc_rx_hist *h)
  150. {
  151. return h->ring[h->loss_start];
  152. }
  153. /* has the packet contained in skb been seen before? */
  154. int tfrc_rx_hist_duplicate(struct tfrc_rx_hist *h, struct sk_buff *skb)
  155. {
  156. const u64 seq = DCCP_SKB_CB(skb)->dccpd_seq;
  157. int i;
  158. if (dccp_delta_seqno(tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno, seq) <= 0)
  159. return 1;
  160. for (i = 1; i <= h->loss_count; i++)
  161. if (tfrc_rx_hist_entry(h, i)->tfrchrx_seqno == seq)
  162. return 1;
  163. return 0;
  164. }
  165. EXPORT_SYMBOL_GPL(tfrc_rx_hist_duplicate);
  166. /* initialise loss detection and disable RTT sampling */
  167. static inline void tfrc_rx_hist_loss_indicated(struct tfrc_rx_hist *h)
  168. {
  169. h->loss_count = 1;
  170. }
  171. /* indicate whether previously a packet was detected missing */
  172. static inline int tfrc_rx_hist_loss_pending(const struct tfrc_rx_hist *h)
  173. {
  174. return h->loss_count;
  175. }
  176. /* any data packets missing between last reception and skb ? */
  177. int tfrc_rx_hist_new_loss_indicated(struct tfrc_rx_hist *h,
  178. const struct sk_buff *skb, u32 ndp)
  179. {
  180. int delta = dccp_delta_seqno(tfrc_rx_hist_last_rcv(h)->tfrchrx_seqno,
  181. DCCP_SKB_CB(skb)->dccpd_seq);
  182. if (delta > 1 && ndp < delta)
  183. tfrc_rx_hist_loss_indicated(h);
  184. return tfrc_rx_hist_loss_pending(h);
  185. }
  186. EXPORT_SYMBOL_GPL(tfrc_rx_hist_new_loss_indicated);
  187. int tfrc_rx_hist_alloc(struct tfrc_rx_hist *h)
  188. {
  189. int i;
  190. for (i = 0; i <= TFRC_NDUPACK; i++) {
  191. h->ring[i] = kmem_cache_alloc(tfrc_rx_hist_slab, GFP_ATOMIC);
  192. if (h->ring[i] == NULL)
  193. goto out_free;
  194. }
  195. h->loss_count = h->loss_start = 0;
  196. return 0;
  197. out_free:
  198. while (i-- != 0) {
  199. kmem_cache_free(tfrc_rx_hist_slab, h->ring[i]);
  200. h->ring[i] = NULL;
  201. }
  202. return -ENOBUFS;
  203. }
  204. EXPORT_SYMBOL_GPL(tfrc_rx_hist_alloc);
  205. void tfrc_rx_hist_purge(struct tfrc_rx_hist *h)
  206. {
  207. int i;
  208. for (i = 0; i <= TFRC_NDUPACK; ++i)
  209. if (h->ring[i] != NULL) {
  210. kmem_cache_free(tfrc_rx_hist_slab, h->ring[i]);
  211. h->ring[i] = NULL;
  212. }
  213. }
  214. EXPORT_SYMBOL_GPL(tfrc_rx_hist_purge);
  215. /**
  216. * tfrc_rx_hist_rtt_last_s - reference entry to compute RTT samples against
  217. */
  218. static inline struct tfrc_rx_hist_entry *
  219. tfrc_rx_hist_rtt_last_s(const struct tfrc_rx_hist *h)
  220. {
  221. return h->ring[0];
  222. }
  223. /**
  224. * tfrc_rx_hist_rtt_prev_s: previously suitable (wrt rtt_last_s) RTT-sampling entry
  225. */
  226. static inline struct tfrc_rx_hist_entry *
  227. tfrc_rx_hist_rtt_prev_s(const struct tfrc_rx_hist *h)
  228. {
  229. return h->ring[h->rtt_sample_prev];
  230. }
  231. /**
  232. * tfrc_rx_hist_sample_rtt - Sample RTT from timestamp / CCVal
  233. * Based on ideas presented in RFC 4342, 8.1. Returns 0 if it was not able
  234. * to compute a sample with given data - calling function should check this.
  235. */
  236. u32 tfrc_rx_hist_sample_rtt(struct tfrc_rx_hist *h, const struct sk_buff *skb)
  237. {
  238. u32 sample = 0,
  239. delta_v = SUB16(dccp_hdr(skb)->dccph_ccval,
  240. tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
  241. if (delta_v < 1 || delta_v > 4) { /* unsuitable CCVal delta */
  242. if (h->rtt_sample_prev == 2) { /* previous candidate stored */
  243. sample = SUB16(tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_ccval,
  244. tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
  245. if (sample)
  246. sample = 4 / sample *
  247. ktime_us_delta(tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_tstamp,
  248. tfrc_rx_hist_rtt_last_s(h)->tfrchrx_tstamp);
  249. else /*
  250. * FIXME: This condition is in principle not
  251. * possible but occurs when CCID is used for
  252. * two-way data traffic. I have tried to trace
  253. * it, but the cause does not seem to be here.
  254. */
  255. DCCP_BUG("please report to dccp@vger.kernel.org"
  256. " => prev = %u, last = %u",
  257. tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_ccval,
  258. tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
  259. } else if (delta_v < 1) {
  260. h->rtt_sample_prev = 1;
  261. goto keep_ref_for_next_time;
  262. }
  263. } else if (delta_v == 4) /* optimal match */
  264. sample = ktime_to_us(net_timedelta(tfrc_rx_hist_rtt_last_s(h)->tfrchrx_tstamp));
  265. else { /* suboptimal match */
  266. h->rtt_sample_prev = 2;
  267. goto keep_ref_for_next_time;
  268. }
  269. if (unlikely(sample > DCCP_SANE_RTT_MAX)) {
  270. DCCP_WARN("RTT sample %u too large, using max\n", sample);
  271. sample = DCCP_SANE_RTT_MAX;
  272. }
  273. h->rtt_sample_prev = 0; /* use current entry as next reference */
  274. keep_ref_for_next_time:
  275. return sample;
  276. }
  277. EXPORT_SYMBOL_GPL(tfrc_rx_hist_sample_rtt);
  278. __init int packet_history_init(void)
  279. {
  280. tfrc_tx_hist_slab = kmem_cache_create("tfrc_tx_hist",
  281. sizeof(struct tfrc_tx_hist_entry), 0,
  282. SLAB_HWCACHE_ALIGN, NULL);
  283. if (tfrc_tx_hist_slab == NULL)
  284. goto out_err;
  285. tfrc_rx_hist_slab = kmem_cache_create("tfrc_rx_hist",
  286. sizeof(struct tfrc_rx_hist_entry), 0,
  287. SLAB_HWCACHE_ALIGN, NULL);
  288. if (tfrc_rx_hist_slab == NULL)
  289. goto out_free_tx;
  290. return 0;
  291. out_free_tx:
  292. kmem_cache_destroy(tfrc_tx_hist_slab);
  293. tfrc_tx_hist_slab = NULL;
  294. out_err:
  295. return -ENOBUFS;
  296. }
  297. void packet_history_exit(void)
  298. {
  299. if (tfrc_tx_hist_slab != NULL) {
  300. kmem_cache_destroy(tfrc_tx_hist_slab);
  301. tfrc_tx_hist_slab = NULL;
  302. }
  303. if (tfrc_rx_hist_slab != NULL) {
  304. kmem_cache_destroy(tfrc_rx_hist_slab);
  305. tfrc_rx_hist_slab = NULL;
  306. }
  307. }