ccid3.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  1. /*
  2. * net/dccp/ccids/ccid3.c
  3. *
  4. * Copyright (c) 2005 The University of Waikato, Hamilton, New Zealand.
  5. * Copyright (c) 2005-6 Ian McDonald <ian.mcdonald@jandi.co.nz>
  6. *
  7. * An implementation of the DCCP protocol
  8. *
  9. * This code has been developed by the University of Waikato WAND
  10. * research group. For further information please see http://www.wand.net.nz/
  11. *
  12. * This code also uses code from Lulea University, rereleased as GPL by its
  13. * authors:
  14. * Copyright (c) 2003 Nils-Erik Mattsson, Joacim Haggmark, Magnus Erixzon
  15. *
  16. * Changes to meet Linux coding standards, to make it meet latest ccid3 draft
  17. * and to make it work as a loadable module in the DCCP stack written by
  18. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>.
  19. *
  20. * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  21. *
  22. * This program is free software; you can redistribute it and/or modify
  23. * it under the terms of the GNU General Public License as published by
  24. * the Free Software Foundation; either version 2 of the License, or
  25. * (at your option) any later version.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU General Public License
  33. * along with this program; if not, write to the Free Software
  34. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  35. */
  36. #include "../ccid.h"
  37. #include "../dccp.h"
  38. #include "lib/packet_history.h"
  39. #include "lib/loss_interval.h"
  40. #include "lib/tfrc.h"
  41. #include "ccid3.h"
  42. #ifdef CONFIG_IP_DCCP_CCID3_DEBUG
  43. static int ccid3_debug;
  44. #define ccid3_pr_debug(format, a...) DCCP_PR_DEBUG(ccid3_debug, format, ##a)
  45. #else
  46. #define ccid3_pr_debug(format, a...)
  47. #endif
  48. static struct dccp_tx_hist *ccid3_tx_hist;
  49. static struct dccp_rx_hist *ccid3_rx_hist;
  50. static struct dccp_li_hist *ccid3_li_hist;
  51. #ifdef CONFIG_IP_DCCP_CCID3_DEBUG
  52. static const char *ccid3_tx_state_name(enum ccid3_hc_tx_states state)
  53. {
  54. static char *ccid3_state_names[] = {
  55. [TFRC_SSTATE_NO_SENT] = "NO_SENT",
  56. [TFRC_SSTATE_NO_FBACK] = "NO_FBACK",
  57. [TFRC_SSTATE_FBACK] = "FBACK",
  58. [TFRC_SSTATE_TERM] = "TERM",
  59. };
  60. return ccid3_state_names[state];
  61. }
  62. #endif
  63. static void ccid3_hc_tx_set_state(struct sock *sk,
  64. enum ccid3_hc_tx_states state)
  65. {
  66. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  67. enum ccid3_hc_tx_states oldstate = hctx->ccid3hctx_state;
  68. ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
  69. dccp_role(sk), sk, ccid3_tx_state_name(oldstate),
  70. ccid3_tx_state_name(state));
  71. WARN_ON(state == oldstate);
  72. hctx->ccid3hctx_state = state;
  73. }
  74. /*
  75. * Recalculate scheduled nominal send time t_nom, inter-packet interval
  76. * t_ipi, and delta value. Should be called after each change to X.
  77. */
  78. static inline void ccid3_update_send_time(struct ccid3_hc_tx_sock *hctx)
  79. {
  80. timeval_sub_usecs(&hctx->ccid3hctx_t_nom, hctx->ccid3hctx_t_ipi);
  81. /* Calculate new t_ipi = s / X_inst (X_inst is in 64 * bytes/second) */
  82. hctx->ccid3hctx_t_ipi = scaled_div(hctx->ccid3hctx_s,
  83. hctx->ccid3hctx_x >> 6);
  84. /* Update nominal send time with regard to the new t_ipi */
  85. timeval_add_usecs(&hctx->ccid3hctx_t_nom, hctx->ccid3hctx_t_ipi);
  86. /* Calculate new delta by delta = min(t_ipi / 2, t_gran / 2) */
  87. hctx->ccid3hctx_delta = min_t(u32, hctx->ccid3hctx_t_ipi / 2,
  88. TFRC_OPSYS_HALF_TIME_GRAN);
  89. }
  90. /*
  91. * Update X by
  92. * If (p > 0)
  93. * X_calc = calcX(s, R, p);
  94. * X = max(min(X_calc, 2 * X_recv), s / t_mbi);
  95. * Else
  96. * If (now - tld >= R)
  97. * X = max(min(2 * X, 2 * X_recv), s / R);
  98. * tld = now;
  99. *
  100. * Note: X and X_recv are both stored in units of 64 * bytes/second, to support
  101. * fine-grained resolution of sending rates. This requires scaling by 2^6
  102. * throughout the code. Only X_calc is unscaled (in bytes/second).
  103. *
  104. * If X has changed, we also update the scheduled send time t_now,
  105. * the inter-packet interval t_ipi, and the delta value.
  106. */
  107. static void ccid3_hc_tx_update_x(struct sock *sk, struct timeval *now)
  108. {
  109. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  110. const __u64 old_x = hctx->ccid3hctx_x;
  111. if (hctx->ccid3hctx_p > 0) {
  112. hctx->ccid3hctx_x = min(((__u64)hctx->ccid3hctx_x_calc) << 6,
  113. hctx->ccid3hctx_x_recv * 2);
  114. hctx->ccid3hctx_x = max(hctx->ccid3hctx_x,
  115. (((__u64)hctx->ccid3hctx_s) << 6) /
  116. TFRC_T_MBI);
  117. } else if (timeval_delta(now, &hctx->ccid3hctx_t_ld) -
  118. (suseconds_t)hctx->ccid3hctx_rtt >= 0) {
  119. hctx->ccid3hctx_x =
  120. max(2 * min(hctx->ccid3hctx_x, hctx->ccid3hctx_x_recv),
  121. scaled_div(((__u64)hctx->ccid3hctx_s) << 6,
  122. hctx->ccid3hctx_rtt));
  123. hctx->ccid3hctx_t_ld = *now;
  124. }
  125. if (hctx->ccid3hctx_x != old_x)
  126. ccid3_update_send_time(hctx);
  127. }
  128. /*
  129. * Track the mean packet size `s' (cf. RFC 4342, 5.3 and RFC 3448, 4.1)
  130. * @len: DCCP packet payload size in bytes
  131. */
  132. static inline void ccid3_hc_tx_update_s(struct ccid3_hc_tx_sock *hctx, int len)
  133. {
  134. if (unlikely(len == 0))
  135. ccid3_pr_debug("Packet payload length is 0 - not updating\n");
  136. else
  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. * We deviate in that we use `s' instead of `MSS'.
  407. */
  408. __u64 w_init = min(4 * hctx->ccid3hctx_s,
  409. max(2 * hctx->ccid3hctx_s, 4380));
  410. hctx->ccid3hctx_rtt = r_sample;
  411. hctx->ccid3hctx_x = scaled_div(w_init << 6, r_sample);
  412. hctx->ccid3hctx_t_ld = now;
  413. ccid3_update_send_time(hctx);
  414. ccid3_pr_debug("%s(%p), s=%u, w_init=%llu, "
  415. "R_sample=%dus, X=%u\n", dccp_role(sk),
  416. sk, hctx->ccid3hctx_s, w_init,
  417. (int)r_sample,
  418. (unsigned)(hctx->ccid3hctx_x >> 6));
  419. ccid3_hc_tx_set_state(sk, TFRC_SSTATE_FBACK);
  420. } else {
  421. hctx->ccid3hctx_rtt = (9 * hctx->ccid3hctx_rtt +
  422. (u32)r_sample) / 10;
  423. /* Update sending rate (step 4 of [RFC 3448, 4.3]) */
  424. if (hctx->ccid3hctx_p > 0)
  425. hctx->ccid3hctx_x_calc =
  426. tfrc_calc_x(hctx->ccid3hctx_s,
  427. hctx->ccid3hctx_rtt,
  428. hctx->ccid3hctx_p);
  429. ccid3_hc_tx_update_x(sk, &now);
  430. ccid3_pr_debug("%s(%p), RTT=%uus (sample=%dus), s=%u, "
  431. "p=%u, X_calc=%u, X_recv=%u, X=%u\n",
  432. dccp_role(sk),
  433. sk, hctx->ccid3hctx_rtt, (int)r_sample,
  434. hctx->ccid3hctx_s, hctx->ccid3hctx_p,
  435. hctx->ccid3hctx_x_calc,
  436. (unsigned)(hctx->ccid3hctx_x_recv >> 6),
  437. (unsigned)(hctx->ccid3hctx_x >> 6));
  438. }
  439. /* unschedule no feedback timer */
  440. sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
  441. /* remove all packets older than the one acked from history */
  442. dccp_tx_hist_purge_older(ccid3_tx_hist,
  443. &hctx->ccid3hctx_hist, packet);
  444. /*
  445. * As we have calculated new ipi, delta, t_nom it is possible
  446. * that we now can send a packet, so wake up dccp_wait_for_ccid
  447. */
  448. sk->sk_write_space(sk);
  449. /*
  450. * Update timeout interval for the nofeedback timer.
  451. * We use a configuration option to increase the lower bound.
  452. * This can help avoid triggering the nofeedback timer too
  453. * often ('spinning') on LANs with small RTTs.
  454. */
  455. hctx->ccid3hctx_t_rto = max_t(u32, 4 * hctx->ccid3hctx_rtt,
  456. CONFIG_IP_DCCP_CCID3_RTO *
  457. (USEC_PER_SEC/1000));
  458. /*
  459. * Schedule no feedback timer to expire in
  460. * max(t_RTO, 2 * s/X) = max(t_RTO, 2 * t_ipi)
  461. */
  462. t_nfb = max(hctx->ccid3hctx_t_rto, 2 * hctx->ccid3hctx_t_ipi);
  463. ccid3_pr_debug("%s(%p), Scheduled no feedback timer to "
  464. "expire in %lu jiffies (%luus)\n",
  465. dccp_role(sk),
  466. sk, usecs_to_jiffies(t_nfb), t_nfb);
  467. sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
  468. jiffies + usecs_to_jiffies(t_nfb));
  469. /* set idle flag */
  470. hctx->ccid3hctx_idle = 1;
  471. break;
  472. case TFRC_SSTATE_NO_SENT:
  473. /*
  474. * XXX when implementing bidirectional rx/tx check this again
  475. */
  476. DCCP_WARN("Illegal ACK received - no packet sent\n");
  477. /* fall through */
  478. case TFRC_SSTATE_TERM: /* ignore feedback when closing */
  479. break;
  480. }
  481. }
  482. static int ccid3_hc_tx_parse_options(struct sock *sk, unsigned char option,
  483. unsigned char len, u16 idx,
  484. unsigned char *value)
  485. {
  486. int rc = 0;
  487. const struct dccp_sock *dp = dccp_sk(sk);
  488. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  489. struct ccid3_options_received *opt_recv;
  490. BUG_ON(hctx == NULL);
  491. opt_recv = &hctx->ccid3hctx_options_received;
  492. if (opt_recv->ccid3or_seqno != dp->dccps_gsr) {
  493. opt_recv->ccid3or_seqno = dp->dccps_gsr;
  494. opt_recv->ccid3or_loss_event_rate = ~0;
  495. opt_recv->ccid3or_loss_intervals_idx = 0;
  496. opt_recv->ccid3or_loss_intervals_len = 0;
  497. opt_recv->ccid3or_receive_rate = 0;
  498. }
  499. switch (option) {
  500. case TFRC_OPT_LOSS_EVENT_RATE:
  501. if (unlikely(len != 4)) {
  502. DCCP_WARN("%s(%p), invalid len %d "
  503. "for TFRC_OPT_LOSS_EVENT_RATE\n",
  504. dccp_role(sk), sk, len);
  505. rc = -EINVAL;
  506. } else {
  507. opt_recv->ccid3or_loss_event_rate =
  508. ntohl(*(__be32 *)value);
  509. ccid3_pr_debug("%s(%p), LOSS_EVENT_RATE=%u\n",
  510. dccp_role(sk), sk,
  511. opt_recv->ccid3or_loss_event_rate);
  512. }
  513. break;
  514. case TFRC_OPT_LOSS_INTERVALS:
  515. opt_recv->ccid3or_loss_intervals_idx = idx;
  516. opt_recv->ccid3or_loss_intervals_len = len;
  517. ccid3_pr_debug("%s(%p), LOSS_INTERVALS=(%u, %u)\n",
  518. dccp_role(sk), sk,
  519. opt_recv->ccid3or_loss_intervals_idx,
  520. opt_recv->ccid3or_loss_intervals_len);
  521. break;
  522. case TFRC_OPT_RECEIVE_RATE:
  523. if (unlikely(len != 4)) {
  524. DCCP_WARN("%s(%p), invalid len %d "
  525. "for TFRC_OPT_RECEIVE_RATE\n",
  526. dccp_role(sk), sk, len);
  527. rc = -EINVAL;
  528. } else {
  529. opt_recv->ccid3or_receive_rate =
  530. ntohl(*(__be32 *)value);
  531. ccid3_pr_debug("%s(%p), RECEIVE_RATE=%u\n",
  532. dccp_role(sk), sk,
  533. opt_recv->ccid3or_receive_rate);
  534. }
  535. break;
  536. }
  537. return rc;
  538. }
  539. static int ccid3_hc_tx_init(struct ccid *ccid, struct sock *sk)
  540. {
  541. struct ccid3_hc_tx_sock *hctx = ccid_priv(ccid);
  542. hctx->ccid3hctx_s = 0;
  543. hctx->ccid3hctx_rtt = 0;
  544. hctx->ccid3hctx_state = TFRC_SSTATE_NO_SENT;
  545. INIT_LIST_HEAD(&hctx->ccid3hctx_hist);
  546. hctx->ccid3hctx_no_feedback_timer.function =
  547. ccid3_hc_tx_no_feedback_timer;
  548. hctx->ccid3hctx_no_feedback_timer.data = (unsigned long)sk;
  549. init_timer(&hctx->ccid3hctx_no_feedback_timer);
  550. return 0;
  551. }
  552. static void ccid3_hc_tx_exit(struct sock *sk)
  553. {
  554. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  555. BUG_ON(hctx == NULL);
  556. ccid3_hc_tx_set_state(sk, TFRC_SSTATE_TERM);
  557. sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
  558. /* Empty packet history */
  559. dccp_tx_hist_purge(ccid3_tx_hist, &hctx->ccid3hctx_hist);
  560. }
  561. /*
  562. * RX Half Connection methods
  563. */
  564. #ifdef CONFIG_IP_DCCP_CCID3_DEBUG
  565. static const char *ccid3_rx_state_name(enum ccid3_hc_rx_states state)
  566. {
  567. static char *ccid3_rx_state_names[] = {
  568. [TFRC_RSTATE_NO_DATA] = "NO_DATA",
  569. [TFRC_RSTATE_DATA] = "DATA",
  570. [TFRC_RSTATE_TERM] = "TERM",
  571. };
  572. return ccid3_rx_state_names[state];
  573. }
  574. #endif
  575. static void ccid3_hc_rx_set_state(struct sock *sk,
  576. enum ccid3_hc_rx_states state)
  577. {
  578. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  579. enum ccid3_hc_rx_states oldstate = hcrx->ccid3hcrx_state;
  580. ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
  581. dccp_role(sk), sk, ccid3_rx_state_name(oldstate),
  582. ccid3_rx_state_name(state));
  583. WARN_ON(state == oldstate);
  584. hcrx->ccid3hcrx_state = state;
  585. }
  586. static inline void ccid3_hc_rx_update_s(struct ccid3_hc_rx_sock *hcrx, int len)
  587. {
  588. if (unlikely(len == 0)) /* don't update on empty packets (e.g. ACKs) */
  589. ccid3_pr_debug("Packet payload length is 0 - not updating\n");
  590. else
  591. hcrx->ccid3hcrx_s = hcrx->ccid3hcrx_s == 0 ? len :
  592. (9 * hcrx->ccid3hcrx_s + len) / 10;
  593. }
  594. static void ccid3_hc_rx_send_feedback(struct sock *sk)
  595. {
  596. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  597. struct dccp_sock *dp = dccp_sk(sk);
  598. struct dccp_rx_hist_entry *packet;
  599. struct timeval now;
  600. suseconds_t delta;
  601. ccid3_pr_debug("%s(%p) - entry \n", dccp_role(sk), sk);
  602. dccp_timestamp(sk, &now);
  603. switch (hcrx->ccid3hcrx_state) {
  604. case TFRC_RSTATE_NO_DATA:
  605. hcrx->ccid3hcrx_x_recv = 0;
  606. break;
  607. case TFRC_RSTATE_DATA:
  608. delta = timeval_delta(&now,
  609. &hcrx->ccid3hcrx_tstamp_last_feedback);
  610. DCCP_BUG_ON(delta < 0);
  611. hcrx->ccid3hcrx_x_recv =
  612. scaled_div32(hcrx->ccid3hcrx_bytes_recv, delta);
  613. break;
  614. case TFRC_RSTATE_TERM:
  615. DCCP_BUG("%s(%p) - Illegal state TERM", dccp_role(sk), sk);
  616. return;
  617. }
  618. packet = dccp_rx_hist_find_data_packet(&hcrx->ccid3hcrx_hist);
  619. if (unlikely(packet == NULL)) {
  620. DCCP_WARN("%s(%p), no data packet in history!\n",
  621. dccp_role(sk), sk);
  622. return;
  623. }
  624. hcrx->ccid3hcrx_tstamp_last_feedback = now;
  625. hcrx->ccid3hcrx_ccval_last_counter = packet->dccphrx_ccval;
  626. hcrx->ccid3hcrx_bytes_recv = 0;
  627. /* Elapsed time information [RFC 4340, 13.2] in units of 10 * usecs */
  628. delta = timeval_delta(&now, &packet->dccphrx_tstamp);
  629. DCCP_BUG_ON(delta < 0);
  630. hcrx->ccid3hcrx_elapsed_time = delta / 10;
  631. if (hcrx->ccid3hcrx_p == 0)
  632. hcrx->ccid3hcrx_pinv = ~0U; /* see RFC 4342, 8.5 */
  633. else if (hcrx->ccid3hcrx_p > 1000000) {
  634. DCCP_WARN("p (%u) > 100%%\n", hcrx->ccid3hcrx_p);
  635. hcrx->ccid3hcrx_pinv = 1; /* use 100% in this case */
  636. } else
  637. hcrx->ccid3hcrx_pinv = 1000000 / hcrx->ccid3hcrx_p;
  638. dp->dccps_hc_rx_insert_options = 1;
  639. dccp_send_ack(sk);
  640. }
  641. static int ccid3_hc_rx_insert_options(struct sock *sk, struct sk_buff *skb)
  642. {
  643. const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  644. __be32 x_recv, pinv;
  645. BUG_ON(hcrx == NULL);
  646. if (!(sk->sk_state == DCCP_OPEN || sk->sk_state == DCCP_PARTOPEN))
  647. return 0;
  648. DCCP_SKB_CB(skb)->dccpd_ccval = hcrx->ccid3hcrx_ccval_last_counter;
  649. if (dccp_packet_without_ack(skb))
  650. return 0;
  651. x_recv = htonl(hcrx->ccid3hcrx_x_recv);
  652. pinv = htonl(hcrx->ccid3hcrx_pinv);
  653. if ((hcrx->ccid3hcrx_elapsed_time != 0 &&
  654. dccp_insert_option_elapsed_time(sk, skb,
  655. hcrx->ccid3hcrx_elapsed_time)) ||
  656. dccp_insert_option_timestamp(sk, skb) ||
  657. dccp_insert_option(sk, skb, TFRC_OPT_LOSS_EVENT_RATE,
  658. &pinv, sizeof(pinv)) ||
  659. dccp_insert_option(sk, skb, TFRC_OPT_RECEIVE_RATE,
  660. &x_recv, sizeof(x_recv)))
  661. return -1;
  662. return 0;
  663. }
  664. /* calculate first loss interval
  665. *
  666. * returns estimated loss interval in usecs */
  667. static u32 ccid3_hc_rx_calc_first_li(struct sock *sk)
  668. {
  669. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  670. struct dccp_rx_hist_entry *entry, *next, *tail = NULL;
  671. u32 x_recv, p;
  672. suseconds_t rtt, delta;
  673. struct timeval tstamp = { 0, };
  674. int interval = 0;
  675. int win_count = 0;
  676. int step = 0;
  677. u64 fval;
  678. list_for_each_entry_safe(entry, next, &hcrx->ccid3hcrx_hist,
  679. dccphrx_node) {
  680. if (dccp_rx_hist_entry_data_packet(entry)) {
  681. tail = entry;
  682. switch (step) {
  683. case 0:
  684. tstamp = entry->dccphrx_tstamp;
  685. win_count = entry->dccphrx_ccval;
  686. step = 1;
  687. break;
  688. case 1:
  689. interval = win_count - entry->dccphrx_ccval;
  690. if (interval < 0)
  691. interval += TFRC_WIN_COUNT_LIMIT;
  692. if (interval > 4)
  693. goto found;
  694. break;
  695. }
  696. }
  697. }
  698. if (unlikely(step == 0)) {
  699. DCCP_WARN("%s(%p), packet history has no data packets!\n",
  700. dccp_role(sk), sk);
  701. return ~0;
  702. }
  703. if (unlikely(interval == 0)) {
  704. DCCP_WARN("%s(%p), Could not find a win_count interval > 0."
  705. "Defaulting to 1\n", dccp_role(sk), sk);
  706. interval = 1;
  707. }
  708. found:
  709. if (!tail) {
  710. DCCP_CRIT("tail is null\n");
  711. return ~0;
  712. }
  713. delta = timeval_delta(&tstamp, &tail->dccphrx_tstamp);
  714. DCCP_BUG_ON(delta < 0);
  715. rtt = delta * 4 / interval;
  716. ccid3_pr_debug("%s(%p), approximated RTT to %dus\n",
  717. dccp_role(sk), sk, (int)rtt);
  718. /*
  719. * Determine the length of the first loss interval via inverse lookup.
  720. * Assume that X_recv can be computed by the throughput equation
  721. * s
  722. * X_recv = --------
  723. * R * fval
  724. * Find some p such that f(p) = fval; return 1/p [RFC 3448, 6.3.1].
  725. */
  726. if (rtt == 0) { /* would result in divide-by-zero */
  727. DCCP_WARN("RTT==0\n");
  728. return ~0;
  729. }
  730. dccp_timestamp(sk, &tstamp);
  731. delta = timeval_delta(&tstamp, &hcrx->ccid3hcrx_tstamp_last_feedback);
  732. DCCP_BUG_ON(delta <= 0);
  733. x_recv = scaled_div32(hcrx->ccid3hcrx_bytes_recv, delta);
  734. if (x_recv == 0) { /* would also trigger divide-by-zero */
  735. DCCP_WARN("X_recv==0\n");
  736. if ((x_recv = hcrx->ccid3hcrx_x_recv) == 0) {
  737. DCCP_BUG("stored value of X_recv is zero");
  738. return ~0;
  739. }
  740. }
  741. fval = scaled_div(hcrx->ccid3hcrx_s, rtt);
  742. fval = scaled_div32(fval, x_recv);
  743. p = tfrc_calc_x_reverse_lookup(fval);
  744. ccid3_pr_debug("%s(%p), receive rate=%u bytes/s, implied "
  745. "loss rate=%u\n", dccp_role(sk), sk, x_recv, p);
  746. if (p == 0)
  747. return ~0;
  748. else
  749. return 1000000 / p;
  750. }
  751. static void ccid3_hc_rx_update_li(struct sock *sk, u64 seq_loss, u8 win_loss)
  752. {
  753. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  754. struct dccp_li_hist_entry *head;
  755. u64 seq_temp;
  756. if (list_empty(&hcrx->ccid3hcrx_li_hist)) {
  757. if (!dccp_li_hist_interval_new(ccid3_li_hist,
  758. &hcrx->ccid3hcrx_li_hist, seq_loss, win_loss))
  759. return;
  760. head = list_entry(hcrx->ccid3hcrx_li_hist.next,
  761. struct dccp_li_hist_entry, dccplih_node);
  762. head->dccplih_interval = ccid3_hc_rx_calc_first_li(sk);
  763. } else {
  764. struct dccp_li_hist_entry *entry;
  765. struct list_head *tail;
  766. head = list_entry(hcrx->ccid3hcrx_li_hist.next,
  767. struct dccp_li_hist_entry, dccplih_node);
  768. /* FIXME win count check removed as was wrong */
  769. /* should make this check with receive history */
  770. /* and compare there as per section 10.2 of RFC4342 */
  771. /* new loss event detected */
  772. /* calculate last interval length */
  773. seq_temp = dccp_delta_seqno(head->dccplih_seqno, seq_loss);
  774. entry = dccp_li_hist_entry_new(ccid3_li_hist, GFP_ATOMIC);
  775. if (entry == NULL) {
  776. DCCP_BUG("out of memory - can not allocate entry");
  777. return;
  778. }
  779. list_add(&entry->dccplih_node, &hcrx->ccid3hcrx_li_hist);
  780. tail = hcrx->ccid3hcrx_li_hist.prev;
  781. list_del(tail);
  782. kmem_cache_free(ccid3_li_hist->dccplih_slab, tail);
  783. /* Create the newest interval */
  784. entry->dccplih_seqno = seq_loss;
  785. entry->dccplih_interval = seq_temp;
  786. entry->dccplih_win_count = win_loss;
  787. }
  788. }
  789. static int ccid3_hc_rx_detect_loss(struct sock *sk,
  790. struct dccp_rx_hist_entry *packet)
  791. {
  792. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  793. struct dccp_rx_hist_entry *rx_hist =
  794. dccp_rx_hist_head(&hcrx->ccid3hcrx_hist);
  795. u64 seqno = packet->dccphrx_seqno;
  796. u64 tmp_seqno;
  797. int loss = 0;
  798. u8 ccval;
  799. tmp_seqno = hcrx->ccid3hcrx_seqno_nonloss;
  800. if (!rx_hist ||
  801. follows48(packet->dccphrx_seqno, hcrx->ccid3hcrx_seqno_nonloss)) {
  802. hcrx->ccid3hcrx_seqno_nonloss = seqno;
  803. hcrx->ccid3hcrx_ccval_nonloss = packet->dccphrx_ccval;
  804. goto detect_out;
  805. }
  806. while (dccp_delta_seqno(hcrx->ccid3hcrx_seqno_nonloss, seqno)
  807. > TFRC_RECV_NUM_LATE_LOSS) {
  808. loss = 1;
  809. ccid3_hc_rx_update_li(sk, hcrx->ccid3hcrx_seqno_nonloss,
  810. hcrx->ccid3hcrx_ccval_nonloss);
  811. tmp_seqno = hcrx->ccid3hcrx_seqno_nonloss;
  812. dccp_inc_seqno(&tmp_seqno);
  813. hcrx->ccid3hcrx_seqno_nonloss = tmp_seqno;
  814. dccp_inc_seqno(&tmp_seqno);
  815. while (dccp_rx_hist_find_entry(&hcrx->ccid3hcrx_hist,
  816. tmp_seqno, &ccval)) {
  817. hcrx->ccid3hcrx_seqno_nonloss = tmp_seqno;
  818. hcrx->ccid3hcrx_ccval_nonloss = ccval;
  819. dccp_inc_seqno(&tmp_seqno);
  820. }
  821. }
  822. /* FIXME - this code could be simplified with above while */
  823. /* but works at moment */
  824. if (follows48(packet->dccphrx_seqno, hcrx->ccid3hcrx_seqno_nonloss)) {
  825. hcrx->ccid3hcrx_seqno_nonloss = seqno;
  826. hcrx->ccid3hcrx_ccval_nonloss = packet->dccphrx_ccval;
  827. }
  828. detect_out:
  829. dccp_rx_hist_add_packet(ccid3_rx_hist, &hcrx->ccid3hcrx_hist,
  830. &hcrx->ccid3hcrx_li_hist, packet,
  831. hcrx->ccid3hcrx_seqno_nonloss);
  832. return loss;
  833. }
  834. static void ccid3_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
  835. {
  836. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  837. const struct dccp_options_received *opt_recv;
  838. struct dccp_rx_hist_entry *packet;
  839. struct timeval now;
  840. u32 p_prev, rtt_prev;
  841. suseconds_t r_sample, t_elapsed;
  842. int loss, payload_size;
  843. BUG_ON(hcrx == NULL);
  844. opt_recv = &dccp_sk(sk)->dccps_options_received;
  845. switch (DCCP_SKB_CB(skb)->dccpd_type) {
  846. case DCCP_PKT_ACK:
  847. if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
  848. return;
  849. case DCCP_PKT_DATAACK:
  850. if (opt_recv->dccpor_timestamp_echo == 0)
  851. break;
  852. rtt_prev = hcrx->ccid3hcrx_rtt;
  853. dccp_timestamp(sk, &now);
  854. timeval_sub_usecs(&now, opt_recv->dccpor_timestamp_echo * 10);
  855. r_sample = timeval_usecs(&now);
  856. t_elapsed = opt_recv->dccpor_elapsed_time * 10;
  857. DCCP_BUG_ON(r_sample < 0);
  858. if (unlikely(r_sample <= t_elapsed))
  859. DCCP_WARN("r_sample=%ldus, t_elapsed=%ldus\n",
  860. r_sample, t_elapsed);
  861. else
  862. r_sample -= t_elapsed;
  863. CCID3_RTT_SANITY_CHECK(r_sample);
  864. if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
  865. hcrx->ccid3hcrx_rtt = r_sample;
  866. else
  867. hcrx->ccid3hcrx_rtt = (hcrx->ccid3hcrx_rtt * 9) / 10 +
  868. r_sample / 10;
  869. if (rtt_prev != hcrx->ccid3hcrx_rtt)
  870. ccid3_pr_debug("%s(%p), New RTT=%uus, elapsed time=%u\n",
  871. dccp_role(sk), sk, hcrx->ccid3hcrx_rtt,
  872. opt_recv->dccpor_elapsed_time);
  873. break;
  874. case DCCP_PKT_DATA:
  875. break;
  876. default: /* We're not interested in other packet types, move along */
  877. return;
  878. }
  879. packet = dccp_rx_hist_entry_new(ccid3_rx_hist, sk, opt_recv->dccpor_ndp,
  880. skb, GFP_ATOMIC);
  881. if (unlikely(packet == NULL)) {
  882. DCCP_WARN("%s(%p), Not enough mem to add rx packet "
  883. "to history, consider it lost!\n", dccp_role(sk), sk);
  884. return;
  885. }
  886. loss = ccid3_hc_rx_detect_loss(sk, packet);
  887. if (DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK)
  888. return;
  889. payload_size = skb->len - dccp_hdr(skb)->dccph_doff * 4;
  890. ccid3_hc_rx_update_s(hcrx, payload_size);
  891. switch (hcrx->ccid3hcrx_state) {
  892. case TFRC_RSTATE_NO_DATA:
  893. ccid3_pr_debug("%s(%p, state=%s), skb=%p, sending initial "
  894. "feedback\n", dccp_role(sk), sk,
  895. dccp_state_name(sk->sk_state), skb);
  896. ccid3_hc_rx_send_feedback(sk);
  897. ccid3_hc_rx_set_state(sk, TFRC_RSTATE_DATA);
  898. return;
  899. case TFRC_RSTATE_DATA:
  900. hcrx->ccid3hcrx_bytes_recv += payload_size;
  901. if (loss)
  902. break;
  903. dccp_timestamp(sk, &now);
  904. if ((timeval_delta(&now, &hcrx->ccid3hcrx_tstamp_last_ack) -
  905. (suseconds_t)hcrx->ccid3hcrx_rtt) >= 0) {
  906. hcrx->ccid3hcrx_tstamp_last_ack = now;
  907. ccid3_hc_rx_send_feedback(sk);
  908. }
  909. return;
  910. case TFRC_RSTATE_TERM:
  911. DCCP_BUG("%s(%p) - Illegal state TERM", dccp_role(sk), sk);
  912. return;
  913. }
  914. /* Dealing with packet loss */
  915. ccid3_pr_debug("%s(%p, state=%s), data loss! Reacting...\n",
  916. dccp_role(sk), sk, dccp_state_name(sk->sk_state));
  917. p_prev = hcrx->ccid3hcrx_p;
  918. /* Calculate loss event rate */
  919. if (!list_empty(&hcrx->ccid3hcrx_li_hist)) {
  920. u32 i_mean = dccp_li_hist_calc_i_mean(&hcrx->ccid3hcrx_li_hist);
  921. /* Scaling up by 1000000 as fixed decimal */
  922. if (i_mean != 0)
  923. hcrx->ccid3hcrx_p = 1000000 / i_mean;
  924. } else
  925. DCCP_BUG("empty loss history");
  926. if (hcrx->ccid3hcrx_p > p_prev) {
  927. ccid3_hc_rx_send_feedback(sk);
  928. return;
  929. }
  930. }
  931. static int ccid3_hc_rx_init(struct ccid *ccid, struct sock *sk)
  932. {
  933. struct ccid3_hc_rx_sock *hcrx = ccid_priv(ccid);
  934. ccid3_pr_debug("entry\n");
  935. hcrx->ccid3hcrx_state = TFRC_RSTATE_NO_DATA;
  936. INIT_LIST_HEAD(&hcrx->ccid3hcrx_hist);
  937. INIT_LIST_HEAD(&hcrx->ccid3hcrx_li_hist);
  938. dccp_timestamp(sk, &hcrx->ccid3hcrx_tstamp_last_ack);
  939. hcrx->ccid3hcrx_tstamp_last_feedback = hcrx->ccid3hcrx_tstamp_last_ack;
  940. hcrx->ccid3hcrx_s = 0;
  941. hcrx->ccid3hcrx_rtt = 0;
  942. return 0;
  943. }
  944. static void ccid3_hc_rx_exit(struct sock *sk)
  945. {
  946. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  947. BUG_ON(hcrx == NULL);
  948. ccid3_hc_rx_set_state(sk, TFRC_RSTATE_TERM);
  949. /* Empty packet history */
  950. dccp_rx_hist_purge(ccid3_rx_hist, &hcrx->ccid3hcrx_hist);
  951. /* Empty loss interval history */
  952. dccp_li_hist_purge(ccid3_li_hist, &hcrx->ccid3hcrx_li_hist);
  953. }
  954. static void ccid3_hc_rx_get_info(struct sock *sk, struct tcp_info *info)
  955. {
  956. const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  957. /* Listen socks doesn't have a private CCID block */
  958. if (sk->sk_state == DCCP_LISTEN)
  959. return;
  960. BUG_ON(hcrx == NULL);
  961. info->tcpi_ca_state = hcrx->ccid3hcrx_state;
  962. info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
  963. info->tcpi_rcv_rtt = hcrx->ccid3hcrx_rtt;
  964. }
  965. static void ccid3_hc_tx_get_info(struct sock *sk, struct tcp_info *info)
  966. {
  967. const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  968. /* Listen socks doesn't have a private CCID block */
  969. if (sk->sk_state == DCCP_LISTEN)
  970. return;
  971. BUG_ON(hctx == NULL);
  972. info->tcpi_rto = hctx->ccid3hctx_t_rto;
  973. info->tcpi_rtt = hctx->ccid3hctx_rtt;
  974. }
  975. static int ccid3_hc_rx_getsockopt(struct sock *sk, const int optname, int len,
  976. u32 __user *optval, int __user *optlen)
  977. {
  978. const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  979. const void *val;
  980. /* Listen socks doesn't have a private CCID block */
  981. if (sk->sk_state == DCCP_LISTEN)
  982. return -EINVAL;
  983. switch (optname) {
  984. case DCCP_SOCKOPT_CCID_RX_INFO:
  985. if (len < sizeof(hcrx->ccid3hcrx_tfrc))
  986. return -EINVAL;
  987. len = sizeof(hcrx->ccid3hcrx_tfrc);
  988. val = &hcrx->ccid3hcrx_tfrc;
  989. break;
  990. default:
  991. return -ENOPROTOOPT;
  992. }
  993. if (put_user(len, optlen) || copy_to_user(optval, val, len))
  994. return -EFAULT;
  995. return 0;
  996. }
  997. static int ccid3_hc_tx_getsockopt(struct sock *sk, const int optname, int len,
  998. u32 __user *optval, int __user *optlen)
  999. {
  1000. const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  1001. const void *val;
  1002. /* Listen socks doesn't have a private CCID block */
  1003. if (sk->sk_state == DCCP_LISTEN)
  1004. return -EINVAL;
  1005. switch (optname) {
  1006. case DCCP_SOCKOPT_CCID_TX_INFO:
  1007. if (len < sizeof(hctx->ccid3hctx_tfrc))
  1008. return -EINVAL;
  1009. len = sizeof(hctx->ccid3hctx_tfrc);
  1010. val = &hctx->ccid3hctx_tfrc;
  1011. break;
  1012. default:
  1013. return -ENOPROTOOPT;
  1014. }
  1015. if (put_user(len, optlen) || copy_to_user(optval, val, len))
  1016. return -EFAULT;
  1017. return 0;
  1018. }
  1019. static struct ccid_operations ccid3 = {
  1020. .ccid_id = DCCPC_CCID3,
  1021. .ccid_name = "ccid3",
  1022. .ccid_owner = THIS_MODULE,
  1023. .ccid_hc_tx_obj_size = sizeof(struct ccid3_hc_tx_sock),
  1024. .ccid_hc_tx_init = ccid3_hc_tx_init,
  1025. .ccid_hc_tx_exit = ccid3_hc_tx_exit,
  1026. .ccid_hc_tx_send_packet = ccid3_hc_tx_send_packet,
  1027. .ccid_hc_tx_packet_sent = ccid3_hc_tx_packet_sent,
  1028. .ccid_hc_tx_packet_recv = ccid3_hc_tx_packet_recv,
  1029. .ccid_hc_tx_parse_options = ccid3_hc_tx_parse_options,
  1030. .ccid_hc_rx_obj_size = sizeof(struct ccid3_hc_rx_sock),
  1031. .ccid_hc_rx_init = ccid3_hc_rx_init,
  1032. .ccid_hc_rx_exit = ccid3_hc_rx_exit,
  1033. .ccid_hc_rx_insert_options = ccid3_hc_rx_insert_options,
  1034. .ccid_hc_rx_packet_recv = ccid3_hc_rx_packet_recv,
  1035. .ccid_hc_rx_get_info = ccid3_hc_rx_get_info,
  1036. .ccid_hc_tx_get_info = ccid3_hc_tx_get_info,
  1037. .ccid_hc_rx_getsockopt = ccid3_hc_rx_getsockopt,
  1038. .ccid_hc_tx_getsockopt = ccid3_hc_tx_getsockopt,
  1039. };
  1040. #ifdef CONFIG_IP_DCCP_CCID3_DEBUG
  1041. module_param(ccid3_debug, int, 0444);
  1042. MODULE_PARM_DESC(ccid3_debug, "Enable debug messages");
  1043. #endif
  1044. static __init int ccid3_module_init(void)
  1045. {
  1046. int rc = -ENOBUFS;
  1047. ccid3_rx_hist = dccp_rx_hist_new("ccid3");
  1048. if (ccid3_rx_hist == NULL)
  1049. goto out;
  1050. ccid3_tx_hist = dccp_tx_hist_new("ccid3");
  1051. if (ccid3_tx_hist == NULL)
  1052. goto out_free_rx;
  1053. ccid3_li_hist = dccp_li_hist_new("ccid3");
  1054. if (ccid3_li_hist == NULL)
  1055. goto out_free_tx;
  1056. rc = ccid_register(&ccid3);
  1057. if (rc != 0)
  1058. goto out_free_loss_interval_history;
  1059. out:
  1060. return rc;
  1061. out_free_loss_interval_history:
  1062. dccp_li_hist_delete(ccid3_li_hist);
  1063. ccid3_li_hist = NULL;
  1064. out_free_tx:
  1065. dccp_tx_hist_delete(ccid3_tx_hist);
  1066. ccid3_tx_hist = NULL;
  1067. out_free_rx:
  1068. dccp_rx_hist_delete(ccid3_rx_hist);
  1069. ccid3_rx_hist = NULL;
  1070. goto out;
  1071. }
  1072. module_init(ccid3_module_init);
  1073. static __exit void ccid3_module_exit(void)
  1074. {
  1075. ccid_unregister(&ccid3);
  1076. if (ccid3_tx_hist != NULL) {
  1077. dccp_tx_hist_delete(ccid3_tx_hist);
  1078. ccid3_tx_hist = NULL;
  1079. }
  1080. if (ccid3_rx_hist != NULL) {
  1081. dccp_rx_hist_delete(ccid3_rx_hist);
  1082. ccid3_rx_hist = NULL;
  1083. }
  1084. if (ccid3_li_hist != NULL) {
  1085. dccp_li_hist_delete(ccid3_li_hist);
  1086. ccid3_li_hist = NULL;
  1087. }
  1088. }
  1089. module_exit(ccid3_module_exit);
  1090. MODULE_AUTHOR("Ian McDonald <ian.mcdonald@jandi.co.nz>, "
  1091. "Arnaldo Carvalho de Melo <acme@ghostprotocols.net>");
  1092. MODULE_DESCRIPTION("DCCP TFRC CCID3 CCID");
  1093. MODULE_LICENSE("GPL");
  1094. MODULE_ALIAS("net-dccp-ccid-3");