ccid3.c 37 KB

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