ccid2.c 19 KB

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