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