| 1 |
/* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 |
/* Copyright (C) 2013 Cisco Systems, Inc, 2013. |
| 3 |
* |
| 4 |
* Author: Vijay Subramanian <[email protected]> |
| 5 |
* Author: Mythili Prabhu <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <unistd.h> |
| 11 |
#include <fcntl.h> |
| 12 |
#include <sys/socket.h> |
| 13 |
#include <netinet/in.h> |
| 14 |
#include <arpa/inet.h> |
| 15 |
#include <string.h> |
| 16 |
#include <math.h> |
| 17 |
|
| 18 |
#include "utils.h" |
| 19 |
#include "tc_util.h" |
| 20 |
|
| 21 |
static void explain(void) |
| 22 |
{ |
| 23 |
fprintf(stderr, |
| 24 |
"Usage: ... pie [ limit PACKETS ] [ target TIME ]\n" |
| 25 |
" [ tupdate TIME ] [ alpha ALPHA ] [ beta BETA ]\n" |
| 26 |
" [ bytemode | nobytemode ] [ ecn | noecn ]\n" |
| 27 |
" [ dq_rate_estimator | no_dq_rate_estimator ]\n"); |
| 28 |
} |
| 29 |
|
| 30 |
#define ALPHA_MAX 32 |
| 31 |
#define BETA_MAX 32 |
| 32 |
|
| 33 |
static int pie_parse_opt(const struct qdisc_util *qu, int argc, char **argv, |
| 34 |
struct nlmsghdr *n, const char *dev) |
| 35 |
{ |
| 36 |
unsigned int limit = 0; |
| 37 |
unsigned int target = 0; |
| 38 |
unsigned int tupdate = 0; |
| 39 |
unsigned int alpha = 0; |
| 40 |
unsigned int beta = 0; |
| 41 |
int ecn = -1; |
| 42 |
int bytemode = -1; |
| 43 |
int dq_rate_estimator = -1; |
| 44 |
struct rtattr *tail; |
| 45 |
|
| 46 |
while (argc > 0) { |
| 47 |
if (strcmp(*argv, "limit") == 0) { |
| 48 |
NEXT_ARG(); |
| 49 |
if (get_unsigned(&limit, *argv, 0)) { |
| 50 |
fprintf(stderr, "Illegal \"limit\"\n"); |
| 51 |
return -1; |
| 52 |
} |
| 53 |
} else if (strcmp(*argv, "target") == 0) { |
| 54 |
NEXT_ARG(); |
| 55 |
if (get_time(&target, *argv)) { |
| 56 |
fprintf(stderr, "Illegal \"target\"\n"); |
| 57 |
return -1; |
| 58 |
} |
| 59 |
} else if (strcmp(*argv, "tupdate") == 0) { |
| 60 |
NEXT_ARG(); |
| 61 |
if (get_time(&tupdate, *argv)) { |
| 62 |
fprintf(stderr, "Illegal \"tupdate\"\n"); |
| 63 |
return -1; |
| 64 |
} |
| 65 |
} else if (strcmp(*argv, "alpha") == 0) { |
| 66 |
NEXT_ARG(); |
| 67 |
if (get_unsigned(&alpha, *argv, 0) || |
| 68 |
(alpha > ALPHA_MAX)) { |
| 69 |
fprintf(stderr, "Illegal \"alpha\"\n"); |
| 70 |
return -1; |
| 71 |
} |
| 72 |
} else if (strcmp(*argv, "beta") == 0) { |
| 73 |
NEXT_ARG(); |
| 74 |
if (get_unsigned(&beta, *argv, 0) || |
| 75 |
(beta > BETA_MAX)) { |
| 76 |
fprintf(stderr, "Illegal \"beta\"\n"); |
| 77 |
return -1; |
| 78 |
} |
| 79 |
} else if (strcmp(*argv, "ecn") == 0) { |
| 80 |
ecn = 1; |
| 81 |
} else if (strcmp(*argv, "noecn") == 0) { |
| 82 |
ecn = 0; |
| 83 |
} else if (strcmp(*argv, "bytemode") == 0) { |
| 84 |
bytemode = 1; |
| 85 |
} else if (strcmp(*argv, "nobytemode") == 0) { |
| 86 |
bytemode = 0; |
| 87 |
} else if (strcmp(*argv, "dq_rate_estimator") == 0) { |
| 88 |
dq_rate_estimator = 1; |
| 89 |
} else if (strcmp(*argv, "no_dq_rate_estimator") == 0) { |
| 90 |
dq_rate_estimator = 0; |
| 91 |
} else if (strcmp(*argv, "help") == 0) { |
| 92 |
explain(); |
| 93 |
return -1; |
| 94 |
} else { |
| 95 |
fprintf(stderr, "What is \"%s\"?\n", *argv); |
| 96 |
explain(); |
| 97 |
return -1; |
| 98 |
} |
| 99 |
argc--; |
| 100 |
argv++; |
| 101 |
} |
| 102 |
|
| 103 |
tail = addattr_nest(n, 1024, TCA_OPTIONS); |
| 104 |
if (limit) |
| 105 |
addattr_l(n, 1024, TCA_PIE_LIMIT, &limit, sizeof(limit)); |
| 106 |
if (tupdate) |
| 107 |
addattr_l(n, 1024, TCA_PIE_TUPDATE, &tupdate, sizeof(tupdate)); |
| 108 |
if (target) |
| 109 |
addattr_l(n, 1024, TCA_PIE_TARGET, &target, sizeof(target)); |
| 110 |
if (alpha) |
| 111 |
addattr_l(n, 1024, TCA_PIE_ALPHA, &alpha, sizeof(alpha)); |
| 112 |
if (beta) |
| 113 |
addattr_l(n, 1024, TCA_PIE_BETA, &beta, sizeof(beta)); |
| 114 |
if (ecn != -1) |
| 115 |
addattr_l(n, 1024, TCA_PIE_ECN, &ecn, sizeof(ecn)); |
| 116 |
if (bytemode != -1) |
| 117 |
addattr_l(n, 1024, TCA_PIE_BYTEMODE, &bytemode, |
| 118 |
sizeof(bytemode)); |
| 119 |
if (dq_rate_estimator != -1) |
| 120 |
addattr_l(n, 1024, TCA_PIE_DQ_RATE_ESTIMATOR, |
| 121 |
&dq_rate_estimator, sizeof(dq_rate_estimator)); |
| 122 |
|
| 123 |
addattr_nest_end(n, tail); |
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
static int pie_print_opt(const struct qdisc_util *qu, FILE *f, struct rtattr *opt) |
| 128 |
{ |
| 129 |
struct rtattr *tb[TCA_PIE_MAX + 1]; |
| 130 |
unsigned int limit; |
| 131 |
unsigned int tupdate; |
| 132 |
unsigned int target; |
| 133 |
unsigned int alpha; |
| 134 |
unsigned int beta; |
| 135 |
unsigned int ecn; |
| 136 |
unsigned int bytemode; |
| 137 |
unsigned int dq_rate_estimator; |
| 138 |
|
| 139 |
SPRINT_BUF(b1); |
| 140 |
|
| 141 |
if (opt == NULL) |
| 142 |
return 0; |
| 143 |
|
| 144 |
parse_rtattr_nested(tb, TCA_PIE_MAX, opt); |
| 145 |
|
| 146 |
if (tb[TCA_PIE_LIMIT] && |
| 147 |
RTA_PAYLOAD(tb[TCA_PIE_LIMIT]) >= sizeof(__u32)) { |
| 148 |
limit = rta_getattr_u32(tb[TCA_PIE_LIMIT]); |
| 149 |
print_uint(PRINT_ANY, "limit", "limit %up ", limit); |
| 150 |
} |
| 151 |
if (tb[TCA_PIE_TARGET] && |
| 152 |
RTA_PAYLOAD(tb[TCA_PIE_TARGET]) >= sizeof(__u32)) { |
| 153 |
target = rta_getattr_u32(tb[TCA_PIE_TARGET]); |
| 154 |
print_uint(PRINT_JSON, "target", NULL, target); |
| 155 |
print_string(PRINT_FP, NULL, "target %s ", |
| 156 |
sprint_time(target, b1)); |
| 157 |
} |
| 158 |
if (tb[TCA_PIE_TUPDATE] && |
| 159 |
RTA_PAYLOAD(tb[TCA_PIE_TUPDATE]) >= sizeof(__u32)) { |
| 160 |
tupdate = rta_getattr_u32(tb[TCA_PIE_TUPDATE]); |
| 161 |
print_uint(PRINT_JSON, "tupdate", NULL, tupdate); |
| 162 |
print_string(PRINT_FP, NULL, "tupdate %s ", |
| 163 |
sprint_time(tupdate, b1)); |
| 164 |
} |
| 165 |
if (tb[TCA_PIE_ALPHA] && |
| 166 |
RTA_PAYLOAD(tb[TCA_PIE_ALPHA]) >= sizeof(__u32)) { |
| 167 |
alpha = rta_getattr_u32(tb[TCA_PIE_ALPHA]); |
| 168 |
print_uint(PRINT_ANY, "alpha", "alpha %u ", alpha); |
| 169 |
} |
| 170 |
if (tb[TCA_PIE_BETA] && |
| 171 |
RTA_PAYLOAD(tb[TCA_PIE_BETA]) >= sizeof(__u32)) { |
| 172 |
beta = rta_getattr_u32(tb[TCA_PIE_BETA]); |
| 173 |
print_uint(PRINT_ANY, "beta", "beta %u ", beta); |
| 174 |
} |
| 175 |
|
| 176 |
if (tb[TCA_PIE_ECN] && RTA_PAYLOAD(tb[TCA_PIE_ECN]) >= sizeof(__u32)) { |
| 177 |
ecn = rta_getattr_u32(tb[TCA_PIE_ECN]); |
| 178 |
if (ecn) |
| 179 |
print_bool(PRINT_ANY, "ecn", "ecn ", true); |
| 180 |
} |
| 181 |
|
| 182 |
if (tb[TCA_PIE_BYTEMODE] && |
| 183 |
RTA_PAYLOAD(tb[TCA_PIE_BYTEMODE]) >= sizeof(__u32)) { |
| 184 |
bytemode = rta_getattr_u32(tb[TCA_PIE_BYTEMODE]); |
| 185 |
if (bytemode) |
| 186 |
print_bool(PRINT_ANY, "bytemode", "bytemode ", true); |
| 187 |
} |
| 188 |
|
| 189 |
if (tb[TCA_PIE_DQ_RATE_ESTIMATOR] && |
| 190 |
RTA_PAYLOAD(tb[TCA_PIE_DQ_RATE_ESTIMATOR]) >= sizeof(__u32)) { |
| 191 |
dq_rate_estimator = |
| 192 |
rta_getattr_u32(tb[TCA_PIE_DQ_RATE_ESTIMATOR]); |
| 193 |
if (dq_rate_estimator) |
| 194 |
print_bool(PRINT_ANY, "dq_rate_estimator", |
| 195 |
"dq_rate_estimator ", true); |
| 196 |
} |
| 197 |
|
| 198 |
return 0; |
| 199 |
} |
| 200 |
|
| 201 |
static int pie_print_xstats(const struct qdisc_util *qu, FILE *f, |
| 202 |
struct rtattr *xstats) |
| 203 |
{ |
| 204 |
struct tc_pie_xstats *st; |
| 205 |
|
| 206 |
SPRINT_BUF(b1); |
| 207 |
|
| 208 |
if (xstats == NULL) |
| 209 |
return 0; |
| 210 |
|
| 211 |
if (RTA_PAYLOAD(xstats) < sizeof(*st)) |
| 212 |
return -1; |
| 213 |
|
| 214 |
st = RTA_DATA(xstats); |
| 215 |
|
| 216 |
/* prob is returned as a fracion of maximum integer value */ |
| 217 |
print_float(PRINT_ANY, "prob", " prob %lg", |
| 218 |
(double)st->prob / (double)UINT64_MAX); |
| 219 |
print_uint(PRINT_JSON, "delay", NULL, st->delay); |
| 220 |
print_string(PRINT_FP, NULL, " delay %s", sprint_time(st->delay, b1)); |
| 221 |
|
| 222 |
if (st->dq_rate_estimating) |
| 223 |
print_uint(PRINT_ANY, "avg_dq_rate", " avg_dq_rate %u", |
| 224 |
st->avg_dq_rate); |
| 225 |
|
| 226 |
print_nl(); |
| 227 |
print_uint(PRINT_ANY, "pkts_in", " pkts_in %u", st->packets_in); |
| 228 |
print_uint(PRINT_ANY, "overlimit", " overlimit %u", st->overlimit); |
| 229 |
print_uint(PRINT_ANY, "dropped", " dropped %u", st->dropped); |
| 230 |
print_uint(PRINT_ANY, "maxq", " maxq %u", st->maxq); |
| 231 |
print_uint(PRINT_ANY, "ecn_mark", " ecn_mark %u", st->ecn_mark); |
| 232 |
|
| 233 |
return 0; |
| 234 |
|
| 235 |
} |
| 236 |
|
| 237 |
struct qdisc_util pie_qdisc_util = { |
| 238 |
.id = "pie", |
| 239 |
.parse_qopt = pie_parse_opt, |
| 240 |
.print_qopt = pie_print_opt, |
| 241 |
.print_xstats = pie_print_xstats, |
| 242 |
}; |