Line 0
Link Here
|
|
|
1 |
#include <stdio.h> |
2 |
#include <stdlib.h> |
3 |
#include <unistd.h> |
4 |
#include <syslog.h> |
5 |
#include <fcntl.h> |
6 |
#include <sys/socket.h> |
7 |
#include <netinet/in.h> |
8 |
#include <arpa/inet.h> |
9 |
#include <string.h> |
10 |
#include <math.h> |
11 |
|
12 |
#include "utils.h" |
13 |
#include "tc_util.h" |
14 |
|
15 |
#define usage() return(-1) |
16 |
|
17 |
// Returns -1 on error |
18 |
static int wrr_parse_qdisc_weight(int argc, char** argv, |
19 |
struct tc_wrr_qdisc_modf* opt) { |
20 |
int i; |
21 |
|
22 |
opt->weight1.weight_mode=-1; |
23 |
opt->weight2.weight_mode=-1; |
24 |
|
25 |
for(i=0; i<argc; i++) { |
26 |
if(!memcmp(argv[i],"wmode1=",7)) { |
27 |
opt->weight1.weight_mode=atoi(argv[i]+7); |
28 |
} else if(!memcmp(argv[i],"wmode2=",7)) { |
29 |
opt->weight2.weight_mode=atoi(argv[i]+7); |
30 |
} else { |
31 |
printf("Usage: ... [wmode1=0|1|2|3] [wmode2=0|1|2|3]\n"); |
32 |
return -1; |
33 |
} |
34 |
} |
35 |
return 0; |
36 |
} |
37 |
|
38 |
static int wrr_parse_class_modf(int argc, char** argv, |
39 |
struct tc_wrr_class_modf* modf) { |
40 |
int i; |
41 |
|
42 |
if(argc<1) { |
43 |
fprintf(stderr, "Usage: ... [weight1=val] [decr1=val] [incr1=val] [min1=val] [max1=val] [val2=val] ...\n"); |
44 |
fprintf(stderr, " The values can be floating point like 0.42 or divisions like 42/100\n"); |
45 |
return -1; |
46 |
} |
47 |
|
48 |
// Set meaningless values: |
49 |
modf->weight1.val=0; |
50 |
modf->weight1.decr=(__u64)-1; |
51 |
modf->weight1.incr=(__u64)-1; |
52 |
modf->weight1.min=0; |
53 |
modf->weight1.max=0; |
54 |
modf->weight2.val=0; |
55 |
modf->weight2.decr=(__u64)-1; |
56 |
modf->weight2.incr=(__u64)-1; |
57 |
modf->weight2.min=0; |
58 |
modf->weight2.max=0; |
59 |
|
60 |
// And read values: |
61 |
for(i=0; i<argc; i++) { |
62 |
char arg[80]; |
63 |
char* name,*value1=0,*value2=0; |
64 |
long double f_val1,f_val2=1,value; |
65 |
if(strlen(argv[i])>=sizeof(arg)) { |
66 |
fprintf(stderr,"Argument too long: %s\n",argv[i]); |
67 |
return -1; |
68 |
} |
69 |
strcpy(arg,argv[i]); |
70 |
|
71 |
name=strtok(arg,"="); |
72 |
if(name) value1=strtok(0,"/"); |
73 |
if(value1) value2=strtok(0,""); |
74 |
|
75 |
if(!value1) { |
76 |
fprintf(stderr,"No = found in argument: %s\n",argv[i]); |
77 |
return -1; |
78 |
} |
79 |
|
80 |
f_val1=atof(value1); |
81 |
if(value2) f_val2=atof(value2); |
82 |
|
83 |
if(f_val2==0) { |
84 |
fprintf(stderr,"Division by 0\n"); |
85 |
return -1; |
86 |
} |
87 |
|
88 |
value=f_val1/f_val2; |
89 |
if(value>1) value=1; |
90 |
if(value<0) value=0; |
91 |
value*=((__u64)-1); |
92 |
|
93 |
// And find the value set |
94 |
if(!strcmp(name,"weight1")) modf->weight1.val=value; |
95 |
else if(!strcmp(name,"decr1")) modf->weight1.decr=value; |
96 |
else if(!strcmp(name,"incr1")) modf->weight1.incr=value; |
97 |
else if(!strcmp(name,"min1")) modf->weight1.min=value; |
98 |
else if(!strcmp(name,"max1")) modf->weight1.max=value; |
99 |
else if(!strcmp(name,"weight2")) modf->weight2.val=value; |
100 |
else if(!strcmp(name,"decr2")) modf->weight2.decr=value; |
101 |
else if(!strcmp(name,"incr2")) modf->weight2.incr=value; |
102 |
else if(!strcmp(name,"min2")) modf->weight2.min=value; |
103 |
else if(!strcmp(name,"max2")) modf->weight2.max=value; |
104 |
else { |
105 |
fprintf(stderr,"illegal value: %s\n",name); |
106 |
return -1; |
107 |
} |
108 |
} |
109 |
|
110 |
return 0; |
111 |
} |
112 |
|
113 |
static int wrr_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n) |
114 |
{ |
115 |
if(n->nlmsg_flags & NLM_F_CREATE) { |
116 |
// This is a create request: |
117 |
struct tc_wrr_qdisc_crt opt; |
118 |
|
119 |
int sour,dest,ip,mac,masq; |
120 |
|
121 |
if(argc<4) { |
122 |
fprintf(stderr, "Usage: ... wrr sour|dest ip|masq|mac maxclasses proxymaxcon [penalty-setup]\n"); |
123 |
return -1; |
124 |
} |
125 |
|
126 |
// Read sour/dest: |
127 |
memset(&opt,0,sizeof(opt)); |
128 |
sour=!strcmp(argv[0],"sour"); |
129 |
dest=!strcmp(argv[0],"dest"); |
130 |
|
131 |
if(!sour && !dest) { |
132 |
fprintf(stderr,"sour or dest must be specified\n"); |
133 |
return -1; |
134 |
} |
135 |
|
136 |
// Read ip/mac |
137 |
ip=!strcmp(argv[1],"ip"); |
138 |
mac=!strcmp(argv[1],"mac"); |
139 |
masq=!strcmp(argv[1],"masq"); |
140 |
|
141 |
if(!ip && !mac && !masq) { |
142 |
fprintf(stderr,"ip, masq or mac must be specified\n"); |
143 |
return -1; |
144 |
} |
145 |
|
146 |
opt.srcaddr=sour; |
147 |
opt.usemac=mac; |
148 |
opt.usemasq=masq; |
149 |
opt.bands_max=atoi(argv[2]); |
150 |
|
151 |
opt.proxy_maxconn=atoi(argv[3]); |
152 |
|
153 |
// Read weights: |
154 |
if(wrr_parse_qdisc_weight(argc-4,argv+4,&opt.qdisc_modf)<0) return -1; |
155 |
if(opt.qdisc_modf.weight1.weight_mode==-1) opt.qdisc_modf.weight1.weight_mode=0; |
156 |
if(opt.qdisc_modf.weight2.weight_mode==-1) opt.qdisc_modf.weight2.weight_mode=0; |
157 |
|
158 |
addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt)); |
159 |
} else { |
160 |
struct tc_wrr_qdisc_modf_std opt; |
161 |
char qdisc,class; |
162 |
|
163 |
// This is a modify request: |
164 |
if(argc<1) { |
165 |
fprintf(stderr,"... qdisc ... or ... class ...\n"); |
166 |
return -1; |
167 |
} |
168 |
|
169 |
qdisc=!strcmp(argv[0],"qdisc"); |
170 |
class=!strcmp(argv[0],"class"); |
171 |
|
172 |
if(!qdisc && !class) { |
173 |
fprintf(stderr,"qdisc or class must be specified\n"); |
174 |
return -1; |
175 |
} |
176 |
|
177 |
argc--; |
178 |
argv++; |
179 |
|
180 |
opt.proxy=0; |
181 |
|
182 |
if(qdisc) { |
183 |
opt.change_class=0; |
184 |
if(wrr_parse_qdisc_weight(argc, argv, &opt.qdisc_modf)<0) return -1; |
185 |
} else { |
186 |
int a0,a1,a2,a3,a4=0,a5=0; |
187 |
|
188 |
opt.change_class=1; |
189 |
|
190 |
if(argc<1) { |
191 |
fprintf(stderr,"... <mac>|<ip>|<masq> ...\n"); |
192 |
return -1; |
193 |
} |
194 |
memset(opt.addr,0,sizeof(opt.addr)); |
195 |
|
196 |
if((sscanf(argv[0],"%i.%i.%i.%i",&a0,&a1,&a2,&a3)!=4) && |
197 |
(sscanf(argv[0],"%x:%x:%x:%x:%x:%x",&a0,&a1,&a2,&a3,&a4,&a5)!=6)) { |
198 |
fprintf(stderr,"Wrong format of mac or ip address\n"); |
199 |
return -1; |
200 |
} |
201 |
|
202 |
opt.addr[0]=a0; opt.addr[1]=a1; opt.addr[2]=a2; |
203 |
opt.addr[3]=a3; opt.addr[4]=a4; opt.addr[5]=a5; |
204 |
|
205 |
if(wrr_parse_class_modf(argc-1, argv+1, &opt.class_modf)<0) return -1; |
206 |
} |
207 |
|
208 |
addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt)); |
209 |
} |
210 |
return 0; |
211 |
} |
212 |
|
213 |
static int wrr_parse_copt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n) { |
214 |
struct tc_wrr_class_modf opt; |
215 |
|
216 |
memset(&opt,0,sizeof(opt)); |
217 |
if(wrr_parse_class_modf(argc,argv,&opt)<0) return -1; |
218 |
|
219 |
addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt)); |
220 |
return 0; |
221 |
} |
222 |
|
223 |
static int wrr_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) |
224 |
{ |
225 |
struct tc_wrr_qdisc_stats *qopt; |
226 |
|
227 |
if (opt == NULL) |
228 |
return 0; |
229 |
|
230 |
if (RTA_PAYLOAD(opt) < sizeof(*qopt)) |
231 |
return -1; |
232 |
qopt = RTA_DATA(opt); |
233 |
|
234 |
fprintf(f,"\n (%s/%s) (maxclasses %i) (usedclasses %i) (reused classes %i)\n", |
235 |
qopt->qdisc_crt.srcaddr ? "sour" : "dest", |
236 |
qopt->qdisc_crt.usemac ? "mac" : (qopt->qdisc_crt.usemasq ? "masq" : "ip"), |
237 |
qopt->qdisc_crt.bands_max, |
238 |
qopt->bands_cur, |
239 |
qopt->bands_reused |
240 |
); |
241 |
|
242 |
if(qopt->qdisc_crt.proxy_maxconn) { |
243 |
fprintf(f," (proxy maxcon %i) (proxy curcon %i)\n", |
244 |
qopt->qdisc_crt.proxy_maxconn,qopt->proxy_curconn); |
245 |
} |
246 |
|
247 |
fprintf(f," (waiting classes %i) (packets requeued %i) (priosum: %Lg)\n", |
248 |
qopt->nodes_in_heap, |
249 |
qopt->packets_requed, |
250 |
qopt->priosum/((long double)((__u32)-1)) |
251 |
); |
252 |
|
253 |
fprintf(f," (wmode1 %i) (wmode2 %i) \n", |
254 |
qopt->qdisc_crt.qdisc_modf.weight1.weight_mode, |
255 |
qopt->qdisc_crt.qdisc_modf.weight2.weight_mode); |
256 |
|
257 |
return 0; |
258 |
} |
259 |
|
260 |
static int wrr_print_copt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) { |
261 |
struct tc_wrr_class_stats *copt; |
262 |
long double d=(__u64)-1; |
263 |
|
264 |
if (opt == NULL) return 0; |
265 |
|
266 |
if (RTA_PAYLOAD(opt) < sizeof(*copt)) |
267 |
return -1; |
268 |
copt = RTA_DATA(opt); |
269 |
|
270 |
if(!copt->used) { |
271 |
fprintf(f,"(unused)"); |
272 |
return 0; |
273 |
} |
274 |
|
275 |
if(copt->usemac) { |
276 |
fprintf(f,"\n (address: %.2X:%.2X:%.2X:%.2X:%.2X:%.2X)\n", |
277 |
copt->addr[0],copt->addr[1],copt->addr[2], |
278 |
copt->addr[3],copt->addr[4],copt->addr[5]); |
279 |
} else { |
280 |
fprintf(f,"\n (address: %i.%i.%i.%i)\n",copt->addr[0],copt->addr[1],copt->addr[2],copt->addr[3]); |
281 |
} |
282 |
|
283 |
fprintf(f," (total weight: %Lg) (current position: %i) (counters: %u %u : %u %u)\n", |
284 |
(copt->class_modf.weight1.val/d)*(copt->class_modf.weight2.val/d), |
285 |
copt->heappos, |
286 |
(unsigned)(copt->penal_ms>>32), |
287 |
(unsigned)(copt->penal_ms & 0xffffffffU), |
288 |
(unsigned)(copt->penal_ls>>32), |
289 |
(unsigned)(copt->penal_ls & 0xffffffffU) |
290 |
); |
291 |
|
292 |
fprintf(f," Pars 1: (weight %Lg) (decr: %Lg) (incr: %Lg) (min: %Lg) (max: %Lg)\n", |
293 |
copt->class_modf.weight1.val/d, |
294 |
copt->class_modf.weight1.decr/d, |
295 |
copt->class_modf.weight1.incr/d, |
296 |
copt->class_modf.weight1.min/d, |
297 |
copt->class_modf.weight1.max/d); |
298 |
|
299 |
fprintf(f," Pars 2: (weight %Lg) (decr: %Lg) (incr: %Lg) (min: %Lg) (max: %Lg)", |
300 |
copt->class_modf.weight2.val/d, |
301 |
copt->class_modf.weight2.decr/d, |
302 |
copt->class_modf.weight2.incr/d, |
303 |
copt->class_modf.weight2.min/d, |
304 |
copt->class_modf.weight2.max/d); |
305 |
|
306 |
return 0; |
307 |
} |
308 |
|
309 |
static int wrr_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats) |
310 |
{ |
311 |
return 0; |
312 |
} |
313 |
|
314 |
|
315 |
struct qdisc_util wrr_util = { |
316 |
.id = "wrr", |
317 |
.parse_qopt = wrr_parse_opt, |
318 |
.print_qopt = wrr_print_opt, |
319 |
.print_xstats = wrr_print_xstats, |
320 |
.parse_copt = wrr_parse_copt, |
321 |
.print_copt = wrr_print_copt |
322 |
}; |