From: Vinicius Costa Gomes <vinicius.go...@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.go...@intel.com> Signed-off-by: Jesus Sanchez-Palencia <jesus.sanchez-palen...@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.go...@intel.com> + * Jesus Sanchez-Palencia <jesus.sanchez-palen...@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