tp_meter.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2012-2018 B.A.T.M.A.N. contributors:
  3. *
  4. * Edo Monticelli, Antonio Quartulli
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "tp_meter.h"
  19. #include "main.h"
  20. #include <linux/atomic.h>
  21. #include <linux/build_bug.h>
  22. #include <linux/byteorder/generic.h>
  23. #include <linux/cache.h>
  24. #include <linux/compiler.h>
  25. #include <linux/err.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/gfp.h>
  28. #include <linux/if_ether.h>
  29. #include <linux/init.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/kernel.h>
  32. #include <linux/kref.h>
  33. #include <linux/kthread.h>
  34. #include <linux/list.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/param.h>
  37. #include <linux/printk.h>
  38. #include <linux/random.h>
  39. #include <linux/rculist.h>
  40. #include <linux/rcupdate.h>
  41. #include <linux/sched.h>
  42. #include <linux/skbuff.h>
  43. #include <linux/slab.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/stddef.h>
  46. #include <linux/string.h>
  47. #include <linux/timer.h>
  48. #include <linux/wait.h>
  49. #include <linux/workqueue.h>
  50. #include <uapi/linux/batadv_packet.h>
  51. #include <uapi/linux/batman_adv.h>
  52. #include "hard-interface.h"
  53. #include "log.h"
  54. #include "netlink.h"
  55. #include "originator.h"
  56. #include "send.h"
  57. /**
  58. * BATADV_TP_DEF_TEST_LENGTH - Default test length if not specified by the user
  59. * in milliseconds
  60. */
  61. #define BATADV_TP_DEF_TEST_LENGTH 10000
  62. /**
  63. * BATADV_TP_AWND - Advertised window by the receiver (in bytes)
  64. */
  65. #define BATADV_TP_AWND 0x20000000
  66. /**
  67. * BATADV_TP_RECV_TIMEOUT - Receiver activity timeout. If the receiver does not
  68. * get anything for such amount of milliseconds, the connection is killed
  69. */
  70. #define BATADV_TP_RECV_TIMEOUT 1000
  71. /**
  72. * BATADV_TP_MAX_RTO - Maximum sender timeout. If the sender RTO gets beyond
  73. * such amound of milliseconds, the receiver is considered unreachable and the
  74. * connection is killed
  75. */
  76. #define BATADV_TP_MAX_RTO 30000
  77. /**
  78. * BATADV_TP_FIRST_SEQ - First seqno of each session. The number is rather high
  79. * in order to immediately trigger a wrap around (test purposes)
  80. */
  81. #define BATADV_TP_FIRST_SEQ ((u32)-1 - 2000)
  82. /**
  83. * BATADV_TP_PLEN - length of the payload (data after the batadv_unicast header)
  84. * to simulate
  85. */
  86. #define BATADV_TP_PLEN (BATADV_TP_PACKET_LEN - ETH_HLEN - \
  87. sizeof(struct batadv_unicast_packet))
  88. static u8 batadv_tp_prerandom[4096] __read_mostly;
  89. /**
  90. * batadv_tp_session_cookie() - generate session cookie based on session ids
  91. * @session: TP session identifier
  92. * @icmp_uid: icmp pseudo uid of the tp session
  93. *
  94. * Return: 32 bit tp_meter session cookie
  95. */
  96. static u32 batadv_tp_session_cookie(const u8 session[2], u8 icmp_uid)
  97. {
  98. u32 cookie;
  99. cookie = icmp_uid << 16;
  100. cookie |= session[0] << 8;
  101. cookie |= session[1];
  102. return cookie;
  103. }
  104. /**
  105. * batadv_tp_cwnd() - compute the new cwnd size
  106. * @base: base cwnd size value
  107. * @increment: the value to add to base to get the new size
  108. * @min: minumim cwnd value (usually MSS)
  109. *
  110. * Return the new cwnd size and ensures it does not exceed the Advertised
  111. * Receiver Window size. It is wrap around safe.
  112. * For details refer to Section 3.1 of RFC5681
  113. *
  114. * Return: new congestion window size in bytes
  115. */
  116. static u32 batadv_tp_cwnd(u32 base, u32 increment, u32 min)
  117. {
  118. u32 new_size = base + increment;
  119. /* check for wrap-around */
  120. if (new_size < base)
  121. new_size = (u32)ULONG_MAX;
  122. new_size = min_t(u32, new_size, BATADV_TP_AWND);
  123. return max_t(u32, new_size, min);
  124. }
  125. /**
  126. * batadv_tp_updated_cwnd() - update the Congestion Windows
  127. * @tp_vars: the private data of the current TP meter session
  128. * @mss: maximum segment size of transmission
  129. *
  130. * 1) if the session is in Slow Start, the CWND has to be increased by 1
  131. * MSS every unique received ACK
  132. * 2) if the session is in Congestion Avoidance, the CWND has to be
  133. * increased by MSS * MSS / CWND for every unique received ACK
  134. */
  135. static void batadv_tp_update_cwnd(struct batadv_tp_vars *tp_vars, u32 mss)
  136. {
  137. spin_lock_bh(&tp_vars->cwnd_lock);
  138. /* slow start... */
  139. if (tp_vars->cwnd <= tp_vars->ss_threshold) {
  140. tp_vars->dec_cwnd = 0;
  141. tp_vars->cwnd = batadv_tp_cwnd(tp_vars->cwnd, mss, mss);
  142. spin_unlock_bh(&tp_vars->cwnd_lock);
  143. return;
  144. }
  145. /* increment CWND at least of 1 (section 3.1 of RFC5681) */
  146. tp_vars->dec_cwnd += max_t(u32, 1U << 3,
  147. ((mss * mss) << 6) / (tp_vars->cwnd << 3));
  148. if (tp_vars->dec_cwnd < (mss << 3)) {
  149. spin_unlock_bh(&tp_vars->cwnd_lock);
  150. return;
  151. }
  152. tp_vars->cwnd = batadv_tp_cwnd(tp_vars->cwnd, mss, mss);
  153. tp_vars->dec_cwnd = 0;
  154. spin_unlock_bh(&tp_vars->cwnd_lock);
  155. }
  156. /**
  157. * batadv_tp_update_rto() - calculate new retransmission timeout
  158. * @tp_vars: the private data of the current TP meter session
  159. * @new_rtt: new roundtrip time in msec
  160. */
  161. static void batadv_tp_update_rto(struct batadv_tp_vars *tp_vars,
  162. u32 new_rtt)
  163. {
  164. long m = new_rtt;
  165. /* RTT update
  166. * Details in Section 2.2 and 2.3 of RFC6298
  167. *
  168. * It's tricky to understand. Don't lose hair please.
  169. * Inspired by tcp_rtt_estimator() tcp_input.c
  170. */
  171. if (tp_vars->srtt != 0) {
  172. m -= (tp_vars->srtt >> 3); /* m is now error in rtt est */
  173. tp_vars->srtt += m; /* rtt = 7/8 srtt + 1/8 new */
  174. if (m < 0)
  175. m = -m;
  176. m -= (tp_vars->rttvar >> 2);
  177. tp_vars->rttvar += m; /* mdev ~= 3/4 rttvar + 1/4 new */
  178. } else {
  179. /* first measure getting in */
  180. tp_vars->srtt = m << 3; /* take the measured time to be srtt */
  181. tp_vars->rttvar = m << 1; /* new_rtt / 2 */
  182. }
  183. /* rto = srtt + 4 * rttvar.
  184. * rttvar is scaled by 4, therefore doesn't need to be multiplied
  185. */
  186. tp_vars->rto = (tp_vars->srtt >> 3) + tp_vars->rttvar;
  187. }
  188. /**
  189. * batadv_tp_batctl_notify() - send client status result to client
  190. * @reason: reason for tp meter session stop
  191. * @dst: destination of tp_meter session
  192. * @bat_priv: the bat priv with all the soft interface information
  193. * @start_time: start of transmission in jiffies
  194. * @total_sent: bytes acked to the receiver
  195. * @cookie: cookie of tp_meter session
  196. */
  197. static void batadv_tp_batctl_notify(enum batadv_tp_meter_reason reason,
  198. const u8 *dst, struct batadv_priv *bat_priv,
  199. unsigned long start_time, u64 total_sent,
  200. u32 cookie)
  201. {
  202. u32 test_time;
  203. u8 result;
  204. u32 total_bytes;
  205. if (!batadv_tp_is_error(reason)) {
  206. result = BATADV_TP_REASON_COMPLETE;
  207. test_time = jiffies_to_msecs(jiffies - start_time);
  208. total_bytes = total_sent;
  209. } else {
  210. result = reason;
  211. test_time = 0;
  212. total_bytes = 0;
  213. }
  214. batadv_netlink_tpmeter_notify(bat_priv, dst, result, test_time,
  215. total_bytes, cookie);
  216. }
  217. /**
  218. * batadv_tp_batctl_error_notify() - send client error result to client
  219. * @reason: reason for tp meter session stop
  220. * @dst: destination of tp_meter session
  221. * @bat_priv: the bat priv with all the soft interface information
  222. * @cookie: cookie of tp_meter session
  223. */
  224. static void batadv_tp_batctl_error_notify(enum batadv_tp_meter_reason reason,
  225. const u8 *dst,
  226. struct batadv_priv *bat_priv,
  227. u32 cookie)
  228. {
  229. batadv_tp_batctl_notify(reason, dst, bat_priv, 0, 0, cookie);
  230. }
  231. /**
  232. * batadv_tp_list_find() - find a tp_vars object in the global list
  233. * @bat_priv: the bat priv with all the soft interface information
  234. * @dst: the other endpoint MAC address to look for
  235. *
  236. * Look for a tp_vars object matching dst as end_point and return it after
  237. * having incremented the refcounter. Return NULL is not found
  238. *
  239. * Return: matching tp_vars or NULL when no tp_vars with @dst was found
  240. */
  241. static struct batadv_tp_vars *batadv_tp_list_find(struct batadv_priv *bat_priv,
  242. const u8 *dst)
  243. {
  244. struct batadv_tp_vars *pos, *tp_vars = NULL;
  245. rcu_read_lock();
  246. hlist_for_each_entry_rcu(pos, &bat_priv->tp_list, list) {
  247. if (!batadv_compare_eth(pos->other_end, dst))
  248. continue;
  249. /* most of the time this function is invoked during the normal
  250. * process..it makes sens to pay more when the session is
  251. * finished and to speed the process up during the measurement
  252. */
  253. if (unlikely(!kref_get_unless_zero(&pos->refcount)))
  254. continue;
  255. tp_vars = pos;
  256. break;
  257. }
  258. rcu_read_unlock();
  259. return tp_vars;
  260. }
  261. /**
  262. * batadv_tp_list_find_session() - find tp_vars session object in the global
  263. * list
  264. * @bat_priv: the bat priv with all the soft interface information
  265. * @dst: the other endpoint MAC address to look for
  266. * @session: session identifier
  267. *
  268. * Look for a tp_vars object matching dst as end_point, session as tp meter
  269. * session and return it after having incremented the refcounter. Return NULL
  270. * is not found
  271. *
  272. * Return: matching tp_vars or NULL when no tp_vars was found
  273. */
  274. static struct batadv_tp_vars *
  275. batadv_tp_list_find_session(struct batadv_priv *bat_priv, const u8 *dst,
  276. const u8 *session)
  277. {
  278. struct batadv_tp_vars *pos, *tp_vars = NULL;
  279. rcu_read_lock();
  280. hlist_for_each_entry_rcu(pos, &bat_priv->tp_list, list) {
  281. if (!batadv_compare_eth(pos->other_end, dst))
  282. continue;
  283. if (memcmp(pos->session, session, sizeof(pos->session)) != 0)
  284. continue;
  285. /* most of the time this function is invoked during the normal
  286. * process..it makes sense to pay more when the session is
  287. * finished and to speed the process up during the measurement
  288. */
  289. if (unlikely(!kref_get_unless_zero(&pos->refcount)))
  290. continue;
  291. tp_vars = pos;
  292. break;
  293. }
  294. rcu_read_unlock();
  295. return tp_vars;
  296. }
  297. /**
  298. * batadv_tp_vars_release() - release batadv_tp_vars from lists and queue for
  299. * free after rcu grace period
  300. * @ref: kref pointer of the batadv_tp_vars
  301. */
  302. static void batadv_tp_vars_release(struct kref *ref)
  303. {
  304. struct batadv_tp_vars *tp_vars;
  305. struct batadv_tp_unacked *un, *safe;
  306. tp_vars = container_of(ref, struct batadv_tp_vars, refcount);
  307. /* lock should not be needed because this object is now out of any
  308. * context!
  309. */
  310. spin_lock_bh(&tp_vars->unacked_lock);
  311. list_for_each_entry_safe(un, safe, &tp_vars->unacked_list, list) {
  312. list_del(&un->list);
  313. kfree(un);
  314. }
  315. spin_unlock_bh(&tp_vars->unacked_lock);
  316. kfree_rcu(tp_vars, rcu);
  317. }
  318. /**
  319. * batadv_tp_vars_put() - decrement the batadv_tp_vars refcounter and possibly
  320. * release it
  321. * @tp_vars: the private data of the current TP meter session to be free'd
  322. */
  323. static void batadv_tp_vars_put(struct batadv_tp_vars *tp_vars)
  324. {
  325. kref_put(&tp_vars->refcount, batadv_tp_vars_release);
  326. }
  327. /**
  328. * batadv_tp_sender_cleanup() - cleanup sender data and drop and timer
  329. * @bat_priv: the bat priv with all the soft interface information
  330. * @tp_vars: the private data of the current TP meter session to cleanup
  331. */
  332. static void batadv_tp_sender_cleanup(struct batadv_priv *bat_priv,
  333. struct batadv_tp_vars *tp_vars)
  334. {
  335. cancel_delayed_work(&tp_vars->finish_work);
  336. spin_lock_bh(&tp_vars->bat_priv->tp_list_lock);
  337. hlist_del_rcu(&tp_vars->list);
  338. spin_unlock_bh(&tp_vars->bat_priv->tp_list_lock);
  339. /* drop list reference */
  340. batadv_tp_vars_put(tp_vars);
  341. atomic_dec(&tp_vars->bat_priv->tp_num);
  342. /* kill the timer and remove its reference */
  343. del_timer_sync(&tp_vars->timer);
  344. /* the worker might have rearmed itself therefore we kill it again. Note
  345. * that if the worker should run again before invoking the following
  346. * del_timer(), it would not re-arm itself once again because the status
  347. * is OFF now
  348. */
  349. del_timer(&tp_vars->timer);
  350. batadv_tp_vars_put(tp_vars);
  351. }
  352. /**
  353. * batadv_tp_sender_end() - print info about ended session and inform client
  354. * @bat_priv: the bat priv with all the soft interface information
  355. * @tp_vars: the private data of the current TP meter session
  356. */
  357. static void batadv_tp_sender_end(struct batadv_priv *bat_priv,
  358. struct batadv_tp_vars *tp_vars)
  359. {
  360. u32 session_cookie;
  361. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  362. "Test towards %pM finished..shutting down (reason=%d)\n",
  363. tp_vars->other_end, tp_vars->reason);
  364. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  365. "Last timing stats: SRTT=%ums RTTVAR=%ums RTO=%ums\n",
  366. tp_vars->srtt >> 3, tp_vars->rttvar >> 2, tp_vars->rto);
  367. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  368. "Final values: cwnd=%u ss_threshold=%u\n",
  369. tp_vars->cwnd, tp_vars->ss_threshold);
  370. session_cookie = batadv_tp_session_cookie(tp_vars->session,
  371. tp_vars->icmp_uid);
  372. batadv_tp_batctl_notify(tp_vars->reason,
  373. tp_vars->other_end,
  374. bat_priv,
  375. tp_vars->start_time,
  376. atomic64_read(&tp_vars->tot_sent),
  377. session_cookie);
  378. }
  379. /**
  380. * batadv_tp_sender_shutdown() - let sender thread/timer stop gracefully
  381. * @tp_vars: the private data of the current TP meter session
  382. * @reason: reason for tp meter session stop
  383. */
  384. static void batadv_tp_sender_shutdown(struct batadv_tp_vars *tp_vars,
  385. enum batadv_tp_meter_reason reason)
  386. {
  387. if (!atomic_dec_and_test(&tp_vars->sending))
  388. return;
  389. tp_vars->reason = reason;
  390. }
  391. /**
  392. * batadv_tp_sender_finish() - stop sender session after test_length was reached
  393. * @work: delayed work reference of the related tp_vars
  394. */
  395. static void batadv_tp_sender_finish(struct work_struct *work)
  396. {
  397. struct delayed_work *delayed_work;
  398. struct batadv_tp_vars *tp_vars;
  399. delayed_work = to_delayed_work(work);
  400. tp_vars = container_of(delayed_work, struct batadv_tp_vars,
  401. finish_work);
  402. batadv_tp_sender_shutdown(tp_vars, BATADV_TP_REASON_COMPLETE);
  403. }
  404. /**
  405. * batadv_tp_reset_sender_timer() - reschedule the sender timer
  406. * @tp_vars: the private TP meter data for this session
  407. *
  408. * Reschedule the timer using tp_vars->rto as delay
  409. */
  410. static void batadv_tp_reset_sender_timer(struct batadv_tp_vars *tp_vars)
  411. {
  412. /* most of the time this function is invoked while normal packet
  413. * reception...
  414. */
  415. if (unlikely(atomic_read(&tp_vars->sending) == 0))
  416. /* timer ref will be dropped in batadv_tp_sender_cleanup */
  417. return;
  418. mod_timer(&tp_vars->timer, jiffies + msecs_to_jiffies(tp_vars->rto));
  419. }
  420. /**
  421. * batadv_tp_sender_timeout() - timer that fires in case of packet loss
  422. * @t: address to timer_list inside tp_vars
  423. *
  424. * If fired it means that there was packet loss.
  425. * Switch to Slow Start, set the ss_threshold to half of the current cwnd and
  426. * reset the cwnd to 3*MSS
  427. */
  428. static void batadv_tp_sender_timeout(struct timer_list *t)
  429. {
  430. struct batadv_tp_vars *tp_vars = from_timer(tp_vars, t, timer);
  431. struct batadv_priv *bat_priv = tp_vars->bat_priv;
  432. if (atomic_read(&tp_vars->sending) == 0)
  433. return;
  434. /* if the user waited long enough...shutdown the test */
  435. if (unlikely(tp_vars->rto >= BATADV_TP_MAX_RTO)) {
  436. batadv_tp_sender_shutdown(tp_vars,
  437. BATADV_TP_REASON_DST_UNREACHABLE);
  438. return;
  439. }
  440. /* RTO exponential backoff
  441. * Details in Section 5.5 of RFC6298
  442. */
  443. tp_vars->rto <<= 1;
  444. spin_lock_bh(&tp_vars->cwnd_lock);
  445. tp_vars->ss_threshold = tp_vars->cwnd >> 1;
  446. if (tp_vars->ss_threshold < BATADV_TP_PLEN * 2)
  447. tp_vars->ss_threshold = BATADV_TP_PLEN * 2;
  448. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  449. "Meter: RTO fired during test towards %pM! cwnd=%u new ss_thr=%u, resetting last_sent to %u\n",
  450. tp_vars->other_end, tp_vars->cwnd, tp_vars->ss_threshold,
  451. atomic_read(&tp_vars->last_acked));
  452. tp_vars->cwnd = BATADV_TP_PLEN * 3;
  453. spin_unlock_bh(&tp_vars->cwnd_lock);
  454. /* resend the non-ACKed packets.. */
  455. tp_vars->last_sent = atomic_read(&tp_vars->last_acked);
  456. wake_up(&tp_vars->more_bytes);
  457. batadv_tp_reset_sender_timer(tp_vars);
  458. }
  459. /**
  460. * batadv_tp_fill_prerandom() - Fill buffer with prefetched random bytes
  461. * @tp_vars: the private TP meter data for this session
  462. * @buf: Buffer to fill with bytes
  463. * @nbytes: amount of pseudorandom bytes
  464. */
  465. static void batadv_tp_fill_prerandom(struct batadv_tp_vars *tp_vars,
  466. u8 *buf, size_t nbytes)
  467. {
  468. u32 local_offset;
  469. size_t bytes_inbuf;
  470. size_t to_copy;
  471. size_t pos = 0;
  472. spin_lock_bh(&tp_vars->prerandom_lock);
  473. local_offset = tp_vars->prerandom_offset;
  474. tp_vars->prerandom_offset += nbytes;
  475. tp_vars->prerandom_offset %= sizeof(batadv_tp_prerandom);
  476. spin_unlock_bh(&tp_vars->prerandom_lock);
  477. while (nbytes) {
  478. local_offset %= sizeof(batadv_tp_prerandom);
  479. bytes_inbuf = sizeof(batadv_tp_prerandom) - local_offset;
  480. to_copy = min(nbytes, bytes_inbuf);
  481. memcpy(&buf[pos], &batadv_tp_prerandom[local_offset], to_copy);
  482. pos += to_copy;
  483. nbytes -= to_copy;
  484. local_offset = 0;
  485. }
  486. }
  487. /**
  488. * batadv_tp_send_msg() - send a single message
  489. * @tp_vars: the private TP meter data for this session
  490. * @src: source mac address
  491. * @orig_node: the originator of the destination
  492. * @seqno: sequence number of this packet
  493. * @len: length of the entire packet
  494. * @session: session identifier
  495. * @uid: local ICMP "socket" index
  496. * @timestamp: timestamp in jiffies which is replied in ack
  497. *
  498. * Create and send a single TP Meter message.
  499. *
  500. * Return: 0 on success, BATADV_TP_REASON_DST_UNREACHABLE if the destination is
  501. * not reachable, BATADV_TP_REASON_MEMORY_ERROR if the packet couldn't be
  502. * allocated
  503. */
  504. static int batadv_tp_send_msg(struct batadv_tp_vars *tp_vars, const u8 *src,
  505. struct batadv_orig_node *orig_node,
  506. u32 seqno, size_t len, const u8 *session,
  507. int uid, u32 timestamp)
  508. {
  509. struct batadv_icmp_tp_packet *icmp;
  510. struct sk_buff *skb;
  511. int r;
  512. u8 *data;
  513. size_t data_len;
  514. skb = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN);
  515. if (unlikely(!skb))
  516. return BATADV_TP_REASON_MEMORY_ERROR;
  517. skb_reserve(skb, ETH_HLEN);
  518. icmp = skb_put(skb, sizeof(*icmp));
  519. /* fill the icmp header */
  520. ether_addr_copy(icmp->dst, orig_node->orig);
  521. ether_addr_copy(icmp->orig, src);
  522. icmp->version = BATADV_COMPAT_VERSION;
  523. icmp->packet_type = BATADV_ICMP;
  524. icmp->ttl = BATADV_TTL;
  525. icmp->msg_type = BATADV_TP;
  526. icmp->uid = uid;
  527. icmp->subtype = BATADV_TP_MSG;
  528. memcpy(icmp->session, session, sizeof(icmp->session));
  529. icmp->seqno = htonl(seqno);
  530. icmp->timestamp = htonl(timestamp);
  531. data_len = len - sizeof(*icmp);
  532. data = skb_put(skb, data_len);
  533. batadv_tp_fill_prerandom(tp_vars, data, data_len);
  534. r = batadv_send_skb_to_orig(skb, orig_node, NULL);
  535. if (r == NET_XMIT_SUCCESS)
  536. return 0;
  537. return BATADV_TP_REASON_CANT_SEND;
  538. }
  539. /**
  540. * batadv_tp_recv_ack() - ACK receiving function
  541. * @bat_priv: the bat priv with all the soft interface information
  542. * @skb: the buffer containing the received packet
  543. *
  544. * Process a received TP ACK packet
  545. */
  546. static void batadv_tp_recv_ack(struct batadv_priv *bat_priv,
  547. const struct sk_buff *skb)
  548. {
  549. struct batadv_hard_iface *primary_if = NULL;
  550. struct batadv_orig_node *orig_node = NULL;
  551. const struct batadv_icmp_tp_packet *icmp;
  552. struct batadv_tp_vars *tp_vars;
  553. size_t packet_len, mss;
  554. u32 rtt, recv_ack, cwnd;
  555. unsigned char *dev_addr;
  556. packet_len = BATADV_TP_PLEN;
  557. mss = BATADV_TP_PLEN;
  558. packet_len += sizeof(struct batadv_unicast_packet);
  559. icmp = (struct batadv_icmp_tp_packet *)skb->data;
  560. /* find the tp_vars */
  561. tp_vars = batadv_tp_list_find_session(bat_priv, icmp->orig,
  562. icmp->session);
  563. if (unlikely(!tp_vars))
  564. return;
  565. if (unlikely(atomic_read(&tp_vars->sending) == 0))
  566. goto out;
  567. /* old ACK? silently drop it.. */
  568. if (batadv_seq_before(ntohl(icmp->seqno),
  569. (u32)atomic_read(&tp_vars->last_acked)))
  570. goto out;
  571. primary_if = batadv_primary_if_get_selected(bat_priv);
  572. if (unlikely(!primary_if))
  573. goto out;
  574. orig_node = batadv_orig_hash_find(bat_priv, icmp->orig);
  575. if (unlikely(!orig_node))
  576. goto out;
  577. /* update RTO with the new sampled RTT, if any */
  578. rtt = jiffies_to_msecs(jiffies) - ntohl(icmp->timestamp);
  579. if (icmp->timestamp && rtt)
  580. batadv_tp_update_rto(tp_vars, rtt);
  581. /* ACK for new data... reset the timer */
  582. batadv_tp_reset_sender_timer(tp_vars);
  583. recv_ack = ntohl(icmp->seqno);
  584. /* check if this ACK is a duplicate */
  585. if (atomic_read(&tp_vars->last_acked) == recv_ack) {
  586. atomic_inc(&tp_vars->dup_acks);
  587. if (atomic_read(&tp_vars->dup_acks) != 3)
  588. goto out;
  589. if (recv_ack >= tp_vars->recover)
  590. goto out;
  591. /* if this is the third duplicate ACK do Fast Retransmit */
  592. batadv_tp_send_msg(tp_vars, primary_if->net_dev->dev_addr,
  593. orig_node, recv_ack, packet_len,
  594. icmp->session, icmp->uid,
  595. jiffies_to_msecs(jiffies));
  596. spin_lock_bh(&tp_vars->cwnd_lock);
  597. /* Fast Recovery */
  598. tp_vars->fast_recovery = true;
  599. /* Set recover to the last outstanding seqno when Fast Recovery
  600. * is entered. RFC6582, Section 3.2, step 1
  601. */
  602. tp_vars->recover = tp_vars->last_sent;
  603. tp_vars->ss_threshold = tp_vars->cwnd >> 1;
  604. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  605. "Meter: Fast Recovery, (cur cwnd=%u) ss_thr=%u last_sent=%u recv_ack=%u\n",
  606. tp_vars->cwnd, tp_vars->ss_threshold,
  607. tp_vars->last_sent, recv_ack);
  608. tp_vars->cwnd = batadv_tp_cwnd(tp_vars->ss_threshold, 3 * mss,
  609. mss);
  610. tp_vars->dec_cwnd = 0;
  611. tp_vars->last_sent = recv_ack;
  612. spin_unlock_bh(&tp_vars->cwnd_lock);
  613. } else {
  614. /* count the acked data */
  615. atomic64_add(recv_ack - atomic_read(&tp_vars->last_acked),
  616. &tp_vars->tot_sent);
  617. /* reset the duplicate ACKs counter */
  618. atomic_set(&tp_vars->dup_acks, 0);
  619. if (tp_vars->fast_recovery) {
  620. /* partial ACK */
  621. if (batadv_seq_before(recv_ack, tp_vars->recover)) {
  622. /* this is another hole in the window. React
  623. * immediately as specified by NewReno (see
  624. * Section 3.2 of RFC6582 for details)
  625. */
  626. dev_addr = primary_if->net_dev->dev_addr;
  627. batadv_tp_send_msg(tp_vars, dev_addr,
  628. orig_node, recv_ack,
  629. packet_len, icmp->session,
  630. icmp->uid,
  631. jiffies_to_msecs(jiffies));
  632. tp_vars->cwnd = batadv_tp_cwnd(tp_vars->cwnd,
  633. mss, mss);
  634. } else {
  635. tp_vars->fast_recovery = false;
  636. /* set cwnd to the value of ss_threshold at the
  637. * moment that Fast Recovery was entered.
  638. * RFC6582, Section 3.2, step 3
  639. */
  640. cwnd = batadv_tp_cwnd(tp_vars->ss_threshold, 0,
  641. mss);
  642. tp_vars->cwnd = cwnd;
  643. }
  644. goto move_twnd;
  645. }
  646. if (recv_ack - atomic_read(&tp_vars->last_acked) >= mss)
  647. batadv_tp_update_cwnd(tp_vars, mss);
  648. move_twnd:
  649. /* move the Transmit Window */
  650. atomic_set(&tp_vars->last_acked, recv_ack);
  651. }
  652. wake_up(&tp_vars->more_bytes);
  653. out:
  654. if (likely(primary_if))
  655. batadv_hardif_put(primary_if);
  656. if (likely(orig_node))
  657. batadv_orig_node_put(orig_node);
  658. if (likely(tp_vars))
  659. batadv_tp_vars_put(tp_vars);
  660. }
  661. /**
  662. * batadv_tp_avail() - check if congestion window is not full
  663. * @tp_vars: the private data of the current TP meter session
  664. * @payload_len: size of the payload of a single message
  665. *
  666. * Return: true when congestion window is not full, false otherwise
  667. */
  668. static bool batadv_tp_avail(struct batadv_tp_vars *tp_vars,
  669. size_t payload_len)
  670. {
  671. u32 win_left, win_limit;
  672. win_limit = atomic_read(&tp_vars->last_acked) + tp_vars->cwnd;
  673. win_left = win_limit - tp_vars->last_sent;
  674. return win_left >= payload_len;
  675. }
  676. /**
  677. * batadv_tp_wait_available() - wait until congestion window becomes free or
  678. * timeout is reached
  679. * @tp_vars: the private data of the current TP meter session
  680. * @plen: size of the payload of a single message
  681. *
  682. * Return: 0 if the condition evaluated to false after the timeout elapsed,
  683. * 1 if the condition evaluated to true after the timeout elapsed, the
  684. * remaining jiffies (at least 1) if the condition evaluated to true before
  685. * the timeout elapsed, or -ERESTARTSYS if it was interrupted by a signal.
  686. */
  687. static int batadv_tp_wait_available(struct batadv_tp_vars *tp_vars, size_t plen)
  688. {
  689. int ret;
  690. ret = wait_event_interruptible_timeout(tp_vars->more_bytes,
  691. batadv_tp_avail(tp_vars, plen),
  692. HZ / 10);
  693. return ret;
  694. }
  695. /**
  696. * batadv_tp_send() - main sending thread of a tp meter session
  697. * @arg: address of the related tp_vars
  698. *
  699. * Return: nothing, this function never returns
  700. */
  701. static int batadv_tp_send(void *arg)
  702. {
  703. struct batadv_tp_vars *tp_vars = arg;
  704. struct batadv_priv *bat_priv = tp_vars->bat_priv;
  705. struct batadv_hard_iface *primary_if = NULL;
  706. struct batadv_orig_node *orig_node = NULL;
  707. size_t payload_len, packet_len;
  708. int err = 0;
  709. if (unlikely(tp_vars->role != BATADV_TP_SENDER)) {
  710. err = BATADV_TP_REASON_DST_UNREACHABLE;
  711. tp_vars->reason = err;
  712. goto out;
  713. }
  714. orig_node = batadv_orig_hash_find(bat_priv, tp_vars->other_end);
  715. if (unlikely(!orig_node)) {
  716. err = BATADV_TP_REASON_DST_UNREACHABLE;
  717. tp_vars->reason = err;
  718. goto out;
  719. }
  720. primary_if = batadv_primary_if_get_selected(bat_priv);
  721. if (unlikely(!primary_if)) {
  722. err = BATADV_TP_REASON_DST_UNREACHABLE;
  723. tp_vars->reason = err;
  724. goto out;
  725. }
  726. /* assume that all the hard_interfaces have a correctly
  727. * configured MTU, so use the soft_iface MTU as MSS.
  728. * This might not be true and in that case the fragmentation
  729. * should be used.
  730. * Now, try to send the packet as it is
  731. */
  732. payload_len = BATADV_TP_PLEN;
  733. BUILD_BUG_ON(sizeof(struct batadv_icmp_tp_packet) > BATADV_TP_PLEN);
  734. batadv_tp_reset_sender_timer(tp_vars);
  735. /* queue the worker in charge of terminating the test */
  736. queue_delayed_work(batadv_event_workqueue, &tp_vars->finish_work,
  737. msecs_to_jiffies(tp_vars->test_length));
  738. while (atomic_read(&tp_vars->sending) != 0) {
  739. if (unlikely(!batadv_tp_avail(tp_vars, payload_len))) {
  740. batadv_tp_wait_available(tp_vars, payload_len);
  741. continue;
  742. }
  743. /* to emulate normal unicast traffic, add to the payload len
  744. * the size of the unicast header
  745. */
  746. packet_len = payload_len + sizeof(struct batadv_unicast_packet);
  747. err = batadv_tp_send_msg(tp_vars, primary_if->net_dev->dev_addr,
  748. orig_node, tp_vars->last_sent,
  749. packet_len,
  750. tp_vars->session, tp_vars->icmp_uid,
  751. jiffies_to_msecs(jiffies));
  752. /* something went wrong during the preparation/transmission */
  753. if (unlikely(err && err != BATADV_TP_REASON_CANT_SEND)) {
  754. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  755. "Meter: %s() cannot send packets (%d)\n",
  756. __func__, err);
  757. /* ensure nobody else tries to stop the thread now */
  758. if (atomic_dec_and_test(&tp_vars->sending))
  759. tp_vars->reason = err;
  760. break;
  761. }
  762. /* right-shift the TWND */
  763. if (!err)
  764. tp_vars->last_sent += payload_len;
  765. cond_resched();
  766. }
  767. out:
  768. if (likely(primary_if))
  769. batadv_hardif_put(primary_if);
  770. if (likely(orig_node))
  771. batadv_orig_node_put(orig_node);
  772. batadv_tp_sender_end(bat_priv, tp_vars);
  773. batadv_tp_sender_cleanup(bat_priv, tp_vars);
  774. batadv_tp_vars_put(tp_vars);
  775. do_exit(0);
  776. }
  777. /**
  778. * batadv_tp_start_kthread() - start new thread which manages the tp meter
  779. * sender
  780. * @tp_vars: the private data of the current TP meter session
  781. */
  782. static void batadv_tp_start_kthread(struct batadv_tp_vars *tp_vars)
  783. {
  784. struct task_struct *kthread;
  785. struct batadv_priv *bat_priv = tp_vars->bat_priv;
  786. u32 session_cookie;
  787. kref_get(&tp_vars->refcount);
  788. kthread = kthread_create(batadv_tp_send, tp_vars, "kbatadv_tp_meter");
  789. if (IS_ERR(kthread)) {
  790. session_cookie = batadv_tp_session_cookie(tp_vars->session,
  791. tp_vars->icmp_uid);
  792. pr_err("batadv: cannot create tp meter kthread\n");
  793. batadv_tp_batctl_error_notify(BATADV_TP_REASON_MEMORY_ERROR,
  794. tp_vars->other_end,
  795. bat_priv, session_cookie);
  796. /* drop reserved reference for kthread */
  797. batadv_tp_vars_put(tp_vars);
  798. /* cleanup of failed tp meter variables */
  799. batadv_tp_sender_cleanup(bat_priv, tp_vars);
  800. return;
  801. }
  802. wake_up_process(kthread);
  803. }
  804. /**
  805. * batadv_tp_start() - start a new tp meter session
  806. * @bat_priv: the bat priv with all the soft interface information
  807. * @dst: the receiver MAC address
  808. * @test_length: test length in milliseconds
  809. * @cookie: session cookie
  810. */
  811. void batadv_tp_start(struct batadv_priv *bat_priv, const u8 *dst,
  812. u32 test_length, u32 *cookie)
  813. {
  814. struct batadv_tp_vars *tp_vars;
  815. u8 session_id[2];
  816. u8 icmp_uid;
  817. u32 session_cookie;
  818. get_random_bytes(session_id, sizeof(session_id));
  819. get_random_bytes(&icmp_uid, 1);
  820. session_cookie = batadv_tp_session_cookie(session_id, icmp_uid);
  821. *cookie = session_cookie;
  822. /* look for an already existing test towards this node */
  823. spin_lock_bh(&bat_priv->tp_list_lock);
  824. tp_vars = batadv_tp_list_find(bat_priv, dst);
  825. if (tp_vars) {
  826. spin_unlock_bh(&bat_priv->tp_list_lock);
  827. batadv_tp_vars_put(tp_vars);
  828. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  829. "Meter: test to or from the same node already ongoing, aborting\n");
  830. batadv_tp_batctl_error_notify(BATADV_TP_REASON_ALREADY_ONGOING,
  831. dst, bat_priv, session_cookie);
  832. return;
  833. }
  834. if (!atomic_add_unless(&bat_priv->tp_num, 1, BATADV_TP_MAX_NUM)) {
  835. spin_unlock_bh(&bat_priv->tp_list_lock);
  836. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  837. "Meter: too many ongoing sessions, aborting (SEND)\n");
  838. batadv_tp_batctl_error_notify(BATADV_TP_REASON_TOO_MANY, dst,
  839. bat_priv, session_cookie);
  840. return;
  841. }
  842. tp_vars = kmalloc(sizeof(*tp_vars), GFP_ATOMIC);
  843. if (!tp_vars) {
  844. spin_unlock_bh(&bat_priv->tp_list_lock);
  845. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  846. "Meter: %s cannot allocate list elements\n",
  847. __func__);
  848. batadv_tp_batctl_error_notify(BATADV_TP_REASON_MEMORY_ERROR,
  849. dst, bat_priv, session_cookie);
  850. return;
  851. }
  852. /* initialize tp_vars */
  853. ether_addr_copy(tp_vars->other_end, dst);
  854. kref_init(&tp_vars->refcount);
  855. tp_vars->role = BATADV_TP_SENDER;
  856. atomic_set(&tp_vars->sending, 1);
  857. memcpy(tp_vars->session, session_id, sizeof(session_id));
  858. tp_vars->icmp_uid = icmp_uid;
  859. tp_vars->last_sent = BATADV_TP_FIRST_SEQ;
  860. atomic_set(&tp_vars->last_acked, BATADV_TP_FIRST_SEQ);
  861. tp_vars->fast_recovery = false;
  862. tp_vars->recover = BATADV_TP_FIRST_SEQ;
  863. /* initialise the CWND to 3*MSS (Section 3.1 in RFC5681).
  864. * For batman-adv the MSS is the size of the payload received by the
  865. * soft_interface, hence its MTU
  866. */
  867. tp_vars->cwnd = BATADV_TP_PLEN * 3;
  868. /* at the beginning initialise the SS threshold to the biggest possible
  869. * window size, hence the AWND size
  870. */
  871. tp_vars->ss_threshold = BATADV_TP_AWND;
  872. /* RTO initial value is 3 seconds.
  873. * Details in Section 2.1 of RFC6298
  874. */
  875. tp_vars->rto = 1000;
  876. tp_vars->srtt = 0;
  877. tp_vars->rttvar = 0;
  878. atomic64_set(&tp_vars->tot_sent, 0);
  879. kref_get(&tp_vars->refcount);
  880. timer_setup(&tp_vars->timer, batadv_tp_sender_timeout, 0);
  881. tp_vars->bat_priv = bat_priv;
  882. tp_vars->start_time = jiffies;
  883. init_waitqueue_head(&tp_vars->more_bytes);
  884. spin_lock_init(&tp_vars->unacked_lock);
  885. INIT_LIST_HEAD(&tp_vars->unacked_list);
  886. spin_lock_init(&tp_vars->cwnd_lock);
  887. tp_vars->prerandom_offset = 0;
  888. spin_lock_init(&tp_vars->prerandom_lock);
  889. kref_get(&tp_vars->refcount);
  890. hlist_add_head_rcu(&tp_vars->list, &bat_priv->tp_list);
  891. spin_unlock_bh(&bat_priv->tp_list_lock);
  892. tp_vars->test_length = test_length;
  893. if (!tp_vars->test_length)
  894. tp_vars->test_length = BATADV_TP_DEF_TEST_LENGTH;
  895. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  896. "Meter: starting throughput meter towards %pM (length=%ums)\n",
  897. dst, test_length);
  898. /* init work item for finished tp tests */
  899. INIT_DELAYED_WORK(&tp_vars->finish_work, batadv_tp_sender_finish);
  900. /* start tp kthread. This way the write() call issued from userspace can
  901. * happily return and avoid to block
  902. */
  903. batadv_tp_start_kthread(tp_vars);
  904. /* don't return reference to new tp_vars */
  905. batadv_tp_vars_put(tp_vars);
  906. }
  907. /**
  908. * batadv_tp_stop() - stop currently running tp meter session
  909. * @bat_priv: the bat priv with all the soft interface information
  910. * @dst: the receiver MAC address
  911. * @return_value: reason for tp meter session stop
  912. */
  913. void batadv_tp_stop(struct batadv_priv *bat_priv, const u8 *dst,
  914. u8 return_value)
  915. {
  916. struct batadv_orig_node *orig_node;
  917. struct batadv_tp_vars *tp_vars;
  918. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  919. "Meter: stopping test towards %pM\n", dst);
  920. orig_node = batadv_orig_hash_find(bat_priv, dst);
  921. if (!orig_node)
  922. return;
  923. tp_vars = batadv_tp_list_find(bat_priv, orig_node->orig);
  924. if (!tp_vars) {
  925. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  926. "Meter: trying to interrupt an already over connection\n");
  927. goto out;
  928. }
  929. batadv_tp_sender_shutdown(tp_vars, return_value);
  930. batadv_tp_vars_put(tp_vars);
  931. out:
  932. batadv_orig_node_put(orig_node);
  933. }
  934. /**
  935. * batadv_tp_reset_receiver_timer() - reset the receiver shutdown timer
  936. * @tp_vars: the private data of the current TP meter session
  937. *
  938. * start the receiver shutdown timer or reset it if already started
  939. */
  940. static void batadv_tp_reset_receiver_timer(struct batadv_tp_vars *tp_vars)
  941. {
  942. mod_timer(&tp_vars->timer,
  943. jiffies + msecs_to_jiffies(BATADV_TP_RECV_TIMEOUT));
  944. }
  945. /**
  946. * batadv_tp_receiver_shutdown() - stop a tp meter receiver when timeout is
  947. * reached without received ack
  948. * @t: address to timer_list inside tp_vars
  949. */
  950. static void batadv_tp_receiver_shutdown(struct timer_list *t)
  951. {
  952. struct batadv_tp_vars *tp_vars = from_timer(tp_vars, t, timer);
  953. struct batadv_tp_unacked *un, *safe;
  954. struct batadv_priv *bat_priv;
  955. bat_priv = tp_vars->bat_priv;
  956. /* if there is recent activity rearm the timer */
  957. if (!batadv_has_timed_out(tp_vars->last_recv_time,
  958. BATADV_TP_RECV_TIMEOUT)) {
  959. /* reset the receiver shutdown timer */
  960. batadv_tp_reset_receiver_timer(tp_vars);
  961. return;
  962. }
  963. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  964. "Shutting down for inactivity (more than %dms) from %pM\n",
  965. BATADV_TP_RECV_TIMEOUT, tp_vars->other_end);
  966. spin_lock_bh(&tp_vars->bat_priv->tp_list_lock);
  967. hlist_del_rcu(&tp_vars->list);
  968. spin_unlock_bh(&tp_vars->bat_priv->tp_list_lock);
  969. /* drop list reference */
  970. batadv_tp_vars_put(tp_vars);
  971. atomic_dec(&bat_priv->tp_num);
  972. spin_lock_bh(&tp_vars->unacked_lock);
  973. list_for_each_entry_safe(un, safe, &tp_vars->unacked_list, list) {
  974. list_del(&un->list);
  975. kfree(un);
  976. }
  977. spin_unlock_bh(&tp_vars->unacked_lock);
  978. /* drop reference of timer */
  979. batadv_tp_vars_put(tp_vars);
  980. }
  981. /**
  982. * batadv_tp_send_ack() - send an ACK packet
  983. * @bat_priv: the bat priv with all the soft interface information
  984. * @dst: the mac address of the destination originator
  985. * @seq: the sequence number to ACK
  986. * @timestamp: the timestamp to echo back in the ACK
  987. * @session: session identifier
  988. * @socket_index: local ICMP socket identifier
  989. *
  990. * Return: 0 on success, a positive integer representing the reason of the
  991. * failure otherwise
  992. */
  993. static int batadv_tp_send_ack(struct batadv_priv *bat_priv, const u8 *dst,
  994. u32 seq, __be32 timestamp, const u8 *session,
  995. int socket_index)
  996. {
  997. struct batadv_hard_iface *primary_if = NULL;
  998. struct batadv_orig_node *orig_node;
  999. struct batadv_icmp_tp_packet *icmp;
  1000. struct sk_buff *skb;
  1001. int r, ret;
  1002. orig_node = batadv_orig_hash_find(bat_priv, dst);
  1003. if (unlikely(!orig_node)) {
  1004. ret = BATADV_TP_REASON_DST_UNREACHABLE;
  1005. goto out;
  1006. }
  1007. primary_if = batadv_primary_if_get_selected(bat_priv);
  1008. if (unlikely(!primary_if)) {
  1009. ret = BATADV_TP_REASON_DST_UNREACHABLE;
  1010. goto out;
  1011. }
  1012. skb = netdev_alloc_skb_ip_align(NULL, sizeof(*icmp) + ETH_HLEN);
  1013. if (unlikely(!skb)) {
  1014. ret = BATADV_TP_REASON_MEMORY_ERROR;
  1015. goto out;
  1016. }
  1017. skb_reserve(skb, ETH_HLEN);
  1018. icmp = skb_put(skb, sizeof(*icmp));
  1019. icmp->packet_type = BATADV_ICMP;
  1020. icmp->version = BATADV_COMPAT_VERSION;
  1021. icmp->ttl = BATADV_TTL;
  1022. icmp->msg_type = BATADV_TP;
  1023. ether_addr_copy(icmp->dst, orig_node->orig);
  1024. ether_addr_copy(icmp->orig, primary_if->net_dev->dev_addr);
  1025. icmp->uid = socket_index;
  1026. icmp->subtype = BATADV_TP_ACK;
  1027. memcpy(icmp->session, session, sizeof(icmp->session));
  1028. icmp->seqno = htonl(seq);
  1029. icmp->timestamp = timestamp;
  1030. /* send the ack */
  1031. r = batadv_send_skb_to_orig(skb, orig_node, NULL);
  1032. if (unlikely(r < 0) || r == NET_XMIT_DROP) {
  1033. ret = BATADV_TP_REASON_DST_UNREACHABLE;
  1034. goto out;
  1035. }
  1036. ret = 0;
  1037. out:
  1038. if (likely(orig_node))
  1039. batadv_orig_node_put(orig_node);
  1040. if (likely(primary_if))
  1041. batadv_hardif_put(primary_if);
  1042. return ret;
  1043. }
  1044. /**
  1045. * batadv_tp_handle_out_of_order() - store an out of order packet
  1046. * @tp_vars: the private data of the current TP meter session
  1047. * @skb: the buffer containing the received packet
  1048. *
  1049. * Store the out of order packet in the unacked list for late processing. This
  1050. * packets are kept in this list so that they can be ACKed at once as soon as
  1051. * all the previous packets have been received
  1052. *
  1053. * Return: true if the packed has been successfully processed, false otherwise
  1054. */
  1055. static bool batadv_tp_handle_out_of_order(struct batadv_tp_vars *tp_vars,
  1056. const struct sk_buff *skb)
  1057. {
  1058. const struct batadv_icmp_tp_packet *icmp;
  1059. struct batadv_tp_unacked *un, *new;
  1060. u32 payload_len;
  1061. bool added = false;
  1062. new = kmalloc(sizeof(*new), GFP_ATOMIC);
  1063. if (unlikely(!new))
  1064. return false;
  1065. icmp = (struct batadv_icmp_tp_packet *)skb->data;
  1066. new->seqno = ntohl(icmp->seqno);
  1067. payload_len = skb->len - sizeof(struct batadv_unicast_packet);
  1068. new->len = payload_len;
  1069. spin_lock_bh(&tp_vars->unacked_lock);
  1070. /* if the list is empty immediately attach this new object */
  1071. if (list_empty(&tp_vars->unacked_list)) {
  1072. list_add(&new->list, &tp_vars->unacked_list);
  1073. goto out;
  1074. }
  1075. /* otherwise loop over the list and either drop the packet because this
  1076. * is a duplicate or store it at the right position.
  1077. *
  1078. * The iteration is done in the reverse way because it is likely that
  1079. * the last received packet (the one being processed now) has a bigger
  1080. * seqno than all the others already stored.
  1081. */
  1082. list_for_each_entry_reverse(un, &tp_vars->unacked_list, list) {
  1083. /* check for duplicates */
  1084. if (new->seqno == un->seqno) {
  1085. if (new->len > un->len)
  1086. un->len = new->len;
  1087. kfree(new);
  1088. added = true;
  1089. break;
  1090. }
  1091. /* look for the right position */
  1092. if (batadv_seq_before(new->seqno, un->seqno))
  1093. continue;
  1094. /* as soon as an entry having a bigger seqno is found, the new
  1095. * one is attached _after_ it. In this way the list is kept in
  1096. * ascending order
  1097. */
  1098. list_add_tail(&new->list, &un->list);
  1099. added = true;
  1100. break;
  1101. }
  1102. /* received packet with smallest seqno out of order; add it to front */
  1103. if (!added)
  1104. list_add(&new->list, &tp_vars->unacked_list);
  1105. out:
  1106. spin_unlock_bh(&tp_vars->unacked_lock);
  1107. return true;
  1108. }
  1109. /**
  1110. * batadv_tp_ack_unordered() - update number received bytes in current stream
  1111. * without gaps
  1112. * @tp_vars: the private data of the current TP meter session
  1113. */
  1114. static void batadv_tp_ack_unordered(struct batadv_tp_vars *tp_vars)
  1115. {
  1116. struct batadv_tp_unacked *un, *safe;
  1117. u32 to_ack;
  1118. /* go through the unacked packet list and possibly ACK them as
  1119. * well
  1120. */
  1121. spin_lock_bh(&tp_vars->unacked_lock);
  1122. list_for_each_entry_safe(un, safe, &tp_vars->unacked_list, list) {
  1123. /* the list is ordered, therefore it is possible to stop as soon
  1124. * there is a gap between the last acked seqno and the seqno of
  1125. * the packet under inspection
  1126. */
  1127. if (batadv_seq_before(tp_vars->last_recv, un->seqno))
  1128. break;
  1129. to_ack = un->seqno + un->len - tp_vars->last_recv;
  1130. if (batadv_seq_before(tp_vars->last_recv, un->seqno + un->len))
  1131. tp_vars->last_recv += to_ack;
  1132. list_del(&un->list);
  1133. kfree(un);
  1134. }
  1135. spin_unlock_bh(&tp_vars->unacked_lock);
  1136. }
  1137. /**
  1138. * batadv_tp_init_recv() - return matching or create new receiver tp_vars
  1139. * @bat_priv: the bat priv with all the soft interface information
  1140. * @icmp: received icmp tp msg
  1141. *
  1142. * Return: corresponding tp_vars or NULL on errors
  1143. */
  1144. static struct batadv_tp_vars *
  1145. batadv_tp_init_recv(struct batadv_priv *bat_priv,
  1146. const struct batadv_icmp_tp_packet *icmp)
  1147. {
  1148. struct batadv_tp_vars *tp_vars;
  1149. spin_lock_bh(&bat_priv->tp_list_lock);
  1150. tp_vars = batadv_tp_list_find_session(bat_priv, icmp->orig,
  1151. icmp->session);
  1152. if (tp_vars)
  1153. goto out_unlock;
  1154. if (!atomic_add_unless(&bat_priv->tp_num, 1, BATADV_TP_MAX_NUM)) {
  1155. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  1156. "Meter: too many ongoing sessions, aborting (RECV)\n");
  1157. goto out_unlock;
  1158. }
  1159. tp_vars = kmalloc(sizeof(*tp_vars), GFP_ATOMIC);
  1160. if (!tp_vars)
  1161. goto out_unlock;
  1162. ether_addr_copy(tp_vars->other_end, icmp->orig);
  1163. tp_vars->role = BATADV_TP_RECEIVER;
  1164. memcpy(tp_vars->session, icmp->session, sizeof(tp_vars->session));
  1165. tp_vars->last_recv = BATADV_TP_FIRST_SEQ;
  1166. tp_vars->bat_priv = bat_priv;
  1167. kref_init(&tp_vars->refcount);
  1168. spin_lock_init(&tp_vars->unacked_lock);
  1169. INIT_LIST_HEAD(&tp_vars->unacked_list);
  1170. kref_get(&tp_vars->refcount);
  1171. hlist_add_head_rcu(&tp_vars->list, &bat_priv->tp_list);
  1172. kref_get(&tp_vars->refcount);
  1173. timer_setup(&tp_vars->timer, batadv_tp_receiver_shutdown, 0);
  1174. batadv_tp_reset_receiver_timer(tp_vars);
  1175. out_unlock:
  1176. spin_unlock_bh(&bat_priv->tp_list_lock);
  1177. return tp_vars;
  1178. }
  1179. /**
  1180. * batadv_tp_recv_msg() - process a single data message
  1181. * @bat_priv: the bat priv with all the soft interface information
  1182. * @skb: the buffer containing the received packet
  1183. *
  1184. * Process a received TP MSG packet
  1185. */
  1186. static void batadv_tp_recv_msg(struct batadv_priv *bat_priv,
  1187. const struct sk_buff *skb)
  1188. {
  1189. const struct batadv_icmp_tp_packet *icmp;
  1190. struct batadv_tp_vars *tp_vars;
  1191. size_t packet_size;
  1192. u32 seqno;
  1193. icmp = (struct batadv_icmp_tp_packet *)skb->data;
  1194. seqno = ntohl(icmp->seqno);
  1195. /* check if this is the first seqno. This means that if the
  1196. * first packet is lost, the tp meter does not work anymore!
  1197. */
  1198. if (seqno == BATADV_TP_FIRST_SEQ) {
  1199. tp_vars = batadv_tp_init_recv(bat_priv, icmp);
  1200. if (!tp_vars) {
  1201. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  1202. "Meter: seqno != BATADV_TP_FIRST_SEQ cannot initiate connection\n");
  1203. goto out;
  1204. }
  1205. } else {
  1206. tp_vars = batadv_tp_list_find_session(bat_priv, icmp->orig,
  1207. icmp->session);
  1208. if (!tp_vars) {
  1209. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  1210. "Unexpected packet from %pM!\n",
  1211. icmp->orig);
  1212. goto out;
  1213. }
  1214. }
  1215. if (unlikely(tp_vars->role != BATADV_TP_RECEIVER)) {
  1216. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  1217. "Meter: dropping packet: not expected (role=%u)\n",
  1218. tp_vars->role);
  1219. goto out;
  1220. }
  1221. tp_vars->last_recv_time = jiffies;
  1222. /* if the packet is a duplicate, it may be the case that an ACK has been
  1223. * lost. Resend the ACK
  1224. */
  1225. if (batadv_seq_before(seqno, tp_vars->last_recv))
  1226. goto send_ack;
  1227. /* if the packet is out of order enqueue it */
  1228. if (ntohl(icmp->seqno) != tp_vars->last_recv) {
  1229. /* exit immediately (and do not send any ACK) if the packet has
  1230. * not been enqueued correctly
  1231. */
  1232. if (!batadv_tp_handle_out_of_order(tp_vars, skb))
  1233. goto out;
  1234. /* send a duplicate ACK */
  1235. goto send_ack;
  1236. }
  1237. /* if everything was fine count the ACKed bytes */
  1238. packet_size = skb->len - sizeof(struct batadv_unicast_packet);
  1239. tp_vars->last_recv += packet_size;
  1240. /* check if this ordered message filled a gap.... */
  1241. batadv_tp_ack_unordered(tp_vars);
  1242. send_ack:
  1243. /* send the ACK. If the received packet was out of order, the ACK that
  1244. * is going to be sent is a duplicate (the sender will count them and
  1245. * possibly enter Fast Retransmit as soon as it has reached 3)
  1246. */
  1247. batadv_tp_send_ack(bat_priv, icmp->orig, tp_vars->last_recv,
  1248. icmp->timestamp, icmp->session, icmp->uid);
  1249. out:
  1250. if (likely(tp_vars))
  1251. batadv_tp_vars_put(tp_vars);
  1252. }
  1253. /**
  1254. * batadv_tp_meter_recv() - main TP Meter receiving function
  1255. * @bat_priv: the bat priv with all the soft interface information
  1256. * @skb: the buffer containing the received packet
  1257. */
  1258. void batadv_tp_meter_recv(struct batadv_priv *bat_priv, struct sk_buff *skb)
  1259. {
  1260. struct batadv_icmp_tp_packet *icmp;
  1261. icmp = (struct batadv_icmp_tp_packet *)skb->data;
  1262. switch (icmp->subtype) {
  1263. case BATADV_TP_MSG:
  1264. batadv_tp_recv_msg(bat_priv, skb);
  1265. break;
  1266. case BATADV_TP_ACK:
  1267. batadv_tp_recv_ack(bat_priv, skb);
  1268. break;
  1269. default:
  1270. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  1271. "Received unknown TP Metric packet type %u\n",
  1272. icmp->subtype);
  1273. }
  1274. consume_skb(skb);
  1275. }
  1276. /**
  1277. * batadv_tp_meter_init() - initialize global tp_meter structures
  1278. */
  1279. void __init batadv_tp_meter_init(void)
  1280. {
  1281. get_random_bytes(batadv_tp_prerandom, sizeof(batadv_tp_prerandom));
  1282. }