ccid2.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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->tx_seqh;
  37. /* there is data in the chain */
  38. if (seqp != hctx->tx_seqt) {
  39. seqp = seqp->ccid2s_prev;
  40. len++;
  41. if (!seqp->ccid2s_acked)
  42. pipe++;
  43. while (seqp != hctx->tx_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->tx_pipe);
  57. ccid2_pr_debug("len of chain=%d\n", len);
  58. do {
  59. seqp = seqp->ccid2s_prev;
  60. len++;
  61. } while (seqp != hctx->tx_seqh);
  62. ccid2_pr_debug("total len=%d\n", len);
  63. BUG_ON(len != hctx->tx_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->tx_seqbufc >= (sizeof(hctx->tx_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->tx_seqbufc == 0)
  89. hctx->tx_seqh = hctx->tx_seqt = seqp;
  90. else {
  91. /* link the existing list with the one we just created */
  92. hctx->tx_seqh->ccid2s_next = seqp;
  93. seqp->ccid2s_prev = hctx->tx_seqh;
  94. hctx->tx_seqt->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1];
  95. seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = hctx->tx_seqt;
  96. }
  97. /* store the original pointer to the buffer so we can free it */
  98. hctx->tx_seqbuf[hctx->tx_seqbufc] = seqp;
  99. hctx->tx_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->tx_pipe < hctx->tx_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)->tx_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->tx_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->tx_rtotimer, jiffies + HZ / 5);
  144. goto out;
  145. }
  146. ccid2_pr_debug("RTO_EXPIRE\n");
  147. ccid2_hc_tx_check_sanity(hctx);
  148. /* back-off timer */
  149. hctx->tx_rto <<= 1;
  150. s = hctx->tx_rto / HZ;
  151. if (s > 60)
  152. hctx->tx_rto = 60 * HZ;
  153. ccid2_start_rto_timer(sk);
  154. /* adjust pipe, cwnd etc */
  155. hctx->tx_ssthresh = hctx->tx_cwnd / 2;
  156. if (hctx->tx_ssthresh < 2)
  157. hctx->tx_ssthresh = 2;
  158. hctx->tx_cwnd = 1;
  159. hctx->tx_pipe = 0;
  160. /* clear state about stuff we sent */
  161. hctx->tx_seqt = hctx->tx_seqh;
  162. hctx->tx_packets_acked = 0;
  163. /* clear ack ratio state. */
  164. hctx->tx_rpseq = 0;
  165. hctx->tx_rpdupack = -1;
  166. ccid2_change_l_ack_ratio(sk, 1);
  167. ccid2_hc_tx_check_sanity(hctx);
  168. out:
  169. bh_unlock_sock(sk);
  170. sock_put(sk);
  171. }
  172. static void ccid2_start_rto_timer(struct sock *sk)
  173. {
  174. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  175. ccid2_pr_debug("setting RTO timeout=%ld\n", hctx->tx_rto);
  176. BUG_ON(timer_pending(&hctx->tx_rtotimer));
  177. sk_reset_timer(sk, &hctx->tx_rtotimer, jiffies + hctx->tx_rto);
  178. }
  179. static void ccid2_hc_tx_packet_sent(struct sock *sk, int more, unsigned int len)
  180. {
  181. struct dccp_sock *dp = dccp_sk(sk);
  182. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  183. struct ccid2_seq *next;
  184. hctx->tx_pipe++;
  185. hctx->tx_seqh->ccid2s_seq = dp->dccps_gss;
  186. hctx->tx_seqh->ccid2s_acked = 0;
  187. hctx->tx_seqh->ccid2s_sent = jiffies;
  188. next = hctx->tx_seqh->ccid2s_next;
  189. /* check if we need to alloc more space */
  190. if (next == hctx->tx_seqt) {
  191. if (ccid2_hc_tx_alloc_seq(hctx)) {
  192. DCCP_CRIT("packet history - out of memory!");
  193. /* FIXME: find a more graceful way to bail out */
  194. return;
  195. }
  196. next = hctx->tx_seqh->ccid2s_next;
  197. BUG_ON(next == hctx->tx_seqt);
  198. }
  199. hctx->tx_seqh = next;
  200. ccid2_pr_debug("cwnd=%d pipe=%d\n", hctx->tx_cwnd, hctx->tx_pipe);
  201. /*
  202. * FIXME: The code below is broken and the variables have been removed
  203. * from the socket struct. The `ackloss' variable was always set to 0,
  204. * and with arsent there are several problems:
  205. * (i) it doesn't just count the number of Acks, but all sent packets;
  206. * (ii) it is expressed in # of packets, not # of windows, so the
  207. * comparison below uses the wrong formula: Appendix A of RFC 4341
  208. * comes up with the number K = cwnd / (R^2 - R) of consecutive windows
  209. * of data with no lost or marked Ack packets. If arsent were the # of
  210. * consecutive Acks received without loss, then Ack Ratio needs to be
  211. * decreased by 1 when
  212. * arsent >= K * cwnd / R = cwnd^2 / (R^3 - R^2)
  213. * where cwnd / R is the number of Acks received per window of data
  214. * (cf. RFC 4341, App. A). The problems are that
  215. * - arsent counts other packets as well;
  216. * - the comparison uses a formula different from RFC 4341;
  217. * - computing a cubic/quadratic equation each time is too complicated.
  218. * Hence a different algorithm is needed.
  219. */
  220. #if 0
  221. /* Ack Ratio. Need to maintain a concept of how many windows we sent */
  222. hctx->tx_arsent++;
  223. /* We had an ack loss in this window... */
  224. if (hctx->tx_ackloss) {
  225. if (hctx->tx_arsent >= hctx->tx_cwnd) {
  226. hctx->tx_arsent = 0;
  227. hctx->tx_ackloss = 0;
  228. }
  229. } else {
  230. /* No acks lost up to now... */
  231. /* decrease ack ratio if enough packets were sent */
  232. if (dp->dccps_l_ack_ratio > 1) {
  233. /* XXX don't calculate denominator each time */
  234. int denom = dp->dccps_l_ack_ratio * dp->dccps_l_ack_ratio -
  235. dp->dccps_l_ack_ratio;
  236. denom = hctx->tx_cwnd * hctx->tx_cwnd / denom;
  237. if (hctx->tx_arsent >= denom) {
  238. ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio - 1);
  239. hctx->tx_arsent = 0;
  240. }
  241. } else {
  242. /* we can't increase ack ratio further [1] */
  243. hctx->tx_arsent = 0; /* or maybe set it to cwnd*/
  244. }
  245. }
  246. #endif
  247. /* setup RTO timer */
  248. if (!timer_pending(&hctx->tx_rtotimer))
  249. ccid2_start_rto_timer(sk);
  250. #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
  251. do {
  252. struct ccid2_seq *seqp = hctx->tx_seqt;
  253. while (seqp != hctx->tx_seqh) {
  254. ccid2_pr_debug("out seq=%llu acked=%d time=%lu\n",
  255. (unsigned long long)seqp->ccid2s_seq,
  256. seqp->ccid2s_acked, seqp->ccid2s_sent);
  257. seqp = seqp->ccid2s_next;
  258. }
  259. } while (0);
  260. ccid2_pr_debug("=========\n");
  261. ccid2_hc_tx_check_sanity(hctx);
  262. #endif
  263. }
  264. /* XXX Lame code duplication!
  265. * returns -1 if none was found.
  266. * else returns the next offset to use in the function call.
  267. */
  268. static int ccid2_ackvector(struct sock *sk, struct sk_buff *skb, int offset,
  269. unsigned char **vec, unsigned char *veclen)
  270. {
  271. const struct dccp_hdr *dh = dccp_hdr(skb);
  272. unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb);
  273. unsigned char *opt_ptr;
  274. const unsigned char *opt_end = (unsigned char *)dh +
  275. (dh->dccph_doff * 4);
  276. unsigned char opt, len;
  277. unsigned char *value;
  278. BUG_ON(offset < 0);
  279. options += offset;
  280. opt_ptr = options;
  281. if (opt_ptr >= opt_end)
  282. return -1;
  283. while (opt_ptr != opt_end) {
  284. opt = *opt_ptr++;
  285. len = 0;
  286. value = NULL;
  287. /* Check if this isn't a single byte option */
  288. if (opt > DCCPO_MAX_RESERVED) {
  289. if (opt_ptr == opt_end)
  290. goto out_invalid_option;
  291. len = *opt_ptr++;
  292. if (len < 3)
  293. goto out_invalid_option;
  294. /*
  295. * Remove the type and len fields, leaving
  296. * just the value size
  297. */
  298. len -= 2;
  299. value = opt_ptr;
  300. opt_ptr += len;
  301. if (opt_ptr > opt_end)
  302. goto out_invalid_option;
  303. }
  304. switch (opt) {
  305. case DCCPO_ACK_VECTOR_0:
  306. case DCCPO_ACK_VECTOR_1:
  307. *vec = value;
  308. *veclen = len;
  309. return offset + (opt_ptr - options);
  310. }
  311. }
  312. return -1;
  313. out_invalid_option:
  314. DCCP_BUG("Invalid option - this should not happen (previous parsing)!");
  315. return -1;
  316. }
  317. static void ccid2_hc_tx_kill_rto_timer(struct sock *sk)
  318. {
  319. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  320. sk_stop_timer(sk, &hctx->tx_rtotimer);
  321. ccid2_pr_debug("deleted RTO timer\n");
  322. }
  323. static inline void ccid2_new_ack(struct sock *sk,
  324. struct ccid2_seq *seqp,
  325. unsigned int *maxincr)
  326. {
  327. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  328. if (hctx->tx_cwnd < hctx->tx_ssthresh) {
  329. if (*maxincr > 0 && ++hctx->tx_packets_acked == 2) {
  330. hctx->tx_cwnd += 1;
  331. *maxincr -= 1;
  332. hctx->tx_packets_acked = 0;
  333. }
  334. } else if (++hctx->tx_packets_acked >= hctx->tx_cwnd) {
  335. hctx->tx_cwnd += 1;
  336. hctx->tx_packets_acked = 0;
  337. }
  338. /* update RTO */
  339. if (hctx->tx_srtt == -1 ||
  340. time_after(jiffies, hctx->tx_lastrtt + hctx->tx_srtt)) {
  341. unsigned long r = (long)jiffies - (long)seqp->ccid2s_sent;
  342. int s;
  343. /* first measurement */
  344. if (hctx->tx_srtt == -1) {
  345. ccid2_pr_debug("R: %lu Time=%lu seq=%llu\n",
  346. r, jiffies,
  347. (unsigned long long)seqp->ccid2s_seq);
  348. ccid2_change_srtt(hctx, r);
  349. hctx->tx_rttvar = r >> 1;
  350. } else {
  351. /* RTTVAR */
  352. long tmp = hctx->tx_srtt - r;
  353. long srtt;
  354. if (tmp < 0)
  355. tmp *= -1;
  356. tmp >>= 2;
  357. hctx->tx_rttvar *= 3;
  358. hctx->tx_rttvar >>= 2;
  359. hctx->tx_rttvar += tmp;
  360. /* SRTT */
  361. srtt = hctx->tx_srtt;
  362. srtt *= 7;
  363. srtt >>= 3;
  364. tmp = r >> 3;
  365. srtt += tmp;
  366. ccid2_change_srtt(hctx, srtt);
  367. }
  368. s = hctx->tx_rttvar << 2;
  369. /* clock granularity is 1 when based on jiffies */
  370. if (!s)
  371. s = 1;
  372. hctx->tx_rto = hctx->tx_srtt + s;
  373. /* must be at least a second */
  374. s = hctx->tx_rto / HZ;
  375. /* DCCP doesn't require this [but I like it cuz my code sux] */
  376. #if 1
  377. if (s < 1)
  378. hctx->tx_rto = HZ;
  379. #endif
  380. /* max 60 seconds */
  381. if (s > 60)
  382. hctx->tx_rto = HZ * 60;
  383. hctx->tx_lastrtt = jiffies;
  384. ccid2_pr_debug("srtt: %ld rttvar: %ld rto: %ld (HZ=%d) R=%lu\n",
  385. hctx->tx_srtt, hctx->tx_rttvar,
  386. hctx->tx_rto, HZ, r);
  387. }
  388. /* we got a new ack, so re-start RTO timer */
  389. ccid2_hc_tx_kill_rto_timer(sk);
  390. ccid2_start_rto_timer(sk);
  391. }
  392. static void ccid2_hc_tx_dec_pipe(struct sock *sk)
  393. {
  394. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  395. if (hctx->tx_pipe == 0)
  396. DCCP_BUG("pipe == 0");
  397. else
  398. hctx->tx_pipe--;
  399. if (hctx->tx_pipe == 0)
  400. ccid2_hc_tx_kill_rto_timer(sk);
  401. }
  402. static void ccid2_congestion_event(struct sock *sk, struct ccid2_seq *seqp)
  403. {
  404. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  405. if (time_before(seqp->ccid2s_sent, hctx->tx_last_cong)) {
  406. ccid2_pr_debug("Multiple losses in an RTT---treating as one\n");
  407. return;
  408. }
  409. hctx->tx_last_cong = jiffies;
  410. hctx->tx_cwnd = hctx->tx_cwnd / 2 ? : 1U;
  411. hctx->tx_ssthresh = max(hctx->tx_cwnd, 2U);
  412. /* Avoid spurious timeouts resulting from Ack Ratio > cwnd */
  413. if (dccp_sk(sk)->dccps_l_ack_ratio > hctx->tx_cwnd)
  414. ccid2_change_l_ack_ratio(sk, hctx->tx_cwnd);
  415. }
  416. static void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
  417. {
  418. struct dccp_sock *dp = dccp_sk(sk);
  419. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  420. u64 ackno, seqno;
  421. struct ccid2_seq *seqp;
  422. unsigned char *vector;
  423. unsigned char veclen;
  424. int offset = 0;
  425. int done = 0;
  426. unsigned int maxincr = 0;
  427. ccid2_hc_tx_check_sanity(hctx);
  428. /* check reverse path congestion */
  429. seqno = DCCP_SKB_CB(skb)->dccpd_seq;
  430. /* XXX this whole "algorithm" is broken. Need to fix it to keep track
  431. * of the seqnos of the dupacks so that rpseq and rpdupack are correct
  432. * -sorbo.
  433. */
  434. /* need to bootstrap */
  435. if (hctx->tx_rpdupack == -1) {
  436. hctx->tx_rpdupack = 0;
  437. hctx->tx_rpseq = seqno;
  438. } else {
  439. /* check if packet is consecutive */
  440. if (dccp_delta_seqno(hctx->tx_rpseq, seqno) == 1)
  441. hctx->tx_rpseq = seqno;
  442. /* it's a later packet */
  443. else if (after48(seqno, hctx->tx_rpseq)) {
  444. hctx->tx_rpdupack++;
  445. /* check if we got enough dupacks */
  446. if (hctx->tx_rpdupack >= NUMDUPACK) {
  447. hctx->tx_rpdupack = -1; /* XXX lame */
  448. hctx->tx_rpseq = 0;
  449. ccid2_change_l_ack_ratio(sk, 2 * dp->dccps_l_ack_ratio);
  450. }
  451. }
  452. }
  453. /* check forward path congestion */
  454. /* still didn't send out new data packets */
  455. if (hctx->tx_seqh == hctx->tx_seqt)
  456. return;
  457. switch (DCCP_SKB_CB(skb)->dccpd_type) {
  458. case DCCP_PKT_ACK:
  459. case DCCP_PKT_DATAACK:
  460. break;
  461. default:
  462. return;
  463. }
  464. ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
  465. if (after48(ackno, hctx->tx_high_ack))
  466. hctx->tx_high_ack = ackno;
  467. seqp = hctx->tx_seqt;
  468. while (before48(seqp->ccid2s_seq, ackno)) {
  469. seqp = seqp->ccid2s_next;
  470. if (seqp == hctx->tx_seqh) {
  471. seqp = hctx->tx_seqh->ccid2s_prev;
  472. break;
  473. }
  474. }
  475. /*
  476. * In slow-start, cwnd can increase up to a maximum of Ack Ratio/2
  477. * packets per acknowledgement. Rounding up avoids that cwnd is not
  478. * advanced when Ack Ratio is 1 and gives a slight edge otherwise.
  479. */
  480. if (hctx->tx_cwnd < hctx->tx_ssthresh)
  481. maxincr = DIV_ROUND_UP(dp->dccps_l_ack_ratio, 2);
  482. /* go through all ack vectors */
  483. while ((offset = ccid2_ackvector(sk, skb, offset,
  484. &vector, &veclen)) != -1) {
  485. /* go through this ack vector */
  486. while (veclen--) {
  487. const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
  488. u64 ackno_end_rl = SUB48(ackno, rl);
  489. ccid2_pr_debug("ackvec start:%llu end:%llu\n",
  490. (unsigned long long)ackno,
  491. (unsigned long long)ackno_end_rl);
  492. /* if the seqno we are analyzing is larger than the
  493. * current ackno, then move towards the tail of our
  494. * seqnos.
  495. */
  496. while (after48(seqp->ccid2s_seq, ackno)) {
  497. if (seqp == hctx->tx_seqt) {
  498. done = 1;
  499. break;
  500. }
  501. seqp = seqp->ccid2s_prev;
  502. }
  503. if (done)
  504. break;
  505. /* check all seqnos in the range of the vector
  506. * run length
  507. */
  508. while (between48(seqp->ccid2s_seq,ackno_end_rl,ackno)) {
  509. const u8 state = *vector &
  510. DCCP_ACKVEC_STATE_MASK;
  511. /* new packet received or marked */
  512. if (state != DCCP_ACKVEC_STATE_NOT_RECEIVED &&
  513. !seqp->ccid2s_acked) {
  514. if (state ==
  515. DCCP_ACKVEC_STATE_ECN_MARKED) {
  516. ccid2_congestion_event(sk,
  517. seqp);
  518. } else
  519. ccid2_new_ack(sk, seqp,
  520. &maxincr);
  521. seqp->ccid2s_acked = 1;
  522. ccid2_pr_debug("Got ack for %llu\n",
  523. (unsigned long long)seqp->ccid2s_seq);
  524. ccid2_hc_tx_dec_pipe(sk);
  525. }
  526. if (seqp == hctx->tx_seqt) {
  527. done = 1;
  528. break;
  529. }
  530. seqp = seqp->ccid2s_prev;
  531. }
  532. if (done)
  533. break;
  534. ackno = SUB48(ackno_end_rl, 1);
  535. vector++;
  536. }
  537. if (done)
  538. break;
  539. }
  540. /* The state about what is acked should be correct now
  541. * Check for NUMDUPACK
  542. */
  543. seqp = hctx->tx_seqt;
  544. while (before48(seqp->ccid2s_seq, hctx->tx_high_ack)) {
  545. seqp = seqp->ccid2s_next;
  546. if (seqp == hctx->tx_seqh) {
  547. seqp = hctx->tx_seqh->ccid2s_prev;
  548. break;
  549. }
  550. }
  551. done = 0;
  552. while (1) {
  553. if (seqp->ccid2s_acked) {
  554. done++;
  555. if (done == NUMDUPACK)
  556. break;
  557. }
  558. if (seqp == hctx->tx_seqt)
  559. break;
  560. seqp = seqp->ccid2s_prev;
  561. }
  562. /* If there are at least 3 acknowledgements, anything unacknowledged
  563. * below the last sequence number is considered lost
  564. */
  565. if (done == NUMDUPACK) {
  566. struct ccid2_seq *last_acked = seqp;
  567. /* check for lost packets */
  568. while (1) {
  569. if (!seqp->ccid2s_acked) {
  570. ccid2_pr_debug("Packet lost: %llu\n",
  571. (unsigned long long)seqp->ccid2s_seq);
  572. /* XXX need to traverse from tail -> head in
  573. * order to detect multiple congestion events in
  574. * one ack vector.
  575. */
  576. ccid2_congestion_event(sk, seqp);
  577. ccid2_hc_tx_dec_pipe(sk);
  578. }
  579. if (seqp == hctx->tx_seqt)
  580. break;
  581. seqp = seqp->ccid2s_prev;
  582. }
  583. hctx->tx_seqt = last_acked;
  584. }
  585. /* trim acked packets in tail */
  586. while (hctx->tx_seqt != hctx->tx_seqh) {
  587. if (!hctx->tx_seqt->ccid2s_acked)
  588. break;
  589. hctx->tx_seqt = hctx->tx_seqt->ccid2s_next;
  590. }
  591. ccid2_hc_tx_check_sanity(hctx);
  592. }
  593. static int ccid2_hc_tx_init(struct ccid *ccid, struct sock *sk)
  594. {
  595. struct ccid2_hc_tx_sock *hctx = ccid_priv(ccid);
  596. struct dccp_sock *dp = dccp_sk(sk);
  597. u32 max_ratio;
  598. /* RFC 4341, 5: initialise ssthresh to arbitrarily high (max) value */
  599. hctx->tx_ssthresh = ~0U;
  600. /*
  601. * RFC 4341, 5: "The cwnd parameter is initialized to at most four
  602. * packets for new connections, following the rules from [RFC3390]".
  603. * We need to convert the bytes of RFC3390 into the packets of RFC 4341.
  604. */
  605. hctx->tx_cwnd = clamp(4380U / dp->dccps_mss_cache, 2U, 4U);
  606. /* Make sure that Ack Ratio is enabled and within bounds. */
  607. max_ratio = DIV_ROUND_UP(hctx->tx_cwnd, 2);
  608. if (dp->dccps_l_ack_ratio == 0 || dp->dccps_l_ack_ratio > max_ratio)
  609. dp->dccps_l_ack_ratio = max_ratio;
  610. /* XXX init ~ to window size... */
  611. if (ccid2_hc_tx_alloc_seq(hctx))
  612. return -ENOMEM;
  613. hctx->tx_rto = 3 * HZ;
  614. ccid2_change_srtt(hctx, -1);
  615. hctx->tx_rttvar = -1;
  616. hctx->tx_rpdupack = -1;
  617. hctx->tx_last_cong = jiffies;
  618. setup_timer(&hctx->tx_rtotimer, ccid2_hc_tx_rto_expire,
  619. (unsigned long)sk);
  620. ccid2_hc_tx_check_sanity(hctx);
  621. return 0;
  622. }
  623. static void ccid2_hc_tx_exit(struct sock *sk)
  624. {
  625. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  626. int i;
  627. ccid2_hc_tx_kill_rto_timer(sk);
  628. for (i = 0; i < hctx->tx_seqbufc; i++)
  629. kfree(hctx->tx_seqbuf[i]);
  630. hctx->tx_seqbufc = 0;
  631. }
  632. static void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
  633. {
  634. const struct dccp_sock *dp = dccp_sk(sk);
  635. struct ccid2_hc_rx_sock *hcrx = ccid2_hc_rx_sk(sk);
  636. switch (DCCP_SKB_CB(skb)->dccpd_type) {
  637. case DCCP_PKT_DATA:
  638. case DCCP_PKT_DATAACK:
  639. hcrx->rx_data++;
  640. if (hcrx->rx_data >= dp->dccps_r_ack_ratio) {
  641. dccp_send_ack(sk);
  642. hcrx->rx_data = 0;
  643. }
  644. break;
  645. }
  646. }
  647. struct ccid_operations ccid2_ops = {
  648. .ccid_id = DCCPC_CCID2,
  649. .ccid_name = "TCP-like",
  650. .ccid_hc_tx_obj_size = sizeof(struct ccid2_hc_tx_sock),
  651. .ccid_hc_tx_init = ccid2_hc_tx_init,
  652. .ccid_hc_tx_exit = ccid2_hc_tx_exit,
  653. .ccid_hc_tx_send_packet = ccid2_hc_tx_send_packet,
  654. .ccid_hc_tx_packet_sent = ccid2_hc_tx_packet_sent,
  655. .ccid_hc_tx_packet_recv = ccid2_hc_tx_packet_recv,
  656. .ccid_hc_rx_obj_size = sizeof(struct ccid2_hc_rx_sock),
  657. .ccid_hc_rx_packet_recv = ccid2_hc_rx_packet_recv,
  658. };
  659. #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
  660. module_param(ccid2_debug, bool, 0644);
  661. MODULE_PARM_DESC(ccid2_debug, "Enable CCID-2 debug messages");
  662. #endif