ccid2.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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 <linux/slab.h>
  26. #include "../feat.h"
  27. #include "ccid2.h"
  28. #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
  29. static int ccid2_debug;
  30. #define ccid2_pr_debug(format, a...) DCCP_PR_DEBUG(ccid2_debug, format, ##a)
  31. #else
  32. #define ccid2_pr_debug(format, a...)
  33. #endif
  34. static int ccid2_hc_tx_alloc_seq(struct ccid2_hc_tx_sock *hc)
  35. {
  36. struct ccid2_seq *seqp;
  37. int i;
  38. /* check if we have space to preserve the pointer to the buffer */
  39. if (hc->tx_seqbufc >= (sizeof(hc->tx_seqbuf) /
  40. sizeof(struct ccid2_seq *)))
  41. return -ENOMEM;
  42. /* allocate buffer and initialize linked list */
  43. seqp = kmalloc(CCID2_SEQBUF_LEN * sizeof(struct ccid2_seq), gfp_any());
  44. if (seqp == NULL)
  45. return -ENOMEM;
  46. for (i = 0; i < (CCID2_SEQBUF_LEN - 1); i++) {
  47. seqp[i].ccid2s_next = &seqp[i + 1];
  48. seqp[i + 1].ccid2s_prev = &seqp[i];
  49. }
  50. seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = seqp;
  51. seqp->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1];
  52. /* This is the first allocation. Initiate the head and tail. */
  53. if (hc->tx_seqbufc == 0)
  54. hc->tx_seqh = hc->tx_seqt = seqp;
  55. else {
  56. /* link the existing list with the one we just created */
  57. hc->tx_seqh->ccid2s_next = seqp;
  58. seqp->ccid2s_prev = hc->tx_seqh;
  59. hc->tx_seqt->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1];
  60. seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = hc->tx_seqt;
  61. }
  62. /* store the original pointer to the buffer so we can free it */
  63. hc->tx_seqbuf[hc->tx_seqbufc] = seqp;
  64. hc->tx_seqbufc++;
  65. return 0;
  66. }
  67. static int ccid2_hc_tx_send_packet(struct sock *sk, struct sk_buff *skb)
  68. {
  69. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  70. if (hc->tx_pipe < hc->tx_cwnd)
  71. return 0;
  72. return 1; /* XXX CCID should dequeue when ready instead of polling */
  73. }
  74. static void ccid2_change_l_ack_ratio(struct sock *sk, u32 val)
  75. {
  76. struct dccp_sock *dp = dccp_sk(sk);
  77. u32 max_ratio = DIV_ROUND_UP(ccid2_hc_tx_sk(sk)->tx_cwnd, 2);
  78. /*
  79. * Ensure that Ack Ratio does not exceed ceil(cwnd/2), which is (2) from
  80. * RFC 4341, 6.1.2. We ignore the statement that Ack Ratio 2 is always
  81. * acceptable since this causes starvation/deadlock whenever cwnd < 2.
  82. * The same problem arises when Ack Ratio is 0 (ie. Ack Ratio disabled).
  83. */
  84. if (val == 0 || val > max_ratio) {
  85. DCCP_WARN("Limiting Ack Ratio (%u) to %u\n", val, max_ratio);
  86. val = max_ratio;
  87. }
  88. if (val > DCCPF_ACK_RATIO_MAX)
  89. val = DCCPF_ACK_RATIO_MAX;
  90. if (val == dp->dccps_l_ack_ratio)
  91. return;
  92. ccid2_pr_debug("changing local ack ratio to %u\n", val);
  93. dp->dccps_l_ack_ratio = val;
  94. }
  95. static void ccid2_start_rto_timer(struct sock *sk);
  96. static void ccid2_hc_tx_rto_expire(unsigned long data)
  97. {
  98. struct sock *sk = (struct sock *)data;
  99. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  100. bh_lock_sock(sk);
  101. if (sock_owned_by_user(sk)) {
  102. sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + HZ / 5);
  103. goto out;
  104. }
  105. ccid2_pr_debug("RTO_EXPIRE\n");
  106. /* back-off timer */
  107. hc->tx_rto <<= 1;
  108. if (hc->tx_rto > DCCP_RTO_MAX)
  109. hc->tx_rto = DCCP_RTO_MAX;
  110. ccid2_start_rto_timer(sk);
  111. /* adjust pipe, cwnd etc */
  112. hc->tx_ssthresh = hc->tx_cwnd / 2;
  113. if (hc->tx_ssthresh < 2)
  114. hc->tx_ssthresh = 2;
  115. hc->tx_cwnd = 1;
  116. hc->tx_pipe = 0;
  117. /* clear state about stuff we sent */
  118. hc->tx_seqt = hc->tx_seqh;
  119. hc->tx_packets_acked = 0;
  120. /* clear ack ratio state. */
  121. hc->tx_rpseq = 0;
  122. hc->tx_rpdupack = -1;
  123. ccid2_change_l_ack_ratio(sk, 1);
  124. out:
  125. bh_unlock_sock(sk);
  126. sock_put(sk);
  127. }
  128. static void ccid2_start_rto_timer(struct sock *sk)
  129. {
  130. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  131. ccid2_pr_debug("setting RTO timeout=%u\n", hc->tx_rto);
  132. BUG_ON(timer_pending(&hc->tx_rtotimer));
  133. sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto);
  134. }
  135. static void ccid2_hc_tx_packet_sent(struct sock *sk, int more, unsigned int len)
  136. {
  137. struct dccp_sock *dp = dccp_sk(sk);
  138. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  139. struct ccid2_seq *next;
  140. hc->tx_pipe++;
  141. hc->tx_seqh->ccid2s_seq = dp->dccps_gss;
  142. hc->tx_seqh->ccid2s_acked = 0;
  143. hc->tx_seqh->ccid2s_sent = ccid2_time_stamp;
  144. next = hc->tx_seqh->ccid2s_next;
  145. /* check if we need to alloc more space */
  146. if (next == hc->tx_seqt) {
  147. if (ccid2_hc_tx_alloc_seq(hc)) {
  148. DCCP_CRIT("packet history - out of memory!");
  149. /* FIXME: find a more graceful way to bail out */
  150. return;
  151. }
  152. next = hc->tx_seqh->ccid2s_next;
  153. BUG_ON(next == hc->tx_seqt);
  154. }
  155. hc->tx_seqh = next;
  156. ccid2_pr_debug("cwnd=%d pipe=%d\n", hc->tx_cwnd, hc->tx_pipe);
  157. /*
  158. * FIXME: The code below is broken and the variables have been removed
  159. * from the socket struct. The `ackloss' variable was always set to 0,
  160. * and with arsent there are several problems:
  161. * (i) it doesn't just count the number of Acks, but all sent packets;
  162. * (ii) it is expressed in # of packets, not # of windows, so the
  163. * comparison below uses the wrong formula: Appendix A of RFC 4341
  164. * comes up with the number K = cwnd / (R^2 - R) of consecutive windows
  165. * of data with no lost or marked Ack packets. If arsent were the # of
  166. * consecutive Acks received without loss, then Ack Ratio needs to be
  167. * decreased by 1 when
  168. * arsent >= K * cwnd / R = cwnd^2 / (R^3 - R^2)
  169. * where cwnd / R is the number of Acks received per window of data
  170. * (cf. RFC 4341, App. A). The problems are that
  171. * - arsent counts other packets as well;
  172. * - the comparison uses a formula different from RFC 4341;
  173. * - computing a cubic/quadratic equation each time is too complicated.
  174. * Hence a different algorithm is needed.
  175. */
  176. #if 0
  177. /* Ack Ratio. Need to maintain a concept of how many windows we sent */
  178. hc->tx_arsent++;
  179. /* We had an ack loss in this window... */
  180. if (hc->tx_ackloss) {
  181. if (hc->tx_arsent >= hc->tx_cwnd) {
  182. hc->tx_arsent = 0;
  183. hc->tx_ackloss = 0;
  184. }
  185. } else {
  186. /* No acks lost up to now... */
  187. /* decrease ack ratio if enough packets were sent */
  188. if (dp->dccps_l_ack_ratio > 1) {
  189. /* XXX don't calculate denominator each time */
  190. int denom = dp->dccps_l_ack_ratio * dp->dccps_l_ack_ratio -
  191. dp->dccps_l_ack_ratio;
  192. denom = hc->tx_cwnd * hc->tx_cwnd / denom;
  193. if (hc->tx_arsent >= denom) {
  194. ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio - 1);
  195. hc->tx_arsent = 0;
  196. }
  197. } else {
  198. /* we can't increase ack ratio further [1] */
  199. hc->tx_arsent = 0; /* or maybe set it to cwnd*/
  200. }
  201. }
  202. #endif
  203. /* setup RTO timer */
  204. if (!timer_pending(&hc->tx_rtotimer))
  205. ccid2_start_rto_timer(sk);
  206. #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
  207. do {
  208. struct ccid2_seq *seqp = hc->tx_seqt;
  209. while (seqp != hc->tx_seqh) {
  210. ccid2_pr_debug("out seq=%llu acked=%d time=%u\n",
  211. (unsigned long long)seqp->ccid2s_seq,
  212. seqp->ccid2s_acked, seqp->ccid2s_sent);
  213. seqp = seqp->ccid2s_next;
  214. }
  215. } while (0);
  216. ccid2_pr_debug("=========\n");
  217. #endif
  218. }
  219. /* XXX Lame code duplication!
  220. * returns -1 if none was found.
  221. * else returns the next offset to use in the function call.
  222. */
  223. static int ccid2_ackvector(struct sock *sk, struct sk_buff *skb, int offset,
  224. unsigned char **vec, unsigned char *veclen)
  225. {
  226. const struct dccp_hdr *dh = dccp_hdr(skb);
  227. unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb);
  228. unsigned char *opt_ptr;
  229. const unsigned char *opt_end = (unsigned char *)dh +
  230. (dh->dccph_doff * 4);
  231. unsigned char opt, len;
  232. unsigned char *value;
  233. BUG_ON(offset < 0);
  234. options += offset;
  235. opt_ptr = options;
  236. if (opt_ptr >= opt_end)
  237. return -1;
  238. while (opt_ptr != opt_end) {
  239. opt = *opt_ptr++;
  240. len = 0;
  241. value = NULL;
  242. /* Check if this isn't a single byte option */
  243. if (opt > DCCPO_MAX_RESERVED) {
  244. if (opt_ptr == opt_end)
  245. goto out_invalid_option;
  246. len = *opt_ptr++;
  247. if (len < 3)
  248. goto out_invalid_option;
  249. /*
  250. * Remove the type and len fields, leaving
  251. * just the value size
  252. */
  253. len -= 2;
  254. value = opt_ptr;
  255. opt_ptr += len;
  256. if (opt_ptr > opt_end)
  257. goto out_invalid_option;
  258. }
  259. switch (opt) {
  260. case DCCPO_ACK_VECTOR_0:
  261. case DCCPO_ACK_VECTOR_1:
  262. *vec = value;
  263. *veclen = len;
  264. return offset + (opt_ptr - options);
  265. }
  266. }
  267. return -1;
  268. out_invalid_option:
  269. DCCP_BUG("Invalid option - this should not happen (previous parsing)!");
  270. return -1;
  271. }
  272. static void ccid2_hc_tx_kill_rto_timer(struct sock *sk)
  273. {
  274. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  275. sk_stop_timer(sk, &hc->tx_rtotimer);
  276. ccid2_pr_debug("deleted RTO timer\n");
  277. }
  278. /**
  279. * ccid2_rtt_estimator - Sample RTT and compute RTO using RFC2988 algorithm
  280. * This code is almost identical with TCP's tcp_rtt_estimator(), since
  281. * - it has a higher sampling frequency (recommended by RFC 1323),
  282. * - the RTO does not collapse into RTT due to RTTVAR going towards zero,
  283. * - it is simple (cf. more complex proposals such as Eifel timer or research
  284. * which suggests that the gain should be set according to window size),
  285. * - in tests it was found to work well with CCID2 [gerrit].
  286. */
  287. static void ccid2_rtt_estimator(struct sock *sk, const long mrtt)
  288. {
  289. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  290. long m = mrtt ? : 1;
  291. if (hc->tx_srtt == 0) {
  292. /* First measurement m */
  293. hc->tx_srtt = m << 3;
  294. hc->tx_mdev = m << 1;
  295. hc->tx_mdev_max = max(TCP_RTO_MIN, hc->tx_mdev);
  296. hc->tx_rttvar = hc->tx_mdev_max;
  297. hc->tx_rtt_seq = dccp_sk(sk)->dccps_gss;
  298. } else {
  299. /* Update scaled SRTT as SRTT += 1/8 * (m - SRTT) */
  300. m -= (hc->tx_srtt >> 3);
  301. hc->tx_srtt += m;
  302. /* Similarly, update scaled mdev with regard to |m| */
  303. if (m < 0) {
  304. m = -m;
  305. m -= (hc->tx_mdev >> 2);
  306. /*
  307. * This neutralises RTO increase when RTT < SRTT - mdev
  308. * (see P. Sarolahti, A. Kuznetsov,"Congestion Control
  309. * in Linux TCP", USENIX 2002, pp. 49-62).
  310. */
  311. if (m > 0)
  312. m >>= 3;
  313. } else {
  314. m -= (hc->tx_mdev >> 2);
  315. }
  316. hc->tx_mdev += m;
  317. if (hc->tx_mdev > hc->tx_mdev_max) {
  318. hc->tx_mdev_max = hc->tx_mdev;
  319. if (hc->tx_mdev_max > hc->tx_rttvar)
  320. hc->tx_rttvar = hc->tx_mdev_max;
  321. }
  322. /*
  323. * Decay RTTVAR at most once per flight, exploiting that
  324. * 1) pipe <= cwnd <= Sequence_Window = W (RFC 4340, 7.5.2)
  325. * 2) AWL = GSS-W+1 <= GAR <= GSS (RFC 4340, 7.5.1)
  326. * GAR is a useful bound for FlightSize = pipe.
  327. * AWL is probably too low here, as it over-estimates pipe.
  328. */
  329. if (after48(dccp_sk(sk)->dccps_gar, hc->tx_rtt_seq)) {
  330. if (hc->tx_mdev_max < hc->tx_rttvar)
  331. hc->tx_rttvar -= (hc->tx_rttvar -
  332. hc->tx_mdev_max) >> 2;
  333. hc->tx_rtt_seq = dccp_sk(sk)->dccps_gss;
  334. hc->tx_mdev_max = TCP_RTO_MIN;
  335. }
  336. }
  337. /*
  338. * Set RTO from SRTT and RTTVAR
  339. * As in TCP, 4 * RTTVAR >= TCP_RTO_MIN, giving a minimum RTO of 200 ms.
  340. * This agrees with RFC 4341, 5:
  341. * "Because DCCP does not retransmit data, DCCP does not require
  342. * TCP's recommended minimum timeout of one second".
  343. */
  344. hc->tx_rto = (hc->tx_srtt >> 3) + hc->tx_rttvar;
  345. if (hc->tx_rto > DCCP_RTO_MAX)
  346. hc->tx_rto = DCCP_RTO_MAX;
  347. }
  348. static void ccid2_new_ack(struct sock *sk, struct ccid2_seq *seqp,
  349. unsigned int *maxincr)
  350. {
  351. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  352. if (hc->tx_cwnd < hc->tx_ssthresh) {
  353. if (*maxincr > 0 && ++hc->tx_packets_acked == 2) {
  354. hc->tx_cwnd += 1;
  355. *maxincr -= 1;
  356. hc->tx_packets_acked = 0;
  357. }
  358. } else if (++hc->tx_packets_acked >= hc->tx_cwnd) {
  359. hc->tx_cwnd += 1;
  360. hc->tx_packets_acked = 0;
  361. }
  362. /*
  363. * FIXME: RTT is sampled several times per acknowledgment (for each
  364. * entry in the Ack Vector), instead of once per Ack (as in TCP SACK).
  365. * This causes the RTT to be over-estimated, since the older entries
  366. * in the Ack Vector have earlier sending times.
  367. * The cleanest solution is to not use the ccid2s_sent field at all
  368. * and instead use DCCP timestamps: requires changes in other places.
  369. */
  370. ccid2_rtt_estimator(sk, ccid2_time_stamp - seqp->ccid2s_sent);
  371. }
  372. static void ccid2_congestion_event(struct sock *sk, struct ccid2_seq *seqp)
  373. {
  374. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  375. if ((s32)(seqp->ccid2s_sent - hc->tx_last_cong) < 0) {
  376. ccid2_pr_debug("Multiple losses in an RTT---treating as one\n");
  377. return;
  378. }
  379. hc->tx_last_cong = ccid2_time_stamp;
  380. hc->tx_cwnd = hc->tx_cwnd / 2 ? : 1U;
  381. hc->tx_ssthresh = max(hc->tx_cwnd, 2U);
  382. /* Avoid spurious timeouts resulting from Ack Ratio > cwnd */
  383. if (dccp_sk(sk)->dccps_l_ack_ratio > hc->tx_cwnd)
  384. ccid2_change_l_ack_ratio(sk, hc->tx_cwnd);
  385. }
  386. static void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
  387. {
  388. struct dccp_sock *dp = dccp_sk(sk);
  389. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  390. u64 ackno, seqno;
  391. struct ccid2_seq *seqp;
  392. unsigned char *vector;
  393. unsigned char veclen;
  394. int offset = 0;
  395. int done = 0;
  396. unsigned int maxincr = 0;
  397. /* check reverse path congestion */
  398. seqno = DCCP_SKB_CB(skb)->dccpd_seq;
  399. /* XXX this whole "algorithm" is broken. Need to fix it to keep track
  400. * of the seqnos of the dupacks so that rpseq and rpdupack are correct
  401. * -sorbo.
  402. */
  403. /* need to bootstrap */
  404. if (hc->tx_rpdupack == -1) {
  405. hc->tx_rpdupack = 0;
  406. hc->tx_rpseq = seqno;
  407. } else {
  408. /* check if packet is consecutive */
  409. if (dccp_delta_seqno(hc->tx_rpseq, seqno) == 1)
  410. hc->tx_rpseq = seqno;
  411. /* it's a later packet */
  412. else if (after48(seqno, hc->tx_rpseq)) {
  413. hc->tx_rpdupack++;
  414. /* check if we got enough dupacks */
  415. if (hc->tx_rpdupack >= NUMDUPACK) {
  416. hc->tx_rpdupack = -1; /* XXX lame */
  417. hc->tx_rpseq = 0;
  418. ccid2_change_l_ack_ratio(sk, 2 * dp->dccps_l_ack_ratio);
  419. }
  420. }
  421. }
  422. /* check forward path congestion */
  423. /* still didn't send out new data packets */
  424. if (hc->tx_seqh == hc->tx_seqt)
  425. return;
  426. switch (DCCP_SKB_CB(skb)->dccpd_type) {
  427. case DCCP_PKT_ACK:
  428. case DCCP_PKT_DATAACK:
  429. break;
  430. default:
  431. return;
  432. }
  433. ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
  434. if (after48(ackno, hc->tx_high_ack))
  435. hc->tx_high_ack = ackno;
  436. seqp = hc->tx_seqt;
  437. while (before48(seqp->ccid2s_seq, ackno)) {
  438. seqp = seqp->ccid2s_next;
  439. if (seqp == hc->tx_seqh) {
  440. seqp = hc->tx_seqh->ccid2s_prev;
  441. break;
  442. }
  443. }
  444. /*
  445. * In slow-start, cwnd can increase up to a maximum of Ack Ratio/2
  446. * packets per acknowledgement. Rounding up avoids that cwnd is not
  447. * advanced when Ack Ratio is 1 and gives a slight edge otherwise.
  448. */
  449. if (hc->tx_cwnd < hc->tx_ssthresh)
  450. maxincr = DIV_ROUND_UP(dp->dccps_l_ack_ratio, 2);
  451. /* go through all ack vectors */
  452. while ((offset = ccid2_ackvector(sk, skb, offset,
  453. &vector, &veclen)) != -1) {
  454. /* go through this ack vector */
  455. while (veclen--) {
  456. const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
  457. u64 ackno_end_rl = SUB48(ackno, rl);
  458. ccid2_pr_debug("ackvec start:%llu end:%llu\n",
  459. (unsigned long long)ackno,
  460. (unsigned long long)ackno_end_rl);
  461. /* if the seqno we are analyzing is larger than the
  462. * current ackno, then move towards the tail of our
  463. * seqnos.
  464. */
  465. while (after48(seqp->ccid2s_seq, ackno)) {
  466. if (seqp == hc->tx_seqt) {
  467. done = 1;
  468. break;
  469. }
  470. seqp = seqp->ccid2s_prev;
  471. }
  472. if (done)
  473. break;
  474. /* check all seqnos in the range of the vector
  475. * run length
  476. */
  477. while (between48(seqp->ccid2s_seq,ackno_end_rl,ackno)) {
  478. const u8 state = *vector &
  479. DCCP_ACKVEC_STATE_MASK;
  480. /* new packet received or marked */
  481. if (state != DCCP_ACKVEC_STATE_NOT_RECEIVED &&
  482. !seqp->ccid2s_acked) {
  483. if (state ==
  484. DCCP_ACKVEC_STATE_ECN_MARKED) {
  485. ccid2_congestion_event(sk,
  486. seqp);
  487. } else
  488. ccid2_new_ack(sk, seqp,
  489. &maxincr);
  490. seqp->ccid2s_acked = 1;
  491. ccid2_pr_debug("Got ack for %llu\n",
  492. (unsigned long long)seqp->ccid2s_seq);
  493. hc->tx_pipe--;
  494. }
  495. if (seqp == hc->tx_seqt) {
  496. done = 1;
  497. break;
  498. }
  499. seqp = seqp->ccid2s_prev;
  500. }
  501. if (done)
  502. break;
  503. ackno = SUB48(ackno_end_rl, 1);
  504. vector++;
  505. }
  506. if (done)
  507. break;
  508. }
  509. /* The state about what is acked should be correct now
  510. * Check for NUMDUPACK
  511. */
  512. seqp = hc->tx_seqt;
  513. while (before48(seqp->ccid2s_seq, hc->tx_high_ack)) {
  514. seqp = seqp->ccid2s_next;
  515. if (seqp == hc->tx_seqh) {
  516. seqp = hc->tx_seqh->ccid2s_prev;
  517. break;
  518. }
  519. }
  520. done = 0;
  521. while (1) {
  522. if (seqp->ccid2s_acked) {
  523. done++;
  524. if (done == NUMDUPACK)
  525. break;
  526. }
  527. if (seqp == hc->tx_seqt)
  528. break;
  529. seqp = seqp->ccid2s_prev;
  530. }
  531. /* If there are at least 3 acknowledgements, anything unacknowledged
  532. * below the last sequence number is considered lost
  533. */
  534. if (done == NUMDUPACK) {
  535. struct ccid2_seq *last_acked = seqp;
  536. /* check for lost packets */
  537. while (1) {
  538. if (!seqp->ccid2s_acked) {
  539. ccid2_pr_debug("Packet lost: %llu\n",
  540. (unsigned long long)seqp->ccid2s_seq);
  541. /* XXX need to traverse from tail -> head in
  542. * order to detect multiple congestion events in
  543. * one ack vector.
  544. */
  545. ccid2_congestion_event(sk, seqp);
  546. hc->tx_pipe--;
  547. }
  548. if (seqp == hc->tx_seqt)
  549. break;
  550. seqp = seqp->ccid2s_prev;
  551. }
  552. hc->tx_seqt = last_acked;
  553. }
  554. /* trim acked packets in tail */
  555. while (hc->tx_seqt != hc->tx_seqh) {
  556. if (!hc->tx_seqt->ccid2s_acked)
  557. break;
  558. hc->tx_seqt = hc->tx_seqt->ccid2s_next;
  559. }
  560. /* restart RTO timer if not all outstanding data has been acked */
  561. if (hc->tx_pipe == 0)
  562. sk_stop_timer(sk, &hc->tx_rtotimer);
  563. else
  564. sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto);
  565. }
  566. static int ccid2_hc_tx_init(struct ccid *ccid, struct sock *sk)
  567. {
  568. struct ccid2_hc_tx_sock *hc = ccid_priv(ccid);
  569. struct dccp_sock *dp = dccp_sk(sk);
  570. u32 max_ratio;
  571. /* RFC 4341, 5: initialise ssthresh to arbitrarily high (max) value */
  572. hc->tx_ssthresh = ~0U;
  573. /*
  574. * RFC 4341, 5: "The cwnd parameter is initialized to at most four
  575. * packets for new connections, following the rules from [RFC3390]".
  576. * We need to convert the bytes of RFC3390 into the packets of RFC 4341.
  577. */
  578. hc->tx_cwnd = clamp(4380U / dp->dccps_mss_cache, 2U, 4U);
  579. /* Make sure that Ack Ratio is enabled and within bounds. */
  580. max_ratio = DIV_ROUND_UP(hc->tx_cwnd, 2);
  581. if (dp->dccps_l_ack_ratio == 0 || dp->dccps_l_ack_ratio > max_ratio)
  582. dp->dccps_l_ack_ratio = max_ratio;
  583. /* XXX init ~ to window size... */
  584. if (ccid2_hc_tx_alloc_seq(hc))
  585. return -ENOMEM;
  586. hc->tx_rto = DCCP_TIMEOUT_INIT;
  587. hc->tx_rpdupack = -1;
  588. hc->tx_last_cong = ccid2_time_stamp;
  589. setup_timer(&hc->tx_rtotimer, ccid2_hc_tx_rto_expire,
  590. (unsigned long)sk);
  591. return 0;
  592. }
  593. static void ccid2_hc_tx_exit(struct sock *sk)
  594. {
  595. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  596. int i;
  597. ccid2_hc_tx_kill_rto_timer(sk);
  598. for (i = 0; i < hc->tx_seqbufc; i++)
  599. kfree(hc->tx_seqbuf[i]);
  600. hc->tx_seqbufc = 0;
  601. }
  602. static void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
  603. {
  604. const struct dccp_sock *dp = dccp_sk(sk);
  605. struct ccid2_hc_rx_sock *hc = ccid2_hc_rx_sk(sk);
  606. switch (DCCP_SKB_CB(skb)->dccpd_type) {
  607. case DCCP_PKT_DATA:
  608. case DCCP_PKT_DATAACK:
  609. hc->rx_data++;
  610. if (hc->rx_data >= dp->dccps_r_ack_ratio) {
  611. dccp_send_ack(sk);
  612. hc->rx_data = 0;
  613. }
  614. break;
  615. }
  616. }
  617. struct ccid_operations ccid2_ops = {
  618. .ccid_id = DCCPC_CCID2,
  619. .ccid_name = "TCP-like",
  620. .ccid_hc_tx_obj_size = sizeof(struct ccid2_hc_tx_sock),
  621. .ccid_hc_tx_init = ccid2_hc_tx_init,
  622. .ccid_hc_tx_exit = ccid2_hc_tx_exit,
  623. .ccid_hc_tx_send_packet = ccid2_hc_tx_send_packet,
  624. .ccid_hc_tx_packet_sent = ccid2_hc_tx_packet_sent,
  625. .ccid_hc_tx_packet_recv = ccid2_hc_tx_packet_recv,
  626. .ccid_hc_rx_obj_size = sizeof(struct ccid2_hc_rx_sock),
  627. .ccid_hc_rx_packet_recv = ccid2_hc_rx_packet_recv,
  628. };
  629. #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
  630. module_param(ccid2_debug, bool, 0644);
  631. MODULE_PARM_DESC(ccid2_debug, "Enable CCID-2 debug messages");
  632. #endif