Skip to contentSkip to conversation

reorganized kernel interface

B.A.T.M.A.N. In review Marek Lindner 4 participants 10 messages Feb 27, 2010 – Mar 1, 2010

lore
Share & tools
lore
#0 lore raw

Hi folks,

during the last batman developer meeting in Bruxelles we decided that it is time to reorganize the batman-adv kernel interface to address numerous issues and feature requests we have been hearing about throughout the last year. The required changes are extensive, therefore it will take a while to get it all sorted out.

Specifically we are targeting the following items:
* People with larger layer2 meshes have expressed the idea of having multiple meshes running on the same device in order to build a node which sits inbetween 2 (or more) mesh networks and routes the traffic from one to the other (thereby reducing the overhead). Due to code limitations batman-adv can only create a single mesh on a machine. There are numerous global variables and functions that need to be modfied in order to support multiple meshes.

* Despite the fact that we have a growing number of events which would allow user space applications to implement some intelligence to improve the mesh experience batman-adv has no way of communicating these events back to applications. To address this issue we will be moving batman-adv from /proc to sysfs which empowers batman-adv to trigger uevents that can be handled from user space.

* Since batman-adv's existence its parameter always were configurable using simple commands like echo or cat. Although this interface was easy it is not possible to direct error messages back to the shell, In case you misspelled a parameter you had to check the system logs to learn about it. The following patch introduces a way of exporting "allowed values", so that user space applications can parse it and alert the user immediately. See batctl as an example.

Please review and comment the following patches. As soon as those were digested I will send more.

Known issues:
* When sending a wrong input value to aggregate_ogm or bonding the kernel processes the input 3 times.

Cheers,
Marek

#1 lore raw

PATCH 1/2 batman-adv: convert multiple /proc files to use sysfs

This is the first patch in a series of patches which aim to convert all batman-adv /proc files to sysfs. To keep the changes in a digestable size it has been split up into smaller chunks. During the transition period batman-adv will use /proc as well as sysfs.

As a first step the following files have been converted:
aggregate_ogm, bonding, originators, transtable_global, transtable_local

---
 batman-adv-kernelland/Makefile.kbuild     |    2 +-
 batman-adv-kernelland/aggregation.c       |    5 +-
 batman-adv-kernelland/aggregation.h       |    3 +-
 batman-adv-kernelland/bat_sysfs.c         |  318 ++++++++++++++++++++++++++
 batman-adv-kernelland/bat_sysfs.h         |   26 +++
 batman-adv-kernelland/main.c              |   13 +-
 batman-adv-kernelland/main.h              |    1 -
 batman-adv-kernelland/originator.c        |   79 +++++++-
 batman-adv-kernelland/originator.h        |    1 +
 batman-adv-kernelland/proc.c              |  350 +----------------------------
 batman-adv-kernelland/proc.h              |    5 -
 batman-adv-kernelland/routing.c           |   29 ++-
 batman-adv-kernelland/routing.h           |    3 +-
 batman-adv-kernelland/send.c              |   17 +-
 batman-adv-kernelland/translation-table.c |   44 +++-
 batman-adv-kernelland/translation-table.h |    6 +-
 batman-adv-kernelland/types.h             |    3 +
 17 files changed, 512 insertions(+), 393 deletions(-)
 create mode 100644 batman-adv-kernelland/bat_sysfs.c
 create mode 100644 batman-adv-kernelland/bat_sysfs.h
batman-adv-kernelland/Makefile.kbuild +1 -1
index 3cee49c..b206bc6 100644
@@ -32,4 +32,4 @@ EXTRA_CFLAGS += -DREVISION_VERSION=\"r$(REVISION)\"
 endif
 
 obj-m += batman-adv.o
-batman-adv-objs := main.o proc.o send.o routing.o soft-interface.o device.o translation-table.o bitarray.o hash.o ring_buffer.o vis.o hard-interface.o aggregation.o originator.o gateway_common.o gateway_client.o $(shell [ "2" -eq "$(VERSION)" ] 2>&- && [ "6" -eq "$(PATCHLEVEL)" ] 2>&- && [ "$(SUBLEVEL)" -le "28" ] 2>&- && echo bat_printk.o)
+batman-adv-objs := main.o proc.o bat_sysfs.o send.o routing.o soft-interface.o device.o translation-table.o bitarray.o hash.o ring_buffer.o vis.o hard-interface.o aggregation.o originator.o gateway_common.o gateway_client.o $(shell [ "2" -eq "$(VERSION)" ] 2>&- && [ "6" -eq "$(PATCHLEVEL)" ] 2>&- && [ "$(SUBLEVEL)" -le "28" ] 2>&- && echo bat_printk.o)
batman-adv-kernelland/aggregation.c +3 -2
index 7917322..7b63be8 100644
@@ -159,7 +159,8 @@ static void aggregate(struct forw_packet *forw_packet_aggr,
 
 void add_bat_packet_to_list(unsigned char *packet_buff, int packet_len,
 			    struct batman_if *if_incoming, char own_packet,
-			    unsigned long send_time)
+			    unsigned long send_time,
+			    atomic_t *aggregation_enabled)
 {
 	/**
 	 * _aggr -> pointer to the packet we want to aggregate with
@@ -175,7 +176,7 @@ void add_bat_packet_to_list(unsigned char *packet_buff, int packet_len,
 	/* find position for the packet in the forward queue */
 	spin_lock_irqsave(&forw_bat_list_lock, flags);
 	/* own packets are not to be aggregated */
-	if ((atomic_read(&aggregation_enabled)) && (!own_packet)) {
+	if ((atomic_read(aggregation_enabled)) && (!own_packet)) {
 		hlist_for_each_entry(forw_packet_pos, tmp_node, &forw_bat_list,
 				     list) {
 			if (can_aggregate_with(batman_packet,
batman-adv-kernelland/aggregation.h +2 -1
index 6da8df9..27cb039 100644
@@ -32,6 +32,7 @@ static inline int aggregated_packet(int buff_pos, int packet_len, int num_hna)
 
 void add_bat_packet_to_list(unsigned char *packet_buff, int packet_len,
 			    struct batman_if *if_outgoing, char own_packet,
-			    unsigned long send_time);
+			    unsigned long send_time,
+			    atomic_t *aggregation_enabled);
 void receive_aggr_bat_packet(struct ethhdr *ethhdr, unsigned char *packet_buff,
 			     int packet_len, struct batman_if *if_incoming);
batman-adv-kernelland/bat_sysfs.c new file +318 -0

318 lines not on this page · raw message

batman-adv-kernelland/bat_sysfs.h new file +26 -0
new file mode 100644
index 0000000..d215c5d
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 20010 B.A.T.M.A.N. contributors:
+ *
+ * Marek Lindner
+ *
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA
+ *
+ */
+
+
+#define SYSFS_IF_MESH_SUBDIR "mesh"
+
+int sysfs_add_meshif(struct net_device *dev);
+void sysfs_del_meshif(struct net_device *dev);
batman-adv-kernelland/main.c +9 -4
index 5c5bde9..a1f7111 100644
@@ -21,6 +21,7 @@
 
 #include "main.h"
 #include "proc.h"
+#include "bat_sysfs.h"
 #include "routing.h"
 #include "send.h"
 #include "originator.h"
@@ -47,8 +48,6 @@ DEFINE_SPINLOCK(forw_bcast_list_lock);
 atomic_t originator_interval;
 atomic_t vis_interval;
 atomic_t vis_mode;
-atomic_t aggregation_enabled;
-atomic_t bonding_enabled;
 int16_t num_hna;
 int16_t num_ifs;
 
@@ -89,8 +88,6 @@ int init_module(void)
 	atomic_set(&vis_interval, 1000);/* TODO: raise this later, this is only
 					 * for debugging now. */
 	atomic_set(&vis_mode, VIS_TYPE_CLIENT_UPDATE);
-	atomic_set(&aggregation_enabled, 1);
-	atomic_set(&bonding_enabled, 0);
 	atomic_set(&gw_mode, GW_MODE_OFF);
 	atomic_set(&gw_srv_class, 0);
 	atomic_set(&gw_clnt_class, 0);
@@ -124,6 +121,11 @@ int init_module(void)
 		goto free_soft_device;
 	}
 
+	retval = sysfs_add_meshif(soft_device);
+
+	if (retval < 0)
+		goto unreg_soft_device;
+
 	register_netdevice_notifier(&hard_if_notifier);
 	dev_add_pack(&batman_adv_packet_type);
 
@@ -132,6 +134,8 @@ int init_module(void)
 
 	return 0;
 
+unreg_soft_device:
+	unregister_netdevice(soft_device);
 free_soft_device:
 	free_netdev(soft_device);
 	soft_device = NULL;
@@ -144,6 +148,7 @@ void cleanup_module(void)
 	shutdown_module();
 
 	if (soft_device) {
+		sysfs_del_meshif(soft_device);
 		unregister_netdev(soft_device);
 		soft_device = NULL;
 	}
batman-adv-kernelland/main.h +0 -1
index 2018c4e..307a6e6 100644
@@ -136,7 +136,6 @@ extern spinlock_t forw_bcast_list_lock;
 extern atomic_t originator_interval;
 extern atomic_t vis_interval;
 extern atomic_t vis_mode;
-extern atomic_t aggregation_enabled;
 extern atomic_t bonding_enabled;
 extern int16_t num_hna;
 extern int16_t num_ifs;
batman-adv-kernelland/originator.c +78 -1

94 lines not on this page · raw message

batman-adv-kernelland/originator.h +1 -0
index c1a9c2d..3d22259 100644
@@ -28,3 +28,4 @@ struct orig_node *get_orig_node(uint8_t *addr);
 struct neigh_node *
 create_neighbor(struct orig_node *orig_node, struct orig_node *orig_neigh_node,
 		uint8_t *neigh, struct batman_if *if_incoming);
+ssize_t orig_fill_buffer_text(char *buff, size_t count, loff_t off);
batman-adv-kernelland/proc.c +1 -349

422 lines not on this page · raw message

batman-adv-kernelland/proc.h +0 -5
index ae33057..6d74c54 100644
@@ -25,16 +25,11 @@
 #define PROC_ROOT_DIR "batman-adv"
 #define PROC_FILE_INTERFACES "interfaces"
 #define PROC_FILE_ORIG_INTERVAL "orig_interval"
-#define PROC_FILE_ORIGINATORS "originators"
 #define PROC_FILE_GATEWAYS "gateways"
 #define PROC_FILE_LOG "log"
 #define PROC_FILE_LOG_LEVEL "log_level"
-#define PROC_FILE_TRANST_LOCAL "transtable_local"
-#define PROC_FILE_TRANST_GLOBAL "transtable_global"
 #define PROC_FILE_VIS_SRV "vis_server"
 #define PROC_FILE_VIS_DATA "vis_data"
-#define PROC_FILE_AGGR "aggregate_ogm"
-#define PROC_FILE_BOND "bonding"
 #define PROC_FILE_GW_MODE "gateway_mode"
 #define PROC_FILE_GW_SRV_LIST "gateway_srv_list"
 
batman-adv-kernelland/routing.c +18 -11
index 47d5757..ff6fcbe 100644
@@ -364,33 +364,35 @@ static char count_real_packets(struct ethhdr *ethhdr,
 /* copy primary address for bonding */
 static void mark_bonding_address(struct orig_node *orig_node,
 				struct orig_node *orig_neigh_node,
-				struct batman_packet *batman_packet)
+				struct batman_packet *batman_packet,
+				atomic_t *bonding_enabled)
 
 {
 	/* don't care if bonding is not enabled */
-	if (!atomic_read(&bonding_enabled)) {
+	if (!atomic_read(bonding_enabled)) {
 		orig_node->bond.candidates = 0;
 		return;
 	}
 
 	if (batman_packet->flags & PRIMARIES_FIRST_HOP)
 		memcpy(orig_neigh_node->primary_addr,
-						orig_node->orig, ETH_ALEN);
-	 else
-		return;
+		       orig_node->orig, ETH_ALEN);
+
+	return;
 }
 
 /* mark possible bonding candidates in the neighbor list */
-void update_bonding_candidates(struct orig_node *orig_node)
+void update_bonding_candidates(struct orig_node *orig_node,
+			       atomic_t *bonding_enabled)
 {
 	int candidates;
 	int interference_candidate;
 	int best_tq;
 	struct neigh_node *tmp_neigh_node, *tmp_neigh_node2;
-    struct neigh_node *first_candidate, *last_candidate;
+	struct neigh_node *first_candidate, *last_candidate;
 
 	/* don't care if bonding is not enabled */
-	if (!atomic_read(&bonding_enabled)) {
+	if (!atomic_read(bonding_enabled)) {
 		orig_node->bond.candidates = 0;
 		return;
 	}
@@ -477,6 +479,8 @@ void receive_bat_packet(struct ethhdr *ethhdr,
 				unsigned char *hna_buff, int hna_buff_len,
 				struct batman_if *if_incoming)
 {
+	/* FIXME: each orig_node->batman_if will be attached to a softif */
+	struct bat_priv *bat_priv = netdev_priv(soft_device);
 	struct batman_if *batman_if;
 	struct orig_node *orig_neigh_node, *orig_node;
 	char has_directlink_flag;
@@ -638,8 +642,9 @@ void receive_bat_packet(struct ethhdr *ethhdr,
 		update_orig(orig_node, ethhdr, batman_packet,
 			    if_incoming, hna_buff, hna_buff_len, is_duplicate);
 
-	mark_bonding_address(orig_node, orig_neigh_node, batman_packet);
-	update_bonding_candidates(orig_node);
+	mark_bonding_address(orig_node, orig_neigh_node, batman_packet,
+			     &bat_priv->bonding_enabled);
+	update_bonding_candidates(orig_node, &bat_priv->bonding_enabled);
 
 	/* is single hop (direct) neighbor */
 	if (is_single_hop_neigh) {
@@ -928,6 +933,8 @@ int recv_icmp_packet(struct sk_buff *skb)
  * bonding if possible. */
 struct neigh_node *find_router(struct orig_node *orig_node)
 {
+	/* FIXME: each orig_node->batman_if will be attached to a softif */
+	struct bat_priv *bat_priv = netdev_priv(soft_device);
 	struct orig_node *primary_orig_node;
 	struct orig_node *router_orig;
 	struct neigh_node *router;
@@ -940,7 +947,7 @@ struct neigh_node *find_router(struct orig_node *orig_node)
 		return NULL;
 
 	/* don't care if bonding is not enabled */
-	if (!atomic_read(&bonding_enabled))
+	if (!atomic_read(&bat_priv->bonding_enabled))
 		return orig_node->router;
 
 	router_orig = orig_node->router->orig_node;
batman-adv-kernelland/routing.h +2 -1
index 725779f..51f7a8b 100644
@@ -38,4 +38,5 @@ int recv_vis_packet(struct sk_buff *skb);
 int recv_bat_packet(struct sk_buff *skb,
 				struct batman_if *batman_if);
 struct neigh_node *find_router(struct orig_node *orig_node);
-void update_bonding_candidates(struct orig_node *orig_node);
+void update_bonding_candidates(struct orig_node *orig_node,
+			       atomic_t *bonding_enabled);
batman-adv-kernelland/send.c +12 -5
index 3e63535..f692554 100644
@@ -47,11 +47,11 @@ static unsigned long own_send_time(void)
 }
 
 /* when do we schedule a forwarded packet to be sent */
-static unsigned long forward_send_time(void)
+static unsigned long forward_send_time(atomic_t *aggregation_enabled)
 {
 	unsigned long send_time = jiffies; /* Starting now plus... */
 
-	if (atomic_read(&aggregation_enabled))
+	if (atomic_read(aggregation_enabled))
 		send_time += (((MAX_AGGREGATION_MS - (JITTER/2) +
 				(random32() % JITTER)) * HZ) / 1000);
 	else
@@ -249,6 +249,8 @@ static void rebuild_batman_packet(struct batman_if *batman_if)
 
 void schedule_own_packet(struct batman_if *batman_if)
 {
+	/* FIXME: each batman_if will be attached to a softif */
+	struct bat_priv *bat_priv = netdev_priv(soft_device);
 	unsigned long send_time;
 	struct batman_packet *batman_packet;
 	int vis_server = atomic_read(&vis_mode);
@@ -289,7 +291,9 @@ void schedule_own_packet(struct batman_if *batman_if)
 	slide_own_bcast_window(batman_if);
 	send_time = own_send_time();
 	add_bat_packet_to_list(batman_if->packet_buff,
-			       batman_if->packet_len, batman_if, 1, send_time);
+			       batman_if->packet_len,
+			       batman_if, 1, send_time,
+			       &bat_priv->aggregation_enabled);
 }
 
 void schedule_forward_packet(struct orig_node *orig_node,
@@ -298,6 +302,8 @@ void schedule_forward_packet(struct orig_node *orig_node,
 			     uint8_t directlink, int hna_buff_len,
 			     struct batman_if *if_incoming)
 {
+	/* FIXME: each batman_if will be attached to a softif */
+	struct bat_priv *bat_priv = netdev_priv(soft_device);
 	unsigned char in_tq, in_ttl, tq_avg = 0;
 	unsigned long send_time;
 
@@ -343,10 +349,11 @@ void schedule_forward_packet(struct orig_node *orig_node,
 	else
 		batman_packet->flags &= ~DIRECTLINK;
 
-	send_time = forward_send_time();
+	send_time = forward_send_time(&bat_priv->aggregation_enabled);
 	add_bat_packet_to_list((unsigned char *)batman_packet,
 			       sizeof(struct batman_packet) + hna_buff_len,
-			       if_incoming, 0, send_time);
+			       if_incoming, 0, send_time,
+			       &bat_priv->aggregation_enabled);
 }
 
 static void forw_packet_free(struct forw_packet *forw_packet)
batman-adv-kernelland/translation-table.c +34 -10

95 lines not on this page · raw message

batman-adv-kernelland/translation-table.h +4 -2
index 281125b..fc1be65 100644
@@ -25,13 +25,15 @@ int hna_local_init(void);
 void hna_local_add(uint8_t *addr);
 void hna_local_remove(uint8_t *addr, char *message);
 int hna_local_fill_buffer(unsigned char *buff, int buff_len);
-int hna_local_fill_buffer_text(unsigned char *buff, int buff_len);
+int hna_local_fill_buffer_text(struct net_device *net_dev, char *buff,
+			       size_t count, loff_t off);
 void hna_local_purge(struct work_struct *work);
 void hna_local_free(void);
 int hna_global_init(void);
 void hna_global_add_orig(struct orig_node *orig_node, unsigned char *hna_buff,
 			 int hna_buff_len);
-int hna_global_fill_buffer_text(unsigned char *buff, int buff_len);
+int hna_global_fill_buffer_text(struct net_device *net_dev, char *buff,
+				size_t count, loff_t off);
 void _hna_global_del_orig(struct hna_global_entry *hna_global_entry,
 			  char *orig_str);
 void hna_global_del_orig(struct orig_node *orig_node, char *message);
batman-adv-kernelland/types.h +3 -0
index a82b52d..feb3343 100644
@@ -94,6 +94,9 @@ struct neigh_node {
 
 struct bat_priv {
 	struct net_device_stats stats;
+	atomic_t aggregation_enabled;
+	atomic_t bonding_enabled;
+	struct kobject *mesh_obj;
 };
 
 struct device_client {
Signature · 1 line
1.6.6.2
#2 lore raw

PATCH 2/2 batctl: follow /proc to sysfs conversion & introduce input validation

batman-adv is in the process of migrating to sysfs, hence batctl needs to adapt the file paths. In addition batctl makes use of the input validation support of the new files in sysfs.

---
 batctl/functions.c |    9 +++-
 batctl/functions.h |    3 +
 batctl/main.c      |    8 ++--
 batctl/proc.c      |   34 ---------------
 batctl/proc.h      |    4 --
 batctl/sys.c       |  121 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 batctl/sys.h       |   10 +++-
 7 files changed, 141 insertions(+), 48 deletions(-)
batctl/functions.c +7 -2
index 2ead2fc..cf4d264 100644
@@ -35,8 +35,6 @@
 #include "functions.h"
 #include "bat-hosts.h"
 
-#define BATMAN_ADV_TAG "batman-adv:"
-
 static struct timeval start_time;
 static char *host_name;
 char *line_ptr = NULL;
@@ -174,6 +172,13 @@ open:
 
 read:
 	while ((read = getline(&line_ptr, &len, fp)) != -1) {
+		if (read_opt & SEARCH_ARGS) {
+			/* omit log lines which don't start with the correct tag */
+			if (strncmp(line_ptr, SEARCH_ARGS_TAG, strlen(SEARCH_ARGS_TAG)) == 0)
+				break;
+
+			continue;
+		}
 
 		/* the buffer will be handled elsewhere */
 		if (read_opt & USE_READ_BUFF)
batctl/functions.h +3 -0
index 88fa53b..c21e9a3 100644
@@ -25,6 +25,8 @@
 
 
 #define ETH_STR_LEN 17
+#define BATMAN_ADV_TAG "batman-adv:"
+#define SEARCH_ARGS_TAG "commands:"
 
 /* return time delta from start to end in milliseconds */
 void start_timer(void);
@@ -45,4 +47,5 @@ enum {
 	USE_BAT_HOSTS = 0x04,
 	LOG_MODE = 0x08,
 	USE_READ_BUFF = 0x10,
+	SEARCH_ARGS = 0x20,
 };
batctl/main.c +4 -4
index 6e4493d..99bfb5b 100644
@@ -102,15 +102,15 @@ int main(int argc, char **argv)
 
 	} else if ((strcmp(argv[1], "originators") == 0) || (strcmp(argv[1], "o") == 0)) {
 
-		ret = handle_table(argc - 1, argv + 1, PROC_ORIGINATORS, originators_usage);
+		ret = handle_sys_table(argc - 1, argv + 1, SYS_ORIGINATORS, originators_usage);
 
 	} else if ((strcmp(argv[1], "translocal") == 0) || (strcmp(argv[1], "tl") == 0)) {
 
-		ret = handle_table(argc - 1, argv + 1, PROC_TRANSTABLE_LOCAL, trans_local_usage);
+		ret = handle_sys_table(argc - 1, argv + 1, SYS_TRANSTABLE_LOCAL, trans_local_usage);
 
 	} else if ((strcmp(argv[1], "transglobal") == 0) || (strcmp(argv[1], "tg") == 0)) {
 
-		ret = handle_table(argc - 1, argv + 1, PROC_TRANSTABLE_GLOBAL, trans_global_usage);
+		ret = handle_sys_table(argc - 1, argv + 1, SYS_TRANSTABLE_GLOBAL, trans_global_usage);
 
 	} else if ((strcmp(argv[1], "loglevel") == 0) || (strcmp(argv[1], "ll") == 0)) {
 
@@ -142,7 +142,7 @@ int main(int argc, char **argv)
 
 	} else if ((strcmp(argv[1], "aggregation") == 0) || (strcmp(argv[1], "ag") == 0)) {
 
-		ret = handle_proc_setting(argc - 1, argv + 1, PROC_AGGR, aggregation_usage);
+		ret = handle_sys_setting(argc - 1, argv + 1, SYS_AGGR, aggregation_usage);
 
 	} else if ((strcmp(argv[1], "bisect") == 0)) {
 
batctl/proc.c +0 -34
index 3294662..e323a52 100644
@@ -68,33 +68,6 @@ int interface(int argc, char **argv)
 	return EXIT_SUCCESS;
 }
 
-void originators_usage(void)
-{
-	printf("Usage: batctl [options] originators \n");
-	printf("options:\n");
-	printf(" \t -h print this help\n");
-	printf(" \t -n don't replace mac addresses with bat-host names\n");
-	printf(" \t -w watch mode - refresh the originator table continuously\n");
-}
-
-void trans_local_usage(void)
-{
-	printf("Usage: batctl [options] translocal \n");
-	printf("options:\n");
-	printf(" \t -h print this help\n");
-	printf(" \t -n don't replace mac addresses with bat-host names\n");
-	printf(" \t -w watch mode - refresh the local translation table continuously\n");
-}
-
-void trans_global_usage(void)
-{
-	printf("Usage: batctl [options] transglobal \n");
-	printf("options:\n");
-	printf(" \t -h print this help\n");
-	printf(" \t -n don't replace mac addresses with bat-host names\n");
-	printf(" \t -w watch mode - refresh the global translation table continuously\n");
-}
-
 void gw_srv_list_usage(void)
 {
 	printf("Usage: batctl [options] gw_srv_list \n");
@@ -118,13 +91,6 @@ void vis_server_usage(void)
 	printf(" \t -h print this help\n");
 }
 
-void aggregation_usage(void)
-{
-	printf("Usage: batctl [options] aggregation \n");
-	printf("options:\n");
-	printf(" \t -h print this help\n");
-}
-
 void gw_mode_usage(void)
 {
 	printf("Usage: batctl [options] gw_mode [mode]\n");
batctl/proc.h +0 -4
index d867b78..74ac398 100644
@@ -21,13 +21,9 @@
 
 #define PROC_ROOT_PATH "/proc/net/batman-adv/"
 #define PROC_INTERFACES "interfaces"
-#define PROC_ORIGINATORS "originators"
 #define PROC_ORIG_INTERVAL "orig_interval"
-#define PROC_TRANSTABLE_LOCAL "transtable_local"
-#define PROC_TRANSTABLE_GLOBAL "transtable_global"
 #define PROC_VIS_SERVER "vis_server"
 #define PROC_VIS_DATA "vis_data"
-#define PROC_AGGR "aggregate_ogm"
 #define PROC_GW_MODE "gateway_mode"
 #define PROC_GW_SRV_LIST "gateway_srv_list"
 
batctl/sys.c +119 -2

133 lines not on this page · raw message

batctl/sys.h +8 -2
index 812da63..0d6f334 100644
@@ -20,10 +20,16 @@
  */
 
 
-#define SYS_ROOT_PATH "/sys/module/batman_adv/"
+#define SYS_MODULE_PATH "/sys/module/batman_adv/"
+#define SYS_BATIF_PATH "/sys/devices/virtual/net/bat0/mesh/"
 #define SYS_LOG_LEVEL "parameters/debug"
 #define SYS_LOG "log"
-
+#define SYS_ORIGINATORS "originators"
+#define SYS_TRANSTABLE_LOCAL "transtable_local"
+#define SYS_TRANSTABLE_GLOBAL "transtable_global"
+#define SYS_AGGR "aggregate_ogm"
 
 int log_print(int argc, char **argv);
 int handle_loglevel(int argc, char **argv);
+int handle_sys_table(int argc, char **argv, char *file_path, void table_usage(void));
+int handle_sys_setting(int argc, char **argv, char *file_path, void setting_usage(void));
Signature · 1 line
1.6.6.2
#3 lore raw

Re: [B.A.T.M.A.N.] [PATCH 2/2] batctl: follow /proc to sysfs conversion & introduce input validation

Hi Marek!

I like :). This already looks well structured for introducing more than one bat-interface later. On the first test, I found two smaller issues:

Just noticed, that I don't have this /sys/devices/virtual folder on my Debian stable VM. But I think we should be on the safe side to use
#define SYS_BATIF_PATH "/sys/class/net/bat0/mesh/" instead, they look equivalent and I'm having them both avaiable on Debian stable (2.6.26 kernel) and Debian unstable (2.6.32 kernel).

The second thing is, I'm getting two harmless compile time warnings on the Debian stable VM here:
- bat-hosts.c:79/functions.c:174: warning: implicit declaration of function getline

Cheers, Linus

Quoted 328 lines · Marek Lindner

batman-adv is in the process of migrating to sysfs, hence batctl needs to adapt the file paths. In addition batctl makes use of the input validation support of the new files in sysfs.

---
 batctl/functions.c |    9 +++-
 batctl/functions.h |    3 +
 batctl/main.c      |    8 ++--
 batctl/proc.c      |   34 ---------------
 batctl/proc.h      |    4 --
 batctl/sys.c       |  121 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 batctl/sys.h       |   10 +++-
 7 files changed, 141 insertions(+), 48 deletions(-)
batctl/functions.c +7 -2
index 2ead2fc..cf4d264 100644
@@ -35,8 +35,6 @@
 #include "functions.h"
 #include "bat-hosts.h"
 
-#define BATMAN_ADV_TAG "batman-adv:"
-
 static struct timeval start_time;
 static char *host_name;
 char *line_ptr = NULL;
@@ -174,6 +172,13 @@ open:
 
 read:
 	while ((read = getline(&line_ptr, &len, fp)) != -1) {
+		if (read_opt & SEARCH_ARGS) {
+			/* omit log lines which don't start with the correct tag */
+			if (strncmp(line_ptr, SEARCH_ARGS_TAG, strlen(SEARCH_ARGS_TAG)) == 0)
+				break;
+
+			continue;
+		}
 
 		/* the buffer will be handled elsewhere */
 		if (read_opt & USE_READ_BUFF)
batctl/functions.h +3 -0
index 88fa53b..c21e9a3 100644
@@ -25,6 +25,8 @@
 
 
 #define ETH_STR_LEN 17
+#define BATMAN_ADV_TAG "batman-adv:"
+#define SEARCH_ARGS_TAG "commands:"
 
 /* return time delta from start to end in milliseconds */
 void start_timer(void);
@@ -45,4 +47,5 @@ enum {
 	USE_BAT_HOSTS = 0x04,
 	LOG_MODE = 0x08,
 	USE_READ_BUFF = 0x10,
+	SEARCH_ARGS = 0x20,
 };
batctl/main.c +4 -4
index 6e4493d..99bfb5b 100644
@@ -102,15 +102,15 @@ int main(int argc, char **argv)
 
 	} else if ((strcmp(argv[1], "originators") == 0) || (strcmp(argv[1], "o") == 0)) {
 
-		ret = handle_table(argc - 1, argv + 1, PROC_ORIGINATORS, originators_usage);
+		ret = handle_sys_table(argc - 1, argv + 1, SYS_ORIGINATORS, originators_usage);
 
 	} else if ((strcmp(argv[1], "translocal") == 0) || (strcmp(argv[1], "tl") == 0)) {
 
-		ret = handle_table(argc - 1, argv + 1, PROC_TRANSTABLE_LOCAL, trans_local_usage);
+		ret = handle_sys_table(argc - 1, argv + 1, SYS_TRANSTABLE_LOCAL, trans_local_usage);
 
 	} else if ((strcmp(argv[1], "transglobal") == 0) || (strcmp(argv[1], "tg") == 0)) {
 
-		ret = handle_table(argc - 1, argv + 1, PROC_TRANSTABLE_GLOBAL, trans_global_usage);
+		ret = handle_sys_table(argc - 1, argv + 1, SYS_TRANSTABLE_GLOBAL, trans_global_usage);
 
 	} else if ((strcmp(argv[1], "loglevel") == 0) || (strcmp(argv[1], "ll") == 0)) {
 
@@ -142,7 +142,7 @@ int main(int argc, char **argv)
 
 	} else if ((strcmp(argv[1], "aggregation") == 0) || (strcmp(argv[1], "ag") == 0)) {
 
-		ret = handle_proc_setting(argc - 1, argv + 1, PROC_AGGR, aggregation_usage);
+		ret = handle_sys_setting(argc - 1, argv + 1, SYS_AGGR, aggregation_usage);
 
 	} else if ((strcmp(argv[1], "bisect") == 0)) {
 
batctl/proc.c +0 -34
index 3294662..e323a52 100644
@@ -68,33 +68,6 @@ int interface(int argc, char **argv)
 	return EXIT_SUCCESS;
 }
 
-void originators_usage(void)
-{
-	printf("Usage: batctl [options] originators \n");
-	printf("options:\n");
-	printf(" \t -h print this help\n");
-	printf(" \t -n don't replace mac addresses with bat-host names\n");
-	printf(" \t -w watch mode - refresh the originator table continuously\n");
-}
-
-void trans_local_usage(void)
-{
-	printf("Usage: batctl [options] translocal \n");
-	printf("options:\n");
-	printf(" \t -h print this help\n");
-	printf(" \t -n don't replace mac addresses with bat-host names\n");
-	printf(" \t -w watch mode - refresh the local translation table continuously\n");
-}
-
-void trans_global_usage(void)
-{
-	printf("Usage: batctl [options] transglobal \n");
-	printf("options:\n");
-	printf(" \t -h print this help\n");
-	printf(" \t -n don't replace mac addresses with bat-host names\n");
-	printf(" \t -w watch mode - refresh the global translation table continuously\n");
-}
-
 void gw_srv_list_usage(void)
 {
 	printf("Usage: batctl [options] gw_srv_list \n");
@@ -118,13 +91,6 @@ void vis_server_usage(void)
 	printf(" \t -h print this help\n");
 }
 
-void aggregation_usage(void)
-{
-	printf("Usage: batctl [options] aggregation \n");
-	printf("options:\n");
-	printf(" \t -h print this help\n");
-}
-
 void gw_mode_usage(void)
 {
 	printf("Usage: batctl [options] gw_mode [mode]\n");
batctl/proc.h +0 -4
index d867b78..74ac398 100644
@@ -21,13 +21,9 @@
 
 #define PROC_ROOT_PATH "/proc/net/batman-adv/"
 #define PROC_INTERFACES "interfaces"
-#define PROC_ORIGINATORS "originators"
 #define PROC_ORIG_INTERVAL "orig_interval"
-#define PROC_TRANSTABLE_LOCAL "transtable_local"
-#define PROC_TRANSTABLE_GLOBAL "transtable_global"
 #define PROC_VIS_SERVER "vis_server"
 #define PROC_VIS_DATA "vis_data"
-#define PROC_AGGR "aggregate_ogm"
 #define PROC_GW_MODE "gateway_mode"
 #define PROC_GW_SRV_LIST "gateway_srv_list"
 
batctl/sys.c +119 -2
index d67a915..65f1ebd 100644
@@ -94,11 +94,11 @@ int handle_loglevel(int argc, char **argv)
 	}
 
 	if (argc != 1) {
-		res = write_file(SYS_ROOT_PATH, SYS_LOG_LEVEL, argv[1]);
+		res = write_file(SYS_MODULE_PATH, SYS_LOG_LEVEL, argv[1]);
 		goto out;
 	}
 
-	res = read_file(SYS_ROOT_PATH, SYS_LOG_LEVEL, SINGLE_READ | USE_READ_BUFF);
+	res = read_file(SYS_MODULE_PATH, SYS_LOG_LEVEL, SINGLE_READ | USE_READ_BUFF);
 
 	if (res != EXIT_SUCCESS)
 		goto out;
@@ -118,3 +118,120 @@ out:
 
 	return res;
 }
+
+void originators_usage(void)
+{
+	printf("Usage: batctl [options] originators \n");
+	printf("options:\n");
+	printf(" \t -h print this help\n");
+	printf(" \t -n don't replace mac addresses with bat-host names\n");
+	printf(" \t -w watch mode - refresh the originator table continuously\n");
+}
+
+void trans_local_usage(void)
+{
+	printf("Usage: batctl [options] translocal \n");
+	printf("options:\n");
+	printf(" \t -h print this help\n");
+	printf(" \t -n don't replace mac addresses with bat-host names\n");
+	printf(" \t -w watch mode - refresh the local translation table continuously\n");
+}
+
+void trans_global_usage(void)
+{
+	printf("Usage: batctl [options] transglobal \n");
+	printf("options:\n");
+	printf(" \t -h print this help\n");
+	printf(" \t -n don't replace mac addresses with bat-host names\n");
+	printf(" \t -w watch mode - refresh the global translation table continuously\n");
+}
+
+int handle_sys_table(int argc, char **argv, char *file_path, void table_usage(void))
+{
+	int optchar, read_opt = USE_BAT_HOSTS;
+
+	while ((optchar = getopt(argc, argv, "hnw")) != -1) {
+		switch (optchar) {
+		case 'h':
+			table_usage();
+			return EXIT_SUCCESS;
+		case 'n':
+			read_opt &= ~USE_BAT_HOSTS;
+			break;
+		case 'w':
+			read_opt |= CLR_CONT_READ;
+			break;
+		default:
+			table_usage();
+			return EXIT_FAILURE;
+		}
+	}
+
+	return read_file(SYS_BATIF_PATH, file_path, read_opt);
+}
+
+void aggregation_usage(void)
+{
+	printf("Usage: batctl [options] aggregation \n");
+	printf("options:\n");
+	printf(" \t -h print this help\n");
+}
+
+int handle_sys_setting(int argc, char **argv, char *file_path, void setting_usage(void))
+{
+	int optchar, res;
+	char *space_ptr, *comma_char, *cmds = NULL;
+
+	while ((optchar = getopt(argc, argv, "h")) != -1) {
+		switch (optchar) {
+		case 'h':
+			setting_usage();
+			return EXIT_SUCCESS;
+		default:
+			setting_usage();
+			return EXIT_FAILURE;
+		}
+	}
+
+	if (argc == 1)
+		return read_file(SYS_BATIF_PATH, file_path, SINGLE_READ);
+
+	res = read_file(SYS_BATIF_PATH, file_path, SEARCH_ARGS);
+	if (res != EXIT_SUCCESS)
+		return res;
+
+	while ((space_ptr = strchr(line_ptr, ' ')) != NULL) {
+		*space_ptr = '\0';
+
+		if (strncmp(line_ptr, SEARCH_ARGS_TAG, strlen(SEARCH_ARGS_TAG)) == 0) {
+			cmds = space_ptr + 1;
+			goto next;
+		}
+
+		comma_char = NULL;
+		if (line_ptr[strlen(line_ptr) - 1] == ',') {
+			comma_char = line_ptr + strlen(line_ptr) - 1;
+			*comma_char = '\0';
+		}
+
+		if (strcmp(line_ptr, argv[1]) == 0)
+			goto write_file;
+
+		*space_ptr = ' ';
+		if (comma_char)
+			*comma_char = ',';
+
+next:
+		line_ptr = space_ptr + 1;
+	}
+
+	if (!cmds)
+		goto write_file;
+
+	printf("Error - the supplied argument is invalid: %s\n", argv[1]);
+	printf("The following values are allowed: %s", cmds);
+	return EXIT_FAILURE;
+
+write_file:
+	return write_file(SYS_BATIF_PATH, file_path, argv[1]);
+}
batctl/sys.h +8 -2
index 812da63..0d6f334 100644
@@ -20,10 +20,16 @@
  */
 
 
-#define SYS_ROOT_PATH "/sys/module/batman_adv/"
+#define SYS_MODULE_PATH "/sys/module/batman_adv/"
+#define SYS_BATIF_PATH "/sys/devices/virtual/net/bat0/mesh/"
 #define SYS_LOG_LEVEL "parameters/debug"
 #define SYS_LOG "log"
-
+#define SYS_ORIGINATORS "originators"
+#define SYS_TRANSTABLE_LOCAL "transtable_local"
+#define SYS_TRANSTABLE_GLOBAL "transtable_global"
+#define SYS_AGGR "aggregate_ogm"
 
 int log_print(int argc, char **argv);
 int handle_loglevel(int argc, char **argv);
+int handle_sys_table(int argc, char **argv, char *file_path, void table_usage(void));
+int handle_sys_setting(int argc, char **argv, char *file_path, void setting_usage(void));
Signature · 1 line
1.6.6.2
#4 lore raw

Re: [B.A.T.M.A.N.] [PATCH 2/2] batctl: follow /proc to sysfs conversion & introduce input validation

Quoted 4 lines

The second thing is, I'm getting two harmless compile time warnings on the Debian stable VM here:
- bat-hosts.c:79/functions.c:174: warning: implicit declaration of function getline

getline(3) is a GNU extension. The man page says:

#define _GNU_SOURCE
#include <stdio.h>

ssize_t getline(char **lineptr, size_t *n, FILE *stream);

I guess it compiles fine on uclibc without the _GNU_SOURCE, but glibc wants you to explicitly enable the extension so you know you are not POSIX compatible.

BTW: batctl has used this for a while now. Anybody had problems with this GNU extension? Is it available for all the C libraries used in embedded systems which use BATMAN?

Andrew

#5 lore raw

Re: [B.A.T.M.A.N.] [PATCH 2/2] batctl: follow /proc to sysfs conversion & introduce input validation

Quoted 3 lines · On Monday 01 March 2010 14:06:51 Andrew Lunn wrote:

BTW: batctl has used this for a while now. Anybody had problems with this GNU extension? Is it available for all the C libraries used in embedded systems which use BATMAN?

I used it since a while on OpenWRT and it works fine for me.

Cheers,
Marek

#6 lore raw

Re: [B.A.T.M.A.N.] [PATCH 2/2] batctl: follow /proc to sysfs conversion & introduce input validation

In reply to Linus Lüssing

Hey,

Quoted 2 lines

I like :). This already looks well structured for introducing more than one bat-interface later.

glad to hear that! :)

Quoted 6 lines

Just noticed, that I don't have this /sys/devices/virtual folder on my Debian stable VM. But I think we should be on the safe side to use
#define SYS_BATIF_PATH "/sys/class/net/bat0/mesh/" instead, they look equivalent and I'm having them both avaiable on Debian stable (2.6.26 kernel) and Debian unstable (2.6.32 kernel).

The path "/sys/class" simply contains symlinks to the real paths. On my OpenWRT it looks like this:
ls -l /sys/class/net/
bat0 -> ../../devices/virtual/net/bat0
br-lan -> ../../devices/virtual/net/br-lan
lo -> ../../devices/virtual/net/lo

If the virtual path does not exist on your debian where does the symlink point to ? Btw, on my debian (2.6.32) the virtual path exists but we use /sys/class/net instead to be on the safe side.

Regards,
Marek

#7 lore raw

Re: [B.A.T.M.A.N.] [PATCH 1/2] batman-adv: convert multiple /proc files to use sysfs

In reply to Marek Lindner

Hi Marek

I've not looked into the details yet, just a quick glans. However maybe we should discuss this point:

Quoted 21 lines
batman-adv-kernelland/send.c +5 -1
@@ -249,6 +249,8 @@ static void rebuild_batman_packet(struct batman_if *batman_if)
 
 void schedule_own_packet(struct batman_if *batman_if)
 {
+	/* FIXME: each batman_if will be attached to a softif */
+	struct bat_priv *bat_priv = netdev_priv(soft_device);
 	unsigned long send_time;
 	struct batman_packet *batman_packet;
 	int vis_server = atomic_read(&vis_mode);
@@ -289,7 +291,9 @@ void schedule_own_packet(struct batman_if *batman_if)
 	slide_own_bcast_window(batman_if);
 	send_time = own_send_time();
 	add_bat_packet_to_list(batman_if->packet_buff,
-			       batman_if->packet_len, batman_if, 1, send_time);
+			       batman_if->packet_len,
+			       batman_if, 1, send_time,
+			       &bat_priv->aggregation_enabled);
 }
 
 

struct bat_priv {

batman-adv-kernelland/types.h struct neigh_node { · line 100

Quoted 5 lines
batman-adv-kernelland/types.h +3 -0
 	struct net_device_stats stats;
+	atomic_t aggregation_enabled;
+	atomic_t bonding_enabled;
+	struct kobject *mesh_obj;
 };

With time i expect that bat_priv will gain many more members as global variables are replaced with variables per mesh. Rather than passing to functions a member, pass the pointer and let the function dereference it. I think it will make the APIs neater in the long run.

Andrew

#8 lore raw

Re: [B.A.T.M.A.N.] [PATCH 1/2] batman-adv: convert multiple /proc files to use sysfs

Quoted 4 lines · On Sunday 28 February 2010 17:37:28 Andrew Lunn wrote:

With time i expect that bat_priv will gain many more members as global variables are replaced with variables per mesh. Rather than passing to functions a member, pass the pointer and let the function dereference it. I think it will make the APIs neater in the long run.

I agree - in the long run that will be better. I'll put that onto my todo list.

Regards,
Marek

#9 lore raw

Re: [B.A.T.M.A.N.] [PATCH 1/2] batman-adv: convert multiple /proc files to use sysfs

In reply to Marek Lindner
Quoted 4 lines · Marek Lindner

This is the first patch in a series of patches which aim to convert all batman-adv /proc files to sysfs. To keep the changes in a digestable size it has been split up into smaller chunks. During the transition period batman-adv will use /proc as well as sysfs.

[snip]

batman-adv-kernelland/bat_sysfs.c line 2

Quoted 6 lines

index 0000000..9502cf6

batman-adv-kernelland/bat_sysfs.c new file +2 -0
@@ -0,0 +1,318 @@
+/*
+ * Copyright (C) 20010 B.A.T.M.A.N. contributors:
                      ^

I know you're looking toward the future but isn't 20000 years a bit much?

Gus