tp_meter.c 42 KB

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