ccid3.c 37 KB

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