ackvec.c 14 KB

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