ackvec.c 13 KB

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