]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - tc/tc_qdisc.c
ll_map: Add function to remove link cache entry by index
[mirror_iproute2.git] / tc / tc_qdisc.c
index c52114a272cf61c7a88a1af4cd3e72869e941fc5..c5da5b5c1ed5164b4a2bb7e4514242aada193749 100644 (file)
@@ -13,7 +13,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <syslog.h>
 #include <fcntl.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
@@ -32,6 +31,7 @@ static int usage(void)
        fprintf(stderr, "       [ handle QHANDLE ] [ root | ingress | clsact | parent CLASSID ]\n");
        fprintf(stderr, "       [ estimator INTERVAL TIME_CONSTANT ]\n");
        fprintf(stderr, "       [ stab [ help | STAB_OPTIONS] ]\n");
+       fprintf(stderr, "       [ ingress_block BLOCK_INDEX ] [ egress_block BLOCK_INDEX ]\n");
        fprintf(stderr, "       [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n");
        fprintf(stderr, "\n");
        fprintf(stderr, "       tc qdisc show [ dev STRING ] [ ingress | clsact ] [ invisible ]\n");
@@ -50,8 +50,8 @@ static int tc_qdisc_modify(int cmd, unsigned int flags, int argc, char **argv)
                struct tc_sizespec      szopts;
                __u16                   *data;
        } stab = {};
-       char  d[16] = {};
-       char  k[16] = {};
+       char  d[IFNAMSIZ] = {};
+       char  k[FILTER_NAMESZ] = {};
        struct {
                struct nlmsghdr n;
                struct tcmsg            t;
@@ -62,6 +62,8 @@ static int tc_qdisc_modify(int cmd, unsigned int flags, int argc, char **argv)
                .n.nlmsg_type = cmd,
                .t.tcm_family = AF_UNSPEC,
        };
+       __u32 ingress_block = 0;
+       __u32 egress_block = 0;
 
        while (argc > 0) {
                if (strcmp(*argv, "dev") == 0) {
@@ -122,6 +124,14 @@ static int tc_qdisc_modify(int cmd, unsigned int flags, int argc, char **argv)
                        if (parse_size_table(&argc, &argv, &stab.szopts) < 0)
                                return -1;
                        continue;
+               } else if (matches(*argv, "ingress_block") == 0) {
+                       NEXT_ARG();
+                       if (get_u32(&ingress_block, *argv, 0) || !ingress_block)
+                               invarg("invalid ingress block index value", *argv);
+               } else if (matches(*argv, "egress_block") == 0) {
+                       NEXT_ARG();
+                       if (get_u32(&egress_block, *argv, 0) || !egress_block)
+                               invarg("invalid egress block index value", *argv);
                } else if (matches(*argv, "help") == 0) {
                        usage();
                } else {
@@ -139,9 +149,16 @@ static int tc_qdisc_modify(int cmd, unsigned int flags, int argc, char **argv)
        if (est.ewma_log)
                addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est));
 
+       if (ingress_block)
+               addattr32(&req.n, sizeof(req),
+                         TCA_INGRESS_BLOCK, ingress_block);
+       if (egress_block)
+               addattr32(&req.n, sizeof(req),
+                         TCA_EGRESS_BLOCK, egress_block);
+
        if (q) {
                if (q->parse_qopt) {
-                       if (q->parse_qopt(q, argc, argv, &req.n))
+                       if (q->parse_qopt(q, argc, argv, &req.n, d))
                                return 1;
                } else if (argc) {
                        fprintf(stderr, "qdisc '%s' does not support option parsing\n", k);
@@ -165,14 +182,13 @@ static int tc_qdisc_modify(int cmd, unsigned int flags, int argc, char **argv)
                        return -1;
                }
 
-               tail = NLMSG_TAIL(&req.n);
-               addattr_l(&req.n, sizeof(req), TCA_STAB, NULL, 0);
+               tail = addattr_nest(&req.n, sizeof(req), TCA_STAB);
                addattr_l(&req.n, sizeof(req), TCA_STAB_BASE, &stab.szopts,
                          sizeof(stab.szopts));
                if (stab.data)
                        addattr_l(&req.n, sizeof(req), TCA_STAB_DATA, stab.data,
                                  stab.szopts.tsize * sizeof(__u16));
-               tail->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)tail;
+               addattr_nest_end(&req.n, tail);
                if (stab.data)
                        free(stab.data);
        }
@@ -183,10 +199,8 @@ static int tc_qdisc_modify(int cmd, unsigned int flags, int argc, char **argv)
                ll_init_map(&rth);
 
                idx = ll_name_to_index(d);
-               if (idx == 0) {
-                       fprintf(stderr, "Cannot find device \"%s\"\n", d);
-                       return 1;
-               }
+               if (!idx)
+                       return -nodev(d);
                req.t.tcm_ifindex = idx;
        }
 
@@ -198,8 +212,7 @@ static int tc_qdisc_modify(int cmd, unsigned int flags, int argc, char **argv)
 
 static int filter_ifindex;
 
-int print_qdisc(const struct sockaddr_nl *who,
-               struct nlmsghdr *n, void *arg)
+int print_qdisc(struct nlmsghdr *n, void *arg)
 {
        FILE *fp = (FILE *)arg;
        struct tcmsg *t = NLMSG_DATA(n);
@@ -228,29 +241,65 @@ int print_qdisc(const struct sockaddr_nl *who,
                return -1;
        }
 
-       if (n->nlmsg_type == RTM_DELQDISC)
-               fprintf(fp, "deleted ");
+       open_json_object(NULL);
 
-       if (show_raw)
-               fprintf(fp, "qdisc %s %x:[%08x]  ",
-                       rta_getattr_str(tb[TCA_KIND]),
-                       t->tcm_handle >> 16, t->tcm_handle);
-       else
-               fprintf(fp, "qdisc %s %x: ", rta_getattr_str(tb[TCA_KIND]),
-                       t->tcm_handle >> 16);
+       if (n->nlmsg_type == RTM_DELQDISC)
+               print_bool(PRINT_ANY, "deleted", "deleted ", true);
+
+       if (n->nlmsg_type == RTM_NEWQDISC &&
+                       (n->nlmsg_flags & NLM_F_CREATE) &&
+                       (n->nlmsg_flags & NLM_F_REPLACE))
+               print_bool(PRINT_ANY, "replaced", "replaced ", true);
+
+       if (n->nlmsg_type == RTM_NEWQDISC &&
+                       (n->nlmsg_flags & NLM_F_CREATE) &&
+                       (n->nlmsg_flags & NLM_F_EXCL))
+               print_bool(PRINT_ANY, "added", "added ", true);
+
+       print_string(PRINT_ANY, "kind", "qdisc %s",
+                    rta_getattr_str(tb[TCA_KIND]));
+       sprintf(abuf, "%x:", t->tcm_handle >> 16);
+       print_string(PRINT_ANY, "handle", " %s", abuf);
+       if (show_raw) {
+               sprintf(abuf, "[%08x]", t->tcm_handle);
+               print_string(PRINT_FP, NULL, "%s", abuf);
+       }
+       print_string(PRINT_FP, NULL, " ", NULL);
 
        if (filter_ifindex == 0)
-               fprintf(fp, "dev %s ", ll_index_to_name(t->tcm_ifindex));
+               print_devname(PRINT_ANY, t->tcm_ifindex);
 
        if (t->tcm_parent == TC_H_ROOT)
-               fprintf(fp, "root ");
+               print_bool(PRINT_ANY, "root", "root ", true);
        else if (t->tcm_parent) {
                print_tc_classid(abuf, sizeof(abuf), t->tcm_parent);
-               fprintf(fp, "parent %s ", abuf);
+               print_string(PRINT_ANY, "parent", "parent %s ", abuf);
        }
 
        if (t->tcm_info != 1)
-               fprintf(fp, "refcnt %d ", t->tcm_info);
+               print_uint(PRINT_ANY, "refcnt", "refcnt %u ", t->tcm_info);
+
+       if (tb[TCA_HW_OFFLOAD] &&
+           (rta_getattr_u8(tb[TCA_HW_OFFLOAD])))
+               print_bool(PRINT_ANY, "offloaded", "offloaded ", true);
+
+       if (tb[TCA_INGRESS_BLOCK] &&
+           RTA_PAYLOAD(tb[TCA_INGRESS_BLOCK]) >= sizeof(__u32)) {
+               __u32 block = rta_getattr_u32(tb[TCA_INGRESS_BLOCK]);
+
+               if (block)
+                       print_uint(PRINT_ANY, "ingress_block",
+                                  "ingress_block %u ", block);
+       }
+
+       if (tb[TCA_EGRESS_BLOCK] &&
+           RTA_PAYLOAD(tb[TCA_EGRESS_BLOCK]) >= sizeof(__u32)) {
+               __u32 block = rta_getattr_u32(tb[TCA_EGRESS_BLOCK]);
+
+               if (block)
+                       print_uint(PRINT_ANY, "egress_block",
+                                  "egress_block %u ", block);
+       }
 
        /* pfifo_fast is generic enough to warrant the hardcoding --JHS */
        if (strcmp("pfifo_fast", RTA_DATA(tb[TCA_KIND])) == 0)
@@ -258,17 +307,21 @@ int print_qdisc(const struct sockaddr_nl *who,
        else
                q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND]));
 
+       open_json_object("options");
        if (tb[TCA_OPTIONS]) {
                if (q)
                        q->print_qopt(q, fp, tb[TCA_OPTIONS]);
                else
-                       fprintf(fp, "[cannot parse qdisc parameters]");
+                       print_string(PRINT_FP, NULL,
+                                    "[cannot parse qdisc parameters]", NULL);
        }
-       fprintf(fp, "\n");
+       close_json_object();
+
+       print_string(PRINT_FP, NULL, "\n", NULL);
 
        if (show_details && tb[TCA_STAB]) {
                print_size_table(fp, " ", tb[TCA_STAB]);
-               fprintf(fp, "\n");
+               print_string(PRINT_FP, NULL, "\n", NULL);
        }
 
        if (show_stats) {
@@ -276,14 +329,15 @@ int print_qdisc(const struct sockaddr_nl *who,
 
                if (tb[TCA_STATS] || tb[TCA_STATS2] || tb[TCA_XSTATS]) {
                        print_tcstats_attr(fp, tb, " ", &xstats);
-                       fprintf(fp, "\n");
+                       print_string(PRINT_FP, NULL, "\n", NULL);
                }
 
                if (q && xstats && q->print_xstats) {
                        q->print_xstats(q, fp, xstats);
-                       fprintf(fp, "\n");
+                       print_string(PRINT_FP, NULL, "\n", NULL);
                }
        }
+       close_json_object();
        fflush(fp);
        return 0;
 }
@@ -291,7 +345,7 @@ int print_qdisc(const struct sockaddr_nl *who,
 static int tc_qdisc_list(int argc, char **argv)
 {
        struct tcmsg t = { .tcm_family = AF_UNSPEC };
-       char d[16] = {};
+       char d[IFNAMSIZ] = {};
        bool dump_invisible = false;
 
        while (argc > 0) {
@@ -321,10 +375,8 @@ static int tc_qdisc_list(int argc, char **argv)
 
        if (d[0]) {
                t.tcm_ifindex = ll_name_to_index(d);
-               if (t.tcm_ifindex == 0) {
-                       fprintf(stderr, "Cannot find device \"%s\"\n", d);
-                       return 1;
-               }
+               if (!t.tcm_ifindex)
+                       return -nodev(d);
                filter_ifindex = t.tcm_ifindex;
        }
 
@@ -351,10 +403,12 @@ static int tc_qdisc_list(int argc, char **argv)
                return 1;
        }
 
+       new_json_obj(json);
        if (rtnl_dump_filter(&rth, print_qdisc, stdout) < 0) {
                fprintf(stderr, "Dump terminated\n");
                return 1;
        }
+       delete_json_obj();
 
        return 0;
 }
@@ -387,3 +441,63 @@ int do_qdisc(int argc, char **argv)
        fprintf(stderr, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv);
        return -1;
 }
+
+struct tc_qdisc_block_exists_ctx {
+       __u32 block_index;
+       bool found;
+};
+
+static int tc_qdisc_block_exists_cb(struct nlmsghdr *n, void *arg)
+{
+       struct tc_qdisc_block_exists_ctx *ctx = arg;
+       struct tcmsg *t = NLMSG_DATA(n);
+       struct rtattr *tb[TCA_MAX+1];
+       int len = n->nlmsg_len;
+
+       if (n->nlmsg_type != RTM_NEWQDISC)
+               return 0;
+
+       len -= NLMSG_LENGTH(sizeof(*t));
+       if (len < 0)
+               return -1;
+
+       parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len);
+
+       if (tb[TCA_KIND] == NULL)
+               return -1;
+
+       if (tb[TCA_INGRESS_BLOCK] &&
+           RTA_PAYLOAD(tb[TCA_INGRESS_BLOCK]) >= sizeof(__u32)) {
+               __u32 block = rta_getattr_u32(tb[TCA_INGRESS_BLOCK]);
+
+               if (block == ctx->block_index)
+                       ctx->found = true;
+       }
+
+       if (tb[TCA_EGRESS_BLOCK] &&
+           RTA_PAYLOAD(tb[TCA_EGRESS_BLOCK]) >= sizeof(__u32)) {
+               __u32 block = rta_getattr_u32(tb[TCA_EGRESS_BLOCK]);
+
+               if (block == ctx->block_index)
+                       ctx->found = true;
+       }
+       return 0;
+}
+
+bool tc_qdisc_block_exists(__u32 block_index)
+{
+       struct tc_qdisc_block_exists_ctx ctx = { .block_index = block_index };
+       struct tcmsg t = { .tcm_family = AF_UNSPEC };
+
+       if (rtnl_dump_request(&rth, RTM_GETQDISC, &t, sizeof(t)) < 0) {
+               perror("Cannot send dump request");
+               return false;
+       }
+
+       if (rtnl_dump_filter(&rth, tc_qdisc_block_exists_cb, &ctx) < 0) {
+               perror("Dump terminated\n");
+               return false;
+       }
+
+       return ctx.found;
+}