]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
tc: unify clockid handling
authorStephen Hemminger <stephen@networkplumber.org>
Fri, 19 Jan 2024 16:38:58 +0000 (08:38 -0800)
committerStephen Hemminger <stephen@networkplumber.org>
Fri, 19 Jan 2024 16:38:58 +0000 (08:38 -0800)
There are three places in tc which all have same code for
handling clockid (copy/paste). Move it into tc_util.c.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
tc/m_gate.c
tc/q_etf.c
tc/q_taprio.c
tc/tc_util.c
tc/tc_util.h

index c091ae19c1cc101afacb510cf0026713020c2c30..37afa426a2c8368de448507f2015d3ae0318181d 100644 (file)
@@ -20,18 +20,6 @@ struct gate_entry {
        int32_t maxoctets;
 };
 
-#define CLOCKID_INVALID (-1)
-static const struct clockid_table {
-       const char *name;
-       clockid_t clockid;
-} clockt_map[] = {
-       { "REALTIME", CLOCK_REALTIME },
-       { "TAI", CLOCK_TAI },
-       { "BOOTTIME", CLOCK_BOOTTIME },
-       { "MONOTONIC", CLOCK_MONOTONIC },
-       { NULL }
-};
-
 static void explain(void)
 {
        fprintf(stderr,
@@ -78,35 +66,6 @@ struct action_util gate_action_util = {
        .print_aopt = print_gate,
 };
 
-static int get_clockid(__s32 *val, const char *arg)
-{
-       const struct clockid_table *c;
-
-       if (strcasestr(arg, "CLOCK_") != NULL)
-               arg += sizeof("CLOCK_") - 1;
-
-       for (c = clockt_map; c->name; c++) {
-               if (strcasecmp(c->name, arg) == 0) {
-                       *val = c->clockid;
-                       return 0;
-               }
-       }
-
-       return -1;
-}
-
-static const char *get_clock_name(clockid_t clockid)
-{
-       const struct clockid_table *c;
-
-       for (c = clockt_map; c->name; c++) {
-               if (clockid == c->clockid)
-                       return c->name;
-       }
-
-       return "invalid";
-}
-
 static int get_gate_state(__u8 *val, const char *arg)
 {
        if (!strcasecmp("OPEN", arg)) {
index 572e2bc89fc1de666f16c75909254e4c678d673e..d16188daabbdc757054574ab985bf037b929c276 100644 (file)
 #include "utils.h"
 #include "tc_util.h"
 
-#define CLOCKID_INVALID (-1)
-static const struct static_clockid {
-       const char *name;
-       clockid_t clockid;
-} clockids_sysv[] = {
-       { "REALTIME", CLOCK_REALTIME },
-       { "TAI", CLOCK_TAI },
-       { "BOOTTIME", CLOCK_BOOTTIME },
-       { "MONOTONIC", CLOCK_MONOTONIC },
-       { NULL }
-};
-
 static void explain(void)
 {
        fprintf(stderr,
@@ -51,37 +39,6 @@ static void explain_clockid(const char *val)
                val);
 }
 
-static int get_clockid(__s32 *val, const char *arg)
-{
-       const struct static_clockid *c;
-
-       /* Drop the CLOCK_ prefix if that is being used. */
-       if (strcasestr(arg, "CLOCK_") != NULL)
-               arg += sizeof("CLOCK_") - 1;
-
-       for (c = clockids_sysv; c->name; c++) {
-               if (strcasecmp(c->name, arg) == 0) {
-                       *val = c->clockid;
-
-                       return 0;
-               }
-       }
-
-       return -1;
-}
-
-static const char* get_clock_name(clockid_t clockid)
-{
-       const struct static_clockid *c;
-
-       for (c = clockids_sysv; c->name; c++) {
-               if (clockid == c->clockid)
-                       return c->name;
-       }
-
-       return "invalid";
-}
-
 static int etf_parse_opt(struct qdisc_util *qu, int argc,
                         char **argv, struct nlmsghdr *n, const char *dev)
 {
index ef8fc7a05fc225c198252aaaa61284d73d7f191c..c47fe244369f2e2b801a539f62e46c8c474e33bf 100644 (file)
@@ -29,18 +29,6 @@ struct sched_entry {
        uint8_t cmd;
 };
 
-#define CLOCKID_INVALID (-1)
-static const struct static_clockid {
-       const char *name;
-       clockid_t clockid;
-} clockids_sysv[] = {
-       { "REALTIME", CLOCK_REALTIME },
-       { "TAI", CLOCK_TAI },
-       { "BOOTTIME", CLOCK_BOOTTIME },
-       { "MONOTONIC", CLOCK_MONOTONIC },
-       { NULL }
-};
-
 static void explain(void)
 {
        fprintf(stderr,
@@ -60,37 +48,6 @@ static void explain_clockid(const char *val)
        fprintf(stderr, "It must be a valid SYS-V id (i.e. CLOCK_TAI)\n");
 }
 
-static int get_clockid(__s32 *val, const char *arg)
-{
-       const struct static_clockid *c;
-
-       /* Drop the CLOCK_ prefix if that is being used. */
-       if (strcasestr(arg, "CLOCK_") != NULL)
-               arg += sizeof("CLOCK_") - 1;
-
-       for (c = clockids_sysv; c->name; c++) {
-               if (strcasecmp(c->name, arg) == 0) {
-                       *val = c->clockid;
-
-                       return 0;
-               }
-       }
-
-       return -1;
-}
-
-static const char* get_clock_name(clockid_t clockid)
-{
-       const struct static_clockid *c;
-
-       for (c = clockids_sysv; c->name; c++) {
-               if (clockid == c->clockid)
-                       return c->name;
-       }
-
-       return "invalid";
-}
-
 static const char *entry_cmd_to_str(__u8 cmd)
 {
        switch (cmd) {
index 8c0e19e452d5f60c1ce4edd946965625f86eede6..a799a6299c0409ea8c7b7b488c3c4c2abff4efb6 100644 (file)
@@ -596,6 +596,46 @@ char *sprint_linklayer(unsigned int linklayer, char *buf)
        return buf;
 }
 
+static const struct clockid_table {
+       const char *name;
+       clockid_t clockid;
+} clockt_map[] = {
+       { "REALTIME", CLOCK_REALTIME },
+       { "TAI", CLOCK_TAI },
+       { "BOOTTIME", CLOCK_BOOTTIME },
+       { "MONOTONIC", CLOCK_MONOTONIC },
+       { NULL }
+};
+
+int get_clockid(__s32 *val, const char *arg)
+{
+       const struct clockid_table *c;
+
+       if (strcasestr(arg, "CLOCK_") != NULL)
+               arg += sizeof("CLOCK_") - 1;
+
+       for (c = clockt_map; c->name; c++) {
+               if (strcasecmp(c->name, arg) == 0) {
+                       *val = c->clockid;
+                       return 0;
+               }
+       }
+
+       return -1;
+}
+
+const char *get_clock_name(clockid_t clockid)
+{
+       const struct clockid_table *c;
+
+       for (c = clockt_map; c->name; c++) {
+               if (clockid == c->clockid)
+                       return c->name;
+       }
+
+       return "invalid";
+}
+
 void print_tm(FILE *f, const struct tcf_t *tm)
 {
        int hz = get_user_hz();
index c535dccbc20046e838f07bc4261629ad5ed70f00..aaf10e433fd1572c2ef785d0270ee930184e9d64 100644 (file)
@@ -121,6 +121,10 @@ int prio_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt);
 int cls_names_init(char *path);
 void cls_names_uninit(void);
 
+#define CLOCKID_INVALID (-1)
+int get_clockid(__s32 *val, const char *arg);
+const char *get_clock_name(clockid_t clockid);
+
 int action_a2n(char *arg, int *result, bool allow_num);
 
 bool tc_qdisc_block_exists(__u32 block_index);