packet_history.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Copyright (c) 2007 The University of Aberdeen, Scotland, UK
  3. * Copyright (c) 2005-7 The University of Waikato, Hamilton, New Zealand.
  4. *
  5. * An implementation of the DCCP protocol
  6. *
  7. * This code has been developed by the University of Waikato WAND
  8. * research group. For further information please see http://www.wand.net.nz/
  9. * or e-mail Ian McDonald - ian.mcdonald@jandi.co.nz
  10. *
  11. * This code also uses code from Lulea University, rereleased as GPL by its
  12. * authors:
  13. * Copyright (c) 2003 Nils-Erik Mattsson, Joacim Haggmark, Magnus Erixzon
  14. *
  15. * Changes to meet Linux coding standards, to make it meet latest ccid3 draft
  16. * and to make it work as a loadable module in the DCCP stack written by
  17. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>.
  18. *
  19. * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  20. *
  21. * This program is free software; you can redistribute it and/or modify
  22. * it under the terms of the GNU General Public License as published by
  23. * the Free Software Foundation; either version 2 of the License, or
  24. * (at your option) any later version.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU General Public License
  32. * along with this program; if not, write to the Free Software
  33. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  34. */
  35. #include <linux/string.h>
  36. #include <linux/slab.h>
  37. #include "packet_history.h"
  38. #include "../../dccp.h"
  39. /*
  40. * Transmitter History Routines
  41. */
  42. static struct kmem_cache *tfrc_tx_hist_slab;
  43. int __init tfrc_tx_packet_history_init(void)
  44. {
  45. tfrc_tx_hist_slab = kmem_cache_create("tfrc_tx_hist",
  46. sizeof(struct tfrc_tx_hist_entry),
  47. 0, SLAB_HWCACHE_ALIGN, NULL);
  48. return tfrc_tx_hist_slab == NULL ? -ENOBUFS : 0;
  49. }
  50. void tfrc_tx_packet_history_exit(void)
  51. {
  52. if (tfrc_tx_hist_slab != NULL) {
  53. kmem_cache_destroy(tfrc_tx_hist_slab);
  54. tfrc_tx_hist_slab = NULL;
  55. }
  56. }
  57. int tfrc_tx_hist_add(struct tfrc_tx_hist_entry **headp, u64 seqno)
  58. {
  59. struct tfrc_tx_hist_entry *entry = kmem_cache_alloc(tfrc_tx_hist_slab, gfp_any());
  60. if (entry == NULL)
  61. return -ENOBUFS;
  62. entry->seqno = seqno;
  63. entry->stamp = ktime_get_real();
  64. entry->next = *headp;
  65. *headp = entry;
  66. return 0;
  67. }
  68. void tfrc_tx_hist_purge(struct tfrc_tx_hist_entry **headp)
  69. {
  70. struct tfrc_tx_hist_entry *head = *headp;
  71. while (head != NULL) {
  72. struct tfrc_tx_hist_entry *next = head->next;
  73. kmem_cache_free(tfrc_tx_hist_slab, head);
  74. head = next;
  75. }
  76. *headp = NULL;
  77. }
  78. /*
  79. * Receiver History Routines
  80. */
  81. static struct kmem_cache *tfrc_rx_hist_slab;
  82. int __init tfrc_rx_packet_history_init(void)
  83. {
  84. tfrc_rx_hist_slab = kmem_cache_create("tfrc_rxh_cache",
  85. sizeof(struct tfrc_rx_hist_entry),
  86. 0, SLAB_HWCACHE_ALIGN, NULL);
  87. return tfrc_rx_hist_slab == NULL ? -ENOBUFS : 0;
  88. }
  89. void tfrc_rx_packet_history_exit(void)
  90. {
  91. if (tfrc_rx_hist_slab != NULL) {
  92. kmem_cache_destroy(tfrc_rx_hist_slab);
  93. tfrc_rx_hist_slab = NULL;
  94. }
  95. }
  96. static inline void tfrc_rx_hist_entry_from_skb(struct tfrc_rx_hist_entry *entry,
  97. const struct sk_buff *skb,
  98. const u64 ndp)
  99. {
  100. const struct dccp_hdr *dh = dccp_hdr(skb);
  101. entry->tfrchrx_seqno = DCCP_SKB_CB(skb)->dccpd_seq;
  102. entry->tfrchrx_ccval = dh->dccph_ccval;
  103. entry->tfrchrx_type = dh->dccph_type;
  104. entry->tfrchrx_ndp = ndp;
  105. entry->tfrchrx_tstamp = ktime_get_real();
  106. }
  107. void tfrc_rx_hist_add_packet(struct tfrc_rx_hist *h,
  108. const struct sk_buff *skb,
  109. const u64 ndp)
  110. {
  111. struct tfrc_rx_hist_entry *entry = tfrc_rx_hist_last_rcv(h);
  112. tfrc_rx_hist_entry_from_skb(entry, skb, ndp);
  113. }
  114. /* has the packet contained in skb been seen before? */
  115. int tfrc_rx_hist_duplicate(struct tfrc_rx_hist *h, struct sk_buff *skb)
  116. {
  117. const u64 seq = DCCP_SKB_CB(skb)->dccpd_seq;
  118. int i;
  119. if (dccp_delta_seqno(tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno, seq) <= 0)
  120. return 1;
  121. for (i = 1; i <= h->loss_count; i++)
  122. if (tfrc_rx_hist_entry(h, i)->tfrchrx_seqno == seq)
  123. return 1;
  124. return 0;
  125. }
  126. static void tfrc_rx_hist_swap(struct tfrc_rx_hist *h, const u8 a, const u8 b)
  127. {
  128. const u8 idx_a = tfrc_rx_hist_index(h, a),
  129. idx_b = tfrc_rx_hist_index(h, b);
  130. struct tfrc_rx_hist_entry *tmp = h->ring[idx_a];
  131. h->ring[idx_a] = h->ring[idx_b];
  132. h->ring[idx_b] = tmp;
  133. }
  134. /*
  135. * Private helper functions for loss detection.
  136. *
  137. * In the descriptions, `Si' refers to the sequence number of entry number i,
  138. * whose NDP count is `Ni' (lower case is used for variables).
  139. * Note: All __xxx_loss functions expect that a test against duplicates has been
  140. * performed already: the seqno of the skb must not be less than the seqno
  141. * of loss_prev; and it must not equal that of any valid history entry.
  142. */
  143. static void __do_track_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u64 n1)
  144. {
  145. u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,
  146. s1 = DCCP_SKB_CB(skb)->dccpd_seq;
  147. if (!dccp_loss_free(s0, s1, n1)) { /* gap between S0 and S1 */
  148. h->loss_count = 1;
  149. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n1);
  150. }
  151. }
  152. static void __one_after_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u32 n2)
  153. {
  154. u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,
  155. s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,
  156. s2 = DCCP_SKB_CB(skb)->dccpd_seq;
  157. if (likely(dccp_delta_seqno(s1, s2) > 0)) { /* S1 < S2 */
  158. h->loss_count = 2;
  159. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 2), skb, n2);
  160. return;
  161. }
  162. /* S0 < S2 < S1 */
  163. if (dccp_loss_free(s0, s2, n2)) {
  164. u64 n1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_ndp;
  165. if (dccp_loss_free(s2, s1, n1)) {
  166. /* hole is filled: S0, S2, and S1 are consecutive */
  167. h->loss_count = 0;
  168. h->loss_start = tfrc_rx_hist_index(h, 1);
  169. } else
  170. /* gap between S2 and S1: just update loss_prev */
  171. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_loss_prev(h), skb, n2);
  172. } else { /* gap between S0 and S2 */
  173. /*
  174. * Reorder history to insert S2 between S0 and S1
  175. */
  176. tfrc_rx_hist_swap(h, 0, 3);
  177. h->loss_start = tfrc_rx_hist_index(h, 3);
  178. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n2);
  179. h->loss_count = 2;
  180. }
  181. }
  182. /* return 1 if a new loss event has been identified */
  183. static int __two_after_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u32 n3)
  184. {
  185. u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,
  186. s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,
  187. s2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_seqno,
  188. s3 = DCCP_SKB_CB(skb)->dccpd_seq;
  189. if (likely(dccp_delta_seqno(s2, s3) > 0)) { /* S2 < S3 */
  190. h->loss_count = 3;
  191. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 3), skb, n3);
  192. return 1;
  193. }
  194. /* S3 < S2 */
  195. if (dccp_delta_seqno(s1, s3) > 0) { /* S1 < S3 < S2 */
  196. /*
  197. * Reorder history to insert S3 between S1 and S2
  198. */
  199. tfrc_rx_hist_swap(h, 2, 3);
  200. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 2), skb, n3);
  201. h->loss_count = 3;
  202. return 1;
  203. }
  204. /* S0 < S3 < S1 */
  205. if (dccp_loss_free(s0, s3, n3)) {
  206. u64 n1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_ndp;
  207. if (dccp_loss_free(s3, s1, n1)) {
  208. /* hole between S0 and S1 filled by S3 */
  209. u64 n2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_ndp;
  210. if (dccp_loss_free(s1, s2, n2)) {
  211. /* entire hole filled by S0, S3, S1, S2 */
  212. h->loss_start = tfrc_rx_hist_index(h, 2);
  213. h->loss_count = 0;
  214. } else {
  215. /* gap remains between S1 and S2 */
  216. h->loss_start = tfrc_rx_hist_index(h, 1);
  217. h->loss_count = 1;
  218. }
  219. } else /* gap exists between S3 and S1, loss_count stays at 2 */
  220. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_loss_prev(h), skb, n3);
  221. return 0;
  222. }
  223. /*
  224. * The remaining case: S0 < S3 < S1 < S2; gap between S0 and S3
  225. * Reorder history to insert S3 between S0 and S1.
  226. */
  227. tfrc_rx_hist_swap(h, 0, 3);
  228. h->loss_start = tfrc_rx_hist_index(h, 3);
  229. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n3);
  230. h->loss_count = 3;
  231. return 1;
  232. }
  233. /* recycle RX history records to continue loss detection if necessary */
  234. static void __three_after_loss(struct tfrc_rx_hist *h)
  235. {
  236. /*
  237. * At this stage we know already that there is a gap between S0 and S1
  238. * (since S0 was the highest sequence number received before detecting
  239. * the loss). To recycle the loss record, it is thus only necessary to
  240. * check for other possible gaps between S1/S2 and between S2/S3.
  241. */
  242. u64 s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,
  243. s2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_seqno,
  244. s3 = tfrc_rx_hist_entry(h, 3)->tfrchrx_seqno;
  245. u64 n2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_ndp,
  246. n3 = tfrc_rx_hist_entry(h, 3)->tfrchrx_ndp;
  247. if (dccp_loss_free(s1, s2, n2)) {
  248. if (dccp_loss_free(s2, s3, n3)) {
  249. /* no gap between S2 and S3: entire hole is filled */
  250. h->loss_start = tfrc_rx_hist_index(h, 3);
  251. h->loss_count = 0;
  252. } else {
  253. /* gap between S2 and S3 */
  254. h->loss_start = tfrc_rx_hist_index(h, 2);
  255. h->loss_count = 1;
  256. }
  257. } else { /* gap between S1 and S2 */
  258. h->loss_start = tfrc_rx_hist_index(h, 1);
  259. h->loss_count = 2;
  260. }
  261. }
  262. /**
  263. * tfrc_rx_handle_loss - Loss detection and further processing
  264. * @h: The non-empty RX history object
  265. * @lh: Loss Intervals database to update
  266. * @skb: Currently received packet
  267. * @ndp: The NDP count belonging to @skb
  268. * @calc_first_li: Caller-dependent computation of first loss interval in @lh
  269. * @sk: Used by @calc_first_li (see tfrc_lh_interval_add)
  270. *
  271. * Chooses action according to pending loss, updates LI database when a new
  272. * loss was detected, and does required post-processing. Returns 1 when caller
  273. * should send feedback, 0 otherwise.
  274. * Since it also takes care of reordering during loss detection and updates the
  275. * records accordingly, the caller should not perform any more RX history
  276. * operations when loss_count is greater than 0 after calling this function.
  277. */
  278. int tfrc_rx_handle_loss(struct tfrc_rx_hist *h,
  279. struct tfrc_loss_hist *lh,
  280. struct sk_buff *skb, const u64 ndp,
  281. u32 (*calc_first_li)(struct sock *), struct sock *sk)
  282. {
  283. int is_new_loss = 0;
  284. if (h->loss_count == 0) {
  285. __do_track_loss(h, skb, ndp);
  286. } else if (h->loss_count == 1) {
  287. __one_after_loss(h, skb, ndp);
  288. } else if (h->loss_count != 2) {
  289. DCCP_BUG("invalid loss_count %d", h->loss_count);
  290. } else if (__two_after_loss(h, skb, ndp)) {
  291. /*
  292. * Update Loss Interval database and recycle RX records
  293. */
  294. is_new_loss = tfrc_lh_interval_add(lh, h, calc_first_li, sk);
  295. __three_after_loss(h);
  296. }
  297. return is_new_loss;
  298. }
  299. int tfrc_rx_hist_alloc(struct tfrc_rx_hist *h)
  300. {
  301. int i;
  302. for (i = 0; i <= TFRC_NDUPACK; i++) {
  303. h->ring[i] = kmem_cache_alloc(tfrc_rx_hist_slab, GFP_ATOMIC);
  304. if (h->ring[i] == NULL)
  305. goto out_free;
  306. }
  307. h->loss_count = h->loss_start = 0;
  308. return 0;
  309. out_free:
  310. while (i-- != 0) {
  311. kmem_cache_free(tfrc_rx_hist_slab, h->ring[i]);
  312. h->ring[i] = NULL;
  313. }
  314. return -ENOBUFS;
  315. }
  316. void tfrc_rx_hist_purge(struct tfrc_rx_hist *h)
  317. {
  318. int i;
  319. for (i = 0; i <= TFRC_NDUPACK; ++i)
  320. if (h->ring[i] != NULL) {
  321. kmem_cache_free(tfrc_rx_hist_slab, h->ring[i]);
  322. h->ring[i] = NULL;
  323. }
  324. }
  325. /**
  326. * tfrc_rx_hist_rtt_last_s - reference entry to compute RTT samples against
  327. */
  328. static inline struct tfrc_rx_hist_entry *
  329. tfrc_rx_hist_rtt_last_s(const struct tfrc_rx_hist *h)
  330. {
  331. return h->ring[0];
  332. }
  333. /**
  334. * tfrc_rx_hist_rtt_prev_s - previously suitable (wrt rtt_last_s) RTT-sampling entry
  335. */
  336. static inline struct tfrc_rx_hist_entry *
  337. tfrc_rx_hist_rtt_prev_s(const struct tfrc_rx_hist *h)
  338. {
  339. return h->ring[h->rtt_sample_prev];
  340. }
  341. /**
  342. * tfrc_rx_hist_sample_rtt - Sample RTT from timestamp / CCVal
  343. * Based on ideas presented in RFC 4342, 8.1. Returns 0 if it was not able
  344. * to compute a sample with given data - calling function should check this.
  345. */
  346. u32 tfrc_rx_hist_sample_rtt(struct tfrc_rx_hist *h, const struct sk_buff *skb)
  347. {
  348. u32 sample = 0,
  349. delta_v = SUB16(dccp_hdr(skb)->dccph_ccval,
  350. tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
  351. if (delta_v < 1 || delta_v > 4) { /* unsuitable CCVal delta */
  352. if (h->rtt_sample_prev == 2) { /* previous candidate stored */
  353. sample = SUB16(tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_ccval,
  354. tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
  355. if (sample)
  356. sample = 4 / sample *
  357. ktime_us_delta(tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_tstamp,
  358. tfrc_rx_hist_rtt_last_s(h)->tfrchrx_tstamp);
  359. else /*
  360. * FIXME: This condition is in principle not
  361. * possible but occurs when CCID is used for
  362. * two-way data traffic. I have tried to trace
  363. * it, but the cause does not seem to be here.
  364. */
  365. DCCP_BUG("please report to dccp@vger.kernel.org"
  366. " => prev = %u, last = %u",
  367. tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_ccval,
  368. tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
  369. } else if (delta_v < 1) {
  370. h->rtt_sample_prev = 1;
  371. goto keep_ref_for_next_time;
  372. }
  373. } else if (delta_v == 4) /* optimal match */
  374. sample = ktime_to_us(net_timedelta(tfrc_rx_hist_rtt_last_s(h)->tfrchrx_tstamp));
  375. else { /* suboptimal match */
  376. h->rtt_sample_prev = 2;
  377. goto keep_ref_for_next_time;
  378. }
  379. if (unlikely(sample > DCCP_SANE_RTT_MAX)) {
  380. DCCP_WARN("RTT sample %u too large, using max\n", sample);
  381. sample = DCCP_SANE_RTT_MAX;
  382. }
  383. h->rtt_sample_prev = 0; /* use current entry as next reference */
  384. keep_ref_for_next_time:
  385. return sample;
  386. }