2
votes

I need some good examples on how to obtain the NAME of a net namespace from inside the kernel. Getting device names is straightforward but it's not so obvious for net namespaces. Does anyone know a simple method to get the net namespace name from inside the kernel.

The following code does print out all devices in all net namespaces, how do I get the name of the namespace to print from inside the kernel. The struct net structure does not point to the name as near as I can tell.

for_each_net(net)
    for_each_netdev(net, dev)
       printk("dev_name:  %s  netns: %p\n", dev->name, net);                                                            
2

2 Answers

1
votes

The kernel does not store namespaces using names. The names are only used for easy manipulation and usage of namespaces. A namespace with name NAME is generally stored on the file system as /var/run/netns/NAME Namespaces could be stored anywhere else on the filesystem as well. Docker, for example stores it's namespaces in /var/run/docker/netns

You could have a look at https://medium.com/@chughtapan/networking-namespaces-in-linux-b76070963964#.z1azv3467 to understand more about network namespaces.

1
votes

Indeed there is no notion of namespace names in the kernel. Regarding names of network namespaces under /var/run/netns - these represent only network namespaces which were created by the "ip netns add". When you create a network namespace not by "ip netns add", but by fork() or clone(), no entry is created under /var/run/netns.

Rami Rosen