ccid2.c 22 KB

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