]> git.proxmox.com Git - mirror_iproute2.git/commitdiff
ip{,6}tunnel: have a shared stats parser/printer
authorPhil Sutter <phil@nwl.cc>
Fri, 18 Dec 2015 10:58:06 +0000 (11:58 +0100)
committerStephen Hemminger <shemming@brocade.com>
Fri, 18 Dec 2015 19:46:21 +0000 (11:46 -0800)
This has a slight side-effect of not aborting when /proc/net/dev is
malformed, but OTOH stats are not parsed for uninteresting interfaces.

Signed-off-by: Phil Sutter <phil@nwl.cc>
ip/ip6tunnel.c
ip/iptunnel.c
ip/tunnel.c
ip/tunnel.h

index 1737d884319ecb55a8deaca8cf41c532480b1462..7a3cd0461fff10caa7fc41bfed6953763791ee3f 100644 (file)
@@ -341,10 +341,6 @@ static int do_tunnels_list(struct ip6_tnl_parm2 *p)
        while (fgets(buf, sizeof(buf), fp) != NULL) {
                char name[IFNAMSIZ];
                int index, type;
-               unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
-                       rx_fifo, rx_frame,
-                       tx_bytes, tx_packets, tx_errs, tx_drops,
-                       tx_fifo, tx_colls, tx_carrier, rx_multi;
                struct ip6_tnl_parm2 p1;
                char *ptr;
 
@@ -354,12 +350,6 @@ static int do_tunnels_list(struct ip6_tnl_parm2 *p)
                        fprintf(stderr, "Wrong format for /proc/net/dev. Giving up.\n");
                        goto end;
                }
-               if (sscanf(ptr, "%lu%lu%lu%lu%lu%lu%lu%*d%lu%lu%lu%lu%lu%lu%lu",
-                          &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
-                          &rx_fifo, &rx_frame, &rx_multi,
-                          &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
-                          &tx_fifo, &tx_colls, &tx_carrier) != 14)
-                       continue;
                if (p->name[0] && strcmp(p->name, name))
                        continue;
                index = ll_name_to_index(name);
@@ -385,15 +375,8 @@ static int do_tunnels_list(struct ip6_tnl_parm2 *p)
                if (!ip6_tnl_parm_match(p, &p1))
                        continue;
                print_tunnel(&p1);
-               if (show_stats) {
-                       printf("%s", _SL_);
-                       printf("RX: Packets    Bytes        Errors CsumErrs OutOfSeq Mcasts%s", _SL_);
-                       printf("    %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s",
-                              rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi, _SL_);
-                       printf("TX: Packets    Bytes        Errors DeadLoop NoRoute  NoBufs%s", _SL_);
-                       printf("    %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld",
-                              tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops);
-               }
+               if (show_stats)
+                       tnl_print_stats(ptr);
                printf("\n");
        }
        err = 0;
index a3ff99bd87eb8c29c8a2003b9a0532f85c2a28b8..65a4e6e9c1a5a96ad5811f62ee4ec2f9aa2e43ba 100644 (file)
@@ -405,10 +405,6 @@ static int do_tunnels_list(struct ip_tunnel_parm *p)
        while (fgets(buf, sizeof(buf), fp) != NULL) {
                char name[IFNAMSIZ];
                int index, type;
-               unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
-                       rx_fifo, rx_frame,
-                       tx_bytes, tx_packets, tx_errs, tx_drops,
-                       tx_fifo, tx_colls, tx_carrier, rx_multi;
                struct ip_tunnel_parm p1;
                char *ptr;
 
@@ -419,12 +415,6 @@ static int do_tunnels_list(struct ip_tunnel_parm *p)
                        fprintf(stderr, "Wrong format for /proc/net/dev. Giving up.\n");
                        goto end;
                }
-               if (sscanf(ptr, "%lu%lu%lu%lu%lu%lu%lu%*d%lu%lu%lu%lu%lu%lu%lu",
-                          &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
-                          &rx_fifo, &rx_frame, &rx_multi,
-                          &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
-                          &tx_fifo, &tx_colls, &tx_carrier) != 14)
-                       continue;
                if (p->name[0] && strcmp(p->name, name))
                        continue;
                index = ll_name_to_index(name);
@@ -447,15 +437,8 @@ static int do_tunnels_list(struct ip_tunnel_parm *p)
                    (p->i_key && p1.i_key != p->i_key))
                        continue;
                print_tunnel(&p1);
-               if (show_stats) {
-                       printf("%s", _SL_);
-                       printf("RX: Packets    Bytes        Errors CsumErrs OutOfSeq Mcasts%s", _SL_);
-                       printf("    %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s",
-                              rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi, _SL_);
-                       printf("TX: Packets    Bytes        Errors DeadLoop NoRoute  NoBufs%s", _SL_);
-                       printf("    %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld",
-                              tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops);
-               }
+               if (show_stats)
+                       tnl_print_stats(ptr);
                printf("\n");
        }
        err = 0;
index 79f2201f2356f9d271aa9933ff056d5231ceb33e..1dd809227fed447af5c8a1eae2a6073df86b336d 100644 (file)
@@ -195,3 +195,31 @@ __be32 tnl_parse_key(const char *name, const char *key)
        }
        return htonl(uval);
 }
+
+/* tnl_print_stats - print tunnel statistics
+ *
+ * @buf - tunnel interface's line in /proc/net/dev,
+ *        starting past the interface name and following colon
+ */
+void tnl_print_stats(const char *buf)
+{
+       unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
+                     rx_fifo, rx_frame,
+                     tx_bytes, tx_packets, tx_errs, tx_drops,
+                     tx_fifo, tx_colls, tx_carrier, rx_multi;
+
+       if (sscanf(ptr, "%lu%lu%lu%lu%lu%lu%lu%*d%lu%lu%lu%lu%lu%lu%lu",
+                  &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
+                  &rx_fifo, &rx_frame, &rx_multi,
+                  &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
+                  &tx_fifo, &tx_colls, &tx_carrier) != 14)
+               return;
+
+       printf("%s", _SL_);
+       printf("RX: Packets    Bytes        Errors CsumErrs OutOfSeq Mcasts%s", _SL_);
+       printf("    %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s",
+              rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi, _SL_);
+       printf("TX: Packets    Bytes        Errors DeadLoop NoRoute  NoBufs%s", _SL_);
+       printf("    %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld",
+              tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops);
+}
index 9fb4a186176fa3cda0e3529a451c67b4dfc32690..9a03c0d7756e3fb05ee13bd94278d9a654ab41a4 100644 (file)
@@ -32,5 +32,6 @@ int tnl_prl_ioctl(int cmd, const char *name, void *p);
 int tnl_6rd_ioctl(int cmd, const char *name, void *p);
 int tnl_ioctl_get_6rd(const char *name, void *p);
 __be32 tnl_parse_key(const char *name, const char *key);
+void tnl_print_stats(const char *buf);
 
 #endif