ccid2.c 21 KB

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