ccid2.c 23 KB

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