ackvec.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * net/dccp/ackvec.c
  3. *
  4. * An implementation of the DCCP protocol
  5. * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; version 2 of the License;
  10. */
  11. #include "ackvec.h"
  12. #include "dccp.h"
  13. #include <linux/init.h>
  14. #include <linux/errno.h>
  15. #include <linux/kernel.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/slab.h>
  18. #include <net/sock.h>
  19. static struct kmem_cache *dccp_ackvec_slab;
  20. static struct kmem_cache *dccp_ackvec_record_slab;
  21. static struct dccp_ackvec_record *dccp_ackvec_record_new(void)
  22. {
  23. struct dccp_ackvec_record *avr =
  24. kmem_cache_alloc(dccp_ackvec_record_slab, GFP_ATOMIC);
  25. if (avr != NULL)
  26. INIT_LIST_HEAD(&avr->avr_node);
  27. return avr;
  28. }
  29. static void dccp_ackvec_record_delete(struct dccp_ackvec_record *avr)
  30. {
  31. if (unlikely(avr == NULL))
  32. return;
  33. /* Check if deleting a linked record */
  34. WARN_ON(!list_empty(&avr->avr_node));
  35. kmem_cache_free(dccp_ackvec_record_slab, avr);
  36. }
  37. static void dccp_ackvec_insert_avr(struct dccp_ackvec *av,
  38. struct dccp_ackvec_record *avr)
  39. {
  40. /*
  41. * AVRs are sorted by seqno. Since we are sending them in order, we
  42. * just add the AVR at the head of the list.
  43. * -sorbo.
  44. */
  45. if (!list_empty(&av->av_records)) {
  46. const struct dccp_ackvec_record *head =
  47. list_entry(av->av_records.next,
  48. struct dccp_ackvec_record,
  49. avr_node);
  50. BUG_ON(before48(avr->avr_ack_seqno, head->avr_ack_seqno));
  51. }
  52. list_add(&avr->avr_node, &av->av_records);
  53. }
  54. int dccp_insert_option_ackvec(struct sock *sk, struct sk_buff *skb)
  55. {
  56. struct dccp_sock *dp = dccp_sk(sk);
  57. struct dccp_ackvec *av = dp->dccps_hc_rx_ackvec;
  58. /* Figure out how many options do we need to represent the ackvec */
  59. const u8 nr_opts = DIV_ROUND_UP(av->av_vec_len, DCCP_SINGLE_OPT_MAXLEN);
  60. u16 len = av->av_vec_len + 2 * nr_opts, i;
  61. u32 elapsed_time;
  62. const unsigned char *tail, *from;
  63. unsigned char *to;
  64. struct dccp_ackvec_record *avr;
  65. suseconds_t delta;
  66. if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN)
  67. return -1;
  68. delta = ktime_us_delta(ktime_get_real(), av->av_time);
  69. elapsed_time = delta / 10;
  70. if (elapsed_time != 0 &&
  71. dccp_insert_option_elapsed_time(sk, skb, elapsed_time))
  72. return -1;
  73. avr = dccp_ackvec_record_new();
  74. if (avr == NULL)
  75. return -1;
  76. DCCP_SKB_CB(skb)->dccpd_opt_len += len;
  77. to = skb_push(skb, len);
  78. len = av->av_vec_len;
  79. from = av->av_buf + av->av_buf_head;
  80. tail = av->av_buf + DCCP_MAX_ACKVEC_LEN;
  81. for (i = 0; i < nr_opts; ++i) {
  82. int copylen = len;
  83. if (len > DCCP_SINGLE_OPT_MAXLEN)
  84. copylen = DCCP_SINGLE_OPT_MAXLEN;
  85. *to++ = DCCPO_ACK_VECTOR_0;
  86. *to++ = copylen + 2;
  87. /* Check if buf_head wraps */
  88. if (from + copylen > tail) {
  89. const u16 tailsize = tail - from;
  90. memcpy(to, from, tailsize);
  91. to += tailsize;
  92. len -= tailsize;
  93. copylen -= tailsize;
  94. from = av->av_buf;
  95. }
  96. memcpy(to, from, copylen);
  97. from += copylen;
  98. to += copylen;
  99. len -= copylen;
  100. }
  101. /*
  102. * From RFC 4340, A.2:
  103. *
  104. * For each acknowledgement it sends, the HC-Receiver will add an
  105. * acknowledgement record. ack_seqno will equal the HC-Receiver
  106. * sequence number it used for the ack packet; ack_ptr will equal
  107. * buf_head; ack_ackno will equal buf_ackno; and ack_nonce will
  108. * equal buf_nonce.
  109. */
  110. avr->avr_ack_seqno = DCCP_SKB_CB(skb)->dccpd_seq;
  111. avr->avr_ack_ptr = av->av_buf_head;
  112. avr->avr_ack_ackno = av->av_buf_ackno;
  113. avr->avr_ack_nonce = av->av_buf_nonce;
  114. avr->avr_sent_len = av->av_vec_len;
  115. dccp_ackvec_insert_avr(av, avr);
  116. dccp_pr_debug("%s ACK Vector 0, len=%d, ack_seqno=%llu, "
  117. "ack_ackno=%llu\n",
  118. dccp_role(sk), avr->avr_sent_len,
  119. (unsigned long long)avr->avr_ack_seqno,
  120. (unsigned long long)avr->avr_ack_ackno);
  121. return 0;
  122. }
  123. struct dccp_ackvec *dccp_ackvec_alloc(const gfp_t priority)
  124. {
  125. struct dccp_ackvec *av = kmem_cache_alloc(dccp_ackvec_slab, priority);
  126. if (av != NULL) {
  127. av->av_buf_head = DCCP_MAX_ACKVEC_LEN - 1;
  128. av->av_buf_ackno = UINT48_MAX + 1;
  129. av->av_buf_nonce = 0;
  130. av->av_time = ktime_set(0, 0);
  131. av->av_vec_len = 0;
  132. INIT_LIST_HEAD(&av->av_records);
  133. }
  134. return av;
  135. }
  136. void dccp_ackvec_free(struct dccp_ackvec *av)
  137. {
  138. if (unlikely(av == NULL))
  139. return;
  140. if (!list_empty(&av->av_records)) {
  141. struct dccp_ackvec_record *avr, *next;
  142. list_for_each_entry_safe(avr, next, &av->av_records, avr_node) {
  143. list_del_init(&avr->avr_node);
  144. dccp_ackvec_record_delete(avr);
  145. }
  146. }
  147. kmem_cache_free(dccp_ackvec_slab, av);
  148. }
  149. static inline u8 dccp_ackvec_state(const struct dccp_ackvec *av,
  150. const u32 index)
  151. {
  152. return av->av_buf[index] & DCCP_ACKVEC_STATE_MASK;
  153. }
  154. static inline u8 dccp_ackvec_len(const struct dccp_ackvec *av,
  155. const u32 index)
  156. {
  157. return av->av_buf[index] & DCCP_ACKVEC_LEN_MASK;
  158. }
  159. /*
  160. * If several packets are missing, the HC-Receiver may prefer to enter multiple
  161. * bytes with run length 0, rather than a single byte with a larger run length;
  162. * this simplifies table updates if one of the missing packets arrives.
  163. */
  164. static inline int dccp_ackvec_set_buf_head_state(struct dccp_ackvec *av,
  165. const unsigned int packets,
  166. const unsigned char state)
  167. {
  168. unsigned int gap;
  169. long new_head;
  170. if (av->av_vec_len + packets > DCCP_MAX_ACKVEC_LEN)
  171. return -ENOBUFS;
  172. gap = packets - 1;
  173. new_head = av->av_buf_head - packets;
  174. if (new_head < 0) {
  175. if (gap > 0) {
  176. memset(av->av_buf, DCCP_ACKVEC_STATE_NOT_RECEIVED,
  177. gap + new_head + 1);
  178. gap = -new_head;
  179. }
  180. new_head += DCCP_MAX_ACKVEC_LEN;
  181. }
  182. av->av_buf_head = new_head;
  183. if (gap > 0)
  184. memset(av->av_buf + av->av_buf_head + 1,
  185. DCCP_ACKVEC_STATE_NOT_RECEIVED, gap);
  186. av->av_buf[av->av_buf_head] = state;
  187. av->av_vec_len += packets;
  188. return 0;
  189. }
  190. /*
  191. * Implements the RFC 4340, Appendix A
  192. */
  193. int dccp_ackvec_add(struct dccp_ackvec *av, const struct sock *sk,
  194. const u64 ackno, const u8 state)
  195. {
  196. /*
  197. * Check at the right places if the buffer is full, if it is, tell the
  198. * caller to start dropping packets till the HC-Sender acks our ACK
  199. * vectors, when we will free up space in av_buf.
  200. *
  201. * We may well decide to do buffer compression, etc, but for now lets
  202. * just drop.
  203. *
  204. * From Appendix A.1.1 (`New Packets'):
  205. *
  206. * Of course, the circular buffer may overflow, either when the
  207. * HC-Sender is sending data at a very high rate, when the
  208. * HC-Receiver's acknowledgements are not reaching the HC-Sender,
  209. * or when the HC-Sender is forgetting to acknowledge those acks
  210. * (so the HC-Receiver is unable to clean up old state). In this
  211. * case, the HC-Receiver should either compress the buffer (by
  212. * increasing run lengths when possible), transfer its state to
  213. * a larger buffer, or, as a last resort, drop all received
  214. * packets, without processing them whatsoever, until its buffer
  215. * shrinks again.
  216. */
  217. /* See if this is the first ackno being inserted */
  218. if (av->av_vec_len == 0) {
  219. av->av_buf[av->av_buf_head] = state;
  220. av->av_vec_len = 1;
  221. } else if (after48(ackno, av->av_buf_ackno)) {
  222. const u64 delta = dccp_delta_seqno(av->av_buf_ackno, ackno);
  223. /*
  224. * Look if the state of this packet is the same as the
  225. * previous ackno and if so if we can bump the head len.
  226. */
  227. if (delta == 1 &&
  228. dccp_ackvec_state(av, av->av_buf_head) == state &&
  229. dccp_ackvec_len(av, av->av_buf_head) < DCCP_ACKVEC_LEN_MASK)
  230. av->av_buf[av->av_buf_head]++;
  231. else if (dccp_ackvec_set_buf_head_state(av, delta, state))
  232. return -ENOBUFS;
  233. } else {
  234. /*
  235. * A.1.2. Old Packets
  236. *
  237. * When a packet with Sequence Number S <= buf_ackno
  238. * arrives, the HC-Receiver will scan the table for
  239. * the byte corresponding to S. (Indexing structures
  240. * could reduce the complexity of this scan.)
  241. */
  242. u64 delta = dccp_delta_seqno(ackno, av->av_buf_ackno);
  243. u32 index = av->av_buf_head;
  244. while (1) {
  245. const u8 len = dccp_ackvec_len(av, index);
  246. const u8 av_state = dccp_ackvec_state(av, index);
  247. /*
  248. * valid packets not yet in av_buf have a reserved
  249. * entry, with a len equal to 0.
  250. */
  251. if (av_state == DCCP_ACKVEC_STATE_NOT_RECEIVED &&
  252. len == 0 && delta == 0) { /* Found our
  253. reserved seat! */
  254. dccp_pr_debug("Found %llu reserved seat!\n",
  255. (unsigned long long)ackno);
  256. av->av_buf[index] = state;
  257. goto out;
  258. }
  259. /* len == 0 means one packet */
  260. if (delta < len + 1)
  261. goto out_duplicate;
  262. delta -= len + 1;
  263. if (++index == DCCP_MAX_ACKVEC_LEN)
  264. index = 0;
  265. }
  266. }
  267. av->av_buf_ackno = ackno;
  268. av->av_time = ktime_get_real();
  269. out:
  270. return 0;
  271. out_duplicate:
  272. /* Duplicate packet */
  273. dccp_pr_debug("Received a dup or already considered lost "
  274. "packet: %llu\n", (unsigned long long)ackno);
  275. return -EILSEQ;
  276. }
  277. static void dccp_ackvec_throw_record(struct dccp_ackvec *av,
  278. struct dccp_ackvec_record *avr)
  279. {
  280. struct dccp_ackvec_record *next;
  281. /* sort out vector length */
  282. if (av->av_buf_head <= avr->avr_ack_ptr)
  283. av->av_vec_len = avr->avr_ack_ptr - av->av_buf_head;
  284. else
  285. av->av_vec_len = DCCP_MAX_ACKVEC_LEN - 1 -
  286. av->av_buf_head + avr->avr_ack_ptr;
  287. /* free records */
  288. list_for_each_entry_safe_from(avr, next, &av->av_records, avr_node) {
  289. list_del_init(&avr->avr_node);
  290. dccp_ackvec_record_delete(avr);
  291. }
  292. }
  293. void dccp_ackvec_check_rcv_ackno(struct dccp_ackvec *av, struct sock *sk,
  294. const u64 ackno)
  295. {
  296. struct dccp_ackvec_record *avr;
  297. /*
  298. * If we traverse backwards, it should be faster when we have large
  299. * windows. We will be receiving ACKs for stuff we sent a while back
  300. * -sorbo.
  301. */
  302. list_for_each_entry_reverse(avr, &av->av_records, avr_node) {
  303. if (ackno == avr->avr_ack_seqno) {
  304. dccp_pr_debug("%s ACK packet 0, len=%d, ack_seqno=%llu, "
  305. "ack_ackno=%llu, ACKED!\n",
  306. dccp_role(sk), 1,
  307. (unsigned long long)avr->avr_ack_seqno,
  308. (unsigned long long)avr->avr_ack_ackno);
  309. dccp_ackvec_throw_record(av, avr);
  310. break;
  311. } else if (avr->avr_ack_seqno > ackno)
  312. break; /* old news */
  313. }
  314. }
  315. static void dccp_ackvec_check_rcv_ackvector(struct dccp_ackvec *av,
  316. struct sock *sk, u64 *ackno,
  317. const unsigned char len,
  318. const unsigned char *vector)
  319. {
  320. unsigned char i;
  321. struct dccp_ackvec_record *avr;
  322. /* Check if we actually sent an ACK vector */
  323. if (list_empty(&av->av_records))
  324. return;
  325. i = len;
  326. /*
  327. * XXX
  328. * I think it might be more efficient to work backwards. See comment on
  329. * rcv_ackno. -sorbo.
  330. */
  331. avr = list_entry(av->av_records.next, struct dccp_ackvec_record, avr_node);
  332. while (i--) {
  333. const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
  334. u64 ackno_end_rl;
  335. dccp_set_seqno(&ackno_end_rl, *ackno - rl);
  336. /*
  337. * If our AVR sequence number is greater than the ack, go
  338. * forward in the AVR list until it is not so.
  339. */
  340. list_for_each_entry_from(avr, &av->av_records, avr_node) {
  341. if (!after48(avr->avr_ack_seqno, *ackno))
  342. goto found;
  343. }
  344. /* End of the av_records list, not found, exit */
  345. break;
  346. found:
  347. if (between48(avr->avr_ack_seqno, ackno_end_rl, *ackno)) {
  348. const u8 state = *vector & DCCP_ACKVEC_STATE_MASK;
  349. if (state != DCCP_ACKVEC_STATE_NOT_RECEIVED) {
  350. dccp_pr_debug("%s ACK vector 0, len=%d, "
  351. "ack_seqno=%llu, ack_ackno=%llu, "
  352. "ACKED!\n",
  353. dccp_role(sk), len,
  354. (unsigned long long)
  355. avr->avr_ack_seqno,
  356. (unsigned long long)
  357. avr->avr_ack_ackno);
  358. dccp_ackvec_throw_record(av, avr);
  359. break;
  360. }
  361. /*
  362. * If it wasn't received, continue scanning... we might
  363. * find another one.
  364. */
  365. }
  366. dccp_set_seqno(ackno, ackno_end_rl - 1);
  367. ++vector;
  368. }
  369. }
  370. int dccp_ackvec_parse(struct sock *sk, const struct sk_buff *skb,
  371. u64 *ackno, const u8 opt, const u8 *value, const u8 len)
  372. {
  373. if (len > DCCP_SINGLE_OPT_MAXLEN)
  374. return -1;
  375. /* dccp_ackvector_print(DCCP_SKB_CB(skb)->dccpd_ack_seq, value, len); */
  376. dccp_ackvec_check_rcv_ackvector(dccp_sk(sk)->dccps_hc_rx_ackvec, sk,
  377. ackno, len, value);
  378. return 0;
  379. }
  380. int __init dccp_ackvec_init(void)
  381. {
  382. dccp_ackvec_slab = kmem_cache_create("dccp_ackvec",
  383. sizeof(struct dccp_ackvec), 0,
  384. SLAB_HWCACHE_ALIGN, NULL);
  385. if (dccp_ackvec_slab == NULL)
  386. goto out_err;
  387. dccp_ackvec_record_slab =
  388. kmem_cache_create("dccp_ackvec_record",
  389. sizeof(struct dccp_ackvec_record),
  390. 0, SLAB_HWCACHE_ALIGN, NULL);
  391. if (dccp_ackvec_record_slab == NULL)
  392. goto out_destroy_slab;
  393. return 0;
  394. out_destroy_slab:
  395. kmem_cache_destroy(dccp_ackvec_slab);
  396. dccp_ackvec_slab = NULL;
  397. out_err:
  398. DCCP_CRIT("Unable to create Ack Vector slab cache");
  399. return -ENOBUFS;
  400. }
  401. void dccp_ackvec_exit(void)
  402. {
  403. if (dccp_ackvec_slab != NULL) {
  404. kmem_cache_destroy(dccp_ackvec_slab);
  405. dccp_ackvec_slab = NULL;
  406. }
  407. if (dccp_ackvec_record_slab != NULL) {
  408. kmem_cache_destroy(dccp_ackvec_record_slab);
  409. dccp_ackvec_record_slab = NULL;
  410. }
  411. }