ccid2.c 20 KB

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