2 * Copyright (c) 2007-2011 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_subr.c,v 1.24 2007/12/11 00:30:14 mikeb Exp $ */
30 /* $KAME: altq_subr.c,v 1.11 2002/01/11 08:11:49 kjc Exp $ */
33 * Copyright (C) 1997-2003
34 * Sony Computer Science Laboratories Inc. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * 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.
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.
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
58 #include <sys/cdefs.h>
60 #include <sys/param.h>
61 #include <sys/malloc.h>
63 #include <sys/systm.h>
65 #include <sys/socket.h>
66 #include <sys/socketvar.h>
67 #include <sys/kernel.h>
68 #include <sys/errno.h>
69 #include <sys/syslog.h>
70 #include <sys/sysctl.h>
71 #include <sys/queue.h>
72 #include <sys/mcache.h>
75 #include <net/if_var.h>
76 #include <net/if_dl.h>
77 #include <net/if_types.h>
78 #include <net/pfvar.h>
79 #include <net/altq/altq.h>
80 #include <net/pktsched/pktsched.h>
82 #include <pexpert/pexpert.h>
84 SYSCTL_NODE(_net
, OID_AUTO
, altq
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "ALTQ");
86 static u_int32_t altq_debug
;
87 SYSCTL_UINT(_net_altq
, OID_AUTO
, debug
, CTLFLAG_RW
, &altq_debug
, 0,
88 "Enable ALTQ debugging");
91 * look up the queue state by the interface name and the queueing type;
92 * upon success, returns with the interface send queue lock held, and
93 * the caller is responsible for releasing it.
96 altq_lookup(char *name
, u_int32_t type
)
101 if ((ifp
= ifunit(name
)) != NULL
) {
102 IFCQ_LOCK(&ifp
->if_snd
);
103 if (type
!= ALTQT_NONE
&&
104 IFCQ_ALTQ(&ifp
->if_snd
)->altq_type
== type
)
105 state
= IFCQ_ALTQ(&ifp
->if_snd
)->altq_disc
;
107 IFCQ_UNLOCK(&ifp
->if_snd
);
111 IFCQ_LOCK_ASSERT_HELD(&ifp
->if_snd
);
117 altq_attach(struct ifaltq
*altq
, u_int32_t type
, void *discipline
,
118 altq_enq_func enqueue
, altq_deq_func dequeue
,
119 altq_deq_sc_func dequeue_sc
, altq_req_func request
)
121 IFCQ_LOCK_ASSERT_HELD(altq
->altq_ifcq
);
123 if (!ALTQ_IS_READY(altq
))
126 VERIFY(enqueue
!= NULL
);
127 VERIFY(!(dequeue
!= NULL
&& dequeue_sc
!= NULL
));
128 VERIFY(request
!= NULL
);
130 altq
->altq_type
= type
;
131 altq
->altq_disc
= discipline
;
132 altq
->altq_enqueue
= enqueue
;
133 altq
->altq_dequeue
= dequeue
;
134 altq
->altq_dequeue_sc
= dequeue_sc
;
135 altq
->altq_request
= request
;
136 altq
->altq_flags
&= (ALTQF_CANTCHANGE
|ALTQF_ENABLED
);
142 altq_detach(struct ifaltq
*altq
)
144 IFCQ_LOCK_ASSERT_HELD(altq
->altq_ifcq
);
146 if (!ALTQ_IS_READY(altq
))
148 if (ALTQ_IS_ENABLED(altq
))
150 if (!ALTQ_IS_ATTACHED(altq
))
153 altq
->altq_type
= ALTQT_NONE
;
154 altq
->altq_disc
= NULL
;
155 altq
->altq_enqueue
= NULL
;
156 altq
->altq_dequeue
= NULL
;
157 altq
->altq_dequeue_sc
= NULL
;
158 altq
->altq_request
= NULL
;
159 altq
->altq_flags
&= ALTQF_CANTCHANGE
;
165 altq_enable(struct ifaltq
*altq
)
167 struct ifclassq
*ifq
= altq
->altq_ifcq
;
169 IFCQ_LOCK_ASSERT_HELD(ifq
);
171 if (!ALTQ_IS_READY(altq
))
173 if (ALTQ_IS_ENABLED(altq
))
176 altq
->altq_flags
|= ALTQF_ENABLED
;
182 altq_disable(struct ifaltq
*altq
)
184 struct ifclassq
*ifq
= altq
->altq_ifcq
;
186 IFCQ_LOCK_ASSERT_HELD(ifq
);
188 if (!ALTQ_IS_ENABLED(altq
))
191 if_qflush(ifq
->ifcq_ifp
, 1);
193 altq
->altq_flags
&= ~ALTQF_ENABLED
;
199 * add a discipline or a queue
202 altq_add(struct pf_altq
*a
)
206 VERIFY(machclk_freq
!= 0);
208 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
210 if (a
->qname
[0] != 0)
211 return (altq_add_queue(a
));
213 switch (a
->scheduler
) {
216 error
= altq_cbq_add(a
);
218 #endif /* PKTSCHED_CBQ */
221 error
= altq_priq_add(a
);
223 #endif /* PKTSCHED_PRIQ */
226 error
= altq_hfsc_add(a
);
228 #endif /* PKTSCHED_HFSC */
231 error
= altq_fairq_add(a
);
233 #endif /* PKTSCHED_FAIRQ */
235 error
= altq_qfq_add(a
);
245 * remove a discipline or a queue
248 altq_remove(struct pf_altq
*a
)
252 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
254 if (a
->qname
[0] != 0)
255 return (altq_remove_queue(a
));
257 switch (a
->scheduler
) {
260 error
= altq_cbq_remove(a
);
262 #endif /* PKTSCHED_CBQ */
265 error
= altq_priq_remove(a
);
267 #endif /* PKTSCHED_PRIQ */
270 error
= altq_hfsc_remove(a
);
272 #endif /* PKTSCHED_HFSC */
275 error
= altq_fairq_remove(a
);
277 #endif /* PKTSCHED_FAIRQ */
279 error
= altq_qfq_remove(a
);
289 * add a queue to the discipline
292 altq_add_queue(struct pf_altq
*a
)
296 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
298 switch (a
->scheduler
) {
301 error
= altq_cbq_add_queue(a
);
303 #endif /* PKTSCHED_CBQ */
306 error
= altq_priq_add_queue(a
);
308 #endif /* PKTSCHED_PRIQ */
311 error
= altq_hfsc_add_queue(a
);
313 #endif /* PKTSCHED_HFSC */
316 error
= altq_fairq_add_queue(a
);
318 #endif /* PKTSCHED_FAIRQ */
320 error
= altq_qfq_add_queue(a
);
330 * remove a queue from the discipline
333 altq_remove_queue(struct pf_altq
*a
)
337 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
339 switch (a
->scheduler
) {
342 error
= altq_cbq_remove_queue(a
);
344 #endif /* PKTSCHED_CBQ */
347 error
= altq_priq_remove_queue(a
);
349 #endif /* PKTSCHED_PRIQ */
352 error
= altq_hfsc_remove_queue(a
);
354 #endif /* PKTSCHED_HFSC */
357 error
= altq_fairq_remove_queue(a
);
359 #endif /* PKTSCHED_FAIRQ */
361 error
= altq_qfq_remove_queue(a
);
371 * get queue statistics
374 altq_getqstats(struct pf_altq
*a
, void *ubuf
, int *nbytes
)
378 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
380 switch (a
->scheduler
) {
383 error
= altq_cbq_getqstats(a
, ubuf
, nbytes
);
385 #endif /* PKTSCHED_CBQ */
388 error
= altq_priq_getqstats(a
, ubuf
, nbytes
);
390 #endif /* PKTSCHED_PRIQ */
393 error
= altq_hfsc_getqstats(a
, ubuf
, nbytes
);
395 #endif /* PKTSCHED_HFSC */
398 error
= altq_fairq_getqstats(a
, ubuf
, nbytes
);
400 #endif /* PKTSCHED_FAIRQ */
402 error
= altq_qfq_getqstats(a
, ubuf
, nbytes
);
412 * attach a discipline to the interface. if one already exists, it is
416 altq_pfattach(struct pf_altq
*a
)
420 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
422 switch (a
->scheduler
) {
427 error
= altq_cbq_pfattach(a
);
429 #endif /* PKTSCHED_CBQ */
432 error
= altq_priq_pfattach(a
);
434 #endif /* PKTSCHED_PRIQ */
437 error
= altq_hfsc_pfattach(a
);
439 #endif /* PKTSCHED_HFSC */
442 error
= altq_fairq_pfattach(a
);
444 #endif /* PKTSCHED_FAIRQ */
446 error
= altq_qfq_pfattach(a
);
456 * detach a discipline from the interface.
457 * it is possible that the discipline was already overridden by another
461 altq_pfdetach(struct pf_altq
*a
)
466 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
468 if ((ifp
= ifunit(a
->ifname
)) == NULL
)
471 /* if this discipline is no longer referenced, just return */
472 IFCQ_LOCK(&ifp
->if_snd
);
473 if (a
->altq_disc
== NULL
||
474 a
->altq_disc
!= IFCQ_ALTQ(&ifp
->if_snd
)->altq_disc
) {
475 IFCQ_UNLOCK(&ifp
->if_snd
);
479 if (ALTQ_IS_ENABLED(IFCQ_ALTQ(&ifp
->if_snd
)))
480 error
= altq_disable(IFCQ_ALTQ(&ifp
->if_snd
));
482 error
= altq_detach(IFCQ_ALTQ(&ifp
->if_snd
));
483 IFCQ_UNLOCK(&ifp
->if_snd
);