]>
Commit | Line | Data |
---|---|---|
7da5ef22 VCG |
1 | /* |
2 | * q_etf.c Earliest TxTime First (ETF). | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Vinicius Costa Gomes <vinicius.gomes@intel.com> | |
10 | * Jesus Sanchez-Palencia <jesus.sanchez-palencia@intel.com> | |
11 | * | |
12 | */ | |
13 | ||
14 | #include <stdio.h> | |
15 | #include <stdlib.h> | |
16 | #include <unistd.h> | |
17 | #include <fcntl.h> | |
18 | #include <sys/ioctl.h> | |
19 | #include <sys/socket.h> | |
20 | #include <netinet/in.h> | |
21 | #include <arpa/inet.h> | |
22 | #include <string.h> | |
23 | ||
24 | #include "utils.h" | |
25 | #include "tc_util.h" | |
26 | ||
27 | #define CLOCKID_INVALID (-1) | |
28 | static const struct static_clockid { | |
29 | const char *name; | |
30 | clockid_t clockid; | |
31 | } clockids_sysv[] = { | |
32 | { "REALTIME", CLOCK_REALTIME }, | |
33 | { "TAI", CLOCK_TAI }, | |
34 | { "BOOTTIME", CLOCK_BOOTTIME }, | |
35 | { "MONOTONIC", CLOCK_MONOTONIC }, | |
36 | { NULL } | |
37 | }; | |
38 | ||
39 | static void explain(void) | |
40 | { | |
8589eb4e MC |
41 | fprintf(stderr, |
42 | "Usage: ... etf delta NANOS clockid CLOCKID [offload] [deadline_mode]\n" | |
43 | "CLOCKID must be a valid SYS-V id (i.e. CLOCK_TAI)\n"); | |
7da5ef22 VCG |
44 | } |
45 | ||
46 | static void explain1(const char *arg, const char *val) | |
47 | { | |
48 | fprintf(stderr, "etf: illegal value for \"%s\": \"%s\"\n", arg, val); | |
49 | } | |
50 | ||
51 | static void explain_clockid(const char *val) | |
52 | { | |
8589eb4e MC |
53 | fprintf(stderr, |
54 | "etf: illegal value for \"clockid\": \"%s\".\n" | |
55 | "It must be a valid SYS-V id (i.e. CLOCK_TAI)\n", | |
56 | val); | |
7da5ef22 VCG |
57 | } |
58 | ||
59 | static int get_clockid(__s32 *val, const char *arg) | |
60 | { | |
61 | const struct static_clockid *c; | |
62 | ||
63 | /* Drop the CLOCK_ prefix if that is being used. */ | |
64 | if (strcasestr(arg, "CLOCK_") != NULL) | |
65 | arg += sizeof("CLOCK_") - 1; | |
66 | ||
67 | for (c = clockids_sysv; c->name; c++) { | |
68 | if (strcasecmp(c->name, arg) == 0) { | |
69 | *val = c->clockid; | |
70 | ||
71 | return 0; | |
72 | } | |
73 | } | |
74 | ||
75 | return -1; | |
76 | } | |
77 | ||
78 | static const char* get_clock_name(clockid_t clockid) | |
79 | { | |
80 | const struct static_clockid *c; | |
81 | ||
82 | for (c = clockids_sysv; c->name; c++) { | |
83 | if (clockid == c->clockid) | |
84 | return c->name; | |
85 | } | |
86 | ||
87 | return "invalid"; | |
88 | } | |
89 | ||
90 | static int etf_parse_opt(struct qdisc_util *qu, int argc, | |
91 | char **argv, struct nlmsghdr *n, const char *dev) | |
92 | { | |
93 | struct tc_etf_qopt opt = { | |
94 | .clockid = CLOCKID_INVALID, | |
95 | }; | |
96 | struct rtattr *tail; | |
97 | ||
98 | while (argc > 0) { | |
99 | if (matches(*argv, "offload") == 0) { | |
100 | if (opt.flags & TC_ETF_OFFLOAD_ON) { | |
101 | fprintf(stderr, "etf: duplicate \"offload\" specification\n"); | |
102 | return -1; | |
103 | } | |
104 | ||
105 | opt.flags |= TC_ETF_OFFLOAD_ON; | |
106 | } else if (matches(*argv, "deadline_mode") == 0) { | |
107 | if (opt.flags & TC_ETF_DEADLINE_MODE_ON) { | |
108 | fprintf(stderr, "etf: duplicate \"deadline_mode\" specification\n"); | |
109 | return -1; | |
110 | } | |
111 | ||
112 | opt.flags |= TC_ETF_DEADLINE_MODE_ON; | |
113 | } else if (matches(*argv, "delta") == 0) { | |
114 | NEXT_ARG(); | |
115 | if (opt.delta) { | |
116 | fprintf(stderr, "etf: duplicate \"delta\" specification\n"); | |
117 | return -1; | |
118 | } | |
119 | if (get_s32(&opt.delta, *argv, 0)) { | |
120 | explain1("delta", *argv); | |
121 | return -1; | |
122 | } | |
123 | } else if (matches(*argv, "clockid") == 0) { | |
124 | NEXT_ARG(); | |
125 | if (opt.clockid != CLOCKID_INVALID) { | |
126 | fprintf(stderr, "etf: duplicate \"clockid\" specification\n"); | |
127 | return -1; | |
128 | } | |
129 | if (get_clockid(&opt.clockid, *argv)) { | |
130 | explain_clockid(*argv); | |
131 | return -1; | |
132 | } | |
d9114263 VP |
133 | } else if (strcmp(*argv, "skip_sock_check") == 0) { |
134 | if (opt.flags & TC_ETF_SKIP_SOCK_CHECK) { | |
135 | fprintf(stderr, "etf: duplicate \"skip_sock_check\" specification\n"); | |
136 | return -1; | |
137 | } | |
138 | ||
139 | opt.flags |= TC_ETF_SKIP_SOCK_CHECK; | |
7da5ef22 VCG |
140 | } else if (strcmp(*argv, "help") == 0) { |
141 | explain(); | |
142 | return -1; | |
143 | } else { | |
144 | fprintf(stderr, "etf: unknown parameter \"%s\"\n", *argv); | |
145 | explain(); | |
146 | return -1; | |
147 | } | |
148 | argc--; argv++; | |
149 | } | |
150 | ||
151 | tail = NLMSG_TAIL(n); | |
152 | addattr_l(n, 1024, TCA_OPTIONS, NULL, 0); | |
153 | addattr_l(n, 2024, TCA_ETF_PARMS, &opt, sizeof(opt)); | |
154 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; | |
155 | return 0; | |
156 | } | |
157 | ||
158 | static int etf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) | |
159 | { | |
160 | struct rtattr *tb[TCA_ETF_MAX+1]; | |
161 | struct tc_etf_qopt *qopt; | |
162 | ||
163 | if (opt == NULL) | |
164 | return 0; | |
165 | ||
166 | parse_rtattr_nested(tb, TCA_ETF_MAX, opt); | |
167 | ||
168 | if (tb[TCA_ETF_PARMS] == NULL) | |
169 | return -1; | |
170 | ||
171 | qopt = RTA_DATA(tb[TCA_ETF_PARMS]); | |
172 | if (RTA_PAYLOAD(tb[TCA_ETF_PARMS]) < sizeof(*qopt)) | |
173 | return -1; | |
174 | ||
175 | print_string(PRINT_ANY, "clockid", "clockid %s ", | |
176 | get_clock_name(qopt->clockid)); | |
177 | ||
178 | print_uint(PRINT_ANY, "delta", "delta %d ", qopt->delta); | |
179 | print_string(PRINT_ANY, "offload", "offload %s ", | |
180 | (qopt->flags & TC_ETF_OFFLOAD_ON) ? "on" : "off"); | |
d9114263 | 181 | print_string(PRINT_ANY, "deadline_mode", "deadline_mode %s ", |
7da5ef22 | 182 | (qopt->flags & TC_ETF_DEADLINE_MODE_ON) ? "on" : "off"); |
d9114263 VP |
183 | print_string(PRINT_ANY, "skip_sock_check", "skip_sock_check %s", |
184 | (qopt->flags & TC_ETF_SKIP_SOCK_CHECK) ? "on" : "off"); | |
7da5ef22 VCG |
185 | |
186 | return 0; | |
187 | } | |
188 | ||
189 | struct qdisc_util etf_qdisc_util = { | |
190 | .id = "etf", | |
191 | .parse_qopt = etf_parse_opt, | |
192 | .print_qopt = etf_print_opt, | |
193 | }; |