ccid3.c 37 KB

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