ccid3.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. /*
  2. * net/dccp/ccids/ccid3.c
  3. *
  4. * Copyright (c) 2005-7 The University of Waikato, Hamilton, New Zealand.
  5. * Copyright (c) 2005-7 Ian McDonald <ian.mcdonald@jandi.co.nz>
  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. #include "../ccid.h"
  37. #include "../dccp.h"
  38. #include "lib/packet_history.h"
  39. #include "lib/loss_interval.h"
  40. #include "lib/tfrc.h"
  41. #include "ccid3.h"
  42. #ifdef CONFIG_IP_DCCP_CCID3_DEBUG
  43. static int ccid3_debug;
  44. #define ccid3_pr_debug(format, a...) DCCP_PR_DEBUG(ccid3_debug, format, ##a)
  45. #else
  46. #define ccid3_pr_debug(format, a...)
  47. #endif
  48. static struct dccp_tx_hist *ccid3_tx_hist;
  49. static struct dccp_rx_hist *ccid3_rx_hist;
  50. /*
  51. * Transmitter Half-Connection Routines
  52. */
  53. #ifdef CONFIG_IP_DCCP_CCID3_DEBUG
  54. static const char *ccid3_tx_state_name(enum ccid3_hc_tx_states state)
  55. {
  56. static char *ccid3_state_names[] = {
  57. [TFRC_SSTATE_NO_SENT] = "NO_SENT",
  58. [TFRC_SSTATE_NO_FBACK] = "NO_FBACK",
  59. [TFRC_SSTATE_FBACK] = "FBACK",
  60. [TFRC_SSTATE_TERM] = "TERM",
  61. };
  62. return ccid3_state_names[state];
  63. }
  64. #endif
  65. static void ccid3_hc_tx_set_state(struct sock *sk,
  66. enum ccid3_hc_tx_states state)
  67. {
  68. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  69. enum ccid3_hc_tx_states oldstate = hctx->ccid3hctx_state;
  70. ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
  71. dccp_role(sk), sk, ccid3_tx_state_name(oldstate),
  72. ccid3_tx_state_name(state));
  73. WARN_ON(state == oldstate);
  74. hctx->ccid3hctx_state = state;
  75. }
  76. /*
  77. * Compute the initial sending rate X_init according to RFC 3390:
  78. * w_init = min(4 * MSS, max(2 * MSS, 4380 bytes))
  79. * X_init = w_init / RTT
  80. * For consistency with other parts of the code, X_init is scaled by 2^6.
  81. */
  82. static inline u64 rfc3390_initial_rate(struct sock *sk)
  83. {
  84. const struct dccp_sock *dp = dccp_sk(sk);
  85. const __u32 w_init = min(4 * dp->dccps_mss_cache,
  86. max(2 * dp->dccps_mss_cache, 4380U));
  87. return scaled_div(w_init << 6, ccid3_hc_tx_sk(sk)->ccid3hctx_rtt);
  88. }
  89. /*
  90. * Recalculate t_ipi and delta (should be called whenever X changes)
  91. */
  92. static inline void ccid3_update_send_interval(struct ccid3_hc_tx_sock *hctx)
  93. {
  94. /* Calculate new t_ipi = s / X_inst (X_inst is in 64 * bytes/second) */
  95. hctx->ccid3hctx_t_ipi = scaled_div32(((u64)hctx->ccid3hctx_s) << 6,
  96. hctx->ccid3hctx_x);
  97. /* Calculate new delta by delta = min(t_ipi / 2, t_gran / 2) */
  98. hctx->ccid3hctx_delta = min_t(u32, hctx->ccid3hctx_t_ipi / 2,
  99. TFRC_OPSYS_HALF_TIME_GRAN);
  100. ccid3_pr_debug("t_ipi=%u, delta=%u, s=%u, X=%u\n",
  101. hctx->ccid3hctx_t_ipi, hctx->ccid3hctx_delta,
  102. hctx->ccid3hctx_s, (unsigned)(hctx->ccid3hctx_x >> 6));
  103. }
  104. /*
  105. * Update X by
  106. * If (p > 0)
  107. * X_calc = calcX(s, R, p);
  108. * X = max(min(X_calc, 2 * X_recv), s / t_mbi);
  109. * Else
  110. * If (now - tld >= R)
  111. * X = max(min(2 * X, 2 * X_recv), s / R);
  112. * tld = now;
  113. *
  114. * Note: X and X_recv are both stored in units of 64 * bytes/second, to support
  115. * fine-grained resolution of sending rates. This requires scaling by 2^6
  116. * throughout the code. Only X_calc is unscaled (in bytes/second).
  117. *
  118. */
  119. static void ccid3_hc_tx_update_x(struct sock *sk)
  120. {
  121. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  122. __u64 min_rate = 2 * hctx->ccid3hctx_x_recv;
  123. const __u64 old_x = hctx->ccid3hctx_x;
  124. /*
  125. * Handle IDLE periods: do not reduce below RFC3390 initial sending rate
  126. * when idling [RFC 4342, 5.1]. See also draft-ietf-dccp-rfc3448bis.
  127. * For consistency with X and X_recv, min_rate is also scaled by 2^6.
  128. */
  129. if (unlikely(hctx->ccid3hctx_idle)) {
  130. min_rate = rfc3390_initial_rate(sk);
  131. min_rate = max(min_rate, 2 * hctx->ccid3hctx_x_recv);
  132. }
  133. if (hctx->ccid3hctx_p > 0) {
  134. hctx->ccid3hctx_x = min(((__u64)hctx->ccid3hctx_x_calc) << 6,
  135. min_rate);
  136. hctx->ccid3hctx_x = max(hctx->ccid3hctx_x,
  137. (((__u64)hctx->ccid3hctx_s) << 6) /
  138. TFRC_T_MBI);
  139. } else {
  140. const ktime_t now = ktime_get_real();
  141. if ((ktime_us_delta(now, hctx->ccid3hctx_t_ld) -
  142. (s64)hctx->ccid3hctx_rtt) >= 0) {
  143. hctx->ccid3hctx_x =
  144. max(min(2 * hctx->ccid3hctx_x, min_rate),
  145. scaled_div(((__u64)hctx->ccid3hctx_s) << 6,
  146. hctx->ccid3hctx_rtt));
  147. hctx->ccid3hctx_t_ld = now;
  148. }
  149. }
  150. if (hctx->ccid3hctx_x != old_x) {
  151. ccid3_pr_debug("X_prev=%u, X_now=%u, X_calc=%u, "
  152. "X_recv=%u\n", (unsigned)(old_x >> 6),
  153. (unsigned)(hctx->ccid3hctx_x >> 6),
  154. hctx->ccid3hctx_x_calc,
  155. (unsigned)(hctx->ccid3hctx_x_recv >> 6));
  156. ccid3_update_send_interval(hctx);
  157. }
  158. }
  159. /*
  160. * Track the mean packet size `s' (cf. RFC 4342, 5.3 and RFC 3448, 4.1)
  161. * @len: DCCP packet payload size in bytes
  162. */
  163. static inline void ccid3_hc_tx_update_s(struct ccid3_hc_tx_sock *hctx, int len)
  164. {
  165. const u16 old_s = hctx->ccid3hctx_s;
  166. hctx->ccid3hctx_s = old_s == 0 ? len : (9 * old_s + len) / 10;
  167. if (hctx->ccid3hctx_s != old_s)
  168. ccid3_update_send_interval(hctx);
  169. }
  170. /*
  171. * Update Window Counter using the algorithm from [RFC 4342, 8.1].
  172. * The algorithm is not applicable if RTT < 4 microseconds.
  173. */
  174. static inline void ccid3_hc_tx_update_win_count(struct ccid3_hc_tx_sock *hctx,
  175. ktime_t now)
  176. {
  177. u32 quarter_rtts;
  178. if (unlikely(hctx->ccid3hctx_rtt < 4)) /* avoid divide-by-zero */
  179. return;
  180. quarter_rtts = ktime_us_delta(now, hctx->ccid3hctx_t_last_win_count);
  181. quarter_rtts /= hctx->ccid3hctx_rtt / 4;
  182. if (quarter_rtts > 0) {
  183. hctx->ccid3hctx_t_last_win_count = now;
  184. hctx->ccid3hctx_last_win_count += min_t(u32, quarter_rtts, 5);
  185. hctx->ccid3hctx_last_win_count &= 0xF; /* mod 16 */
  186. }
  187. }
  188. static void ccid3_hc_tx_no_feedback_timer(unsigned long data)
  189. {
  190. struct sock *sk = (struct sock *)data;
  191. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  192. unsigned long t_nfb = USEC_PER_SEC / 5;
  193. bh_lock_sock(sk);
  194. if (sock_owned_by_user(sk)) {
  195. /* Try again later. */
  196. /* XXX: set some sensible MIB */
  197. goto restart_timer;
  198. }
  199. ccid3_pr_debug("%s(%p, state=%s) - entry \n", dccp_role(sk), sk,
  200. ccid3_tx_state_name(hctx->ccid3hctx_state));
  201. hctx->ccid3hctx_idle = 1;
  202. switch (hctx->ccid3hctx_state) {
  203. case TFRC_SSTATE_NO_FBACK:
  204. /* RFC 3448, 4.4: Halve send rate directly */
  205. hctx->ccid3hctx_x = max(hctx->ccid3hctx_x / 2,
  206. (((__u64)hctx->ccid3hctx_s) << 6) /
  207. TFRC_T_MBI);
  208. ccid3_pr_debug("%s(%p, state=%s), updated tx rate to %u "
  209. "bytes/s\n", dccp_role(sk), sk,
  210. ccid3_tx_state_name(hctx->ccid3hctx_state),
  211. (unsigned)(hctx->ccid3hctx_x >> 6));
  212. /* The value of R is still undefined and so we can not recompute
  213. * the timout value. Keep initial value as per [RFC 4342, 5]. */
  214. t_nfb = TFRC_INITIAL_TIMEOUT;
  215. ccid3_update_send_interval(hctx);
  216. break;
  217. case TFRC_SSTATE_FBACK:
  218. /*
  219. * Modify the cached value of X_recv [RFC 3448, 4.4]
  220. *
  221. * If (p == 0 || X_calc > 2 * X_recv)
  222. * X_recv = max(X_recv / 2, s / (2 * t_mbi));
  223. * Else
  224. * X_recv = X_calc / 4;
  225. *
  226. * Note that X_recv is scaled by 2^6 while X_calc is not
  227. */
  228. BUG_ON(hctx->ccid3hctx_p && !hctx->ccid3hctx_x_calc);
  229. if (hctx->ccid3hctx_p == 0 ||
  230. (hctx->ccid3hctx_x_calc > (hctx->ccid3hctx_x_recv >> 5))) {
  231. hctx->ccid3hctx_x_recv =
  232. max(hctx->ccid3hctx_x_recv / 2,
  233. (((__u64)hctx->ccid3hctx_s) << 6) /
  234. (2 * TFRC_T_MBI));
  235. } else {
  236. hctx->ccid3hctx_x_recv = hctx->ccid3hctx_x_calc;
  237. hctx->ccid3hctx_x_recv <<= 4;
  238. }
  239. /* Now recalculate X [RFC 3448, 4.3, step (4)] */
  240. ccid3_hc_tx_update_x(sk);
  241. /*
  242. * Schedule no feedback timer to expire in
  243. * max(t_RTO, 2 * s/X) = max(t_RTO, 2 * t_ipi)
  244. * See comments in packet_recv() regarding the value of t_RTO.
  245. */
  246. t_nfb = max(hctx->ccid3hctx_t_rto, 2 * hctx->ccid3hctx_t_ipi);
  247. break;
  248. case TFRC_SSTATE_NO_SENT:
  249. DCCP_BUG("%s(%p) - Illegal state NO_SENT", dccp_role(sk), sk);
  250. /* fall through */
  251. case TFRC_SSTATE_TERM:
  252. goto out;
  253. }
  254. restart_timer:
  255. sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
  256. jiffies + usecs_to_jiffies(t_nfb));
  257. out:
  258. bh_unlock_sock(sk);
  259. sock_put(sk);
  260. }
  261. /*
  262. * returns
  263. * > 0: delay (in msecs) that should pass before actually sending
  264. * = 0: can send immediately
  265. * < 0: error condition; do not send packet
  266. */
  267. static int ccid3_hc_tx_send_packet(struct sock *sk, struct sk_buff *skb)
  268. {
  269. struct dccp_sock *dp = dccp_sk(sk);
  270. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  271. ktime_t now = ktime_get_real();
  272. s64 delay;
  273. BUG_ON(hctx == NULL);
  274. /*
  275. * This function is called only for Data and DataAck packets. Sending
  276. * zero-sized Data(Ack)s is theoretically possible, but for congestion
  277. * control this case is pathological - ignore it.
  278. */
  279. if (unlikely(skb->len == 0))
  280. return -EBADMSG;
  281. switch (hctx->ccid3hctx_state) {
  282. case TFRC_SSTATE_NO_SENT:
  283. sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
  284. (jiffies +
  285. usecs_to_jiffies(TFRC_INITIAL_TIMEOUT)));
  286. hctx->ccid3hctx_last_win_count = 0;
  287. hctx->ccid3hctx_t_last_win_count = now;
  288. /* Set t_0 for initial packet */
  289. hctx->ccid3hctx_t_nom = now;
  290. hctx->ccid3hctx_s = skb->len;
  291. /*
  292. * Use initial RTT sample when available: recommended by erratum
  293. * to RFC 4342. This implements the initialisation procedure of
  294. * draft rfc3448bis, section 4.2. Remember, X is scaled by 2^6.
  295. */
  296. if (dp->dccps_syn_rtt) {
  297. ccid3_pr_debug("SYN RTT = %uus\n", dp->dccps_syn_rtt);
  298. hctx->ccid3hctx_rtt = dp->dccps_syn_rtt;
  299. hctx->ccid3hctx_x = rfc3390_initial_rate(sk);
  300. hctx->ccid3hctx_t_ld = now;
  301. } else {
  302. /* Sender does not have RTT sample: X = MSS/second */
  303. hctx->ccid3hctx_x = dp->dccps_mss_cache;
  304. hctx->ccid3hctx_x <<= 6;
  305. }
  306. ccid3_update_send_interval(hctx);
  307. ccid3_hc_tx_set_state(sk, TFRC_SSTATE_NO_FBACK);
  308. break;
  309. case TFRC_SSTATE_NO_FBACK:
  310. case TFRC_SSTATE_FBACK:
  311. delay = ktime_us_delta(hctx->ccid3hctx_t_nom, now);
  312. ccid3_pr_debug("delay=%ld\n", (long)delay);
  313. /*
  314. * Scheduling of packet transmissions [RFC 3448, 4.6]
  315. *
  316. * if (t_now > t_nom - delta)
  317. * // send the packet now
  318. * else
  319. * // send the packet in (t_nom - t_now) milliseconds.
  320. */
  321. if (delay - (s64)hctx->ccid3hctx_delta >= 1000)
  322. return (u32)delay / 1000L;
  323. ccid3_hc_tx_update_win_count(hctx, now);
  324. break;
  325. case TFRC_SSTATE_TERM:
  326. DCCP_BUG("%s(%p) - Illegal state TERM", dccp_role(sk), sk);
  327. return -EINVAL;
  328. }
  329. /* prepare to send now (add options etc.) */
  330. dp->dccps_hc_tx_insert_options = 1;
  331. DCCP_SKB_CB(skb)->dccpd_ccval = hctx->ccid3hctx_last_win_count;
  332. hctx->ccid3hctx_idle = 0;
  333. /* set the nominal send time for the next following packet */
  334. hctx->ccid3hctx_t_nom = ktime_add_us(hctx->ccid3hctx_t_nom,
  335. hctx->ccid3hctx_t_ipi);
  336. return 0;
  337. }
  338. static void ccid3_hc_tx_packet_sent(struct sock *sk, int more,
  339. unsigned int len)
  340. {
  341. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  342. struct dccp_tx_hist_entry *packet;
  343. BUG_ON(hctx == NULL);
  344. ccid3_hc_tx_update_s(hctx, len);
  345. packet = dccp_tx_hist_entry_new(ccid3_tx_hist, GFP_ATOMIC);
  346. if (unlikely(packet == NULL)) {
  347. DCCP_CRIT("packet history - out of memory!");
  348. return;
  349. }
  350. dccp_tx_hist_add_entry(&hctx->ccid3hctx_hist, packet);
  351. packet->dccphtx_tstamp = ktime_to_timeval(ktime_get_real());
  352. packet->dccphtx_seqno = dccp_sk(sk)->dccps_gss;
  353. packet->dccphtx_rtt = hctx->ccid3hctx_rtt;
  354. packet->dccphtx_sent = 1;
  355. }
  356. static void ccid3_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
  357. {
  358. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  359. struct ccid3_options_received *opt_recv;
  360. struct dccp_tx_hist_entry *packet;
  361. ktime_t now, t_hist;
  362. unsigned long t_nfb;
  363. u32 pinv, r_sample;
  364. BUG_ON(hctx == NULL);
  365. /* we are only interested in ACKs */
  366. if (!(DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK ||
  367. DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_DATAACK))
  368. return;
  369. opt_recv = &hctx->ccid3hctx_options_received;
  370. switch (hctx->ccid3hctx_state) {
  371. case TFRC_SSTATE_NO_FBACK:
  372. case TFRC_SSTATE_FBACK:
  373. /* get packet from history to look up t_recvdata */
  374. packet = dccp_tx_hist_find_entry(&hctx->ccid3hctx_hist,
  375. DCCP_SKB_CB(skb)->dccpd_ack_seq);
  376. if (unlikely(packet == NULL)) {
  377. DCCP_WARN("%s(%p), seqno %llu(%s) doesn't exist "
  378. "in history!\n", dccp_role(sk), sk,
  379. (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
  380. dccp_packet_name(DCCP_SKB_CB(skb)->dccpd_type));
  381. return;
  382. }
  383. /* Update receive rate in units of 64 * bytes/second */
  384. hctx->ccid3hctx_x_recv = opt_recv->ccid3or_receive_rate;
  385. hctx->ccid3hctx_x_recv <<= 6;
  386. /* Update loss event rate */
  387. pinv = opt_recv->ccid3or_loss_event_rate;
  388. if (pinv == ~0U || pinv == 0) /* see RFC 4342, 8.5 */
  389. hctx->ccid3hctx_p = 0;
  390. else /* can not exceed 100% */
  391. hctx->ccid3hctx_p = 1000000 / pinv;
  392. now = ktime_get_real();
  393. t_hist = timeval_to_ktime(packet->dccphtx_tstamp);
  394. /*
  395. * Calculate new round trip sample as per [RFC 3448, 4.3] by
  396. * R_sample = (now - t_recvdata) - t_elapsed
  397. */
  398. r_sample = dccp_sample_rtt(sk, now, &t_hist);
  399. /*
  400. * Update RTT estimate by
  401. * If (No feedback recv)
  402. * R = R_sample;
  403. * Else
  404. * R = q * R + (1 - q) * R_sample;
  405. *
  406. * q is a constant, RFC 3448 recomments 0.9
  407. */
  408. if (hctx->ccid3hctx_state == TFRC_SSTATE_NO_FBACK) {
  409. /*
  410. * Larger Initial Windows [RFC 4342, sec. 5]
  411. */
  412. hctx->ccid3hctx_rtt = r_sample;
  413. hctx->ccid3hctx_x = rfc3390_initial_rate(sk);
  414. hctx->ccid3hctx_t_ld = now;
  415. ccid3_update_send_interval(hctx);
  416. ccid3_pr_debug("%s(%p), s=%u, MSS=%u, "
  417. "R_sample=%uus, X=%u\n", dccp_role(sk),
  418. sk, hctx->ccid3hctx_s,
  419. dccp_sk(sk)->dccps_mss_cache, r_sample,
  420. (unsigned)(hctx->ccid3hctx_x >> 6));
  421. ccid3_hc_tx_set_state(sk, TFRC_SSTATE_FBACK);
  422. } else {
  423. hctx->ccid3hctx_rtt = (9 * hctx->ccid3hctx_rtt +
  424. r_sample) / 10;
  425. /* Update sending rate (step 4 of [RFC 3448, 4.3]) */
  426. if (hctx->ccid3hctx_p > 0)
  427. hctx->ccid3hctx_x_calc =
  428. tfrc_calc_x(hctx->ccid3hctx_s,
  429. hctx->ccid3hctx_rtt,
  430. hctx->ccid3hctx_p);
  431. ccid3_hc_tx_update_x(sk);
  432. ccid3_pr_debug("%s(%p), RTT=%uus (sample=%uus), s=%u, "
  433. "p=%u, X_calc=%u, X_recv=%u, X=%u\n",
  434. dccp_role(sk),
  435. sk, hctx->ccid3hctx_rtt, r_sample,
  436. hctx->ccid3hctx_s, hctx->ccid3hctx_p,
  437. hctx->ccid3hctx_x_calc,
  438. (unsigned)(hctx->ccid3hctx_x_recv >> 6),
  439. (unsigned)(hctx->ccid3hctx_x >> 6));
  440. }
  441. /* unschedule no feedback timer */
  442. sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
  443. /* remove all packets older than the one acked from history */
  444. dccp_tx_hist_purge_older(ccid3_tx_hist,
  445. &hctx->ccid3hctx_hist, packet);
  446. /*
  447. * As we have calculated new ipi, delta, t_nom it is possible
  448. * that we now can send a packet, so wake up dccp_wait_for_ccid
  449. */
  450. sk->sk_write_space(sk);
  451. /*
  452. * Update timeout interval for the nofeedback timer.
  453. * We use a configuration option to increase the lower bound.
  454. * This can help avoid triggering the nofeedback timer too
  455. * often ('spinning') on LANs with small RTTs.
  456. */
  457. hctx->ccid3hctx_t_rto = max_t(u32, 4 * hctx->ccid3hctx_rtt,
  458. CONFIG_IP_DCCP_CCID3_RTO *
  459. (USEC_PER_SEC/1000));
  460. /*
  461. * Schedule no feedback timer to expire in
  462. * max(t_RTO, 2 * s/X) = max(t_RTO, 2 * t_ipi)
  463. */
  464. t_nfb = max(hctx->ccid3hctx_t_rto, 2 * hctx->ccid3hctx_t_ipi);
  465. ccid3_pr_debug("%s(%p), Scheduled no feedback timer to "
  466. "expire in %lu jiffies (%luus)\n",
  467. dccp_role(sk),
  468. sk, usecs_to_jiffies(t_nfb), t_nfb);
  469. sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
  470. jiffies + usecs_to_jiffies(t_nfb));
  471. /* set idle flag */
  472. hctx->ccid3hctx_idle = 1;
  473. break;
  474. case TFRC_SSTATE_NO_SENT: /* fall through */
  475. case TFRC_SSTATE_TERM: /* ignore feedback when closing */
  476. break;
  477. }
  478. }
  479. static int ccid3_hc_tx_parse_options(struct sock *sk, unsigned char option,
  480. unsigned char len, u16 idx,
  481. unsigned char *value)
  482. {
  483. int rc = 0;
  484. const struct dccp_sock *dp = dccp_sk(sk);
  485. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  486. struct ccid3_options_received *opt_recv;
  487. BUG_ON(hctx == NULL);
  488. opt_recv = &hctx->ccid3hctx_options_received;
  489. if (opt_recv->ccid3or_seqno != dp->dccps_gsr) {
  490. opt_recv->ccid3or_seqno = dp->dccps_gsr;
  491. opt_recv->ccid3or_loss_event_rate = ~0;
  492. opt_recv->ccid3or_loss_intervals_idx = 0;
  493. opt_recv->ccid3or_loss_intervals_len = 0;
  494. opt_recv->ccid3or_receive_rate = 0;
  495. }
  496. switch (option) {
  497. case TFRC_OPT_LOSS_EVENT_RATE:
  498. if (unlikely(len != 4)) {
  499. DCCP_WARN("%s(%p), invalid len %d "
  500. "for TFRC_OPT_LOSS_EVENT_RATE\n",
  501. dccp_role(sk), sk, len);
  502. rc = -EINVAL;
  503. } else {
  504. opt_recv->ccid3or_loss_event_rate =
  505. ntohl(*(__be32 *)value);
  506. ccid3_pr_debug("%s(%p), LOSS_EVENT_RATE=%u\n",
  507. dccp_role(sk), sk,
  508. opt_recv->ccid3or_loss_event_rate);
  509. }
  510. break;
  511. case TFRC_OPT_LOSS_INTERVALS:
  512. opt_recv->ccid3or_loss_intervals_idx = idx;
  513. opt_recv->ccid3or_loss_intervals_len = len;
  514. ccid3_pr_debug("%s(%p), LOSS_INTERVALS=(%u, %u)\n",
  515. dccp_role(sk), sk,
  516. opt_recv->ccid3or_loss_intervals_idx,
  517. opt_recv->ccid3or_loss_intervals_len);
  518. break;
  519. case TFRC_OPT_RECEIVE_RATE:
  520. if (unlikely(len != 4)) {
  521. DCCP_WARN("%s(%p), invalid len %d "
  522. "for TFRC_OPT_RECEIVE_RATE\n",
  523. dccp_role(sk), sk, len);
  524. rc = -EINVAL;
  525. } else {
  526. opt_recv->ccid3or_receive_rate =
  527. ntohl(*(__be32 *)value);
  528. ccid3_pr_debug("%s(%p), RECEIVE_RATE=%u\n",
  529. dccp_role(sk), sk,
  530. opt_recv->ccid3or_receive_rate);
  531. }
  532. break;
  533. }
  534. return rc;
  535. }
  536. static int ccid3_hc_tx_init(struct ccid *ccid, struct sock *sk)
  537. {
  538. struct ccid3_hc_tx_sock *hctx = ccid_priv(ccid);
  539. hctx->ccid3hctx_s = 0;
  540. hctx->ccid3hctx_rtt = 0;
  541. hctx->ccid3hctx_state = TFRC_SSTATE_NO_SENT;
  542. INIT_LIST_HEAD(&hctx->ccid3hctx_hist);
  543. hctx->ccid3hctx_no_feedback_timer.function =
  544. ccid3_hc_tx_no_feedback_timer;
  545. hctx->ccid3hctx_no_feedback_timer.data = (unsigned long)sk;
  546. init_timer(&hctx->ccid3hctx_no_feedback_timer);
  547. return 0;
  548. }
  549. static void ccid3_hc_tx_exit(struct sock *sk)
  550. {
  551. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  552. BUG_ON(hctx == NULL);
  553. ccid3_hc_tx_set_state(sk, TFRC_SSTATE_TERM);
  554. sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
  555. /* Empty packet history */
  556. dccp_tx_hist_purge(ccid3_tx_hist, &hctx->ccid3hctx_hist);
  557. }
  558. static void ccid3_hc_tx_get_info(struct sock *sk, struct tcp_info *info)
  559. {
  560. const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  561. /* Listen socks doesn't have a private CCID block */
  562. if (sk->sk_state == DCCP_LISTEN)
  563. return;
  564. BUG_ON(hctx == NULL);
  565. info->tcpi_rto = hctx->ccid3hctx_t_rto;
  566. info->tcpi_rtt = hctx->ccid3hctx_rtt;
  567. }
  568. static int ccid3_hc_tx_getsockopt(struct sock *sk, const int optname, int len,
  569. u32 __user *optval, int __user *optlen)
  570. {
  571. const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  572. const void *val;
  573. /* Listen socks doesn't have a private CCID block */
  574. if (sk->sk_state == DCCP_LISTEN)
  575. return -EINVAL;
  576. switch (optname) {
  577. case DCCP_SOCKOPT_CCID_TX_INFO:
  578. if (len < sizeof(hctx->ccid3hctx_tfrc))
  579. return -EINVAL;
  580. len = sizeof(hctx->ccid3hctx_tfrc);
  581. val = &hctx->ccid3hctx_tfrc;
  582. break;
  583. default:
  584. return -ENOPROTOOPT;
  585. }
  586. if (put_user(len, optlen) || copy_to_user(optval, val, len))
  587. return -EFAULT;
  588. return 0;
  589. }
  590. /*
  591. * Receiver Half-Connection Routines
  592. */
  593. #ifdef CONFIG_IP_DCCP_CCID3_DEBUG
  594. static const char *ccid3_rx_state_name(enum ccid3_hc_rx_states state)
  595. {
  596. static char *ccid3_rx_state_names[] = {
  597. [TFRC_RSTATE_NO_DATA] = "NO_DATA",
  598. [TFRC_RSTATE_DATA] = "DATA",
  599. [TFRC_RSTATE_TERM] = "TERM",
  600. };
  601. return ccid3_rx_state_names[state];
  602. }
  603. #endif
  604. static void ccid3_hc_rx_set_state(struct sock *sk,
  605. enum ccid3_hc_rx_states state)
  606. {
  607. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  608. enum ccid3_hc_rx_states oldstate = hcrx->ccid3hcrx_state;
  609. ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
  610. dccp_role(sk), sk, ccid3_rx_state_name(oldstate),
  611. ccid3_rx_state_name(state));
  612. WARN_ON(state == oldstate);
  613. hcrx->ccid3hcrx_state = state;
  614. }
  615. static inline void ccid3_hc_rx_update_s(struct ccid3_hc_rx_sock *hcrx, int len)
  616. {
  617. if (unlikely(len == 0)) /* don't update on empty packets (e.g. ACKs) */
  618. ccid3_pr_debug("Packet payload length is 0 - not updating\n");
  619. else
  620. hcrx->ccid3hcrx_s = hcrx->ccid3hcrx_s == 0 ? len :
  621. (9 * hcrx->ccid3hcrx_s + len) / 10;
  622. }
  623. static void ccid3_hc_rx_send_feedback(struct sock *sk)
  624. {
  625. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  626. struct dccp_sock *dp = dccp_sk(sk);
  627. struct dccp_rx_hist_entry *packet;
  628. ktime_t now, t_hist;
  629. suseconds_t delta;
  630. ccid3_pr_debug("%s(%p) - entry \n", dccp_role(sk), sk);
  631. now = ktime_get_real();
  632. switch (hcrx->ccid3hcrx_state) {
  633. case TFRC_RSTATE_NO_DATA:
  634. hcrx->ccid3hcrx_x_recv = 0;
  635. break;
  636. case TFRC_RSTATE_DATA:
  637. delta = ktime_us_delta(now,
  638. hcrx->ccid3hcrx_tstamp_last_feedback);
  639. DCCP_BUG_ON(delta < 0);
  640. hcrx->ccid3hcrx_x_recv =
  641. scaled_div32(hcrx->ccid3hcrx_bytes_recv, delta);
  642. break;
  643. case TFRC_RSTATE_TERM:
  644. DCCP_BUG("%s(%p) - Illegal state TERM", dccp_role(sk), sk);
  645. return;
  646. }
  647. packet = dccp_rx_hist_find_data_packet(&hcrx->ccid3hcrx_hist);
  648. if (unlikely(packet == NULL)) {
  649. DCCP_WARN("%s(%p), no data packet in history!\n",
  650. dccp_role(sk), sk);
  651. return;
  652. }
  653. hcrx->ccid3hcrx_tstamp_last_feedback = now;
  654. hcrx->ccid3hcrx_ccval_last_counter = packet->dccphrx_ccval;
  655. hcrx->ccid3hcrx_bytes_recv = 0;
  656. /* Elapsed time information [RFC 4340, 13.2] in units of 10 * usecs */
  657. t_hist = timeval_to_ktime(packet->dccphrx_tstamp);
  658. delta = ktime_us_delta(now, t_hist);
  659. DCCP_BUG_ON(delta < 0);
  660. hcrx->ccid3hcrx_elapsed_time = delta / 10;
  661. if (hcrx->ccid3hcrx_p == 0)
  662. hcrx->ccid3hcrx_pinv = ~0U; /* see RFC 4342, 8.5 */
  663. else if (hcrx->ccid3hcrx_p > 1000000) {
  664. DCCP_WARN("p (%u) > 100%%\n", hcrx->ccid3hcrx_p);
  665. hcrx->ccid3hcrx_pinv = 1; /* use 100% in this case */
  666. } else
  667. hcrx->ccid3hcrx_pinv = 1000000 / hcrx->ccid3hcrx_p;
  668. dp->dccps_hc_rx_insert_options = 1;
  669. dccp_send_ack(sk);
  670. }
  671. static int ccid3_hc_rx_insert_options(struct sock *sk, struct sk_buff *skb)
  672. {
  673. const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  674. __be32 x_recv, pinv;
  675. BUG_ON(hcrx == NULL);
  676. if (!(sk->sk_state == DCCP_OPEN || sk->sk_state == DCCP_PARTOPEN))
  677. return 0;
  678. DCCP_SKB_CB(skb)->dccpd_ccval = hcrx->ccid3hcrx_ccval_last_counter;
  679. if (dccp_packet_without_ack(skb))
  680. return 0;
  681. x_recv = htonl(hcrx->ccid3hcrx_x_recv);
  682. pinv = htonl(hcrx->ccid3hcrx_pinv);
  683. if ((hcrx->ccid3hcrx_elapsed_time != 0 &&
  684. dccp_insert_option_elapsed_time(sk, skb,
  685. hcrx->ccid3hcrx_elapsed_time)) ||
  686. dccp_insert_option_timestamp(sk, skb) ||
  687. dccp_insert_option(sk, skb, TFRC_OPT_LOSS_EVENT_RATE,
  688. &pinv, sizeof(pinv)) ||
  689. dccp_insert_option(sk, skb, TFRC_OPT_RECEIVE_RATE,
  690. &x_recv, sizeof(x_recv)))
  691. return -1;
  692. return 0;
  693. }
  694. static int ccid3_hc_rx_detect_loss(struct sock *sk,
  695. struct dccp_rx_hist_entry *packet)
  696. {
  697. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  698. struct dccp_rx_hist_entry *rx_hist =
  699. dccp_rx_hist_head(&hcrx->ccid3hcrx_hist);
  700. u64 seqno = packet->dccphrx_seqno;
  701. u64 tmp_seqno;
  702. int loss = 0;
  703. u8 ccval;
  704. tmp_seqno = hcrx->ccid3hcrx_seqno_nonloss;
  705. if (!rx_hist ||
  706. follows48(packet->dccphrx_seqno, hcrx->ccid3hcrx_seqno_nonloss)) {
  707. hcrx->ccid3hcrx_seqno_nonloss = seqno;
  708. hcrx->ccid3hcrx_ccval_nonloss = packet->dccphrx_ccval;
  709. goto detect_out;
  710. }
  711. while (dccp_delta_seqno(hcrx->ccid3hcrx_seqno_nonloss, seqno)
  712. > TFRC_RECV_NUM_LATE_LOSS) {
  713. struct timeval tstamp =
  714. ktime_to_timeval(hcrx->ccid3hcrx_tstamp_last_feedback);
  715. loss = 1;
  716. dccp_li_update_li(sk,
  717. &hcrx->ccid3hcrx_li_hist,
  718. &hcrx->ccid3hcrx_hist,
  719. &tstamp,
  720. hcrx->ccid3hcrx_s,
  721. hcrx->ccid3hcrx_bytes_recv,
  722. hcrx->ccid3hcrx_x_recv,
  723. hcrx->ccid3hcrx_seqno_nonloss,
  724. hcrx->ccid3hcrx_ccval_nonloss);
  725. tmp_seqno = hcrx->ccid3hcrx_seqno_nonloss;
  726. dccp_inc_seqno(&tmp_seqno);
  727. hcrx->ccid3hcrx_seqno_nonloss = tmp_seqno;
  728. dccp_inc_seqno(&tmp_seqno);
  729. while (dccp_rx_hist_find_entry(&hcrx->ccid3hcrx_hist,
  730. tmp_seqno, &ccval)) {
  731. hcrx->ccid3hcrx_seqno_nonloss = tmp_seqno;
  732. hcrx->ccid3hcrx_ccval_nonloss = ccval;
  733. dccp_inc_seqno(&tmp_seqno);
  734. }
  735. }
  736. /* FIXME - this code could be simplified with above while */
  737. /* but works at moment */
  738. if (follows48(packet->dccphrx_seqno, hcrx->ccid3hcrx_seqno_nonloss)) {
  739. hcrx->ccid3hcrx_seqno_nonloss = seqno;
  740. hcrx->ccid3hcrx_ccval_nonloss = packet->dccphrx_ccval;
  741. }
  742. detect_out:
  743. dccp_rx_hist_add_packet(ccid3_rx_hist, &hcrx->ccid3hcrx_hist,
  744. &hcrx->ccid3hcrx_li_hist, packet,
  745. hcrx->ccid3hcrx_seqno_nonloss);
  746. return loss;
  747. }
  748. static void ccid3_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
  749. {
  750. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  751. const struct dccp_options_received *opt_recv;
  752. struct dccp_rx_hist_entry *packet;
  753. u32 p_prev, r_sample, rtt_prev;
  754. int loss, payload_size;
  755. ktime_t now;
  756. BUG_ON(hcrx == NULL);
  757. opt_recv = &dccp_sk(sk)->dccps_options_received;
  758. switch (DCCP_SKB_CB(skb)->dccpd_type) {
  759. case DCCP_PKT_ACK:
  760. if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
  761. return;
  762. case DCCP_PKT_DATAACK:
  763. if (opt_recv->dccpor_timestamp_echo == 0)
  764. break;
  765. rtt_prev = hcrx->ccid3hcrx_rtt;
  766. now = ktime_get_real();
  767. r_sample = dccp_sample_rtt(sk, now, NULL);
  768. if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
  769. hcrx->ccid3hcrx_rtt = r_sample;
  770. else
  771. hcrx->ccid3hcrx_rtt = (hcrx->ccid3hcrx_rtt * 9) / 10 +
  772. r_sample / 10;
  773. if (rtt_prev != hcrx->ccid3hcrx_rtt)
  774. ccid3_pr_debug("%s(%p), New RTT=%uus, elapsed time=%u\n",
  775. dccp_role(sk), sk, hcrx->ccid3hcrx_rtt,
  776. opt_recv->dccpor_elapsed_time);
  777. break;
  778. case DCCP_PKT_DATA:
  779. break;
  780. default: /* We're not interested in other packet types, move along */
  781. return;
  782. }
  783. packet = dccp_rx_hist_entry_new(ccid3_rx_hist, sk, opt_recv->dccpor_ndp,
  784. skb, GFP_ATOMIC);
  785. if (unlikely(packet == NULL)) {
  786. DCCP_WARN("%s(%p), Not enough mem to add rx packet "
  787. "to history, consider it lost!\n", dccp_role(sk), sk);
  788. return;
  789. }
  790. loss = ccid3_hc_rx_detect_loss(sk, packet);
  791. if (DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK)
  792. return;
  793. payload_size = skb->len - dccp_hdr(skb)->dccph_doff * 4;
  794. ccid3_hc_rx_update_s(hcrx, payload_size);
  795. switch (hcrx->ccid3hcrx_state) {
  796. case TFRC_RSTATE_NO_DATA:
  797. ccid3_pr_debug("%s(%p, state=%s), skb=%p, sending initial "
  798. "feedback\n", dccp_role(sk), sk,
  799. dccp_state_name(sk->sk_state), skb);
  800. ccid3_hc_rx_send_feedback(sk);
  801. ccid3_hc_rx_set_state(sk, TFRC_RSTATE_DATA);
  802. return;
  803. case TFRC_RSTATE_DATA:
  804. hcrx->ccid3hcrx_bytes_recv += payload_size;
  805. if (loss)
  806. break;
  807. now = ktime_get_real();
  808. if ((ktime_us_delta(now, hcrx->ccid3hcrx_tstamp_last_ack) -
  809. (s64)hcrx->ccid3hcrx_rtt) >= 0) {
  810. hcrx->ccid3hcrx_tstamp_last_ack = now;
  811. ccid3_hc_rx_send_feedback(sk);
  812. }
  813. return;
  814. case TFRC_RSTATE_TERM:
  815. DCCP_BUG("%s(%p) - Illegal state TERM", dccp_role(sk), sk);
  816. return;
  817. }
  818. /* Dealing with packet loss */
  819. ccid3_pr_debug("%s(%p, state=%s), data loss! Reacting...\n",
  820. dccp_role(sk), sk, dccp_state_name(sk->sk_state));
  821. p_prev = hcrx->ccid3hcrx_p;
  822. /* Calculate loss event rate */
  823. if (!list_empty(&hcrx->ccid3hcrx_li_hist)) {
  824. u32 i_mean = dccp_li_hist_calc_i_mean(&hcrx->ccid3hcrx_li_hist);
  825. /* Scaling up by 1000000 as fixed decimal */
  826. if (i_mean != 0)
  827. hcrx->ccid3hcrx_p = 1000000 / i_mean;
  828. } else
  829. DCCP_BUG("empty loss history");
  830. if (hcrx->ccid3hcrx_p > p_prev) {
  831. ccid3_hc_rx_send_feedback(sk);
  832. return;
  833. }
  834. }
  835. static int ccid3_hc_rx_init(struct ccid *ccid, struct sock *sk)
  836. {
  837. struct ccid3_hc_rx_sock *hcrx = ccid_priv(ccid);
  838. ccid3_pr_debug("entry\n");
  839. hcrx->ccid3hcrx_state = TFRC_RSTATE_NO_DATA;
  840. INIT_LIST_HEAD(&hcrx->ccid3hcrx_hist);
  841. INIT_LIST_HEAD(&hcrx->ccid3hcrx_li_hist);
  842. hcrx->ccid3hcrx_tstamp_last_feedback =
  843. hcrx->ccid3hcrx_tstamp_last_ack = ktime_get_real();
  844. hcrx->ccid3hcrx_s = 0;
  845. hcrx->ccid3hcrx_rtt = 0;
  846. return 0;
  847. }
  848. static void ccid3_hc_rx_exit(struct sock *sk)
  849. {
  850. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  851. BUG_ON(hcrx == NULL);
  852. ccid3_hc_rx_set_state(sk, TFRC_RSTATE_TERM);
  853. /* Empty packet history */
  854. dccp_rx_hist_purge(ccid3_rx_hist, &hcrx->ccid3hcrx_hist);
  855. /* Empty loss interval history */
  856. dccp_li_hist_purge(&hcrx->ccid3hcrx_li_hist);
  857. }
  858. static void ccid3_hc_rx_get_info(struct sock *sk, struct tcp_info *info)
  859. {
  860. const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  861. /* Listen socks doesn't have a private CCID block */
  862. if (sk->sk_state == DCCP_LISTEN)
  863. return;
  864. BUG_ON(hcrx == NULL);
  865. info->tcpi_ca_state = hcrx->ccid3hcrx_state;
  866. info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
  867. info->tcpi_rcv_rtt = hcrx->ccid3hcrx_rtt;
  868. }
  869. static int ccid3_hc_rx_getsockopt(struct sock *sk, const int optname, int len,
  870. u32 __user *optval, int __user *optlen)
  871. {
  872. const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  873. const void *val;
  874. /* Listen socks doesn't have a private CCID block */
  875. if (sk->sk_state == DCCP_LISTEN)
  876. return -EINVAL;
  877. switch (optname) {
  878. case DCCP_SOCKOPT_CCID_RX_INFO:
  879. if (len < sizeof(hcrx->ccid3hcrx_tfrc))
  880. return -EINVAL;
  881. len = sizeof(hcrx->ccid3hcrx_tfrc);
  882. val = &hcrx->ccid3hcrx_tfrc;
  883. break;
  884. default:
  885. return -ENOPROTOOPT;
  886. }
  887. if (put_user(len, optlen) || copy_to_user(optval, val, len))
  888. return -EFAULT;
  889. return 0;
  890. }
  891. static struct ccid_operations ccid3 = {
  892. .ccid_id = DCCPC_CCID3,
  893. .ccid_name = "ccid3",
  894. .ccid_owner = THIS_MODULE,
  895. .ccid_hc_tx_obj_size = sizeof(struct ccid3_hc_tx_sock),
  896. .ccid_hc_tx_init = ccid3_hc_tx_init,
  897. .ccid_hc_tx_exit = ccid3_hc_tx_exit,
  898. .ccid_hc_tx_send_packet = ccid3_hc_tx_send_packet,
  899. .ccid_hc_tx_packet_sent = ccid3_hc_tx_packet_sent,
  900. .ccid_hc_tx_packet_recv = ccid3_hc_tx_packet_recv,
  901. .ccid_hc_tx_parse_options = ccid3_hc_tx_parse_options,
  902. .ccid_hc_rx_obj_size = sizeof(struct ccid3_hc_rx_sock),
  903. .ccid_hc_rx_init = ccid3_hc_rx_init,
  904. .ccid_hc_rx_exit = ccid3_hc_rx_exit,
  905. .ccid_hc_rx_insert_options = ccid3_hc_rx_insert_options,
  906. .ccid_hc_rx_packet_recv = ccid3_hc_rx_packet_recv,
  907. .ccid_hc_rx_get_info = ccid3_hc_rx_get_info,
  908. .ccid_hc_tx_get_info = ccid3_hc_tx_get_info,
  909. .ccid_hc_rx_getsockopt = ccid3_hc_rx_getsockopt,
  910. .ccid_hc_tx_getsockopt = ccid3_hc_tx_getsockopt,
  911. };
  912. #ifdef CONFIG_IP_DCCP_CCID3_DEBUG
  913. module_param(ccid3_debug, int, 0444);
  914. MODULE_PARM_DESC(ccid3_debug, "Enable debug messages");
  915. #endif
  916. static __init int ccid3_module_init(void)
  917. {
  918. int rc = -ENOBUFS;
  919. ccid3_rx_hist = dccp_rx_hist_new("ccid3");
  920. if (ccid3_rx_hist == NULL)
  921. goto out;
  922. ccid3_tx_hist = dccp_tx_hist_new("ccid3");
  923. if (ccid3_tx_hist == NULL)
  924. goto out_free_rx;
  925. rc = ccid_register(&ccid3);
  926. if (rc != 0)
  927. goto out_free_tx;
  928. out:
  929. return rc;
  930. out_free_tx:
  931. dccp_tx_hist_delete(ccid3_tx_hist);
  932. ccid3_tx_hist = NULL;
  933. out_free_rx:
  934. dccp_rx_hist_delete(ccid3_rx_hist);
  935. ccid3_rx_hist = NULL;
  936. goto out;
  937. }
  938. module_init(ccid3_module_init);
  939. static __exit void ccid3_module_exit(void)
  940. {
  941. ccid_unregister(&ccid3);
  942. if (ccid3_tx_hist != NULL) {
  943. dccp_tx_hist_delete(ccid3_tx_hist);
  944. ccid3_tx_hist = NULL;
  945. }
  946. if (ccid3_rx_hist != NULL) {
  947. dccp_rx_hist_delete(ccid3_rx_hist);
  948. ccid3_rx_hist = NULL;
  949. }
  950. }
  951. module_exit(ccid3_module_exit);
  952. MODULE_AUTHOR("Ian McDonald <ian.mcdonald@jandi.co.nz>, "
  953. "Arnaldo Carvalho de Melo <acme@ghostprotocols.net>");
  954. MODULE_DESCRIPTION("DCCP TFRC CCID3 CCID");
  955. MODULE_LICENSE("GPL");
  956. MODULE_ALIAS("net-dccp-ccid-3");