ackvec.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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/skbuff.h>
  15. #include <net/sock.h>
  16. int dccp_insert_option_ackvec(struct sock *sk, struct sk_buff *skb)
  17. {
  18. struct dccp_sock *dp = dccp_sk(sk);
  19. struct dccp_ackvec *av = dp->dccps_hc_rx_ackvec;
  20. int len = av->dccpav_vec_len + 2;
  21. struct timeval now;
  22. u32 elapsed_time;
  23. unsigned char *to, *from;
  24. dccp_timestamp(sk, &now);
  25. elapsed_time = timeval_delta(&now, &av->dccpav_time) / 10;
  26. if (elapsed_time != 0)
  27. dccp_insert_option_elapsed_time(sk, skb, elapsed_time);
  28. if (DCCP_SKB_CB(skb)->dccpd_opt_len + len > DCCP_MAX_OPT_LEN)
  29. return -1;
  30. /*
  31. * XXX: now we have just one ack vector sent record, so
  32. * we have to wait for it to be cleared.
  33. *
  34. * Of course this is not acceptable, but this is just for
  35. * basic testing now.
  36. */
  37. if (av->dccpav_ack_seqno != DCCP_MAX_SEQNO + 1)
  38. return -1;
  39. DCCP_SKB_CB(skb)->dccpd_opt_len += len;
  40. to = skb_push(skb, len);
  41. *to++ = DCCPO_ACK_VECTOR_0;
  42. *to++ = len;
  43. len = av->dccpav_vec_len;
  44. from = av->dccpav_buf + av->dccpav_buf_head;
  45. /* Check if buf_head wraps */
  46. if ((int)av->dccpav_buf_head + len > av->dccpav_vec_len) {
  47. const u32 tailsize = av->dccpav_vec_len - av->dccpav_buf_head;
  48. memcpy(to, from, tailsize);
  49. to += tailsize;
  50. len -= tailsize;
  51. from = av->dccpav_buf;
  52. }
  53. memcpy(to, from, len);
  54. /*
  55. * From draft-ietf-dccp-spec-11.txt:
  56. *
  57. * For each acknowledgement it sends, the HC-Receiver will add an
  58. * acknowledgement record. ack_seqno will equal the HC-Receiver
  59. * sequence number it used for the ack packet; ack_ptr will equal
  60. * buf_head; ack_ackno will equal buf_ackno; and ack_nonce will
  61. * equal buf_nonce.
  62. *
  63. * This implemention uses just one ack record for now.
  64. */
  65. av->dccpav_ack_seqno = DCCP_SKB_CB(skb)->dccpd_seq;
  66. av->dccpav_ack_ptr = av->dccpav_buf_head;
  67. av->dccpav_ack_ackno = av->dccpav_buf_ackno;
  68. av->dccpav_ack_nonce = av->dccpav_buf_nonce;
  69. av->dccpav_sent_len = av->dccpav_vec_len;
  70. dccp_pr_debug("%sACK Vector 0, len=%d, ack_seqno=%llu, "
  71. "ack_ackno=%llu\n",
  72. debug_prefix, av->dccpav_sent_len,
  73. (unsigned long long)av->dccpav_ack_seqno,
  74. (unsigned long long)av->dccpav_ack_ackno);
  75. return -1;
  76. }
  77. struct dccp_ackvec *dccp_ackvec_alloc(const unsigned int len,
  78. const gfp_t priority)
  79. {
  80. struct dccp_ackvec *av;
  81. BUG_ON(len == 0);
  82. if (len > DCCP_MAX_ACKVEC_LEN)
  83. return NULL;
  84. av = kmalloc(sizeof(*av) + len, priority);
  85. if (av != NULL) {
  86. av->dccpav_buf_len = len;
  87. av->dccpav_buf_head =
  88. av->dccpav_buf_tail = av->dccpav_buf_len - 1;
  89. av->dccpav_buf_ackno =
  90. av->dccpav_ack_ackno = av->dccpav_ack_seqno = ~0LLU;
  91. av->dccpav_buf_nonce = av->dccpav_buf_nonce = 0;
  92. av->dccpav_ack_ptr = 0;
  93. av->dccpav_time.tv_sec = 0;
  94. av->dccpav_time.tv_usec = 0;
  95. av->dccpav_sent_len = av->dccpav_vec_len = 0;
  96. }
  97. return av;
  98. }
  99. void dccp_ackvec_free(struct dccp_ackvec *av)
  100. {
  101. kfree(av);
  102. }
  103. static inline u8 dccp_ackvec_state(const struct dccp_ackvec *av,
  104. const u8 index)
  105. {
  106. return av->dccpav_buf[index] & DCCP_ACKVEC_STATE_MASK;
  107. }
  108. static inline u8 dccp_ackvec_len(const struct dccp_ackvec *av,
  109. const u8 index)
  110. {
  111. return av->dccpav_buf[index] & DCCP_ACKVEC_LEN_MASK;
  112. }
  113. /*
  114. * If several packets are missing, the HC-Receiver may prefer to enter multiple
  115. * bytes with run length 0, rather than a single byte with a larger run length;
  116. * this simplifies table updates if one of the missing packets arrives.
  117. */
  118. static inline int dccp_ackvec_set_buf_head_state(struct dccp_ackvec *av,
  119. const unsigned int packets,
  120. const unsigned char state)
  121. {
  122. unsigned int gap;
  123. long new_head;
  124. if (av->dccpav_vec_len + packets > av->dccpav_buf_len)
  125. return -ENOBUFS;
  126. gap = packets - 1;
  127. new_head = av->dccpav_buf_head - packets;
  128. if (new_head < 0) {
  129. if (gap > 0) {
  130. memset(av->dccpav_buf, DCCP_ACKVEC_STATE_NOT_RECEIVED,
  131. gap + new_head + 1);
  132. gap = -new_head;
  133. }
  134. new_head += av->dccpav_buf_len;
  135. }
  136. av->dccpav_buf_head = new_head;
  137. if (gap > 0)
  138. memset(av->dccpav_buf + av->dccpav_buf_head + 1,
  139. DCCP_ACKVEC_STATE_NOT_RECEIVED, gap);
  140. av->dccpav_buf[av->dccpav_buf_head] = state;
  141. av->dccpav_vec_len += packets;
  142. return 0;
  143. }
  144. /*
  145. * Implements the draft-ietf-dccp-spec-11.txt Appendix A
  146. */
  147. int dccp_ackvec_add(struct dccp_ackvec *av, const struct sock *sk,
  148. const u64 ackno, const u8 state)
  149. {
  150. /*
  151. * Check at the right places if the buffer is full, if it is, tell the
  152. * caller to start dropping packets till the HC-Sender acks our ACK
  153. * vectors, when we will free up space in dccpav_buf.
  154. *
  155. * We may well decide to do buffer compression, etc, but for now lets
  156. * just drop.
  157. *
  158. * From Appendix A:
  159. *
  160. * Of course, the circular buffer may overflow, either when the
  161. * HC-Sender is sending data at a very high rate, when the
  162. * HC-Receiver's acknowledgements are not reaching the HC-Sender,
  163. * or when the HC-Sender is forgetting to acknowledge those acks
  164. * (so the HC-Receiver is unable to clean up old state). In this
  165. * case, the HC-Receiver should either compress the buffer (by
  166. * increasing run lengths when possible), transfer its state to
  167. * a larger buffer, or, as a last resort, drop all received
  168. * packets, without processing them whatsoever, until its buffer
  169. * shrinks again.
  170. */
  171. /* See if this is the first ackno being inserted */
  172. if (av->dccpav_vec_len == 0) {
  173. av->dccpav_buf[av->dccpav_buf_head] = state;
  174. av->dccpav_vec_len = 1;
  175. } else if (after48(ackno, av->dccpav_buf_ackno)) {
  176. const u64 delta = dccp_delta_seqno(av->dccpav_buf_ackno,
  177. ackno);
  178. /*
  179. * Look if the state of this packet is the same as the
  180. * previous ackno and if so if we can bump the head len.
  181. */
  182. if (delta == 1 &&
  183. dccp_ackvec_state(av, av->dccpav_buf_head) == state &&
  184. (dccp_ackvec_len(av, av->dccpav_buf_head) <
  185. DCCP_ACKVEC_LEN_MASK))
  186. av->dccpav_buf[av->dccpav_buf_head]++;
  187. else if (dccp_ackvec_set_buf_head_state(av, delta, state))
  188. return -ENOBUFS;
  189. } else {
  190. /*
  191. * A.1.2. Old Packets
  192. *
  193. * When a packet with Sequence Number S arrives, and
  194. * S <= buf_ackno, the HC-Receiver will scan the table
  195. * for the byte corresponding to S. (Indexing structures
  196. * could reduce the complexity of this scan.)
  197. */
  198. u64 delta = dccp_delta_seqno(ackno, av->dccpav_buf_ackno);
  199. u8 index = av->dccpav_buf_head;
  200. while (1) {
  201. const u8 len = dccp_ackvec_len(av, index);
  202. const u8 state = dccp_ackvec_state(av, index);
  203. /*
  204. * valid packets not yet in dccpav_buf have a reserved
  205. * entry, with a len equal to 0.
  206. */
  207. if (state == DCCP_ACKVEC_STATE_NOT_RECEIVED &&
  208. len == 0 && delta == 0) { /* Found our
  209. reserved seat! */
  210. dccp_pr_debug("Found %llu reserved seat!\n",
  211. (unsigned long long)ackno);
  212. av->dccpav_buf[index] = state;
  213. goto out;
  214. }
  215. /* len == 0 means one packet */
  216. if (delta < len + 1)
  217. goto out_duplicate;
  218. delta -= len + 1;
  219. if (++index == av->dccpav_buf_len)
  220. index = 0;
  221. }
  222. }
  223. av->dccpav_buf_ackno = ackno;
  224. dccp_timestamp(sk, &av->dccpav_time);
  225. out:
  226. dccp_pr_debug("");
  227. return 0;
  228. out_duplicate:
  229. /* Duplicate packet */
  230. dccp_pr_debug("Received a dup or already considered lost "
  231. "packet: %llu\n", (unsigned long long)ackno);
  232. return -EILSEQ;
  233. }
  234. #ifdef CONFIG_IP_DCCP_DEBUG
  235. void dccp_ackvector_print(const u64 ackno, const unsigned char *vector, int len)
  236. {
  237. if (!dccp_debug)
  238. return;
  239. printk("ACK vector len=%d, ackno=%llu |", len,
  240. (unsigned long long)ackno);
  241. while (len--) {
  242. const u8 state = (*vector & DCCP_ACKVEC_STATE_MASK) >> 6;
  243. const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
  244. printk("%d,%d|", state, rl);
  245. ++vector;
  246. }
  247. printk("\n");
  248. }
  249. void dccp_ackvec_print(const struct dccp_ackvec *av)
  250. {
  251. dccp_ackvector_print(av->dccpav_buf_ackno,
  252. av->dccpav_buf + av->dccpav_buf_head,
  253. av->dccpav_vec_len);
  254. }
  255. #endif
  256. static void dccp_ackvec_throw_away_ack_record(struct dccp_ackvec *av)
  257. {
  258. /*
  259. * As we're keeping track of the ack vector size (dccpav_vec_len) and
  260. * the sent ack vector size (dccpav_sent_len) we don't need
  261. * dccpav_buf_tail at all, but keep this code here as in the future
  262. * we'll implement a vector of ack records, as suggested in
  263. * draft-ietf-dccp-spec-11.txt Appendix A. -acme
  264. */
  265. #if 0
  266. u32 new_buf_tail = av->dccpav_ack_ptr + 1;
  267. if (new_buf_tail >= av->dccpav_vec_len)
  268. new_buf_tail -= av->dccpav_vec_len;
  269. av->dccpav_buf_tail = new_buf_tail;
  270. #endif
  271. av->dccpav_vec_len -= av->dccpav_sent_len;
  272. }
  273. void dccp_ackvec_check_rcv_ackno(struct dccp_ackvec *av, struct sock *sk,
  274. const u64 ackno)
  275. {
  276. /* Check if we actually sent an ACK vector */
  277. if (av->dccpav_ack_seqno == DCCP_MAX_SEQNO + 1)
  278. return;
  279. if (ackno == av->dccpav_ack_seqno) {
  280. #ifdef CONFIG_IP_DCCP_DEBUG
  281. struct dccp_sock *dp = dccp_sk(sk);
  282. const char *debug_prefix = dp->dccps_role == DCCP_ROLE_CLIENT ?
  283. "CLIENT rx ack: " : "server rx ack: ";
  284. #endif
  285. dccp_pr_debug("%sACK packet 0, len=%d, ack_seqno=%llu, "
  286. "ack_ackno=%llu, ACKED!\n",
  287. debug_prefix, 1,
  288. (unsigned long long)av->dccpav_ack_seqno,
  289. (unsigned long long)av->dccpav_ack_ackno);
  290. dccp_ackvec_throw_away_ack_record(av);
  291. av->dccpav_ack_seqno = DCCP_MAX_SEQNO + 1;
  292. }
  293. }
  294. static void dccp_ackvec_check_rcv_ackvector(struct dccp_ackvec *av,
  295. struct sock *sk, u64 ackno,
  296. const unsigned char len,
  297. const unsigned char *vector)
  298. {
  299. unsigned char i;
  300. /* Check if we actually sent an ACK vector */
  301. if (av->dccpav_ack_seqno == DCCP_MAX_SEQNO + 1)
  302. return;
  303. /*
  304. * We're in the receiver half connection, so if the received an ACK
  305. * vector ackno (e.g. 50) before dccpav_ack_seqno (e.g. 52), we're
  306. * not interested.
  307. *
  308. * Extra explanation with example:
  309. *
  310. * if we received an ACK vector with ackno 50, it can only be acking
  311. * 50, 49, 48, etc, not 52 (the seqno for the ACK vector we sent).
  312. */
  313. /* dccp_pr_debug("is %llu < %llu? ", ackno, av->dccpav_ack_seqno); */
  314. if (before48(ackno, av->dccpav_ack_seqno)) {
  315. /* dccp_pr_debug_cat("yes\n"); */
  316. return;
  317. }
  318. /* dccp_pr_debug_cat("no\n"); */
  319. i = len;
  320. while (i--) {
  321. const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
  322. u64 ackno_end_rl;
  323. dccp_set_seqno(&ackno_end_rl, ackno - rl);
  324. /*
  325. * dccp_pr_debug("is %llu <= %llu <= %llu? ", ackno_end_rl,
  326. * av->dccpav_ack_seqno, ackno);
  327. */
  328. if (between48(av->dccpav_ack_seqno, ackno_end_rl, ackno)) {
  329. const u8 state = (*vector &
  330. DCCP_ACKVEC_STATE_MASK) >> 6;
  331. /* dccp_pr_debug_cat("yes\n"); */
  332. if (state != DCCP_ACKVEC_STATE_NOT_RECEIVED) {
  333. #ifdef CONFIG_IP_DCCP_DEBUG
  334. struct dccp_sock *dp = dccp_sk(sk);
  335. const char *debug_prefix =
  336. dp->dccps_role == DCCP_ROLE_CLIENT ?
  337. "CLIENT rx ack: " : "server rx ack: ";
  338. #endif
  339. dccp_pr_debug("%sACK vector 0, len=%d, "
  340. "ack_seqno=%llu, ack_ackno=%llu, "
  341. "ACKED!\n",
  342. debug_prefix, len,
  343. (unsigned long long)
  344. av->dccpav_ack_seqno,
  345. (unsigned long long)
  346. av->dccpav_ack_ackno);
  347. dccp_ackvec_throw_away_ack_record(av);
  348. }
  349. /*
  350. * If dccpav_ack_seqno was not received, no problem
  351. * we'll send another ACK vector.
  352. */
  353. av->dccpav_ack_seqno = DCCP_MAX_SEQNO + 1;
  354. break;
  355. }
  356. /* dccp_pr_debug_cat("no\n"); */
  357. dccp_set_seqno(&ackno, ackno_end_rl - 1);
  358. ++vector;
  359. }
  360. }
  361. int dccp_ackvec_parse(struct sock *sk, const struct sk_buff *skb,
  362. const u8 opt, const u8 *value, const u8 len)
  363. {
  364. if (len > DCCP_MAX_ACKVEC_LEN)
  365. return -1;
  366. /* dccp_ackvector_print(DCCP_SKB_CB(skb)->dccpd_ack_seq, value, len); */
  367. dccp_ackvec_check_rcv_ackvector(dccp_sk(sk)->dccps_hc_rx_ackvec, sk,
  368. DCCP_SKB_CB(skb)->dccpd_ack_seq,
  369. len, value);
  370. return 0;
  371. }