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