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