packet_history.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. int __init tfrc_tx_packet_history_init(void)
  57. {
  58. tfrc_tx_hist_slab = kmem_cache_create("tfrc_tx_hist",
  59. sizeof(struct tfrc_tx_hist_entry),
  60. 0, SLAB_HWCACHE_ALIGN, NULL);
  61. return tfrc_tx_hist_slab == NULL ? -ENOBUFS : 0;
  62. }
  63. void tfrc_tx_packet_history_exit(void)
  64. {
  65. if (tfrc_tx_hist_slab != NULL) {
  66. kmem_cache_destroy(tfrc_tx_hist_slab);
  67. tfrc_tx_hist_slab = NULL;
  68. }
  69. }
  70. static struct tfrc_tx_hist_entry *
  71. tfrc_tx_hist_find_entry(struct tfrc_tx_hist_entry *head, u64 seqno)
  72. {
  73. while (head != NULL && head->seqno != seqno)
  74. head = head->next;
  75. return head;
  76. }
  77. int tfrc_tx_hist_add(struct tfrc_tx_hist_entry **headp, u64 seqno)
  78. {
  79. struct tfrc_tx_hist_entry *entry = kmem_cache_alloc(tfrc_tx_hist_slab, gfp_any());
  80. if (entry == NULL)
  81. return -ENOBUFS;
  82. entry->seqno = seqno;
  83. entry->stamp = ktime_get_real();
  84. entry->next = *headp;
  85. *headp = entry;
  86. return 0;
  87. }
  88. EXPORT_SYMBOL_GPL(tfrc_tx_hist_add);
  89. void tfrc_tx_hist_purge(struct tfrc_tx_hist_entry **headp)
  90. {
  91. struct tfrc_tx_hist_entry *head = *headp;
  92. while (head != NULL) {
  93. struct tfrc_tx_hist_entry *next = head->next;
  94. kmem_cache_free(tfrc_tx_hist_slab, head);
  95. head = next;
  96. }
  97. *headp = NULL;
  98. }
  99. EXPORT_SYMBOL_GPL(tfrc_tx_hist_purge);
  100. u32 tfrc_tx_hist_rtt(struct tfrc_tx_hist_entry *head, const u64 seqno,
  101. const ktime_t now)
  102. {
  103. u32 rtt = 0;
  104. struct tfrc_tx_hist_entry *packet = tfrc_tx_hist_find_entry(head, seqno);
  105. if (packet != NULL) {
  106. rtt = ktime_us_delta(now, packet->stamp);
  107. /*
  108. * Garbage-collect older (irrelevant) entries:
  109. */
  110. tfrc_tx_hist_purge(&packet->next);
  111. }
  112. return rtt;
  113. }
  114. EXPORT_SYMBOL_GPL(tfrc_tx_hist_rtt);
  115. /*
  116. * Receiver History Routines
  117. */
  118. static struct kmem_cache *tfrc_rx_hist_slab;
  119. int __init tfrc_rx_packet_history_init(void)
  120. {
  121. tfrc_rx_hist_slab = kmem_cache_create("tfrc_rxh_cache",
  122. sizeof(struct tfrc_rx_hist_entry),
  123. 0, SLAB_HWCACHE_ALIGN, NULL);
  124. return tfrc_rx_hist_slab == NULL ? -ENOBUFS : 0;
  125. }
  126. void tfrc_rx_packet_history_exit(void)
  127. {
  128. if (tfrc_rx_hist_slab != NULL) {
  129. kmem_cache_destroy(tfrc_rx_hist_slab);
  130. tfrc_rx_hist_slab = NULL;
  131. }
  132. }
  133. static inline void tfrc_rx_hist_entry_from_skb(struct tfrc_rx_hist_entry *entry,
  134. const struct sk_buff *skb,
  135. const u64 ndp)
  136. {
  137. const struct dccp_hdr *dh = dccp_hdr(skb);
  138. entry->tfrchrx_seqno = DCCP_SKB_CB(skb)->dccpd_seq;
  139. entry->tfrchrx_ccval = dh->dccph_ccval;
  140. entry->tfrchrx_type = dh->dccph_type;
  141. entry->tfrchrx_ndp = ndp;
  142. entry->tfrchrx_tstamp = ktime_get_real();
  143. }
  144. void tfrc_rx_hist_add_packet(struct tfrc_rx_hist *h,
  145. const struct sk_buff *skb,
  146. const u64 ndp)
  147. {
  148. struct tfrc_rx_hist_entry *entry = tfrc_rx_hist_last_rcv(h);
  149. tfrc_rx_hist_entry_from_skb(entry, skb, ndp);
  150. }
  151. EXPORT_SYMBOL_GPL(tfrc_rx_hist_add_packet);
  152. /* has the packet contained in skb been seen before? */
  153. int tfrc_rx_hist_duplicate(struct tfrc_rx_hist *h, struct sk_buff *skb)
  154. {
  155. const u64 seq = DCCP_SKB_CB(skb)->dccpd_seq;
  156. int i;
  157. if (dccp_delta_seqno(tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno, seq) <= 0)
  158. return 1;
  159. for (i = 1; i <= h->loss_count; i++)
  160. if (tfrc_rx_hist_entry(h, i)->tfrchrx_seqno == seq)
  161. return 1;
  162. return 0;
  163. }
  164. EXPORT_SYMBOL_GPL(tfrc_rx_hist_duplicate);
  165. static void tfrc_rx_hist_swap(struct tfrc_rx_hist *h, const u8 a, const u8 b)
  166. {
  167. const u8 idx_a = tfrc_rx_hist_index(h, a),
  168. idx_b = tfrc_rx_hist_index(h, b);
  169. struct tfrc_rx_hist_entry *tmp = h->ring[idx_a];
  170. h->ring[idx_a] = h->ring[idx_b];
  171. h->ring[idx_b] = tmp;
  172. }
  173. /*
  174. * Private helper functions for loss detection.
  175. *
  176. * In the descriptions, `Si' refers to the sequence number of entry number i,
  177. * whose NDP count is `Ni' (lower case is used for variables).
  178. * Note: All __xxx_loss functions expect that a test against duplicates has been
  179. * performed already: the seqno of the skb must not be less than the seqno
  180. * of loss_prev; and it must not equal that of any valid history entry.
  181. */
  182. static void __do_track_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u64 n1)
  183. {
  184. u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,
  185. s1 = DCCP_SKB_CB(skb)->dccpd_seq;
  186. if (!dccp_loss_free(s0, s1, n1)) { /* gap between S0 and S1 */
  187. h->loss_count = 1;
  188. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n1);
  189. }
  190. }
  191. static void __one_after_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u32 n2)
  192. {
  193. u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,
  194. s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,
  195. s2 = DCCP_SKB_CB(skb)->dccpd_seq;
  196. if (likely(dccp_delta_seqno(s1, s2) > 0)) { /* S1 < S2 */
  197. h->loss_count = 2;
  198. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 2), skb, n2);
  199. return;
  200. }
  201. /* S0 < S2 < S1 */
  202. if (dccp_loss_free(s0, s2, n2)) {
  203. u64 n1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_ndp;
  204. if (dccp_loss_free(s2, s1, n1)) {
  205. /* hole is filled: S0, S2, and S1 are consecutive */
  206. h->loss_count = 0;
  207. h->loss_start = tfrc_rx_hist_index(h, 1);
  208. } else
  209. /* gap between S2 and S1: just update loss_prev */
  210. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_loss_prev(h), skb, n2);
  211. } else { /* gap between S0 and S2 */
  212. /*
  213. * Reorder history to insert S2 between S0 and S1
  214. */
  215. tfrc_rx_hist_swap(h, 0, 3);
  216. h->loss_start = tfrc_rx_hist_index(h, 3);
  217. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n2);
  218. h->loss_count = 2;
  219. }
  220. }
  221. /* return 1 if a new loss event has been identified */
  222. static int __two_after_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u32 n3)
  223. {
  224. u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,
  225. s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,
  226. s2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_seqno,
  227. s3 = DCCP_SKB_CB(skb)->dccpd_seq;
  228. if (likely(dccp_delta_seqno(s2, s3) > 0)) { /* S2 < S3 */
  229. h->loss_count = 3;
  230. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 3), skb, n3);
  231. return 1;
  232. }
  233. /* S3 < S2 */
  234. if (dccp_delta_seqno(s1, s3) > 0) { /* S1 < S3 < S2 */
  235. /*
  236. * Reorder history to insert S3 between S1 and S2
  237. */
  238. tfrc_rx_hist_swap(h, 2, 3);
  239. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 2), skb, n3);
  240. h->loss_count = 3;
  241. return 1;
  242. }
  243. /* S0 < S3 < S1 */
  244. if (dccp_loss_free(s0, s3, n3)) {
  245. u64 n1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_ndp;
  246. if (dccp_loss_free(s3, s1, n1)) {
  247. /* hole between S0 and S1 filled by S3 */
  248. u64 n2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_ndp;
  249. if (dccp_loss_free(s1, s2, n2)) {
  250. /* entire hole filled by S0, S3, S1, S2 */
  251. h->loss_start = tfrc_rx_hist_index(h, 2);
  252. h->loss_count = 0;
  253. } else {
  254. /* gap remains between S1 and S2 */
  255. h->loss_start = tfrc_rx_hist_index(h, 1);
  256. h->loss_count = 1;
  257. }
  258. } else /* gap exists between S3 and S1, loss_count stays at 2 */
  259. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_loss_prev(h), skb, n3);
  260. return 0;
  261. }
  262. /*
  263. * The remaining case: S0 < S3 < S1 < S2; gap between S0 and S3
  264. * Reorder history to insert S3 between S0 and S1.
  265. */
  266. tfrc_rx_hist_swap(h, 0, 3);
  267. h->loss_start = tfrc_rx_hist_index(h, 3);
  268. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n3);
  269. h->loss_count = 3;
  270. return 1;
  271. }
  272. /* recycle RX history records to continue loss detection if necessary */
  273. static void __three_after_loss(struct tfrc_rx_hist *h)
  274. {
  275. /*
  276. * At this stage we know already that there is a gap between S0 and S1
  277. * (since S0 was the highest sequence number received before detecting
  278. * the loss). To recycle the loss record, it is thus only necessary to
  279. * check for other possible gaps between S1/S2 and between S2/S3.
  280. */
  281. u64 s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,
  282. s2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_seqno,
  283. s3 = tfrc_rx_hist_entry(h, 3)->tfrchrx_seqno;
  284. u64 n2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_ndp,
  285. n3 = tfrc_rx_hist_entry(h, 3)->tfrchrx_ndp;
  286. if (dccp_loss_free(s1, s2, n2)) {
  287. if (dccp_loss_free(s2, s3, n3)) {
  288. /* no gap between S2 and S3: entire hole is filled */
  289. h->loss_start = tfrc_rx_hist_index(h, 3);
  290. h->loss_count = 0;
  291. } else {
  292. /* gap between S2 and S3 */
  293. h->loss_start = tfrc_rx_hist_index(h, 2);
  294. h->loss_count = 1;
  295. }
  296. } else { /* gap between S1 and S2 */
  297. h->loss_start = tfrc_rx_hist_index(h, 1);
  298. h->loss_count = 2;
  299. }
  300. }
  301. /**
  302. * tfrc_rx_handle_loss - Loss detection and further processing
  303. * @h: The non-empty RX history object
  304. * @lh: Loss Intervals database to update
  305. * @skb: Currently received packet
  306. * @ndp: The NDP count belonging to @skb
  307. * @calc_first_li: Caller-dependent computation of first loss interval in @lh
  308. * @sk: Used by @calc_first_li (see tfrc_lh_interval_add)
  309. * Chooses action according to pending loss, updates LI database when a new
  310. * loss was detected, and does required post-processing. Returns 1 when caller
  311. * should send feedback, 0 otherwise.
  312. * Since it also takes care of reordering during loss detection and updates the
  313. * records accordingly, the caller should not perform any more RX history
  314. * operations when loss_count is greater than 0 after calling this function.
  315. */
  316. int tfrc_rx_handle_loss(struct tfrc_rx_hist *h,
  317. struct tfrc_loss_hist *lh,
  318. struct sk_buff *skb, const u64 ndp,
  319. u32 (*calc_first_li)(struct sock *), struct sock *sk)
  320. {
  321. int is_new_loss = 0;
  322. if (h->loss_count == 0) {
  323. __do_track_loss(h, skb, ndp);
  324. } else if (h->loss_count == 1) {
  325. __one_after_loss(h, skb, ndp);
  326. } else if (h->loss_count != 2) {
  327. DCCP_BUG("invalid loss_count %d", h->loss_count);
  328. } else if (__two_after_loss(h, skb, ndp)) {
  329. /*
  330. * Update Loss Interval database and recycle RX records
  331. */
  332. is_new_loss = tfrc_lh_interval_add(lh, h, calc_first_li, sk);
  333. __three_after_loss(h);
  334. }
  335. return is_new_loss;
  336. }
  337. EXPORT_SYMBOL_GPL(tfrc_rx_handle_loss);
  338. int tfrc_rx_hist_alloc(struct tfrc_rx_hist *h)
  339. {
  340. int i;
  341. for (i = 0; i <= TFRC_NDUPACK; i++) {
  342. h->ring[i] = kmem_cache_alloc(tfrc_rx_hist_slab, GFP_ATOMIC);
  343. if (h->ring[i] == NULL)
  344. goto out_free;
  345. }
  346. h->loss_count = h->loss_start = 0;
  347. return 0;
  348. out_free:
  349. while (i-- != 0) {
  350. kmem_cache_free(tfrc_rx_hist_slab, h->ring[i]);
  351. h->ring[i] = NULL;
  352. }
  353. return -ENOBUFS;
  354. }
  355. EXPORT_SYMBOL_GPL(tfrc_rx_hist_alloc);
  356. void tfrc_rx_hist_purge(struct tfrc_rx_hist *h)
  357. {
  358. int i;
  359. for (i = 0; i <= TFRC_NDUPACK; ++i)
  360. if (h->ring[i] != NULL) {
  361. kmem_cache_free(tfrc_rx_hist_slab, h->ring[i]);
  362. h->ring[i] = NULL;
  363. }
  364. }
  365. EXPORT_SYMBOL_GPL(tfrc_rx_hist_purge);
  366. /**
  367. * tfrc_rx_hist_rtt_last_s - reference entry to compute RTT samples against
  368. */
  369. static inline struct tfrc_rx_hist_entry *
  370. tfrc_rx_hist_rtt_last_s(const struct tfrc_rx_hist *h)
  371. {
  372. return h->ring[0];
  373. }
  374. /**
  375. * tfrc_rx_hist_rtt_prev_s: previously suitable (wrt rtt_last_s) RTT-sampling entry
  376. */
  377. static inline struct tfrc_rx_hist_entry *
  378. tfrc_rx_hist_rtt_prev_s(const struct tfrc_rx_hist *h)
  379. {
  380. return h->ring[h->rtt_sample_prev];
  381. }
  382. /**
  383. * tfrc_rx_hist_sample_rtt - Sample RTT from timestamp / CCVal
  384. * Based on ideas presented in RFC 4342, 8.1. Returns 0 if it was not able
  385. * to compute a sample with given data - calling function should check this.
  386. */
  387. u32 tfrc_rx_hist_sample_rtt(struct tfrc_rx_hist *h, const struct sk_buff *skb)
  388. {
  389. u32 sample = 0,
  390. delta_v = SUB16(dccp_hdr(skb)->dccph_ccval,
  391. tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
  392. if (delta_v < 1 || delta_v > 4) { /* unsuitable CCVal delta */
  393. if (h->rtt_sample_prev == 2) { /* previous candidate stored */
  394. sample = SUB16(tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_ccval,
  395. tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
  396. if (sample)
  397. sample = 4 / sample *
  398. ktime_us_delta(tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_tstamp,
  399. tfrc_rx_hist_rtt_last_s(h)->tfrchrx_tstamp);
  400. else /*
  401. * FIXME: This condition is in principle not
  402. * possible but occurs when CCID is used for
  403. * two-way data traffic. I have tried to trace
  404. * it, but the cause does not seem to be here.
  405. */
  406. DCCP_BUG("please report to dccp@vger.kernel.org"
  407. " => prev = %u, last = %u",
  408. tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_ccval,
  409. tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
  410. } else if (delta_v < 1) {
  411. h->rtt_sample_prev = 1;
  412. goto keep_ref_for_next_time;
  413. }
  414. } else if (delta_v == 4) /* optimal match */
  415. sample = ktime_to_us(net_timedelta(tfrc_rx_hist_rtt_last_s(h)->tfrchrx_tstamp));
  416. else { /* suboptimal match */
  417. h->rtt_sample_prev = 2;
  418. goto keep_ref_for_next_time;
  419. }
  420. if (unlikely(sample > DCCP_SANE_RTT_MAX)) {
  421. DCCP_WARN("RTT sample %u too large, using max\n", sample);
  422. sample = DCCP_SANE_RTT_MAX;
  423. }
  424. h->rtt_sample_prev = 0; /* use current entry as next reference */
  425. keep_ref_for_next_time:
  426. return sample;
  427. }
  428. EXPORT_SYMBOL_GPL(tfrc_rx_hist_sample_rtt);