| 1 | /* | 
|---|
| 2 | * Copyright (c) 2007-2021 Apple Inc. All rights reserved. | 
|---|
| 3 | * | 
|---|
| 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | 
|---|
| 5 | * | 
|---|
| 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. | 
|---|
| 14 | * | 
|---|
| 15 | * Please obtain a copy of the License at | 
|---|
| 16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | 
|---|
| 17 | * | 
|---|
| 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. | 
|---|
| 25 | * | 
|---|
| 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | /*	$NetBSD: altq_classq.h,v 1.7 2006/10/12 19:59:08 peter Exp $	*/ | 
|---|
| 30 | /*	$KAME: altq_classq.h,v 1.6 2003/01/07 07:33:38 kjc Exp $	*/ | 
|---|
| 31 |  | 
|---|
| 32 | /* | 
|---|
| 33 | * Copyright (c) 1991-1997 Regents of the University of California. | 
|---|
| 34 | * All rights reserved. | 
|---|
| 35 | * | 
|---|
| 36 | * Redistribution and use in source and binary forms, with or without | 
|---|
| 37 | * modification, are permitted provided that the following conditions | 
|---|
| 38 | * are met: | 
|---|
| 39 | * 1. Redistributions of source code must retain the above copyright | 
|---|
| 40 | *    notice, this list of conditions and the following disclaimer. | 
|---|
| 41 | * 2. Redistributions in binary form must reproduce the above copyright | 
|---|
| 42 | *    notice, this list of conditions and the following disclaimer in the | 
|---|
| 43 | *    documentation and/or other materials provided with the distribution. | 
|---|
| 44 | * 3. All advertising materials mentioning features or use of this software | 
|---|
| 45 | *    must display the following acknowledgement: | 
|---|
| 46 | *	This product includes software developed by the Network Research | 
|---|
| 47 | *	Group at Lawrence Berkeley Laboratory. | 
|---|
| 48 | * 4. Neither the name of the University nor of the Laboratory may be used | 
|---|
| 49 | *    to endorse or promote products derived from this software without | 
|---|
| 50 | *    specific prior written permission. | 
|---|
| 51 | * | 
|---|
| 52 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 
|---|
| 53 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
|---|
| 54 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 
|---|
| 55 | * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 
|---|
| 56 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
|---|
| 57 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 
|---|
| 58 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 
|---|
| 59 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 
|---|
| 60 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 
|---|
| 61 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|---|
| 62 | * SUCH DAMAGE. | 
|---|
| 63 | */ | 
|---|
| 64 | /* | 
|---|
| 65 | * class queue definitions extracted from rm_class.h. | 
|---|
| 66 | */ | 
|---|
| 67 | #ifndef _NET_CLASSQ_CLASSQ_H_ | 
|---|
| 68 | #define _NET_CLASSQ_CLASSQ_H_ | 
|---|
| 69 |  | 
|---|
| 70 | #ifdef PRIVATE | 
|---|
| 71 | #ifdef __cplusplus | 
|---|
| 72 | extern "C"{ | 
|---|
| 73 | #endif | 
|---|
| 74 |  | 
|---|
| 75 | /* | 
|---|
| 76 | * Packet types | 
|---|
| 77 | */ | 
|---|
| 78 | typedef enum classq_pkt_type { | 
|---|
| 79 | QP_INVALID = 0, | 
|---|
| 80 | QP_MBUF,        /* mbuf packet */ | 
|---|
| 81 | #if SKYWALK | 
|---|
| 82 | QP_PACKET,      /* skywalk packet */ | 
|---|
| 83 | #endif /* SKYWALK */ | 
|---|
| 84 | } classq_pkt_type_t; | 
|---|
| 85 |  | 
|---|
| 86 | /* | 
|---|
| 87 | * Packet | 
|---|
| 88 | */ | 
|---|
| 89 | typedef struct classq_pkt { | 
|---|
| 90 | union { | 
|---|
| 91 | struct mbuf             *cp_mbuf;       /* mbuf packet */ | 
|---|
| 92 | #if SKYWALK | 
|---|
| 93 | struct __kern_packet    *cp_kpkt;       /* skywalk packet */ | 
|---|
| 94 | #endif /* SKYWALK */ | 
|---|
| 95 | }; | 
|---|
| 96 | classq_pkt_type_t       cp_ptype; | 
|---|
| 97 | } classq_pkt_t; | 
|---|
| 98 |  | 
|---|
| 99 | #define CLASSQ_PKT_INITIALIZER(_p)      \ | 
|---|
| 100 | (classq_pkt_t){ .cp_mbuf = NULL, .cp_ptype = QP_INVALID } | 
|---|
| 101 |  | 
|---|
| 102 | #define CLASSQ_PKT_INIT(_p)    do {    \ | 
|---|
| 103 | (_p)->cp_ptype = QP_INVALID;   \ | 
|---|
| 104 | (_p)->cp_mbuf = NULL;          \ | 
|---|
| 105 | } while (0) | 
|---|
| 106 |  | 
|---|
| 107 | #define CLASSQ_PKT_INIT_MBUF(_p, _m)    do {    \ | 
|---|
| 108 | (_p)->cp_ptype = QP_MBUF;               \ | 
|---|
| 109 | (_p)->cp_mbuf = (_m);                   \ | 
|---|
| 110 | } while (0) | 
|---|
| 111 |  | 
|---|
| 112 | #if SKYWALK | 
|---|
| 113 | #define CLASSQ_PKT_INIT_PACKET(_p, _k)  do {    \ | 
|---|
| 114 | (_p)->cp_ptype = QP_PACKET;             \ | 
|---|
| 115 | (_p)->cp_kpkt = (_k);                   \ | 
|---|
| 116 | } while (0) | 
|---|
| 117 | #endif /* SKYWALK */ | 
|---|
| 118 |  | 
|---|
| 119 | /* | 
|---|
| 120 | * Packet Queue types | 
|---|
| 121 | */ | 
|---|
| 122 | typedef enum classq_type { | 
|---|
| 123 | Q_DROPHEAD, | 
|---|
| 124 | Q_DROPTAIL, | 
|---|
| 125 | Q_SFB | 
|---|
| 126 | } classq_type_t; | 
|---|
| 127 |  | 
|---|
| 128 | /* | 
|---|
| 129 | * Packet Queue states | 
|---|
| 130 | */ | 
|---|
| 131 | typedef enum classq_state { | 
|---|
| 132 | QS_RUNNING, | 
|---|
| 133 | QS_SUSPENDED | 
|---|
| 134 | } classq_state_t; | 
|---|
| 135 |  | 
|---|
| 136 | #define DEFAULT_QLIMIT  128 /* default */ | 
|---|
| 137 |  | 
|---|
| 138 | #define CLASSQ_DEQUEUE_MAX_PKT_LIMIT    2048 | 
|---|
| 139 | #define CLASSQ_DEQUEUE_MAX_BYTE_LIMIT   (1024 * 1024) | 
|---|
| 140 |  | 
|---|
| 141 | /* | 
|---|
| 142 | * generic packet counter | 
|---|
| 143 | */ | 
|---|
| 144 | struct pktcntr { | 
|---|
| 145 | u_int64_t       packets; | 
|---|
| 146 | u_int64_t       bytes; | 
|---|
| 147 | }; | 
|---|
| 148 |  | 
|---|
| 149 | #ifdef BSD_KERNEL_PRIVATE | 
|---|
| 150 | #include <sys/mcache.h> | 
|---|
| 151 | #include <sys/mbuf.h> | 
|---|
| 152 | #include <sys/sysctl.h> | 
|---|
| 153 | #if SKYWALK | 
|---|
| 154 | #include <skywalk/packet/packet_queue.h> | 
|---|
| 155 | #endif /* SKYWALK */ | 
|---|
| 156 |  | 
|---|
| 157 | /* | 
|---|
| 158 | * Packet Queue structures and macros to manipulate them. | 
|---|
| 159 | */ | 
|---|
| 160 | typedef struct _class_queue_ { | 
|---|
| 161 | union { | 
|---|
| 162 | MBUFQ_HEAD(mq_head) __mbufq; /* mbuf packet queue */ | 
|---|
| 163 | #if SKYWALK | 
|---|
| 164 | KPKTQ_HEAD(kq_head) __kpktq; /* skywalk packet queue */ | 
|---|
| 165 | #endif /* SKYWALK */ | 
|---|
| 166 | } __pktq_u; | 
|---|
| 167 | u_int32_t       qlen;   /* Queue length (in number of packets) */ | 
|---|
| 168 | u_int32_t       qlim;   /* Queue limit (in number of packets*) */ | 
|---|
| 169 | u_int64_t       qsize;  /* Approx. queue size (in number of bytes) */ | 
|---|
| 170 | classq_type_t   qtype;  /* Queue type */ | 
|---|
| 171 | classq_state_t  qstate; /* Queue state */ | 
|---|
| 172 | classq_pkt_type_t       qptype; /* Packet type */ | 
|---|
| 173 | } class_queue_t; | 
|---|
| 174 |  | 
|---|
| 175 | #define qmbufq(q)       (q)->__pktq_u.__mbufq   /* Get mbuf packet queue */ | 
|---|
| 176 | #if SKYWALK | 
|---|
| 177 | #define qkpktq(q)       (q)->__pktq_u.__kpktq   /* Get kernel packet queue */ | 
|---|
| 178 | #endif /* SKYWALK */ | 
|---|
| 179 | #define qptype(q)       (q)->qptype             /* Get queue packet type */ | 
|---|
| 180 | #define qtype(q)        (q)->qtype              /* Get queue type */ | 
|---|
| 181 | #define qstate(q)       (q)->qstate             /* Get queue state */ | 
|---|
| 182 | #define qlimit(q)       (q)->qlim               /* Max packets to be queued */ | 
|---|
| 183 | #define qlen(q)         (q)->qlen               /* Current queue length. */ | 
|---|
| 184 | #define qsize(q)        (q)->qsize              /* Approx. bytes in queue */ | 
|---|
| 185 |  | 
|---|
| 186 | #if SKYWALK | 
|---|
| 187 | #define qhead(q)        ((qptype(q) == QP_MBUF) ?               \ | 
|---|
| 188 | (void *)MBUFQ_FIRST(&qmbufq(q)) :   \ | 
|---|
| 189 | (void *)KPKTQ_FIRST(&qkpktq(q))) | 
|---|
| 190 | #else /* !SKYWALK */ | 
|---|
| 191 | #define qhead(q)        MBUFQ_FIRST(&qmbufq(q)) | 
|---|
| 192 | #endif /* !SKYWALK */ | 
|---|
| 193 |  | 
|---|
| 194 | #define qempty(q)       (qlen(q) == 0)  /* Is the queue empty?? */ | 
|---|
| 195 | #define q_is_red(q)     (qtype(q) == Q_RED)     /* Is the queue a RED queue */ | 
|---|
| 196 | #define q_is_rio(q)     (qtype(q) == Q_RIO)     /* Is the queue a RIO queue */ | 
|---|
| 197 | #define q_is_blue(q)    (qtype(q) == Q_BLUE)    /* Is the queue a BLUE queue */ | 
|---|
| 198 | #define q_is_sfb(q)     (qtype(q) == Q_SFB)     /* Is the queue a SFB queue */ | 
|---|
| 199 | #define q_is_red_or_rio(q) (qtype(q) == Q_RED || qtype(q) == Q_RIO) | 
|---|
| 200 | #define q_is_suspended(q) (qstate(q) == QS_SUSPENDED) | 
|---|
| 201 |  | 
|---|
| 202 | #define PKTCNTR_ADD(_cntr, _pkt, _len) do {                             \ | 
|---|
| 203 | (_cntr)->packets += (_pkt);                                     \ | 
|---|
| 204 | (_cntr)->bytes += (_len);                                       \ | 
|---|
| 205 | } while (0) | 
|---|
| 206 |  | 
|---|
| 207 | #define PKTCNTR_CLEAR(_cntr) do {                                       \ | 
|---|
| 208 | (_cntr)->packets = 0;                                           \ | 
|---|
| 209 | (_cntr)->bytes = 0;                                             \ | 
|---|
| 210 | } while (0) | 
|---|
| 211 |  | 
|---|
| 212 | /* flags for mark_ecn() */ | 
|---|
| 213 | #define CLASSQF_ECN4    0x01    /* use packet marking for IPv4 packets */ | 
|---|
| 214 | #define CLASSQF_ECN6    0x02    /* use packet marking for IPv6 packets */ | 
|---|
| 215 | #define CLASSQF_ECN     (CLASSQF_ECN4 | CLASSQF_ECN6) | 
|---|
| 216 |  | 
|---|
| 217 | extern u_int32_t classq_verbose; | 
|---|
| 218 | #if DEBUG || DEVELOPMENT | 
|---|
| 219 | extern uint16_t fq_codel_quantum; | 
|---|
| 220 | #endif /* DEBUG || DEVELOPMENT */ | 
|---|
| 221 |  | 
|---|
| 222 | SYSCTL_DECL(_net_classq); | 
|---|
| 223 |  | 
|---|
| 224 | extern void _qinit(class_queue_t *, int, int, classq_pkt_type_t); | 
|---|
| 225 | extern void _addq(class_queue_t *, classq_pkt_t *); | 
|---|
| 226 | extern void _addq_multi(class_queue_t *, classq_pkt_t *, classq_pkt_t *, | 
|---|
| 227 | u_int32_t, u_int64_t); | 
|---|
| 228 | extern void _getq(class_queue_t *, classq_pkt_t *); | 
|---|
| 229 | extern void _getq_all(class_queue_t *, classq_pkt_t *, classq_pkt_t *, | 
|---|
| 230 | u_int32_t *, u_int64_t *); | 
|---|
| 231 | extern void _getq_tail(class_queue_t *, classq_pkt_t *); | 
|---|
| 232 | extern void _getq_random(class_queue_t *, classq_pkt_t *); | 
|---|
| 233 | extern void _getq_flow(class_queue_t *, classq_pkt_t *, u_int32_t); | 
|---|
| 234 | extern void _getq_scidx_lt(class_queue_t *, classq_pkt_t *, u_int32_t); | 
|---|
| 235 | extern void _removeq(class_queue_t *, classq_pkt_t *); | 
|---|
| 236 | extern void _flushq(class_queue_t *); | 
|---|
| 237 | extern void _flushq_flow(class_queue_t *, u_int32_t, u_int32_t *, u_int32_t *); | 
|---|
| 238 |  | 
|---|
| 239 | extern void classq_init(void); | 
|---|
| 240 |  | 
|---|
| 241 | #if PF_ECN | 
|---|
| 242 | extern u_int8_t read_dsfield(struct mbuf *, struct pf_mtag *); | 
|---|
| 243 | extern void     write_dsfield(struct mbuf *, struct pf_mtag *, u_int8_t); | 
|---|
| 244 | extern int      mark_ecn(struct mbuf *, struct pf_mtag *, int); | 
|---|
| 245 | #endif /* PF_ECN */ | 
|---|
| 246 | #endif /* BSD_KERNEL_PRIVATE */ | 
|---|
| 247 |  | 
|---|
| 248 | #ifdef __cplusplus | 
|---|
| 249 | } | 
|---|
| 250 | #endif | 
|---|
| 251 | #endif /* PRIVATE */ | 
|---|
| 252 | #endif /* _NET_CLASSQ_CLASSQ_H_ */ | 
|---|
| 253 |  | 
|---|