ackvec.c 14 KB

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