1/* 2 * Copyright (c) 2007-2011 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_cbq.c,v 1.23 2007/09/13 20:40:02 chl Exp $ */ 30/* $KAME: altq_cbq.c,v 1.9 2000/12/14 08:12:45 thorpej Exp $ */ 31 32/* 33 * Copyright (c) Sun Microsystems, Inc. 1993-1998 All rights reserved. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 46 * 3. All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed by the SMCC Technology 49 * Development Group at Sun Microsystems, Inc. 50 * 51 * 4. The name of the Sun Microsystems, Inc nor may not be used to endorse or 52 * promote products derived from this software without specific prior 53 * written permission. 54 * 55 * SUN MICROSYSTEMS DOES NOT CLAIM MERCHANTABILITY OF THIS SOFTWARE OR THE 56 * SUITABILITY OF THIS SOFTWARE FOR ANY PARTICULAR PURPOSE. The software is 57 * provided "as is" without express or implied warranty of any kind. 58 * 59 * These notices must be retained in any copies of any part of this software. 60 */ 61 62#if PF_ALTQ && PKTSCHED_CBQ 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_cbq.h> 77#include <netinet/in.h> 78 79/* 80 * Forward Declarations. 81 */ 82static int altq_cbq_request(struct ifaltq *, enum altrq, void *); 83static int altq_cbq_enqueue(struct ifaltq *, struct mbuf *); 84static struct mbuf *altq_cbq_dequeue(struct ifaltq *, enum altdq_op); 85 86int 87altq_cbq_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_CBQ, a->altq_disc, 99 altq_cbq_enqueue, altq_cbq_dequeue, NULL, altq_cbq_request); 100 IFCQ_UNLOCK(&ifp->if_snd); 101 102 return (error); 103} 104 105int 106altq_cbq_add(struct pf_altq *a) 107{ 108 cbq_state_t *cbqp; 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 cbqp = cbq_alloc(ifp, M_WAITOK, TRUE); 119 if (cbqp == NULL) 120 return (ENOMEM); 121 122 /* keep the state in pf_altq */ 123 a->altq_disc = cbqp; 124 125 return (0); 126} 127 128int 129altq_cbq_remove(struct pf_altq *a) 130{ 131 cbq_state_t *cbqp; 132 133 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED); 134 135 if ((cbqp = a->altq_disc) == NULL) 136 return (EINVAL); 137 a->altq_disc = NULL; 138 139 return (cbq_destroy(cbqp)); 140} 141 142int 143altq_cbq_add_queue(struct pf_altq *a) 144{ 145 struct cbq_opts *opts = &a->pq_u.cbq_opts; 146 cbq_state_t *cbqp; 147 int err; 148 149 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED); 150 151 if ((cbqp = a->altq_disc) == NULL) 152 return (EINVAL); 153 154 IFCQ_LOCK(cbqp->ifnp.ifq_); 155 err = cbq_add_queue(cbqp, a->qlimit, a->priority, 156 opts->minburst, opts->maxburst, opts->pktsize, opts->maxpktsize, 157 opts->ns_per_byte, opts->maxidle, opts->minidle, opts->offtime, 158 opts->flags, a->parent_qid, a->qid, NULL); 159 IFCQ_UNLOCK(cbqp->ifnp.ifq_); 160 161 return (err); 162} 163 164int 165altq_cbq_remove_queue(struct pf_altq *a) 166{ 167 cbq_state_t *cbqp; 168 int err; 169 170 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED); 171 172 if ((cbqp = a->altq_disc) == NULL) 173 return (EINVAL); 174 175 IFCQ_LOCK(cbqp->ifnp.ifq_); 176 err = cbq_remove_queue(cbqp, a->qid); 177 IFCQ_UNLOCK(cbqp->ifnp.ifq_); 178 179 return (err); 180} 181 182int 183altq_cbq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes) 184{ 185 struct ifclassq *ifq = NULL; 186 cbq_state_t *cbqp; 187 class_stats_t stats; 188 int error = 0; 189 190 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED); 191 192 if ((unsigned)*nbytes < sizeof (stats)) 193 return (EINVAL); 194 195 if ((cbqp = altq_lookup(a->ifname, ALTQT_CBQ)) == NULL) 196 return (EBADF); 197 198 ifq = cbqp->ifnp.ifq_; 199 IFCQ_LOCK_ASSERT_HELD(ifq); /* lock held by altq_lookup */ 200 error = cbq_get_class_stats(cbqp, a->qid, &stats); 201 IFCQ_UNLOCK(ifq); 202 if (error != 0) 203 return (error); 204 205 if ((error = copyout((caddr_t)&stats, (user_addr_t)(uintptr_t)ubuf, 206 sizeof (stats))) != 0) 207 return (error); 208 209 *nbytes = sizeof (stats); 210 211 return (0); 212} 213 214static int 215altq_cbq_request(struct ifaltq *altq, enum altrq req, void *arg) 216{ 217 cbq_state_t *cbqp = (cbq_state_t *)altq->altq_disc; 218 219 switch (req) { 220 case ALTRQ_PURGE: 221 cbq_purge(cbqp); 222 break; 223 224 case ALTRQ_PURGE_SC: 225 /* not supported for ALTQ instance */ 226 break; 227 228 case ALTRQ_EVENT: 229 cbq_event(cbqp, (cqev_t)arg); 230 break; 231 } 232 return (0); 233} 234 235/* 236 * altq_cbq_enqueue is an enqueue function to be registered to 237 * (*altq_enqueue) in struct ifaltq. 238 */ 239static int 240altq_cbq_enqueue(struct ifaltq *altq, struct mbuf *m) 241{ 242 /* grab class set by classifier */ 243 if (!(m->m_flags & M_PKTHDR)) { 244 /* should not happen */ 245 printf("%s: packet for %s does not have pkthdr\n", __func__, 246 if_name(altq->altq_ifcq->ifcq_ifp)); 247 m_freem(m); 248 return (ENOBUFS); 249 } 250 251 return (cbq_enqueue(altq->altq_disc, NULL, m, m_pftag(m))); 252} 253 254/* 255 * altq_cbq_dequeue is a dequeue function to be registered to 256 * (*altq_dequeue) in struct ifaltq. 257 * 258 * note: ALTDQ_POLL returns the next packet without removing the packet 259 * from the queue. ALTDQ_REMOVE is a normal dequeue operation. 260 * ALTDQ_REMOVE must return the same packet if called immediately 261 * after ALTDQ_POLL. 262 */ 263static struct mbuf * 264altq_cbq_dequeue(struct ifaltq *altq, enum altdq_op op) 265{ 266 return (cbq_dequeue(altq->altq_disc, (cqdq_op_t)op)); 267} 268#endif /* PF_ALTQ && PKTSCHED_CBQ */ 269