Deleted Added
sdiff udiff text old ( 165220 ) new ( 167598 )
full compact
1#ifndef __sctp_lock_bsd_h__
2#define __sctp_lock_bsd_h__
3/*-
4 * Copyright (c) 2001-2006, 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: head/sys/netinet/sctp_lock_bsd.h 165220 2006-12-14 17:02:55Z rrs $");
73
74
75extern struct sctp_foo_stuff sctp_logoff[];
76extern 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#define SCTP_STATLOG_GETREF(x) { \
86 x = atomic_fetchadd_int(&global_sctp_cwnd_log_at, 1); \
87 if(x == SCTP_STAT_LOG_SIZE) { \
88 global_sctp_cwnd_log_at = 1; \
89 x = 0; \
90 global_sctp_cwnd_log_rolled = 1; \
91 } \
92}
93
94
95#define SCTP_INP_INFO_LOCK_INIT() \
96 mtx_init(&sctppcbinfo.ipi_ep_mtx, "sctp-info", "inp_info", MTX_DEF)
97
98
99#define SCTP_INP_INFO_RLOCK() do { \
100 mtx_lock(&sctppcbinfo.ipi_ep_mtx); \
101} while (0)
102
103
104#define SCTP_INP_INFO_WLOCK() do { \
105 mtx_lock(&sctppcbinfo.ipi_ep_mtx); \
106} while (0)
107
108
109
110#define SCTP_IPI_ADDR_INIT() \
111 mtx_init(&sctppcbinfo.ipi_addr_mtx, "sctp-addr-wq", "sctp_addr_wq", MTX_DEF)
112
113#define SCTP_IPI_ADDR_DESTROY() \
114 mtx_destroy(&sctppcbinfo.ipi_addr_mtx)
115
116#define SCTP_IPI_ADDR_LOCK() do { \
117 mtx_lock(&sctppcbinfo.ipi_addr_mtx); \
118} while (0)
119
120#define SCTP_IPI_ADDR_UNLOCK() mtx_unlock(&sctppcbinfo.ipi_addr_mtx)
121
122#define SCTP_INP_INFO_RUNLOCK() mtx_unlock(&sctppcbinfo.ipi_ep_mtx)
123#define SCTP_INP_INFO_WUNLOCK() mtx_unlock(&sctppcbinfo.ipi_ep_mtx)
124
125/*
126 * The INP locks we will use for locking an SCTP endpoint, so for example if
127 * we want to change something at the endpoint level for example random_store
128 * or cookie secrets we lock the INP level.
129 */
130
131#define SCTP_INP_READ_INIT(_inp) \
132 mtx_init(&(_inp)->inp_rdata_mtx, "sctp-read", "inpr", MTX_DEF | MTX_DUPOK)
133
134#define SCTP_INP_READ_DESTROY(_inp) \
135 mtx_destroy(&(_inp)->inp_rdata_mtx)
136
137#define SCTP_INP_READ_LOCK(_inp) do { \
138 mtx_lock(&(_inp)->inp_rdata_mtx); \
139} while (0)
140
141
142#define SCTP_INP_READ_UNLOCK(_inp) mtx_unlock(&(_inp)->inp_rdata_mtx)
143
144
145#define SCTP_INP_LOCK_INIT(_inp) \
146 mtx_init(&(_inp)->inp_mtx, "sctp-inp", "inp", MTX_DEF | MTX_DUPOK)
147#define SCTP_ASOC_CREATE_LOCK_INIT(_inp) \
148 mtx_init(&(_inp)->inp_create_mtx, "sctp-create", "inp_create", \
149 MTX_DEF | MTX_DUPOK)
150
151#define SCTP_INP_LOCK_DESTROY(_inp) \
152 mtx_destroy(&(_inp)->inp_mtx)
153
154#define SCTP_ASOC_CREATE_LOCK_DESTROY(_inp) \
155 mtx_destroy(&(_inp)->inp_create_mtx)
156
157
158#ifdef SCTP_LOCK_LOGGING
159#define SCTP_INP_RLOCK(_inp) do { \
160 sctp_log_lock(_inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_INP);\
161 mtx_lock(&(_inp)->inp_mtx); \
162} while (0)
163
164#define SCTP_INP_WLOCK(_inp) do { \
165 sctp_log_lock(_inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_INP);\
166 mtx_lock(&(_inp)->inp_mtx); \
167} while (0)
168
169#else
170
171#define SCTP_INP_RLOCK(_inp) do { \
172 mtx_lock(&(_inp)->inp_mtx); \
173} while (0)
174
175#define SCTP_INP_WLOCK(_inp) do { \
176 mtx_lock(&(_inp)->inp_mtx); \
177} while (0)
178
179#endif
180
181
182#define SCTP_TCB_SEND_LOCK_INIT(_tcb) \
183 mtx_init(&(_tcb)->tcb_send_mtx, "sctp-send-tcb", "tcbs", MTX_DEF | MTX_DUPOK)
184
185#define SCTP_TCB_SEND_LOCK_DESTROY(_tcb) mtx_destroy(&(_tcb)->tcb_send_mtx)
186
187#define SCTP_TCB_SEND_LOCK(_tcb) do { \
188 mtx_lock(&(_tcb)->tcb_send_mtx); \
189} while (0)
190
191#define SCTP_TCB_SEND_UNLOCK(_tcb) mtx_unlock(&(_tcb)->tcb_send_mtx)
192
193#ifdef INVARIANTS
194
195#define SCTP_INP_INCR_REF(_inp) { int x; \
196 atomic_add_int(&((_inp)->refcount), 1); \
197 x = atomic_fetchadd_int(&sctp_logoff_stuff, 1); \
198 if(x == 30000) \
199 sctp_logoff_stuff = x = 0; \
200 sctp_logoff[x].inp = _inp; \
201 sctp_logoff[x].ticks = ticks; \
202 sctp_logoff[x].lineno = __LINE__; \
203 sctp_logoff[x].updown = 1; \
204}
205
206#define SCTP_INP_DECR_REF(_inp) { int x; \
207 if (atomic_fetchadd_int(&((_inp)->refcount), -1) == 0 ) panic("refcount goes negative"); \
208 x = atomic_fetchadd_int(&sctp_logoff_stuff, 1); \
209 if(x == 30000) \
210 sctp_logoff_stuff = x = 0; \
211 sctp_logoff[x].inp = _inp; \
212 sctp_logoff[x].ticks = ticks; \
213 sctp_logoff[x].lineno = __LINE__; \
214 sctp_logoff[x].updown = 0; \
215}
216
217#else
218
219#define SCTP_INP_INCR_REF(_inp) atomic_add_int(&((_inp)->refcount), 1)
220#define SCTP_INP_DECR_REF(_inp) atomic_add_int(&((_inp)->refcount), -1)
221
222#endif
223
224#ifdef SCTP_LOCK_LOGGING
225#define SCTP_ASOC_CREATE_LOCK(_inp) \
226 do { \
227 sctp_log_lock(_inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_CREATE); \
228 mtx_lock(&(_inp)->inp_create_mtx); \
229 } while (0)
230#else
231
232#define SCTP_ASOC_CREATE_LOCK(_inp) \
233 do { \
234 mtx_lock(&(_inp)->inp_create_mtx); \
235 } while (0)
236#endif
237
238#define SCTP_INP_RUNLOCK(_inp) mtx_unlock(&(_inp)->inp_mtx)
239#define SCTP_INP_WUNLOCK(_inp) mtx_unlock(&(_inp)->inp_mtx)
240#define SCTP_ASOC_CREATE_UNLOCK(_inp) mtx_unlock(&(_inp)->inp_create_mtx)
241
242/*
243 * For the majority of things (once we have found the association) we will
244 * lock the actual association mutex. This will protect all the assoiciation
245 * level queues and streams and such. We will need to lock the socket layer
246 * when we stuff data up into the receiving sb_mb. I.e. we will need to do an
247 * extra SOCKBUF_LOCK(&so->so_rcv) even though the association is locked.
248 */
249
250#define SCTP_TCB_LOCK_INIT(_tcb) \
251 mtx_init(&(_tcb)->tcb_mtx, "sctp-tcb", "tcb", MTX_DEF | MTX_DUPOK)
252
253#define SCTP_TCB_LOCK_DESTROY(_tcb) mtx_destroy(&(_tcb)->tcb_mtx)
254
255#ifdef SCTP_LOCK_LOGGING
256#define SCTP_TCB_LOCK(_tcb) do { \
257 sctp_log_lock(_tcb->sctp_ep, _tcb, SCTP_LOG_LOCK_TCB); \
258 mtx_lock(&(_tcb)->tcb_mtx); \
259} while (0)
260
261#else
262#define SCTP_TCB_LOCK(_tcb) do { \
263 mtx_lock(&(_tcb)->tcb_mtx); \
264} while (0)
265
266#endif
267
268
269#define SCTP_TCB_TRYLOCK(_tcb) mtx_trylock(&(_tcb)->tcb_mtx)
270
271#define SCTP_TCB_UNLOCK(_tcb) mtx_unlock(&(_tcb)->tcb_mtx)
272
273#define SCTP_TCB_UNLOCK_IFOWNED(_tcb) do { \
274 if (mtx_owned(&(_tcb)->tcb_mtx)) \
275 mtx_unlock(&(_tcb)->tcb_mtx); \
276 } while (0)
277
278
279
280#ifdef INVARIANTS
281#define SCTP_TCB_LOCK_ASSERT(_tcb) do { \
282 if (mtx_owned(&(_tcb)->tcb_mtx) == 0) \
283 panic("Don't own TCB lock"); \
284 } while (0)
285#else
286#define SCTP_TCB_LOCK_ASSERT(_tcb)
287#endif
288
289#define SCTP_ITERATOR_LOCK_INIT() \
290 mtx_init(&sctppcbinfo.it_mtx, "sctp-it", "iterator", MTX_DEF)
291
292#ifdef INVARIANTS
293#define SCTP_ITERATOR_LOCK() \
294 do { \
295 if (mtx_owned(&sctppcbinfo.it_mtx)) \
296 panic("Iterator Lock"); \
297 mtx_lock(&sctppcbinfo.it_mtx); \
298 } while (0)
299#else
300#define SCTP_ITERATOR_LOCK() \
301 do { \
302 mtx_lock(&sctppcbinfo.it_mtx); \
303 } while (0)
304
305#endif
306
307#define SCTP_ITERATOR_UNLOCK() mtx_unlock(&sctppcbinfo.it_mtx)
308#define SCTP_ITERATOR_LOCK_DESTROY() mtx_destroy(&sctppcbinfo.it_mtx)
309
310
311#define SCTP_INCR_EP_COUNT() \
312 do { \
313 atomic_add_int(&sctppcbinfo.ipi_count_ep, 1); \
314 } while (0)
315
316#define SCTP_DECR_EP_COUNT() \
317 do { \
318 atomic_add_int(&sctppcbinfo.ipi_count_ep,-1); \
319 } while (0)
320
321#define SCTP_INCR_ASOC_COUNT() \
322 do { \
323 atomic_add_int(&sctppcbinfo.ipi_count_asoc, 1); \
324 } while (0)
325
326#define SCTP_DECR_ASOC_COUNT() \
327 do { \
328 atomic_add_int(&sctppcbinfo.ipi_count_asoc, -1); \
329 } while (0)
330
331#define SCTP_INCR_LADDR_COUNT() \
332 do { \
333 atomic_add_int(&sctppcbinfo.ipi_count_laddr, 1); \
334 } while (0)
335
336#define SCTP_DECR_LADDR_COUNT() \
337 do { \
338 atomic_add_int(&sctppcbinfo.ipi_count_laddr, -1); \
339 } while (0)
340
341#define SCTP_INCR_RADDR_COUNT() \
342 do { \
343 atomic_add_int(&sctppcbinfo.ipi_count_raddr,1); \
344 } while (0)
345
346#define SCTP_DECR_RADDR_COUNT() \
347 do { \
348 atomic_add_int(&sctppcbinfo.ipi_count_raddr,-1); \
349 } while (0)
350
351#define SCTP_INCR_CHK_COUNT() \
352 do { \
353 atomic_add_int(&sctppcbinfo.ipi_count_chunk, 1); \
354 } while (0)
355
356#define SCTP_DECR_CHK_COUNT() \
357 do { \
358 if(sctppcbinfo.ipi_count_chunk == 0) \
359 panic("chunk count to 0?"); \
360 atomic_add_int(&sctppcbinfo.ipi_count_chunk,-1); \
361 } while (0)
362
363#define SCTP_INCR_READQ_COUNT() \
364 do { \
365 atomic_add_int(&sctppcbinfo.ipi_count_readq,1); \
366 } while (0)
367
368#define SCTP_DECR_READQ_COUNT() \
369 do { \
370 atomic_add_int(&sctppcbinfo.ipi_count_readq, -1); \
371 } while (0)
372
373#define SCTP_INCR_STRMOQ_COUNT() \
374 do { \
375 atomic_add_int(&sctppcbinfo.ipi_count_strmoq, 1); \
376 } while (0)
377
378#define SCTP_DECR_STRMOQ_COUNT() \
379 do { \
380 atomic_add_int(&sctppcbinfo.ipi_count_strmoq,-1); \
381 } while (0)
382
383
384
385
386
387#endif