ccid3.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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. #include <asm/unaligned.h>
  43. #ifdef CONFIG_IP_DCCP_CCID3_DEBUG
  44. static int ccid3_debug;
  45. #define ccid3_pr_debug(format, a...) DCCP_PR_DEBUG(ccid3_debug, format, ##a)
  46. #else
  47. #define ccid3_pr_debug(format, a...)
  48. #endif
  49. /*
  50. * Transmitter Half-Connection Routines
  51. */
  52. #ifdef CONFIG_IP_DCCP_CCID3_DEBUG
  53. static const char *ccid3_tx_state_name(enum ccid3_hc_tx_states state)
  54. {
  55. static char *ccid3_state_names[] = {
  56. [TFRC_SSTATE_NO_SENT] = "NO_SENT",
  57. [TFRC_SSTATE_NO_FBACK] = "NO_FBACK",
  58. [TFRC_SSTATE_FBACK] = "FBACK",
  59. [TFRC_SSTATE_TERM] = "TERM",
  60. };
  61. return ccid3_state_names[state];
  62. }
  63. #endif
  64. static void ccid3_hc_tx_set_state(struct sock *sk,
  65. enum ccid3_hc_tx_states state)
  66. {
  67. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  68. enum ccid3_hc_tx_states oldstate = hctx->ccid3hctx_state;
  69. ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
  70. dccp_role(sk), sk, ccid3_tx_state_name(oldstate),
  71. ccid3_tx_state_name(state));
  72. WARN_ON(state == oldstate);
  73. hctx->ccid3hctx_state = state;
  74. }
  75. /*
  76. * Compute the initial sending rate X_init in the manner of RFC 3390:
  77. *
  78. * X_init = min(4 * s, max(2 * s, 4380 bytes)) / RTT
  79. *
  80. * Note that RFC 3390 uses MSS, RFC 4342 refers to RFC 3390, and rfc3448bis
  81. * (rev-02) clarifies the use of RFC 3390 with regard to the above formula.
  82. * For consistency with other parts of the code, X_init is scaled by 2^6.
  83. */
  84. static inline u64 rfc3390_initial_rate(struct sock *sk)
  85. {
  86. const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  87. const __u32 w_init = min_t(__u32, 4 * hctx->ccid3hctx_s,
  88. max_t(__u32, 2 * hctx->ccid3hctx_s, 4380));
  89. return scaled_div(w_init << 6, hctx->ccid3hctx_rtt);
  90. }
  91. /*
  92. * Recalculate t_ipi and delta (should be called whenever X changes)
  93. */
  94. static inline void ccid3_update_send_interval(struct ccid3_hc_tx_sock *hctx)
  95. {
  96. /* Calculate new t_ipi = s / X_inst (X_inst is in 64 * bytes/second) */
  97. hctx->ccid3hctx_t_ipi = scaled_div32(((u64)hctx->ccid3hctx_s) << 6,
  98. hctx->ccid3hctx_x);
  99. /* Calculate new delta by delta = min(t_ipi / 2, t_gran / 2) */
  100. hctx->ccid3hctx_delta = min_t(u32, hctx->ccid3hctx_t_ipi / 2,
  101. TFRC_OPSYS_HALF_TIME_GRAN);
  102. ccid3_pr_debug("t_ipi=%u, delta=%u, s=%u, X=%u\n",
  103. hctx->ccid3hctx_t_ipi, hctx->ccid3hctx_delta,
  104. hctx->ccid3hctx_s, (unsigned)(hctx->ccid3hctx_x >> 6));
  105. }
  106. static u32 ccid3_hc_tx_idle_rtt(struct ccid3_hc_tx_sock *hctx, ktime_t now)
  107. {
  108. u32 delta = ktime_us_delta(now, hctx->ccid3hctx_t_last_win_count);
  109. return delta / hctx->ccid3hctx_rtt;
  110. }
  111. /**
  112. * ccid3_hc_tx_update_x - Update allowed sending rate X
  113. * @stamp: most recent time if available - can be left NULL.
  114. * This function tracks draft rfc3448bis, check there for latest details.
  115. *
  116. * Note: X and X_recv are both stored in units of 64 * bytes/second, to support
  117. * fine-grained resolution of sending rates. This requires scaling by 2^6
  118. * throughout the code. Only X_calc is unscaled (in bytes/second).
  119. *
  120. */
  121. static void ccid3_hc_tx_update_x(struct sock *sk, ktime_t *stamp)
  122. {
  123. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  124. __u64 min_rate = 2 * hctx->ccid3hctx_x_recv;
  125. const __u64 old_x = hctx->ccid3hctx_x;
  126. ktime_t now = stamp? *stamp : ktime_get_real();
  127. /*
  128. * Handle IDLE periods: do not reduce below RFC3390 initial sending rate
  129. * when idling [RFC 4342, 5.1]. Definition of idling is from rfc3448bis:
  130. * a sender is idle if it has not sent anything over a 2-RTT-period.
  131. * For consistency with X and X_recv, min_rate is also scaled by 2^6.
  132. */
  133. if (ccid3_hc_tx_idle_rtt(hctx, now) >= 2) {
  134. min_rate = rfc3390_initial_rate(sk);
  135. min_rate = max(min_rate, 2 * hctx->ccid3hctx_x_recv);
  136. }
  137. if (hctx->ccid3hctx_p > 0) {
  138. hctx->ccid3hctx_x = min(((__u64)hctx->ccid3hctx_x_calc) << 6,
  139. min_rate);
  140. hctx->ccid3hctx_x = max(hctx->ccid3hctx_x,
  141. (((__u64)hctx->ccid3hctx_s) << 6) /
  142. TFRC_T_MBI);
  143. } else if (ktime_us_delta(now, hctx->ccid3hctx_t_ld)
  144. - (s64)hctx->ccid3hctx_rtt >= 0) {
  145. hctx->ccid3hctx_x =
  146. max(min(2 * hctx->ccid3hctx_x, min_rate),
  147. scaled_div(((__u64)hctx->ccid3hctx_s) << 6,
  148. hctx->ccid3hctx_rtt));
  149. hctx->ccid3hctx_t_ld = now;
  150. }
  151. if (hctx->ccid3hctx_x != old_x) {
  152. ccid3_pr_debug("X_prev=%u, X_now=%u, X_calc=%u, "
  153. "X_recv=%u\n", (unsigned)(old_x >> 6),
  154. (unsigned)(hctx->ccid3hctx_x >> 6),
  155. hctx->ccid3hctx_x_calc,
  156. (unsigned)(hctx->ccid3hctx_x_recv >> 6));
  157. ccid3_update_send_interval(hctx);
  158. }
  159. }
  160. /*
  161. * Track the mean packet size `s' (cf. RFC 4342, 5.3 and RFC 3448, 4.1)
  162. * @len: DCCP packet payload size in bytes
  163. */
  164. static inline void ccid3_hc_tx_update_s(struct ccid3_hc_tx_sock *hctx, int len)
  165. {
  166. const u16 old_s = hctx->ccid3hctx_s;
  167. hctx->ccid3hctx_s = tfrc_ewma(hctx->ccid3hctx_s, len, 9);
  168. if (hctx->ccid3hctx_s != old_s)
  169. ccid3_update_send_interval(hctx);
  170. }
  171. /*
  172. * Update Window Counter using the algorithm from [RFC 4342, 8.1].
  173. * The algorithm is not applicable if RTT < 4 microseconds.
  174. */
  175. static inline void ccid3_hc_tx_update_win_count(struct ccid3_hc_tx_sock *hctx,
  176. ktime_t now)
  177. {
  178. u32 quarter_rtts;
  179. if (unlikely(hctx->ccid3hctx_rtt < 4)) /* avoid divide-by-zero */
  180. return;
  181. quarter_rtts = ktime_us_delta(now, hctx->ccid3hctx_t_last_win_count);
  182. quarter_rtts /= hctx->ccid3hctx_rtt / 4;
  183. if (quarter_rtts > 0) {
  184. hctx->ccid3hctx_t_last_win_count = now;
  185. hctx->ccid3hctx_last_win_count += min_t(u32, quarter_rtts, 5);
  186. hctx->ccid3hctx_last_win_count &= 0xF; /* mod 16 */
  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. unsigned long t_nfb = USEC_PER_SEC / 5;
  194. bh_lock_sock(sk);
  195. if (sock_owned_by_user(sk)) {
  196. /* Try again later. */
  197. /* XXX: set some sensible MIB */
  198. goto restart_timer;
  199. }
  200. ccid3_pr_debug("%s(%p, state=%s) - entry \n", dccp_role(sk), sk,
  201. ccid3_tx_state_name(hctx->ccid3hctx_state));
  202. switch (hctx->ccid3hctx_state) {
  203. case TFRC_SSTATE_NO_FBACK:
  204. /* RFC 3448, 4.4: Halve send rate directly */
  205. hctx->ccid3hctx_x = max(hctx->ccid3hctx_x / 2,
  206. (((__u64)hctx->ccid3hctx_s) << 6) /
  207. TFRC_T_MBI);
  208. ccid3_pr_debug("%s(%p, state=%s), updated tx rate to %u "
  209. "bytes/s\n", dccp_role(sk), sk,
  210. ccid3_tx_state_name(hctx->ccid3hctx_state),
  211. (unsigned)(hctx->ccid3hctx_x >> 6));
  212. /* The value of R is still undefined and so we can not recompute
  213. * the timeout value. Keep initial value as per [RFC 4342, 5]. */
  214. t_nfb = TFRC_INITIAL_TIMEOUT;
  215. ccid3_update_send_interval(hctx);
  216. break;
  217. case TFRC_SSTATE_FBACK:
  218. /*
  219. * Modify the cached value of X_recv [RFC 3448, 4.4]
  220. *
  221. * If (p == 0 || X_calc > 2 * X_recv)
  222. * X_recv = max(X_recv / 2, s / (2 * t_mbi));
  223. * Else
  224. * X_recv = X_calc / 4;
  225. *
  226. * Note that X_recv is scaled by 2^6 while X_calc is not
  227. */
  228. BUG_ON(hctx->ccid3hctx_p && !hctx->ccid3hctx_x_calc);
  229. if (hctx->ccid3hctx_p == 0 ||
  230. (hctx->ccid3hctx_x_calc > (hctx->ccid3hctx_x_recv >> 5))) {
  231. hctx->ccid3hctx_x_recv =
  232. max(hctx->ccid3hctx_x_recv / 2,
  233. (((__u64)hctx->ccid3hctx_s) << 6) /
  234. (2 * TFRC_T_MBI));
  235. } else {
  236. hctx->ccid3hctx_x_recv = hctx->ccid3hctx_x_calc;
  237. hctx->ccid3hctx_x_recv <<= 4;
  238. }
  239. /* Now recalculate X [RFC 3448, 4.3, step (4)] */
  240. ccid3_hc_tx_update_x(sk, NULL);
  241. /*
  242. * Schedule no feedback timer to expire in
  243. * max(t_RTO, 2 * s/X) = max(t_RTO, 2 * t_ipi)
  244. * See comments in packet_recv() regarding the value of t_RTO.
  245. */
  246. t_nfb = max(hctx->ccid3hctx_t_rto, 2 * hctx->ccid3hctx_t_ipi);
  247. break;
  248. case TFRC_SSTATE_NO_SENT:
  249. DCCP_BUG("%s(%p) - Illegal state NO_SENT", dccp_role(sk), sk);
  250. /* fall through */
  251. case TFRC_SSTATE_TERM:
  252. goto out;
  253. }
  254. restart_timer:
  255. sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
  256. jiffies + usecs_to_jiffies(t_nfb));
  257. out:
  258. bh_unlock_sock(sk);
  259. sock_put(sk);
  260. }
  261. /*
  262. * returns
  263. * > 0: delay (in msecs) that should pass before actually sending
  264. * = 0: can send immediately
  265. * < 0: error condition; do not send packet
  266. */
  267. static int ccid3_hc_tx_send_packet(struct sock *sk, struct sk_buff *skb)
  268. {
  269. struct dccp_sock *dp = dccp_sk(sk);
  270. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  271. ktime_t now = ktime_get_real();
  272. s64 delay;
  273. /*
  274. * This function is called only for Data and DataAck packets. Sending
  275. * zero-sized Data(Ack)s is theoretically possible, but for congestion
  276. * control this case is pathological - ignore it.
  277. */
  278. if (unlikely(skb->len == 0))
  279. return -EBADMSG;
  280. switch (hctx->ccid3hctx_state) {
  281. case TFRC_SSTATE_NO_SENT:
  282. sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
  283. (jiffies +
  284. usecs_to_jiffies(TFRC_INITIAL_TIMEOUT)));
  285. hctx->ccid3hctx_last_win_count = 0;
  286. hctx->ccid3hctx_t_last_win_count = now;
  287. /* Set t_0 for initial packet */
  288. hctx->ccid3hctx_t_nom = now;
  289. hctx->ccid3hctx_s = skb->len;
  290. /*
  291. * Use initial RTT sample when available: recommended by erratum
  292. * to RFC 4342. This implements the initialisation procedure of
  293. * draft rfc3448bis, section 4.2. Remember, X is scaled by 2^6.
  294. */
  295. if (dp->dccps_syn_rtt) {
  296. ccid3_pr_debug("SYN RTT = %uus\n", dp->dccps_syn_rtt);
  297. hctx->ccid3hctx_rtt = dp->dccps_syn_rtt;
  298. hctx->ccid3hctx_x = rfc3390_initial_rate(sk);
  299. hctx->ccid3hctx_t_ld = now;
  300. } else {
  301. /* Sender does not have RTT sample: X_pps = 1 pkt/sec */
  302. hctx->ccid3hctx_x = hctx->ccid3hctx_s;
  303. hctx->ccid3hctx_x <<= 6;
  304. }
  305. ccid3_update_send_interval(hctx);
  306. ccid3_hc_tx_set_state(sk, TFRC_SSTATE_NO_FBACK);
  307. break;
  308. case TFRC_SSTATE_NO_FBACK:
  309. case TFRC_SSTATE_FBACK:
  310. delay = ktime_us_delta(hctx->ccid3hctx_t_nom, now);
  311. ccid3_pr_debug("delay=%ld\n", (long)delay);
  312. /*
  313. * Scheduling of packet transmissions [RFC 3448, 4.6]
  314. *
  315. * if (t_now > t_nom - delta)
  316. * // send the packet now
  317. * else
  318. * // send the packet in (t_nom - t_now) milliseconds.
  319. */
  320. if (delay - (s64)hctx->ccid3hctx_delta >= 1000)
  321. return (u32)delay / 1000L;
  322. ccid3_hc_tx_update_win_count(hctx, now);
  323. break;
  324. case TFRC_SSTATE_TERM:
  325. DCCP_BUG("%s(%p) - Illegal state TERM", dccp_role(sk), sk);
  326. return -EINVAL;
  327. }
  328. /* prepare to send now (add options etc.) */
  329. dp->dccps_hc_tx_insert_options = 1;
  330. DCCP_SKB_CB(skb)->dccpd_ccval = hctx->ccid3hctx_last_win_count;
  331. /* set the nominal send time for the next following packet */
  332. hctx->ccid3hctx_t_nom = ktime_add_us(hctx->ccid3hctx_t_nom,
  333. hctx->ccid3hctx_t_ipi);
  334. return 0;
  335. }
  336. static void ccid3_hc_tx_packet_sent(struct sock *sk, int more,
  337. unsigned int len)
  338. {
  339. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  340. ccid3_hc_tx_update_s(hctx, len);
  341. if (tfrc_tx_hist_add(&hctx->ccid3hctx_hist, dccp_sk(sk)->dccps_gss))
  342. DCCP_CRIT("packet history - out of memory!");
  343. }
  344. static void ccid3_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
  345. {
  346. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  347. struct ccid3_options_received *opt_recv;
  348. ktime_t now;
  349. unsigned long t_nfb;
  350. u32 pinv, r_sample;
  351. /* we are only interested in ACKs */
  352. if (!(DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK ||
  353. DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_DATAACK))
  354. return;
  355. opt_recv = &hctx->ccid3hctx_options_received;
  356. switch (hctx->ccid3hctx_state) {
  357. case TFRC_SSTATE_NO_FBACK:
  358. case TFRC_SSTATE_FBACK:
  359. now = ktime_get_real();
  360. /* estimate RTT from history if ACK number is valid */
  361. r_sample = tfrc_tx_hist_rtt(hctx->ccid3hctx_hist,
  362. DCCP_SKB_CB(skb)->dccpd_ack_seq, now);
  363. if (r_sample == 0) {
  364. DCCP_WARN("%s(%p): %s with bogus ACK-%llu\n", dccp_role(sk), sk,
  365. dccp_packet_name(DCCP_SKB_CB(skb)->dccpd_type),
  366. (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq);
  367. return;
  368. }
  369. /* Update receive rate in units of 64 * bytes/second */
  370. hctx->ccid3hctx_x_recv = opt_recv->ccid3or_receive_rate;
  371. hctx->ccid3hctx_x_recv <<= 6;
  372. /* Update loss event rate */
  373. pinv = opt_recv->ccid3or_loss_event_rate;
  374. if (pinv == ~0U || pinv == 0) /* see RFC 4342, 8.5 */
  375. hctx->ccid3hctx_p = 0;
  376. else /* can not exceed 100% */
  377. hctx->ccid3hctx_p = 1000000 / pinv;
  378. /*
  379. * Validate new RTT sample and update moving average
  380. */
  381. r_sample = dccp_sample_rtt(sk, r_sample);
  382. hctx->ccid3hctx_rtt = tfrc_ewma(hctx->ccid3hctx_rtt, r_sample, 9);
  383. if (hctx->ccid3hctx_state == TFRC_SSTATE_NO_FBACK) {
  384. /*
  385. * Larger Initial Windows [RFC 4342, sec. 5]
  386. */
  387. hctx->ccid3hctx_x = rfc3390_initial_rate(sk);
  388. hctx->ccid3hctx_t_ld = now;
  389. ccid3_update_send_interval(hctx);
  390. ccid3_pr_debug("%s(%p), s=%u, MSS=%u, "
  391. "R_sample=%uus, X=%u\n", dccp_role(sk),
  392. sk, hctx->ccid3hctx_s,
  393. dccp_sk(sk)->dccps_mss_cache, r_sample,
  394. (unsigned)(hctx->ccid3hctx_x >> 6));
  395. ccid3_hc_tx_set_state(sk, TFRC_SSTATE_FBACK);
  396. } else {
  397. /* Update sending rate (step 4 of [RFC 3448, 4.3]) */
  398. if (hctx->ccid3hctx_p > 0)
  399. hctx->ccid3hctx_x_calc =
  400. tfrc_calc_x(hctx->ccid3hctx_s,
  401. hctx->ccid3hctx_rtt,
  402. hctx->ccid3hctx_p);
  403. ccid3_hc_tx_update_x(sk, &now);
  404. ccid3_pr_debug("%s(%p), RTT=%uus (sample=%uus), s=%u, "
  405. "p=%u, X_calc=%u, X_recv=%u, X=%u\n",
  406. dccp_role(sk),
  407. sk, hctx->ccid3hctx_rtt, r_sample,
  408. hctx->ccid3hctx_s, hctx->ccid3hctx_p,
  409. hctx->ccid3hctx_x_calc,
  410. (unsigned)(hctx->ccid3hctx_x_recv >> 6),
  411. (unsigned)(hctx->ccid3hctx_x >> 6));
  412. }
  413. /* unschedule no feedback timer */
  414. sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
  415. /*
  416. * As we have calculated new ipi, delta, t_nom it is possible
  417. * that we now can send a packet, so wake up dccp_wait_for_ccid
  418. */
  419. sk->sk_write_space(sk);
  420. /*
  421. * Update timeout interval for the nofeedback timer.
  422. * We use a configuration option to increase the lower bound.
  423. * This can help avoid triggering the nofeedback timer too
  424. * often ('spinning') on LANs with small RTTs.
  425. */
  426. hctx->ccid3hctx_t_rto = max_t(u32, 4 * hctx->ccid3hctx_rtt,
  427. CONFIG_IP_DCCP_CCID3_RTO *
  428. (USEC_PER_SEC/1000));
  429. /*
  430. * Schedule no feedback timer to expire in
  431. * max(t_RTO, 2 * s/X) = max(t_RTO, 2 * t_ipi)
  432. */
  433. t_nfb = max(hctx->ccid3hctx_t_rto, 2 * hctx->ccid3hctx_t_ipi);
  434. ccid3_pr_debug("%s(%p), Scheduled no feedback timer to "
  435. "expire in %lu jiffies (%luus)\n",
  436. dccp_role(sk),
  437. sk, usecs_to_jiffies(t_nfb), t_nfb);
  438. sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
  439. jiffies + usecs_to_jiffies(t_nfb));
  440. break;
  441. case TFRC_SSTATE_NO_SENT: /* fall through */
  442. case TFRC_SSTATE_TERM: /* ignore feedback when closing */
  443. break;
  444. }
  445. }
  446. static int ccid3_hc_tx_parse_options(struct sock *sk, unsigned char option,
  447. unsigned char len, u16 idx,
  448. unsigned char *value)
  449. {
  450. int rc = 0;
  451. const struct dccp_sock *dp = dccp_sk(sk);
  452. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  453. struct ccid3_options_received *opt_recv;
  454. __be32 opt_val;
  455. opt_recv = &hctx->ccid3hctx_options_received;
  456. if (opt_recv->ccid3or_seqno != dp->dccps_gsr) {
  457. opt_recv->ccid3or_seqno = dp->dccps_gsr;
  458. opt_recv->ccid3or_loss_event_rate = ~0;
  459. opt_recv->ccid3or_loss_intervals_idx = 0;
  460. opt_recv->ccid3or_loss_intervals_len = 0;
  461. opt_recv->ccid3or_receive_rate = 0;
  462. }
  463. switch (option) {
  464. case TFRC_OPT_LOSS_EVENT_RATE:
  465. if (unlikely(len != 4)) {
  466. DCCP_WARN("%s(%p), invalid len %d "
  467. "for TFRC_OPT_LOSS_EVENT_RATE\n",
  468. dccp_role(sk), sk, len);
  469. rc = -EINVAL;
  470. } else {
  471. opt_val = get_unaligned((__be32 *)value);
  472. opt_recv->ccid3or_loss_event_rate = ntohl(opt_val);
  473. ccid3_pr_debug("%s(%p), LOSS_EVENT_RATE=%u\n",
  474. dccp_role(sk), sk,
  475. opt_recv->ccid3or_loss_event_rate);
  476. }
  477. break;
  478. case TFRC_OPT_LOSS_INTERVALS:
  479. opt_recv->ccid3or_loss_intervals_idx = idx;
  480. opt_recv->ccid3or_loss_intervals_len = len;
  481. ccid3_pr_debug("%s(%p), LOSS_INTERVALS=(%u, %u)\n",
  482. dccp_role(sk), sk,
  483. opt_recv->ccid3or_loss_intervals_idx,
  484. opt_recv->ccid3or_loss_intervals_len);
  485. break;
  486. case TFRC_OPT_RECEIVE_RATE:
  487. if (unlikely(len != 4)) {
  488. DCCP_WARN("%s(%p), invalid len %d "
  489. "for TFRC_OPT_RECEIVE_RATE\n",
  490. dccp_role(sk), sk, len);
  491. rc = -EINVAL;
  492. } else {
  493. opt_val = get_unaligned((__be32 *)value);
  494. opt_recv->ccid3or_receive_rate = ntohl(opt_val);
  495. ccid3_pr_debug("%s(%p), RECEIVE_RATE=%u\n",
  496. dccp_role(sk), sk,
  497. opt_recv->ccid3or_receive_rate);
  498. }
  499. break;
  500. }
  501. return rc;
  502. }
  503. static int ccid3_hc_tx_init(struct ccid *ccid, struct sock *sk)
  504. {
  505. struct ccid3_hc_tx_sock *hctx = ccid_priv(ccid);
  506. hctx->ccid3hctx_state = TFRC_SSTATE_NO_SENT;
  507. hctx->ccid3hctx_hist = NULL;
  508. setup_timer(&hctx->ccid3hctx_no_feedback_timer,
  509. ccid3_hc_tx_no_feedback_timer, (unsigned long)sk);
  510. return 0;
  511. }
  512. static void ccid3_hc_tx_exit(struct sock *sk)
  513. {
  514. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  515. ccid3_hc_tx_set_state(sk, TFRC_SSTATE_TERM);
  516. sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
  517. tfrc_tx_hist_purge(&hctx->ccid3hctx_hist);
  518. }
  519. static void ccid3_hc_tx_get_info(struct sock *sk, struct tcp_info *info)
  520. {
  521. struct ccid3_hc_tx_sock *hctx;
  522. /* Listen socks doesn't have a private CCID block */
  523. if (sk->sk_state == DCCP_LISTEN)
  524. return;
  525. hctx = ccid3_hc_tx_sk(sk);
  526. info->tcpi_rto = hctx->ccid3hctx_t_rto;
  527. info->tcpi_rtt = hctx->ccid3hctx_rtt;
  528. }
  529. static int ccid3_hc_tx_getsockopt(struct sock *sk, const int optname, int len,
  530. u32 __user *optval, int __user *optlen)
  531. {
  532. const struct ccid3_hc_tx_sock *hctx;
  533. const void *val;
  534. /* Listen socks doesn't have a private CCID block */
  535. if (sk->sk_state == DCCP_LISTEN)
  536. return -EINVAL;
  537. hctx = ccid3_hc_tx_sk(sk);
  538. switch (optname) {
  539. case DCCP_SOCKOPT_CCID_TX_INFO:
  540. if (len < sizeof(hctx->ccid3hctx_tfrc))
  541. return -EINVAL;
  542. len = sizeof(hctx->ccid3hctx_tfrc);
  543. val = &hctx->ccid3hctx_tfrc;
  544. break;
  545. default:
  546. return -ENOPROTOOPT;
  547. }
  548. if (put_user(len, optlen) || copy_to_user(optval, val, len))
  549. return -EFAULT;
  550. return 0;
  551. }
  552. /*
  553. * Receiver Half-Connection Routines
  554. */
  555. /* CCID3 feedback types */
  556. enum ccid3_fback_type {
  557. CCID3_FBACK_NONE = 0,
  558. CCID3_FBACK_INITIAL,
  559. CCID3_FBACK_PERIODIC,
  560. CCID3_FBACK_PARAM_CHANGE
  561. };
  562. #ifdef CONFIG_IP_DCCP_CCID3_DEBUG
  563. static const char *ccid3_rx_state_name(enum ccid3_hc_rx_states state)
  564. {
  565. static char *ccid3_rx_state_names[] = {
  566. [TFRC_RSTATE_NO_DATA] = "NO_DATA",
  567. [TFRC_RSTATE_DATA] = "DATA",
  568. [TFRC_RSTATE_TERM] = "TERM",
  569. };
  570. return ccid3_rx_state_names[state];
  571. }
  572. #endif
  573. static void ccid3_hc_rx_set_state(struct sock *sk,
  574. enum ccid3_hc_rx_states state)
  575. {
  576. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  577. enum ccid3_hc_rx_states oldstate = hcrx->ccid3hcrx_state;
  578. ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
  579. dccp_role(sk), sk, ccid3_rx_state_name(oldstate),
  580. ccid3_rx_state_name(state));
  581. WARN_ON(state == oldstate);
  582. hcrx->ccid3hcrx_state = state;
  583. }
  584. static void ccid3_hc_rx_send_feedback(struct sock *sk,
  585. const struct sk_buff *skb,
  586. enum ccid3_fback_type fbtype)
  587. {
  588. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  589. struct dccp_sock *dp = dccp_sk(sk);
  590. ktime_t now;
  591. s64 delta = 0;
  592. if (unlikely(hcrx->ccid3hcrx_state == TFRC_RSTATE_TERM))
  593. return;
  594. now = ktime_get_real();
  595. switch (fbtype) {
  596. case CCID3_FBACK_INITIAL:
  597. hcrx->ccid3hcrx_x_recv = 0;
  598. hcrx->ccid3hcrx_pinv = ~0U; /* see RFC 4342, 8.5 */
  599. break;
  600. case CCID3_FBACK_PARAM_CHANGE:
  601. /*
  602. * When parameters change (new loss or p > p_prev), we do not
  603. * have a reliable estimate for R_m of [RFC 3448, 6.2] and so
  604. * need to reuse the previous value of X_recv. However, when
  605. * X_recv was 0 (due to early loss), this would kill X down to
  606. * s/t_mbi (i.e. one packet in 64 seconds).
  607. * To avoid such drastic reduction, we approximate X_recv as
  608. * the number of bytes since last feedback.
  609. * This is a safe fallback, since X is bounded above by X_calc.
  610. */
  611. if (hcrx->ccid3hcrx_x_recv > 0)
  612. break;
  613. /* fall through */
  614. case CCID3_FBACK_PERIODIC:
  615. delta = ktime_us_delta(now, hcrx->ccid3hcrx_tstamp_last_feedback);
  616. if (delta <= 0)
  617. DCCP_BUG("delta (%ld) <= 0", (long)delta);
  618. else
  619. hcrx->ccid3hcrx_x_recv =
  620. scaled_div32(hcrx->ccid3hcrx_bytes_recv, delta);
  621. break;
  622. default:
  623. return;
  624. }
  625. ccid3_pr_debug("Interval %ldusec, X_recv=%u, 1/p=%u\n", (long)delta,
  626. hcrx->ccid3hcrx_x_recv, hcrx->ccid3hcrx_pinv);
  627. hcrx->ccid3hcrx_tstamp_last_feedback = now;
  628. hcrx->ccid3hcrx_last_counter = dccp_hdr(skb)->dccph_ccval;
  629. hcrx->ccid3hcrx_bytes_recv = 0;
  630. dp->dccps_hc_rx_insert_options = 1;
  631. dccp_send_ack(sk);
  632. }
  633. static int ccid3_hc_rx_insert_options(struct sock *sk, struct sk_buff *skb)
  634. {
  635. const struct ccid3_hc_rx_sock *hcrx;
  636. __be32 x_recv, pinv;
  637. if (!(sk->sk_state == DCCP_OPEN || sk->sk_state == DCCP_PARTOPEN))
  638. return 0;
  639. hcrx = ccid3_hc_rx_sk(sk);
  640. if (dccp_packet_without_ack(skb))
  641. return 0;
  642. x_recv = htonl(hcrx->ccid3hcrx_x_recv);
  643. pinv = htonl(hcrx->ccid3hcrx_pinv);
  644. if (dccp_insert_option(sk, skb, TFRC_OPT_LOSS_EVENT_RATE,
  645. &pinv, sizeof(pinv)) ||
  646. dccp_insert_option(sk, skb, TFRC_OPT_RECEIVE_RATE,
  647. &x_recv, sizeof(x_recv)))
  648. return -1;
  649. return 0;
  650. }
  651. static void ccid3_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
  652. {
  653. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  654. enum ccid3_fback_type do_feedback = CCID3_FBACK_NONE;
  655. const u32 ndp = dccp_sk(sk)->dccps_options_received.dccpor_ndp;
  656. const bool is_data_packet = dccp_data_packet(skb);
  657. if (unlikely(hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)) {
  658. if (is_data_packet) {
  659. const u32 payload = skb->len - dccp_hdr(skb)->dccph_doff * 4;
  660. do_feedback = CCID3_FBACK_INITIAL;
  661. ccid3_hc_rx_set_state(sk, TFRC_RSTATE_DATA);
  662. hcrx->ccid3hcrx_s = payload;
  663. /*
  664. * Not necessary to update ccid3hcrx_bytes_recv here,
  665. * since X_recv = 0 for the first feedback packet (cf.
  666. * RFC 3448, 6.3) -- gerrit
  667. */
  668. }
  669. goto update_records;
  670. }
  671. if (tfrc_rx_hist_duplicate(&hcrx->ccid3hcrx_hist, skb))
  672. return; /* done receiving */
  673. if (is_data_packet) {
  674. const u32 payload = skb->len - dccp_hdr(skb)->dccph_doff * 4;
  675. /*
  676. * Update moving-average of s and the sum of received payload bytes
  677. */
  678. hcrx->ccid3hcrx_s = tfrc_ewma(hcrx->ccid3hcrx_s, payload, 9);
  679. hcrx->ccid3hcrx_bytes_recv += payload;
  680. }
  681. /*
  682. * Handle pending losses and otherwise check for new loss
  683. */
  684. if (tfrc_rx_hist_new_loss_indicated(&hcrx->ccid3hcrx_hist, skb, ndp))
  685. goto update_records;
  686. /*
  687. * Handle data packets: RTT sampling and monitoring p
  688. */
  689. if (unlikely(!is_data_packet))
  690. goto update_records;
  691. if (list_empty(&hcrx->ccid3hcrx_li_hist)) { /* no loss so far: p = 0 */
  692. const u32 sample = tfrc_rx_hist_sample_rtt(&hcrx->ccid3hcrx_hist, skb);
  693. /*
  694. * Empty loss history: no loss so far, hence p stays 0.
  695. * Sample RTT values, since an RTT estimate is required for the
  696. * computation of p when the first loss occurs; RFC 3448, 6.3.1.
  697. */
  698. if (sample != 0)
  699. hcrx->ccid3hcrx_rtt = tfrc_ewma(hcrx->ccid3hcrx_rtt, sample, 9);
  700. }
  701. /*
  702. * Check if the periodic once-per-RTT feedback is due; RFC 4342, 10.3
  703. */
  704. if (SUB16(dccp_hdr(skb)->dccph_ccval, hcrx->ccid3hcrx_last_counter) > 3)
  705. do_feedback = CCID3_FBACK_PERIODIC;
  706. update_records:
  707. tfrc_rx_hist_add_packet(&hcrx->ccid3hcrx_hist, skb, ndp);
  708. if (do_feedback)
  709. ccid3_hc_rx_send_feedback(sk, skb, do_feedback);
  710. }
  711. static int ccid3_hc_rx_init(struct ccid *ccid, struct sock *sk)
  712. {
  713. struct ccid3_hc_rx_sock *hcrx = ccid_priv(ccid);
  714. ccid3_pr_debug("entry\n");
  715. hcrx->ccid3hcrx_state = TFRC_RSTATE_NO_DATA;
  716. INIT_LIST_HEAD(&hcrx->ccid3hcrx_li_hist);
  717. return tfrc_rx_hist_alloc(&hcrx->ccid3hcrx_hist);
  718. }
  719. static void ccid3_hc_rx_exit(struct sock *sk)
  720. {
  721. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  722. ccid3_hc_rx_set_state(sk, TFRC_RSTATE_TERM);
  723. /* Empty packet history */
  724. tfrc_rx_hist_purge(&hcrx->ccid3hcrx_hist);
  725. /* Empty loss interval history */
  726. dccp_li_hist_purge(&hcrx->ccid3hcrx_li_hist);
  727. }
  728. static void ccid3_hc_rx_get_info(struct sock *sk, struct tcp_info *info)
  729. {
  730. const struct ccid3_hc_rx_sock *hcrx;
  731. /* Listen socks doesn't have a private CCID block */
  732. if (sk->sk_state == DCCP_LISTEN)
  733. return;
  734. hcrx = ccid3_hc_rx_sk(sk);
  735. info->tcpi_ca_state = hcrx->ccid3hcrx_state;
  736. info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
  737. info->tcpi_rcv_rtt = hcrx->ccid3hcrx_rtt;
  738. }
  739. static int ccid3_hc_rx_getsockopt(struct sock *sk, const int optname, int len,
  740. u32 __user *optval, int __user *optlen)
  741. {
  742. const struct ccid3_hc_rx_sock *hcrx;
  743. const void *val;
  744. /* Listen socks doesn't have a private CCID block */
  745. if (sk->sk_state == DCCP_LISTEN)
  746. return -EINVAL;
  747. hcrx = ccid3_hc_rx_sk(sk);
  748. switch (optname) {
  749. case DCCP_SOCKOPT_CCID_RX_INFO:
  750. if (len < sizeof(hcrx->ccid3hcrx_tfrc))
  751. return -EINVAL;
  752. len = sizeof(hcrx->ccid3hcrx_tfrc);
  753. val = &hcrx->ccid3hcrx_tfrc;
  754. break;
  755. default:
  756. return -ENOPROTOOPT;
  757. }
  758. if (put_user(len, optlen) || copy_to_user(optval, val, len))
  759. return -EFAULT;
  760. return 0;
  761. }
  762. static struct ccid_operations ccid3 = {
  763. .ccid_id = DCCPC_CCID3,
  764. .ccid_name = "ccid3",
  765. .ccid_owner = THIS_MODULE,
  766. .ccid_hc_tx_obj_size = sizeof(struct ccid3_hc_tx_sock),
  767. .ccid_hc_tx_init = ccid3_hc_tx_init,
  768. .ccid_hc_tx_exit = ccid3_hc_tx_exit,
  769. .ccid_hc_tx_send_packet = ccid3_hc_tx_send_packet,
  770. .ccid_hc_tx_packet_sent = ccid3_hc_tx_packet_sent,
  771. .ccid_hc_tx_packet_recv = ccid3_hc_tx_packet_recv,
  772. .ccid_hc_tx_parse_options = ccid3_hc_tx_parse_options,
  773. .ccid_hc_rx_obj_size = sizeof(struct ccid3_hc_rx_sock),
  774. .ccid_hc_rx_init = ccid3_hc_rx_init,
  775. .ccid_hc_rx_exit = ccid3_hc_rx_exit,
  776. .ccid_hc_rx_insert_options = ccid3_hc_rx_insert_options,
  777. .ccid_hc_rx_packet_recv = ccid3_hc_rx_packet_recv,
  778. .ccid_hc_rx_get_info = ccid3_hc_rx_get_info,
  779. .ccid_hc_tx_get_info = ccid3_hc_tx_get_info,
  780. .ccid_hc_rx_getsockopt = ccid3_hc_rx_getsockopt,
  781. .ccid_hc_tx_getsockopt = ccid3_hc_tx_getsockopt,
  782. };
  783. #ifdef CONFIG_IP_DCCP_CCID3_DEBUG
  784. module_param(ccid3_debug, bool, 0444);
  785. MODULE_PARM_DESC(ccid3_debug, "Enable debug messages");
  786. #endif
  787. static __init int ccid3_module_init(void)
  788. {
  789. return ccid_register(&ccid3);
  790. }
  791. module_init(ccid3_module_init);
  792. static __exit void ccid3_module_exit(void)
  793. {
  794. ccid_unregister(&ccid3);
  795. }
  796. module_exit(ccid3_module_exit);
  797. MODULE_AUTHOR("Ian McDonald <ian.mcdonald@jandi.co.nz>, "
  798. "Arnaldo Carvalho de Melo <acme@ghostprotocols.net>");
  799. MODULE_DESCRIPTION("DCCP TFRC CCID3 CCID");
  800. MODULE_LICENSE("GPL");
  801. MODULE_ALIAS("net-dccp-ccid-3");