ackvec.c 12 KB

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