ccid3.c 37 KB

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