2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include "kpi_protocol.h"
26 #include <sys/param.h>
27 #include <sys/malloc.h>
28 #include <sys/socket.h>
29 #include <sys/systm.h>
30 #include <sys/kpi_mbuf.h>
31 #include <sys/domain.h>
34 #include <libkern/OSAtomic.h>
36 void proto_kpi_init(void);
37 void proto_input_run(void);
39 typedef int (*attach_t
)(struct ifnet
*ifp
, u_long protocol_family
);
40 typedef int (*detach_t
)(struct ifnet
*ifp
, u_long protocol_family
);
42 /****************************************************************************/
43 /* WARNING: Big assumption made here - there can be only one input thread */
44 struct proto_input_entry
{
45 struct proto_input_entry
*next
;
47 struct domain
*domain
;
49 protocol_family_t protocol
;
50 proto_input_handler input
;
51 proto_input_detached_handler detached
;
57 #define PROTO_HASH_SLOTS 5
59 static struct proto_input_entry
*proto_hash
[PROTO_HASH_SLOTS
];
60 static struct proto_input_entry
*proto_input_add_list
;
61 static lck_mtx_t
*proto_input_lock
= 0;
62 __private_extern__ u_int32_t inject_buckets
= 0;
64 extern thread_t dlil_input_thread_ptr
;
65 extern int dlil_input_thread_wakeup
;
69 protocol_family_t protocol
)
84 __private_extern__
void
87 lck_grp_attr_t
*grp_attrib
= 0;
88 lck_attr_t
*lck_attrib
= 0;
89 lck_grp_t
*lck_group
= 0;
91 /* Allocate a mtx lock */
92 grp_attrib
= lck_grp_attr_alloc_init();
93 lck_grp_attr_setdefault(grp_attrib
);
94 lck_group
= lck_grp_alloc_init("protocol kpi", grp_attrib
);
95 lck_grp_attr_free(grp_attrib
);
96 lck_attrib
= lck_attr_alloc_init();
97 lck_attr_setdefault(lck_attrib
);
98 proto_input_lock
= lck_mtx_alloc_init(lck_group
, lck_attrib
);
99 lck_grp_free(lck_group
);
100 lck_attr_free(lck_attrib
);
103 __private_extern__ errno_t
104 proto_register_input(
105 protocol_family_t protocol
,
106 proto_input_handler input
,
107 proto_input_detached_handler detached
)
110 struct proto_input_entry
*entry
;
112 entry
= _MALLOC(sizeof(*entry
), M_IFADDR
, M_WAITOK
);
117 bzero(entry
, sizeof(*entry
));
118 entry
->protocol
= protocol
;
119 entry
->input
= input
;
120 entry
->detached
= detached
;
123 struct domain
*dp
= domains
;
124 extern lck_mtx_t
*domain_proto_mtx
;
126 lck_mtx_assert(domain_proto_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
127 lck_mtx_lock(domain_proto_mtx
);
128 while (dp
&& dp
->dom_family
!= protocol
)
131 lck_mtx_unlock(domain_proto_mtx
);
136 entry
->next
= proto_input_add_list
;
137 } while(!OSCompareAndSwap((UInt32
)entry
->next
, (UInt32
)entry
, (UInt32
*)&proto_input_add_list
));
139 wakeup((caddr_t
)&dlil_input_thread_wakeup
);
145 __private_extern__
void
146 proto_unregister_input(
147 protocol_family_t protocol
)
149 struct proto_input_entry
*entry
= NULL
;
151 for (entry
= proto_hash
[proto_hash_value(protocol
)]; entry
; entry
= entry
->next
)
152 if (entry
->protocol
== protocol
)
161 proto_delayed_attach(
162 struct proto_input_entry
*entry
)
164 struct proto_input_entry
*next_entry
;
165 for (next_entry
= entry
->next
; entry
; entry
= next_entry
) {
166 struct proto_input_entry
*exist
;
169 hash_slot
= proto_hash_value(entry
->protocol
);
170 next_entry
= entry
->next
;
172 for (exist
= proto_hash
[hash_slot
]; exist
; exist
= exist
->next
)
173 if (exist
->protocol
== entry
->protocol
)
176 /* If the entry already exists, call detached and dispose */
179 entry
->detached(entry
->protocol
);
180 FREE(entry
, M_IFADDR
);
183 entry
->next
= proto_hash
[hash_slot
];
184 proto_hash
[hash_slot
] = entry
;
190 proto_delayed_inject(
191 struct proto_input_entry
*entry
)
197 lck_mtx_lock(proto_input_lock
);
198 packet_list
= entry
->first_packet
;
199 entry
->first_packet
= entry
->last_packet
= 0;
200 lck_mtx_unlock(proto_input_lock
);
202 if (packet_list
== NULL
)
205 if (entry
->domain
&& (entry
->domain
->dom_flags
& DOM_REENTRANT
) == 0) {
206 lck_mtx_lock(entry
->domain
->dom_mtx
);
210 for (packet
= packet_list
; packet
; packet
= packet_list
) {
211 packet_list
= mbuf_nextpkt(packet
);
212 mbuf_setnextpkt(packet
, NULL
);
213 entry
->input(entry
->protocol
, packet
);
217 lck_mtx_unlock(entry
->domain
->dom_mtx
);
221 /* This function must be called from a single dlil input thread */
222 __private_extern__
void
223 proto_input_run(void)
225 struct proto_input_entry
*entry
;
229 if (current_thread() != dlil_input_thread_ptr
)
230 panic("proto_input_run called from a thread other than dlil_input_thread!\n");
233 entry
= proto_input_add_list
;
234 } while (entry
&& !OSCompareAndSwap((UInt32
)entry
, 0, (UInt32
*)&proto_input_add_list
));
237 proto_delayed_attach(entry
);
240 inject
= inject_buckets
;
241 } while (inject
&& !OSCompareAndSwap(inject
, 0, (UInt32
*)&inject_buckets
));
244 for (i
= 0; i
< PROTO_HASH_SLOTS
; i
++) {
245 if ((inject
& (1L << i
)) != 0) {
246 for (entry
= proto_hash
[i
]; entry
; entry
= entry
->next
) {
247 if (entry
->first_packet
) {
248 proto_delayed_inject(entry
);
258 protocol_family_t protocol
,
261 struct proto_input_entry
*entry
;
263 if (current_thread() != dlil_input_thread_ptr
)
264 panic("proto_input called from a thread other than dlil_input_thread!\n");
266 for (entry
= proto_hash
[proto_hash_value(protocol
)]; entry
; entry
= entry
->next
) {
267 if (entry
->protocol
== protocol
)
273 #if DIRECT_PROTO_INPUT
274 // See <rdar://problem/3687868> for why this is disabled
275 // We need to release the dlil lock before taking the protocol lock
276 for (packet
= packet_list
; packet
; packet
= packet_list
) {
277 packet_list
= mbuf_nextpkt(packet
);
278 mbuf_setnextpkt(packet
, NULL
);
279 entry
->input(entry
->protocol
, packet
);
283 int hash_slot
= proto_hash_value(protocol
);
285 for (last_packet
= packet_list
; mbuf_nextpkt(last_packet
);
286 last_packet
= mbuf_nextpkt(last_packet
))
287 /* find the last packet */;
289 lck_mtx_lock(proto_input_lock
);
290 if (entry
->first_packet
== NULL
) {
291 entry
->first_packet
= packet_list
;
294 mbuf_setnextpkt(entry
->last_packet
, packet_list
);
296 entry
->last_packet
= last_packet
;
297 lck_mtx_unlock(proto_input_lock
);
299 OSBitOrAtomic((1L << hash_slot
), (UInt32
*)&inject_buckets
);
312 protocol_family_t protocol
,
315 struct proto_input_entry
*entry
;
317 int hash_slot
= proto_hash_value(protocol
);
319 for (last_packet
= packet_list
; mbuf_nextpkt(last_packet
);
320 last_packet
= mbuf_nextpkt(last_packet
))
321 /* find the last packet */;
323 for (entry
= proto_hash
[hash_slot
]; entry
; entry
= entry
->next
) {
324 if (entry
->protocol
== protocol
)
329 lck_mtx_lock(proto_input_lock
);
330 if (entry
->first_packet
== NULL
) {
331 entry
->first_packet
= packet_list
;
334 mbuf_setnextpkt(entry
->last_packet
, packet_list
);
336 entry
->last_packet
= last_packet
;
337 lck_mtx_unlock(proto_input_lock
);
339 OSBitOrAtomic((1L << hash_slot
), (UInt32
*)&inject_buckets
);
341 wakeup((caddr_t
)&dlil_input_thread_wakeup
);
352 proto_register_plumber(
353 protocol_family_t proto_fam
,
354 ifnet_family_t if_fam
,
355 proto_plumb_handler plumb
,
356 proto_unplumb_handler unplumb
)
358 return dlil_reg_proto_module(proto_fam
, if_fam
, (attach_t
)plumb
, (detach_t
)unplumb
);
362 proto_unregister_plumber(
363 protocol_family_t proto_fam
,
364 ifnet_family_t if_fam
)
366 (void)dlil_dereg_proto_module(proto_fam
, if_fam
);