ccid2.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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, int more, 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. /* XXX Lame code duplication!
  267. * returns -1 if none was found.
  268. * else returns the next offset to use in the function call.
  269. */
  270. static int ccid2_ackvector(struct sock *sk, struct sk_buff *skb, int offset,
  271. unsigned char **vec, unsigned char *veclen)
  272. {
  273. const struct dccp_hdr *dh = dccp_hdr(skb);
  274. unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb);
  275. unsigned char *opt_ptr;
  276. const unsigned char *opt_end = (unsigned char *)dh +
  277. (dh->dccph_doff * 4);
  278. unsigned char opt, len;
  279. unsigned char *value;
  280. BUG_ON(offset < 0);
  281. options += offset;
  282. opt_ptr = options;
  283. if (opt_ptr >= opt_end)
  284. return -1;
  285. while (opt_ptr != opt_end) {
  286. opt = *opt_ptr++;
  287. len = 0;
  288. value = NULL;
  289. /* Check if this isn't a single byte option */
  290. if (opt > DCCPO_MAX_RESERVED) {
  291. if (opt_ptr == opt_end)
  292. goto out_invalid_option;
  293. len = *opt_ptr++;
  294. if (len < 3)
  295. goto out_invalid_option;
  296. /*
  297. * Remove the type and len fields, leaving
  298. * just the value size
  299. */
  300. len -= 2;
  301. value = opt_ptr;
  302. opt_ptr += len;
  303. if (opt_ptr > opt_end)
  304. goto out_invalid_option;
  305. }
  306. switch (opt) {
  307. case DCCPO_ACK_VECTOR_0:
  308. case DCCPO_ACK_VECTOR_1:
  309. *vec = value;
  310. *veclen = len;
  311. return offset + (opt_ptr - options);
  312. }
  313. }
  314. return -1;
  315. out_invalid_option:
  316. DCCP_BUG("Invalid option - this should not happen (previous parsing)!");
  317. return -1;
  318. }
  319. static void ccid2_hc_tx_kill_rto_timer(struct sock *sk)
  320. {
  321. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  322. sk_stop_timer(sk, &hctx->rtotimer);
  323. ccid2_pr_debug("deleted RTO timer\n");
  324. }
  325. static inline void ccid2_new_ack(struct sock *sk,
  326. struct ccid2_seq *seqp,
  327. unsigned int *maxincr)
  328. {
  329. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  330. if (hctx->cwnd < hctx->ssthresh) {
  331. if (*maxincr > 0 && ++hctx->packets_acked == 2) {
  332. hctx->cwnd += 1;
  333. *maxincr -= 1;
  334. hctx->packets_acked = 0;
  335. }
  336. } else if (++hctx->packets_acked >= hctx->cwnd) {
  337. hctx->cwnd += 1;
  338. hctx->packets_acked = 0;
  339. }
  340. /* update RTO */
  341. if (hctx->srtt == -1 ||
  342. time_after(jiffies, hctx->lastrtt + hctx->srtt)) {
  343. unsigned long r = (long)jiffies - (long)seqp->ccid2s_sent;
  344. int s;
  345. /* first measurement */
  346. if (hctx->srtt == -1) {
  347. ccid2_pr_debug("R: %lu Time=%lu seq=%llu\n",
  348. r, jiffies,
  349. (unsigned long long)seqp->ccid2s_seq);
  350. ccid2_change_srtt(hctx, r);
  351. hctx->rttvar = r >> 1;
  352. } else {
  353. /* RTTVAR */
  354. long tmp = hctx->srtt - r;
  355. long srtt;
  356. if (tmp < 0)
  357. tmp *= -1;
  358. tmp >>= 2;
  359. hctx->rttvar *= 3;
  360. hctx->rttvar >>= 2;
  361. hctx->rttvar += tmp;
  362. /* SRTT */
  363. srtt = hctx->srtt;
  364. srtt *= 7;
  365. srtt >>= 3;
  366. tmp = r >> 3;
  367. srtt += tmp;
  368. ccid2_change_srtt(hctx, srtt);
  369. }
  370. s = hctx->rttvar << 2;
  371. /* clock granularity is 1 when based on jiffies */
  372. if (!s)
  373. s = 1;
  374. hctx->rto = hctx->srtt + s;
  375. /* must be at least a second */
  376. s = hctx->rto / HZ;
  377. /* DCCP doesn't require this [but I like it cuz my code sux] */
  378. #if 1
  379. if (s < 1)
  380. hctx->rto = HZ;
  381. #endif
  382. /* max 60 seconds */
  383. if (s > 60)
  384. hctx->rto = HZ * 60;
  385. hctx->lastrtt = jiffies;
  386. ccid2_pr_debug("srtt: %ld rttvar: %ld rto: %ld (HZ=%d) R=%lu\n",
  387. hctx->srtt, hctx->rttvar,
  388. hctx->rto, HZ, r);
  389. }
  390. /* we got a new ack, so re-start RTO timer */
  391. ccid2_hc_tx_kill_rto_timer(sk);
  392. ccid2_start_rto_timer(sk);
  393. }
  394. static void ccid2_hc_tx_dec_pipe(struct sock *sk)
  395. {
  396. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  397. if (hctx->pipe == 0)
  398. DCCP_BUG("pipe == 0");
  399. else
  400. hctx->pipe--;
  401. if (hctx->pipe == 0)
  402. ccid2_hc_tx_kill_rto_timer(sk);
  403. }
  404. static void ccid2_congestion_event(struct sock *sk, struct ccid2_seq *seqp)
  405. {
  406. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  407. if (time_before(seqp->ccid2s_sent, hctx->last_cong)) {
  408. ccid2_pr_debug("Multiple losses in an RTT---treating as one\n");
  409. return;
  410. }
  411. hctx->last_cong = jiffies;
  412. hctx->cwnd = hctx->cwnd / 2 ? : 1U;
  413. hctx->ssthresh = max(hctx->cwnd, 2U);
  414. /* Avoid spurious timeouts resulting from Ack Ratio > cwnd */
  415. if (dccp_sk(sk)->dccps_l_ack_ratio > hctx->cwnd)
  416. ccid2_change_l_ack_ratio(sk, hctx->cwnd);
  417. }
  418. static void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
  419. {
  420. struct dccp_sock *dp = dccp_sk(sk);
  421. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  422. u64 ackno, seqno;
  423. struct ccid2_seq *seqp;
  424. unsigned char *vector;
  425. unsigned char veclen;
  426. int offset = 0;
  427. int done = 0;
  428. unsigned int maxincr = 0;
  429. ccid2_hc_tx_check_sanity(hctx);
  430. /* check reverse path congestion */
  431. seqno = DCCP_SKB_CB(skb)->dccpd_seq;
  432. /* XXX this whole "algorithm" is broken. Need to fix it to keep track
  433. * of the seqnos of the dupacks so that rpseq and rpdupack are correct
  434. * -sorbo.
  435. */
  436. /* need to bootstrap */
  437. if (hctx->rpdupack == -1) {
  438. hctx->rpdupack = 0;
  439. hctx->rpseq = seqno;
  440. } else {
  441. /* check if packet is consecutive */
  442. if (dccp_delta_seqno(hctx->rpseq, seqno) == 1)
  443. hctx->rpseq = seqno;
  444. /* it's a later packet */
  445. else if (after48(seqno, hctx->rpseq)) {
  446. hctx->rpdupack++;
  447. /* check if we got enough dupacks */
  448. if (hctx->rpdupack >= NUMDUPACK) {
  449. hctx->rpdupack = -1; /* XXX lame */
  450. hctx->rpseq = 0;
  451. ccid2_change_l_ack_ratio(sk, 2 * dp->dccps_l_ack_ratio);
  452. }
  453. }
  454. }
  455. /* check forward path congestion */
  456. /* still didn't send out new data packets */
  457. if (hctx->seqh == hctx->seqt)
  458. return;
  459. switch (DCCP_SKB_CB(skb)->dccpd_type) {
  460. case DCCP_PKT_ACK:
  461. case DCCP_PKT_DATAACK:
  462. break;
  463. default:
  464. return;
  465. }
  466. ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
  467. if (after48(ackno, hctx->high_ack))
  468. hctx->high_ack = ackno;
  469. seqp = hctx->seqt;
  470. while (before48(seqp->ccid2s_seq, ackno)) {
  471. seqp = seqp->ccid2s_next;
  472. if (seqp == hctx->seqh) {
  473. seqp = hctx->seqh->ccid2s_prev;
  474. break;
  475. }
  476. }
  477. /*
  478. * In slow-start, cwnd can increase up to a maximum of Ack Ratio/2
  479. * packets per acknowledgement. Rounding up avoids that cwnd is not
  480. * advanced when Ack Ratio is 1 and gives a slight edge otherwise.
  481. */
  482. if (hctx->cwnd < hctx->ssthresh)
  483. maxincr = DIV_ROUND_UP(dp->dccps_l_ack_ratio, 2);
  484. /* go through all ack vectors */
  485. while ((offset = ccid2_ackvector(sk, skb, offset,
  486. &vector, &veclen)) != -1) {
  487. /* go through this ack vector */
  488. while (veclen--) {
  489. const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
  490. u64 ackno_end_rl = SUB48(ackno, rl);
  491. ccid2_pr_debug("ackvec start:%llu end:%llu\n",
  492. (unsigned long long)ackno,
  493. (unsigned long long)ackno_end_rl);
  494. /* if the seqno we are analyzing is larger than the
  495. * current ackno, then move towards the tail of our
  496. * seqnos.
  497. */
  498. while (after48(seqp->ccid2s_seq, ackno)) {
  499. if (seqp == hctx->seqt) {
  500. done = 1;
  501. break;
  502. }
  503. seqp = seqp->ccid2s_prev;
  504. }
  505. if (done)
  506. break;
  507. /* check all seqnos in the range of the vector
  508. * run length
  509. */
  510. while (between48(seqp->ccid2s_seq,ackno_end_rl,ackno)) {
  511. const u8 state = *vector &
  512. DCCP_ACKVEC_STATE_MASK;
  513. /* new packet received or marked */
  514. if (state != DCCP_ACKVEC_STATE_NOT_RECEIVED &&
  515. !seqp->ccid2s_acked) {
  516. if (state ==
  517. DCCP_ACKVEC_STATE_ECN_MARKED) {
  518. ccid2_congestion_event(sk,
  519. seqp);
  520. } else
  521. ccid2_new_ack(sk, seqp,
  522. &maxincr);
  523. seqp->ccid2s_acked = 1;
  524. ccid2_pr_debug("Got ack for %llu\n",
  525. (unsigned long long)seqp->ccid2s_seq);
  526. ccid2_hc_tx_dec_pipe(sk);
  527. }
  528. if (seqp == hctx->seqt) {
  529. done = 1;
  530. break;
  531. }
  532. seqp = seqp->ccid2s_prev;
  533. }
  534. if (done)
  535. break;
  536. ackno = SUB48(ackno_end_rl, 1);
  537. vector++;
  538. }
  539. if (done)
  540. break;
  541. }
  542. /* The state about what is acked should be correct now
  543. * Check for NUMDUPACK
  544. */
  545. seqp = hctx->seqt;
  546. while (before48(seqp->ccid2s_seq, hctx->high_ack)) {
  547. seqp = seqp->ccid2s_next;
  548. if (seqp == hctx->seqh) {
  549. seqp = hctx->seqh->ccid2s_prev;
  550. break;
  551. }
  552. }
  553. done = 0;
  554. while (1) {
  555. if (seqp->ccid2s_acked) {
  556. done++;
  557. if (done == NUMDUPACK)
  558. break;
  559. }
  560. if (seqp == hctx->seqt)
  561. break;
  562. seqp = seqp->ccid2s_prev;
  563. }
  564. /* If there are at least 3 acknowledgements, anything unacknowledged
  565. * below the last sequence number is considered lost
  566. */
  567. if (done == NUMDUPACK) {
  568. struct ccid2_seq *last_acked = seqp;
  569. /* check for lost packets */
  570. while (1) {
  571. if (!seqp->ccid2s_acked) {
  572. ccid2_pr_debug("Packet lost: %llu\n",
  573. (unsigned long long)seqp->ccid2s_seq);
  574. /* XXX need to traverse from tail -> head in
  575. * order to detect multiple congestion events in
  576. * one ack vector.
  577. */
  578. ccid2_congestion_event(sk, seqp);
  579. ccid2_hc_tx_dec_pipe(sk);
  580. }
  581. if (seqp == hctx->seqt)
  582. break;
  583. seqp = seqp->ccid2s_prev;
  584. }
  585. hctx->seqt = last_acked;
  586. }
  587. /* trim acked packets in tail */
  588. while (hctx->seqt != hctx->seqh) {
  589. if (!hctx->seqt->ccid2s_acked)
  590. break;
  591. hctx->seqt = hctx->seqt->ccid2s_next;
  592. }
  593. ccid2_hc_tx_check_sanity(hctx);
  594. }
  595. static int ccid2_hc_tx_init(struct ccid *ccid, struct sock *sk)
  596. {
  597. struct ccid2_hc_tx_sock *hctx = ccid_priv(ccid);
  598. struct dccp_sock *dp = dccp_sk(sk);
  599. u32 max_ratio;
  600. /* RFC 4341, 5: initialise ssthresh to arbitrarily high (max) value */
  601. hctx->ssthresh = ~0U;
  602. /*
  603. * RFC 4341, 5: "The cwnd parameter is initialized to at most four
  604. * packets for new connections, following the rules from [RFC3390]".
  605. * We need to convert the bytes of RFC3390 into the packets of RFC 4341.
  606. */
  607. hctx->cwnd = clamp(4380U / dp->dccps_mss_cache, 2U, 4U);
  608. /* Make sure that Ack Ratio is enabled and within bounds. */
  609. max_ratio = DIV_ROUND_UP(hctx->cwnd, 2);
  610. if (dp->dccps_l_ack_ratio == 0 || dp->dccps_l_ack_ratio > max_ratio)
  611. dp->dccps_l_ack_ratio = max_ratio;
  612. /* XXX init ~ to window size... */
  613. if (ccid2_hc_tx_alloc_seq(hctx))
  614. return -ENOMEM;
  615. hctx->rto = 3 * HZ;
  616. ccid2_change_srtt(hctx, -1);
  617. hctx->rttvar = -1;
  618. hctx->rpdupack = -1;
  619. hctx->last_cong = jiffies;
  620. setup_timer(&hctx->rtotimer, ccid2_hc_tx_rto_expire, (unsigned long)sk);
  621. ccid2_hc_tx_check_sanity(hctx);
  622. return 0;
  623. }
  624. static void ccid2_hc_tx_exit(struct sock *sk)
  625. {
  626. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  627. int i;
  628. ccid2_hc_tx_kill_rto_timer(sk);
  629. for (i = 0; i < hctx->seqbufc; i++)
  630. kfree(hctx->seqbuf[i]);
  631. hctx->seqbufc = 0;
  632. }
  633. static void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
  634. {
  635. const struct dccp_sock *dp = dccp_sk(sk);
  636. struct ccid2_hc_rx_sock *hcrx = ccid2_hc_rx_sk(sk);
  637. switch (DCCP_SKB_CB(skb)->dccpd_type) {
  638. case DCCP_PKT_DATA:
  639. case DCCP_PKT_DATAACK:
  640. hcrx->data++;
  641. if (hcrx->data >= dp->dccps_r_ack_ratio) {
  642. dccp_send_ack(sk);
  643. hcrx->data = 0;
  644. }
  645. break;
  646. }
  647. }
  648. static struct ccid_operations ccid2 = {
  649. .ccid_id = DCCPC_CCID2,
  650. .ccid_name = "TCP-like",
  651. .ccid_owner = THIS_MODULE,
  652. .ccid_hc_tx_obj_size = sizeof(struct ccid2_hc_tx_sock),
  653. .ccid_hc_tx_init = ccid2_hc_tx_init,
  654. .ccid_hc_tx_exit = ccid2_hc_tx_exit,
  655. .ccid_hc_tx_send_packet = ccid2_hc_tx_send_packet,
  656. .ccid_hc_tx_packet_sent = ccid2_hc_tx_packet_sent,
  657. .ccid_hc_tx_packet_recv = ccid2_hc_tx_packet_recv,
  658. .ccid_hc_rx_obj_size = sizeof(struct ccid2_hc_rx_sock),
  659. .ccid_hc_rx_packet_recv = ccid2_hc_rx_packet_recv,
  660. };
  661. #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
  662. module_param(ccid2_debug, bool, 0644);
  663. MODULE_PARM_DESC(ccid2_debug, "Enable debug messages");
  664. #endif
  665. static __init int ccid2_module_init(void)
  666. {
  667. return ccid_register(&ccid2);
  668. }
  669. module_init(ccid2_module_init);
  670. static __exit void ccid2_module_exit(void)
  671. {
  672. ccid_unregister(&ccid2);
  673. }
  674. module_exit(ccid2_module_exit);
  675. MODULE_AUTHOR("Andrea Bittau <a.bittau@cs.ucl.ac.uk>");
  676. MODULE_DESCRIPTION("DCCP TCP-Like (CCID2) CCID");
  677. MODULE_LICENSE("GPL");
  678. MODULE_ALIAS("net-dccp-ccid-2");