packet_history.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 u32 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 u32 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 __after_loss functions expect that a test against duplicates has
  179. * been performed already: the seqno of the skb must not be less than the
  180. * seqno of loss_prev; and it must not equal that of any valid hist_entry.
  181. */
  182. static void __one_after_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u32 n2)
  183. {
  184. u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,
  185. s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,
  186. s2 = DCCP_SKB_CB(skb)->dccpd_seq;
  187. int n1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_ndp,
  188. d12 = dccp_delta_seqno(s1, s2), d2;
  189. if (d12 > 0) { /* S1 < S2 */
  190. h->loss_count = 2;
  191. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 2), skb, n2);
  192. return;
  193. }
  194. /* S0 < S2 < S1 */
  195. d2 = dccp_delta_seqno(s0, s2);
  196. if (d2 == 1 || n2 >= d2) { /* S2 is direct successor of S0 */
  197. int d21 = -d12;
  198. if (d21 == 1 || n1 >= d21) {
  199. /* hole is filled: S0, S2, and S1 are consecutive */
  200. h->loss_count = 0;
  201. h->loss_start = tfrc_rx_hist_index(h, 1);
  202. } else
  203. /* gap between S2 and S1: just update loss_prev */
  204. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_loss_prev(h), skb, n2);
  205. } else { /* hole between S0 and S2 */
  206. /*
  207. * Reorder history to insert S2 between S0 and s1
  208. */
  209. tfrc_rx_hist_swap(h, 0, 3);
  210. h->loss_start = tfrc_rx_hist_index(h, 3);
  211. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n2);
  212. h->loss_count = 2;
  213. }
  214. }
  215. /* return 1 if a new loss event has been identified */
  216. static int __two_after_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u32 n3)
  217. {
  218. u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,
  219. s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,
  220. s2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_seqno,
  221. s3 = DCCP_SKB_CB(skb)->dccpd_seq;
  222. int n1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_ndp,
  223. d23 = dccp_delta_seqno(s2, s3), d13, d3, d31;
  224. if (d23 > 0) { /* S2 < S3 */
  225. h->loss_count = 3;
  226. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 3), skb, n3);
  227. return 1;
  228. }
  229. /* S3 < S2 */
  230. d13 = dccp_delta_seqno(s1, s3);
  231. if (d13 > 0) {
  232. /*
  233. * The sequence number order is S1, S3, S2
  234. * Reorder history to insert entry between S1 and S2
  235. */
  236. tfrc_rx_hist_swap(h, 2, 3);
  237. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 2), skb, n3);
  238. h->loss_count = 3;
  239. return 1;
  240. }
  241. /* S0 < S3 < S1 */
  242. d31 = -d13;
  243. d3 = dccp_delta_seqno(s0, s3);
  244. if (d3 == 1 || n3 >= d3) { /* S3 is a successor of S0 */
  245. if (d31 == 1 || n1 >= d31) {
  246. /* hole between S0 and S1 filled by S3 */
  247. int d2 = dccp_delta_seqno(s1, s2),
  248. n2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_ndp;
  249. if (d2 == 1 || n2 >= d2) {
  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: S3 is not a successor of S0.
  264. * Sequence order is S0, S3, S1, S2; reorder to insert 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. /* return the signed modulo-2^48 sequence number distance from entry e1 to e2 */
  273. static s64 tfrc_rx_hist_delta_seqno(struct tfrc_rx_hist *h, u8 e1, u8 e2)
  274. {
  275. DCCP_BUG_ON(e1 > h->loss_count || e2 > h->loss_count);
  276. return dccp_delta_seqno(tfrc_rx_hist_entry(h, e1)->tfrchrx_seqno,
  277. tfrc_rx_hist_entry(h, e2)->tfrchrx_seqno);
  278. }
  279. /* recycle RX history records to continue loss detection if necessary */
  280. static void __three_after_loss(struct tfrc_rx_hist *h)
  281. {
  282. /*
  283. * The distance between S0 and S1 is always greater than 1 and the NDP
  284. * count of S1 is smaller than this distance. Otherwise there would
  285. * have been no loss. Hence it is only necessary to see whether there
  286. * are further missing data packets between S1/S2 and S2/S3.
  287. */
  288. int d2 = tfrc_rx_hist_delta_seqno(h, 1, 2),
  289. d3 = tfrc_rx_hist_delta_seqno(h, 2, 3),
  290. n2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_ndp,
  291. n3 = tfrc_rx_hist_entry(h, 3)->tfrchrx_ndp;
  292. if (d2 == 1 || n2 >= d2) { /* S2 is successor to S1 */
  293. if (d3 == 1 || n3 >= d3) {
  294. /* S3 is successor of S2: entire hole is filled */
  295. h->loss_start = tfrc_rx_hist_index(h, 3);
  296. h->loss_count = 0;
  297. } else {
  298. /* gap between S2 and S3 */
  299. h->loss_start = tfrc_rx_hist_index(h, 2);
  300. h->loss_count = 1;
  301. }
  302. } else { /* gap between S1 and S2 */
  303. h->loss_start = tfrc_rx_hist_index(h, 1);
  304. h->loss_count = 2;
  305. }
  306. }
  307. /**
  308. * tfrc_rx_handle_loss - Loss detection and further processing
  309. * @h: The non-empty RX history object
  310. * @lh: Loss Intervals database to update
  311. * @skb: Currently received packet
  312. * @ndp: The NDP count belonging to @skb
  313. * @calc_first_li: Caller-dependent computation of first loss interval in @lh
  314. * @sk: Used by @calc_first_li (see tfrc_lh_interval_add)
  315. * Chooses action according to pending loss, updates LI database when a new
  316. * loss was detected, and does required post-processing. Returns 1 when caller
  317. * should send feedback, 0 otherwise.
  318. */
  319. int tfrc_rx_handle_loss(struct tfrc_rx_hist *h,
  320. struct tfrc_loss_hist *lh,
  321. struct sk_buff *skb, u32 ndp,
  322. u32 (*calc_first_li)(struct sock *), struct sock *sk)
  323. {
  324. int is_new_loss = 0;
  325. if (h->loss_count == 1) {
  326. __one_after_loss(h, skb, ndp);
  327. } else if (h->loss_count != 2) {
  328. DCCP_BUG("invalid loss_count %d", h->loss_count);
  329. } else if (__two_after_loss(h, skb, ndp)) {
  330. /*
  331. * Update Loss Interval database and recycle RX records
  332. */
  333. is_new_loss = tfrc_lh_interval_add(lh, h, calc_first_li, sk);
  334. __three_after_loss(h);
  335. }
  336. return is_new_loss;
  337. }
  338. EXPORT_SYMBOL_GPL(tfrc_rx_handle_loss);
  339. int tfrc_rx_hist_alloc(struct tfrc_rx_hist *h)
  340. {
  341. int i;
  342. for (i = 0; i <= TFRC_NDUPACK; i++) {
  343. h->ring[i] = kmem_cache_alloc(tfrc_rx_hist_slab, GFP_ATOMIC);
  344. if (h->ring[i] == NULL)
  345. goto out_free;
  346. }
  347. h->loss_count = h->loss_start = 0;
  348. return 0;
  349. out_free:
  350. while (i-- != 0) {
  351. kmem_cache_free(tfrc_rx_hist_slab, h->ring[i]);
  352. h->ring[i] = NULL;
  353. }
  354. return -ENOBUFS;
  355. }
  356. EXPORT_SYMBOL_GPL(tfrc_rx_hist_alloc);
  357. void tfrc_rx_hist_purge(struct tfrc_rx_hist *h)
  358. {
  359. int i;
  360. for (i = 0; i <= TFRC_NDUPACK; ++i)
  361. if (h->ring[i] != NULL) {
  362. kmem_cache_free(tfrc_rx_hist_slab, h->ring[i]);
  363. h->ring[i] = NULL;
  364. }
  365. }
  366. EXPORT_SYMBOL_GPL(tfrc_rx_hist_purge);
  367. /**
  368. * tfrc_rx_hist_rtt_last_s - reference entry to compute RTT samples against
  369. */
  370. static inline struct tfrc_rx_hist_entry *
  371. tfrc_rx_hist_rtt_last_s(const struct tfrc_rx_hist *h)
  372. {
  373. return h->ring[0];
  374. }
  375. /**
  376. * tfrc_rx_hist_rtt_prev_s: previously suitable (wrt rtt_last_s) RTT-sampling entry
  377. */
  378. static inline struct tfrc_rx_hist_entry *
  379. tfrc_rx_hist_rtt_prev_s(const struct tfrc_rx_hist *h)
  380. {
  381. return h->ring[h->rtt_sample_prev];
  382. }
  383. /**
  384. * tfrc_rx_hist_sample_rtt - Sample RTT from timestamp / CCVal
  385. * Based on ideas presented in RFC 4342, 8.1. Returns 0 if it was not able
  386. * to compute a sample with given data - calling function should check this.
  387. */
  388. u32 tfrc_rx_hist_sample_rtt(struct tfrc_rx_hist *h, const struct sk_buff *skb)
  389. {
  390. u32 sample = 0,
  391. delta_v = SUB16(dccp_hdr(skb)->dccph_ccval,
  392. tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
  393. if (delta_v < 1 || delta_v > 4) { /* unsuitable CCVal delta */
  394. if (h->rtt_sample_prev == 2) { /* previous candidate stored */
  395. sample = SUB16(tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_ccval,
  396. tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
  397. if (sample)
  398. sample = 4 / sample *
  399. ktime_us_delta(tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_tstamp,
  400. tfrc_rx_hist_rtt_last_s(h)->tfrchrx_tstamp);
  401. else /*
  402. * FIXME: This condition is in principle not
  403. * possible but occurs when CCID is used for
  404. * two-way data traffic. I have tried to trace
  405. * it, but the cause does not seem to be here.
  406. */
  407. DCCP_BUG("please report to dccp@vger.kernel.org"
  408. " => prev = %u, last = %u",
  409. tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_ccval,
  410. tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);
  411. } else if (delta_v < 1) {
  412. h->rtt_sample_prev = 1;
  413. goto keep_ref_for_next_time;
  414. }
  415. } else if (delta_v == 4) /* optimal match */
  416. sample = ktime_to_us(net_timedelta(tfrc_rx_hist_rtt_last_s(h)->tfrchrx_tstamp));
  417. else { /* suboptimal match */
  418. h->rtt_sample_prev = 2;
  419. goto keep_ref_for_next_time;
  420. }
  421. if (unlikely(sample > DCCP_SANE_RTT_MAX)) {
  422. DCCP_WARN("RTT sample %u too large, using max\n", sample);
  423. sample = DCCP_SANE_RTT_MAX;
  424. }
  425. h->rtt_sample_prev = 0; /* use current entry as next reference */
  426. keep_ref_for_next_time:
  427. return sample;
  428. }
  429. EXPORT_SYMBOL_GPL(tfrc_rx_hist_sample_rtt);