]> 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 70279b9dc8767390b880f8c83e6fa84a5d769aed..c5da5b5c1ed5164b4a2bb7e4514242aada193749 100644 (file)
@@ -31,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");
@@ -61,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) {
@@ -121,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 {
@@ -138,6 +149,13 @@ 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, d))
@@ -164,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);
        }
@@ -182,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;
        }
 
@@ -197,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);
@@ -253,8 +267,7 @@ int print_qdisc(const struct sockaddr_nl *who,
        print_string(PRINT_FP, NULL, " ", NULL);
 
        if (filter_ifindex == 0)
-               print_string(PRINT_ANY, "dev", "dev %s ",
-                            ll_index_to_name(t->tcm_ifindex));
+               print_devname(PRINT_ANY, t->tcm_ifindex);
 
        if (t->tcm_parent == TC_H_ROOT)
                print_bool(PRINT_ANY, "root", "root ", true);
@@ -270,6 +283,24 @@ int print_qdisc(const struct sockaddr_nl *who,
            (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)
                q = get_qdisc_kind("prio");
@@ -344,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;
        }
 
@@ -412,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;
+}