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