source: rtems-libbsd/freebsd/sbin/pfctl/pfctl_altq.c @ a5ddb0e

5
Last change on this file since a5ddb0e was a5ddb0e, checked in by Sebastian Huber <sebastian.huber@…>, on 09/24/19 at 09:05:03

Update to FreeBSD head 2019-09-24

Git mirror commit 6b0307a0a5184339393f555d5d424190d8a8277a.

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