batctl: Add support for network namespaces

Message ID 1453953259-30022-1-git-send-email-andrew@lunn.ch (mailing list archive)
State Superseded, archived
Delegated to: Marek Lindner
Headers

Commit Message

Andrew Lunn Jan. 28, 2016, 3:54 a.m. UTC
  When running within a network namespace, access to files within
debugfs have to take into account the network name space. Each
namespace has its own directory under
/sys/kernel/debug/batman_adv/netns.

Signed-off-by: Andrew Lunn <andrew@lunn.ch>
---
 debug.h   |  2 +-
 debugfs.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 49 insertions(+), 2 deletions(-)
  

Comments

Antonio Quartulli Jan. 28, 2016, 4:06 a.m. UTC | #1
On Thu, Jan 28, 2016 at 04:54:19AM +0100, Andrew Lunn wrote:
> When running within a network namespace, access to files within
> debugfs have to take into account the network name space. Each
> namespace has its own directory under
> /sys/kernel/debug/batman_adv/netns.

Thanks for providing the batctl patch Andrew.

What do you think about documenting this feature in the README file as well?
Imho a few words in the "How does it work?" section plus an example on how to
use it would be really nice!


Cheers,
  
Andrew Lunn Jan. 29, 2016, 3:01 a.m. UTC | #2
On Thu, Jan 28, 2016 at 12:06:38PM +0800, Antonio Quartulli wrote:
> On Thu, Jan 28, 2016 at 04:54:19AM +0100, Andrew Lunn wrote:
> > When running within a network namespace, access to files within
> > debugfs have to take into account the network name space. Each
> > namespace has its own directory under
> > /sys/kernel/debug/batman_adv/netns.
> 
> Thanks for providing the batctl patch Andrew.
> 
> What do you think about documenting this feature in the README file as well?
> Imho a few words in the "How does it work?" section plus an example on how to
> use it would be really nice!

Yes, i can do that. Here is a little example for three nodes which i
used for testing.

     Andrew

EMU1="ip netns exec emu1"
EMU2="ip netns exec emu2"

ip netns add emu1
ip netns add emu2

ip link add emu1-veth1 type veth peer name emu2-veth1
ip link set emu1-veth1 netns emu1
ip link set emu2-veth1 netns emu2

$EMU1 ip link set emu1-veth1 name veth1
$EMU2 ip link set emu2-veth1 name veth1

$EMU1 ip link set veth1 up
$EMU2 ip link set veth1 up

ip link add emu1-veth2 type veth peer name veth2
ip link set emu1-veth2 netns emu1
$EMU1 ip link set emu1-veth2 name veth2

$EMU1 ip link set veth2 up
ip link set veth2 up

$EMU1 batctl if add veth1
$EMU1 batctl if add veth2
$EMU1 ip link set bat0 up

$EMU2 batctl if add veth1
$EMU2 ip link set bat0 up

batctl if add veth2
ip link set bat0 up

# Give DAD time to complete
echo Waiting for DAD
sleep 4

($EMU1 alfred -m -i bat0 -u /var/run/emu1-alfred.soc) &
($EMU2 alfred -m -i bat0 -u /var/run/emu2-alfred.soc) &
alfred -m -i bat0 &

sleep 1

($EMU1 batadv-vis -s -u /var/run/emu1-alfred.soc) &
($EMU2 batadv-vis -s -u /var/run/emu2-alfred.soc) &
batadv-vis -s &
  

Patch

diff --git a/debug.h b/debug.h
index df65f50..ac7a97b 100644
--- a/debug.h
+++ b/debug.h
@@ -25,7 +25,7 @@ 
 #include <stddef.h>
 #include "main.h"
 
-#define DEBUG_BATIF_PATH_FMT "%s/batman_adv/%s"
+#define DEBUG_BATIF_PATH_FMT "%s/batman_adv/%s%s"
 #define DEBUG_TRANSTABLE_GLOBAL "transtable_global"
 #define DEBUG_LOG "log"
 #define DEBUG_ROUTING_ALGOS "routing_algos"
diff --git a/debugfs.c b/debugfs.c
index 3c58195..a66dbdd 100644
--- a/debugfs.c
+++ b/debugfs.c
@@ -20,11 +20,15 @@ 
 
 #include "debugfs.h"
 #include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
 #include <stdio.h>
 #include <string.h>
 #include <sys/mount.h>
 #include <sys/stat.h>
 #include <sys/statfs.h>
+#include <sys/types.h>
+#include <unistd.h>
 
 #ifndef DEBUGFS_MAGIC
 #define DEBUGFS_MAGIC          0x64626720
@@ -39,15 +43,58 @@  static const char *debugfs_known_mountpoints[] = {
 	NULL,
 };
 
+/* Return the current net namespace number. 0 is never a valid
+ * namespace, so use it to return that there is no name space
+ * support.
+ */
+
+static unsigned int debugfs_get_netns_inum(void)
+{
+	char net_path[] = "/proc/self/ns/net";
+	struct stat netst;
+	int netns;
+
+	netns = open(net_path, O_RDONLY);
+	if (netns < 0) {
+		if (errno == ENOENT)
+			/* Probably means no netns support in the kernel */
+			return 0;
+
+		fprintf(stderr,
+			"Error - can't open /proc/self/ns/net for read: %s\n",
+			strerror(errno));
+		return 0;
+	}
+
+	if (fstat(netns, &netst) < 0) {
+		fprintf(stderr, "Stat of netns failed: %s\n",
+			strerror(errno));
+		return 0;
+	}
+	close (netns);
+
+	return netst.st_ino;
+}
+
 /* construct a full path to a debugfs element */
 int debugfs_make_path(const char *fmt, char *mesh_iface, char *buffer, int size)
 {
+	unsigned int ns =  debugfs_get_netns_inum();
+	char ns_dir[PATH_MAX];
+
 	if (strlen(debugfs_mountpoint) == 0) {
 		buffer[0] = '\0';
 		return -1;
 	}
 
-	return snprintf(buffer, size, fmt, debugfs_mountpoint, mesh_iface);
+	if (ns) {
+		snprintf(ns_dir, PATH_MAX, "netns/%u/", ns);
+		return snprintf(buffer, size, fmt, debugfs_mountpoint, ns_dir,
+				mesh_iface);
+	} else {
+		return snprintf(buffer, size, fmt, debugfs_mountpoint, "",
+				mesh_iface);
+	}
 }
 
 static int debugfs_found;