2 * net/dccp/ccids/ccid3.c
4 * Copyright (c) 2005 The University of Waikato, Hamilton, New Zealand.
5 * Copyright (c) 2005-6 Ian McDonald <imcdnzl@gmail.com>
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 /* TFRC sender states */
80 enum ccid3_hc_tx_states {
81 TFRC_SSTATE_NO_SENT = 1,
88 static const char *ccid3_tx_state_name(enum ccid3_hc_tx_states state)
90 static char *ccid3_state_names[] = {
91 [TFRC_SSTATE_NO_SENT] = "NO_SENT",
92 [TFRC_SSTATE_NO_FBACK] = "NO_FBACK",
93 [TFRC_SSTATE_FBACK] = "FBACK",
94 [TFRC_SSTATE_TERM] = "TERM",
97 return ccid3_state_names[state];
101 static inline void ccid3_hc_tx_set_state(struct sock *sk,
102 enum ccid3_hc_tx_states state)
104 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
105 enum ccid3_hc_tx_states oldstate = hctx->ccid3hctx_state;
107 ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
108 dccp_role(sk), sk, ccid3_tx_state_name(oldstate),
109 ccid3_tx_state_name(state));
110 WARN_ON(state == oldstate);
111 hctx->ccid3hctx_state = state;
114 /* Calculate new t_ipi (inter packet interval) by t_ipi = s / X_inst */
115 static inline void ccid3_calc_new_t_ipi(struct ccid3_hc_tx_sock *hctx)
118 * If no feedback spec says t_ipi is 1 second (set elsewhere and then
119 * doubles after every no feedback timer (separate function)
121 if (hctx->ccid3hctx_state != TFRC_SSTATE_NO_FBACK)
122 hctx->ccid3hctx_t_ipi = usecs_div(hctx->ccid3hctx_s,
126 /* Calculate new delta by delta = min(t_ipi / 2, t_gran / 2) */
127 static inline void ccid3_calc_new_delta(struct ccid3_hc_tx_sock *hctx)
129 hctx->ccid3hctx_delta = min_t(u32, hctx->ccid3hctx_t_ipi / 2,
130 TFRC_OPSYS_HALF_TIME_GRAN);
136 * x_calc = calcX(s, R, p);
137 * X = max(min(X_calc, 2 * X_recv), s / t_mbi);
139 * If (now - tld >= R)
140 * X = max(min(2 * X, 2 * X_recv), s / R);
143 static void ccid3_hc_tx_update_x(struct sock *sk)
145 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
147 /* To avoid large error in calcX */
148 if (hctx->ccid3hctx_p >= TFRC_SMALLEST_P) {
149 hctx->ccid3hctx_x_calc = tfrc_calc_x(hctx->ccid3hctx_s,
152 hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_calc,
153 2 * hctx->ccid3hctx_x_recv),
155 TFRC_MAX_BACK_OFF_TIME));
159 dccp_timestamp(sk, &now);
160 if (timeval_delta(&now, &hctx->ccid3hctx_t_ld) >=
161 hctx->ccid3hctx_rtt) {
162 hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_recv,
163 hctx->ccid3hctx_x) * 2,
164 usecs_div(hctx->ccid3hctx_s,
165 hctx->ccid3hctx_rtt));
166 hctx->ccid3hctx_t_ld = now;
171 static void ccid3_hc_tx_no_feedback_timer(unsigned long data)
173 struct sock *sk = (struct sock *)data;
174 unsigned long next_tmout = 0;
175 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
178 if (sock_owned_by_user(sk)) {
179 /* Try again later. */
180 /* XXX: set some sensible MIB */
181 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
186 ccid3_pr_debug("%s, sk=%p, state=%s\n", dccp_role(sk), sk,
187 ccid3_tx_state_name(hctx->ccid3hctx_state));
189 switch (hctx->ccid3hctx_state) {
190 case TFRC_SSTATE_TERM:
192 case TFRC_SSTATE_NO_FBACK:
193 /* Halve send rate */
194 hctx->ccid3hctx_x /= 2;
195 if (hctx->ccid3hctx_x < (hctx->ccid3hctx_s /
196 TFRC_MAX_BACK_OFF_TIME))
197 hctx->ccid3hctx_x = (hctx->ccid3hctx_s /
198 TFRC_MAX_BACK_OFF_TIME);
200 ccid3_pr_debug("%s, sk=%p, state=%s, updated tx rate to %d "
203 ccid3_tx_state_name(hctx->ccid3hctx_state),
205 next_tmout = max_t(u32, 2 * usecs_div(hctx->ccid3hctx_s,
207 TFRC_INITIAL_TIMEOUT);
209 * FIXME - not sure above calculation is correct. See section
210 * 5 of CCID3 11 should adjust tx_t_ipi and double that to
214 case TFRC_SSTATE_FBACK:
216 * Check if IDLE since last timeout and recv rate is less than
219 if (!hctx->ccid3hctx_idle ||
220 (hctx->ccid3hctx_x_recv >=
221 4 * usecs_div(hctx->ccid3hctx_s, hctx->ccid3hctx_rtt))) {
222 ccid3_pr_debug("%s, sk=%p, state=%s, not idle\n",
224 ccid3_tx_state_name(hctx->ccid3hctx_state));
225 /* Halve sending rate */
227 /* If (X_calc > 2 * X_recv)
228 * X_recv = max(X_recv / 2, s / (2 * t_mbi));
230 * X_recv = X_calc / 4;
232 BUG_ON(hctx->ccid3hctx_p >= TFRC_SMALLEST_P &&
233 hctx->ccid3hctx_x_calc == 0);
235 /* check also if p is zero -> x_calc is infinity? */
236 if (hctx->ccid3hctx_p < TFRC_SMALLEST_P ||
237 hctx->ccid3hctx_x_calc > 2 * hctx->ccid3hctx_x_recv)
238 hctx->ccid3hctx_x_recv = max_t(u32, hctx->ccid3hctx_x_recv / 2,
239 hctx->ccid3hctx_s / (2 * TFRC_MAX_BACK_OFF_TIME));
241 hctx->ccid3hctx_x_recv = hctx->ccid3hctx_x_calc / 4;
243 /* Update sending rate */
244 ccid3_hc_tx_update_x(sk);
247 * Schedule no feedback timer to expire in
248 * max(4 * R, 2 * s / X)
250 next_tmout = max_t(u32, hctx->ccid3hctx_t_rto,
251 2 * usecs_div(hctx->ccid3hctx_s,
255 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
256 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
261 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
262 jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
263 hctx->ccid3hctx_idle = 1;
269 static int ccid3_hc_tx_send_packet(struct sock *sk,
270 struct sk_buff *skb, int len)
272 struct dccp_sock *dp = dccp_sk(sk);
273 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
274 struct dccp_tx_hist_entry *new_packet;
279 BUG_ON(hctx == NULL || hctx->ccid3hctx_state == TFRC_SSTATE_TERM);
281 /* Check if pure ACK or Terminating*/
283 * XXX: We only call this function for DATA and DATAACK, on, these
284 * packets can have zero length, but why the comment about "pure ACK"?
286 if (unlikely(len == 0))
289 /* See if last packet allocated was not sent */
290 new_packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
291 if (new_packet == NULL || new_packet->dccphtx_sent) {
292 new_packet = dccp_tx_hist_entry_new(ccid3_tx_hist,
296 if (unlikely(new_packet == NULL)) {
297 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, not enough "
298 "mem to add to history, send refused\n",
299 __FUNCTION__, dccp_role(sk), sk);
303 dccp_tx_hist_add_entry(&hctx->ccid3hctx_hist, new_packet);
306 dccp_timestamp(sk, &now);
308 switch (hctx->ccid3hctx_state) {
309 case TFRC_SSTATE_NO_SENT:
310 hctx->ccid3hctx_no_feedback_timer.function = ccid3_hc_tx_no_feedback_timer;
311 hctx->ccid3hctx_no_feedback_timer.data = (unsigned long)sk;
312 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
313 jiffies + usecs_to_jiffies(TFRC_INITIAL_TIMEOUT));
314 hctx->ccid3hctx_last_win_count = 0;
315 hctx->ccid3hctx_t_last_win_count = now;
316 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_NO_FBACK);
317 hctx->ccid3hctx_t_ipi = TFRC_INITIAL_IPI;
319 /* Set nominal send time for initial packet */
320 hctx->ccid3hctx_t_nom = now;
321 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
322 hctx->ccid3hctx_t_ipi);
323 ccid3_calc_new_delta(hctx);
326 case TFRC_SSTATE_NO_FBACK:
327 case TFRC_SSTATE_FBACK:
328 delay = (timeval_delta(&now, &hctx->ccid3hctx_t_nom) -
329 hctx->ccid3hctx_delta);
331 /* divide by -1000 is to convert to ms and get sign right */
332 rc = delay > 0 ? delay : 0;
335 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
336 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
342 /* Can we send? if so add options and add to packet history */
344 dp->dccps_hc_tx_insert_options = 1;
345 new_packet->dccphtx_ccval =
346 DCCP_SKB_CB(skb)->dccpd_ccval =
347 hctx->ccid3hctx_last_win_count;
353 static void ccid3_hc_tx_packet_sent(struct sock *sk, int more, int len)
355 const struct dccp_sock *dp = dccp_sk(sk);
356 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
359 BUG_ON(hctx == NULL || hctx->ccid3hctx_state == TFRC_SSTATE_TERM);
361 dccp_timestamp(sk, &now);
363 /* check if we have sent a data packet */
365 unsigned long quarter_rtt;
366 struct dccp_tx_hist_entry *packet;
368 packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
369 if (unlikely(packet == NULL)) {
370 LIMIT_NETDEBUG(KERN_WARNING "%s: packet doesn't "
371 "exists in history!\n", __FUNCTION__);
374 if (unlikely(packet->dccphtx_sent)) {
375 LIMIT_NETDEBUG(KERN_WARNING "%s: no unsent packet in "
376 "history!\n", __FUNCTION__);
379 packet->dccphtx_tstamp = now;
380 packet->dccphtx_seqno = dp->dccps_gss;
382 * Check if win_count have changed
383 * Algorithm in "8.1. Window Counter Valuer" in
384 * draft-ietf-dccp-ccid3-11.txt
386 quarter_rtt = timeval_delta(&now, &hctx->ccid3hctx_t_last_win_count);
387 if (likely(hctx->ccid3hctx_rtt > 8))
388 quarter_rtt /= hctx->ccid3hctx_rtt / 4;
390 if (quarter_rtt > 0) {
391 hctx->ccid3hctx_t_last_win_count = now;
392 hctx->ccid3hctx_last_win_count = (hctx->ccid3hctx_last_win_count +
393 min_t(unsigned long, quarter_rtt, 5)) % 16;
394 ccid3_pr_debug("%s, sk=%p, window changed from "
397 packet->dccphtx_ccval,
398 hctx->ccid3hctx_last_win_count);
401 hctx->ccid3hctx_idle = 0;
402 packet->dccphtx_rtt = hctx->ccid3hctx_rtt;
403 packet->dccphtx_sent = 1;
405 ccid3_pr_debug("%s, sk=%p, seqno=%llu NOT inserted!\n",
406 dccp_role(sk), sk, dp->dccps_gss);
408 switch (hctx->ccid3hctx_state) {
409 case TFRC_SSTATE_NO_SENT:
410 /* if first wasn't pure ack */
412 printk(KERN_CRIT "%s: %s, First packet sent is noted "
413 "as a data packet\n",
414 __FUNCTION__, dccp_role(sk));
416 case TFRC_SSTATE_NO_FBACK:
417 case TFRC_SSTATE_FBACK:
419 hctx->ccid3hctx_t_nom = now;
420 ccid3_calc_new_t_ipi(hctx);
421 ccid3_calc_new_delta(hctx);
422 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
423 hctx->ccid3hctx_t_ipi);
427 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
428 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
434 static void ccid3_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
436 const struct dccp_sock *dp = dccp_sk(sk);
437 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
438 struct ccid3_options_received *opt_recv;
439 struct dccp_tx_hist_entry *packet;
441 unsigned long next_tmout;
447 BUG_ON(hctx == NULL || hctx->ccid3hctx_state == TFRC_SSTATE_TERM);
449 /* we are only interested in ACKs */
450 if (!(DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK ||
451 DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_DATAACK))
454 opt_recv = &hctx->ccid3hctx_options_received;
456 t_elapsed = dp->dccps_options_received.dccpor_elapsed_time * 10;
457 x_recv = opt_recv->ccid3or_receive_rate;
458 pinv = opt_recv->ccid3or_loss_event_rate;
460 switch (hctx->ccid3hctx_state) {
461 case TFRC_SSTATE_NO_SENT:
462 /* FIXME: what to do here? */
464 case TFRC_SSTATE_NO_FBACK:
465 case TFRC_SSTATE_FBACK:
466 /* Calculate new round trip sample by
467 * R_sample = (now - t_recvdata) - t_delay */
468 /* get t_recvdata from history */
469 packet = dccp_tx_hist_find_entry(&hctx->ccid3hctx_hist,
470 DCCP_SKB_CB(skb)->dccpd_ack_seq);
471 if (unlikely(packet == NULL)) {
472 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, seqno "
473 "%llu(%s) does't exist in history!\n",
474 __FUNCTION__, dccp_role(sk), sk,
475 (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
476 dccp_packet_name(DCCP_SKB_CB(skb)->dccpd_type));
481 dccp_timestamp(sk, &now);
482 r_sample = timeval_delta(&now, &packet->dccphtx_tstamp);
483 if (unlikely(r_sample <= t_elapsed))
484 LIMIT_NETDEBUG(KERN_WARNING "%s: r_sample=%uus, "
486 __FUNCTION__, r_sample, t_elapsed);
488 r_sample -= t_elapsed;
490 /* Update RTT estimate by
491 * If (No feedback recv)
494 * R = q * R + (1 - q) * R_sample;
496 * q is a constant, RFC 3448 recomments 0.9
498 if (hctx->ccid3hctx_state == TFRC_SSTATE_NO_FBACK) {
499 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_FBACK);
500 hctx->ccid3hctx_rtt = r_sample;
502 hctx->ccid3hctx_rtt = (hctx->ccid3hctx_rtt * 9) / 10 +
505 ccid3_pr_debug("%s, sk=%p, New RTT estimate=%uus, "
506 "r_sample=%us\n", dccp_role(sk), sk,
507 hctx->ccid3hctx_rtt, r_sample);
509 /* Update timeout interval */
510 hctx->ccid3hctx_t_rto = max_t(u32, 4 * hctx->ccid3hctx_rtt,
513 /* Update receive rate */
514 hctx->ccid3hctx_x_recv = x_recv;/* X_recv in bytes per sec */
516 /* Update loss event rate */
517 if (pinv == ~0 || pinv == 0)
518 hctx->ccid3hctx_p = 0;
520 hctx->ccid3hctx_p = 1000000 / pinv;
522 if (hctx->ccid3hctx_p < TFRC_SMALLEST_P) {
523 hctx->ccid3hctx_p = TFRC_SMALLEST_P;
524 ccid3_pr_debug("%s, sk=%p, Smallest p used!\n",
529 /* unschedule no feedback timer */
530 sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
532 /* Update sending rate */
533 ccid3_hc_tx_update_x(sk);
535 /* Update next send time */
536 timeval_sub_usecs(&hctx->ccid3hctx_t_nom,
537 hctx->ccid3hctx_t_ipi);
538 ccid3_calc_new_t_ipi(hctx);
539 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
540 hctx->ccid3hctx_t_ipi);
541 ccid3_calc_new_delta(hctx);
543 /* remove all packets older than the one acked from history */
544 dccp_tx_hist_purge_older(ccid3_tx_hist,
545 &hctx->ccid3hctx_hist, packet);
547 * As we have calculated new ipi, delta, t_nom it is possible that
548 * we now can send a packet, so wake up dccp_wait_for_ccids.
550 sk->sk_write_space(sk);
553 * Schedule no feedback timer to expire in
554 * max(4 * R, 2 * s / X)
556 next_tmout = max(hctx->ccid3hctx_t_rto,
557 2 * usecs_div(hctx->ccid3hctx_s,
560 ccid3_pr_debug("%s, sk=%p, Scheduled no feedback timer to "
561 "expire in %lu jiffies (%luus)\n",
563 usecs_to_jiffies(next_tmout), next_tmout);
565 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
566 jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
569 hctx->ccid3hctx_idle = 1;
572 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
573 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
579 static void ccid3_hc_tx_insert_options(struct sock *sk, struct sk_buff *skb)
581 const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
583 BUG_ON(hctx == NULL);
585 if (!(sk->sk_state == DCCP_OPEN || sk->sk_state == DCCP_PARTOPEN))
588 DCCP_SKB_CB(skb)->dccpd_ccval = hctx->ccid3hctx_last_win_count;
591 static int ccid3_hc_tx_parse_options(struct sock *sk, unsigned char option,
592 unsigned char len, u16 idx,
593 unsigned char *value)
596 const struct dccp_sock *dp = dccp_sk(sk);
597 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
598 struct ccid3_options_received *opt_recv;
600 BUG_ON(hctx == NULL);
602 opt_recv = &hctx->ccid3hctx_options_received;
604 if (opt_recv->ccid3or_seqno != dp->dccps_gsr) {
605 opt_recv->ccid3or_seqno = dp->dccps_gsr;
606 opt_recv->ccid3or_loss_event_rate = ~0;
607 opt_recv->ccid3or_loss_intervals_idx = 0;
608 opt_recv->ccid3or_loss_intervals_len = 0;
609 opt_recv->ccid3or_receive_rate = 0;
613 case TFRC_OPT_LOSS_EVENT_RATE:
614 if (unlikely(len != 4)) {
615 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, invalid "
616 "len for TFRC_OPT_LOSS_EVENT_RATE\n",
617 __FUNCTION__, dccp_role(sk), sk);
620 opt_recv->ccid3or_loss_event_rate = ntohl(*(u32 *)value);
621 ccid3_pr_debug("%s, sk=%p, LOSS_EVENT_RATE=%u\n",
623 opt_recv->ccid3or_loss_event_rate);
626 case TFRC_OPT_LOSS_INTERVALS:
627 opt_recv->ccid3or_loss_intervals_idx = idx;
628 opt_recv->ccid3or_loss_intervals_len = len;
629 ccid3_pr_debug("%s, sk=%p, LOSS_INTERVALS=(%u, %u)\n",
631 opt_recv->ccid3or_loss_intervals_idx,
632 opt_recv->ccid3or_loss_intervals_len);
634 case TFRC_OPT_RECEIVE_RATE:
635 if (unlikely(len != 4)) {
636 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, invalid "
637 "len for TFRC_OPT_RECEIVE_RATE\n",
638 __FUNCTION__, dccp_role(sk), sk);
641 opt_recv->ccid3or_receive_rate = ntohl(*(u32 *)value);
642 ccid3_pr_debug("%s, sk=%p, RECEIVE_RATE=%u\n",
644 opt_recv->ccid3or_receive_rate);
652 static int ccid3_hc_tx_init(struct sock *sk)
654 struct dccp_sock *dp = dccp_sk(sk);
655 struct ccid3_hc_tx_sock *hctx;
657 dp->dccps_hc_tx_ccid_private = kmalloc(sizeof(*hctx), gfp_any());
658 if (dp->dccps_hc_tx_ccid_private == NULL)
661 hctx = ccid3_hc_tx_sk(sk);
662 memset(hctx, 0, sizeof(*hctx));
664 if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
665 dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
666 hctx->ccid3hctx_s = dp->dccps_packet_size;
668 hctx->ccid3hctx_s = TFRC_STD_PACKET_SIZE;
670 /* Set transmission rate to 1 packet per second */
671 hctx->ccid3hctx_x = hctx->ccid3hctx_s;
672 hctx->ccid3hctx_t_rto = USEC_PER_SEC;
673 hctx->ccid3hctx_state = TFRC_SSTATE_NO_SENT;
674 INIT_LIST_HEAD(&hctx->ccid3hctx_hist);
675 init_timer(&hctx->ccid3hctx_no_feedback_timer);
680 static void ccid3_hc_tx_exit(struct sock *sk)
682 struct dccp_sock *dp = dccp_sk(sk);
683 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
685 BUG_ON(hctx == NULL);
687 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_TERM);
688 sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
690 /* Empty packet history */
691 dccp_tx_hist_purge(ccid3_tx_hist, &hctx->ccid3hctx_hist);
693 kfree(dp->dccps_hc_tx_ccid_private);
694 dp->dccps_hc_tx_ccid_private = NULL;
698 * RX Half Connection methods
701 /* TFRC receiver states */
702 enum ccid3_hc_rx_states {
703 TFRC_RSTATE_NO_DATA = 1,
705 TFRC_RSTATE_TERM = 127,
709 static const char *ccid3_rx_state_name(enum ccid3_hc_rx_states state)
711 static char *ccid3_rx_state_names[] = {
712 [TFRC_RSTATE_NO_DATA] = "NO_DATA",
713 [TFRC_RSTATE_DATA] = "DATA",
714 [TFRC_RSTATE_TERM] = "TERM",
717 return ccid3_rx_state_names[state];
721 static inline void ccid3_hc_rx_set_state(struct sock *sk,
722 enum ccid3_hc_rx_states state)
724 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
725 enum ccid3_hc_rx_states oldstate = hcrx->ccid3hcrx_state;
727 ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
728 dccp_role(sk), sk, ccid3_rx_state_name(oldstate),
729 ccid3_rx_state_name(state));
730 WARN_ON(state == oldstate);
731 hcrx->ccid3hcrx_state = state;
734 static void ccid3_hc_rx_send_feedback(struct sock *sk)
736 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
737 struct dccp_sock *dp = dccp_sk(sk);
738 struct dccp_rx_hist_entry *packet;
741 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
743 dccp_timestamp(sk, &now);
745 switch (hcrx->ccid3hcrx_state) {
746 case TFRC_RSTATE_NO_DATA:
747 hcrx->ccid3hcrx_x_recv = 0;
749 case TFRC_RSTATE_DATA: {
750 const u32 delta = timeval_delta(&now,
751 &hcrx->ccid3hcrx_tstamp_last_feedback);
752 hcrx->ccid3hcrx_x_recv = usecs_div(hcrx->ccid3hcrx_bytes_recv,
757 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
758 __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
763 packet = dccp_rx_hist_find_data_packet(&hcrx->ccid3hcrx_hist);
764 if (unlikely(packet == NULL)) {
765 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, no data packet "
767 __FUNCTION__, dccp_role(sk), sk);
771 hcrx->ccid3hcrx_tstamp_last_feedback = now;
772 hcrx->ccid3hcrx_last_counter = packet->dccphrx_ccval;
773 hcrx->ccid3hcrx_seqno_last_counter = packet->dccphrx_seqno;
774 hcrx->ccid3hcrx_bytes_recv = 0;
776 /* Convert to multiples of 10us */
777 hcrx->ccid3hcrx_elapsed_time =
778 timeval_delta(&now, &packet->dccphrx_tstamp) / 10;
779 if (hcrx->ccid3hcrx_p == 0)
780 hcrx->ccid3hcrx_pinv = ~0;
782 hcrx->ccid3hcrx_pinv = 1000000 / hcrx->ccid3hcrx_p;
783 dp->dccps_hc_rx_insert_options = 1;
787 static void ccid3_hc_rx_insert_options(struct sock *sk, struct sk_buff *skb)
789 const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
792 BUG_ON(hcrx == NULL);
794 if (!(sk->sk_state == DCCP_OPEN || sk->sk_state == DCCP_PARTOPEN))
797 DCCP_SKB_CB(skb)->dccpd_ccval = hcrx->ccid3hcrx_last_counter;
799 if (dccp_packet_without_ack(skb))
802 if (hcrx->ccid3hcrx_elapsed_time != 0)
803 dccp_insert_option_elapsed_time(sk, skb,
804 hcrx->ccid3hcrx_elapsed_time);
805 dccp_insert_option_timestamp(sk, skb);
806 x_recv = htonl(hcrx->ccid3hcrx_x_recv);
807 pinv = htonl(hcrx->ccid3hcrx_pinv);
808 dccp_insert_option(sk, skb, TFRC_OPT_LOSS_EVENT_RATE,
809 &pinv, sizeof(pinv));
810 dccp_insert_option(sk, skb, TFRC_OPT_RECEIVE_RATE,
811 &x_recv, sizeof(x_recv));
814 /* calculate first loss interval
816 * returns estimated loss interval in usecs */
818 static u32 ccid3_hc_rx_calc_first_li(struct sock *sk)
820 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
821 struct dccp_rx_hist_entry *entry, *next, *tail = NULL;
822 u32 rtt, delta, x_recv, fval, p, tmp2;
823 struct timeval tstamp = { 0, };
829 list_for_each_entry_safe(entry, next, &hcrx->ccid3hcrx_hist,
831 if (dccp_rx_hist_entry_data_packet(entry)) {
836 tstamp = entry->dccphrx_tstamp;
837 win_count = entry->dccphrx_ccval;
841 interval = win_count - entry->dccphrx_ccval;
843 interval += TFRC_WIN_COUNT_LIMIT;
851 if (unlikely(step == 0)) {
852 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, packet history "
853 "contains no data packets!\n",
854 __FUNCTION__, dccp_role(sk), sk);
858 if (unlikely(interval == 0)) {
859 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, Could not find a "
860 "win_count interval > 0. Defaulting to 1\n",
861 __FUNCTION__, dccp_role(sk), sk);
865 rtt = timeval_delta(&tstamp, &tail->dccphrx_tstamp) * 4 / interval;
866 ccid3_pr_debug("%s, sk=%p, approximated RTT to %uus\n",
867 dccp_role(sk), sk, rtt);
871 dccp_timestamp(sk, &tstamp);
872 delta = timeval_delta(&tstamp, &hcrx->ccid3hcrx_tstamp_last_feedback);
873 x_recv = usecs_div(hcrx->ccid3hcrx_bytes_recv, delta);
875 tmp1 = (u64)x_recv * (u64)rtt;
876 do_div(tmp1,10000000);
878 fval = (hcrx->ccid3hcrx_s * 100000) / tmp2;
879 /* do not alter order above or you will get overflow on 32 bit */
880 p = tfrc_calc_x_reverse_lookup(fval);
881 ccid3_pr_debug("%s, sk=%p, receive rate=%u bytes/s, implied "
882 "loss rate=%u\n", dccp_role(sk), sk, x_recv, p);
890 static void ccid3_hc_rx_update_li(struct sock *sk, u64 seq_loss, u8 win_loss)
892 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
894 if (seq_loss != DCCP_MAX_SEQNO + 1 &&
895 list_empty(&hcrx->ccid3hcrx_li_hist)) {
896 struct dccp_li_hist_entry *li_tail;
898 li_tail = dccp_li_hist_interval_new(ccid3_li_hist,
899 &hcrx->ccid3hcrx_li_hist,
903 li_tail->dccplih_interval = ccid3_hc_rx_calc_first_li(sk);
905 LIMIT_NETDEBUG(KERN_WARNING "%s: FIXME: find end of "
906 "interval\n", __FUNCTION__);
909 static void ccid3_hc_rx_detect_loss(struct sock *sk)
911 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
913 const u64 seq_loss = dccp_rx_hist_detect_loss(&hcrx->ccid3hcrx_hist,
914 &hcrx->ccid3hcrx_li_hist,
917 ccid3_hc_rx_update_li(sk, seq_loss, win_loss);
920 static void ccid3_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
922 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
923 const struct dccp_options_received *opt_recv;
924 struct dccp_rx_hist_entry *packet;
927 u32 p_prev, r_sample, t_elapsed;
930 BUG_ON(hcrx == NULL ||
931 !(hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA ||
932 hcrx->ccid3hcrx_state == TFRC_RSTATE_DATA));
934 opt_recv = &dccp_sk(sk)->dccps_options_received;
936 switch (DCCP_SKB_CB(skb)->dccpd_type) {
938 if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
940 case DCCP_PKT_DATAACK:
941 if (opt_recv->dccpor_timestamp_echo == 0)
943 p_prev = hcrx->ccid3hcrx_rtt;
944 dccp_timestamp(sk, &now);
945 timeval_sub_usecs(&now, opt_recv->dccpor_timestamp_echo * 10);
946 r_sample = timeval_usecs(&now);
947 t_elapsed = opt_recv->dccpor_elapsed_time * 10;
949 if (unlikely(r_sample <= t_elapsed))
950 LIMIT_NETDEBUG(KERN_WARNING "%s: r_sample=%uus, "
952 __FUNCTION__, r_sample, t_elapsed);
954 r_sample -= t_elapsed;
956 if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
957 hcrx->ccid3hcrx_rtt = r_sample;
959 hcrx->ccid3hcrx_rtt = (hcrx->ccid3hcrx_rtt * 9) / 10 +
962 if (p_prev != hcrx->ccid3hcrx_rtt)
963 ccid3_pr_debug("%s, New RTT=%luus, elapsed time=%u\n",
964 dccp_role(sk), hcrx->ccid3hcrx_rtt,
965 opt_recv->dccpor_elapsed_time);
969 default: /* We're not interested in other packet types, move along */
973 packet = dccp_rx_hist_entry_new(ccid3_rx_hist, sk, opt_recv->dccpor_ndp,
975 if (unlikely(packet == NULL)) {
976 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, Not enough mem to "
977 "add rx packet to history, consider it lost!\n",
978 __FUNCTION__, dccp_role(sk), sk);
982 win_count = packet->dccphrx_ccval;
984 ins = dccp_rx_hist_add_packet(ccid3_rx_hist, &hcrx->ccid3hcrx_hist,
985 &hcrx->ccid3hcrx_li_hist, packet);
987 if (DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK)
990 switch (hcrx->ccid3hcrx_state) {
991 case TFRC_RSTATE_NO_DATA:
992 ccid3_pr_debug("%s, sk=%p(%s), skb=%p, sending initial "
995 dccp_state_name(sk->sk_state), skb);
996 ccid3_hc_rx_send_feedback(sk);
997 ccid3_hc_rx_set_state(sk, TFRC_RSTATE_DATA);
999 case TFRC_RSTATE_DATA:
1000 hcrx->ccid3hcrx_bytes_recv += skb->len -
1001 dccp_hdr(skb)->dccph_doff * 4;
1005 dccp_timestamp(sk, &now);
1006 if (timeval_delta(&now, &hcrx->ccid3hcrx_tstamp_last_ack) >=
1007 hcrx->ccid3hcrx_rtt) {
1008 hcrx->ccid3hcrx_tstamp_last_ack = now;
1009 ccid3_hc_rx_send_feedback(sk);
1013 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
1014 __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
1019 /* Dealing with packet loss */
1020 ccid3_pr_debug("%s, sk=%p(%s), data loss! Reacting...\n",
1021 dccp_role(sk), sk, dccp_state_name(sk->sk_state));
1023 ccid3_hc_rx_detect_loss(sk);
1024 p_prev = hcrx->ccid3hcrx_p;
1026 /* Calculate loss event rate */
1027 if (!list_empty(&hcrx->ccid3hcrx_li_hist)) {
1028 u32 i_mean = dccp_li_hist_calc_i_mean(&hcrx->ccid3hcrx_li_hist);
1030 /* Scaling up by 1000000 as fixed decimal */
1032 hcrx->ccid3hcrx_p = 1000000 / i_mean;
1035 if (hcrx->ccid3hcrx_p > p_prev) {
1036 ccid3_hc_rx_send_feedback(sk);
1041 static int ccid3_hc_rx_init(struct sock *sk)
1043 struct dccp_sock *dp = dccp_sk(sk);
1044 struct ccid3_hc_rx_sock *hcrx;
1046 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
1048 dp->dccps_hc_rx_ccid_private = kmalloc(sizeof(*hcrx), gfp_any());
1049 if (dp->dccps_hc_rx_ccid_private == NULL)
1052 hcrx = ccid3_hc_rx_sk(sk);
1053 memset(hcrx, 0, sizeof(*hcrx));
1055 if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
1056 dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
1057 hcrx->ccid3hcrx_s = dp->dccps_packet_size;
1059 hcrx->ccid3hcrx_s = TFRC_STD_PACKET_SIZE;
1061 hcrx->ccid3hcrx_state = TFRC_RSTATE_NO_DATA;
1062 INIT_LIST_HEAD(&hcrx->ccid3hcrx_hist);
1063 INIT_LIST_HEAD(&hcrx->ccid3hcrx_li_hist);
1064 dccp_timestamp(sk, &hcrx->ccid3hcrx_tstamp_last_ack);
1065 hcrx->ccid3hcrx_tstamp_last_feedback = hcrx->ccid3hcrx_tstamp_last_ack;
1066 hcrx->ccid3hcrx_rtt = 5000; /* XXX 5ms for now... */
1070 static void ccid3_hc_rx_exit(struct sock *sk)
1072 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
1073 struct dccp_sock *dp = dccp_sk(sk);
1075 BUG_ON(hcrx == NULL);
1077 ccid3_hc_rx_set_state(sk, TFRC_RSTATE_TERM);
1079 /* Empty packet history */
1080 dccp_rx_hist_purge(ccid3_rx_hist, &hcrx->ccid3hcrx_hist);
1082 /* Empty loss interval history */
1083 dccp_li_hist_purge(ccid3_li_hist, &hcrx->ccid3hcrx_li_hist);
1085 kfree(dp->dccps_hc_rx_ccid_private);
1086 dp->dccps_hc_rx_ccid_private = NULL;
1089 static void ccid3_hc_rx_get_info(struct sock *sk, struct tcp_info *info)
1091 const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
1093 /* Listen socks doesn't have a private CCID block */
1094 if (sk->sk_state == DCCP_LISTEN)
1097 BUG_ON(hcrx == NULL);
1099 info->tcpi_ca_state = hcrx->ccid3hcrx_state;
1100 info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1101 info->tcpi_rcv_rtt = hcrx->ccid3hcrx_rtt;
1104 static void ccid3_hc_tx_get_info(struct sock *sk, struct tcp_info *info)
1106 const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
1108 /* Listen socks doesn't have a private CCID block */
1109 if (sk->sk_state == DCCP_LISTEN)
1112 BUG_ON(hctx == NULL);
1114 info->tcpi_rto = hctx->ccid3hctx_t_rto;
1115 info->tcpi_rtt = hctx->ccid3hctx_rtt;
1118 static int ccid3_hc_rx_getsockopt(struct sock *sk, const int optname, int len,
1119 u32 __user *optval, int __user *optlen)
1121 const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
1124 /* Listen socks doesn't have a private CCID block */
1125 if (sk->sk_state == DCCP_LISTEN)
1129 case DCCP_SOCKOPT_CCID_RX_INFO:
1130 if (len < sizeof(hcrx->ccid3hcrx_tfrc))
1132 len = sizeof(hcrx->ccid3hcrx_tfrc);
1133 val = &hcrx->ccid3hcrx_tfrc;
1136 return -ENOPROTOOPT;
1139 if (put_user(len, optlen) || copy_to_user(optval, val, len))
1145 static int ccid3_hc_tx_getsockopt(struct sock *sk, const int optname, int len,
1146 u32 __user *optval, int __user *optlen)
1148 const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
1151 /* Listen socks doesn't have a private CCID block */
1152 if (sk->sk_state == DCCP_LISTEN)
1156 case DCCP_SOCKOPT_CCID_TX_INFO:
1157 if (len < sizeof(hctx->ccid3hctx_tfrc))
1159 len = sizeof(hctx->ccid3hctx_tfrc);
1160 val = &hctx->ccid3hctx_tfrc;
1163 return -ENOPROTOOPT;
1166 if (put_user(len, optlen) || copy_to_user(optval, val, len))
1172 static struct ccid ccid3 = {
1174 .ccid_name = "ccid3",
1175 .ccid_owner = THIS_MODULE,
1176 .ccid_hc_tx_init = ccid3_hc_tx_init,
1177 .ccid_hc_tx_exit = ccid3_hc_tx_exit,
1178 .ccid_hc_tx_send_packet = ccid3_hc_tx_send_packet,
1179 .ccid_hc_tx_packet_sent = ccid3_hc_tx_packet_sent,
1180 .ccid_hc_tx_packet_recv = ccid3_hc_tx_packet_recv,
1181 .ccid_hc_tx_insert_options = ccid3_hc_tx_insert_options,
1182 .ccid_hc_tx_parse_options = ccid3_hc_tx_parse_options,
1183 .ccid_hc_rx_init = ccid3_hc_rx_init,
1184 .ccid_hc_rx_exit = ccid3_hc_rx_exit,
1185 .ccid_hc_rx_insert_options = ccid3_hc_rx_insert_options,
1186 .ccid_hc_rx_packet_recv = ccid3_hc_rx_packet_recv,
1187 .ccid_hc_rx_get_info = ccid3_hc_rx_get_info,
1188 .ccid_hc_tx_get_info = ccid3_hc_tx_get_info,
1189 .ccid_hc_rx_getsockopt = ccid3_hc_rx_getsockopt,
1190 .ccid_hc_tx_getsockopt = ccid3_hc_tx_getsockopt,
1193 module_param(ccid3_debug, int, 0444);
1194 MODULE_PARM_DESC(ccid3_debug, "Enable debug messages");
1196 static __init int ccid3_module_init(void)
1200 ccid3_rx_hist = dccp_rx_hist_new("ccid3");
1201 if (ccid3_rx_hist == NULL)
1204 ccid3_tx_hist = dccp_tx_hist_new("ccid3");
1205 if (ccid3_tx_hist == NULL)
1208 ccid3_li_hist = dccp_li_hist_new("ccid3");
1209 if (ccid3_li_hist == NULL)
1212 rc = ccid_register(&ccid3);
1214 goto out_free_loss_interval_history;
1218 out_free_loss_interval_history:
1219 dccp_li_hist_delete(ccid3_li_hist);
1220 ccid3_li_hist = NULL;
1222 dccp_tx_hist_delete(ccid3_tx_hist);
1223 ccid3_tx_hist = NULL;
1225 dccp_rx_hist_delete(ccid3_rx_hist);
1226 ccid3_rx_hist = NULL;
1229 module_init(ccid3_module_init);
1231 static __exit void ccid3_module_exit(void)
1233 #ifdef CONFIG_IP_DCCP_UNLOAD_HACK
1235 * Hack to use while developing, so that we get rid of the control
1236 * sock, that is what keeps a refcount on dccp.ko -acme
1238 extern void dccp_ctl_sock_exit(void);
1240 dccp_ctl_sock_exit();
1242 ccid_unregister(&ccid3);
1244 if (ccid3_tx_hist != NULL) {
1245 dccp_tx_hist_delete(ccid3_tx_hist);
1246 ccid3_tx_hist = NULL;
1248 if (ccid3_rx_hist != NULL) {
1249 dccp_rx_hist_delete(ccid3_rx_hist);
1250 ccid3_rx_hist = NULL;
1252 if (ccid3_li_hist != NULL) {
1253 dccp_li_hist_delete(ccid3_li_hist);
1254 ccid3_li_hist = NULL;
1257 module_exit(ccid3_module_exit);
1259 MODULE_AUTHOR("Ian McDonald <iam4@cs.waikato.ac.nz>, "
1260 "Arnaldo Carvalho de Melo <acme@ghostprotocols.net>");
1261 MODULE_DESCRIPTION("DCCP TFRC CCID3 CCID");
1262 MODULE_LICENSE("GPL");
1263 MODULE_ALIAS("net-dccp-ccid-3");