]>
git.saurik.com Git - apple/xnu.git/blob - bsd/net/altq/altq_cbq.c
2 * Copyright (c) 2007-2013 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@
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 $ */
33 * Copyright (c) Sun Microsystems, Inc. 1993-1998 All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
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.
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.
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
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.
59 * These notices must be retained in any copies of any part of this software.
62 #if PF_ALTQ && PKTSCHED_CBQ
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>
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>
80 * Forward Declarations.
82 static int altq_cbq_request(struct ifaltq
*, enum altrq
, void *);
83 static int altq_cbq_enqueue(struct ifaltq
*, struct mbuf
*);
84 static struct mbuf
*altq_cbq_dequeue(struct ifaltq
*, enum altdq_op
);
87 altq_cbq_pfattach(struct pf_altq
*a
)
92 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
94 if ((ifp
= ifunit(a
->ifname
)) == NULL
|| a
->altq_disc
== NULL
)
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
);
106 altq_cbq_add(struct pf_altq
*a
)
111 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
113 if ((ifp
= ifunit(a
->ifname
)) == NULL
)
115 if (!ALTQ_IS_READY(IFCQ_ALTQ(&ifp
->if_snd
)))
118 cbqp
= cbq_alloc(ifp
, M_WAITOK
, TRUE
);
122 /* keep the state in pf_altq */
129 altq_cbq_remove(struct pf_altq
*a
)
133 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
135 if ((cbqp
= a
->altq_disc
) == NULL
)
139 return (cbq_destroy(cbqp
));
143 altq_cbq_add_queue(struct pf_altq
*a
)
145 struct cbq_opts
*opts
= &a
->pq_u
.cbq_opts
;
149 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
151 if ((cbqp
= a
->altq_disc
) == NULL
)
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_
);
165 altq_cbq_remove_queue(struct pf_altq
*a
)
170 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
172 if ((cbqp
= a
->altq_disc
) == NULL
)
175 IFCQ_LOCK(cbqp
->ifnp
.ifq_
);
176 err
= cbq_remove_queue(cbqp
, a
->qid
);
177 IFCQ_UNLOCK(cbqp
->ifnp
.ifq_
);
183 altq_cbq_getqstats(struct pf_altq
*a
, void *ubuf
, int *nbytes
)
185 struct ifclassq
*ifq
= NULL
;
190 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
192 if ((unsigned)*nbytes
< sizeof (stats
))
195 if ((cbqp
= altq_lookup(a
->ifname
, ALTQT_CBQ
)) == NULL
)
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
);
205 if ((error
= copyout((caddr_t
)&stats
, (user_addr_t
)(uintptr_t)ubuf
,
206 sizeof (stats
))) != 0)
209 *nbytes
= sizeof (stats
);
215 altq_cbq_request(struct ifaltq
*altq
, enum altrq req
, void *arg
)
217 cbq_state_t
*cbqp
= (cbq_state_t
*)altq
->altq_disc
;
225 /* not supported for ALTQ instance */
229 cbq_event(cbqp
, (cqev_t
)arg
);
240 * altq_cbq_enqueue is an enqueue function to be registered to
241 * (*altq_enqueue) in struct ifaltq.
244 altq_cbq_enqueue(struct ifaltq
*altq
, struct mbuf
*m
)
246 /* grab class set by classifier */
247 if (!(m
->m_flags
& M_PKTHDR
)) {
248 /* should not happen */
249 printf("%s: packet for %s does not have pkthdr\n", __func__
,
250 if_name(altq
->altq_ifcq
->ifcq_ifp
));
255 return (cbq_enqueue(altq
->altq_disc
, NULL
, m
, m_pftag(m
)));
259 * altq_cbq_dequeue is a dequeue function to be registered to
260 * (*altq_dequeue) in struct ifaltq.
262 * note: ALTDQ_POLL returns the next packet without removing the packet
263 * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
264 * ALTDQ_REMOVE must return the same packet if called immediately
268 altq_cbq_dequeue(struct ifaltq
*altq
, enum altdq_op op
)
270 return (cbq_dequeue(altq
->altq_disc
, (cqdq_op_t
)op
));
272 #endif /* PF_ALTQ && PKTSCHED_CBQ */