]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/tc_util.c
(Logical change 1.3)
[mirror_iproute2.git] / tc / tc_util.c
CommitLineData
aba5acdf
SH
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
27int get_qdisc_handle(__u32 *h, char *str)
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;
41ok:
42 *h = maj;
43 return 0;
44}
45
46int get_tc_classid(__u32 *h, char *str)
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
73ok:
74 *h = maj;
75 return 0;
76}
77
78int 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
93char * 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
100
101int get_rate(unsigned *rate, char *str)
102{
103 char *p;
104 double bps = strtod(str, &p);
105
106 if (p == str)
107 return -1;
108
109 if (*p) {
110 if (strcasecmp(p, "kbps") == 0)
111 bps *= 1024;
112 else if (strcasecmp(p, "mbps") == 0)
113 bps *= 1024*1024;
114 else if (strcasecmp(p, "mbit") == 0)
115 bps *= 1024*1024/8;
116 else if (strcasecmp(p, "kbit") == 0)
117 bps *= 1024/8;
118 else if (strcasecmp(p, "bps") != 0)
119 return -1;
120 } else
121 bps /= 8;
122
123 *rate = bps;
124 return 0;
125}
126
127int get_rate_and_cell(unsigned *rate, int *cell_log, char *str)
128{
129 char * slash = strchr(str, '/');
130
131 if (slash)
132 *slash = 0;
133
134 if (get_rate(rate, str))
135 return -1;
136
137 if (slash) {
138 int cell;
139 int i;
140
141 if (get_integer(&cell, slash+1, 0))
142 return -1;
143 *slash = '/';
144
145 for (i=0; i<32; i++) {
146 if ((1<<i) == cell) {
147 *cell_log = i;
148 return 0;
149 }
150 }
151 return -1;
152 }
153 return 0;
154}
155
156
157int print_rate(char *buf, int len, __u32 rate)
158{
159 double tmp = (double)rate*8;
160
161 if (tmp >= 1024*1023 && fabs(1024*1024*rint(tmp/(1024*1024)) - tmp) < 1024)
162 snprintf(buf, len, "%gMbit", rint(tmp/(1024*1024)));
163 else if (tmp >= 1024-16 && fabs(1024*rint(tmp/1024) - tmp) < 16)
164 snprintf(buf, len, "%gKbit", rint(tmp/1024));
165 else
166 snprintf(buf, len, "%ubps", rate);
167 return 0;
168}
169
170char * sprint_rate(__u32 rate, char *buf)
171{
172 if (print_rate(buf, SPRINT_BSIZE-1, rate))
173 strcpy(buf, "???");
174 return buf;
175}
176
177int get_usecs(unsigned *usecs, char *str)
178{
179 double t;
180 char *p;
181
182 t = strtod(str, &p);
183 if (p == str)
184 return -1;
185
186 if (*p) {
187 if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 ||
188 strcasecmp(p, "secs")==0)
189 t *= 1000000;
190 else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 ||
191 strcasecmp(p, "msecs") == 0)
192 t *= 1000;
193 else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 ||
194 strcasecmp(p, "usecs") == 0)
195 t *= 1;
196 else
197 return -1;
198 }
199
200 *usecs = t;
201 return 0;
202}
203
204
205int print_usecs(char *buf, int len, __u32 usec)
206{
207 double tmp = usec;
208
209 if (tmp >= 1000000)
210 snprintf(buf, len, "%.1fs", tmp/1000000);
211 else if (tmp >= 1000)
212 snprintf(buf, len, "%.1fms", tmp/1000);
213 else
214 snprintf(buf, len, "%uus", usec);
215 return 0;
216}
217
218char * sprint_usecs(__u32 usecs, char *buf)
219{
220 if (print_usecs(buf, SPRINT_BSIZE-1, usecs))
221 strcpy(buf, "???");
222 return buf;
223}
224
225int get_size(unsigned *size, char *str)
226{
227 double sz;
228 char *p;
229
230 sz = strtod(str, &p);
231 if (p == str)
232 return -1;
233
234 if (*p) {
235 if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k")==0)
236 sz *= 1024;
237 else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m")==0)
238 sz *= 1024*1024;
239 else if (strcasecmp(p, "mbit") == 0)
240 sz *= 1024*1024/8;
241 else if (strcasecmp(p, "kbit") == 0)
242 sz *= 1024/8;
243 else if (strcasecmp(p, "b") != 0)
244 return -1;
245 }
246
247 *size = sz;
248 return 0;
249}
250
251int get_size_and_cell(unsigned *size, int *cell_log, char *str)
252{
253 char * slash = strchr(str, '/');
254
255 if (slash)
256 *slash = 0;
257
258 if (get_size(size, str))
259 return -1;
260
261 if (slash) {
262 int cell;
263 int i;
264
265 if (get_integer(&cell, slash+1, 0))
266 return -1;
267 *slash = '/';
268
269 for (i=0; i<32; i++) {
270 if ((1<<i) == cell) {
271 *cell_log = i;
272 return 0;
273 }
274 }
275 return -1;
276 }
277 return 0;
278}
279
280int print_size(char *buf, int len, __u32 sz)
281{
282 double tmp = sz;
283
284 if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
285 snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
286 else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
287 snprintf(buf, len, "%gKb", rint(tmp/1024));
288 else
289 snprintf(buf, len, "%ub", sz);
290 return 0;
291}
292
293char * sprint_size(__u32 size, char *buf)
294{
295 if (print_size(buf, SPRINT_BSIZE-1, size))
296 strcpy(buf, "???");
297 return buf;
298}
299
300int print_qdisc_handle(char *buf, int len, __u32 h)
301{
302 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
303 return 0;
304}
305
306char * sprint_qdisc_handle(__u32 h, char *buf)
307{
308 if (print_qdisc_handle(buf, SPRINT_BSIZE-1, h))
309 strcpy(buf, "???");
310 return buf;
311}
312
313