2 * Copyright (c) 2004 Apple Computer, 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 #include "kpi_protocol.h"
31 #include <sys/param.h>
32 #include <sys/malloc.h>
33 #include <sys/socket.h>
34 #include <sys/systm.h>
35 #include <sys/kpi_mbuf.h>
36 #include <sys/domain.h>
39 #include <libkern/OSAtomic.h>
41 void proto_kpi_init(void);
42 void proto_input_run(void);
44 typedef int (*attach_t
)(struct ifnet
*ifp
, u_long protocol_family
);
45 typedef int (*detach_t
)(struct ifnet
*ifp
, u_long protocol_family
);
47 /****************************************************************************/
48 /* WARNING: Big assumption made here - there can be only one input thread */
49 struct proto_input_entry
{
50 struct proto_input_entry
*next
;
52 struct domain
*domain
;
54 protocol_family_t protocol
;
55 proto_input_handler input
;
56 proto_input_detached_handler detached
;
62 #define PROTO_HASH_SLOTS 5
64 static struct proto_input_entry
*proto_hash
[PROTO_HASH_SLOTS
];
65 static struct proto_input_entry
*proto_input_add_list
;
66 static lck_mtx_t
*proto_input_lock
= 0;
67 __private_extern__ u_int32_t inject_buckets
= 0;
69 extern thread_t dlil_input_thread_ptr
;
70 extern int dlil_input_thread_wakeup
;
74 protocol_family_t protocol
)
89 __private_extern__
void
92 lck_grp_attr_t
*grp_attrib
= 0;
93 lck_attr_t
*lck_attrib
= 0;
94 lck_grp_t
*lck_group
= 0;
96 /* Allocate a mtx lock */
97 grp_attrib
= lck_grp_attr_alloc_init();
98 lck_group
= lck_grp_alloc_init("protocol kpi", grp_attrib
);
99 lck_grp_attr_free(grp_attrib
);
100 lck_attrib
= lck_attr_alloc_init();
101 proto_input_lock
= lck_mtx_alloc_init(lck_group
, lck_attrib
);
102 lck_grp_free(lck_group
);
103 lck_attr_free(lck_attrib
);
106 __private_extern__ errno_t
107 proto_register_input(
108 protocol_family_t protocol
,
109 proto_input_handler input
,
110 proto_input_detached_handler detached
)
113 struct proto_input_entry
*entry
;
115 entry
= _MALLOC(sizeof(*entry
), M_IFADDR
, M_WAITOK
);
120 bzero(entry
, sizeof(*entry
));
121 entry
->protocol
= protocol
;
122 entry
->input
= input
;
123 entry
->detached
= detached
;
126 struct domain
*dp
= domains
;
127 extern lck_mtx_t
*domain_proto_mtx
;
129 lck_mtx_assert(domain_proto_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
130 lck_mtx_lock(domain_proto_mtx
);
131 while (dp
&& dp
->dom_family
!= protocol
)
134 lck_mtx_unlock(domain_proto_mtx
);
139 entry
->next
= proto_input_add_list
;
140 } while(!OSCompareAndSwap((UInt32
)entry
->next
, (UInt32
)entry
, (UInt32
*)&proto_input_add_list
));
142 wakeup((caddr_t
)&dlil_input_thread_wakeup
);
148 __private_extern__
void
149 proto_unregister_input(
150 protocol_family_t protocol
)
152 struct proto_input_entry
*entry
= NULL
;
154 for (entry
= proto_hash
[proto_hash_value(protocol
)]; entry
; entry
= entry
->next
)
155 if (entry
->protocol
== protocol
)
164 proto_delayed_attach(
165 struct proto_input_entry
*entry
)
167 struct proto_input_entry
*next_entry
;
168 for (next_entry
= entry
->next
; entry
; entry
= next_entry
) {
169 struct proto_input_entry
*exist
;
172 hash_slot
= proto_hash_value(entry
->protocol
);
173 next_entry
= entry
->next
;
175 for (exist
= proto_hash
[hash_slot
]; exist
; exist
= exist
->next
)
176 if (exist
->protocol
== entry
->protocol
)
179 /* If the entry already exists, call detached and dispose */
182 entry
->detached(entry
->protocol
);
183 FREE(entry
, M_IFADDR
);
186 entry
->next
= proto_hash
[hash_slot
];
187 proto_hash
[hash_slot
] = entry
;
193 proto_delayed_inject(
194 struct proto_input_entry
*entry
)
200 lck_mtx_lock(proto_input_lock
);
201 packet_list
= entry
->first_packet
;
202 entry
->first_packet
= entry
->last_packet
= 0;
203 lck_mtx_unlock(proto_input_lock
);
205 if (packet_list
== NULL
)
208 if (entry
->domain
&& (entry
->domain
->dom_flags
& DOM_REENTRANT
) == 0) {
209 lck_mtx_lock(entry
->domain
->dom_mtx
);
213 for (packet
= packet_list
; packet
; packet
= packet_list
) {
214 packet_list
= mbuf_nextpkt(packet
);
215 mbuf_setnextpkt(packet
, NULL
);
216 entry
->input(entry
->protocol
, packet
);
220 lck_mtx_unlock(entry
->domain
->dom_mtx
);
224 /* This function must be called from a single dlil input thread */
225 __private_extern__
void
226 proto_input_run(void)
228 struct proto_input_entry
*entry
;
232 if (current_thread() != dlil_input_thread_ptr
)
233 panic("proto_input_run called from a thread other than dlil_input_thread!\n");
236 entry
= proto_input_add_list
;
237 } while (entry
&& !OSCompareAndSwap((UInt32
)entry
, 0, (UInt32
*)&proto_input_add_list
));
240 proto_delayed_attach(entry
);
243 inject
= inject_buckets
;
244 } while (inject
&& !OSCompareAndSwap(inject
, 0, (UInt32
*)&inject_buckets
));
247 for (i
= 0; i
< PROTO_HASH_SLOTS
; i
++) {
248 if ((inject
& (1L << i
)) != 0) {
249 for (entry
= proto_hash
[i
]; entry
; entry
= entry
->next
) {
250 if (entry
->first_packet
) {
251 proto_delayed_inject(entry
);
261 protocol_family_t protocol
,
264 struct proto_input_entry
*entry
;
266 if (current_thread() != dlil_input_thread_ptr
)
267 panic("proto_input called from a thread other than dlil_input_thread!\n");
269 for (entry
= proto_hash
[proto_hash_value(protocol
)]; entry
; entry
= entry
->next
) {
270 if (entry
->protocol
== protocol
)
276 #if DIRECT_PROTO_INPUT
277 // See <rdar://problem/3687868> for why this is disabled
278 // We need to release the dlil lock before taking the protocol lock
279 for (packet
= packet_list
; packet
; packet
= packet_list
) {
280 packet_list
= mbuf_nextpkt(packet
);
281 mbuf_setnextpkt(packet
, NULL
);
282 entry
->input(entry
->protocol
, packet
);
286 int hash_slot
= proto_hash_value(protocol
);
288 for (last_packet
= packet_list
; mbuf_nextpkt(last_packet
);
289 last_packet
= mbuf_nextpkt(last_packet
))
290 /* find the last packet */;
292 lck_mtx_lock(proto_input_lock
);
293 if (entry
->first_packet
== NULL
) {
294 entry
->first_packet
= packet_list
;
297 mbuf_setnextpkt(entry
->last_packet
, packet_list
);
299 entry
->last_packet
= last_packet
;
300 lck_mtx_unlock(proto_input_lock
);
302 OSBitOrAtomic((1L << hash_slot
), (UInt32
*)&inject_buckets
);
315 protocol_family_t protocol
,
318 struct proto_input_entry
*entry
;
320 int hash_slot
= proto_hash_value(protocol
);
322 for (last_packet
= packet_list
; mbuf_nextpkt(last_packet
);
323 last_packet
= mbuf_nextpkt(last_packet
))
324 /* find the last packet */;
326 for (entry
= proto_hash
[hash_slot
]; entry
; entry
= entry
->next
) {
327 if (entry
->protocol
== protocol
)
332 lck_mtx_lock(proto_input_lock
);
333 if (entry
->first_packet
== NULL
) {
334 entry
->first_packet
= packet_list
;
337 mbuf_setnextpkt(entry
->last_packet
, packet_list
);
339 entry
->last_packet
= last_packet
;
340 lck_mtx_unlock(proto_input_lock
);
342 OSBitOrAtomic((1L << hash_slot
), (UInt32
*)&inject_buckets
);
344 wakeup((caddr_t
)&dlil_input_thread_wakeup
);
355 proto_register_plumber(
356 protocol_family_t proto_fam
,
357 ifnet_family_t if_fam
,
358 proto_plumb_handler plumb
,
359 proto_unplumb_handler unplumb
)
361 return dlil_reg_proto_module(proto_fam
, if_fam
, (attach_t
)plumb
, (detach_t
)unplumb
);
365 proto_unregister_plumber(
366 protocol_family_t proto_fam
,
367 ifnet_family_t if_fam
)
369 (void)dlil_dereg_proto_module(proto_fam
, if_fam
);