ackvec.c 13 KB

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