ccid2.c 21 KB

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