packet_history.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. * Transmitter History Routines
  43. */
  44. static struct kmem_cache *tfrc_tx_hist_slab;
  45. int __init tfrc_tx_packet_history_init(void)
  46. {
  47. tfrc_tx_hist_slab = kmem_cache_create("tfrc_tx_hist",
  48. sizeof(struct tfrc_tx_hist_entry),
  49. 0, SLAB_HWCACHE_ALIGN, NULL);
  50. return tfrc_tx_hist_slab == NULL ? -ENOBUFS : 0;
  51. }
  52. void tfrc_tx_packet_history_exit(void)
  53. {
  54. if (tfrc_tx_hist_slab != NULL) {
  55. kmem_cache_destroy(tfrc_tx_hist_slab);
  56. tfrc_tx_hist_slab = NULL;
  57. }
  58. }
  59. int tfrc_tx_hist_add(struct tfrc_tx_hist_entry **headp, u64 seqno)
  60. {
  61. struct tfrc_tx_hist_entry *entry = kmem_cache_alloc(tfrc_tx_hist_slab, gfp_any());
  62. if (entry == NULL)
  63. return -ENOBUFS;
  64. entry->seqno = seqno;
  65. entry->stamp = ktime_get_real();
  66. entry->next = *headp;
  67. *headp = entry;
  68. return 0;
  69. }
  70. EXPORT_SYMBOL_GPL(tfrc_tx_hist_add);
  71. void tfrc_tx_hist_purge(struct tfrc_tx_hist_entry **headp)
  72. {
  73. struct tfrc_tx_hist_entry *head = *headp;
  74. while (head != NULL) {
  75. struct tfrc_tx_hist_entry *next = head->next;
  76. kmem_cache_free(tfrc_tx_hist_slab, head);
  77. head = next;
  78. }
  79. *headp = NULL;
  80. }
  81. EXPORT_SYMBOL_GPL(tfrc_tx_hist_purge);
  82. /*
  83. * Receiver History Routines
  84. */
  85. static struct kmem_cache *tfrc_rx_hist_slab;
  86. int __init tfrc_rx_packet_history_init(void)
  87. {
  88. tfrc_rx_hist_slab = kmem_cache_create("tfrc_rxh_cache",
  89. sizeof(struct tfrc_rx_hist_entry),
  90. 0, SLAB_HWCACHE_ALIGN, NULL);
  91. return tfrc_rx_hist_slab == NULL ? -ENOBUFS : 0;
  92. }
  93. void tfrc_rx_packet_history_exit(void)
  94. {
  95. if (tfrc_rx_hist_slab != NULL) {
  96. kmem_cache_destroy(tfrc_rx_hist_slab);
  97. tfrc_rx_hist_slab = NULL;
  98. }
  99. }
  100. static inline void tfrc_rx_hist_entry_from_skb(struct tfrc_rx_hist_entry *entry,
  101. const struct sk_buff *skb,
  102. const u64 ndp)
  103. {
  104. const struct dccp_hdr *dh = dccp_hdr(skb);
  105. entry->tfrchrx_seqno = DCCP_SKB_CB(skb)->dccpd_seq;
  106. entry->tfrchrx_ccval = dh->dccph_ccval;
  107. entry->tfrchrx_type = dh->dccph_type;
  108. entry->tfrchrx_ndp = ndp;
  109. entry->tfrchrx_tstamp = ktime_get_real();
  110. }
  111. void tfrc_rx_hist_add_packet(struct tfrc_rx_hist *h,
  112. const struct sk_buff *skb,
  113. const u64 ndp)
  114. {
  115. struct tfrc_rx_hist_entry *entry = tfrc_rx_hist_last_rcv(h);
  116. tfrc_rx_hist_entry_from_skb(entry, skb, ndp);
  117. }
  118. EXPORT_SYMBOL_GPL(tfrc_rx_hist_add_packet);
  119. /* has the packet contained in skb been seen before? */
  120. int tfrc_rx_hist_duplicate(struct tfrc_rx_hist *h, struct sk_buff *skb)
  121. {
  122. const u64 seq = DCCP_SKB_CB(skb)->dccpd_seq;
  123. int i;
  124. if (dccp_delta_seqno(tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno, seq) <= 0)
  125. return 1;
  126. for (i = 1; i <= h->loss_count; i++)
  127. if (tfrc_rx_hist_entry(h, i)->tfrchrx_seqno == seq)
  128. return 1;
  129. return 0;
  130. }
  131. EXPORT_SYMBOL_GPL(tfrc_rx_hist_duplicate);
  132. static void __tfrc_rx_hist_swap(struct tfrc_rx_hist *h, const u8 a, const u8 b)
  133. {
  134. struct tfrc_rx_hist_entry *tmp = h->ring[a];
  135. h->ring[a] = h->ring[b];
  136. h->ring[b] = tmp;
  137. }
  138. static void tfrc_rx_hist_swap(struct tfrc_rx_hist *h, const u8 a, const u8 b)
  139. {
  140. __tfrc_rx_hist_swap(h, tfrc_rx_hist_index(h, a),
  141. tfrc_rx_hist_index(h, b));
  142. }
  143. /**
  144. * tfrc_rx_hist_resume_rtt_sampling - Prepare RX history for RTT sampling
  145. * This is called after loss detection has finished, when the history entry
  146. * with the index of `loss_count' holds the highest-received sequence number.
  147. * RTT sampling requires this information at ring[0] (tfrc_rx_hist_sample_rtt).
  148. */
  149. static inline void tfrc_rx_hist_resume_rtt_sampling(struct tfrc_rx_hist *h)
  150. {
  151. __tfrc_rx_hist_swap(h, 0, tfrc_rx_hist_index(h, h->loss_count));
  152. h->loss_count = h->loss_start = 0;
  153. }
  154. /*
  155. * Private helper functions for loss detection.
  156. *
  157. * In the descriptions, `Si' refers to the sequence number of entry number i,
  158. * whose NDP count is `Ni' (lower case is used for variables).
  159. * Note: All __xxx_loss functions expect that a test against duplicates has been
  160. * performed already: the seqno of the skb must not be less than the seqno
  161. * of loss_prev; and it must not equal that of any valid history entry.
  162. */
  163. static void __do_track_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u64 n1)
  164. {
  165. u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,
  166. s1 = DCCP_SKB_CB(skb)->dccpd_seq;
  167. if (!dccp_loss_free(s0, s1, n1)) /* gap between S0 and S1 */
  168. h->loss_count = 1;
  169. }
  170. static void __one_after_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u32 n2)
  171. {
  172. u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,
  173. s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,
  174. s2 = DCCP_SKB_CB(skb)->dccpd_seq;
  175. if (likely(dccp_delta_seqno(s1, s2) > 0)) { /* S1 < S2 */
  176. h->loss_count = 2;
  177. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 2), skb, n2);
  178. return;
  179. }
  180. /* S0 < S2 < S1 */
  181. if (dccp_loss_free(s0, s2, n2)) {
  182. u64 n1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_ndp;
  183. if (dccp_loss_free(s2, s1, n1)) {
  184. /* hole is filled: S0, S2, and S1 are consecutive */
  185. tfrc_rx_hist_resume_rtt_sampling(h);
  186. } else
  187. /* gap between S2 and S1: just update loss_prev */
  188. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_loss_prev(h), skb, n2);
  189. } else { /* gap between S0 and S2 */
  190. /*
  191. * Reorder history to insert S2 between S0 and S1
  192. */
  193. tfrc_rx_hist_swap(h, 0, 3);
  194. h->loss_start = tfrc_rx_hist_index(h, 3);
  195. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n2);
  196. h->loss_count = 2;
  197. }
  198. }
  199. /* return 1 if a new loss event has been identified */
  200. static int __two_after_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u32 n3)
  201. {
  202. u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,
  203. s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,
  204. s2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_seqno,
  205. s3 = DCCP_SKB_CB(skb)->dccpd_seq;
  206. if (likely(dccp_delta_seqno(s2, s3) > 0)) { /* S2 < S3 */
  207. h->loss_count = 3;
  208. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 3), skb, n3);
  209. return 1;
  210. }
  211. /* S3 < S2 */
  212. if (dccp_delta_seqno(s1, s3) > 0) { /* S1 < S3 < S2 */
  213. /*
  214. * Reorder history to insert S3 between S1 and S2
  215. */
  216. tfrc_rx_hist_swap(h, 2, 3);
  217. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 2), skb, n3);
  218. h->loss_count = 3;
  219. return 1;
  220. }
  221. /* S0 < S3 < S1 */
  222. if (dccp_loss_free(s0, s3, n3)) {
  223. u64 n1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_ndp;
  224. if (dccp_loss_free(s3, s1, n1)) {
  225. /* hole between S0 and S1 filled by S3 */
  226. u64 n2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_ndp;
  227. if (dccp_loss_free(s1, s2, n2)) {
  228. /* entire hole filled by S0, S3, S1, S2 */
  229. tfrc_rx_hist_resume_rtt_sampling(h);
  230. } else {
  231. /* gap remains between S1 and S2 */
  232. h->loss_start = tfrc_rx_hist_index(h, 1);
  233. h->loss_count = 1;
  234. }
  235. } else /* gap exists between S3 and S1, loss_count stays at 2 */
  236. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_loss_prev(h), skb, n3);
  237. return 0;
  238. }
  239. /*
  240. * The remaining case: S0 < S3 < S1 < S2; gap between S0 and S3
  241. * Reorder history to insert S3 between S0 and S1.
  242. */
  243. tfrc_rx_hist_swap(h, 0, 3);
  244. h->loss_start = tfrc_rx_hist_index(h, 3);
  245. tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n3);
  246. h->loss_count = 3;
  247. return 1;
  248. }
  249. /* recycle RX history records to continue loss detection if necessary */
  250. static void __three_after_loss(struct tfrc_rx_hist *h)
  251. {
  252. /*
  253. * At this stage we know already that there is a gap between S0 and S1
  254. * (since S0 was the highest sequence number received before detecting
  255. * the loss). To recycle the loss record, it is thus only necessary to
  256. * check for other possible gaps between S1/S2 and between S2/S3.
  257. */
  258. u64 s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,
  259. s2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_seqno,
  260. s3 = tfrc_rx_hist_entry(h, 3)->tfrchrx_seqno;
  261. u64 n2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_ndp,
  262. n3 = tfrc_rx_hist_entry(h, 3)->tfrchrx_ndp;
  263. if (dccp_loss_free(s1, s2, n2)) {
  264. if (dccp_loss_free(s2, s3, n3)) {
  265. /* no gap between S2 and S3: entire hole is filled */
  266. tfrc_rx_hist_resume_rtt_sampling(h);
  267. } else {
  268. /* gap between S2 and S3 */
  269. h->loss_start = tfrc_rx_hist_index(h, 2);
  270. h->loss_count = 1;
  271. }
  272. } else { /* gap between S1 and S2 */
  273. h->loss_start = tfrc_rx_hist_index(h, 1);
  274. h->loss_count = 2;
  275. }
  276. }
  277. /**
  278. * tfrc_rx_congestion_event - Loss detection and further processing
  279. * @h: The non-empty RX history object
  280. * @lh: Loss Intervals database to update
  281. * @skb: Currently received packet
  282. * @ndp: The NDP count belonging to @skb
  283. * @first_li: Caller-dependent computation of first loss interval in @lh
  284. * @sk: Used by @calc_first_li (see tfrc_lh_interval_add)
  285. * Chooses action according to pending loss, updates LI database when a new
  286. * loss was detected, and does required post-processing. Returns 1 when caller
  287. * should send feedback, 0 otherwise.
  288. * Since it also takes care of reordering during loss detection and updates the
  289. * records accordingly, the caller should not perform any more RX history
  290. * operations when loss_count is greater than 0 after calling this function.
  291. */
  292. bool tfrc_rx_congestion_event(struct tfrc_rx_hist *h,
  293. struct tfrc_loss_hist *lh,
  294. struct sk_buff *skb, const u64 ndp,
  295. u32 (*first_li)(struct sock *), struct sock *sk)
  296. {
  297. bool new_event = false;
  298. if (tfrc_rx_hist_duplicate(h, skb))
  299. return 0;
  300. if (h->loss_count == 0) {
  301. __do_track_loss(h, skb, ndp);
  302. tfrc_rx_hist_sample_rtt(h, skb);
  303. tfrc_rx_hist_add_packet(h, skb, ndp);
  304. } else if (h->loss_count == 1) {
  305. __one_after_loss(h, skb, ndp);
  306. } else if (h->loss_count != 2) {
  307. DCCP_BUG("invalid loss_count %d", h->loss_count);
  308. } else if (__two_after_loss(h, skb, ndp)) {
  309. /*
  310. * Update Loss Interval database and recycle RX records
  311. */
  312. new_event = tfrc_lh_interval_add(lh, h, first_li, sk);
  313. __three_after_loss(h);
  314. }
  315. /*
  316. * Update moving-average of `s' and the sum of received payload bytes.
  317. */
  318. if (dccp_data_packet(skb)) {
  319. const u32 payload = skb->len - dccp_hdr(skb)->dccph_doff * 4;
  320. h->packet_size = tfrc_ewma(h->packet_size, payload, 9);
  321. h->bytes_recvd += payload;
  322. }
  323. /* RFC 3448, 6.1: update I_0, whose growth implies p <= p_prev */
  324. if (!new_event)
  325. tfrc_lh_update_i_mean(lh, skb);
  326. return new_event;
  327. }
  328. EXPORT_SYMBOL_GPL(tfrc_rx_congestion_event);
  329. /* Compute the sending rate X_recv measured between feedback intervals */
  330. u32 tfrc_rx_hist_x_recv(struct tfrc_rx_hist *h, const u32 last_x_recv)
  331. {
  332. u64 bytes = h->bytes_recvd, last_rtt = h->rtt_estimate;
  333. s64 delta = ktime_to_us(net_timedelta(h->bytes_start));
  334. WARN_ON(delta <= 0);
  335. /*
  336. * Ensure that the sampling interval for X_recv is at least one RTT,
  337. * by extending the sampling interval backwards in time, over the last
  338. * R_(m-1) seconds, as per rfc3448bis-06, 6.2.
  339. * To reduce noise (e.g. when the RTT changes often), this is only
  340. * done when delta is smaller than RTT/2.
  341. */
  342. if (last_x_recv > 0 && delta < last_rtt/2) {
  343. tfrc_pr_debug("delta < RTT ==> %ld us < %u us\n",
  344. (long)delta, (unsigned)last_rtt);
  345. delta = (bytes ? delta : 0) + last_rtt;
  346. bytes += div_u64((u64)last_x_recv * last_rtt, USEC_PER_SEC);
  347. }
  348. if (unlikely(bytes == 0)) {
  349. DCCP_WARN("X_recv == 0, using old value of %u\n", last_x_recv);
  350. return last_x_recv;
  351. }
  352. return scaled_div32(bytes, delta);
  353. }
  354. EXPORT_SYMBOL_GPL(tfrc_rx_hist_x_recv);
  355. void tfrc_rx_hist_purge(struct tfrc_rx_hist *h)
  356. {
  357. int i;
  358. for (i = 0; i <= TFRC_NDUPACK; ++i)
  359. if (h->ring[i] != NULL) {
  360. kmem_cache_free(tfrc_rx_hist_slab, h->ring[i]);
  361. h->ring[i] = NULL;
  362. }
  363. }
  364. EXPORT_SYMBOL_GPL(tfrc_rx_hist_purge);
  365. static int tfrc_rx_hist_alloc(struct tfrc_rx_hist *h)
  366. {
  367. int i;
  368. memset(h, 0, sizeof(*h));
  369. for (i = 0; i <= TFRC_NDUPACK; i++) {
  370. h->ring[i] = kmem_cache_alloc(tfrc_rx_hist_slab, GFP_ATOMIC);
  371. if (h->ring[i] == NULL) {
  372. tfrc_rx_hist_purge(h);
  373. return -ENOBUFS;
  374. }
  375. }
  376. return 0;
  377. }
  378. int tfrc_rx_hist_init(struct tfrc_rx_hist *h, struct sock *sk)
  379. {
  380. if (tfrc_rx_hist_alloc(h))
  381. return -ENOBUFS;
  382. /*
  383. * Initialise first entry with GSR to start loss detection as early as
  384. * possible. Code using this must not use any other fields. The entry
  385. * will be overwritten once the CCID updates its received packets.
  386. */
  387. tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno = dccp_sk(sk)->dccps_gsr;
  388. return 0;
  389. }
  390. EXPORT_SYMBOL_GPL(tfrc_rx_hist_init);
  391. /**
  392. * tfrc_rx_hist_sample_rtt - Sample RTT from timestamp / CCVal
  393. * Based on ideas presented in RFC 4342, 8.1. This function expects that no loss
  394. * is pending and uses the following history entries (via rtt_sample_prev):
  395. * - h->ring[0] contains the most recent history entry prior to @skb;
  396. * - h->ring[1] is an unused `dummy' entry when the current difference is 0;
  397. */
  398. void tfrc_rx_hist_sample_rtt(struct tfrc_rx_hist *h, const struct sk_buff *skb)
  399. {
  400. struct tfrc_rx_hist_entry *last = h->ring[0];
  401. u32 sample, delta_v;
  402. /*
  403. * When not to sample:
  404. * - on non-data packets
  405. * (RFC 4342, 8.1: CCVal only fully defined for data packets);
  406. * - when no data packets have been received yet
  407. * (FIXME: using sampled packet size as indicator here);
  408. * - as long as there are gaps in the sequence space (pending loss).
  409. */
  410. if (!dccp_data_packet(skb) || h->packet_size == 0 ||
  411. tfrc_rx_hist_loss_pending(h))
  412. return;
  413. h->rtt_sample_prev = 0; /* reset previous candidate */
  414. delta_v = SUB16(dccp_hdr(skb)->dccph_ccval, last->tfrchrx_ccval);
  415. if (delta_v == 0) { /* less than RTT/4 difference */
  416. h->rtt_sample_prev = 1;
  417. return;
  418. }
  419. sample = dccp_sane_rtt(ktime_to_us(net_timedelta(last->tfrchrx_tstamp)));
  420. if (delta_v <= 4) /* between RTT/4 and RTT */
  421. sample *= 4 / delta_v;
  422. else if (!(sample < h->rtt_estimate && sample > h->rtt_estimate/2))
  423. /*
  424. * Optimisation: CCVal difference is greater than 1 RTT, yet the
  425. * sample is less than the local RTT estimate; which means that
  426. * the RTT estimate is too high.
  427. * To avoid noise, it is not done if the sample is below RTT/2.
  428. */
  429. return;
  430. /* Use a lower weight than usual to increase responsiveness */
  431. h->rtt_estimate = tfrc_ewma(h->rtt_estimate, sample, 5);
  432. }
  433. EXPORT_SYMBOL_GPL(tfrc_rx_hist_sample_rtt);