| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* m_police.c Parse/print policing module options. |
| 4 |
* |
| 5 |
* Authors: Alexey Kuznetsov, <[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 |
|
| 17 |
#include "utils.h" |
| 18 |
#include "tc_util.h" |
| 19 |
|
| 20 |
static int act_parse_police(const struct action_util *a, int *argc_p, |
| 21 |
char ***argv_p, int tca_id, struct nlmsghdr *n); |
| 22 |
static int print_police(const struct action_util *a, FILE *f, struct rtattr *tb); |
| 23 |
|
| 24 |
struct action_util police_action_util = { |
| 25 |
.id = "police", |
| 26 |
.parse_aopt = act_parse_police, |
| 27 |
.print_aopt = print_police, |
| 28 |
}; |
| 29 |
|
| 30 |
static void usage(void) |
| 31 |
{ |
| 32 |
fprintf(stderr, |
| 33 |
"Usage: ... police [ rate BPS burst BYTES[/BYTES] ] \n" |
| 34 |
" [ pkts_rate RATE pkts_burst PACKETS ] [ mtu BYTES[/BYTES] ]\n" |
| 35 |
" [ peakrate BPS ] [ avrate BPS ] [ overhead BYTES ]\n" |
| 36 |
" [ linklayer TYPE ] [ CONTROL ]\n" |
| 37 |
"Where: CONTROL := conform-exceed <EXCEEDACT>[/NOTEXCEEDACT]\n" |
| 38 |
" Define how to handle packets which exceed (<EXCEEDACT>)\n" |
| 39 |
" or conform (<NOTEXCEEDACT>) the configured bandwidth limit.\n" |
| 40 |
" EXCEEDACT/NOTEXCEEDACT := { pipe | ok | reclassify | drop | continue |\n" |
| 41 |
" goto chain <CHAIN_INDEX> }\n"); |
| 42 |
exit(-1); |
| 43 |
} |
| 44 |
|
| 45 |
static int act_parse_police(const struct action_util *a, int *argc_p, char ***argv_p, |
| 46 |
int tca_id, struct nlmsghdr *n) |
| 47 |
{ |
| 48 |
int argc = *argc_p; |
| 49 |
char **argv = *argv_p; |
| 50 |
int res = -1; |
| 51 |
int ok = 0; |
| 52 |
struct tc_police p = { .action = TC_POLICE_RECLASSIFY }; |
| 53 |
__u32 rtab[256]; |
| 54 |
__u32 ptab[256]; |
| 55 |
__u32 avrate = 0; |
| 56 |
int presult = 0; |
| 57 |
unsigned mtu = 0, mpu = 0; |
| 58 |
unsigned short overhead = 0; |
| 59 |
unsigned int linklayer = LINKLAYER_ETHERNET; /* Assume ethernet */ |
| 60 |
int Rcell_log = -1, Pcell_log = -1; |
| 61 |
struct rtattr *tail; |
| 62 |
__u64 rate64 = 0, prate64 = 0, buffer64 = 0; |
| 63 |
__u64 pps64 = 0, ppsburst64 = 0; |
| 64 |
|
| 65 |
if (a) /* new way of doing things */ |
| 66 |
NEXT_ARG(); |
| 67 |
|
| 68 |
if (argc <= 0) |
| 69 |
return -1; |
| 70 |
|
| 71 |
while (argc > 0) { |
| 72 |
|
| 73 |
if (matches(*argv, "index") == 0) { |
| 74 |
NEXT_ARG(); |
| 75 |
if (get_u32(&p.index, *argv, 10)) |
| 76 |
invarg("index", *argv); |
| 77 |
} else if (matches(*argv, "burst") == 0 || |
| 78 |
strcmp(*argv, "buffer") == 0 || |
| 79 |
strcmp(*argv, "maxburst") == 0) { |
| 80 |
NEXT_ARG(); |
| 81 |
if (buffer64) |
| 82 |
duparg("buffer/burst", *argv); |
| 83 |
if (get_size64_and_cell(&buffer64, &Rcell_log, |
| 84 |
*argv) < 0) |
| 85 |
invarg("buffer", *argv); |
| 86 |
} else if (strcmp(*argv, "mtu") == 0 || |
| 87 |
strcmp(*argv, "minburst") == 0) { |
| 88 |
NEXT_ARG(); |
| 89 |
if (mtu) |
| 90 |
duparg("mtu/minburst", *argv); |
| 91 |
if (get_size_and_cell(&mtu, &Pcell_log, *argv) < 0) |
| 92 |
invarg("mtu", *argv); |
| 93 |
} else if (strcmp(*argv, "mpu") == 0) { |
| 94 |
NEXT_ARG(); |
| 95 |
if (mpu) |
| 96 |
duparg("mpu", *argv); |
| 97 |
if (get_size(&mpu, *argv)) |
| 98 |
invarg("mpu", *argv); |
| 99 |
} else if (strcmp(*argv, "rate") == 0) { |
| 100 |
NEXT_ARG(); |
| 101 |
if (rate64) |
| 102 |
duparg("rate", *argv); |
| 103 |
if (get_rate64(&rate64, *argv)) |
| 104 |
invarg("rate", *argv); |
| 105 |
} else if (strcmp(*argv, "avrate") == 0) { |
| 106 |
NEXT_ARG(); |
| 107 |
if (avrate) |
| 108 |
duparg("avrate", *argv); |
| 109 |
if (get_rate(&avrate, *argv)) |
| 110 |
invarg("avrate", *argv); |
| 111 |
} else if (matches(*argv, "peakrate") == 0) { |
| 112 |
NEXT_ARG(); |
| 113 |
if (prate64) |
| 114 |
duparg("peakrate", *argv); |
| 115 |
if (get_rate64(&prate64, *argv)) |
| 116 |
invarg("peakrate", *argv); |
| 117 |
} else if (matches(*argv, "reclassify") == 0 || |
| 118 |
matches(*argv, "drop") == 0 || |
| 119 |
matches(*argv, "shot") == 0 || |
| 120 |
matches(*argv, "continue") == 0 || |
| 121 |
matches(*argv, "pass") == 0 || |
| 122 |
matches(*argv, "ok") == 0 || |
| 123 |
matches(*argv, "pipe") == 0 || |
| 124 |
matches(*argv, "goto") == 0) { |
| 125 |
if (!parse_action_control(&argc, &argv, &p.action, false)) |
| 126 |
goto action_ctrl_ok; |
| 127 |
return -1; |
| 128 |
} else if (strcmp(*argv, "conform-exceed") == 0) { |
| 129 |
NEXT_ARG(); |
| 130 |
if (!parse_action_control_slash(&argc, &argv, &p.action, |
| 131 |
&presult, true)) |
| 132 |
goto action_ctrl_ok; |
| 133 |
return -1; |
| 134 |
} else if (matches(*argv, "overhead") == 0) { |
| 135 |
NEXT_ARG(); |
| 136 |
if (get_u16(&overhead, *argv, 10)) |
| 137 |
invarg("overhead", *argv); |
| 138 |
} else if (matches(*argv, "linklayer") == 0) { |
| 139 |
NEXT_ARG(); |
| 140 |
if (get_linklayer(&linklayer, *argv)) |
| 141 |
invarg("linklayer", *argv); |
| 142 |
} else if (matches(*argv, "pkts_rate") == 0) { |
| 143 |
NEXT_ARG(); |
| 144 |
if (pps64) |
| 145 |
duparg("pkts_rate", *argv); |
| 146 |
if (get_u64(&pps64, *argv, 10)) |
| 147 |
invarg("pkts_rate", *argv); |
| 148 |
} else if (matches(*argv, "pkts_burst") == 0) { |
| 149 |
NEXT_ARG(); |
| 150 |
if (ppsburst64) |
| 151 |
duparg("pkts_burst", *argv); |
| 152 |
if (get_u64(&ppsburst64, *argv, 10)) |
| 153 |
invarg("pkts_burst", *argv); |
| 154 |
} else if (strcmp(*argv, "help") == 0) { |
| 155 |
usage(); |
| 156 |
} else { |
| 157 |
break; |
| 158 |
} |
| 159 |
NEXT_ARG_FWD(); |
| 160 |
action_ctrl_ok: |
| 161 |
ok++; |
| 162 |
} |
| 163 |
|
| 164 |
if (!ok) |
| 165 |
return -1; |
| 166 |
|
| 167 |
if (rate64 && avrate) |
| 168 |
return -1; |
| 169 |
|
| 170 |
/* Must at least do late binding, use TB or ewma policing */ |
| 171 |
if (!rate64 && !avrate && !p.index && !mtu && !pps64) { |
| 172 |
fprintf(stderr, "'rate' or 'avrate' or 'mtu' or 'pkts_rate' MUST be specified.\n"); |
| 173 |
return -1; |
| 174 |
} |
| 175 |
|
| 176 |
/* When the TB policer is used, burst is required */ |
| 177 |
if (rate64 && !buffer64 && !avrate) { |
| 178 |
fprintf(stderr, "'burst' requires 'rate'.\n"); |
| 179 |
return -1; |
| 180 |
} |
| 181 |
|
| 182 |
/* When the packets TB policer is used, pkts_burst is required */ |
| 183 |
if (pps64 && !ppsburst64) { |
| 184 |
fprintf(stderr, "'pkts_burst' requires 'pkts_rate'.\n"); |
| 185 |
return -1; |
| 186 |
} |
| 187 |
|
| 188 |
/* forbid rate and pkts_rate in same action */ |
| 189 |
if (pps64 && rate64) { |
| 190 |
fprintf(stderr, "'rate' and 'pkts_rate' are not allowed in same action.\n"); |
| 191 |
return -1; |
| 192 |
} |
| 193 |
|
| 194 |
if (prate64) { |
| 195 |
if (!rate64) { |
| 196 |
fprintf(stderr, "'peakrate' requires 'rate'.\n"); |
| 197 |
return -1; |
| 198 |
} |
| 199 |
if (!mtu) { |
| 200 |
fprintf(stderr, "'mtu' is required, if 'peakrate' is requested.\n"); |
| 201 |
return -1; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
if (rate64) { |
| 206 |
p.rate.rate = (rate64 >= (1ULL << 32)) ? ~0U : rate64; |
| 207 |
p.rate.mpu = mpu; |
| 208 |
p.rate.overhead = overhead; |
| 209 |
if (tc_calc_rtable_64(&p.rate, rtab, Rcell_log, mtu, |
| 210 |
linklayer, rate64) < 0) { |
| 211 |
fprintf(stderr, "POLICE: failed to calculate rate table.\n"); |
| 212 |
return -1; |
| 213 |
} |
| 214 |
p.burst = tc_calc_xmittime(rate64, buffer64); |
| 215 |
if (p.burst == UINT_MAX) { |
| 216 |
fprintf(stderr, "POLICE: burst out of range\n"); |
| 217 |
return -1; |
| 218 |
} |
| 219 |
} |
| 220 |
p.mtu = mtu; |
| 221 |
if (prate64) { |
| 222 |
p.peakrate.rate = (prate64 >= (1ULL << 32)) ? ~0U : prate64; |
| 223 |
p.peakrate.mpu = mpu; |
| 224 |
p.peakrate.overhead = overhead; |
| 225 |
if (tc_calc_rtable_64(&p.peakrate, ptab, Pcell_log, mtu, |
| 226 |
linklayer, prate64) < 0) { |
| 227 |
fprintf(stderr, "POLICE: failed to calculate peak rate table.\n"); |
| 228 |
return -1; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
tail = addattr_nest(n, MAX_MSG, tca_id); |
| 233 |
addattr_l(n, MAX_MSG, TCA_POLICE_TBF, &p, sizeof(p)); |
| 234 |
if (rate64) { |
| 235 |
addattr_l(n, MAX_MSG, TCA_POLICE_RATE, rtab, 1024); |
| 236 |
if (rate64 >= (1ULL << 32)) |
| 237 |
addattr64(n, MAX_MSG, TCA_POLICE_RATE64, rate64); |
| 238 |
} |
| 239 |
if (prate64) { |
| 240 |
addattr_l(n, MAX_MSG, TCA_POLICE_PEAKRATE, ptab, 1024); |
| 241 |
if (prate64 >= (1ULL << 32)) |
| 242 |
addattr64(n, MAX_MSG, TCA_POLICE_PEAKRATE64, prate64); |
| 243 |
} |
| 244 |
if (avrate) |
| 245 |
addattr32(n, MAX_MSG, TCA_POLICE_AVRATE, avrate); |
| 246 |
if (presult) |
| 247 |
addattr32(n, MAX_MSG, TCA_POLICE_RESULT, presult); |
| 248 |
|
| 249 |
if (pps64) { |
| 250 |
addattr64(n, MAX_MSG, TCA_POLICE_PKTRATE64, pps64); |
| 251 |
ppsburst64 = tc_calc_xmittime(pps64, ppsburst64); |
| 252 |
addattr64(n, MAX_MSG, TCA_POLICE_PKTBURST64, ppsburst64); |
| 253 |
} |
| 254 |
|
| 255 |
addattr_nest_end(n, tail); |
| 256 |
res = 0; |
| 257 |
|
| 258 |
*argc_p = argc; |
| 259 |
*argv_p = argv; |
| 260 |
return res; |
| 261 |
} |
| 262 |
|
| 263 |
int parse_police(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n) |
| 264 |
{ |
| 265 |
return act_parse_police(NULL, argc_p, argv_p, tca_id, n); |
| 266 |
} |
| 267 |
|
| 268 |
static int print_police(const struct action_util *a, FILE *funused, struct rtattr *arg) |
| 269 |
{ |
| 270 |
SPRINT_BUF(b2); |
| 271 |
struct tc_police *p; |
| 272 |
struct rtattr *tb[TCA_POLICE_MAX+1]; |
| 273 |
unsigned int linklayer; |
| 274 |
__u64 rate64, prate64, buffer64; |
| 275 |
__u64 pps64, ppsburst64; |
| 276 |
|
| 277 |
print_string(PRINT_JSON, "kind", "%s", "police"); |
| 278 |
if (arg == NULL) |
| 279 |
return 0; |
| 280 |
|
| 281 |
parse_rtattr_nested(tb, TCA_POLICE_MAX, arg); |
| 282 |
|
| 283 |
if (tb[TCA_POLICE_TBF] == NULL) { |
| 284 |
fprintf(stderr, "[NULL police tbf]"); |
| 285 |
return -1; |
| 286 |
} |
| 287 |
#ifndef STOOPID_8BYTE |
| 288 |
if (RTA_PAYLOAD(tb[TCA_POLICE_TBF]) < sizeof(*p)) { |
| 289 |
fprintf(stderr, "[truncated police tbf]"); |
| 290 |
return -1; |
| 291 |
} |
| 292 |
#endif |
| 293 |
p = RTA_DATA(tb[TCA_POLICE_TBF]); |
| 294 |
|
| 295 |
rate64 = p->rate.rate; |
| 296 |
if (tb[TCA_POLICE_RATE64] && |
| 297 |
RTA_PAYLOAD(tb[TCA_POLICE_RATE64]) >= sizeof(rate64)) |
| 298 |
rate64 = rta_getattr_u64(tb[TCA_POLICE_RATE64]); |
| 299 |
|
| 300 |
print_hex(PRINT_FP, NULL, " police 0x%x ", p->index); |
| 301 |
print_uint(PRINT_JSON, "index", NULL, p->index); |
| 302 |
tc_print_rate(PRINT_FP, NULL, "rate %s ", rate64); |
| 303 |
buffer64 = tc_calc_xmitsize(rate64, p->burst); |
| 304 |
print_size(PRINT_FP, NULL, "burst %s ", buffer64); |
| 305 |
print_size(PRINT_FP, NULL, "mtu %s ", p->mtu); |
| 306 |
if (show_raw) |
| 307 |
print_hex(PRINT_FP, NULL, "[%08x] ", p->burst); |
| 308 |
|
| 309 |
prate64 = p->peakrate.rate; |
| 310 |
if (tb[TCA_POLICE_PEAKRATE64] && |
| 311 |
RTA_PAYLOAD(tb[TCA_POLICE_PEAKRATE64]) >= sizeof(prate64)) |
| 312 |
prate64 = rta_getattr_u64(tb[TCA_POLICE_PEAKRATE64]); |
| 313 |
|
| 314 |
if (prate64) |
| 315 |
tc_print_rate(PRINT_FP, NULL, "peakrate %s ", prate64); |
| 316 |
|
| 317 |
if (tb[TCA_POLICE_AVRATE]) |
| 318 |
tc_print_rate(PRINT_FP, NULL, "avrate %s ", |
| 319 |
rta_getattr_u32(tb[TCA_POLICE_AVRATE])); |
| 320 |
|
| 321 |
if ((tb[TCA_POLICE_PKTRATE64] && |
| 322 |
RTA_PAYLOAD(tb[TCA_POLICE_PKTRATE64]) >= sizeof(pps64)) && |
| 323 |
(tb[TCA_POLICE_PKTBURST64] && |
| 324 |
RTA_PAYLOAD(tb[TCA_POLICE_PKTBURST64]) >= sizeof(ppsburst64))) { |
| 325 |
pps64 = rta_getattr_u64(tb[TCA_POLICE_PKTRATE64]); |
| 326 |
ppsburst64 = rta_getattr_u64(tb[TCA_POLICE_PKTBURST64]); |
| 327 |
ppsburst64 = tc_calc_xmitsize(pps64, ppsburst64); |
| 328 |
print_u64(PRINT_ANY, "pkts_rate", "pkts_rate %llu ", pps64); |
| 329 |
print_u64(PRINT_ANY, "pkts_burst", "pkts_burst %llu ", ppsburst64); |
| 330 |
} |
| 331 |
|
| 332 |
print_action_control("action ", p->action, ""); |
| 333 |
|
| 334 |
if (tb[TCA_POLICE_RESULT]) { |
| 335 |
__u32 action = rta_getattr_u32(tb[TCA_POLICE_RESULT]); |
| 336 |
|
| 337 |
print_action_control("/", action, " "); |
| 338 |
} else { |
| 339 |
print_string(PRINT_FP, NULL, " ", NULL); |
| 340 |
} |
| 341 |
|
| 342 |
print_size(PRINT_ANY, "overhead", "overhead %s ", p->rate.overhead); |
| 343 |
linklayer = (p->rate.linklayer & TC_LINKLAYER_MASK); |
| 344 |
if (linklayer > TC_LINKLAYER_ETHERNET || show_details) |
| 345 |
print_string(PRINT_ANY, "linklayer", "linklayer %s ", |
| 346 |
sprint_linklayer(linklayer, b2)); |
| 347 |
print_nl(); |
| 348 |
print_int(PRINT_ANY, "ref", "\tref %d ", p->refcnt); |
| 349 |
print_int(PRINT_ANY, "bind", "bind %d ", p->bindcnt); |
| 350 |
if (show_stats) { |
| 351 |
if (tb[TCA_POLICE_TM]) { |
| 352 |
struct tcf_t *tm = RTA_DATA(tb[TCA_POLICE_TM]); |
| 353 |
|
| 354 |
print_tm(tm); |
| 355 |
} |
| 356 |
} |
| 357 |
print_nl(); |
| 358 |
|
| 359 |
|
| 360 |
return 0; |
| 361 |
} |
| 362 |
|
| 363 |
int tc_print_police(struct rtattr *arg) |
| 364 |
{ |
| 365 |
return print_police(&police_action_util, NULL, arg); |
| 366 |
} |