ccid2.c 20 KB

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