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 dccp_sock *dp = dccp_sk(sk);
116 struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
117 enum ccid3_hc_tx_states oldstate = hctx->ccid3hctx_state;
119 ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
120 dccp_role(sk), sk, ccid3_tx_state_name(oldstate),
121 ccid3_tx_state_name(state));
122 WARN_ON(state == oldstate);
123 hctx->ccid3hctx_state = state;
126 /* Calculate new t_ipi (inter packet interval) by t_ipi = s / X_inst */
127 static inline void ccid3_calc_new_t_ipi(struct ccid3_hc_tx_sock *hctx)
130 * If no feedback spec says t_ipi is 1 second (set elsewhere and then
131 * doubles after every no feedback timer (separate function)
133 if (hctx->ccid3hctx_state != TFRC_SSTATE_NO_FBACK)
134 hctx->ccid3hctx_t_ipi = usecs_div(hctx->ccid3hctx_s,
138 /* Calculate new delta by delta = min(t_ipi / 2, t_gran / 2) */
139 static inline void ccid3_calc_new_delta(struct ccid3_hc_tx_sock *hctx)
141 hctx->ccid3hctx_delta = min_t(u32, hctx->ccid3hctx_t_ipi / 2,
142 TFRC_OPSYS_HALF_TIME_GRAN);
148 * x_calc = calcX(s, R, p);
149 * X = max(min(X_calc, 2 * X_recv), s / t_mbi);
151 * If (now - tld >= R)
152 * X = max(min(2 * X, 2 * X_recv), s / R);
155 static void ccid3_hc_tx_update_x(struct sock *sk)
157 struct dccp_sock *dp = dccp_sk(sk);
158 struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
160 /* To avoid large error in calcX */
161 if (hctx->ccid3hctx_p >= TFRC_SMALLEST_P) {
162 hctx->ccid3hctx_x_calc = tfrc_calc_x(hctx->ccid3hctx_s,
165 hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_calc,
166 2 * hctx->ccid3hctx_x_recv),
168 TFRC_MAX_BACK_OFF_TIME));
172 dccp_timestamp(sk, &now);
173 if (timeval_delta(&now, &hctx->ccid3hctx_t_ld) >=
174 hctx->ccid3hctx_rtt) {
175 hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_recv,
176 hctx->ccid3hctx_x) * 2,
177 usecs_div(hctx->ccid3hctx_s,
178 hctx->ccid3hctx_rtt));
179 hctx->ccid3hctx_t_ld = now;
184 static void ccid3_hc_tx_no_feedback_timer(unsigned long data)
186 struct sock *sk = (struct sock *)data;
187 struct dccp_sock *dp = dccp_sk(sk);
188 unsigned long next_tmout = 0;
189 struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
192 if (sock_owned_by_user(sk)) {
193 /* Try again later. */
194 /* XXX: set some sensible MIB */
195 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
200 ccid3_pr_debug("%s, sk=%p, state=%s\n", dccp_role(sk), sk,
201 ccid3_tx_state_name(hctx->ccid3hctx_state));
203 switch (hctx->ccid3hctx_state) {
204 case TFRC_SSTATE_TERM:
206 case TFRC_SSTATE_NO_FBACK:
207 /* Halve send rate */
208 hctx->ccid3hctx_x /= 2;
209 if (hctx->ccid3hctx_x < (hctx->ccid3hctx_s /
210 TFRC_MAX_BACK_OFF_TIME))
211 hctx->ccid3hctx_x = (hctx->ccid3hctx_s /
212 TFRC_MAX_BACK_OFF_TIME);
214 ccid3_pr_debug("%s, sk=%p, state=%s, updated tx rate to %d "
217 ccid3_tx_state_name(hctx->ccid3hctx_state),
219 next_tmout = max_t(u32, 2 * usecs_div(hctx->ccid3hctx_s,
221 TFRC_INITIAL_TIMEOUT);
223 * FIXME - not sure above calculation is correct. See section
224 * 5 of CCID3 11 should adjust tx_t_ipi and double that to
228 case TFRC_SSTATE_FBACK:
230 * Check if IDLE since last timeout and recv rate is less than
233 if (!hctx->ccid3hctx_idle ||
234 (hctx->ccid3hctx_x_recv >=
235 4 * usecs_div(hctx->ccid3hctx_s, hctx->ccid3hctx_rtt))) {
236 ccid3_pr_debug("%s, sk=%p, state=%s, not idle\n",
238 ccid3_tx_state_name(hctx->ccid3hctx_state));
239 /* Halve sending rate */
241 /* If (X_calc > 2 * X_recv)
242 * X_recv = max(X_recv / 2, s / (2 * t_mbi));
244 * X_recv = X_calc / 4;
246 BUG_ON(hctx->ccid3hctx_p >= TFRC_SMALLEST_P &&
247 hctx->ccid3hctx_x_calc == 0);
249 /* check also if p is zero -> x_calc is infinity? */
250 if (hctx->ccid3hctx_p < TFRC_SMALLEST_P ||
251 hctx->ccid3hctx_x_calc > 2 * hctx->ccid3hctx_x_recv)
252 hctx->ccid3hctx_x_recv = max_t(u32, hctx->ccid3hctx_x_recv / 2,
253 hctx->ccid3hctx_s / (2 * TFRC_MAX_BACK_OFF_TIME));
255 hctx->ccid3hctx_x_recv = hctx->ccid3hctx_x_calc / 4;
257 /* Update sending rate */
258 ccid3_hc_tx_update_x(sk);
261 * Schedule no feedback timer to expire in
262 * max(4 * R, 2 * s / X)
264 next_tmout = max_t(u32, hctx->ccid3hctx_t_rto,
265 2 * usecs_div(hctx->ccid3hctx_s,
269 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
270 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
275 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
276 jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
277 hctx->ccid3hctx_idle = 1;
283 static int ccid3_hc_tx_send_packet(struct sock *sk,
284 struct sk_buff *skb, int len)
286 struct dccp_sock *dp = dccp_sk(sk);
287 struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
288 struct dccp_tx_hist_entry *new_packet;
293 /* Check if pure ACK or Terminating*/
296 * XXX: We only call this function for DATA and DATAACK, on, these
297 * packets can have zero length, but why the comment about "pure ACK"?
299 if (hctx == NULL || len == 0 ||
300 hctx->ccid3hctx_state == TFRC_SSTATE_TERM)
303 /* See if last packet allocated was not sent */
304 new_packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
305 if (new_packet == NULL || new_packet->dccphtx_sent) {
306 new_packet = dccp_tx_hist_entry_new(ccid3_tx_hist,
310 if (new_packet == NULL) {
311 ccid3_pr_debug("%s, sk=%p, not enough mem to add "
312 "to history, send refused\n",
317 dccp_tx_hist_add_entry(&hctx->ccid3hctx_hist, new_packet);
320 dccp_timestamp(sk, &now);
322 switch (hctx->ccid3hctx_state) {
323 case TFRC_SSTATE_NO_SENT:
324 ccid3_pr_debug("%s, sk=%p, first packet(%llu)\n",
325 dccp_role(sk), sk, dp->dccps_gss);
327 hctx->ccid3hctx_no_feedback_timer.function = ccid3_hc_tx_no_feedback_timer;
328 hctx->ccid3hctx_no_feedback_timer.data = (unsigned long)sk;
329 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
330 jiffies + usecs_to_jiffies(TFRC_INITIAL_TIMEOUT));
331 hctx->ccid3hctx_last_win_count = 0;
332 hctx->ccid3hctx_t_last_win_count = now;
333 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_NO_FBACK);
334 hctx->ccid3hctx_t_ipi = TFRC_INITIAL_TIMEOUT;
336 /* Set nominal send time for initial packet */
337 hctx->ccid3hctx_t_nom = now;
338 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
339 hctx->ccid3hctx_t_ipi);
340 ccid3_calc_new_delta(hctx);
343 case TFRC_SSTATE_NO_FBACK:
344 case TFRC_SSTATE_FBACK:
345 delay = (timeval_delta(&now, &hctx->ccid3hctx_t_nom) -
346 hctx->ccid3hctx_delta);
347 ccid3_pr_debug("send_packet delay=%ld\n", delay);
349 /* divide by -1000 is to convert to ms and get sign right */
350 rc = delay > 0 ? delay : 0;
353 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
354 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
360 /* Can we send? if so add options and add to packet history */
362 dp->dccps_hc_tx_insert_options = 1;
363 new_packet->dccphtx_ccval =
364 DCCP_SKB_CB(skb)->dccpd_ccval =
365 hctx->ccid3hctx_last_win_count;
371 static void ccid3_hc_tx_packet_sent(struct sock *sk, int more, int len)
373 struct dccp_sock *dp = dccp_sk(sk);
374 struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
377 BUG_ON(hctx == NULL);
379 if (hctx->ccid3hctx_state == TFRC_SSTATE_TERM) {
380 ccid3_pr_debug("%s, sk=%p, while state is TFRC_SSTATE_TERM!\n",
385 dccp_timestamp(sk, &now);
387 /* check if we have sent a data packet */
389 unsigned long quarter_rtt;
390 struct dccp_tx_hist_entry *packet;
392 packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
393 if (packet == NULL) {
394 printk(KERN_CRIT "%s: packet doesn't exists in "
395 "history!\n", __FUNCTION__);
398 if (packet->dccphtx_sent) {
399 printk(KERN_CRIT "%s: no unsent packet in history!\n",
403 packet->dccphtx_tstamp = now;
404 packet->dccphtx_seqno = dp->dccps_gss;
406 * Check if win_count have changed
407 * Algorithm in "8.1. Window Counter Valuer" in
408 * draft-ietf-dccp-ccid3-11.txt
410 quarter_rtt = timeval_delta(&now, &hctx->ccid3hctx_t_last_win_count);
411 if (likely(hctx->ccid3hctx_rtt > 8))
412 quarter_rtt /= hctx->ccid3hctx_rtt / 4;
414 if (quarter_rtt > 0) {
415 hctx->ccid3hctx_t_last_win_count = now;
416 hctx->ccid3hctx_last_win_count = (hctx->ccid3hctx_last_win_count +
417 min_t(unsigned long, quarter_rtt, 5)) % 16;
418 ccid3_pr_debug("%s, sk=%p, window changed from "
421 packet->dccphtx_ccval,
422 hctx->ccid3hctx_last_win_count);
425 hctx->ccid3hctx_idle = 0;
426 packet->dccphtx_rtt = hctx->ccid3hctx_rtt;
427 packet->dccphtx_sent = 1;
429 ccid3_pr_debug("%s, sk=%p, seqno=%llu NOT inserted!\n",
430 dccp_role(sk), sk, dp->dccps_gss);
432 switch (hctx->ccid3hctx_state) {
433 case TFRC_SSTATE_NO_SENT:
434 /* if first wasn't pure ack */
436 printk(KERN_CRIT "%s: %s, First packet sent is noted "
437 "as a data packet\n",
438 __FUNCTION__, dccp_role(sk));
440 case TFRC_SSTATE_NO_FBACK:
441 case TFRC_SSTATE_FBACK:
443 hctx->ccid3hctx_t_nom = now;
444 ccid3_calc_new_t_ipi(hctx);
445 ccid3_calc_new_delta(hctx);
446 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
447 hctx->ccid3hctx_t_ipi);
451 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
452 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
458 static void ccid3_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
460 struct dccp_sock *dp = dccp_sk(sk);
461 struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
462 struct ccid3_options_received *opt_recv;
463 struct dccp_tx_hist_entry *packet;
465 unsigned long next_tmout;
474 if (hctx->ccid3hctx_state == TFRC_SSTATE_TERM) {
475 ccid3_pr_debug("%s, sk=%p, received a packet when "
476 "terminating!\n", dccp_role(sk), sk);
480 /* we are only interested in ACKs */
481 if (!(DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK ||
482 DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_DATAACK))
485 opt_recv = &hctx->ccid3hctx_options_received;
487 t_elapsed = dp->dccps_options_received.dccpor_elapsed_time * 10;
488 x_recv = opt_recv->ccid3or_receive_rate;
489 pinv = opt_recv->ccid3or_loss_event_rate;
491 switch (hctx->ccid3hctx_state) {
492 case TFRC_SSTATE_NO_SENT:
493 /* FIXME: what to do here? */
495 case TFRC_SSTATE_NO_FBACK:
496 case TFRC_SSTATE_FBACK:
497 /* Calculate new round trip sample by
498 * R_sample = (now - t_recvdata) - t_delay */
499 /* get t_recvdata from history */
500 packet = dccp_tx_hist_find_entry(&hctx->ccid3hctx_hist,
501 DCCP_SKB_CB(skb)->dccpd_ack_seq);
502 if (packet == NULL) {
503 ccid3_pr_debug("%s, sk=%p, seqno %llu(%s) does't "
504 "exist in history!\n",
506 DCCP_SKB_CB(skb)->dccpd_ack_seq,
507 dccp_packet_name(DCCP_SKB_CB(skb)->dccpd_type));
512 dccp_timestamp(sk, &now);
513 r_sample = timeval_delta(&now, &packet->dccphtx_tstamp);
514 if (unlikely(r_sample <= t_elapsed))
515 LIMIT_NETDEBUG(KERN_WARNING
516 "%s: r_sample=%uus, t_elapsed=%uus\n",
517 __FUNCTION__, r_sample, t_elapsed);
519 r_sample -= t_elapsed;
521 /* Update RTT estimate by
522 * If (No feedback recv)
525 * R = q * R + (1 - q) * R_sample;
527 * q is a constant, RFC 3448 recomments 0.9
529 if (hctx->ccid3hctx_state == TFRC_SSTATE_NO_FBACK) {
530 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_FBACK);
531 hctx->ccid3hctx_rtt = r_sample;
533 hctx->ccid3hctx_rtt = (hctx->ccid3hctx_rtt * 9) / 10 +
536 ccid3_pr_debug("%s, sk=%p, New RTT estimate=%uus, "
537 "r_sample=%us\n", dccp_role(sk), sk,
538 hctx->ccid3hctx_rtt, r_sample);
540 /* Update timeout interval */
541 hctx->ccid3hctx_t_rto = max_t(u32, 4 * hctx->ccid3hctx_rtt,
544 /* Update receive rate */
545 hctx->ccid3hctx_x_recv = x_recv;/* X_recv in bytes per sec */
547 /* Update loss event rate */
548 if (pinv == ~0 || pinv == 0)
549 hctx->ccid3hctx_p = 0;
551 hctx->ccid3hctx_p = 1000000 / pinv;
553 if (hctx->ccid3hctx_p < TFRC_SMALLEST_P) {
554 hctx->ccid3hctx_p = TFRC_SMALLEST_P;
555 ccid3_pr_debug("%s, sk=%p, Smallest p used!\n",
560 /* unschedule no feedback timer */
561 sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
563 /* Update sending rate */
564 ccid3_hc_tx_update_x(sk);
566 /* Update next send time */
567 timeval_sub_usecs(&hctx->ccid3hctx_t_nom,
568 hctx->ccid3hctx_t_ipi);
569 ccid3_calc_new_t_ipi(hctx);
570 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
571 hctx->ccid3hctx_t_ipi);
572 ccid3_calc_new_delta(hctx);
574 /* remove all packets older than the one acked from history */
575 dccp_tx_hist_purge_older(ccid3_tx_hist,
576 &hctx->ccid3hctx_hist, packet);
578 * As we have calculated new ipi, delta, t_nom it is possible that
579 * we now can send a packet, so wake up dccp_wait_for_ccids.
581 sk->sk_write_space(sk);
584 * Schedule no feedback timer to expire in
585 * max(4 * R, 2 * s / X)
587 next_tmout = max(hctx->ccid3hctx_t_rto,
588 2 * usecs_div(hctx->ccid3hctx_s,
591 ccid3_pr_debug("%s, sk=%p, Scheduled no feedback timer to "
592 "expire in %lu jiffies (%luus)\n",
594 usecs_to_jiffies(next_tmout), next_tmout);
596 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
597 jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
600 hctx->ccid3hctx_idle = 1;
603 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
604 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
610 static void ccid3_hc_tx_insert_options(struct sock *sk, struct sk_buff *skb)
612 const struct dccp_sock *dp = dccp_sk(sk);
613 struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
615 if (hctx == NULL || !(sk->sk_state == DCCP_OPEN ||
616 sk->sk_state == DCCP_PARTOPEN))
619 DCCP_SKB_CB(skb)->dccpd_ccval = hctx->ccid3hctx_last_win_count;
622 static int ccid3_hc_tx_parse_options(struct sock *sk, unsigned char option,
623 unsigned char len, u16 idx,
624 unsigned char *value)
627 struct dccp_sock *dp = dccp_sk(sk);
628 struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
629 struct ccid3_options_received *opt_recv;
634 opt_recv = &hctx->ccid3hctx_options_received;
636 if (opt_recv->ccid3or_seqno != dp->dccps_gsr) {
637 opt_recv->ccid3or_seqno = dp->dccps_gsr;
638 opt_recv->ccid3or_loss_event_rate = ~0;
639 opt_recv->ccid3or_loss_intervals_idx = 0;
640 opt_recv->ccid3or_loss_intervals_len = 0;
641 opt_recv->ccid3or_receive_rate = 0;
645 case TFRC_OPT_LOSS_EVENT_RATE:
647 ccid3_pr_debug("%s, sk=%p, invalid len for "
648 "TFRC_OPT_LOSS_EVENT_RATE\n",
652 opt_recv->ccid3or_loss_event_rate = ntohl(*(u32 *)value);
653 ccid3_pr_debug("%s, sk=%p, LOSS_EVENT_RATE=%u\n",
655 opt_recv->ccid3or_loss_event_rate);
658 case TFRC_OPT_LOSS_INTERVALS:
659 opt_recv->ccid3or_loss_intervals_idx = idx;
660 opt_recv->ccid3or_loss_intervals_len = len;
661 ccid3_pr_debug("%s, sk=%p, LOSS_INTERVALS=(%u, %u)\n",
663 opt_recv->ccid3or_loss_intervals_idx,
664 opt_recv->ccid3or_loss_intervals_len);
666 case TFRC_OPT_RECEIVE_RATE:
668 ccid3_pr_debug("%s, sk=%p, invalid len for "
669 "TFRC_OPT_RECEIVE_RATE\n",
673 opt_recv->ccid3or_receive_rate = ntohl(*(u32 *)value);
674 ccid3_pr_debug("%s, sk=%p, RECEIVE_RATE=%u\n",
676 opt_recv->ccid3or_receive_rate);
684 static int ccid3_hc_tx_init(struct sock *sk)
686 struct dccp_sock *dp = dccp_sk(sk);
687 struct ccid3_hc_tx_sock *hctx;
689 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
691 hctx = dp->dccps_hc_tx_ccid_private = kmalloc(sizeof(*hctx),
696 memset(hctx, 0, sizeof(*hctx));
698 if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
699 dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
700 hctx->ccid3hctx_s = dp->dccps_packet_size;
702 hctx->ccid3hctx_s = TFRC_STD_PACKET_SIZE;
704 /* Set transmission rate to 1 packet per second */
705 hctx->ccid3hctx_x = hctx->ccid3hctx_s;
706 hctx->ccid3hctx_t_rto = USEC_PER_SEC;
707 hctx->ccid3hctx_state = TFRC_SSTATE_NO_SENT;
708 INIT_LIST_HEAD(&hctx->ccid3hctx_hist);
709 init_timer(&hctx->ccid3hctx_no_feedback_timer);
714 static void ccid3_hc_tx_exit(struct sock *sk)
716 struct dccp_sock *dp = dccp_sk(sk);
717 struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
719 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
720 BUG_ON(hctx == NULL);
722 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_TERM);
723 sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
725 /* Empty packet history */
726 dccp_tx_hist_purge(ccid3_tx_hist, &hctx->ccid3hctx_hist);
728 kfree(dp->dccps_hc_tx_ccid_private);
729 dp->dccps_hc_tx_ccid_private = NULL;
733 * RX Half Connection methods
736 /* TFRC receiver states */
737 enum ccid3_hc_rx_states {
738 TFRC_RSTATE_NO_DATA = 1,
740 TFRC_RSTATE_TERM = 127,
744 static const char *ccid3_rx_state_name(enum ccid3_hc_rx_states state)
746 static char *ccid3_rx_state_names[] = {
747 [TFRC_RSTATE_NO_DATA] = "NO_DATA",
748 [TFRC_RSTATE_DATA] = "DATA",
749 [TFRC_RSTATE_TERM] = "TERM",
752 return ccid3_rx_state_names[state];
756 static inline void ccid3_hc_rx_set_state(struct sock *sk,
757 enum ccid3_hc_rx_states state)
759 struct dccp_sock *dp = dccp_sk(sk);
760 struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
761 enum ccid3_hc_rx_states oldstate = hcrx->ccid3hcrx_state;
763 ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
764 dccp_role(sk), sk, ccid3_rx_state_name(oldstate),
765 ccid3_rx_state_name(state));
766 WARN_ON(state == oldstate);
767 hcrx->ccid3hcrx_state = state;
770 static void ccid3_hc_rx_send_feedback(struct sock *sk)
772 struct dccp_sock *dp = dccp_sk(sk);
773 struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
774 struct dccp_rx_hist_entry *packet;
777 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
779 dccp_timestamp(sk, &now);
781 switch (hcrx->ccid3hcrx_state) {
782 case TFRC_RSTATE_NO_DATA:
783 hcrx->ccid3hcrx_x_recv = 0;
785 case TFRC_RSTATE_DATA: {
786 const u32 delta = timeval_delta(&now,
787 &hcrx->ccid3hcrx_tstamp_last_feedback);
788 hcrx->ccid3hcrx_x_recv = usecs_div(hcrx->ccid3hcrx_bytes_recv,
793 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
794 __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
799 packet = dccp_rx_hist_find_data_packet(&hcrx->ccid3hcrx_hist);
800 if (packet == NULL) {
801 printk(KERN_CRIT "%s: %s, sk=%p, no data packet in history!\n",
802 __FUNCTION__, dccp_role(sk), sk);
807 hcrx->ccid3hcrx_tstamp_last_feedback = now;
808 hcrx->ccid3hcrx_last_counter = packet->dccphrx_ccval;
809 hcrx->ccid3hcrx_seqno_last_counter = packet->dccphrx_seqno;
810 hcrx->ccid3hcrx_bytes_recv = 0;
812 /* Convert to multiples of 10us */
813 hcrx->ccid3hcrx_elapsed_time =
814 timeval_delta(&now, &packet->dccphrx_tstamp) / 10;
815 if (hcrx->ccid3hcrx_p == 0)
816 hcrx->ccid3hcrx_pinv = ~0;
818 hcrx->ccid3hcrx_pinv = 1000000 / hcrx->ccid3hcrx_p;
819 dp->dccps_hc_rx_insert_options = 1;
823 static void ccid3_hc_rx_insert_options(struct sock *sk, struct sk_buff *skb)
825 const struct dccp_sock *dp = dccp_sk(sk);
827 struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
829 if (hcrx == NULL || !(sk->sk_state == DCCP_OPEN ||
830 sk->sk_state == DCCP_PARTOPEN))
833 DCCP_SKB_CB(skb)->dccpd_ccval = hcrx->ccid3hcrx_last_counter;
835 if (dccp_packet_without_ack(skb))
838 if (hcrx->ccid3hcrx_elapsed_time != 0)
839 dccp_insert_option_elapsed_time(sk, skb,
840 hcrx->ccid3hcrx_elapsed_time);
841 dccp_insert_option_timestamp(sk, skb);
842 x_recv = htonl(hcrx->ccid3hcrx_x_recv);
843 pinv = htonl(hcrx->ccid3hcrx_pinv);
844 dccp_insert_option(sk, skb, TFRC_OPT_LOSS_EVENT_RATE,
845 &pinv, sizeof(pinv));
846 dccp_insert_option(sk, skb, TFRC_OPT_RECEIVE_RATE,
847 &x_recv, sizeof(x_recv));
850 /* calculate first loss interval
852 * returns estimated loss interval in usecs */
854 static u32 ccid3_hc_rx_calc_first_li(struct sock *sk)
856 struct dccp_sock *dp = dccp_sk(sk);
857 struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
858 struct dccp_rx_hist_entry *entry, *next, *tail = NULL;
859 u32 rtt, delta, x_recv, fval, p, tmp2;
860 struct timeval tstamp = { 0, };
866 list_for_each_entry_safe(entry, next, &hcrx->ccid3hcrx_hist,
868 if (dccp_rx_hist_entry_data_packet(entry)) {
873 tstamp = entry->dccphrx_tstamp;
874 win_count = entry->dccphrx_ccval;
878 interval = win_count - entry->dccphrx_ccval;
880 interval += TFRC_WIN_COUNT_LIMIT;
889 printk(KERN_CRIT "%s: %s, sk=%p, packet history contains no "
891 __FUNCTION__, dccp_role(sk), sk);
896 ccid3_pr_debug("%s, sk=%p, Could not find a win_count "
897 "interval > 0. Defaulting to 1\n",
902 rtt = timeval_delta(&tstamp, &tail->dccphrx_tstamp) * 4 / interval;
903 ccid3_pr_debug("%s, sk=%p, approximated RTT to %uus\n",
904 dccp_role(sk), sk, rtt);
908 dccp_timestamp(sk, &tstamp);
909 delta = timeval_delta(&tstamp, &hcrx->ccid3hcrx_tstamp_last_feedback);
910 x_recv = usecs_div(hcrx->ccid3hcrx_bytes_recv, delta);
912 tmp1 = (u64)x_recv * (u64)rtt;
913 do_div(tmp1,10000000);
915 fval = (hcrx->ccid3hcrx_s * 100000) / tmp2;
916 /* do not alter order above or you will get overflow on 32 bit */
917 p = tfrc_calc_x_reverse_lookup(fval);
918 ccid3_pr_debug("%s, sk=%p, receive rate=%u bytes/s, implied "
919 "loss rate=%u\n", dccp_role(sk), sk, x_recv, p);
927 static void ccid3_hc_rx_update_li(struct sock *sk, u64 seq_loss, u8 win_loss)
929 struct dccp_sock *dp = dccp_sk(sk);
930 struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
932 if (seq_loss != DCCP_MAX_SEQNO + 1 &&
933 list_empty(&hcrx->ccid3hcrx_li_hist)) {
934 struct dccp_li_hist_entry *li_tail;
936 li_tail = dccp_li_hist_interval_new(ccid3_li_hist,
937 &hcrx->ccid3hcrx_li_hist,
941 li_tail->dccplih_interval = ccid3_hc_rx_calc_first_li(sk);
943 /* FIXME: find end of interval */
946 static void ccid3_hc_rx_detect_loss(struct sock *sk)
948 struct dccp_sock *dp = dccp_sk(sk);
949 struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
951 const u64 seq_loss = dccp_rx_hist_detect_loss(&hcrx->ccid3hcrx_hist,
952 &hcrx->ccid3hcrx_li_hist,
955 ccid3_hc_rx_update_li(sk, seq_loss, win_loss);
958 static void ccid3_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
960 struct dccp_sock *dp = dccp_sk(sk);
961 struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
962 const struct dccp_options_received *opt_recv;
963 struct dccp_rx_hist_entry *packet;
966 u32 p_prev, r_sample, t_elapsed;
972 BUG_ON(!(hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA ||
973 hcrx->ccid3hcrx_state == TFRC_RSTATE_DATA));
975 opt_recv = &dp->dccps_options_received;
977 switch (DCCP_SKB_CB(skb)->dccpd_type) {
979 if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
981 case DCCP_PKT_DATAACK:
982 if (opt_recv->dccpor_timestamp_echo == 0)
984 p_prev = hcrx->ccid3hcrx_rtt;
985 dccp_timestamp(sk, &now);
986 timeval_sub_usecs(&now, opt_recv->dccpor_timestamp_echo * 10);
987 r_sample = timeval_usecs(&now);
988 t_elapsed = opt_recv->dccpor_elapsed_time * 10;
990 if (unlikely(r_sample <= t_elapsed))
991 LIMIT_NETDEBUG(KERN_WARNING
992 "%s: r_sample=%uus, t_elapsed=%uus\n",
993 __FUNCTION__, r_sample, t_elapsed);
995 r_sample -= t_elapsed;
997 if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
998 hcrx->ccid3hcrx_rtt = r_sample;
1000 hcrx->ccid3hcrx_rtt = (hcrx->ccid3hcrx_rtt * 9) / 10 +
1003 if (p_prev != hcrx->ccid3hcrx_rtt)
1004 ccid3_pr_debug("%s, New RTT=%luus, elapsed time=%u\n",
1005 dccp_role(sk), hcrx->ccid3hcrx_rtt,
1006 opt_recv->dccpor_elapsed_time);
1011 ccid3_pr_debug("%s, sk=%p, not DATA/DATAACK/ACK packet(%s)\n",
1013 dccp_packet_name(DCCP_SKB_CB(skb)->dccpd_type));
1017 packet = dccp_rx_hist_entry_new(ccid3_rx_hist, sk, opt_recv->dccpor_ndp,
1019 if (packet == NULL) {
1020 ccid3_pr_debug("%s, sk=%p, Not enough mem to add rx packet "
1021 "to history (consider it lost)!",
1026 win_count = packet->dccphrx_ccval;
1028 ins = dccp_rx_hist_add_packet(ccid3_rx_hist, &hcrx->ccid3hcrx_hist,
1029 &hcrx->ccid3hcrx_li_hist, packet);
1031 if (DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK)
1034 switch (hcrx->ccid3hcrx_state) {
1035 case TFRC_RSTATE_NO_DATA:
1036 ccid3_pr_debug("%s, sk=%p(%s), skb=%p, sending initial "
1039 dccp_state_name(sk->sk_state), skb);
1040 ccid3_hc_rx_send_feedback(sk);
1041 ccid3_hc_rx_set_state(sk, TFRC_RSTATE_DATA);
1043 case TFRC_RSTATE_DATA:
1044 hcrx->ccid3hcrx_bytes_recv += skb->len -
1045 dccp_hdr(skb)->dccph_doff * 4;
1049 dccp_timestamp(sk, &now);
1050 if (timeval_delta(&now, &hcrx->ccid3hcrx_tstamp_last_ack) >=
1051 hcrx->ccid3hcrx_rtt) {
1052 hcrx->ccid3hcrx_tstamp_last_ack = now;
1053 ccid3_hc_rx_send_feedback(sk);
1057 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
1058 __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
1063 /* Dealing with packet loss */
1064 ccid3_pr_debug("%s, sk=%p(%s), data loss! Reacting...\n",
1065 dccp_role(sk), sk, dccp_state_name(sk->sk_state));
1067 ccid3_hc_rx_detect_loss(sk);
1068 p_prev = hcrx->ccid3hcrx_p;
1070 /* Calculate loss event rate */
1071 if (!list_empty(&hcrx->ccid3hcrx_li_hist))
1072 /* Scaling up by 1000000 as fixed decimal */
1073 hcrx->ccid3hcrx_p = 1000000 / dccp_li_hist_calc_i_mean(&hcrx->ccid3hcrx_li_hist);
1075 if (hcrx->ccid3hcrx_p > p_prev) {
1076 ccid3_hc_rx_send_feedback(sk);
1081 static int ccid3_hc_rx_init(struct sock *sk)
1083 struct dccp_sock *dp = dccp_sk(sk);
1084 struct ccid3_hc_rx_sock *hcrx;
1086 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
1088 hcrx = dp->dccps_hc_rx_ccid_private = kmalloc(sizeof(*hcrx),
1093 memset(hcrx, 0, sizeof(*hcrx));
1095 if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
1096 dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
1097 hcrx->ccid3hcrx_s = dp->dccps_packet_size;
1099 hcrx->ccid3hcrx_s = TFRC_STD_PACKET_SIZE;
1101 hcrx->ccid3hcrx_state = TFRC_RSTATE_NO_DATA;
1102 INIT_LIST_HEAD(&hcrx->ccid3hcrx_hist);
1103 INIT_LIST_HEAD(&hcrx->ccid3hcrx_li_hist);
1104 dccp_timestamp(sk, &hcrx->ccid3hcrx_tstamp_last_ack);
1105 hcrx->ccid3hcrx_tstamp_last_feedback = hcrx->ccid3hcrx_tstamp_last_ack;
1106 hcrx->ccid3hcrx_rtt = 5000; /* XXX 5ms for now... */
1110 static void ccid3_hc_rx_exit(struct sock *sk)
1112 struct dccp_sock *dp = dccp_sk(sk);
1113 struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
1115 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
1120 ccid3_hc_rx_set_state(sk, TFRC_RSTATE_TERM);
1122 /* Empty packet history */
1123 dccp_rx_hist_purge(ccid3_rx_hist, &hcrx->ccid3hcrx_hist);
1125 /* Empty loss interval history */
1126 dccp_li_hist_purge(ccid3_li_hist, &hcrx->ccid3hcrx_li_hist);
1128 kfree(dp->dccps_hc_rx_ccid_private);
1129 dp->dccps_hc_rx_ccid_private = NULL;
1132 static void ccid3_hc_rx_get_info(struct sock *sk, struct tcp_info *info)
1134 const struct dccp_sock *dp = dccp_sk(sk);
1135 const struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
1140 info->tcpi_ca_state = hcrx->ccid3hcrx_state;
1141 info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1142 info->tcpi_rcv_rtt = hcrx->ccid3hcrx_rtt;
1145 static void ccid3_hc_tx_get_info(struct sock *sk, struct tcp_info *info)
1147 const struct dccp_sock *dp = dccp_sk(sk);
1148 const struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
1153 info->tcpi_rto = hctx->ccid3hctx_t_rto;
1154 info->tcpi_rtt = hctx->ccid3hctx_rtt;
1157 static struct ccid ccid3 = {
1159 .ccid_name = "ccid3",
1160 .ccid_owner = THIS_MODULE,
1161 .ccid_init = ccid3_init,
1162 .ccid_exit = ccid3_exit,
1163 .ccid_hc_tx_init = ccid3_hc_tx_init,
1164 .ccid_hc_tx_exit = ccid3_hc_tx_exit,
1165 .ccid_hc_tx_send_packet = ccid3_hc_tx_send_packet,
1166 .ccid_hc_tx_packet_sent = ccid3_hc_tx_packet_sent,
1167 .ccid_hc_tx_packet_recv = ccid3_hc_tx_packet_recv,
1168 .ccid_hc_tx_insert_options = ccid3_hc_tx_insert_options,
1169 .ccid_hc_tx_parse_options = ccid3_hc_tx_parse_options,
1170 .ccid_hc_rx_init = ccid3_hc_rx_init,
1171 .ccid_hc_rx_exit = ccid3_hc_rx_exit,
1172 .ccid_hc_rx_insert_options = ccid3_hc_rx_insert_options,
1173 .ccid_hc_rx_packet_recv = ccid3_hc_rx_packet_recv,
1174 .ccid_hc_rx_get_info = ccid3_hc_rx_get_info,
1175 .ccid_hc_tx_get_info = ccid3_hc_tx_get_info,
1178 module_param(ccid3_debug, int, 0444);
1179 MODULE_PARM_DESC(ccid3_debug, "Enable debug messages");
1181 static __init int ccid3_module_init(void)
1185 ccid3_rx_hist = dccp_rx_hist_new("ccid3");
1186 if (ccid3_rx_hist == NULL)
1189 ccid3_tx_hist = dccp_tx_hist_new("ccid3");
1190 if (ccid3_tx_hist == NULL)
1193 ccid3_li_hist = dccp_li_hist_new("ccid3");
1194 if (ccid3_li_hist == NULL)
1197 rc = ccid_register(&ccid3);
1199 goto out_free_loss_interval_history;
1203 out_free_loss_interval_history:
1204 dccp_li_hist_delete(ccid3_li_hist);
1205 ccid3_li_hist = NULL;
1207 dccp_tx_hist_delete(ccid3_tx_hist);
1208 ccid3_tx_hist = NULL;
1210 dccp_rx_hist_delete(ccid3_rx_hist);
1211 ccid3_rx_hist = NULL;
1214 module_init(ccid3_module_init);
1216 static __exit void ccid3_module_exit(void)
1218 #ifdef CONFIG_IP_DCCP_UNLOAD_HACK
1220 * Hack to use while developing, so that we get rid of the control
1221 * sock, that is what keeps a refcount on dccp.ko -acme
1223 extern void dccp_ctl_sock_exit(void);
1225 dccp_ctl_sock_exit();
1227 ccid_unregister(&ccid3);
1229 if (ccid3_tx_hist != NULL) {
1230 dccp_tx_hist_delete(ccid3_tx_hist);
1231 ccid3_tx_hist = NULL;
1233 if (ccid3_rx_hist != NULL) {
1234 dccp_rx_hist_delete(ccid3_rx_hist);
1235 ccid3_rx_hist = NULL;
1237 if (ccid3_li_hist != NULL) {
1238 dccp_li_hist_delete(ccid3_li_hist);
1239 ccid3_li_hist = NULL;
1242 module_exit(ccid3_module_exit);
1244 MODULE_AUTHOR("Ian McDonald <iam4@cs.waikato.ac.nz>, "
1245 "Arnaldo Carvalho de Melo <acme@ghostprotocols.net>");
1246 MODULE_DESCRIPTION("DCCP TFRC CCID3 CCID");
1247 MODULE_LICENSE("GPL");
1248 MODULE_ALIAS("net-dccp-ccid-3");