ccid2.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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 bool 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. u32 max_ratio = DIV_ROUND_UP(ccid2_hc_tx_sk(sk)->tx_cwnd, 2);
  76. /*
  77. * Ensure that Ack Ratio does not exceed ceil(cwnd/2), which is (2) from
  78. * RFC 4341, 6.1.2. We ignore the statement that Ack Ratio 2 is always
  79. * acceptable since this causes starvation/deadlock whenever cwnd < 2.
  80. * The same problem arises when Ack Ratio is 0 (ie. Ack Ratio disabled).
  81. */
  82. if (val == 0 || val > max_ratio) {
  83. DCCP_WARN("Limiting Ack Ratio (%u) to %u\n", val, max_ratio);
  84. val = max_ratio;
  85. }
  86. dccp_feat_signal_nn_change(sk, DCCPF_ACK_RATIO,
  87. min_t(u32, val, DCCPF_ACK_RATIO_MAX));
  88. }
  89. static void ccid2_check_l_ack_ratio(struct sock *sk)
  90. {
  91. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  92. /*
  93. * After a loss, idle period, application limited period, or RTO we
  94. * need to check that the ack ratio is still less than the congestion
  95. * window. Otherwise, we will send an entire congestion window of
  96. * packets and got no response because we haven't sent ack ratio
  97. * packets yet.
  98. * If the ack ratio does need to be reduced, we reduce it to half of
  99. * the congestion window (or 1 if that's zero) instead of to the
  100. * congestion window. This prevents problems if one ack is lost.
  101. */
  102. if (dccp_feat_nn_get(sk, DCCPF_ACK_RATIO) > hc->tx_cwnd)
  103. ccid2_change_l_ack_ratio(sk, hc->tx_cwnd/2 ? : 1U);
  104. }
  105. static void ccid2_change_l_seq_window(struct sock *sk, u64 val)
  106. {
  107. dccp_feat_signal_nn_change(sk, DCCPF_SEQUENCE_WINDOW,
  108. clamp_val(val, DCCPF_SEQ_WMIN,
  109. DCCPF_SEQ_WMAX));
  110. }
  111. static void ccid2_hc_tx_rto_expire(struct timer_list *t)
  112. {
  113. struct ccid2_hc_tx_sock *hc = from_timer(hc, t, tx_rtotimer);
  114. struct sock *sk = hc->sk;
  115. const bool sender_was_blocked = ccid2_cwnd_network_limited(hc);
  116. bh_lock_sock(sk);
  117. if (sock_owned_by_user(sk)) {
  118. sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + HZ / 5);
  119. goto out;
  120. }
  121. ccid2_pr_debug("RTO_EXPIRE\n");
  122. if (sk->sk_state == DCCP_CLOSED)
  123. goto out;
  124. /* back-off timer */
  125. hc->tx_rto <<= 1;
  126. if (hc->tx_rto > DCCP_RTO_MAX)
  127. hc->tx_rto = DCCP_RTO_MAX;
  128. /* adjust pipe, cwnd etc */
  129. hc->tx_ssthresh = hc->tx_cwnd / 2;
  130. if (hc->tx_ssthresh < 2)
  131. hc->tx_ssthresh = 2;
  132. hc->tx_cwnd = 1;
  133. hc->tx_pipe = 0;
  134. /* clear state about stuff we sent */
  135. hc->tx_seqt = hc->tx_seqh;
  136. hc->tx_packets_acked = 0;
  137. /* clear ack ratio state. */
  138. hc->tx_rpseq = 0;
  139. hc->tx_rpdupack = -1;
  140. ccid2_change_l_ack_ratio(sk, 1);
  141. /* if we were blocked before, we may now send cwnd=1 packet */
  142. if (sender_was_blocked)
  143. tasklet_schedule(&dccp_sk(sk)->dccps_xmitlet);
  144. /* restart backed-off timer */
  145. sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto);
  146. out:
  147. bh_unlock_sock(sk);
  148. sock_put(sk);
  149. }
  150. /*
  151. * Congestion window validation (RFC 2861).
  152. */
  153. static bool ccid2_do_cwv = true;
  154. module_param(ccid2_do_cwv, bool, 0644);
  155. MODULE_PARM_DESC(ccid2_do_cwv, "Perform RFC2861 Congestion Window Validation");
  156. /**
  157. * ccid2_update_used_window - Track how much of cwnd is actually used
  158. * This is done in addition to CWV. The sender needs to have an idea of how many
  159. * packets may be in flight, to set the local Sequence Window value accordingly
  160. * (RFC 4340, 7.5.2). The CWV mechanism is exploited to keep track of the
  161. * maximum-used window. We use an EWMA low-pass filter to filter out noise.
  162. */
  163. static void ccid2_update_used_window(struct ccid2_hc_tx_sock *hc, u32 new_wnd)
  164. {
  165. hc->tx_expected_wnd = (3 * hc->tx_expected_wnd + new_wnd) / 4;
  166. }
  167. /* This borrows the code of tcp_cwnd_application_limited() */
  168. static void ccid2_cwnd_application_limited(struct sock *sk, const u32 now)
  169. {
  170. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  171. /* don't reduce cwnd below the initial window (IW) */
  172. u32 init_win = rfc3390_bytes_to_packets(dccp_sk(sk)->dccps_mss_cache),
  173. win_used = max(hc->tx_cwnd_used, init_win);
  174. if (win_used < hc->tx_cwnd) {
  175. hc->tx_ssthresh = max(hc->tx_ssthresh,
  176. (hc->tx_cwnd >> 1) + (hc->tx_cwnd >> 2));
  177. hc->tx_cwnd = (hc->tx_cwnd + win_used) >> 1;
  178. }
  179. hc->tx_cwnd_used = 0;
  180. hc->tx_cwnd_stamp = now;
  181. ccid2_check_l_ack_ratio(sk);
  182. }
  183. /* This borrows the code of tcp_cwnd_restart() */
  184. static void ccid2_cwnd_restart(struct sock *sk, const u32 now)
  185. {
  186. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  187. u32 cwnd = hc->tx_cwnd, restart_cwnd,
  188. iwnd = rfc3390_bytes_to_packets(dccp_sk(sk)->dccps_mss_cache);
  189. hc->tx_ssthresh = max(hc->tx_ssthresh, (cwnd >> 1) + (cwnd >> 2));
  190. /* don't reduce cwnd below the initial window (IW) */
  191. restart_cwnd = min(cwnd, iwnd);
  192. cwnd >>= (now - hc->tx_lsndtime) / hc->tx_rto;
  193. hc->tx_cwnd = max(cwnd, restart_cwnd);
  194. hc->tx_cwnd_stamp = now;
  195. hc->tx_cwnd_used = 0;
  196. ccid2_check_l_ack_ratio(sk);
  197. }
  198. static void ccid2_hc_tx_packet_sent(struct sock *sk, unsigned int len)
  199. {
  200. struct dccp_sock *dp = dccp_sk(sk);
  201. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  202. const u32 now = ccid2_jiffies32;
  203. struct ccid2_seq *next;
  204. /* slow-start after idle periods (RFC 2581, RFC 2861) */
  205. if (ccid2_do_cwv && !hc->tx_pipe &&
  206. (s32)(now - hc->tx_lsndtime) >= hc->tx_rto)
  207. ccid2_cwnd_restart(sk, now);
  208. hc->tx_lsndtime = now;
  209. hc->tx_pipe += 1;
  210. /* see whether cwnd was fully used (RFC 2861), update expected window */
  211. if (ccid2_cwnd_network_limited(hc)) {
  212. ccid2_update_used_window(hc, hc->tx_cwnd);
  213. hc->tx_cwnd_used = 0;
  214. hc->tx_cwnd_stamp = now;
  215. } else {
  216. if (hc->tx_pipe > hc->tx_cwnd_used)
  217. hc->tx_cwnd_used = hc->tx_pipe;
  218. ccid2_update_used_window(hc, hc->tx_cwnd_used);
  219. if (ccid2_do_cwv && (s32)(now - hc->tx_cwnd_stamp) >= hc->tx_rto)
  220. ccid2_cwnd_application_limited(sk, now);
  221. }
  222. hc->tx_seqh->ccid2s_seq = dp->dccps_gss;
  223. hc->tx_seqh->ccid2s_acked = 0;
  224. hc->tx_seqh->ccid2s_sent = now;
  225. next = hc->tx_seqh->ccid2s_next;
  226. /* check if we need to alloc more space */
  227. if (next == hc->tx_seqt) {
  228. if (ccid2_hc_tx_alloc_seq(hc)) {
  229. DCCP_CRIT("packet history - out of memory!");
  230. /* FIXME: find a more graceful way to bail out */
  231. return;
  232. }
  233. next = hc->tx_seqh->ccid2s_next;
  234. BUG_ON(next == hc->tx_seqt);
  235. }
  236. hc->tx_seqh = next;
  237. ccid2_pr_debug("cwnd=%d pipe=%d\n", hc->tx_cwnd, hc->tx_pipe);
  238. /*
  239. * FIXME: The code below is broken and the variables have been removed
  240. * from the socket struct. The `ackloss' variable was always set to 0,
  241. * and with arsent there are several problems:
  242. * (i) it doesn't just count the number of Acks, but all sent packets;
  243. * (ii) it is expressed in # of packets, not # of windows, so the
  244. * comparison below uses the wrong formula: Appendix A of RFC 4341
  245. * comes up with the number K = cwnd / (R^2 - R) of consecutive windows
  246. * of data with no lost or marked Ack packets. If arsent were the # of
  247. * consecutive Acks received without loss, then Ack Ratio needs to be
  248. * decreased by 1 when
  249. * arsent >= K * cwnd / R = cwnd^2 / (R^3 - R^2)
  250. * where cwnd / R is the number of Acks received per window of data
  251. * (cf. RFC 4341, App. A). The problems are that
  252. * - arsent counts other packets as well;
  253. * - the comparison uses a formula different from RFC 4341;
  254. * - computing a cubic/quadratic equation each time is too complicated.
  255. * Hence a different algorithm is needed.
  256. */
  257. #if 0
  258. /* Ack Ratio. Need to maintain a concept of how many windows we sent */
  259. hc->tx_arsent++;
  260. /* We had an ack loss in this window... */
  261. if (hc->tx_ackloss) {
  262. if (hc->tx_arsent >= hc->tx_cwnd) {
  263. hc->tx_arsent = 0;
  264. hc->tx_ackloss = 0;
  265. }
  266. } else {
  267. /* No acks lost up to now... */
  268. /* decrease ack ratio if enough packets were sent */
  269. if (dp->dccps_l_ack_ratio > 1) {
  270. /* XXX don't calculate denominator each time */
  271. int denom = dp->dccps_l_ack_ratio * dp->dccps_l_ack_ratio -
  272. dp->dccps_l_ack_ratio;
  273. denom = hc->tx_cwnd * hc->tx_cwnd / denom;
  274. if (hc->tx_arsent >= denom) {
  275. ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio - 1);
  276. hc->tx_arsent = 0;
  277. }
  278. } else {
  279. /* we can't increase ack ratio further [1] */
  280. hc->tx_arsent = 0; /* or maybe set it to cwnd*/
  281. }
  282. }
  283. #endif
  284. sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto);
  285. #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
  286. do {
  287. struct ccid2_seq *seqp = hc->tx_seqt;
  288. while (seqp != hc->tx_seqh) {
  289. ccid2_pr_debug("out seq=%llu acked=%d time=%u\n",
  290. (unsigned long long)seqp->ccid2s_seq,
  291. seqp->ccid2s_acked, seqp->ccid2s_sent);
  292. seqp = seqp->ccid2s_next;
  293. }
  294. } while (0);
  295. ccid2_pr_debug("=========\n");
  296. #endif
  297. }
  298. /**
  299. * ccid2_rtt_estimator - Sample RTT and compute RTO using RFC2988 algorithm
  300. * This code is almost identical with TCP's tcp_rtt_estimator(), since
  301. * - it has a higher sampling frequency (recommended by RFC 1323),
  302. * - the RTO does not collapse into RTT due to RTTVAR going towards zero,
  303. * - it is simple (cf. more complex proposals such as Eifel timer or research
  304. * which suggests that the gain should be set according to window size),
  305. * - in tests it was found to work well with CCID2 [gerrit].
  306. */
  307. static void ccid2_rtt_estimator(struct sock *sk, const long mrtt)
  308. {
  309. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  310. long m = mrtt ? : 1;
  311. if (hc->tx_srtt == 0) {
  312. /* First measurement m */
  313. hc->tx_srtt = m << 3;
  314. hc->tx_mdev = m << 1;
  315. hc->tx_mdev_max = max(hc->tx_mdev, tcp_rto_min(sk));
  316. hc->tx_rttvar = hc->tx_mdev_max;
  317. hc->tx_rtt_seq = dccp_sk(sk)->dccps_gss;
  318. } else {
  319. /* Update scaled SRTT as SRTT += 1/8 * (m - SRTT) */
  320. m -= (hc->tx_srtt >> 3);
  321. hc->tx_srtt += m;
  322. /* Similarly, update scaled mdev with regard to |m| */
  323. if (m < 0) {
  324. m = -m;
  325. m -= (hc->tx_mdev >> 2);
  326. /*
  327. * This neutralises RTO increase when RTT < SRTT - mdev
  328. * (see P. Sarolahti, A. Kuznetsov,"Congestion Control
  329. * in Linux TCP", USENIX 2002, pp. 49-62).
  330. */
  331. if (m > 0)
  332. m >>= 3;
  333. } else {
  334. m -= (hc->tx_mdev >> 2);
  335. }
  336. hc->tx_mdev += m;
  337. if (hc->tx_mdev > hc->tx_mdev_max) {
  338. hc->tx_mdev_max = hc->tx_mdev;
  339. if (hc->tx_mdev_max > hc->tx_rttvar)
  340. hc->tx_rttvar = hc->tx_mdev_max;
  341. }
  342. /*
  343. * Decay RTTVAR at most once per flight, exploiting that
  344. * 1) pipe <= cwnd <= Sequence_Window = W (RFC 4340, 7.5.2)
  345. * 2) AWL = GSS-W+1 <= GAR <= GSS (RFC 4340, 7.5.1)
  346. * GAR is a useful bound for FlightSize = pipe.
  347. * AWL is probably too low here, as it over-estimates pipe.
  348. */
  349. if (after48(dccp_sk(sk)->dccps_gar, hc->tx_rtt_seq)) {
  350. if (hc->tx_mdev_max < hc->tx_rttvar)
  351. hc->tx_rttvar -= (hc->tx_rttvar -
  352. hc->tx_mdev_max) >> 2;
  353. hc->tx_rtt_seq = dccp_sk(sk)->dccps_gss;
  354. hc->tx_mdev_max = tcp_rto_min(sk);
  355. }
  356. }
  357. /*
  358. * Set RTO from SRTT and RTTVAR
  359. * As in TCP, 4 * RTTVAR >= TCP_RTO_MIN, giving a minimum RTO of 200 ms.
  360. * This agrees with RFC 4341, 5:
  361. * "Because DCCP does not retransmit data, DCCP does not require
  362. * TCP's recommended minimum timeout of one second".
  363. */
  364. hc->tx_rto = (hc->tx_srtt >> 3) + hc->tx_rttvar;
  365. if (hc->tx_rto > DCCP_RTO_MAX)
  366. hc->tx_rto = DCCP_RTO_MAX;
  367. }
  368. static void ccid2_new_ack(struct sock *sk, struct ccid2_seq *seqp,
  369. unsigned int *maxincr)
  370. {
  371. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  372. struct dccp_sock *dp = dccp_sk(sk);
  373. int r_seq_used = hc->tx_cwnd / dp->dccps_l_ack_ratio;
  374. if (hc->tx_cwnd < dp->dccps_l_seq_win &&
  375. r_seq_used < dp->dccps_r_seq_win) {
  376. if (hc->tx_cwnd < hc->tx_ssthresh) {
  377. if (*maxincr > 0 && ++hc->tx_packets_acked >= 2) {
  378. hc->tx_cwnd += 1;
  379. *maxincr -= 1;
  380. hc->tx_packets_acked = 0;
  381. }
  382. } else if (++hc->tx_packets_acked >= hc->tx_cwnd) {
  383. hc->tx_cwnd += 1;
  384. hc->tx_packets_acked = 0;
  385. }
  386. }
  387. /*
  388. * Adjust the local sequence window and the ack ratio to allow about
  389. * 5 times the number of packets in the network (RFC 4340 7.5.2)
  390. */
  391. if (r_seq_used * CCID2_WIN_CHANGE_FACTOR >= dp->dccps_r_seq_win)
  392. ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio * 2);
  393. else if (r_seq_used * CCID2_WIN_CHANGE_FACTOR < dp->dccps_r_seq_win/2)
  394. ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio / 2 ? : 1U);
  395. if (hc->tx_cwnd * CCID2_WIN_CHANGE_FACTOR >= dp->dccps_l_seq_win)
  396. ccid2_change_l_seq_window(sk, dp->dccps_l_seq_win * 2);
  397. else if (hc->tx_cwnd * CCID2_WIN_CHANGE_FACTOR < dp->dccps_l_seq_win/2)
  398. ccid2_change_l_seq_window(sk, dp->dccps_l_seq_win / 2);
  399. /*
  400. * FIXME: RTT is sampled several times per acknowledgment (for each
  401. * entry in the Ack Vector), instead of once per Ack (as in TCP SACK).
  402. * This causes the RTT to be over-estimated, since the older entries
  403. * in the Ack Vector have earlier sending times.
  404. * The cleanest solution is to not use the ccid2s_sent field at all
  405. * and instead use DCCP timestamps: requires changes in other places.
  406. */
  407. ccid2_rtt_estimator(sk, ccid2_jiffies32 - seqp->ccid2s_sent);
  408. }
  409. static void ccid2_congestion_event(struct sock *sk, struct ccid2_seq *seqp)
  410. {
  411. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  412. if ((s32)(seqp->ccid2s_sent - hc->tx_last_cong) < 0) {
  413. ccid2_pr_debug("Multiple losses in an RTT---treating as one\n");
  414. return;
  415. }
  416. hc->tx_last_cong = ccid2_jiffies32;
  417. hc->tx_cwnd = hc->tx_cwnd / 2 ? : 1U;
  418. hc->tx_ssthresh = max(hc->tx_cwnd, 2U);
  419. ccid2_check_l_ack_ratio(sk);
  420. }
  421. static int ccid2_hc_tx_parse_options(struct sock *sk, u8 packet_type,
  422. u8 option, u8 *optval, u8 optlen)
  423. {
  424. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  425. switch (option) {
  426. case DCCPO_ACK_VECTOR_0:
  427. case DCCPO_ACK_VECTOR_1:
  428. return dccp_ackvec_parsed_add(&hc->tx_av_chunks, optval, optlen,
  429. option - DCCPO_ACK_VECTOR_0);
  430. }
  431. return 0;
  432. }
  433. static void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
  434. {
  435. struct dccp_sock *dp = dccp_sk(sk);
  436. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  437. const bool sender_was_blocked = ccid2_cwnd_network_limited(hc);
  438. struct dccp_ackvec_parsed *avp;
  439. u64 ackno, seqno;
  440. struct ccid2_seq *seqp;
  441. int done = 0;
  442. unsigned int maxincr = 0;
  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 (hc->tx_rpdupack == -1) {
  451. hc->tx_rpdupack = 0;
  452. hc->tx_rpseq = seqno;
  453. } else {
  454. /* check if packet is consecutive */
  455. if (dccp_delta_seqno(hc->tx_rpseq, seqno) == 1)
  456. hc->tx_rpseq = seqno;
  457. /* it's a later packet */
  458. else if (after48(seqno, hc->tx_rpseq)) {
  459. hc->tx_rpdupack++;
  460. /* check if we got enough dupacks */
  461. if (hc->tx_rpdupack >= NUMDUPACK) {
  462. hc->tx_rpdupack = -1; /* XXX lame */
  463. hc->tx_rpseq = 0;
  464. #ifdef __CCID2_COPES_GRACEFULLY_WITH_ACK_CONGESTION_CONTROL__
  465. /*
  466. * FIXME: Ack Congestion Control is broken; in
  467. * the current state instabilities occurred with
  468. * Ack Ratios greater than 1; causing hang-ups
  469. * and long RTO timeouts. This needs to be fixed
  470. * before opening up dynamic changes. -- gerrit
  471. */
  472. ccid2_change_l_ack_ratio(sk, 2 * dp->dccps_l_ack_ratio);
  473. #endif
  474. }
  475. }
  476. }
  477. /* check forward path congestion */
  478. if (dccp_packet_without_ack(skb))
  479. return;
  480. /* still didn't send out new data packets */
  481. if (hc->tx_seqh == hc->tx_seqt)
  482. goto done;
  483. ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
  484. if (after48(ackno, hc->tx_high_ack))
  485. hc->tx_high_ack = ackno;
  486. seqp = hc->tx_seqt;
  487. while (before48(seqp->ccid2s_seq, ackno)) {
  488. seqp = seqp->ccid2s_next;
  489. if (seqp == hc->tx_seqh) {
  490. seqp = hc->tx_seqh->ccid2s_prev;
  491. break;
  492. }
  493. }
  494. /*
  495. * In slow-start, cwnd can increase up to a maximum of Ack Ratio/2
  496. * packets per acknowledgement. Rounding up avoids that cwnd is not
  497. * advanced when Ack Ratio is 1 and gives a slight edge otherwise.
  498. */
  499. if (hc->tx_cwnd < hc->tx_ssthresh)
  500. maxincr = DIV_ROUND_UP(dp->dccps_l_ack_ratio, 2);
  501. /* go through all ack vectors */
  502. list_for_each_entry(avp, &hc->tx_av_chunks, node) {
  503. /* go through this ack vector */
  504. for (; avp->len--; avp->vec++) {
  505. u64 ackno_end_rl = SUB48(ackno,
  506. dccp_ackvec_runlen(avp->vec));
  507. ccid2_pr_debug("ackvec %llu |%u,%u|\n",
  508. (unsigned long long)ackno,
  509. dccp_ackvec_state(avp->vec) >> 6,
  510. dccp_ackvec_runlen(avp->vec));
  511. /* if the seqno we are analyzing is larger than the
  512. * current ackno, then move towards the tail of our
  513. * seqnos.
  514. */
  515. while (after48(seqp->ccid2s_seq, ackno)) {
  516. if (seqp == hc->tx_seqt) {
  517. done = 1;
  518. break;
  519. }
  520. seqp = seqp->ccid2s_prev;
  521. }
  522. if (done)
  523. break;
  524. /* check all seqnos in the range of the vector
  525. * run length
  526. */
  527. while (between48(seqp->ccid2s_seq,ackno_end_rl,ackno)) {
  528. const u8 state = dccp_ackvec_state(avp->vec);
  529. /* new packet received or marked */
  530. if (state != DCCPAV_NOT_RECEIVED &&
  531. !seqp->ccid2s_acked) {
  532. if (state == DCCPAV_ECN_MARKED)
  533. ccid2_congestion_event(sk,
  534. seqp);
  535. else
  536. ccid2_new_ack(sk, seqp,
  537. &maxincr);
  538. seqp->ccid2s_acked = 1;
  539. ccid2_pr_debug("Got ack for %llu\n",
  540. (unsigned long long)seqp->ccid2s_seq);
  541. hc->tx_pipe--;
  542. }
  543. if (seqp == hc->tx_seqt) {
  544. done = 1;
  545. break;
  546. }
  547. seqp = seqp->ccid2s_prev;
  548. }
  549. if (done)
  550. break;
  551. ackno = SUB48(ackno_end_rl, 1);
  552. }
  553. if (done)
  554. break;
  555. }
  556. /* The state about what is acked should be correct now
  557. * Check for NUMDUPACK
  558. */
  559. seqp = hc->tx_seqt;
  560. while (before48(seqp->ccid2s_seq, hc->tx_high_ack)) {
  561. seqp = seqp->ccid2s_next;
  562. if (seqp == hc->tx_seqh) {
  563. seqp = hc->tx_seqh->ccid2s_prev;
  564. break;
  565. }
  566. }
  567. done = 0;
  568. while (1) {
  569. if (seqp->ccid2s_acked) {
  570. done++;
  571. if (done == NUMDUPACK)
  572. break;
  573. }
  574. if (seqp == hc->tx_seqt)
  575. break;
  576. seqp = seqp->ccid2s_prev;
  577. }
  578. /* If there are at least 3 acknowledgements, anything unacknowledged
  579. * below the last sequence number is considered lost
  580. */
  581. if (done == NUMDUPACK) {
  582. struct ccid2_seq *last_acked = seqp;
  583. /* check for lost packets */
  584. while (1) {
  585. if (!seqp->ccid2s_acked) {
  586. ccid2_pr_debug("Packet lost: %llu\n",
  587. (unsigned long long)seqp->ccid2s_seq);
  588. /* XXX need to traverse from tail -> head in
  589. * order to detect multiple congestion events in
  590. * one ack vector.
  591. */
  592. ccid2_congestion_event(sk, seqp);
  593. hc->tx_pipe--;
  594. }
  595. if (seqp == hc->tx_seqt)
  596. break;
  597. seqp = seqp->ccid2s_prev;
  598. }
  599. hc->tx_seqt = last_acked;
  600. }
  601. /* trim acked packets in tail */
  602. while (hc->tx_seqt != hc->tx_seqh) {
  603. if (!hc->tx_seqt->ccid2s_acked)
  604. break;
  605. hc->tx_seqt = hc->tx_seqt->ccid2s_next;
  606. }
  607. /* restart RTO timer if not all outstanding data has been acked */
  608. if (hc->tx_pipe == 0)
  609. sk_stop_timer(sk, &hc->tx_rtotimer);
  610. else
  611. sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto);
  612. done:
  613. /* check if incoming Acks allow pending packets to be sent */
  614. if (sender_was_blocked && !ccid2_cwnd_network_limited(hc))
  615. tasklet_schedule(&dccp_sk(sk)->dccps_xmitlet);
  616. dccp_ackvec_parsed_cleanup(&hc->tx_av_chunks);
  617. }
  618. static int ccid2_hc_tx_init(struct ccid *ccid, struct sock *sk)
  619. {
  620. struct ccid2_hc_tx_sock *hc = ccid_priv(ccid);
  621. struct dccp_sock *dp = dccp_sk(sk);
  622. u32 max_ratio;
  623. /* RFC 4341, 5: initialise ssthresh to arbitrarily high (max) value */
  624. hc->tx_ssthresh = ~0U;
  625. /* Use larger initial windows (RFC 4341, section 5). */
  626. hc->tx_cwnd = rfc3390_bytes_to_packets(dp->dccps_mss_cache);
  627. hc->tx_expected_wnd = hc->tx_cwnd;
  628. /* Make sure that Ack Ratio is enabled and within bounds. */
  629. max_ratio = DIV_ROUND_UP(hc->tx_cwnd, 2);
  630. if (dp->dccps_l_ack_ratio == 0 || dp->dccps_l_ack_ratio > max_ratio)
  631. dp->dccps_l_ack_ratio = max_ratio;
  632. /* XXX init ~ to window size... */
  633. if (ccid2_hc_tx_alloc_seq(hc))
  634. return -ENOMEM;
  635. hc->tx_rto = DCCP_TIMEOUT_INIT;
  636. hc->tx_rpdupack = -1;
  637. hc->tx_last_cong = hc->tx_lsndtime = hc->tx_cwnd_stamp = ccid2_jiffies32;
  638. hc->tx_cwnd_used = 0;
  639. hc->sk = sk;
  640. timer_setup(&hc->tx_rtotimer, ccid2_hc_tx_rto_expire, 0);
  641. INIT_LIST_HEAD(&hc->tx_av_chunks);
  642. return 0;
  643. }
  644. static void ccid2_hc_tx_exit(struct sock *sk)
  645. {
  646. struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
  647. int i;
  648. sk_stop_timer(sk, &hc->tx_rtotimer);
  649. for (i = 0; i < hc->tx_seqbufc; i++)
  650. kfree(hc->tx_seqbuf[i]);
  651. hc->tx_seqbufc = 0;
  652. dccp_ackvec_parsed_cleanup(&hc->tx_av_chunks);
  653. }
  654. static void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
  655. {
  656. struct ccid2_hc_rx_sock *hc = ccid2_hc_rx_sk(sk);
  657. if (!dccp_data_packet(skb))
  658. return;
  659. if (++hc->rx_num_data_pkts >= dccp_sk(sk)->dccps_r_ack_ratio) {
  660. dccp_send_ack(sk);
  661. hc->rx_num_data_pkts = 0;
  662. }
  663. }
  664. struct ccid_operations ccid2_ops = {
  665. .ccid_id = DCCPC_CCID2,
  666. .ccid_name = "TCP-like",
  667. .ccid_hc_tx_obj_size = sizeof(struct ccid2_hc_tx_sock),
  668. .ccid_hc_tx_init = ccid2_hc_tx_init,
  669. .ccid_hc_tx_exit = ccid2_hc_tx_exit,
  670. .ccid_hc_tx_send_packet = ccid2_hc_tx_send_packet,
  671. .ccid_hc_tx_packet_sent = ccid2_hc_tx_packet_sent,
  672. .ccid_hc_tx_parse_options = ccid2_hc_tx_parse_options,
  673. .ccid_hc_tx_packet_recv = ccid2_hc_tx_packet_recv,
  674. .ccid_hc_rx_obj_size = sizeof(struct ccid2_hc_rx_sock),
  675. .ccid_hc_rx_packet_recv = ccid2_hc_rx_packet_recv,
  676. };
  677. #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
  678. module_param(ccid2_debug, bool, 0644);
  679. MODULE_PARM_DESC(ccid2_debug, "Enable CCID-2 debug messages");
  680. #endif