2 * Copyright (c) 2011-2017 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (c) 2010 Fabio Checconi, Luigi Rizzo, Paolo Valente
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
42 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * Quick Fair Queueing is described in
57 * "QFQ: Efficient Packet Scheduling with Tight Bandwidth Distribution
58 * Guarantees" by Fabio Checconi, Paolo Valente, and Luigi Rizzo.
60 * This code is ported from the dummynet(4) QFQ implementation.
61 * See also http://info.iet.unipi.it/~luigi/qfq/
64 #include <sys/cdefs.h>
65 #include <sys/param.h>
66 #include <sys/malloc.h>
68 #include <sys/systm.h>
69 #include <sys/errno.h>
70 #include <sys/kernel.h>
71 #include <sys/syslog.h>
73 #include <kern/zalloc.h>
76 #include <net/net_osdep.h>
78 #include <net/pktsched/pktsched_qfq.h>
79 #include <netinet/in.h>
85 static int qfq_enqueue_ifclassq(struct ifclassq
*, void *, classq_pkt_type_t
,
87 static void *qfq_dequeue_ifclassq(struct ifclassq
*, classq_pkt_type_t
*);
88 static int qfq_request_ifclassq(struct ifclassq
*, cqrq_t
, void *);
89 static int qfq_clear_interface(struct qfq_if
*);
90 static struct qfq_class
*qfq_class_create(struct qfq_if
*, u_int32_t
,
91 u_int32_t
, u_int32_t
, u_int32_t
, u_int32_t
, classq_pkt_type_t
);
92 static int qfq_class_destroy(struct qfq_if
*, struct qfq_class
*);
93 static int qfq_destroy_locked(struct qfq_if
*);
94 static inline int qfq_addq(struct qfq_class
*, pktsched_pkt_t
*,
96 static inline void qfq_getq(struct qfq_class
*, pktsched_pkt_t
*);
97 static void qfq_purgeq(struct qfq_if
*, struct qfq_class
*, u_int32_t
,
98 u_int32_t
*, u_int32_t
*);
99 static void qfq_purge_sc(struct qfq_if
*, cqrq_purge_sc_t
*);
100 static void qfq_updateq(struct qfq_if
*, struct qfq_class
*, cqev_t
);
101 static int qfq_throttle(struct qfq_if
*, cqrq_throttle_t
*);
102 static int qfq_resumeq(struct qfq_if
*, struct qfq_class
*);
103 static int qfq_suspendq(struct qfq_if
*, struct qfq_class
*);
104 static int qfq_stat_sc(struct qfq_if
*, cqrq_stat_sc_t
*);
105 static inline struct qfq_class
*qfq_clh_to_clp(struct qfq_if
*, u_int32_t
);
106 static const char *qfq_style(struct qfq_if
*);
108 static inline int qfq_gt(u_int64_t
, u_int64_t
);
109 static inline u_int64_t
qfq_round_down(u_int64_t
, u_int32_t
);
110 static inline struct qfq_group
*qfq_ffs(struct qfq_if
*, pktsched_bitmap_t
);
111 static int qfq_calc_index(struct qfq_class
*, u_int32_t
, u_int32_t
);
112 static inline pktsched_bitmap_t
mask_from(pktsched_bitmap_t
, int);
113 static inline u_int32_t
qfq_calc_state(struct qfq_if
*, struct qfq_group
*);
114 static inline void qfq_move_groups(struct qfq_if
*, pktsched_bitmap_t
,
116 static inline void qfq_unblock_groups(struct qfq_if
*, int, u_int64_t
);
117 static inline void qfq_make_eligible(struct qfq_if
*, u_int64_t
);
118 static inline void qfq_slot_insert(struct qfq_if
*, struct qfq_group
*,
119 struct qfq_class
*, u_int64_t
);
120 static inline void qfq_front_slot_remove(struct qfq_group
*);
121 static inline struct qfq_class
*qfq_slot_scan(struct qfq_if
*,
123 static inline void qfq_slot_rotate(struct qfq_if
*, struct qfq_group
*,
125 static inline void qfq_update_eligible(struct qfq_if
*, u_int64_t
);
126 static inline int qfq_update_class(struct qfq_if
*, struct qfq_group
*,
128 static inline void qfq_update_start(struct qfq_if
*, struct qfq_class
*);
129 static inline void qfq_slot_remove(struct qfq_if
*, struct qfq_group
*,
131 static void qfq_deactivate_class(struct qfq_if
*, struct qfq_class
*);
132 static const char *qfq_state2str(int);
134 static void qfq_dump_groups(struct qfq_if
*, u_int32_t
);
135 static void qfq_dump_sched(struct qfq_if
*, const char *);
136 #endif /* QFQ_DEBUG */
138 #define QFQ_ZONE_MAX 32 /* maximum elements in zone */
139 #define QFQ_ZONE_NAME "pktsched_qfq" /* zone name */
141 static unsigned int qfq_size
; /* size of zone element */
142 static struct zone
*qfq_zone
; /* zone for qfq */
144 #define QFQ_CL_ZONE_MAX 32 /* maximum elements in zone */
145 #define QFQ_CL_ZONE_NAME "pktsched_qfq_cl" /* zone name */
147 static unsigned int qfq_cl_size
; /* size of zone element */
148 static struct zone
*qfq_cl_zone
; /* zone for qfq_class */
151 * Maximum number of consecutive slots occupied by backlogged classes
152 * inside a group. This is approx lmax/lmin + 5. Used when ALTQ is
155 * XXX check because it poses constraints on MAX_INDEX
157 #define QFQ_MAX_SLOTS 32 /* default when ALTQ is available */
162 qfq_size
= sizeof(struct qfq_if
);
163 qfq_zone
= zinit(qfq_size
, QFQ_ZONE_MAX
* qfq_size
,
165 if (qfq_zone
== NULL
) {
166 panic("%s: failed allocating %s", __func__
, QFQ_ZONE_NAME
);
169 zone_change(qfq_zone
, Z_EXPAND
, TRUE
);
170 zone_change(qfq_zone
, Z_CALLERACCT
, TRUE
);
172 qfq_cl_size
= sizeof(struct qfq_class
);
173 qfq_cl_zone
= zinit(qfq_cl_size
, QFQ_CL_ZONE_MAX
* qfq_cl_size
,
174 0, QFQ_CL_ZONE_NAME
);
175 if (qfq_cl_zone
== NULL
) {
176 panic("%s: failed allocating %s", __func__
, QFQ_CL_ZONE_NAME
);
179 zone_change(qfq_cl_zone
, Z_EXPAND
, TRUE
);
180 zone_change(qfq_cl_zone
, Z_CALLERACCT
, TRUE
);
184 qfq_alloc(struct ifnet
*ifp
, int how
)
188 qif
= (how
== M_WAITOK
) ? zalloc(qfq_zone
) : zalloc_noblock(qfq_zone
);
193 bzero(qif
, qfq_size
);
194 qif
->qif_ifq
= &ifp
->if_snd
;
196 qif
->qif_maxclasses
= IFCQ_SC_MAX
;
198 * TODO: adi@apple.com
200 * Ideally I would like to have the following
201 * but QFQ needs further modifications.
203 * qif->qif_maxslots = IFCQ_SC_MAX;
205 qif
->qif_maxslots
= QFQ_MAX_SLOTS
;
207 if ((qif
->qif_class_tbl
= _MALLOC(sizeof(struct qfq_class
*) *
208 qif
->qif_maxclasses
, M_DEVBUF
, M_WAITOK
| M_ZERO
)) == NULL
) {
209 log(LOG_ERR
, "%s: %s unable to allocate class table array\n",
210 if_name(ifp
), qfq_style(qif
));
214 if ((qif
->qif_groups
= _MALLOC(sizeof(struct qfq_group
*) *
215 (QFQ_MAX_INDEX
+ 1), M_DEVBUF
, M_WAITOK
| M_ZERO
)) == NULL
) {
216 log(LOG_ERR
, "%s: %s unable to allocate group array\n",
217 if_name(ifp
), qfq_style(qif
));
221 if (pktsched_verbose
) {
222 log(LOG_DEBUG
, "%s: %s scheduler allocated\n",
223 if_name(ifp
), qfq_style(qif
));
229 if (qif
->qif_class_tbl
!= NULL
) {
230 _FREE(qif
->qif_class_tbl
, M_DEVBUF
);
231 qif
->qif_class_tbl
= NULL
;
233 if (qif
->qif_groups
!= NULL
) {
234 _FREE(qif
->qif_groups
, M_DEVBUF
);
235 qif
->qif_groups
= NULL
;
237 zfree(qfq_zone
, qif
);
243 qfq_destroy(struct qfq_if
*qif
)
245 struct ifclassq
*ifq
= qif
->qif_ifq
;
249 err
= qfq_destroy_locked(qif
);
256 qfq_destroy_locked(struct qfq_if
*qif
)
260 IFCQ_LOCK_ASSERT_HELD(qif
->qif_ifq
);
262 (void) qfq_clear_interface(qif
);
264 VERIFY(qif
->qif_class_tbl
!= NULL
);
265 _FREE(qif
->qif_class_tbl
, M_DEVBUF
);
266 qif
->qif_class_tbl
= NULL
;
268 VERIFY(qif
->qif_groups
!= NULL
);
269 for (i
= 0; i
<= QFQ_MAX_INDEX
; i
++) {
270 struct qfq_group
*grp
= qif
->qif_groups
[i
];
273 VERIFY(grp
->qfg_slots
!= NULL
);
274 _FREE(grp
->qfg_slots
, M_DEVBUF
);
275 grp
->qfg_slots
= NULL
;
276 _FREE(grp
, M_DEVBUF
);
277 qif
->qif_groups
[i
] = NULL
;
280 _FREE(qif
->qif_groups
, M_DEVBUF
);
281 qif
->qif_groups
= NULL
;
283 if (pktsched_verbose
) {
284 log(LOG_DEBUG
, "%s: %s scheduler destroyed\n",
285 if_name(QFQIF_IFP(qif
)), qfq_style(qif
));
288 zfree(qfq_zone
, qif
);
294 * bring the interface back to the initial state by discarding
295 * all the filters and classes.
298 qfq_clear_interface(struct qfq_if
*qif
)
300 struct qfq_class
*cl
;
303 IFCQ_LOCK_ASSERT_HELD(qif
->qif_ifq
);
305 /* clear out the classes */
306 for (i
= 0; i
< qif
->qif_maxclasses
; i
++) {
307 if ((cl
= qif
->qif_class_tbl
[i
]) != NULL
) {
308 qfq_class_destroy(qif
, cl
);
315 /* discard all the queued packets on the interface */
317 qfq_purge(struct qfq_if
*qif
)
319 struct qfq_class
*cl
;
322 IFCQ_LOCK_ASSERT_HELD(qif
->qif_ifq
);
324 for (i
= 0; i
< qif
->qif_maxclasses
; i
++) {
325 if ((cl
= qif
->qif_class_tbl
[i
]) != NULL
) {
326 qfq_purgeq(qif
, cl
, 0, NULL
, NULL
);
329 VERIFY(IFCQ_LEN(qif
->qif_ifq
) == 0);
333 qfq_purge_sc(struct qfq_if
*qif
, cqrq_purge_sc_t
*pr
)
335 struct ifclassq
*ifq
= qif
->qif_ifq
;
338 IFCQ_LOCK_ASSERT_HELD(ifq
);
340 VERIFY(pr
->sc
== MBUF_SC_UNSPEC
|| MBUF_VALID_SC(pr
->sc
));
341 VERIFY(pr
->flow
!= 0);
343 if (pr
->sc
!= MBUF_SC_UNSPEC
) {
344 i
= MBUF_SCIDX(pr
->sc
);
345 VERIFY(i
< IFCQ_SC_MAX
);
347 qfq_purgeq(qif
, ifq
->ifcq_disc_slots
[i
].cl
,
348 pr
->flow
, &pr
->packets
, &pr
->bytes
);
355 for (i
= 0; i
< IFCQ_SC_MAX
; i
++) {
356 qfq_purgeq(qif
, ifq
->ifcq_disc_slots
[i
].cl
,
357 pr
->flow
, &cnt
, &len
);
365 qfq_event(struct qfq_if
*qif
, cqev_t ev
)
367 struct qfq_class
*cl
;
370 IFCQ_LOCK_ASSERT_HELD(qif
->qif_ifq
);
372 for (i
= 0; i
< qif
->qif_maxclasses
; i
++) {
373 if ((cl
= qif
->qif_class_tbl
[i
]) != NULL
) {
374 qfq_updateq(qif
, cl
, ev
);
380 qfq_add_queue(struct qfq_if
*qif
, u_int32_t qlimit
, u_int32_t weight
,
381 u_int32_t maxsz
, u_int32_t flags
, u_int32_t qid
, struct qfq_class
**clp
,
382 classq_pkt_type_t ptype
)
384 struct qfq_class
*cl
;
387 IFCQ_LOCK_ASSERT_HELD(qif
->qif_ifq
);
389 if (qfq_clh_to_clp(qif
, qid
) != NULL
) {
393 /* check parameters */
394 if (weight
== 0 || weight
> QFQ_MAX_WEIGHT
) {
398 w
= (QFQ_ONE_FP
/ (QFQ_ONE_FP
/ weight
));
399 if (qif
->qif_wsum
+ w
> QFQ_MAX_WSUM
) {
403 if (maxsz
== 0 || maxsz
> (1 << QFQ_MTU_SHIFT
)) {
407 cl
= qfq_class_create(qif
, weight
, qlimit
, flags
, maxsz
, qid
, ptype
);
419 static struct qfq_class
*
420 qfq_class_create(struct qfq_if
*qif
, u_int32_t weight
, u_int32_t qlimit
,
421 u_int32_t flags
, u_int32_t maxsz
, u_int32_t qid
, classq_pkt_type_t ptype
)
424 struct ifclassq
*ifq
;
425 struct qfq_group
*grp
;
426 struct qfq_class
*cl
;
427 u_int32_t w
; /* approximated weight */
430 IFCQ_LOCK_ASSERT_HELD(qif
->qif_ifq
);
432 if (qif
->qif_classes
>= qif
->qif_maxclasses
) {
433 log(LOG_ERR
, "%s: %s out of classes! (max %d)\n",
434 if_name(QFQIF_IFP(qif
)), qfq_style(qif
),
435 qif
->qif_maxclasses
);
440 ifp
= QFQIF_IFP(qif
);
442 cl
= zalloc(qfq_cl_zone
);
447 bzero(cl
, qfq_cl_size
);
449 if (qlimit
== 0 || qlimit
> IFCQ_MAXLEN(ifq
)) {
450 qlimit
= IFCQ_MAXLEN(ifq
);
452 qlimit
= DEFAULT_QLIMIT
; /* use default */
455 _qinit(&cl
->cl_q
, Q_DROPTAIL
, qlimit
, ptype
);
457 cl
->cl_flags
= flags
;
461 * Find a free slot in the class table. If the slot matching
462 * the lower bits of qid is free, use this slot. Otherwise,
463 * use the first free slot.
465 i
= qid
% qif
->qif_maxclasses
;
466 if (qif
->qif_class_tbl
[i
] == NULL
) {
467 qif
->qif_class_tbl
[i
] = cl
;
469 for (i
= 0; i
< qif
->qif_maxclasses
; i
++) {
470 if (qif
->qif_class_tbl
[i
] == NULL
) {
471 qif
->qif_class_tbl
[i
] = cl
;
475 if (i
== qif
->qif_maxclasses
) {
476 zfree(qfq_cl_zone
, cl
);
482 VERIFY(w
> 0 && w
<= QFQ_MAX_WEIGHT
);
484 cl
->cl_inv_w
= (QFQ_ONE_FP
/ w
);
485 w
= (QFQ_ONE_FP
/ cl
->cl_inv_w
);
486 VERIFY(qif
->qif_wsum
+ w
<= QFQ_MAX_WSUM
);
488 i
= qfq_calc_index(cl
, cl
->cl_inv_w
, cl
->cl_lmax
);
489 VERIFY(i
<= QFQ_MAX_INDEX
);
490 grp
= qif
->qif_groups
[i
];
492 grp
= _MALLOC(sizeof(*grp
), M_DEVBUF
, M_WAITOK
| M_ZERO
);
495 grp
->qfg_slot_shift
=
496 QFQ_MTU_SHIFT
+ QFQ_FRAC_BITS
- (QFQ_MAX_INDEX
- i
);
497 grp
->qfg_slots
= _MALLOC(sizeof(struct qfq_class
*) *
498 qif
->qif_maxslots
, M_DEVBUF
, M_WAITOK
| M_ZERO
);
499 if (grp
->qfg_slots
== NULL
) {
500 log(LOG_ERR
, "%s: %s unable to allocate group "
501 "slots for index %d\n", if_name(ifp
),
505 log(LOG_ERR
, "%s: %s unable to allocate group for "
506 "qid=%d\n", if_name(ifp
), qfq_style(qif
),
509 if (grp
== NULL
|| grp
->qfg_slots
== NULL
) {
510 qif
->qif_class_tbl
[qid
% qif
->qif_maxclasses
] = NULL
;
512 _FREE(grp
, M_DEVBUF
);
514 zfree(qfq_cl_zone
, cl
);
517 qif
->qif_groups
[i
] = grp
;
522 /* XXX cl->cl_S = qif->qif_V; ? */
523 /* XXX compute qif->qif_i_wsum */
527 if (flags
& QFCF_DEFAULTCLASS
) {
528 qif
->qif_default
= cl
;
531 if (flags
& QFCF_SFB
) {
533 if (flags
& QFCF_ECN
) {
534 cl
->cl_qflags
|= SFBF_ECN
;
536 if (flags
& QFCF_FLOWCTL
) {
537 cl
->cl_qflags
|= SFBF_FLOWCTL
;
539 if (flags
& QFCF_DELAYBASED
) {
540 cl
->cl_qflags
|= SFBF_DELAYBASED
;
542 if (!(cl
->cl_flags
& QFCF_LAZY
)) {
543 cl
->cl_sfb
= sfb_alloc(ifp
, cl
->cl_handle
,
544 qlimit(&cl
->cl_q
), cl
->cl_qflags
);
546 if (cl
->cl_sfb
!= NULL
|| (cl
->cl_flags
& QFCF_LAZY
)) {
547 qtype(&cl
->cl_q
) = Q_SFB
;
551 if (pktsched_verbose
) {
552 log(LOG_DEBUG
, "%s: %s created qid=%d grp=%d weight=%d "
553 "qlimit=%d flags=%b\n", if_name(ifp
), qfq_style(qif
),
554 cl
->cl_handle
, cl
->cl_grp
->qfg_index
, weight
, qlimit
,
562 qfq_remove_queue(struct qfq_if
*qif
, u_int32_t qid
)
564 struct qfq_class
*cl
;
566 IFCQ_LOCK_ASSERT_HELD(qif
->qif_ifq
);
568 if ((cl
= qfq_clh_to_clp(qif
, qid
)) == NULL
) {
572 return qfq_class_destroy(qif
, cl
);
576 qfq_class_destroy(struct qfq_if
*qif
, struct qfq_class
*cl
)
578 struct ifclassq
*ifq
= qif
->qif_ifq
;
584 IFCQ_LOCK_ASSERT_HELD(ifq
);
586 qfq_purgeq(qif
, cl
, 0, NULL
, NULL
);
588 if (cl
->cl_inv_w
!= 0) {
589 qif
->qif_wsum
-= (QFQ_ONE_FP
/ cl
->cl_inv_w
);
590 cl
->cl_inv_w
= 0; /* reset weight to avoid run twice */
593 for (i
= 0; i
< qif
->qif_maxclasses
; i
++) {
594 if (qif
->qif_class_tbl
[i
] == cl
) {
595 qif
->qif_class_tbl
[i
] = NULL
;
601 if (cl
->cl_qalg
.ptr
!= NULL
) {
602 if (q_is_sfb(&cl
->cl_q
) && cl
->cl_sfb
!= NULL
) {
603 sfb_destroy(cl
->cl_sfb
);
605 cl
->cl_qalg
.ptr
= NULL
;
606 qtype(&cl
->cl_q
) = Q_DROPTAIL
;
607 qstate(&cl
->cl_q
) = QS_RUNNING
;
610 if (qif
->qif_default
== cl
) {
611 qif
->qif_default
= NULL
;
614 if (pktsched_verbose
) {
615 log(LOG_DEBUG
, "%s: %s destroyed qid=%d\n",
616 if_name(QFQIF_IFP(qif
)), qfq_style(qif
), cl
->cl_handle
);
619 zfree(qfq_cl_zone
, cl
);
625 * Calculate a mask to mimic what would be ffs_from()
627 static inline pktsched_bitmap_t
628 mask_from(pktsched_bitmap_t bitmap
, int from
)
630 return bitmap
& ~((1UL << from
) - 1);
634 * The state computation relies on ER=0, IR=1, EB=2, IB=3
635 * First compute eligibility comparing grp->qfg_S, qif->qif_V,
636 * then check if someone is blocking us and possibly add EB
638 static inline u_int32_t
639 qfq_calc_state(struct qfq_if
*qif
, struct qfq_group
*grp
)
641 /* if S > V we are not eligible */
642 u_int32_t state
= qfq_gt(grp
->qfg_S
, qif
->qif_V
);
643 pktsched_bitmap_t mask
= mask_from(qif
->qif_bitmaps
[ER
],
645 struct qfq_group
*next
;
648 next
= qfq_ffs(qif
, mask
);
649 if (qfq_gt(grp
->qfg_F
, next
->qfg_F
)) {
659 * qif->qif_bitmaps[dst] |= qif->qif_bitmaps[src] & mask;
660 * qif->qif_bitmaps[src] &= ~mask;
661 * but we should make sure that src != dst
664 qfq_move_groups(struct qfq_if
*qif
, pktsched_bitmap_t mask
, int src
, int dst
)
666 qif
->qif_bitmaps
[dst
] |= qif
->qif_bitmaps
[src
] & mask
;
667 qif
->qif_bitmaps
[src
] &= ~mask
;
671 qfq_unblock_groups(struct qfq_if
*qif
, int index
, u_int64_t old_finish
)
673 pktsched_bitmap_t mask
= mask_from(qif
->qif_bitmaps
[ER
], index
+ 1);
674 struct qfq_group
*next
;
677 next
= qfq_ffs(qif
, mask
);
678 if (!qfq_gt(next
->qfg_F
, old_finish
)) {
683 mask
= (1UL << index
) - 1;
684 qfq_move_groups(qif
, mask
, EB
, ER
);
685 qfq_move_groups(qif
, mask
, IB
, IR
);
691 * old_V ^= qif->qif_V;
692 * old_V >>= QFQ_MIN_SLOT_SHIFT;
698 qfq_make_eligible(struct qfq_if
*qif
, u_int64_t old_V
)
700 pktsched_bitmap_t mask
, vslot
, old_vslot
;
702 vslot
= qif
->qif_V
>> QFQ_MIN_SLOT_SHIFT
;
703 old_vslot
= old_V
>> QFQ_MIN_SLOT_SHIFT
;
705 if (vslot
!= old_vslot
) {
706 mask
= (2UL << (__fls(vslot
^ old_vslot
))) - 1;
707 qfq_move_groups(qif
, mask
, IR
, ER
);
708 qfq_move_groups(qif
, mask
, IB
, EB
);
713 * XXX we should make sure that slot becomes less than 32.
714 * This is guaranteed by the input values.
715 * roundedS is always cl->qfg_S rounded on grp->qfg_slot_shift bits.
718 qfq_slot_insert(struct qfq_if
*qif
, struct qfq_group
*grp
,
719 struct qfq_class
*cl
, u_int64_t roundedS
)
721 u_int64_t slot
= (roundedS
- grp
->qfg_S
) >> grp
->qfg_slot_shift
;
722 u_int32_t i
= (grp
->qfg_front
+ slot
) % qif
->qif_maxslots
;
724 cl
->cl_next
= grp
->qfg_slots
[i
];
725 grp
->qfg_slots
[i
] = cl
;
726 pktsched_bit_set(slot
, &grp
->qfg_full_slots
);
730 * remove the entry from the slot
733 qfq_front_slot_remove(struct qfq_group
*grp
)
735 struct qfq_class
**h
= &grp
->qfg_slots
[grp
->qfg_front
];
739 pktsched_bit_clr(0, &grp
->qfg_full_slots
);
744 * Returns the first full queue in a group. As a side effect,
745 * adjust the bucket list so the first non-empty bucket is at
746 * position 0 in qfg_full_slots.
748 static inline struct qfq_class
*
749 qfq_slot_scan(struct qfq_if
*qif
, struct qfq_group
*grp
)
753 if (pktsched_verbose
> 2) {
754 log(LOG_DEBUG
, "%s: %s grp=%d full_slots=0x%x\n",
755 if_name(QFQIF_IFP(qif
)), qfq_style(qif
), grp
->qfg_index
,
756 grp
->qfg_full_slots
);
759 if (grp
->qfg_full_slots
== 0) {
763 i
= pktsched_ffs(grp
->qfg_full_slots
) - 1; /* zero-based */
765 grp
->qfg_front
= (grp
->qfg_front
+ i
) % qif
->qif_maxslots
;
766 grp
->qfg_full_slots
>>= i
;
769 return grp
->qfg_slots
[grp
->qfg_front
];
773 * adjust the bucket list. When the start time of a group decreases,
774 * we move the index down (modulo qif->qif_maxslots) so we don't need to
775 * move the objects. The mask of occupied slots must be shifted
776 * because we use ffs() to find the first non-empty slot.
777 * This covers decreases in the group's start time, but what about
778 * increases of the start time ?
779 * Here too we should make sure that i is less than 32
782 qfq_slot_rotate(struct qfq_if
*qif
, struct qfq_group
*grp
, u_int64_t roundedS
)
785 u_int32_t i
= (grp
->qfg_S
- roundedS
) >> grp
->qfg_slot_shift
;
787 grp
->qfg_full_slots
<<= i
;
788 grp
->qfg_front
= (grp
->qfg_front
- i
) % qif
->qif_maxslots
;
792 qfq_update_eligible(struct qfq_if
*qif
, u_int64_t old_V
)
794 pktsched_bitmap_t ineligible
;
796 ineligible
= qif
->qif_bitmaps
[IR
] | qif
->qif_bitmaps
[IB
];
798 if (!qif
->qif_bitmaps
[ER
]) {
799 struct qfq_group
*grp
;
800 grp
= qfq_ffs(qif
, ineligible
);
801 if (qfq_gt(grp
->qfg_S
, qif
->qif_V
)) {
802 qif
->qif_V
= grp
->qfg_S
;
805 qfq_make_eligible(qif
, old_V
);
810 * Updates the class, returns true if also the group needs to be updated.
813 qfq_update_class(struct qfq_if
*qif
, struct qfq_group
*grp
,
814 struct qfq_class
*cl
)
818 if (qempty(&cl
->cl_q
)) {
819 qfq_front_slot_remove(grp
);
824 len
= m_pktlen((struct mbuf
*)qhead(&cl
->cl_q
));
825 cl
->cl_F
= cl
->cl_S
+ (u_int64_t
)len
* cl
->cl_inv_w
;
826 roundedS
= qfq_round_down(cl
->cl_S
, grp
->qfg_slot_shift
);
827 if (roundedS
== grp
->qfg_S
) {
831 qfq_front_slot_remove(grp
);
832 qfq_slot_insert(qif
, grp
, cl
, roundedS
);
838 * note: CLASSQDQ_POLL returns the next packet without removing the packet
839 * from the queue. CLASSQDQ_REMOVE is a normal dequeue operation.
840 * CLASSQDQ_REMOVE must return the same packet if called immediately
841 * after CLASSQDQ_POLL.
844 qfq_dequeue(struct qfq_if
*qif
, pktsched_pkt_t
*pkt
)
846 pktsched_bitmap_t er_bits
= qif
->qif_bitmaps
[ER
];
847 struct ifclassq
*ifq
= qif
->qif_ifq
;
848 struct qfq_group
*grp
;
849 struct qfq_class
*cl
;
853 IFCQ_LOCK_ASSERT_HELD(ifq
);
855 pkt
->pktsched_pkt
= NULL
;
860 if (qif
->qif_queued
&& pktsched_verbose
> 1) {
861 qfq_dump_sched(qif
, "start dequeue");
863 #endif /* QFQ_DEBUG */
864 /* no eligible and ready packet */
867 grp
= qfq_ffs(qif
, er_bits
);
868 /* if group is non-empty, use it */
869 if (grp
->qfg_full_slots
!= 0) {
872 pktsched_bit_clr(grp
->qfg_index
, &er_bits
);
875 #endif /* QFQ_DEBUG */
877 VERIFY(!IFCQ_IS_EMPTY(ifq
));
879 cl
= grp
->qfg_slots
[grp
->qfg_front
];
880 VERIFY(cl
!= NULL
&& !qempty(&cl
->cl_q
));
883 VERIFY(pkt
->pktsched_pkt
!= NULL
); /* qalg must be work conserving */
884 len
= pktsched_get_pkt_len(pkt
);
888 #endif /* QFQ_DEBUG */
891 IFCQ_DEC_BYTES(ifq
, len
);
892 if (qempty(&cl
->cl_q
)) {
895 PKTCNTR_ADD(&cl
->cl_xmitcnt
, 1, len
);
896 IFCQ_XMIT_ADD(ifq
, 1, len
);
899 qif
->qif_V
+= (u_int64_t
)len
* QFQ_IWSUM
;
901 if (pktsched_verbose
> 2) {
902 log(LOG_DEBUG
, "%s: %s qid=%d dequeue pkt=0x%llx F=0x%llx "
903 "V=0x%llx", if_name(QFQIF_IFP(qif
)), qfq_style(qif
),
905 (uint64_t)VM_KERNEL_ADDRPERM(pkt
->pktsched_pkt
), cl
->cl_F
,
909 if (qfq_update_class(qif
, grp
, cl
)) {
910 u_int64_t old_F
= grp
->qfg_F
;
912 cl
= qfq_slot_scan(qif
, grp
);
913 if (!cl
) { /* group gone, remove from ER */
914 pktsched_bit_clr(grp
->qfg_index
, &qif
->qif_bitmaps
[ER
]);
918 qfq_round_down(cl
->cl_S
, grp
->qfg_slot_shift
);
920 if (grp
->qfg_S
== roundedS
) {
924 grp
->qfg_S
= roundedS
;
925 grp
->qfg_F
= roundedS
+ (2ULL << grp
->qfg_slot_shift
);
927 /* remove from ER and put in the new set */
928 pktsched_bit_clr(grp
->qfg_index
, &qif
->qif_bitmaps
[ER
]);
929 s
= qfq_calc_state(qif
, grp
);
930 pktsched_bit_set(grp
->qfg_index
, &qif
->qif_bitmaps
[s
]);
932 /* we need to unblock even if the group has gone away */
933 qfq_unblock_groups(qif
, grp
->qfg_index
, old_F
);
937 qfq_update_eligible(qif
, old_V
);
940 if (!qif
->qif_bitmaps
[ER
] && qif
->qif_queued
&& pktsched_verbose
> 1) {
941 qfq_dump_sched(qif
, "end dequeue");
943 #endif /* QFQ_DEBUG */
947 * Assign a reasonable start time for a new flow k in group i.
948 * Admissible values for hat(F) are multiples of sigma_i
949 * no greater than V+sigma_i . Larger values mean that
950 * we had a wraparound so we consider the timestamp to be stale.
952 * If F is not stale and F >= V then we set S = F.
953 * Otherwise we should assign S = V, but this may violate
954 * the ordering in ER. So, if we have groups in ER, set S to
955 * the F_j of the first group j which would be blocking us.
956 * We are guaranteed not to move S backward because
957 * otherwise our group i would still be blocked.
960 qfq_update_start(struct qfq_if
*qif
, struct qfq_class
*cl
)
962 pktsched_bitmap_t mask
;
963 u_int64_t limit
, roundedF
;
964 int slot_shift
= cl
->cl_grp
->qfg_slot_shift
;
966 roundedF
= qfq_round_down(cl
->cl_F
, slot_shift
);
967 limit
= qfq_round_down(qif
->qif_V
, slot_shift
) + (1UL << slot_shift
);
969 if (!qfq_gt(cl
->cl_F
, qif
->qif_V
) || qfq_gt(roundedF
, limit
)) {
970 /* timestamp was stale */
971 mask
= mask_from(qif
->qif_bitmaps
[ER
], cl
->cl_grp
->qfg_index
);
973 struct qfq_group
*next
= qfq_ffs(qif
, mask
);
974 if (qfq_gt(roundedF
, next
->qfg_F
)) {
975 cl
->cl_S
= next
->qfg_F
;
979 cl
->cl_S
= qif
->qif_V
;
980 } else { /* timestamp is not stale */
986 qfq_enqueue(struct qfq_if
*qif
, struct qfq_class
*cl
, pktsched_pkt_t
*pkt
,
989 struct ifclassq
*ifq
= qif
->qif_ifq
;
990 struct qfq_group
*grp
;
994 IFCQ_LOCK_ASSERT_HELD(ifq
);
995 VERIFY(cl
== NULL
|| cl
->cl_qif
== qif
);
998 cl
= qfq_clh_to_clp(qif
, 0);
1000 cl
= qif
->qif_default
;
1002 IFCQ_CONVERT_LOCK(ifq
);
1003 return CLASSQEQ_DROP
;
1008 VERIFY(pkt
->pktsched_ptype
== qptype(&cl
->cl_q
));
1009 len
= pktsched_get_pkt_len(pkt
);
1011 ret
= qfq_addq(cl
, pkt
, t
);
1012 if ((ret
!= 0) && (ret
!= CLASSQEQ_SUCCESS_FC
)) {
1013 VERIFY(ret
== CLASSQEQ_DROP
||
1014 ret
== CLASSQEQ_DROP_FC
||
1015 ret
== CLASSQEQ_DROP_SP
);
1016 PKTCNTR_ADD(&cl
->cl_dropcnt
, 1, len
);
1017 IFCQ_DROP_ADD(ifq
, 1, len
);
1021 IFCQ_INC_BYTES(ifq
, len
);
1025 #endif /* QFQ_DEBUG */
1027 /* queue was not idle, we're done */
1028 if (qlen(&cl
->cl_q
) > 1) {
1032 /* queue was idle */
1034 qfq_update_start(qif
, cl
); /* adjust start time */
1036 /* compute new finish time and rounded start */
1037 cl
->cl_F
= cl
->cl_S
+ (u_int64_t
)len
* cl
->cl_inv_w
;
1038 roundedS
= qfq_round_down(cl
->cl_S
, grp
->qfg_slot_shift
);
1041 * Insert cl in the correct bucket.
1043 * If cl->cl_S >= grp->qfg_S we don't need to adjust the bucket list
1044 * and simply go to the insertion phase. Otherwise grp->qfg_S is
1045 * decreasing, we must make room in the bucket list, and also
1046 * recompute the group state. Finally, if there were no flows
1047 * in this group and nobody was in ER make sure to adjust V.
1049 if (grp
->qfg_full_slots
!= 0) {
1050 if (!qfq_gt(grp
->qfg_S
, cl
->cl_S
)) {
1054 /* create a slot for this cl->cl_S */
1055 qfq_slot_rotate(qif
, grp
, roundedS
);
1057 /* group was surely ineligible, remove */
1058 pktsched_bit_clr(grp
->qfg_index
, &qif
->qif_bitmaps
[IR
]);
1059 pktsched_bit_clr(grp
->qfg_index
, &qif
->qif_bitmaps
[IB
]);
1060 } else if (!qif
->qif_bitmaps
[ER
] && qfq_gt(roundedS
, qif
->qif_V
)) {
1061 qif
->qif_V
= roundedS
;
1064 grp
->qfg_S
= roundedS
;
1066 roundedS
+ (2ULL << grp
->qfg_slot_shift
); /* i.e. 2 sigma_i */
1067 s
= qfq_calc_state(qif
, grp
);
1068 pktsched_bit_set(grp
->qfg_index
, &qif
->qif_bitmaps
[s
]);
1070 if (pktsched_verbose
> 2) {
1071 log(LOG_DEBUG
, "%s: %s qid=%d enqueue m=0x%llx state=%s 0x%x "
1072 "S=0x%llx F=0x%llx V=0x%llx\n", if_name(QFQIF_IFP(qif
)),
1073 qfq_style(qif
), cl
->cl_handle
,
1074 (uint64_t)VM_KERNEL_ADDRPERM(pkt
->pktsched_pkt
),
1076 qif
->qif_bitmaps
[s
], cl
->cl_S
, cl
->cl_F
, qif
->qif_V
);
1080 qfq_slot_insert(qif
, grp
, cl
, roundedS
);
1083 /* successfully queued. */
1088 qfq_slot_remove(struct qfq_if
*qif
, struct qfq_group
*grp
,
1089 struct qfq_class
*cl
)
1092 struct qfq_class
**pprev
;
1093 u_int32_t i
, offset
;
1096 roundedS
= qfq_round_down(cl
->cl_S
, grp
->qfg_slot_shift
);
1097 offset
= (roundedS
- grp
->qfg_S
) >> grp
->qfg_slot_shift
;
1098 i
= (grp
->qfg_front
+ offset
) % qif
->qif_maxslots
;
1100 pprev
= &grp
->qfg_slots
[i
];
1101 while (*pprev
&& *pprev
!= cl
) {
1102 pprev
= &(*pprev
)->cl_next
;
1105 *pprev
= cl
->cl_next
;
1106 if (!grp
->qfg_slots
[i
]) {
1107 pktsched_bit_clr(offset
, &grp
->qfg_full_slots
);
1112 * Called to forcibly destroy a queue.
1113 * If the queue is not in the front bucket, or if it has
1114 * other queues in the front bucket, we can simply remove
1115 * the queue with no other side effects.
1116 * Otherwise we must propagate the event up.
1117 * XXX description to be completed.
1120 qfq_deactivate_class(struct qfq_if
*qif
, struct qfq_class
*cl
)
1122 struct qfq_group
*grp
= cl
->cl_grp
;
1123 pktsched_bitmap_t mask
;
1127 if (pktsched_verbose
) {
1128 log(LOG_DEBUG
, "%s: %s deactivate qid=%d grp=%d "
1129 "full_slots=0x%x front=%d bitmaps={ER=0x%x,EB=0x%x,"
1130 "IR=0x%x,IB=0x%x}\n",
1131 if_name(QFQIF_IFP(cl
->cl_qif
)), qfq_style(cl
->cl_qif
),
1132 cl
->cl_handle
, grp
->qfg_index
, grp
->qfg_full_slots
,
1133 grp
->qfg_front
, qif
->qif_bitmaps
[ER
], qif
->qif_bitmaps
[EB
],
1134 qif
->qif_bitmaps
[IR
], qif
->qif_bitmaps
[IB
]);
1136 if (pktsched_verbose
> 1) {
1137 qfq_dump_sched(qif
, "start deactivate");
1139 #endif /* QFQ_DEBUG */
1142 cl
->cl_F
= cl
->cl_S
; /* not needed if the class goes away */
1143 qfq_slot_remove(qif
, grp
, cl
);
1145 if (grp
->qfg_full_slots
== 0) {
1147 * Nothing left in the group, remove from all sets.
1148 * Do ER last because if we were blocking other groups
1149 * we must unblock them.
1151 pktsched_bit_clr(grp
->qfg_index
, &qif
->qif_bitmaps
[IR
]);
1152 pktsched_bit_clr(grp
->qfg_index
, &qif
->qif_bitmaps
[EB
]);
1153 pktsched_bit_clr(grp
->qfg_index
, &qif
->qif_bitmaps
[IB
]);
1155 if (pktsched_bit_tst(grp
->qfg_index
, &qif
->qif_bitmaps
[ER
]) &&
1156 !(qif
->qif_bitmaps
[ER
] & ~((1UL << grp
->qfg_index
) - 1))) {
1157 mask
= qif
->qif_bitmaps
[ER
] &
1158 ((1UL << grp
->qfg_index
) - 1);
1160 mask
= ~((1UL << __fls(mask
)) - 1);
1162 mask
= (pktsched_bitmap_t
)~0UL;
1164 qfq_move_groups(qif
, mask
, EB
, ER
);
1165 qfq_move_groups(qif
, mask
, IB
, IR
);
1167 pktsched_bit_clr(grp
->qfg_index
, &qif
->qif_bitmaps
[ER
]);
1168 } else if (!grp
->qfg_slots
[grp
->qfg_front
]) {
1169 cl
= qfq_slot_scan(qif
, grp
);
1170 roundedS
= qfq_round_down(cl
->cl_S
, grp
->qfg_slot_shift
);
1171 if (grp
->qfg_S
!= roundedS
) {
1172 pktsched_bit_clr(grp
->qfg_index
, &qif
->qif_bitmaps
[ER
]);
1173 pktsched_bit_clr(grp
->qfg_index
, &qif
->qif_bitmaps
[IR
]);
1174 pktsched_bit_clr(grp
->qfg_index
, &qif
->qif_bitmaps
[EB
]);
1175 pktsched_bit_clr(grp
->qfg_index
, &qif
->qif_bitmaps
[IB
]);
1176 grp
->qfg_S
= roundedS
;
1177 grp
->qfg_F
= roundedS
+ (2ULL << grp
->qfg_slot_shift
);
1178 s
= qfq_calc_state(qif
, grp
);
1179 pktsched_bit_set(grp
->qfg_index
, &qif
->qif_bitmaps
[s
]);
1182 qfq_update_eligible(qif
, qif
->qif_V
);
1185 if (pktsched_verbose
> 1) {
1186 qfq_dump_sched(qif
, "end deactivate");
1188 #endif /* QFQ_DEBUG */
1192 qfq_state2str(int s
)
1217 qfq_addq(struct qfq_class
*cl
, pktsched_pkt_t
*pkt
, struct pf_mtag
*t
)
1219 struct qfq_if
*qif
= cl
->cl_qif
;
1220 struct ifclassq
*ifq
= qif
->qif_ifq
;
1222 IFCQ_LOCK_ASSERT_HELD(ifq
);
1224 if (q_is_sfb(&cl
->cl_q
)) {
1225 if (cl
->cl_sfb
== NULL
) {
1226 struct ifnet
*ifp
= QFQIF_IFP(qif
);
1228 VERIFY(cl
->cl_flags
& QFCF_LAZY
);
1229 cl
->cl_flags
&= ~QFCF_LAZY
;
1231 IFCQ_CONVERT_LOCK(ifq
);
1232 cl
->cl_sfb
= sfb_alloc(ifp
, cl
->cl_handle
,
1233 qlimit(&cl
->cl_q
), cl
->cl_qflags
);
1234 if (cl
->cl_sfb
== NULL
) {
1235 /* fall back to droptail */
1236 qtype(&cl
->cl_q
) = Q_DROPTAIL
;
1237 cl
->cl_flags
&= ~QFCF_SFB
;
1238 cl
->cl_qflags
&= ~(SFBF_ECN
| SFBF_FLOWCTL
);
1240 log(LOG_ERR
, "%s: %s SFB lazy allocation "
1241 "failed for qid=%d grp=%d, falling back "
1242 "to DROPTAIL\n", if_name(ifp
),
1243 qfq_style(qif
), cl
->cl_handle
,
1244 cl
->cl_grp
->qfg_index
);
1245 } else if (qif
->qif_throttle
!= IFNET_THROTTLE_OFF
) {
1246 /* if there's pending throttling, set it */
1247 cqrq_throttle_t tr
= { 1, qif
->qif_throttle
};
1248 int err
= qfq_throttle(qif
, &tr
);
1250 if (err
== EALREADY
) {
1254 tr
.level
= IFNET_THROTTLE_OFF
;
1255 (void) qfq_throttle(qif
, &tr
);
1259 if (cl
->cl_sfb
!= NULL
) {
1260 return sfb_addq(cl
->cl_sfb
, &cl
->cl_q
, pkt
, t
);
1262 } else if (qlen(&cl
->cl_q
) >= qlimit(&cl
->cl_q
)) {
1263 IFCQ_CONVERT_LOCK(ifq
);
1264 return CLASSQEQ_DROP
;
1268 if (cl
->cl_flags
& QFCF_CLEARDSCP
) {
1269 /* not supported for non-mbuf type packets */
1270 VERIFY(pkt
->pktsched_ptype
== QP_MBUF
);
1271 write_dsfield(m
, t
, 0);
1275 VERIFY(pkt
->pktsched_ptype
== qptype(&cl
->cl_q
));
1276 _addq(&cl
->cl_q
, pkt
->pktsched_pkt
);
1281 qfq_getq(struct qfq_class
*cl
, pktsched_pkt_t
*pkt
)
1283 IFCQ_LOCK_ASSERT_HELD(cl
->cl_qif
->qif_ifq
);
1285 if (q_is_sfb(&cl
->cl_q
) && cl
->cl_sfb
!= NULL
) {
1286 return sfb_getq(cl
->cl_sfb
, &cl
->cl_q
, pkt
);
1289 return pktsched_pkt_encap(pkt
, qptype(&cl
->cl_q
), _getq(&cl
->cl_q
));
1293 qfq_purgeq(struct qfq_if
*qif
, struct qfq_class
*cl
, u_int32_t flow
,
1294 u_int32_t
*packets
, u_int32_t
*bytes
)
1296 struct ifclassq
*ifq
= qif
->qif_ifq
;
1297 u_int32_t cnt
= 0, len
= 0, qlen
;
1299 IFCQ_LOCK_ASSERT_HELD(ifq
);
1301 if ((qlen
= qlen(&cl
->cl_q
)) == 0) {
1305 IFCQ_CONVERT_LOCK(ifq
);
1306 if (q_is_sfb(&cl
->cl_q
) && cl
->cl_sfb
!= NULL
) {
1307 sfb_purgeq(cl
->cl_sfb
, &cl
->cl_q
, flow
, &cnt
, &len
);
1309 _flushq_flow(&cl
->cl_q
, flow
, &cnt
, &len
);
1313 VERIFY(qlen(&cl
->cl_q
) == (qlen
- cnt
));
1315 VERIFY(qif
->qif_queued
>= cnt
);
1316 qif
->qif_queued
-= cnt
;
1317 #endif /* QFQ_DEBUG */
1319 PKTCNTR_ADD(&cl
->cl_dropcnt
, cnt
, len
);
1320 IFCQ_DROP_ADD(ifq
, cnt
, len
);
1322 VERIFY(((signed)IFCQ_LEN(ifq
) - cnt
) >= 0);
1323 IFCQ_LEN(ifq
) -= cnt
;
1325 if (qempty(&cl
->cl_q
)) {
1326 qfq_deactivate_class(qif
, cl
);
1329 if (pktsched_verbose
) {
1330 log(LOG_DEBUG
, "%s: %s purge qid=%d weight=%d "
1331 "qlen=[%d,%d] cnt=%d len=%d flow=0x%x\n",
1332 if_name(QFQIF_IFP(qif
)),
1333 qfq_style(qif
), cl
->cl_handle
,
1334 (u_int32_t
)(QFQ_ONE_FP
/ cl
->cl_inv_w
), qlen
,
1335 qlen(&cl
->cl_q
), cnt
, len
, flow
);
1339 if (packets
!= NULL
) {
1342 if (bytes
!= NULL
) {
1348 qfq_updateq(struct qfq_if
*qif
, struct qfq_class
*cl
, cqev_t ev
)
1350 IFCQ_LOCK_ASSERT_HELD(qif
->qif_ifq
);
1352 if (pktsched_verbose
) {
1353 log(LOG_DEBUG
, "%s: %s update qid=%d weight=%d event=%s\n",
1354 if_name(QFQIF_IFP(qif
)), qfq_style(qif
),
1355 cl
->cl_handle
, (u_int32_t
)(QFQ_ONE_FP
/ cl
->cl_inv_w
),
1356 ifclassq_ev2str(ev
));
1359 if (q_is_sfb(&cl
->cl_q
) && cl
->cl_sfb
!= NULL
) {
1360 return sfb_updateq(cl
->cl_sfb
, ev
);
1365 qfq_get_class_stats(struct qfq_if
*qif
, u_int32_t qid
,
1366 struct qfq_classstats
*sp
)
1368 struct qfq_class
*cl
;
1370 IFCQ_LOCK_ASSERT_HELD(qif
->qif_ifq
);
1372 if ((cl
= qfq_clh_to_clp(qif
, qid
)) == NULL
) {
1376 sp
->class_handle
= cl
->cl_handle
;
1377 sp
->index
= cl
->cl_grp
->qfg_index
;
1378 sp
->weight
= (QFQ_ONE_FP
/ cl
->cl_inv_w
);
1379 sp
->lmax
= cl
->cl_lmax
;
1380 sp
->qlength
= qlen(&cl
->cl_q
);
1381 sp
->qlimit
= qlimit(&cl
->cl_q
);
1382 sp
->period
= cl
->cl_period
;
1383 sp
->xmitcnt
= cl
->cl_xmitcnt
;
1384 sp
->dropcnt
= cl
->cl_dropcnt
;
1386 sp
->qtype
= qtype(&cl
->cl_q
);
1387 sp
->qstate
= qstate(&cl
->cl_q
);
1389 if (q_is_sfb(&cl
->cl_q
) && cl
->cl_sfb
!= NULL
) {
1390 sfb_getstats(cl
->cl_sfb
, &sp
->sfb
);
1397 qfq_stat_sc(struct qfq_if
*qif
, cqrq_stat_sc_t
*sr
)
1399 struct ifclassq
*ifq
= qif
->qif_ifq
;
1400 struct qfq_class
*cl
;
1403 IFCQ_LOCK_ASSERT_HELD(ifq
);
1405 VERIFY(sr
->sc
== MBUF_SC_UNSPEC
|| MBUF_VALID_SC(sr
->sc
));
1407 i
= MBUF_SCIDX(sr
->sc
);
1408 VERIFY(i
< IFCQ_SC_MAX
);
1410 cl
= ifq
->ifcq_disc_slots
[i
].cl
;
1411 sr
->packets
= qlen(&cl
->cl_q
);
1412 sr
->bytes
= qsize(&cl
->cl_q
);
1417 /* convert a class handle to the corresponding class pointer */
1418 static inline struct qfq_class
*
1419 qfq_clh_to_clp(struct qfq_if
*qif
, u_int32_t chandle
)
1421 struct qfq_class
*cl
;
1424 IFCQ_LOCK_ASSERT_HELD(qif
->qif_ifq
);
1427 * First, try optimistically the slot matching the lower bits of
1428 * the handle. If it fails, do the linear table search.
1430 i
= chandle
% qif
->qif_maxclasses
;
1431 if ((cl
= qif
->qif_class_tbl
[i
]) != NULL
&& cl
->cl_handle
== chandle
) {
1434 for (i
= 0; i
< qif
->qif_maxclasses
; i
++) {
1435 if ((cl
= qif
->qif_class_tbl
[i
]) != NULL
&&
1436 cl
->cl_handle
== chandle
) {
1445 qfq_style(struct qfq_if
*qif
)
1452 * Generic comparison function, handling wraparound
1455 qfq_gt(u_int64_t a
, u_int64_t b
)
1457 return (int64_t)(a
- b
) > 0;
1461 * Round a precise timestamp to its slotted value
1463 static inline u_int64_t
1464 qfq_round_down(u_int64_t ts
, u_int32_t shift
)
1466 return ts
& ~((1ULL << shift
) - 1);
1470 * Return the pointer to the group with lowest index in the bitmap
1472 static inline struct qfq_group
*
1473 qfq_ffs(struct qfq_if
*qif
, pktsched_bitmap_t bitmap
)
1475 int index
= pktsched_ffs(bitmap
) - 1; /* zero-based */
1476 VERIFY(index
>= 0 && index
<= QFQ_MAX_INDEX
&&
1477 qif
->qif_groups
[index
] != NULL
);
1478 return qif
->qif_groups
[index
];
1482 * Calculate a flow index, given its weight and maximum packet length.
1483 * index = log_2(maxlen/weight) but we need to apply the scaling.
1484 * This is used only once at flow creation.
1487 qfq_calc_index(struct qfq_class
*cl
, u_int32_t inv_w
, u_int32_t maxlen
)
1489 u_int64_t slot_size
= (u_int64_t
)maxlen
* inv_w
;
1490 pktsched_bitmap_t size_map
;
1493 size_map
= (pktsched_bitmap_t
)(slot_size
>> QFQ_MIN_SLOT_SHIFT
);
1498 index
= __fls(size_map
) + 1; /* basically a log_2() */
1499 index
-= !(slot_size
- (1ULL << (index
+ QFQ_MIN_SLOT_SHIFT
- 1)));
1505 if (pktsched_verbose
) {
1506 log(LOG_DEBUG
, "%s: %s qid=%d grp=%d W=%u, L=%u, I=%d\n",
1507 if_name(QFQIF_IFP(cl
->cl_qif
)), qfq_style(cl
->cl_qif
),
1508 cl
->cl_handle
, index
, (u_int32_t
)(QFQ_ONE_FP
/ inv_w
),
1516 qfq_dump_groups(struct qfq_if
*qif
, u_int32_t mask
)
1520 for (i
= 0; i
< QFQ_MAX_INDEX
+ 1; i
++) {
1521 struct qfq_group
*g
= qif
->qif_groups
[i
];
1523 if (0 == (mask
& (1 << i
))) {
1530 log(LOG_DEBUG
, "%s: %s [%2d] full_slots 0x%x\n",
1531 if_name(QFQIF_IFP(qif
)), qfq_style(qif
), i
,
1533 log(LOG_DEBUG
, "%s: %s S 0x%20llx F 0x%llx %c\n",
1534 if_name(QFQIF_IFP(qif
)), qfq_style(qif
),
1535 g
->qfg_S
, g
->qfg_F
, mask
& (1 << i
) ? '1' : '0');
1537 for (j
= 0; j
< qif
->qif_maxslots
; j
++) {
1538 if (g
->qfg_slots
[j
]) {
1539 log(LOG_DEBUG
, "%s: %s bucket %d 0x%llx "
1540 "qid %d\n", if_name(QFQIF_IFP(qif
)),
1542 (uint64_t)VM_KERNEL_ADDRPERM(
1544 g
->qfg_slots
[j
]->cl_handle
);
1551 qfq_dump_sched(struct qfq_if
*qif
, const char *msg
)
1553 log(LOG_DEBUG
, "%s: %s --- in %s: ---\n",
1554 if_name(QFQIF_IFP(qif
)), qfq_style(qif
), msg
);
1555 log(LOG_DEBUG
, "%s: %s emptygrp %d queued %d V 0x%llx\n",
1556 if_name(QFQIF_IFP(qif
)), qfq_style(qif
), qif
->qif_emptygrp
,
1557 qif
->qif_queued
, qif
->qif_V
);
1558 log(LOG_DEBUG
, "%s: %s ER 0x%08x\n",
1559 if_name(QFQIF_IFP(qif
)), qfq_style(qif
), qif
->qif_bitmaps
[ER
]);
1560 log(LOG_DEBUG
, "%s: %s EB 0x%08x\n",
1561 if_name(QFQIF_IFP(qif
)), qfq_style(qif
), qif
->qif_bitmaps
[EB
]);
1562 log(LOG_DEBUG
, "%s: %s IR 0x%08x\n",
1563 if_name(QFQIF_IFP(qif
)), qfq_style(qif
), qif
->qif_bitmaps
[IR
]);
1564 log(LOG_DEBUG
, "%s: %s IB 0x%08x\n",
1565 if_name(QFQIF_IFP(qif
)), qfq_style(qif
), qif
->qif_bitmaps
[IB
]);
1566 qfq_dump_groups(qif
, 0xffffffff);
1568 #endif /* QFQ_DEBUG */
1571 * qfq_enqueue_ifclassq is an enqueue function to be registered to
1572 * (*ifcq_enqueue) in struct ifclassq.
1575 qfq_enqueue_ifclassq(struct ifclassq
*ifq
, void *p
, classq_pkt_type_t ptype
,
1581 struct pf_mtag
*t
= NULL
;
1583 IFCQ_LOCK_ASSERT_HELD(ifq
);
1588 if (!(m
->m_flags
& M_PKTHDR
)) {
1589 /* should not happen */
1590 log(LOG_ERR
, "%s: packet does not have pkthdr\n",
1591 if_name(ifq
->ifcq_ifp
));
1592 IFCQ_CONVERT_LOCK(ifq
);
1597 i
= MBUF_SCIDX(mbuf_get_service_class(m
));
1608 VERIFY((u_int32_t
)i
< IFCQ_SC_MAX
);
1610 pktsched_pkt_encap(&pkt
, ptype
, p
);
1612 ret
= qfq_enqueue(ifq
->ifcq_disc
,
1613 ifq
->ifcq_disc_slots
[i
].cl
, &pkt
, t
);
1615 if ((ret
!= 0) && (ret
!= CLASSQEQ_SUCCESS_FC
)) {
1616 pktsched_free_pkt(&pkt
);
1626 case CLASSQEQ_DROP_FC
:
1629 case CLASSQEQ_DROP_SP
:
1632 case CLASSQEQ_SUCCESS_FC
:
1635 case CLASSQEQ_SUCCESS
:
1645 * qfq_dequeue_ifclassq is a dequeue function to be registered to
1646 * (*ifcq_dequeue) in struct ifclass.
1648 * note: CLASSQDQ_POLL returns the next packet without removing the packet
1649 * from the queue. CLASSQDQ_REMOVE is a normal dequeue operation.
1650 * CLASSQDQ_REMOVE must return the same packet if called immediately
1651 * after CLASSQDQ_POLL.
1654 qfq_dequeue_ifclassq(struct ifclassq
*ifq
, classq_pkt_type_t
*ptype
)
1657 bzero(&pkt
, sizeof(pkt
));
1658 qfq_dequeue(ifq
->ifcq_disc
, &pkt
);
1659 *ptype
= pkt
.pktsched_ptype
;
1660 return pkt
.pktsched_pkt
;
1664 qfq_request_ifclassq(struct ifclassq
*ifq
, cqrq_t req
, void *arg
)
1666 struct qfq_if
*qif
= (struct qfq_if
*)ifq
->ifcq_disc
;
1669 IFCQ_LOCK_ASSERT_HELD(ifq
);
1672 case CLASSQRQ_PURGE
:
1676 case CLASSQRQ_PURGE_SC
:
1677 qfq_purge_sc(qif
, (cqrq_purge_sc_t
*)arg
);
1680 case CLASSQRQ_EVENT
:
1681 qfq_event(qif
, (cqev_t
)arg
);
1684 case CLASSQRQ_THROTTLE
:
1685 err
= qfq_throttle(qif
, (cqrq_throttle_t
*)arg
);
1687 case CLASSQRQ_STAT_SC
:
1688 err
= qfq_stat_sc(qif
, (cqrq_stat_sc_t
*)arg
);
1695 qfq_setup_ifclassq(struct ifclassq
*ifq
, u_int32_t flags
,
1696 classq_pkt_type_t ptype
)
1698 struct ifnet
*ifp
= ifq
->ifcq_ifp
;
1699 struct qfq_class
*cl0
, *cl1
, *cl2
, *cl3
, *cl4
;
1700 struct qfq_class
*cl5
, *cl6
, *cl7
, *cl8
, *cl9
;
1702 u_int32_t maxlen
= 0, qflags
= 0;
1705 IFCQ_LOCK_ASSERT_HELD(ifq
);
1706 VERIFY(ifq
->ifcq_disc
== NULL
);
1707 VERIFY(ifq
->ifcq_type
== PKTSCHEDT_NONE
);
1709 if (flags
& PKTSCHEDF_QALG_SFB
) {
1712 if (flags
& PKTSCHEDF_QALG_ECN
) {
1715 if (flags
& PKTSCHEDF_QALG_FLOWCTL
) {
1716 qflags
|= QFCF_FLOWCTL
;
1718 if (flags
& PKTSCHEDF_QALG_DELAYBASED
) {
1719 qflags
|= QFCF_DELAYBASED
;
1722 qif
= qfq_alloc(ifp
, M_WAITOK
);
1727 if ((maxlen
= IFCQ_MAXLEN(ifq
)) == 0) {
1728 maxlen
= if_sndq_maxlen
;
1731 if ((err
= qfq_add_queue(qif
, maxlen
, 300, 1200,
1732 qflags
| QFCF_LAZY
, SCIDX_BK_SYS
, &cl0
, ptype
)) != 0) {
1736 if ((err
= qfq_add_queue(qif
, maxlen
, 600, 1400,
1737 qflags
| QFCF_LAZY
, SCIDX_BK
, &cl1
, ptype
)) != 0) {
1741 if ((err
= qfq_add_queue(qif
, maxlen
, 2400, 600,
1742 qflags
| QFCF_DEFAULTCLASS
, SCIDX_BE
, &cl2
, ptype
)) != 0) {
1746 if ((err
= qfq_add_queue(qif
, maxlen
, 2700, 600,
1747 qflags
| QFCF_LAZY
, SCIDX_RD
, &cl3
, ptype
)) != 0) {
1751 if ((err
= qfq_add_queue(qif
, maxlen
, 3000, 400,
1752 qflags
| QFCF_LAZY
, SCIDX_OAM
, &cl4
, ptype
)) != 0) {
1756 if ((err
= qfq_add_queue(qif
, maxlen
, 8000, 1000,
1757 qflags
| QFCF_LAZY
, SCIDX_AV
, &cl5
, ptype
)) != 0) {
1761 if ((err
= qfq_add_queue(qif
, maxlen
, 15000, 1200,
1762 qflags
| QFCF_LAZY
, SCIDX_RV
, &cl6
, ptype
)) != 0) {
1766 if ((err
= qfq_add_queue(qif
, maxlen
, 20000, 1400,
1767 qflags
| QFCF_LAZY
, SCIDX_VI
, &cl7
, ptype
)) != 0) {
1771 if ((err
= qfq_add_queue(qif
, maxlen
, 23000, 200,
1772 qflags
| QFCF_LAZY
, SCIDX_VO
, &cl8
, ptype
)) != 0) {
1776 if ((err
= qfq_add_queue(qif
, maxlen
, 25000, 200,
1777 qflags
, SCIDX_CTL
, &cl9
, ptype
)) != 0) {
1781 err
= ifclassq_attach(ifq
, PKTSCHEDT_QFQ
, qif
,
1782 qfq_enqueue_ifclassq
, qfq_dequeue_ifclassq
, NULL
,
1783 NULL
, NULL
, qfq_request_ifclassq
);
1785 /* cache these for faster lookup */
1787 ifq
->ifcq_disc_slots
[SCIDX_BK_SYS
].qid
= SCIDX_BK_SYS
;
1788 ifq
->ifcq_disc_slots
[SCIDX_BK_SYS
].cl
= cl0
;
1790 ifq
->ifcq_disc_slots
[SCIDX_BK
].qid
= SCIDX_BK
;
1791 ifq
->ifcq_disc_slots
[SCIDX_BK
].cl
= cl1
;
1793 ifq
->ifcq_disc_slots
[SCIDX_BE
].qid
= SCIDX_BE
;
1794 ifq
->ifcq_disc_slots
[SCIDX_BE
].cl
= cl2
;
1796 ifq
->ifcq_disc_slots
[SCIDX_RD
].qid
= SCIDX_RD
;
1797 ifq
->ifcq_disc_slots
[SCIDX_RD
].cl
= cl3
;
1799 ifq
->ifcq_disc_slots
[SCIDX_OAM
].qid
= SCIDX_OAM
;
1800 ifq
->ifcq_disc_slots
[SCIDX_OAM
].cl
= cl4
;
1802 ifq
->ifcq_disc_slots
[SCIDX_AV
].qid
= SCIDX_AV
;
1803 ifq
->ifcq_disc_slots
[SCIDX_AV
].cl
= cl5
;
1805 ifq
->ifcq_disc_slots
[SCIDX_RV
].qid
= SCIDX_RV
;
1806 ifq
->ifcq_disc_slots
[SCIDX_RV
].cl
= cl6
;
1808 ifq
->ifcq_disc_slots
[SCIDX_VI
].qid
= SCIDX_VI
;
1809 ifq
->ifcq_disc_slots
[SCIDX_VI
].cl
= cl7
;
1811 ifq
->ifcq_disc_slots
[SCIDX_VO
].qid
= SCIDX_VO
;
1812 ifq
->ifcq_disc_slots
[SCIDX_VO
].cl
= cl8
;
1814 ifq
->ifcq_disc_slots
[SCIDX_CTL
].qid
= SCIDX_CTL
;
1815 ifq
->ifcq_disc_slots
[SCIDX_CTL
].cl
= cl9
;
1820 (void) qfq_destroy_locked(qif
);
1827 qfq_teardown_ifclassq(struct ifclassq
*ifq
)
1829 struct qfq_if
*qif
= ifq
->ifcq_disc
;
1832 IFCQ_LOCK_ASSERT_HELD(ifq
);
1833 VERIFY(qif
!= NULL
&& ifq
->ifcq_type
== PKTSCHEDT_QFQ
);
1835 (void) qfq_destroy_locked(qif
);
1837 ifq
->ifcq_disc
= NULL
;
1838 for (i
= 0; i
< IFCQ_SC_MAX
; i
++) {
1839 ifq
->ifcq_disc_slots
[i
].qid
= 0;
1840 ifq
->ifcq_disc_slots
[i
].cl
= NULL
;
1843 return ifclassq_detach(ifq
);
1847 qfq_getqstats_ifclassq(struct ifclassq
*ifq
, u_int32_t slot
,
1848 struct if_ifclassq_stats
*ifqs
)
1850 struct qfq_if
*qif
= ifq
->ifcq_disc
;
1852 IFCQ_LOCK_ASSERT_HELD(ifq
);
1853 VERIFY(ifq
->ifcq_type
== PKTSCHEDT_QFQ
);
1855 if (slot
>= IFCQ_SC_MAX
) {
1859 return qfq_get_class_stats(qif
, ifq
->ifcq_disc_slots
[slot
].qid
,
1860 &ifqs
->ifqs_qfq_stats
);
1864 qfq_throttle(struct qfq_if
*qif
, cqrq_throttle_t
*tr
)
1866 struct ifclassq
*ifq
= qif
->qif_ifq
;
1867 struct qfq_class
*cl
;
1870 IFCQ_LOCK_ASSERT_HELD(ifq
);
1873 tr
->level
= qif
->qif_throttle
;
1877 if (tr
->level
== qif
->qif_throttle
) {
1881 /* Current throttling levels only involve BK_SYS class */
1882 cl
= ifq
->ifcq_disc_slots
[SCIDX_BK_SYS
].cl
;
1884 switch (tr
->level
) {
1885 case IFNET_THROTTLE_OFF
:
1886 err
= qfq_resumeq(qif
, cl
);
1889 case IFNET_THROTTLE_OPPORTUNISTIC
:
1890 err
= qfq_suspendq(qif
, cl
);
1898 if (err
== 0 || err
== ENXIO
) {
1899 if (pktsched_verbose
) {
1900 log(LOG_DEBUG
, "%s: %s throttling level %sset %d->%d\n",
1901 if_name(QFQIF_IFP(qif
)), qfq_style(qif
),
1902 (err
== 0) ? "" : "lazy ", qif
->qif_throttle
,
1905 qif
->qif_throttle
= tr
->level
;
1909 qfq_purgeq(qif
, cl
, 0, NULL
, NULL
);
1912 log(LOG_ERR
, "%s: %s unable to set throttling level "
1913 "%d->%d [error=%d]\n", if_name(QFQIF_IFP(qif
)),
1914 qfq_style(qif
), qif
->qif_throttle
, tr
->level
, err
);
1921 qfq_resumeq(struct qfq_if
*qif
, struct qfq_class
*cl
)
1923 struct ifclassq
*ifq
= qif
->qif_ifq
;
1928 IFCQ_LOCK_ASSERT_HELD(ifq
);
1930 if (q_is_sfb(&cl
->cl_q
) && cl
->cl_sfb
!= NULL
) {
1931 err
= sfb_suspendq(cl
->cl_sfb
, &cl
->cl_q
, FALSE
);
1935 qstate(&cl
->cl_q
) = QS_RUNNING
;
1942 qfq_suspendq(struct qfq_if
*qif
, struct qfq_class
*cl
)
1944 struct ifclassq
*ifq
= qif
->qif_ifq
;
1949 IFCQ_LOCK_ASSERT_HELD(ifq
);
1951 if (q_is_sfb(&cl
->cl_q
)) {
1952 if (cl
->cl_sfb
!= NULL
) {
1953 err
= sfb_suspendq(cl
->cl_sfb
, &cl
->cl_q
, TRUE
);
1955 VERIFY(cl
->cl_flags
& QFCF_LAZY
);
1956 err
= ENXIO
; /* delayed throttling */
1960 if (err
== 0 || err
== ENXIO
) {
1961 qstate(&cl
->cl_q
) = QS_SUSPENDED
;