bat_v_elp.c 16 KB

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