ackvec.c 12 KB

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