2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * a) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
14 * b) Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the distribution.
18 * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #ifndef _NETINET_SCTP_LOCK_BSD_H_
39 #define _NETINET_SCTP_LOCK_BSD_H_
42 * General locking concepts: The goal of our locking is to of course provide
43 * consistency and yet minimize overhead. We will attempt to use
44 * non-recursive locks which are supposed to be quite inexpensive. Now in
45 * order to do this the goal is that most functions are not aware of locking.
46 * Once we have a TCB we lock it and unlock when we are through. This means
47 * that the TCB lock is kind-of a "global" lock when working on an
48 * association. Caution must be used when asserting a TCB_LOCK since if we
49 * recurse we deadlock.
51 * Most other locks (INP and INFO) attempt to localize the locking i.e. we try
52 * to contain the lock and unlock within the function that needs to lock it.
53 * This sometimes mean we do extra locks and unlocks and lose a bit of
54 * efficiency, but if the performance statements about non-recursive locks are
55 * true this should not be a problem. One issue that arises with this only
56 * lock when needed is that if an implicit association setup is done we have
57 * a problem. If at the time I lookup an association I have NULL in the tcb
58 * return, by the time I call to create the association some other processor
59 * could have created it. This is what the CREATE lock on the endpoint.
60 * Places where we will be implicitly creating the association OR just
61 * creating an association (the connect call) will assert the CREATE_INP
62 * lock. This will assure us that during all the lookup of INP and INFO if
63 * another creator is also locking/looking up we can gate the two to
64 * synchronize. So the CREATE_INP lock is also another one we must use
65 * extreme caution in locking to make sure we don't hit a re-entrancy issue.
70 * When working with the global SCTP lists we lock and unlock the INP_INFO
71 * lock. So when we go to lookup an association we will want to do a
72 * SCTP_INP_INFO_RLOCK() and then when we want to add a new association to
73 * the SCTP_BASE_INFO() list's we will do a SCTP_INP_INFO_WLOCK().
76 #define SCTP_IPI_COUNT_INIT()
78 #define SCTP_STATLOG_INIT_LOCK()
79 #define SCTP_STATLOG_DESTROY()
80 #define SCTP_STATLOG_LOCK()
81 #define SCTP_STATLOG_UNLOCK()
83 #define SCTP_INP_INFO_LOCK_INIT() do { \
84 rw_init(&SCTP_BASE_INFO(ipi_ep_mtx), "sctp-info"); \
87 #define SCTP_INP_INFO_LOCK_DESTROY() do { \
88 if (rw_wowned(&SCTP_BASE_INFO(ipi_ep_mtx))) { \
89 rw_wunlock(&SCTP_BASE_INFO(ipi_ep_mtx)); \
91 rw_destroy(&SCTP_BASE_INFO(ipi_ep_mtx)); \
94 #define SCTP_INP_INFO_RLOCK() do { \
95 rw_rlock(&SCTP_BASE_INFO(ipi_ep_mtx)); \
98 #define SCTP_INP_INFO_WLOCK() do { \
99 rw_wlock(&SCTP_BASE_INFO(ipi_ep_mtx)); \
102 #define SCTP_INP_INFO_RUNLOCK() do { \
103 rw_runlock(&SCTP_BASE_INFO(ipi_ep_mtx)); \
106 #define SCTP_INP_INFO_WUNLOCK() do { \
107 rw_wunlock(&SCTP_BASE_INFO(ipi_ep_mtx)); \
111 #define SCTP_MCORE_QLOCK_INIT(cpstr) do { \
112 mtx_init(&(cpstr)->que_mtx, "sctp-mcore_queue","queue_lock", \
113 MTX_DEF | MTX_DUPOK); \
116 #define SCTP_MCORE_QDESTROY(cpstr) do { \
117 if (mtx_owned(&(cpstr)->core_mtx)) { \
118 mtx_unlock(&(cpstr)->que_mtx); \
120 mtx_destroy(&(cpstr)->que_mtx); \
123 #define SCTP_MCORE_QLOCK(cpstr) do { \
124 mtx_lock(&(cpstr)->que_mtx); \
127 #define SCTP_MCORE_QUNLOCK(cpstr) do { \
128 mtx_unlock(&(cpstr)->que_mtx); \
132 #define SCTP_MCORE_LOCK_INIT(cpstr) do { \
133 mtx_init(&(cpstr)->core_mtx, "sctp-cpulck","cpu_proc_lock", \
134 MTX_DEF | MTX_DUPOK); \
137 #define SCTP_MCORE_DESTROY(cpstr) do { \
138 if (mtx_owned(&(cpstr)->core_mtx)) { \
139 mtx_unlock(&(cpstr)->core_mtx); \
141 mtx_destroy(&(cpstr)->core_mtx); \
144 #define SCTP_MCORE_LOCK(cpstr) do { \
145 mtx_lock(&(cpstr)->core_mtx); \
148 #define SCTP_MCORE_UNLOCK(cpstr) do { \
149 mtx_unlock(&(cpstr)->core_mtx); \
153 #define SCTP_IPI_ADDR_INIT() do { \
154 rw_init(&SCTP_BASE_INFO(ipi_addr_mtx), "sctp-addr"); \
157 #define SCTP_IPI_ADDR_DESTROY() do { \
158 if (rw_wowned(&SCTP_BASE_INFO(ipi_addr_mtx))) { \
159 rw_wunlock(&SCTP_BASE_INFO(ipi_addr_mtx)); \
161 rw_destroy(&SCTP_BASE_INFO(ipi_addr_mtx)); \
164 #define SCTP_IPI_ADDR_RLOCK() do { \
165 rw_rlock(&SCTP_BASE_INFO(ipi_addr_mtx)); \
168 #define SCTP_IPI_ADDR_WLOCK() do { \
169 rw_wlock(&SCTP_BASE_INFO(ipi_addr_mtx)); \
172 #define SCTP_IPI_ADDR_RUNLOCK() do { \
173 rw_runlock(&SCTP_BASE_INFO(ipi_addr_mtx)); \
176 #define SCTP_IPI_ADDR_WUNLOCK() do { \
177 rw_wunlock(&SCTP_BASE_INFO(ipi_addr_mtx)); \
181 #define SCTP_IPI_ITERATOR_WQ_INIT() do { \
182 mtx_init(&sctp_it_ctl.ipi_iterator_wq_mtx, "sctp-it-wq", \
183 "sctp_it_wq", MTX_DEF); \
186 #define SCTP_IPI_ITERATOR_WQ_DESTROY() do { \
187 mtx_destroy(&sctp_it_ctl.ipi_iterator_wq_mtx); \
190 #define SCTP_IPI_ITERATOR_WQ_LOCK() do { \
191 mtx_lock(&sctp_it_ctl.ipi_iterator_wq_mtx); \
194 #define SCTP_IPI_ITERATOR_WQ_UNLOCK() do { \
195 mtx_unlock(&sctp_it_ctl.ipi_iterator_wq_mtx); \
199 #define SCTP_IP_PKTLOG_INIT() do { \
200 mtx_init(&SCTP_BASE_INFO(ipi_pktlog_mtx), "sctp-pktlog", \
201 "packetlog", MTX_DEF); \
204 #define SCTP_IP_PKTLOG_DESTROY() do { \
205 mtx_destroy(&SCTP_BASE_INFO(ipi_pktlog_mtx)); \
208 #define SCTP_IP_PKTLOG_LOCK() do { \
209 mtx_lock(&SCTP_BASE_INFO(ipi_pktlog_mtx)); \
212 #define SCTP_IP_PKTLOG_UNLOCK() do { \
213 mtx_unlock(&SCTP_BASE_INFO(ipi_pktlog_mtx)); \
218 * The INP locks we will use for locking an SCTP endpoint, so for example if
219 * we want to change something at the endpoint level for example random_store
220 * or cookie secrets we lock the INP level.
223 #define SCTP_INP_READ_INIT(_inp) do { \
224 mtx_init(&(_inp)->inp_rdata_mtx, "sctp-read", "inpr", \
225 MTX_DEF | MTX_DUPOK); \
228 #define SCTP_INP_READ_DESTROY(_inp) do { \
229 mtx_destroy(&(_inp)->inp_rdata_mtx); \
232 #define SCTP_INP_READ_LOCK(_inp) do { \
233 mtx_lock(&(_inp)->inp_rdata_mtx); \
236 #define SCTP_INP_READ_UNLOCK(_inp) do { \
237 mtx_unlock(&(_inp)->inp_rdata_mtx); \
241 #define SCTP_INP_LOCK_INIT(_inp) do { \
242 mtx_init(&(_inp)->inp_mtx, "sctp-inp", "inp", \
243 MTX_DEF | MTX_DUPOK); \
246 #define SCTP_INP_LOCK_DESTROY(_inp) do { \
247 mtx_destroy(&(_inp)->inp_mtx); \
250 #define SCTP_INP_LOCK_CONTENDED(_inp) \
251 ((_inp)->inp_mtx.mtx_lock & MTX_CONTESTED)
253 #define SCTP_INP_READ_CONTENDED(_inp) \
254 ((_inp)->inp_rdata_mtx.mtx_lock & MTX_CONTESTED)
256 #ifdef SCTP_LOCK_LOGGING
257 #define SCTP_INP_RLOCK(_inp) do { \
258 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) \
259 sctp_log_lock(_inp, NULL, SCTP_LOG_LOCK_INP); \
260 mtx_lock(&(_inp)->inp_mtx); \
263 #define SCTP_INP_WLOCK(_inp) do { \
264 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) \
265 sctp_log_lock(_inp, NULL, SCTP_LOG_LOCK_INP); \
266 mtx_lock(&(_inp)->inp_mtx); \
269 #define SCTP_INP_RLOCK(_inp) do { \
270 mtx_lock(&(_inp)->inp_mtx); \
273 #define SCTP_INP_WLOCK(_inp) do { \
274 mtx_lock(&(_inp)->inp_mtx); \
278 #define SCTP_INP_RUNLOCK(_inp) do { \
279 mtx_unlock(&(_inp)->inp_mtx); \
282 #define SCTP_INP_WUNLOCK(_inp) do { \
283 mtx_unlock(&(_inp)->inp_mtx); \
286 #define SCTP_INP_RLOCK_ASSERT(_inp) do { \
287 KASSERT(mtx_owned(&(_inp)->inp_mtx), \
288 ("Don't own INP read lock")); \
291 #define SCTP_INP_WLOCK_ASSERT(_inp) do { \
292 KASSERT(mtx_owned(&(_inp)->inp_mtx), \
293 ("Don't own INP write lock")); \
296 #define SCTP_INP_INCR_REF(_inp) atomic_add_int(&((_inp)->refcount), 1)
297 #define SCTP_INP_DECR_REF(_inp) atomic_add_int(&((_inp)->refcount), -1)
299 #define SCTP_ASOC_CREATE_LOCK_INIT(_inp) do { \
300 mtx_init(&(_inp)->inp_create_mtx, "sctp-create", "inp_create", \
301 MTX_DEF | MTX_DUPOK); \
304 #define SCTP_ASOC_CREATE_LOCK_DESTROY(_inp) do { \
305 mtx_destroy(&(_inp)->inp_create_mtx); \
308 #ifdef SCTP_LOCK_LOGGING
309 #define SCTP_ASOC_CREATE_LOCK(_inp) do { \
310 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) \
311 sctp_log_lock(_inp, NULL, SCTP_LOG_LOCK_CREATE); \
312 mtx_lock(&(_inp)->inp_create_mtx); \
315 #define SCTP_ASOC_CREATE_LOCK(_inp) do { \
316 mtx_lock(&(_inp)->inp_create_mtx); \
320 #define SCTP_ASOC_CREATE_UNLOCK(_inp) do { \
321 mtx_unlock(&(_inp)->inp_create_mtx); \
324 #define SCTP_ASOC_CREATE_LOCK_CONTENDED(_inp) \
325 ((_inp)->inp_create_mtx.mtx_lock & MTX_CONTESTED)
328 #define SCTP_TCB_SEND_LOCK_INIT(_tcb) do { \
329 mtx_init(&(_tcb)->tcb_send_mtx, "sctp-send-tcb", "tcbs", \
330 MTX_DEF | MTX_DUPOK); \
333 #define SCTP_TCB_SEND_LOCK_DESTROY(_tcb) do { \
334 mtx_destroy(&(_tcb)->tcb_send_mtx); \
337 #define SCTP_TCB_SEND_LOCK(_tcb) do { \
338 mtx_lock(&(_tcb)->tcb_send_mtx); \
341 #define SCTP_TCB_SEND_UNLOCK(_tcb) do { \
342 mtx_unlock(&(_tcb)->tcb_send_mtx); \
346 * For the majority of things (once we have found the association) we will
347 * lock the actual association mutex. This will protect all the assoiciation
348 * level queues and streams and such. We will need to lock the socket layer
349 * when we stuff data up into the receiving sb_mb. I.e. we will need to do an
350 * extra SOCKBUF_LOCK(&so->so_rcv) even though the association is locked.
353 #define SCTP_TCB_LOCK_INIT(_tcb) do { \
354 mtx_init(&(_tcb)->tcb_mtx, "sctp-tcb", "tcb", \
355 MTX_DEF | MTX_DUPOK); \
358 #define SCTP_TCB_LOCK_DESTROY(_tcb) do { \
359 mtx_destroy(&(_tcb)->tcb_mtx); \
362 #ifdef SCTP_LOCK_LOGGING
363 #define SCTP_TCB_LOCK(_tcb) do { \
364 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) \
365 sctp_log_lock(_tcb->sctp_ep, _tcb, SCTP_LOG_LOCK_TCB); \
366 mtx_lock(&(_tcb)->tcb_mtx); \
369 #define SCTP_TCB_LOCK(_tcb) do { \
370 mtx_lock(&(_tcb)->tcb_mtx); \
375 #define SCTP_TCB_TRYLOCK(_tcb) \
376 mtx_trylock(&(_tcb)->tcb_mtx)
378 #define SCTP_TCB_UNLOCK(_tcb) do { \
379 mtx_unlock(&(_tcb)->tcb_mtx); \
382 #define SCTP_TCB_UNLOCK_IFOWNED(_tcb) do { \
383 if (mtx_owned(&(_tcb)->tcb_mtx)) \
384 mtx_unlock(&(_tcb)->tcb_mtx); \
387 #define SCTP_TCB_LOCK_ASSERT(_tcb) do { \
388 KASSERT(mtx_owned(&(_tcb)->tcb_mtx), \
389 ("Don't own TCB lock")); \
393 #define SCTP_ITERATOR_LOCK_INIT() do { \
394 mtx_init(&sctp_it_ctl.it_mtx, "sctp-it", "iterator", MTX_DEF); \
397 #define SCTP_ITERATOR_LOCK_DESTROY() do { \
398 mtx_destroy(&sctp_it_ctl.it_mtx); \
401 #define SCTP_ITERATOR_LOCK() \
403 KASSERT(!mtx_owned(&sctp_it_ctl.it_mtx), \
404 ("Own the iterator lock")); \
405 mtx_lock(&sctp_it_ctl.it_mtx); \
408 #define SCTP_ITERATOR_UNLOCK() do { \
409 mtx_unlock(&sctp_it_ctl.it_mtx); \
413 #define SCTP_WQ_ADDR_INIT() do { \
414 mtx_init(&SCTP_BASE_INFO(wq_addr_mtx), \
415 "sctp-addr-wq","sctp_addr_wq", MTX_DEF); \
418 #define SCTP_WQ_ADDR_DESTROY() do { \
419 if (mtx_owned(&SCTP_BASE_INFO(wq_addr_mtx))) { \
420 mtx_unlock(&SCTP_BASE_INFO(wq_addr_mtx)); \
422 mtx_destroy(&SCTP_BASE_INFO(wq_addr_mtx)); \
425 #define SCTP_WQ_ADDR_LOCK() do { \
426 mtx_lock(&SCTP_BASE_INFO(wq_addr_mtx)); \
429 #define SCTP_WQ_ADDR_UNLOCK() do { \
430 mtx_unlock(&SCTP_BASE_INFO(wq_addr_mtx)); \
433 #define SCTP_WQ_ADDR_LOCK_ASSERT() do { \
434 KASSERT(mtx_owned(&SCTP_BASE_INFO(wq_addr_mtx)), \
435 ("Don't own the ADDR-WQ lock")); \
438 #define SCTP_INCR_EP_COUNT() do { \
439 atomic_add_int(&SCTP_BASE_INFO(ipi_count_ep), 1); \
442 #define SCTP_DECR_EP_COUNT() do { \
443 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ep), 1); \
446 #define SCTP_INCR_ASOC_COUNT() do { \
447 atomic_add_int(&SCTP_BASE_INFO(ipi_count_asoc), 1); \
450 #define SCTP_DECR_ASOC_COUNT() do { \
451 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_asoc), 1); \
454 #define SCTP_INCR_LADDR_COUNT() do { \
455 atomic_add_int(&SCTP_BASE_INFO(ipi_count_laddr), 1); \
458 #define SCTP_DECR_LADDR_COUNT() do { \
459 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_laddr), 1); \
462 #define SCTP_INCR_RADDR_COUNT() do { \
463 atomic_add_int(&SCTP_BASE_INFO(ipi_count_raddr), 1); \
466 #define SCTP_DECR_RADDR_COUNT() do { \
467 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_raddr),1); \
470 #define SCTP_INCR_CHK_COUNT() do { \
471 atomic_add_int(&SCTP_BASE_INFO(ipi_count_chunk), 1); \
474 #define SCTP_DECR_CHK_COUNT() do { \
475 KASSERT(SCTP_BASE_INFO(ipi_count_chunk) > 0, \
476 ("ipi_count_chunk would become negative")); \
477 if (SCTP_BASE_INFO(ipi_count_chunk) != 0) \
478 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_chunk), \
482 #define SCTP_INCR_READQ_COUNT() do { \
483 atomic_add_int(&SCTP_BASE_INFO(ipi_count_readq), 1); \
486 #define SCTP_DECR_READQ_COUNT() do { \
487 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_readq), 1); \
490 #define SCTP_INCR_STRMOQ_COUNT() do { \
491 atomic_add_int(&SCTP_BASE_INFO(ipi_count_strmoq), 1); \
494 #define SCTP_DECR_STRMOQ_COUNT() do { \
495 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_strmoq), 1); \
498 #if defined(SCTP_SO_LOCK_TESTING)
499 #define SCTP_INP_SO(sctpinp) \
500 (sctpinp)->ip_inp.inp.inp_socket
501 #define SCTP_SOCKET_LOCK(so, refcnt)
502 #define SCTP_SOCKET_UNLOCK(so, refcnt)