2 * net/dccp/ccids/ccid2.c
4 * Copyright (c) 2005, 2006 Andrea Bittau <a.bittau@cs.ucl.ac.uk>
6 * Changes to meet Linux coding standards, and DCCP infrastructure fixes.
8 * Copyright (c) 2006 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * This implementation should follow RFC 4341
34 #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
35 static int ccid2_debug;
36 #define ccid2_pr_debug(format, a...) DCCP_PR_DEBUG(ccid2_debug, format, ##a)
38 static void ccid2_hc_tx_check_sanity(const struct ccid2_hc_tx_sock *hctx)
42 struct ccid2_seq *seqp = hctx->seqh;
44 /* there is data in the chain */
45 if (seqp != hctx->seqt) {
46 seqp = seqp->ccid2s_prev;
48 if (!seqp->ccid2s_acked)
51 while (seqp != hctx->seqt) {
52 struct ccid2_seq *prev = seqp->ccid2s_prev;
55 if (!prev->ccid2s_acked)
58 /* packets are sent sequentially */
59 BUG_ON(dccp_delta_seqno(seqp->ccid2s_seq,
60 prev->ccid2s_seq ) >= 0);
61 BUG_ON(time_before(seqp->ccid2s_sent,
68 BUG_ON(pipe != hctx->pipe);
69 ccid2_pr_debug("len of chain=%d\n", len);
72 seqp = seqp->ccid2s_prev;
74 } while (seqp != hctx->seqh);
76 ccid2_pr_debug("total len=%d\n", len);
77 BUG_ON(len != hctx->seqbufc * CCID2_SEQBUF_LEN);
80 #define ccid2_pr_debug(format, a...)
81 #define ccid2_hc_tx_check_sanity(hctx)
84 static int ccid2_hc_tx_alloc_seq(struct ccid2_hc_tx_sock *hctx)
86 struct ccid2_seq *seqp;
89 /* check if we have space to preserve the pointer to the buffer */
90 if (hctx->seqbufc >= sizeof(hctx->seqbuf) / sizeof(struct ccid2_seq *))
93 /* allocate buffer and initialize linked list */
94 seqp = kmalloc(CCID2_SEQBUF_LEN * sizeof(struct ccid2_seq), gfp_any());
98 for (i = 0; i < (CCID2_SEQBUF_LEN - 1); i++) {
99 seqp[i].ccid2s_next = &seqp[i + 1];
100 seqp[i + 1].ccid2s_prev = &seqp[i];
102 seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = seqp;
103 seqp->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1];
105 /* This is the first allocation. Initiate the head and tail. */
106 if (hctx->seqbufc == 0)
107 hctx->seqh = hctx->seqt = seqp;
109 /* link the existing list with the one we just created */
110 hctx->seqh->ccid2s_next = seqp;
111 seqp->ccid2s_prev = hctx->seqh;
113 hctx->seqt->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1];
114 seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = hctx->seqt;
117 /* store the original pointer to the buffer so we can free it */
118 hctx->seqbuf[hctx->seqbufc] = seqp;
124 static int ccid2_hc_tx_send_packet(struct sock *sk, struct sk_buff *skb)
126 if (ccid2_cwnd_network_limited(ccid2_hc_tx_sk(sk)))
127 return CCID_PACKET_WILL_DEQUEUE_LATER;
128 return CCID_PACKET_SEND_AT_ONCE;
131 static void ccid2_change_l_ack_ratio(struct sock *sk, u32 val)
133 struct dccp_sock *dp = dccp_sk(sk);
134 u32 max_ratio = DIV_ROUND_UP(ccid2_hc_tx_sk(sk)->cwnd, 2);
137 * Ensure that Ack Ratio does not exceed ceil(cwnd/2), which is (2) from
138 * RFC 4341, 6.1.2. We ignore the statement that Ack Ratio 2 is always
139 * acceptable since this causes starvation/deadlock whenever cwnd < 2.
140 * The same problem arises when Ack Ratio is 0 (ie. Ack Ratio disabled).
142 if (val == 0 || val > max_ratio) {
143 DCCP_WARN("Limiting Ack Ratio (%u) to %u\n", val, max_ratio);
146 if (val > DCCPF_ACK_RATIO_MAX)
147 val = DCCPF_ACK_RATIO_MAX;
149 if (val == dp->dccps_l_ack_ratio)
152 ccid2_pr_debug("changing local ack ratio to %u\n", val);
153 dp->dccps_l_ack_ratio = val;
156 static void ccid2_change_srtt(struct ccid2_hc_tx_sock *hctx, long val)
158 ccid2_pr_debug("change SRTT to %ld\n", val);
162 static void ccid2_start_rto_timer(struct sock *sk);
164 static void ccid2_hc_tx_rto_expire(unsigned long data)
166 struct sock *sk = (struct sock *)data;
167 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
168 const bool sender_was_blocked = ccid2_cwnd_network_limited(hctx);
172 if (sock_owned_by_user(sk)) {
173 sk_reset_timer(sk, &hctx->rtotimer, jiffies + HZ / 5);
177 ccid2_pr_debug("RTO_EXPIRE\n");
179 ccid2_hc_tx_check_sanity(hctx);
188 /* adjust pipe, cwnd etc */
189 hctx->ssthresh = hctx->cwnd / 2;
190 if (hctx->ssthresh < 2)
195 /* clear state about stuff we sent */
196 hctx->seqt = hctx->seqh;
197 hctx->packets_acked = 0;
199 /* clear ack ratio state. */
202 ccid2_change_l_ack_ratio(sk, 1);
203 ccid2_hc_tx_check_sanity(hctx);
205 /* if we were blocked before, we may now send cwnd=1 packet */
206 if (sender_was_blocked)
207 tasklet_schedule(&dccp_sk(sk)->dccps_xmitlet);
208 ccid2_start_rto_timer(sk);
214 static void ccid2_start_rto_timer(struct sock *sk)
216 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
218 ccid2_pr_debug("setting RTO timeout=%ld\n", hctx->rto);
220 BUG_ON(timer_pending(&hctx->rtotimer));
221 sk_reset_timer(sk, &hctx->rtotimer,
222 jiffies + hctx->rto);
225 static void ccid2_hc_tx_packet_sent(struct sock *sk, unsigned int len)
227 struct dccp_sock *dp = dccp_sk(sk);
228 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
229 struct ccid2_seq *next;
233 hctx->seqh->ccid2s_seq = dp->dccps_gss;
234 hctx->seqh->ccid2s_acked = 0;
235 hctx->seqh->ccid2s_sent = jiffies;
237 next = hctx->seqh->ccid2s_next;
238 /* check if we need to alloc more space */
239 if (next == hctx->seqt) {
240 if (ccid2_hc_tx_alloc_seq(hctx)) {
241 DCCP_CRIT("packet history - out of memory!");
242 /* FIXME: find a more graceful way to bail out */
245 next = hctx->seqh->ccid2s_next;
246 BUG_ON(next == hctx->seqt);
250 ccid2_pr_debug("cwnd=%d pipe=%d\n", hctx->cwnd, hctx->pipe);
253 * FIXME: The code below is broken and the variables have been removed
254 * from the socket struct. The `ackloss' variable was always set to 0,
255 * and with arsent there are several problems:
256 * (i) it doesn't just count the number of Acks, but all sent packets;
257 * (ii) it is expressed in # of packets, not # of windows, so the
258 * comparison below uses the wrong formula: Appendix A of RFC 4341
259 * comes up with the number K = cwnd / (R^2 - R) of consecutive windows
260 * of data with no lost or marked Ack packets. If arsent were the # of
261 * consecutive Acks received without loss, then Ack Ratio needs to be
262 * decreased by 1 when
263 * arsent >= K * cwnd / R = cwnd^2 / (R^3 - R^2)
264 * where cwnd / R is the number of Acks received per window of data
265 * (cf. RFC 4341, App. A). The problems are that
266 * - arsent counts other packets as well;
267 * - the comparison uses a formula different from RFC 4341;
268 * - computing a cubic/quadratic equation each time is too complicated.
269 * Hence a different algorithm is needed.
272 /* Ack Ratio. Need to maintain a concept of how many windows we sent */
274 /* We had an ack loss in this window... */
276 if (hctx->arsent >= hctx->cwnd) {
281 /* No acks lost up to now... */
282 /* decrease ack ratio if enough packets were sent */
283 if (dp->dccps_l_ack_ratio > 1) {
284 /* XXX don't calculate denominator each time */
285 int denom = dp->dccps_l_ack_ratio * dp->dccps_l_ack_ratio -
286 dp->dccps_l_ack_ratio;
288 denom = hctx->cwnd * hctx->cwnd / denom;
290 if (hctx->arsent >= denom) {
291 ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio - 1);
295 /* we can't increase ack ratio further [1] */
296 hctx->arsent = 0; /* or maybe set it to cwnd*/
301 /* setup RTO timer */
302 if (!timer_pending(&hctx->rtotimer))
303 ccid2_start_rto_timer(sk);
305 #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
307 struct ccid2_seq *seqp = hctx->seqt;
309 while (seqp != hctx->seqh) {
310 ccid2_pr_debug("out seq=%llu acked=%d time=%lu\n",
311 (unsigned long long)seqp->ccid2s_seq,
312 seqp->ccid2s_acked, seqp->ccid2s_sent);
313 seqp = seqp->ccid2s_next;
316 ccid2_pr_debug("=========\n");
317 ccid2_hc_tx_check_sanity(hctx);
321 static void ccid2_hc_tx_kill_rto_timer(struct sock *sk)
323 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
325 sk_stop_timer(sk, &hctx->rtotimer);
326 ccid2_pr_debug("deleted RTO timer\n");
329 static inline void ccid2_new_ack(struct sock *sk,
330 struct ccid2_seq *seqp,
331 unsigned int *maxincr)
333 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
335 if (hctx->cwnd < hctx->ssthresh) {
336 if (*maxincr > 0 && ++hctx->packets_acked == 2) {
339 hctx->packets_acked = 0;
341 } else if (++hctx->packets_acked >= hctx->cwnd) {
343 hctx->packets_acked = 0;
347 if (hctx->srtt == -1 ||
348 time_after(jiffies, hctx->lastrtt + hctx->srtt)) {
349 unsigned long r = (long)jiffies - (long)seqp->ccid2s_sent;
352 /* first measurement */
353 if (hctx->srtt == -1) {
354 ccid2_pr_debug("R: %lu Time=%lu seq=%llu\n",
356 (unsigned long long)seqp->ccid2s_seq);
357 ccid2_change_srtt(hctx, r);
358 hctx->rttvar = r >> 1;
361 long tmp = hctx->srtt - r;
378 ccid2_change_srtt(hctx, srtt);
380 s = hctx->rttvar << 2;
381 /* clock granularity is 1 when based on jiffies */
384 hctx->rto = hctx->srtt + s;
386 /* must be at least a second */
388 /* DCCP doesn't require this [but I like it cuz my code sux] */
397 hctx->lastrtt = jiffies;
399 ccid2_pr_debug("srtt: %ld rttvar: %ld rto: %ld (HZ=%d) R=%lu\n",
400 hctx->srtt, hctx->rttvar,
404 /* we got a new ack, so re-start RTO timer */
405 ccid2_hc_tx_kill_rto_timer(sk);
406 ccid2_start_rto_timer(sk);
409 static void ccid2_hc_tx_dec_pipe(struct sock *sk)
411 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
414 DCCP_BUG("pipe == 0");
419 ccid2_hc_tx_kill_rto_timer(sk);
422 static void ccid2_congestion_event(struct sock *sk, struct ccid2_seq *seqp)
424 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
426 if (time_before(seqp->ccid2s_sent, hctx->last_cong)) {
427 ccid2_pr_debug("Multiple losses in an RTT---treating as one\n");
431 hctx->last_cong = jiffies;
433 hctx->cwnd = hctx->cwnd / 2 ? : 1U;
434 hctx->ssthresh = max(hctx->cwnd, 2U);
436 /* Avoid spurious timeouts resulting from Ack Ratio > cwnd */
437 if (dccp_sk(sk)->dccps_l_ack_ratio > hctx->cwnd)
438 ccid2_change_l_ack_ratio(sk, hctx->cwnd);
441 static int ccid2_hc_tx_parse_options(struct sock *sk, u8 packet_type,
442 u8 option, u8 *optval, u8 optlen)
444 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
447 case DCCPO_ACK_VECTOR_0:
448 case DCCPO_ACK_VECTOR_1:
449 return dccp_ackvec_parsed_add(&hctx->av_chunks, optval, optlen,
450 option - DCCPO_ACK_VECTOR_0);
455 static void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
457 struct dccp_sock *dp = dccp_sk(sk);
458 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
459 const bool sender_was_blocked = ccid2_cwnd_network_limited(hctx);
460 struct dccp_ackvec_parsed *avp;
462 struct ccid2_seq *seqp;
464 unsigned int maxincr = 0;
466 ccid2_hc_tx_check_sanity(hctx);
467 /* check reverse path congestion */
468 seqno = DCCP_SKB_CB(skb)->dccpd_seq;
470 /* XXX this whole "algorithm" is broken. Need to fix it to keep track
471 * of the seqnos of the dupacks so that rpseq and rpdupack are correct
474 /* need to bootstrap */
475 if (hctx->rpdupack == -1) {
479 /* check if packet is consecutive */
480 if (dccp_delta_seqno(hctx->rpseq, seqno) == 1)
482 /* it's a later packet */
483 else if (after48(seqno, hctx->rpseq)) {
486 /* check if we got enough dupacks */
487 if (hctx->rpdupack >= NUMDUPACK) {
488 hctx->rpdupack = -1; /* XXX lame */
491 ccid2_change_l_ack_ratio(sk, 2 * dp->dccps_l_ack_ratio);
496 /* check forward path congestion */
497 if (dccp_packet_without_ack(skb))
500 /* still didn't send out new data packets */
501 if (hctx->seqh == hctx->seqt)
504 ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
505 if (after48(ackno, hctx->high_ack))
506 hctx->high_ack = ackno;
509 while (before48(seqp->ccid2s_seq, ackno)) {
510 seqp = seqp->ccid2s_next;
511 if (seqp == hctx->seqh) {
512 seqp = hctx->seqh->ccid2s_prev;
518 * In slow-start, cwnd can increase up to a maximum of Ack Ratio/2
519 * packets per acknowledgement. Rounding up avoids that cwnd is not
520 * advanced when Ack Ratio is 1 and gives a slight edge otherwise.
522 if (hctx->cwnd < hctx->ssthresh)
523 maxincr = DIV_ROUND_UP(dp->dccps_l_ack_ratio, 2);
525 /* go through all ack vectors */
526 list_for_each_entry(avp, &hctx->av_chunks, node) {
527 /* go through this ack vector */
528 for (; avp->len--; avp->vec++) {
529 u64 ackno_end_rl = SUB48(ackno,
530 dccp_ackvec_runlen(avp->vec));
532 ccid2_pr_debug("ackvec %llu |%u,%u|\n",
533 (unsigned long long)ackno,
534 dccp_ackvec_state(avp->vec) >> 6,
535 dccp_ackvec_runlen(avp->vec));
536 /* if the seqno we are analyzing is larger than the
537 * current ackno, then move towards the tail of our
540 while (after48(seqp->ccid2s_seq, ackno)) {
541 if (seqp == hctx->seqt) {
545 seqp = seqp->ccid2s_prev;
550 /* check all seqnos in the range of the vector
553 while (between48(seqp->ccid2s_seq,ackno_end_rl,ackno)) {
554 const u8 state = dccp_ackvec_state(avp->vec);
556 /* new packet received or marked */
557 if (state != DCCPAV_NOT_RECEIVED &&
558 !seqp->ccid2s_acked) {
559 if (state == DCCPAV_ECN_MARKED)
560 ccid2_congestion_event(sk,
563 ccid2_new_ack(sk, seqp,
566 seqp->ccid2s_acked = 1;
567 ccid2_pr_debug("Got ack for %llu\n",
568 (unsigned long long)seqp->ccid2s_seq);
569 ccid2_hc_tx_dec_pipe(sk);
571 if (seqp == hctx->seqt) {
575 seqp = seqp->ccid2s_prev;
580 ackno = SUB48(ackno_end_rl, 1);
586 /* The state about what is acked should be correct now
587 * Check for NUMDUPACK
590 while (before48(seqp->ccid2s_seq, hctx->high_ack)) {
591 seqp = seqp->ccid2s_next;
592 if (seqp == hctx->seqh) {
593 seqp = hctx->seqh->ccid2s_prev;
599 if (seqp->ccid2s_acked) {
601 if (done == NUMDUPACK)
604 if (seqp == hctx->seqt)
606 seqp = seqp->ccid2s_prev;
609 /* If there are at least 3 acknowledgements, anything unacknowledged
610 * below the last sequence number is considered lost
612 if (done == NUMDUPACK) {
613 struct ccid2_seq *last_acked = seqp;
615 /* check for lost packets */
617 if (!seqp->ccid2s_acked) {
618 ccid2_pr_debug("Packet lost: %llu\n",
619 (unsigned long long)seqp->ccid2s_seq);
620 /* XXX need to traverse from tail -> head in
621 * order to detect multiple congestion events in
624 ccid2_congestion_event(sk, seqp);
625 ccid2_hc_tx_dec_pipe(sk);
627 if (seqp == hctx->seqt)
629 seqp = seqp->ccid2s_prev;
632 hctx->seqt = last_acked;
635 /* trim acked packets in tail */
636 while (hctx->seqt != hctx->seqh) {
637 if (!hctx->seqt->ccid2s_acked)
640 hctx->seqt = hctx->seqt->ccid2s_next;
643 ccid2_hc_tx_check_sanity(hctx);
645 /* check if incoming Acks allow pending packets to be sent */
646 if (sender_was_blocked && !ccid2_cwnd_network_limited(hctx))
647 tasklet_schedule(&dccp_sk(sk)->dccps_xmitlet);
648 dccp_ackvec_parsed_cleanup(&hctx->av_chunks);
651 static int ccid2_hc_tx_init(struct ccid *ccid, struct sock *sk)
653 struct ccid2_hc_tx_sock *hctx = ccid_priv(ccid);
654 struct dccp_sock *dp = dccp_sk(sk);
657 /* RFC 4341, 5: initialise ssthresh to arbitrarily high (max) value */
658 hctx->ssthresh = ~0U;
661 * RFC 4341, 5: "The cwnd parameter is initialized to at most four
662 * packets for new connections, following the rules from [RFC3390]".
663 * We need to convert the bytes of RFC3390 into the packets of RFC 4341.
665 hctx->cwnd = clamp(4380U / dp->dccps_mss_cache, 2U, 4U);
667 /* Make sure that Ack Ratio is enabled and within bounds. */
668 max_ratio = DIV_ROUND_UP(hctx->cwnd, 2);
669 if (dp->dccps_l_ack_ratio == 0 || dp->dccps_l_ack_ratio > max_ratio)
670 dp->dccps_l_ack_ratio = max_ratio;
672 /* XXX init ~ to window size... */
673 if (ccid2_hc_tx_alloc_seq(hctx))
677 ccid2_change_srtt(hctx, -1);
680 hctx->last_cong = jiffies;
681 setup_timer(&hctx->rtotimer, ccid2_hc_tx_rto_expire, (unsigned long)sk);
682 INIT_LIST_HEAD(&hctx->av_chunks);
684 ccid2_hc_tx_check_sanity(hctx);
688 static void ccid2_hc_tx_exit(struct sock *sk)
690 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
693 ccid2_hc_tx_kill_rto_timer(sk);
695 for (i = 0; i < hctx->seqbufc; i++)
696 kfree(hctx->seqbuf[i]);
700 static void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
702 const struct dccp_sock *dp = dccp_sk(sk);
703 struct ccid2_hc_rx_sock *hcrx = ccid2_hc_rx_sk(sk);
705 switch (DCCP_SKB_CB(skb)->dccpd_type) {
707 case DCCP_PKT_DATAACK:
709 if (hcrx->data >= dp->dccps_r_ack_ratio) {
717 static struct ccid_operations ccid2 = {
718 .ccid_id = DCCPC_CCID2,
719 .ccid_name = "TCP-like",
720 .ccid_owner = THIS_MODULE,
721 .ccid_hc_tx_obj_size = sizeof(struct ccid2_hc_tx_sock),
722 .ccid_hc_tx_init = ccid2_hc_tx_init,
723 .ccid_hc_tx_exit = ccid2_hc_tx_exit,
724 .ccid_hc_tx_send_packet = ccid2_hc_tx_send_packet,
725 .ccid_hc_tx_packet_sent = ccid2_hc_tx_packet_sent,
726 .ccid_hc_tx_parse_options = ccid2_hc_tx_parse_options,
727 .ccid_hc_tx_packet_recv = ccid2_hc_tx_packet_recv,
728 .ccid_hc_rx_obj_size = sizeof(struct ccid2_hc_rx_sock),
729 .ccid_hc_rx_packet_recv = ccid2_hc_rx_packet_recv,
732 #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
733 module_param(ccid2_debug, bool, 0644);
734 MODULE_PARM_DESC(ccid2_debug, "Enable debug messages");
737 static __init int ccid2_module_init(void)
739 return ccid_register(&ccid2);
741 module_init(ccid2_module_init);
743 static __exit void ccid2_module_exit(void)
745 ccid_unregister(&ccid2);
747 module_exit(ccid2_module_exit);
749 MODULE_AUTHOR("Andrea Bittau <a.bittau@cs.ucl.ac.uk>");
750 MODULE_DESCRIPTION("DCCP TCP-Like (CCID2) CCID");
751 MODULE_LICENSE("GPL");
752 MODULE_ALIAS("net-dccp-ccid-2");