Hi,
this series contains some major cleanup patches (at the beginning of the series) and some minor cleanups at the end of the series. The behavior of batman should not be influenced by this series as these patches are only transformations to make the code more readable and maintainable. If it does influence behavior something is wrong with this series. The series was tested on ARM SoC with mwifiex driver.
Major changes (Patch 1-12): - Compiling debugfs.c only when CONFIG_DEBUG_FS is selected. This reduces the amount of unnecessary code that is executed. At the moment all calls to debugfs functions will result in NOOPs. However there is some more code that we simply don't need without DEBUG_FS. - tvlv is separated from the large main.c file into its own tvlv.c. I don't see a reason to have this set of functions for tvlv inside the main.c file. - The hashtable implementation now has accessor functions to avoid direct access on the private hashtable structure. This should improve the hashtable interface and create proper encapsulation.
Minor changes (Patch 13-31): - Removing unnecessary return value variables - Fixing some comments - Reordering functions to increase readability - Coding style fixes - Declare boolean return types as bool - Add missing includes
Best Regards,
Markus
Markus Pargmann (31): batman-adv: debugfs, avoid compiling for !DEBUG_FS batman-adv: Separate logging header batman-adv: iv_ogm, Reduce code duplication batman-adv: iv_ogm, divide and round for ring buffer avg batman-adv: init, Add some error handling batman-adv: tvlv realloc, move error handling into if block batman-adv: split tvlv into a seperate file batman-adv: hash, remove function implementations from header batman-adv: hash, Add helper functions batman-adv: hash, replace direct hash structure accesses batman-adv: hash, make struct hashtable private batman-adv: hash, add used linux headers batman-adv: Makefile, Sort alphabetically batman-adv: iv_ogm_iface_enable, direct return values batman-adv: iv_ogm_aggr_packet, bool return value batman-adv: iv_ogm_send_to_if, declare char* as const batman-adv: iv_ogm_can_aggregate, code readability batman-adv: iv_ogm_orig_update, remove unnecessary brackets batman-adv: iv_ogm_aggregate_new, simplify error handling batman-adv: iv_ogm_queue_add, Simplify expressions batman-adv: iv_ogm_orig_update, style, add missin brackets batman-adv: iv_ogm, Fix dup_status comment batman-adv: iv_ogm, fix coding style batman-adv: iv_ogm, fix comment function name batman-adv: types, Fix comment on bcast_own batman-adv: main, Convert is_my_mac() to bool batman-adv: main, batadv_compare_eth return bool batman-adv: Remove unnecessary ret variable batman-adv: Remove unnecessary ret variable in algo_register batman-adv: packet.h, add some missing includes batman-adv: types.h, add missing include
Makefile.kbuild | 5 +- bat_iv_ogm.c | 247 +++++++++---------- bitarray.c | 1 + bridge_loop_avoidance.c | 58 +++-- debugfs.c | 9 +- debugfs.h | 39 +++ distributed-arp-table.c | 28 ++- gateway_client.c | 1 + gateway_common.c | 2 + hard-interface.c | 1 + hash.c | 163 +++++++++++++ hash.h | 164 ++----------- icmp_socket.c | 1 + log.h | 82 +++++++ main.c | 626 +++--------------------------------------------- main.h | 101 +------- multicast.c | 1 + network-coding.c | 38 +-- originator.c | 29 +-- originator.h | 4 +- packet.h | 3 + routing.c | 2 + send.c | 1 + sysfs.c | 1 + translation-table.c | 90 ++++--- tvlv.c | 592 +++++++++++++++++++++++++++++++++++++++++++++ tvlv.h | 62 +++++ types.h | 8 +- 28 files changed, 1266 insertions(+), 1093 deletions(-) create mode 100644 log.h create mode 100644 tvlv.c create mode 100644 tvlv.h
Normally the debugfs framework will return error pointer with -ENODEV for function calls when DEBUG_FS is not set.
batman does not notice this error code and continues trying to create debugfs files and executes more code. We can avoid this code execution by disabling compiling debugfs.c when DEBUG_FS is not set.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- Makefile.kbuild | 2 +- debugfs.c | 8 -------- debugfs.h | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 9 deletions(-)
diff --git a/Makefile.kbuild b/Makefile.kbuild index eb7d8c0388e4..96fa99b3a409 100644 --- a/Makefile.kbuild +++ b/Makefile.kbuild @@ -20,7 +20,7 @@ obj-$(CONFIG_BATMAN_ADV) += batman-adv.o batman-adv-y += bat_iv_ogm.o batman-adv-y += bitarray.o batman-adv-$(CONFIG_BATMAN_ADV_BLA) += bridge_loop_avoidance.o -batman-adv-y += debugfs.o +batman-adv-$(CONFIG_DEBUG_FS) += debugfs.o batman-adv-$(CONFIG_BATMAN_ADV_DAT) += distributed-arp-table.o batman-adv-y += fragmentation.o batman-adv-y += gateway_client.o diff --git a/debugfs.c b/debugfs.c index d9b9a8eb17fb..026a25ce364c 100644 --- a/debugfs.c +++ b/debugfs.c @@ -483,11 +483,7 @@ rem_attr: debugfs_remove_recursive(hard_iface->debug_dir); hard_iface->debug_dir = NULL; out: -#ifdef CONFIG_DEBUG_FS return -ENOMEM; -#else - return 0; -#endif /* CONFIG_DEBUG_FS */ }
/** @@ -542,11 +538,7 @@ rem_attr: debugfs_remove_recursive(bat_priv->debug_dir); bat_priv->debug_dir = NULL; out: -#ifdef CONFIG_DEBUG_FS return -ENOMEM; -#else - return 0; -#endif /* CONFIG_DEBUG_FS */ }
void batadv_debugfs_del_meshif(struct net_device *dev) diff --git a/debugfs.h b/debugfs.h index 37c4d6ddd04d..5db336a6ef57 100644 --- a/debugfs.h +++ b/debugfs.h @@ -20,6 +20,8 @@
#define BATADV_DEBUGFS_SUBDIR "batman_adv"
+#if IS_ENABLED(CONFIG_DEBUG_FS) + void batadv_debugfs_init(void); void batadv_debugfs_destroy(void); int batadv_debugfs_add_meshif(struct net_device *dev); @@ -27,4 +29,36 @@ void batadv_debugfs_del_meshif(struct net_device *dev); int batadv_debugfs_add_hardif(struct batadv_hard_iface *hard_iface); void batadv_debugfs_del_hardif(struct batadv_hard_iface *hard_iface);
+#else + +static inline void batadv_debugfs_init(void) +{ +} + +static inline void batadv_debugfs_destroy(void) +{ +} + +static inline int batadv_debugfs_add_meshif(struct net_device *dev) +{ + return 0; +} + +static inline void batadv_debugfs_del_meshif(struct net_device *dev) +{ +} + +static inline int batadv_debugfs_add_hardif( + struct batadv_hard_iface *hard_iface) +{ + return 0; +} + +static inline void batadv_debugfs_del_hardif( + struct batadv_hard_iface *hard_iface) +{ +} + +#endif + #endif /* _NET_BATMAN_ADV_DEBUGFS_H_ */
This patch creates a separate header file for logging related functions and definitions.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- bat_iv_ogm.c | 1 + bitarray.c | 1 + bridge_loop_avoidance.c | 1 + debugfs.c | 1 + debugfs.h | 5 +++ distributed-arp-table.c | 1 + gateway_client.c | 1 + gateway_common.c | 1 + hard-interface.c | 1 + icmp_socket.c | 1 + log.h | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ main.c | 1 + main.h | 62 ------------------------------------- network-coding.c | 1 + originator.c | 1 + routing.c | 1 + send.c | 1 + sysfs.c | 1 + translation-table.c | 1 + 19 files changed, 103 insertions(+), 62 deletions(-) create mode 100644 log.h
diff --git a/bat_iv_ogm.c b/bat_iv_ogm.c index 00e00e09b000..2d064a71613f 100644 --- a/bat_iv_ogm.c +++ b/bat_iv_ogm.c @@ -25,6 +25,7 @@ #include "send.h" #include "bat_algo.h" #include "network-coding.h" +#include "log.h"
/** * enum batadv_dup_status - duplicate status diff --git a/bitarray.c b/bitarray.c index e3da07a64026..3fbaf540d56d 100644 --- a/bitarray.c +++ b/bitarray.c @@ -17,6 +17,7 @@
#include "main.h" #include "bitarray.h" +#include "log.h"
#include <linux/bitops.h>
diff --git a/bridge_loop_avoidance.c b/bridge_loop_avoidance.c index 4fc6cab7ed46..d93288377f80 100644 --- a/bridge_loop_avoidance.c +++ b/bridge_loop_avoidance.c @@ -22,6 +22,7 @@ #include "bridge_loop_avoidance.h" #include "translation-table.h" #include "send.h" +#include "log.h"
#include <linux/etherdevice.h> #include <linux/crc16.h> diff --git a/debugfs.c b/debugfs.c index 026a25ce364c..3f21e855a13d 100644 --- a/debugfs.c +++ b/debugfs.c @@ -30,6 +30,7 @@ #include "bridge_loop_avoidance.h" #include "distributed-arp-table.h" #include "network-coding.h" +#include "log.h"
static struct dentry *batadv_debugfs;
diff --git a/debugfs.h b/debugfs.h index 5db336a6ef57..1fda474defe8 100644 --- a/debugfs.h +++ b/debugfs.h @@ -20,6 +20,11 @@
#define BATADV_DEBUGFS_SUBDIR "batman_adv"
+#ifdef CONFIG_BATMAN_ADV_DEBUG +int batadv_debug_log(struct batadv_priv *bat_priv, const char *fmt, ...) +__printf(2, 3); +#endif + #if IS_ENABLED(CONFIG_DEBUG_FS)
void batadv_debugfs_init(void); diff --git a/distributed-arp-table.c b/distributed-arp-table.c index aad022dd15df..e2f0677a5a01 100644 --- a/distributed-arp-table.c +++ b/distributed-arp-table.c @@ -28,6 +28,7 @@ #include "send.h" #include "types.h" #include "translation-table.h" +#include "log.h"
static void batadv_dat_purge(struct work_struct *work);
diff --git a/gateway_client.c b/gateway_client.c index 27649e85f3f6..d4aa578c78f9 100644 --- a/gateway_client.c +++ b/gateway_client.c @@ -23,6 +23,7 @@ #include "originator.h" #include "translation-table.h" #include "routing.h" +#include "log.h" #include <linux/ip.h> #include <linux/ipv6.h> #include <linux/udp.h> diff --git a/gateway_common.c b/gateway_common.c index 6f5e621f220a..cbef7fd97971 100644 --- a/gateway_common.c +++ b/gateway_common.c @@ -18,6 +18,7 @@ #include "main.h" #include "gateway_common.h" #include "gateway_client.h" +#include "log.h"
/** * batadv_parse_gw_bandwidth - parse supplied string buffer to extract download diff --git a/hard-interface.c b/hard-interface.c index fbda6b54baff..5138f11babb4 100644 --- a/hard-interface.c +++ b/hard-interface.c @@ -28,6 +28,7 @@ #include "hash.h" #include "bridge_loop_avoidance.h" #include "gateway_client.h" +#include "log.h"
#include <linux/if_arp.h> #include <linux/if_ether.h> diff --git a/icmp_socket.c b/icmp_socket.c index 161ef8f17d2e..520b2f4739d1 100644 --- a/icmp_socket.c +++ b/icmp_socket.c @@ -23,6 +23,7 @@ #include "hash.h" #include "originator.h" #include "hard-interface.h" +#include "log.h"
static struct batadv_socket_client *batadv_socket_client_hash[256];
diff --git a/log.h b/log.h new file mode 100644 index 000000000000..05b0b9d15883 --- /dev/null +++ b/log.h @@ -0,0 +1,82 @@ +/* Copyright (C) 2007-2014 B.A.T.M.A.N. contributors: + * + * Marek Lindner, Simon Wunderlich + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see http://www.gnu.org/licenses/. + */ + +#ifndef _NET_BATMAN_ADV_LOG_H_ +#define _NET_BATMAN_ADV_LOG_H_ + +#include "debugfs.h" + +/** + * enum batadv_dbg_level - available log levels + * @BATADV_DBG_BATMAN: OGM and TQ computations related messages + * @BATADV_DBG_ROUTES: route added / changed / deleted + * @BATADV_DBG_TT: translation table messages + * @BATADV_DBG_BLA: bridge loop avoidance messages + * @BATADV_DBG_DAT: ARP snooping and DAT related messages + * @BATADV_DBG_NC: network coding related messages + * @BATADV_DBG_ALL: the union of all the above log levels + */ +enum batadv_dbg_level { + BATADV_DBG_BATMAN = BIT(0), + BATADV_DBG_ROUTES = BIT(1), + BATADV_DBG_TT = BIT(2), + BATADV_DBG_BLA = BIT(3), + BATADV_DBG_DAT = BIT(4), + BATADV_DBG_NC = BIT(5), + BATADV_DBG_ALL = 63, +}; + +#ifdef CONFIG_BATMAN_ADV_DEBUG +/* possibly ratelimited debug output */ +#define _batadv_dbg(type, bat_priv, ratelimited, fmt, arg...) \ + do { \ + if (atomic_read(&bat_priv->log_level) & type && \ + (!ratelimited || net_ratelimit())) \ + batadv_debug_log(bat_priv, fmt, ## arg);\ + } \ + while (0) +#else /* !CONFIG_BATMAN_ADV_DEBUG */ +__printf(4, 5) +static inline void _batadv_dbg(int type __always_unused, + struct batadv_priv *bat_priv __always_unused, + int ratelimited __always_unused, + const char *fmt __always_unused, ...) +{ +} +#endif + +#define batadv_dbg(type, bat_priv, arg...) \ + _batadv_dbg(type, bat_priv, 0, ## arg) +#define batadv_dbg_ratelimited(type, bat_priv, arg...) \ + _batadv_dbg(type, bat_priv, 1, ## arg) + +#define batadv_info(net_dev, fmt, arg...) \ + do { \ + struct net_device *_netdev = (net_dev); \ + struct batadv_priv *_batpriv = netdev_priv(_netdev); \ + batadv_dbg(BATADV_DBG_ALL, _batpriv, fmt, ## arg); \ + pr_info("%s: " fmt, _netdev->name, ## arg); \ + } while (0) +#define batadv_err(net_dev, fmt, arg...) \ + do { \ + struct net_device *_netdev = (net_dev); \ + struct batadv_priv *_batpriv = netdev_priv(_netdev); \ + batadv_dbg(BATADV_DBG_ALL, _batpriv, fmt, ## arg); \ + pr_err("%s: " fmt, _netdev->name, ## arg); \ + } while (0) + +#endif /* _NET_BATMAN_ADV_LOG_H_ */ diff --git a/main.c b/main.c index 3bcd847a8f9f..a9e09e852c4b 100644 --- a/main.c +++ b/main.c @@ -40,6 +40,7 @@ #include "bat_algo.h" #include "network-coding.h" #include "fragmentation.h" +#include "log.h"
/* List manipulations on hardif_list have to be rtnl_lock()'ed, * list traversals just rcu-locked diff --git a/main.h b/main.h index 52cb0072bfc8..1cd4ebdbe060 100644 --- a/main.h +++ b/main.h @@ -214,68 +214,6 @@ int batadv_algo_select(struct batadv_priv *bat_priv, char *name); int batadv_algo_seq_print_text(struct seq_file *seq, void *offset); __be32 batadv_skb_crc32(struct sk_buff *skb, u8 *payload_ptr);
-/** - * enum batadv_dbg_level - available log levels - * @BATADV_DBG_BATMAN: OGM and TQ computations related messages - * @BATADV_DBG_ROUTES: route added / changed / deleted - * @BATADV_DBG_TT: translation table messages - * @BATADV_DBG_BLA: bridge loop avoidance messages - * @BATADV_DBG_DAT: ARP snooping and DAT related messages - * @BATADV_DBG_NC: network coding related messages - * @BATADV_DBG_ALL: the union of all the above log levels - */ -enum batadv_dbg_level { - BATADV_DBG_BATMAN = BIT(0), - BATADV_DBG_ROUTES = BIT(1), - BATADV_DBG_TT = BIT(2), - BATADV_DBG_BLA = BIT(3), - BATADV_DBG_DAT = BIT(4), - BATADV_DBG_NC = BIT(5), - BATADV_DBG_ALL = 63, -}; - -#ifdef CONFIG_BATMAN_ADV_DEBUG -int batadv_debug_log(struct batadv_priv *bat_priv, const char *fmt, ...) -__printf(2, 3); - -/* possibly ratelimited debug output */ -#define _batadv_dbg(type, bat_priv, ratelimited, fmt, arg...) \ - do { \ - if (atomic_read(&bat_priv->log_level) & type && \ - (!ratelimited || net_ratelimit())) \ - batadv_debug_log(bat_priv, fmt, ## arg);\ - } \ - while (0) -#else /* !CONFIG_BATMAN_ADV_DEBUG */ -__printf(4, 5) -static inline void _batadv_dbg(int type __always_unused, - struct batadv_priv *bat_priv __always_unused, - int ratelimited __always_unused, - const char *fmt __always_unused, ...) -{ -} -#endif - -#define batadv_dbg(type, bat_priv, arg...) \ - _batadv_dbg(type, bat_priv, 0, ## arg) -#define batadv_dbg_ratelimited(type, bat_priv, arg...) \ - _batadv_dbg(type, bat_priv, 1, ## arg) - -#define batadv_info(net_dev, fmt, arg...) \ - do { \ - struct net_device *_netdev = (net_dev); \ - struct batadv_priv *_batpriv = netdev_priv(_netdev); \ - batadv_dbg(BATADV_DBG_ALL, _batpriv, fmt, ## arg); \ - pr_info("%s: " fmt, _netdev->name, ## arg); \ - } while (0) -#define batadv_err(net_dev, fmt, arg...) \ - do { \ - struct net_device *_netdev = (net_dev); \ - struct batadv_priv *_batpriv = netdev_priv(_netdev); \ - batadv_dbg(BATADV_DBG_ALL, _batpriv, fmt, ## arg); \ - pr_err("%s: " fmt, _netdev->name, ## arg); \ - } while (0) - /* returns 1 if they are the same ethernet addr * * note: can't use ether_addr_equal() as it requires aligned memory diff --git a/network-coding.c b/network-coding.c index 127cc4d7380a..93190fc25f76 100644 --- a/network-coding.c +++ b/network-coding.c @@ -24,6 +24,7 @@ #include "originator.h" #include "hard-interface.h" #include "routing.h" +#include "log.h"
static struct lock_class_key batadv_nc_coding_hash_lock_class_key; static struct lock_class_key batadv_nc_decoding_hash_lock_class_key; diff --git a/originator.c b/originator.c index 6c2cb2da9865..3386ae54ede5 100644 --- a/originator.c +++ b/originator.c @@ -28,6 +28,7 @@ #include "network-coding.h" #include "fragmentation.h" #include "multicast.h" +#include "log.h"
/* hash class keys */ static struct lock_class_key batadv_orig_hash_lock_class_key; diff --git a/routing.c b/routing.c index 139d2f65728e..07205591b05f 100644 --- a/routing.c +++ b/routing.c @@ -27,6 +27,7 @@ #include "distributed-arp-table.h" #include "network-coding.h" #include "fragmentation.h" +#include "log.h"
#include <linux/if_vlan.h>
diff --git a/send.c b/send.c index d27161e1088d..043ee89596be 100644 --- a/send.c +++ b/send.c @@ -28,6 +28,7 @@ #include "network-coding.h" #include "fragmentation.h" #include "multicast.h" +#include "log.h"
static void batadv_send_outstanding_bcast_packet(struct work_struct *work);
diff --git a/sysfs.c b/sysfs.c index a63c3ebfbf53..6e39cdef1e1f 100644 --- a/sysfs.c +++ b/sysfs.c @@ -25,6 +25,7 @@ #include "soft-interface.h" #include "gateway_common.h" #include "gateway_client.h" +#include "log.h"
static struct net_device *batadv_kobj_to_netdev(struct kobject *obj) { diff --git a/translation-table.c b/translation-table.c index 84e6f01b734f..be75a2ac543e 100644 --- a/translation-table.c +++ b/translation-table.c @@ -25,6 +25,7 @@ #include "routing.h" #include "bridge_loop_avoidance.h" #include "multicast.h" +#include "log.h"
#include <linux/crc32c.h>
Hi Markus,
On 2014-12-02 12:16, Markus Pargmann wrote:
This patch creates a separate header file for logging related functions and definitions.
This description is quite obvious from looking at the patch. Can you please extend it, so that it is clear *why* the change is needed? This wasn't clear to me...
Thanks, Martin
The difference between tq1 and tq2 are calculated the same way in two separate functions.
This patch moves the common code to a seperate function 'batadv_iv_ogm_neigh_diff' which handles everything necessary. The other two functions can then handle errors and use the difference directly.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- bat_iv_ogm.c | 80 +++++++++++++++++++++++++++++++----------------------------- 1 file changed, 41 insertions(+), 39 deletions(-)
diff --git a/bat_iv_ogm.c b/bat_iv_ogm.c index 2d064a71613f..1458ecfa66b8 100644 --- a/bat_iv_ogm.c +++ b/bat_iv_ogm.c @@ -1858,36 +1858,27 @@ next: seq_puts(seq, "No batman nodes in range ...\n"); }
-/** - * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors - * @neigh1: the first neighbor object of the comparison - * @if_outgoing1: outgoing interface for the first neighbor - * @neigh2: the second neighbor object of the comparison - * @if_outgoing2: outgoing interface for the second neighbor - * - * Returns a value less, equal to or greater than 0 if the metric via neigh1 is - * lower, the same as or higher than the metric via neigh2 - */ -static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1, - struct batadv_hard_iface *if_outgoing1, - struct batadv_neigh_node *neigh2, - struct batadv_hard_iface *if_outgoing2) +static int batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1, + struct batadv_hard_iface *if_outgoing1, + struct batadv_neigh_node *neigh2, + struct batadv_hard_iface *if_outgoing2, + int *diff) { struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo; uint8_t tq1, tq2; - int diff; + int ret;
neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1); neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
if (!neigh1_ifinfo || !neigh2_ifinfo) { - diff = 0; + ret = -EINVAL; goto out; }
tq1 = neigh1_ifinfo->bat_iv.tq_avg; tq2 = neigh2_ifinfo->bat_iv.tq_avg; - diff = tq1 - tq2; + *diff = (int)tq1 - (int)tq2;
out: if (neigh1_ifinfo) @@ -1895,6 +1886,32 @@ out: if (neigh2_ifinfo) batadv_neigh_ifinfo_free_ref(neigh2_ifinfo);
+ return ret; +} + +/** + * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors + * @neigh1: the first neighbor object of the comparison + * @if_outgoing1: outgoing interface for the first neighbor + * @neigh2: the second neighbor object of the comparison + * @if_outgoing2: outgoing interface for the second neighbor + * + * Returns a value less, equal to or greater than 0 if the metric via neigh1 is + * lower, the same as or higher than the metric via neigh2 + */ +static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1, + struct batadv_hard_iface *if_outgoing1, + struct batadv_neigh_node *neigh2, + struct batadv_hard_iface *if_outgoing2) +{ + int ret; + int diff; + + ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2, + if_outgoing2, &diff); + if (ret) + return 0; + return diff; }
@@ -1915,30 +1932,15 @@ batadv_iv_ogm_neigh_is_eob(struct batadv_neigh_node *neigh1, struct batadv_neigh_node *neigh2, struct batadv_hard_iface *if_outgoing2) { - struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo; - uint8_t tq1, tq2; - bool ret; - - neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1); - neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2); - - /* we can't say that the metric is better */ - if (!neigh1_ifinfo || !neigh2_ifinfo) { - ret = false; - goto out; - } - - tq1 = neigh1_ifinfo->bat_iv.tq_avg; - tq2 = neigh2_ifinfo->bat_iv.tq_avg; - ret = (tq1 - tq2) > -BATADV_TQ_SIMILARITY_THRESHOLD; + int ret; + int diff;
-out: - if (neigh1_ifinfo) - batadv_neigh_ifinfo_free_ref(neigh1_ifinfo); - if (neigh2_ifinfo) - batadv_neigh_ifinfo_free_ref(neigh2_ifinfo); + ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2, + if_outgoing2, &diff); + if (ret) + return false;
- return ret; + return diff > -BATADV_TQ_SIMILARITY_THRESHOLD; }
static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
Instead of the normal division which looses precision, use a division with rounding.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- bat_iv_ogm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/bat_iv_ogm.c b/bat_iv_ogm.c index 1458ecfa66b8..10eada270015 100644 --- a/bat_iv_ogm.c +++ b/bat_iv_ogm.c @@ -82,7 +82,7 @@ static uint8_t batadv_ring_buffer_avg(const uint8_t lq_recv[]) if (count == 0) return 0;
- return (uint8_t)(sum / count); + return (uint8_t)DIV_ROUND_CLOSEST(sum, count); }
/**
On Tuesday 02 December 2014 12:16:22 Markus Pargmann wrote:
Instead of the normal division which looses precision, use a division with rounding.
Signed-off-by: Markus Pargmann mpa@pengutronix.de
Why do we need to have more precise rounding here? In doubt, we should rather always round down to avoid any spurious routing loops - the loop free property depends on monotonicity after all, and therefore its better to always round down.
I'm not convinced that this change is safe in that regard, if you think it is please explain further.
bat_iv_ogm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/bat_iv_ogm.c b/bat_iv_ogm.c index 1458ecfa66b8..10eada270015 100644 --- a/bat_iv_ogm.c +++ b/bat_iv_ogm.c @@ -82,7 +82,7 @@ static uint8_t batadv_ring_buffer_avg(const uint8_t lq_recv[]) if (count == 0) return 0;
- return (uint8_t)(sum / count);
- return (uint8_t)DIV_ROUND_CLOSEST(sum, count);
}
/**
This patch adds some error handling for the main init function. It checks the return values of all the function calls that provide return values.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- main.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-)
diff --git a/main.c b/main.c index a9e09e852c4b..65021ea567e8 100644 --- a/main.c +++ b/main.c @@ -59,29 +59,47 @@ static void batadv_recv_handler_init(void);
static int __init batadv_init(void) { + int ret; + INIT_LIST_HEAD(&batadv_hardif_list); INIT_HLIST_HEAD(&batadv_algo_list);
batadv_recv_handler_init();
- batadv_iv_init(); - batadv_nc_init(); + ret = batadv_iv_init(); + if (ret) + return ret; + + ret = batadv_nc_init(); + if (ret) + return ret;
batadv_event_workqueue = create_singlethread_workqueue("bat_events"); - if (!batadv_event_workqueue) return -ENOMEM;
batadv_socket_init(); batadv_debugfs_init();
- register_netdevice_notifier(&batadv_hard_if_notifier); - rtnl_link_register(&batadv_link_ops); + ret = register_netdevice_notifier(&batadv_hard_if_notifier); + if (ret) + goto err_netdev_notifier; + + ret = rtnl_link_register(&batadv_link_ops); + if (ret) + goto err_link_register;
pr_info("B.A.T.M.A.N. advanced %s (compatibility version %i) loaded\n", BATADV_SOURCE_VERSION, BATADV_COMPAT_VERSION);
return 0; + +err_link_register: + unregister_netdevice_notifier(&batadv_hard_if_notifier); +err_netdev_notifier: + batadv_debugfs_destroy(); + + return ret; }
static void __exit batadv_exit(void)
Instead of hiding the normal function flow inside an if block, we should just put the error handling into the if block.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- main.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/main.c b/main.c index 65021ea567e8..00a1107109e8 100644 --- a/main.c +++ b/main.c @@ -838,15 +838,15 @@ static bool batadv_tvlv_realloc_packet_buff(unsigned char **packet_buff, new_buff = kmalloc(min_packet_len + additional_packet_len, GFP_ATOMIC);
/* keep old buffer if kmalloc should fail */ - if (new_buff) { - memcpy(new_buff, *packet_buff, min_packet_len); - kfree(*packet_buff); - *packet_buff = new_buff; - *packet_buff_len = min_packet_len + additional_packet_len; - return true; - } + if (!new_buff) + return false; + + memcpy(new_buff, *packet_buff, min_packet_len); + kfree(*packet_buff); + *packet_buff = new_buff; + *packet_buff_len = min_packet_len + additional_packet_len;
- return false; + return true; }
/**
The main file includes a lot of helper functions. tvlv is a seperate set of functions so we should split them into a seperate file. There is no need for tvlv to be implemented in main.c.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- Makefile.kbuild | 1 + bat_iv_ogm.c | 1 + distributed-arp-table.c | 1 + gateway_common.c | 1 + main.c | 568 ---------------------------------------------- main.h | 35 --- multicast.c | 1 + network-coding.c | 1 + routing.c | 1 + translation-table.c | 1 + tvlv.c | 592 ++++++++++++++++++++++++++++++++++++++++++++++++ tvlv.h | 62 +++++ 12 files changed, 662 insertions(+), 603 deletions(-) create mode 100644 tvlv.c create mode 100644 tvlv.h
diff --git a/Makefile.kbuild b/Makefile.kbuild index 96fa99b3a409..a8c34baddd63 100644 --- a/Makefile.kbuild +++ b/Makefile.kbuild @@ -36,4 +36,5 @@ batman-adv-y += send.o batman-adv-y += soft-interface.o batman-adv-y += sysfs.o batman-adv-y += translation-table.o +batman-adv-y += tvlv.o batman-adv-$(CONFIG_BATMAN_ADV_MCAST) += multicast.o diff --git a/bat_iv_ogm.c b/bat_iv_ogm.c index 10eada270015..20295c5e5121 100644 --- a/bat_iv_ogm.c +++ b/bat_iv_ogm.c @@ -23,6 +23,7 @@ #include "gateway_client.h" #include "hard-interface.h" #include "send.h" +#include "tvlv.h" #include "bat_algo.h" #include "network-coding.h" #include "log.h" diff --git a/distributed-arp-table.c b/distributed-arp-table.c index e2f0677a5a01..e3deff63d393 100644 --- a/distributed-arp-table.c +++ b/distributed-arp-table.c @@ -29,6 +29,7 @@ #include "types.h" #include "translation-table.h" #include "log.h" +#include "tvlv.h"
static void batadv_dat_purge(struct work_struct *work);
diff --git a/gateway_common.c b/gateway_common.c index cbef7fd97971..c2dfcc03ddfa 100644 --- a/gateway_common.c +++ b/gateway_common.c @@ -19,6 +19,7 @@ #include "gateway_common.h" #include "gateway_client.h" #include "log.h" +#include "tvlv.h"
/** * batadv_parse_gw_bandwidth - parse supplied string buffer to extract download diff --git a/main.c b/main.c index 00a1107109e8..cff31bb9bb14 100644 --- a/main.c +++ b/main.c @@ -623,574 +623,6 @@ __be32 batadv_skb_crc32(struct sk_buff *skb, u8 *payload_ptr) }
/** - * batadv_tvlv_handler_free_ref - decrement the tvlv handler refcounter and - * possibly free it - * @tvlv_handler: the tvlv handler to free - */ -static void -batadv_tvlv_handler_free_ref(struct batadv_tvlv_handler *tvlv_handler) -{ - if (atomic_dec_and_test(&tvlv_handler->refcount)) - kfree_rcu(tvlv_handler, rcu); -} - -/** - * batadv_tvlv_handler_get - retrieve tvlv handler from the tvlv handler list - * based on the provided type and version (both need to match) - * @bat_priv: the bat priv with all the soft interface information - * @type: tvlv handler type to look for - * @version: tvlv handler version to look for - * - * Returns tvlv handler if found or NULL otherwise. - */ -static struct batadv_tvlv_handler -*batadv_tvlv_handler_get(struct batadv_priv *bat_priv, - uint8_t type, uint8_t version) -{ - struct batadv_tvlv_handler *tvlv_handler_tmp, *tvlv_handler = NULL; - - rcu_read_lock(); - hlist_for_each_entry_rcu(tvlv_handler_tmp, - &bat_priv->tvlv.handler_list, list) { - if (tvlv_handler_tmp->type != type) - continue; - - if (tvlv_handler_tmp->version != version) - continue; - - if (!atomic_inc_not_zero(&tvlv_handler_tmp->refcount)) - continue; - - tvlv_handler = tvlv_handler_tmp; - break; - } - rcu_read_unlock(); - - return tvlv_handler; -} - -/** - * batadv_tvlv_container_free_ref - decrement the tvlv container refcounter and - * possibly free it - * @tvlv: the tvlv container to free - */ -static void batadv_tvlv_container_free_ref(struct batadv_tvlv_container *tvlv) -{ - if (atomic_dec_and_test(&tvlv->refcount)) - kfree(tvlv); -} - -/** - * batadv_tvlv_container_get - retrieve tvlv container from the tvlv container - * list based on the provided type and version (both need to match) - * @bat_priv: the bat priv with all the soft interface information - * @type: tvlv container type to look for - * @version: tvlv container version to look for - * - * Has to be called with the appropriate locks being acquired - * (tvlv.container_list_lock). - * - * Returns tvlv container if found or NULL otherwise. - */ -static struct batadv_tvlv_container -*batadv_tvlv_container_get(struct batadv_priv *bat_priv, - uint8_t type, uint8_t version) -{ - struct batadv_tvlv_container *tvlv_tmp, *tvlv = NULL; - - hlist_for_each_entry(tvlv_tmp, &bat_priv->tvlv.container_list, list) { - if (tvlv_tmp->tvlv_hdr.type != type) - continue; - - if (tvlv_tmp->tvlv_hdr.version != version) - continue; - - if (!atomic_inc_not_zero(&tvlv_tmp->refcount)) - continue; - - tvlv = tvlv_tmp; - break; - } - - return tvlv; -} - -/** - * batadv_tvlv_container_list_size - calculate the size of the tvlv container - * list entries - * @bat_priv: the bat priv with all the soft interface information - * - * Has to be called with the appropriate locks being acquired - * (tvlv.container_list_lock). - * - * Returns size of all currently registered tvlv containers in bytes. - */ -static uint16_t batadv_tvlv_container_list_size(struct batadv_priv *bat_priv) -{ - struct batadv_tvlv_container *tvlv; - uint16_t tvlv_len = 0; - - hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) { - tvlv_len += sizeof(struct batadv_tvlv_hdr); - tvlv_len += ntohs(tvlv->tvlv_hdr.len); - } - - return tvlv_len; -} - -/** - * batadv_tvlv_container_remove - remove tvlv container from the tvlv container - * list - * @tvlv: the to be removed tvlv container - * - * Has to be called with the appropriate locks being acquired - * (tvlv.container_list_lock). - */ -static void batadv_tvlv_container_remove(struct batadv_tvlv_container *tvlv) -{ - if (!tvlv) - return; - - hlist_del(&tvlv->list); - - /* first call to decrement the counter, second call to free */ - batadv_tvlv_container_free_ref(tvlv); - batadv_tvlv_container_free_ref(tvlv); -} - -/** - * batadv_tvlv_container_unregister - unregister tvlv container based on the - * provided type and version (both need to match) - * @bat_priv: the bat priv with all the soft interface information - * @type: tvlv container type to unregister - * @version: tvlv container type to unregister - */ -void batadv_tvlv_container_unregister(struct batadv_priv *bat_priv, - uint8_t type, uint8_t version) -{ - struct batadv_tvlv_container *tvlv; - - spin_lock_bh(&bat_priv->tvlv.container_list_lock); - tvlv = batadv_tvlv_container_get(bat_priv, type, version); - batadv_tvlv_container_remove(tvlv); - spin_unlock_bh(&bat_priv->tvlv.container_list_lock); -} - -/** - * batadv_tvlv_container_register - register tvlv type, version and content - * to be propagated with each (primary interface) OGM - * @bat_priv: the bat priv with all the soft interface information - * @type: tvlv container type - * @version: tvlv container version - * @tvlv_value: tvlv container content - * @tvlv_value_len: tvlv container content length - * - * If a container of the same type and version was already registered the new - * content is going to replace the old one. - */ -void batadv_tvlv_container_register(struct batadv_priv *bat_priv, - uint8_t type, uint8_t version, - void *tvlv_value, uint16_t tvlv_value_len) -{ - struct batadv_tvlv_container *tvlv_old, *tvlv_new; - - if (!tvlv_value) - tvlv_value_len = 0; - - tvlv_new = kzalloc(sizeof(*tvlv_new) + tvlv_value_len, GFP_ATOMIC); - if (!tvlv_new) - return; - - tvlv_new->tvlv_hdr.version = version; - tvlv_new->tvlv_hdr.type = type; - tvlv_new->tvlv_hdr.len = htons(tvlv_value_len); - - memcpy(tvlv_new + 1, tvlv_value, ntohs(tvlv_new->tvlv_hdr.len)); - INIT_HLIST_NODE(&tvlv_new->list); - atomic_set(&tvlv_new->refcount, 1); - - spin_lock_bh(&bat_priv->tvlv.container_list_lock); - tvlv_old = batadv_tvlv_container_get(bat_priv, type, version); - batadv_tvlv_container_remove(tvlv_old); - hlist_add_head(&tvlv_new->list, &bat_priv->tvlv.container_list); - spin_unlock_bh(&bat_priv->tvlv.container_list_lock); -} - -/** - * batadv_tvlv_realloc_packet_buff - reallocate packet buffer to accomodate - * requested packet size - * @packet_buff: packet buffer - * @packet_buff_len: packet buffer size - * @min_packet_len: requested packet minimum size - * @additional_packet_len: requested additional packet size on top of minimum - * size - * - * Returns true of the packet buffer could be changed to the requested size, - * false otherwise. - */ -static bool batadv_tvlv_realloc_packet_buff(unsigned char **packet_buff, - int *packet_buff_len, - int min_packet_len, - int additional_packet_len) -{ - unsigned char *new_buff; - - new_buff = kmalloc(min_packet_len + additional_packet_len, GFP_ATOMIC); - - /* keep old buffer if kmalloc should fail */ - if (!new_buff) - return false; - - memcpy(new_buff, *packet_buff, min_packet_len); - kfree(*packet_buff); - *packet_buff = new_buff; - *packet_buff_len = min_packet_len + additional_packet_len; - - return true; -} - -/** - * batadv_tvlv_container_ogm_append - append tvlv container content to given - * OGM packet buffer - * @bat_priv: the bat priv with all the soft interface information - * @packet_buff: ogm packet buffer - * @packet_buff_len: ogm packet buffer size including ogm header and tvlv - * content - * @packet_min_len: ogm header size to be preserved for the OGM itself - * - * The ogm packet might be enlarged or shrunk depending on the current size - * and the size of the to-be-appended tvlv containers. - * - * Returns size of all appended tvlv containers in bytes. - */ -uint16_t batadv_tvlv_container_ogm_append(struct batadv_priv *bat_priv, - unsigned char **packet_buff, - int *packet_buff_len, - int packet_min_len) -{ - struct batadv_tvlv_container *tvlv; - struct batadv_tvlv_hdr *tvlv_hdr; - uint16_t tvlv_value_len; - void *tvlv_value; - bool ret; - - spin_lock_bh(&bat_priv->tvlv.container_list_lock); - tvlv_value_len = batadv_tvlv_container_list_size(bat_priv); - - ret = batadv_tvlv_realloc_packet_buff(packet_buff, packet_buff_len, - packet_min_len, tvlv_value_len); - - if (!ret) - goto end; - - if (!tvlv_value_len) - goto end; - - tvlv_value = (*packet_buff) + packet_min_len; - - hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) { - tvlv_hdr = tvlv_value; - tvlv_hdr->type = tvlv->tvlv_hdr.type; - tvlv_hdr->version = tvlv->tvlv_hdr.version; - tvlv_hdr->len = tvlv->tvlv_hdr.len; - tvlv_value = tvlv_hdr + 1; - memcpy(tvlv_value, tvlv + 1, ntohs(tvlv->tvlv_hdr.len)); - tvlv_value = (uint8_t *)tvlv_value + ntohs(tvlv->tvlv_hdr.len); - } - -end: - spin_unlock_bh(&bat_priv->tvlv.container_list_lock); - return tvlv_value_len; -} - -/** - * batadv_tvlv_call_handler - parse the given tvlv buffer to call the - * appropriate handlers - * @bat_priv: the bat priv with all the soft interface information - * @tvlv_handler: tvlv callback function handling the tvlv content - * @ogm_source: flag indicating wether the tvlv is an ogm or a unicast packet - * @orig_node: orig node emitting the ogm packet - * @src: source mac address of the unicast packet - * @dst: destination mac address of the unicast packet - * @tvlv_value: tvlv content - * @tvlv_value_len: tvlv content length - * - * Returns success if handler was not found or the return value of the handler - * callback. - */ -static int batadv_tvlv_call_handler(struct batadv_priv *bat_priv, - struct batadv_tvlv_handler *tvlv_handler, - bool ogm_source, - struct batadv_orig_node *orig_node, - uint8_t *src, uint8_t *dst, - void *tvlv_value, uint16_t tvlv_value_len) -{ - if (!tvlv_handler) - return NET_RX_SUCCESS; - - if (ogm_source) { - if (!tvlv_handler->ogm_handler) - return NET_RX_SUCCESS; - - if (!orig_node) - return NET_RX_SUCCESS; - - tvlv_handler->ogm_handler(bat_priv, orig_node, - BATADV_NO_FLAGS, - tvlv_value, tvlv_value_len); - tvlv_handler->flags |= BATADV_TVLV_HANDLER_OGM_CALLED; - } else { - if (!src) - return NET_RX_SUCCESS; - - if (!dst) - return NET_RX_SUCCESS; - - if (!tvlv_handler->unicast_handler) - return NET_RX_SUCCESS; - - return tvlv_handler->unicast_handler(bat_priv, src, - dst, tvlv_value, - tvlv_value_len); - } - - return NET_RX_SUCCESS; -} - -/** - * batadv_tvlv_containers_process - parse the given tvlv buffer to call the - * appropriate handlers - * @bat_priv: the bat priv with all the soft interface information - * @ogm_source: flag indicating wether the tvlv is an ogm or a unicast packet - * @orig_node: orig node emitting the ogm packet - * @src: source mac address of the unicast packet - * @dst: destination mac address of the unicast packet - * @tvlv_value: tvlv content - * @tvlv_value_len: tvlv content length - * - * Returns success when processing an OGM or the return value of all called - * handler callbacks. - */ -int batadv_tvlv_containers_process(struct batadv_priv *bat_priv, - bool ogm_source, - struct batadv_orig_node *orig_node, - uint8_t *src, uint8_t *dst, - void *tvlv_value, uint16_t tvlv_value_len) -{ - struct batadv_tvlv_handler *tvlv_handler; - struct batadv_tvlv_hdr *tvlv_hdr; - uint16_t tvlv_value_cont_len; - uint8_t cifnotfound = BATADV_TVLV_HANDLER_OGM_CIFNOTFND; - int ret = NET_RX_SUCCESS; - - while (tvlv_value_len >= sizeof(*tvlv_hdr)) { - tvlv_hdr = tvlv_value; - tvlv_value_cont_len = ntohs(tvlv_hdr->len); - tvlv_value = tvlv_hdr + 1; - tvlv_value_len -= sizeof(*tvlv_hdr); - - if (tvlv_value_cont_len > tvlv_value_len) - break; - - tvlv_handler = batadv_tvlv_handler_get(bat_priv, - tvlv_hdr->type, - tvlv_hdr->version); - - ret |= batadv_tvlv_call_handler(bat_priv, tvlv_handler, - ogm_source, orig_node, - src, dst, tvlv_value, - tvlv_value_cont_len); - if (tvlv_handler) - batadv_tvlv_handler_free_ref(tvlv_handler); - tvlv_value = (uint8_t *)tvlv_value + tvlv_value_cont_len; - tvlv_value_len -= tvlv_value_cont_len; - } - - if (!ogm_source) - return ret; - - rcu_read_lock(); - hlist_for_each_entry_rcu(tvlv_handler, - &bat_priv->tvlv.handler_list, list) { - if ((tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) && - !(tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CALLED)) - tvlv_handler->ogm_handler(bat_priv, orig_node, - cifnotfound, NULL, 0); - - tvlv_handler->flags &= ~BATADV_TVLV_HANDLER_OGM_CALLED; - } - rcu_read_unlock(); - - return NET_RX_SUCCESS; -} - -/** - * batadv_tvlv_ogm_receive - process an incoming ogm and call the appropriate - * handlers - * @bat_priv: the bat priv with all the soft interface information - * @batadv_ogm_packet: ogm packet containing the tvlv containers - * @orig_node: orig node emitting the ogm packet - */ -void batadv_tvlv_ogm_receive(struct batadv_priv *bat_priv, - struct batadv_ogm_packet *batadv_ogm_packet, - struct batadv_orig_node *orig_node) -{ - void *tvlv_value; - uint16_t tvlv_value_len; - - if (!batadv_ogm_packet) - return; - - tvlv_value_len = ntohs(batadv_ogm_packet->tvlv_len); - if (!tvlv_value_len) - return; - - tvlv_value = batadv_ogm_packet + 1; - - batadv_tvlv_containers_process(bat_priv, true, orig_node, NULL, NULL, - tvlv_value, tvlv_value_len); -} - -/** - * batadv_tvlv_handler_register - register tvlv handler based on the provided - * type and version (both need to match) for ogm tvlv payload and/or unicast - * payload - * @bat_priv: the bat priv with all the soft interface information - * @optr: ogm tvlv handler callback function. This function receives the orig - * node, flags and the tvlv content as argument to process. - * @uptr: unicast tvlv handler callback function. This function receives the - * source & destination of the unicast packet as well as the tvlv content - * to process. - * @type: tvlv handler type to be registered - * @version: tvlv handler version to be registered - * @flags: flags to enable or disable TVLV API behavior - */ -void batadv_tvlv_handler_register(struct batadv_priv *bat_priv, - void (*optr)(struct batadv_priv *bat_priv, - struct batadv_orig_node *orig, - uint8_t flags, - void *tvlv_value, - uint16_t tvlv_value_len), - int (*uptr)(struct batadv_priv *bat_priv, - uint8_t *src, uint8_t *dst, - void *tvlv_value, - uint16_t tvlv_value_len), - uint8_t type, uint8_t version, uint8_t flags) -{ - struct batadv_tvlv_handler *tvlv_handler; - - tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version); - if (tvlv_handler) { - batadv_tvlv_handler_free_ref(tvlv_handler); - return; - } - - tvlv_handler = kzalloc(sizeof(*tvlv_handler), GFP_ATOMIC); - if (!tvlv_handler) - return; - - tvlv_handler->ogm_handler = optr; - tvlv_handler->unicast_handler = uptr; - tvlv_handler->type = type; - tvlv_handler->version = version; - tvlv_handler->flags = flags; - atomic_set(&tvlv_handler->refcount, 1); - INIT_HLIST_NODE(&tvlv_handler->list); - - spin_lock_bh(&bat_priv->tvlv.handler_list_lock); - hlist_add_head_rcu(&tvlv_handler->list, &bat_priv->tvlv.handler_list); - spin_unlock_bh(&bat_priv->tvlv.handler_list_lock); -} - -/** - * batadv_tvlv_handler_unregister - unregister tvlv handler based on the - * provided type and version (both need to match) - * @bat_priv: the bat priv with all the soft interface information - * @type: tvlv handler type to be unregistered - * @version: tvlv handler version to be unregistered - */ -void batadv_tvlv_handler_unregister(struct batadv_priv *bat_priv, - uint8_t type, uint8_t version) -{ - struct batadv_tvlv_handler *tvlv_handler; - - tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version); - if (!tvlv_handler) - return; - - batadv_tvlv_handler_free_ref(tvlv_handler); - spin_lock_bh(&bat_priv->tvlv.handler_list_lock); - hlist_del_rcu(&tvlv_handler->list); - spin_unlock_bh(&bat_priv->tvlv.handler_list_lock); - batadv_tvlv_handler_free_ref(tvlv_handler); -} - -/** - * batadv_tvlv_unicast_send - send a unicast packet with tvlv payload to the - * specified host - * @bat_priv: the bat priv with all the soft interface information - * @src: source mac address of the unicast packet - * @dst: destination mac address of the unicast packet - * @type: tvlv type - * @version: tvlv version - * @tvlv_value: tvlv content - * @tvlv_value_len: tvlv content length - */ -void batadv_tvlv_unicast_send(struct batadv_priv *bat_priv, uint8_t *src, - uint8_t *dst, uint8_t type, uint8_t version, - void *tvlv_value, uint16_t tvlv_value_len) -{ - struct batadv_unicast_tvlv_packet *unicast_tvlv_packet; - struct batadv_tvlv_hdr *tvlv_hdr; - struct batadv_orig_node *orig_node; - struct sk_buff *skb = NULL; - unsigned char *tvlv_buff; - unsigned int tvlv_len; - ssize_t hdr_len = sizeof(*unicast_tvlv_packet); - bool ret = false; - - orig_node = batadv_orig_hash_find(bat_priv, dst); - if (!orig_node) - goto out; - - tvlv_len = sizeof(*tvlv_hdr) + tvlv_value_len; - - skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + hdr_len + tvlv_len); - if (!skb) - goto out; - - skb->priority = TC_PRIO_CONTROL; - skb_reserve(skb, ETH_HLEN); - tvlv_buff = skb_put(skb, sizeof(*unicast_tvlv_packet) + tvlv_len); - unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)tvlv_buff; - unicast_tvlv_packet->packet_type = BATADV_UNICAST_TVLV; - unicast_tvlv_packet->version = BATADV_COMPAT_VERSION; - unicast_tvlv_packet->ttl = BATADV_TTL; - unicast_tvlv_packet->reserved = 0; - unicast_tvlv_packet->tvlv_len = htons(tvlv_len); - unicast_tvlv_packet->align = 0; - ether_addr_copy(unicast_tvlv_packet->src, src); - ether_addr_copy(unicast_tvlv_packet->dst, dst); - - tvlv_buff = (unsigned char *)(unicast_tvlv_packet + 1); - tvlv_hdr = (struct batadv_tvlv_hdr *)tvlv_buff; - tvlv_hdr->version = version; - tvlv_hdr->type = type; - tvlv_hdr->len = htons(tvlv_value_len); - tvlv_buff += sizeof(*tvlv_hdr); - memcpy(tvlv_buff, tvlv_value, tvlv_value_len); - - if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP) - ret = true; - -out: - if (skb && !ret) - kfree_skb(skb); - if (orig_node) - batadv_orig_node_free_ref(orig_node); -} - -/** * batadv_get_vid - extract the VLAN identifier from skb if any * @skb: the buffer containing the packet * @header_len: length of the batman header preceding the ethernet header diff --git a/main.h b/main.h index 1cd4ebdbe060..013de2f7ee11 100644 --- a/main.h +++ b/main.h @@ -287,41 +287,6 @@ static inline uint64_t batadv_sum_counter(struct batadv_priv *bat_priv, * The macro is inspired by the similar macro TCP_SKB_CB() in tcp.h. */ #define BATADV_SKB_CB(__skb) ((struct batadv_skb_cb *)&((__skb)->cb[0])) - -void batadv_tvlv_container_register(struct batadv_priv *bat_priv, - uint8_t type, uint8_t version, - void *tvlv_value, uint16_t tvlv_value_len); -uint16_t batadv_tvlv_container_ogm_append(struct batadv_priv *bat_priv, - unsigned char **packet_buff, - int *packet_buff_len, - int packet_min_len); -void batadv_tvlv_ogm_receive(struct batadv_priv *bat_priv, - struct batadv_ogm_packet *batadv_ogm_packet, - struct batadv_orig_node *orig_node); -void batadv_tvlv_container_unregister(struct batadv_priv *bat_priv, - uint8_t type, uint8_t version); - -void batadv_tvlv_handler_register(struct batadv_priv *bat_priv, - void (*optr)(struct batadv_priv *bat_priv, - struct batadv_orig_node *orig, - uint8_t flags, - void *tvlv_value, - uint16_t tvlv_value_len), - int (*uptr)(struct batadv_priv *bat_priv, - uint8_t *src, uint8_t *dst, - void *tvlv_value, - uint16_t tvlv_value_len), - uint8_t type, uint8_t version, uint8_t flags); -void batadv_tvlv_handler_unregister(struct batadv_priv *bat_priv, - uint8_t type, uint8_t version); -int batadv_tvlv_containers_process(struct batadv_priv *bat_priv, - bool ogm_source, - struct batadv_orig_node *orig_node, - uint8_t *src, uint8_t *dst, - void *tvlv_buff, uint16_t tvlv_buff_len); -void batadv_tvlv_unicast_send(struct batadv_priv *bat_priv, uint8_t *src, - uint8_t *dst, uint8_t type, uint8_t version, - void *tvlv_value, uint16_t tvlv_value_len); unsigned short batadv_get_vid(struct sk_buff *skb, size_t header_len); bool batadv_vlan_ap_isola_get(struct batadv_priv *bat_priv, unsigned short vid);
diff --git a/multicast.c b/multicast.c index b24e4bb64fb5..abc410b7e705 100644 --- a/multicast.c +++ b/multicast.c @@ -20,6 +20,7 @@ #include "originator.h" #include "hard-interface.h" #include "translation-table.h" +#include "tvlv.h"
/** * batadv_mcast_mla_softif_get - get softif multicast listeners diff --git a/network-coding.c b/network-coding.c index 93190fc25f76..68b1f9c880cd 100644 --- a/network-coding.c +++ b/network-coding.c @@ -25,6 +25,7 @@ #include "hard-interface.h" #include "routing.h" #include "log.h" +#include "tvlv.h"
static struct lock_class_key batadv_nc_coding_hash_lock_class_key; static struct lock_class_key batadv_nc_decoding_hash_lock_class_key; diff --git a/routing.c b/routing.c index 07205591b05f..99b31499941b 100644 --- a/routing.c +++ b/routing.c @@ -28,6 +28,7 @@ #include "network-coding.h" #include "fragmentation.h" #include "log.h" +#include "tvlv.h"
#include <linux/if_vlan.h>
diff --git a/translation-table.c b/translation-table.c index be75a2ac543e..2d0bad466b68 100644 --- a/translation-table.c +++ b/translation-table.c @@ -26,6 +26,7 @@ #include "bridge_loop_avoidance.h" #include "multicast.h" #include "log.h" +#include "tvlv.h"
#include <linux/crc32c.h>
diff --git a/tvlv.c b/tvlv.c new file mode 100644 index 000000000000..43f9d9456f42 --- /dev/null +++ b/tvlv.c @@ -0,0 +1,592 @@ +/* Copyright (C) 2007-2014 B.A.T.M.A.N. contributors: + * + * Marek Lindner, Simon Wunderlich + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see http://www.gnu.org/licenses/. + */ + +#include <linux/netdevice.h> + +#include "main.h" +#include "tvlv.h" +#include "types.h" +#include "originator.h" +#include "send.h" + +/** + * batadv_tvlv_handler_free_ref - decrement the tvlv handler refcounter and + * possibly free it + * @tvlv_handler: the tvlv handler to free + */ +static void +batadv_tvlv_handler_free_ref(struct batadv_tvlv_handler *tvlv_handler) +{ + if (atomic_dec_and_test(&tvlv_handler->refcount)) + kfree_rcu(tvlv_handler, rcu); +} + +/** + * batadv_tvlv_handler_get - retrieve tvlv handler from the tvlv handler list + * based on the provided type and version (both need to match) + * @bat_priv: the bat priv with all the soft interface information + * @type: tvlv handler type to look for + * @version: tvlv handler version to look for + * + * Returns tvlv handler if found or NULL otherwise. + */ +static struct batadv_tvlv_handler +*batadv_tvlv_handler_get(struct batadv_priv *bat_priv, + uint8_t type, uint8_t version) +{ + struct batadv_tvlv_handler *tvlv_handler_tmp, *tvlv_handler = NULL; + + rcu_read_lock(); + hlist_for_each_entry_rcu(tvlv_handler_tmp, + &bat_priv->tvlv.handler_list, list) { + if (tvlv_handler_tmp->type != type) + continue; + + if (tvlv_handler_tmp->version != version) + continue; + + if (!atomic_inc_not_zero(&tvlv_handler_tmp->refcount)) + continue; + + tvlv_handler = tvlv_handler_tmp; + break; + } + rcu_read_unlock(); + + return tvlv_handler; +} + +/** + * batadv_tvlv_container_free_ref - decrement the tvlv container refcounter and + * possibly free it + * @tvlv: the tvlv container to free + */ +static void batadv_tvlv_container_free_ref(struct batadv_tvlv_container *tvlv) +{ + if (atomic_dec_and_test(&tvlv->refcount)) + kfree(tvlv); +} + +/** + * batadv_tvlv_container_get - retrieve tvlv container from the tvlv container + * list based on the provided type and version (both need to match) + * @bat_priv: the bat priv with all the soft interface information + * @type: tvlv container type to look for + * @version: tvlv container version to look for + * + * Has to be called with the appropriate locks being acquired + * (tvlv.container_list_lock). + * + * Returns tvlv container if found or NULL otherwise. + */ +static struct batadv_tvlv_container +*batadv_tvlv_container_get(struct batadv_priv *bat_priv, + uint8_t type, uint8_t version) +{ + struct batadv_tvlv_container *tvlv_tmp, *tvlv = NULL; + + hlist_for_each_entry(tvlv_tmp, &bat_priv->tvlv.container_list, list) { + if (tvlv_tmp->tvlv_hdr.type != type) + continue; + + if (tvlv_tmp->tvlv_hdr.version != version) + continue; + + if (!atomic_inc_not_zero(&tvlv_tmp->refcount)) + continue; + + tvlv = tvlv_tmp; + break; + } + + return tvlv; +} + +/** + * batadv_tvlv_container_list_size - calculate the size of the tvlv container + * list entries + * @bat_priv: the bat priv with all the soft interface information + * + * Has to be called with the appropriate locks being acquired + * (tvlv.container_list_lock). + * + * Returns size of all currently registered tvlv containers in bytes. + */ +static uint16_t batadv_tvlv_container_list_size(struct batadv_priv *bat_priv) +{ + struct batadv_tvlv_container *tvlv; + uint16_t tvlv_len = 0; + + hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) { + tvlv_len += sizeof(struct batadv_tvlv_hdr); + tvlv_len += ntohs(tvlv->tvlv_hdr.len); + } + + return tvlv_len; +} + +/** + * batadv_tvlv_container_remove - remove tvlv container from the tvlv container + * list + * @tvlv: the to be removed tvlv container + * + * Has to be called with the appropriate locks being acquired + * (tvlv.container_list_lock). + */ +static void batadv_tvlv_container_remove(struct batadv_tvlv_container *tvlv) +{ + if (!tvlv) + return; + + hlist_del(&tvlv->list); + + /* first call to decrement the counter, second call to free */ + batadv_tvlv_container_free_ref(tvlv); + batadv_tvlv_container_free_ref(tvlv); +} + +/** + * batadv_tvlv_container_unregister - unregister tvlv container based on the + * provided type and version (both need to match) + * @bat_priv: the bat priv with all the soft interface information + * @type: tvlv container type to unregister + * @version: tvlv container type to unregister + */ +void batadv_tvlv_container_unregister(struct batadv_priv *bat_priv, + uint8_t type, uint8_t version) +{ + struct batadv_tvlv_container *tvlv; + + spin_lock_bh(&bat_priv->tvlv.container_list_lock); + tvlv = batadv_tvlv_container_get(bat_priv, type, version); + batadv_tvlv_container_remove(tvlv); + spin_unlock_bh(&bat_priv->tvlv.container_list_lock); +} + +/** + * batadv_tvlv_container_register - register tvlv type, version and content + * to be propagated with each (primary interface) OGM + * @bat_priv: the bat priv with all the soft interface information + * @type: tvlv container type + * @version: tvlv container version + * @tvlv_value: tvlv container content + * @tvlv_value_len: tvlv container content length + * + * If a container of the same type and version was already registered the new + * content is going to replace the old one. + */ +void batadv_tvlv_container_register(struct batadv_priv *bat_priv, + uint8_t type, uint8_t version, + void *tvlv_value, uint16_t tvlv_value_len) +{ + struct batadv_tvlv_container *tvlv_old, *tvlv_new; + + if (!tvlv_value) + tvlv_value_len = 0; + + tvlv_new = kzalloc(sizeof(*tvlv_new) + tvlv_value_len, GFP_ATOMIC); + if (!tvlv_new) + return; + + tvlv_new->tvlv_hdr.version = version; + tvlv_new->tvlv_hdr.type = type; + tvlv_new->tvlv_hdr.len = htons(tvlv_value_len); + + memcpy(tvlv_new + 1, tvlv_value, ntohs(tvlv_new->tvlv_hdr.len)); + INIT_HLIST_NODE(&tvlv_new->list); + atomic_set(&tvlv_new->refcount, 1); + + spin_lock_bh(&bat_priv->tvlv.container_list_lock); + tvlv_old = batadv_tvlv_container_get(bat_priv, type, version); + batadv_tvlv_container_remove(tvlv_old); + hlist_add_head(&tvlv_new->list, &bat_priv->tvlv.container_list); + spin_unlock_bh(&bat_priv->tvlv.container_list_lock); +} + +/** + * batadv_tvlv_realloc_packet_buff - reallocate packet buffer to accomodate + * requested packet size + * @packet_buff: packet buffer + * @packet_buff_len: packet buffer size + * @min_packet_len: requested packet minimum size + * @additional_packet_len: requested additional packet size on top of minimum + * size + * + * Returns true of the packet buffer could be changed to the requested size, + * false otherwise. + */ +static bool batadv_tvlv_realloc_packet_buff(unsigned char **packet_buff, + int *packet_buff_len, + int min_packet_len, + int additional_packet_len) +{ + unsigned char *new_buff; + + new_buff = kmalloc(min_packet_len + additional_packet_len, GFP_ATOMIC); + + /* keep old buffer if kmalloc should fail */ + if (!new_buff) + return false; + + memcpy(new_buff, *packet_buff, min_packet_len); + kfree(*packet_buff); + *packet_buff = new_buff; + *packet_buff_len = min_packet_len + additional_packet_len; + + return true; +} + +/** + * batadv_tvlv_container_ogm_append - append tvlv container content to given + * OGM packet buffer + * @bat_priv: the bat priv with all the soft interface information + * @packet_buff: ogm packet buffer + * @packet_buff_len: ogm packet buffer size including ogm header and tvlv + * content + * @packet_min_len: ogm header size to be preserved for the OGM itself + * + * The ogm packet might be enlarged or shrunk depending on the current size + * and the size of the to-be-appended tvlv containers. + * + * Returns size of all appended tvlv containers in bytes. + */ +uint16_t batadv_tvlv_container_ogm_append(struct batadv_priv *bat_priv, + unsigned char **packet_buff, + int *packet_buff_len, + int packet_min_len) +{ + struct batadv_tvlv_container *tvlv; + struct batadv_tvlv_hdr *tvlv_hdr; + uint16_t tvlv_value_len; + void *tvlv_value; + bool ret; + + spin_lock_bh(&bat_priv->tvlv.container_list_lock); + tvlv_value_len = batadv_tvlv_container_list_size(bat_priv); + + ret = batadv_tvlv_realloc_packet_buff(packet_buff, packet_buff_len, + packet_min_len, tvlv_value_len); + + if (!ret) + goto end; + + if (!tvlv_value_len) + goto end; + + tvlv_value = (*packet_buff) + packet_min_len; + + hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) { + tvlv_hdr = tvlv_value; + tvlv_hdr->type = tvlv->tvlv_hdr.type; + tvlv_hdr->version = tvlv->tvlv_hdr.version; + tvlv_hdr->len = tvlv->tvlv_hdr.len; + tvlv_value = tvlv_hdr + 1; + memcpy(tvlv_value, tvlv + 1, ntohs(tvlv->tvlv_hdr.len)); + tvlv_value = (uint8_t *)tvlv_value + ntohs(tvlv->tvlv_hdr.len); + } + +end: + spin_unlock_bh(&bat_priv->tvlv.container_list_lock); + return tvlv_value_len; +} + +/** + * batadv_tvlv_call_handler - parse the given tvlv buffer to call the + * appropriate handlers + * @bat_priv: the bat priv with all the soft interface information + * @tvlv_handler: tvlv callback function handling the tvlv content + * @ogm_source: flag indicating wether the tvlv is an ogm or a unicast packet + * @orig_node: orig node emitting the ogm packet + * @src: source mac address of the unicast packet + * @dst: destination mac address of the unicast packet + * @tvlv_value: tvlv content + * @tvlv_value_len: tvlv content length + * + * Returns success if handler was not found or the return value of the handler + * callback. + */ +static int batadv_tvlv_call_handler(struct batadv_priv *bat_priv, + struct batadv_tvlv_handler *tvlv_handler, + bool ogm_source, + struct batadv_orig_node *orig_node, + uint8_t *src, uint8_t *dst, + void *tvlv_value, uint16_t tvlv_value_len) +{ + if (!tvlv_handler) + return NET_RX_SUCCESS; + + if (ogm_source) { + if (!tvlv_handler->ogm_handler) + return NET_RX_SUCCESS; + + if (!orig_node) + return NET_RX_SUCCESS; + + tvlv_handler->ogm_handler(bat_priv, orig_node, + BATADV_NO_FLAGS, + tvlv_value, tvlv_value_len); + tvlv_handler->flags |= BATADV_TVLV_HANDLER_OGM_CALLED; + } else { + if (!src) + return NET_RX_SUCCESS; + + if (!dst) + return NET_RX_SUCCESS; + + if (!tvlv_handler->unicast_handler) + return NET_RX_SUCCESS; + + return tvlv_handler->unicast_handler(bat_priv, src, + dst, tvlv_value, + tvlv_value_len); + } + + return NET_RX_SUCCESS; +} + +/** + * batadv_tvlv_containers_process - parse the given tvlv buffer to call the + * appropriate handlers + * @bat_priv: the bat priv with all the soft interface information + * @ogm_source: flag indicating wether the tvlv is an ogm or a unicast packet + * @orig_node: orig node emitting the ogm packet + * @src: source mac address of the unicast packet + * @dst: destination mac address of the unicast packet + * @tvlv_value: tvlv content + * @tvlv_value_len: tvlv content length + * + * Returns success when processing an OGM or the return value of all called + * handler callbacks. + */ +int batadv_tvlv_containers_process(struct batadv_priv *bat_priv, + bool ogm_source, + struct batadv_orig_node *orig_node, + uint8_t *src, uint8_t *dst, + void *tvlv_value, uint16_t tvlv_value_len) +{ + struct batadv_tvlv_handler *tvlv_handler; + struct batadv_tvlv_hdr *tvlv_hdr; + uint16_t tvlv_value_cont_len; + uint8_t cifnotfound = BATADV_TVLV_HANDLER_OGM_CIFNOTFND; + int ret = NET_RX_SUCCESS; + + while (tvlv_value_len >= sizeof(*tvlv_hdr)) { + tvlv_hdr = tvlv_value; + tvlv_value_cont_len = ntohs(tvlv_hdr->len); + tvlv_value = tvlv_hdr + 1; + tvlv_value_len -= sizeof(*tvlv_hdr); + + if (tvlv_value_cont_len > tvlv_value_len) + break; + + tvlv_handler = batadv_tvlv_handler_get(bat_priv, + tvlv_hdr->type, + tvlv_hdr->version); + + ret |= batadv_tvlv_call_handler(bat_priv, tvlv_handler, + ogm_source, orig_node, + src, dst, tvlv_value, + tvlv_value_cont_len); + if (tvlv_handler) + batadv_tvlv_handler_free_ref(tvlv_handler); + tvlv_value = (uint8_t *)tvlv_value + tvlv_value_cont_len; + tvlv_value_len -= tvlv_value_cont_len; + } + + if (!ogm_source) + return ret; + + rcu_read_lock(); + hlist_for_each_entry_rcu(tvlv_handler, + &bat_priv->tvlv.handler_list, list) { + if ((tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) && + !(tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CALLED)) + tvlv_handler->ogm_handler(bat_priv, orig_node, + cifnotfound, NULL, 0); + + tvlv_handler->flags &= ~BATADV_TVLV_HANDLER_OGM_CALLED; + } + rcu_read_unlock(); + + return NET_RX_SUCCESS; +} + +/** + * batadv_tvlv_ogm_receive - process an incoming ogm and call the appropriate + * handlers + * @bat_priv: the bat priv with all the soft interface information + * @batadv_ogm_packet: ogm packet containing the tvlv containers + * @orig_node: orig node emitting the ogm packet + */ +void batadv_tvlv_ogm_receive(struct batadv_priv *bat_priv, + struct batadv_ogm_packet *batadv_ogm_packet, + struct batadv_orig_node *orig_node) +{ + void *tvlv_value; + uint16_t tvlv_value_len; + + if (!batadv_ogm_packet) + return; + + tvlv_value_len = ntohs(batadv_ogm_packet->tvlv_len); + if (!tvlv_value_len) + return; + + tvlv_value = batadv_ogm_packet + 1; + + batadv_tvlv_containers_process(bat_priv, true, orig_node, NULL, NULL, + tvlv_value, tvlv_value_len); +} + +/** + * batadv_tvlv_handler_register - register tvlv handler based on the provided + * type and version (both need to match) for ogm tvlv payload and/or unicast + * payload + * @bat_priv: the bat priv with all the soft interface information + * @optr: ogm tvlv handler callback function. This function receives the orig + * node, flags and the tvlv content as argument to process. + * @uptr: unicast tvlv handler callback function. This function receives the + * source & destination of the unicast packet as well as the tvlv content + * to process. + * @type: tvlv handler type to be registered + * @version: tvlv handler version to be registered + * @flags: flags to enable or disable TVLV API behavior + */ +void batadv_tvlv_handler_register(struct batadv_priv *bat_priv, + void (*optr)(struct batadv_priv *bat_priv, + struct batadv_orig_node *orig, + uint8_t flags, + void *tvlv_value, + uint16_t tvlv_value_len), + int (*uptr)(struct batadv_priv *bat_priv, + uint8_t *src, uint8_t *dst, + void *tvlv_value, + uint16_t tvlv_value_len), + uint8_t type, uint8_t version, uint8_t flags) +{ + struct batadv_tvlv_handler *tvlv_handler; + + tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version); + if (tvlv_handler) { + batadv_tvlv_handler_free_ref(tvlv_handler); + return; + } + + tvlv_handler = kzalloc(sizeof(*tvlv_handler), GFP_ATOMIC); + if (!tvlv_handler) + return; + + tvlv_handler->ogm_handler = optr; + tvlv_handler->unicast_handler = uptr; + tvlv_handler->type = type; + tvlv_handler->version = version; + tvlv_handler->flags = flags; + atomic_set(&tvlv_handler->refcount, 1); + INIT_HLIST_NODE(&tvlv_handler->list); + + spin_lock_bh(&bat_priv->tvlv.handler_list_lock); + hlist_add_head_rcu(&tvlv_handler->list, &bat_priv->tvlv.handler_list); + spin_unlock_bh(&bat_priv->tvlv.handler_list_lock); +} + +/** + * batadv_tvlv_handler_unregister - unregister tvlv handler based on the + * provided type and version (both need to match) + * @bat_priv: the bat priv with all the soft interface information + * @type: tvlv handler type to be unregistered + * @version: tvlv handler version to be unregistered + */ +void batadv_tvlv_handler_unregister(struct batadv_priv *bat_priv, + uint8_t type, uint8_t version) +{ + struct batadv_tvlv_handler *tvlv_handler; + + tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version); + if (!tvlv_handler) + return; + + batadv_tvlv_handler_free_ref(tvlv_handler); + spin_lock_bh(&bat_priv->tvlv.handler_list_lock); + hlist_del_rcu(&tvlv_handler->list); + spin_unlock_bh(&bat_priv->tvlv.handler_list_lock); + batadv_tvlv_handler_free_ref(tvlv_handler); +} + +/** + * batadv_tvlv_unicast_send - send a unicast packet with tvlv payload to the + * specified host + * @bat_priv: the bat priv with all the soft interface information + * @src: source mac address of the unicast packet + * @dst: destination mac address of the unicast packet + * @type: tvlv type + * @version: tvlv version + * @tvlv_value: tvlv content + * @tvlv_value_len: tvlv content length + */ +void batadv_tvlv_unicast_send(struct batadv_priv *bat_priv, uint8_t *src, + uint8_t *dst, uint8_t type, uint8_t version, + void *tvlv_value, uint16_t tvlv_value_len) +{ + struct batadv_unicast_tvlv_packet *unicast_tvlv_packet; + struct batadv_tvlv_hdr *tvlv_hdr; + struct batadv_orig_node *orig_node; + struct sk_buff *skb = NULL; + unsigned char *tvlv_buff; + unsigned int tvlv_len; + ssize_t hdr_len = sizeof(*unicast_tvlv_packet); + bool ret = false; + + orig_node = batadv_orig_hash_find(bat_priv, dst); + if (!orig_node) + goto out; + + tvlv_len = sizeof(*tvlv_hdr) + tvlv_value_len; + + skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + hdr_len + tvlv_len); + if (!skb) + goto out; + + skb->priority = TC_PRIO_CONTROL; + skb_reserve(skb, ETH_HLEN); + tvlv_buff = skb_put(skb, sizeof(*unicast_tvlv_packet) + tvlv_len); + unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)tvlv_buff; + unicast_tvlv_packet->packet_type = BATADV_UNICAST_TVLV; + unicast_tvlv_packet->version = BATADV_COMPAT_VERSION; + unicast_tvlv_packet->ttl = BATADV_TTL; + unicast_tvlv_packet->reserved = 0; + unicast_tvlv_packet->tvlv_len = htons(tvlv_len); + unicast_tvlv_packet->align = 0; + ether_addr_copy(unicast_tvlv_packet->src, src); + ether_addr_copy(unicast_tvlv_packet->dst, dst); + + tvlv_buff = (unsigned char *)(unicast_tvlv_packet + 1); + tvlv_hdr = (struct batadv_tvlv_hdr *)tvlv_buff; + tvlv_hdr->version = version; + tvlv_hdr->type = type; + tvlv_hdr->len = htons(tvlv_value_len); + tvlv_buff += sizeof(*tvlv_hdr); + memcpy(tvlv_buff, tvlv_value, tvlv_value_len); + + if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP) + ret = true; + +out: + if (skb && !ret) + kfree_skb(skb); + if (orig_node) + batadv_orig_node_free_ref(orig_node); +} diff --git a/tvlv.h b/tvlv.h new file mode 100644 index 000000000000..26436c01bc78 --- /dev/null +++ b/tvlv.h @@ -0,0 +1,62 @@ +/* Copyright (C) 2007-2014 B.A.T.M.A.N. contributors: + * + * Marek Lindner, Simon Wunderlich + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see http://www.gnu.org/licenses/. + */ + +#ifndef _NET_BATMAN_ADV_TVLV_H_ +#define _NET_BATMAN_ADV_TVLV_H_ + +#include <linux/types.h> + +struct batadv_priv; +struct batadv_ogm_packet; +struct batadv_orig_node; + +void batadv_tvlv_container_register(struct batadv_priv *bat_priv, + uint8_t type, uint8_t version, + void *tvlv_value, uint16_t tvlv_value_len); +uint16_t batadv_tvlv_container_ogm_append(struct batadv_priv *bat_priv, + unsigned char **packet_buff, + int *packet_buff_len, + int packet_min_len); +void batadv_tvlv_ogm_receive(struct batadv_priv *bat_priv, + struct batadv_ogm_packet *batadv_ogm_packet, + struct batadv_orig_node *orig_node); +void batadv_tvlv_container_unregister(struct batadv_priv *bat_priv, + uint8_t type, uint8_t version); + +void batadv_tvlv_handler_register(struct batadv_priv *bat_priv, + void (*optr)(struct batadv_priv *bat_priv, + struct batadv_orig_node *orig, + uint8_t flags, + void *tvlv_value, + uint16_t tvlv_value_len), + int (*uptr)(struct batadv_priv *bat_priv, + uint8_t *src, uint8_t *dst, + void *tvlv_value, + uint16_t tvlv_value_len), + uint8_t type, uint8_t version, uint8_t flags); +void batadv_tvlv_handler_unregister(struct batadv_priv *bat_priv, + uint8_t type, uint8_t version); +int batadv_tvlv_containers_process(struct batadv_priv *bat_priv, + bool ogm_source, + struct batadv_orig_node *orig_node, + uint8_t *src, uint8_t *dst, + void *tvlv_buff, uint16_t tvlv_buff_len); +void batadv_tvlv_unicast_send(struct batadv_priv *bat_priv, uint8_t *src, + uint8_t *dst, uint8_t type, uint8_t version, + void *tvlv_value, uint16_t tvlv_value_len); + +#endif /* _NET_BATMAN_ADV_TVLV_H_ */
These functions are too big to be implemented as inline functions in the header file. This patch moves them to the hash c file.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- hash.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ hash.h | 141 +++++------------------------------------------------------------ 2 files changed, 142 insertions(+), 131 deletions(-)
diff --git a/hash.c b/hash.c index 7c1c63080e20..c68bb8b3e8b9 100644 --- a/hash.c +++ b/hash.c @@ -37,6 +37,35 @@ void batadv_hash_destroy(struct batadv_hashtable *hash) kfree(hash); }
+/* remove the hash structure. if hashdata_free_cb != NULL, this function will be + * called to remove the elements inside of the hash. if you don't remove the + * elements, memory might be leaked. + */ +void batadv_hash_delete(struct batadv_hashtable *hash, + batadv_hashdata_free_cb free_cb, void *arg) +{ + struct hlist_head *head; + struct hlist_node *node, *node_tmp; + spinlock_t *list_lock; /* spinlock to protect write access */ + uint32_t i; + + for (i = 0; i < hash->size; i++) { + head = &hash->table[i]; + list_lock = &hash->list_locks[i]; + + spin_lock_bh(list_lock); + hlist_for_each_safe(node, node_tmp, head) { + hlist_del_rcu(node); + + if (free_cb) + free_cb(node, arg); + } + spin_unlock_bh(list_lock); + } + + batadv_hash_destroy(hash); +} + /* allocates and clears the hash */ struct batadv_hashtable *batadv_hash_new(uint32_t size) { @@ -74,3 +103,106 @@ void batadv_hash_set_lock_class(struct batadv_hashtable *hash, for (i = 0; i < hash->size; i++) lockdep_set_class(&hash->list_locks[i], key); } + +/** + * batadv_hash_bytes - hash some bytes and add them to the previous hash + * @hash: previous hash value + * @data: data to be hashed + * @size: number of bytes to be hashed + * + * Returns the new hash value. + */ +uint32_t batadv_hash_bytes(uint32_t hash, const void *data, uint32_t size) +{ + const unsigned char *key = data; + int i; + + for (i = 0; i < size; i++) { + hash += key[i]; + hash += (hash << 10); + hash ^= (hash >> 6); + } + + return hash; +} + +/** + * batadv_hash_add - adds data to the hashtable + * @hash: storage hash table + * @compare: callback to determine if 2 hash elements are identical + * @choose: callback calculating the hash index + * @data: data passed to the aforementioned callbacks as argument + * @data_node: to be added element + * + * Returns 0 on success, 1 if the element already is in the hash + * and -1 on error. + */ +int batadv_hash_add(struct batadv_hashtable *hash, + batadv_hashdata_compare_cb compare, + batadv_hashdata_choose_cb choose, const void *data, + struct hlist_node *data_node) +{ + uint32_t index; + int ret = -1; + struct hlist_head *head; + struct hlist_node *node; + spinlock_t *list_lock; /* spinlock to protect write access */ + + if (!hash) + goto out; + + index = choose(data, hash->size); + head = &hash->table[index]; + list_lock = &hash->list_locks[index]; + + spin_lock_bh(list_lock); + + hlist_for_each(node, head) { + if (!compare(node, data)) + continue; + + ret = 1; + goto unlock; + } + + /* no duplicate found in list, add new element */ + hlist_add_head_rcu(data_node, head); + + ret = 0; + +unlock: + spin_unlock_bh(list_lock); +out: + return ret; +} + +/* removes data from hash, if found. returns pointer do data on success, so you + * can remove the used structure yourself, or NULL on error . data could be the + * structure you use with just the key filled, we just need the key for + * comparing. + */ +void *batadv_hash_remove(struct batadv_hashtable *hash, + batadv_hashdata_compare_cb compare, + batadv_hashdata_choose_cb choose, void *data) +{ + uint32_t index; + struct hlist_node *node; + struct hlist_head *head; + void *data_save = NULL; + + index = choose(data, hash->size); + head = &hash->table[index]; + + spin_lock_bh(&hash->list_locks[index]); + hlist_for_each(node, head) { + if (!compare(node, data)) + continue; + + data_save = node; + hlist_del_rcu(node); + break; + } + spin_unlock_bh(&hash->list_locks[index]); + + return data_save; +} diff --git a/hash.h b/hash.h index 539fc1266793..0761b64be337 100644 --- a/hash.h +++ b/hash.h @@ -49,139 +49,18 @@ void batadv_hash_set_lock_class(struct batadv_hashtable *hash, /* free only the hashtable and the hash itself. */ void batadv_hash_destroy(struct batadv_hashtable *hash);
-/* remove the hash structure. if hashdata_free_cb != NULL, this function will be - * called to remove the elements inside of the hash. if you don't remove the - * elements, memory might be leaked. - */ -static inline void batadv_hash_delete(struct batadv_hashtable *hash, - batadv_hashdata_free_cb free_cb, - void *arg) -{ - struct hlist_head *head; - struct hlist_node *node, *node_tmp; - spinlock_t *list_lock; /* spinlock to protect write access */ - uint32_t i; - - for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; - list_lock = &hash->list_locks[i]; - - spin_lock_bh(list_lock); - hlist_for_each_safe(node, node_tmp, head) { - hlist_del_rcu(node); - - if (free_cb) - free_cb(node, arg); - } - spin_unlock_bh(list_lock); - } - - batadv_hash_destroy(hash); -} - -/** - * batadv_hash_bytes - hash some bytes and add them to the previous hash - * @hash: previous hash value - * @data: data to be hashed - * @size: number of bytes to be hashed - * - * Returns the new hash value. - */ -static inline uint32_t batadv_hash_bytes(uint32_t hash, const void *data, - uint32_t size) -{ - const unsigned char *key = data; - int i; - - for (i = 0; i < size; i++) { - hash += key[i]; - hash += (hash << 10); - hash ^= (hash >> 6); - } - return hash; -} - -/** - * batadv_hash_add - adds data to the hashtable - * @hash: storage hash table - * @compare: callback to determine if 2 hash elements are identical - * @choose: callback calculating the hash index - * @data: data passed to the aforementioned callbacks as argument - * @data_node: to be added element - * - * Returns 0 on success, 1 if the element already is in the hash - * and -1 on error. - */ -static inline int batadv_hash_add(struct batadv_hashtable *hash, - batadv_hashdata_compare_cb compare, - batadv_hashdata_choose_cb choose, - const void *data, - struct hlist_node *data_node) -{ - uint32_t index; - int ret = -1; - struct hlist_head *head; - struct hlist_node *node; - spinlock_t *list_lock; /* spinlock to protect write access */ - - if (!hash) - goto out; - - index = choose(data, hash->size); - head = &hash->table[index]; - list_lock = &hash->list_locks[index]; - - spin_lock_bh(list_lock); - - hlist_for_each(node, head) { - if (!compare(node, data)) - continue; - - ret = 1; - goto unlock; - } - - /* no duplicate found in list, add new element */ - hlist_add_head_rcu(data_node, head); - - ret = 0; - -unlock: - spin_unlock_bh(list_lock); -out: - return ret; -} - -/* removes data from hash, if found. returns pointer do data on success, so you - * can remove the used structure yourself, or NULL on error . data could be the - * structure you use with just the key filled, we just need the key for - * comparing. - */ -static inline void *batadv_hash_remove(struct batadv_hashtable *hash, - batadv_hashdata_compare_cb compare, - batadv_hashdata_choose_cb choose, - void *data) -{ - uint32_t index; - struct hlist_node *node; - struct hlist_head *head; - void *data_save = NULL; - - index = choose(data, hash->size); - head = &hash->table[index]; +uint32_t batadv_hash_bytes(uint32_t hash, const void *data, uint32_t size);
- spin_lock_bh(&hash->list_locks[index]); - hlist_for_each(node, head) { - if (!compare(node, data)) - continue; +int batadv_hash_add(struct batadv_hashtable *hash, + batadv_hashdata_compare_cb compare, + batadv_hashdata_choose_cb choose, const void *data, + struct hlist_node *data_node);
- data_save = node; - hlist_del_rcu(node); - break; - } - spin_unlock_bh(&hash->list_locks[index]); +void batadv_hash_delete(struct batadv_hashtable *hash, + batadv_hashdata_free_cb free_cb, void *arg);
- return data_save; -} +void *batadv_hash_remove(struct batadv_hashtable *hash, + batadv_hashdata_compare_cb compare, + batadv_hashdata_choose_cb choose, void *data);
#endif /* _NET_BATMAN_ADV_HASH_H_ */
Add helper functions for the hash structure in an attempt to capsulate the hash struct. It creates a cleaner and more defined interface to the hashtable and it is more obvious how the hash table is accessed.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- hash.c | 21 +++++++++++++++++++++ hash.h | 9 +++++++++ 2 files changed, 30 insertions(+)
diff --git a/hash.c b/hash.c index c68bb8b3e8b9..d3313b54347c 100644 --- a/hash.c +++ b/hash.c @@ -18,6 +18,27 @@ #include "main.h" #include "hash.h"
+uint32_t batadv_hash_size(struct batadv_hashtable *hash) +{ + return hash->size; +} + +struct hlist_head *batadv_hash_get(struct batadv_hashtable *hash, + uint32_t index) +{ + return &hash->table[index]; +} + +void batadv_hash_lock(struct batadv_hashtable *hash, uint32_t index) +{ + spin_lock_bh(&hash->list_locks[index]); +} + +void batadv_hash_unlock(struct batadv_hashtable *hash, uint32_t index) +{ + spin_unlock_bh(&hash->list_locks[index]); +} + /* clears the hash */ static void batadv_hash_init(struct batadv_hashtable *hash) { diff --git a/hash.h b/hash.h index 0761b64be337..d0681ecac0f0 100644 --- a/hash.h +++ b/hash.h @@ -63,4 +63,13 @@ void *batadv_hash_remove(struct batadv_hashtable *hash, batadv_hashdata_compare_cb compare, batadv_hashdata_choose_cb choose, void *data);
+uint32_t batadv_hash_size(struct batadv_hashtable *hash); + +struct hlist_head *batadv_hash_get(struct batadv_hashtable *hash, + uint32_t index); + +void batadv_hash_lock(struct batadv_hashtable *hash, uint32_t index); + +void batadv_hash_unlock(struct batadv_hashtable *hash, uint32_t index); + #endif /* _NET_BATMAN_ADV_HASH_H_ */
Use the hash helper functions instead of directly accessing the hash struct. This will provide a cleaner interface to the hashtable implementation.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- bat_iv_ogm.c | 8 ++--- bridge_loop_avoidance.c | 57 +++++++++++++++----------------- distributed-arp-table.c | 26 +++++++-------- network-coding.c | 36 ++++++++++---------- originator.c | 28 +++++++--------- originator.h | 4 +-- translation-table.c | 88 +++++++++++++++++++++---------------------------- 7 files changed, 114 insertions(+), 133 deletions(-)
diff --git a/bat_iv_ogm.c b/bat_iv_ogm.c index 20295c5e5121..ee85ea495421 100644 --- a/bat_iv_ogm.c +++ b/bat_iv_ogm.c @@ -873,8 +873,8 @@ batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface) uint8_t *w; int if_num;
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
rcu_read_lock(); hlist_for_each_entry_rcu(orig_node, head, hash_entry) { @@ -1813,8 +1813,8 @@ static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv, "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE, "Nexthop", "outgoingIF", "Potential nexthops");
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
rcu_read_lock(); hlist_for_each_entry_rcu(orig_node, head, hash_entry) { diff --git a/bridge_loop_avoidance.c b/bridge_loop_avoidance.c index d93288377f80..6e7692828366 100644 --- a/bridge_loop_avoidance.c +++ b/bridge_loop_avoidance.c @@ -151,8 +151,8 @@ static struct batadv_bla_claim if (!hash) return NULL;
- index = batadv_choose_claim(data, hash->size); - head = &hash->table[index]; + index = batadv_choose_claim(data, batadv_hash_size(hash)); + head = batadv_hash_get(hash, index);
rcu_read_lock(); hlist_for_each_entry_rcu(claim, head, hash_entry) { @@ -194,8 +194,9 @@ batadv_backbone_hash_find(struct batadv_priv *bat_priv, ether_addr_copy(search_entry.orig, addr); search_entry.vid = vid;
- index = batadv_choose_backbone_gw(&search_entry, hash->size); - head = &hash->table[index]; + index = batadv_choose_backbone_gw(&search_entry, + batadv_hash_size(hash)); + head = batadv_hash_get(hash, index);
rcu_read_lock(); hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) { @@ -223,17 +224,15 @@ batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw) struct hlist_head *head; struct batadv_bla_claim *claim; int i; - spinlock_t *list_lock; /* protects write access to the hash lists */
hash = backbone_gw->bat_priv->bla.claim_hash; if (!hash) return;
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; - list_lock = &hash->list_locks[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
- spin_lock_bh(list_lock); + batadv_hash_lock(hash, i); hlist_for_each_entry_safe(claim, node_tmp, head, hash_entry) { if (claim->backbone_gw != backbone_gw) @@ -242,7 +241,7 @@ batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw) batadv_claim_free_ref(claim); hlist_del_rcu(&claim->hash_entry); } - spin_unlock_bh(list_lock); + batadv_hash_unlock(hash, i); }
/* all claims gone, intialize CRC */ @@ -481,8 +480,8 @@ static void batadv_bla_answer_request(struct batadv_priv *bat_priv, return;
hash = bat_priv->bla.claim_hash; - for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
rcu_read_lock(); hlist_for_each_entry_rcu(claim, head, hash_entry) { @@ -996,18 +995,16 @@ static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now) struct hlist_node *node_tmp; struct hlist_head *head; struct batadv_hashtable *hash; - spinlock_t *list_lock; /* protects write access to the hash lists */ int i;
hash = bat_priv->bla.backbone_hash; if (!hash) return;
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; - list_lock = &hash->list_locks[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
- spin_lock_bh(list_lock); + batadv_hash_lock(hash, i); hlist_for_each_entry_safe(backbone_gw, node_tmp, head, hash_entry) { if (now) @@ -1030,7 +1027,7 @@ purge_now: hlist_del_rcu(&backbone_gw->hash_entry); batadv_backbone_gw_free_ref(backbone_gw); } - spin_unlock_bh(list_lock); + batadv_hash_unlock(hash, i); } }
@@ -1056,8 +1053,8 @@ static void batadv_bla_purge_claims(struct batadv_priv *bat_priv, if (!hash) return;
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
rcu_read_lock(); hlist_for_each_entry_rcu(claim, head, hash_entry) { @@ -1119,8 +1116,8 @@ void batadv_bla_update_orig_address(struct batadv_priv *bat_priv, if (!hash) return;
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
rcu_read_lock(); hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) { @@ -1172,8 +1169,8 @@ static void batadv_bla_periodic_work(struct work_struct *work) if (!hash) goto out;
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
rcu_read_lock(); hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) { @@ -1369,8 +1366,8 @@ bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, uint8_t *orig, if (!hash) return false;
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
rcu_read_lock(); hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) { @@ -1649,8 +1646,8 @@ int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset) ntohs(bat_priv->bla.claim_dest.group)); seq_printf(seq, " %-17s %-5s %-17s [o] (%-6s)\n", "Client", "VID", "Originator", "CRC"); - for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
rcu_read_lock(); hlist_for_each_entry_rcu(claim, head, hash_entry) { @@ -1694,8 +1691,8 @@ int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset) ntohs(bat_priv->bla.claim_dest.group)); seq_printf(seq, " %-17s %-5s %-9s (%-6s)\n", "Originator", "VID", "last seen", "CRC"); - for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
rcu_read_lock(); hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) { diff --git a/distributed-arp-table.c b/distributed-arp-table.c index e3deff63d393..5855ead2018d 100644 --- a/distributed-arp-table.c +++ b/distributed-arp-table.c @@ -81,20 +81,20 @@ static bool batadv_dat_to_purge(struct batadv_dat_entry *dat_entry) static void __batadv_dat_purge(struct batadv_priv *bat_priv, bool (*to_purge)(struct batadv_dat_entry *)) { - spinlock_t *list_lock; /* protects write access to the hash lists */ struct batadv_dat_entry *dat_entry; struct hlist_node *node_tmp; struct hlist_head *head; + struct batadv_hashtable *hash; uint32_t i;
- if (!bat_priv->dat.hash) + hash = bat_priv->dat.hash; + if (!hash) return;
- for (i = 0; i < bat_priv->dat.hash->size; i++) { - head = &bat_priv->dat.hash->table[i]; - list_lock = &bat_priv->dat.hash->list_locks[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
- spin_lock_bh(list_lock); + batadv_hash_lock(hash, i); hlist_for_each_entry_safe(dat_entry, node_tmp, head, hash_entry) { /* if a helper function has been passed as parameter, @@ -106,7 +106,7 @@ static void __batadv_dat_purge(struct batadv_priv *bat_priv, hlist_del_rcu(&dat_entry->hash_entry); batadv_dat_entry_free_ref(dat_entry); } - spin_unlock_bh(list_lock); + batadv_hash_unlock(hash, i); } }
@@ -243,8 +243,8 @@ batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip, to_find.ip = ip; to_find.vid = vid;
- index = batadv_hash_dat(&to_find, hash->size); - head = &hash->table[index]; + index = batadv_hash_dat(&to_find, batadv_hash_size(hash)); + head = batadv_hash_get(hash, index);
rcu_read_lock(); hlist_for_each_entry_rcu(dat_entry, head, hash_entry) { @@ -481,8 +481,8 @@ static void batadv_choose_next_candidate(struct batadv_priv *bat_priv, /* iterate over the originator list and find the node with the closest * dat_address which has not been selected yet */ - for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
rcu_read_lock(); hlist_for_each_entry_rcu(orig_node, head, hash_entry) { @@ -767,8 +767,8 @@ int batadv_dat_cache_seq_print_text(struct seq_file *seq, void *offset) seq_printf(seq, " %-7s %-9s %4s %11s\n", "IPv4", "MAC", "VID", "last-seen");
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
rcu_read_lock(); hlist_for_each_entry_rcu(dat_entry, head, hash_entry) { diff --git a/network-coding.c b/network-coding.c index 68b1f9c880cd..cf3783f560a9 100644 --- a/network-coding.c +++ b/network-coding.c @@ -360,8 +360,8 @@ static void batadv_nc_purge_orig_hash(struct batadv_priv *bat_priv) return;
/* For each orig_node */ - for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
rcu_read_lock(); hlist_for_each_entry_rcu(orig_node, head, hash_entry) @@ -389,15 +389,13 @@ static void batadv_nc_purge_paths(struct batadv_priv *bat_priv, struct hlist_head *head; struct hlist_node *node_tmp; struct batadv_nc_path *nc_path; - spinlock_t *lock; /* Protects lists in hash */ uint32_t i;
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; - lock = &hash->list_locks[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
/* For each nc_path in this bin */ - spin_lock_bh(lock); + batadv_hash_lock(hash, i); hlist_for_each_entry_safe(nc_path, node_tmp, head, hash_entry) { /* if an helper function has been passed as parameter, * ask it if the entry has to be purged or not @@ -426,7 +424,7 @@ static void batadv_nc_purge_paths(struct batadv_priv *bat_priv, hlist_del_rcu(&nc_path->hash_entry); batadv_nc_path_free_ref(nc_path); } - spin_unlock_bh(lock); + batadv_hash_unlock(hash, i); } }
@@ -513,8 +511,8 @@ batadv_nc_hash_find(struct batadv_hashtable *hash, if (!hash) return NULL;
- index = batadv_nc_hash_choose(data, hash->size); - head = &hash->table[index]; + index = batadv_nc_hash_choose(data, batadv_hash_size(hash)); + head = batadv_hash_get(hash, index);
rcu_read_lock(); hlist_for_each_entry_rcu(nc_path, head, hash_entry) { @@ -644,8 +642,8 @@ batadv_nc_process_nc_paths(struct batadv_priv *bat_priv, return;
/* Loop hash table bins */ - for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
/* Loop coding paths */ rcu_read_lock(); @@ -1247,11 +1245,12 @@ batadv_nc_path_search(struct batadv_priv *bat_priv, /* Create almost path key */ batadv_nc_hash_key_gen(&nc_path_key, in_nc_node->addr, out_nc_node->addr); - idx = batadv_nc_hash_choose(&nc_path_key, hash->size); + idx = batadv_nc_hash_choose(&nc_path_key, batadv_hash_size(hash));
/* Check for coding opportunities in this nc_path */ rcu_read_lock(); - hlist_for_each_entry_rcu(nc_path, &hash->table[idx], hash_entry) { + hlist_for_each_entry_rcu(nc_path, batadv_hash_get(hash, idx), + hash_entry) { if (!batadv_compare_eth(nc_path->prev_hop, in_nc_node->addr)) continue;
@@ -1726,11 +1725,12 @@ batadv_nc_find_decoding_packet(struct batadv_priv *bat_priv, }
batadv_nc_hash_key_gen(&nc_path_key, source, dest); - index = batadv_nc_hash_choose(&nc_path_key, hash->size); + index = batadv_nc_hash_choose(&nc_path_key, batadv_hash_size(hash));
/* Search for matching coding path */ rcu_read_lock(); - hlist_for_each_entry_rcu(nc_path, &hash->table[index], hash_entry) { + hlist_for_each_entry_rcu(nc_path, batadv_hash_get(hash, index), + hash_entry) { /* Find matching nc_packet */ spin_lock_bh(&nc_path->packet_list_lock); list_for_each_entry(tmp_nc_packet, @@ -1862,8 +1862,8 @@ int batadv_nc_nodes_seq_print_text(struct seq_file *seq, void *offset) goto out;
/* Traverse list of originators */ - for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
/* For each orig_node in this bin */ rcu_read_lock(); diff --git a/originator.c b/originator.c index 3386ae54ede5..3aee951c5b91 100644 --- a/originator.c +++ b/originator.c @@ -608,7 +608,6 @@ void batadv_originator_free(struct batadv_priv *bat_priv) struct batadv_hashtable *hash = bat_priv->orig_hash; struct hlist_node *node_tmp; struct hlist_head *head; - spinlock_t *list_lock; /* spinlock to protect write access */ struct batadv_orig_node *orig_node; uint32_t i;
@@ -619,17 +618,16 @@ void batadv_originator_free(struct batadv_priv *bat_priv)
bat_priv->orig_hash = NULL;
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; - list_lock = &hash->list_locks[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
- spin_lock_bh(list_lock); + batadv_hash_lock(hash, i); hlist_for_each_entry_safe(orig_node, node_tmp, head, hash_entry) { hlist_del_rcu(&orig_node->hash_entry); batadv_orig_node_free_ref(orig_node); } - spin_unlock_bh(list_lock); + batadv_hash_unlock(hash, i); }
batadv_hash_destroy(hash); @@ -960,7 +958,6 @@ static void _batadv_purge_orig(struct batadv_priv *bat_priv) struct batadv_hashtable *hash = bat_priv->orig_hash; struct hlist_node *node_tmp; struct hlist_head *head; - spinlock_t *list_lock; /* spinlock to protect write access */ struct batadv_orig_node *orig_node; uint32_t i;
@@ -968,11 +965,10 @@ static void _batadv_purge_orig(struct batadv_priv *bat_priv) return;
/* for all origins... */ - for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; - list_lock = &hash->list_locks[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
- spin_lock_bh(list_lock); + batadv_hash_lock(hash, i); hlist_for_each_entry_safe(orig_node, node_tmp, head, hash_entry) { if (batadv_purge_orig_node(bat_priv, orig_node)) { @@ -985,7 +981,7 @@ static void _batadv_purge_orig(struct batadv_priv *bat_priv) batadv_frag_purge_orig(orig_node, batadv_frag_check_entry); } - spin_unlock_bh(list_lock); + batadv_hash_unlock(hash, i); }
batadv_gw_node_purge(bat_priv); @@ -1099,8 +1095,8 @@ int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface, /* resize all orig nodes because orig_node->bcast_own(_sum) depend on * if_num */ - for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
rcu_read_lock(); hlist_for_each_entry_rcu(orig_node, head, hash_entry) { @@ -1136,8 +1132,8 @@ int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface, /* resize all orig nodes because orig_node->bcast_own(_sum) depend on * if_num */ - for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
rcu_read_lock(); hlist_for_each_entry_rcu(orig_node, head, hash_entry) { diff --git a/originator.h b/originator.h index aa4a43696295..5687be7c7378 100644 --- a/originator.h +++ b/originator.h @@ -103,8 +103,8 @@ batadv_orig_hash_find(struct batadv_priv *bat_priv, const void *data) if (!hash) return NULL;
- index = batadv_choose_orig(data, hash->size); - head = &hash->table[index]; + index = batadv_choose_orig(data, batadv_hash_size(hash)); + head = batadv_hash_get(hash, index);
rcu_read_lock(); hlist_for_each_entry_rcu(orig_node, head, hash_entry) { diff --git a/translation-table.c b/translation-table.c index 2d0bad466b68..f7a75eaceb89 100644 --- a/translation-table.c +++ b/translation-table.c @@ -102,8 +102,8 @@ batadv_tt_hash_find(struct batadv_hashtable *hash, const uint8_t *addr, ether_addr_copy(to_search.addr, addr); to_search.vid = vid;
- index = batadv_choose_tt(&to_search, hash->size); - head = &hash->table[index]; + index = batadv_choose_tt(&to_search, batadv_hash_size(hash)); + head = batadv_hash_get(hash, index);
rcu_read_lock(); hlist_for_each_entry_rcu(tt, head, hash_entry) { @@ -928,8 +928,8 @@ int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset) seq_printf(seq, " %-13s %s %-8s %-9s (%-10s)\n", "Client", "VID", "Flags", "Last seen", "CRC");
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
rcu_read_lock(); hlist_for_each_entry_rcu(tt_common_entry, @@ -1105,23 +1105,20 @@ static void batadv_tt_local_purge(struct batadv_priv *bat_priv, { struct batadv_hashtable *hash = bat_priv->tt.local_hash; struct hlist_head *head; - spinlock_t *list_lock; /* protects write access to the hash lists */ uint32_t i;
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; - list_lock = &hash->list_locks[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
- spin_lock_bh(list_lock); + batadv_hash_lock(hash, i); batadv_tt_local_purge_list(bat_priv, head, timeout); - spin_unlock_bh(list_lock); + batadv_hash_unlock(hash, i); } }
static void batadv_tt_local_table_free(struct batadv_priv *bat_priv) { struct batadv_hashtable *hash; - spinlock_t *list_lock; /* protects write access to the hash lists */ struct batadv_tt_common_entry *tt_common_entry; struct batadv_tt_local_entry *tt_local; struct batadv_softif_vlan *vlan; @@ -1134,11 +1131,10 @@ static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
hash = bat_priv->tt.local_hash;
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; - list_lock = &hash->list_locks[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
- spin_lock_bh(list_lock); + batadv_hash_lock(hash, i); hlist_for_each_entry_safe(tt_common_entry, node_tmp, head, hash_entry) { hlist_del_rcu(&tt_common_entry->hash_entry); @@ -1154,7 +1150,7 @@ static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
batadv_tt_local_entry_free_ref(tt_local); } - spin_unlock_bh(list_lock); + batadv_hash_unlock(hash, i); }
batadv_hash_destroy(hash); @@ -1593,8 +1589,8 @@ int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset) "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)", "CRC", "Flags");
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
rcu_read_lock(); hlist_for_each_entry_rcu(tt_common_entry, @@ -1809,17 +1805,15 @@ void batadv_tt_global_del_orig(struct batadv_priv *bat_priv, struct batadv_hashtable *hash = bat_priv->tt.global_hash; struct hlist_node *safe; struct hlist_head *head; - spinlock_t *list_lock; /* protects write access to the hash lists */ unsigned short vid;
if (!hash) return;
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; - list_lock = &hash->list_locks[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
- spin_lock_bh(list_lock); + batadv_hash_lock(hash, i); hlist_for_each_entry_safe(tt_common_entry, safe, head, hash_entry) { /* remove only matching entries */ @@ -1843,7 +1837,7 @@ void batadv_tt_global_del_orig(struct batadv_priv *bat_priv, batadv_tt_global_entry_free_ref(tt_global); } } - spin_unlock_bh(list_lock); + batadv_hash_unlock(hash, i); } orig_node->capa_initialized &= ~BATADV_ORIG_CAPA_HAS_TT; } @@ -1875,17 +1869,15 @@ static void batadv_tt_global_purge(struct batadv_priv *bat_priv) struct batadv_hashtable *hash = bat_priv->tt.global_hash; struct hlist_head *head; struct hlist_node *node_tmp; - spinlock_t *list_lock; /* protects write access to the hash lists */ uint32_t i; char *msg = NULL; struct batadv_tt_common_entry *tt_common; struct batadv_tt_global_entry *tt_global;
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; - list_lock = &hash->list_locks[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
- spin_lock_bh(list_lock); + batadv_hash_lock(hash, i); hlist_for_each_entry_safe(tt_common, node_tmp, head, hash_entry) { tt_global = container_of(tt_common, @@ -1905,14 +1897,13 @@ static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
batadv_tt_global_entry_free_ref(tt_global); } - spin_unlock_bh(list_lock); + batadv_hash_unlock(hash, i); } }
static void batadv_tt_global_table_free(struct batadv_priv *bat_priv) { struct batadv_hashtable *hash; - spinlock_t *list_lock; /* protects write access to the hash lists */ struct batadv_tt_common_entry *tt_common_entry; struct batadv_tt_global_entry *tt_global; struct hlist_node *node_tmp; @@ -1924,11 +1915,10 @@ static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
hash = bat_priv->tt.global_hash;
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; - list_lock = &hash->list_locks[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
- spin_lock_bh(list_lock); + batadv_hash_lock(hash, i); hlist_for_each_entry_safe(tt_common_entry, node_tmp, head, hash_entry) { hlist_del_rcu(&tt_common_entry->hash_entry); @@ -1937,7 +1927,7 @@ static void batadv_tt_global_table_free(struct batadv_priv *bat_priv) common); batadv_tt_global_entry_free_ref(tt_global); } - spin_unlock_bh(list_lock); + batadv_hash_unlock(hash, i); }
batadv_hash_destroy(hash); @@ -2059,8 +2049,8 @@ static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv, uint8_t flags; __be16 tmp_vid;
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
rcu_read_lock(); hlist_for_each_entry_rcu(tt_common, head, hash_entry) { @@ -2134,8 +2124,8 @@ static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv, uint8_t flags; __be16 tmp_vid;
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
rcu_read_lock(); hlist_for_each_entry_rcu(tt_common, head, hash_entry) { @@ -2311,8 +2301,8 @@ static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv, tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
rcu_read_lock(); - for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
hlist_for_each_entry_rcu(tt_common_entry, head, hash_entry) { @@ -3125,8 +3115,8 @@ static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, if (!hash) return;
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
rcu_read_lock(); hlist_for_each_entry_rcu(tt_common_entry, @@ -3161,17 +3151,15 @@ static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv) struct batadv_softif_vlan *vlan; struct hlist_node *node_tmp; struct hlist_head *head; - spinlock_t *list_lock; /* protects write access to the hash lists */ uint32_t i;
if (!hash) return;
- for (i = 0; i < hash->size; i++) { - head = &hash->table[i]; - list_lock = &hash->list_locks[i]; + for (i = 0; i < batadv_hash_size(hash); i++) { + head = batadv_hash_get(hash, i);
- spin_lock_bh(list_lock); + batadv_hash_lock(hash, i); hlist_for_each_entry_safe(tt_common, node_tmp, head, hash_entry) { if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING)) @@ -3195,7 +3183,7 @@ static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
batadv_tt_local_entry_free_ref(tt_local); } - spin_unlock_bh(list_lock); + batadv_hash_unlock(hash, i); } }
All accesses to the struct hashtable were replaced by accessor functions so we can move the struct into the private scope of the hashtable implementation.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- hash.c | 6 ++++++ hash.h | 8 ++------ 2 files changed, 8 insertions(+), 6 deletions(-)
diff --git a/hash.c b/hash.c index d3313b54347c..1d3e5ebaa7ad 100644 --- a/hash.c +++ b/hash.c @@ -18,6 +18,12 @@ #include "main.h" #include "hash.h"
+struct batadv_hashtable { + struct hlist_head *table; /* the hashtable itself with the buckets */ + spinlock_t *list_locks; /* spinlock for each hash list entry */ + uint32_t size; /* size of hashtable */ +}; + uint32_t batadv_hash_size(struct batadv_hashtable *hash) { return hash->size; diff --git a/hash.h b/hash.h index d0681ecac0f0..8f5f832acd7c 100644 --- a/hash.h +++ b/hash.h @@ -20,6 +20,8 @@
#include <linux/list.h>
+struct batadv_hashtable; + /* callback to a compare function. should compare 2 element datas for their * keys, return 0 if same and not 0 if not same */ @@ -33,12 +35,6 @@ typedef int (*batadv_hashdata_compare_cb)(const struct hlist_node *, typedef uint32_t (*batadv_hashdata_choose_cb)(const void *, uint32_t); typedef void (*batadv_hashdata_free_cb)(struct hlist_node *, void *);
-struct batadv_hashtable { - struct hlist_head *table; /* the hashtable itself with the buckets */ - spinlock_t *list_locks; /* spinlock for each hash list entry */ - uint32_t size; /* size of hashtable */ -}; - /* allocates and clears the hash */ struct batadv_hashtable *batadv_hash_new(uint32_t size);
Explicit list of linux headers necessary for this file.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- hash.c | 4 ++++ 1 file changed, 4 insertions(+)
diff --git a/hash.c b/hash.c index 1d3e5ebaa7ad..783ea8103a80 100644 --- a/hash.c +++ b/hash.c @@ -15,6 +15,10 @@ * along with this program; if not, see http://www.gnu.org/licenses/. */
+#include <linux/slab.h> +#include <linux/spinlock.h> +#include <linux/types.h> + #include "main.h" #include "hash.h"
The whole Makefile is sorted, just the multicast rule is not at the right position.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- Makefile.kbuild | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/Makefile.kbuild b/Makefile.kbuild index a8c34baddd63..6fb243c5b13c 100644 --- a/Makefile.kbuild +++ b/Makefile.kbuild @@ -29,6 +29,7 @@ batman-adv-y += hard-interface.o batman-adv-y += hash.o batman-adv-y += icmp_socket.o batman-adv-y += main.o +batman-adv-$(CONFIG_BATMAN_ADV_MCAST) += multicast.o batman-adv-$(CONFIG_BATMAN_ADV_NC) += network-coding.o batman-adv-y += originator.o batman-adv-y += routing.o @@ -37,4 +38,3 @@ batman-adv-y += soft-interface.o batman-adv-y += sysfs.o batman-adv-y += translation-table.o batman-adv-y += tvlv.o -batman-adv-$(CONFIG_BATMAN_ADV_MCAST) += multicast.o
Directly return error values. No need to use a return variable.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- bat_iv_ogm.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
diff --git a/bat_iv_ogm.c b/bat_iv_ogm.c index ee85ea495421..d583a1b4aec9 100644 --- a/bat_iv_ogm.c +++ b/bat_iv_ogm.c @@ -310,7 +310,6 @@ static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface) struct batadv_ogm_packet *batadv_ogm_packet; unsigned char *ogm_buff; uint32_t random_seqno; - int res = -ENOMEM;
/* randomize initial seqno to avoid collision */ get_random_bytes(&random_seqno, sizeof(random_seqno)); @@ -319,7 +318,7 @@ static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface) hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN; ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC); if (!ogm_buff) - goto out; + return -ENOMEM;
hard_iface->bat_iv.ogm_buff = ogm_buff;
@@ -331,10 +330,7 @@ static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface) batadv_ogm_packet->reserved = 0; batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
- res = 0; - -out: - return res; + return 0; }
static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
This function returns bool values, so it should be defined to return them instead of the whole int range.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- bat_iv_ogm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/bat_iv_ogm.c b/bat_iv_ogm.c index d583a1b4aec9..c421a550b303 100644 --- a/bat_iv_ogm.c +++ b/bat_iv_ogm.c @@ -394,8 +394,8 @@ static uint8_t batadv_hop_penalty(uint8_t tq, }
/* is there another aggregated packet here? */ -static int batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len, - __be16 tvlv_len) +static bool batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len, + __be16 tvlv_len) { int next_buff_pos = 0;
This string pointer is later assigned to a constant string, so it should be defined constant at the beginning.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- bat_iv_ogm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/bat_iv_ogm.c b/bat_iv_ogm.c index c421a550b303..8ebbd2eb81ee 100644 --- a/bat_iv_ogm.c +++ b/bat_iv_ogm.c @@ -411,7 +411,7 @@ static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet, struct batadv_hard_iface *hard_iface) { struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); - char *fwd_str; + const char *fwd_str; uint8_t packet_num; int16_t buff_pos; struct batadv_ogm_packet *batadv_ogm_packet;
This patch tries to increase code readability by negating the first if block and rearranging some of the other conditional blocks. This way we save an indentation level, we also save some allocation that is not necessary for one of the conditions.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- bat_iv_ogm.c | 98 +++++++++++++++++++++++++++++++----------------------------- 1 file changed, 50 insertions(+), 48 deletions(-)
diff --git a/bat_iv_ogm.c b/bat_iv_ogm.c index 8ebbd2eb81ee..f8fd535dbdc9 100644 --- a/bat_iv_ogm.c +++ b/bat_iv_ogm.c @@ -546,58 +546,60 @@ batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet, * - the send time is within our MAX_AGGREGATION_MS time * - the resulting packet wont be bigger than * MAX_AGGREGATION_BYTES + * otherwise aggregation is not possible */ - if (time_before(send_time, forw_packet->send_time) && - time_after_eq(aggregation_end_time, forw_packet->send_time) && - (aggregated_bytes <= BATADV_MAX_AGGREGATION_BYTES)) { - /* check aggregation compatibility - * -> direct link packets are broadcasted on - * their interface only - * -> aggregate packet if the current packet is - * a "global" packet as well as the base - * packet - */ - primary_if = batadv_primary_if_get_selected(bat_priv); - if (!primary_if) - goto out; + if (!time_before(send_time, forw_packet->send_time) || + !time_after_eq(aggregation_end_time, forw_packet->send_time) || + aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES) + return false;
- /* packet is not leaving on the same interface. */ - if (forw_packet->if_outgoing != if_outgoing) - goto out; + /* packet is not leaving on the same interface. */ + if (forw_packet->if_outgoing != if_outgoing) + return false;
- /* packets without direct link flag and high TTL - * are flooded through the net - */ - if ((!directlink) && - (!(batadv_ogm_packet->flags & BATADV_DIRECTLINK)) && - (batadv_ogm_packet->ttl != 1) && - - /* own packets originating non-primary - * interfaces leave only that interface - */ - ((!forw_packet->own) || - (forw_packet->if_incoming == primary_if))) { - res = true; - goto out; - } + /* check aggregation compatibility + * -> direct link packets are broadcasted on + * their interface only + * -> aggregate packet if the current packet is + * a "global" packet as well as the base + * packet + */ + primary_if = batadv_primary_if_get_selected(bat_priv); + if (!primary_if) + return false;
- /* if the incoming packet is sent via this one - * interface only - we still can aggregate - */ - if ((directlink) && - (new_bat_ogm_packet->ttl == 1) && - (forw_packet->if_incoming == if_incoming) && - - /* packets from direct neighbors or - * own secondary interface packets - * (= secondary interface packets in general) - */ - (batadv_ogm_packet->flags & BATADV_DIRECTLINK || - (forw_packet->own && - forw_packet->if_incoming != primary_if))) { - res = true; - goto out; - } + /* packets without direct link flag and high TTL + * are flooded through the net + */ + if (!directlink && + !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) && + batadv_ogm_packet->ttl != 1 && + + /* own packets originating non-primary + * interfaces leave only that interface + */ + (!forw_packet->own || + forw_packet->if_incoming == primary_if)) { + res = true; + goto out; + } + + /* if the incoming packet is sent via this one + * interface only - we still can aggregate + */ + if (directlink && + new_bat_ogm_packet->ttl == 1 && + forw_packet->if_incoming == if_incoming && + + /* packets from direct neighbors or + * own secondary interface packets + * (= secondary interface packets in general) + */ + (batadv_ogm_packet->flags & BATADV_DIRECTLINK || + (forw_packet->own && + forw_packet->if_incoming != primary_if))) { + res = true; + goto out; }
out:
Remove these unnecessary brackets inside a condition.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- bat_iv_ogm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/bat_iv_ogm.c b/bat_iv_ogm.c index f8fd535dbdc9..232cd9ab3e6e 100644 --- a/bat_iv_ogm.c +++ b/bat_iv_ogm.c @@ -1081,7 +1081,7 @@ batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv, * won't consider it either */ if (router_ifinfo && - (neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg)) { + neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) { orig_node_tmp = router->orig_node; spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock); if_num = router->if_incoming->if_num;
It is just a bit easier to put the error handling at one place and let multiple error paths use the same calls.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- bat_iv_ogm.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-)
diff --git a/bat_iv_ogm.c b/bat_iv_ogm.c index 232cd9ab3e6e..1f3ed3173005 100644 --- a/bat_iv_ogm.c +++ b/bat_iv_ogm.c @@ -647,14 +647,11 @@ static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff, }
forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC); - if (!forw_packet_aggr) { - if (!own_packet) - atomic_inc(&bat_priv->batman_queue_left); - goto out; - } + if (!forw_packet_aggr) + goto out_nomem;
- if ((atomic_read(&bat_priv->aggregated_ogms)) && - (packet_len < BATADV_MAX_AGGREGATION_BYTES)) + if (atomic_read(&bat_priv->aggregated_ogms) && + packet_len < BATADV_MAX_AGGREGATION_BYTES) skb_size = BATADV_MAX_AGGREGATION_BYTES; else skb_size = packet_len; @@ -662,12 +659,8 @@ static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff, skb_size += ETH_HLEN;
forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size); - if (!forw_packet_aggr->skb) { - if (!own_packet) - atomic_inc(&bat_priv->batman_queue_left); - kfree(forw_packet_aggr); - goto out; - } + if (!forw_packet_aggr->skb) + goto out_free_forw_packet; forw_packet_aggr->skb->priority = TC_PRIO_CONTROL; skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
@@ -699,6 +692,11 @@ static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff, send_time - jiffies);
return; +out_free_forw_packet: + kfree(forw_packet_aggr); +out_nomem: + if (!own_packet) + atomic_inc(&bat_priv->batman_queue_left); out: batadv_hardif_free_ref(if_outgoing); out_free_incoming:
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- bat_iv_ogm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/bat_iv_ogm.c b/bat_iv_ogm.c index 1f3ed3173005..9482a0889201 100644 --- a/bat_iv_ogm.c +++ b/bat_iv_ogm.c @@ -750,13 +750,13 @@ static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv, unsigned long max_aggregation_jiffies;
batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff; - direct_link = batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0; + direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK); max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
/* find position for the packet in the forward queue */ spin_lock_bh(&bat_priv->forw_bat_list_lock); /* own packets are not to be aggregated */ - if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) { + if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) { hlist_for_each_entry(forw_packet_pos, &bat_priv->forw_bat_list, list) { if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
CodingStyle describes that either none or both branches of a conditional have to have brackets.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- bat_iv_ogm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/bat_iv_ogm.c b/bat_iv_ogm.c index 9482a0889201..46d1d28b5d97 100644 --- a/bat_iv_ogm.c +++ b/bat_iv_ogm.c @@ -1032,9 +1032,10 @@ batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv, batadv_orig_node_free_ref(orig_tmp); if (!neigh_node) goto unlock; - } else + } else { batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "Updating existing last-hop neighbor of originator\n"); + }
rcu_read_unlock(); neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- bat_iv_ogm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/bat_iv_ogm.c b/bat_iv_ogm.c index 46d1d28b5d97..6946e89397b8 100644 --- a/bat_iv_ogm.c +++ b/bat_iv_ogm.c @@ -30,7 +30,7 @@
/** * enum batadv_dup_status - duplicate status - * @BATADV_NO_DUP: the packet is a duplicate + * @BATADV_NO_DUP: the packet is no duplicate * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the * neighbor) * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
The kernel coding style says, that there should not be multiple assignments in one row.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- bat_iv_ogm.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/bat_iv_ogm.c b/bat_iv_ogm.c index 6946e89397b8..808a6363f4a1 100644 --- a/bat_iv_ogm.c +++ b/bat_iv_ogm.c @@ -66,7 +66,9 @@ static void batadv_ring_buffer_set(uint8_t lq_recv[], uint8_t *lq_index, static uint8_t batadv_ring_buffer_avg(const uint8_t lq_recv[]) { const uint8_t *ptr; - uint16_t count = 0, i = 0, sum = 0; + uint16_t count = 0; + uint16_t i = 0; + uint16_t sum = 0;
ptr = lq_recv;
This is a small copy paste fix for batadv_ing_buffer_avg.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- bat_iv_ogm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/bat_iv_ogm.c b/bat_iv_ogm.c index 808a6363f4a1..bdc64e2bf7bd 100644 --- a/bat_iv_ogm.c +++ b/bat_iv_ogm.c @@ -57,7 +57,7 @@ static void batadv_ring_buffer_set(uint8_t lq_recv[], uint8_t *lq_index, }
/** - * batadv_ring_buffer_set - compute the average of all non-zero values stored + * batadv_ring_buffer_avg - compute the average of all non-zero values stored * in the given ring buffer * @lq_recv: pointer to the ring buffer *
batadv_orig_bat_iv->bcast_own is actually not a bitfield, it is an array. Adjust the comment accordingly.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- types.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/types.h b/types.h index 462a70c0db8f..cd331974321b 100644 --- a/types.h +++ b/types.h @@ -181,9 +181,10 @@ struct batadv_orig_node_vlan {
/** * struct batadv_orig_bat_iv - B.A.T.M.A.N. IV private orig_node members - * @bcast_own: bitfield containing the number of our OGMs this orig_node - * rebroadcasted "back" to us (relative to last_real_seqno) - * @bcast_own_sum: counted result of bcast_own + * @bcast_own: array containing the number of our OGMs this orig_node + * rebroadcasted "back" to us (relative to last_real_seqno) on each hard + * interface + * @bcast_own_sum: sum of bcast_own * @ogm_cnt_lock: lock protecting bcast_own, bcast_own_sum, * neigh_node->bat_iv.real_bits & neigh_node->bat_iv.real_packet_count */
It is much clearer to see a bool type as return value than 'int' for functions that are supposed to return true or false.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- main.c | 11 +++++++---- main.h | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-)
diff --git a/main.c b/main.c index cff31bb9bb14..d5c79ae1db6f 100644 --- a/main.c +++ b/main.c @@ -228,10 +228,13 @@ void batadv_mesh_free(struct net_device *soft_iface) * interfaces in the current mesh * @bat_priv: the bat priv with all the soft interface information * @addr: the address to check + * + * Returns 'true' if the mac address was found, false otherwise. */ -int batadv_is_my_mac(struct batadv_priv *bat_priv, const uint8_t *addr) +bool batadv_is_my_mac(struct batadv_priv *bat_priv, const uint8_t *addr) { const struct batadv_hard_iface *hard_iface; + bool is_my_mac = false;
rcu_read_lock(); list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { @@ -242,12 +245,12 @@ int batadv_is_my_mac(struct batadv_priv *bat_priv, const uint8_t *addr) continue;
if (batadv_compare_eth(hard_iface->net_dev->dev_addr, addr)) { - rcu_read_unlock(); - return 1; + is_my_mac = true; + break; } } rcu_read_unlock(); - return 0; + return is_my_mac; }
/** diff --git a/main.h b/main.h index 013de2f7ee11..6cd339090658 100644 --- a/main.h +++ b/main.h @@ -196,7 +196,7 @@ extern struct workqueue_struct *batadv_event_workqueue;
int batadv_mesh_init(struct net_device *soft_iface); void batadv_mesh_free(struct net_device *soft_iface); -int batadv_is_my_mac(struct batadv_priv *bat_priv, const uint8_t *addr); +bool batadv_is_my_mac(struct batadv_priv *bat_priv, const uint8_t *addr); struct batadv_hard_iface * batadv_seq_print_text_primary_if_get(struct seq_file *seq); int batadv_max_header_len(void);
Declare the returntype of batadv_compare_eth as bool. The function called inside this helper function (ether_addr_equal_unaligned) also uses bool as return value, so there is no need to return int.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- main.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/main.h b/main.h index 6cd339090658..2e70d4dc748d 100644 --- a/main.h +++ b/main.h @@ -218,7 +218,7 @@ __be32 batadv_skb_crc32(struct sk_buff *skb, u8 *payload_ptr); * * note: can't use ether_addr_equal() as it requires aligned memory */ -static inline int batadv_compare_eth(const void *data1, const void *data2) +static inline bool batadv_compare_eth(const void *data1, const void *data2) { return ether_addr_equal_unaligned(data1, data2); }
We can avoid this indirect return variable by directly returning the error values.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- main.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-)
diff --git a/main.c b/main.c index d5c79ae1db6f..2e2ad071a085 100644 --- a/main.c +++ b/main.c @@ -532,14 +532,12 @@ static struct batadv_algo_ops *batadv_algo_get(char *name) int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops) { struct batadv_algo_ops *bat_algo_ops_tmp; - int ret;
bat_algo_ops_tmp = batadv_algo_get(bat_algo_ops->name); if (bat_algo_ops_tmp) { pr_info("Trying to register already registered routing algorithm: %s\n", bat_algo_ops->name); - ret = -EEXIST; - goto out; + return -EEXIST; }
/* all algorithms must implement all ops (for now) */ @@ -553,16 +551,13 @@ int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops) !bat_algo_ops->bat_neigh_is_equiv_or_better) { pr_info("Routing algo '%s' does not implement required ops\n", bat_algo_ops->name); - ret = -EINVAL; - goto out; + return -EINVAL; }
INIT_HLIST_NODE(&bat_algo_ops->list); hlist_add_head(&bat_algo_ops->list, &batadv_algo_list); - ret = 0;
-out: - return ret; + return 0; }
int batadv_algo_select(struct batadv_priv *bat_priv, char *name)
Remove ret variable and all jumps.
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- main.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/main.c b/main.c index 2e2ad071a085..d67be132e5df 100644 --- a/main.c +++ b/main.c @@ -563,17 +563,14 @@ int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops) int batadv_algo_select(struct batadv_priv *bat_priv, char *name) { struct batadv_algo_ops *bat_algo_ops; - int ret = -EINVAL;
bat_algo_ops = batadv_algo_get(name); if (!bat_algo_ops) - goto out; + return -EINVAL;
bat_priv->bat_algo_ops = bat_algo_ops; - ret = 0;
-out: - return ret; + return 0; }
int batadv_algo_seq_print_text(struct seq_file *seq, void *offset)
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- packet.h | 3 +++ 1 file changed, 3 insertions(+)
diff --git a/packet.h b/packet.h index facd1feddd4e..cbf1a9140d5e 100644 --- a/packet.h +++ b/packet.h @@ -18,6 +18,9 @@ #ifndef _NET_BATMAN_ADV_PACKET_H_ #define _NET_BATMAN_ADV_PACKET_H_
+#include <linux/bitops.h> +#include <uapi/linux/if_ether.h> + /** * enum batadv_packettype - types for batman-adv encapsulated packets * @BATADV_IV_OGM: originator messages for B.A.T.M.A.N. IV
Signed-off-by: Markus Pargmann mpa@pengutronix.de --- types.h | 1 + 1 file changed, 1 insertion(+)
diff --git a/types.h b/types.h index cd331974321b..7c22dfadff5e 100644 --- a/types.h +++ b/types.h @@ -21,6 +21,7 @@ #include "packet.h" #include "bitarray.h" #include <linux/kernel.h> +#include <linux/netdevice.h>
#ifdef CONFIG_BATMAN_ADV_DAT
b.a.t.m.a.n@lists.open-mesh.org