ccid3.c 37 KB

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