ackvec.c 13 KB

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