2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include "kpi_protocol.h"
25 #include <sys/param.h>
26 #include <sys/malloc.h>
27 #include <sys/socket.h>
28 #include <sys/systm.h>
29 #include <sys/kpi_mbuf.h>
30 #include <sys/domain.h>
33 #include <libkern/OSAtomic.h>
35 void proto_kpi_init(void);
36 void proto_input_run(void);
38 typedef int (*attach_t
)(struct ifnet
*ifp
, u_long protocol_family
);
39 typedef int (*detach_t
)(struct ifnet
*ifp
, u_long protocol_family
);
41 /****************************************************************************/
42 /* WARNING: Big assumption made here - there can be only one input thread */
43 struct proto_input_entry
{
44 struct proto_input_entry
*next
;
46 struct domain
*domain
;
48 protocol_family_t protocol
;
49 proto_input_handler input
;
50 proto_input_detached_handler detached
;
56 #define PROTO_HASH_SLOTS 5
58 static struct proto_input_entry
*proto_hash
[PROTO_HASH_SLOTS
];
59 static struct proto_input_entry
*proto_input_add_list
;
60 static lck_mtx_t
*proto_input_lock
= 0;
61 __private_extern__ u_int32_t inject_buckets
= 0;
63 extern thread_t dlil_input_thread_ptr
;
64 extern int dlil_input_thread_wakeup
;
68 protocol_family_t protocol
)
83 __private_extern__
void
86 lck_grp_attr_t
*grp_attrib
= 0;
87 lck_attr_t
*lck_attrib
= 0;
88 lck_grp_t
*lck_group
= 0;
90 /* Allocate a mtx lock */
91 grp_attrib
= lck_grp_attr_alloc_init();
92 lck_group
= lck_grp_alloc_init("protocol kpi", grp_attrib
);
93 lck_grp_attr_free(grp_attrib
);
94 lck_attrib
= lck_attr_alloc_init();
95 proto_input_lock
= lck_mtx_alloc_init(lck_group
, lck_attrib
);
96 lck_grp_free(lck_group
);
97 lck_attr_free(lck_attrib
);
100 __private_extern__ errno_t
101 proto_register_input(
102 protocol_family_t protocol
,
103 proto_input_handler input
,
104 proto_input_detached_handler detached
)
107 struct proto_input_entry
*entry
;
109 entry
= _MALLOC(sizeof(*entry
), M_IFADDR
, M_WAITOK
);
114 bzero(entry
, sizeof(*entry
));
115 entry
->protocol
= protocol
;
116 entry
->input
= input
;
117 entry
->detached
= detached
;
120 struct domain
*dp
= domains
;
121 extern lck_mtx_t
*domain_proto_mtx
;
123 lck_mtx_assert(domain_proto_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
124 lck_mtx_lock(domain_proto_mtx
);
125 while (dp
&& dp
->dom_family
!= protocol
)
128 lck_mtx_unlock(domain_proto_mtx
);
133 entry
->next
= proto_input_add_list
;
134 } while(!OSCompareAndSwap((UInt32
)entry
->next
, (UInt32
)entry
, (UInt32
*)&proto_input_add_list
));
136 wakeup((caddr_t
)&dlil_input_thread_wakeup
);
142 __private_extern__
void
143 proto_unregister_input(
144 protocol_family_t protocol
)
146 struct proto_input_entry
*entry
= NULL
;
148 for (entry
= proto_hash
[proto_hash_value(protocol
)]; entry
; entry
= entry
->next
)
149 if (entry
->protocol
== protocol
)
158 proto_delayed_attach(
159 struct proto_input_entry
*entry
)
161 struct proto_input_entry
*next_entry
;
162 for (next_entry
= entry
->next
; entry
; entry
= next_entry
) {
163 struct proto_input_entry
*exist
;
166 hash_slot
= proto_hash_value(entry
->protocol
);
167 next_entry
= entry
->next
;
169 for (exist
= proto_hash
[hash_slot
]; exist
; exist
= exist
->next
)
170 if (exist
->protocol
== entry
->protocol
)
173 /* If the entry already exists, call detached and dispose */
176 entry
->detached(entry
->protocol
);
177 FREE(entry
, M_IFADDR
);
180 entry
->next
= proto_hash
[hash_slot
];
181 proto_hash
[hash_slot
] = entry
;
187 proto_delayed_inject(
188 struct proto_input_entry
*entry
)
194 lck_mtx_lock(proto_input_lock
);
195 packet_list
= entry
->first_packet
;
196 entry
->first_packet
= entry
->last_packet
= 0;
197 lck_mtx_unlock(proto_input_lock
);
199 if (packet_list
== NULL
)
202 if (entry
->domain
&& (entry
->domain
->dom_flags
& DOM_REENTRANT
) == 0) {
203 lck_mtx_lock(entry
->domain
->dom_mtx
);
207 for (packet
= packet_list
; packet
; packet
= packet_list
) {
208 packet_list
= mbuf_nextpkt(packet
);
209 mbuf_setnextpkt(packet
, NULL
);
210 entry
->input(entry
->protocol
, packet
);
214 lck_mtx_unlock(entry
->domain
->dom_mtx
);
218 /* This function must be called from a single dlil input thread */
219 __private_extern__
void
220 proto_input_run(void)
222 struct proto_input_entry
*entry
;
226 if (current_thread() != dlil_input_thread_ptr
)
227 panic("proto_input_run called from a thread other than dlil_input_thread!\n");
230 entry
= proto_input_add_list
;
231 } while (entry
&& !OSCompareAndSwap((UInt32
)entry
, 0, (UInt32
*)&proto_input_add_list
));
234 proto_delayed_attach(entry
);
237 inject
= inject_buckets
;
238 } while (inject
&& !OSCompareAndSwap(inject
, 0, (UInt32
*)&inject_buckets
));
241 for (i
= 0; i
< PROTO_HASH_SLOTS
; i
++) {
242 if ((inject
& (1L << i
)) != 0) {
243 for (entry
= proto_hash
[i
]; entry
; entry
= entry
->next
) {
244 if (entry
->first_packet
) {
245 proto_delayed_inject(entry
);
255 protocol_family_t protocol
,
258 struct proto_input_entry
*entry
;
260 if (current_thread() != dlil_input_thread_ptr
)
261 panic("proto_input called from a thread other than dlil_input_thread!\n");
263 for (entry
= proto_hash
[proto_hash_value(protocol
)]; entry
; entry
= entry
->next
) {
264 if (entry
->protocol
== protocol
)
270 #if DIRECT_PROTO_INPUT
271 // See <rdar://problem/3687868> for why this is disabled
272 // We need to release the dlil lock before taking the protocol lock
273 for (packet
= packet_list
; packet
; packet
= packet_list
) {
274 packet_list
= mbuf_nextpkt(packet
);
275 mbuf_setnextpkt(packet
, NULL
);
276 entry
->input(entry
->protocol
, packet
);
280 int hash_slot
= proto_hash_value(protocol
);
282 for (last_packet
= packet_list
; mbuf_nextpkt(last_packet
);
283 last_packet
= mbuf_nextpkt(last_packet
))
284 /* find the last packet */;
286 lck_mtx_lock(proto_input_lock
);
287 if (entry
->first_packet
== NULL
) {
288 entry
->first_packet
= packet_list
;
291 mbuf_setnextpkt(entry
->last_packet
, packet_list
);
293 entry
->last_packet
= last_packet
;
294 lck_mtx_unlock(proto_input_lock
);
296 OSBitOrAtomic((1L << hash_slot
), (UInt32
*)&inject_buckets
);
309 protocol_family_t protocol
,
312 struct proto_input_entry
*entry
;
314 int hash_slot
= proto_hash_value(protocol
);
316 for (last_packet
= packet_list
; mbuf_nextpkt(last_packet
);
317 last_packet
= mbuf_nextpkt(last_packet
))
318 /* find the last packet */;
320 for (entry
= proto_hash
[hash_slot
]; entry
; entry
= entry
->next
) {
321 if (entry
->protocol
== protocol
)
326 lck_mtx_lock(proto_input_lock
);
327 if (entry
->first_packet
== NULL
) {
328 entry
->first_packet
= packet_list
;
331 mbuf_setnextpkt(entry
->last_packet
, packet_list
);
333 entry
->last_packet
= last_packet
;
334 lck_mtx_unlock(proto_input_lock
);
336 OSBitOrAtomic((1L << hash_slot
), (UInt32
*)&inject_buckets
);
338 wakeup((caddr_t
)&dlil_input_thread_wakeup
);
349 proto_register_plumber(
350 protocol_family_t proto_fam
,
351 ifnet_family_t if_fam
,
352 proto_plumb_handler plumb
,
353 proto_unplumb_handler unplumb
)
355 return dlil_reg_proto_module(proto_fam
, if_fam
, (attach_t
)plumb
, (detach_t
)unplumb
);
359 proto_unregister_plumber(
360 protocol_family_t proto_fam
,
361 ifnet_family_t if_fam
)
363 (void)dlil_dereg_proto_module(proto_fam
, if_fam
);