ccid2.c 19 KB

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