Skip to contentSkip to conversation

lsns: add netnsid column

PATCH Superseded Masatake YAMATO 2 participants 9 messages Nov 15, 2017 – Nov 24, 2017 Util-Linux package

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 lsns: add netnsid column

Linux network subsystem assigns an unique integer to a network namespace.

term0# ip netns add UTIL-LINUX-LSNS-TEST-NS
term0# ip netns list
UTIL-LINUX-LSNS-TEST-NS
term0# ip link add name lsns-vetha type veth peer name lsns-vethb term0 # ip link set lsns-vethb netns UTIL-LINUX-LSNS-TEST-NS term0# ip netns list
UTIL-LINUX-LSNS-TEST-NS (id: 0)
term0# ip link show dev lsns-vetha
230: lsns-vetha@if229: <BROADCAST,MULTICAST> mtu 1500 qdisc noop ...

         link/ether 3e:27:68:ba:b3:95 brd ff:ff:ff:ff:ff:ff link-netnsid 0

In this example 0 is assigned to UTIL-LINUX-LSNS-TEST-NS net namespace. The name, UTIL-LINUX-LSNS-TEST-NS, and it semantics is given and defined by iproute2 in userland; and nothing to do with util-linux.

However, the id, 0, is managed in linux kernel. If lsns can show the ids, it helps users understand the state of network namespaces.

This commit adds NETNSID column to the output.

Here is an example of session:

term0# ip netns exec UTIL-LINUX-LSNS-TEST-NS cat

(Open another terminal)

term1# ./lsns --type net
NS TYPE NPROCS PID USER NETNSID COMMAND
4026531993 net 383 1 root unassigned /usr/lib/systemd/...
4026532433 net 1 1219 rtkit unassigned /usr/libexec/rtkit-daemon
4026532562 net 1 18201 root 0 cat

0 is shown as NETNSID for the cat process.

For the initial name space, "unassigned" is printed. For the namespaces other type than net, "n/a" is printed. If an error occurred during getting the id, "n/a" is printed.

---
 configure.ac     |   1 +
 sys-utils/lsns.c | 190 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 189 insertions(+), 2 deletions(-)
configure.ac +1 -0
index eebe92259..b2ad5ea98 100644
@@ -223,6 +223,7 @@ AC_CHECK_HEADERS([ \
 	linux/tiocl.h \
 	linux/version.h \
 	linux/securebits.h \
+	linux/net_namespace.h \
 	locale.h \
 	mntent.h \
 	net/if.h \
sys-utils/lsns.c +188 -2

286 lines not on this page · raw message

Signature · 1 line
2.13.6
#1 lore raw

PATCH 2/2 lsns: add a case for testing netnsid column

---
 tests/commands.sh           |  1 +
 tests/expected/lsns/netnsid |  1 +
 tests/ts/lsns/netnsid       | 65 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+)
 create mode 100644 tests/expected/lsns/netnsid
 create mode 100644 tests/ts/lsns/netnsid
tests/commands.sh +1 -0
index 655344102..502935ede 100644
@@ -74,6 +74,7 @@ TS_CMD_LOSETUP=${TS_CMD_LOSETUP:-"$top_builddir/losetup"}
 TS_CMD_LSBLK=${TS_CMD_LSBLK-"$top_builddir/lsblk"}
 TS_CMD_LSCPU=${TS_CMD_LSCPU-"$top_builddir/lscpu"}
 TS_CMD_LSMEM=${TS_CMD_LSMEM-"$top_builddir/lsmem"}
+TS_CMD_LSNS=${TS_CMD_LSNS-"$top_builddir/lsns"}
 TS_CMD_MCOOKIE=${TS_CMD_MCOOKIE-"$top_builddir/mcookie"}
 TS_CMD_MKCRAMFS=${TS_CMD_MKCRAMFS:-"$top_builddir/mkfs.cramfs"}
 TS_CMD_MKMINIX=${TS_CMD_MKMINIX:-"$top_builddir/mkfs.minix"}
tests/expected/lsns/netnsid new file +1 -0
new file mode 100644
index 000000000..573541ac9
@@ -0,0 +1 @@
+0
tests/ts/lsns/netnsid new file +65 -0

65 lines not on this page · raw message

Signature · 1 line
2.13.6
maintainer
#2 lore raw
In reply to Masatake YAMATO
Quoted 12 lines · Masatake YAMATO

Linux network subsystem assigns an unique integer to a network namespace.

term0# ip netns add UTIL-LINUX-LSNS-TEST-NS
term0# ip netns list
UTIL-LINUX-LSNS-TEST-NS
term0# ip link add name lsns-vetha type veth peer name lsns-vethb term0 # ip link set lsns-vethb netns UTIL-LINUX-LSNS-TEST-NS term0# ip netns list
UTIL-LINUX-LSNS-TEST-NS (id: 0)
term0# ip link show dev lsns-vetha
230: lsns-vetha@if229: <BROADCAST,MULTICAST> mtu 1500 qdisc noop ...

Quoted 7 lines
         link/ether 3e:27:68:ba:b3:95 brd ff:ff:ff:ff:ff:ff link-netnsid 0

In this example 0 is assigned to UTIL-LINUX-LSNS-TEST-NS net namespace. The name, UTIL-LINUX-LSNS-TEST-NS, and it semantics is given and defined by iproute2 in userland; and nothing to do with util-linux.

However, the id, 0, is managed in linux kernel. If lsns can show the ids, it helps users understand the state of network namespaces.

Where iproute2 stores the logical name of the namespace (e.g. UTIL-LINUX-LSNS-TEST-NS)? Cannot we read it? :-)

sys-utils/lsns.c static int cmp_namespaces(struct list_head *a, struct list_head *b, · line 584

Quoted 10 lines
sys-utils/lsns.c +10 -0
+static int netnsid_xasputs(char **str, int netnsid)
+{
+	if (netnsid >= 0)
+		return xasprintf(&str, "%d", netnsid);
+#ifdef NETNSA_NSID_NOT_ASSIGNED
+	else if (netnsid == NETNSA_NSID_NOT_ASSIGNED)
+		return xasprintf(&str, "%s", "unassigned");
+#endif
+	else
+		return xasprintf(&str, "%s", "n/a");

We usually use blank space rather than n/a if data are not available. In this case "return NULL;" is good enough.

sys-utils/lsns.c int main(int argc, char *argv[]) · line 946

Quoted 5 lines
sys-utils/lsns.c +5 -0
+#ifdef HAVE_LINUX_NET_NAMESPACE_H
+	netlink_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
+#endif
+	if (netlink_fd < 0)
+		netlink_fd = -errno;

I guess this if() is unnecessary. The rest of the code check for netlink_fd < 0.

It would be also nice to call socket() only if NETNSID column is wanted.

What about to add NETNSID collumn to the default output only if "--type net" specified on lsns command line?

I'm ready to do all the changes (you don't have to resend the patch).

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#3 lore raw
Quoted 18 lines · Karel Zak
Quoted 17 lines · Masatake YAMATO

Linux network subsystem assigns an unique integer to a network namespace.

term0# ip netns add UTIL-LINUX-LSNS-TEST-NS
term0# ip netns list
UTIL-LINUX-LSNS-TEST-NS
term0# ip link add name lsns-vetha type veth peer name lsns-vethb term0 # ip link set lsns-vethb netns UTIL-LINUX-LSNS-TEST-NS term0# ip netns list
UTIL-LINUX-LSNS-TEST-NS (id: 0)
term0# ip link show dev lsns-vetha
230: lsns-vetha@if229: <BROADCAST,MULTICAST> mtu 1500 qdisc noop ...

         link/ether 3e:27:68:ba:b3:95 brd ff:ff:ff:ff:ff:ff link-netnsid 0

In this example 0 is assigned to UTIL-LINUX-LSNS-TEST-NS net namespace. The name, UTIL-LINUX-LSNS-TEST-NS, and it semantics is given and defined by iproute2 in userland; and nothing to do with util-linux.

Quoted 5 lines
Quoted 2 lines

However, the id, 0, is managed in linux kernel. If lsns can show the ids, it helps users understand the state of network namespaces.

Where iproute2 stores the logical name of the namespace (e.g. UTIL-LINUX-LSNS-TEST-NS)? Cannot we read it? :-)

I found I don't understand the mechanism of namespace well enough to answer this question. So I made study.

Before studying I thought logical name of the namespace is just iproute2 local hack. iproute2 stores the name to a file under /var/run/netns with using "bind mount". Though iproute2 is very popular software, I thought a low level program like lsns should rely on and utilize only kernel to get runtime information. I assumed making a mount point under /var/run/netns was just private rule of iproute2.

After I studied I found it is not just iproute2 and net namespace private things.

nsfs provides kernel level interface for logical name:

commit 0226f4923f6c9b40cfa1c1c1b19a6ac6b3924ead
Author: Al Viro <[email protected]>
Date: Tue Dec 6 12:21:54 2011 -0500

vfs: take /proc/*/mounts and friends to fs/proc_namespace.c

rationale: that stuff is far tighter bound to fs/namespace.c than to the guts of procfs proper.

[yamato@master]~/var/util-linux% cat /proc/self/mountinfo | grep nsfs
247 240 0:3 net:[4026532562] /run/netns/UTIL-LINUX-LSNS-TEST-NS rw shared:190 - nsfs nsfs rw,seclabel 270 26 0:3 net:[4026532562] /run/netns/UTIL-LINUX-LSNS-TEST-NS rw shared:190 - nsfs nsfs rw,seclabel

By parsing this file (with libmount), we can say /run/netns/UTIL-LINUX-LSNS-TEST-NS is the logical name for net:[4026532562] either the mount point is created by ip command or not.

"ip link show" still uses netnsid, so I think my patch still makes sense. However, as you wrote, making a column for logical name, is more useful. It is not limited to net namespace.

A namespace can have more than one logical names.
I wonder how to print them in a column.

Quoted 23 lines

sys-utils/lsns.c static int cmp_namespaces(struct list_head *a, struct list_head *b, · line 584

Quoted 10 lines
sys-utils/lsns.c +10 -0
+static int netnsid_xasputs(char **str, int netnsid)
+{
+	if (netnsid >= 0)
+		return xasprintf(&str, "%d", netnsid);
+#ifdef NETNSA_NSID_NOT_ASSIGNED
+	else if (netnsid == NETNSA_NSID_NOT_ASSIGNED)
+		return xasprintf(&str, "%s", "unassigned");
+#endif
+	else
+		return xasprintf(&str, "%s", "n/a");

We usually use blank space rather than n/a if data are not available. In this case "return NULL;" is good enough.

sys-utils/lsns.c int main(int argc, char *argv[]) · line 946

Quoted 5 lines
sys-utils/lsns.c +5 -0
+#ifdef HAVE_LINUX_NET_NAMESPACE_H
+	netlink_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
+#endif
+	if (netlink_fd < 0)
+		netlink_fd = -errno;

I guess this if() is unnecessary. The rest of the code check for netlink_fd < 0.

Quoted 4 lines

It would be also nice to call socket() only if NETNSID column is wanted.

What about to add NETNSID collumn to the default output only if "--type net" specified on lsns command line?

I think it is good idea.

Masatake YAMATO

Quoted 7 lines

I'm ready to do all the changes (you don't have to resend the patch).

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
maintainer
#4 lore raw
Quoted 4 lines · Masatake YAMATO

By parsing this file (with libmount), we can say /run/netns/UTIL-LINUX-LSNS-TEST-NS is the logical name for net:[4026532562] either the mount point is created by ip command or not.

Nice. Yes, all you need is:

   tab = mnt_new_table_from_file(_PATH_PROC_MOUNTINFO);

.. see for example sys-utils/fstrim.c, and mnt_fs_get_fstype() and mnt_fs_get_source().

Quoted 6 lines

"ip link show" still uses netnsid, so I think my patch still makes sense. However, as you wrote, making a column for logical name, is more useful. It is not limited to net namespace.

A namespace can have more than one logical names.
I wonder how to print them in a column.

libsmart supports multi-line cells:

* use SCOLS_FL_WRAP for the column

* set wrap function, for example in lsns.c: init_scols_table():

      cl =  scols_table_new_column(tab, col->name, col->whint, flags)
 
      if (flags & SCOLS_FL_WRAP) {
                scols_column_set_wrapfunc(cl,
                                scols_wrapnl_chunksize,
                                scols_wrapnl_nextchunk,
                                NULL);
                scols_column_set_safechars(cl, "\n");
      }

* and in add_scols_line() where you compose output add to the table string where namespace names are separated by '\n', e.g. "aaa\nbbb"

This feature should be probably disabled for --raw, maybe add --nowrap and in this case use "," as separator and remove SCOLS_FL_WRAP for the column in init_scols_table().

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#5 lore raw

So, my biggest question is "should I send an updated version of patch?" :-)

You wrote in https://marc.info/?l=util-linux-ng&m=151074416720085&w=2:

Quoted 1 line

I'm ready to do all the changes (you don't have to resend the patch).

I think you may have improved version of code.
I would like to start from it for reflecting the new comments (if I should do).

Masatake YAMATO

Quoted 50 lines · Masatake YAMATO
Quoted 4 lines

By parsing this file (with libmount), we can say /run/netns/UTIL-LINUX-LSNS-TEST-NS is the logical name for net:[4026532562] either the mount point is created by ip command or not.

Nice. Yes, all you need is:

   tab = mnt_new_table_from_file(_PATH_PROC_MOUNTINFO);

.. see for example sys-utils/fstrim.c, and mnt_fs_get_fstype() and mnt_fs_get_source().

Quoted 6 lines

"ip link show" still uses netnsid, so I think my patch still makes sense. However, as you wrote, making a column for logical name, is more useful. It is not limited to net namespace.

A namespace can have more than one logical names.
I wonder how to print them in a column.

libsmart supports multi-line cells:

* use SCOLS_FL_WRAP for the column

* set wrap function, for example in lsns.c: init_scols_table():

      cl =  scols_table_new_column(tab, col->name, col->whint, flags)
 
      if (flags & SCOLS_FL_WRAP) {
                scols_column_set_wrapfunc(cl,
                                scols_wrapnl_chunksize,
                                scols_wrapnl_nextchunk,
                                NULL);
                scols_column_set_safechars(cl, "\n");
      }

* and in add_scols_line() where you compose output add to the table string where namespace names are separated by '\n', e.g. "aaa\nbbb"

This feature should be probably disabled for --raw, maybe add --nowrap and in this case use "," as separator and remove SCOLS_FL_WRAP for the column in init_scols_table().

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
maintainer
#6 lore raw
Quoted 7 lines · Masatake YAMATO

So, my biggest question is "should I send an updated version of patch?" :-)

You wrote in https://marc.info/?l=util-linux-ng&m=151074416720085&w=2:

Quoted 1 line

I'm ready to do all the changes (you don't have to resend the patch).

I think you may have improved version of code.
I would like to start from it for reflecting the new comments (if I should do).

I do not have any changes yet ;-) So, you can start, or if you're busy with something else I can do that.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#7 lore raw

Hi,

Quoted 2 lines

I do not have any changes yet ;-) So, you can start, or if you're busy with something else I can do that.

It works:

# ./lsns --type=net
        NS TYPE NPROCS   PID USER     NETNSID NSFS           COMMAND
4026531993 net     405     1 root  unassigned                /usr/lib/systemd/systemd --switched-root --system --deserialize 25
4026532433 net       1  1515 rtkit unassigned                /usr/libexec/rtkit-daemon
4026532579 net       1 10432 root  unassigned /run/netns/QWE bash
                                              /tmp/RTY
Quoted 3 lines

This feature should be probably disabled for --raw, maybe add --nowrap and in this case use "," as separator and remove SCOLS_FL_WRAP for the column in init_scols_table().

It seems that nsfs is still not so popular.
So I think enabling the column only when --type net is given, as you suggested about netnsid.

I would like to make clarify what you meant.

If both --raw and -o NSFS are given, what I should do? Hidden NSFS anyway? or Use ',' as separator instead of '\n'?

Masatake YAMATO

maintainer
#8 lore raw
Quoted 6 lines · Masatake YAMATO

Hi,

Quoted 2 lines

I do not have any changes yet ;-) So, you can start, or if you're busy with something else I can do that.

It works:

Cool!

Quoted 7 lines
# ./lsns --type=net
        NS TYPE NPROCS   PID USER     NETNSID NSFS           COMMAND
4026531993 net     405     1 root  unassigned                /usr/lib/systemd/systemd --switched-root --system --deserialize 25
4026532433 net       1  1515 rtkit unassigned                /usr/libexec/rtkit-daemon
4026532579 net       1 10432 root  unassigned /run/netns/QWE bash
                                              /tmp/RTY
Quoted 7 lines
Quoted 3 lines

This feature should be probably disabled for --raw, maybe add --nowrap and in this case use "," as separator and remove SCOLS_FL_WRAP for the column in init_scols_table().

It seems that nsfs is still not so popular.
So I think enabling the column only when --type net is given, as you suggested about netnsid.

Yes.

Quoted 4 lines

I would like to make clarify what you meant.

If both --raw and -o NSFS are given, what I should do? Hidden NSFS anyway? or Use ',' as separator instead of '\n'?

I prefer ',' as separator and add to the man page a note that NSFS uses multi-line cells (to DESCRIPTION where we already have soemthing about --output).

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com