ccid2.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /*
  2. * Copyright (c) 2005, 2006 Andrea Bittau <a.bittau@cs.ucl.ac.uk>
  3. *
  4. * Changes to meet Linux coding standards, and DCCP infrastructure fixes.
  5. *
  6. * Copyright (c) 2006 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. /*
  23. * This implementation should follow RFC 4341
  24. */
  25. #include <linux/slab.h>
  26. #include "../feat.h"
  27. #include "../ccid.h"
  28. #include "../dccp.h"
  29. #include "ccid2.h"
  30. #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
  31. static int ccid2_debug;
  32. #define ccid2_pr_debug(format, a...) DCCP_PR_DEBUG(ccid2_debug, format, ##a)
  33. static void ccid2_hc_tx_check_sanity(const struct ccid2_hc_tx_sock *hc)
  34. {
  35. int len = 0;
  36. int pipe = 0;
  37. struct ccid2_seq *seqp = hc->tx_seqh;
  38. /* there is data in the chain */
  39. if (seqp != hc->tx_seqt) {
  40. seqp = seqp->ccid2s_prev;
  41. len++;
  42. if (!seqp->ccid2s_acked)
  43. pipe++;
  44. while (seqp != hc->tx_seqt) {
  45. struct ccid2_seq *prev = seqp->ccid2s_prev;
  46. len++;
  47. if (!prev->ccid2s_acked)
  48. pipe++;
  49. /* packets are sent sequentially */
  50. BUG_ON(dccp_delta_seqno(seqp->ccid2s_seq,
  51. prev->ccid2s_seq ) >= 0);
  52. BUG_ON(time_before(seqp->ccid2s_sent,
  53. prev->ccid2s_sent));
  54. seqp = prev;
  55. }
  56. }
  57. BUG_ON(pipe != hc->tx_pipe);
  58. ccid2_pr_debug("len of chain=%d\n", len);
  59. do {
  60. seqp = seqp->ccid2s_prev;
  61. len++;
  62. } while (seqp != hc->tx_seqh);
  63. ccid2_pr_debug("total len=%d\n", len);
  64. BUG_ON(len != hc->tx_seqbufc * CCID2_SEQBUF_LEN);
  65. }
  66. #else
  67. #define ccid2_pr_debug(format, a...)
  68. #define ccid2_hc_tx_check_sanity(hc)
  69. #endif
  70. static int ccid2_hc_tx_alloc_seq(struct ccid2_hc_tx_sock *hc)
  71. {
  72. struct ccid2_seq *seqp;
  73. int i;
  74. /* check if we have space to preserve the pointer to the buffer */
  75. if (hc->tx_seqbufc >= (sizeof(hc->tx_seqbuf) /
  76. sizeof(struct ccid2_seq *)))
  77. return -ENOMEM;
  78. /* allocate buffer and initialize linked list */
  79. seqp = kmalloc(CCID2_SEQBUF_LEN * sizeof(struct ccid2_seq), gfp_any());
  80. if (seqp == NULL)
  81. return -ENOMEM;
  82. for (i = 0; i < (CCID2_SEQBUF_LEN - 1); i++) {
  83. seqp[i].ccid2s_next = &seqp[i + 1];
  84. seqp[i + 1].ccid2s_prev = &seqp[i];
  85. }
  86. seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = seqp;
  87. seqp->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1];
  88. /* This is the first allocation. Initiate the head and tail. */
  89. if (hc->tx_seqbufc == 0)
  90. hc->tx_seqh = hc->tx_seqt = seqp;
  91. else {
  92. /* link the existing list with the one we just created */
  93. hc->tx_seqh->ccid2s_next = seqp;
  94. seqp->ccid2s_prev = hc->tx_seqh;
  95. hc->tx_seqt->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1];
  96. seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = hc->tx_seqt;
  97. }
  98. /* store the original pointer to the buffer so we can free it */
  99. hc->tx_seqbuf[hc->tx_seqbufc] = seqp;
  100. hc->tx_seqbufc++;
  101. return 0;
  102. }
  103. static int ccid2_hc_tx_send_packet(struct sock *sk, struct sk_buff *skb)
  104. {
  105. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  106. if (hc->tx_pipe < hc->tx_cwnd)
  107. return 0;
  108. return 1; /* XXX CCID should dequeue when ready instead of polling */
  109. }
  110. static void ccid2_change_l_ack_ratio(struct sock *sk, u32 val)
  111. {
  112. struct dccp_sock *dp = dccp_sk(sk);
  113. u32 max_ratio = DIV_ROUND_UP(ccid2_hc_tx_sk(sk)->tx_cwnd, 2);
  114. /*
  115. * Ensure that Ack Ratio does not exceed ceil(cwnd/2), which is (2) from
  116. * RFC 4341, 6.1.2. We ignore the statement that Ack Ratio 2 is always
  117. * acceptable since this causes starvation/deadlock whenever cwnd < 2.
  118. * The same problem arises when Ack Ratio is 0 (ie. Ack Ratio disabled).
  119. */
  120. if (val == 0 || val > max_ratio) {
  121. DCCP_WARN("Limiting Ack Ratio (%u) to %u\n", val, max_ratio);
  122. val = max_ratio;
  123. }
  124. if (val > DCCPF_ACK_RATIO_MAX)
  125. val = DCCPF_ACK_RATIO_MAX;
  126. if (val == dp->dccps_l_ack_ratio)
  127. return;
  128. ccid2_pr_debug("changing local ack ratio to %u\n", val);
  129. dp->dccps_l_ack_ratio = val;
  130. }
  131. static void ccid2_change_srtt(struct ccid2_hc_tx_sock *hc, long val)
  132. {
  133. ccid2_pr_debug("change SRTT to %ld\n", val);
  134. hc->tx_srtt = val;
  135. }
  136. static void ccid2_start_rto_timer(struct sock *sk);
  137. static void ccid2_hc_tx_rto_expire(unsigned long data)
  138. {
  139. struct sock *sk = (struct sock *)data;
  140. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  141. long s;
  142. bh_lock_sock(sk);
  143. if (sock_owned_by_user(sk)) {
  144. sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + HZ / 5);
  145. goto out;
  146. }
  147. ccid2_pr_debug("RTO_EXPIRE\n");
  148. ccid2_hc_tx_check_sanity(hc);
  149. /* back-off timer */
  150. hc->tx_rto <<= 1;
  151. s = hc->tx_rto / HZ;
  152. if (s > 60)
  153. hc->tx_rto = 60 * HZ;
  154. ccid2_start_rto_timer(sk);
  155. /* adjust pipe, cwnd etc */
  156. hc->tx_ssthresh = hc->tx_cwnd / 2;
  157. if (hc->tx_ssthresh < 2)
  158. hc->tx_ssthresh = 2;
  159. hc->tx_cwnd = 1;
  160. hc->tx_pipe = 0;
  161. /* clear state about stuff we sent */
  162. hc->tx_seqt = hc->tx_seqh;
  163. hc->tx_packets_acked = 0;
  164. /* clear ack ratio state. */
  165. hc->tx_rpseq = 0;
  166. hc->tx_rpdupack = -1;
  167. ccid2_change_l_ack_ratio(sk, 1);
  168. ccid2_hc_tx_check_sanity(hc);
  169. out:
  170. bh_unlock_sock(sk);
  171. sock_put(sk);
  172. }
  173. static void ccid2_start_rto_timer(struct sock *sk)
  174. {
  175. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  176. ccid2_pr_debug("setting RTO timeout=%ld\n", hc->tx_rto);
  177. BUG_ON(timer_pending(&hc->tx_rtotimer));
  178. sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto);
  179. }
  180. static void ccid2_hc_tx_packet_sent(struct sock *sk, int more, unsigned int len)
  181. {
  182. struct dccp_sock *dp = dccp_sk(sk);
  183. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  184. struct ccid2_seq *next;
  185. hc->tx_pipe++;
  186. hc->tx_seqh->ccid2s_seq = dp->dccps_gss;
  187. hc->tx_seqh->ccid2s_acked = 0;
  188. hc->tx_seqh->ccid2s_sent = jiffies;
  189. next = hc->tx_seqh->ccid2s_next;
  190. /* check if we need to alloc more space */
  191. if (next == hc->tx_seqt) {
  192. if (ccid2_hc_tx_alloc_seq(hc)) {
  193. DCCP_CRIT("packet history - out of memory!");
  194. /* FIXME: find a more graceful way to bail out */
  195. return;
  196. }
  197. next = hc->tx_seqh->ccid2s_next;
  198. BUG_ON(next == hc->tx_seqt);
  199. }
  200. hc->tx_seqh = next;
  201. ccid2_pr_debug("cwnd=%d pipe=%d\n", hc->tx_cwnd, hc->tx_pipe);
  202. /*
  203. * FIXME: The code below is broken and the variables have been removed
  204. * from the socket struct. The `ackloss' variable was always set to 0,
  205. * and with arsent there are several problems:
  206. * (i) it doesn't just count the number of Acks, but all sent packets;
  207. * (ii) it is expressed in # of packets, not # of windows, so the
  208. * comparison below uses the wrong formula: Appendix A of RFC 4341
  209. * comes up with the number K = cwnd / (R^2 - R) of consecutive windows
  210. * of data with no lost or marked Ack packets. If arsent were the # of
  211. * consecutive Acks received without loss, then Ack Ratio needs to be
  212. * decreased by 1 when
  213. * arsent >= K * cwnd / R = cwnd^2 / (R^3 - R^2)
  214. * where cwnd / R is the number of Acks received per window of data
  215. * (cf. RFC 4341, App. A). The problems are that
  216. * - arsent counts other packets as well;
  217. * - the comparison uses a formula different from RFC 4341;
  218. * - computing a cubic/quadratic equation each time is too complicated.
  219. * Hence a different algorithm is needed.
  220. */
  221. #if 0
  222. /* Ack Ratio. Need to maintain a concept of how many windows we sent */
  223. hc->tx_arsent++;
  224. /* We had an ack loss in this window... */
  225. if (hc->tx_ackloss) {
  226. if (hc->tx_arsent >= hc->tx_cwnd) {
  227. hc->tx_arsent = 0;
  228. hc->tx_ackloss = 0;
  229. }
  230. } else {
  231. /* No acks lost up to now... */
  232. /* decrease ack ratio if enough packets were sent */
  233. if (dp->dccps_l_ack_ratio > 1) {
  234. /* XXX don't calculate denominator each time */
  235. int denom = dp->dccps_l_ack_ratio * dp->dccps_l_ack_ratio -
  236. dp->dccps_l_ack_ratio;
  237. denom = hc->tx_cwnd * hc->tx_cwnd / denom;
  238. if (hc->tx_arsent >= denom) {
  239. ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio - 1);
  240. hc->tx_arsent = 0;
  241. }
  242. } else {
  243. /* we can't increase ack ratio further [1] */
  244. hc->tx_arsent = 0; /* or maybe set it to cwnd*/
  245. }
  246. }
  247. #endif
  248. /* setup RTO timer */
  249. if (!timer_pending(&hc->tx_rtotimer))
  250. ccid2_start_rto_timer(sk);
  251. #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
  252. do {
  253. struct ccid2_seq *seqp = hc->tx_seqt;
  254. while (seqp != hc->tx_seqh) {
  255. ccid2_pr_debug("out seq=%llu acked=%d time=%lu\n",
  256. (unsigned long long)seqp->ccid2s_seq,
  257. seqp->ccid2s_acked, seqp->ccid2s_sent);
  258. seqp = seqp->ccid2s_next;
  259. }
  260. } while (0);
  261. ccid2_pr_debug("=========\n");
  262. ccid2_hc_tx_check_sanity(hc);
  263. #endif
  264. }
  265. /* XXX Lame code duplication!
  266. * returns -1 if none was found.
  267. * else returns the next offset to use in the function call.
  268. */
  269. static int ccid2_ackvector(struct sock *sk, struct sk_buff *skb, int offset,
  270. unsigned char **vec, unsigned char *veclen)
  271. {
  272. const struct dccp_hdr *dh = dccp_hdr(skb);
  273. unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb);
  274. unsigned char *opt_ptr;
  275. const unsigned char *opt_end = (unsigned char *)dh +
  276. (dh->dccph_doff * 4);
  277. unsigned char opt, len;
  278. unsigned char *value;
  279. BUG_ON(offset < 0);
  280. options += offset;
  281. opt_ptr = options;
  282. if (opt_ptr >= opt_end)
  283. return -1;
  284. while (opt_ptr != opt_end) {
  285. opt = *opt_ptr++;
  286. len = 0;
  287. value = NULL;
  288. /* Check if this isn't a single byte option */
  289. if (opt > DCCPO_MAX_RESERVED) {
  290. if (opt_ptr == opt_end)
  291. goto out_invalid_option;
  292. len = *opt_ptr++;
  293. if (len < 3)
  294. goto out_invalid_option;
  295. /*
  296. * Remove the type and len fields, leaving
  297. * just the value size
  298. */
  299. len -= 2;
  300. value = opt_ptr;
  301. opt_ptr += len;
  302. if (opt_ptr > opt_end)
  303. goto out_invalid_option;
  304. }
  305. switch (opt) {
  306. case DCCPO_ACK_VECTOR_0:
  307. case DCCPO_ACK_VECTOR_1:
  308. *vec = value;
  309. *veclen = len;
  310. return offset + (opt_ptr - options);
  311. }
  312. }
  313. return -1;
  314. out_invalid_option:
  315. DCCP_BUG("Invalid option - this should not happen (previous parsing)!");
  316. return -1;
  317. }
  318. static void ccid2_hc_tx_kill_rto_timer(struct sock *sk)
  319. {
  320. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  321. sk_stop_timer(sk, &hc->tx_rtotimer);
  322. ccid2_pr_debug("deleted RTO timer\n");
  323. }
  324. static inline void ccid2_new_ack(struct sock *sk,
  325. struct ccid2_seq *seqp,
  326. unsigned int *maxincr)
  327. {
  328. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  329. if (hc->tx_cwnd < hc->tx_ssthresh) {
  330. if (*maxincr > 0 && ++hc->tx_packets_acked == 2) {
  331. hc->tx_cwnd += 1;
  332. *maxincr -= 1;
  333. hc->tx_packets_acked = 0;
  334. }
  335. } else if (++hc->tx_packets_acked >= hc->tx_cwnd) {
  336. hc->tx_cwnd += 1;
  337. hc->tx_packets_acked = 0;
  338. }
  339. /* update RTO */
  340. if (hc->tx_srtt == -1 ||
  341. time_after(jiffies, hc->tx_lastrtt + hc->tx_srtt)) {
  342. unsigned long r = (long)jiffies - (long)seqp->ccid2s_sent;
  343. int s;
  344. /* first measurement */
  345. if (hc->tx_srtt == -1) {
  346. ccid2_pr_debug("R: %lu Time=%lu seq=%llu\n",
  347. r, jiffies,
  348. (unsigned long long)seqp->ccid2s_seq);
  349. ccid2_change_srtt(hc, r);
  350. hc->tx_rttvar = r >> 1;
  351. } else {
  352. /* RTTVAR */
  353. long tmp = hc->tx_srtt - r;
  354. long srtt;
  355. if (tmp < 0)
  356. tmp *= -1;
  357. tmp >>= 2;
  358. hc->tx_rttvar *= 3;
  359. hc->tx_rttvar >>= 2;
  360. hc->tx_rttvar += tmp;
  361. /* SRTT */
  362. srtt = hc->tx_srtt;
  363. srtt *= 7;
  364. srtt >>= 3;
  365. tmp = r >> 3;
  366. srtt += tmp;
  367. ccid2_change_srtt(hc, srtt);
  368. }
  369. s = hc->tx_rttvar << 2;
  370. /* clock granularity is 1 when based on jiffies */
  371. if (!s)
  372. s = 1;
  373. hc->tx_rto = hc->tx_srtt + s;
  374. /* must be at least a second */
  375. s = hc->tx_rto / HZ;
  376. /* DCCP doesn't require this [but I like it cuz my code sux] */
  377. #if 1
  378. if (s < 1)
  379. hc->tx_rto = HZ;
  380. #endif
  381. /* max 60 seconds */
  382. if (s > 60)
  383. hc->tx_rto = HZ * 60;
  384. hc->tx_lastrtt = jiffies;
  385. ccid2_pr_debug("srtt: %ld rttvar: %ld rto: %ld (HZ=%d) R=%lu\n",
  386. hc->tx_srtt, hc->tx_rttvar,
  387. hc->tx_rto, HZ, r);
  388. }
  389. /* we got a new ack, so re-start RTO timer */
  390. ccid2_hc_tx_kill_rto_timer(sk);
  391. ccid2_start_rto_timer(sk);
  392. }
  393. static void ccid2_hc_tx_dec_pipe(struct sock *sk)
  394. {
  395. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  396. if (hc->tx_pipe == 0)
  397. DCCP_BUG("pipe == 0");
  398. else
  399. hc->tx_pipe--;
  400. if (hc->tx_pipe == 0)
  401. ccid2_hc_tx_kill_rto_timer(sk);
  402. }
  403. static void ccid2_congestion_event(struct sock *sk, struct ccid2_seq *seqp)
  404. {
  405. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  406. if (time_before(seqp->ccid2s_sent, hc->tx_last_cong)) {
  407. ccid2_pr_debug("Multiple losses in an RTT---treating as one\n");
  408. return;
  409. }
  410. hc->tx_last_cong = jiffies;
  411. hc->tx_cwnd = hc->tx_cwnd / 2 ? : 1U;
  412. hc->tx_ssthresh = max(hc->tx_cwnd, 2U);
  413. /* Avoid spurious timeouts resulting from Ack Ratio > cwnd */
  414. if (dccp_sk(sk)->dccps_l_ack_ratio > hc->tx_cwnd)
  415. ccid2_change_l_ack_ratio(sk, hc->tx_cwnd);
  416. }
  417. static void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
  418. {
  419. struct dccp_sock *dp = dccp_sk(sk);
  420. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  421. u64 ackno, seqno;
  422. struct ccid2_seq *seqp;
  423. unsigned char *vector;
  424. unsigned char veclen;
  425. int offset = 0;
  426. int done = 0;
  427. unsigned int maxincr = 0;
  428. ccid2_hc_tx_check_sanity(hc);
  429. /* check reverse path congestion */
  430. seqno = DCCP_SKB_CB(skb)->dccpd_seq;
  431. /* XXX this whole "algorithm" is broken. Need to fix it to keep track
  432. * of the seqnos of the dupacks so that rpseq and rpdupack are correct
  433. * -sorbo.
  434. */
  435. /* need to bootstrap */
  436. if (hc->tx_rpdupack == -1) {
  437. hc->tx_rpdupack = 0;
  438. hc->tx_rpseq = seqno;
  439. } else {
  440. /* check if packet is consecutive */
  441. if (dccp_delta_seqno(hc->tx_rpseq, seqno) == 1)
  442. hc->tx_rpseq = seqno;
  443. /* it's a later packet */
  444. else if (after48(seqno, hc->tx_rpseq)) {
  445. hc->tx_rpdupack++;
  446. /* check if we got enough dupacks */
  447. if (hc->tx_rpdupack >= NUMDUPACK) {
  448. hc->tx_rpdupack = -1; /* XXX lame */
  449. hc->tx_rpseq = 0;
  450. ccid2_change_l_ack_ratio(sk, 2 * dp->dccps_l_ack_ratio);
  451. }
  452. }
  453. }
  454. /* check forward path congestion */
  455. /* still didn't send out new data packets */
  456. if (hc->tx_seqh == hc->tx_seqt)
  457. return;
  458. switch (DCCP_SKB_CB(skb)->dccpd_type) {
  459. case DCCP_PKT_ACK:
  460. case DCCP_PKT_DATAACK:
  461. break;
  462. default:
  463. return;
  464. }
  465. ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
  466. if (after48(ackno, hc->tx_high_ack))
  467. hc->tx_high_ack = ackno;
  468. seqp = hc->tx_seqt;
  469. while (before48(seqp->ccid2s_seq, ackno)) {
  470. seqp = seqp->ccid2s_next;
  471. if (seqp == hc->tx_seqh) {
  472. seqp = hc->tx_seqh->ccid2s_prev;
  473. break;
  474. }
  475. }
  476. /*
  477. * In slow-start, cwnd can increase up to a maximum of Ack Ratio/2
  478. * packets per acknowledgement. Rounding up avoids that cwnd is not
  479. * advanced when Ack Ratio is 1 and gives a slight edge otherwise.
  480. */
  481. if (hc->tx_cwnd < hc->tx_ssthresh)
  482. maxincr = DIV_ROUND_UP(dp->dccps_l_ack_ratio, 2);
  483. /* go through all ack vectors */
  484. while ((offset = ccid2_ackvector(sk, skb, offset,
  485. &vector, &veclen)) != -1) {
  486. /* go through this ack vector */
  487. while (veclen--) {
  488. const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
  489. u64 ackno_end_rl = SUB48(ackno, rl);
  490. ccid2_pr_debug("ackvec start:%llu end:%llu\n",
  491. (unsigned long long)ackno,
  492. (unsigned long long)ackno_end_rl);
  493. /* if the seqno we are analyzing is larger than the
  494. * current ackno, then move towards the tail of our
  495. * seqnos.
  496. */
  497. while (after48(seqp->ccid2s_seq, ackno)) {
  498. if (seqp == hc->tx_seqt) {
  499. done = 1;
  500. break;
  501. }
  502. seqp = seqp->ccid2s_prev;
  503. }
  504. if (done)
  505. break;
  506. /* check all seqnos in the range of the vector
  507. * run length
  508. */
  509. while (between48(seqp->ccid2s_seq,ackno_end_rl,ackno)) {
  510. const u8 state = *vector &
  511. DCCP_ACKVEC_STATE_MASK;
  512. /* new packet received or marked */
  513. if (state != DCCP_ACKVEC_STATE_NOT_RECEIVED &&
  514. !seqp->ccid2s_acked) {
  515. if (state ==
  516. DCCP_ACKVEC_STATE_ECN_MARKED) {
  517. ccid2_congestion_event(sk,
  518. seqp);
  519. } else
  520. ccid2_new_ack(sk, seqp,
  521. &maxincr);
  522. seqp->ccid2s_acked = 1;
  523. ccid2_pr_debug("Got ack for %llu\n",
  524. (unsigned long long)seqp->ccid2s_seq);
  525. ccid2_hc_tx_dec_pipe(sk);
  526. }
  527. if (seqp == hc->tx_seqt) {
  528. done = 1;
  529. break;
  530. }
  531. seqp = seqp->ccid2s_prev;
  532. }
  533. if (done)
  534. break;
  535. ackno = SUB48(ackno_end_rl, 1);
  536. vector++;
  537. }
  538. if (done)
  539. break;
  540. }
  541. /* The state about what is acked should be correct now
  542. * Check for NUMDUPACK
  543. */
  544. seqp = hc->tx_seqt;
  545. while (before48(seqp->ccid2s_seq, hc->tx_high_ack)) {
  546. seqp = seqp->ccid2s_next;
  547. if (seqp == hc->tx_seqh) {
  548. seqp = hc->tx_seqh->ccid2s_prev;
  549. break;
  550. }
  551. }
  552. done = 0;
  553. while (1) {
  554. if (seqp->ccid2s_acked) {
  555. done++;
  556. if (done == NUMDUPACK)
  557. break;
  558. }
  559. if (seqp == hc->tx_seqt)
  560. break;
  561. seqp = seqp->ccid2s_prev;
  562. }
  563. /* If there are at least 3 acknowledgements, anything unacknowledged
  564. * below the last sequence number is considered lost
  565. */
  566. if (done == NUMDUPACK) {
  567. struct ccid2_seq *last_acked = seqp;
  568. /* check for lost packets */
  569. while (1) {
  570. if (!seqp->ccid2s_acked) {
  571. ccid2_pr_debug("Packet lost: %llu\n",
  572. (unsigned long long)seqp->ccid2s_seq);
  573. /* XXX need to traverse from tail -> head in
  574. * order to detect multiple congestion events in
  575. * one ack vector.
  576. */
  577. ccid2_congestion_event(sk, seqp);
  578. ccid2_hc_tx_dec_pipe(sk);
  579. }
  580. if (seqp == hc->tx_seqt)
  581. break;
  582. seqp = seqp->ccid2s_prev;
  583. }
  584. hc->tx_seqt = last_acked;
  585. }
  586. /* trim acked packets in tail */
  587. while (hc->tx_seqt != hc->tx_seqh) {
  588. if (!hc->tx_seqt->ccid2s_acked)
  589. break;
  590. hc->tx_seqt = hc->tx_seqt->ccid2s_next;
  591. }
  592. ccid2_hc_tx_check_sanity(hc);
  593. }
  594. static int ccid2_hc_tx_init(struct ccid *ccid, struct sock *sk)
  595. {
  596. struct ccid2_hc_tx_sock *hc = ccid_priv(ccid);
  597. struct dccp_sock *dp = dccp_sk(sk);
  598. u32 max_ratio;
  599. /* RFC 4341, 5: initialise ssthresh to arbitrarily high (max) value */
  600. hc->tx_ssthresh = ~0U;
  601. /*
  602. * RFC 4341, 5: "The cwnd parameter is initialized to at most four
  603. * packets for new connections, following the rules from [RFC3390]".
  604. * We need to convert the bytes of RFC3390 into the packets of RFC 4341.
  605. */
  606. hc->tx_cwnd = clamp(4380U / dp->dccps_mss_cache, 2U, 4U);
  607. /* Make sure that Ack Ratio is enabled and within bounds. */
  608. max_ratio = DIV_ROUND_UP(hc->tx_cwnd, 2);
  609. if (dp->dccps_l_ack_ratio == 0 || dp->dccps_l_ack_ratio > max_ratio)
  610. dp->dccps_l_ack_ratio = max_ratio;
  611. /* XXX init ~ to window size... */
  612. if (ccid2_hc_tx_alloc_seq(hc))
  613. return -ENOMEM;
  614. hc->tx_rto = 3 * HZ;
  615. ccid2_change_srtt(hc, -1);
  616. hc->tx_rttvar = -1;
  617. hc->tx_rpdupack = -1;
  618. hc->tx_last_cong = jiffies;
  619. setup_timer(&hc->tx_rtotimer, ccid2_hc_tx_rto_expire,
  620. (unsigned long)sk);
  621. ccid2_hc_tx_check_sanity(hc);
  622. return 0;
  623. }
  624. static void ccid2_hc_tx_exit(struct sock *sk)
  625. {
  626. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  627. int i;
  628. ccid2_hc_tx_kill_rto_timer(sk);
  629. for (i = 0; i < hc->tx_seqbufc; i++)
  630. kfree(hc->tx_seqbuf[i]);
  631. hc->tx_seqbufc = 0;
  632. }
  633. static void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
  634. {
  635. const struct dccp_sock *dp = dccp_sk(sk);
  636. struct ccid2_hc_rx_sock *hc = ccid2_hc_rx_sk(sk);
  637. switch (DCCP_SKB_CB(skb)->dccpd_type) {
  638. case DCCP_PKT_DATA:
  639. case DCCP_PKT_DATAACK:
  640. hc->rx_data++;
  641. if (hc->rx_data >= dp->dccps_r_ack_ratio) {
  642. dccp_send_ack(sk);
  643. hc->rx_data = 0;
  644. }
  645. break;
  646. }
  647. }
  648. struct ccid_operations ccid2_ops = {
  649. .ccid_id = DCCPC_CCID2,
  650. .ccid_name = "TCP-like",
  651. .ccid_hc_tx_obj_size = sizeof(struct ccid2_hc_tx_sock),
  652. .ccid_hc_tx_init = ccid2_hc_tx_init,
  653. .ccid_hc_tx_exit = ccid2_hc_tx_exit,
  654. .ccid_hc_tx_send_packet = ccid2_hc_tx_send_packet,
  655. .ccid_hc_tx_packet_sent = ccid2_hc_tx_packet_sent,
  656. .ccid_hc_tx_packet_recv = ccid2_hc_tx_packet_recv,
  657. .ccid_hc_rx_obj_size = sizeof(struct ccid2_hc_rx_sock),
  658. .ccid_hc_rx_packet_recv = ccid2_hc_rx_packet_recv,
  659. };
  660. #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
  661. module_param(ccid2_debug, bool, 0644);
  662. MODULE_PARM_DESC(ccid2_debug, "Enable CCID-2 debug messages");
  663. #endif