ccid3.c 35 KB

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