public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Jesus Sanchez-Palencia <jesus.sanchez-palencia@intel.com>
To: netdev@vger.kernel.org
Cc: jhs@mojatatu.com, xiyou.wangcong@gmail.com, jiri@resnulli.us,
	Vinicius Costa Gomes <vinicius.gomes@intel.com>,
	Jesus Sanchez-Palencia <jesus.sanchez-palencia@intel.com>
Subject: [RFC v3 iproute2 3/3] tc: Add support for the TBS Qdisc
Date: Tue,  6 Mar 2018 17:16:08 -0800	[thread overview]
Message-ID: <20180307011608.24186-3-jesus.sanchez-palencia@intel.com> (raw)
In-Reply-To: <20180307011608.24186-1-jesus.sanchez-palencia@intel.com>

From: Vinicius Costa Gomes <vinicius.gomes@intel.com>

The Time Based Scheduler (TBS) queueing discipline allows precise
control of the transmission time of packets.

The syntax is:

tc qdisc add dev DEV parent NODE tbs delta <DELTA>
                     clockid <CLOCKID> [offload] [sorting]

Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
Signed-off-by: Jesus Sanchez-Palencia <jesus.sanchez-palencia@intel.com>
---
 tc/Makefile |   1 +
 tc/q_tbs.c  | 200 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 201 insertions(+)
 create mode 100644 tc/q_tbs.c

diff --git a/tc/Makefile b/tc/Makefile
index 3716dd6a..3c87b0dc 100644
--- a/tc/Makefile
+++ b/tc/Makefile
@@ -71,6 +71,7 @@ TCMODULES += q_clsact.o
 TCMODULES += e_bpf.o
 TCMODULES += f_matchall.o
 TCMODULES += q_cbs.o
+TCMODULES += q_tbs.o
 
 TCSO :=
 ifeq ($(TC_CONFIG_ATM),y)
diff --git a/tc/q_tbs.c b/tc/q_tbs.c
new file mode 100644
index 00000000..b0823dc9
--- /dev/null
+++ b/tc/q_tbs.c
@@ -0,0 +1,200 @@
+/*
+ * q_tbs.c		TBS.
+ *
+ *		This program is free software; you can redistribute it and/or
+ *		modify it under the terms of the GNU General Public License
+ *		as published by the Free Software Foundation; either version
+ *		2 of the License, or (at your option) any later version.
+ *
+ * Authors:	Vinicius Costa Gomes <vinicius.gomes@intel.com>
+ *		Jesus Sanchez-Palencia <jesus.sanchez-palencia@intel.com>
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <linux/ptp_clock.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <string.h>
+
+#include "utils.h"
+#include "tc_util.h"
+
+/* clockid is invalid if bits 0, 1, 2 are set as described by posix-timers.h */
+#define CLOCKID_INVALID (BIT(0) | BIT(1) | BIT(2))
+#define PTP_MAX_DEV_PATH 16
+
+/* fd to clockid helpers. Copied from posix-timers.h. */
+#define CLOCKFD 3
+static inline clockid_t make_process_cpuclock(const unsigned int pid,
+                const clockid_t clock)
+{
+        return ((~pid) << 3) | clock;
+}
+
+static inline clockid_t fd_to_clockid(const int fd)
+{
+        return make_process_cpuclock((unsigned int) fd, CLOCKFD);
+}
+
+static void explain(void)
+{
+	fprintf(stderr, "Usage: ... tbs delta NANOS clockid CLOCKID [offload] [sorting]\n");
+	fprintf(stderr, "CLOCKID must be a valid SYS-V id (i.e. CLOCK_TAI) or \
+			 a dynamic clock (i.e. /dev/ptp0).\n");
+}
+
+static void explain1(const char *arg, const char *val)
+{
+	fprintf(stderr, "tbs: illegal value for \"%s\": \"%s\"\n", arg, val);
+}
+
+static void explain_clockid(const char *val)
+{
+	fprintf(stderr, "tbs: illegal value for \"clockid\": \"%s\".\n", val);
+	fprintf(stderr, "It must be a valid SYS-V id (i.e. CLOCK_TAI) or "\
+			"dynamic clock (i.e. /dev/ptp0).\n");
+}
+
+static int get_clockid(__s32 *val, const char *arg)
+{
+	const struct static_clockid {
+		const char *name;
+		clockid_t clockid;
+	} clockids_sysv[] = {
+		{ "CLOCK_REALTIME", CLOCK_REALTIME },
+		{ "CLOCK_TAI", CLOCK_TAI },
+		{ "CLOCK_BOOTTIME", CLOCK_BOOTTIME },
+		{ "CLOCK_MONOTONIC", CLOCK_MONOTONIC },
+		{ NULL }
+	};
+
+	struct ptp_clock_caps capabilities;
+	char ptp_path[PTP_MAX_DEV_PATH];
+	const struct static_clockid *c;
+	int fd_ptp;
+
+	for (c = clockids_sysv; c->name; c++) {
+		if (strncasecmp(c->name, arg, 25) == 0) {
+			*val = c->clockid;
+
+			return 0;
+		}
+	}
+
+	snprintf(ptp_path, sizeof(ptp_path), "%s", arg);
+	fd_ptp = open(ptp_path, O_RDONLY);
+
+	/* Make sure the path provided points to a PTP chardev. */
+	if (fd_ptp < 0 || ioctl(fd_ptp, PTP_CLOCK_GETCAPS, &capabilities) < 0) {
+		return -1;
+	}
+
+	*val = fd_to_clockid(fd_ptp);
+	return 0;
+}
+
+
+static int tbs_parse_opt(struct qdisc_util *qu, int argc,
+			 char **argv, struct nlmsghdr *n, const char *dev)
+{
+	struct tc_tbs_qopt opt = {
+		.clockid = CLOCKID_INVALID,
+	};
+	struct rtattr *tail;
+
+	while (argc > 0) {
+		if (matches(*argv, "offload") == 0) {
+			if (opt.flags & TC_TBS_OFFLOAD_ON) {
+				fprintf(stderr, "tbs: duplicate \"offload\" specification\n");
+				return -1;
+			}
+
+			opt.flags |= TC_TBS_OFFLOAD_ON;
+		} else if (matches(*argv, "sorting") == 0) {
+			if (opt.flags & TC_TBS_SORTING_ON) {
+				fprintf(stderr, "tbs: duplicate \"sorting\" specification\n");
+				return -1;
+			}
+
+			opt.flags |= TC_TBS_SORTING_ON;
+		} else if (matches(*argv, "delta") == 0) {
+			NEXT_ARG();
+			if (opt.delta) {
+				fprintf(stderr, "tbs: duplicate \"delta\" specification\n");
+				return -1;
+			}
+			if (get_s32(&opt.delta, *argv, 0)) {
+				explain1("delta", *argv);
+				return -1;
+			}
+		} else if (matches(*argv, "clockid") == 0) {
+			NEXT_ARG();
+			if (opt.clockid != CLOCKID_INVALID) {
+				fprintf(stderr, "tbs: duplicate \"clockid\" specification\n");
+				return -1;
+			}
+			if (get_clockid(&opt.clockid, *argv)) {
+				explain_clockid(*argv);
+				return -1;
+			}
+		} else if (strcmp(*argv, "help") == 0) {
+			explain();
+			return -1;
+		} else {
+			fprintf(stderr, "tbs: unknown parameter \"%s\"\n", *argv);
+			explain();
+			return -1;
+		}
+		argc--; argv++;
+	}
+
+	tail = NLMSG_TAIL(n);
+	addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
+	addattr_l(n, 2024, TCA_TBS_PARMS, &opt, sizeof(opt));
+	tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
+	return 0;
+}
+
+static int tbs_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
+{
+	struct rtattr *tb[TCA_TBS_MAX+1];
+	struct tc_tbs_qopt *qopt;
+
+	if (opt == NULL)
+		return 0;
+
+	parse_rtattr_nested(tb, TCA_TBS_MAX, opt);
+
+	if (tb[TCA_TBS_PARMS] == NULL)
+		return -1;
+
+	qopt = RTA_DATA(tb[TCA_TBS_PARMS]);
+	if (RTA_PAYLOAD(tb[TCA_TBS_PARMS])  < sizeof(*qopt))
+		return -1;
+
+	fprintf(f, "clockid ");
+	if (qopt->clockid == CLOCKID_INVALID)
+		fprintf(f, "invalid ");
+	else
+		fprintf(f, "%d ", qopt->clockid);
+
+	fprintf(f, "delta %d ", qopt->delta);
+	fprintf(f, "offload %s ", (qopt->flags & TC_TBS_OFFLOAD_ON) ?
+					"on" : "off");
+	fprintf(f, "sorting %s", (qopt->flags & TC_TBS_SORTING_ON) ?
+					"on" : "off");
+
+	return 0;
+}
+
+struct qdisc_util tbs_qdisc_util = {
+	.id		= "tbs",
+	.parse_qopt	= tbs_parse_opt,
+	.print_qopt	= tbs_print_opt,
+};
-- 
2.16.2

  parent reply	other threads:[~2018-03-07  1:18 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-07  1:16 [RFC v3 iproute2 1/3] include: Add ptp_clock.h to linux uapi Jesus Sanchez-Palencia
2018-03-07  1:16 ` [RFC v3 iproute2 2/3] uapi pkt_sched: Add tbs info - DO NOT COMMIT Jesus Sanchez-Palencia
2018-03-07  1:16 ` Jesus Sanchez-Palencia [this message]
2018-03-07  1:51   ` [RFC v3 iproute2 3/3] tc: Add support for the TBS Qdisc Stephen Hemminger
2018-03-07 22:29     ` Jesus Sanchez-Palencia
2018-03-07 22:53       ` Stephen Hemminger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180307011608.24186-3-jesus.sanchez-palencia@intel.com \
    --to=jesus.sanchez-palencia@intel.com \
    --cc=jhs@mojatatu.com \
    --cc=jiri@resnulli.us \
    --cc=netdev@vger.kernel.org \
    --cc=vinicius.gomes@intel.com \
    --cc=xiyou.wangcong@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox