2 * Copyright (c) 2005, 2006 Andrea Bittau <a.bittau@cs.ucl.ac.uk>
4 * Changes to meet Linux coding standards, and DCCP infrastructure fixes.
6 * Copyright (c) 2006 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
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.
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.
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.
24 * This implementation should follow RFC 4341
26 #include <linux/slab.h>
33 #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
34 static int ccid2_debug;
35 #define ccid2_pr_debug(format, a...) DCCP_PR_DEBUG(ccid2_debug, format, ##a)
37 #define ccid2_pr_debug(format, a...)
40 static int ccid2_hc_tx_alloc_seq(struct ccid2_hc_tx_sock *hc)
42 struct ccid2_seq *seqp;
45 /* check if we have space to preserve the pointer to the buffer */
46 if (hc->tx_seqbufc >= (sizeof(hc->tx_seqbuf) /
47 sizeof(struct ccid2_seq *)))
50 /* allocate buffer and initialize linked list */
51 seqp = kmalloc(CCID2_SEQBUF_LEN * sizeof(struct ccid2_seq), gfp_any());
55 for (i = 0; i < (CCID2_SEQBUF_LEN - 1); i++) {
56 seqp[i].ccid2s_next = &seqp[i + 1];
57 seqp[i + 1].ccid2s_prev = &seqp[i];
59 seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = seqp;
60 seqp->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1];
62 /* This is the first allocation. Initiate the head and tail. */
63 if (hc->tx_seqbufc == 0)
64 hc->tx_seqh = hc->tx_seqt = seqp;
66 /* link the existing list with the one we just created */
67 hc->tx_seqh->ccid2s_next = seqp;
68 seqp->ccid2s_prev = hc->tx_seqh;
70 hc->tx_seqt->ccid2s_prev = &seqp[CCID2_SEQBUF_LEN - 1];
71 seqp[CCID2_SEQBUF_LEN - 1].ccid2s_next = hc->tx_seqt;
74 /* store the original pointer to the buffer so we can free it */
75 hc->tx_seqbuf[hc->tx_seqbufc] = seqp;
81 static int ccid2_hc_tx_send_packet(struct sock *sk, struct sk_buff *skb)
83 struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
85 if (hc->tx_pipe < hc->tx_cwnd)
88 return 1; /* XXX CCID should dequeue when ready instead of polling */
91 static void ccid2_change_l_ack_ratio(struct sock *sk, u32 val)
93 struct dccp_sock *dp = dccp_sk(sk);
94 u32 max_ratio = DIV_ROUND_UP(ccid2_hc_tx_sk(sk)->tx_cwnd, 2);
97 * Ensure that Ack Ratio does not exceed ceil(cwnd/2), which is (2) from
98 * RFC 4341, 6.1.2. We ignore the statement that Ack Ratio 2 is always
99 * acceptable since this causes starvation/deadlock whenever cwnd < 2.
100 * The same problem arises when Ack Ratio is 0 (ie. Ack Ratio disabled).
102 if (val == 0 || val > max_ratio) {
103 DCCP_WARN("Limiting Ack Ratio (%u) to %u\n", val, max_ratio);
106 if (val > DCCPF_ACK_RATIO_MAX)
107 val = DCCPF_ACK_RATIO_MAX;
109 if (val == dp->dccps_l_ack_ratio)
112 ccid2_pr_debug("changing local ack ratio to %u\n", val);
113 dp->dccps_l_ack_ratio = val;
116 static void ccid2_change_srtt(struct ccid2_hc_tx_sock *hc, long val)
118 ccid2_pr_debug("change SRTT to %ld\n", val);
122 static void ccid2_start_rto_timer(struct sock *sk);
124 static void ccid2_hc_tx_rto_expire(unsigned long data)
126 struct sock *sk = (struct sock *)data;
127 struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
131 if (sock_owned_by_user(sk)) {
132 sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + HZ / 5);
136 ccid2_pr_debug("RTO_EXPIRE\n");
143 hc->tx_rto = 60 * HZ;
145 ccid2_start_rto_timer(sk);
147 /* adjust pipe, cwnd etc */
148 hc->tx_ssthresh = hc->tx_cwnd / 2;
149 if (hc->tx_ssthresh < 2)
154 /* clear state about stuff we sent */
155 hc->tx_seqt = hc->tx_seqh;
156 hc->tx_packets_acked = 0;
158 /* clear ack ratio state. */
160 hc->tx_rpdupack = -1;
161 ccid2_change_l_ack_ratio(sk, 1);
167 static void ccid2_start_rto_timer(struct sock *sk)
169 struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
171 ccid2_pr_debug("setting RTO timeout=%ld\n", hc->tx_rto);
173 BUG_ON(timer_pending(&hc->tx_rtotimer));
174 sk_reset_timer(sk, &hc->tx_rtotimer, jiffies + hc->tx_rto);
177 static void ccid2_hc_tx_packet_sent(struct sock *sk, int more, unsigned int len)
179 struct dccp_sock *dp = dccp_sk(sk);
180 struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
181 struct ccid2_seq *next;
185 hc->tx_seqh->ccid2s_seq = dp->dccps_gss;
186 hc->tx_seqh->ccid2s_acked = 0;
187 hc->tx_seqh->ccid2s_sent = jiffies;
189 next = hc->tx_seqh->ccid2s_next;
190 /* check if we need to alloc more space */
191 if (next == hc->tx_seqt) {
192 if (ccid2_hc_tx_alloc_seq(hc)) {
193 DCCP_CRIT("packet history - out of memory!");
194 /* FIXME: find a more graceful way to bail out */
197 next = hc->tx_seqh->ccid2s_next;
198 BUG_ON(next == hc->tx_seqt);
202 ccid2_pr_debug("cwnd=%d pipe=%d\n", hc->tx_cwnd, hc->tx_pipe);
205 * FIXME: The code below is broken and the variables have been removed
206 * from the socket struct. The `ackloss' variable was always set to 0,
207 * and with arsent there are several problems:
208 * (i) it doesn't just count the number of Acks, but all sent packets;
209 * (ii) it is expressed in # of packets, not # of windows, so the
210 * comparison below uses the wrong formula: Appendix A of RFC 4341
211 * comes up with the number K = cwnd / (R^2 - R) of consecutive windows
212 * of data with no lost or marked Ack packets. If arsent were the # of
213 * consecutive Acks received without loss, then Ack Ratio needs to be
214 * decreased by 1 when
215 * arsent >= K * cwnd / R = cwnd^2 / (R^3 - R^2)
216 * where cwnd / R is the number of Acks received per window of data
217 * (cf. RFC 4341, App. A). The problems are that
218 * - arsent counts other packets as well;
219 * - the comparison uses a formula different from RFC 4341;
220 * - computing a cubic/quadratic equation each time is too complicated.
221 * Hence a different algorithm is needed.
224 /* Ack Ratio. Need to maintain a concept of how many windows we sent */
226 /* We had an ack loss in this window... */
227 if (hc->tx_ackloss) {
228 if (hc->tx_arsent >= hc->tx_cwnd) {
233 /* No acks lost up to now... */
234 /* decrease ack ratio if enough packets were sent */
235 if (dp->dccps_l_ack_ratio > 1) {
236 /* XXX don't calculate denominator each time */
237 int denom = dp->dccps_l_ack_ratio * dp->dccps_l_ack_ratio -
238 dp->dccps_l_ack_ratio;
240 denom = hc->tx_cwnd * hc->tx_cwnd / denom;
242 if (hc->tx_arsent >= denom) {
243 ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio - 1);
247 /* we can't increase ack ratio further [1] */
248 hc->tx_arsent = 0; /* or maybe set it to cwnd*/
253 /* setup RTO timer */
254 if (!timer_pending(&hc->tx_rtotimer))
255 ccid2_start_rto_timer(sk);
257 #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
259 struct ccid2_seq *seqp = hc->tx_seqt;
261 while (seqp != hc->tx_seqh) {
262 ccid2_pr_debug("out seq=%llu acked=%d time=%lu\n",
263 (unsigned long long)seqp->ccid2s_seq,
264 seqp->ccid2s_acked, seqp->ccid2s_sent);
265 seqp = seqp->ccid2s_next;
268 ccid2_pr_debug("=========\n");
272 /* XXX Lame code duplication!
273 * returns -1 if none was found.
274 * else returns the next offset to use in the function call.
276 static int ccid2_ackvector(struct sock *sk, struct sk_buff *skb, int offset,
277 unsigned char **vec, unsigned char *veclen)
279 const struct dccp_hdr *dh = dccp_hdr(skb);
280 unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb);
281 unsigned char *opt_ptr;
282 const unsigned char *opt_end = (unsigned char *)dh +
283 (dh->dccph_doff * 4);
284 unsigned char opt, len;
285 unsigned char *value;
290 if (opt_ptr >= opt_end)
293 while (opt_ptr != opt_end) {
298 /* Check if this isn't a single byte option */
299 if (opt > DCCPO_MAX_RESERVED) {
300 if (opt_ptr == opt_end)
301 goto out_invalid_option;
305 goto out_invalid_option;
307 * Remove the type and len fields, leaving
308 * just the value size
314 if (opt_ptr > opt_end)
315 goto out_invalid_option;
319 case DCCPO_ACK_VECTOR_0:
320 case DCCPO_ACK_VECTOR_1:
323 return offset + (opt_ptr - options);
330 DCCP_BUG("Invalid option - this should not happen (previous parsing)!");
334 static void ccid2_hc_tx_kill_rto_timer(struct sock *sk)
336 struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
338 sk_stop_timer(sk, &hc->tx_rtotimer);
339 ccid2_pr_debug("deleted RTO timer\n");
342 static inline void ccid2_new_ack(struct sock *sk,
343 struct ccid2_seq *seqp,
344 unsigned int *maxincr)
346 struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
348 if (hc->tx_cwnd < hc->tx_ssthresh) {
349 if (*maxincr > 0 && ++hc->tx_packets_acked == 2) {
352 hc->tx_packets_acked = 0;
354 } else if (++hc->tx_packets_acked >= hc->tx_cwnd) {
356 hc->tx_packets_acked = 0;
360 if (hc->tx_srtt == -1 ||
361 time_after(jiffies, hc->tx_lastrtt + hc->tx_srtt)) {
362 unsigned long r = (long)jiffies - (long)seqp->ccid2s_sent;
365 /* first measurement */
366 if (hc->tx_srtt == -1) {
367 ccid2_pr_debug("R: %lu Time=%lu seq=%llu\n",
369 (unsigned long long)seqp->ccid2s_seq);
370 ccid2_change_srtt(hc, r);
371 hc->tx_rttvar = r >> 1;
374 long tmp = hc->tx_srtt - r;
383 hc->tx_rttvar += tmp;
391 ccid2_change_srtt(hc, srtt);
393 s = hc->tx_rttvar << 2;
394 /* clock granularity is 1 when based on jiffies */
397 hc->tx_rto = hc->tx_srtt + s;
399 /* must be at least a second */
401 /* DCCP doesn't require this [but I like it cuz my code sux] */
408 hc->tx_rto = HZ * 60;
410 hc->tx_lastrtt = jiffies;
412 ccid2_pr_debug("srtt: %ld rttvar: %ld rto: %ld (HZ=%d) R=%lu\n",
413 hc->tx_srtt, hc->tx_rttvar,
417 /* we got a new ack, so re-start RTO timer */
418 ccid2_hc_tx_kill_rto_timer(sk);
419 ccid2_start_rto_timer(sk);
422 static void ccid2_hc_tx_dec_pipe(struct sock *sk)
424 struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
426 if (hc->tx_pipe == 0)
427 DCCP_BUG("pipe == 0");
431 if (hc->tx_pipe == 0)
432 ccid2_hc_tx_kill_rto_timer(sk);
435 static void ccid2_congestion_event(struct sock *sk, struct ccid2_seq *seqp)
437 struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
439 if (time_before(seqp->ccid2s_sent, hc->tx_last_cong)) {
440 ccid2_pr_debug("Multiple losses in an RTT---treating as one\n");
444 hc->tx_last_cong = jiffies;
446 hc->tx_cwnd = hc->tx_cwnd / 2 ? : 1U;
447 hc->tx_ssthresh = max(hc->tx_cwnd, 2U);
449 /* Avoid spurious timeouts resulting from Ack Ratio > cwnd */
450 if (dccp_sk(sk)->dccps_l_ack_ratio > hc->tx_cwnd)
451 ccid2_change_l_ack_ratio(sk, hc->tx_cwnd);
454 static void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
456 struct dccp_sock *dp = dccp_sk(sk);
457 struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
459 struct ccid2_seq *seqp;
460 unsigned char *vector;
461 unsigned char veclen;
464 unsigned int maxincr = 0;
466 /* check reverse path congestion */
467 seqno = DCCP_SKB_CB(skb)->dccpd_seq;
469 /* XXX this whole "algorithm" is broken. Need to fix it to keep track
470 * of the seqnos of the dupacks so that rpseq and rpdupack are correct
473 /* need to bootstrap */
474 if (hc->tx_rpdupack == -1) {
476 hc->tx_rpseq = seqno;
478 /* check if packet is consecutive */
479 if (dccp_delta_seqno(hc->tx_rpseq, seqno) == 1)
480 hc->tx_rpseq = seqno;
481 /* it's a later packet */
482 else if (after48(seqno, hc->tx_rpseq)) {
485 /* check if we got enough dupacks */
486 if (hc->tx_rpdupack >= NUMDUPACK) {
487 hc->tx_rpdupack = -1; /* XXX lame */
490 ccid2_change_l_ack_ratio(sk, 2 * dp->dccps_l_ack_ratio);
495 /* check forward path congestion */
496 /* still didn't send out new data packets */
497 if (hc->tx_seqh == hc->tx_seqt)
500 switch (DCCP_SKB_CB(skb)->dccpd_type) {
502 case DCCP_PKT_DATAACK:
508 ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
509 if (after48(ackno, hc->tx_high_ack))
510 hc->tx_high_ack = ackno;
513 while (before48(seqp->ccid2s_seq, ackno)) {
514 seqp = seqp->ccid2s_next;
515 if (seqp == hc->tx_seqh) {
516 seqp = hc->tx_seqh->ccid2s_prev;
522 * In slow-start, cwnd can increase up to a maximum of Ack Ratio/2
523 * packets per acknowledgement. Rounding up avoids that cwnd is not
524 * advanced when Ack Ratio is 1 and gives a slight edge otherwise.
526 if (hc->tx_cwnd < hc->tx_ssthresh)
527 maxincr = DIV_ROUND_UP(dp->dccps_l_ack_ratio, 2);
529 /* go through all ack vectors */
530 while ((offset = ccid2_ackvector(sk, skb, offset,
531 &vector, &veclen)) != -1) {
532 /* go through this ack vector */
534 const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
535 u64 ackno_end_rl = SUB48(ackno, rl);
537 ccid2_pr_debug("ackvec start:%llu end:%llu\n",
538 (unsigned long long)ackno,
539 (unsigned long long)ackno_end_rl);
540 /* if the seqno we are analyzing is larger than the
541 * current ackno, then move towards the tail of our
544 while (after48(seqp->ccid2s_seq, ackno)) {
545 if (seqp == hc->tx_seqt) {
549 seqp = seqp->ccid2s_prev;
554 /* check all seqnos in the range of the vector
557 while (between48(seqp->ccid2s_seq,ackno_end_rl,ackno)) {
558 const u8 state = *vector &
559 DCCP_ACKVEC_STATE_MASK;
561 /* new packet received or marked */
562 if (state != DCCP_ACKVEC_STATE_NOT_RECEIVED &&
563 !seqp->ccid2s_acked) {
565 DCCP_ACKVEC_STATE_ECN_MARKED) {
566 ccid2_congestion_event(sk,
569 ccid2_new_ack(sk, seqp,
572 seqp->ccid2s_acked = 1;
573 ccid2_pr_debug("Got ack for %llu\n",
574 (unsigned long long)seqp->ccid2s_seq);
575 ccid2_hc_tx_dec_pipe(sk);
577 if (seqp == hc->tx_seqt) {
581 seqp = seqp->ccid2s_prev;
586 ackno = SUB48(ackno_end_rl, 1);
593 /* The state about what is acked should be correct now
594 * Check for NUMDUPACK
597 while (before48(seqp->ccid2s_seq, hc->tx_high_ack)) {
598 seqp = seqp->ccid2s_next;
599 if (seqp == hc->tx_seqh) {
600 seqp = hc->tx_seqh->ccid2s_prev;
606 if (seqp->ccid2s_acked) {
608 if (done == NUMDUPACK)
611 if (seqp == hc->tx_seqt)
613 seqp = seqp->ccid2s_prev;
616 /* If there are at least 3 acknowledgements, anything unacknowledged
617 * below the last sequence number is considered lost
619 if (done == NUMDUPACK) {
620 struct ccid2_seq *last_acked = seqp;
622 /* check for lost packets */
624 if (!seqp->ccid2s_acked) {
625 ccid2_pr_debug("Packet lost: %llu\n",
626 (unsigned long long)seqp->ccid2s_seq);
627 /* XXX need to traverse from tail -> head in
628 * order to detect multiple congestion events in
631 ccid2_congestion_event(sk, seqp);
632 ccid2_hc_tx_dec_pipe(sk);
634 if (seqp == hc->tx_seqt)
636 seqp = seqp->ccid2s_prev;
639 hc->tx_seqt = last_acked;
642 /* trim acked packets in tail */
643 while (hc->tx_seqt != hc->tx_seqh) {
644 if (!hc->tx_seqt->ccid2s_acked)
647 hc->tx_seqt = hc->tx_seqt->ccid2s_next;
651 static int ccid2_hc_tx_init(struct ccid *ccid, struct sock *sk)
653 struct ccid2_hc_tx_sock *hc = ccid_priv(ccid);
654 struct dccp_sock *dp = dccp_sk(sk);
657 /* RFC 4341, 5: initialise ssthresh to arbitrarily high (max) value */
658 hc->tx_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 hc->tx_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(hc->tx_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(hc))
677 ccid2_change_srtt(hc, -1);
679 hc->tx_rpdupack = -1;
680 hc->tx_last_cong = jiffies;
681 setup_timer(&hc->tx_rtotimer, ccid2_hc_tx_rto_expire,
686 static void ccid2_hc_tx_exit(struct sock *sk)
688 struct ccid2_hc_tx_sock *hc = ccid2_hc_tx_sk(sk);
691 ccid2_hc_tx_kill_rto_timer(sk);
693 for (i = 0; i < hc->tx_seqbufc; i++)
694 kfree(hc->tx_seqbuf[i]);
698 static void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
700 const struct dccp_sock *dp = dccp_sk(sk);
701 struct ccid2_hc_rx_sock *hc = ccid2_hc_rx_sk(sk);
703 switch (DCCP_SKB_CB(skb)->dccpd_type) {
705 case DCCP_PKT_DATAACK:
707 if (hc->rx_data >= dp->dccps_r_ack_ratio) {
715 struct ccid_operations ccid2_ops = {
716 .ccid_id = DCCPC_CCID2,
717 .ccid_name = "TCP-like",
718 .ccid_hc_tx_obj_size = sizeof(struct ccid2_hc_tx_sock),
719 .ccid_hc_tx_init = ccid2_hc_tx_init,
720 .ccid_hc_tx_exit = ccid2_hc_tx_exit,
721 .ccid_hc_tx_send_packet = ccid2_hc_tx_send_packet,
722 .ccid_hc_tx_packet_sent = ccid2_hc_tx_packet_sent,
723 .ccid_hc_tx_packet_recv = ccid2_hc_tx_packet_recv,
724 .ccid_hc_rx_obj_size = sizeof(struct ccid2_hc_rx_sock),
725 .ccid_hc_rx_packet_recv = ccid2_hc_rx_packet_recv,
728 #ifdef CONFIG_IP_DCCP_CCID2_DEBUG
729 module_param(ccid2_debug, bool, 0644);
730 MODULE_PARM_DESC(ccid2_debug, "Enable CCID-2 debug messages");