38#ifndef _NETINET_SCTP_LOCK_BSD_H_
39#define _NETINET_SCTP_LOCK_BSD_H_
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)); \
110#define SCTP_INP_INFO_LOCK_ASSERT() do { \
111 rw_assert(&SCTP_BASE_INFO(ipi_ep_mtx), RA_LOCKED); \
114#define SCTP_INP_INFO_RLOCK_ASSERT() do { \
115 rw_assert(&SCTP_BASE_INFO(ipi_ep_mtx), RA_RLOCKED); \
118#define SCTP_INP_INFO_WLOCK_ASSERT() do { \
119 rw_assert(&SCTP_BASE_INFO(ipi_ep_mtx), RA_WLOCKED); \
122#define SCTP_MCORE_QLOCK_INIT(cpstr) do { \
123 mtx_init(&(cpstr)->que_mtx, "sctp-mcore_queue","queue_lock", \
124 MTX_DEF | MTX_DUPOK); \
127#define SCTP_MCORE_QDESTROY(cpstr) do { \
128 if (mtx_owned(&(cpstr)->core_mtx)) { \
129 mtx_unlock(&(cpstr)->que_mtx); \
131 mtx_destroy(&(cpstr)->que_mtx); \
134#define SCTP_MCORE_QLOCK(cpstr) do { \
135 mtx_lock(&(cpstr)->que_mtx); \
138#define SCTP_MCORE_QUNLOCK(cpstr) do { \
139 mtx_unlock(&(cpstr)->que_mtx); \
142#define SCTP_MCORE_LOCK_INIT(cpstr) do { \
143 mtx_init(&(cpstr)->core_mtx, "sctp-cpulck","cpu_proc_lock", \
144 MTX_DEF | MTX_DUPOK); \
147#define SCTP_MCORE_DESTROY(cpstr) do { \
148 if (mtx_owned(&(cpstr)->core_mtx)) { \
149 mtx_unlock(&(cpstr)->core_mtx); \
151 mtx_destroy(&(cpstr)->core_mtx); \
154#define SCTP_MCORE_LOCK(cpstr) do { \
155 mtx_lock(&(cpstr)->core_mtx); \
158#define SCTP_MCORE_UNLOCK(cpstr) do { \
159 mtx_unlock(&(cpstr)->core_mtx); \
162#define SCTP_IPI_ADDR_INIT() do { \
163 rw_init(&SCTP_BASE_INFO(ipi_addr_mtx), "sctp-addr"); \
166#define SCTP_IPI_ADDR_DESTROY() do { \
167 if (rw_wowned(&SCTP_BASE_INFO(ipi_addr_mtx))) { \
168 rw_wunlock(&SCTP_BASE_INFO(ipi_addr_mtx)); \
170 rw_destroy(&SCTP_BASE_INFO(ipi_addr_mtx)); \
173#define SCTP_IPI_ADDR_RLOCK() do { \
174 rw_rlock(&SCTP_BASE_INFO(ipi_addr_mtx)); \
177#define SCTP_IPI_ADDR_WLOCK() do { \
178 rw_wlock(&SCTP_BASE_INFO(ipi_addr_mtx)); \
181#define SCTP_IPI_ADDR_RUNLOCK() do { \
182 rw_runlock(&SCTP_BASE_INFO(ipi_addr_mtx)); \
185#define SCTP_IPI_ADDR_WUNLOCK() do { \
186 rw_wunlock(&SCTP_BASE_INFO(ipi_addr_mtx)); \
189#define SCTP_IPI_ADDR_LOCK_ASSERT() do { \
190 rw_assert(&SCTP_BASE_INFO(ipi_addr_mtx), RA_LOCKED); \
193#define SCTP_IPI_ADDR_WLOCK_ASSERT() do { \
194 rw_assert(&SCTP_BASE_INFO(ipi_addr_mtx), RA_WLOCKED); \
197#define SCTP_IPI_ITERATOR_WQ_INIT() do { \
198 mtx_init(&sctp_it_ctl.ipi_iterator_wq_mtx, "sctp-it-wq", \
199 "sctp_it_wq", MTX_DEF); \
202#define SCTP_IPI_ITERATOR_WQ_DESTROY() do { \
203 mtx_destroy(&sctp_it_ctl.ipi_iterator_wq_mtx); \
206#define SCTP_IPI_ITERATOR_WQ_LOCK() do { \
207 mtx_lock(&sctp_it_ctl.ipi_iterator_wq_mtx); \
210#define SCTP_IPI_ITERATOR_WQ_UNLOCK() do { \
211 mtx_unlock(&sctp_it_ctl.ipi_iterator_wq_mtx); \
214#define SCTP_IP_PKTLOG_INIT() do { \
215 mtx_init(&SCTP_BASE_INFO(ipi_pktlog_mtx), "sctp-pktlog", \
216 "packetlog", MTX_DEF); \
219#define SCTP_IP_PKTLOG_DESTROY() do { \
220 mtx_destroy(&SCTP_BASE_INFO(ipi_pktlog_mtx)); \
223#define SCTP_IP_PKTLOG_LOCK() do { \
224 mtx_lock(&SCTP_BASE_INFO(ipi_pktlog_mtx)); \
227#define SCTP_IP_PKTLOG_UNLOCK() do { \
228 mtx_unlock(&SCTP_BASE_INFO(ipi_pktlog_mtx)); \
237#define SCTP_INP_READ_INIT(_inp) do { \
238 mtx_init(&(_inp)->inp_rdata_mtx, "sctp-read", "inpr", \
239 MTX_DEF | MTX_DUPOK); \
242#define SCTP_INP_READ_DESTROY(_inp) do { \
243 mtx_destroy(&(_inp)->inp_rdata_mtx); \
246#define SCTP_INP_READ_LOCK(_inp) do { \
247 mtx_lock(&(_inp)->inp_rdata_mtx); \
250#define SCTP_INP_READ_UNLOCK(_inp) do { \
251 mtx_unlock(&(_inp)->inp_rdata_mtx); \
254#define SCTP_INP_LOCK_INIT(_inp) do { \
255 mtx_init(&(_inp)->inp_mtx, "sctp-inp", "inp", \
256 MTX_DEF | MTX_DUPOK); \
259#define SCTP_INP_LOCK_DESTROY(_inp) do { \
260 mtx_destroy(&(_inp)->inp_mtx); \
263#define SCTP_INP_LOCK_CONTENDED(_inp) \
264 ((_inp)->inp_mtx.mtx_lock & MTX_CONTESTED)
266#define SCTP_INP_READ_CONTENDED(_inp) \
267 ((_inp)->inp_rdata_mtx.mtx_lock & MTX_CONTESTED)
269#ifdef SCTP_LOCK_LOGGING
270#define SCTP_INP_RLOCK(_inp) do { \
271 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) \
272 sctp_log_lock(_inp, NULL, SCTP_LOG_LOCK_INP); \
273 mtx_lock(&(_inp)->inp_mtx); \
276#define SCTP_INP_WLOCK(_inp) do { \
277 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) \
278 sctp_log_lock(_inp, NULL, SCTP_LOG_LOCK_INP); \
279 mtx_lock(&(_inp)->inp_mtx); \
282#define SCTP_INP_RLOCK(_inp) do { \
283 mtx_lock(&(_inp)->inp_mtx); \
286#define SCTP_INP_WLOCK(_inp) do { \
287 mtx_lock(&(_inp)->inp_mtx); \
291#define SCTP_INP_RUNLOCK(_inp) do { \
292 mtx_unlock(&(_inp)->inp_mtx); \
295#define SCTP_INP_WUNLOCK(_inp) do { \
296 mtx_unlock(&(_inp)->inp_mtx); \
299#define SCTP_INP_RLOCK_ASSERT(_inp) do { \
300 KASSERT(mtx_owned(&(_inp)->inp_mtx), \
301 ("Don't own INP read lock")); \
304#define SCTP_INP_WLOCK_ASSERT(_inp) do { \
305 KASSERT(mtx_owned(&(_inp)->inp_mtx), \
306 ("Don't own INP write lock")); \
309#define SCTP_INP_INCR_REF(_inp) atomic_add_int(&((_inp)->refcount), 1)
310#define SCTP_INP_DECR_REF(_inp) atomic_add_int(&((_inp)->refcount), -1)
312#define SCTP_ASOC_CREATE_LOCK_INIT(_inp) do { \
313 mtx_init(&(_inp)->inp_create_mtx, "sctp-create", "inp_create", \
314 MTX_DEF | MTX_DUPOK); \
317#define SCTP_ASOC_CREATE_LOCK_DESTROY(_inp) do { \
318 mtx_destroy(&(_inp)->inp_create_mtx); \
321#ifdef SCTP_LOCK_LOGGING
322#define SCTP_ASOC_CREATE_LOCK(_inp) do { \
323 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) \
324 sctp_log_lock(_inp, NULL, SCTP_LOG_LOCK_CREATE); \
325 mtx_lock(&(_inp)->inp_create_mtx); \
328#define SCTP_ASOC_CREATE_LOCK(_inp) do { \
329 mtx_lock(&(_inp)->inp_create_mtx); \
333#define SCTP_ASOC_CREATE_UNLOCK(_inp) do { \
334 mtx_unlock(&(_inp)->inp_create_mtx); \
337#define SCTP_ASOC_CREATE_LOCK_CONTENDED(_inp) \
338 ((_inp)->inp_create_mtx.mtx_lock & MTX_CONTESTED)
340#define SCTP_TCB_SEND_LOCK_INIT(_tcb) do { \
341 mtx_init(&(_tcb)->tcb_send_mtx, "sctp-send-tcb", "tcbs", \
342 MTX_DEF | MTX_DUPOK); \
345#define SCTP_TCB_SEND_LOCK_DESTROY(_tcb) do { \
346 mtx_destroy(&(_tcb)->tcb_send_mtx); \
349#define SCTP_TCB_SEND_LOCK(_tcb) do { \
350 mtx_lock(&(_tcb)->tcb_send_mtx); \
353#define SCTP_TCB_SEND_UNLOCK(_tcb) do { \
354 mtx_unlock(&(_tcb)->tcb_send_mtx); \
357#define SCTP_TCB_SEND_LOCK_ASSERT(_tcb) do { \
358 KASSERT(mtx_owned(&(_tcb)->tcb_send_mtx), \
359 ("Don't own TCB send lock")); \
370#define SCTP_TCB_LOCK_INIT(_tcb) do { \
371 mtx_init(&(_tcb)->tcb_mtx, "sctp-tcb", "tcb", \
372 MTX_DEF | MTX_DUPOK); \
375#define SCTP_TCB_LOCK_DESTROY(_tcb) do { \
376 mtx_destroy(&(_tcb)->tcb_mtx); \
379#ifdef SCTP_LOCK_LOGGING
380#define SCTP_TCB_LOCK(_tcb) do { \
381 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) \
382 sctp_log_lock(_tcb->sctp_ep, _tcb, SCTP_LOG_LOCK_TCB); \
383 mtx_lock(&(_tcb)->tcb_mtx); \
386#define SCTP_TCB_LOCK(_tcb) do { \
387 mtx_lock(&(_tcb)->tcb_mtx); \
392#define SCTP_TCB_TRYLOCK(_tcb) \
393 mtx_trylock(&(_tcb)->tcb_mtx)
395#define SCTP_TCB_UNLOCK(_tcb) do { \
396 mtx_unlock(&(_tcb)->tcb_mtx); \
399#define SCTP_TCB_UNLOCK_IFOWNED(_tcb) do { \
400 if (mtx_owned(&(_tcb)->tcb_mtx)) \
401 mtx_unlock(&(_tcb)->tcb_mtx); \
404#define SCTP_TCB_LOCK_ASSERT(_tcb) do { \
405 KASSERT(mtx_owned(&(_tcb)->tcb_mtx), \
406 ("Don't own TCB lock")); \
409#define SCTP_ITERATOR_LOCK_INIT() do { \
410 mtx_init(&sctp_it_ctl.it_mtx, "sctp-it", "iterator", MTX_DEF); \
413#define SCTP_ITERATOR_LOCK_DESTROY() do { \
414 mtx_destroy(&sctp_it_ctl.it_mtx); \
417#define SCTP_ITERATOR_LOCK() \
419 KASSERT(!mtx_owned(&sctp_it_ctl.it_mtx), \
420 ("Own the iterator lock")); \
421 mtx_lock(&sctp_it_ctl.it_mtx); \
424#define SCTP_ITERATOR_UNLOCK() do { \
425 mtx_unlock(&sctp_it_ctl.it_mtx); \
428#define SCTP_WQ_ADDR_INIT() do { \
429 mtx_init(&SCTP_BASE_INFO(wq_addr_mtx), \
430 "sctp-addr-wq","sctp_addr_wq", MTX_DEF); \
433#define SCTP_WQ_ADDR_DESTROY() do { \
434 if (mtx_owned(&SCTP_BASE_INFO(wq_addr_mtx))) { \
435 mtx_unlock(&SCTP_BASE_INFO(wq_addr_mtx)); \
437 mtx_destroy(&SCTP_BASE_INFO(wq_addr_mtx)); \
440#define SCTP_WQ_ADDR_LOCK() do { \
441 mtx_lock(&SCTP_BASE_INFO(wq_addr_mtx)); \
444#define SCTP_WQ_ADDR_UNLOCK() do { \
445 mtx_unlock(&SCTP_BASE_INFO(wq_addr_mtx)); \
448#define SCTP_WQ_ADDR_LOCK_ASSERT() do { \
449 KASSERT(mtx_owned(&SCTP_BASE_INFO(wq_addr_mtx)), \
450 ("Don't own the ADDR-WQ lock")); \
453#define SCTP_INCR_EP_COUNT() do { \
454 atomic_add_int(&SCTP_BASE_INFO(ipi_count_ep), 1); \
457#define SCTP_DECR_EP_COUNT() do { \
458 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ep), 1); \
461#define SCTP_INCR_ASOC_COUNT() do { \
462 atomic_add_int(&SCTP_BASE_INFO(ipi_count_asoc), 1); \
465#define SCTP_DECR_ASOC_COUNT() do { \
466 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_asoc), 1); \
469#define SCTP_INCR_LADDR_COUNT() do { \
470 atomic_add_int(&SCTP_BASE_INFO(ipi_count_laddr), 1); \
473#define SCTP_DECR_LADDR_COUNT() do { \
474 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_laddr), 1); \
477#define SCTP_INCR_RADDR_COUNT() do { \
478 atomic_add_int(&SCTP_BASE_INFO(ipi_count_raddr), 1); \
481#define SCTP_DECR_RADDR_COUNT() do { \
482 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_raddr),1); \
485#define SCTP_INCR_CHK_COUNT() do { \
486 atomic_add_int(&SCTP_BASE_INFO(ipi_count_chunk), 1); \
489#define SCTP_DECR_CHK_COUNT() do { \
490 KASSERT(SCTP_BASE_INFO(ipi_count_chunk) > 0, \
491 ("ipi_count_chunk would become negative")); \
492 if (SCTP_BASE_INFO(ipi_count_chunk) != 0) \
493 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_chunk), \
497#define SCTP_INCR_READQ_COUNT() do { \
498 atomic_add_int(&SCTP_BASE_INFO(ipi_count_readq), 1); \
501#define SCTP_DECR_READQ_COUNT() do { \
502 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_readq), 1); \
505#define SCTP_INCR_STRMOQ_COUNT() do { \
506 atomic_add_int(&SCTP_BASE_INFO(ipi_count_strmoq), 1); \
509#define SCTP_DECR_STRMOQ_COUNT() do { \
510 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_strmoq), 1); \
513#if defined(SCTP_SO_LOCK_TESTING)
514#define SCTP_INP_SO(sctpinp) \
515 (sctpinp)->ip_inp.inp.inp_socket
516#define SCTP_SOCKET_LOCK(so, refcnt)
517#define SCTP_SOCKET_UNLOCK(so, refcnt)