bat_v_elp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /* Copyright (C) 2011-2017 B.A.T.M.A.N. contributors:
  2. *
  3. * Linus Lüssing, Marek Lindner
  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 "bat_v_elp.h"
  18. #include "main.h"
  19. #include <linux/atomic.h>
  20. #include <linux/bitops.h>
  21. #include <linux/byteorder/generic.h>
  22. #include <linux/errno.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/ethtool.h>
  25. #include <linux/fs.h>
  26. #include <linux/if_ether.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/kernel.h>
  29. #include <linux/kref.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/nl80211.h>
  32. #include <linux/random.h>
  33. #include <linux/rculist.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/rtnetlink.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/stddef.h>
  38. #include <linux/string.h>
  39. #include <linux/types.h>
  40. #include <linux/workqueue.h>
  41. #include <net/cfg80211.h>
  42. #include "bat_algo.h"
  43. #include "bat_v_ogm.h"
  44. #include "hard-interface.h"
  45. #include "log.h"
  46. #include "originator.h"
  47. #include "packet.h"
  48. #include "routing.h"
  49. #include "send.h"
  50. /**
  51. * batadv_v_elp_start_timer - restart timer for ELP periodic work
  52. * @hard_iface: the interface for which the timer has to be reset
  53. */
  54. static void batadv_v_elp_start_timer(struct batadv_hard_iface *hard_iface)
  55. {
  56. unsigned int msecs;
  57. msecs = atomic_read(&hard_iface->bat_v.elp_interval) - BATADV_JITTER;
  58. msecs += prandom_u32() % (2 * BATADV_JITTER);
  59. queue_delayed_work(batadv_event_workqueue, &hard_iface->bat_v.elp_wq,
  60. msecs_to_jiffies(msecs));
  61. }
  62. /**
  63. * batadv_v_elp_get_throughput - get the throughput towards a neighbour
  64. * @neigh: the neighbour for which the throughput has to be obtained
  65. *
  66. * Return: The throughput towards the given neighbour in multiples of 100kpbs
  67. * (a value of '1' equals to 0.1Mbps, '10' equals 1Mbps, etc).
  68. */
  69. static u32 batadv_v_elp_get_throughput(struct batadv_hardif_neigh_node *neigh)
  70. {
  71. struct batadv_hard_iface *hard_iface = neigh->if_incoming;
  72. struct ethtool_link_ksettings link_settings;
  73. struct net_device *real_netdev;
  74. struct station_info sinfo;
  75. u32 throughput;
  76. int ret;
  77. /* if the user specified a customised value for this interface, then
  78. * return it directly
  79. */
  80. throughput = atomic_read(&hard_iface->bat_v.throughput_override);
  81. if (throughput != 0)
  82. return throughput;
  83. /* if this is a wireless device, then ask its throughput through
  84. * cfg80211 API
  85. */
  86. if (batadv_is_wifi_hardif(hard_iface)) {
  87. if (!batadv_is_cfg80211_hardif(hard_iface))
  88. /* unsupported WiFi driver version */
  89. goto default_throughput;
  90. real_netdev = batadv_get_real_netdev(hard_iface->net_dev);
  91. if (!real_netdev)
  92. goto default_throughput;
  93. ret = cfg80211_get_station(real_netdev, neigh->addr, &sinfo);
  94. dev_put(real_netdev);
  95. if (ret == -ENOENT) {
  96. /* Node is not associated anymore! It would be
  97. * possible to delete this neighbor. For now set
  98. * the throughput metric to 0.
  99. */
  100. return 0;
  101. }
  102. if (ret)
  103. goto default_throughput;
  104. if (!(sinfo.filled & BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT)))
  105. goto default_throughput;
  106. return sinfo.expected_throughput / 100;
  107. }
  108. /* if not a wifi interface, check if this device provides data via
  109. * ethtool (e.g. an Ethernet adapter)
  110. */
  111. memset(&link_settings, 0, sizeof(link_settings));
  112. rtnl_lock();
  113. ret = __ethtool_get_link_ksettings(hard_iface->net_dev, &link_settings);
  114. rtnl_unlock();
  115. if (ret == 0) {
  116. /* link characteristics might change over time */
  117. if (link_settings.base.duplex == DUPLEX_FULL)
  118. hard_iface->bat_v.flags |= BATADV_FULL_DUPLEX;
  119. else
  120. hard_iface->bat_v.flags &= ~BATADV_FULL_DUPLEX;
  121. throughput = link_settings.base.speed;
  122. if (throughput && (throughput != SPEED_UNKNOWN))
  123. return throughput * 10;
  124. }
  125. default_throughput:
  126. if (!(hard_iface->bat_v.flags & BATADV_WARNING_DEFAULT)) {
  127. batadv_info(hard_iface->soft_iface,
  128. "WiFi driver or ethtool info does not provide information about link speeds on interface %s, therefore defaulting to hardcoded throughput values of %u.%1u Mbps. Consider overriding the throughput manually or checking your driver.\n",
  129. hard_iface->net_dev->name,
  130. BATADV_THROUGHPUT_DEFAULT_VALUE / 10,
  131. BATADV_THROUGHPUT_DEFAULT_VALUE % 10);
  132. hard_iface->bat_v.flags |= BATADV_WARNING_DEFAULT;
  133. }
  134. /* if none of the above cases apply, return the base_throughput */
  135. return BATADV_THROUGHPUT_DEFAULT_VALUE;
  136. }
  137. /**
  138. * batadv_v_elp_throughput_metric_update - worker updating the throughput metric
  139. * of a single hop neighbour
  140. * @work: the work queue item
  141. */
  142. void batadv_v_elp_throughput_metric_update(struct work_struct *work)
  143. {
  144. struct batadv_hardif_neigh_node_bat_v *neigh_bat_v;
  145. struct batadv_hardif_neigh_node *neigh;
  146. neigh_bat_v = container_of(work, struct batadv_hardif_neigh_node_bat_v,
  147. metric_work);
  148. neigh = container_of(neigh_bat_v, struct batadv_hardif_neigh_node,
  149. bat_v);
  150. ewma_throughput_add(&neigh->bat_v.throughput,
  151. batadv_v_elp_get_throughput(neigh));
  152. /* decrement refcounter to balance increment performed before scheduling
  153. * this task
  154. */
  155. batadv_hardif_neigh_put(neigh);
  156. }
  157. /**
  158. * batadv_v_elp_wifi_neigh_probe - send link probing packets to a neighbour
  159. * @neigh: the neighbour to probe
  160. *
  161. * Sends a predefined number of unicast wifi packets to a given neighbour in
  162. * order to trigger the throughput estimation on this link by the RC algorithm.
  163. * Packets are sent only if there there is not enough payload unicast traffic
  164. * towards this neighbour..
  165. *
  166. * Return: True on success and false in case of error during skb preparation.
  167. */
  168. static bool
  169. batadv_v_elp_wifi_neigh_probe(struct batadv_hardif_neigh_node *neigh)
  170. {
  171. struct batadv_hard_iface *hard_iface = neigh->if_incoming;
  172. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  173. unsigned long last_tx_diff;
  174. struct sk_buff *skb;
  175. int probe_len, i;
  176. int elp_skb_len;
  177. /* this probing routine is for Wifi neighbours only */
  178. if (!batadv_is_wifi_hardif(hard_iface))
  179. return true;
  180. /* probe the neighbor only if no unicast packets have been sent
  181. * to it in the last 100 milliseconds: this is the rate control
  182. * algorithm sampling interval (minstrel). In this way, if not
  183. * enough traffic has been sent to the neighbor, batman-adv can
  184. * generate 2 probe packets and push the RC algorithm to perform
  185. * the sampling
  186. */
  187. last_tx_diff = jiffies_to_msecs(jiffies - neigh->bat_v.last_unicast_tx);
  188. if (last_tx_diff <= BATADV_ELP_PROBE_MAX_TX_DIFF)
  189. return true;
  190. probe_len = max_t(int, sizeof(struct batadv_elp_packet),
  191. BATADV_ELP_MIN_PROBE_SIZE);
  192. for (i = 0; i < BATADV_ELP_PROBES_PER_NODE; i++) {
  193. elp_skb_len = hard_iface->bat_v.elp_skb->len;
  194. skb = skb_copy_expand(hard_iface->bat_v.elp_skb, 0,
  195. probe_len - elp_skb_len,
  196. GFP_ATOMIC);
  197. if (!skb)
  198. return false;
  199. /* Tell the skb to get as big as the allocated space (we want
  200. * the packet to be exactly of that size to make the link
  201. * throughput estimation effective.
  202. */
  203. skb_put(skb, probe_len - hard_iface->bat_v.elp_skb->len);
  204. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  205. "Sending unicast (probe) ELP packet on interface %s to %pM\n",
  206. hard_iface->net_dev->name, neigh->addr);
  207. batadv_send_skb_packet(skb, hard_iface, neigh->addr);
  208. }
  209. return true;
  210. }
  211. /**
  212. * batadv_v_elp_periodic_work - ELP periodic task per interface
  213. * @work: work queue item
  214. *
  215. * Emits broadcast ELP message in regular intervals.
  216. */
  217. static void batadv_v_elp_periodic_work(struct work_struct *work)
  218. {
  219. struct batadv_hardif_neigh_node *hardif_neigh;
  220. struct batadv_hard_iface *hard_iface;
  221. struct batadv_hard_iface_bat_v *bat_v;
  222. struct batadv_elp_packet *elp_packet;
  223. struct batadv_priv *bat_priv;
  224. struct sk_buff *skb;
  225. u32 elp_interval;
  226. bat_v = container_of(work, struct batadv_hard_iface_bat_v, elp_wq.work);
  227. hard_iface = container_of(bat_v, struct batadv_hard_iface, bat_v);
  228. bat_priv = netdev_priv(hard_iface->soft_iface);
  229. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
  230. goto out;
  231. /* we are in the process of shutting this interface down */
  232. if ((hard_iface->if_status == BATADV_IF_NOT_IN_USE) ||
  233. (hard_iface->if_status == BATADV_IF_TO_BE_REMOVED))
  234. goto out;
  235. /* the interface was enabled but may not be ready yet */
  236. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  237. goto restart_timer;
  238. skb = skb_copy(hard_iface->bat_v.elp_skb, GFP_ATOMIC);
  239. if (!skb)
  240. goto restart_timer;
  241. elp_packet = (struct batadv_elp_packet *)skb->data;
  242. elp_packet->seqno = htonl(atomic_read(&hard_iface->bat_v.elp_seqno));
  243. elp_interval = atomic_read(&hard_iface->bat_v.elp_interval);
  244. elp_packet->elp_interval = htonl(elp_interval);
  245. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  246. "Sending broadcast ELP packet on interface %s, seqno %u\n",
  247. hard_iface->net_dev->name,
  248. atomic_read(&hard_iface->bat_v.elp_seqno));
  249. batadv_send_broadcast_skb(skb, hard_iface);
  250. atomic_inc(&hard_iface->bat_v.elp_seqno);
  251. /* The throughput metric is updated on each sent packet. This way, if a
  252. * node is dead and no longer sends packets, batman-adv is still able to
  253. * react timely to its death.
  254. *
  255. * The throughput metric is updated by following these steps:
  256. * 1) if the hard_iface is wifi => send a number of unicast ELPs for
  257. * probing/sampling to each neighbor
  258. * 2) update the throughput metric value of each neighbor (note that the
  259. * value retrieved in this step might be 100ms old because the
  260. * probing packets at point 1) could still be in the HW queue)
  261. */
  262. rcu_read_lock();
  263. hlist_for_each_entry_rcu(hardif_neigh, &hard_iface->neigh_list, list) {
  264. if (!batadv_v_elp_wifi_neigh_probe(hardif_neigh))
  265. /* if something goes wrong while probing, better to stop
  266. * sending packets immediately and reschedule the task
  267. */
  268. break;
  269. if (!kref_get_unless_zero(&hardif_neigh->refcount))
  270. continue;
  271. /* Reading the estimated throughput from cfg80211 is a task that
  272. * may sleep and that is not allowed in an rcu protected
  273. * context. Therefore schedule a task for that.
  274. */
  275. queue_work(batadv_event_workqueue,
  276. &hardif_neigh->bat_v.metric_work);
  277. }
  278. rcu_read_unlock();
  279. restart_timer:
  280. batadv_v_elp_start_timer(hard_iface);
  281. out:
  282. return;
  283. }
  284. /**
  285. * batadv_v_elp_iface_enable - setup the ELP interface private resources
  286. * @hard_iface: interface for which the data has to be prepared
  287. *
  288. * Return: 0 on success or a -ENOMEM in case of failure.
  289. */
  290. int batadv_v_elp_iface_enable(struct batadv_hard_iface *hard_iface)
  291. {
  292. struct batadv_elp_packet *elp_packet;
  293. unsigned char *elp_buff;
  294. u32 random_seqno;
  295. size_t size;
  296. int res = -ENOMEM;
  297. size = ETH_HLEN + NET_IP_ALIGN + BATADV_ELP_HLEN;
  298. hard_iface->bat_v.elp_skb = dev_alloc_skb(size);
  299. if (!hard_iface->bat_v.elp_skb)
  300. goto out;
  301. skb_reserve(hard_iface->bat_v.elp_skb, ETH_HLEN + NET_IP_ALIGN);
  302. elp_buff = skb_put_zero(hard_iface->bat_v.elp_skb, BATADV_ELP_HLEN);
  303. elp_packet = (struct batadv_elp_packet *)elp_buff;
  304. elp_packet->packet_type = BATADV_ELP;
  305. elp_packet->version = BATADV_COMPAT_VERSION;
  306. /* randomize initial seqno to avoid collision */
  307. get_random_bytes(&random_seqno, sizeof(random_seqno));
  308. atomic_set(&hard_iface->bat_v.elp_seqno, random_seqno);
  309. /* assume full-duplex by default */
  310. hard_iface->bat_v.flags |= BATADV_FULL_DUPLEX;
  311. /* warn the user (again) if there is no throughput data is available */
  312. hard_iface->bat_v.flags &= ~BATADV_WARNING_DEFAULT;
  313. if (batadv_is_wifi_hardif(hard_iface))
  314. hard_iface->bat_v.flags &= ~BATADV_FULL_DUPLEX;
  315. INIT_DELAYED_WORK(&hard_iface->bat_v.elp_wq,
  316. batadv_v_elp_periodic_work);
  317. batadv_v_elp_start_timer(hard_iface);
  318. res = 0;
  319. out:
  320. return res;
  321. }
  322. /**
  323. * batadv_v_elp_iface_disable - release ELP interface private resources
  324. * @hard_iface: interface for which the resources have to be released
  325. */
  326. void batadv_v_elp_iface_disable(struct batadv_hard_iface *hard_iface)
  327. {
  328. cancel_delayed_work_sync(&hard_iface->bat_v.elp_wq);
  329. dev_kfree_skb(hard_iface->bat_v.elp_skb);
  330. hard_iface->bat_v.elp_skb = NULL;
  331. }
  332. /**
  333. * batadv_v_elp_iface_activate - update the ELP buffer belonging to the given
  334. * hard-interface
  335. * @primary_iface: the new primary interface
  336. * @hard_iface: interface holding the to-be-updated buffer
  337. */
  338. void batadv_v_elp_iface_activate(struct batadv_hard_iface *primary_iface,
  339. struct batadv_hard_iface *hard_iface)
  340. {
  341. struct batadv_elp_packet *elp_packet;
  342. struct sk_buff *skb;
  343. if (!hard_iface->bat_v.elp_skb)
  344. return;
  345. skb = hard_iface->bat_v.elp_skb;
  346. elp_packet = (struct batadv_elp_packet *)skb->data;
  347. ether_addr_copy(elp_packet->orig,
  348. primary_iface->net_dev->dev_addr);
  349. }
  350. /**
  351. * batadv_v_elp_primary_iface_set - change internal data to reflect the new
  352. * primary interface
  353. * @primary_iface: the new primary interface
  354. */
  355. void batadv_v_elp_primary_iface_set(struct batadv_hard_iface *primary_iface)
  356. {
  357. struct batadv_hard_iface *hard_iface;
  358. /* update orig field of every elp iface belonging to this mesh */
  359. rcu_read_lock();
  360. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  361. if (primary_iface->soft_iface != hard_iface->soft_iface)
  362. continue;
  363. batadv_v_elp_iface_activate(primary_iface, hard_iface);
  364. }
  365. rcu_read_unlock();
  366. }
  367. /**
  368. * batadv_v_elp_neigh_update - update an ELP neighbour node
  369. * @bat_priv: the bat priv with all the soft interface information
  370. * @neigh_addr: the neighbour interface address
  371. * @if_incoming: the interface the packet was received through
  372. * @elp_packet: the received ELP packet
  373. *
  374. * Updates the ELP neighbour node state with the data received within the new
  375. * ELP packet.
  376. */
  377. static void batadv_v_elp_neigh_update(struct batadv_priv *bat_priv,
  378. u8 *neigh_addr,
  379. struct batadv_hard_iface *if_incoming,
  380. struct batadv_elp_packet *elp_packet)
  381. {
  382. struct batadv_neigh_node *neigh;
  383. struct batadv_orig_node *orig_neigh;
  384. struct batadv_hardif_neigh_node *hardif_neigh;
  385. s32 seqno_diff;
  386. s32 elp_latest_seqno;
  387. orig_neigh = batadv_v_ogm_orig_get(bat_priv, elp_packet->orig);
  388. if (!orig_neigh)
  389. return;
  390. neigh = batadv_neigh_node_get_or_create(orig_neigh,
  391. if_incoming, neigh_addr);
  392. if (!neigh)
  393. goto orig_free;
  394. hardif_neigh = batadv_hardif_neigh_get(if_incoming, neigh_addr);
  395. if (!hardif_neigh)
  396. goto neigh_free;
  397. elp_latest_seqno = hardif_neigh->bat_v.elp_latest_seqno;
  398. seqno_diff = ntohl(elp_packet->seqno) - elp_latest_seqno;
  399. /* known or older sequence numbers are ignored. However always adopt
  400. * if the router seems to have been restarted.
  401. */
  402. if (seqno_diff < 1 && seqno_diff > -BATADV_ELP_MAX_AGE)
  403. goto hardif_free;
  404. neigh->last_seen = jiffies;
  405. hardif_neigh->last_seen = jiffies;
  406. hardif_neigh->bat_v.elp_latest_seqno = ntohl(elp_packet->seqno);
  407. hardif_neigh->bat_v.elp_interval = ntohl(elp_packet->elp_interval);
  408. hardif_free:
  409. if (hardif_neigh)
  410. batadv_hardif_neigh_put(hardif_neigh);
  411. neigh_free:
  412. if (neigh)
  413. batadv_neigh_node_put(neigh);
  414. orig_free:
  415. if (orig_neigh)
  416. batadv_orig_node_put(orig_neigh);
  417. }
  418. /**
  419. * batadv_v_elp_packet_recv - main ELP packet handler
  420. * @skb: the received packet
  421. * @if_incoming: the interface this packet was received through
  422. *
  423. * Return: NET_RX_SUCCESS and consumes the skb if the packet was peoperly
  424. * processed or NET_RX_DROP in case of failure.
  425. */
  426. int batadv_v_elp_packet_recv(struct sk_buff *skb,
  427. struct batadv_hard_iface *if_incoming)
  428. {
  429. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  430. struct batadv_elp_packet *elp_packet;
  431. struct batadv_hard_iface *primary_if;
  432. struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
  433. bool res;
  434. int ret = NET_RX_DROP;
  435. res = batadv_check_management_packet(skb, if_incoming, BATADV_ELP_HLEN);
  436. if (!res)
  437. goto free_skb;
  438. if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
  439. goto free_skb;
  440. /* did we receive a B.A.T.M.A.N. V ELP packet on an interface
  441. * that does not have B.A.T.M.A.N. V ELP enabled ?
  442. */
  443. if (strcmp(bat_priv->algo_ops->name, "BATMAN_V") != 0)
  444. goto free_skb;
  445. elp_packet = (struct batadv_elp_packet *)skb->data;
  446. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  447. "Received ELP packet from %pM seqno %u ORIG: %pM\n",
  448. ethhdr->h_source, ntohl(elp_packet->seqno),
  449. elp_packet->orig);
  450. primary_if = batadv_primary_if_get_selected(bat_priv);
  451. if (!primary_if)
  452. goto free_skb;
  453. batadv_v_elp_neigh_update(bat_priv, ethhdr->h_source, if_incoming,
  454. elp_packet);
  455. ret = NET_RX_SUCCESS;
  456. batadv_hardif_put(primary_if);
  457. free_skb:
  458. if (ret == NET_RX_SUCCESS)
  459. consume_skb(skb);
  460. else
  461. kfree_skb(skb);
  462. return ret;
  463. }