ccid2.c 20 KB

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