ccid2.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. /*
  2. * net/dccp/ccids/ccid2.c
  3. *
  4. * Copyright (c) 2005, 2006 Andrea Bittau <a.bittau@cs.ucl.ac.uk>
  5. *
  6. * Changes to meet Linux coding standards, and DCCP infrastructure fixes.
  7. *
  8. * Copyright (c) 2006 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. /*
  25. * This implementation should follow RFC 4341
  26. */
  27. #include "../feat.h"
  28. #include "../ccid.h"
  29. #include "../dccp.h"
  30. #include "ccid2.h"
  31. #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
  32. static int ccid2_debug;
  33. #define ccid2_pr_debug(format, a...) DCCP_PR_DEBUG(ccid2_debug, format, ##a)
  34. static void ccid2_hc_tx_check_sanity(const struct ccid2_hc_tx_sock *hctx)
  35. {
  36. int len = 0;
  37. int pipe = 0;
  38. struct ccid2_seq *seqp = hctx->ccid2hctx_seqh;
  39. /* there is data in the chain */
  40. if (seqp != hctx->ccid2hctx_seqt) {
  41. seqp = seqp->ccid2s_prev;
  42. len++;
  43. if (!seqp->ccid2s_acked)
  44. pipe++;
  45. while (seqp != hctx->ccid2hctx_seqt) {
  46. struct ccid2_seq *prev = seqp->ccid2s_prev;
  47. len++;
  48. if (!prev->ccid2s_acked)
  49. pipe++;
  50. /* packets are sent sequentially */
  51. BUG_ON(dccp_delta_seqno(seqp->ccid2s_seq,
  52. prev->ccid2s_seq ) >= 0);
  53. BUG_ON(time_before(seqp->ccid2s_sent,
  54. prev->ccid2s_sent));
  55. seqp = prev;
  56. }
  57. }
  58. BUG_ON(pipe != hctx->ccid2hctx_pipe);
  59. ccid2_pr_debug("len of chain=%d\n", len);
  60. do {
  61. seqp = seqp->ccid2s_prev;
  62. len++;
  63. } while (seqp != hctx->ccid2hctx_seqh);
  64. ccid2_pr_debug("total len=%d\n", len);
  65. BUG_ON(len != hctx->ccid2hctx_seqbufc * CCID2_SEQBUF_LEN);
  66. }
  67. #else
  68. #define ccid2_pr_debug(format, a...)
  69. #define ccid2_hc_tx_check_sanity(hctx)
  70. #endif
  71. static int ccid2_hc_tx_alloc_seq(struct ccid2_hc_tx_sock *hctx)
  72. {
  73. struct ccid2_seq *seqp;
  74. int i;
  75. /* check if we have space to preserve the pointer to the buffer */
  76. if (hctx->ccid2hctx_seqbufc >= (sizeof(hctx->ccid2hctx_seqbuf) /
  77. sizeof(struct ccid2_seq*)))
  78. return -ENOMEM;
  79. /* allocate buffer and initialize linked list */
  80. seqp = kmalloc(CCID2_SEQBUF_LEN * sizeof(struct ccid2_seq), gfp_any());
  81. if (seqp == NULL)
  82. return -ENOMEM;
  83. for (i = 0; i < (CCID2_SEQBUF_LEN - 1); i++) {
  84. seqp[i].ccid2s_next = &seqp[i + 1];
  85. seqp[i + 1].ccid2s_prev = &seqp[i];
  86. }
  87. seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = seqp;
  88. seqp->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1];
  89. /* This is the first allocation. Initiate the head and tail. */
  90. if (hctx->ccid2hctx_seqbufc == 0)
  91. hctx->ccid2hctx_seqh = hctx->ccid2hctx_seqt = seqp;
  92. else {
  93. /* link the existing list with the one we just created */
  94. hctx->ccid2hctx_seqh->ccid2s_next = seqp;
  95. seqp->ccid2s_prev = hctx->ccid2hctx_seqh;
  96. hctx->ccid2hctx_seqt->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1];
  97. seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = hctx->ccid2hctx_seqt;
  98. }
  99. /* store the original pointer to the buffer so we can free it */
  100. hctx->ccid2hctx_seqbuf[hctx->ccid2hctx_seqbufc] = seqp;
  101. hctx->ccid2hctx_seqbufc++;
  102. return 0;
  103. }
  104. static int ccid2_hc_tx_send_packet(struct sock *sk, struct sk_buff *skb)
  105. {
  106. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  107. if (hctx->ccid2hctx_pipe < hctx->ccid2hctx_cwnd)
  108. return 0;
  109. return 1; /* XXX CCID should dequeue when ready instead of polling */
  110. }
  111. static void ccid2_change_l_ack_ratio(struct sock *sk, u32 val)
  112. {
  113. struct dccp_sock *dp = dccp_sk(sk);
  114. u32 max_ratio = DIV_ROUND_UP(ccid2_hc_tx_sk(sk)->ccid2hctx_cwnd, 2);
  115. /*
  116. * Ensure that Ack Ratio does not exceed ceil(cwnd/2), which is (2) from
  117. * RFC 4341, 6.1.2. We ignore the statement that Ack Ratio 2 is always
  118. * acceptable since this causes starvation/deadlock whenever cwnd < 2.
  119. * The same problem arises when Ack Ratio is 0 (ie. Ack Ratio disabled).
  120. */
  121. if (val == 0 || val > max_ratio) {
  122. DCCP_WARN("Limiting Ack Ratio (%u) to %u\n", val, max_ratio);
  123. val = max_ratio;
  124. }
  125. if (val > DCCPF_ACK_RATIO_MAX)
  126. val = DCCPF_ACK_RATIO_MAX;
  127. if (val == dp->dccps_l_ack_ratio)
  128. return;
  129. ccid2_pr_debug("changing local ack ratio to %u\n", val);
  130. dp->dccps_l_ack_ratio = val;
  131. }
  132. static void ccid2_change_srtt(struct ccid2_hc_tx_sock *hctx, long val)
  133. {
  134. ccid2_pr_debug("change SRTT to %ld\n", val);
  135. hctx->ccid2hctx_srtt = val;
  136. }
  137. static void ccid2_start_rto_timer(struct sock *sk);
  138. static void ccid2_hc_tx_rto_expire(unsigned long data)
  139. {
  140. struct sock *sk = (struct sock *)data;
  141. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  142. long s;
  143. bh_lock_sock(sk);
  144. if (sock_owned_by_user(sk)) {
  145. sk_reset_timer(sk, &hctx->ccid2hctx_rtotimer,
  146. jiffies + HZ / 5);
  147. goto out;
  148. }
  149. ccid2_pr_debug("RTO_EXPIRE\n");
  150. ccid2_hc_tx_check_sanity(hctx);
  151. /* back-off timer */
  152. hctx->ccid2hctx_rto <<= 1;
  153. s = hctx->ccid2hctx_rto / HZ;
  154. if (s > 60)
  155. hctx->ccid2hctx_rto = 60 * HZ;
  156. ccid2_start_rto_timer(sk);
  157. /* adjust pipe, cwnd etc */
  158. hctx->ccid2hctx_ssthresh = hctx->ccid2hctx_cwnd / 2;
  159. if (hctx->ccid2hctx_ssthresh < 2)
  160. hctx->ccid2hctx_ssthresh = 2;
  161. hctx->ccid2hctx_cwnd = 1;
  162. hctx->ccid2hctx_pipe = 0;
  163. /* clear state about stuff we sent */
  164. hctx->ccid2hctx_seqt = hctx->ccid2hctx_seqh;
  165. hctx->ccid2hctx_packets_acked = 0;
  166. /* clear ack ratio state. */
  167. hctx->ccid2hctx_rpseq = 0;
  168. hctx->ccid2hctx_rpdupack = -1;
  169. ccid2_change_l_ack_ratio(sk, 1);
  170. ccid2_hc_tx_check_sanity(hctx);
  171. out:
  172. bh_unlock_sock(sk);
  173. sock_put(sk);
  174. }
  175. static void ccid2_start_rto_timer(struct sock *sk)
  176. {
  177. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  178. ccid2_pr_debug("setting RTO timeout=%ld\n", hctx->ccid2hctx_rto);
  179. BUG_ON(timer_pending(&hctx->ccid2hctx_rtotimer));
  180. sk_reset_timer(sk, &hctx->ccid2hctx_rtotimer,
  181. jiffies + hctx->ccid2hctx_rto);
  182. }
  183. static void ccid2_hc_tx_packet_sent(struct sock *sk, int more, unsigned int len)
  184. {
  185. struct dccp_sock *dp = dccp_sk(sk);
  186. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  187. struct ccid2_seq *next;
  188. hctx->ccid2hctx_pipe++;
  189. hctx->ccid2hctx_seqh->ccid2s_seq = dp->dccps_gss;
  190. hctx->ccid2hctx_seqh->ccid2s_acked = 0;
  191. hctx->ccid2hctx_seqh->ccid2s_sent = jiffies;
  192. next = hctx->ccid2hctx_seqh->ccid2s_next;
  193. /* check if we need to alloc more space */
  194. if (next == hctx->ccid2hctx_seqt) {
  195. if (ccid2_hc_tx_alloc_seq(hctx)) {
  196. DCCP_CRIT("packet history - out of memory!");
  197. /* FIXME: find a more graceful way to bail out */
  198. return;
  199. }
  200. next = hctx->ccid2hctx_seqh->ccid2s_next;
  201. BUG_ON(next == hctx->ccid2hctx_seqt);
  202. }
  203. hctx->ccid2hctx_seqh = next;
  204. ccid2_pr_debug("cwnd=%d pipe=%d\n", hctx->ccid2hctx_cwnd,
  205. hctx->ccid2hctx_pipe);
  206. /*
  207. * FIXME: The code below is broken and the variables have been removed
  208. * from the socket struct. The `ackloss' variable was always set to 0,
  209. * and with arsent there are several problems:
  210. * (i) it doesn't just count the number of Acks, but all sent packets;
  211. * (ii) it is expressed in # of packets, not # of windows, so the
  212. * comparison below uses the wrong formula: Appendix A of RFC 4341
  213. * comes up with the number K = cwnd / (R^2 - R) of consecutive windows
  214. * of data with no lost or marked Ack packets. If arsent were the # of
  215. * consecutive Acks received without loss, then Ack Ratio needs to be
  216. * decreased by 1 when
  217. * arsent >= K * cwnd / R = cwnd^2 / (R^3 - R^2)
  218. * where cwnd / R is the number of Acks received per window of data
  219. * (cf. RFC 4341, App. A). The problems are that
  220. * - arsent counts other packets as well;
  221. * - the comparison uses a formula different from RFC 4341;
  222. * - computing a cubic/quadratic equation each time is too complicated.
  223. * Hence a different algorithm is needed.
  224. */
  225. #if 0
  226. /* Ack Ratio. Need to maintain a concept of how many windows we sent */
  227. hctx->ccid2hctx_arsent++;
  228. /* We had an ack loss in this window... */
  229. if (hctx->ccid2hctx_ackloss) {
  230. if (hctx->ccid2hctx_arsent >= hctx->ccid2hctx_cwnd) {
  231. hctx->ccid2hctx_arsent = 0;
  232. hctx->ccid2hctx_ackloss = 0;
  233. }
  234. } else {
  235. /* No acks lost up to now... */
  236. /* decrease ack ratio if enough packets were sent */
  237. if (dp->dccps_l_ack_ratio > 1) {
  238. /* XXX don't calculate denominator each time */
  239. int denom = dp->dccps_l_ack_ratio * dp->dccps_l_ack_ratio -
  240. dp->dccps_l_ack_ratio;
  241. denom = hctx->ccid2hctx_cwnd * hctx->ccid2hctx_cwnd / denom;
  242. if (hctx->ccid2hctx_arsent >= denom) {
  243. ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio - 1);
  244. hctx->ccid2hctx_arsent = 0;
  245. }
  246. } else {
  247. /* we can't increase ack ratio further [1] */
  248. hctx->ccid2hctx_arsent = 0; /* or maybe set it to cwnd*/
  249. }
  250. }
  251. #endif
  252. /* setup RTO timer */
  253. if (!timer_pending(&hctx->ccid2hctx_rtotimer))
  254. ccid2_start_rto_timer(sk);
  255. #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
  256. do {
  257. struct ccid2_seq *seqp = hctx->ccid2hctx_seqt;
  258. while (seqp != hctx->ccid2hctx_seqh) {
  259. ccid2_pr_debug("out seq=%llu acked=%d time=%lu\n",
  260. (unsigned long long)seqp->ccid2s_seq,
  261. seqp->ccid2s_acked, seqp->ccid2s_sent);
  262. seqp = seqp->ccid2s_next;
  263. }
  264. } while (0);
  265. ccid2_pr_debug("=========\n");
  266. ccid2_hc_tx_check_sanity(hctx);
  267. #endif
  268. }
  269. /* XXX Lame code duplication!
  270. * returns -1 if none was found.
  271. * else returns the next offset to use in the function call.
  272. */
  273. static int ccid2_ackvector(struct sock *sk, struct sk_buff *skb, int offset,
  274. unsigned char **vec, unsigned char *veclen)
  275. {
  276. const struct dccp_hdr *dh = dccp_hdr(skb);
  277. unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb);
  278. unsigned char *opt_ptr;
  279. const unsigned char *opt_end = (unsigned char *)dh +
  280. (dh->dccph_doff * 4);
  281. unsigned char opt, len;
  282. unsigned char *value;
  283. BUG_ON(offset < 0);
  284. options += offset;
  285. opt_ptr = options;
  286. if (opt_ptr >= opt_end)
  287. return -1;
  288. while (opt_ptr != opt_end) {
  289. opt = *opt_ptr++;
  290. len = 0;
  291. value = NULL;
  292. /* Check if this isn't a single byte option */
  293. if (opt > DCCPO_MAX_RESERVED) {
  294. if (opt_ptr == opt_end)
  295. goto out_invalid_option;
  296. len = *opt_ptr++;
  297. if (len < 3)
  298. goto out_invalid_option;
  299. /*
  300. * Remove the type and len fields, leaving
  301. * just the value size
  302. */
  303. len -= 2;
  304. value = opt_ptr;
  305. opt_ptr += len;
  306. if (opt_ptr > opt_end)
  307. goto out_invalid_option;
  308. }
  309. switch (opt) {
  310. case DCCPO_ACK_VECTOR_0:
  311. case DCCPO_ACK_VECTOR_1:
  312. *vec = value;
  313. *veclen = len;
  314. return offset + (opt_ptr - options);
  315. }
  316. }
  317. return -1;
  318. out_invalid_option:
  319. DCCP_BUG("Invalid option - this should not happen (previous parsing)!");
  320. return -1;
  321. }
  322. static void ccid2_hc_tx_kill_rto_timer(struct sock *sk)
  323. {
  324. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  325. sk_stop_timer(sk, &hctx->ccid2hctx_rtotimer);
  326. ccid2_pr_debug("deleted RTO timer\n");
  327. }
  328. static inline void ccid2_new_ack(struct sock *sk,
  329. struct ccid2_seq *seqp,
  330. unsigned int *maxincr)
  331. {
  332. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  333. if (hctx->ccid2hctx_cwnd < hctx->ccid2hctx_ssthresh) {
  334. if (*maxincr > 0 && ++hctx->ccid2hctx_packets_acked == 2) {
  335. hctx->ccid2hctx_cwnd += 1;
  336. *maxincr -= 1;
  337. hctx->ccid2hctx_packets_acked = 0;
  338. }
  339. } else if (++hctx->ccid2hctx_packets_acked >= hctx->ccid2hctx_cwnd) {
  340. hctx->ccid2hctx_cwnd += 1;
  341. hctx->ccid2hctx_packets_acked = 0;
  342. }
  343. /* update RTO */
  344. if (hctx->ccid2hctx_srtt == -1 ||
  345. time_after(jiffies, hctx->ccid2hctx_lastrtt + hctx->ccid2hctx_srtt)) {
  346. unsigned long r = (long)jiffies - (long)seqp->ccid2s_sent;
  347. int s;
  348. /* first measurement */
  349. if (hctx->ccid2hctx_srtt == -1) {
  350. ccid2_pr_debug("R: %lu Time=%lu seq=%llu\n",
  351. r, jiffies,
  352. (unsigned long long)seqp->ccid2s_seq);
  353. ccid2_change_srtt(hctx, r);
  354. hctx->ccid2hctx_rttvar = r >> 1;
  355. } else {
  356. /* RTTVAR */
  357. long tmp = hctx->ccid2hctx_srtt - r;
  358. long srtt;
  359. if (tmp < 0)
  360. tmp *= -1;
  361. tmp >>= 2;
  362. hctx->ccid2hctx_rttvar *= 3;
  363. hctx->ccid2hctx_rttvar >>= 2;
  364. hctx->ccid2hctx_rttvar += tmp;
  365. /* SRTT */
  366. srtt = hctx->ccid2hctx_srtt;
  367. srtt *= 7;
  368. srtt >>= 3;
  369. tmp = r >> 3;
  370. srtt += tmp;
  371. ccid2_change_srtt(hctx, srtt);
  372. }
  373. s = hctx->ccid2hctx_rttvar << 2;
  374. /* clock granularity is 1 when based on jiffies */
  375. if (!s)
  376. s = 1;
  377. hctx->ccid2hctx_rto = hctx->ccid2hctx_srtt + s;
  378. /* must be at least a second */
  379. s = hctx->ccid2hctx_rto / HZ;
  380. /* DCCP doesn't require this [but I like it cuz my code sux] */
  381. #if 1
  382. if (s < 1)
  383. hctx->ccid2hctx_rto = HZ;
  384. #endif
  385. /* max 60 seconds */
  386. if (s > 60)
  387. hctx->ccid2hctx_rto = HZ * 60;
  388. hctx->ccid2hctx_lastrtt = jiffies;
  389. ccid2_pr_debug("srtt: %ld rttvar: %ld rto: %ld (HZ=%d) R=%lu\n",
  390. hctx->ccid2hctx_srtt, hctx->ccid2hctx_rttvar,
  391. hctx->ccid2hctx_rto, HZ, r);
  392. }
  393. /* we got a new ack, so re-start RTO timer */
  394. ccid2_hc_tx_kill_rto_timer(sk);
  395. ccid2_start_rto_timer(sk);
  396. }
  397. static void ccid2_hc_tx_dec_pipe(struct sock *sk)
  398. {
  399. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  400. if (hctx->ccid2hctx_pipe == 0)
  401. DCCP_BUG("pipe == 0");
  402. else
  403. hctx->ccid2hctx_pipe--;
  404. if (hctx->ccid2hctx_pipe == 0)
  405. ccid2_hc_tx_kill_rto_timer(sk);
  406. }
  407. static void ccid2_congestion_event(struct sock *sk, struct ccid2_seq *seqp)
  408. {
  409. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  410. if (time_before(seqp->ccid2s_sent, hctx->ccid2hctx_last_cong)) {
  411. ccid2_pr_debug("Multiple losses in an RTT---treating as one\n");
  412. return;
  413. }
  414. hctx->ccid2hctx_last_cong = jiffies;
  415. hctx->ccid2hctx_cwnd = hctx->ccid2hctx_cwnd / 2 ? : 1U;
  416. hctx->ccid2hctx_ssthresh = max(hctx->ccid2hctx_cwnd, 2U);
  417. /* Avoid spurious timeouts resulting from Ack Ratio > cwnd */
  418. if (dccp_sk(sk)->dccps_l_ack_ratio > hctx->ccid2hctx_cwnd)
  419. ccid2_change_l_ack_ratio(sk, hctx->ccid2hctx_cwnd);
  420. }
  421. static void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
  422. {
  423. struct dccp_sock *dp = dccp_sk(sk);
  424. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  425. u64 ackno, seqno;
  426. struct ccid2_seq *seqp;
  427. unsigned char *vector;
  428. unsigned char veclen;
  429. int offset = 0;
  430. int done = 0;
  431. unsigned int maxincr = 0;
  432. ccid2_hc_tx_check_sanity(hctx);
  433. /* check reverse path congestion */
  434. seqno = DCCP_SKB_CB(skb)->dccpd_seq;
  435. /* XXX this whole "algorithm" is broken. Need to fix it to keep track
  436. * of the seqnos of the dupacks so that rpseq and rpdupack are correct
  437. * -sorbo.
  438. */
  439. /* need to bootstrap */
  440. if (hctx->ccid2hctx_rpdupack == -1) {
  441. hctx->ccid2hctx_rpdupack = 0;
  442. hctx->ccid2hctx_rpseq = seqno;
  443. } else {
  444. /* check if packet is consecutive */
  445. if (dccp_delta_seqno(hctx->ccid2hctx_rpseq, seqno) == 1)
  446. hctx->ccid2hctx_rpseq = seqno;
  447. /* it's a later packet */
  448. else if (after48(seqno, hctx->ccid2hctx_rpseq)) {
  449. hctx->ccid2hctx_rpdupack++;
  450. /* check if we got enough dupacks */
  451. if (hctx->ccid2hctx_rpdupack >= NUMDUPACK) {
  452. hctx->ccid2hctx_rpdupack = -1; /* XXX lame */
  453. hctx->ccid2hctx_rpseq = 0;
  454. ccid2_change_l_ack_ratio(sk, 2 * dp->dccps_l_ack_ratio);
  455. }
  456. }
  457. }
  458. /* check forward path congestion */
  459. /* still didn't send out new data packets */
  460. if (hctx->ccid2hctx_seqh == hctx->ccid2hctx_seqt)
  461. return;
  462. switch (DCCP_SKB_CB(skb)->dccpd_type) {
  463. case DCCP_PKT_ACK:
  464. case DCCP_PKT_DATAACK:
  465. break;
  466. default:
  467. return;
  468. }
  469. ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
  470. if (after48(ackno, hctx->ccid2hctx_high_ack))
  471. hctx->ccid2hctx_high_ack = ackno;
  472. seqp = hctx->ccid2hctx_seqt;
  473. while (before48(seqp->ccid2s_seq, ackno)) {
  474. seqp = seqp->ccid2s_next;
  475. if (seqp == hctx->ccid2hctx_seqh) {
  476. seqp = hctx->ccid2hctx_seqh->ccid2s_prev;
  477. break;
  478. }
  479. }
  480. /*
  481. * In slow-start, cwnd can increase up to a maximum of Ack Ratio/2
  482. * packets per acknowledgement. Rounding up avoids that cwnd is not
  483. * advanced when Ack Ratio is 1 and gives a slight edge otherwise.
  484. */
  485. if (hctx->ccid2hctx_cwnd < hctx->ccid2hctx_ssthresh)
  486. maxincr = DIV_ROUND_UP(dp->dccps_l_ack_ratio, 2);
  487. /* go through all ack vectors */
  488. while ((offset = ccid2_ackvector(sk, skb, offset,
  489. &vector, &veclen)) != -1) {
  490. /* go through this ack vector */
  491. while (veclen--) {
  492. const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
  493. u64 ackno_end_rl = SUB48(ackno, rl);
  494. ccid2_pr_debug("ackvec start:%llu end:%llu\n",
  495. (unsigned long long)ackno,
  496. (unsigned long long)ackno_end_rl);
  497. /* if the seqno we are analyzing is larger than the
  498. * current ackno, then move towards the tail of our
  499. * seqnos.
  500. */
  501. while (after48(seqp->ccid2s_seq, ackno)) {
  502. if (seqp == hctx->ccid2hctx_seqt) {
  503. done = 1;
  504. break;
  505. }
  506. seqp = seqp->ccid2s_prev;
  507. }
  508. if (done)
  509. break;
  510. /* check all seqnos in the range of the vector
  511. * run length
  512. */
  513. while (between48(seqp->ccid2s_seq,ackno_end_rl,ackno)) {
  514. const u8 state = *vector &
  515. DCCP_ACKVEC_STATE_MASK;
  516. /* new packet received or marked */
  517. if (state != DCCP_ACKVEC_STATE_NOT_RECEIVED &&
  518. !seqp->ccid2s_acked) {
  519. if (state ==
  520. DCCP_ACKVEC_STATE_ECN_MARKED) {
  521. ccid2_congestion_event(sk,
  522. seqp);
  523. } else
  524. ccid2_new_ack(sk, seqp,
  525. &maxincr);
  526. seqp->ccid2s_acked = 1;
  527. ccid2_pr_debug("Got ack for %llu\n",
  528. (unsigned long long)seqp->ccid2s_seq);
  529. ccid2_hc_tx_dec_pipe(sk);
  530. }
  531. if (seqp == hctx->ccid2hctx_seqt) {
  532. done = 1;
  533. break;
  534. }
  535. seqp = seqp->ccid2s_prev;
  536. }
  537. if (done)
  538. break;
  539. ackno = SUB48(ackno_end_rl, 1);
  540. vector++;
  541. }
  542. if (done)
  543. break;
  544. }
  545. /* The state about what is acked should be correct now
  546. * Check for NUMDUPACK
  547. */
  548. seqp = hctx->ccid2hctx_seqt;
  549. while (before48(seqp->ccid2s_seq, hctx->ccid2hctx_high_ack)) {
  550. seqp = seqp->ccid2s_next;
  551. if (seqp == hctx->ccid2hctx_seqh) {
  552. seqp = hctx->ccid2hctx_seqh->ccid2s_prev;
  553. break;
  554. }
  555. }
  556. done = 0;
  557. while (1) {
  558. if (seqp->ccid2s_acked) {
  559. done++;
  560. if (done == NUMDUPACK)
  561. break;
  562. }
  563. if (seqp == hctx->ccid2hctx_seqt)
  564. break;
  565. seqp = seqp->ccid2s_prev;
  566. }
  567. /* If there are at least 3 acknowledgements, anything unacknowledged
  568. * below the last sequence number is considered lost
  569. */
  570. if (done == NUMDUPACK) {
  571. struct ccid2_seq *last_acked = seqp;
  572. /* check for lost packets */
  573. while (1) {
  574. if (!seqp->ccid2s_acked) {
  575. ccid2_pr_debug("Packet lost: %llu\n",
  576. (unsigned long long)seqp->ccid2s_seq);
  577. /* XXX need to traverse from tail -> head in
  578. * order to detect multiple congestion events in
  579. * one ack vector.
  580. */
  581. ccid2_congestion_event(sk, seqp);
  582. ccid2_hc_tx_dec_pipe(sk);
  583. }
  584. if (seqp == hctx->ccid2hctx_seqt)
  585. break;
  586. seqp = seqp->ccid2s_prev;
  587. }
  588. hctx->ccid2hctx_seqt = last_acked;
  589. }
  590. /* trim acked packets in tail */
  591. while (hctx->ccid2hctx_seqt != hctx->ccid2hctx_seqh) {
  592. if (!hctx->ccid2hctx_seqt->ccid2s_acked)
  593. break;
  594. hctx->ccid2hctx_seqt = hctx->ccid2hctx_seqt->ccid2s_next;
  595. }
  596. ccid2_hc_tx_check_sanity(hctx);
  597. }
  598. static int ccid2_hc_tx_init(struct ccid *ccid, struct sock *sk)
  599. {
  600. struct ccid2_hc_tx_sock *hctx = ccid_priv(ccid);
  601. struct dccp_sock *dp = dccp_sk(sk);
  602. u32 max_ratio;
  603. /* RFC 4341, 5: initialise ssthresh to arbitrarily high (max) value */
  604. hctx->ccid2hctx_ssthresh = ~0U;
  605. /*
  606. * RFC 4341, 5: "The cwnd parameter is initialized to at most four
  607. * packets for new connections, following the rules from [RFC3390]".
  608. * We need to convert the bytes of RFC3390 into the packets of RFC 4341.
  609. */
  610. hctx->ccid2hctx_cwnd = clamp(4380U / dp->dccps_mss_cache, 2U, 4U);
  611. /* Make sure that Ack Ratio is enabled and within bounds. */
  612. max_ratio = DIV_ROUND_UP(hctx->ccid2hctx_cwnd, 2);
  613. if (dp->dccps_l_ack_ratio == 0 || dp->dccps_l_ack_ratio > max_ratio)
  614. dp->dccps_l_ack_ratio = max_ratio;
  615. /* XXX init ~ to window size... */
  616. if (ccid2_hc_tx_alloc_seq(hctx))
  617. return -ENOMEM;
  618. hctx->ccid2hctx_rto = 3 * HZ;
  619. ccid2_change_srtt(hctx, -1);
  620. hctx->ccid2hctx_rttvar = -1;
  621. hctx->ccid2hctx_rpdupack = -1;
  622. hctx->ccid2hctx_last_cong = jiffies;
  623. setup_timer(&hctx->ccid2hctx_rtotimer, ccid2_hc_tx_rto_expire,
  624. (unsigned long)sk);
  625. ccid2_hc_tx_check_sanity(hctx);
  626. return 0;
  627. }
  628. static void ccid2_hc_tx_exit(struct sock *sk)
  629. {
  630. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  631. int i;
  632. ccid2_hc_tx_kill_rto_timer(sk);
  633. for (i = 0; i < hctx->ccid2hctx_seqbufc; i++)
  634. kfree(hctx->ccid2hctx_seqbuf[i]);
  635. hctx->ccid2hctx_seqbufc = 0;
  636. }
  637. static void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
  638. {
  639. const struct dccp_sock *dp = dccp_sk(sk);
  640. struct ccid2_hc_rx_sock *hcrx = ccid2_hc_rx_sk(sk);
  641. switch (DCCP_SKB_CB(skb)->dccpd_type) {
  642. case DCCP_PKT_DATA:
  643. case DCCP_PKT_DATAACK:
  644. hcrx->ccid2hcrx_data++;
  645. if (hcrx->ccid2hcrx_data >= dp->dccps_r_ack_ratio) {
  646. dccp_send_ack(sk);
  647. hcrx->ccid2hcrx_data = 0;
  648. }
  649. break;
  650. }
  651. }
  652. struct ccid_operations ccid2_ops = {
  653. .ccid_id = DCCPC_CCID2,
  654. .ccid_name = "TCP-like",
  655. .ccid_hc_tx_obj_size = sizeof(struct ccid2_hc_tx_sock),
  656. .ccid_hc_tx_init = ccid2_hc_tx_init,
  657. .ccid_hc_tx_exit = ccid2_hc_tx_exit,
  658. .ccid_hc_tx_send_packet = ccid2_hc_tx_send_packet,
  659. .ccid_hc_tx_packet_sent = ccid2_hc_tx_packet_sent,
  660. .ccid_hc_tx_packet_recv = ccid2_hc_tx_packet_recv,
  661. .ccid_hc_rx_obj_size = sizeof(struct ccid2_hc_rx_sock),
  662. .ccid_hc_rx_packet_recv = ccid2_hc_rx_packet_recv,
  663. };
  664. #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
  665. module_param(ccid2_debug, bool, 0644);
  666. MODULE_PARM_DESC(ccid2_debug, "Enable CCID-2 debug messages");
  667. #endif