xref: /freebsd/sys/netinet/sctp_lock_bsd.h (revision f5147e312f43a9050468de539aeafa072caa1a60)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
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.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * a) Redistributions of source code must retain the above copyright notice,
12  *   this list of conditions and the following disclaimer.
13  *
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.
17  *
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.
21  *
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.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #ifndef _NETINET_SCTP_LOCK_BSD_H_
39 #define _NETINET_SCTP_LOCK_BSD_H_
40 
41 /*
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.
50  *
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.
66  *
67  * For non FreeBSD 5.x we provide a bunch of EMPTY lock macros so we can
68  * blatantly put locks everywhere and they reduce to nothing on
69  * NetBSD/OpenBSD and FreeBSD 4.x
70  *
71  */
72 
73 /*
74  * When working with the global SCTP lists we lock and unlock the INP_INFO
75  * lock. So when we go to lookup an association we will want to do a
76  * SCTP_INP_INFO_RLOCK() and then when we want to add a new association to
77  * the SCTP_BASE_INFO() list's we will do a SCTP_INP_INFO_WLOCK().
78  */
79 
80 extern struct sctp_foo_stuff sctp_logoff[];
81 extern int sctp_logoff_stuff;
82 
83 #define SCTP_IPI_COUNT_INIT()
84 
85 #define SCTP_STATLOG_INIT_LOCK()
86 #define SCTP_STATLOG_LOCK()
87 #define SCTP_STATLOG_UNLOCK()
88 #define SCTP_STATLOG_DESTROY()
89 
90 #define SCTP_INP_INFO_LOCK_DESTROY() do { \
91         if(rw_wowned(&SCTP_BASE_INFO(ipi_ep_mtx))) { \
92              rw_wunlock(&SCTP_BASE_INFO(ipi_ep_mtx)); \
93         } \
94         rw_destroy(&SCTP_BASE_INFO(ipi_ep_mtx)); \
95       }  while (0)
96 
97 #define SCTP_INP_INFO_LOCK_INIT() \
98         rw_init(&SCTP_BASE_INFO(ipi_ep_mtx), "sctp-info");
99 
100 
101 #define SCTP_INP_INFO_RLOCK()	do { 					\
102              rw_rlock(&SCTP_BASE_INFO(ipi_ep_mtx));                         \
103 } while (0)
104 
105 #define SCTP_MCORE_QLOCK_INIT(cpstr) do { \
106 		mtx_init(&(cpstr)->que_mtx,	      \
107 			 "sctp-mcore_queue","queue_lock",	\
108 			 MTX_DEF|MTX_DUPOK);		\
109 } while (0)
110 
111 #define SCTP_MCORE_QLOCK(cpstr)  do { \
112 		mtx_lock(&(cpstr)->que_mtx);	\
113 } while (0)
114 
115 #define SCTP_MCORE_QUNLOCK(cpstr)  do { \
116 		mtx_unlock(&(cpstr)->que_mtx);	\
117 } while (0)
118 
119 #define SCTP_MCORE_QDESTROY(cpstr)  do { \
120 	if(mtx_owned(&(cpstr)->core_mtx)) {	\
121 		mtx_unlock(&(cpstr)->que_mtx);	\
122         } \
123 	mtx_destroy(&(cpstr)->que_mtx);	\
124 } while (0)
125 
126 
127 #define SCTP_MCORE_LOCK_INIT(cpstr) do { \
128 		mtx_init(&(cpstr)->core_mtx,	      \
129 			 "sctp-cpulck","cpu_proc_lock",	\
130 			 MTX_DEF|MTX_DUPOK);		\
131 } while (0)
132 
133 #define SCTP_MCORE_LOCK(cpstr)  do { \
134 		mtx_lock(&(cpstr)->core_mtx);	\
135 } while (0)
136 
137 #define SCTP_MCORE_UNLOCK(cpstr)  do { \
138 		mtx_unlock(&(cpstr)->core_mtx);	\
139 } while (0)
140 
141 #define SCTP_MCORE_DESTROY(cpstr)  do { \
142 	if(mtx_owned(&(cpstr)->core_mtx)) {	\
143 		mtx_unlock(&(cpstr)->core_mtx);	\
144         } \
145 	mtx_destroy(&(cpstr)->core_mtx);	\
146 } while (0)
147 
148 #define SCTP_INP_INFO_WLOCK()	do { 					\
149             rw_wlock(&SCTP_BASE_INFO(ipi_ep_mtx));                         \
150 } while (0)
151 
152 
153 #define SCTP_INP_INFO_RUNLOCK()		rw_runlock(&SCTP_BASE_INFO(ipi_ep_mtx))
154 #define SCTP_INP_INFO_WUNLOCK()		rw_wunlock(&SCTP_BASE_INFO(ipi_ep_mtx))
155 
156 
157 #define SCTP_IPI_ADDR_INIT()								\
158         rw_init(&SCTP_BASE_INFO(ipi_addr_mtx), "sctp-addr")
159 #define SCTP_IPI_ADDR_DESTROY() do  { \
160         if(rw_wowned(&SCTP_BASE_INFO(ipi_addr_mtx))) { \
161              rw_wunlock(&SCTP_BASE_INFO(ipi_addr_mtx)); \
162         } \
163 	rw_destroy(&SCTP_BASE_INFO(ipi_addr_mtx)); \
164       }  while (0)
165 #define SCTP_IPI_ADDR_RLOCK()	do { 					\
166              rw_rlock(&SCTP_BASE_INFO(ipi_addr_mtx));                         \
167 } while (0)
168 #define SCTP_IPI_ADDR_WLOCK()	do { 					\
169              rw_wlock(&SCTP_BASE_INFO(ipi_addr_mtx));                         \
170 } while (0)
171 
172 #define SCTP_IPI_ADDR_RUNLOCK()		rw_runlock(&SCTP_BASE_INFO(ipi_addr_mtx))
173 #define SCTP_IPI_ADDR_WUNLOCK()		rw_wunlock(&SCTP_BASE_INFO(ipi_addr_mtx))
174 
175 
176 #define SCTP_IPI_ITERATOR_WQ_INIT() \
177         mtx_init(&sctp_it_ctl.ipi_iterator_wq_mtx, "sctp-it-wq", "sctp_it_wq", MTX_DEF)
178 
179 #define SCTP_IPI_ITERATOR_WQ_DESTROY() \
180 	mtx_destroy(&sctp_it_ctl.ipi_iterator_wq_mtx)
181 
182 #define SCTP_IPI_ITERATOR_WQ_LOCK()	do { 					\
183              mtx_lock(&sctp_it_ctl.ipi_iterator_wq_mtx);                \
184 } while (0)
185 
186 #define SCTP_IPI_ITERATOR_WQ_UNLOCK()		mtx_unlock(&sctp_it_ctl.ipi_iterator_wq_mtx)
187 
188 
189 #define SCTP_IP_PKTLOG_INIT() \
190         mtx_init(&SCTP_BASE_INFO(ipi_pktlog_mtx), "sctp-pktlog", "packetlog", MTX_DEF)
191 
192 
193 #define SCTP_IP_PKTLOG_LOCK()	do { 			\
194              mtx_lock(&SCTP_BASE_INFO(ipi_pktlog_mtx));     \
195 } while (0)
196 
197 #define SCTP_IP_PKTLOG_UNLOCK()	mtx_unlock(&SCTP_BASE_INFO(ipi_pktlog_mtx))
198 
199 #define SCTP_IP_PKTLOG_DESTROY() \
200 	mtx_destroy(&SCTP_BASE_INFO(ipi_pktlog_mtx))
201 
202 
203 
204 
205 
206 /*
207  * The INP locks we will use for locking an SCTP endpoint, so for example if
208  * we want to change something at the endpoint level for example random_store
209  * or cookie secrets we lock the INP level.
210  */
211 
212 #define SCTP_INP_READ_INIT(_inp) \
213 	mtx_init(&(_inp)->inp_rdata_mtx, "sctp-read", "inpr", MTX_DEF | MTX_DUPOK)
214 
215 #define SCTP_INP_READ_DESTROY(_inp) \
216 	mtx_destroy(&(_inp)->inp_rdata_mtx)
217 
218 #define SCTP_INP_READ_LOCK(_inp)	do { \
219         mtx_lock(&(_inp)->inp_rdata_mtx);    \
220 } while (0)
221 
222 
223 #define SCTP_INP_READ_UNLOCK(_inp) mtx_unlock(&(_inp)->inp_rdata_mtx)
224 
225 
226 #define SCTP_INP_LOCK_INIT(_inp) \
227 	mtx_init(&(_inp)->inp_mtx, "sctp-inp", "inp", MTX_DEF | MTX_DUPOK)
228 #define SCTP_ASOC_CREATE_LOCK_INIT(_inp) \
229 	mtx_init(&(_inp)->inp_create_mtx, "sctp-create", "inp_create", \
230 		 MTX_DEF | MTX_DUPOK)
231 
232 #define SCTP_INP_LOCK_DESTROY(_inp) \
233 	mtx_destroy(&(_inp)->inp_mtx)
234 
235 #define SCTP_INP_LOCK_CONTENDED(_inp) ((_inp)->inp_mtx.mtx_lock & MTX_CONTESTED)
236 
237 #define SCTP_INP_READ_CONTENDED(_inp) ((_inp)->inp_rdata_mtx.mtx_lock & MTX_CONTESTED)
238 
239 #define SCTP_ASOC_CREATE_LOCK_CONTENDED(_inp) ((_inp)->inp_create_mtx.mtx_lock & MTX_CONTESTED)
240 
241 
242 #define SCTP_ASOC_CREATE_LOCK_DESTROY(_inp) \
243 	mtx_destroy(&(_inp)->inp_create_mtx)
244 
245 
246 #ifdef SCTP_LOCK_LOGGING
247 #define SCTP_INP_RLOCK(_inp)	do { 					\
248 	if(SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) sctp_log_lock(_inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_INP);\
249         mtx_lock(&(_inp)->inp_mtx);                                     \
250 } while (0)
251 
252 #define SCTP_INP_WLOCK(_inp)	do { 					\
253 	if(SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) sctp_log_lock(_inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_INP);\
254         mtx_lock(&(_inp)->inp_mtx);                                     \
255 } while (0)
256 
257 #else
258 
259 #define SCTP_INP_RLOCK(_inp)	do { 					\
260         mtx_lock(&(_inp)->inp_mtx);                                     \
261 } while (0)
262 
263 #define SCTP_INP_WLOCK(_inp)	do { 					\
264         mtx_lock(&(_inp)->inp_mtx);                                     \
265 } while (0)
266 
267 #endif
268 
269 
270 #define SCTP_TCB_SEND_LOCK_INIT(_tcb) \
271 	mtx_init(&(_tcb)->tcb_send_mtx, "sctp-send-tcb", "tcbs", MTX_DEF | MTX_DUPOK)
272 
273 #define SCTP_TCB_SEND_LOCK_DESTROY(_tcb) mtx_destroy(&(_tcb)->tcb_send_mtx)
274 
275 #define SCTP_TCB_SEND_LOCK(_tcb)  do { \
276 	mtx_lock(&(_tcb)->tcb_send_mtx); \
277 } while (0)
278 
279 #define SCTP_TCB_SEND_UNLOCK(_tcb) mtx_unlock(&(_tcb)->tcb_send_mtx)
280 
281 #define SCTP_INP_INCR_REF(_inp) atomic_add_int(&((_inp)->refcount), 1)
282 #define SCTP_INP_DECR_REF(_inp) atomic_add_int(&((_inp)->refcount), -1)
283 
284 
285 #ifdef SCTP_LOCK_LOGGING
286 #define SCTP_ASOC_CREATE_LOCK(_inp) \
287 	do {								\
288 	if(SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) sctp_log_lock(_inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_CREATE); \
289 		mtx_lock(&(_inp)->inp_create_mtx);			\
290 	} while (0)
291 #else
292 
293 #define SCTP_ASOC_CREATE_LOCK(_inp) \
294 	do {								\
295 		mtx_lock(&(_inp)->inp_create_mtx);			\
296 	} while (0)
297 #endif
298 
299 #define SCTP_INP_RUNLOCK(_inp)		mtx_unlock(&(_inp)->inp_mtx)
300 #define SCTP_INP_WUNLOCK(_inp)		mtx_unlock(&(_inp)->inp_mtx)
301 #define SCTP_ASOC_CREATE_UNLOCK(_inp)	mtx_unlock(&(_inp)->inp_create_mtx)
302 
303 /*
304  * For the majority of things (once we have found the association) we will
305  * lock the actual association mutex. This will protect all the assoiciation
306  * level queues and streams and such. We will need to lock the socket layer
307  * when we stuff data up into the receiving sb_mb. I.e. we will need to do an
308  * extra SOCKBUF_LOCK(&so->so_rcv) even though the association is locked.
309  */
310 
311 #define SCTP_TCB_LOCK_INIT(_tcb) \
312 	mtx_init(&(_tcb)->tcb_mtx, "sctp-tcb", "tcb", MTX_DEF | MTX_DUPOK)
313 
314 #define SCTP_TCB_LOCK_DESTROY(_tcb)	mtx_destroy(&(_tcb)->tcb_mtx)
315 
316 #ifdef SCTP_LOCK_LOGGING
317 #define SCTP_TCB_LOCK(_tcb)  do {					\
318 	if(SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE)  sctp_log_lock(_tcb->sctp_ep, _tcb, SCTP_LOG_LOCK_TCB);          \
319 	mtx_lock(&(_tcb)->tcb_mtx);                                     \
320 } while (0)
321 
322 #else
323 #define SCTP_TCB_LOCK(_tcb)  do {					\
324 	mtx_lock(&(_tcb)->tcb_mtx);                                     \
325 } while (0)
326 
327 #endif
328 
329 
330 #define SCTP_TCB_TRYLOCK(_tcb) 	mtx_trylock(&(_tcb)->tcb_mtx)
331 
332 #define SCTP_TCB_UNLOCK(_tcb)		mtx_unlock(&(_tcb)->tcb_mtx)
333 
334 #define SCTP_TCB_UNLOCK_IFOWNED(_tcb)	      do { \
335                                                 if (mtx_owned(&(_tcb)->tcb_mtx)) \
336                                                      mtx_unlock(&(_tcb)->tcb_mtx); \
337                                               } while (0)
338 
339 
340 
341 #ifdef INVARIANTS
342 #define SCTP_TCB_LOCK_ASSERT(_tcb) do { \
343                             if (mtx_owned(&(_tcb)->tcb_mtx) == 0) \
344                                 panic("Don't own TCB lock"); \
345                             } while (0)
346 #else
347 #define SCTP_TCB_LOCK_ASSERT(_tcb)
348 #endif
349 
350 #define SCTP_ITERATOR_LOCK_INIT() \
351         mtx_init(&sctp_it_ctl.it_mtx, "sctp-it", "iterator", MTX_DEF)
352 
353 #ifdef INVARIANTS
354 #define SCTP_ITERATOR_LOCK() \
355 	do {								\
356 		if (mtx_owned(&sctp_it_ctl.it_mtx))			\
357 			panic("Iterator Lock");				\
358 		mtx_lock(&sctp_it_ctl.it_mtx);				\
359 	} while (0)
360 #else
361 #define SCTP_ITERATOR_LOCK() \
362 	do {								\
363 		mtx_lock(&sctp_it_ctl.it_mtx);				\
364 	} while (0)
365 
366 #endif
367 
368 #define SCTP_ITERATOR_UNLOCK()	        mtx_unlock(&sctp_it_ctl.it_mtx)
369 #define SCTP_ITERATOR_LOCK_DESTROY()	mtx_destroy(&sctp_it_ctl.it_mtx)
370 
371 
372 #define SCTP_WQ_ADDR_INIT() do { \
373         mtx_init(&SCTP_BASE_INFO(wq_addr_mtx), "sctp-addr-wq","sctp_addr_wq",MTX_DEF); \
374  } while (0)
375 
376 #define SCTP_WQ_ADDR_DESTROY() do  { \
377         if(mtx_owned(&SCTP_BASE_INFO(wq_addr_mtx))) { \
378              mtx_unlock(&SCTP_BASE_INFO(wq_addr_mtx)); \
379         } \
380 	    mtx_destroy(&SCTP_BASE_INFO(wq_addr_mtx)); \
381       }  while (0)
382 
383 #define SCTP_WQ_ADDR_LOCK()	do { \
384              mtx_lock(&SCTP_BASE_INFO(wq_addr_mtx));  \
385 } while (0)
386 #define SCTP_WQ_ADDR_UNLOCK() do { \
387 		mtx_unlock(&SCTP_BASE_INFO(wq_addr_mtx)); \
388 } while (0)
389 
390 
391 
392 #define SCTP_INCR_EP_COUNT() \
393                 do { \
394 		       atomic_add_int(&SCTP_BASE_INFO(ipi_count_ep), 1); \
395 	        } while (0)
396 
397 #define SCTP_DECR_EP_COUNT() \
398                 do { \
399 		       atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ep), 1); \
400 	        } while (0)
401 
402 #define SCTP_INCR_ASOC_COUNT() \
403                 do { \
404 	               atomic_add_int(&SCTP_BASE_INFO(ipi_count_asoc), 1); \
405 	        } while (0)
406 
407 #define SCTP_DECR_ASOC_COUNT() \
408                 do { \
409 	               atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_asoc), 1); \
410 	        } while (0)
411 
412 #define SCTP_INCR_LADDR_COUNT() \
413                 do { \
414 	               atomic_add_int(&SCTP_BASE_INFO(ipi_count_laddr), 1); \
415 	        } while (0)
416 
417 #define SCTP_DECR_LADDR_COUNT() \
418                 do { \
419 	               atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_laddr), 1); \
420 	        } while (0)
421 
422 #define SCTP_INCR_RADDR_COUNT() \
423                 do { \
424  	               atomic_add_int(&SCTP_BASE_INFO(ipi_count_raddr), 1); \
425 	        } while (0)
426 
427 #define SCTP_DECR_RADDR_COUNT() \
428                 do { \
429  	               atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_raddr),1); \
430 	        } while (0)
431 
432 #define SCTP_INCR_CHK_COUNT() \
433                 do { \
434   	               atomic_add_int(&SCTP_BASE_INFO(ipi_count_chunk), 1); \
435 	        } while (0)
436 #ifdef INVARIANTS
437 #define SCTP_DECR_CHK_COUNT() \
438                 do { \
439                        if(SCTP_BASE_INFO(ipi_count_chunk) == 0) \
440                              panic("chunk count to 0?");    \
441   	               atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_chunk), 1); \
442 	        } while (0)
443 #else
444 #define SCTP_DECR_CHK_COUNT() \
445                 do { \
446                        if(SCTP_BASE_INFO(ipi_count_chunk) != 0) \
447   	               atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_chunk), 1); \
448 	        } while (0)
449 #endif
450 #define SCTP_INCR_READQ_COUNT() \
451                 do { \
452 		       atomic_add_int(&SCTP_BASE_INFO(ipi_count_readq),1); \
453 	        } while (0)
454 
455 #define SCTP_DECR_READQ_COUNT() \
456                 do { \
457 		       atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_readq), 1); \
458 	        } while (0)
459 
460 #define SCTP_INCR_STRMOQ_COUNT() \
461                 do { \
462 		       atomic_add_int(&SCTP_BASE_INFO(ipi_count_strmoq), 1); \
463 	        } while (0)
464 
465 #define SCTP_DECR_STRMOQ_COUNT() \
466                 do { \
467 		       atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_strmoq), 1); \
468 	        } while (0)
469 
470 
471 #if defined(SCTP_SO_LOCK_TESTING)
472 #define SCTP_INP_SO(sctpinp)	(sctpinp)->ip_inp.inp.inp_socket
473 #define SCTP_SOCKET_LOCK(so, refcnt)
474 #define SCTP_SOCKET_UNLOCK(so, refcnt)
475 #endif
476 
477 #endif
478