ccid2.c 21 KB

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