ccid2.c 21 KB

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