ccid2.c 20 KB

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