tp_meter.c 43 KB

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