2 * net/dccp/ccids/ccid3.c
4 * Copyright (c) 2005 The University of Waikato, Hamilton, New Zealand.
5 * Copyright (c) 2005 Ian McDonald <iam4@cs.waikato.ac.nz>
7 * An implementation of the DCCP protocol
9 * This code has been developed by the University of Waikato WAND
10 * research group. For further information please see http://www.wand.net.nz/
12 * This code also uses code from Lulea University, rereleased as GPL by its
14 * Copyright (c) 2003 Nils-Erik Mattsson, Joacim Haggmark, Magnus Erixzon
16 * Changes to meet Linux coding standards, to make it meet latest ccid3 draft
17 * and to make it work as a loadable module in the DCCP stack written by
18 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>.
20 * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 #include <linux/config.h>
40 #include "lib/packet_history.h"
41 #include "lib/loss_interval.h"
46 * Reason for maths here is to avoid 32 bit overflow when a is big.
47 * With this we get close to the limit.
49 static inline u32 usecs_div(const u32 a, const u32 b)
51 const u32 div = a < (UINT_MAX / (USEC_PER_SEC / 10)) ? 10 :
52 a < (UINT_MAX / (USEC_PER_SEC / 50)) ? 50 :
53 a < (UINT_MAX / (USEC_PER_SEC / 100)) ? 100 :
54 a < (UINT_MAX / (USEC_PER_SEC / 500)) ? 500 :
55 a < (UINT_MAX / (USEC_PER_SEC / 1000)) ? 1000 :
56 a < (UINT_MAX / (USEC_PER_SEC / 5000)) ? 5000 :
57 a < (UINT_MAX / (USEC_PER_SEC / 10000)) ? 10000 :
58 a < (UINT_MAX / (USEC_PER_SEC / 50000)) ? 50000 :
60 const u32 tmp = a * (USEC_PER_SEC / div);
61 return (b >= 2 * div) ? tmp / (b / div) : tmp;
64 static int ccid3_debug;
67 #define ccid3_pr_debug(format, a...) \
68 do { if (ccid3_debug) \
69 printk(KERN_DEBUG "%s: " format, __FUNCTION__, ##a); \
72 #define ccid3_pr_debug(format, a...)
75 static struct dccp_tx_hist *ccid3_tx_hist;
76 static struct dccp_rx_hist *ccid3_rx_hist;
77 static struct dccp_li_hist *ccid3_li_hist;
79 static int ccid3_init(struct sock *sk)
81 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
85 static void ccid3_exit(struct sock *sk)
87 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
90 /* TFRC sender states */
91 enum ccid3_hc_tx_states {
92 TFRC_SSTATE_NO_SENT = 1,
99 static const char *ccid3_tx_state_name(enum ccid3_hc_tx_states state)
101 static char *ccid3_state_names[] = {
102 [TFRC_SSTATE_NO_SENT] = "NO_SENT",
103 [TFRC_SSTATE_NO_FBACK] = "NO_FBACK",
104 [TFRC_SSTATE_FBACK] = "FBACK",
105 [TFRC_SSTATE_TERM] = "TERM",
108 return ccid3_state_names[state];
112 static inline void ccid3_hc_tx_set_state(struct sock *sk,
113 enum ccid3_hc_tx_states state)
115 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
116 enum ccid3_hc_tx_states oldstate = hctx->ccid3hctx_state;
118 ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
119 dccp_role(sk), sk, ccid3_tx_state_name(oldstate),
120 ccid3_tx_state_name(state));
121 WARN_ON(state == oldstate);
122 hctx->ccid3hctx_state = state;
125 /* Calculate new t_ipi (inter packet interval) by t_ipi = s / X_inst */
126 static inline void ccid3_calc_new_t_ipi(struct ccid3_hc_tx_sock *hctx)
129 * If no feedback spec says t_ipi is 1 second (set elsewhere and then
130 * doubles after every no feedback timer (separate function)
132 if (hctx->ccid3hctx_state != TFRC_SSTATE_NO_FBACK)
133 hctx->ccid3hctx_t_ipi = usecs_div(hctx->ccid3hctx_s,
137 /* Calculate new delta by delta = min(t_ipi / 2, t_gran / 2) */
138 static inline void ccid3_calc_new_delta(struct ccid3_hc_tx_sock *hctx)
140 hctx->ccid3hctx_delta = min_t(u32, hctx->ccid3hctx_t_ipi / 2,
141 TFRC_OPSYS_HALF_TIME_GRAN);
147 * x_calc = calcX(s, R, p);
148 * X = max(min(X_calc, 2 * X_recv), s / t_mbi);
150 * If (now - tld >= R)
151 * X = max(min(2 * X, 2 * X_recv), s / R);
154 static void ccid3_hc_tx_update_x(struct sock *sk)
156 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
158 /* To avoid large error in calcX */
159 if (hctx->ccid3hctx_p >= TFRC_SMALLEST_P) {
160 hctx->ccid3hctx_x_calc = tfrc_calc_x(hctx->ccid3hctx_s,
163 hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_calc,
164 2 * hctx->ccid3hctx_x_recv),
166 TFRC_MAX_BACK_OFF_TIME));
170 dccp_timestamp(sk, &now);
171 if (timeval_delta(&now, &hctx->ccid3hctx_t_ld) >=
172 hctx->ccid3hctx_rtt) {
173 hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_recv,
174 hctx->ccid3hctx_x) * 2,
175 usecs_div(hctx->ccid3hctx_s,
176 hctx->ccid3hctx_rtt));
177 hctx->ccid3hctx_t_ld = now;
182 static void ccid3_hc_tx_no_feedback_timer(unsigned long data)
184 struct sock *sk = (struct sock *)data;
185 unsigned long next_tmout = 0;
186 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
189 if (sock_owned_by_user(sk)) {
190 /* Try again later. */
191 /* XXX: set some sensible MIB */
192 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
197 ccid3_pr_debug("%s, sk=%p, state=%s\n", dccp_role(sk), sk,
198 ccid3_tx_state_name(hctx->ccid3hctx_state));
200 switch (hctx->ccid3hctx_state) {
201 case TFRC_SSTATE_TERM:
203 case TFRC_SSTATE_NO_FBACK:
204 /* Halve send rate */
205 hctx->ccid3hctx_x /= 2;
206 if (hctx->ccid3hctx_x < (hctx->ccid3hctx_s /
207 TFRC_MAX_BACK_OFF_TIME))
208 hctx->ccid3hctx_x = (hctx->ccid3hctx_s /
209 TFRC_MAX_BACK_OFF_TIME);
211 ccid3_pr_debug("%s, sk=%p, state=%s, updated tx rate to %d "
214 ccid3_tx_state_name(hctx->ccid3hctx_state),
216 next_tmout = max_t(u32, 2 * usecs_div(hctx->ccid3hctx_s,
218 TFRC_INITIAL_TIMEOUT);
220 * FIXME - not sure above calculation is correct. See section
221 * 5 of CCID3 11 should adjust tx_t_ipi and double that to
225 case TFRC_SSTATE_FBACK:
227 * Check if IDLE since last timeout and recv rate is less than
230 if (!hctx->ccid3hctx_idle ||
231 (hctx->ccid3hctx_x_recv >=
232 4 * usecs_div(hctx->ccid3hctx_s, hctx->ccid3hctx_rtt))) {
233 ccid3_pr_debug("%s, sk=%p, state=%s, not idle\n",
235 ccid3_tx_state_name(hctx->ccid3hctx_state));
236 /* Halve sending rate */
238 /* If (X_calc > 2 * X_recv)
239 * X_recv = max(X_recv / 2, s / (2 * t_mbi));
241 * X_recv = X_calc / 4;
243 BUG_ON(hctx->ccid3hctx_p >= TFRC_SMALLEST_P &&
244 hctx->ccid3hctx_x_calc == 0);
246 /* check also if p is zero -> x_calc is infinity? */
247 if (hctx->ccid3hctx_p < TFRC_SMALLEST_P ||
248 hctx->ccid3hctx_x_calc > 2 * hctx->ccid3hctx_x_recv)
249 hctx->ccid3hctx_x_recv = max_t(u32, hctx->ccid3hctx_x_recv / 2,
250 hctx->ccid3hctx_s / (2 * TFRC_MAX_BACK_OFF_TIME));
252 hctx->ccid3hctx_x_recv = hctx->ccid3hctx_x_calc / 4;
254 /* Update sending rate */
255 ccid3_hc_tx_update_x(sk);
258 * Schedule no feedback timer to expire in
259 * max(4 * R, 2 * s / X)
261 next_tmout = max_t(u32, hctx->ccid3hctx_t_rto,
262 2 * usecs_div(hctx->ccid3hctx_s,
266 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
267 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
272 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
273 jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
274 hctx->ccid3hctx_idle = 1;
280 static int ccid3_hc_tx_send_packet(struct sock *sk,
281 struct sk_buff *skb, int len)
283 struct dccp_sock *dp = dccp_sk(sk);
284 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
285 struct dccp_tx_hist_entry *new_packet;
290 /* Check if pure ACK or Terminating*/
293 * XXX: We only call this function for DATA and DATAACK, on, these
294 * packets can have zero length, but why the comment about "pure ACK"?
296 if (hctx == NULL || len == 0 ||
297 hctx->ccid3hctx_state == TFRC_SSTATE_TERM)
300 /* See if last packet allocated was not sent */
301 new_packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
302 if (new_packet == NULL || new_packet->dccphtx_sent) {
303 new_packet = dccp_tx_hist_entry_new(ccid3_tx_hist,
307 if (new_packet == NULL) {
308 ccid3_pr_debug("%s, sk=%p, not enough mem to add "
309 "to history, send refused\n",
314 dccp_tx_hist_add_entry(&hctx->ccid3hctx_hist, new_packet);
317 dccp_timestamp(sk, &now);
319 switch (hctx->ccid3hctx_state) {
320 case TFRC_SSTATE_NO_SENT:
321 ccid3_pr_debug("%s, sk=%p, first packet(%llu)\n",
322 dccp_role(sk), sk, dp->dccps_gss);
324 hctx->ccid3hctx_no_feedback_timer.function = ccid3_hc_tx_no_feedback_timer;
325 hctx->ccid3hctx_no_feedback_timer.data = (unsigned long)sk;
326 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
327 jiffies + usecs_to_jiffies(TFRC_INITIAL_TIMEOUT));
328 hctx->ccid3hctx_last_win_count = 0;
329 hctx->ccid3hctx_t_last_win_count = now;
330 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_NO_FBACK);
331 hctx->ccid3hctx_t_ipi = TFRC_INITIAL_TIMEOUT;
333 /* Set nominal send time for initial packet */
334 hctx->ccid3hctx_t_nom = now;
335 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
336 hctx->ccid3hctx_t_ipi);
337 ccid3_calc_new_delta(hctx);
340 case TFRC_SSTATE_NO_FBACK:
341 case TFRC_SSTATE_FBACK:
342 delay = (timeval_delta(&now, &hctx->ccid3hctx_t_nom) -
343 hctx->ccid3hctx_delta);
344 ccid3_pr_debug("send_packet delay=%ld\n", delay);
346 /* divide by -1000 is to convert to ms and get sign right */
347 rc = delay > 0 ? delay : 0;
350 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
351 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
357 /* Can we send? if so add options and add to packet history */
359 dp->dccps_hc_tx_insert_options = 1;
360 new_packet->dccphtx_ccval =
361 DCCP_SKB_CB(skb)->dccpd_ccval =
362 hctx->ccid3hctx_last_win_count;
368 static void ccid3_hc_tx_packet_sent(struct sock *sk, int more, int len)
370 const struct dccp_sock *dp = dccp_sk(sk);
371 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
374 BUG_ON(hctx == NULL);
376 if (hctx->ccid3hctx_state == TFRC_SSTATE_TERM) {
377 ccid3_pr_debug("%s, sk=%p, while state is TFRC_SSTATE_TERM!\n",
382 dccp_timestamp(sk, &now);
384 /* check if we have sent a data packet */
386 unsigned long quarter_rtt;
387 struct dccp_tx_hist_entry *packet;
389 packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
390 if (packet == NULL) {
391 printk(KERN_CRIT "%s: packet doesn't exists in "
392 "history!\n", __FUNCTION__);
395 if (packet->dccphtx_sent) {
396 printk(KERN_CRIT "%s: no unsent packet in history!\n",
400 packet->dccphtx_tstamp = now;
401 packet->dccphtx_seqno = dp->dccps_gss;
403 * Check if win_count have changed
404 * Algorithm in "8.1. Window Counter Valuer" in
405 * draft-ietf-dccp-ccid3-11.txt
407 quarter_rtt = timeval_delta(&now, &hctx->ccid3hctx_t_last_win_count);
408 if (likely(hctx->ccid3hctx_rtt > 8))
409 quarter_rtt /= hctx->ccid3hctx_rtt / 4;
411 if (quarter_rtt > 0) {
412 hctx->ccid3hctx_t_last_win_count = now;
413 hctx->ccid3hctx_last_win_count = (hctx->ccid3hctx_last_win_count +
414 min_t(unsigned long, quarter_rtt, 5)) % 16;
415 ccid3_pr_debug("%s, sk=%p, window changed from "
418 packet->dccphtx_ccval,
419 hctx->ccid3hctx_last_win_count);
422 hctx->ccid3hctx_idle = 0;
423 packet->dccphtx_rtt = hctx->ccid3hctx_rtt;
424 packet->dccphtx_sent = 1;
426 ccid3_pr_debug("%s, sk=%p, seqno=%llu NOT inserted!\n",
427 dccp_role(sk), sk, dp->dccps_gss);
429 switch (hctx->ccid3hctx_state) {
430 case TFRC_SSTATE_NO_SENT:
431 /* if first wasn't pure ack */
433 printk(KERN_CRIT "%s: %s, First packet sent is noted "
434 "as a data packet\n",
435 __FUNCTION__, dccp_role(sk));
437 case TFRC_SSTATE_NO_FBACK:
438 case TFRC_SSTATE_FBACK:
440 hctx->ccid3hctx_t_nom = now;
441 ccid3_calc_new_t_ipi(hctx);
442 ccid3_calc_new_delta(hctx);
443 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
444 hctx->ccid3hctx_t_ipi);
448 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
449 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
455 static void ccid3_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
457 const struct dccp_sock *dp = dccp_sk(sk);
458 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
459 struct ccid3_options_received *opt_recv;
460 struct dccp_tx_hist_entry *packet;
462 unsigned long next_tmout;
471 if (hctx->ccid3hctx_state == TFRC_SSTATE_TERM) {
472 ccid3_pr_debug("%s, sk=%p, received a packet when "
473 "terminating!\n", dccp_role(sk), sk);
477 /* we are only interested in ACKs */
478 if (!(DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK ||
479 DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_DATAACK))
482 opt_recv = &hctx->ccid3hctx_options_received;
484 t_elapsed = dp->dccps_options_received.dccpor_elapsed_time * 10;
485 x_recv = opt_recv->ccid3or_receive_rate;
486 pinv = opt_recv->ccid3or_loss_event_rate;
488 switch (hctx->ccid3hctx_state) {
489 case TFRC_SSTATE_NO_SENT:
490 /* FIXME: what to do here? */
492 case TFRC_SSTATE_NO_FBACK:
493 case TFRC_SSTATE_FBACK:
494 /* Calculate new round trip sample by
495 * R_sample = (now - t_recvdata) - t_delay */
496 /* get t_recvdata from history */
497 packet = dccp_tx_hist_find_entry(&hctx->ccid3hctx_hist,
498 DCCP_SKB_CB(skb)->dccpd_ack_seq);
499 if (packet == NULL) {
500 ccid3_pr_debug("%s, sk=%p, seqno %llu(%s) does't "
501 "exist in history!\n",
503 DCCP_SKB_CB(skb)->dccpd_ack_seq,
504 dccp_packet_name(DCCP_SKB_CB(skb)->dccpd_type));
509 dccp_timestamp(sk, &now);
510 r_sample = timeval_delta(&now, &packet->dccphtx_tstamp);
511 if (unlikely(r_sample <= t_elapsed))
512 LIMIT_NETDEBUG(KERN_WARNING
513 "%s: r_sample=%uus, t_elapsed=%uus\n",
514 __FUNCTION__, r_sample, t_elapsed);
516 r_sample -= t_elapsed;
518 /* Update RTT estimate by
519 * If (No feedback recv)
522 * R = q * R + (1 - q) * R_sample;
524 * q is a constant, RFC 3448 recomments 0.9
526 if (hctx->ccid3hctx_state == TFRC_SSTATE_NO_FBACK) {
527 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_FBACK);
528 hctx->ccid3hctx_rtt = r_sample;
530 hctx->ccid3hctx_rtt = (hctx->ccid3hctx_rtt * 9) / 10 +
533 ccid3_pr_debug("%s, sk=%p, New RTT estimate=%uus, "
534 "r_sample=%us\n", dccp_role(sk), sk,
535 hctx->ccid3hctx_rtt, r_sample);
537 /* Update timeout interval */
538 hctx->ccid3hctx_t_rto = max_t(u32, 4 * hctx->ccid3hctx_rtt,
541 /* Update receive rate */
542 hctx->ccid3hctx_x_recv = x_recv;/* X_recv in bytes per sec */
544 /* Update loss event rate */
545 if (pinv == ~0 || pinv == 0)
546 hctx->ccid3hctx_p = 0;
548 hctx->ccid3hctx_p = 1000000 / pinv;
550 if (hctx->ccid3hctx_p < TFRC_SMALLEST_P) {
551 hctx->ccid3hctx_p = TFRC_SMALLEST_P;
552 ccid3_pr_debug("%s, sk=%p, Smallest p used!\n",
557 /* unschedule no feedback timer */
558 sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
560 /* Update sending rate */
561 ccid3_hc_tx_update_x(sk);
563 /* Update next send time */
564 timeval_sub_usecs(&hctx->ccid3hctx_t_nom,
565 hctx->ccid3hctx_t_ipi);
566 ccid3_calc_new_t_ipi(hctx);
567 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
568 hctx->ccid3hctx_t_ipi);
569 ccid3_calc_new_delta(hctx);
571 /* remove all packets older than the one acked from history */
572 dccp_tx_hist_purge_older(ccid3_tx_hist,
573 &hctx->ccid3hctx_hist, packet);
575 * As we have calculated new ipi, delta, t_nom it is possible that
576 * we now can send a packet, so wake up dccp_wait_for_ccids.
578 sk->sk_write_space(sk);
581 * Schedule no feedback timer to expire in
582 * max(4 * R, 2 * s / X)
584 next_tmout = max(hctx->ccid3hctx_t_rto,
585 2 * usecs_div(hctx->ccid3hctx_s,
588 ccid3_pr_debug("%s, sk=%p, Scheduled no feedback timer to "
589 "expire in %lu jiffies (%luus)\n",
591 usecs_to_jiffies(next_tmout), next_tmout);
593 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
594 jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
597 hctx->ccid3hctx_idle = 1;
600 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
601 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
607 static void ccid3_hc_tx_insert_options(struct sock *sk, struct sk_buff *skb)
609 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
611 if (hctx == NULL || !(sk->sk_state == DCCP_OPEN ||
612 sk->sk_state == DCCP_PARTOPEN))
615 DCCP_SKB_CB(skb)->dccpd_ccval = hctx->ccid3hctx_last_win_count;
618 static int ccid3_hc_tx_parse_options(struct sock *sk, unsigned char option,
619 unsigned char len, u16 idx,
620 unsigned char *value)
623 const struct dccp_sock *dp = dccp_sk(sk);
624 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
625 struct ccid3_options_received *opt_recv;
630 opt_recv = &hctx->ccid3hctx_options_received;
632 if (opt_recv->ccid3or_seqno != dp->dccps_gsr) {
633 opt_recv->ccid3or_seqno = dp->dccps_gsr;
634 opt_recv->ccid3or_loss_event_rate = ~0;
635 opt_recv->ccid3or_loss_intervals_idx = 0;
636 opt_recv->ccid3or_loss_intervals_len = 0;
637 opt_recv->ccid3or_receive_rate = 0;
641 case TFRC_OPT_LOSS_EVENT_RATE:
643 ccid3_pr_debug("%s, sk=%p, invalid len for "
644 "TFRC_OPT_LOSS_EVENT_RATE\n",
648 opt_recv->ccid3or_loss_event_rate = ntohl(*(u32 *)value);
649 ccid3_pr_debug("%s, sk=%p, LOSS_EVENT_RATE=%u\n",
651 opt_recv->ccid3or_loss_event_rate);
654 case TFRC_OPT_LOSS_INTERVALS:
655 opt_recv->ccid3or_loss_intervals_idx = idx;
656 opt_recv->ccid3or_loss_intervals_len = len;
657 ccid3_pr_debug("%s, sk=%p, LOSS_INTERVALS=(%u, %u)\n",
659 opt_recv->ccid3or_loss_intervals_idx,
660 opt_recv->ccid3or_loss_intervals_len);
662 case TFRC_OPT_RECEIVE_RATE:
664 ccid3_pr_debug("%s, sk=%p, invalid len for "
665 "TFRC_OPT_RECEIVE_RATE\n",
669 opt_recv->ccid3or_receive_rate = ntohl(*(u32 *)value);
670 ccid3_pr_debug("%s, sk=%p, RECEIVE_RATE=%u\n",
672 opt_recv->ccid3or_receive_rate);
680 static int ccid3_hc_tx_init(struct sock *sk)
682 struct dccp_sock *dp = dccp_sk(sk);
683 struct ccid3_hc_tx_sock *hctx;
685 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
687 dp->dccps_hc_tx_ccid_private = kmalloc(sizeof(*hctx), gfp_any());
688 if (dp->dccps_hc_tx_ccid_private == NULL)
691 hctx = ccid3_hc_tx_sk(sk);
692 memset(hctx, 0, sizeof(*hctx));
694 if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
695 dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
696 hctx->ccid3hctx_s = dp->dccps_packet_size;
698 hctx->ccid3hctx_s = TFRC_STD_PACKET_SIZE;
700 /* Set transmission rate to 1 packet per second */
701 hctx->ccid3hctx_x = hctx->ccid3hctx_s;
702 hctx->ccid3hctx_t_rto = USEC_PER_SEC;
703 hctx->ccid3hctx_state = TFRC_SSTATE_NO_SENT;
704 INIT_LIST_HEAD(&hctx->ccid3hctx_hist);
705 init_timer(&hctx->ccid3hctx_no_feedback_timer);
710 static void ccid3_hc_tx_exit(struct sock *sk)
712 struct dccp_sock *dp = dccp_sk(sk);
713 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
715 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
716 BUG_ON(hctx == NULL);
718 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_TERM);
719 sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
721 /* Empty packet history */
722 dccp_tx_hist_purge(ccid3_tx_hist, &hctx->ccid3hctx_hist);
724 kfree(dp->dccps_hc_tx_ccid_private);
725 dp->dccps_hc_tx_ccid_private = NULL;
729 * RX Half Connection methods
732 /* TFRC receiver states */
733 enum ccid3_hc_rx_states {
734 TFRC_RSTATE_NO_DATA = 1,
736 TFRC_RSTATE_TERM = 127,
740 static const char *ccid3_rx_state_name(enum ccid3_hc_rx_states state)
742 static char *ccid3_rx_state_names[] = {
743 [TFRC_RSTATE_NO_DATA] = "NO_DATA",
744 [TFRC_RSTATE_DATA] = "DATA",
745 [TFRC_RSTATE_TERM] = "TERM",
748 return ccid3_rx_state_names[state];
752 static inline void ccid3_hc_rx_set_state(struct sock *sk,
753 enum ccid3_hc_rx_states state)
755 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
756 enum ccid3_hc_rx_states oldstate = hcrx->ccid3hcrx_state;
758 ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
759 dccp_role(sk), sk, ccid3_rx_state_name(oldstate),
760 ccid3_rx_state_name(state));
761 WARN_ON(state == oldstate);
762 hcrx->ccid3hcrx_state = state;
765 static void ccid3_hc_rx_send_feedback(struct sock *sk)
767 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
768 struct dccp_sock *dp = dccp_sk(sk);
769 struct dccp_rx_hist_entry *packet;
772 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
774 dccp_timestamp(sk, &now);
776 switch (hcrx->ccid3hcrx_state) {
777 case TFRC_RSTATE_NO_DATA:
778 hcrx->ccid3hcrx_x_recv = 0;
780 case TFRC_RSTATE_DATA: {
781 const u32 delta = timeval_delta(&now,
782 &hcrx->ccid3hcrx_tstamp_last_feedback);
783 hcrx->ccid3hcrx_x_recv = usecs_div(hcrx->ccid3hcrx_bytes_recv,
788 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
789 __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
794 packet = dccp_rx_hist_find_data_packet(&hcrx->ccid3hcrx_hist);
795 if (packet == NULL) {
796 printk(KERN_CRIT "%s: %s, sk=%p, no data packet in history!\n",
797 __FUNCTION__, dccp_role(sk), sk);
802 hcrx->ccid3hcrx_tstamp_last_feedback = now;
803 hcrx->ccid3hcrx_last_counter = packet->dccphrx_ccval;
804 hcrx->ccid3hcrx_seqno_last_counter = packet->dccphrx_seqno;
805 hcrx->ccid3hcrx_bytes_recv = 0;
807 /* Convert to multiples of 10us */
808 hcrx->ccid3hcrx_elapsed_time =
809 timeval_delta(&now, &packet->dccphrx_tstamp) / 10;
810 if (hcrx->ccid3hcrx_p == 0)
811 hcrx->ccid3hcrx_pinv = ~0;
813 hcrx->ccid3hcrx_pinv = 1000000 / hcrx->ccid3hcrx_p;
814 dp->dccps_hc_rx_insert_options = 1;
818 static void ccid3_hc_rx_insert_options(struct sock *sk, struct sk_buff *skb)
820 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
823 if (hcrx == NULL || !(sk->sk_state == DCCP_OPEN ||
824 sk->sk_state == DCCP_PARTOPEN))
827 DCCP_SKB_CB(skb)->dccpd_ccval = hcrx->ccid3hcrx_last_counter;
829 if (dccp_packet_without_ack(skb))
832 if (hcrx->ccid3hcrx_elapsed_time != 0)
833 dccp_insert_option_elapsed_time(sk, skb,
834 hcrx->ccid3hcrx_elapsed_time);
835 dccp_insert_option_timestamp(sk, skb);
836 x_recv = htonl(hcrx->ccid3hcrx_x_recv);
837 pinv = htonl(hcrx->ccid3hcrx_pinv);
838 dccp_insert_option(sk, skb, TFRC_OPT_LOSS_EVENT_RATE,
839 &pinv, sizeof(pinv));
840 dccp_insert_option(sk, skb, TFRC_OPT_RECEIVE_RATE,
841 &x_recv, sizeof(x_recv));
844 /* calculate first loss interval
846 * returns estimated loss interval in usecs */
848 static u32 ccid3_hc_rx_calc_first_li(struct sock *sk)
850 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
851 struct dccp_rx_hist_entry *entry, *next, *tail = NULL;
852 u32 rtt, delta, x_recv, fval, p, tmp2;
853 struct timeval tstamp = { 0, };
859 list_for_each_entry_safe(entry, next, &hcrx->ccid3hcrx_hist,
861 if (dccp_rx_hist_entry_data_packet(entry)) {
866 tstamp = entry->dccphrx_tstamp;
867 win_count = entry->dccphrx_ccval;
871 interval = win_count - entry->dccphrx_ccval;
873 interval += TFRC_WIN_COUNT_LIMIT;
882 printk(KERN_CRIT "%s: %s, sk=%p, packet history contains no "
884 __FUNCTION__, dccp_role(sk), sk);
889 ccid3_pr_debug("%s, sk=%p, Could not find a win_count "
890 "interval > 0. Defaulting to 1\n",
895 rtt = timeval_delta(&tstamp, &tail->dccphrx_tstamp) * 4 / interval;
896 ccid3_pr_debug("%s, sk=%p, approximated RTT to %uus\n",
897 dccp_role(sk), sk, rtt);
901 dccp_timestamp(sk, &tstamp);
902 delta = timeval_delta(&tstamp, &hcrx->ccid3hcrx_tstamp_last_feedback);
903 x_recv = usecs_div(hcrx->ccid3hcrx_bytes_recv, delta);
905 tmp1 = (u64)x_recv * (u64)rtt;
906 do_div(tmp1,10000000);
908 fval = (hcrx->ccid3hcrx_s * 100000) / tmp2;
909 /* do not alter order above or you will get overflow on 32 bit */
910 p = tfrc_calc_x_reverse_lookup(fval);
911 ccid3_pr_debug("%s, sk=%p, receive rate=%u bytes/s, implied "
912 "loss rate=%u\n", dccp_role(sk), sk, x_recv, p);
920 static void ccid3_hc_rx_update_li(struct sock *sk, u64 seq_loss, u8 win_loss)
922 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
924 if (seq_loss != DCCP_MAX_SEQNO + 1 &&
925 list_empty(&hcrx->ccid3hcrx_li_hist)) {
926 struct dccp_li_hist_entry *li_tail;
928 li_tail = dccp_li_hist_interval_new(ccid3_li_hist,
929 &hcrx->ccid3hcrx_li_hist,
933 li_tail->dccplih_interval = ccid3_hc_rx_calc_first_li(sk);
935 /* FIXME: find end of interval */
938 static void ccid3_hc_rx_detect_loss(struct sock *sk)
940 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
942 const u64 seq_loss = dccp_rx_hist_detect_loss(&hcrx->ccid3hcrx_hist,
943 &hcrx->ccid3hcrx_li_hist,
946 ccid3_hc_rx_update_li(sk, seq_loss, win_loss);
949 static void ccid3_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
951 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
952 const struct dccp_options_received *opt_recv;
953 struct dccp_rx_hist_entry *packet;
956 u32 p_prev, r_sample, t_elapsed;
962 BUG_ON(!(hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA ||
963 hcrx->ccid3hcrx_state == TFRC_RSTATE_DATA));
965 opt_recv = &dccp_sk(sk)->dccps_options_received;
967 switch (DCCP_SKB_CB(skb)->dccpd_type) {
969 if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
971 case DCCP_PKT_DATAACK:
972 if (opt_recv->dccpor_timestamp_echo == 0)
974 p_prev = hcrx->ccid3hcrx_rtt;
975 dccp_timestamp(sk, &now);
976 timeval_sub_usecs(&now, opt_recv->dccpor_timestamp_echo * 10);
977 r_sample = timeval_usecs(&now);
978 t_elapsed = opt_recv->dccpor_elapsed_time * 10;
980 if (unlikely(r_sample <= t_elapsed))
981 LIMIT_NETDEBUG(KERN_WARNING
982 "%s: r_sample=%uus, t_elapsed=%uus\n",
983 __FUNCTION__, r_sample, t_elapsed);
985 r_sample -= t_elapsed;
987 if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
988 hcrx->ccid3hcrx_rtt = r_sample;
990 hcrx->ccid3hcrx_rtt = (hcrx->ccid3hcrx_rtt * 9) / 10 +
993 if (p_prev != hcrx->ccid3hcrx_rtt)
994 ccid3_pr_debug("%s, New RTT=%luus, elapsed time=%u\n",
995 dccp_role(sk), hcrx->ccid3hcrx_rtt,
996 opt_recv->dccpor_elapsed_time);
1001 ccid3_pr_debug("%s, sk=%p, not DATA/DATAACK/ACK packet(%s)\n",
1003 dccp_packet_name(DCCP_SKB_CB(skb)->dccpd_type));
1007 packet = dccp_rx_hist_entry_new(ccid3_rx_hist, sk, opt_recv->dccpor_ndp,
1009 if (packet == NULL) {
1010 ccid3_pr_debug("%s, sk=%p, Not enough mem to add rx packet "
1011 "to history (consider it lost)!",
1016 win_count = packet->dccphrx_ccval;
1018 ins = dccp_rx_hist_add_packet(ccid3_rx_hist, &hcrx->ccid3hcrx_hist,
1019 &hcrx->ccid3hcrx_li_hist, packet);
1021 if (DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK)
1024 switch (hcrx->ccid3hcrx_state) {
1025 case TFRC_RSTATE_NO_DATA:
1026 ccid3_pr_debug("%s, sk=%p(%s), skb=%p, sending initial "
1029 dccp_state_name(sk->sk_state), skb);
1030 ccid3_hc_rx_send_feedback(sk);
1031 ccid3_hc_rx_set_state(sk, TFRC_RSTATE_DATA);
1033 case TFRC_RSTATE_DATA:
1034 hcrx->ccid3hcrx_bytes_recv += skb->len -
1035 dccp_hdr(skb)->dccph_doff * 4;
1039 dccp_timestamp(sk, &now);
1040 if (timeval_delta(&now, &hcrx->ccid3hcrx_tstamp_last_ack) >=
1041 hcrx->ccid3hcrx_rtt) {
1042 hcrx->ccid3hcrx_tstamp_last_ack = now;
1043 ccid3_hc_rx_send_feedback(sk);
1047 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
1048 __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
1053 /* Dealing with packet loss */
1054 ccid3_pr_debug("%s, sk=%p(%s), data loss! Reacting...\n",
1055 dccp_role(sk), sk, dccp_state_name(sk->sk_state));
1057 ccid3_hc_rx_detect_loss(sk);
1058 p_prev = hcrx->ccid3hcrx_p;
1060 /* Calculate loss event rate */
1061 if (!list_empty(&hcrx->ccid3hcrx_li_hist))
1062 /* Scaling up by 1000000 as fixed decimal */
1063 hcrx->ccid3hcrx_p = 1000000 / dccp_li_hist_calc_i_mean(&hcrx->ccid3hcrx_li_hist);
1065 if (hcrx->ccid3hcrx_p > p_prev) {
1066 ccid3_hc_rx_send_feedback(sk);
1071 static int ccid3_hc_rx_init(struct sock *sk)
1073 struct dccp_sock *dp = dccp_sk(sk);
1074 struct ccid3_hc_rx_sock *hcrx;
1076 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
1078 dp->dccps_hc_rx_ccid_private = kmalloc(sizeof(*hcrx), gfp_any());
1079 if (dp->dccps_hc_rx_ccid_private == NULL)
1082 hcrx = ccid3_hc_rx_sk(sk);
1083 memset(hcrx, 0, sizeof(*hcrx));
1085 if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
1086 dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
1087 hcrx->ccid3hcrx_s = dp->dccps_packet_size;
1089 hcrx->ccid3hcrx_s = TFRC_STD_PACKET_SIZE;
1091 hcrx->ccid3hcrx_state = TFRC_RSTATE_NO_DATA;
1092 INIT_LIST_HEAD(&hcrx->ccid3hcrx_hist);
1093 INIT_LIST_HEAD(&hcrx->ccid3hcrx_li_hist);
1094 dccp_timestamp(sk, &hcrx->ccid3hcrx_tstamp_last_ack);
1095 hcrx->ccid3hcrx_tstamp_last_feedback = hcrx->ccid3hcrx_tstamp_last_ack;
1096 hcrx->ccid3hcrx_rtt = 5000; /* XXX 5ms for now... */
1100 static void ccid3_hc_rx_exit(struct sock *sk)
1102 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
1103 struct dccp_sock *dp = dccp_sk(sk);
1105 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
1110 ccid3_hc_rx_set_state(sk, TFRC_RSTATE_TERM);
1112 /* Empty packet history */
1113 dccp_rx_hist_purge(ccid3_rx_hist, &hcrx->ccid3hcrx_hist);
1115 /* Empty loss interval history */
1116 dccp_li_hist_purge(ccid3_li_hist, &hcrx->ccid3hcrx_li_hist);
1118 kfree(dp->dccps_hc_rx_ccid_private);
1119 dp->dccps_hc_rx_ccid_private = NULL;
1122 static void ccid3_hc_rx_get_info(struct sock *sk, struct tcp_info *info)
1124 const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
1129 info->tcpi_ca_state = hcrx->ccid3hcrx_state;
1130 info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1131 info->tcpi_rcv_rtt = hcrx->ccid3hcrx_rtt;
1134 static void ccid3_hc_tx_get_info(struct sock *sk, struct tcp_info *info)
1136 const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
1141 info->tcpi_rto = hctx->ccid3hctx_t_rto;
1142 info->tcpi_rtt = hctx->ccid3hctx_rtt;
1145 static struct ccid ccid3 = {
1147 .ccid_name = "ccid3",
1148 .ccid_owner = THIS_MODULE,
1149 .ccid_init = ccid3_init,
1150 .ccid_exit = ccid3_exit,
1151 .ccid_hc_tx_init = ccid3_hc_tx_init,
1152 .ccid_hc_tx_exit = ccid3_hc_tx_exit,
1153 .ccid_hc_tx_send_packet = ccid3_hc_tx_send_packet,
1154 .ccid_hc_tx_packet_sent = ccid3_hc_tx_packet_sent,
1155 .ccid_hc_tx_packet_recv = ccid3_hc_tx_packet_recv,
1156 .ccid_hc_tx_insert_options = ccid3_hc_tx_insert_options,
1157 .ccid_hc_tx_parse_options = ccid3_hc_tx_parse_options,
1158 .ccid_hc_rx_init = ccid3_hc_rx_init,
1159 .ccid_hc_rx_exit = ccid3_hc_rx_exit,
1160 .ccid_hc_rx_insert_options = ccid3_hc_rx_insert_options,
1161 .ccid_hc_rx_packet_recv = ccid3_hc_rx_packet_recv,
1162 .ccid_hc_rx_get_info = ccid3_hc_rx_get_info,
1163 .ccid_hc_tx_get_info = ccid3_hc_tx_get_info,
1166 module_param(ccid3_debug, int, 0444);
1167 MODULE_PARM_DESC(ccid3_debug, "Enable debug messages");
1169 static __init int ccid3_module_init(void)
1173 ccid3_rx_hist = dccp_rx_hist_new("ccid3");
1174 if (ccid3_rx_hist == NULL)
1177 ccid3_tx_hist = dccp_tx_hist_new("ccid3");
1178 if (ccid3_tx_hist == NULL)
1181 ccid3_li_hist = dccp_li_hist_new("ccid3");
1182 if (ccid3_li_hist == NULL)
1185 rc = ccid_register(&ccid3);
1187 goto out_free_loss_interval_history;
1191 out_free_loss_interval_history:
1192 dccp_li_hist_delete(ccid3_li_hist);
1193 ccid3_li_hist = NULL;
1195 dccp_tx_hist_delete(ccid3_tx_hist);
1196 ccid3_tx_hist = NULL;
1198 dccp_rx_hist_delete(ccid3_rx_hist);
1199 ccid3_rx_hist = NULL;
1202 module_init(ccid3_module_init);
1204 static __exit void ccid3_module_exit(void)
1206 #ifdef CONFIG_IP_DCCP_UNLOAD_HACK
1208 * Hack to use while developing, so that we get rid of the control
1209 * sock, that is what keeps a refcount on dccp.ko -acme
1211 extern void dccp_ctl_sock_exit(void);
1213 dccp_ctl_sock_exit();
1215 ccid_unregister(&ccid3);
1217 if (ccid3_tx_hist != NULL) {
1218 dccp_tx_hist_delete(ccid3_tx_hist);
1219 ccid3_tx_hist = NULL;
1221 if (ccid3_rx_hist != NULL) {
1222 dccp_rx_hist_delete(ccid3_rx_hist);
1223 ccid3_rx_hist = NULL;
1225 if (ccid3_li_hist != NULL) {
1226 dccp_li_hist_delete(ccid3_li_hist);
1227 ccid3_li_hist = NULL;
1230 module_exit(ccid3_module_exit);
1232 MODULE_AUTHOR("Ian McDonald <iam4@cs.waikato.ac.nz>, "
1233 "Arnaldo Carvalho de Melo <acme@ghostprotocols.net>");
1234 MODULE_DESCRIPTION("DCCP TFRC CCID3 CCID");
1235 MODULE_LICENSE("GPL");
1236 MODULE_ALIAS("net-dccp-ccid-3");