ccid2.c 21 KB

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