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 == ':') { |
| 64 | maj <<= 16; |
| 65 | str = p+1; |
| 66 | min = strtoul(str, &p, 16); |
| 67 | if (*p != 0) |
| 68 | return -1; |
| 69 | maj |= min; |
| 70 | } else if (*p != 0) |
| 71 | return -1; |
| 72 | |
| 73 | ok: |
| 74 | *h = maj; |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | int print_tc_classid(char *buf, int len, __u32 h) |
| 79 | { |
| 80 | if (h == TC_H_ROOT) |
| 81 | sprintf(buf, "root"); |
| 82 | else if (h == TC_H_UNSPEC) |
| 83 | snprintf(buf, len, "none"); |
| 84 | else if (TC_H_MAJ(h) == 0) |
| 85 | snprintf(buf, len, ":%x", TC_H_MIN(h)); |
| 86 | else if (TC_H_MIN(h) == 0) |
| 87 | snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16); |
| 88 | else |
| 89 | snprintf(buf, len, "%x:%x", TC_H_MAJ(h)>>16, TC_H_MIN(h)); |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | char * sprint_tc_classid(__u32 h, char *buf) |
| 94 | { |
| 95 | if (print_tc_classid(buf, SPRINT_BSIZE-1, h)) |
| 96 | strcpy(buf, "???"); |
| 97 | return buf; |
| 98 | } |
| 99 | |
osdl.org!shemminger | 4703e08 | 2004-06-09 21:29:27 +0000 | [diff] [blame] | 100 | /* |
osdl.org!shemminger | 72fc204 | 2004-06-09 22:03:03 +0000 | [diff] [blame] | 101 | * NB: rates are scaled differently depending on bits or bytes. |
| 102 | * if bits are requested then k == 1000 |
| 103 | * for bytes k = 1024 |
osdl.org!shemminger | 4703e08 | 2004-06-09 21:29:27 +0000 | [diff] [blame] | 104 | */ |
osdl.org!shemminger | dbd90dc | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 105 | int get_rate(unsigned *rate, const char *str) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 106 | { |
| 107 | char *p; |
| 108 | double bps = strtod(str, &p); |
| 109 | |
| 110 | if (p == str) |
| 111 | return -1; |
| 112 | |
osdl.org!shemminger | 72fc204 | 2004-06-09 22:03:03 +0000 | [diff] [blame] | 113 | if (*p == 0 || strcasecmp(p, "bit") == 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 114 | bps /= 8; |
osdl.org!shemminger | 72fc204 | 2004-06-09 22:03:03 +0000 | [diff] [blame] | 115 | else if (strcasecmp(p, "kbit") == 0) |
| 116 | bps = (bps * 1000.) / 8; |
| 117 | else if (strcasecmp(p, "mbit") == 0) |
| 118 | bps = (bps * 1000000.)/8; |
| 119 | else if (strcasecmp(p, "gbit") == 0) |
| 120 | bps = (bps * 1000000000.)/8; |
| 121 | else if (strcasecmp(p, "kibit") == 0) |
| 122 | bps *= 1024 / 8; |
| 123 | else if (strcasecmp(p, "mibit") == 0) |
| 124 | bps *= 1024*1024/8; |
| 125 | else if (strcasecmp(p, "gibit") == 0) |
| 126 | bps *= 1024*1024*1024/8; |
| 127 | else if (strcasecmp(p, "kbps") == 0) |
| 128 | bps *= 1024; |
| 129 | else if (strcasecmp(p, "mbps") == 0) |
| 130 | bps *= 1024*1024; |
| 131 | else if (strcasecmp(p, "gbps") == 0) |
| 132 | bps *= 1024*1024*1024; |
| 133 | else if (strcasecmp(p, "bps") != 0) |
| 134 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 135 | |
| 136 | *rate = bps; |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | int get_rate_and_cell(unsigned *rate, int *cell_log, char *str) |
| 141 | { |
| 142 | char * slash = strchr(str, '/'); |
| 143 | |
| 144 | if (slash) |
| 145 | *slash = 0; |
| 146 | |
| 147 | if (get_rate(rate, str)) |
| 148 | return -1; |
| 149 | |
| 150 | if (slash) { |
| 151 | int cell; |
| 152 | int i; |
| 153 | |
| 154 | if (get_integer(&cell, slash+1, 0)) |
| 155 | return -1; |
| 156 | *slash = '/'; |
| 157 | |
| 158 | for (i=0; i<32; i++) { |
| 159 | if ((1<<i) == cell) { |
| 160 | *cell_log = i; |
| 161 | return 0; |
| 162 | } |
| 163 | } |
| 164 | return -1; |
| 165 | } |
| 166 | return 0; |
| 167 | } |
| 168 | |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame^] | 169 | void print_rate(char *buf, int len, __u32 rate) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 170 | { |
| 171 | double tmp = (double)rate*8; |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame^] | 172 | extern int use_iec; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 173 | |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame^] | 174 | if (use_iec) { |
| 175 | if (tmp >= 1024*1023 && |
| 176 | fabs(1024*1024*rint(tmp/(1024*1024)) - tmp) < 1024) |
| 177 | snprintf(buf, len, "%gMibps", rint(tmp/(1024*1024))); |
| 178 | else if (tmp >= 1024-16 && fabs(1024*rint(tmp/1024) - tmp) < 16) |
| 179 | snprintf(buf, len, "%gKibps", rint(tmp/1024)); |
| 180 | else |
| 181 | snprintf(buf, len, "%ubps", rate); |
| 182 | |
| 183 | } else { |
| 184 | if (tmp >= 999999 && |
| 185 | fabs(1000000.*rint(tmp/1000000.) - tmp) < 1000) |
| 186 | snprintf(buf, len, "%gMbit", rint(tmp/1000000.)); |
| 187 | else if (tmp >= 990 && fabs(1000.*rint(tmp/1000.) - tmp) < 10) |
| 188 | snprintf(buf, len, "%gKbit", rint(tmp/1000.)); |
| 189 | else |
| 190 | snprintf(buf, len, "%ubit", rate); |
| 191 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | char * sprint_rate(__u32 rate, char *buf) |
| 195 | { |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame^] | 196 | print_rate(buf, SPRINT_BSIZE-1, rate); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 197 | return buf; |
| 198 | } |
| 199 | |
osdl.org!shemminger | dbd90dc | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 200 | int get_usecs(unsigned *usecs, const char *str) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 201 | { |
| 202 | double t; |
| 203 | char *p; |
| 204 | |
| 205 | t = strtod(str, &p); |
| 206 | if (p == str) |
| 207 | return -1; |
| 208 | |
| 209 | if (*p) { |
| 210 | if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 || |
| 211 | strcasecmp(p, "secs")==0) |
| 212 | t *= 1000000; |
| 213 | else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 || |
| 214 | strcasecmp(p, "msecs") == 0) |
| 215 | t *= 1000; |
| 216 | else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 || |
| 217 | strcasecmp(p, "usecs") == 0) |
| 218 | t *= 1; |
| 219 | else |
| 220 | return -1; |
| 221 | } |
| 222 | |
| 223 | *usecs = t; |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame^] | 228 | void print_usecs(char *buf, int len, __u32 usec) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 229 | { |
| 230 | double tmp = usec; |
| 231 | |
| 232 | if (tmp >= 1000000) |
| 233 | snprintf(buf, len, "%.1fs", tmp/1000000); |
| 234 | else if (tmp >= 1000) |
| 235 | snprintf(buf, len, "%.1fms", tmp/1000); |
| 236 | else |
| 237 | snprintf(buf, len, "%uus", usec); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | char * sprint_usecs(__u32 usecs, char *buf) |
| 241 | { |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame^] | 242 | print_usecs(buf, SPRINT_BSIZE-1, usecs); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 243 | return buf; |
| 244 | } |
| 245 | |
osdl.org!shemminger | dbd90dc | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 246 | int get_size(unsigned *size, const char *str) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 247 | { |
| 248 | double sz; |
| 249 | char *p; |
| 250 | |
| 251 | sz = strtod(str, &p); |
| 252 | if (p == str) |
| 253 | return -1; |
| 254 | |
| 255 | if (*p) { |
| 256 | if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k")==0) |
| 257 | sz *= 1024; |
osdl.org!shemminger | dbd90dc | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 258 | else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g")==0) |
| 259 | sz *= 1024*1024*1024; |
| 260 | else if (strcasecmp(p, "gbit") == 0) |
| 261 | sz *= 1024*1024*1024/8; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 262 | else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m")==0) |
| 263 | sz *= 1024*1024; |
| 264 | else if (strcasecmp(p, "mbit") == 0) |
| 265 | sz *= 1024*1024/8; |
| 266 | else if (strcasecmp(p, "kbit") == 0) |
| 267 | sz *= 1024/8; |
| 268 | else if (strcasecmp(p, "b") != 0) |
| 269 | return -1; |
| 270 | } |
| 271 | |
| 272 | *size = sz; |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | int get_size_and_cell(unsigned *size, int *cell_log, char *str) |
| 277 | { |
| 278 | char * slash = strchr(str, '/'); |
| 279 | |
| 280 | if (slash) |
| 281 | *slash = 0; |
| 282 | |
| 283 | if (get_size(size, str)) |
| 284 | return -1; |
| 285 | |
| 286 | if (slash) { |
| 287 | int cell; |
| 288 | int i; |
| 289 | |
| 290 | if (get_integer(&cell, slash+1, 0)) |
| 291 | return -1; |
| 292 | *slash = '/'; |
| 293 | |
| 294 | for (i=0; i<32; i++) { |
| 295 | if ((1<<i) == cell) { |
| 296 | *cell_log = i; |
| 297 | return 0; |
| 298 | } |
| 299 | } |
| 300 | return -1; |
| 301 | } |
| 302 | return 0; |
| 303 | } |
| 304 | |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame^] | 305 | void print_size(char *buf, int len, __u32 sz) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 306 | { |
| 307 | double tmp = sz; |
| 308 | |
| 309 | if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024) |
| 310 | snprintf(buf, len, "%gMb", rint(tmp/(1024*1024))); |
| 311 | else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16) |
| 312 | snprintf(buf, len, "%gKb", rint(tmp/1024)); |
| 313 | else |
| 314 | snprintf(buf, len, "%ub", sz); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | char * sprint_size(__u32 size, char *buf) |
| 318 | { |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame^] | 319 | print_size(buf, SPRINT_BSIZE-1, size); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 320 | return buf; |
| 321 | } |
| 322 | |
osdl.net!shemminger | 63e989f | 2004-06-25 20:59:28 +0000 | [diff] [blame] | 323 | static double percent_scale = (double)(1ull << 32) / 100.; |
| 324 | |
| 325 | int get_percent(__u32 *percent, const char *str) |
| 326 | { |
| 327 | char *p; |
| 328 | double per = strtod(str, &p); |
| 329 | |
| 330 | if (per > 100.) |
| 331 | return -1; |
| 332 | if (*p && strcmp(p, "%")) |
| 333 | return -1; |
| 334 | |
| 335 | *percent = per * percent_scale; |
| 336 | return 0; |
| 337 | } |
| 338 | |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame^] | 339 | void print_percent(char *buf, int len, __u32 per) |
osdl.net!shemminger | 63e989f | 2004-06-25 20:59:28 +0000 | [diff] [blame] | 340 | { |
| 341 | snprintf(buf, len, "%g%%", (double) per / percent_scale); |
osdl.net!shemminger | 63e989f | 2004-06-25 20:59:28 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | char * sprint_percent(__u32 per, char *buf) |
| 345 | { |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame^] | 346 | print_percent(buf, SPRINT_BSIZE-1, per); |
osdl.net!shemminger | 63e989f | 2004-06-25 20:59:28 +0000 | [diff] [blame] | 347 | return buf; |
| 348 | } |
| 349 | |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame^] | 350 | void print_qdisc_handle(char *buf, int len, __u32 h) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 351 | { |
| 352 | snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | char * sprint_qdisc_handle(__u32 h, char *buf) |
| 356 | { |
osdl.net!shemminger | d40b38b | 2004-06-28 20:42:59 +0000 | [diff] [blame^] | 357 | print_qdisc_handle(buf, SPRINT_BSIZE-1, h); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 358 | return buf; |
| 359 | } |
| 360 | |
| 361 | |