osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * tc_util.c Misc TC utility functions. |
| 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: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <unistd.h> |
| 16 | #include <syslog.h> |
| 17 | #include <fcntl.h> |
| 18 | #include <sys/socket.h> |
Natanael Copa | dd9cc0e | 2014-05-27 07:40:10 +0000 | [diff] [blame] | 19 | #include <sys/param.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 20 | #include <netinet/in.h> |
| 21 | #include <arpa/inet.h> |
| 22 | #include <string.h> |
| 23 | #include <math.h> |
| 24 | |
| 25 | #include "utils.h" |
| 26 | #include "tc_util.h" |
| 27 | |
Andreas Henriksson | 5e3bb53 | 2008-08-22 16:54:12 +0200 | [diff] [blame] | 28 | #ifndef LIBDIR |
Christoph J. Thompson | 5c434a9 | 2012-03-01 17:46:26 +0100 | [diff] [blame] | 29 | #define LIBDIR "/usr/lib" |
Rafael Almeida | b514b35 | 2008-06-01 21:33:44 -0300 | [diff] [blame] | 30 | #endif |
| 31 | |
Stephen Hemminger | aa27f88 | 2007-06-20 15:27:22 -0700 | [diff] [blame] | 32 | const char *get_tc_lib(void) |
| 33 | { |
| 34 | const char *lib_dir; |
| 35 | |
| 36 | lib_dir = getenv("TC_LIB_DIR"); |
| 37 | if (!lib_dir) |
Andreas Henriksson | 5e3bb53 | 2008-08-22 16:54:12 +0200 | [diff] [blame] | 38 | lib_dir = LIBDIR "/tc/"; |
Stephen Hemminger | aa27f88 | 2007-06-20 15:27:22 -0700 | [diff] [blame] | 39 | |
| 40 | return lib_dir; |
| 41 | } |
| 42 | |
osdl.org!shemminger | dbd90dc | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 43 | int get_qdisc_handle(__u32 *h, const char *str) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 44 | { |
| 45 | __u32 maj; |
| 46 | char *p; |
| 47 | |
| 48 | maj = TC_H_UNSPEC; |
| 49 | if (strcmp(str, "none") == 0) |
| 50 | goto ok; |
| 51 | maj = strtoul(str, &p, 16); |
| 52 | if (p == str) |
| 53 | return -1; |
| 54 | maj <<= 16; |
| 55 | if (*p != ':' && *p!=0) |
| 56 | return -1; |
| 57 | ok: |
| 58 | *h = maj; |
| 59 | return 0; |
| 60 | } |
| 61 | |
osdl.org!shemminger | dbd90dc | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 62 | int get_tc_classid(__u32 *h, const char *str) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 63 | { |
| 64 | __u32 maj, min; |
| 65 | char *p; |
| 66 | |
| 67 | maj = TC_H_ROOT; |
| 68 | if (strcmp(str, "root") == 0) |
| 69 | goto ok; |
| 70 | maj = TC_H_UNSPEC; |
| 71 | if (strcmp(str, "none") == 0) |
| 72 | goto ok; |
| 73 | maj = strtoul(str, &p, 16); |
| 74 | if (p == str) { |
| 75 | maj = 0; |
| 76 | if (*p != ':') |
| 77 | return -1; |
| 78 | } |
| 79 | if (*p == ':') { |
osdl.net!shemminger | a8b303c | 2005-02-07 18:16:29 +0000 | [diff] [blame] | 80 | if (maj >= (1<<16)) |
| 81 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 82 | maj <<= 16; |
| 83 | str = p+1; |
| 84 | min = strtoul(str, &p, 16); |
| 85 | if (*p != 0) |
| 86 | return -1; |
osdl.net!shemminger | a8b303c | 2005-02-07 18:16:29 +0000 | [diff] [blame] | 87 | if (min >= (1<<16)) |
| 88 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 89 | maj |= min; |
| 90 | } else if (*p != 0) |
| 91 | return -1; |
| 92 | |
| 93 | ok: |
| 94 | *h = maj; |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | int print_tc_classid(char *buf, int len, __u32 h) |
| 99 | { |
| 100 | if (h == TC_H_ROOT) |
| 101 | sprintf(buf, "root"); |
| 102 | else if (h == TC_H_UNSPEC) |
| 103 | snprintf(buf, len, "none"); |
| 104 | else if (TC_H_MAJ(h) == 0) |
| 105 | snprintf(buf, len, ":%x", TC_H_MIN(h)); |
| 106 | else if (TC_H_MIN(h) == 0) |
| 107 | snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16); |
| 108 | else |
| 109 | snprintf(buf, len, "%x:%x", TC_H_MAJ(h)>>16, TC_H_MIN(h)); |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | char * sprint_tc_classid(__u32 h, char *buf) |
| 114 | { |
| 115 | if (print_tc_classid(buf, SPRINT_BSIZE-1, h)) |
| 116 | strcpy(buf, "???"); |
| 117 | return buf; |
| 118 | } |
| 119 | |
osdl.net!shemminger | 26ab0b1 | 2004-08-13 23:23:46 +0000 | [diff] [blame] | 120 | /* See http://physics.nist.gov/cuu/Units/binary.html */ |
| 121 | static const struct rate_suffix { |
| 122 | const char *name; |
| 123 | double scale; |
| 124 | } suffixes[] = { |
| 125 | { "bit", 1. }, |
| 126 | { "Kibit", 1024. }, |
| 127 | { "kbit", 1000. }, |
| 128 | { "mibit", 1024.*1024. }, |
| 129 | { "mbit", 1000000. }, |
| 130 | { "gibit", 1024.*1024.*1024. }, |
| 131 | { "gbit", 1000000000. }, |
| 132 | { "tibit", 1024.*1024.*1024.*1024. }, |
| 133 | { "tbit", 1000000000000. }, |
| 134 | { "Bps", 8. }, |
| 135 | { "KiBps", 8.*1024. }, |
| 136 | { "KBps", 8000. }, |
| 137 | { "MiBps", 8.*1024*1024. }, |
| 138 | { "MBps", 8000000. }, |
| 139 | { "GiBps", 8.*1024.*1024.*1024. }, |
| 140 | { "GBps", 8000000000. }, |
| 141 | { "TiBps", 8.*1024.*1024.*1024.*1024. }, |
| 142 | { "TBps", 8000000000000. }, |
| 143 | { NULL } |
| 144 | }; |
| 145 | |
| 146 | |
osdl.org!shemminger | dbd90dc | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 147 | int get_rate(unsigned *rate, const char *str) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 148 | { |
| 149 | char *p; |
| 150 | double bps = strtod(str, &p); |
osdl.net!shemminger | 26ab0b1 | 2004-08-13 23:23:46 +0000 | [diff] [blame] | 151 | const struct rate_suffix *s; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 152 | |
| 153 | if (p == str) |
| 154 | return -1; |
| 155 | |
osdl.net!shemminger | 26ab0b1 | 2004-08-13 23:23:46 +0000 | [diff] [blame] | 156 | for (s = suffixes; s->name; ++s) { |
| 157 | if (strcasecmp(s->name, p) == 0) { |
Eric Dumazet | a303853 | 2013-06-03 05:51:33 +0000 | [diff] [blame] | 158 | bps *= s->scale; |
| 159 | p += strlen(p); |
| 160 | break; |
osdl.net!shemminger | 26ab0b1 | 2004-08-13 23:23:46 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
Eric Dumazet | a303853 | 2013-06-03 05:51:33 +0000 | [diff] [blame] | 164 | if (*p) |
| 165 | return -1; /* unknown suffix */ |
| 166 | |
| 167 | bps /= 8; /* -> bytes per second */ |
| 168 | *rate = bps; |
| 169 | /* detect if an overflow happened */ |
| 170 | if (*rate != floor(bps)) |
| 171 | return -1; |
| 172 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Eric Dumazet | 8334bb3 | 2013-11-12 14:34:07 -0800 | [diff] [blame] | 175 | int get_rate64(__u64 *rate, const char *str) |
| 176 | { |
| 177 | char *p; |
| 178 | double bps = strtod(str, &p); |
| 179 | const struct rate_suffix *s; |
| 180 | |
| 181 | if (p == str) |
| 182 | return -1; |
| 183 | |
| 184 | for (s = suffixes; s->name; ++s) { |
| 185 | if (strcasecmp(s->name, p) == 0) { |
| 186 | bps *= s->scale; |
| 187 | p += strlen(p); |
| 188 | break; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | if (*p) |
| 193 | return -1; /* unknown suffix */ |
| 194 | |
| 195 | bps /= 8; /* -> bytes per second */ |
| 196 | *rate = bps; |
| 197 | return 0; |
| 198 | } |
| 199 | |
Eric Dumazet | 8f7574e | 2013-09-17 04:19:03 -0700 | [diff] [blame] | 200 | void print_rate(char *buf, int len, __u64 rate) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 201 | { |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame] | 202 | extern int use_iec; |
Eric Dumazet | 8cecdc2 | 2013-11-21 22:37:17 -0800 | [diff] [blame] | 203 | unsigned long kilo = use_iec ? 1024 : 1000; |
| 204 | const char *str = use_iec ? "i" : ""; |
| 205 | int i = 0; |
| 206 | static char *units[5] = {"", "K", "M", "G", "T"}; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 207 | |
Eric Dumazet | 8cecdc2 | 2013-11-21 22:37:17 -0800 | [diff] [blame] | 208 | rate <<= 3; /* bytes/sec -> bits/sec */ |
| 209 | |
| 210 | for (i = 0; i < ARRAY_SIZE(units); i++) { |
| 211 | if (rate < kilo) |
| 212 | break; |
| 213 | if (((rate % kilo) != 0) && rate < 1000*kilo) |
| 214 | break; |
| 215 | rate /= kilo; |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame] | 216 | } |
Eric Dumazet | 8cecdc2 | 2013-11-21 22:37:17 -0800 | [diff] [blame] | 217 | snprintf(buf, len, "%.0f%s%sbit", (double)rate, units[i], str); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Eric Dumazet | 8f7574e | 2013-09-17 04:19:03 -0700 | [diff] [blame] | 220 | char * sprint_rate(__u64 rate, char *buf) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 221 | { |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame] | 222 | print_rate(buf, SPRINT_BSIZE-1, rate); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 223 | return buf; |
| 224 | } |
| 225 | |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 226 | int get_time(unsigned *time, const char *str) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 227 | { |
| 228 | double t; |
| 229 | char *p; |
| 230 | |
| 231 | t = strtod(str, &p); |
| 232 | if (p == str) |
| 233 | return -1; |
| 234 | |
| 235 | if (*p) { |
| 236 | if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 || |
| 237 | strcasecmp(p, "secs")==0) |
Patrick McHardy | f0bda7e | 2007-03-04 20:14:59 +0100 | [diff] [blame] | 238 | t *= TIME_UNITS_PER_SEC; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 239 | else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 || |
| 240 | strcasecmp(p, "msecs") == 0) |
Patrick McHardy | f0bda7e | 2007-03-04 20:14:59 +0100 | [diff] [blame] | 241 | t *= TIME_UNITS_PER_SEC/1000; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 242 | else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 || |
| 243 | strcasecmp(p, "usecs") == 0) |
Patrick McHardy | f0bda7e | 2007-03-04 20:14:59 +0100 | [diff] [blame] | 244 | t *= TIME_UNITS_PER_SEC/1000000; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 245 | else |
| 246 | return -1; |
| 247 | } |
| 248 | |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 249 | *time = t; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 254 | void print_time(char *buf, int len, __u32 time) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 255 | { |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 256 | double tmp = time; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 257 | |
Patrick McHardy | f0bda7e | 2007-03-04 20:14:59 +0100 | [diff] [blame] | 258 | if (tmp >= TIME_UNITS_PER_SEC) |
| 259 | snprintf(buf, len, "%.1fs", tmp/TIME_UNITS_PER_SEC); |
| 260 | else if (tmp >= TIME_UNITS_PER_SEC/1000) |
| 261 | snprintf(buf, len, "%.1fms", tmp/(TIME_UNITS_PER_SEC/1000)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 262 | else |
Stephen Hemminger | 8915144 | 2007-03-14 10:14:07 -0700 | [diff] [blame] | 263 | snprintf(buf, len, "%uus", time); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 266 | char * sprint_time(__u32 time, char *buf) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 267 | { |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 268 | print_time(buf, SPRINT_BSIZE-1, time); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 269 | return buf; |
| 270 | } |
| 271 | |
Patrick McHardy | bd29e35 | 2007-03-04 20:15:01 +0100 | [diff] [blame] | 272 | char * sprint_ticks(__u32 ticks, char *buf) |
| 273 | { |
| 274 | return sprint_time(tc_core_tick2time(ticks), buf); |
| 275 | } |
| 276 | |
osdl.org!shemminger | dbd90dc | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 277 | int get_size(unsigned *size, const char *str) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 278 | { |
| 279 | double sz; |
| 280 | char *p; |
| 281 | |
| 282 | sz = strtod(str, &p); |
| 283 | if (p == str) |
| 284 | return -1; |
| 285 | |
| 286 | if (*p) { |
| 287 | if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k")==0) |
| 288 | sz *= 1024; |
osdl.org!shemminger | dbd90dc | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 289 | else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g")==0) |
| 290 | sz *= 1024*1024*1024; |
| 291 | else if (strcasecmp(p, "gbit") == 0) |
| 292 | sz *= 1024*1024*1024/8; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 293 | else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m")==0) |
| 294 | sz *= 1024*1024; |
| 295 | else if (strcasecmp(p, "mbit") == 0) |
| 296 | sz *= 1024*1024/8; |
| 297 | else if (strcasecmp(p, "kbit") == 0) |
| 298 | sz *= 1024/8; |
| 299 | else if (strcasecmp(p, "b") != 0) |
| 300 | return -1; |
| 301 | } |
| 302 | |
| 303 | *size = sz; |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | int get_size_and_cell(unsigned *size, int *cell_log, char *str) |
| 308 | { |
| 309 | char * slash = strchr(str, '/'); |
| 310 | |
| 311 | if (slash) |
| 312 | *slash = 0; |
| 313 | |
| 314 | if (get_size(size, str)) |
| 315 | return -1; |
| 316 | |
| 317 | if (slash) { |
| 318 | int cell; |
| 319 | int i; |
| 320 | |
| 321 | if (get_integer(&cell, slash+1, 0)) |
| 322 | return -1; |
| 323 | *slash = '/'; |
| 324 | |
| 325 | for (i=0; i<32; i++) { |
| 326 | if ((1<<i) == cell) { |
| 327 | *cell_log = i; |
| 328 | return 0; |
| 329 | } |
| 330 | } |
| 331 | return -1; |
| 332 | } |
| 333 | return 0; |
| 334 | } |
| 335 | |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame] | 336 | void print_size(char *buf, int len, __u32 sz) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 337 | { |
| 338 | double tmp = sz; |
| 339 | |
| 340 | if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024) |
| 341 | snprintf(buf, len, "%gMb", rint(tmp/(1024*1024))); |
| 342 | else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16) |
| 343 | snprintf(buf, len, "%gKb", rint(tmp/1024)); |
| 344 | else |
| 345 | snprintf(buf, len, "%ub", sz); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | char * sprint_size(__u32 size, char *buf) |
| 349 | { |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame] | 350 | print_size(buf, SPRINT_BSIZE-1, size); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 351 | return buf; |
| 352 | } |
| 353 | |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame] | 354 | void print_qdisc_handle(char *buf, int len, __u32 h) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 355 | { |
| 356 | snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | char * sprint_qdisc_handle(__u32 h, char *buf) |
| 360 | { |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame] | 361 | print_qdisc_handle(buf, SPRINT_BSIZE-1, h); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 362 | return buf; |
| 363 | } |
| 364 | |
osdl.net!shemminger | 2373fde | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 365 | char * action_n2a(int action, char *buf, int len) |
| 366 | { |
| 367 | switch (action) { |
| 368 | case -1: |
| 369 | return "continue"; |
| 370 | break; |
| 371 | case TC_ACT_OK: |
| 372 | return "pass"; |
| 373 | break; |
| 374 | case TC_ACT_SHOT: |
| 375 | return "drop"; |
| 376 | break; |
| 377 | case TC_ACT_RECLASSIFY: |
| 378 | return "reclassify"; |
| 379 | case TC_ACT_PIPE: |
| 380 | return "pipe"; |
| 381 | case TC_ACT_STOLEN: |
| 382 | return "stolen"; |
| 383 | default: |
| 384 | snprintf(buf, len, "%d", action); |
| 385 | return buf; |
| 386 | } |
| 387 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 388 | |
osdl.net!shemminger | 2373fde | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 389 | int action_a2n(char *arg, int *result) |
| 390 | { |
| 391 | int res; |
| 392 | |
| 393 | if (matches(arg, "continue") == 0) |
| 394 | res = -1; |
| 395 | else if (matches(arg, "drop") == 0) |
| 396 | res = TC_ACT_SHOT; |
| 397 | else if (matches(arg, "shot") == 0) |
| 398 | res = TC_ACT_SHOT; |
| 399 | else if (matches(arg, "pass") == 0) |
| 400 | res = TC_ACT_OK; |
| 401 | else if (strcmp(arg, "ok") == 0) |
| 402 | res = TC_ACT_OK; |
| 403 | else if (matches(arg, "reclassify") == 0) |
| 404 | res = TC_ACT_RECLASSIFY; |
| 405 | else { |
| 406 | char dummy; |
| 407 | if (sscanf(arg, "%d%c", &res, &dummy) != 1) |
| 408 | return -1; |
| 409 | } |
| 410 | *result = res; |
| 411 | return 0; |
| 412 | } |
| 413 | |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 414 | int get_linklayer(unsigned *val, const char *arg) |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame] | 415 | { |
| 416 | int res; |
| 417 | |
| 418 | if (matches(arg, "ethernet") == 0) |
| 419 | res = LINKLAYER_ETHERNET; |
| 420 | else if (matches(arg, "atm") == 0) |
| 421 | res = LINKLAYER_ATM; |
| 422 | else if (matches(arg, "adsl") == 0) |
| 423 | res = LINKLAYER_ATM; |
| 424 | else |
| 425 | return -1; /* Indicate error */ |
| 426 | |
| 427 | *val = res; |
| 428 | return 0; |
| 429 | } |
| 430 | |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 431 | void print_linklayer(char *buf, int len, unsigned linklayer) |
| 432 | { |
| 433 | switch (linklayer) { |
| 434 | case LINKLAYER_UNSPEC: |
| 435 | snprintf(buf, len, "%s", "unspec"); |
| 436 | return; |
| 437 | case LINKLAYER_ETHERNET: |
| 438 | snprintf(buf, len, "%s", "ethernet"); |
| 439 | return; |
| 440 | case LINKLAYER_ATM: |
| 441 | snprintf(buf, len, "%s", "atm"); |
| 442 | return; |
| 443 | default: |
| 444 | snprintf(buf, len, "%s", "unknown"); |
| 445 | return; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | char *sprint_linklayer(unsigned linklayer, char *buf) |
| 450 | { |
| 451 | print_linklayer(buf, SPRINT_BSIZE-1, linklayer); |
| 452 | return buf; |
| 453 | } |
| 454 | |
osdl.net!shemminger | 6dc9f01 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 455 | void print_tm(FILE * f, const struct tcf_t *tm) |
osdl.net!shemminger | 2373fde | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 456 | { |
net[shemminger]!shemminger | 5e8bc63 | 2005-03-14 18:44:54 +0000 | [diff] [blame] | 457 | int hz = get_user_hz(); |
osdl.net!shemminger | 2373fde | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 458 | if (tm->install != 0) |
osdl.net!shemminger | 63ae25d | 2005-03-10 19:04:17 +0000 | [diff] [blame] | 459 | fprintf(f, " installed %u sec", (unsigned)(tm->install/hz)); |
osdl.net!shemminger | 2373fde | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 460 | if (tm->lastuse != 0) |
osdl.net!shemminger | 63ae25d | 2005-03-10 19:04:17 +0000 | [diff] [blame] | 461 | fprintf(f, " used %u sec", (unsigned)(tm->lastuse/hz)); |
osdl.net!shemminger | 2373fde | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 462 | if (tm->expires != 0) |
osdl.net!shemminger | 63ae25d | 2005-03-10 19:04:17 +0000 | [diff] [blame] | 463 | fprintf(f, " expires %u sec", (unsigned)(tm->expires/hz)); |
osdl.net!shemminger | 2373fde | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 464 | } |
ch[shemminger]!tgraf | e5879dc | 2004-12-07 23:52:52 +0000 | [diff] [blame] | 465 | |
| 466 | void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats) |
| 467 | { |
| 468 | SPRINT_BUF(b1); |
14!tgraf | 7d69fd9 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 469 | struct rtattr *tbs[TCA_STATS_MAX + 1]; |
ch[shemminger]!tgraf | e5879dc | 2004-12-07 23:52:52 +0000 | [diff] [blame] | 470 | |
14!tgraf | 7d69fd9 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 471 | parse_rtattr_nested(tbs, TCA_STATS_MAX, rta); |
ch[shemminger]!tgraf | e5879dc | 2004-12-07 23:52:52 +0000 | [diff] [blame] | 472 | |
| 473 | if (tbs[TCA_STATS_BASIC]) { |
| 474 | struct gnet_stats_basic bs = {0}; |
| 475 | memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs))); |
| 476 | fprintf(fp, "%sSent %llu bytes %u pkt", |
net[shemminger]!shemminger | b906243 | 2005-01-17 23:28:16 +0000 | [diff] [blame] | 477 | prefix, (unsigned long long) bs.bytes, bs.packets); |
ch[shemminger]!tgraf | e5879dc | 2004-12-07 23:52:52 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | if (tbs[TCA_STATS_QUEUE]) { |
| 481 | struct gnet_stats_queue q = {0}; |
| 482 | memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q))); |
| 483 | fprintf(fp, " (dropped %u, overlimits %u requeues %u) ", |
| 484 | q.drops, q.overlimits, q.requeues); |
| 485 | } |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 486 | |
Eric Dumazet | 8f7574e | 2013-09-17 04:19:03 -0700 | [diff] [blame] | 487 | if (tbs[TCA_STATS_RATE_EST64]) { |
| 488 | struct gnet_stats_rate_est64 re = {0}; |
| 489 | |
| 490 | memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST64]), |
| 491 | MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST64]), |
| 492 | sizeof(re))); |
| 493 | fprintf(fp, "\n%srate %s %llupps ", |
| 494 | prefix, sprint_rate(re.bps, b1), re.pps); |
| 495 | } else if (tbs[TCA_STATS_RATE_EST]) { |
ch[shemminger]!tgraf | e5879dc | 2004-12-07 23:52:52 +0000 | [diff] [blame] | 496 | struct gnet_stats_rate_est re = {0}; |
Eric Dumazet | 8f7574e | 2013-09-17 04:19:03 -0700 | [diff] [blame] | 497 | |
| 498 | memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]), |
| 499 | MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re))); |
ch[shemminger]!tgraf | e5879dc | 2004-12-07 23:52:52 +0000 | [diff] [blame] | 500 | fprintf(fp, "\n%srate %s %upps ", |
| 501 | prefix, sprint_rate(re.bps, b1), re.pps); |
| 502 | } |
| 503 | |
| 504 | if (tbs[TCA_STATS_QUEUE]) { |
| 505 | struct gnet_stats_queue q = {0}; |
| 506 | memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q))); |
| 507 | if (!tbs[TCA_STATS_RATE_EST]) |
| 508 | fprintf(fp, "\n%s", prefix); |
| 509 | fprintf(fp, "backlog %s %up requeues %u ", |
| 510 | sprint_size(q.backlog, b1), q.qlen, q.requeues); |
| 511 | } |
| 512 | |
| 513 | if (xstats) |
| 514 | *xstats = tbs[TCA_STATS_APP] ? : NULL; |
| 515 | } |
| 516 | |
| 517 | void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats) |
| 518 | { |
| 519 | SPRINT_BUF(b1); |
| 520 | |
| 521 | if (tb[TCA_STATS2]) { |
| 522 | print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats); |
| 523 | if (xstats && NULL == *xstats) |
| 524 | goto compat_xstats; |
| 525 | return; |
| 526 | } |
| 527 | /* backward compatibility */ |
| 528 | if (tb[TCA_STATS]) { |
| 529 | struct tc_stats st; |
| 530 | |
| 531 | /* handle case where kernel returns more/less than we know about */ |
| 532 | memset(&st, 0, sizeof(st)); |
| 533 | memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st))); |
| 534 | |
| 535 | fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ", |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 536 | prefix, (unsigned long long)st.bytes, st.packets, st.drops, |
ch[shemminger]!tgraf | e5879dc | 2004-12-07 23:52:52 +0000 | [diff] [blame] | 537 | st.overlimits); |
| 538 | |
| 539 | if (st.bps || st.pps || st.qlen || st.backlog) { |
| 540 | fprintf(fp, "\n%s", prefix); |
| 541 | if (st.bps || st.pps) { |
| 542 | fprintf(fp, "rate "); |
| 543 | if (st.bps) |
| 544 | fprintf(fp, "%s ", sprint_rate(st.bps, b1)); |
| 545 | if (st.pps) |
| 546 | fprintf(fp, "%upps ", st.pps); |
| 547 | } |
| 548 | if (st.qlen || st.backlog) { |
| 549 | fprintf(fp, "backlog "); |
| 550 | if (st.backlog) |
| 551 | fprintf(fp, "%s ", sprint_size(st.backlog, b1)); |
| 552 | if (st.qlen) |
| 553 | fprintf(fp, "%up ", st.qlen); |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | compat_xstats: |
| 559 | if (tb[TCA_XSTATS] && xstats) |
| 560 | *xstats = tb[TCA_XSTATS]; |
| 561 | } |
| 562 | |