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