ccid2.c 20 KB

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