ccid2.c 19 KB

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