ccid2.c 23 KB

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