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