tp_meter.c 43 KB

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