Skip to contentSkip to conversation

tc: unify clockid handling

PATCH Applied Stephen Hemminger 3 participants 3 messages Jan 19, 2024 – Jan 21, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tc: unify clockid handling

There are three places in tc which all have same code for handling clockid (copy/paste). Move it into tc_util.c.

---
Motivated by (rejected) pull request to deal with missing clockid's on really old versions of glibc.

 tc/m_gate.c   | 41 -----------------------------------------
 tc/q_etf.c    | 43 -------------------------------------------
 tc/q_taprio.c | 43 -------------------------------------------
 tc/tc_util.c  | 40 ++++++++++++++++++++++++++++++++++++++++
 tc/tc_util.h  |  4 ++++
 5 files changed, 44 insertions(+), 127 deletions(-)
tc/m_gate.c +0 -41
index c091ae19c1cc..37afa426a2c8 100644
@@ -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)) {
tc/q_etf.c +0 -43

55 lines not on this page · raw message

tc/q_taprio.c +0 -43

55 lines not on this page · raw message

tc/tc_util.c +40 -0
index 8c0e19e452d5..a799a6299c04 100644
@@ -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();
tc/tc_util.h +4 -0
index c535dccbc200..aaf10e433fd1 100644
@@ -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);
Signature · 1 line
2.43.0
#2 lore raw
In reply to Stephen Hemminger
This patch was applied to iproute2/iproute2.git (main) by Stephen Hemminger <[email protected]>:

Hello:

This patch was applied to iproute2/iproute2.git (main) by Stephen Hemminger <[email protected]>:

Quoted 9 lines · On Fri, 19 Jan 2024 08:40:20 -0800 you wrote:

There are three places in tc which all have same code for handling clockid (copy/paste). Move it into tc_util.c.

---
Motivated by (rejected) pull request to deal with missing clockid's on really old versions of glibc.

[...]

Here is the summary with links:
- [iproute2] tc: unify clockid handling

    https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/commit/?id=91cca2aee76b

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html