1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /* $OpenBSD: pfctl_altq.c,v 1.93 2007/10/15 02:16:35 deraadt Exp $ */ |
---|
4 | |
---|
5 | /* |
---|
6 | * Copyright (c) 2002 |
---|
7 | * Sony Computer Science Laboratories Inc. |
---|
8 | * Copyright (c) 2002, 2003 Henning Brauer <henning@openbsd.org> |
---|
9 | * |
---|
10 | * Permission to use, copy, modify, and distribute this software for any |
---|
11 | * purpose with or without fee is hereby granted, provided that the above |
---|
12 | * copyright notice and this permission notice appear in all copies. |
---|
13 | * |
---|
14 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
---|
15 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
16 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
---|
17 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
18 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
19 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
---|
20 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
21 | */ |
---|
22 | |
---|
23 | #include <sys/cdefs.h> |
---|
24 | __FBSDID("$FreeBSD$"); |
---|
25 | |
---|
26 | #include <rtems/bsd/sys/types.h> |
---|
27 | #include <sys/ioctl.h> |
---|
28 | #include <sys/socket.h> |
---|
29 | |
---|
30 | #include <net/if.h> |
---|
31 | #include <netinet/in.h> |
---|
32 | #include <net/pfvar.h> |
---|
33 | |
---|
34 | #include <err.h> |
---|
35 | #include <errno.h> |
---|
36 | #include <limits.h> |
---|
37 | #include <math.h> |
---|
38 | #include <stdio.h> |
---|
39 | #include <stdlib.h> |
---|
40 | #include <string.h> |
---|
41 | #include <unistd.h> |
---|
42 | |
---|
43 | #include <altq/altq.h> |
---|
44 | #include <altq/altq_cbq.h> |
---|
45 | #include <altq/altq_priq.h> |
---|
46 | #include <altq/altq_hfsc.h> |
---|
47 | |
---|
48 | #include "pfctl_parser.h" |
---|
49 | #include "pfctl.h" |
---|
50 | |
---|
51 | #define is_sc_null(sc) (((sc) == NULL) || ((sc)->m1 == 0 && (sc)->m2 == 0)) |
---|
52 | |
---|
53 | TAILQ_HEAD(altqs, pf_altq) altqs = TAILQ_HEAD_INITIALIZER(altqs); |
---|
54 | LIST_HEAD(gen_sc, segment) rtsc, lssc; |
---|
55 | |
---|
56 | struct pf_altq *qname_to_pfaltq(const char *, const char *); |
---|
57 | u_int32_t qname_to_qid(const char *); |
---|
58 | |
---|
59 | static int eval_pfqueue_cbq(struct pfctl *, struct pf_altq *); |
---|
60 | static int cbq_compute_idletime(struct pfctl *, struct pf_altq *); |
---|
61 | static int check_commit_cbq(int, int, struct pf_altq *); |
---|
62 | static int print_cbq_opts(const struct pf_altq *); |
---|
63 | |
---|
64 | static int eval_pfqueue_priq(struct pfctl *, struct pf_altq *); |
---|
65 | static int check_commit_priq(int, int, struct pf_altq *); |
---|
66 | static int print_priq_opts(const struct pf_altq *); |
---|
67 | |
---|
68 | static int eval_pfqueue_hfsc(struct pfctl *, struct pf_altq *); |
---|
69 | static int check_commit_hfsc(int, int, struct pf_altq *); |
---|
70 | static int print_hfsc_opts(const struct pf_altq *, |
---|
71 | const struct node_queue_opt *); |
---|
72 | |
---|
73 | static void gsc_add_sc(struct gen_sc *, struct service_curve *); |
---|
74 | static int is_gsc_under_sc(struct gen_sc *, |
---|
75 | struct service_curve *); |
---|
76 | static void gsc_destroy(struct gen_sc *); |
---|
77 | static struct segment *gsc_getentry(struct gen_sc *, double); |
---|
78 | static int gsc_add_seg(struct gen_sc *, double, double, double, |
---|
79 | double); |
---|
80 | static double sc_x2y(struct service_curve *, double); |
---|
81 | |
---|
82 | #ifdef __FreeBSD__ |
---|
83 | u_int32_t getifspeed(int, char *); |
---|
84 | #else |
---|
85 | u_int32_t getifspeed(char *); |
---|
86 | #endif |
---|
87 | u_long getifmtu(char *); |
---|
88 | int eval_queue_opts(struct pf_altq *, struct node_queue_opt *, |
---|
89 | u_int32_t); |
---|
90 | u_int32_t eval_bwspec(struct node_queue_bw *, u_int32_t); |
---|
91 | void print_hfsc_sc(const char *, u_int, u_int, u_int, |
---|
92 | const struct node_hfsc_sc *); |
---|
93 | |
---|
94 | void |
---|
95 | pfaltq_store(struct pf_altq *a) |
---|
96 | { |
---|
97 | struct pf_altq *altq; |
---|
98 | |
---|
99 | if ((altq = malloc(sizeof(*altq))) == NULL) |
---|
100 | err(1, "malloc"); |
---|
101 | memcpy(altq, a, sizeof(struct pf_altq)); |
---|
102 | TAILQ_INSERT_TAIL(&altqs, altq, entries); |
---|
103 | } |
---|
104 | |
---|
105 | struct pf_altq * |
---|
106 | pfaltq_lookup(const char *ifname) |
---|
107 | { |
---|
108 | struct pf_altq *altq; |
---|
109 | |
---|
110 | TAILQ_FOREACH(altq, &altqs, entries) { |
---|
111 | if (strncmp(ifname, altq->ifname, IFNAMSIZ) == 0 && |
---|
112 | altq->qname[0] == 0) |
---|
113 | return (altq); |
---|
114 | } |
---|
115 | return (NULL); |
---|
116 | } |
---|
117 | |
---|
118 | struct pf_altq * |
---|
119 | qname_to_pfaltq(const char *qname, const char *ifname) |
---|
120 | { |
---|
121 | struct pf_altq *altq; |
---|
122 | |
---|
123 | TAILQ_FOREACH(altq, &altqs, entries) { |
---|
124 | if (strncmp(ifname, altq->ifname, IFNAMSIZ) == 0 && |
---|
125 | strncmp(qname, altq->qname, PF_QNAME_SIZE) == 0) |
---|
126 | return (altq); |
---|
127 | } |
---|
128 | return (NULL); |
---|
129 | } |
---|
130 | |
---|
131 | u_int32_t |
---|
132 | qname_to_qid(const char *qname) |
---|
133 | { |
---|
134 | struct pf_altq *altq; |
---|
135 | |
---|
136 | /* |
---|
137 | * We guarantee that same named queues on different interfaces |
---|
138 | * have the same qid, so we do NOT need to limit matching on |
---|
139 | * one interface! |
---|
140 | */ |
---|
141 | |
---|
142 | TAILQ_FOREACH(altq, &altqs, entries) { |
---|
143 | if (strncmp(qname, altq->qname, PF_QNAME_SIZE) == 0) |
---|
144 | return (altq->qid); |
---|
145 | } |
---|
146 | return (0); |
---|
147 | } |
---|
148 | |
---|
149 | void |
---|
150 | print_altq(const struct pf_altq *a, unsigned int level, |
---|
151 | struct node_queue_bw *bw, struct node_queue_opt *qopts) |
---|
152 | { |
---|
153 | if (a->qname[0] != 0) { |
---|
154 | print_queue(a, level, bw, 1, qopts); |
---|
155 | return; |
---|
156 | } |
---|
157 | |
---|
158 | #ifdef __FreeBSD__ |
---|
159 | if (a->local_flags & PFALTQ_FLAG_IF_REMOVED) |
---|
160 | printf("INACTIVE "); |
---|
161 | #endif |
---|
162 | |
---|
163 | printf("altq on %s ", a->ifname); |
---|
164 | |
---|
165 | switch (a->scheduler) { |
---|
166 | case ALTQT_CBQ: |
---|
167 | if (!print_cbq_opts(a)) |
---|
168 | printf("cbq "); |
---|
169 | break; |
---|
170 | case ALTQT_PRIQ: |
---|
171 | if (!print_priq_opts(a)) |
---|
172 | printf("priq "); |
---|
173 | break; |
---|
174 | case ALTQT_HFSC: |
---|
175 | if (!print_hfsc_opts(a, qopts)) |
---|
176 | printf("hfsc "); |
---|
177 | break; |
---|
178 | } |
---|
179 | |
---|
180 | if (bw != NULL && bw->bw_percent > 0) { |
---|
181 | if (bw->bw_percent < 100) |
---|
182 | printf("bandwidth %u%% ", bw->bw_percent); |
---|
183 | } else |
---|
184 | printf("bandwidth %s ", rate2str((double)a->ifbandwidth)); |
---|
185 | |
---|
186 | if (a->qlimit != DEFAULT_QLIMIT) |
---|
187 | printf("qlimit %u ", a->qlimit); |
---|
188 | printf("tbrsize %u ", a->tbrsize); |
---|
189 | } |
---|
190 | |
---|
191 | void |
---|
192 | print_queue(const struct pf_altq *a, unsigned int level, |
---|
193 | struct node_queue_bw *bw, int print_interface, |
---|
194 | struct node_queue_opt *qopts) |
---|
195 | { |
---|
196 | unsigned int i; |
---|
197 | |
---|
198 | #ifdef __FreeBSD__ |
---|
199 | if (a->local_flags & PFALTQ_FLAG_IF_REMOVED) |
---|
200 | printf("INACTIVE "); |
---|
201 | #endif |
---|
202 | printf("queue "); |
---|
203 | for (i = 0; i < level; ++i) |
---|
204 | printf(" "); |
---|
205 | printf("%s ", a->qname); |
---|
206 | if (print_interface) |
---|
207 | printf("on %s ", a->ifname); |
---|
208 | if (a->scheduler == ALTQT_CBQ || a->scheduler == ALTQT_HFSC) { |
---|
209 | if (bw != NULL && bw->bw_percent > 0) { |
---|
210 | if (bw->bw_percent < 100) |
---|
211 | printf("bandwidth %u%% ", bw->bw_percent); |
---|
212 | } else |
---|
213 | printf("bandwidth %s ", rate2str((double)a->bandwidth)); |
---|
214 | } |
---|
215 | if (a->priority != DEFAULT_PRIORITY) |
---|
216 | printf("priority %u ", a->priority); |
---|
217 | if (a->qlimit != DEFAULT_QLIMIT) |
---|
218 | printf("qlimit %u ", a->qlimit); |
---|
219 | switch (a->scheduler) { |
---|
220 | case ALTQT_CBQ: |
---|
221 | print_cbq_opts(a); |
---|
222 | break; |
---|
223 | case ALTQT_PRIQ: |
---|
224 | print_priq_opts(a); |
---|
225 | break; |
---|
226 | case ALTQT_HFSC: |
---|
227 | print_hfsc_opts(a, qopts); |
---|
228 | break; |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | /* |
---|
233 | * eval_pfaltq computes the discipline parameters. |
---|
234 | */ |
---|
235 | int |
---|
236 | eval_pfaltq(struct pfctl *pf, struct pf_altq *pa, struct node_queue_bw *bw, |
---|
237 | struct node_queue_opt *opts) |
---|
238 | { |
---|
239 | u_int rate, size, errors = 0; |
---|
240 | |
---|
241 | if (bw->bw_absolute > 0) |
---|
242 | pa->ifbandwidth = bw->bw_absolute; |
---|
243 | else |
---|
244 | #ifdef __FreeBSD__ |
---|
245 | if ((rate = getifspeed(pf->dev, pa->ifname)) == 0) { |
---|
246 | #else |
---|
247 | if ((rate = getifspeed(pa->ifname)) == 0) { |
---|
248 | #endif |
---|
249 | fprintf(stderr, "interface %s does not know its bandwidth, " |
---|
250 | "please specify an absolute bandwidth\n", |
---|
251 | pa->ifname); |
---|
252 | errors++; |
---|
253 | } else if ((pa->ifbandwidth = eval_bwspec(bw, rate)) == 0) |
---|
254 | pa->ifbandwidth = rate; |
---|
255 | |
---|
256 | errors += eval_queue_opts(pa, opts, pa->ifbandwidth); |
---|
257 | |
---|
258 | /* if tbrsize is not specified, use heuristics */ |
---|
259 | if (pa->tbrsize == 0) { |
---|
260 | rate = pa->ifbandwidth; |
---|
261 | if (rate <= 1 * 1000 * 1000) |
---|
262 | size = 1; |
---|
263 | else if (rate <= 10 * 1000 * 1000) |
---|
264 | size = 4; |
---|
265 | else if (rate <= 200 * 1000 * 1000) |
---|
266 | size = 8; |
---|
267 | else |
---|
268 | size = 24; |
---|
269 | size = size * getifmtu(pa->ifname); |
---|
270 | if (size > 0xffff) |
---|
271 | size = 0xffff; |
---|
272 | pa->tbrsize = size; |
---|
273 | } |
---|
274 | return (errors); |
---|
275 | } |
---|
276 | |
---|
277 | /* |
---|
278 | * check_commit_altq does consistency check for each interface |
---|
279 | */ |
---|
280 | int |
---|
281 | check_commit_altq(int dev, int opts) |
---|
282 | { |
---|
283 | struct pf_altq *altq; |
---|
284 | int error = 0; |
---|
285 | |
---|
286 | /* call the discipline check for each interface. */ |
---|
287 | TAILQ_FOREACH(altq, &altqs, entries) { |
---|
288 | if (altq->qname[0] == 0) { |
---|
289 | switch (altq->scheduler) { |
---|
290 | case ALTQT_CBQ: |
---|
291 | error = check_commit_cbq(dev, opts, altq); |
---|
292 | break; |
---|
293 | case ALTQT_PRIQ: |
---|
294 | error = check_commit_priq(dev, opts, altq); |
---|
295 | break; |
---|
296 | case ALTQT_HFSC: |
---|
297 | error = check_commit_hfsc(dev, opts, altq); |
---|
298 | break; |
---|
299 | default: |
---|
300 | break; |
---|
301 | } |
---|
302 | } |
---|
303 | } |
---|
304 | return (error); |
---|
305 | } |
---|
306 | |
---|
307 | /* |
---|
308 | * eval_pfqueue computes the queue parameters. |
---|
309 | */ |
---|
310 | int |
---|
311 | eval_pfqueue(struct pfctl *pf, struct pf_altq *pa, struct node_queue_bw *bw, |
---|
312 | struct node_queue_opt *opts) |
---|
313 | { |
---|
314 | /* should be merged with expand_queue */ |
---|
315 | struct pf_altq *if_pa, *parent, *altq; |
---|
316 | u_int32_t bwsum; |
---|
317 | int error = 0; |
---|
318 | |
---|
319 | /* find the corresponding interface and copy fields used by queues */ |
---|
320 | if ((if_pa = pfaltq_lookup(pa->ifname)) == NULL) { |
---|
321 | fprintf(stderr, "altq not defined on %s\n", pa->ifname); |
---|
322 | return (1); |
---|
323 | } |
---|
324 | pa->scheduler = if_pa->scheduler; |
---|
325 | pa->ifbandwidth = if_pa->ifbandwidth; |
---|
326 | |
---|
327 | if (qname_to_pfaltq(pa->qname, pa->ifname) != NULL) { |
---|
328 | fprintf(stderr, "queue %s already exists on interface %s\n", |
---|
329 | pa->qname, pa->ifname); |
---|
330 | return (1); |
---|
331 | } |
---|
332 | pa->qid = qname_to_qid(pa->qname); |
---|
333 | |
---|
334 | parent = NULL; |
---|
335 | if (pa->parent[0] != 0) { |
---|
336 | parent = qname_to_pfaltq(pa->parent, pa->ifname); |
---|
337 | if (parent == NULL) { |
---|
338 | fprintf(stderr, "parent %s not found for %s\n", |
---|
339 | pa->parent, pa->qname); |
---|
340 | return (1); |
---|
341 | } |
---|
342 | pa->parent_qid = parent->qid; |
---|
343 | } |
---|
344 | if (pa->qlimit == 0) |
---|
345 | pa->qlimit = DEFAULT_QLIMIT; |
---|
346 | |
---|
347 | if (pa->scheduler == ALTQT_CBQ || pa->scheduler == ALTQT_HFSC) { |
---|
348 | pa->bandwidth = eval_bwspec(bw, |
---|
349 | parent == NULL ? 0 : parent->bandwidth); |
---|
350 | |
---|
351 | if (pa->bandwidth > pa->ifbandwidth) { |
---|
352 | fprintf(stderr, "bandwidth for %s higher than " |
---|
353 | "interface\n", pa->qname); |
---|
354 | return (1); |
---|
355 | } |
---|
356 | /* check the sum of the child bandwidth is under parent's */ |
---|
357 | if (parent != NULL) { |
---|
358 | if (pa->bandwidth > parent->bandwidth) { |
---|
359 | warnx("bandwidth for %s higher than parent", |
---|
360 | pa->qname); |
---|
361 | return (1); |
---|
362 | } |
---|
363 | bwsum = 0; |
---|
364 | TAILQ_FOREACH(altq, &altqs, entries) { |
---|
365 | if (strncmp(altq->ifname, pa->ifname, |
---|
366 | IFNAMSIZ) == 0 && |
---|
367 | altq->qname[0] != 0 && |
---|
368 | strncmp(altq->parent, pa->parent, |
---|
369 | PF_QNAME_SIZE) == 0) |
---|
370 | bwsum += altq->bandwidth; |
---|
371 | } |
---|
372 | bwsum += pa->bandwidth; |
---|
373 | if (bwsum > parent->bandwidth) { |
---|
374 | warnx("the sum of the child bandwidth higher" |
---|
375 | " than parent \"%s\"", parent->qname); |
---|
376 | } |
---|
377 | } |
---|
378 | } |
---|
379 | |
---|
380 | if (eval_queue_opts(pa, opts, parent == NULL? 0 : parent->bandwidth)) |
---|
381 | return (1); |
---|
382 | |
---|
383 | switch (pa->scheduler) { |
---|
384 | case ALTQT_CBQ: |
---|
385 | error = eval_pfqueue_cbq(pf, pa); |
---|
386 | break; |
---|
387 | case ALTQT_PRIQ: |
---|
388 | error = eval_pfqueue_priq(pf, pa); |
---|
389 | break; |
---|
390 | case ALTQT_HFSC: |
---|
391 | error = eval_pfqueue_hfsc(pf, pa); |
---|
392 | break; |
---|
393 | default: |
---|
394 | break; |
---|
395 | } |
---|
396 | return (error); |
---|
397 | } |
---|
398 | |
---|
399 | /* |
---|
400 | * CBQ support functions |
---|
401 | */ |
---|
402 | #define RM_FILTER_GAIN 5 /* log2 of gain, e.g., 5 => 31/32 */ |
---|
403 | #define RM_NS_PER_SEC (1000000000) |
---|
404 | |
---|
405 | static int |
---|
406 | eval_pfqueue_cbq(struct pfctl *pf, struct pf_altq *pa) |
---|
407 | { |
---|
408 | struct cbq_opts *opts; |
---|
409 | u_int ifmtu; |
---|
410 | |
---|
411 | if (pa->priority >= CBQ_MAXPRI) { |
---|
412 | warnx("priority out of range: max %d", CBQ_MAXPRI - 1); |
---|
413 | return (-1); |
---|
414 | } |
---|
415 | |
---|
416 | ifmtu = getifmtu(pa->ifname); |
---|
417 | opts = &pa->pq_u.cbq_opts; |
---|
418 | |
---|
419 | if (opts->pktsize == 0) { /* use default */ |
---|
420 | opts->pktsize = ifmtu; |
---|
421 | if (opts->pktsize > MCLBYTES) /* do what TCP does */ |
---|
422 | opts->pktsize &= ~MCLBYTES; |
---|
423 | } else if (opts->pktsize > ifmtu) |
---|
424 | opts->pktsize = ifmtu; |
---|
425 | if (opts->maxpktsize == 0) /* use default */ |
---|
426 | opts->maxpktsize = ifmtu; |
---|
427 | else if (opts->maxpktsize > ifmtu) |
---|
428 | opts->pktsize = ifmtu; |
---|
429 | |
---|
430 | if (opts->pktsize > opts->maxpktsize) |
---|
431 | opts->pktsize = opts->maxpktsize; |
---|
432 | |
---|
433 | if (pa->parent[0] == 0) |
---|
434 | opts->flags |= (CBQCLF_ROOTCLASS | CBQCLF_WRR); |
---|
435 | |
---|
436 | cbq_compute_idletime(pf, pa); |
---|
437 | return (0); |
---|
438 | } |
---|
439 | |
---|
440 | /* |
---|
441 | * compute ns_per_byte, maxidle, minidle, and offtime |
---|
442 | */ |
---|
443 | static int |
---|
444 | cbq_compute_idletime(struct pfctl *pf, struct pf_altq *pa) |
---|
445 | { |
---|
446 | struct cbq_opts *opts; |
---|
447 | double maxidle_s, maxidle, minidle; |
---|
448 | double offtime, nsPerByte, ifnsPerByte, ptime, cptime; |
---|
449 | double z, g, f, gton, gtom; |
---|
450 | u_int minburst, maxburst; |
---|
451 | |
---|
452 | opts = &pa->pq_u.cbq_opts; |
---|
453 | ifnsPerByte = (1.0 / (double)pa->ifbandwidth) * RM_NS_PER_SEC * 8; |
---|
454 | minburst = opts->minburst; |
---|
455 | maxburst = opts->maxburst; |
---|
456 | |
---|
457 | if (pa->bandwidth == 0) |
---|
458 | f = 0.0001; /* small enough? */ |
---|
459 | else |
---|
460 | f = ((double) pa->bandwidth / (double) pa->ifbandwidth); |
---|
461 | |
---|
462 | nsPerByte = ifnsPerByte / f; |
---|
463 | ptime = (double)opts->pktsize * ifnsPerByte; |
---|
464 | cptime = ptime * (1.0 - f) / f; |
---|
465 | |
---|
466 | if (nsPerByte * (double)opts->maxpktsize > (double)INT_MAX) { |
---|
467 | /* |
---|
468 | * this causes integer overflow in kernel! |
---|
469 | * (bandwidth < 6Kbps when max_pkt_size=1500) |
---|
470 | */ |
---|
471 | if (pa->bandwidth != 0 && (pf->opts & PF_OPT_QUIET) == 0) |
---|
472 | warnx("queue bandwidth must be larger than %s", |
---|
473 | rate2str(ifnsPerByte * (double)opts->maxpktsize / |
---|
474 | (double)INT_MAX * (double)pa->ifbandwidth)); |
---|
475 | fprintf(stderr, "cbq: queue %s is too slow!\n", |
---|
476 | pa->qname); |
---|
477 | nsPerByte = (double)(INT_MAX / opts->maxpktsize); |
---|
478 | } |
---|
479 | |
---|
480 | if (maxburst == 0) { /* use default */ |
---|
481 | if (cptime > 10.0 * 1000000) |
---|
482 | maxburst = 4; |
---|
483 | else |
---|
484 | maxburst = 16; |
---|
485 | } |
---|
486 | if (minburst == 0) /* use default */ |
---|
487 | minburst = 2; |
---|
488 | if (minburst > maxburst) |
---|
489 | minburst = maxburst; |
---|
490 | |
---|
491 | z = (double)(1 << RM_FILTER_GAIN); |
---|
492 | g = (1.0 - 1.0 / z); |
---|
493 | gton = pow(g, (double)maxburst); |
---|
494 | gtom = pow(g, (double)(minburst-1)); |
---|
495 | maxidle = ((1.0 / f - 1.0) * ((1.0 - gton) / gton)); |
---|
496 | maxidle_s = (1.0 - g); |
---|
497 | if (maxidle > maxidle_s) |
---|
498 | maxidle = ptime * maxidle; |
---|
499 | else |
---|
500 | maxidle = ptime * maxidle_s; |
---|
501 | offtime = cptime * (1.0 + 1.0/(1.0 - g) * (1.0 - gtom) / gtom); |
---|
502 | minidle = -((double)opts->maxpktsize * (double)nsPerByte); |
---|
503 | |
---|
504 | /* scale parameters */ |
---|
505 | maxidle = ((maxidle * 8.0) / nsPerByte) * |
---|
506 | pow(2.0, (double)RM_FILTER_GAIN); |
---|
507 | offtime = (offtime * 8.0) / nsPerByte * |
---|
508 | pow(2.0, (double)RM_FILTER_GAIN); |
---|
509 | minidle = ((minidle * 8.0) / nsPerByte) * |
---|
510 | pow(2.0, (double)RM_FILTER_GAIN); |
---|
511 | |
---|
512 | maxidle = maxidle / 1000.0; |
---|
513 | offtime = offtime / 1000.0; |
---|
514 | minidle = minidle / 1000.0; |
---|
515 | |
---|
516 | opts->minburst = minburst; |
---|
517 | opts->maxburst = maxburst; |
---|
518 | opts->ns_per_byte = (u_int)nsPerByte; |
---|
519 | opts->maxidle = (u_int)fabs(maxidle); |
---|
520 | opts->minidle = (int)minidle; |
---|
521 | opts->offtime = (u_int)fabs(offtime); |
---|
522 | |
---|
523 | return (0); |
---|
524 | } |
---|
525 | |
---|
526 | static int |
---|
527 | check_commit_cbq(int dev, int opts, struct pf_altq *pa) |
---|
528 | { |
---|
529 | struct pf_altq *altq; |
---|
530 | int root_class, default_class; |
---|
531 | int error = 0; |
---|
532 | |
---|
533 | /* |
---|
534 | * check if cbq has one root queue and one default queue |
---|
535 | * for this interface |
---|
536 | */ |
---|
537 | root_class = default_class = 0; |
---|
538 | TAILQ_FOREACH(altq, &altqs, entries) { |
---|
539 | if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0) |
---|
540 | continue; |
---|
541 | if (altq->qname[0] == 0) /* this is for interface */ |
---|
542 | continue; |
---|
543 | if (altq->pq_u.cbq_opts.flags & CBQCLF_ROOTCLASS) |
---|
544 | root_class++; |
---|
545 | if (altq->pq_u.cbq_opts.flags & CBQCLF_DEFCLASS) |
---|
546 | default_class++; |
---|
547 | } |
---|
548 | if (root_class != 1) { |
---|
549 | warnx("should have one root queue on %s", pa->ifname); |
---|
550 | error++; |
---|
551 | } |
---|
552 | if (default_class != 1) { |
---|
553 | warnx("should have one default queue on %s", pa->ifname); |
---|
554 | error++; |
---|
555 | } |
---|
556 | return (error); |
---|
557 | } |
---|
558 | |
---|
559 | static int |
---|
560 | print_cbq_opts(const struct pf_altq *a) |
---|
561 | { |
---|
562 | const struct cbq_opts *opts; |
---|
563 | |
---|
564 | opts = &a->pq_u.cbq_opts; |
---|
565 | if (opts->flags) { |
---|
566 | printf("cbq("); |
---|
567 | if (opts->flags & CBQCLF_RED) |
---|
568 | printf(" red"); |
---|
569 | if (opts->flags & CBQCLF_ECN) |
---|
570 | printf(" ecn"); |
---|
571 | if (opts->flags & CBQCLF_RIO) |
---|
572 | printf(" rio"); |
---|
573 | if (opts->flags & CBQCLF_CLEARDSCP) |
---|
574 | printf(" cleardscp"); |
---|
575 | if (opts->flags & CBQCLF_FLOWVALVE) |
---|
576 | printf(" flowvalve"); |
---|
577 | if (opts->flags & CBQCLF_BORROW) |
---|
578 | printf(" borrow"); |
---|
579 | if (opts->flags & CBQCLF_WRR) |
---|
580 | printf(" wrr"); |
---|
581 | if (opts->flags & CBQCLF_EFFICIENT) |
---|
582 | printf(" efficient"); |
---|
583 | if (opts->flags & CBQCLF_ROOTCLASS) |
---|
584 | printf(" root"); |
---|
585 | if (opts->flags & CBQCLF_DEFCLASS) |
---|
586 | printf(" default"); |
---|
587 | printf(" ) "); |
---|
588 | |
---|
589 | return (1); |
---|
590 | } else |
---|
591 | return (0); |
---|
592 | } |
---|
593 | |
---|
594 | /* |
---|
595 | * PRIQ support functions |
---|
596 | */ |
---|
597 | static int |
---|
598 | eval_pfqueue_priq(struct pfctl *pf, struct pf_altq *pa) |
---|
599 | { |
---|
600 | struct pf_altq *altq; |
---|
601 | |
---|
602 | if (pa->priority >= PRIQ_MAXPRI) { |
---|
603 | warnx("priority out of range: max %d", PRIQ_MAXPRI - 1); |
---|
604 | return (-1); |
---|
605 | } |
---|
606 | /* the priority should be unique for the interface */ |
---|
607 | TAILQ_FOREACH(altq, &altqs, entries) { |
---|
608 | if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) == 0 && |
---|
609 | altq->qname[0] != 0 && altq->priority == pa->priority) { |
---|
610 | warnx("%s and %s have the same priority", |
---|
611 | altq->qname, pa->qname); |
---|
612 | return (-1); |
---|
613 | } |
---|
614 | } |
---|
615 | |
---|
616 | return (0); |
---|
617 | } |
---|
618 | |
---|
619 | static int |
---|
620 | check_commit_priq(int dev, int opts, struct pf_altq *pa) |
---|
621 | { |
---|
622 | struct pf_altq *altq; |
---|
623 | int default_class; |
---|
624 | int error = 0; |
---|
625 | |
---|
626 | /* |
---|
627 | * check if priq has one default class for this interface |
---|
628 | */ |
---|
629 | default_class = 0; |
---|
630 | TAILQ_FOREACH(altq, &altqs, entries) { |
---|
631 | if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0) |
---|
632 | continue; |
---|
633 | if (altq->qname[0] == 0) /* this is for interface */ |
---|
634 | continue; |
---|
635 | if (altq->pq_u.priq_opts.flags & PRCF_DEFAULTCLASS) |
---|
636 | default_class++; |
---|
637 | } |
---|
638 | if (default_class != 1) { |
---|
639 | warnx("should have one default queue on %s", pa->ifname); |
---|
640 | error++; |
---|
641 | } |
---|
642 | return (error); |
---|
643 | } |
---|
644 | |
---|
645 | static int |
---|
646 | print_priq_opts(const struct pf_altq *a) |
---|
647 | { |
---|
648 | const struct priq_opts *opts; |
---|
649 | |
---|
650 | opts = &a->pq_u.priq_opts; |
---|
651 | |
---|
652 | if (opts->flags) { |
---|
653 | printf("priq("); |
---|
654 | if (opts->flags & PRCF_RED) |
---|
655 | printf(" red"); |
---|
656 | if (opts->flags & PRCF_ECN) |
---|
657 | printf(" ecn"); |
---|
658 | if (opts->flags & PRCF_RIO) |
---|
659 | printf(" rio"); |
---|
660 | if (opts->flags & PRCF_CLEARDSCP) |
---|
661 | printf(" cleardscp"); |
---|
662 | if (opts->flags & PRCF_DEFAULTCLASS) |
---|
663 | printf(" default"); |
---|
664 | printf(" ) "); |
---|
665 | |
---|
666 | return (1); |
---|
667 | } else |
---|
668 | return (0); |
---|
669 | } |
---|
670 | |
---|
671 | /* |
---|
672 | * HFSC support functions |
---|
673 | */ |
---|
674 | static int |
---|
675 | eval_pfqueue_hfsc(struct pfctl *pf, struct pf_altq *pa) |
---|
676 | { |
---|
677 | struct pf_altq *altq, *parent; |
---|
678 | struct hfsc_opts *opts; |
---|
679 | struct service_curve sc; |
---|
680 | |
---|
681 | opts = &pa->pq_u.hfsc_opts; |
---|
682 | |
---|
683 | if (pa->parent[0] == 0) { |
---|
684 | /* root queue */ |
---|
685 | opts->lssc_m1 = pa->ifbandwidth; |
---|
686 | opts->lssc_m2 = pa->ifbandwidth; |
---|
687 | opts->lssc_d = 0; |
---|
688 | return (0); |
---|
689 | } |
---|
690 | |
---|
691 | LIST_INIT(&rtsc); |
---|
692 | LIST_INIT(&lssc); |
---|
693 | |
---|
694 | /* if link_share is not specified, use bandwidth */ |
---|
695 | if (opts->lssc_m2 == 0) |
---|
696 | opts->lssc_m2 = pa->bandwidth; |
---|
697 | |
---|
698 | if ((opts->rtsc_m1 > 0 && opts->rtsc_m2 == 0) || |
---|
699 | (opts->lssc_m1 > 0 && opts->lssc_m2 == 0) || |
---|
700 | (opts->ulsc_m1 > 0 && opts->ulsc_m2 == 0)) { |
---|
701 | warnx("m2 is zero for %s", pa->qname); |
---|
702 | return (-1); |
---|
703 | } |
---|
704 | |
---|
705 | if ((opts->rtsc_m1 < opts->rtsc_m2 && opts->rtsc_m1 != 0) || |
---|
706 | (opts->lssc_m1 < opts->lssc_m2 && opts->lssc_m1 != 0) || |
---|
707 | (opts->ulsc_m1 < opts->ulsc_m2 && opts->ulsc_m1 != 0)) { |
---|
708 | warnx("m1 must be zero for convex curve: %s", pa->qname); |
---|
709 | return (-1); |
---|
710 | } |
---|
711 | |
---|
712 | /* |
---|
713 | * admission control: |
---|
714 | * for the real-time service curve, the sum of the service curves |
---|
715 | * should not exceed 80% of the interface bandwidth. 20% is reserved |
---|
716 | * not to over-commit the actual interface bandwidth. |
---|
717 | * for the linkshare service curve, the sum of the child service |
---|
718 | * curve should not exceed the parent service curve. |
---|
719 | * for the upper-limit service curve, the assigned bandwidth should |
---|
720 | * be smaller than the interface bandwidth, and the upper-limit should |
---|
721 | * be larger than the real-time service curve when both are defined. |
---|
722 | */ |
---|
723 | parent = qname_to_pfaltq(pa->parent, pa->ifname); |
---|
724 | if (parent == NULL) |
---|
725 | errx(1, "parent %s not found for %s", pa->parent, pa->qname); |
---|
726 | |
---|
727 | TAILQ_FOREACH(altq, &altqs, entries) { |
---|
728 | if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0) |
---|
729 | continue; |
---|
730 | if (altq->qname[0] == 0) /* this is for interface */ |
---|
731 | continue; |
---|
732 | |
---|
733 | /* if the class has a real-time service curve, add it. */ |
---|
734 | if (opts->rtsc_m2 != 0 && altq->pq_u.hfsc_opts.rtsc_m2 != 0) { |
---|
735 | sc.m1 = altq->pq_u.hfsc_opts.rtsc_m1; |
---|
736 | sc.d = altq->pq_u.hfsc_opts.rtsc_d; |
---|
737 | sc.m2 = altq->pq_u.hfsc_opts.rtsc_m2; |
---|
738 | gsc_add_sc(&rtsc, &sc); |
---|
739 | } |
---|
740 | |
---|
741 | if (strncmp(altq->parent, pa->parent, PF_QNAME_SIZE) != 0) |
---|
742 | continue; |
---|
743 | |
---|
744 | /* if the class has a linkshare service curve, add it. */ |
---|
745 | if (opts->lssc_m2 != 0 && altq->pq_u.hfsc_opts.lssc_m2 != 0) { |
---|
746 | sc.m1 = altq->pq_u.hfsc_opts.lssc_m1; |
---|
747 | sc.d = altq->pq_u.hfsc_opts.lssc_d; |
---|
748 | sc.m2 = altq->pq_u.hfsc_opts.lssc_m2; |
---|
749 | gsc_add_sc(&lssc, &sc); |
---|
750 | } |
---|
751 | } |
---|
752 | |
---|
753 | /* check the real-time service curve. reserve 20% of interface bw */ |
---|
754 | if (opts->rtsc_m2 != 0) { |
---|
755 | /* add this queue to the sum */ |
---|
756 | sc.m1 = opts->rtsc_m1; |
---|
757 | sc.d = opts->rtsc_d; |
---|
758 | sc.m2 = opts->rtsc_m2; |
---|
759 | gsc_add_sc(&rtsc, &sc); |
---|
760 | /* compare the sum with 80% of the interface */ |
---|
761 | sc.m1 = 0; |
---|
762 | sc.d = 0; |
---|
763 | sc.m2 = pa->ifbandwidth / 100 * 80; |
---|
764 | if (!is_gsc_under_sc(&rtsc, &sc)) { |
---|
765 | warnx("real-time sc exceeds 80%% of the interface " |
---|
766 | "bandwidth (%s)", rate2str((double)sc.m2)); |
---|
767 | goto err_ret; |
---|
768 | } |
---|
769 | } |
---|
770 | |
---|
771 | /* check the linkshare service curve. */ |
---|
772 | if (opts->lssc_m2 != 0) { |
---|
773 | /* add this queue to the child sum */ |
---|
774 | sc.m1 = opts->lssc_m1; |
---|
775 | sc.d = opts->lssc_d; |
---|
776 | sc.m2 = opts->lssc_m2; |
---|
777 | gsc_add_sc(&lssc, &sc); |
---|
778 | /* compare the sum of the children with parent's sc */ |
---|
779 | sc.m1 = parent->pq_u.hfsc_opts.lssc_m1; |
---|
780 | sc.d = parent->pq_u.hfsc_opts.lssc_d; |
---|
781 | sc.m2 = parent->pq_u.hfsc_opts.lssc_m2; |
---|
782 | if (!is_gsc_under_sc(&lssc, &sc)) { |
---|
783 | warnx("linkshare sc exceeds parent's sc"); |
---|
784 | goto err_ret; |
---|
785 | } |
---|
786 | } |
---|
787 | |
---|
788 | /* check the upper-limit service curve. */ |
---|
789 | if (opts->ulsc_m2 != 0) { |
---|
790 | if (opts->ulsc_m1 > pa->ifbandwidth || |
---|
791 | opts->ulsc_m2 > pa->ifbandwidth) { |
---|
792 | warnx("upper-limit larger than interface bandwidth"); |
---|
793 | goto err_ret; |
---|
794 | } |
---|
795 | if (opts->rtsc_m2 != 0 && opts->rtsc_m2 > opts->ulsc_m2) { |
---|
796 | warnx("upper-limit sc smaller than real-time sc"); |
---|
797 | goto err_ret; |
---|
798 | } |
---|
799 | } |
---|
800 | |
---|
801 | gsc_destroy(&rtsc); |
---|
802 | gsc_destroy(&lssc); |
---|
803 | |
---|
804 | return (0); |
---|
805 | |
---|
806 | err_ret: |
---|
807 | gsc_destroy(&rtsc); |
---|
808 | gsc_destroy(&lssc); |
---|
809 | return (-1); |
---|
810 | } |
---|
811 | |
---|
812 | static int |
---|
813 | check_commit_hfsc(int dev, int opts, struct pf_altq *pa) |
---|
814 | { |
---|
815 | struct pf_altq *altq, *def = NULL; |
---|
816 | int default_class; |
---|
817 | int error = 0; |
---|
818 | |
---|
819 | /* check if hfsc has one default queue for this interface */ |
---|
820 | default_class = 0; |
---|
821 | TAILQ_FOREACH(altq, &altqs, entries) { |
---|
822 | if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0) |
---|
823 | continue; |
---|
824 | if (altq->qname[0] == 0) /* this is for interface */ |
---|
825 | continue; |
---|
826 | if (altq->parent[0] == 0) /* dummy root */ |
---|
827 | continue; |
---|
828 | if (altq->pq_u.hfsc_opts.flags & HFCF_DEFAULTCLASS) { |
---|
829 | default_class++; |
---|
830 | def = altq; |
---|
831 | } |
---|
832 | } |
---|
833 | if (default_class != 1) { |
---|
834 | warnx("should have one default queue on %s", pa->ifname); |
---|
835 | return (1); |
---|
836 | } |
---|
837 | /* make sure the default queue is a leaf */ |
---|
838 | TAILQ_FOREACH(altq, &altqs, entries) { |
---|
839 | if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0) |
---|
840 | continue; |
---|
841 | if (altq->qname[0] == 0) /* this is for interface */ |
---|
842 | continue; |
---|
843 | if (strncmp(altq->parent, def->qname, PF_QNAME_SIZE) == 0) { |
---|
844 | warnx("default queue is not a leaf"); |
---|
845 | error++; |
---|
846 | } |
---|
847 | } |
---|
848 | return (error); |
---|
849 | } |
---|
850 | |
---|
851 | static int |
---|
852 | print_hfsc_opts(const struct pf_altq *a, const struct node_queue_opt *qopts) |
---|
853 | { |
---|
854 | const struct hfsc_opts *opts; |
---|
855 | const struct node_hfsc_sc *rtsc, *lssc, *ulsc; |
---|
856 | |
---|
857 | opts = &a->pq_u.hfsc_opts; |
---|
858 | if (qopts == NULL) |
---|
859 | rtsc = lssc = ulsc = NULL; |
---|
860 | else { |
---|
861 | rtsc = &qopts->data.hfsc_opts.realtime; |
---|
862 | lssc = &qopts->data.hfsc_opts.linkshare; |
---|
863 | ulsc = &qopts->data.hfsc_opts.upperlimit; |
---|
864 | } |
---|
865 | |
---|
866 | if (opts->flags || opts->rtsc_m2 != 0 || opts->ulsc_m2 != 0 || |
---|
867 | (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth || |
---|
868 | opts->lssc_d != 0))) { |
---|
869 | printf("hfsc("); |
---|
870 | if (opts->flags & HFCF_RED) |
---|
871 | printf(" red"); |
---|
872 | if (opts->flags & HFCF_ECN) |
---|
873 | printf(" ecn"); |
---|
874 | if (opts->flags & HFCF_RIO) |
---|
875 | printf(" rio"); |
---|
876 | if (opts->flags & HFCF_CLEARDSCP) |
---|
877 | printf(" cleardscp"); |
---|
878 | if (opts->flags & HFCF_DEFAULTCLASS) |
---|
879 | printf(" default"); |
---|
880 | if (opts->rtsc_m2 != 0) |
---|
881 | print_hfsc_sc("realtime", opts->rtsc_m1, opts->rtsc_d, |
---|
882 | opts->rtsc_m2, rtsc); |
---|
883 | if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth || |
---|
884 | opts->lssc_d != 0)) |
---|
885 | print_hfsc_sc("linkshare", opts->lssc_m1, opts->lssc_d, |
---|
886 | opts->lssc_m2, lssc); |
---|
887 | if (opts->ulsc_m2 != 0) |
---|
888 | print_hfsc_sc("upperlimit", opts->ulsc_m1, opts->ulsc_d, |
---|
889 | opts->ulsc_m2, ulsc); |
---|
890 | printf(" ) "); |
---|
891 | |
---|
892 | return (1); |
---|
893 | } else |
---|
894 | return (0); |
---|
895 | } |
---|
896 | |
---|
897 | /* |
---|
898 | * admission control using generalized service curve |
---|
899 | */ |
---|
900 | |
---|
901 | /* add a new service curve to a generalized service curve */ |
---|
902 | static void |
---|
903 | gsc_add_sc(struct gen_sc *gsc, struct service_curve *sc) |
---|
904 | { |
---|
905 | if (is_sc_null(sc)) |
---|
906 | return; |
---|
907 | if (sc->d != 0) |
---|
908 | gsc_add_seg(gsc, 0.0, 0.0, (double)sc->d, (double)sc->m1); |
---|
909 | gsc_add_seg(gsc, (double)sc->d, 0.0, INFINITY, (double)sc->m2); |
---|
910 | } |
---|
911 | |
---|
912 | /* |
---|
913 | * check whether all points of a generalized service curve have |
---|
914 | * their y-coordinates no larger than a given two-piece linear |
---|
915 | * service curve. |
---|
916 | */ |
---|
917 | static int |
---|
918 | is_gsc_under_sc(struct gen_sc *gsc, struct service_curve *sc) |
---|
919 | { |
---|
920 | struct segment *s, *last, *end; |
---|
921 | double y; |
---|
922 | |
---|
923 | if (is_sc_null(sc)) { |
---|
924 | if (LIST_EMPTY(gsc)) |
---|
925 | return (1); |
---|
926 | LIST_FOREACH(s, gsc, _next) { |
---|
927 | if (s->m != 0) |
---|
928 | return (0); |
---|
929 | } |
---|
930 | return (1); |
---|
931 | } |
---|
932 | /* |
---|
933 | * gsc has a dummy entry at the end with x = INFINITY. |
---|
934 | * loop through up to this dummy entry. |
---|
935 | */ |
---|
936 | end = gsc_getentry(gsc, INFINITY); |
---|
937 | if (end == NULL) |
---|
938 | return (1); |
---|
939 | last = NULL; |
---|
940 | for (s = LIST_FIRST(gsc); s != end; s = LIST_NEXT(s, _next)) { |
---|
941 | if (s->y > sc_x2y(sc, s->x)) |
---|
942 | return (0); |
---|
943 | last = s; |
---|
944 | } |
---|
945 | /* last now holds the real last segment */ |
---|
946 | if (last == NULL) |
---|
947 | return (1); |
---|
948 | if (last->m > sc->m2) |
---|
949 | return (0); |
---|
950 | if (last->x < sc->d && last->m > sc->m1) { |
---|
951 | y = last->y + (sc->d - last->x) * last->m; |
---|
952 | if (y > sc_x2y(sc, sc->d)) |
---|
953 | return (0); |
---|
954 | } |
---|
955 | return (1); |
---|
956 | } |
---|
957 | |
---|
958 | static void |
---|
959 | gsc_destroy(struct gen_sc *gsc) |
---|
960 | { |
---|
961 | struct segment *s; |
---|
962 | |
---|
963 | while ((s = LIST_FIRST(gsc)) != NULL) { |
---|
964 | LIST_REMOVE(s, _next); |
---|
965 | free(s); |
---|
966 | } |
---|
967 | } |
---|
968 | |
---|
969 | /* |
---|
970 | * return a segment entry starting at x. |
---|
971 | * if gsc has no entry starting at x, a new entry is created at x. |
---|
972 | */ |
---|
973 | static struct segment * |
---|
974 | gsc_getentry(struct gen_sc *gsc, double x) |
---|
975 | { |
---|
976 | struct segment *new, *prev, *s; |
---|
977 | |
---|
978 | prev = NULL; |
---|
979 | LIST_FOREACH(s, gsc, _next) { |
---|
980 | if (s->x == x) |
---|
981 | return (s); /* matching entry found */ |
---|
982 | else if (s->x < x) |
---|
983 | prev = s; |
---|
984 | else |
---|
985 | break; |
---|
986 | } |
---|
987 | |
---|
988 | /* we have to create a new entry */ |
---|
989 | if ((new = calloc(1, sizeof(struct segment))) == NULL) |
---|
990 | return (NULL); |
---|
991 | |
---|
992 | new->x = x; |
---|
993 | if (x == INFINITY || s == NULL) |
---|
994 | new->d = 0; |
---|
995 | else if (s->x == INFINITY) |
---|
996 | new->d = INFINITY; |
---|
997 | else |
---|
998 | new->d = s->x - x; |
---|
999 | if (prev == NULL) { |
---|
1000 | /* insert the new entry at the head of the list */ |
---|
1001 | new->y = 0; |
---|
1002 | new->m = 0; |
---|
1003 | LIST_INSERT_HEAD(gsc, new, _next); |
---|
1004 | } else { |
---|
1005 | /* |
---|
1006 | * the start point intersects with the segment pointed by |
---|
1007 | * prev. divide prev into 2 segments |
---|
1008 | */ |
---|
1009 | if (x == INFINITY) { |
---|
1010 | prev->d = INFINITY; |
---|
1011 | if (prev->m == 0) |
---|
1012 | new->y = prev->y; |
---|
1013 | else |
---|
1014 | new->y = INFINITY; |
---|
1015 | } else { |
---|
1016 | prev->d = x - prev->x; |
---|
1017 | new->y = prev->d * prev->m + prev->y; |
---|
1018 | } |
---|
1019 | new->m = prev->m; |
---|
1020 | LIST_INSERT_AFTER(prev, new, _next); |
---|
1021 | } |
---|
1022 | return (new); |
---|
1023 | } |
---|
1024 | |
---|
1025 | /* add a segment to a generalized service curve */ |
---|
1026 | static int |
---|
1027 | gsc_add_seg(struct gen_sc *gsc, double x, double y, double d, double m) |
---|
1028 | { |
---|
1029 | struct segment *start, *end, *s; |
---|
1030 | double x2; |
---|
1031 | |
---|
1032 | if (d == INFINITY) |
---|
1033 | x2 = INFINITY; |
---|
1034 | else |
---|
1035 | x2 = x + d; |
---|
1036 | start = gsc_getentry(gsc, x); |
---|
1037 | end = gsc_getentry(gsc, x2); |
---|
1038 | if (start == NULL || end == NULL) |
---|
1039 | return (-1); |
---|
1040 | |
---|
1041 | for (s = start; s != end; s = LIST_NEXT(s, _next)) { |
---|
1042 | s->m += m; |
---|
1043 | s->y += y + (s->x - x) * m; |
---|
1044 | } |
---|
1045 | |
---|
1046 | end = gsc_getentry(gsc, INFINITY); |
---|
1047 | for (; s != end; s = LIST_NEXT(s, _next)) { |
---|
1048 | s->y += m * d; |
---|
1049 | } |
---|
1050 | |
---|
1051 | return (0); |
---|
1052 | } |
---|
1053 | |
---|
1054 | /* get y-projection of a service curve */ |
---|
1055 | static double |
---|
1056 | sc_x2y(struct service_curve *sc, double x) |
---|
1057 | { |
---|
1058 | double y; |
---|
1059 | |
---|
1060 | if (x <= (double)sc->d) |
---|
1061 | /* y belongs to the 1st segment */ |
---|
1062 | y = x * (double)sc->m1; |
---|
1063 | else |
---|
1064 | /* y belongs to the 2nd segment */ |
---|
1065 | y = (double)sc->d * (double)sc->m1 |
---|
1066 | + (x - (double)sc->d) * (double)sc->m2; |
---|
1067 | return (y); |
---|
1068 | } |
---|
1069 | |
---|
1070 | /* |
---|
1071 | * misc utilities |
---|
1072 | */ |
---|
1073 | #define R2S_BUFS 8 |
---|
1074 | #define RATESTR_MAX 16 |
---|
1075 | |
---|
1076 | char * |
---|
1077 | rate2str(double rate) |
---|
1078 | { |
---|
1079 | char *buf; |
---|
1080 | static char r2sbuf[R2S_BUFS][RATESTR_MAX]; /* ring bufer */ |
---|
1081 | static int idx = 0; |
---|
1082 | int i; |
---|
1083 | static const char unit[] = " KMG"; |
---|
1084 | |
---|
1085 | buf = r2sbuf[idx++]; |
---|
1086 | if (idx == R2S_BUFS) |
---|
1087 | idx = 0; |
---|
1088 | |
---|
1089 | for (i = 0; rate >= 1000 && i <= 3; i++) |
---|
1090 | rate /= 1000; |
---|
1091 | |
---|
1092 | if ((int)(rate * 100) % 100) |
---|
1093 | snprintf(buf, RATESTR_MAX, "%.2f%cb", rate, unit[i]); |
---|
1094 | else |
---|
1095 | snprintf(buf, RATESTR_MAX, "%d%cb", (int)rate, unit[i]); |
---|
1096 | |
---|
1097 | return (buf); |
---|
1098 | } |
---|
1099 | |
---|
1100 | #ifdef __FreeBSD__ |
---|
1101 | /* |
---|
1102 | * XXX |
---|
1103 | * FreeBSD does not have SIOCGIFDATA. |
---|
1104 | * To emulate this, DIOCGIFSPEED ioctl added to pf. |
---|
1105 | */ |
---|
1106 | u_int32_t |
---|
1107 | getifspeed(int pfdev, char *ifname) |
---|
1108 | { |
---|
1109 | struct pf_ifspeed io; |
---|
1110 | |
---|
1111 | bzero(&io, sizeof io); |
---|
1112 | if (strlcpy(io.ifname, ifname, IFNAMSIZ) >= |
---|
1113 | sizeof(io.ifname)) |
---|
1114 | errx(1, "getifspeed: strlcpy"); |
---|
1115 | if (ioctl(pfdev, DIOCGIFSPEED, &io) == -1) |
---|
1116 | err(1, "DIOCGIFSPEED"); |
---|
1117 | return ((u_int32_t)io.baudrate); |
---|
1118 | } |
---|
1119 | #else |
---|
1120 | u_int32_t |
---|
1121 | getifspeed(char *ifname) |
---|
1122 | { |
---|
1123 | int s; |
---|
1124 | struct ifreq ifr; |
---|
1125 | struct if_data ifrdat; |
---|
1126 | |
---|
1127 | if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) |
---|
1128 | err(1, "socket"); |
---|
1129 | bzero(&ifr, sizeof(ifr)); |
---|
1130 | if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >= |
---|
1131 | sizeof(ifr.ifr_name)) |
---|
1132 | errx(1, "getifspeed: strlcpy"); |
---|
1133 | ifr.ifr_data = (caddr_t)&ifrdat; |
---|
1134 | if (ioctl(s, SIOCGIFDATA, (caddr_t)&ifr) == -1) |
---|
1135 | err(1, "SIOCGIFDATA"); |
---|
1136 | if (close(s)) |
---|
1137 | err(1, "close"); |
---|
1138 | return ((u_int32_t)ifrdat.ifi_baudrate); |
---|
1139 | } |
---|
1140 | #endif |
---|
1141 | |
---|
1142 | u_long |
---|
1143 | getifmtu(char *ifname) |
---|
1144 | { |
---|
1145 | int s; |
---|
1146 | struct ifreq ifr; |
---|
1147 | |
---|
1148 | if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) |
---|
1149 | err(1, "socket"); |
---|
1150 | bzero(&ifr, sizeof(ifr)); |
---|
1151 | if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >= |
---|
1152 | sizeof(ifr.ifr_name)) |
---|
1153 | errx(1, "getifmtu: strlcpy"); |
---|
1154 | if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) == -1) |
---|
1155 | #ifdef __FreeBSD__ |
---|
1156 | ifr.ifr_mtu = 1500; |
---|
1157 | #else |
---|
1158 | err(1, "SIOCGIFMTU"); |
---|
1159 | #endif |
---|
1160 | if (close(s)) |
---|
1161 | err(1, "close"); |
---|
1162 | if (ifr.ifr_mtu > 0) |
---|
1163 | return (ifr.ifr_mtu); |
---|
1164 | else { |
---|
1165 | warnx("could not get mtu for %s, assuming 1500", ifname); |
---|
1166 | return (1500); |
---|
1167 | } |
---|
1168 | } |
---|
1169 | |
---|
1170 | int |
---|
1171 | eval_queue_opts(struct pf_altq *pa, struct node_queue_opt *opts, |
---|
1172 | u_int32_t ref_bw) |
---|
1173 | { |
---|
1174 | int errors = 0; |
---|
1175 | |
---|
1176 | switch (pa->scheduler) { |
---|
1177 | case ALTQT_CBQ: |
---|
1178 | pa->pq_u.cbq_opts = opts->data.cbq_opts; |
---|
1179 | break; |
---|
1180 | case ALTQT_PRIQ: |
---|
1181 | pa->pq_u.priq_opts = opts->data.priq_opts; |
---|
1182 | break; |
---|
1183 | case ALTQT_HFSC: |
---|
1184 | pa->pq_u.hfsc_opts.flags = opts->data.hfsc_opts.flags; |
---|
1185 | if (opts->data.hfsc_opts.linkshare.used) { |
---|
1186 | pa->pq_u.hfsc_opts.lssc_m1 = |
---|
1187 | eval_bwspec(&opts->data.hfsc_opts.linkshare.m1, |
---|
1188 | ref_bw); |
---|
1189 | pa->pq_u.hfsc_opts.lssc_m2 = |
---|
1190 | eval_bwspec(&opts->data.hfsc_opts.linkshare.m2, |
---|
1191 | ref_bw); |
---|
1192 | pa->pq_u.hfsc_opts.lssc_d = |
---|
1193 | opts->data.hfsc_opts.linkshare.d; |
---|
1194 | } |
---|
1195 | if (opts->data.hfsc_opts.realtime.used) { |
---|
1196 | pa->pq_u.hfsc_opts.rtsc_m1 = |
---|
1197 | eval_bwspec(&opts->data.hfsc_opts.realtime.m1, |
---|
1198 | ref_bw); |
---|
1199 | pa->pq_u.hfsc_opts.rtsc_m2 = |
---|
1200 | eval_bwspec(&opts->data.hfsc_opts.realtime.m2, |
---|
1201 | ref_bw); |
---|
1202 | pa->pq_u.hfsc_opts.rtsc_d = |
---|
1203 | opts->data.hfsc_opts.realtime.d; |
---|
1204 | } |
---|
1205 | if (opts->data.hfsc_opts.upperlimit.used) { |
---|
1206 | pa->pq_u.hfsc_opts.ulsc_m1 = |
---|
1207 | eval_bwspec(&opts->data.hfsc_opts.upperlimit.m1, |
---|
1208 | ref_bw); |
---|
1209 | pa->pq_u.hfsc_opts.ulsc_m2 = |
---|
1210 | eval_bwspec(&opts->data.hfsc_opts.upperlimit.m2, |
---|
1211 | ref_bw); |
---|
1212 | pa->pq_u.hfsc_opts.ulsc_d = |
---|
1213 | opts->data.hfsc_opts.upperlimit.d; |
---|
1214 | } |
---|
1215 | break; |
---|
1216 | default: |
---|
1217 | warnx("eval_queue_opts: unknown scheduler type %u", |
---|
1218 | opts->qtype); |
---|
1219 | errors++; |
---|
1220 | break; |
---|
1221 | } |
---|
1222 | |
---|
1223 | return (errors); |
---|
1224 | } |
---|
1225 | |
---|
1226 | u_int32_t |
---|
1227 | eval_bwspec(struct node_queue_bw *bw, u_int32_t ref_bw) |
---|
1228 | { |
---|
1229 | if (bw->bw_absolute > 0) |
---|
1230 | return (bw->bw_absolute); |
---|
1231 | |
---|
1232 | if (bw->bw_percent > 0) |
---|
1233 | return (ref_bw / 100 * bw->bw_percent); |
---|
1234 | |
---|
1235 | return (0); |
---|
1236 | } |
---|
1237 | |
---|
1238 | void |
---|
1239 | print_hfsc_sc(const char *scname, u_int m1, u_int d, u_int m2, |
---|
1240 | const struct node_hfsc_sc *sc) |
---|
1241 | { |
---|
1242 | printf(" %s", scname); |
---|
1243 | |
---|
1244 | if (d != 0) { |
---|
1245 | printf("("); |
---|
1246 | if (sc != NULL && sc->m1.bw_percent > 0) |
---|
1247 | printf("%u%%", sc->m1.bw_percent); |
---|
1248 | else |
---|
1249 | printf("%s", rate2str((double)m1)); |
---|
1250 | printf(" %u", d); |
---|
1251 | } |
---|
1252 | |
---|
1253 | if (sc != NULL && sc->m2.bw_percent > 0) |
---|
1254 | printf(" %u%%", sc->m2.bw_percent); |
---|
1255 | else |
---|
1256 | printf(" %s", rate2str((double)m2)); |
---|
1257 | |
---|
1258 | if (d != 0) |
---|
1259 | printf(")"); |
---|
1260 | } |
---|