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