]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - tc/tc_class.c
tc: util: constrain percentage in 0-100 interval
[mirror_iproute2.git] / tc / tc_class.c
index 7747c8db39d15dc34a5ba75a27fce1b6bf6ffc2d..c7e3cfdf6c87644783c184ad43cde2a9e23b7a92 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>
@@ -44,15 +43,15 @@ static void usage(void);
 
 static void usage(void)
 {
-       fprintf(stderr, "Usage: tc class [ add | del | change | replace | show ] dev STRING\n");
-       fprintf(stderr, "       [ classid CLASSID ] [ root | parent CLASSID ]\n");
-       fprintf(stderr, "       [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n");
-       fprintf(stderr, "\n");
-       fprintf(stderr, "       tc class show [ dev STRING ] [ root | parent CLASSID ]\n");
-       fprintf(stderr, "Where:\n");
-       fprintf(stderr, "QDISC_KIND := { prio | cbq | etc. }\n");
-       fprintf(stderr, "OPTIONS := ... try tc class add <desired QDISC_KIND> help\n");
-       return;
+       fprintf(stderr,
+               "Usage: tc class [ add | del | change | replace | show ] dev STRING\n"
+               "       [ classid CLASSID ] [ root | parent CLASSID ]\n"
+               "       [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n"
+               "\n"
+               "       tc class show [ dev STRING ] [ root | parent CLASSID ]\n"
+               "Where:\n"
+               "QDISC_KIND := { prio | cbq | etc. }\n"
+               "OPTIONS := ... try tc class add <desired QDISC_KIND> help\n");
 }
 
 static int tc_class_modify(int cmd, unsigned int flags, int argc, char **argv)
@@ -61,21 +60,16 @@ static int tc_class_modify(int cmd, unsigned int flags, int argc, char **argv)
                struct nlmsghdr n;
                struct tcmsg            t;
                char                    buf[4096];
-       } req;
+       } req = {
+               .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
+               .n.nlmsg_flags = NLM_F_REQUEST | flags,
+               .n.nlmsg_type = cmd,
+               .t.tcm_family = AF_UNSPEC,
+       };
        struct qdisc_util *q = NULL;
-       struct tc_estimator est;
-       char  d[16];
-       char  k[16];
-
-       memset(&req, 0, sizeof(req));
-       memset(&est, 0, sizeof(est));
-       memset(d, 0, sizeof(d));
-       memset(k, 0, sizeof(k));
-
-       req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
-       req.n.nlmsg_flags = NLM_F_REQUEST|flags;
-       req.n.nlmsg_type = cmd;
-       req.t.tcm_family = AF_UNSPEC;
+       struct tc_estimator est = {};
+       char  d[IFNAMSIZ] = {};
+       char  k[FILTER_NAMESZ] = {};
 
        while (argc > 0) {
                if (strcmp(*argv, "dev") == 0) {
@@ -135,7 +129,7 @@ static int tc_class_modify(int cmd, unsigned int flags, int argc, char **argv)
                        fprintf(stderr, "Error: Qdisc \"%s\" is classless.\n", k);
                        return 1;
                }
-               if (q->parse_copt(q, argc, argv, &req.n))
+               if (q->parse_copt(q, argc, argv, &req.n, d))
                        return 1;
        } else {
                if (argc) {
@@ -149,28 +143,26 @@ static int tc_class_modify(int cmd, unsigned int flags, int argc, char **argv)
        if (d[0])  {
                ll_init_map(&rth);
 
-               if ((req.t.tcm_ifindex = ll_name_to_index(d)) == 0) {
-                       fprintf(stderr, "Cannot find device \"%s\"\n", d);
-                       return 1;
-               }
+               req.t.tcm_ifindex = ll_name_to_index(d);
+               if (!req.t.tcm_ifindex)
+                       return -nodev(d);
        }
 
-       if (rtnl_talk(&rth, &req.n, NULL, 0) < 0)
+       if (rtnl_talk(&rth, &req.n, NULL) < 0)
                return 2;
 
        return 0;
 }
 
-int filter_ifindex;
-__u32 filter_qdisc;
-__u32 filter_classid;
+static int filter_ifindex;
+static __u32 filter_qdisc;
+static __u32 filter_classid;
 
 static void graph_node_add(__u32 parent_id, __u32 id, void *data,
                int len)
 {
-       struct graph_node *node = malloc(sizeof(struct graph_node));
+       struct graph_node *node = calloc(1, sizeof(struct graph_node));
 
-       memset(node, 0, sizeof(*node));
        node->id         = id;
        node->parent_id  = parent_id;
 
@@ -225,9 +217,9 @@ static void graph_cls_show(FILE *fp, char *buf, struct hlist_head *root_list,
 {
        struct hlist_node *n, *tmp_cls;
        char cls_id_str[256] = {};
-       struct rtattr *tb[TCA_MAX + 1] = {};
+       struct rtattr *tb[TCA_MAX + 1];
        struct qdisc_util *q;
-       char str[100] = {};
+       char str[300] = {};
 
        hlist_for_each_safe(n, tmp_cls, root_list) {
                struct hlist_node *c, *tmp_chld;
@@ -250,7 +242,8 @@ static void graph_cls_show(FILE *fp, char *buf, struct hlist_head *root_list,
                graph_indent(buf, cls, 0, 0);
 
                print_tc_classid(cls_id_str, sizeof(cls_id_str), cls->id);
-               sprintf(str, "+---(%s)", cls_id_str);
+               snprintf(str, sizeof(str),
+                        "+---(%s)", cls_id_str);
                strcat(buf, str);
 
                parse_rtattr(tb, TCA_MAX, (struct rtattr *)cls->data,
@@ -304,13 +297,12 @@ static void graph_cls_show(FILE *fp, char *buf, struct hlist_head *root_list,
        }
 }
 
-int print_class(const struct sockaddr_nl *who,
-                      struct nlmsghdr *n, void *arg)
+int print_class(struct nlmsghdr *n, void *arg)
 {
        FILE *fp = (FILE *)arg;
        struct tcmsg *t = NLMSG_DATA(n);
        int len = n->nlmsg_len;
-       struct rtattr *tb[TCA_MAX + 1] = {};
+       struct rtattr *tb[TCA_MAX + 1];
        struct qdisc_util *q;
        char abuf[256];
 
@@ -395,14 +387,10 @@ int print_class(const struct sockaddr_nl *who,
 
 static int tc_class_list(int argc, char **argv)
 {
-       struct tcmsg t;
-       char d[16];
+       struct tcmsg t = { .tcm_family = AF_UNSPEC };
+       char d[IFNAMSIZ] = {};
        char buf[1024] = {0};
 
-       memset(&t, 0, sizeof(t));
-       t.tcm_family = AF_UNSPEC;
-       memset(d, 0, sizeof(d));
-
        filter_qdisc = 0;
        filter_classid = 0;
 
@@ -452,10 +440,9 @@ static int tc_class_list(int argc, char **argv)
        ll_init_map(&rth);
 
        if (d[0]) {
-               if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
-                       fprintf(stderr, "Cannot find device \"%s\"\n", d);
-                       return 1;
-               }
+               t.tcm_ifindex = ll_name_to_index(d);
+               if (!t.tcm_ifindex)
+                       return -nodev(d);
                filter_ifindex = t.tcm_ifindex;
        }