ccid2.c 22 KB

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