ccid2.c 23 KB

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