public inbox for b.a.t.m.a.n@lists.open-mesh.org
 help / color / mirror / Atom feed
From: "Linus Lüssing" <linus.luessing@ascom.ch>
To: b.a.t.m.a.n@lists.open-mesh.org
Cc: "Linus Lüssing" <linus.luessing@ascom.ch>
Subject: [B.A.T.M.A.N.] [PATCH 05/13] batman-adv: Add rcu-refcount wrapper for orig_node hash_find()
Date: Tue, 15 Feb 2011 18:12:20 +0100	[thread overview]
Message-ID: <1297789948-16948-6-git-send-email-linus.luessing@ascom.ch> (raw)
In-Reply-To: <1297789948-16948-1-git-send-email-linus.luessing@ascom.ch>

This structure is being used in several places - removing some
redundancy.

Signed-off-by: Linus Lüssing <linus.luessing@ascom.ch>
---
 hash.h        |   18 +++++++++++++++
 icmp_socket.c |   21 +++++------------
 originator.c  |   11 +-------
 routing.c     |   67 +++++++++++++-------------------------------------------
 send.c        |   20 +++++------------
 unicast.c     |   14 +----------
 6 files changed, 50 insertions(+), 101 deletions(-)

diff --git a/batman-adv/hash.h b/batman-adv/hash.h
index 3c48c6b..2b6a8ae 100644
--- a/batman-adv/hash.h
+++ b/batman-adv/hash.h
@@ -23,6 +23,7 @@
 #define _NET_BATMAN_ADV_HASH_H_
 
 #include <linux/list.h>
+#include "originator.h"
 
 /* callback to a compare function.  should
  * compare 2 element datas for their keys,
@@ -193,4 +194,21 @@ static inline void *hash_find(struct hashtable_t *hash,
 	return bucket_data;
 }
 
+/* increases the orig_node's refcount, if found */
+static inline struct orig_node *hash_find_orig(struct bat_priv *bat_priv,
+					       uint8_t *dest)
+{
+	struct hashtable_t *hash = bat_priv->orig_hash;
+	struct orig_node *orig_node;
+
+	rcu_read_lock();
+
+	orig_node = hash_find(hash, compare_orig, choose_orig, dest);
+	if (orig_node)
+		kref_get(&orig_node->refcount);
+
+	rcu_read_unlock();
+	return orig_node;
+}
+
 #endif /* _NET_BATMAN_ADV_HASH_H_ */
diff --git a/batman-adv/icmp_socket.c b/batman-adv/icmp_socket.c
index 869c9aa..90fb0b7 100644
--- a/batman-adv/icmp_socket.c
+++ b/batman-adv/icmp_socket.c
@@ -218,23 +218,16 @@ static ssize_t bat_socket_write(struct file *file, const char __user *buff,
 	if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
 		goto dst_unreach;
 
-	rcu_read_lock();
-	orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
-						   compare_orig, choose_orig,
-						   icmp_packet->dst));
-
+	orig_node = hash_find_orig(bat_priv, icmp_packet->dst);
 	if (!orig_node)
-		goto unlock;
+		goto dst_unreach;
 
-	kref_get(&orig_node->refcount);
+	rcu_read_lock();
 	neigh_node = orig_node->router;
-
-	if (!neigh_node)
-		goto unlock;
-
-	if (!atomic_inc_not_zero(&neigh_node->refcount)) {
+	if (!neigh_node || !atomic_inc_not_zero(&neigh_node->refcount)) {
 		neigh_node = NULL;
-		goto unlock;
+		rcu_read_unlock();
+		goto dst_unreach;
 	}
 
 	rcu_read_unlock();
@@ -255,8 +248,6 @@ static ssize_t bat_socket_write(struct file *file, const char __user *buff,
 	send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
 	goto out;
 
-unlock:
-	rcu_read_unlock();
 dst_unreach:
 	icmp_packet->msg_type = DESTINATION_UNREACHABLE;
 	bat_socket_add_packet(socket_client, icmp_packet, packet_len);
diff --git a/batman-adv/originator.c b/batman-adv/originator.c
index bde9778..af3f338 100644
--- a/batman-adv/originator.c
+++ b/batman-adv/originator.c
@@ -189,16 +189,9 @@ struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
 	int size;
 	int hash_added;
 
-	rcu_read_lock();
-	orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
-						   compare_orig, choose_orig,
-						   addr));
-	rcu_read_unlock();
-
-	if (orig_node) {
-		kref_get(&orig_node->refcount);
+	orig_node = hash_find_orig(bat_priv, addr);
+	if (orig_node)
 		return orig_node;
-	}
 
 	bat_dbg(DBG_BATMAN, bat_priv,
 		"Creating new originator: %pM\n", addr);
diff --git a/batman-adv/routing.c b/batman-adv/routing.c
index e53f6f5..081c461 100644
--- a/batman-adv/routing.c
+++ b/batman-adv/routing.c
@@ -897,22 +897,16 @@ static int recv_my_icmp_packet(struct bat_priv *bat_priv,
 
 	/* answer echo request (ping) */
 	/* get routing information */
-	rcu_read_lock();
-	orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
-						   compare_orig, choose_orig,
-						   icmp_packet->orig));
+	orig_node = hash_find_orig(bat_priv, icmp_packet->orig);
 	if (!orig_node)
-		goto unlock;
+		goto out;
 
-	kref_get(&orig_node->refcount);
+	rcu_read_lock();
 	neigh_node = orig_node->router;
-
-	if (!neigh_node)
-		goto unlock;
-
-	if (!atomic_inc_not_zero(&neigh_node->refcount)) {
+	if (!neigh_node || !atomic_inc_not_zero(&neigh_node->refcount)) {
 		neigh_node = NULL;
-		goto unlock;
+		rcu_read_unlock();
+		goto out;
 	}
 
 	rcu_read_unlock();
@@ -933,8 +927,6 @@ static int recv_my_icmp_packet(struct bat_priv *bat_priv,
 	ret = NET_RX_SUCCESS;
 	goto out;
 
-unlock:
-	rcu_read_unlock();
 out:
 	if (neigh_node)
 		neigh_node_free_ref(neigh_node);
@@ -992,22 +984,16 @@ int recv_icmp_packet(struct sk_buff *skb, struct batman_if *recv_if)
 		return recv_my_icmp_packet(bat_priv, skb, hdr_size);
 
 	/* get routing information */
-	rcu_read_lock();
-	orig_node = ((struct orig_node *)
-		     hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
-			       icmp_packet->dst));
+	orig_node = hash_find_orig(bat_priv, icmp_packet->dst);
 	if (!orig_node)
-		goto unlock;
+		goto out;
 
-	kref_get(&orig_node->refcount);
+	rcu_read_lock();
 	neigh_node = orig_node->router;
-
-	if (!neigh_node)
-		goto unlock;
-
-	if (!atomic_inc_not_zero(&neigh_node->refcount)) {
+	if (!neigh_node || !atomic_inc_not_zero(&neigh_node->refcount)) {
 		neigh_node = NULL;
-		goto unlock;
+		rcu_read_unlock();
+		goto out;
 	}
 
 	rcu_read_unlock();
@@ -1023,8 +1009,6 @@ int recv_icmp_packet(struct sk_buff *skb, struct batman_if *recv_if)
 	ret = NET_RX_SUCCESS;
 	goto out;
 
-unlock:
-	rcu_read_unlock();
 out:
 	if (neigh_node)
 		neigh_node_free_ref(neigh_node);
@@ -1214,16 +1198,9 @@ int route_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if,
 	struct sk_buff *new_skb;
 
 	/* get routing information */
-	rcu_read_lock();
-	orig_node = ((struct orig_node *)
-		     hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
-			       dest));
-
+	orig_node = hash_find_orig(bat_priv, dest);
 	if (!orig_node)
-		goto unlock;
-
-	kref_get(&orig_node->refcount);
-	rcu_read_unlock();
+		goto out;
 
 	/* find_router() increases neigh_nodes refcount if found. */
 	neigh_node = find_router(bat_priv, orig_node, recv_if);
@@ -1265,8 +1242,6 @@ int route_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if,
 	ret = NET_RX_SUCCESS;
 	goto out;
 
-unlock:
-	rcu_read_unlock();
 out:
 	if (neigh_node)
 		neigh_node_free_ref(neigh_node);
@@ -1364,16 +1339,9 @@ int recv_bcast_packet(struct sk_buff *skb, struct batman_if *recv_if)
 	if (is_my_mac(bcast_packet->orig))
 		goto out;
 
-	rcu_read_lock();
-	orig_node = ((struct orig_node *)
-		     hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
-			       bcast_packet->orig));
-
+	orig_node = hash_find_orig(bat_priv, bcast_packet->orig);
 	if (!orig_node)
-		goto rcu_unlock;
-
-	kref_get(&orig_node->refcount);
-	rcu_read_unlock();
+		goto out;
 
 	spin_lock_bh(&orig_node->bcast_seqno_lock);
 
@@ -1404,9 +1372,6 @@ int recv_bcast_packet(struct sk_buff *skb, struct batman_if *recv_if)
 	ret = NET_RX_SUCCESS;
 	goto out;
 
-rcu_unlock:
-	rcu_read_unlock();
-	goto out;
 spin_unlock:
 	spin_unlock_bh(&orig_node->bcast_seqno_lock);
 out:
diff --git a/batman-adv/send.c b/batman-adv/send.c
index f3e8895..df4cd34 100644
--- a/batman-adv/send.c
+++ b/batman-adv/send.c
@@ -76,22 +76,16 @@ static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
 		goto out;
 
 	/* get routing information */
-	rcu_read_lock();
-	orig_node = ((struct orig_node *)
-		     hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
-			       icmp_packet->orig));
+	orig_node = hash_find_orig(bat_priv, icmp_packet->orig);
 	if (!orig_node)
-		goto unlock;
+		goto out;
 
-	kref_get(&orig_node->refcount);
+	rcu_read_lock();
 	neigh_node = orig_node->router;
-
-	if (!neigh_node)
-		goto unlock;
-
-	if (!atomic_inc_not_zero(&neigh_node->refcount)) {
+	if (!neigh_node || !atomic_inc_not_zero(&neigh_node->refcount)) {
 		neigh_node = NULL;
-		goto unlock;
+		rcu_read_unlock();
+		goto out;
 	}
 
 	rcu_read_unlock();
@@ -112,8 +106,6 @@ static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
 	ret = NET_RX_SUCCESS;
 	goto out;
 
-unlock:
-	rcu_read_unlock();
 out:
 	if (neigh_node)
 		neigh_node_free_ref(neigh_node);
diff --git a/batman-adv/unicast.c b/batman-adv/unicast.c
index 4cbc011..4d794cb 100644
--- a/batman-adv/unicast.c
+++ b/batman-adv/unicast.c
@@ -184,15 +184,9 @@ int frag_reassemble_skb(struct sk_buff *skb, struct bat_priv *bat_priv,
 
 	*new_skb = NULL;
 
-	rcu_read_lock();
-	orig_node = ((struct orig_node *)
-		    hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
-			      unicast_packet->orig));
+	orig_node = hash_find_orig(bat_priv, unicast_packet->orig);
 	if (!orig_node)
-		goto unlock;
-
-	kref_get(&orig_node->refcount);
-	rcu_read_unlock();
+		goto out;
 
 	orig_node->last_frag_packet = jiffies;
 
@@ -217,10 +211,6 @@ int frag_reassemble_skb(struct sk_buff *skb, struct bat_priv *bat_priv,
 	if (*new_skb)
 		ret = NET_RX_SUCCESS;
 
-	goto out;
-
-unlock:
-	rcu_read_unlock();
 out:
 	if (orig_node)
 		kref_put(&orig_node->refcount, orig_node_free_ref);
-- 
1.7.2.3


  parent reply	other threads:[~2011-02-15 17:12 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-15 17:12 [B.A.T.M.A.N.] Redundancy bonding mode patches, v1 Linus Lüssing
2011-02-15 17:12 ` [B.A.T.M.A.N.] [PATCH 01/13] batman-adv: Remove unused hdr_size variable in route_unicast_packet() Linus Lüssing
2011-02-15 17:12 ` [B.A.T.M.A.N.] [PATCH 02/13] batman-adv: Add explicit batman header structure Linus Lüssing
2011-02-16  6:08   ` Andrew Lunn
2011-02-16 15:05     ` Linus Luessing
2011-02-15 17:12 ` [B.A.T.M.A.N.] [PATCH 03/13] batman-adv: Unify TTL handling Linus Lüssing
2011-02-16  6:16   ` Andrew Lunn
2011-02-16 14:42     ` Linus Luessing
2011-02-15 17:12 ` [B.A.T.M.A.N.] [PATCH 04/13] batman-adv: Make route_unicast_packet() packet_type independent Linus Lüssing
2011-02-15 17:12 ` Linus Lüssing [this message]
2011-02-15 17:12 ` [B.A.T.M.A.N.] [PATCH 06/13] batman-adv: Use route_unicast_packet() for sending own unicast packets Linus Lüssing
2011-02-15 17:12 ` [B.A.T.M.A.N.] [PATCH 07/13] batman-adv: Avoid redundant hash_find() call for " Linus Lüssing
2011-02-15 17:12 ` [B.A.T.M.A.N.] [PATCH 08/13] batman-adv: Use packet lists for unicast packet sending Linus Lüssing
2011-02-15 17:12 ` [B.A.T.M.A.N.] [PATCH 09/13] batman-adv: Add sysfs option for enabling redundant bonding mode Linus Lüssing
2011-02-15 17:12 ` [B.A.T.M.A.N.] [PATCH 10/13] batman-adv: Adding redundant bonding mode transmission Linus Lüssing
2011-02-15 17:12 ` [B.A.T.M.A.N.] [PATCH 11/13] batman-adv: Adding unicast_safe packet reception Linus Lüssing
2011-02-15 17:12 ` [B.A.T.M.A.N.] [PATCH 12/13] batman-adv: Generic sequence number checking for data packets Linus Lüssing
2011-02-15 17:12 ` [B.A.T.M.A.N.] [PATCH 13/13] batman-adv: Add sequence number and duplicate checks for unicasts_safe Linus Lüssing

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1297789948-16948-6-git-send-email-linus.luessing@ascom.ch \
    --to=linus.luessing@ascom.ch \
    --cc=b.a.t.m.a.n@lists.open-mesh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox