ccid2.c 22 KB

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