ccid3.c 36 KB

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