ccid3.c 36 KB

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