ccid2.c 22 KB

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