tp_meter.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  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 == NET_XMIT_SUCCESS)
  533. return 0;
  534. return BATADV_TP_REASON_CANT_SEND;
  535. }
  536. /**
  537. * batadv_tp_recv_ack - ACK receiving function
  538. * @bat_priv: the bat priv with all the soft interface information
  539. * @skb: the buffer containing the received packet
  540. *
  541. * Process a received TP ACK packet
  542. */
  543. static void batadv_tp_recv_ack(struct batadv_priv *bat_priv,
  544. const struct sk_buff *skb)
  545. {
  546. struct batadv_hard_iface *primary_if = NULL;
  547. struct batadv_orig_node *orig_node = NULL;
  548. const struct batadv_icmp_tp_packet *icmp;
  549. struct batadv_tp_vars *tp_vars;
  550. size_t packet_len, mss;
  551. u32 rtt, recv_ack, cwnd;
  552. unsigned char *dev_addr;
  553. packet_len = BATADV_TP_PLEN;
  554. mss = BATADV_TP_PLEN;
  555. packet_len += sizeof(struct batadv_unicast_packet);
  556. icmp = (struct batadv_icmp_tp_packet *)skb->data;
  557. /* find the tp_vars */
  558. tp_vars = batadv_tp_list_find_session(bat_priv, icmp->orig,
  559. icmp->session);
  560. if (unlikely(!tp_vars))
  561. return;
  562. if (unlikely(atomic_read(&tp_vars->sending) == 0))
  563. goto out;
  564. /* old ACK? silently drop it.. */
  565. if (batadv_seq_before(ntohl(icmp->seqno),
  566. (u32)atomic_read(&tp_vars->last_acked)))
  567. goto out;
  568. primary_if = batadv_primary_if_get_selected(bat_priv);
  569. if (unlikely(!primary_if))
  570. goto out;
  571. orig_node = batadv_orig_hash_find(bat_priv, icmp->orig);
  572. if (unlikely(!orig_node))
  573. goto out;
  574. /* update RTO with the new sampled RTT, if any */
  575. rtt = jiffies_to_msecs(jiffies) - ntohl(icmp->timestamp);
  576. if (icmp->timestamp && rtt)
  577. batadv_tp_update_rto(tp_vars, rtt);
  578. /* ACK for new data... reset the timer */
  579. batadv_tp_reset_sender_timer(tp_vars);
  580. recv_ack = ntohl(icmp->seqno);
  581. /* check if this ACK is a duplicate */
  582. if (atomic_read(&tp_vars->last_acked) == recv_ack) {
  583. atomic_inc(&tp_vars->dup_acks);
  584. if (atomic_read(&tp_vars->dup_acks) != 3)
  585. goto out;
  586. if (recv_ack >= tp_vars->recover)
  587. goto out;
  588. /* if this is the third duplicate ACK do Fast Retransmit */
  589. batadv_tp_send_msg(tp_vars, primary_if->net_dev->dev_addr,
  590. orig_node, recv_ack, packet_len,
  591. icmp->session, icmp->uid,
  592. jiffies_to_msecs(jiffies));
  593. spin_lock_bh(&tp_vars->cwnd_lock);
  594. /* Fast Recovery */
  595. tp_vars->fast_recovery = true;
  596. /* Set recover to the last outstanding seqno when Fast Recovery
  597. * is entered. RFC6582, Section 3.2, step 1
  598. */
  599. tp_vars->recover = tp_vars->last_sent;
  600. tp_vars->ss_threshold = tp_vars->cwnd >> 1;
  601. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  602. "Meter: Fast Recovery, (cur cwnd=%u) ss_thr=%u last_sent=%u recv_ack=%u\n",
  603. tp_vars->cwnd, tp_vars->ss_threshold,
  604. tp_vars->last_sent, recv_ack);
  605. tp_vars->cwnd = batadv_tp_cwnd(tp_vars->ss_threshold, 3 * mss,
  606. mss);
  607. tp_vars->dec_cwnd = 0;
  608. tp_vars->last_sent = recv_ack;
  609. spin_unlock_bh(&tp_vars->cwnd_lock);
  610. } else {
  611. /* count the acked data */
  612. atomic64_add(recv_ack - atomic_read(&tp_vars->last_acked),
  613. &tp_vars->tot_sent);
  614. /* reset the duplicate ACKs counter */
  615. atomic_set(&tp_vars->dup_acks, 0);
  616. if (tp_vars->fast_recovery) {
  617. /* partial ACK */
  618. if (batadv_seq_before(recv_ack, tp_vars->recover)) {
  619. /* this is another hole in the window. React
  620. * immediately as specified by NewReno (see
  621. * Section 3.2 of RFC6582 for details)
  622. */
  623. dev_addr = primary_if->net_dev->dev_addr;
  624. batadv_tp_send_msg(tp_vars, dev_addr,
  625. orig_node, recv_ack,
  626. packet_len, icmp->session,
  627. icmp->uid,
  628. jiffies_to_msecs(jiffies));
  629. tp_vars->cwnd = batadv_tp_cwnd(tp_vars->cwnd,
  630. mss, mss);
  631. } else {
  632. tp_vars->fast_recovery = false;
  633. /* set cwnd to the value of ss_threshold at the
  634. * moment that Fast Recovery was entered.
  635. * RFC6582, Section 3.2, step 3
  636. */
  637. cwnd = batadv_tp_cwnd(tp_vars->ss_threshold, 0,
  638. mss);
  639. tp_vars->cwnd = cwnd;
  640. }
  641. goto move_twnd;
  642. }
  643. if (recv_ack - atomic_read(&tp_vars->last_acked) >= mss)
  644. batadv_tp_update_cwnd(tp_vars, mss);
  645. move_twnd:
  646. /* move the Transmit Window */
  647. atomic_set(&tp_vars->last_acked, recv_ack);
  648. }
  649. wake_up(&tp_vars->more_bytes);
  650. out:
  651. if (likely(primary_if))
  652. batadv_hardif_put(primary_if);
  653. if (likely(orig_node))
  654. batadv_orig_node_put(orig_node);
  655. if (likely(tp_vars))
  656. batadv_tp_vars_put(tp_vars);
  657. }
  658. /**
  659. * batadv_tp_avail - check if congestion window is not full
  660. * @tp_vars: the private data of the current TP meter session
  661. * @payload_len: size of the payload of a single message
  662. *
  663. * Return: true when congestion window is not full, false otherwise
  664. */
  665. static bool batadv_tp_avail(struct batadv_tp_vars *tp_vars,
  666. size_t payload_len)
  667. {
  668. u32 win_left, win_limit;
  669. win_limit = atomic_read(&tp_vars->last_acked) + tp_vars->cwnd;
  670. win_left = win_limit - tp_vars->last_sent;
  671. return win_left >= payload_len;
  672. }
  673. /**
  674. * batadv_tp_wait_available - wait until congestion window becomes free or
  675. * timeout is reached
  676. * @tp_vars: the private data of the current TP meter session
  677. * @plen: size of the payload of a single message
  678. *
  679. * Return: 0 if the condition evaluated to false after the timeout elapsed,
  680. * 1 if the condition evaluated to true after the timeout elapsed, the
  681. * remaining jiffies (at least 1) if the condition evaluated to true before
  682. * the timeout elapsed, or -ERESTARTSYS if it was interrupted by a signal.
  683. */
  684. static int batadv_tp_wait_available(struct batadv_tp_vars *tp_vars, size_t plen)
  685. {
  686. int ret;
  687. ret = wait_event_interruptible_timeout(tp_vars->more_bytes,
  688. batadv_tp_avail(tp_vars, plen),
  689. HZ / 10);
  690. return ret;
  691. }
  692. /**
  693. * batadv_tp_send - main sending thread of a tp meter session
  694. * @arg: address of the related tp_vars
  695. *
  696. * Return: nothing, this function never returns
  697. */
  698. static int batadv_tp_send(void *arg)
  699. {
  700. struct batadv_tp_vars *tp_vars = arg;
  701. struct batadv_priv *bat_priv = tp_vars->bat_priv;
  702. struct batadv_hard_iface *primary_if = NULL;
  703. struct batadv_orig_node *orig_node = NULL;
  704. size_t payload_len, packet_len;
  705. int err = 0;
  706. if (unlikely(tp_vars->role != BATADV_TP_SENDER)) {
  707. err = BATADV_TP_REASON_DST_UNREACHABLE;
  708. tp_vars->reason = err;
  709. goto out;
  710. }
  711. orig_node = batadv_orig_hash_find(bat_priv, tp_vars->other_end);
  712. if (unlikely(!orig_node)) {
  713. err = BATADV_TP_REASON_DST_UNREACHABLE;
  714. tp_vars->reason = err;
  715. goto out;
  716. }
  717. primary_if = batadv_primary_if_get_selected(bat_priv);
  718. if (unlikely(!primary_if)) {
  719. err = BATADV_TP_REASON_DST_UNREACHABLE;
  720. tp_vars->reason = err;
  721. goto out;
  722. }
  723. /* assume that all the hard_interfaces have a correctly
  724. * configured MTU, so use the soft_iface MTU as MSS.
  725. * This might not be true and in that case the fragmentation
  726. * should be used.
  727. * Now, try to send the packet as it is
  728. */
  729. payload_len = BATADV_TP_PLEN;
  730. BUILD_BUG_ON(sizeof(struct batadv_icmp_tp_packet) > BATADV_TP_PLEN);
  731. batadv_tp_reset_sender_timer(tp_vars);
  732. /* queue the worker in charge of terminating the test */
  733. queue_delayed_work(batadv_event_workqueue, &tp_vars->finish_work,
  734. msecs_to_jiffies(tp_vars->test_length));
  735. while (atomic_read(&tp_vars->sending) != 0) {
  736. if (unlikely(!batadv_tp_avail(tp_vars, payload_len))) {
  737. batadv_tp_wait_available(tp_vars, payload_len);
  738. continue;
  739. }
  740. /* to emulate normal unicast traffic, add to the payload len
  741. * the size of the unicast header
  742. */
  743. packet_len = payload_len + sizeof(struct batadv_unicast_packet);
  744. err = batadv_tp_send_msg(tp_vars, primary_if->net_dev->dev_addr,
  745. orig_node, tp_vars->last_sent,
  746. packet_len,
  747. tp_vars->session, tp_vars->icmp_uid,
  748. jiffies_to_msecs(jiffies));
  749. /* something went wrong during the preparation/transmission */
  750. if (unlikely(err && err != BATADV_TP_REASON_CANT_SEND)) {
  751. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  752. "Meter: batadv_tp_send() cannot send packets (%d)\n",
  753. err);
  754. /* ensure nobody else tries to stop the thread now */
  755. if (atomic_dec_and_test(&tp_vars->sending))
  756. tp_vars->reason = err;
  757. break;
  758. }
  759. /* right-shift the TWND */
  760. if (!err)
  761. tp_vars->last_sent += payload_len;
  762. cond_resched();
  763. }
  764. out:
  765. if (likely(primary_if))
  766. batadv_hardif_put(primary_if);
  767. if (likely(orig_node))
  768. batadv_orig_node_put(orig_node);
  769. batadv_tp_sender_end(bat_priv, tp_vars);
  770. batadv_tp_sender_cleanup(bat_priv, tp_vars);
  771. batadv_tp_vars_put(tp_vars);
  772. do_exit(0);
  773. }
  774. /**
  775. * batadv_tp_start_kthread - start new thread which manages the tp meter sender
  776. * @tp_vars: the private data of the current TP meter session
  777. */
  778. static void batadv_tp_start_kthread(struct batadv_tp_vars *tp_vars)
  779. {
  780. struct task_struct *kthread;
  781. struct batadv_priv *bat_priv = tp_vars->bat_priv;
  782. u32 session_cookie;
  783. kref_get(&tp_vars->refcount);
  784. kthread = kthread_create(batadv_tp_send, tp_vars, "kbatadv_tp_meter");
  785. if (IS_ERR(kthread)) {
  786. session_cookie = batadv_tp_session_cookie(tp_vars->session,
  787. tp_vars->icmp_uid);
  788. pr_err("batadv: cannot create tp meter kthread\n");
  789. batadv_tp_batctl_error_notify(BATADV_TP_REASON_MEMORY_ERROR,
  790. tp_vars->other_end,
  791. bat_priv, session_cookie);
  792. /* drop reserved reference for kthread */
  793. batadv_tp_vars_put(tp_vars);
  794. /* cleanup of failed tp meter variables */
  795. batadv_tp_sender_cleanup(bat_priv, tp_vars);
  796. return;
  797. }
  798. wake_up_process(kthread);
  799. }
  800. /**
  801. * batadv_tp_start - start a new tp meter session
  802. * @bat_priv: the bat priv with all the soft interface information
  803. * @dst: the receiver MAC address
  804. * @test_length: test length in milliseconds
  805. * @cookie: session cookie
  806. */
  807. void batadv_tp_start(struct batadv_priv *bat_priv, const u8 *dst,
  808. u32 test_length, u32 *cookie)
  809. {
  810. struct batadv_tp_vars *tp_vars;
  811. u8 session_id[2];
  812. u8 icmp_uid;
  813. u32 session_cookie;
  814. get_random_bytes(session_id, sizeof(session_id));
  815. get_random_bytes(&icmp_uid, 1);
  816. session_cookie = batadv_tp_session_cookie(session_id, icmp_uid);
  817. *cookie = session_cookie;
  818. /* look for an already existing test towards this node */
  819. spin_lock_bh(&bat_priv->tp_list_lock);
  820. tp_vars = batadv_tp_list_find(bat_priv, dst);
  821. if (tp_vars) {
  822. spin_unlock_bh(&bat_priv->tp_list_lock);
  823. batadv_tp_vars_put(tp_vars);
  824. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  825. "Meter: test to or from the same node already ongoing, aborting\n");
  826. batadv_tp_batctl_error_notify(BATADV_TP_REASON_ALREADY_ONGOING,
  827. dst, bat_priv, session_cookie);
  828. return;
  829. }
  830. if (!atomic_add_unless(&bat_priv->tp_num, 1, BATADV_TP_MAX_NUM)) {
  831. spin_unlock_bh(&bat_priv->tp_list_lock);
  832. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  833. "Meter: too many ongoing sessions, aborting (SEND)\n");
  834. batadv_tp_batctl_error_notify(BATADV_TP_REASON_TOO_MANY, dst,
  835. bat_priv, session_cookie);
  836. return;
  837. }
  838. tp_vars = kmalloc(sizeof(*tp_vars), GFP_ATOMIC);
  839. if (!tp_vars) {
  840. spin_unlock_bh(&bat_priv->tp_list_lock);
  841. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  842. "Meter: batadv_tp_start cannot allocate list elements\n");
  843. batadv_tp_batctl_error_notify(BATADV_TP_REASON_MEMORY_ERROR,
  844. dst, bat_priv, session_cookie);
  845. return;
  846. }
  847. /* initialize tp_vars */
  848. ether_addr_copy(tp_vars->other_end, dst);
  849. kref_init(&tp_vars->refcount);
  850. tp_vars->role = BATADV_TP_SENDER;
  851. atomic_set(&tp_vars->sending, 1);
  852. memcpy(tp_vars->session, session_id, sizeof(session_id));
  853. tp_vars->icmp_uid = icmp_uid;
  854. tp_vars->last_sent = BATADV_TP_FIRST_SEQ;
  855. atomic_set(&tp_vars->last_acked, BATADV_TP_FIRST_SEQ);
  856. tp_vars->fast_recovery = false;
  857. tp_vars->recover = BATADV_TP_FIRST_SEQ;
  858. /* initialise the CWND to 3*MSS (Section 3.1 in RFC5681).
  859. * For batman-adv the MSS is the size of the payload received by the
  860. * soft_interface, hence its MTU
  861. */
  862. tp_vars->cwnd = BATADV_TP_PLEN * 3;
  863. /* at the beginning initialise the SS threshold to the biggest possible
  864. * window size, hence the AWND size
  865. */
  866. tp_vars->ss_threshold = BATADV_TP_AWND;
  867. /* RTO initial value is 3 seconds.
  868. * Details in Section 2.1 of RFC6298
  869. */
  870. tp_vars->rto = 1000;
  871. tp_vars->srtt = 0;
  872. tp_vars->rttvar = 0;
  873. atomic64_set(&tp_vars->tot_sent, 0);
  874. kref_get(&tp_vars->refcount);
  875. setup_timer(&tp_vars->timer, batadv_tp_sender_timeout,
  876. (unsigned long)tp_vars);
  877. tp_vars->bat_priv = bat_priv;
  878. tp_vars->start_time = jiffies;
  879. init_waitqueue_head(&tp_vars->more_bytes);
  880. spin_lock_init(&tp_vars->unacked_lock);
  881. INIT_LIST_HEAD(&tp_vars->unacked_list);
  882. spin_lock_init(&tp_vars->cwnd_lock);
  883. tp_vars->prerandom_offset = 0;
  884. spin_lock_init(&tp_vars->prerandom_lock);
  885. kref_get(&tp_vars->refcount);
  886. hlist_add_head_rcu(&tp_vars->list, &bat_priv->tp_list);
  887. spin_unlock_bh(&bat_priv->tp_list_lock);
  888. tp_vars->test_length = test_length;
  889. if (!tp_vars->test_length)
  890. tp_vars->test_length = BATADV_TP_DEF_TEST_LENGTH;
  891. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  892. "Meter: starting throughput meter towards %pM (length=%ums)\n",
  893. dst, test_length);
  894. /* init work item for finished tp tests */
  895. INIT_DELAYED_WORK(&tp_vars->finish_work, batadv_tp_sender_finish);
  896. /* start tp kthread. This way the write() call issued from userspace can
  897. * happily return and avoid to block
  898. */
  899. batadv_tp_start_kthread(tp_vars);
  900. /* don't return reference to new tp_vars */
  901. batadv_tp_vars_put(tp_vars);
  902. }
  903. /**
  904. * batadv_tp_stop - stop currently running tp meter session
  905. * @bat_priv: the bat priv with all the soft interface information
  906. * @dst: the receiver MAC address
  907. * @return_value: reason for tp meter session stop
  908. */
  909. void batadv_tp_stop(struct batadv_priv *bat_priv, const u8 *dst,
  910. u8 return_value)
  911. {
  912. struct batadv_orig_node *orig_node;
  913. struct batadv_tp_vars *tp_vars;
  914. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  915. "Meter: stopping test towards %pM\n", dst);
  916. orig_node = batadv_orig_hash_find(bat_priv, dst);
  917. if (!orig_node)
  918. return;
  919. tp_vars = batadv_tp_list_find(bat_priv, orig_node->orig);
  920. if (!tp_vars) {
  921. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  922. "Meter: trying to interrupt an already over connection\n");
  923. goto out;
  924. }
  925. batadv_tp_sender_shutdown(tp_vars, return_value);
  926. batadv_tp_vars_put(tp_vars);
  927. out:
  928. batadv_orig_node_put(orig_node);
  929. }
  930. /**
  931. * batadv_tp_reset_receiver_timer - reset the receiver shutdown timer
  932. * @tp_vars: the private data of the current TP meter session
  933. *
  934. * start the receiver shutdown timer or reset it if already started
  935. */
  936. static void batadv_tp_reset_receiver_timer(struct batadv_tp_vars *tp_vars)
  937. {
  938. mod_timer(&tp_vars->timer,
  939. jiffies + msecs_to_jiffies(BATADV_TP_RECV_TIMEOUT));
  940. }
  941. /**
  942. * batadv_tp_receiver_shutdown - stop a tp meter receiver when timeout is
  943. * reached without received ack
  944. * @arg: address of the related tp_vars
  945. */
  946. static void batadv_tp_receiver_shutdown(unsigned long arg)
  947. {
  948. struct batadv_tp_vars *tp_vars = (struct batadv_tp_vars *)arg;
  949. struct batadv_tp_unacked *un, *safe;
  950. struct batadv_priv *bat_priv;
  951. bat_priv = tp_vars->bat_priv;
  952. /* if there is recent activity rearm the timer */
  953. if (!batadv_has_timed_out(tp_vars->last_recv_time,
  954. BATADV_TP_RECV_TIMEOUT)) {
  955. /* reset the receiver shutdown timer */
  956. batadv_tp_reset_receiver_timer(tp_vars);
  957. return;
  958. }
  959. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  960. "Shutting down for inactivity (more than %dms) from %pM\n",
  961. BATADV_TP_RECV_TIMEOUT, tp_vars->other_end);
  962. spin_lock_bh(&tp_vars->bat_priv->tp_list_lock);
  963. hlist_del_rcu(&tp_vars->list);
  964. spin_unlock_bh(&tp_vars->bat_priv->tp_list_lock);
  965. /* drop list reference */
  966. batadv_tp_vars_put(tp_vars);
  967. atomic_dec(&bat_priv->tp_num);
  968. spin_lock_bh(&tp_vars->unacked_lock);
  969. list_for_each_entry_safe(un, safe, &tp_vars->unacked_list, list) {
  970. list_del(&un->list);
  971. kfree(un);
  972. }
  973. spin_unlock_bh(&tp_vars->unacked_lock);
  974. /* drop reference of timer */
  975. batadv_tp_vars_put(tp_vars);
  976. }
  977. /**
  978. * batadv_tp_send_ack - send an ACK packet
  979. * @bat_priv: the bat priv with all the soft interface information
  980. * @dst: the mac address of the destination originator
  981. * @seq: the sequence number to ACK
  982. * @timestamp: the timestamp to echo back in the ACK
  983. * @session: session identifier
  984. * @socket_index: local ICMP socket identifier
  985. *
  986. * Return: 0 on success, a positive integer representing the reason of the
  987. * failure otherwise
  988. */
  989. static int batadv_tp_send_ack(struct batadv_priv *bat_priv, const u8 *dst,
  990. u32 seq, __be32 timestamp, const u8 *session,
  991. int socket_index)
  992. {
  993. struct batadv_hard_iface *primary_if = NULL;
  994. struct batadv_orig_node *orig_node;
  995. struct batadv_icmp_tp_packet *icmp;
  996. struct sk_buff *skb;
  997. int r, ret;
  998. orig_node = batadv_orig_hash_find(bat_priv, dst);
  999. if (unlikely(!orig_node)) {
  1000. ret = BATADV_TP_REASON_DST_UNREACHABLE;
  1001. goto out;
  1002. }
  1003. primary_if = batadv_primary_if_get_selected(bat_priv);
  1004. if (unlikely(!primary_if)) {
  1005. ret = BATADV_TP_REASON_DST_UNREACHABLE;
  1006. goto out;
  1007. }
  1008. skb = netdev_alloc_skb_ip_align(NULL, sizeof(*icmp) + ETH_HLEN);
  1009. if (unlikely(!skb)) {
  1010. ret = BATADV_TP_REASON_MEMORY_ERROR;
  1011. goto out;
  1012. }
  1013. skb_reserve(skb, ETH_HLEN);
  1014. icmp = (struct batadv_icmp_tp_packet *)skb_put(skb, sizeof(*icmp));
  1015. icmp->packet_type = BATADV_ICMP;
  1016. icmp->version = BATADV_COMPAT_VERSION;
  1017. icmp->ttl = BATADV_TTL;
  1018. icmp->msg_type = BATADV_TP;
  1019. ether_addr_copy(icmp->dst, orig_node->orig);
  1020. ether_addr_copy(icmp->orig, primary_if->net_dev->dev_addr);
  1021. icmp->uid = socket_index;
  1022. icmp->subtype = BATADV_TP_ACK;
  1023. memcpy(icmp->session, session, sizeof(icmp->session));
  1024. icmp->seqno = htonl(seq);
  1025. icmp->timestamp = timestamp;
  1026. /* send the ack */
  1027. r = batadv_send_skb_to_orig(skb, orig_node, NULL);
  1028. if (unlikely(r < 0) || (r == NET_XMIT_DROP)) {
  1029. ret = BATADV_TP_REASON_DST_UNREACHABLE;
  1030. goto out;
  1031. }
  1032. ret = 0;
  1033. out:
  1034. if (likely(orig_node))
  1035. batadv_orig_node_put(orig_node);
  1036. if (likely(primary_if))
  1037. batadv_hardif_put(primary_if);
  1038. return ret;
  1039. }
  1040. /**
  1041. * batadv_tp_handle_out_of_order - store an out of order packet
  1042. * @tp_vars: the private data of the current TP meter session
  1043. * @skb: the buffer containing the received packet
  1044. *
  1045. * Store the out of order packet in the unacked list for late processing. This
  1046. * packets are kept in this list so that they can be ACKed at once as soon as
  1047. * all the previous packets have been received
  1048. *
  1049. * Return: true if the packed has been successfully processed, false otherwise
  1050. */
  1051. static bool batadv_tp_handle_out_of_order(struct batadv_tp_vars *tp_vars,
  1052. const struct sk_buff *skb)
  1053. {
  1054. const struct batadv_icmp_tp_packet *icmp;
  1055. struct batadv_tp_unacked *un, *new;
  1056. u32 payload_len;
  1057. bool added = false;
  1058. new = kmalloc(sizeof(*new), GFP_ATOMIC);
  1059. if (unlikely(!new))
  1060. return false;
  1061. icmp = (struct batadv_icmp_tp_packet *)skb->data;
  1062. new->seqno = ntohl(icmp->seqno);
  1063. payload_len = skb->len - sizeof(struct batadv_unicast_packet);
  1064. new->len = payload_len;
  1065. spin_lock_bh(&tp_vars->unacked_lock);
  1066. /* if the list is empty immediately attach this new object */
  1067. if (list_empty(&tp_vars->unacked_list)) {
  1068. list_add(&new->list, &tp_vars->unacked_list);
  1069. goto out;
  1070. }
  1071. /* otherwise loop over the list and either drop the packet because this
  1072. * is a duplicate or store it at the right position.
  1073. *
  1074. * The iteration is done in the reverse way because it is likely that
  1075. * the last received packet (the one being processed now) has a bigger
  1076. * seqno than all the others already stored.
  1077. */
  1078. list_for_each_entry_reverse(un, &tp_vars->unacked_list, list) {
  1079. /* check for duplicates */
  1080. if (new->seqno == un->seqno) {
  1081. if (new->len > un->len)
  1082. un->len = new->len;
  1083. kfree(new);
  1084. added = true;
  1085. break;
  1086. }
  1087. /* look for the right position */
  1088. if (batadv_seq_before(new->seqno, un->seqno))
  1089. continue;
  1090. /* as soon as an entry having a bigger seqno is found, the new
  1091. * one is attached _after_ it. In this way the list is kept in
  1092. * ascending order
  1093. */
  1094. list_add_tail(&new->list, &un->list);
  1095. added = true;
  1096. break;
  1097. }
  1098. /* received packet with smallest seqno out of order; add it to front */
  1099. if (!added)
  1100. list_add(&new->list, &tp_vars->unacked_list);
  1101. out:
  1102. spin_unlock_bh(&tp_vars->unacked_lock);
  1103. return true;
  1104. }
  1105. /**
  1106. * batadv_tp_ack_unordered - update number received bytes in current stream
  1107. * without gaps
  1108. * @tp_vars: the private data of the current TP meter session
  1109. */
  1110. static void batadv_tp_ack_unordered(struct batadv_tp_vars *tp_vars)
  1111. {
  1112. struct batadv_tp_unacked *un, *safe;
  1113. u32 to_ack;
  1114. /* go through the unacked packet list and possibly ACK them as
  1115. * well
  1116. */
  1117. spin_lock_bh(&tp_vars->unacked_lock);
  1118. list_for_each_entry_safe(un, safe, &tp_vars->unacked_list, list) {
  1119. /* the list is ordered, therefore it is possible to stop as soon
  1120. * there is a gap between the last acked seqno and the seqno of
  1121. * the packet under inspection
  1122. */
  1123. if (batadv_seq_before(tp_vars->last_recv, un->seqno))
  1124. break;
  1125. to_ack = un->seqno + un->len - tp_vars->last_recv;
  1126. if (batadv_seq_before(tp_vars->last_recv, un->seqno + un->len))
  1127. tp_vars->last_recv += to_ack;
  1128. list_del(&un->list);
  1129. kfree(un);
  1130. }
  1131. spin_unlock_bh(&tp_vars->unacked_lock);
  1132. }
  1133. /**
  1134. * batadv_tp_init_recv - return matching or create new receiver tp_vars
  1135. * @bat_priv: the bat priv with all the soft interface information
  1136. * @icmp: received icmp tp msg
  1137. *
  1138. * Return: corresponding tp_vars or NULL on errors
  1139. */
  1140. static struct batadv_tp_vars *
  1141. batadv_tp_init_recv(struct batadv_priv *bat_priv,
  1142. const struct batadv_icmp_tp_packet *icmp)
  1143. {
  1144. struct batadv_tp_vars *tp_vars;
  1145. spin_lock_bh(&bat_priv->tp_list_lock);
  1146. tp_vars = batadv_tp_list_find_session(bat_priv, icmp->orig,
  1147. icmp->session);
  1148. if (tp_vars)
  1149. goto out_unlock;
  1150. if (!atomic_add_unless(&bat_priv->tp_num, 1, BATADV_TP_MAX_NUM)) {
  1151. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  1152. "Meter: too many ongoing sessions, aborting (RECV)\n");
  1153. goto out_unlock;
  1154. }
  1155. tp_vars = kmalloc(sizeof(*tp_vars), GFP_ATOMIC);
  1156. if (!tp_vars)
  1157. goto out_unlock;
  1158. ether_addr_copy(tp_vars->other_end, icmp->orig);
  1159. tp_vars->role = BATADV_TP_RECEIVER;
  1160. memcpy(tp_vars->session, icmp->session, sizeof(tp_vars->session));
  1161. tp_vars->last_recv = BATADV_TP_FIRST_SEQ;
  1162. tp_vars->bat_priv = bat_priv;
  1163. kref_init(&tp_vars->refcount);
  1164. spin_lock_init(&tp_vars->unacked_lock);
  1165. INIT_LIST_HEAD(&tp_vars->unacked_list);
  1166. kref_get(&tp_vars->refcount);
  1167. hlist_add_head_rcu(&tp_vars->list, &bat_priv->tp_list);
  1168. kref_get(&tp_vars->refcount);
  1169. setup_timer(&tp_vars->timer, batadv_tp_receiver_shutdown,
  1170. (unsigned long)tp_vars);
  1171. batadv_tp_reset_receiver_timer(tp_vars);
  1172. out_unlock:
  1173. spin_unlock_bh(&bat_priv->tp_list_lock);
  1174. return tp_vars;
  1175. }
  1176. /**
  1177. * batadv_tp_recv_msg - process a single data message
  1178. * @bat_priv: the bat priv with all the soft interface information
  1179. * @skb: the buffer containing the received packet
  1180. *
  1181. * Process a received TP MSG packet
  1182. */
  1183. static void batadv_tp_recv_msg(struct batadv_priv *bat_priv,
  1184. const struct sk_buff *skb)
  1185. {
  1186. const struct batadv_icmp_tp_packet *icmp;
  1187. struct batadv_tp_vars *tp_vars;
  1188. size_t packet_size;
  1189. u32 seqno;
  1190. icmp = (struct batadv_icmp_tp_packet *)skb->data;
  1191. seqno = ntohl(icmp->seqno);
  1192. /* check if this is the first seqno. This means that if the
  1193. * first packet is lost, the tp meter does not work anymore!
  1194. */
  1195. if (seqno == BATADV_TP_FIRST_SEQ) {
  1196. tp_vars = batadv_tp_init_recv(bat_priv, icmp);
  1197. if (!tp_vars) {
  1198. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  1199. "Meter: seqno != BATADV_TP_FIRST_SEQ cannot initiate connection\n");
  1200. goto out;
  1201. }
  1202. } else {
  1203. tp_vars = batadv_tp_list_find_session(bat_priv, icmp->orig,
  1204. icmp->session);
  1205. if (!tp_vars) {
  1206. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  1207. "Unexpected packet from %pM!\n",
  1208. icmp->orig);
  1209. goto out;
  1210. }
  1211. }
  1212. if (unlikely(tp_vars->role != BATADV_TP_RECEIVER)) {
  1213. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  1214. "Meter: dropping packet: not expected (role=%u)\n",
  1215. tp_vars->role);
  1216. goto out;
  1217. }
  1218. tp_vars->last_recv_time = jiffies;
  1219. /* if the packet is a duplicate, it may be the case that an ACK has been
  1220. * lost. Resend the ACK
  1221. */
  1222. if (batadv_seq_before(seqno, tp_vars->last_recv))
  1223. goto send_ack;
  1224. /* if the packet is out of order enqueue it */
  1225. if (ntohl(icmp->seqno) != tp_vars->last_recv) {
  1226. /* exit immediately (and do not send any ACK) if the packet has
  1227. * not been enqueued correctly
  1228. */
  1229. if (!batadv_tp_handle_out_of_order(tp_vars, skb))
  1230. goto out;
  1231. /* send a duplicate ACK */
  1232. goto send_ack;
  1233. }
  1234. /* if everything was fine count the ACKed bytes */
  1235. packet_size = skb->len - sizeof(struct batadv_unicast_packet);
  1236. tp_vars->last_recv += packet_size;
  1237. /* check if this ordered message filled a gap.... */
  1238. batadv_tp_ack_unordered(tp_vars);
  1239. send_ack:
  1240. /* send the ACK. If the received packet was out of order, the ACK that
  1241. * is going to be sent is a duplicate (the sender will count them and
  1242. * possibly enter Fast Retransmit as soon as it has reached 3)
  1243. */
  1244. batadv_tp_send_ack(bat_priv, icmp->orig, tp_vars->last_recv,
  1245. icmp->timestamp, icmp->session, icmp->uid);
  1246. out:
  1247. if (likely(tp_vars))
  1248. batadv_tp_vars_put(tp_vars);
  1249. }
  1250. /**
  1251. * batadv_tp_meter_recv - main TP Meter receiving function
  1252. * @bat_priv: the bat priv with all the soft interface information
  1253. * @skb: the buffer containing the received packet
  1254. */
  1255. void batadv_tp_meter_recv(struct batadv_priv *bat_priv, struct sk_buff *skb)
  1256. {
  1257. struct batadv_icmp_tp_packet *icmp;
  1258. icmp = (struct batadv_icmp_tp_packet *)skb->data;
  1259. switch (icmp->subtype) {
  1260. case BATADV_TP_MSG:
  1261. batadv_tp_recv_msg(bat_priv, skb);
  1262. break;
  1263. case BATADV_TP_ACK:
  1264. batadv_tp_recv_ack(bat_priv, skb);
  1265. break;
  1266. default:
  1267. batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
  1268. "Received unknown TP Metric packet type %u\n",
  1269. icmp->subtype);
  1270. }
  1271. consume_skb(skb);
  1272. }
  1273. /**
  1274. * batadv_tp_meter_init - initialize global tp_meter structures
  1275. */
  1276. void batadv_tp_meter_init(void)
  1277. {
  1278. get_random_bytes(batadv_tp_prerandom, sizeof(batadv_tp_prerandom));
  1279. }