ccid3.c 37 KB

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