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 with 10 here is to avoid 32 bit overflow when a is big.
48 static inline u32 usecs_div(const u32 a, const u32 b)
50 const u32 tmp = a * (USEC_PER_SEC / 10);
51 return b > 20 ? tmp / (b / 10) : tmp;
54 static int ccid3_debug;
57 #define ccid3_pr_debug(format, a...) \
58 do { if (ccid3_debug) \
59 printk(KERN_DEBUG "%s: " format, __FUNCTION__, ##a); \
62 #define ccid3_pr_debug(format, a...)
65 static struct dccp_tx_hist *ccid3_tx_hist;
66 static struct dccp_rx_hist *ccid3_rx_hist;
67 static struct dccp_li_hist *ccid3_li_hist;
69 static int ccid3_init(struct sock *sk)
71 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
75 static void ccid3_exit(struct sock *sk)
77 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
80 /* TFRC sender states */
81 enum ccid3_hc_tx_states {
82 TFRC_SSTATE_NO_SENT = 1,
89 static const char *ccid3_tx_state_name(enum ccid3_hc_tx_states state)
91 static char *ccid3_state_names[] = {
92 [TFRC_SSTATE_NO_SENT] = "NO_SENT",
93 [TFRC_SSTATE_NO_FBACK] = "NO_FBACK",
94 [TFRC_SSTATE_FBACK] = "FBACK",
95 [TFRC_SSTATE_TERM] = "TERM",
98 return ccid3_state_names[state];
102 static inline void ccid3_hc_tx_set_state(struct sock *sk,
103 enum ccid3_hc_tx_states state)
105 struct dccp_sock *dp = dccp_sk(sk);
106 struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
107 enum ccid3_hc_tx_states oldstate = hctx->ccid3hctx_state;
109 ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
110 dccp_role(sk), sk, ccid3_tx_state_name(oldstate),
111 ccid3_tx_state_name(state));
112 WARN_ON(state == oldstate);
113 hctx->ccid3hctx_state = state;
116 /* Calculate new t_ipi (inter packet interval) by t_ipi = s / X_inst */
117 static inline void ccid3_calc_new_t_ipi(struct ccid3_hc_tx_sock *hctx)
120 * If no feedback spec says t_ipi is 1 second (set elsewhere and then
121 * doubles after every no feedback timer (separate function)
123 if (hctx->ccid3hctx_state != TFRC_SSTATE_NO_FBACK)
124 hctx->ccid3hctx_t_ipi = usecs_div(hctx->ccid3hctx_s,
128 /* Calculate new delta by delta = min(t_ipi / 2, t_gran / 2) */
129 static inline void ccid3_calc_new_delta(struct ccid3_hc_tx_sock *hctx)
131 hctx->ccid3hctx_delta = min_t(u32, hctx->ccid3hctx_t_ipi / 2,
132 TFRC_OPSYS_HALF_TIME_GRAN);
138 * x_calc = calcX(s, R, p);
139 * X = max(min(X_calc, 2 * X_recv), s / t_mbi);
141 * If (now - tld >= R)
142 * X = max(min(2 * X, 2 * X_recv), s / R);
145 static void ccid3_hc_tx_update_x(struct sock *sk)
147 struct dccp_sock *dp = dccp_sk(sk);
148 struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
150 /* To avoid large error in calcX */
151 if (hctx->ccid3hctx_p >= TFRC_SMALLEST_P) {
152 hctx->ccid3hctx_x_calc = tfrc_calc_x(hctx->ccid3hctx_s,
155 hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_calc,
156 2 * hctx->ccid3hctx_x_recv),
158 TFRC_MAX_BACK_OFF_TIME));
162 do_gettimeofday(&now);
163 if (timeval_delta(&now, &hctx->ccid3hctx_t_ld) >=
164 hctx->ccid3hctx_rtt) {
165 hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_recv,
166 hctx->ccid3hctx_x) * 2,
167 usecs_div(hctx->ccid3hctx_s,
168 hctx->ccid3hctx_rtt));
169 hctx->ccid3hctx_t_ld = now;
174 static void ccid3_hc_tx_no_feedback_timer(unsigned long data)
176 struct sock *sk = (struct sock *)data;
177 struct dccp_sock *dp = dccp_sk(sk);
178 unsigned long next_tmout = 0;
179 struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
182 if (sock_owned_by_user(sk)) {
183 /* Try again later. */
184 /* XXX: set some sensible MIB */
185 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
190 ccid3_pr_debug("%s, sk=%p, state=%s\n", dccp_role(sk), sk,
191 ccid3_tx_state_name(hctx->ccid3hctx_state));
193 switch (hctx->ccid3hctx_state) {
194 case TFRC_SSTATE_TERM:
196 case TFRC_SSTATE_NO_FBACK:
197 /* Halve send rate */
198 hctx->ccid3hctx_x /= 2;
199 if (hctx->ccid3hctx_x < (hctx->ccid3hctx_s /
200 TFRC_MAX_BACK_OFF_TIME))
201 hctx->ccid3hctx_x = (hctx->ccid3hctx_s /
202 TFRC_MAX_BACK_OFF_TIME);
204 ccid3_pr_debug("%s, sk=%p, state=%s, updated tx rate to %d "
207 ccid3_tx_state_name(hctx->ccid3hctx_state),
209 next_tmout = max_t(u32, 2 * usecs_div(hctx->ccid3hctx_s,
211 TFRC_INITIAL_TIMEOUT);
213 * FIXME - not sure above calculation is correct. See section
214 * 5 of CCID3 11 should adjust tx_t_ipi and double that to
218 case TFRC_SSTATE_FBACK:
220 * Check if IDLE since last timeout and recv rate is less than
223 if (!hctx->ccid3hctx_idle ||
224 (hctx->ccid3hctx_x_recv >=
225 4 * usecs_div(hctx->ccid3hctx_s, hctx->ccid3hctx_rtt))) {
226 ccid3_pr_debug("%s, sk=%p, state=%s, not idle\n",
228 ccid3_tx_state_name(hctx->ccid3hctx_state));
229 /* Halve sending rate */
231 /* If (X_calc > 2 * X_recv)
232 * X_recv = max(X_recv / 2, s / (2 * t_mbi));
234 * X_recv = X_calc / 4;
236 BUG_ON(hctx->ccid3hctx_p >= TFRC_SMALLEST_P &&
237 hctx->ccid3hctx_x_calc == 0);
239 /* check also if p is zero -> x_calc is infinity? */
240 if (hctx->ccid3hctx_p < TFRC_SMALLEST_P ||
241 hctx->ccid3hctx_x_calc > 2 * hctx->ccid3hctx_x_recv)
242 hctx->ccid3hctx_x_recv = max_t(u32, hctx->ccid3hctx_x_recv / 2,
243 hctx->ccid3hctx_s / (2 * TFRC_MAX_BACK_OFF_TIME));
245 hctx->ccid3hctx_x_recv = hctx->ccid3hctx_x_calc / 4;
247 /* Update sending rate */
248 ccid3_hc_tx_update_x(sk);
251 * Schedule no feedback timer to expire in
252 * max(4 * R, 2 * s / X)
254 next_tmout = max_t(u32, hctx->ccid3hctx_t_rto,
255 2 * usecs_div(hctx->ccid3hctx_s,
259 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
260 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
265 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
266 jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
267 hctx->ccid3hctx_idle = 1;
273 static int ccid3_hc_tx_send_packet(struct sock *sk,
274 struct sk_buff *skb, int len)
276 struct dccp_sock *dp = dccp_sk(sk);
277 struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
278 struct dccp_tx_hist_entry *new_packet;
283 /* Check if pure ACK or Terminating*/
286 * XXX: We only call this function for DATA and DATAACK, on, these
287 * packets can have zero length, but why the comment about "pure ACK"?
289 if (hctx == NULL || len == 0 ||
290 hctx->ccid3hctx_state == TFRC_SSTATE_TERM)
293 /* See if last packet allocated was not sent */
294 new_packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
295 if (new_packet == NULL || new_packet->dccphtx_sent) {
296 new_packet = dccp_tx_hist_entry_new(ccid3_tx_hist,
300 if (new_packet == NULL) {
301 ccid3_pr_debug("%s, sk=%p, not enough mem to add "
302 "to history, send refused\n",
307 dccp_tx_hist_add_entry(&hctx->ccid3hctx_hist, new_packet);
310 do_gettimeofday(&now);
312 switch (hctx->ccid3hctx_state) {
313 case TFRC_SSTATE_NO_SENT:
314 ccid3_pr_debug("%s, sk=%p, first packet(%llu)\n",
315 dccp_role(sk), sk, dp->dccps_gss);
317 hctx->ccid3hctx_no_feedback_timer.function = ccid3_hc_tx_no_feedback_timer;
318 hctx->ccid3hctx_no_feedback_timer.data = (unsigned long)sk;
319 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
320 jiffies + usecs_to_jiffies(TFRC_INITIAL_TIMEOUT));
321 hctx->ccid3hctx_last_win_count = 0;
322 hctx->ccid3hctx_t_last_win_count = now;
323 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_NO_FBACK);
324 hctx->ccid3hctx_t_ipi = TFRC_INITIAL_TIMEOUT;
326 /* Set nominal send time for initial packet */
327 hctx->ccid3hctx_t_nom = now;
328 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
329 hctx->ccid3hctx_t_ipi);
330 ccid3_calc_new_delta(hctx);
333 case TFRC_SSTATE_NO_FBACK:
334 case TFRC_SSTATE_FBACK:
335 delay = (timeval_delta(&now, &hctx->ccid3hctx_t_nom) -
336 hctx->ccid3hctx_delta);
337 ccid3_pr_debug("send_packet delay=%ld\n", delay);
339 /* divide by -1000 is to convert to ms and get sign right */
340 rc = delay > 0 ? delay : 0;
343 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
344 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
350 /* Can we send? if so add options and add to packet history */
352 new_packet->dccphtx_ccval =
353 DCCP_SKB_CB(skb)->dccpd_ccval =
354 hctx->ccid3hctx_last_win_count;
359 static void ccid3_hc_tx_packet_sent(struct sock *sk, int more, int len)
361 struct dccp_sock *dp = dccp_sk(sk);
362 struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
365 BUG_ON(hctx == NULL);
367 if (hctx->ccid3hctx_state == TFRC_SSTATE_TERM) {
368 ccid3_pr_debug("%s, sk=%p, while state is TFRC_SSTATE_TERM!\n",
373 do_gettimeofday(&now);
375 /* check if we have sent a data packet */
377 unsigned long quarter_rtt;
378 struct dccp_tx_hist_entry *packet;
380 packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
381 if (packet == NULL) {
382 printk(KERN_CRIT "%s: packet doesn't exists in "
383 "history!\n", __FUNCTION__);
386 if (packet->dccphtx_sent) {
387 printk(KERN_CRIT "%s: no unsent packet in history!\n",
391 packet->dccphtx_tstamp = now;
392 packet->dccphtx_seqno = dp->dccps_gss;
394 * Check if win_count have changed
395 * Algorithm in "8.1. Window Counter Valuer" in
396 * draft-ietf-dccp-ccid3-11.txt
398 quarter_rtt = timeval_delta(&now, &hctx->ccid3hctx_t_last_win_count);
399 if (likely(hctx->ccid3hctx_rtt > 8))
400 quarter_rtt /= hctx->ccid3hctx_rtt / 4;
402 if (quarter_rtt > 0) {
403 hctx->ccid3hctx_t_last_win_count = now;
404 hctx->ccid3hctx_last_win_count = (hctx->ccid3hctx_last_win_count +
405 min_t(unsigned long, quarter_rtt, 5)) % 16;
406 ccid3_pr_debug("%s, sk=%p, window changed from "
409 packet->dccphtx_ccval,
410 hctx->ccid3hctx_last_win_count);
413 hctx->ccid3hctx_idle = 0;
414 packet->dccphtx_rtt = hctx->ccid3hctx_rtt;
415 packet->dccphtx_sent = 1;
417 ccid3_pr_debug("%s, sk=%p, seqno=%llu NOT inserted!\n",
418 dccp_role(sk), sk, dp->dccps_gss);
420 switch (hctx->ccid3hctx_state) {
421 case TFRC_SSTATE_NO_SENT:
422 /* if first wasn't pure ack */
424 printk(KERN_CRIT "%s: %s, First packet sent is noted "
425 "as a data packet\n",
426 __FUNCTION__, dccp_role(sk));
428 case TFRC_SSTATE_NO_FBACK:
429 case TFRC_SSTATE_FBACK:
431 hctx->ccid3hctx_t_nom = now;
432 ccid3_calc_new_t_ipi(hctx);
433 ccid3_calc_new_delta(hctx);
434 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
435 hctx->ccid3hctx_t_ipi);
439 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
440 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
446 static void ccid3_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
448 struct dccp_sock *dp = dccp_sk(sk);
449 struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
450 struct ccid3_options_received *opt_recv;
451 struct dccp_tx_hist_entry *packet;
452 unsigned long next_tmout;
461 if (hctx->ccid3hctx_state == TFRC_SSTATE_TERM) {
462 ccid3_pr_debug("%s, sk=%p, received a packet when "
463 "terminating!\n", dccp_role(sk), sk);
467 /* we are only interested in ACKs */
468 if (!(DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK ||
469 DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_DATAACK))
472 opt_recv = &hctx->ccid3hctx_options_received;
474 t_elapsed = dp->dccps_options_received.dccpor_elapsed_time;
475 x_recv = opt_recv->ccid3or_receive_rate;
476 pinv = opt_recv->ccid3or_loss_event_rate;
478 switch (hctx->ccid3hctx_state) {
479 case TFRC_SSTATE_NO_SENT:
480 /* FIXME: what to do here? */
482 case TFRC_SSTATE_NO_FBACK:
483 case TFRC_SSTATE_FBACK:
484 /* Calculate new round trip sample by
485 * R_sample = (now - t_recvdata) - t_delay */
486 /* get t_recvdata from history */
487 packet = dccp_tx_hist_find_entry(&hctx->ccid3hctx_hist,
488 DCCP_SKB_CB(skb)->dccpd_ack_seq);
489 if (packet == NULL) {
490 ccid3_pr_debug("%s, sk=%p, seqno %llu(%s) does't "
491 "exist in history!\n",
493 DCCP_SKB_CB(skb)->dccpd_ack_seq,
494 dccp_packet_name(DCCP_SKB_CB(skb)->dccpd_type));
499 r_sample = timeval_now_delta(&packet->dccphtx_tstamp);
501 // r_sample -= usecs_to_jiffies(t_elapsed * 10);
503 /* Update RTT estimate by
504 * If (No feedback recv)
507 * R = q * R + (1 - q) * R_sample;
509 * q is a constant, RFC 3448 recomments 0.9
511 if (hctx->ccid3hctx_state == TFRC_SSTATE_NO_FBACK) {
512 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_FBACK);
513 hctx->ccid3hctx_rtt = r_sample;
515 hctx->ccid3hctx_rtt = (hctx->ccid3hctx_rtt * 9) / 10 +
518 ccid3_pr_debug("%s, sk=%p, New RTT estimate=%uus, "
519 "r_sample=%us\n", dccp_role(sk), sk,
520 hctx->ccid3hctx_rtt, r_sample);
522 /* Update timeout interval */
523 hctx->ccid3hctx_t_rto = max_t(u32, 4 * hctx->ccid3hctx_rtt,
526 /* Update receive rate */
527 hctx->ccid3hctx_x_recv = x_recv;/* X_recv in bytes per sec */
529 /* Update loss event rate */
530 if (pinv == ~0 || pinv == 0)
531 hctx->ccid3hctx_p = 0;
533 hctx->ccid3hctx_p = 1000000 / pinv;
535 if (hctx->ccid3hctx_p < TFRC_SMALLEST_P) {
536 hctx->ccid3hctx_p = TFRC_SMALLEST_P;
537 ccid3_pr_debug("%s, sk=%p, Smallest p used!\n",
542 /* unschedule no feedback timer */
543 sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
545 /* Update sending rate */
546 ccid3_hc_tx_update_x(sk);
548 /* Update next send time */
549 timeval_sub_usecs(&hctx->ccid3hctx_t_nom,
550 hctx->ccid3hctx_t_ipi);
551 ccid3_calc_new_t_ipi(hctx);
552 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
553 hctx->ccid3hctx_t_ipi);
554 ccid3_calc_new_delta(hctx);
556 /* remove all packets older than the one acked from history */
557 dccp_tx_hist_purge_older(ccid3_tx_hist,
558 &hctx->ccid3hctx_hist, packet);
561 * Schedule no feedback timer to expire in
562 * max(4 * R, 2 * s / X)
564 next_tmout = max(hctx->ccid3hctx_t_rto,
565 2 * usecs_div(hctx->ccid3hctx_s,
568 ccid3_pr_debug("%s, sk=%p, Scheduled no feedback timer to "
569 "expire in %lu jiffies (%luus)\n",
571 usecs_to_jiffies(next_tmout), next_tmout);
573 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
574 jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
577 hctx->ccid3hctx_idle = 1;
580 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
581 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
587 static void ccid3_hc_tx_insert_options(struct sock *sk, struct sk_buff *skb)
589 const struct dccp_sock *dp = dccp_sk(sk);
590 struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
592 if (hctx == NULL || !(sk->sk_state == DCCP_OPEN ||
593 sk->sk_state == DCCP_PARTOPEN))
596 DCCP_SKB_CB(skb)->dccpd_ccval = hctx->ccid3hctx_last_win_count;
599 static int ccid3_hc_tx_parse_options(struct sock *sk, unsigned char option,
600 unsigned char len, u16 idx,
601 unsigned char *value)
604 struct dccp_sock *dp = dccp_sk(sk);
605 struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
606 struct ccid3_options_received *opt_recv;
611 opt_recv = &hctx->ccid3hctx_options_received;
613 if (opt_recv->ccid3or_seqno != dp->dccps_gsr) {
614 opt_recv->ccid3or_seqno = dp->dccps_gsr;
615 opt_recv->ccid3or_loss_event_rate = ~0;
616 opt_recv->ccid3or_loss_intervals_idx = 0;
617 opt_recv->ccid3or_loss_intervals_len = 0;
618 opt_recv->ccid3or_receive_rate = 0;
622 case TFRC_OPT_LOSS_EVENT_RATE:
624 ccid3_pr_debug("%s, sk=%p, invalid len for "
625 "TFRC_OPT_LOSS_EVENT_RATE\n",
629 opt_recv->ccid3or_loss_event_rate = ntohl(*(u32 *)value);
630 ccid3_pr_debug("%s, sk=%p, LOSS_EVENT_RATE=%u\n",
632 opt_recv->ccid3or_loss_event_rate);
635 case TFRC_OPT_LOSS_INTERVALS:
636 opt_recv->ccid3or_loss_intervals_idx = idx;
637 opt_recv->ccid3or_loss_intervals_len = len;
638 ccid3_pr_debug("%s, sk=%p, LOSS_INTERVALS=(%u, %u)\n",
640 opt_recv->ccid3or_loss_intervals_idx,
641 opt_recv->ccid3or_loss_intervals_len);
643 case TFRC_OPT_RECEIVE_RATE:
645 ccid3_pr_debug("%s, sk=%p, invalid len for "
646 "TFRC_OPT_RECEIVE_RATE\n",
650 opt_recv->ccid3or_receive_rate = ntohl(*(u32 *)value);
651 ccid3_pr_debug("%s, sk=%p, RECEIVE_RATE=%u\n",
653 opt_recv->ccid3or_receive_rate);
661 static int ccid3_hc_tx_init(struct sock *sk)
663 struct dccp_sock *dp = dccp_sk(sk);
664 struct ccid3_hc_tx_sock *hctx;
666 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
668 hctx = dp->dccps_hc_tx_ccid_private = kmalloc(sizeof(*hctx),
673 memset(hctx, 0, sizeof(*hctx));
675 if (dp->dccps_avg_packet_size >= TFRC_MIN_PACKET_SIZE &&
676 dp->dccps_avg_packet_size <= TFRC_MAX_PACKET_SIZE)
677 hctx->ccid3hctx_s = (u16)dp->dccps_avg_packet_size;
679 hctx->ccid3hctx_s = TFRC_STD_PACKET_SIZE;
681 /* Set transmission rate to 1 packet per second */
682 hctx->ccid3hctx_x = hctx->ccid3hctx_s;
683 hctx->ccid3hctx_t_rto = USEC_PER_SEC;
684 hctx->ccid3hctx_state = TFRC_SSTATE_NO_SENT;
685 INIT_LIST_HEAD(&hctx->ccid3hctx_hist);
686 init_timer(&hctx->ccid3hctx_no_feedback_timer);
691 static void ccid3_hc_tx_exit(struct sock *sk)
693 struct dccp_sock *dp = dccp_sk(sk);
694 struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
696 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
697 BUG_ON(hctx == NULL);
699 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_TERM);
700 sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
702 /* Empty packet history */
703 dccp_tx_hist_purge(ccid3_tx_hist, &hctx->ccid3hctx_hist);
705 kfree(dp->dccps_hc_tx_ccid_private);
706 dp->dccps_hc_tx_ccid_private = NULL;
710 * RX Half Connection methods
713 /* TFRC receiver states */
714 enum ccid3_hc_rx_states {
715 TFRC_RSTATE_NO_DATA = 1,
717 TFRC_RSTATE_TERM = 127,
721 static const char *ccid3_rx_state_name(enum ccid3_hc_rx_states state)
723 static char *ccid3_rx_state_names[] = {
724 [TFRC_RSTATE_NO_DATA] = "NO_DATA",
725 [TFRC_RSTATE_DATA] = "DATA",
726 [TFRC_RSTATE_TERM] = "TERM",
729 return ccid3_rx_state_names[state];
733 static inline void ccid3_hc_rx_set_state(struct sock *sk,
734 enum ccid3_hc_rx_states state)
736 struct dccp_sock *dp = dccp_sk(sk);
737 struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
738 enum ccid3_hc_rx_states oldstate = hcrx->ccid3hcrx_state;
740 ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
741 dccp_role(sk), sk, ccid3_rx_state_name(oldstate),
742 ccid3_rx_state_name(state));
743 WARN_ON(state == oldstate);
744 hcrx->ccid3hcrx_state = state;
747 static void ccid3_hc_rx_send_feedback(struct sock *sk)
749 struct dccp_sock *dp = dccp_sk(sk);
750 struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
751 struct dccp_rx_hist_entry *packet;
754 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
756 do_gettimeofday(&now);
758 switch (hcrx->ccid3hcrx_state) {
759 case TFRC_RSTATE_NO_DATA:
760 hcrx->ccid3hcrx_x_recv = 0;
762 case TFRC_RSTATE_DATA: {
763 const u32 delta = timeval_delta(&now,
764 &hcrx->ccid3hcrx_tstamp_last_feedback);
766 hcrx->ccid3hcrx_x_recv = (hcrx->ccid3hcrx_bytes_recv *
768 if (likely(delta > 1))
769 hcrx->ccid3hcrx_x_recv /= delta;
773 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
774 __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
779 packet = dccp_rx_hist_find_data_packet(&hcrx->ccid3hcrx_hist);
780 if (packet == NULL) {
781 printk(KERN_CRIT "%s: %s, sk=%p, no data packet in history!\n",
782 __FUNCTION__, dccp_role(sk), sk);
787 hcrx->ccid3hcrx_tstamp_last_feedback = now;
788 hcrx->ccid3hcrx_last_counter = packet->dccphrx_ccval;
789 hcrx->ccid3hcrx_seqno_last_counter = packet->dccphrx_seqno;
790 hcrx->ccid3hcrx_bytes_recv = 0;
792 /* Convert to multiples of 10us */
793 hcrx->ccid3hcrx_elapsed_time =
794 timeval_delta(&now, &packet->dccphrx_tstamp) / 10;
795 if (hcrx->ccid3hcrx_p == 0)
796 hcrx->ccid3hcrx_pinv = ~0;
798 hcrx->ccid3hcrx_pinv = 1000000 / hcrx->ccid3hcrx_p;
802 static void ccid3_hc_rx_insert_options(struct sock *sk, struct sk_buff *skb)
804 const struct dccp_sock *dp = dccp_sk(sk);
806 struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
808 if (hcrx == NULL || !(sk->sk_state == DCCP_OPEN ||
809 sk->sk_state == DCCP_PARTOPEN))
812 DCCP_SKB_CB(skb)->dccpd_ccval = hcrx->ccid3hcrx_last_counter;
814 if (dccp_packet_without_ack(skb))
817 if (hcrx->ccid3hcrx_elapsed_time != 0)
818 dccp_insert_option_elapsed_time(sk, skb,
819 hcrx->ccid3hcrx_elapsed_time);
820 dccp_insert_option_timestamp(sk, skb);
821 x_recv = htonl(hcrx->ccid3hcrx_x_recv);
822 pinv = htonl(hcrx->ccid3hcrx_pinv);
823 dccp_insert_option(sk, skb, TFRC_OPT_LOSS_EVENT_RATE,
824 &pinv, sizeof(pinv));
825 dccp_insert_option(sk, skb, TFRC_OPT_RECEIVE_RATE,
826 &x_recv, sizeof(x_recv));
829 /* calculate first loss interval
831 * returns estimated loss interval in usecs */
833 static u32 ccid3_hc_rx_calc_first_li(struct sock *sk)
835 struct dccp_sock *dp = dccp_sk(sk);
836 struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
837 struct dccp_rx_hist_entry *entry, *next, *tail = NULL;
838 u32 rtt, delta, x_recv, fval, p, tmp2;
839 struct timeval tstamp = { 0, };
845 list_for_each_entry_safe(entry, next, &hcrx->ccid3hcrx_hist,
847 if (dccp_rx_hist_entry_data_packet(entry)) {
852 tstamp = entry->dccphrx_tstamp;
853 win_count = entry->dccphrx_ccval;
857 interval = win_count - entry->dccphrx_ccval;
859 interval += TFRC_WIN_COUNT_LIMIT;
868 printk(KERN_CRIT "%s: %s, sk=%p, packet history contains no "
870 __FUNCTION__, dccp_role(sk), sk);
875 ccid3_pr_debug("%s, sk=%p, Could not find a win_count "
876 "interval > 0. Defaulting to 1\n",
881 rtt = timeval_delta(&tstamp, &tail->dccphrx_tstamp) * 4 / interval;
882 ccid3_pr_debug("%s, sk=%p, approximated RTT to %uus\n",
883 dccp_role(sk), sk, rtt);
887 delta = timeval_now_delta(&hcrx->ccid3hcrx_tstamp_last_feedback);
888 x_recv = hcrx->ccid3hcrx_bytes_recv * USEC_PER_SEC;
889 if (likely(delta > 1))
892 tmp1 = (u64)x_recv * (u64)rtt;
893 do_div(tmp1,10000000);
895 fval = (hcrx->ccid3hcrx_s * 100000) / tmp2;
896 /* do not alter order above or you will get overflow on 32 bit */
897 p = tfrc_calc_x_reverse_lookup(fval);
898 ccid3_pr_debug("%s, sk=%p, receive rate=%u bytes/s, implied "
899 "loss rate=%u\n", dccp_role(sk), sk, x_recv, p);
907 static void ccid3_hc_rx_update_li(struct sock *sk, u64 seq_loss, u8 win_loss)
909 struct dccp_sock *dp = dccp_sk(sk);
910 struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
912 if (seq_loss != DCCP_MAX_SEQNO + 1 &&
913 list_empty(&hcrx->ccid3hcrx_li_hist)) {
914 struct dccp_li_hist_entry *li_tail;
916 li_tail = dccp_li_hist_interval_new(ccid3_li_hist,
917 &hcrx->ccid3hcrx_li_hist,
921 li_tail->dccplih_interval = ccid3_hc_rx_calc_first_li(sk);
923 /* FIXME: find end of interval */
926 static void ccid3_hc_rx_detect_loss(struct sock *sk)
928 struct dccp_sock *dp = dccp_sk(sk);
929 struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
930 struct dccp_rx_hist_entry *entry, *next, *packet;
931 struct dccp_rx_hist_entry *a_loss = NULL;
932 struct dccp_rx_hist_entry *b_loss = NULL;
933 u64 seq_loss = DCCP_MAX_SEQNO + 1;
935 u8 num_later = TFRC_RECV_NUM_LATE_LOSS;
937 list_for_each_entry_safe(entry, next, &hcrx->ccid3hcrx_hist,
939 if (num_later == 0) {
942 } else if (dccp_rx_hist_entry_data_packet(entry))
951 list_for_each_entry_safe_continue(entry, next, &hcrx->ccid3hcrx_hist,
953 if (num_later == 0) {
956 } else if (dccp_rx_hist_entry_data_packet(entry))
960 if (a_loss == NULL) {
961 if (list_empty(&hcrx->ccid3hcrx_li_hist)) {
962 /* no loss event have occured yet */
963 LIMIT_NETDEBUG("%s: TODO: find a lost data packet by "
964 "comparing to initial seqno\n",
968 pr_info("%s: %s, sk=%p, ERROR! Less than 4 data "
969 "packets in history",
970 __FUNCTION__, dccp_role(sk), sk);
975 /* Locate a lost data packet */
976 entry = packet = b_loss;
977 list_for_each_entry_safe_continue(entry, next, &hcrx->ccid3hcrx_hist,
979 u64 delta = dccp_delta_seqno(entry->dccphrx_seqno,
980 packet->dccphrx_seqno);
983 if (dccp_rx_hist_entry_data_packet(packet))
986 * FIXME: check this, probably this % usage is because
987 * in earlier drafts the ndp count was just 8 bits
988 * long, but now it cam be up to 24 bits long.
991 if (delta % DCCP_NDP_LIMIT !=
992 (packet->dccphrx_ndp -
993 entry->dccphrx_ndp) % DCCP_NDP_LIMIT)
996 packet->dccphrx_ndp - entry->dccphrx_ndp) {
997 seq_loss = entry->dccphrx_seqno;
998 dccp_inc_seqno(&seq_loss);
1002 if (packet == a_loss)
1006 if (seq_loss != DCCP_MAX_SEQNO + 1)
1007 win_loss = a_loss->dccphrx_ccval;
1010 ccid3_hc_rx_update_li(sk, seq_loss, win_loss);
1013 static void ccid3_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
1015 struct dccp_sock *dp = dccp_sk(sk);
1016 struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
1017 const struct dccp_options_received *opt_recv;
1018 struct dccp_rx_hist_entry *packet;
1027 BUG_ON(!(hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA ||
1028 hcrx->ccid3hcrx_state == TFRC_RSTATE_DATA));
1030 opt_recv = &dp->dccps_options_received;
1032 switch (DCCP_SKB_CB(skb)->dccpd_type) {
1034 if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
1036 case DCCP_PKT_DATAACK:
1037 if (opt_recv->dccpor_timestamp_echo == 0)
1039 p_prev = hcrx->ccid3hcrx_rtt;
1040 do_gettimeofday(&now);
1041 hcrx->ccid3hcrx_rtt = timeval_usecs(&now) -
1042 (opt_recv->dccpor_timestamp_echo -
1043 opt_recv->dccpor_elapsed_time) * 10;
1044 if (p_prev != hcrx->ccid3hcrx_rtt)
1045 ccid3_pr_debug("%s, New RTT=%luus, elapsed time=%u\n",
1046 dccp_role(sk), hcrx->ccid3hcrx_rtt,
1047 opt_recv->dccpor_elapsed_time);
1052 ccid3_pr_debug("%s, sk=%p, not DATA/DATAACK/ACK packet(%s)\n",
1054 dccp_packet_name(DCCP_SKB_CB(skb)->dccpd_type));
1058 packet = dccp_rx_hist_entry_new(ccid3_rx_hist, opt_recv->dccpor_ndp,
1060 if (packet == NULL) {
1061 ccid3_pr_debug("%s, sk=%p, Not enough mem to add rx packet "
1062 "to history (consider it lost)!",
1067 win_count = packet->dccphrx_ccval;
1069 ins = dccp_rx_hist_add_packet(ccid3_rx_hist, &hcrx->ccid3hcrx_hist,
1070 &hcrx->ccid3hcrx_li_hist, packet);
1072 if (DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK)
1075 switch (hcrx->ccid3hcrx_state) {
1076 case TFRC_RSTATE_NO_DATA:
1077 ccid3_pr_debug("%s, sk=%p(%s), skb=%p, sending initial "
1080 dccp_state_name(sk->sk_state), skb);
1081 ccid3_hc_rx_send_feedback(sk);
1082 ccid3_hc_rx_set_state(sk, TFRC_RSTATE_DATA);
1084 case TFRC_RSTATE_DATA:
1085 hcrx->ccid3hcrx_bytes_recv += skb->len -
1086 dccp_hdr(skb)->dccph_doff * 4;
1090 do_gettimeofday(&now);
1091 if (timeval_delta(&now, &hcrx->ccid3hcrx_tstamp_last_ack) >=
1092 hcrx->ccid3hcrx_rtt) {
1093 hcrx->ccid3hcrx_tstamp_last_ack = now;
1094 ccid3_hc_rx_send_feedback(sk);
1098 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
1099 __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
1104 /* Dealing with packet loss */
1105 ccid3_pr_debug("%s, sk=%p(%s), data loss! Reacting...\n",
1106 dccp_role(sk), sk, dccp_state_name(sk->sk_state));
1108 ccid3_hc_rx_detect_loss(sk);
1109 p_prev = hcrx->ccid3hcrx_p;
1111 /* Calculate loss event rate */
1112 if (!list_empty(&hcrx->ccid3hcrx_li_hist))
1113 /* Scaling up by 1000000 as fixed decimal */
1114 hcrx->ccid3hcrx_p = 1000000 / dccp_li_hist_calc_i_mean(&hcrx->ccid3hcrx_li_hist);
1116 if (hcrx->ccid3hcrx_p > p_prev) {
1117 ccid3_hc_rx_send_feedback(sk);
1122 static int ccid3_hc_rx_init(struct sock *sk)
1124 struct dccp_sock *dp = dccp_sk(sk);
1125 struct ccid3_hc_rx_sock *hcrx;
1127 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
1129 hcrx = dp->dccps_hc_rx_ccid_private = kmalloc(sizeof(*hcrx),
1134 memset(hcrx, 0, sizeof(*hcrx));
1136 if (dp->dccps_avg_packet_size >= TFRC_MIN_PACKET_SIZE &&
1137 dp->dccps_avg_packet_size <= TFRC_MAX_PACKET_SIZE)
1138 hcrx->ccid3hcrx_s = (u16)dp->dccps_avg_packet_size;
1140 hcrx->ccid3hcrx_s = TFRC_STD_PACKET_SIZE;
1142 hcrx->ccid3hcrx_state = TFRC_RSTATE_NO_DATA;
1143 INIT_LIST_HEAD(&hcrx->ccid3hcrx_hist);
1144 INIT_LIST_HEAD(&hcrx->ccid3hcrx_li_hist);
1146 * XXX this seems to be paranoid, need to think more about this, for
1147 * now start with something different than zero. -acme
1149 hcrx->ccid3hcrx_rtt = USEC_PER_SEC / 5;
1153 static void ccid3_hc_rx_exit(struct sock *sk)
1155 struct dccp_sock *dp = dccp_sk(sk);
1156 struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
1158 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
1163 ccid3_hc_rx_set_state(sk, TFRC_RSTATE_TERM);
1165 /* Empty packet history */
1166 dccp_rx_hist_purge(ccid3_rx_hist, &hcrx->ccid3hcrx_hist);
1168 /* Empty loss interval history */
1169 dccp_li_hist_purge(ccid3_li_hist, &hcrx->ccid3hcrx_li_hist);
1171 kfree(dp->dccps_hc_rx_ccid_private);
1172 dp->dccps_hc_rx_ccid_private = NULL;
1175 static void ccid3_hc_rx_get_info(struct sock *sk, struct tcp_info *info)
1177 const struct dccp_sock *dp = dccp_sk(sk);
1178 const struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
1183 info->tcpi_ca_state = hcrx->ccid3hcrx_state;
1184 info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1185 info->tcpi_rcv_rtt = hcrx->ccid3hcrx_rtt;
1188 static void ccid3_hc_tx_get_info(struct sock *sk, struct tcp_info *info)
1190 const struct dccp_sock *dp = dccp_sk(sk);
1191 const struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
1196 info->tcpi_rto = hctx->ccid3hctx_t_rto;
1197 info->tcpi_rtt = hctx->ccid3hctx_rtt;
1200 static struct ccid ccid3 = {
1202 .ccid_name = "ccid3",
1203 .ccid_owner = THIS_MODULE,
1204 .ccid_init = ccid3_init,
1205 .ccid_exit = ccid3_exit,
1206 .ccid_hc_tx_init = ccid3_hc_tx_init,
1207 .ccid_hc_tx_exit = ccid3_hc_tx_exit,
1208 .ccid_hc_tx_send_packet = ccid3_hc_tx_send_packet,
1209 .ccid_hc_tx_packet_sent = ccid3_hc_tx_packet_sent,
1210 .ccid_hc_tx_packet_recv = ccid3_hc_tx_packet_recv,
1211 .ccid_hc_tx_insert_options = ccid3_hc_tx_insert_options,
1212 .ccid_hc_tx_parse_options = ccid3_hc_tx_parse_options,
1213 .ccid_hc_rx_init = ccid3_hc_rx_init,
1214 .ccid_hc_rx_exit = ccid3_hc_rx_exit,
1215 .ccid_hc_rx_insert_options = ccid3_hc_rx_insert_options,
1216 .ccid_hc_rx_packet_recv = ccid3_hc_rx_packet_recv,
1217 .ccid_hc_rx_get_info = ccid3_hc_rx_get_info,
1218 .ccid_hc_tx_get_info = ccid3_hc_tx_get_info,
1221 module_param(ccid3_debug, int, 0444);
1222 MODULE_PARM_DESC(ccid3_debug, "Enable debug messages");
1224 static __init int ccid3_module_init(void)
1228 ccid3_rx_hist = dccp_rx_hist_new("ccid3");
1229 if (ccid3_rx_hist == NULL)
1232 ccid3_tx_hist = dccp_tx_hist_new("ccid3");
1233 if (ccid3_tx_hist == NULL)
1236 ccid3_li_hist = dccp_li_hist_new("ccid3");
1237 if (ccid3_li_hist == NULL)
1240 rc = ccid_register(&ccid3);
1242 goto out_free_loss_interval_history;
1246 out_free_loss_interval_history:
1247 dccp_li_hist_delete(ccid3_li_hist);
1248 ccid3_li_hist = NULL;
1250 dccp_tx_hist_delete(ccid3_tx_hist);
1251 ccid3_tx_hist = NULL;
1253 dccp_rx_hist_delete(ccid3_rx_hist);
1254 ccid3_rx_hist = NULL;
1257 module_init(ccid3_module_init);
1259 static __exit void ccid3_module_exit(void)
1261 #ifdef CONFIG_IP_DCCP_UNLOAD_HACK
1263 * Hack to use while developing, so that we get rid of the control
1264 * sock, that is what keeps a refcount on dccp.ko -acme
1266 extern void dccp_ctl_sock_exit(void);
1268 dccp_ctl_sock_exit();
1270 ccid_unregister(&ccid3);
1272 if (ccid3_tx_hist != NULL) {
1273 dccp_tx_hist_delete(ccid3_tx_hist);
1274 ccid3_tx_hist = NULL;
1276 if (ccid3_rx_hist != NULL) {
1277 dccp_rx_hist_delete(ccid3_rx_hist);
1278 ccid3_rx_hist = NULL;
1280 if (ccid3_li_hist != NULL) {
1281 dccp_li_hist_delete(ccid3_li_hist);
1282 ccid3_li_hist = NULL;
1285 module_exit(ccid3_module_exit);
1287 MODULE_AUTHOR("Ian McDonald <iam4@cs.waikato.ac.nz>, "
1288 "Arnaldo Carvalho de Melo <acme@ghostprotocols.net>");
1289 MODULE_DESCRIPTION("DCCP TFRC CCID3 CCID");
1290 MODULE_LICENSE("GPL");
1291 MODULE_ALIAS("net-dccp-ccid-3");