ccid2.c 22 KB

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