]>
git.saurik.com Git - apple/xnu.git/blob - bsd/net/altq/altq_priq.c
2 * Copyright (c) 2007-2012 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_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 $ */
33 * Copyright (C) 2000-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
62 #if PF_ALTQ && PKTSCHED_PRIQ
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_priq.h>
77 #include <netinet/in.h>
82 static int altq_priq_enqueue(struct ifaltq
*, struct mbuf
*);
83 static struct mbuf
*altq_priq_dequeue(struct ifaltq
*, enum altdq_op
);
84 static int altq_priq_request(struct ifaltq
*, enum altrq
, void *);
87 altq_priq_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_PRIQ
, a
->altq_disc
,
99 altq_priq_enqueue
, altq_priq_dequeue
, NULL
, altq_priq_request
);
100 IFCQ_UNLOCK(&ifp
->if_snd
);
106 altq_priq_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 pif
= priq_alloc(ifp
, M_WAITOK
, TRUE
);
122 /* keep the state in pf_altq */
129 altq_priq_remove(struct pf_altq
*a
)
133 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
135 if ((pif
= a
->altq_disc
) == NULL
)
139 return (priq_destroy(pif
));
143 altq_priq_add_queue(struct pf_altq
*a
)
148 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
150 if ((pif
= a
->altq_disc
) == NULL
)
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
);
162 altq_priq_remove_queue(struct pf_altq
*a
)
167 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
169 if ((pif
= a
->altq_disc
) == NULL
)
172 IFCQ_LOCK(pif
->pif_ifq
);
173 err
= priq_remove_queue(pif
, a
->qid
);
174 IFCQ_UNLOCK(pif
->pif_ifq
);
180 altq_priq_getqstats(struct pf_altq
*a
, void *ubuf
, int *nbytes
)
182 struct ifclassq
*ifq
= NULL
;
184 struct priq_classstats stats
;
187 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
189 if ((unsigned)*nbytes
< sizeof (stats
))
192 if ((pif
= altq_lookup(a
->ifname
, ALTQT_PRIQ
)) == NULL
)
196 IFCQ_LOCK_ASSERT_HELD(ifq
); /* lock held by altq_lookup */
197 error
= priq_get_class_stats(pif
, a
->qid
, &stats
);
202 if ((error
= copyout((caddr_t
)&stats
, (user_addr_t
)(uintptr_t)ubuf
,
203 sizeof (stats
))) != 0)
206 *nbytes
= sizeof (stats
);
212 altq_priq_request(struct ifaltq
*altq
, enum altrq req
, void *arg
)
214 struct priq_if
*pif
= (struct priq_if
*)altq
->altq_disc
;
223 /* not supported for ALTQ instance */
227 priq_event(pif
, (cqev_t
)arg
);
234 * altq_priq_enqueue is an enqueue function to be registered to
235 * (*altq_enqueue) in struct ifaltq.
238 altq_priq_enqueue(struct ifaltq
*altq
, struct mbuf
*m
)
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
));
249 return (priq_enqueue(altq
->altq_disc
, NULL
, m
, m_pftag(m
)));
253 * altq_priq_dequeue is a dequeue function to be registered to
254 * (*altq_dequeue) in struct ifaltq.
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
262 altq_priq_dequeue(struct ifaltq
*altq
, enum altdq_op op
)
264 return (priq_dequeue(altq
->altq_disc
, (cqdq_op_t
)op
));
266 #endif /* PF_ALTQ && PKTSCHED_PRIQ */