ccid2.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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 sock *sk)
  334. {
  335. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  336. sk_stop_timer(sk, &hctx->ccid2hctx_rtotimer);
  337. ccid2_pr_debug("deleted RTO timer\n");
  338. }
  339. static inline void ccid2_new_ack(struct sock *sk,
  340. struct ccid2_seq *seqp,
  341. unsigned int *maxincr)
  342. {
  343. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  344. /* slow start */
  345. if (hctx->ccid2hctx_cwnd < hctx->ccid2hctx_ssthresh) {
  346. hctx->ccid2hctx_acks = 0;
  347. /* We can increase cwnd at most maxincr [ack_ratio/2] */
  348. if (*maxincr) {
  349. /* increase every 2 acks */
  350. hctx->ccid2hctx_ssacks++;
  351. if (hctx->ccid2hctx_ssacks == 2) {
  352. ccid2_change_cwnd(sk, hctx->ccid2hctx_cwnd + 1);
  353. hctx->ccid2hctx_ssacks = 0;
  354. *maxincr = *maxincr - 1;
  355. }
  356. }
  357. /* increased cwnd enough for this single ack */
  358. else {
  359. hctx->ccid2hctx_ssacks = 0;
  360. }
  361. }
  362. else {
  363. hctx->ccid2hctx_ssacks = 0;
  364. hctx->ccid2hctx_acks++;
  365. if (hctx->ccid2hctx_acks >= hctx->ccid2hctx_cwnd) {
  366. ccid2_change_cwnd(sk, hctx->ccid2hctx_cwnd + 1);
  367. hctx->ccid2hctx_acks = 0;
  368. }
  369. }
  370. /* update RTO */
  371. if (hctx->ccid2hctx_srtt == -1 ||
  372. (jiffies - hctx->ccid2hctx_lastrtt) >= hctx->ccid2hctx_srtt) {
  373. unsigned long r = jiffies - seqp->ccid2s_sent;
  374. int s;
  375. /* first measurement */
  376. if (hctx->ccid2hctx_srtt == -1) {
  377. ccid2_pr_debug("R: %lu Time=%lu seq=%llu\n",
  378. r, jiffies, seqp->ccid2s_seq);
  379. hctx->ccid2hctx_srtt = r;
  380. hctx->ccid2hctx_rttvar = r >> 1;
  381. }
  382. else {
  383. /* RTTVAR */
  384. long tmp = hctx->ccid2hctx_srtt - r;
  385. if (tmp < 0)
  386. tmp *= -1;
  387. tmp >>= 2;
  388. hctx->ccid2hctx_rttvar *= 3;
  389. hctx->ccid2hctx_rttvar >>= 2;
  390. hctx->ccid2hctx_rttvar += tmp;
  391. /* SRTT */
  392. hctx->ccid2hctx_srtt *= 7;
  393. hctx->ccid2hctx_srtt >>= 3;
  394. tmp = r >> 3;
  395. hctx->ccid2hctx_srtt += tmp;
  396. }
  397. s = hctx->ccid2hctx_rttvar << 2;
  398. /* clock granularity is 1 when based on jiffies */
  399. if (!s)
  400. s = 1;
  401. hctx->ccid2hctx_rto = hctx->ccid2hctx_srtt + s;
  402. /* must be at least a second */
  403. s = hctx->ccid2hctx_rto / HZ;
  404. /* DCCP doesn't require this [but I like it cuz my code sux] */
  405. #if 1
  406. if (s < 1)
  407. hctx->ccid2hctx_rto = HZ;
  408. #endif
  409. /* max 60 seconds */
  410. if (s > 60)
  411. hctx->ccid2hctx_rto = HZ * 60;
  412. hctx->ccid2hctx_lastrtt = jiffies;
  413. ccid2_pr_debug("srtt: %ld rttvar: %ld rto: %ld (HZ=%d) R=%lu\n",
  414. hctx->ccid2hctx_srtt, hctx->ccid2hctx_rttvar,
  415. hctx->ccid2hctx_rto, HZ, r);
  416. hctx->ccid2hctx_sent = 0;
  417. }
  418. /* we got a new ack, so re-start RTO timer */
  419. ccid2_hc_tx_kill_rto_timer(sk);
  420. ccid2_start_rto_timer(sk);
  421. }
  422. static void ccid2_hc_tx_dec_pipe(struct sock *sk)
  423. {
  424. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  425. hctx->ccid2hctx_pipe--;
  426. BUG_ON(hctx->ccid2hctx_pipe < 0);
  427. if (hctx->ccid2hctx_pipe == 0)
  428. ccid2_hc_tx_kill_rto_timer(sk);
  429. }
  430. static void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
  431. {
  432. struct dccp_sock *dp = dccp_sk(sk);
  433. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  434. u64 ackno, seqno;
  435. struct ccid2_seq *seqp;
  436. unsigned char *vector;
  437. unsigned char veclen;
  438. int offset = 0;
  439. int done = 0;
  440. int loss = 0;
  441. unsigned int maxincr = 0;
  442. ccid2_hc_tx_check_sanity(hctx);
  443. /* check reverse path congestion */
  444. seqno = DCCP_SKB_CB(skb)->dccpd_seq;
  445. /* XXX this whole "algorithm" is broken. Need to fix it to keep track
  446. * of the seqnos of the dupacks so that rpseq and rpdupack are correct
  447. * -sorbo.
  448. */
  449. /* need to bootstrap */
  450. if (hctx->ccid2hctx_rpdupack == -1) {
  451. hctx->ccid2hctx_rpdupack = 0;
  452. hctx->ccid2hctx_rpseq = seqno;
  453. }
  454. else {
  455. /* check if packet is consecutive */
  456. if ((hctx->ccid2hctx_rpseq + 1) == seqno) {
  457. hctx->ccid2hctx_rpseq++;
  458. }
  459. /* it's a later packet */
  460. else if (after48(seqno, hctx->ccid2hctx_rpseq)) {
  461. hctx->ccid2hctx_rpdupack++;
  462. /* check if we got enough dupacks */
  463. if (hctx->ccid2hctx_rpdupack >=
  464. hctx->ccid2hctx_numdupack) {
  465. hctx->ccid2hctx_rpdupack = -1; /* XXX lame */
  466. hctx->ccid2hctx_rpseq = 0;
  467. ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio << 1);
  468. }
  469. }
  470. }
  471. /* check forward path congestion */
  472. /* still didn't send out new data packets */
  473. if (hctx->ccid2hctx_seqh == hctx->ccid2hctx_seqt)
  474. return;
  475. switch (DCCP_SKB_CB(skb)->dccpd_type) {
  476. case DCCP_PKT_ACK:
  477. case DCCP_PKT_DATAACK:
  478. break;
  479. default:
  480. return;
  481. }
  482. ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
  483. seqp = hctx->ccid2hctx_seqh->ccid2s_prev;
  484. /* If in slow-start, cwnd can increase at most Ack Ratio / 2 packets for
  485. * this single ack. I round up.
  486. * -sorbo.
  487. */
  488. maxincr = dp->dccps_l_ack_ratio >> 1;
  489. maxincr++;
  490. /* go through all ack vectors */
  491. while ((offset = ccid2_ackvector(sk, skb, offset,
  492. &vector, &veclen)) != -1) {
  493. /* go through this ack vector */
  494. while (veclen--) {
  495. const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
  496. u64 ackno_end_rl;
  497. dccp_set_seqno(&ackno_end_rl, ackno - rl);
  498. ccid2_pr_debug("ackvec start:%llu end:%llu\n", ackno,
  499. ackno_end_rl);
  500. /* if the seqno we are analyzing is larger than the
  501. * current ackno, then move towards the tail of our
  502. * seqnos.
  503. */
  504. while (after48(seqp->ccid2s_seq, ackno)) {
  505. if (seqp == hctx->ccid2hctx_seqt) {
  506. done = 1;
  507. break;
  508. }
  509. seqp = seqp->ccid2s_prev;
  510. }
  511. if (done)
  512. break;
  513. /* check all seqnos in the range of the vector
  514. * run length
  515. */
  516. while (between48(seqp->ccid2s_seq,ackno_end_rl,ackno)) {
  517. const u8 state = (*vector &
  518. DCCP_ACKVEC_STATE_MASK) >> 6;
  519. /* new packet received or marked */
  520. if (state != DCCP_ACKVEC_STATE_NOT_RECEIVED &&
  521. !seqp->ccid2s_acked) {
  522. if (state ==
  523. DCCP_ACKVEC_STATE_ECN_MARKED) {
  524. loss = 1;
  525. }
  526. else {
  527. ccid2_new_ack(sk, seqp,
  528. &maxincr);
  529. }
  530. seqp->ccid2s_acked = 1;
  531. ccid2_pr_debug("Got ack for %llu\n",
  532. seqp->ccid2s_seq);
  533. ccid2_hc_tx_dec_pipe(sk);
  534. }
  535. if (seqp == hctx->ccid2hctx_seqt) {
  536. done = 1;
  537. break;
  538. }
  539. seqp = seqp->ccid2s_next;
  540. }
  541. if (done)
  542. break;
  543. dccp_set_seqno(&ackno, ackno_end_rl - 1);
  544. vector++;
  545. }
  546. if (done)
  547. break;
  548. }
  549. /* The state about what is acked should be correct now
  550. * Check for NUMDUPACK
  551. */
  552. seqp = hctx->ccid2hctx_seqh->ccid2s_prev;
  553. done = 0;
  554. while (1) {
  555. if (seqp->ccid2s_acked) {
  556. done++;
  557. if (done == hctx->ccid2hctx_numdupack) {
  558. break;
  559. }
  560. }
  561. if (seqp == hctx->ccid2hctx_seqt) {
  562. break;
  563. }
  564. seqp = seqp->ccid2s_prev;
  565. }
  566. /* If there are at least 3 acknowledgements, anything unacknowledged
  567. * below the last sequence number is considered lost
  568. */
  569. if (done == hctx->ccid2hctx_numdupack) {
  570. struct ccid2_seq *last_acked = seqp;
  571. /* check for lost packets */
  572. while (1) {
  573. if (!seqp->ccid2s_acked) {
  574. loss = 1;
  575. ccid2_hc_tx_dec_pipe(sk);
  576. }
  577. if (seqp == hctx->ccid2hctx_seqt)
  578. break;
  579. seqp = seqp->ccid2s_prev;
  580. }
  581. hctx->ccid2hctx_seqt = last_acked;
  582. }
  583. /* trim acked packets in tail */
  584. while (hctx->ccid2hctx_seqt != hctx->ccid2hctx_seqh) {
  585. if (!hctx->ccid2hctx_seqt->ccid2s_acked)
  586. break;
  587. hctx->ccid2hctx_seqt = hctx->ccid2hctx_seqt->ccid2s_next;
  588. }
  589. if (loss) {
  590. /* XXX do bit shifts guarantee a 0 as the new bit? */
  591. ccid2_change_cwnd(sk, hctx->ccid2hctx_cwnd >> 1);
  592. hctx->ccid2hctx_ssthresh = hctx->ccid2hctx_cwnd;
  593. if (hctx->ccid2hctx_ssthresh < 2)
  594. hctx->ccid2hctx_ssthresh = 2;
  595. }
  596. ccid2_hc_tx_check_sanity(hctx);
  597. }
  598. static int ccid2_hc_tx_init(struct sock *sk)
  599. {
  600. struct dccp_sock *dp = dccp_sk(sk);
  601. struct ccid2_hc_tx_sock *hctx;
  602. int seqcount = ccid2_seq_len;
  603. int i;
  604. dp->dccps_hc_tx_ccid_private = kzalloc(sizeof(*hctx), gfp_any());
  605. if (dp->dccps_hc_tx_ccid_private == NULL)
  606. return -ENOMEM;
  607. hctx = ccid2_hc_tx_sk(sk);
  608. /* XXX init variables with proper values */
  609. hctx->ccid2hctx_cwnd = 1;
  610. hctx->ccid2hctx_ssthresh = 10;
  611. hctx->ccid2hctx_numdupack = 3;
  612. /* XXX init ~ to window size... */
  613. hctx->ccid2hctx_seqbuf = kmalloc(sizeof(*hctx->ccid2hctx_seqbuf) *
  614. seqcount, gfp_any());
  615. if (hctx->ccid2hctx_seqbuf == NULL) {
  616. kfree(dp->dccps_hc_tx_ccid_private);
  617. dp->dccps_hc_tx_ccid_private = NULL;
  618. return -ENOMEM;
  619. }
  620. for (i = 0; i < (seqcount - 1); i++) {
  621. hctx->ccid2hctx_seqbuf[i].ccid2s_next =
  622. &hctx->ccid2hctx_seqbuf[i + 1];
  623. hctx->ccid2hctx_seqbuf[i + 1].ccid2s_prev =
  624. &hctx->ccid2hctx_seqbuf[i];
  625. }
  626. hctx->ccid2hctx_seqbuf[seqcount - 1].ccid2s_next =
  627. hctx->ccid2hctx_seqbuf;
  628. hctx->ccid2hctx_seqbuf->ccid2s_prev =
  629. &hctx->ccid2hctx_seqbuf[seqcount - 1];
  630. hctx->ccid2hctx_seqh = hctx->ccid2hctx_seqbuf;
  631. hctx->ccid2hctx_seqt = hctx->ccid2hctx_seqh;
  632. hctx->ccid2hctx_sent = 0;
  633. hctx->ccid2hctx_rto = 3 * HZ;
  634. hctx->ccid2hctx_srtt = -1;
  635. hctx->ccid2hctx_rttvar = -1;
  636. hctx->ccid2hctx_lastrtt = 0;
  637. hctx->ccid2hctx_rpdupack = -1;
  638. hctx->ccid2hctx_rtotimer.function = &ccid2_hc_tx_rto_expire;
  639. hctx->ccid2hctx_rtotimer.data = (unsigned long)sk;
  640. init_timer(&hctx->ccid2hctx_rtotimer);
  641. ccid2_hc_tx_check_sanity(hctx);
  642. return 0;
  643. }
  644. static void ccid2_hc_tx_exit(struct sock *sk)
  645. {
  646. struct dccp_sock *dp = dccp_sk(sk);
  647. struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
  648. ccid2_hc_tx_kill_rto_timer(sk);
  649. kfree(hctx->ccid2hctx_seqbuf);
  650. kfree(dp->dccps_hc_tx_ccid_private);
  651. dp->dccps_hc_tx_ccid_private = NULL;
  652. }
  653. static void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
  654. {
  655. const struct dccp_sock *dp = dccp_sk(sk);
  656. struct ccid2_hc_rx_sock *hcrx = ccid2_hc_rx_sk(sk);
  657. switch (DCCP_SKB_CB(skb)->dccpd_type) {
  658. case DCCP_PKT_DATA:
  659. case DCCP_PKT_DATAACK:
  660. hcrx->ccid2hcrx_data++;
  661. if (hcrx->ccid2hcrx_data >= dp->dccps_r_ack_ratio) {
  662. dccp_send_ack(sk);
  663. hcrx->ccid2hcrx_data = 0;
  664. }
  665. break;
  666. }
  667. }
  668. static int ccid2_hc_rx_init(struct sock *sk)
  669. {
  670. struct dccp_sock *dp = dccp_sk(sk);
  671. dp->dccps_hc_rx_ccid_private = kzalloc(sizeof(struct ccid2_hc_rx_sock),
  672. gfp_any());
  673. return dp->dccps_hc_rx_ccid_private == NULL ? -ENOMEM : 0;
  674. }
  675. static void ccid2_hc_rx_exit(struct sock *sk)
  676. {
  677. struct dccp_sock *dp = dccp_sk(sk);
  678. kfree(dp->dccps_hc_rx_ccid_private);
  679. dp->dccps_hc_rx_ccid_private = NULL;
  680. }
  681. static struct ccid ccid2 = {
  682. .ccid_id = 2,
  683. .ccid_name = "ccid2",
  684. .ccid_owner = THIS_MODULE,
  685. .ccid_hc_tx_init = ccid2_hc_tx_init,
  686. .ccid_hc_tx_exit = ccid2_hc_tx_exit,
  687. .ccid_hc_tx_send_packet = ccid2_hc_tx_send_packet,
  688. .ccid_hc_tx_packet_sent = ccid2_hc_tx_packet_sent,
  689. .ccid_hc_tx_packet_recv = ccid2_hc_tx_packet_recv,
  690. .ccid_hc_rx_init = ccid2_hc_rx_init,
  691. .ccid_hc_rx_exit = ccid2_hc_rx_exit,
  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 CCID2 CCID");
  708. MODULE_LICENSE("GPL");
  709. MODULE_ALIAS("net-dccp-ccid-2");