xref: /freebsd-src/sys/netinet/sctp_lock_bsd.h (revision 3abc9103eb34adbb48853f2361206eba207d6c4f)
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  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * a) Redistributions of source code must retain the above copyright notice,
10  *   this list of conditions and the following disclaimer.
11  *
12  * b) Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *   the documentation and/or other materials provided with the distribution.
15  *
16  * c) Neither the name of Cisco Systems, Inc. nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * General locking concepts: The goal of our locking is to of course provide
35  * consistency and yet minimize overhead. We will attempt to use
36  * non-recursive locks which are supposed to be quite inexpensive. Now in
37  * order to do this the goal is that most functions are not aware of locking.
38  * Once we have a TCB we lock it and unlock when we are through. This means
39  * that the TCB lock is kind-of a "global" lock when working on an
40  * association. Caution must be used when asserting a TCB_LOCK since if we
41  * recurse we deadlock.
42  *
43  * Most other locks (INP and INFO) attempt to localize the locking i.e. we try
44  * to contain the lock and unlock within the function that needs to lock it.
45  * This sometimes mean we do extra locks and unlocks and lose a bit of
46  * efficency, but if the performance statements about non-recursive locks are
47  * true this should not be a problem.  One issue that arises with this only
48  * lock when needed is that if an implicit association setup is done we have
49  * a problem. If at the time I lookup an association I have NULL in the tcb
50  * return, by the time I call to create the association some other processor
51  * could have created it. This is what the CREATE lock on the endpoint.
52  * Places where we will be implicitly creating the association OR just
53  * creating an association (the connect call) will assert the CREATE_INP
54  * lock. This will assure us that during all the lookup of INP and INFO if
55  * another creator is also locking/looking up we can gate the two to
56  * synchronize. So the CREATE_INP lock is also another one we must use
57  * extreme caution in locking to make sure we don't hit a re-entrancy issue.
58  *
59  * For non FreeBSD 5.x we provide a bunch of EMPTY lock macros so we can
60  * blatantly put locks everywhere and they reduce to nothing on
61  * NetBSD/OpenBSD and FreeBSD 4.x
62  *
63  */
64 
65 /*
66  * When working with the global SCTP lists we lock and unlock the INP_INFO
67  * lock. So when we go to lookup an association we will want to do a
68  * SCTP_INP_INFO_RLOCK() and then when we want to add a new association to
69  * the sctppcbinfo list's we will do a SCTP_INP_INFO_WLOCK().
70  */
71 #include <sys/cdefs.h>
72 __FBSDID("$FreeBSD$");
73 
74 
75 extern struct sctp_foo_stuff sctp_logoff[];
76 extern int sctp_logoff_stuff;
77 
78 #define SCTP_IPI_COUNT_INIT()
79 
80 #define SCTP_STATLOG_INIT_LOCK()
81 #define SCTP_STATLOG_LOCK()
82 #define SCTP_STATLOG_UNLOCK()
83 #define SCTP_STATLOG_DESTROY()
84 
85 
86 #define SCTP_INP_INFO_LOCK_INIT() \
87         mtx_init(&sctppcbinfo.ipi_ep_mtx, "sctp-info", "inp_info", MTX_DEF)
88 
89 
90 #define SCTP_INP_INFO_RLOCK()	do { 					\
91              mtx_lock(&sctppcbinfo.ipi_ep_mtx);                         \
92 } while (0)
93 
94 
95 #define SCTP_INP_INFO_WLOCK()	do { 					\
96              mtx_lock(&sctppcbinfo.ipi_ep_mtx);                         \
97 } while (0)
98 
99 
100 #define SCTP_IPI_ADDR_INIT() \
101         mtx_init(&sctppcbinfo.ipi_addr_mtx, "sctp-addr", "sctp_addr", MTX_DEF)
102 
103 #define SCTP_IPI_ADDR_DESTROY() \
104 	mtx_destroy(&sctppcbinfo.ipi_addr_mtx)
105 
106 #define SCTP_IPI_ADDR_LOCK()	do { 					\
107              mtx_lock(&sctppcbinfo.ipi_addr_mtx);                         \
108 } while (0)
109 
110 #define SCTP_IPI_ADDR_UNLOCK()		mtx_unlock(&sctppcbinfo.ipi_addr_mtx)
111 
112 
113 
114 #define SCTP_IPI_ITERATOR_WQ_INIT() \
115         mtx_init(&sctppcbinfo.ipi_iterator_wq_mtx, "sctp-it-wq", "sctp_it_wq", MTX_DEF)
116 
117 #define SCTP_IPI_ITERATOR_WQ_DESTROY() \
118 	mtx_destroy(&sctppcbinfo.ipi_iterator_wq_mtx)
119 
120 #define SCTP_IPI_ITERATOR_WQ_LOCK()	do { 					\
121              mtx_lock(&sctppcbinfo.ipi_iterator_wq_mtx);                \
122 } while (0)
123 
124 #define SCTP_IPI_ITERATOR_WQ_UNLOCK()		mtx_unlock(&sctppcbinfo.ipi_iterator_wq_mtx)
125 
126 
127 #define SCTP_IP_PKTLOG_INIT() \
128         mtx_init(&sctppcbinfo.ipi_pktlog_mtx, "sctp-pktlog", "packetlog", MTX_DEF)
129 
130 
131 #define SCTP_IP_PKTLOG_LOCK()	do { 			\
132              mtx_lock(&sctppcbinfo.ipi_pktlog_mtx);     \
133 } while (0)
134 
135 #define SCTP_IP_PKTLOG_UNLOCK()	mtx_unlock(&sctppcbinfo.ipi_pktlog_mtx)
136 
137 #define SCTP_IP_PKTLOG_DESTROY() \
138 	mtx_destroy(&sctppcbinfo.ipi_pktlog_mtx)
139 
140 
141 
142 
143 #define SCTP_INP_INFO_RUNLOCK()		mtx_unlock(&sctppcbinfo.ipi_ep_mtx)
144 #define SCTP_INP_INFO_WUNLOCK()		mtx_unlock(&sctppcbinfo.ipi_ep_mtx)
145 
146 /*
147  * The INP locks we will use for locking an SCTP endpoint, so for example if
148  * we want to change something at the endpoint level for example random_store
149  * or cookie secrets we lock the INP level.
150  */
151 
152 #define SCTP_INP_READ_INIT(_inp) \
153 	mtx_init(&(_inp)->inp_rdata_mtx, "sctp-read", "inpr", MTX_DEF | MTX_DUPOK)
154 
155 #define SCTP_INP_READ_DESTROY(_inp) \
156 	mtx_destroy(&(_inp)->inp_rdata_mtx)
157 
158 #define SCTP_INP_READ_LOCK(_inp)	do { \
159         mtx_lock(&(_inp)->inp_rdata_mtx);    \
160 } while (0)
161 
162 
163 #define SCTP_INP_READ_UNLOCK(_inp) mtx_unlock(&(_inp)->inp_rdata_mtx)
164 
165 
166 #define SCTP_INP_LOCK_INIT(_inp) \
167 	mtx_init(&(_inp)->inp_mtx, "sctp-inp", "inp", MTX_DEF | MTX_DUPOK)
168 #define SCTP_ASOC_CREATE_LOCK_INIT(_inp) \
169 	mtx_init(&(_inp)->inp_create_mtx, "sctp-create", "inp_create", \
170 		 MTX_DEF | MTX_DUPOK)
171 
172 #define SCTP_INP_LOCK_DESTROY(_inp) \
173 	mtx_destroy(&(_inp)->inp_mtx)
174 
175 #define SCTP_ASOC_CREATE_LOCK_DESTROY(_inp) \
176 	mtx_destroy(&(_inp)->inp_create_mtx)
177 
178 
179 #ifdef SCTP_LOCK_LOGGING
180 #define SCTP_INP_RLOCK(_inp)	do { 					\
181 	if(sctp_logging_level & SCTP_LOCK_LOGGING_ENABLE) sctp_log_lock(_inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_INP);\
182         mtx_lock(&(_inp)->inp_mtx);                                     \
183 } while (0)
184 
185 #define SCTP_INP_WLOCK(_inp)	do { 					\
186 	if(sctp_logging_level & SCTP_LOCK_LOGGING_ENABLE) sctp_log_lock(_inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_INP);\
187         mtx_lock(&(_inp)->inp_mtx);                                     \
188 } while (0)
189 
190 #else
191 
192 #define SCTP_INP_RLOCK(_inp)	do { 					\
193         mtx_lock(&(_inp)->inp_mtx);                                     \
194 } while (0)
195 
196 #define SCTP_INP_WLOCK(_inp)	do { 					\
197         mtx_lock(&(_inp)->inp_mtx);                                     \
198 } while (0)
199 
200 #endif
201 
202 
203 #define SCTP_TCB_SEND_LOCK_INIT(_tcb) \
204 	mtx_init(&(_tcb)->tcb_send_mtx, "sctp-send-tcb", "tcbs", MTX_DEF | MTX_DUPOK)
205 
206 #define SCTP_TCB_SEND_LOCK_DESTROY(_tcb) mtx_destroy(&(_tcb)->tcb_send_mtx)
207 
208 #define SCTP_TCB_SEND_LOCK(_tcb)  do { \
209 	mtx_lock(&(_tcb)->tcb_send_mtx); \
210 } while (0)
211 
212 #define SCTP_TCB_SEND_UNLOCK(_tcb) mtx_unlock(&(_tcb)->tcb_send_mtx)
213 
214 #define SCTP_INP_INCR_REF(_inp) atomic_add_int(&((_inp)->refcount), 1)
215 #define SCTP_INP_DECR_REF(_inp) atomic_add_int(&((_inp)->refcount), -1)
216 
217 
218 #ifdef SCTP_LOCK_LOGGING
219 #define SCTP_ASOC_CREATE_LOCK(_inp) \
220 	do {								\
221 	if(sctp_logging_level & SCTP_LOCK_LOGGING_ENABLE) sctp_log_lock(_inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_CREATE); \
222 		mtx_lock(&(_inp)->inp_create_mtx);			\
223 	} while (0)
224 #else
225 
226 #define SCTP_ASOC_CREATE_LOCK(_inp) \
227 	do {								\
228 		mtx_lock(&(_inp)->inp_create_mtx);			\
229 	} while (0)
230 #endif
231 
232 #define SCTP_INP_RUNLOCK(_inp)		mtx_unlock(&(_inp)->inp_mtx)
233 #define SCTP_INP_WUNLOCK(_inp)		mtx_unlock(&(_inp)->inp_mtx)
234 #define SCTP_ASOC_CREATE_UNLOCK(_inp)	mtx_unlock(&(_inp)->inp_create_mtx)
235 
236 /*
237  * For the majority of things (once we have found the association) we will
238  * lock the actual association mutex. This will protect all the assoiciation
239  * level queues and streams and such. We will need to lock the socket layer
240  * when we stuff data up into the receiving sb_mb. I.e. we will need to do an
241  * extra SOCKBUF_LOCK(&so->so_rcv) even though the association is locked.
242  */
243 
244 #define SCTP_TCB_LOCK_INIT(_tcb) \
245 	mtx_init(&(_tcb)->tcb_mtx, "sctp-tcb", "tcb", MTX_DEF | MTX_DUPOK)
246 
247 #define SCTP_TCB_LOCK_DESTROY(_tcb)	mtx_destroy(&(_tcb)->tcb_mtx)
248 
249 #ifdef SCTP_LOCK_LOGGING
250 #define SCTP_TCB_LOCK(_tcb)  do {					\
251 	if(sctp_logging_level & SCTP_LOCK_LOGGING_ENABLE)  sctp_log_lock(_tcb->sctp_ep, _tcb, SCTP_LOG_LOCK_TCB);          \
252 	mtx_lock(&(_tcb)->tcb_mtx);                                     \
253 } while (0)
254 
255 #else
256 #define SCTP_TCB_LOCK(_tcb)  do {					\
257 	mtx_lock(&(_tcb)->tcb_mtx);                                     \
258 } while (0)
259 
260 #endif
261 
262 
263 #define SCTP_TCB_TRYLOCK(_tcb) 	mtx_trylock(&(_tcb)->tcb_mtx)
264 
265 #define SCTP_TCB_UNLOCK(_tcb)		mtx_unlock(&(_tcb)->tcb_mtx)
266 
267 #define SCTP_TCB_UNLOCK_IFOWNED(_tcb)	      do { \
268                                                 if (mtx_owned(&(_tcb)->tcb_mtx)) \
269                                                      mtx_unlock(&(_tcb)->tcb_mtx); \
270                                               } while (0)
271 
272 
273 
274 #ifdef INVARIANTS
275 #define SCTP_TCB_LOCK_ASSERT(_tcb) do { \
276                             if (mtx_owned(&(_tcb)->tcb_mtx) == 0) \
277                                 panic("Don't own TCB lock"); \
278                             } while (0)
279 #else
280 #define SCTP_TCB_LOCK_ASSERT(_tcb)
281 #endif
282 
283 #define SCTP_ITERATOR_LOCK_INIT() \
284         mtx_init(&sctppcbinfo.it_mtx, "sctp-it", "iterator", MTX_DEF)
285 
286 #ifdef INVARIANTS
287 #define SCTP_ITERATOR_LOCK() \
288 	do {								\
289 		if (mtx_owned(&sctppcbinfo.it_mtx))			\
290 			panic("Iterator Lock");				\
291 		mtx_lock(&sctppcbinfo.it_mtx);				\
292 	} while (0)
293 #else
294 #define SCTP_ITERATOR_LOCK() \
295 	do {								\
296 		mtx_lock(&sctppcbinfo.it_mtx);				\
297 	} while (0)
298 
299 #endif
300 
301 #define SCTP_ITERATOR_UNLOCK()	        mtx_unlock(&sctppcbinfo.it_mtx)
302 #define SCTP_ITERATOR_LOCK_DESTROY()	mtx_destroy(&sctppcbinfo.it_mtx)
303 
304 
305 #define SCTP_INCR_EP_COUNT() \
306                 do { \
307 		       atomic_add_int(&sctppcbinfo.ipi_count_ep, 1); \
308 	        } while (0)
309 
310 #define SCTP_DECR_EP_COUNT() \
311                 do { \
312 		       atomic_subtract_int(&sctppcbinfo.ipi_count_ep, 1); \
313 	        } while (0)
314 
315 #define SCTP_INCR_ASOC_COUNT() \
316                 do { \
317 	               atomic_add_int(&sctppcbinfo.ipi_count_asoc, 1); \
318 	        } while (0)
319 
320 #define SCTP_DECR_ASOC_COUNT() \
321                 do { \
322 	               atomic_subtract_int(&sctppcbinfo.ipi_count_asoc, 1); \
323 	        } while (0)
324 
325 #define SCTP_INCR_LADDR_COUNT() \
326                 do { \
327 	               atomic_add_int(&sctppcbinfo.ipi_count_laddr, 1); \
328 	        } while (0)
329 
330 #define SCTP_DECR_LADDR_COUNT() \
331                 do { \
332 	               atomic_subtract_int(&sctppcbinfo.ipi_count_laddr, 1); \
333 	        } while (0)
334 
335 #define SCTP_INCR_RADDR_COUNT() \
336                 do { \
337  	               atomic_add_int(&sctppcbinfo.ipi_count_raddr, 1); \
338 	        } while (0)
339 
340 #define SCTP_DECR_RADDR_COUNT() \
341                 do { \
342  	               atomic_subtract_int(&sctppcbinfo.ipi_count_raddr,1); \
343 	        } while (0)
344 
345 #define SCTP_INCR_CHK_COUNT() \
346                 do { \
347   	               atomic_add_int(&sctppcbinfo.ipi_count_chunk, 1); \
348 	        } while (0)
349 
350 #define SCTP_DECR_CHK_COUNT() \
351                 do { \
352                        if(sctppcbinfo.ipi_count_chunk == 0) \
353                              panic("chunk count to 0?");    \
354   	               atomic_subtract_int(&sctppcbinfo.ipi_count_chunk, 1); \
355 	        } while (0)
356 
357 #define SCTP_INCR_READQ_COUNT() \
358                 do { \
359 		       atomic_add_int(&sctppcbinfo.ipi_count_readq,1); \
360 	        } while (0)
361 
362 #define SCTP_DECR_READQ_COUNT() \
363                 do { \
364 		       atomic_subtract_int(&sctppcbinfo.ipi_count_readq, 1); \
365 	        } while (0)
366 
367 #define SCTP_INCR_STRMOQ_COUNT() \
368                 do { \
369 		       atomic_add_int(&sctppcbinfo.ipi_count_strmoq, 1); \
370 	        } while (0)
371 
372 #define SCTP_DECR_STRMOQ_COUNT() \
373                 do { \
374 		       atomic_subtract_int(&sctppcbinfo.ipi_count_strmoq, 1); \
375 	        } while (0)
376 
377 
378 #endif
379