Merge master.kernel.org:/pub/scm/linux/kernel/git/jejb/scsi-rc-fixes-2.6
[GitHub/LineageOS/android_kernel_samsung_universal7580.git] / net / dccp / ccids / ccid3.c
1 /*
2 * net/dccp/ccids/ccid3.c
3 *
4 * Copyright (c) 2005 The University of Waikato, Hamilton, New Zealand.
5 * Copyright (c) 2005-6 Ian McDonald <imcdnzl@gmail.com>
6 *
7 * An implementation of the DCCP protocol
8 *
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/
11 *
12 * This code also uses code from Lulea University, rereleased as GPL by its
13 * authors:
14 * Copyright (c) 2003 Nils-Erik Mattsson, Joacim Haggmark, Magnus Erixzon
15 *
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>.
19 *
20 * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
21 *
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.
26 *
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.
31 *
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.
35 */
36
37 #include <linux/config.h>
38 #include "../ccid.h"
39 #include "../dccp.h"
40 #include "lib/packet_history.h"
41 #include "lib/loss_interval.h"
42 #include "lib/tfrc.h"
43 #include "ccid3.h"
44
45 /*
46 * Reason for maths here is to avoid 32 bit overflow when a is big.
47 * With this we get close to the limit.
48 */
49 static inline u32 usecs_div(const u32 a, const u32 b)
50 {
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 :
59 100000;
60 const u32 tmp = a * (USEC_PER_SEC / div);
61 return (b >= 2 * div) ? tmp / (b / div) : tmp;
62 }
63
64 static int ccid3_debug;
65
66 #ifdef CCID3_DEBUG
67 #define ccid3_pr_debug(format, a...) \
68 do { if (ccid3_debug) \
69 printk(KERN_DEBUG "%s: " format, __FUNCTION__, ##a); \
70 } while (0)
71 #else
72 #define ccid3_pr_debug(format, a...)
73 #endif
74
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;
78
79 static int ccid3_init(struct sock *sk)
80 {
81 return 0;
82 }
83
84 static void ccid3_exit(struct sock *sk)
85 {
86 }
87
88 /* TFRC sender states */
89 enum ccid3_hc_tx_states {
90 TFRC_SSTATE_NO_SENT = 1,
91 TFRC_SSTATE_NO_FBACK,
92 TFRC_SSTATE_FBACK,
93 TFRC_SSTATE_TERM,
94 };
95
96 #ifdef CCID3_DEBUG
97 static const char *ccid3_tx_state_name(enum ccid3_hc_tx_states state)
98 {
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",
104 };
105
106 return ccid3_state_names[state];
107 }
108 #endif
109
110 static inline void ccid3_hc_tx_set_state(struct sock *sk,
111 enum ccid3_hc_tx_states state)
112 {
113 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
114 enum ccid3_hc_tx_states oldstate = hctx->ccid3hctx_state;
115
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;
121 }
122
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)
125 {
126 /*
127 * If no feedback spec says t_ipi is 1 second (set elsewhere and then
128 * doubles after every no feedback timer (separate function)
129 */
130 if (hctx->ccid3hctx_state != TFRC_SSTATE_NO_FBACK)
131 hctx->ccid3hctx_t_ipi = usecs_div(hctx->ccid3hctx_s,
132 hctx->ccid3hctx_x);
133 }
134
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)
137 {
138 hctx->ccid3hctx_delta = min_t(u32, hctx->ccid3hctx_t_ipi / 2,
139 TFRC_OPSYS_HALF_TIME_GRAN);
140 }
141
142 /*
143 * Update X by
144 * If (p > 0)
145 * x_calc = calcX(s, R, p);
146 * X = max(min(X_calc, 2 * X_recv), s / t_mbi);
147 * Else
148 * If (now - tld >= R)
149 * X = max(min(2 * X, 2 * X_recv), s / R);
150 * tld = now;
151 */
152 static void ccid3_hc_tx_update_x(struct sock *sk)
153 {
154 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
155
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,
159 hctx->ccid3hctx_rtt,
160 hctx->ccid3hctx_p);
161 hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_calc,
162 2 * hctx->ccid3hctx_x_recv),
163 (hctx->ccid3hctx_s /
164 TFRC_MAX_BACK_OFF_TIME));
165 } else {
166 struct timeval now;
167
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;
176 }
177 }
178 }
179
180 static void ccid3_hc_tx_no_feedback_timer(unsigned long data)
181 {
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);
185
186 bh_lock_sock(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,
191 jiffies + HZ / 5);
192 goto out;
193 }
194
195 ccid3_pr_debug("%s, sk=%p, state=%s\n", dccp_role(sk), sk,
196 ccid3_tx_state_name(hctx->ccid3hctx_state));
197
198 switch (hctx->ccid3hctx_state) {
199 case TFRC_SSTATE_TERM:
200 goto out;
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);
208
209 ccid3_pr_debug("%s, sk=%p, state=%s, updated tx rate to %d "
210 "bytes/s\n",
211 dccp_role(sk), sk,
212 ccid3_tx_state_name(hctx->ccid3hctx_state),
213 hctx->ccid3hctx_x);
214 next_tmout = max_t(u32, 2 * usecs_div(hctx->ccid3hctx_s,
215 hctx->ccid3hctx_x),
216 TFRC_INITIAL_TIMEOUT);
217 /*
218 * FIXME - not sure above calculation is correct. See section
219 * 5 of CCID3 11 should adjust tx_t_ipi and double that to
220 * achieve it really
221 */
222 break;
223 case TFRC_SSTATE_FBACK:
224 /*
225 * Check if IDLE since last timeout and recv rate is less than
226 * 4 packets per RTT
227 */
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",
232 dccp_role(sk), sk,
233 ccid3_tx_state_name(hctx->ccid3hctx_state));
234 /* Halve sending rate */
235
236 /* If (X_calc > 2 * X_recv)
237 * X_recv = max(X_recv / 2, s / (2 * t_mbi));
238 * Else
239 * X_recv = X_calc / 4;
240 */
241 BUG_ON(hctx->ccid3hctx_p >= TFRC_SMALLEST_P &&
242 hctx->ccid3hctx_x_calc == 0);
243
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));
249 else
250 hctx->ccid3hctx_x_recv = hctx->ccid3hctx_x_calc / 4;
251
252 /* Update sending rate */
253 ccid3_hc_tx_update_x(sk);
254 }
255 /*
256 * Schedule no feedback timer to expire in
257 * max(4 * R, 2 * s / X)
258 */
259 next_tmout = max_t(u32, hctx->ccid3hctx_t_rto,
260 2 * usecs_div(hctx->ccid3hctx_s,
261 hctx->ccid3hctx_x));
262 break;
263 default:
264 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
265 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
266 dump_stack();
267 goto out;
268 }
269
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;
273 out:
274 bh_unlock_sock(sk);
275 sock_put(sk);
276 }
277
278 static int ccid3_hc_tx_send_packet(struct sock *sk,
279 struct sk_buff *skb, int len)
280 {
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;
284 struct timeval now;
285 long delay;
286 int rc = -ENOTCONN;
287
288 BUG_ON(hctx == NULL || hctx->ccid3hctx_state == TFRC_SSTATE_TERM);
289
290 /* Check if pure ACK or Terminating*/
291 /*
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"?
294 */
295 if (unlikely(len == 0))
296 goto out;
297
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,
302 SLAB_ATOMIC);
303
304 rc = -ENOBUFS;
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);
309 goto out;
310 }
311
312 dccp_tx_hist_add_entry(&hctx->ccid3hctx_hist, new_packet);
313 }
314
315 dccp_timestamp(sk, &now);
316
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;
327
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);
333 rc = 0;
334 break;
335 case TFRC_SSTATE_NO_FBACK:
336 case TFRC_SSTATE_FBACK:
337 delay = (timeval_delta(&now, &hctx->ccid3hctx_t_nom) -
338 hctx->ccid3hctx_delta);
339 delay /= -1000;
340 /* divide by -1000 is to convert to ms and get sign right */
341 rc = delay > 0 ? delay : 0;
342 break;
343 default:
344 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
345 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
346 dump_stack();
347 rc = -EINVAL;
348 break;
349 }
350
351 /* Can we send? if so add options and add to packet history */
352 if (rc == 0) {
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;
357 }
358 out:
359 return rc;
360 }
361
362 static void ccid3_hc_tx_packet_sent(struct sock *sk, int more, int len)
363 {
364 const struct dccp_sock *dp = dccp_sk(sk);
365 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
366 struct timeval now;
367
368 BUG_ON(hctx == NULL || hctx->ccid3hctx_state == TFRC_SSTATE_TERM);
369
370 dccp_timestamp(sk, &now);
371
372 /* check if we have sent a data packet */
373 if (len > 0) {
374 unsigned long quarter_rtt;
375 struct dccp_tx_hist_entry *packet;
376
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__);
381 return;
382 }
383 if (unlikely(packet->dccphtx_sent)) {
384 LIMIT_NETDEBUG(KERN_WARNING "%s: no unsent packet in "
385 "history!\n", __FUNCTION__);
386 return;
387 }
388 packet->dccphtx_tstamp = now;
389 packet->dccphtx_seqno = dp->dccps_gss;
390 /*
391 * Check if win_count have changed
392 * Algorithm in "8.1. Window Counter Valuer" in
393 * draft-ietf-dccp-ccid3-11.txt
394 */
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;
398
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 "
404 "%u to %u!\n",
405 dccp_role(sk), sk,
406 packet->dccphtx_ccval,
407 hctx->ccid3hctx_last_win_count);
408 }
409
410 hctx->ccid3hctx_idle = 0;
411 packet->dccphtx_rtt = hctx->ccid3hctx_rtt;
412 packet->dccphtx_sent = 1;
413 } else
414 ccid3_pr_debug("%s, sk=%p, seqno=%llu NOT inserted!\n",
415 dccp_role(sk), sk, dp->dccps_gss);
416
417 switch (hctx->ccid3hctx_state) {
418 case TFRC_SSTATE_NO_SENT:
419 /* if first wasn't pure ack */
420 if (len != 0)
421 printk(KERN_CRIT "%s: %s, First packet sent is noted "
422 "as a data packet\n",
423 __FUNCTION__, dccp_role(sk));
424 return;
425 case TFRC_SSTATE_NO_FBACK:
426 case TFRC_SSTATE_FBACK:
427 if (len > 0) {
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);
433 }
434 break;
435 default:
436 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
437 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
438 dump_stack();
439 break;
440 }
441 }
442
443 static void ccid3_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
444 {
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;
449 struct timeval now;
450 unsigned long next_tmout;
451 u32 t_elapsed;
452 u32 pinv;
453 u32 x_recv;
454 u32 r_sample;
455
456 BUG_ON(hctx == NULL || hctx->ccid3hctx_state == TFRC_SSTATE_TERM);
457
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))
461 return;
462
463 opt_recv = &hctx->ccid3hctx_options_received;
464
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;
468
469 switch (hctx->ccid3hctx_state) {
470 case TFRC_SSTATE_NO_SENT:
471 /* FIXME: what to do here? */
472 return;
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));
486 return;
487 }
488
489 /* Update RTT */
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, "
494 "t_elapsed=%uus\n",
495 __FUNCTION__, r_sample, t_elapsed);
496 else
497 r_sample -= t_elapsed;
498
499 /* Update RTT estimate by
500 * If (No feedback recv)
501 * R = R_sample;
502 * Else
503 * R = q * R + (1 - q) * R_sample;
504 *
505 * q is a constant, RFC 3448 recomments 0.9
506 */
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;
510 } else
511 hctx->ccid3hctx_rtt = (hctx->ccid3hctx_rtt * 9) / 10 +
512 r_sample / 10;
513
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);
517
518 /* Update timeout interval */
519 hctx->ccid3hctx_t_rto = max_t(u32, 4 * hctx->ccid3hctx_rtt,
520 USEC_PER_SEC);
521
522 /* Update receive rate */
523 hctx->ccid3hctx_x_recv = x_recv;/* X_recv in bytes per sec */
524
525 /* Update loss event rate */
526 if (pinv == ~0 || pinv == 0)
527 hctx->ccid3hctx_p = 0;
528 else {
529 hctx->ccid3hctx_p = 1000000 / pinv;
530
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",
534 dccp_role(sk), sk);
535 }
536 }
537
538 /* unschedule no feedback timer */
539 sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
540
541 /* Update sending rate */
542 ccid3_hc_tx_update_x(sk);
543
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);
551
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);
555 /*
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.
558 */
559 sk->sk_write_space(sk);
560
561 /*
562 * Schedule no feedback timer to expire in
563 * max(4 * R, 2 * s / X)
564 */
565 next_tmout = max(hctx->ccid3hctx_t_rto,
566 2 * usecs_div(hctx->ccid3hctx_s,
567 hctx->ccid3hctx_x));
568
569 ccid3_pr_debug("%s, sk=%p, Scheduled no feedback timer to "
570 "expire in %lu jiffies (%luus)\n",
571 dccp_role(sk), sk,
572 usecs_to_jiffies(next_tmout), next_tmout);
573
574 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
575 jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
576
577 /* set idle flag */
578 hctx->ccid3hctx_idle = 1;
579 break;
580 default:
581 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
582 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
583 dump_stack();
584 break;
585 }
586 }
587
588 static void ccid3_hc_tx_insert_options(struct sock *sk, struct sk_buff *skb)
589 {
590 const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
591
592 BUG_ON(hctx == NULL);
593
594 if (!(sk->sk_state == DCCP_OPEN || sk->sk_state == DCCP_PARTOPEN))
595 return;
596
597 DCCP_SKB_CB(skb)->dccpd_ccval = hctx->ccid3hctx_last_win_count;
598 }
599
600 static int ccid3_hc_tx_parse_options(struct sock *sk, unsigned char option,
601 unsigned char len, u16 idx,
602 unsigned char *value)
603 {
604 int rc = 0;
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;
608
609 BUG_ON(hctx == NULL);
610
611 opt_recv = &hctx->ccid3hctx_options_received;
612
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;
619 }
620
621 switch (option) {
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);
627 rc = -EINVAL;
628 } else {
629 opt_recv->ccid3or_loss_event_rate = ntohl(*(u32 *)value);
630 ccid3_pr_debug("%s, sk=%p, LOSS_EVENT_RATE=%u\n",
631 dccp_role(sk), sk,
632 opt_recv->ccid3or_loss_event_rate);
633 }
634 break;
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",
639 dccp_role(sk), sk,
640 opt_recv->ccid3or_loss_intervals_idx,
641 opt_recv->ccid3or_loss_intervals_len);
642 break;
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);
648 rc = -EINVAL;
649 } else {
650 opt_recv->ccid3or_receive_rate = ntohl(*(u32 *)value);
651 ccid3_pr_debug("%s, sk=%p, RECEIVE_RATE=%u\n",
652 dccp_role(sk), sk,
653 opt_recv->ccid3or_receive_rate);
654 }
655 break;
656 }
657
658 return rc;
659 }
660
661 static int ccid3_hc_tx_init(struct sock *sk)
662 {
663 struct dccp_sock *dp = dccp_sk(sk);
664 struct ccid3_hc_tx_sock *hctx;
665
666 dp->dccps_hc_tx_ccid_private = kmalloc(sizeof(*hctx), gfp_any());
667 if (dp->dccps_hc_tx_ccid_private == NULL)
668 return -ENOMEM;
669
670 hctx = ccid3_hc_tx_sk(sk);
671 memset(hctx, 0, sizeof(*hctx));
672
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;
676 else
677 hctx->ccid3hctx_s = TFRC_STD_PACKET_SIZE;
678
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);
685
686 return 0;
687 }
688
689 static void ccid3_hc_tx_exit(struct sock *sk)
690 {
691 struct dccp_sock *dp = dccp_sk(sk);
692 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
693
694 BUG_ON(hctx == NULL);
695
696 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_TERM);
697 sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
698
699 /* Empty packet history */
700 dccp_tx_hist_purge(ccid3_tx_hist, &hctx->ccid3hctx_hist);
701
702 kfree(dp->dccps_hc_tx_ccid_private);
703 dp->dccps_hc_tx_ccid_private = NULL;
704 }
705
706 /*
707 * RX Half Connection methods
708 */
709
710 /* TFRC receiver states */
711 enum ccid3_hc_rx_states {
712 TFRC_RSTATE_NO_DATA = 1,
713 TFRC_RSTATE_DATA,
714 TFRC_RSTATE_TERM = 127,
715 };
716
717 #ifdef CCID3_DEBUG
718 static const char *ccid3_rx_state_name(enum ccid3_hc_rx_states state)
719 {
720 static char *ccid3_rx_state_names[] = {
721 [TFRC_RSTATE_NO_DATA] = "NO_DATA",
722 [TFRC_RSTATE_DATA] = "DATA",
723 [TFRC_RSTATE_TERM] = "TERM",
724 };
725
726 return ccid3_rx_state_names[state];
727 }
728 #endif
729
730 static inline void ccid3_hc_rx_set_state(struct sock *sk,
731 enum ccid3_hc_rx_states state)
732 {
733 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
734 enum ccid3_hc_rx_states oldstate = hcrx->ccid3hcrx_state;
735
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;
741 }
742
743 static void ccid3_hc_rx_send_feedback(struct sock *sk)
744 {
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;
748 struct timeval now;
749
750 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
751
752 dccp_timestamp(sk, &now);
753
754 switch (hcrx->ccid3hcrx_state) {
755 case TFRC_RSTATE_NO_DATA:
756 hcrx->ccid3hcrx_x_recv = 0;
757 break;
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,
762 delta);
763 }
764 break;
765 default:
766 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
767 __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
768 dump_stack();
769 return;
770 }
771
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 "
775 "in history!\n",
776 __FUNCTION__, dccp_role(sk), sk);
777 return;
778 }
779
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;
784
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;
790 else
791 hcrx->ccid3hcrx_pinv = 1000000 / hcrx->ccid3hcrx_p;
792 dp->dccps_hc_rx_insert_options = 1;
793 dccp_send_ack(sk);
794 }
795
796 static void ccid3_hc_rx_insert_options(struct sock *sk, struct sk_buff *skb)
797 {
798 const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
799 u32 x_recv, pinv;
800
801 BUG_ON(hcrx == NULL);
802
803 if (!(sk->sk_state == DCCP_OPEN || sk->sk_state == DCCP_PARTOPEN))
804 return;
805
806 DCCP_SKB_CB(skb)->dccpd_ccval = hcrx->ccid3hcrx_last_counter;
807
808 if (dccp_packet_without_ack(skb))
809 return;
810
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));
821 }
822
823 /* calculate first loss interval
824 *
825 * returns estimated loss interval in usecs */
826
827 static u32 ccid3_hc_rx_calc_first_li(struct sock *sk)
828 {
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, };
833 int interval = 0;
834 int win_count = 0;
835 int step = 0;
836 u64 tmp1;
837
838 list_for_each_entry_safe(entry, next, &hcrx->ccid3hcrx_hist,
839 dccphrx_node) {
840 if (dccp_rx_hist_entry_data_packet(entry)) {
841 tail = entry;
842
843 switch (step) {
844 case 0:
845 tstamp = entry->dccphrx_tstamp;
846 win_count = entry->dccphrx_ccval;
847 step = 1;
848 break;
849 case 1:
850 interval = win_count - entry->dccphrx_ccval;
851 if (interval < 0)
852 interval += TFRC_WIN_COUNT_LIMIT;
853 if (interval > 4)
854 goto found;
855 break;
856 }
857 }
858 }
859
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);
864 return ~0;
865 }
866
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);
871 interval = 1;
872 }
873 found:
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);
877 if (rtt == 0)
878 rtt = 1;
879
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);
883
884 tmp1 = (u64)x_recv * (u64)rtt;
885 do_div(tmp1,10000000);
886 tmp2 = (u32)tmp1;
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);
892
893 if (p == 0)
894 return ~0;
895 else
896 return 1000000 / p;
897 }
898
899 static void ccid3_hc_rx_update_li(struct sock *sk, u64 seq_loss, u8 win_loss)
900 {
901 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
902
903 if (seq_loss != DCCP_MAX_SEQNO + 1 &&
904 list_empty(&hcrx->ccid3hcrx_li_hist)) {
905 struct dccp_li_hist_entry *li_tail;
906
907 li_tail = dccp_li_hist_interval_new(ccid3_li_hist,
908 &hcrx->ccid3hcrx_li_hist,
909 seq_loss, win_loss);
910 if (li_tail == NULL)
911 return;
912 li_tail->dccplih_interval = ccid3_hc_rx_calc_first_li(sk);
913 } else
914 LIMIT_NETDEBUG(KERN_WARNING "%s: FIXME: find end of "
915 "interval\n", __FUNCTION__);
916 }
917
918 static void ccid3_hc_rx_detect_loss(struct sock *sk)
919 {
920 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
921 u8 win_loss;
922 const u64 seq_loss = dccp_rx_hist_detect_loss(&hcrx->ccid3hcrx_hist,
923 &hcrx->ccid3hcrx_li_hist,
924 &win_loss);
925
926 ccid3_hc_rx_update_li(sk, seq_loss, win_loss);
927 }
928
929 static void ccid3_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
930 {
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;
934 struct timeval now;
935 u8 win_count;
936 u32 p_prev, r_sample, t_elapsed;
937 int ins;
938
939 BUG_ON(hcrx == NULL ||
940 !(hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA ||
941 hcrx->ccid3hcrx_state == TFRC_RSTATE_DATA));
942
943 opt_recv = &dccp_sk(sk)->dccps_options_received;
944
945 switch (DCCP_SKB_CB(skb)->dccpd_type) {
946 case DCCP_PKT_ACK:
947 if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
948 return;
949 case DCCP_PKT_DATAACK:
950 if (opt_recv->dccpor_timestamp_echo == 0)
951 break;
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;
957
958 if (unlikely(r_sample <= t_elapsed))
959 LIMIT_NETDEBUG(KERN_WARNING "%s: r_sample=%uus, "
960 "t_elapsed=%uus\n",
961 __FUNCTION__, r_sample, t_elapsed);
962 else
963 r_sample -= t_elapsed;
964
965 if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
966 hcrx->ccid3hcrx_rtt = r_sample;
967 else
968 hcrx->ccid3hcrx_rtt = (hcrx->ccid3hcrx_rtt * 9) / 10 +
969 r_sample / 10;
970
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);
975 break;
976 case DCCP_PKT_DATA:
977 break;
978 default: /* We're not interested in other packet types, move along */
979 return;
980 }
981
982 packet = dccp_rx_hist_entry_new(ccid3_rx_hist, sk, opt_recv->dccpor_ndp,
983 skb, SLAB_ATOMIC);
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);
988 return;
989 }
990
991 win_count = packet->dccphrx_ccval;
992
993 ins = dccp_rx_hist_add_packet(ccid3_rx_hist, &hcrx->ccid3hcrx_hist,
994 &hcrx->ccid3hcrx_li_hist, packet);
995
996 if (DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK)
997 return;
998
999 switch (hcrx->ccid3hcrx_state) {
1000 case TFRC_RSTATE_NO_DATA:
1001 ccid3_pr_debug("%s, sk=%p(%s), skb=%p, sending initial "
1002 "feedback\n",
1003 dccp_role(sk), sk,
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);
1007 return;
1008 case TFRC_RSTATE_DATA:
1009 hcrx->ccid3hcrx_bytes_recv += skb->len -
1010 dccp_hdr(skb)->dccph_doff * 4;
1011 if (ins != 0)
1012 break;
1013
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);
1019 }
1020 return;
1021 default:
1022 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
1023 __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
1024 dump_stack();
1025 return;
1026 }
1027
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));
1031
1032 ccid3_hc_rx_detect_loss(sk);
1033 p_prev = hcrx->ccid3hcrx_p;
1034
1035 /* Calculate loss event rate */
1036 if (!list_empty(&hcrx->ccid3hcrx_li_hist)) {
1037 u32 i_mean = dccp_li_hist_calc_i_mean(&hcrx->ccid3hcrx_li_hist);
1038
1039 /* Scaling up by 1000000 as fixed decimal */
1040 if (i_mean != 0)
1041 hcrx->ccid3hcrx_p = 1000000 / i_mean;
1042 }
1043
1044 if (hcrx->ccid3hcrx_p > p_prev) {
1045 ccid3_hc_rx_send_feedback(sk);
1046 return;
1047 }
1048 }
1049
1050 static int ccid3_hc_rx_init(struct sock *sk)
1051 {
1052 struct dccp_sock *dp = dccp_sk(sk);
1053 struct ccid3_hc_rx_sock *hcrx;
1054
1055 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
1056
1057 dp->dccps_hc_rx_ccid_private = kmalloc(sizeof(*hcrx), gfp_any());
1058 if (dp->dccps_hc_rx_ccid_private == NULL)
1059 return -ENOMEM;
1060
1061 hcrx = ccid3_hc_rx_sk(sk);
1062 memset(hcrx, 0, sizeof(*hcrx));
1063
1064 if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
1065 dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
1066 hcrx->ccid3hcrx_s = dp->dccps_packet_size;
1067 else
1068 hcrx->ccid3hcrx_s = TFRC_STD_PACKET_SIZE;
1069
1070 hcrx->ccid3hcrx_state = TFRC_RSTATE_NO_DATA;
1071 INIT_LIST_HEAD(&hcrx->ccid3hcrx_hist);
1072 INIT_LIST_HEAD(&hcrx->ccid3hcrx_li_hist);
1073 dccp_timestamp(sk, &hcrx->ccid3hcrx_tstamp_last_ack);
1074 hcrx->ccid3hcrx_tstamp_last_feedback = hcrx->ccid3hcrx_tstamp_last_ack;
1075 hcrx->ccid3hcrx_rtt = 5000; /* XXX 5ms for now... */
1076 return 0;
1077 }
1078
1079 static void ccid3_hc_rx_exit(struct sock *sk)
1080 {
1081 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
1082 struct dccp_sock *dp = dccp_sk(sk);
1083
1084 BUG_ON(hcrx == NULL);
1085
1086 ccid3_hc_rx_set_state(sk, TFRC_RSTATE_TERM);
1087
1088 /* Empty packet history */
1089 dccp_rx_hist_purge(ccid3_rx_hist, &hcrx->ccid3hcrx_hist);
1090
1091 /* Empty loss interval history */
1092 dccp_li_hist_purge(ccid3_li_hist, &hcrx->ccid3hcrx_li_hist);
1093
1094 kfree(dp->dccps_hc_rx_ccid_private);
1095 dp->dccps_hc_rx_ccid_private = NULL;
1096 }
1097
1098 static void ccid3_hc_rx_get_info(struct sock *sk, struct tcp_info *info)
1099 {
1100 const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
1101
1102 /* Listen socks doesn't have a private CCID block */
1103 if (sk->sk_state == DCCP_LISTEN)
1104 return;
1105
1106 BUG_ON(hcrx == NULL);
1107
1108 info->tcpi_ca_state = hcrx->ccid3hcrx_state;
1109 info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1110 info->tcpi_rcv_rtt = hcrx->ccid3hcrx_rtt;
1111 }
1112
1113 static void ccid3_hc_tx_get_info(struct sock *sk, struct tcp_info *info)
1114 {
1115 const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
1116
1117 /* Listen socks doesn't have a private CCID block */
1118 if (sk->sk_state == DCCP_LISTEN)
1119 return;
1120
1121 BUG_ON(hctx == NULL);
1122
1123 info->tcpi_rto = hctx->ccid3hctx_t_rto;
1124 info->tcpi_rtt = hctx->ccid3hctx_rtt;
1125 }
1126
1127 static int ccid3_hc_rx_getsockopt(struct sock *sk, const int optname, int len,
1128 u32 __user *optval, int __user *optlen)
1129 {
1130 const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
1131 const void *val;
1132
1133 /* Listen socks doesn't have a private CCID block */
1134 if (sk->sk_state == DCCP_LISTEN)
1135 return -EINVAL;
1136
1137 switch (optname) {
1138 case DCCP_SOCKOPT_CCID_RX_INFO:
1139 if (len < sizeof(hcrx->ccid3hcrx_tfrc))
1140 return -EINVAL;
1141 len = sizeof(hcrx->ccid3hcrx_tfrc);
1142 val = &hcrx->ccid3hcrx_tfrc;
1143 break;
1144 default:
1145 return -ENOPROTOOPT;
1146 }
1147
1148 if (put_user(len, optlen) || copy_to_user(optval, val, len))
1149 return -EFAULT;
1150
1151 return 0;
1152 }
1153
1154 static int ccid3_hc_tx_getsockopt(struct sock *sk, const int optname, int len,
1155 u32 __user *optval, int __user *optlen)
1156 {
1157 const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
1158 const void *val;
1159
1160 /* Listen socks doesn't have a private CCID block */
1161 if (sk->sk_state == DCCP_LISTEN)
1162 return -EINVAL;
1163
1164 switch (optname) {
1165 case DCCP_SOCKOPT_CCID_TX_INFO:
1166 if (len < sizeof(hctx->ccid3hctx_tfrc))
1167 return -EINVAL;
1168 len = sizeof(hctx->ccid3hctx_tfrc);
1169 val = &hctx->ccid3hctx_tfrc;
1170 break;
1171 default:
1172 return -ENOPROTOOPT;
1173 }
1174
1175 if (put_user(len, optlen) || copy_to_user(optval, val, len))
1176 return -EFAULT;
1177
1178 return 0;
1179 }
1180
1181 static struct ccid ccid3 = {
1182 .ccid_id = 3,
1183 .ccid_name = "ccid3",
1184 .ccid_owner = THIS_MODULE,
1185 .ccid_init = ccid3_init,
1186 .ccid_exit = ccid3_exit,
1187 .ccid_hc_tx_init = ccid3_hc_tx_init,
1188 .ccid_hc_tx_exit = ccid3_hc_tx_exit,
1189 .ccid_hc_tx_send_packet = ccid3_hc_tx_send_packet,
1190 .ccid_hc_tx_packet_sent = ccid3_hc_tx_packet_sent,
1191 .ccid_hc_tx_packet_recv = ccid3_hc_tx_packet_recv,
1192 .ccid_hc_tx_insert_options = ccid3_hc_tx_insert_options,
1193 .ccid_hc_tx_parse_options = ccid3_hc_tx_parse_options,
1194 .ccid_hc_rx_init = ccid3_hc_rx_init,
1195 .ccid_hc_rx_exit = ccid3_hc_rx_exit,
1196 .ccid_hc_rx_insert_options = ccid3_hc_rx_insert_options,
1197 .ccid_hc_rx_packet_recv = ccid3_hc_rx_packet_recv,
1198 .ccid_hc_rx_get_info = ccid3_hc_rx_get_info,
1199 .ccid_hc_tx_get_info = ccid3_hc_tx_get_info,
1200 .ccid_hc_rx_getsockopt = ccid3_hc_rx_getsockopt,
1201 .ccid_hc_tx_getsockopt = ccid3_hc_tx_getsockopt,
1202 };
1203
1204 module_param(ccid3_debug, int, 0444);
1205 MODULE_PARM_DESC(ccid3_debug, "Enable debug messages");
1206
1207 static __init int ccid3_module_init(void)
1208 {
1209 int rc = -ENOBUFS;
1210
1211 ccid3_rx_hist = dccp_rx_hist_new("ccid3");
1212 if (ccid3_rx_hist == NULL)
1213 goto out;
1214
1215 ccid3_tx_hist = dccp_tx_hist_new("ccid3");
1216 if (ccid3_tx_hist == NULL)
1217 goto out_free_rx;
1218
1219 ccid3_li_hist = dccp_li_hist_new("ccid3");
1220 if (ccid3_li_hist == NULL)
1221 goto out_free_tx;
1222
1223 rc = ccid_register(&ccid3);
1224 if (rc != 0)
1225 goto out_free_loss_interval_history;
1226 out:
1227 return rc;
1228
1229 out_free_loss_interval_history:
1230 dccp_li_hist_delete(ccid3_li_hist);
1231 ccid3_li_hist = NULL;
1232 out_free_tx:
1233 dccp_tx_hist_delete(ccid3_tx_hist);
1234 ccid3_tx_hist = NULL;
1235 out_free_rx:
1236 dccp_rx_hist_delete(ccid3_rx_hist);
1237 ccid3_rx_hist = NULL;
1238 goto out;
1239 }
1240 module_init(ccid3_module_init);
1241
1242 static __exit void ccid3_module_exit(void)
1243 {
1244 #ifdef CONFIG_IP_DCCP_UNLOAD_HACK
1245 /*
1246 * Hack to use while developing, so that we get rid of the control
1247 * sock, that is what keeps a refcount on dccp.ko -acme
1248 */
1249 extern void dccp_ctl_sock_exit(void);
1250
1251 dccp_ctl_sock_exit();
1252 #endif
1253 ccid_unregister(&ccid3);
1254
1255 if (ccid3_tx_hist != NULL) {
1256 dccp_tx_hist_delete(ccid3_tx_hist);
1257 ccid3_tx_hist = NULL;
1258 }
1259 if (ccid3_rx_hist != NULL) {
1260 dccp_rx_hist_delete(ccid3_rx_hist);
1261 ccid3_rx_hist = NULL;
1262 }
1263 if (ccid3_li_hist != NULL) {
1264 dccp_li_hist_delete(ccid3_li_hist);
1265 ccid3_li_hist = NULL;
1266 }
1267 }
1268 module_exit(ccid3_module_exit);
1269
1270 MODULE_AUTHOR("Ian McDonald <iam4@cs.waikato.ac.nz>, "
1271 "Arnaldo Carvalho de Melo <acme@ghostprotocols.net>");
1272 MODULE_DESCRIPTION("DCCP TFRC CCID3 CCID");
1273 MODULE_LICENSE("GPL");
1274 MODULE_ALIAS("net-dccp-ccid-3");