ccid2.c 22 KB

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