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