ccid3.c 37 KB

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