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 |
Christoph J. Thompson | 5c434a9 | 2012-03-01 17:46:26 +0100 | [diff] [blame] | 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 | for (s = suffixes; s->name; ++s) { |
| 156 | if (strcasecmp(s->name, p) == 0) { |
Eric Dumazet | a303853 | 2013-06-03 05:51:33 +0000 | [diff] [blame^] | 157 | bps *= s->scale; |
| 158 | p += strlen(p); |
| 159 | break; |
osdl.net!shemminger | 26ab0b1 | 2004-08-13 23:23:46 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Eric Dumazet | a303853 | 2013-06-03 05:51:33 +0000 | [diff] [blame^] | 163 | if (*p) |
| 164 | return -1; /* unknown suffix */ |
| 165 | |
| 166 | bps /= 8; /* -> bytes per second */ |
| 167 | *rate = bps; |
| 168 | /* detect if an overflow happened */ |
| 169 | if (*rate != floor(bps)) |
| 170 | return -1; |
| 171 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 172 | } |
| 173 | |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame] | 174 | void print_rate(char *buf, int len, __u32 rate) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 175 | { |
| 176 | double tmp = (double)rate*8; |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame] | 177 | extern int use_iec; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 178 | |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame] | 179 | if (use_iec) { |
osdl.net!shemminger | abf1d0b | 2004-10-19 19:51:41 +0000 | [diff] [blame] | 180 | if (tmp >= 1000.0*1024.0*1024.0) |
Andreas Henriksson | f526af9 | 2012-03-09 17:09:19 +0100 | [diff] [blame] | 181 | snprintf(buf, len, "%.0fMibit", tmp/(1024.0*1024.0)); |
osdl.net!shemminger | abf1d0b | 2004-10-19 19:51:41 +0000 | [diff] [blame] | 182 | else if (tmp >= 1000.0*1024) |
| 183 | snprintf(buf, len, "%.0fKibit", tmp/1024); |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame] | 184 | else |
osdl.net!shemminger | abf1d0b | 2004-10-19 19:51:41 +0000 | [diff] [blame] | 185 | snprintf(buf, len, "%.0fbit", tmp); |
| 186 | } else { |
| 187 | if (tmp >= 1000.0*1000000.0) |
| 188 | snprintf(buf, len, "%.0fMbit", tmp/1000000.0); |
| 189 | else if (tmp >= 1000.0 * 1000.0) |
| 190 | snprintf(buf, len, "%.0fKbit", tmp/1000.0); |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame] | 191 | else |
osdl.net!shemminger | abf1d0b | 2004-10-19 19:51:41 +0000 | [diff] [blame] | 192 | snprintf(buf, len, "%.0fbit", tmp); |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame] | 193 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | char * sprint_rate(__u32 rate, char *buf) |
| 197 | { |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame] | 198 | print_rate(buf, SPRINT_BSIZE-1, rate); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 199 | return buf; |
| 200 | } |
| 201 | |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 202 | int get_time(unsigned *time, const char *str) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 203 | { |
| 204 | double t; |
| 205 | char *p; |
| 206 | |
| 207 | t = strtod(str, &p); |
| 208 | if (p == str) |
| 209 | return -1; |
| 210 | |
| 211 | if (*p) { |
| 212 | if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 || |
| 213 | strcasecmp(p, "secs")==0) |
Patrick McHardy | f0bda7e | 2007-03-04 20:14:59 +0100 | [diff] [blame] | 214 | t *= TIME_UNITS_PER_SEC; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 215 | else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 || |
| 216 | strcasecmp(p, "msecs") == 0) |
Patrick McHardy | f0bda7e | 2007-03-04 20:14:59 +0100 | [diff] [blame] | 217 | t *= TIME_UNITS_PER_SEC/1000; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 218 | else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 || |
| 219 | strcasecmp(p, "usecs") == 0) |
Patrick McHardy | f0bda7e | 2007-03-04 20:14:59 +0100 | [diff] [blame] | 220 | t *= TIME_UNITS_PER_SEC/1000000; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 221 | else |
| 222 | return -1; |
| 223 | } |
| 224 | |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 225 | *time = t; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 230 | void print_time(char *buf, int len, __u32 time) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 231 | { |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 232 | double tmp = time; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 233 | |
Patrick McHardy | f0bda7e | 2007-03-04 20:14:59 +0100 | [diff] [blame] | 234 | if (tmp >= TIME_UNITS_PER_SEC) |
| 235 | snprintf(buf, len, "%.1fs", tmp/TIME_UNITS_PER_SEC); |
| 236 | else if (tmp >= TIME_UNITS_PER_SEC/1000) |
| 237 | snprintf(buf, len, "%.1fms", tmp/(TIME_UNITS_PER_SEC/1000)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 238 | else |
Stephen Hemminger | 8915144 | 2007-03-14 10:14:07 -0700 | [diff] [blame] | 239 | snprintf(buf, len, "%uus", time); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 242 | char * sprint_time(__u32 time, char *buf) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 243 | { |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 244 | print_time(buf, SPRINT_BSIZE-1, time); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 245 | return buf; |
| 246 | } |
| 247 | |
Patrick McHardy | bd29e35 | 2007-03-04 20:15:01 +0100 | [diff] [blame] | 248 | char * sprint_ticks(__u32 ticks, char *buf) |
| 249 | { |
| 250 | return sprint_time(tc_core_tick2time(ticks), buf); |
| 251 | } |
| 252 | |
osdl.org!shemminger | dbd90dc | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 253 | int get_size(unsigned *size, const char *str) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 254 | { |
| 255 | double sz; |
| 256 | char *p; |
| 257 | |
| 258 | sz = strtod(str, &p); |
| 259 | if (p == str) |
| 260 | return -1; |
| 261 | |
| 262 | if (*p) { |
| 263 | if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k")==0) |
| 264 | sz *= 1024; |
osdl.org!shemminger | dbd90dc | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 265 | else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g")==0) |
| 266 | sz *= 1024*1024*1024; |
| 267 | else if (strcasecmp(p, "gbit") == 0) |
| 268 | sz *= 1024*1024*1024/8; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 269 | else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m")==0) |
| 270 | sz *= 1024*1024; |
| 271 | else if (strcasecmp(p, "mbit") == 0) |
| 272 | sz *= 1024*1024/8; |
| 273 | else if (strcasecmp(p, "kbit") == 0) |
| 274 | sz *= 1024/8; |
| 275 | else if (strcasecmp(p, "b") != 0) |
| 276 | return -1; |
| 277 | } |
| 278 | |
| 279 | *size = sz; |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | int get_size_and_cell(unsigned *size, int *cell_log, char *str) |
| 284 | { |
| 285 | char * slash = strchr(str, '/'); |
| 286 | |
| 287 | if (slash) |
| 288 | *slash = 0; |
| 289 | |
| 290 | if (get_size(size, str)) |
| 291 | return -1; |
| 292 | |
| 293 | if (slash) { |
| 294 | int cell; |
| 295 | int i; |
| 296 | |
| 297 | if (get_integer(&cell, slash+1, 0)) |
| 298 | return -1; |
| 299 | *slash = '/'; |
| 300 | |
| 301 | for (i=0; i<32; i++) { |
| 302 | if ((1<<i) == cell) { |
| 303 | *cell_log = i; |
| 304 | return 0; |
| 305 | } |
| 306 | } |
| 307 | return -1; |
| 308 | } |
| 309 | return 0; |
| 310 | } |
| 311 | |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame] | 312 | void print_size(char *buf, int len, __u32 sz) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 313 | { |
| 314 | double tmp = sz; |
| 315 | |
| 316 | if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024) |
| 317 | snprintf(buf, len, "%gMb", rint(tmp/(1024*1024))); |
| 318 | else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16) |
| 319 | snprintf(buf, len, "%gKb", rint(tmp/1024)); |
| 320 | else |
| 321 | snprintf(buf, len, "%ub", sz); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | char * sprint_size(__u32 size, char *buf) |
| 325 | { |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame] | 326 | print_size(buf, SPRINT_BSIZE-1, size); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 327 | return buf; |
| 328 | } |
| 329 | |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame] | 330 | void print_qdisc_handle(char *buf, int len, __u32 h) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 331 | { |
| 332 | snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | char * sprint_qdisc_handle(__u32 h, char *buf) |
| 336 | { |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame] | 337 | print_qdisc_handle(buf, SPRINT_BSIZE-1, h); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 338 | return buf; |
| 339 | } |
| 340 | |
osdl.net!shemminger | 2373fde | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 341 | char * action_n2a(int action, char *buf, int len) |
| 342 | { |
| 343 | switch (action) { |
| 344 | case -1: |
| 345 | return "continue"; |
| 346 | break; |
| 347 | case TC_ACT_OK: |
| 348 | return "pass"; |
| 349 | break; |
| 350 | case TC_ACT_SHOT: |
| 351 | return "drop"; |
| 352 | break; |
| 353 | case TC_ACT_RECLASSIFY: |
| 354 | return "reclassify"; |
| 355 | case TC_ACT_PIPE: |
| 356 | return "pipe"; |
| 357 | case TC_ACT_STOLEN: |
| 358 | return "stolen"; |
| 359 | default: |
| 360 | snprintf(buf, len, "%d", action); |
| 361 | return buf; |
| 362 | } |
| 363 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 364 | |
osdl.net!shemminger | 2373fde | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 365 | int action_a2n(char *arg, int *result) |
| 366 | { |
| 367 | int res; |
| 368 | |
| 369 | if (matches(arg, "continue") == 0) |
| 370 | res = -1; |
| 371 | else if (matches(arg, "drop") == 0) |
| 372 | res = TC_ACT_SHOT; |
| 373 | else if (matches(arg, "shot") == 0) |
| 374 | res = TC_ACT_SHOT; |
| 375 | else if (matches(arg, "pass") == 0) |
| 376 | res = TC_ACT_OK; |
| 377 | else if (strcmp(arg, "ok") == 0) |
| 378 | res = TC_ACT_OK; |
| 379 | else if (matches(arg, "reclassify") == 0) |
| 380 | res = TC_ACT_RECLASSIFY; |
| 381 | else { |
| 382 | char dummy; |
| 383 | if (sscanf(arg, "%d%c", &res, &dummy) != 1) |
| 384 | return -1; |
| 385 | } |
| 386 | *result = res; |
| 387 | return 0; |
| 388 | } |
| 389 | |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 390 | int get_linklayer(unsigned *val, const char *arg) |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame] | 391 | { |
| 392 | int res; |
| 393 | |
| 394 | if (matches(arg, "ethernet") == 0) |
| 395 | res = LINKLAYER_ETHERNET; |
| 396 | else if (matches(arg, "atm") == 0) |
| 397 | res = LINKLAYER_ATM; |
| 398 | else if (matches(arg, "adsl") == 0) |
| 399 | res = LINKLAYER_ATM; |
| 400 | else |
| 401 | return -1; /* Indicate error */ |
| 402 | |
| 403 | *val = res; |
| 404 | return 0; |
| 405 | } |
| 406 | |
Jussi Kivilinna | 839c845 | 2008-07-25 16:19:09 +0300 | [diff] [blame] | 407 | void print_linklayer(char *buf, int len, unsigned linklayer) |
| 408 | { |
| 409 | switch (linklayer) { |
| 410 | case LINKLAYER_UNSPEC: |
| 411 | snprintf(buf, len, "%s", "unspec"); |
| 412 | return; |
| 413 | case LINKLAYER_ETHERNET: |
| 414 | snprintf(buf, len, "%s", "ethernet"); |
| 415 | return; |
| 416 | case LINKLAYER_ATM: |
| 417 | snprintf(buf, len, "%s", "atm"); |
| 418 | return; |
| 419 | default: |
| 420 | snprintf(buf, len, "%s", "unknown"); |
| 421 | return; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | char *sprint_linklayer(unsigned linklayer, char *buf) |
| 426 | { |
| 427 | print_linklayer(buf, SPRINT_BSIZE-1, linklayer); |
| 428 | return buf; |
| 429 | } |
| 430 | |
osdl.net!shemminger | 6dc9f01 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 431 | void print_tm(FILE * f, const struct tcf_t *tm) |
osdl.net!shemminger | 2373fde | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 432 | { |
net[shemminger]!shemminger | 5e8bc63 | 2005-03-14 18:44:54 +0000 | [diff] [blame] | 433 | int hz = get_user_hz(); |
osdl.net!shemminger | 2373fde | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 434 | if (tm->install != 0) |
osdl.net!shemminger | 63ae25d | 2005-03-10 19:04:17 +0000 | [diff] [blame] | 435 | fprintf(f, " installed %u sec", (unsigned)(tm->install/hz)); |
osdl.net!shemminger | 2373fde | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 436 | if (tm->lastuse != 0) |
osdl.net!shemminger | 63ae25d | 2005-03-10 19:04:17 +0000 | [diff] [blame] | 437 | fprintf(f, " used %u sec", (unsigned)(tm->lastuse/hz)); |
osdl.net!shemminger | 2373fde | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 438 | if (tm->expires != 0) |
osdl.net!shemminger | 63ae25d | 2005-03-10 19:04:17 +0000 | [diff] [blame] | 439 | fprintf(f, " expires %u sec", (unsigned)(tm->expires/hz)); |
osdl.net!shemminger | 2373fde | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 440 | } |
ch[shemminger]!tgraf | e5879dc | 2004-12-07 23:52:52 +0000 | [diff] [blame] | 441 | |
| 442 | void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats) |
| 443 | { |
| 444 | SPRINT_BUF(b1); |
14!tgraf | 7d69fd9 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 445 | struct rtattr *tbs[TCA_STATS_MAX + 1]; |
ch[shemminger]!tgraf | e5879dc | 2004-12-07 23:52:52 +0000 | [diff] [blame] | 446 | |
14!tgraf | 7d69fd9 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 447 | parse_rtattr_nested(tbs, TCA_STATS_MAX, rta); |
ch[shemminger]!tgraf | e5879dc | 2004-12-07 23:52:52 +0000 | [diff] [blame] | 448 | |
| 449 | if (tbs[TCA_STATS_BASIC]) { |
| 450 | struct gnet_stats_basic bs = {0}; |
| 451 | memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs))); |
| 452 | fprintf(fp, "%sSent %llu bytes %u pkt", |
net[shemminger]!shemminger | b906243 | 2005-01-17 23:28:16 +0000 | [diff] [blame] | 453 | prefix, (unsigned long long) bs.bytes, bs.packets); |
ch[shemminger]!tgraf | e5879dc | 2004-12-07 23:52:52 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | if (tbs[TCA_STATS_QUEUE]) { |
| 457 | struct gnet_stats_queue q = {0}; |
| 458 | memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q))); |
| 459 | fprintf(fp, " (dropped %u, overlimits %u requeues %u) ", |
| 460 | q.drops, q.overlimits, q.requeues); |
| 461 | } |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 462 | |
ch[shemminger]!tgraf | e5879dc | 2004-12-07 23:52:52 +0000 | [diff] [blame] | 463 | if (tbs[TCA_STATS_RATE_EST]) { |
| 464 | struct gnet_stats_rate_est re = {0}; |
| 465 | memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re))); |
| 466 | fprintf(fp, "\n%srate %s %upps ", |
| 467 | prefix, sprint_rate(re.bps, b1), re.pps); |
| 468 | } |
| 469 | |
| 470 | if (tbs[TCA_STATS_QUEUE]) { |
| 471 | struct gnet_stats_queue q = {0}; |
| 472 | memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q))); |
| 473 | if (!tbs[TCA_STATS_RATE_EST]) |
| 474 | fprintf(fp, "\n%s", prefix); |
| 475 | fprintf(fp, "backlog %s %up requeues %u ", |
| 476 | sprint_size(q.backlog, b1), q.qlen, q.requeues); |
| 477 | } |
| 478 | |
| 479 | if (xstats) |
| 480 | *xstats = tbs[TCA_STATS_APP] ? : NULL; |
| 481 | } |
| 482 | |
| 483 | void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats) |
| 484 | { |
| 485 | SPRINT_BUF(b1); |
| 486 | |
| 487 | if (tb[TCA_STATS2]) { |
| 488 | print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats); |
| 489 | if (xstats && NULL == *xstats) |
| 490 | goto compat_xstats; |
| 491 | return; |
| 492 | } |
| 493 | /* backward compatibility */ |
| 494 | if (tb[TCA_STATS]) { |
| 495 | struct tc_stats st; |
| 496 | |
| 497 | /* handle case where kernel returns more/less than we know about */ |
| 498 | memset(&st, 0, sizeof(st)); |
| 499 | memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st))); |
| 500 | |
| 501 | fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ", |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 502 | prefix, (unsigned long long)st.bytes, st.packets, st.drops, |
ch[shemminger]!tgraf | e5879dc | 2004-12-07 23:52:52 +0000 | [diff] [blame] | 503 | st.overlimits); |
| 504 | |
| 505 | if (st.bps || st.pps || st.qlen || st.backlog) { |
| 506 | fprintf(fp, "\n%s", prefix); |
| 507 | if (st.bps || st.pps) { |
| 508 | fprintf(fp, "rate "); |
| 509 | if (st.bps) |
| 510 | fprintf(fp, "%s ", sprint_rate(st.bps, b1)); |
| 511 | if (st.pps) |
| 512 | fprintf(fp, "%upps ", st.pps); |
| 513 | } |
| 514 | if (st.qlen || st.backlog) { |
| 515 | fprintf(fp, "backlog "); |
| 516 | if (st.backlog) |
| 517 | fprintf(fp, "%s ", sprint_size(st.backlog, b1)); |
| 518 | if (st.qlen) |
| 519 | fprintf(fp, "%up ", st.qlen); |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | compat_xstats: |
| 525 | if (tb[TCA_XSTATS] && xstats) |
| 526 | *xstats = tb[TCA_XSTATS]; |
| 527 | } |
| 528 | |