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)
84 static void ccid3_exit(struct sock *sk)
88 /* TFRC sender states */
89 enum ccid3_hc_tx_states {
90 TFRC_SSTATE_NO_SENT = 1,
97 static const char *ccid3_tx_state_name(enum ccid3_hc_tx_states state)
99 static char *ccid3_state_names[] = {
100 [TFRC_SSTATE_NO_SENT] = "NO_SENT",
101 [TFRC_SSTATE_NO_FBACK] = "NO_FBACK",
102 [TFRC_SSTATE_FBACK] = "FBACK",
103 [TFRC_SSTATE_TERM] = "TERM",
106 return ccid3_state_names[state];
110 static inline void ccid3_hc_tx_set_state(struct sock *sk,
111 enum ccid3_hc_tx_states state)
113 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
114 enum ccid3_hc_tx_states oldstate = hctx->ccid3hctx_state;
116 ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
117 dccp_role(sk), sk, ccid3_tx_state_name(oldstate),
118 ccid3_tx_state_name(state));
119 WARN_ON(state == oldstate);
120 hctx->ccid3hctx_state = state;
123 /* Calculate new t_ipi (inter packet interval) by t_ipi = s / X_inst */
124 static inline void ccid3_calc_new_t_ipi(struct ccid3_hc_tx_sock *hctx)
127 * If no feedback spec says t_ipi is 1 second (set elsewhere and then
128 * doubles after every no feedback timer (separate function)
130 if (hctx->ccid3hctx_state != TFRC_SSTATE_NO_FBACK)
131 hctx->ccid3hctx_t_ipi = usecs_div(hctx->ccid3hctx_s,
135 /* Calculate new delta by delta = min(t_ipi / 2, t_gran / 2) */
136 static inline void ccid3_calc_new_delta(struct ccid3_hc_tx_sock *hctx)
138 hctx->ccid3hctx_delta = min_t(u32, hctx->ccid3hctx_t_ipi / 2,
139 TFRC_OPSYS_HALF_TIME_GRAN);
145 * x_calc = calcX(s, R, p);
146 * X = max(min(X_calc, 2 * X_recv), s / t_mbi);
148 * If (now - tld >= R)
149 * X = max(min(2 * X, 2 * X_recv), s / R);
152 static void ccid3_hc_tx_update_x(struct sock *sk)
154 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
156 /* To avoid large error in calcX */
157 if (hctx->ccid3hctx_p >= TFRC_SMALLEST_P) {
158 hctx->ccid3hctx_x_calc = tfrc_calc_x(hctx->ccid3hctx_s,
161 hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_calc,
162 2 * hctx->ccid3hctx_x_recv),
164 TFRC_MAX_BACK_OFF_TIME));
168 dccp_timestamp(sk, &now);
169 if (timeval_delta(&now, &hctx->ccid3hctx_t_ld) >=
170 hctx->ccid3hctx_rtt) {
171 hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_recv,
172 hctx->ccid3hctx_x) * 2,
173 usecs_div(hctx->ccid3hctx_s,
174 hctx->ccid3hctx_rtt));
175 hctx->ccid3hctx_t_ld = now;
180 static void ccid3_hc_tx_no_feedback_timer(unsigned long data)
182 struct sock *sk = (struct sock *)data;
183 unsigned long next_tmout = 0;
184 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
187 if (sock_owned_by_user(sk)) {
188 /* Try again later. */
189 /* XXX: set some sensible MIB */
190 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
195 ccid3_pr_debug("%s, sk=%p, state=%s\n", dccp_role(sk), sk,
196 ccid3_tx_state_name(hctx->ccid3hctx_state));
198 switch (hctx->ccid3hctx_state) {
199 case TFRC_SSTATE_TERM:
201 case TFRC_SSTATE_NO_FBACK:
202 /* Halve send rate */
203 hctx->ccid3hctx_x /= 2;
204 if (hctx->ccid3hctx_x < (hctx->ccid3hctx_s /
205 TFRC_MAX_BACK_OFF_TIME))
206 hctx->ccid3hctx_x = (hctx->ccid3hctx_s /
207 TFRC_MAX_BACK_OFF_TIME);
209 ccid3_pr_debug("%s, sk=%p, state=%s, updated tx rate to %d "
212 ccid3_tx_state_name(hctx->ccid3hctx_state),
214 next_tmout = max_t(u32, 2 * usecs_div(hctx->ccid3hctx_s,
216 TFRC_INITIAL_TIMEOUT);
218 * FIXME - not sure above calculation is correct. See section
219 * 5 of CCID3 11 should adjust tx_t_ipi and double that to
223 case TFRC_SSTATE_FBACK:
225 * Check if IDLE since last timeout and recv rate is less than
228 if (!hctx->ccid3hctx_idle ||
229 (hctx->ccid3hctx_x_recv >=
230 4 * usecs_div(hctx->ccid3hctx_s, hctx->ccid3hctx_rtt))) {
231 ccid3_pr_debug("%s, sk=%p, state=%s, not idle\n",
233 ccid3_tx_state_name(hctx->ccid3hctx_state));
234 /* Halve sending rate */
236 /* If (X_calc > 2 * X_recv)
237 * X_recv = max(X_recv / 2, s / (2 * t_mbi));
239 * X_recv = X_calc / 4;
241 BUG_ON(hctx->ccid3hctx_p >= TFRC_SMALLEST_P &&
242 hctx->ccid3hctx_x_calc == 0);
244 /* check also if p is zero -> x_calc is infinity? */
245 if (hctx->ccid3hctx_p < TFRC_SMALLEST_P ||
246 hctx->ccid3hctx_x_calc > 2 * hctx->ccid3hctx_x_recv)
247 hctx->ccid3hctx_x_recv = max_t(u32, hctx->ccid3hctx_x_recv / 2,
248 hctx->ccid3hctx_s / (2 * TFRC_MAX_BACK_OFF_TIME));
250 hctx->ccid3hctx_x_recv = hctx->ccid3hctx_x_calc / 4;
252 /* Update sending rate */
253 ccid3_hc_tx_update_x(sk);
256 * Schedule no feedback timer to expire in
257 * max(4 * R, 2 * s / X)
259 next_tmout = max_t(u32, hctx->ccid3hctx_t_rto,
260 2 * usecs_div(hctx->ccid3hctx_s,
264 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
265 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
270 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
271 jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
272 hctx->ccid3hctx_idle = 1;
278 static int ccid3_hc_tx_send_packet(struct sock *sk,
279 struct sk_buff *skb, int len)
281 struct dccp_sock *dp = dccp_sk(sk);
282 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
283 struct dccp_tx_hist_entry *new_packet;
288 BUG_ON(hctx == NULL || hctx->ccid3hctx_state == TFRC_SSTATE_TERM);
290 /* Check if pure ACK or Terminating*/
292 * XXX: We only call this function for DATA and DATAACK, on, these
293 * packets can have zero length, but why the comment about "pure ACK"?
295 if (unlikely(len == 0))
298 /* See if last packet allocated was not sent */
299 new_packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
300 if (new_packet == NULL || new_packet->dccphtx_sent) {
301 new_packet = dccp_tx_hist_entry_new(ccid3_tx_hist,
305 if (unlikely(new_packet == NULL)) {
306 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, not enough "
307 "mem to add to history, send refused\n",
308 __FUNCTION__, dccp_role(sk), sk);
312 dccp_tx_hist_add_entry(&hctx->ccid3hctx_hist, new_packet);
315 dccp_timestamp(sk, &now);
317 switch (hctx->ccid3hctx_state) {
318 case TFRC_SSTATE_NO_SENT:
319 hctx->ccid3hctx_no_feedback_timer.function = ccid3_hc_tx_no_feedback_timer;
320 hctx->ccid3hctx_no_feedback_timer.data = (unsigned long)sk;
321 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
322 jiffies + usecs_to_jiffies(TFRC_INITIAL_TIMEOUT));
323 hctx->ccid3hctx_last_win_count = 0;
324 hctx->ccid3hctx_t_last_win_count = now;
325 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_NO_FBACK);
326 hctx->ccid3hctx_t_ipi = TFRC_INITIAL_IPI;
328 /* Set nominal send time for initial packet */
329 hctx->ccid3hctx_t_nom = now;
330 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
331 hctx->ccid3hctx_t_ipi);
332 ccid3_calc_new_delta(hctx);
335 case TFRC_SSTATE_NO_FBACK:
336 case TFRC_SSTATE_FBACK:
337 delay = (timeval_delta(&now, &hctx->ccid3hctx_t_nom) -
338 hctx->ccid3hctx_delta);
340 /* divide by -1000 is to convert to ms and get sign right */
341 rc = delay > 0 ? delay : 0;
344 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
345 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
351 /* Can we send? if so add options and add to packet history */
353 dp->dccps_hc_tx_insert_options = 1;
354 new_packet->dccphtx_ccval =
355 DCCP_SKB_CB(skb)->dccpd_ccval =
356 hctx->ccid3hctx_last_win_count;
362 static void ccid3_hc_tx_packet_sent(struct sock *sk, int more, int len)
364 const struct dccp_sock *dp = dccp_sk(sk);
365 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
368 BUG_ON(hctx == NULL || hctx->ccid3hctx_state == TFRC_SSTATE_TERM);
370 dccp_timestamp(sk, &now);
372 /* check if we have sent a data packet */
374 unsigned long quarter_rtt;
375 struct dccp_tx_hist_entry *packet;
377 packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
378 if (unlikely(packet == NULL)) {
379 LIMIT_NETDEBUG(KERN_WARNING "%s: packet doesn't "
380 "exists in history!\n", __FUNCTION__);
383 if (unlikely(packet->dccphtx_sent)) {
384 LIMIT_NETDEBUG(KERN_WARNING "%s: no unsent packet in "
385 "history!\n", __FUNCTION__);
388 packet->dccphtx_tstamp = now;
389 packet->dccphtx_seqno = dp->dccps_gss;
391 * Check if win_count have changed
392 * Algorithm in "8.1. Window Counter Valuer" in
393 * draft-ietf-dccp-ccid3-11.txt
395 quarter_rtt = timeval_delta(&now, &hctx->ccid3hctx_t_last_win_count);
396 if (likely(hctx->ccid3hctx_rtt > 8))
397 quarter_rtt /= hctx->ccid3hctx_rtt / 4;
399 if (quarter_rtt > 0) {
400 hctx->ccid3hctx_t_last_win_count = now;
401 hctx->ccid3hctx_last_win_count = (hctx->ccid3hctx_last_win_count +
402 min_t(unsigned long, quarter_rtt, 5)) % 16;
403 ccid3_pr_debug("%s, sk=%p, window changed from "
406 packet->dccphtx_ccval,
407 hctx->ccid3hctx_last_win_count);
410 hctx->ccid3hctx_idle = 0;
411 packet->dccphtx_rtt = hctx->ccid3hctx_rtt;
412 packet->dccphtx_sent = 1;
414 ccid3_pr_debug("%s, sk=%p, seqno=%llu NOT inserted!\n",
415 dccp_role(sk), sk, dp->dccps_gss);
417 switch (hctx->ccid3hctx_state) {
418 case TFRC_SSTATE_NO_SENT:
419 /* if first wasn't pure ack */
421 printk(KERN_CRIT "%s: %s, First packet sent is noted "
422 "as a data packet\n",
423 __FUNCTION__, dccp_role(sk));
425 case TFRC_SSTATE_NO_FBACK:
426 case TFRC_SSTATE_FBACK:
428 hctx->ccid3hctx_t_nom = now;
429 ccid3_calc_new_t_ipi(hctx);
430 ccid3_calc_new_delta(hctx);
431 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
432 hctx->ccid3hctx_t_ipi);
436 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
437 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
443 static void ccid3_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
445 const struct dccp_sock *dp = dccp_sk(sk);
446 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
447 struct ccid3_options_received *opt_recv;
448 struct dccp_tx_hist_entry *packet;
450 unsigned long next_tmout;
456 BUG_ON(hctx == NULL || hctx->ccid3hctx_state == TFRC_SSTATE_TERM);
458 /* we are only interested in ACKs */
459 if (!(DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK ||
460 DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_DATAACK))
463 opt_recv = &hctx->ccid3hctx_options_received;
465 t_elapsed = dp->dccps_options_received.dccpor_elapsed_time * 10;
466 x_recv = opt_recv->ccid3or_receive_rate;
467 pinv = opt_recv->ccid3or_loss_event_rate;
469 switch (hctx->ccid3hctx_state) {
470 case TFRC_SSTATE_NO_SENT:
471 /* FIXME: what to do here? */
473 case TFRC_SSTATE_NO_FBACK:
474 case TFRC_SSTATE_FBACK:
475 /* Calculate new round trip sample by
476 * R_sample = (now - t_recvdata) - t_delay */
477 /* get t_recvdata from history */
478 packet = dccp_tx_hist_find_entry(&hctx->ccid3hctx_hist,
479 DCCP_SKB_CB(skb)->dccpd_ack_seq);
480 if (unlikely(packet == NULL)) {
481 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, seqno "
482 "%llu(%s) does't exist in history!\n",
483 __FUNCTION__, dccp_role(sk), sk,
484 (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
485 dccp_packet_name(DCCP_SKB_CB(skb)->dccpd_type));
490 dccp_timestamp(sk, &now);
491 r_sample = timeval_delta(&now, &packet->dccphtx_tstamp);
492 if (unlikely(r_sample <= t_elapsed))
493 LIMIT_NETDEBUG(KERN_WARNING "%s: r_sample=%uus, "
495 __FUNCTION__, r_sample, t_elapsed);
497 r_sample -= t_elapsed;
499 /* Update RTT estimate by
500 * If (No feedback recv)
503 * R = q * R + (1 - q) * R_sample;
505 * q is a constant, RFC 3448 recomments 0.9
507 if (hctx->ccid3hctx_state == TFRC_SSTATE_NO_FBACK) {
508 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_FBACK);
509 hctx->ccid3hctx_rtt = r_sample;
511 hctx->ccid3hctx_rtt = (hctx->ccid3hctx_rtt * 9) / 10 +
514 ccid3_pr_debug("%s, sk=%p, New RTT estimate=%uus, "
515 "r_sample=%us\n", dccp_role(sk), sk,
516 hctx->ccid3hctx_rtt, r_sample);
518 /* Update timeout interval */
519 hctx->ccid3hctx_t_rto = max_t(u32, 4 * hctx->ccid3hctx_rtt,
522 /* Update receive rate */
523 hctx->ccid3hctx_x_recv = x_recv;/* X_recv in bytes per sec */
525 /* Update loss event rate */
526 if (pinv == ~0 || pinv == 0)
527 hctx->ccid3hctx_p = 0;
529 hctx->ccid3hctx_p = 1000000 / pinv;
531 if (hctx->ccid3hctx_p < TFRC_SMALLEST_P) {
532 hctx->ccid3hctx_p = TFRC_SMALLEST_P;
533 ccid3_pr_debug("%s, sk=%p, Smallest p used!\n",
538 /* unschedule no feedback timer */
539 sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
541 /* Update sending rate */
542 ccid3_hc_tx_update_x(sk);
544 /* Update next send time */
545 timeval_sub_usecs(&hctx->ccid3hctx_t_nom,
546 hctx->ccid3hctx_t_ipi);
547 ccid3_calc_new_t_ipi(hctx);
548 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
549 hctx->ccid3hctx_t_ipi);
550 ccid3_calc_new_delta(hctx);
552 /* remove all packets older than the one acked from history */
553 dccp_tx_hist_purge_older(ccid3_tx_hist,
554 &hctx->ccid3hctx_hist, packet);
556 * As we have calculated new ipi, delta, t_nom it is possible that
557 * we now can send a packet, so wake up dccp_wait_for_ccids.
559 sk->sk_write_space(sk);
562 * Schedule no feedback timer to expire in
563 * max(4 * R, 2 * s / X)
565 next_tmout = max(hctx->ccid3hctx_t_rto,
566 2 * usecs_div(hctx->ccid3hctx_s,
569 ccid3_pr_debug("%s, sk=%p, Scheduled no feedback timer to "
570 "expire in %lu jiffies (%luus)\n",
572 usecs_to_jiffies(next_tmout), next_tmout);
574 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
575 jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
578 hctx->ccid3hctx_idle = 1;
581 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
582 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
588 static void ccid3_hc_tx_insert_options(struct sock *sk, struct sk_buff *skb)
590 const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
592 BUG_ON(hctx == NULL);
594 if (!(sk->sk_state == DCCP_OPEN || sk->sk_state == DCCP_PARTOPEN))
597 DCCP_SKB_CB(skb)->dccpd_ccval = hctx->ccid3hctx_last_win_count;
600 static int ccid3_hc_tx_parse_options(struct sock *sk, unsigned char option,
601 unsigned char len, u16 idx,
602 unsigned char *value)
605 const struct dccp_sock *dp = dccp_sk(sk);
606 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
607 struct ccid3_options_received *opt_recv;
609 BUG_ON(hctx == NULL);
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:
623 if (unlikely(len != 4)) {
624 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, invalid "
625 "len for TFRC_OPT_LOSS_EVENT_RATE\n",
626 __FUNCTION__, dccp_role(sk), sk);
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:
644 if (unlikely(len != 4)) {
645 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, invalid "
646 "len for TFRC_OPT_RECEIVE_RATE\n",
647 __FUNCTION__, dccp_role(sk), sk);
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 dp->dccps_hc_tx_ccid_private = kmalloc(sizeof(*hctx), gfp_any());
667 if (dp->dccps_hc_tx_ccid_private == NULL)
670 hctx = ccid3_hc_tx_sk(sk);
671 memset(hctx, 0, sizeof(*hctx));
673 if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
674 dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
675 hctx->ccid3hctx_s = dp->dccps_packet_size;
677 hctx->ccid3hctx_s = TFRC_STD_PACKET_SIZE;
679 /* Set transmission rate to 1 packet per second */
680 hctx->ccid3hctx_x = hctx->ccid3hctx_s;
681 hctx->ccid3hctx_t_rto = USEC_PER_SEC;
682 hctx->ccid3hctx_state = TFRC_SSTATE_NO_SENT;
683 INIT_LIST_HEAD(&hctx->ccid3hctx_hist);
684 init_timer(&hctx->ccid3hctx_no_feedback_timer);
689 static void ccid3_hc_tx_exit(struct sock *sk)
691 struct dccp_sock *dp = dccp_sk(sk);
692 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
694 BUG_ON(hctx == NULL);
696 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_TERM);
697 sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
699 /* Empty packet history */
700 dccp_tx_hist_purge(ccid3_tx_hist, &hctx->ccid3hctx_hist);
702 kfree(dp->dccps_hc_tx_ccid_private);
703 dp->dccps_hc_tx_ccid_private = NULL;
707 * RX Half Connection methods
710 /* TFRC receiver states */
711 enum ccid3_hc_rx_states {
712 TFRC_RSTATE_NO_DATA = 1,
714 TFRC_RSTATE_TERM = 127,
718 static const char *ccid3_rx_state_name(enum ccid3_hc_rx_states state)
720 static char *ccid3_rx_state_names[] = {
721 [TFRC_RSTATE_NO_DATA] = "NO_DATA",
722 [TFRC_RSTATE_DATA] = "DATA",
723 [TFRC_RSTATE_TERM] = "TERM",
726 return ccid3_rx_state_names[state];
730 static inline void ccid3_hc_rx_set_state(struct sock *sk,
731 enum ccid3_hc_rx_states state)
733 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
734 enum ccid3_hc_rx_states oldstate = hcrx->ccid3hcrx_state;
736 ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
737 dccp_role(sk), sk, ccid3_rx_state_name(oldstate),
738 ccid3_rx_state_name(state));
739 WARN_ON(state == oldstate);
740 hcrx->ccid3hcrx_state = state;
743 static void ccid3_hc_rx_send_feedback(struct sock *sk)
745 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
746 struct dccp_sock *dp = dccp_sk(sk);
747 struct dccp_rx_hist_entry *packet;
750 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
752 dccp_timestamp(sk, &now);
754 switch (hcrx->ccid3hcrx_state) {
755 case TFRC_RSTATE_NO_DATA:
756 hcrx->ccid3hcrx_x_recv = 0;
758 case TFRC_RSTATE_DATA: {
759 const u32 delta = timeval_delta(&now,
760 &hcrx->ccid3hcrx_tstamp_last_feedback);
761 hcrx->ccid3hcrx_x_recv = usecs_div(hcrx->ccid3hcrx_bytes_recv,
766 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
767 __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
772 packet = dccp_rx_hist_find_data_packet(&hcrx->ccid3hcrx_hist);
773 if (unlikely(packet == NULL)) {
774 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, no data packet "
776 __FUNCTION__, dccp_role(sk), sk);
780 hcrx->ccid3hcrx_tstamp_last_feedback = now;
781 hcrx->ccid3hcrx_last_counter = packet->dccphrx_ccval;
782 hcrx->ccid3hcrx_seqno_last_counter = packet->dccphrx_seqno;
783 hcrx->ccid3hcrx_bytes_recv = 0;
785 /* Convert to multiples of 10us */
786 hcrx->ccid3hcrx_elapsed_time =
787 timeval_delta(&now, &packet->dccphrx_tstamp) / 10;
788 if (hcrx->ccid3hcrx_p == 0)
789 hcrx->ccid3hcrx_pinv = ~0;
791 hcrx->ccid3hcrx_pinv = 1000000 / hcrx->ccid3hcrx_p;
792 dp->dccps_hc_rx_insert_options = 1;
796 static void ccid3_hc_rx_insert_options(struct sock *sk, struct sk_buff *skb)
798 const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
801 BUG_ON(hcrx == NULL);
803 if (!(sk->sk_state == DCCP_OPEN || sk->sk_state == DCCP_PARTOPEN))
806 DCCP_SKB_CB(skb)->dccpd_ccval = hcrx->ccid3hcrx_last_counter;
808 if (dccp_packet_without_ack(skb))
811 if (hcrx->ccid3hcrx_elapsed_time != 0)
812 dccp_insert_option_elapsed_time(sk, skb,
813 hcrx->ccid3hcrx_elapsed_time);
814 dccp_insert_option_timestamp(sk, skb);
815 x_recv = htonl(hcrx->ccid3hcrx_x_recv);
816 pinv = htonl(hcrx->ccid3hcrx_pinv);
817 dccp_insert_option(sk, skb, TFRC_OPT_LOSS_EVENT_RATE,
818 &pinv, sizeof(pinv));
819 dccp_insert_option(sk, skb, TFRC_OPT_RECEIVE_RATE,
820 &x_recv, sizeof(x_recv));
823 /* calculate first loss interval
825 * returns estimated loss interval in usecs */
827 static u32 ccid3_hc_rx_calc_first_li(struct sock *sk)
829 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
830 struct dccp_rx_hist_entry *entry, *next, *tail = NULL;
831 u32 rtt, delta, x_recv, fval, p, tmp2;
832 struct timeval tstamp = { 0, };
838 list_for_each_entry_safe(entry, next, &hcrx->ccid3hcrx_hist,
840 if (dccp_rx_hist_entry_data_packet(entry)) {
845 tstamp = entry->dccphrx_tstamp;
846 win_count = entry->dccphrx_ccval;
850 interval = win_count - entry->dccphrx_ccval;
852 interval += TFRC_WIN_COUNT_LIMIT;
860 if (unlikely(step == 0)) {
861 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, packet history "
862 "contains no data packets!\n",
863 __FUNCTION__, dccp_role(sk), sk);
867 if (unlikely(interval == 0)) {
868 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, Could not find a "
869 "win_count interval > 0. Defaulting to 1\n",
870 __FUNCTION__, dccp_role(sk), sk);
874 rtt = timeval_delta(&tstamp, &tail->dccphrx_tstamp) * 4 / interval;
875 ccid3_pr_debug("%s, sk=%p, approximated RTT to %uus\n",
876 dccp_role(sk), sk, rtt);
880 dccp_timestamp(sk, &tstamp);
881 delta = timeval_delta(&tstamp, &hcrx->ccid3hcrx_tstamp_last_feedback);
882 x_recv = usecs_div(hcrx->ccid3hcrx_bytes_recv, delta);
884 tmp1 = (u64)x_recv * (u64)rtt;
885 do_div(tmp1,10000000);
887 fval = (hcrx->ccid3hcrx_s * 100000) / tmp2;
888 /* do not alter order above or you will get overflow on 32 bit */
889 p = tfrc_calc_x_reverse_lookup(fval);
890 ccid3_pr_debug("%s, sk=%p, receive rate=%u bytes/s, implied "
891 "loss rate=%u\n", dccp_role(sk), sk, x_recv, p);
899 static void ccid3_hc_rx_update_li(struct sock *sk, u64 seq_loss, u8 win_loss)
901 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
903 if (seq_loss != DCCP_MAX_SEQNO + 1 &&
904 list_empty(&hcrx->ccid3hcrx_li_hist)) {
905 struct dccp_li_hist_entry *li_tail;
907 li_tail = dccp_li_hist_interval_new(ccid3_li_hist,
908 &hcrx->ccid3hcrx_li_hist,
912 li_tail->dccplih_interval = ccid3_hc_rx_calc_first_li(sk);
914 LIMIT_NETDEBUG(KERN_WARNING "%s: FIXME: find end of "
915 "interval\n", __FUNCTION__);
918 static void ccid3_hc_rx_detect_loss(struct sock *sk)
920 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
922 const u64 seq_loss = dccp_rx_hist_detect_loss(&hcrx->ccid3hcrx_hist,
923 &hcrx->ccid3hcrx_li_hist,
926 ccid3_hc_rx_update_li(sk, seq_loss, win_loss);
929 static void ccid3_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
931 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
932 const struct dccp_options_received *opt_recv;
933 struct dccp_rx_hist_entry *packet;
936 u32 p_prev, r_sample, t_elapsed;
939 BUG_ON(hcrx == NULL ||
940 !(hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA ||
941 hcrx->ccid3hcrx_state == TFRC_RSTATE_DATA));
943 opt_recv = &dccp_sk(sk)->dccps_options_received;
945 switch (DCCP_SKB_CB(skb)->dccpd_type) {
947 if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
949 case DCCP_PKT_DATAACK:
950 if (opt_recv->dccpor_timestamp_echo == 0)
952 p_prev = hcrx->ccid3hcrx_rtt;
953 dccp_timestamp(sk, &now);
954 timeval_sub_usecs(&now, opt_recv->dccpor_timestamp_echo * 10);
955 r_sample = timeval_usecs(&now);
956 t_elapsed = opt_recv->dccpor_elapsed_time * 10;
958 if (unlikely(r_sample <= t_elapsed))
959 LIMIT_NETDEBUG(KERN_WARNING "%s: r_sample=%uus, "
961 __FUNCTION__, r_sample, t_elapsed);
963 r_sample -= t_elapsed;
965 if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
966 hcrx->ccid3hcrx_rtt = r_sample;
968 hcrx->ccid3hcrx_rtt = (hcrx->ccid3hcrx_rtt * 9) / 10 +
971 if (p_prev != hcrx->ccid3hcrx_rtt)
972 ccid3_pr_debug("%s, New RTT=%luus, elapsed time=%u\n",
973 dccp_role(sk), hcrx->ccid3hcrx_rtt,
974 opt_recv->dccpor_elapsed_time);
978 default: /* We're not interested in other packet types, move along */
982 packet = dccp_rx_hist_entry_new(ccid3_rx_hist, sk, opt_recv->dccpor_ndp,
984 if (unlikely(packet == NULL)) {
985 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, Not enough mem to "
986 "add rx packet to history, consider it lost!\n",
987 __FUNCTION__, dccp_role(sk), sk);
991 win_count = packet->dccphrx_ccval;
993 ins = dccp_rx_hist_add_packet(ccid3_rx_hist, &hcrx->ccid3hcrx_hist,
994 &hcrx->ccid3hcrx_li_hist, packet);
996 if (DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK)
999 switch (hcrx->ccid3hcrx_state) {
1000 case TFRC_RSTATE_NO_DATA:
1001 ccid3_pr_debug("%s, sk=%p(%s), skb=%p, sending initial "
1004 dccp_state_name(sk->sk_state), skb);
1005 ccid3_hc_rx_send_feedback(sk);
1006 ccid3_hc_rx_set_state(sk, TFRC_RSTATE_DATA);
1008 case TFRC_RSTATE_DATA:
1009 hcrx->ccid3hcrx_bytes_recv += skb->len -
1010 dccp_hdr(skb)->dccph_doff * 4;
1014 dccp_timestamp(sk, &now);
1015 if (timeval_delta(&now, &hcrx->ccid3hcrx_tstamp_last_ack) >=
1016 hcrx->ccid3hcrx_rtt) {
1017 hcrx->ccid3hcrx_tstamp_last_ack = now;
1018 ccid3_hc_rx_send_feedback(sk);
1022 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
1023 __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
1028 /* Dealing with packet loss */
1029 ccid3_pr_debug("%s, sk=%p(%s), data loss! Reacting...\n",
1030 dccp_role(sk), sk, dccp_state_name(sk->sk_state));
1032 ccid3_hc_rx_detect_loss(sk);
1033 p_prev = hcrx->ccid3hcrx_p;
1035 /* Calculate loss event rate */
1036 if (!list_empty(&hcrx->ccid3hcrx_li_hist))
1037 /* Scaling up by 1000000 as fixed decimal */
1038 hcrx->ccid3hcrx_p = 1000000 / dccp_li_hist_calc_i_mean(&hcrx->ccid3hcrx_li_hist);
1040 if (hcrx->ccid3hcrx_p > p_prev) {
1041 ccid3_hc_rx_send_feedback(sk);
1046 static int ccid3_hc_rx_init(struct sock *sk)
1048 struct dccp_sock *dp = dccp_sk(sk);
1049 struct ccid3_hc_rx_sock *hcrx;
1051 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
1053 dp->dccps_hc_rx_ccid_private = kmalloc(sizeof(*hcrx), gfp_any());
1054 if (dp->dccps_hc_rx_ccid_private == NULL)
1057 hcrx = ccid3_hc_rx_sk(sk);
1058 memset(hcrx, 0, sizeof(*hcrx));
1060 if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
1061 dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
1062 hcrx->ccid3hcrx_s = dp->dccps_packet_size;
1064 hcrx->ccid3hcrx_s = TFRC_STD_PACKET_SIZE;
1066 hcrx->ccid3hcrx_state = TFRC_RSTATE_NO_DATA;
1067 INIT_LIST_HEAD(&hcrx->ccid3hcrx_hist);
1068 INIT_LIST_HEAD(&hcrx->ccid3hcrx_li_hist);
1069 dccp_timestamp(sk, &hcrx->ccid3hcrx_tstamp_last_ack);
1070 hcrx->ccid3hcrx_tstamp_last_feedback = hcrx->ccid3hcrx_tstamp_last_ack;
1071 hcrx->ccid3hcrx_rtt = 5000; /* XXX 5ms for now... */
1075 static void ccid3_hc_rx_exit(struct sock *sk)
1077 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
1078 struct dccp_sock *dp = dccp_sk(sk);
1080 BUG_ON(hcrx == NULL);
1082 ccid3_hc_rx_set_state(sk, TFRC_RSTATE_TERM);
1084 /* Empty packet history */
1085 dccp_rx_hist_purge(ccid3_rx_hist, &hcrx->ccid3hcrx_hist);
1087 /* Empty loss interval history */
1088 dccp_li_hist_purge(ccid3_li_hist, &hcrx->ccid3hcrx_li_hist);
1090 kfree(dp->dccps_hc_rx_ccid_private);
1091 dp->dccps_hc_rx_ccid_private = NULL;
1094 static void ccid3_hc_rx_get_info(struct sock *sk, struct tcp_info *info)
1096 const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
1098 /* Listen socks doesn't have a private CCID block */
1099 if (sk->sk_state == DCCP_LISTEN)
1102 BUG_ON(hcrx == NULL);
1104 info->tcpi_ca_state = hcrx->ccid3hcrx_state;
1105 info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1106 info->tcpi_rcv_rtt = hcrx->ccid3hcrx_rtt;
1109 static void ccid3_hc_tx_get_info(struct sock *sk, struct tcp_info *info)
1111 const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
1113 /* Listen socks doesn't have a private CCID block */
1114 if (sk->sk_state == DCCP_LISTEN)
1117 BUG_ON(hctx == NULL);
1119 info->tcpi_rto = hctx->ccid3hctx_t_rto;
1120 info->tcpi_rtt = hctx->ccid3hctx_rtt;
1123 static int ccid3_hc_rx_getsockopt(struct sock *sk, const int optname, int len,
1124 u32 __user *optval, int __user *optlen)
1126 const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
1129 /* Listen socks doesn't have a private CCID block */
1130 if (sk->sk_state == DCCP_LISTEN)
1134 case DCCP_SOCKOPT_CCID_RX_INFO:
1135 if (len < sizeof(hcrx->ccid3hcrx_tfrc))
1137 len = sizeof(hcrx->ccid3hcrx_tfrc);
1138 val = &hcrx->ccid3hcrx_tfrc;
1141 return -ENOPROTOOPT;
1144 if (put_user(len, optlen) || copy_to_user(optval, val, len))
1150 static int ccid3_hc_tx_getsockopt(struct sock *sk, const int optname, int len,
1151 u32 __user *optval, int __user *optlen)
1153 const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
1156 /* Listen socks doesn't have a private CCID block */
1157 if (sk->sk_state == DCCP_LISTEN)
1161 case DCCP_SOCKOPT_CCID_TX_INFO:
1162 if (len < sizeof(hctx->ccid3hctx_tfrc))
1164 len = sizeof(hctx->ccid3hctx_tfrc);
1165 val = &hctx->ccid3hctx_tfrc;
1168 return -ENOPROTOOPT;
1171 if (put_user(len, optlen) || copy_to_user(optval, val, len))
1177 static struct ccid ccid3 = {
1179 .ccid_name = "ccid3",
1180 .ccid_owner = THIS_MODULE,
1181 .ccid_init = ccid3_init,
1182 .ccid_exit = ccid3_exit,
1183 .ccid_hc_tx_init = ccid3_hc_tx_init,
1184 .ccid_hc_tx_exit = ccid3_hc_tx_exit,
1185 .ccid_hc_tx_send_packet = ccid3_hc_tx_send_packet,
1186 .ccid_hc_tx_packet_sent = ccid3_hc_tx_packet_sent,
1187 .ccid_hc_tx_packet_recv = ccid3_hc_tx_packet_recv,
1188 .ccid_hc_tx_insert_options = ccid3_hc_tx_insert_options,
1189 .ccid_hc_tx_parse_options = ccid3_hc_tx_parse_options,
1190 .ccid_hc_rx_init = ccid3_hc_rx_init,
1191 .ccid_hc_rx_exit = ccid3_hc_rx_exit,
1192 .ccid_hc_rx_insert_options = ccid3_hc_rx_insert_options,
1193 .ccid_hc_rx_packet_recv = ccid3_hc_rx_packet_recv,
1194 .ccid_hc_rx_get_info = ccid3_hc_rx_get_info,
1195 .ccid_hc_tx_get_info = ccid3_hc_tx_get_info,
1196 .ccid_hc_rx_getsockopt = ccid3_hc_rx_getsockopt,
1197 .ccid_hc_tx_getsockopt = ccid3_hc_tx_getsockopt,
1200 module_param(ccid3_debug, int, 0444);
1201 MODULE_PARM_DESC(ccid3_debug, "Enable debug messages");
1203 static __init int ccid3_module_init(void)
1207 ccid3_rx_hist = dccp_rx_hist_new("ccid3");
1208 if (ccid3_rx_hist == NULL)
1211 ccid3_tx_hist = dccp_tx_hist_new("ccid3");
1212 if (ccid3_tx_hist == NULL)
1215 ccid3_li_hist = dccp_li_hist_new("ccid3");
1216 if (ccid3_li_hist == NULL)
1219 rc = ccid_register(&ccid3);
1221 goto out_free_loss_interval_history;
1225 out_free_loss_interval_history:
1226 dccp_li_hist_delete(ccid3_li_hist);
1227 ccid3_li_hist = NULL;
1229 dccp_tx_hist_delete(ccid3_tx_hist);
1230 ccid3_tx_hist = NULL;
1232 dccp_rx_hist_delete(ccid3_rx_hist);
1233 ccid3_rx_hist = NULL;
1236 module_init(ccid3_module_init);
1238 static __exit void ccid3_module_exit(void)
1240 #ifdef CONFIG_IP_DCCP_UNLOAD_HACK
1242 * Hack to use while developing, so that we get rid of the control
1243 * sock, that is what keeps a refcount on dccp.ko -acme
1245 extern void dccp_ctl_sock_exit(void);
1247 dccp_ctl_sock_exit();
1249 ccid_unregister(&ccid3);
1251 if (ccid3_tx_hist != NULL) {
1252 dccp_tx_hist_delete(ccid3_tx_hist);
1253 ccid3_tx_hist = NULL;
1255 if (ccid3_rx_hist != NULL) {
1256 dccp_rx_hist_delete(ccid3_rx_hist);
1257 ccid3_rx_hist = NULL;
1259 if (ccid3_li_hist != NULL) {
1260 dccp_li_hist_delete(ccid3_li_hist);
1261 ccid3_li_hist = NULL;
1264 module_exit(ccid3_module_exit);
1266 MODULE_AUTHOR("Ian McDonald <iam4@cs.waikato.ac.nz>, "
1267 "Arnaldo Carvalho de Melo <acme@ghostprotocols.net>");
1268 MODULE_DESCRIPTION("DCCP TFRC CCID3 CCID");
1269 MODULE_LICENSE("GPL");
1270 MODULE_ALIAS("net-dccp-ccid-3");