/net/dccp/ccids/lib/packet_history.c

http://github.com/mirrors/linux · C · 434 lines · 271 code · 63 blank · 100 comment · 61 complexity · 1a0cce65fe3909c29640f7698412bc72 MD5 · raw file

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