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