public inbox for b.a.t.m.a.n@lists.open-mesh.org
 help / color / mirror / Atom feed
From: Sven Eckelmann <sven@narfation.org>
To: b.a.t.m.a.n@lists.open-mesh.org
Subject: [B.A.T.M.A.N.] [PATCH 11/13] batman-adv: Directly print to seq_file in vis
Date: Wed, 16 May 2012 20:23:23 +0200	[thread overview]
Message-ID: <1337192605-8652-11-git-send-email-sven@narfation.org> (raw)
In-Reply-To: <1337192605-8652-1-git-send-email-sven@narfation.org>

The vis output doesn't need to be buffered in an character buffer before it can
be send to the userspace program that reads from the vis debug file.

Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
 vis.c |  228 ++++++++++++++++++++++++-----------------------------------------
 1 file changed, 83 insertions(+), 145 deletions(-)

diff --git a/vis.c b/vis.c
index 6b7a1c0..3095c41 100644
--- a/vis.c
+++ b/vis.c
@@ -140,72 +140,117 @@ static void batadv_vis_data_insert_interface(const uint8_t *interface,
 	hlist_add_head(&entry->list, if_list);
 }
 
-static ssize_t batadv_vis_prim_sec(char *buff, const struct hlist_head *if_list)
+static void batadv_vis_data_read_prim_sec(struct seq_file *seq,
+					  const struct hlist_head *if_list)
 {
 	struct if_list_entry *entry;
 	struct hlist_node *pos;
-	size_t len = 0;
 
 	hlist_for_each_entry(entry, pos, if_list, list) {
 		if (entry->primary)
-			len += sprintf(buff + len, "PRIMARY, ");
+			seq_printf(seq, "PRIMARY, ");
 		else
-			len += sprintf(buff + len,  "SEC %pM, ", entry->addr);
+			seq_printf(seq,  "SEC %pM, ", entry->addr);
 	}
-
-	return len;
-}
-
-static size_t batadv_vis_cnt_prim_sec(struct hlist_head *if_list)
-{
-	struct if_list_entry *entry;
-	struct hlist_node *pos;
-	size_t count = 0;
-
-	hlist_for_each_entry(entry, pos, if_list, list) {
-		if (entry->primary)
-			count += 9;
-		else
-			count += 23;
-	}
-
-	return count;
 }
 
 /* read an entry  */
-static ssize_t batadv_vis_data_read_entry(char *buff,
+static ssize_t batadv_vis_data_read_entry(struct seq_file *seq,
 					  const struct vis_info_entry *entry,
 					  const uint8_t *src, bool primary)
 {
-	/* maximal length: max(4+17+2, 3+17+1+3+2) == 26 */
 	if (primary && entry->quality == 0)
-		return sprintf(buff, "TT %pM, ", entry->dest);
+		return seq_printf(seq, "TT %pM, ", entry->dest);
 	else if (batadv_compare_eth(entry->src, src))
-		return sprintf(buff, "TQ %pM %d, ", entry->dest,
-			       entry->quality);
+		return seq_printf(seq, "TQ %pM %d, ", entry->dest,
+				  entry->quality);
 
 	return 0;
 }
 
+static void batadv_vis_data_insert_interfaces(struct hlist_head *list,
+					      struct vis_packet *packet,
+					      struct vis_info_entry *entries)
+{
+	int i;
+
+	for (i = 0; i < packet->entries; i++) {
+		if (entries[i].quality == 0)
+			continue;
+
+		if (batadv_compare_eth(entries[i].src, packet->vis_orig))
+			continue;
+
+		batadv_vis_data_insert_interface(entries[i].src, list, false);
+	}
+}
+
+static void batadv_vis_data_read_entries(struct seq_file *seq,
+					 struct hlist_head *list,
+					 struct vis_packet *packet,
+					 struct vis_info_entry *entries)
+{
+	int i;
+	struct if_list_entry *entry;
+	struct hlist_node *pos;
+
+	hlist_for_each_entry(entry, pos, list, list) {
+		seq_printf(seq, "%pM,", entry->addr);
+
+		for (i = 0; i < packet->entries; i++)
+			batadv_vis_data_read_entry(seq, &entries[i],
+						   entry->addr, entry->primary);
+
+		/* add primary/secondary records */
+		if (batadv_compare_eth(entry->addr, packet->vis_orig))
+			batadv_vis_data_read_prim_sec(seq, list);
+
+		seq_printf(seq, "\n");
+	}
+}
+
+static void batadv_vis_seq_print_text_bucket(struct seq_file *seq,
+					     const struct hlist_head *head)
+{
+	struct hlist_node *node;
+	struct vis_info *info;
+	struct vis_packet *packet;
+	uint8_t *entries_pos;
+	struct vis_info_entry *entries;
+	struct if_list_entry *entry;
+	struct hlist_node *pos, *n;
+
+	HLIST_HEAD(vis_if_list);
+
+	hlist_for_each_entry_rcu(info, node, head, hash_entry) {
+		packet = (struct vis_packet *)info->skb_packet->data;
+		entries_pos = (uint8_t *)packet + sizeof(*packet);
+		entries = (struct vis_info_entry *)entries_pos;
+
+		batadv_vis_data_insert_interface(packet->vis_orig, &vis_if_list,
+						 true);
+		batadv_vis_data_insert_interfaces(&vis_if_list, packet,
+						  entries);
+		batadv_vis_data_read_entries(seq, &vis_if_list, packet,
+					     entries);
+
+		hlist_for_each_entry_safe(entry, pos, n, &vis_if_list, list) {
+			hlist_del(&entry->list);
+			kfree(entry);
+		}
+	}
+}
+
 int batadv_vis_seq_print_text(struct seq_file *seq, void *offset)
 {
 	struct hard_iface *primary_if;
-	struct hlist_node *node;
 	struct hlist_head *head;
-	struct vis_info *info;
-	struct vis_packet *packet;
-	struct vis_info_entry *entries;
 	struct net_device *net_dev = (struct net_device *)seq->private;
 	struct bat_priv *bat_priv = netdev_priv(net_dev);
 	struct hashtable_t *hash = bat_priv->vis_hash;
-	HLIST_HEAD(vis_if_list);
-	struct if_list_entry *entry;
-	struct hlist_node *pos, *n;
 	uint32_t i;
-	int j, ret = 0;
+	int ret = 0;
 	int vis_server = atomic_read(&bat_priv->vis_mode);
-	size_t buff_pos, buf_size;
-	char *buff;
 
 	primary_if = batadv_primary_if_get_selected(bat_priv);
 	if (!primary_if)
@@ -214,120 +259,13 @@ int batadv_vis_seq_print_text(struct seq_file *seq, void *offset)
 	if (vis_server == VIS_TYPE_CLIENT_UPDATE)
 		goto out;
 
-	buf_size = 1;
-	/* Estimate length */
 	spin_lock_bh(&bat_priv->vis_hash_lock);
 	for (i = 0; i < hash->size; i++) {
 		head = &hash->table[i];
-
-		rcu_read_lock();
-		hlist_for_each_entry_rcu(info, node, head, hash_entry) {
-			packet = (struct vis_packet *)info->skb_packet->data;
-			entries = (struct vis_info_entry *)
-				((char *)packet + sizeof(*packet));
-
-			batadv_vis_data_insert_interface(packet->vis_orig,
-							 &vis_if_list, true);
-
-			for (j = 0; j < packet->entries; j++) {
-				if (entries[j].quality == 0)
-					continue;
-				if (batadv_compare_eth(entries[j].src,
-						       packet->vis_orig))
-					continue;
-				batadv_vis_data_insert_interface(entries[j].src,
-								 &vis_if_list,
-								 false);
-			}
-
-			hlist_for_each_entry(entry, pos, &vis_if_list, list) {
-				buf_size += 18 + 26 * packet->entries;
-
-				/* add primary/secondary records */
-				if (batadv_compare_eth(entry->addr,
-						       packet->vis_orig))
-					buf_size +=
-					  batadv_vis_cnt_prim_sec(&vis_if_list);
-
-				buf_size += 1;
-			}
-
-			hlist_for_each_entry_safe(entry, pos, n, &vis_if_list,
-						  list) {
-				hlist_del(&entry->list);
-				kfree(entry);
-			}
-		}
-		rcu_read_unlock();
+		batadv_vis_seq_print_text_bucket(seq, head);
 	}
-
-	buff = kmalloc(buf_size, GFP_ATOMIC);
-	if (!buff) {
-		spin_unlock_bh(&bat_priv->vis_hash_lock);
-		ret = -ENOMEM;
-		goto out;
-	}
-	buff[0] = '\0';
-	buff_pos = 0;
-
-	for (i = 0; i < hash->size; i++) {
-		head = &hash->table[i];
-
-		rcu_read_lock();
-		hlist_for_each_entry_rcu(info, node, head, hash_entry) {
-			packet = (struct vis_packet *)info->skb_packet->data;
-			entries = (struct vis_info_entry *)
-				((char *)packet + sizeof(*packet));
-
-			batadv_vis_data_insert_interface(packet->vis_orig,
-							 &vis_if_list, true);
-
-			for (j = 0; j < packet->entries; j++) {
-				if (entries[j].quality == 0)
-					continue;
-				if (batadv_compare_eth(entries[j].src,
-						       packet->vis_orig))
-					continue;
-				batadv_vis_data_insert_interface(entries[j].src,
-								 &vis_if_list,
-								 false);
-			}
-
-			hlist_for_each_entry(entry, pos, &vis_if_list, list) {
-				buff_pos += sprintf(buff + buff_pos, "%pM,",
-						entry->addr);
-
-				for (j = 0; j < packet->entries; j++)
-					buff_pos += batadv_vis_data_read_entry(
-							buff + buff_pos,
-							&entries[j],
-							entry->addr,
-							entry->primary);
-
-				/* add primary/secondary records */
-				if (batadv_compare_eth(entry->addr,
-						       packet->vis_orig))
-					buff_pos +=
-					 batadv_vis_prim_sec(buff + buff_pos,
-							     &vis_if_list);
-
-				buff_pos += sprintf(buff + buff_pos, "\n");
-			}
-
-			hlist_for_each_entry_safe(entry, pos, n, &vis_if_list,
-						  list) {
-				hlist_del(&entry->list);
-				kfree(entry);
-			}
-		}
-		rcu_read_unlock();
-	}
-
 	spin_unlock_bh(&bat_priv->vis_hash_lock);
 
-	seq_printf(seq, "%s", buff);
-	kfree(buff);
-
 out:
 	if (primary_if)
 		batadv_hardif_free_ref(primary_if);
-- 
1.7.10


  parent reply	other threads:[~2012-05-16 18:23 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-16 17:38 [B.A.T.M.A.N.] Remaining cleanup/prefixing patches Sven Eckelmann
2012-05-16 18:23 ` [B.A.T.M.A.N.] [PATCH 01/13] batman-adv: Prefix routing local static functions with batadv_ Sven Eckelmann
2012-05-16 18:23   ` [B.A.T.M.A.N.] [PATCH 02/13] batman-adv: Prefix send " Sven Eckelmann
2012-05-18  8:26     ` Marek Lindner
2012-05-16 18:23   ` [B.A.T.M.A.N.] [PATCH 03/13] batman-adv: Prefix soft-interface " Sven Eckelmann
2012-05-18  8:28     ` Marek Lindner
2012-05-16 18:23   ` [B.A.T.M.A.N.] [PATCH 04/13] batman-adv: Prefix translation-table " Sven Eckelmann
2012-05-18  8:34     ` Marek Lindner
2012-05-16 18:23   ` [B.A.T.M.A.N.] [PATCH 05/13] batman-adv: Prefix unicast " Sven Eckelmann
2012-05-18  8:36     ` Marek Lindner
2012-05-16 18:23   ` [B.A.T.M.A.N.] [PATCH 06/13] batman-adv: Prefix vis " Sven Eckelmann
2012-05-18  9:58     ` Marek Lindner
2012-05-16 18:23   ` [B.A.T.M.A.N.] [PATCH 07/13] batman-adv: Prefix main " Sven Eckelmann
2012-05-18 12:37     ` Marek Lindner
2012-05-16 18:23   ` [B.A.T.M.A.N.] [PATCH 08/13] batman-adv: Mark only locally used symbol batadv_tt_local_crc as static Sven Eckelmann
2012-05-18 12:44     ` Marek Lindner
2012-05-16 18:23   ` [B.A.T.M.A.N.] [PATCH 09/13] batman-adv: Replace simple function like defines with functions Sven Eckelmann
2012-05-18 14:41     ` Marek Lindner
2012-05-16 18:23   ` [B.A.T.M.A.N.] [PATCH 10/13] batman-adv: Prefix remaining function like macros with batadv_ Sven Eckelmann
2012-05-18 15:00     ` Marek Lindner
2012-05-16 18:23   ` Sven Eckelmann [this message]
2012-05-18 15:03     ` [B.A.T.M.A.N.] [PATCH 11/13] batman-adv: Directly print to seq_file in vis Marek Lindner
2012-05-16 18:23   ` [B.A.T.M.A.N.] [PATCH 12/13] batman-adv: Remove unused define BAT_ATTR_HIF_UINT Sven Eckelmann
2012-05-18 15:16     ` [B.A.T.M.A.N.] [PATCH] batman-adv: Prefix bat_sysfs local static functions with batadv_ Sven Eckelmann
2012-05-18 16:01       ` Marek Lindner
2012-05-16 18:23   ` [B.A.T.M.A.N.] [PATCH 13/13] batman-adv: Use typedef instead of define for type dat_addr_t Sven Eckelmann
2012-05-18 16:03     ` Marek Lindner
2012-05-18  8:24   ` [B.A.T.M.A.N.] [PATCH 01/13] batman-adv: Prefix routing local static functions with batadv_ Marek Lindner

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=1337192605-8652-11-git-send-email-sven@narfation.org \
    --to=sven@narfation.org \
    --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