]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - misc/ifstat.c
Merge branch 'main' into next
[mirror_iproute2.git] / misc / ifstat.c
index 9467119192ba2670789699784934d3b677ca40c8..c05183d79a13aedaf17812aad46085b8dcd257a1 100644 (file)
@@ -33,7 +33,7 @@
 
 #include "libnetlink.h"
 #include "json_writer.h"
-#include "SNAPSHOT.h"
+#include "version.h"
 #include "utils.h"
 
 int dump_zeros;
@@ -45,7 +45,6 @@ int no_update;
 int scan_interval;
 int time_constant;
 int show_errors;
-int pretty;
 double W;
 char **patterns;
 int npatterns;
@@ -105,14 +104,13 @@ static int match(const char *id)
                return 1;
 
        for (i = 0; i < npatterns; i++) {
-               if (!fnmatch(patterns[i], id, 0))
+               if (!fnmatch(patterns[i], id, FNM_CASEFOLD))
                        return 1;
        }
        return 0;
 }
 
-static int get_nlmsg_extended(const struct sockaddr_nl *who,
-                             struct nlmsghdr *m, void *arg)
+static int get_nlmsg_extended(struct nlmsghdr *m, void *arg)
 {
        struct if_stats_msg *ifsm = NLMSG_DATA(m);
        struct rtattr *tb[IFLA_STATS_MAX+1];
@@ -143,8 +141,10 @@ static int get_nlmsg_extended(const struct sockaddr_nl *who,
                struct rtattr *attr;
 
                attr = parse_rtattr_one_nested(sub_type, tb[filter_type]);
-               if (attr == NULL)
+               if (attr == NULL) {
+                       free(n);
                        return 0;
+               }
                memcpy(&n->val, RTA_DATA(attr), sizeof(n->val));
        }
        memset(&n->rate, 0, sizeof(n->rate));
@@ -153,8 +153,7 @@ static int get_nlmsg_extended(const struct sockaddr_nl *who,
        return 0;
 }
 
-static int get_nlmsg(const struct sockaddr_nl *who,
-                    struct nlmsghdr *m, void *arg)
+static int get_nlmsg(struct nlmsghdr *m, void *arg)
 {
        struct ifinfomsg *ifi = NLMSG_DATA(m);
        struct rtattr *tb[IFLA_MAX+1];
@@ -202,8 +201,8 @@ static void load_info(void)
        if (is_extended) {
                ll_init_map(&rth);
                filter_mask = IFLA_STATS_FILTER_BIT(filter_type);
-               if (rtnl_wilddump_stats_req_filter(&rth, AF_UNSPEC, RTM_GETSTATS,
-                                                  filter_mask) < 0) {
+               if (rtnl_statsdump_req_filter(&rth, AF_UNSPEC,
+                                             filter_mask) < 0) {
                        perror("Cannot send dump request");
                        exit(1);
                }
@@ -213,7 +212,7 @@ static void load_info(void)
                        exit(1);
                }
        } else {
-               if (rtnl_wilddump_request(&rth, AF_INET, RTM_GETLINK) < 0) {
+               if (rtnl_linkdump_req(&rth, AF_INET) < 0) {
                        perror("Cannot send dump request");
                        exit(1);
                }
@@ -533,8 +532,12 @@ static void dump_kern_db(FILE *fp)
                else
                        print_one_if(fp, n, n->val);
        }
-       if (json_output)
-               fprintf(fp, "\n} }\n");
+       if (jw) {
+               jsonw_end_object(jw);
+
+               jsonw_end_object(jw);
+               jsonw_destroy(&jw);
+       }
 }
 
 static void dump_incr_db(FILE *fp)
@@ -729,7 +732,8 @@ static int verify_forging(int fd)
 static void xstat_usage(void)
 {
        fprintf(stderr,
-"Usage: ifstat supported xstats:\n");
+"Usage: ifstat supported xstats:\n"
+"       cpu_hits       Counts only packets that went via the CPU.\n");
 }
 
 struct extended_stats_options_t {
@@ -743,6 +747,7 @@ struct extended_stats_options_t {
  * Name length must be under 64 chars.
  */
 static const struct extended_stats_options_t extended_stats_options[] = {
+       {"cpu_hits",  IFLA_STATS_LINK_OFFLOAD_XSTATS, IFLA_OFFLOAD_XSTATS_CPU_HIT},
 };
 
 static const char *get_filter_type(const char *name)
@@ -864,7 +869,7 @@ int main(int argc, char *argv[])
                        break;
                case 'v':
                case 'V':
-                       printf("ifstat utility, iproute2-ss%s\n", SNAPSHOT);
+                       printf("ifstat utility, iproute2-%s\n", version);
                        exit(0);
                case 'h':
                case '?':
@@ -984,12 +989,18 @@ int main(int argc, char *argv[])
            && verify_forging(fd) == 0) {
                FILE *sfp = fdopen(fd, "r");
 
-               load_raw_table(sfp);
-               if (hist_db && source_mismatch) {
-                       fprintf(stderr, "ifstat: history is stale, ignoring it.\n");
-                       hist_db = NULL;
+               if (!sfp) {
+                       fprintf(stderr, "ifstat: fdopen failed: %s\n",
+                               strerror(errno));
+                       close(fd);
+               } else  {
+                       load_raw_table(sfp);
+                       if (hist_db && source_mismatch) {
+                               fprintf(stderr, "ifstat: history is stale, ignoring it.\n");
+                               hist_db = NULL;
+                       }
+                       fclose(sfp);
                }
-               fclose(sfp);
        } else {
                if (fd >= 0)
                        close(fd);