ccid2.c 21 KB

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