When calles rtnl_dsfield_n2a(), we get the dsfield name from
/etc/iproute2/rt_dsfield. But different distribution may have
different names. So add a new parameter '-Numeric' to only show
the dsfield number.

This parameter is only used for tos value at present. We could enable
this for other fields if needed in the future.

Suggested-by: Phil Sutter <p...@nwl.cc>
Signed-off-by: Hangbin Liu <liuhang...@gmail.com>
---
 include/utils.h | 1 +
 ip/ip.c         | 6 +++++-
 lib/rt_names.c  | 4 +++-
 man/man8/ip.8   | 6 ++++++
 4 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/include/utils.h b/include/utils.h
index 8a9c3020..0f57ee97 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -34,6 +34,7 @@ extern int timestamp_short;
 extern const char * _SL_;
 extern int max_flush_loops;
 extern int batch_mode;
+extern int numeric;
 extern bool do_all;
 
 #ifndef CONFDIR
diff --git a/ip/ip.c b/ip/ip.c
index e4131714..2db5bbb0 100644
--- a/ip/ip.c
+++ b/ip/ip.c
@@ -36,6 +36,7 @@ int timestamp;
 int force;
 int max_flush_loops = 10;
 int batch_mode;
+int numeric;
 bool do_all;
 
 struct rtnl_handle rth = { .fd = -1 };
@@ -57,7 +58,8 @@ static void usage(void)
 "                    -4 | -6 | -I | -D | -M | -B | -0 |\n"
 "                    -l[oops] { maximum-addr-flush-attempts } | -br[ief] |\n"
 "                    -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] 
[filename] |\n"
-"                    -rc[vbuf] [size] | -n[etns] name | -a[ll] | -c[olor]}\n");
+"                    -rc[vbuf] [size] | -n[etns] name | -N[umeric]} | -a[ll] 
|\n"
+"                    -c[olor]}\n");
        exit(-1);
 }
 
@@ -287,6 +289,8 @@ int main(int argc, char **argv)
                        NEXT_ARG();
                        if (netns_switch(argv[1]))
                                exit(-1);
+               } else if (matches(opt, "-Numeric") == 0) {
+                       ++numeric;
                } else if (matches(opt, "-all") == 0) {
                        do_all = true;
                } else {
diff --git a/lib/rt_names.c b/lib/rt_names.c
index 66d5f2f0..122bdd74 100644
--- a/lib/rt_names.c
+++ b/lib/rt_names.c
@@ -27,6 +27,8 @@
 
 #define NAME_MAX_LEN 512
 
+int numeric;
+
 struct rtnl_hash_entry {
        struct rtnl_hash_entry  *next;
        const char              *name;
@@ -480,7 +482,7 @@ const char *rtnl_dsfield_n2a(int id, char *buf, int len)
                snprintf(buf, len, "%d", id);
                return buf;
        }
-       if (!rtnl_rtdsfield_tab[id]) {
+       if (!rtnl_rtdsfield_tab[id] && !numeric) {
                if (!rtnl_rtdsfield_init)
                        rtnl_rtdsfield_initialize();
        }
diff --git a/man/man8/ip.8 b/man/man8/ip.8
index f4cbfc03..c2a8a92d 100644
--- a/man/man8/ip.8
+++ b/man/man8/ip.8
@@ -47,6 +47,7 @@ ip \- show / manipulate routing, network devices, interfaces 
and tunnels
 \fB\-t\fR[\fIimestamp\fR] |
 \fB\-ts\fR[\fIhort\fR] |
 \fB\-n\fR[\fIetns\fR] name |
+\fB\-N\fR[\fIumeric\fR] |
 \fB\-a\fR[\fIll\fR] |
 \fB\-c\fR[\fIolor\fR] |
 \fB\-br\fR[\fIief\fR] |
@@ -174,6 +175,11 @@ to
 .RI "-n[etns] " NETNS " [ " OPTIONS " ] " OBJECT " { " COMMAND " | "
 .BR help " }"
 
+.TP
+.BR "\-N" , " \-Numeric"
+Print the dsfield's numeric directly instead of converting to the name from
+/etc/iproute2/rt_dsfield.
+
 .TP
 .BR "\-a" , " \-all"
 executes specified command over all objects, it depends if command
-- 
2.19.2

Reply via email to