1/* 2 * Copyright (c) 2007-2012 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/* $OpenBSD: altq_priq.c,v 1.21 2007/09/13 20:40:02 chl Exp $ */ 30/* $KAME: altq_priq.c,v 1.1 2000/10/18 09:15:23 kjc Exp $ */ 31 32/* 33 * Copyright (C) 2000-2003 34 * Sony Computer Science Laboratories Inc. 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 * 45 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND 46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 48 * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE 49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 55 * SUCH DAMAGE. 56 */ 57 58/* 59 * priority queue 60 */ 61 62#if PF_ALTQ && PKTSCHED_PRIQ 63 64#include <sys/cdefs.h> 65#include <sys/param.h> 66#include <sys/malloc.h> 67#include <sys/mbuf.h> 68#include <sys/systm.h> 69#include <sys/errno.h> 70#include <sys/kernel.h> 71 72#include <net/if.h> 73#include <net/pfvar.h> 74#include <net/net_osdep.h> 75#include <net/altq/altq.h> 76#include <net/altq/altq_priq.h> 77#include <netinet/in.h> 78 79/* 80 * function prototypes 81 */ 82static int altq_priq_enqueue(struct ifaltq *, struct mbuf *); 83static struct mbuf *altq_priq_dequeue(struct ifaltq *, enum altdq_op); 84static int altq_priq_request(struct ifaltq *, enum altrq, void *); 85 86int 87altq_priq_pfattach(struct pf_altq *a) 88{ 89 struct ifnet *ifp; 90 int error; 91 92 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED); 93 94 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL) 95 return (EINVAL); 96 97 IFCQ_LOCK(&ifp->if_snd); 98 error = altq_attach(IFCQ_ALTQ(&ifp->if_snd), ALTQT_PRIQ, a->altq_disc, 99 altq_priq_enqueue, altq_priq_dequeue, NULL, altq_priq_request); 100 IFCQ_UNLOCK(&ifp->if_snd); 101 102 return (error); 103} 104 105int 106altq_priq_add(struct pf_altq *a) 107{ 108 struct priq_if *pif; 109 struct ifnet *ifp; 110 111 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED); 112 113 if ((ifp = ifunit(a->ifname)) == NULL) 114 return (EINVAL); 115 if (!ALTQ_IS_READY(IFCQ_ALTQ(&ifp->if_snd))) 116 return (ENODEV); 117 118 pif = priq_alloc(ifp, M_WAITOK, TRUE); 119 if (pif == NULL) 120 return (ENOMEM); 121 122 /* keep the state in pf_altq */ 123 a->altq_disc = pif; 124 125 return (0); 126} 127 128int 129altq_priq_remove(struct pf_altq *a) 130{ 131 struct priq_if *pif; 132 133 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED); 134 135 if ((pif = a->altq_disc) == NULL) 136 return (EINVAL); 137 a->altq_disc = NULL; 138 139 return (priq_destroy(pif)); 140} 141 142int 143altq_priq_add_queue(struct pf_altq *a) 144{ 145 struct priq_if *pif; 146 int err; 147 148 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED); 149 150 if ((pif = a->altq_disc) == NULL) 151 return (EINVAL); 152 153 IFCQ_LOCK(pif->pif_ifq); 154 err = priq_add_queue(pif, a->priority, a->qlimit, 155 a->pq_u.priq_opts.flags, a->qid, NULL); 156 IFCQ_UNLOCK(pif->pif_ifq); 157 158 return (err); 159} 160 161int 162altq_priq_remove_queue(struct pf_altq *a) 163{ 164 struct priq_if *pif; 165 int err; 166 167 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED); 168 169 if ((pif = a->altq_disc) == NULL) 170 return (EINVAL); 171 172 IFCQ_LOCK(pif->pif_ifq); 173 err = priq_remove_queue(pif, a->qid); 174 IFCQ_UNLOCK(pif->pif_ifq); 175 176 return (err); 177} 178 179int 180altq_priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes) 181{ 182 struct ifclassq *ifq = NULL; 183 struct priq_if *pif; 184 struct priq_classstats stats; 185 int error = 0; 186 187 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED); 188 189 if ((unsigned)*nbytes < sizeof (stats)) 190 return (EINVAL); 191 192 if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL) 193 return (EBADF); 194 195 ifq = pif->pif_ifq; 196 IFCQ_LOCK_ASSERT_HELD(ifq); /* lock held by altq_lookup */ 197 error = priq_get_class_stats(pif, a->qid, &stats); 198 IFCQ_UNLOCK(ifq); 199 if (error != 0) 200 return (error); 201 202 if ((error = copyout((caddr_t)&stats, (user_addr_t)(uintptr_t)ubuf, 203 sizeof (stats))) != 0) 204 return (error); 205 206 *nbytes = sizeof (stats); 207 208 return (0); 209} 210 211static int 212altq_priq_request(struct ifaltq *altq, enum altrq req, void *arg) 213{ 214 struct priq_if *pif = (struct priq_if *)altq->altq_disc; 215 216 switch (req) { 217 case ALTRQ_PURGE: 218 priq_purge(pif); 219 break; 220 221 case ALTRQ_PURGE_SC: 222 case ALTRQ_THROTTLE: 223 /* not supported for ALTQ instance */ 224 break; 225 226 case ALTRQ_EVENT: 227 priq_event(pif, (cqev_t)arg); 228 break; 229 } 230 return (0); 231} 232 233/* 234 * altq_priq_enqueue is an enqueue function to be registered to 235 * (*altq_enqueue) in struct ifaltq. 236 */ 237static int 238altq_priq_enqueue(struct ifaltq *altq, struct mbuf *m) 239{ 240 /* grab class set by classifier */ 241 if (!(m->m_flags & M_PKTHDR)) { 242 /* should not happen */ 243 printf("%s: packet for %s does not have pkthdr\n", __func__, 244 if_name(altq->altq_ifcq->ifcq_ifp)); 245 m_freem(m); 246 return (ENOBUFS); 247 } 248 249 return (priq_enqueue(altq->altq_disc, NULL, m, m_pftag(m))); 250} 251 252/* 253 * altq_priq_dequeue is a dequeue function to be registered to 254 * (*altq_dequeue) in struct ifaltq. 255 * 256 * note: ALTDQ_POLL returns the next packet without removing the packet 257 * from the queue. ALTDQ_REMOVE is a normal dequeue operation. 258 * ALTDQ_REMOVE must return the same packet if called immediately 259 * after ALTDQ_POLL. 260 */ 261static struct mbuf * 262altq_priq_dequeue(struct ifaltq *altq, enum altdq_op op) 263{ 264 return (priq_dequeue(altq->altq_disc, (cqdq_op_t)op)); 265} 266#endif /* PF_ALTQ && PKTSCHED_PRIQ */ 267