ccid3.c 36 KB

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