1 /*- 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_param.h" 36 37 #include <sys/param.h> 38 #include <sys/aio.h> /* for aio_swake proto */ 39 #include <sys/kernel.h> 40 #include <sys/lock.h> 41 #include <sys/mbuf.h> 42 #include <sys/mutex.h> 43 #include <sys/proc.h> 44 #include <sys/protosw.h> 45 #include <sys/resourcevar.h> 46 #include <sys/signalvar.h> 47 #include <sys/socket.h> 48 #include <sys/socketvar.h> 49 #include <sys/sx.h> 50 #include <sys/sysctl.h> 51 52 /* 53 * Function pointer set by the AIO routines so that the socket buffer code 54 * can call back into the AIO module if it is loaded. 55 */ 56 void (*aio_swake)(struct socket *, struct sockbuf *); 57 58 /* 59 * Primitive routines for operating on socket buffers 60 */ 61 62 u_long sb_max = SB_MAX; 63 u_long sb_max_adj = 64 (quad_t)SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */ 65 66 static u_long sb_efficiency = 8; /* parameter for sbreserve() */ 67 68 static struct mbuf *sbcut_internal(struct sockbuf *sb, int len); 69 static void sbflush_internal(struct sockbuf *sb); 70 71 /* 72 * Our own version of m_clrprotoflags(), that can preserve M_NOTREADY. 73 */ 74 static void 75 sbm_clrprotoflags(struct mbuf *m, int flags) 76 { 77 int mask; 78 79 mask = ~M_PROTOFLAGS; 80 if (flags & PRUS_NOTREADY) 81 mask |= M_NOTREADY; 82 while (m) { 83 m->m_flags &= mask; 84 m = m->m_next; 85 } 86 } 87 88 /* 89 * Mark ready "count" mbufs starting with "m". 90 */ 91 int 92 sbready(struct sockbuf *sb, struct mbuf *m, int count) 93 { 94 u_int blocker; 95 96 SOCKBUF_LOCK_ASSERT(sb); 97 KASSERT(sb->sb_fnrdy != NULL, ("%s: sb %p NULL fnrdy", __func__, sb)); 98 99 blocker = (sb->sb_fnrdy == m) ? M_BLOCKED : 0; 100 101 for (int i = 0; i < count; i++, m = m->m_next) { 102 KASSERT(m->m_flags & M_NOTREADY, 103 ("%s: m %p !M_NOTREADY", __func__, m)); 104 m->m_flags &= ~(M_NOTREADY | blocker); 105 if (blocker) 106 sb->sb_acc += m->m_len; 107 } 108 109 if (!blocker) 110 return (EINPROGRESS); 111 112 /* This one was blocking all the queue. */ 113 for (; m && (m->m_flags & M_NOTREADY) == 0; m = m->m_next) { 114 KASSERT(m->m_flags & M_BLOCKED, 115 ("%s: m %p !M_BLOCKED", __func__, m)); 116 m->m_flags &= ~M_BLOCKED; 117 sb->sb_acc += m->m_len; 118 } 119 120 sb->sb_fnrdy = m; 121 122 return (0); 123 } 124 125 /* 126 * Adjust sockbuf state reflecting allocation of m. 127 */ 128 void 129 sballoc(struct sockbuf *sb, struct mbuf *m) 130 { 131 132 SOCKBUF_LOCK_ASSERT(sb); 133 134 sb->sb_ccc += m->m_len; 135 136 if (sb->sb_fnrdy == NULL) { 137 if (m->m_flags & M_NOTREADY) 138 sb->sb_fnrdy = m; 139 else 140 sb->sb_acc += m->m_len; 141 } else 142 m->m_flags |= M_BLOCKED; 143 144 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA) 145 sb->sb_ctl += m->m_len; 146 147 sb->sb_mbcnt += MSIZE; 148 sb->sb_mcnt += 1; 149 150 if (m->m_flags & M_EXT) { 151 sb->sb_mbcnt += m->m_ext.ext_size; 152 sb->sb_ccnt += 1; 153 } 154 } 155 156 /* 157 * Adjust sockbuf state reflecting freeing of m. 158 */ 159 void 160 sbfree(struct sockbuf *sb, struct mbuf *m) 161 { 162 163 #if 0 /* XXX: not yet: soclose() call path comes here w/o lock. */ 164 SOCKBUF_LOCK_ASSERT(sb); 165 #endif 166 167 sb->sb_ccc -= m->m_len; 168 169 if (!(m->m_flags & M_NOTAVAIL)) 170 sb->sb_acc -= m->m_len; 171 172 if (m == sb->sb_fnrdy) { 173 struct mbuf *n; 174 175 KASSERT(m->m_flags & M_NOTREADY, 176 ("%s: m %p !M_NOTREADY", __func__, m)); 177 178 n = m->m_next; 179 while (n != NULL && !(n->m_flags & M_NOTREADY)) { 180 n->m_flags &= ~M_BLOCKED; 181 sb->sb_acc += n->m_len; 182 n = n->m_next; 183 } 184 sb->sb_fnrdy = n; 185 } 186 187 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA) 188 sb->sb_ctl -= m->m_len; 189 190 sb->sb_mbcnt -= MSIZE; 191 sb->sb_mcnt -= 1; 192 if (m->m_flags & M_EXT) { 193 sb->sb_mbcnt -= m->m_ext.ext_size; 194 sb->sb_ccnt -= 1; 195 } 196 197 if (sb->sb_sndptr == m) { 198 sb->sb_sndptr = NULL; 199 sb->sb_sndptroff = 0; 200 } 201 if (sb->sb_sndptroff != 0) 202 sb->sb_sndptroff -= m->m_len; 203 } 204 205 /* 206 * Socantsendmore indicates that no more data will be sent on the socket; it 207 * would normally be applied to a socket when the user informs the system 208 * that no more data is to be sent, by the protocol code (in case 209 * PRU_SHUTDOWN). Socantrcvmore indicates that no more data will be 210 * received, and will normally be applied to the socket by a protocol when it 211 * detects that the peer will send no more data. Data queued for reading in 212 * the socket may yet be read. 213 */ 214 void 215 socantsendmore_locked(struct socket *so) 216 { 217 218 SOCKBUF_LOCK_ASSERT(&so->so_snd); 219 220 so->so_snd.sb_state |= SBS_CANTSENDMORE; 221 sowwakeup_locked(so); 222 mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED); 223 } 224 225 void 226 socantsendmore(struct socket *so) 227 { 228 229 SOCKBUF_LOCK(&so->so_snd); 230 socantsendmore_locked(so); 231 mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED); 232 } 233 234 void 235 socantrcvmore_locked(struct socket *so) 236 { 237 238 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 239 240 so->so_rcv.sb_state |= SBS_CANTRCVMORE; 241 sorwakeup_locked(so); 242 mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED); 243 } 244 245 void 246 socantrcvmore(struct socket *so) 247 { 248 249 SOCKBUF_LOCK(&so->so_rcv); 250 socantrcvmore_locked(so); 251 mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED); 252 } 253 254 /* 255 * Wait for data to arrive at/drain from a socket buffer. 256 */ 257 int 258 sbwait(struct sockbuf *sb) 259 { 260 261 SOCKBUF_LOCK_ASSERT(sb); 262 263 sb->sb_flags |= SB_WAIT; 264 return (msleep_sbt(&sb->sb_acc, &sb->sb_mtx, 265 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait", 266 sb->sb_timeo, 0, 0)); 267 } 268 269 int 270 sblock(struct sockbuf *sb, int flags) 271 { 272 273 KASSERT((flags & SBL_VALID) == flags, 274 ("sblock: flags invalid (0x%x)", flags)); 275 276 if (flags & SBL_WAIT) { 277 if ((sb->sb_flags & SB_NOINTR) || 278 (flags & SBL_NOINTR)) { 279 sx_xlock(&sb->sb_sx); 280 return (0); 281 } 282 return (sx_xlock_sig(&sb->sb_sx)); 283 } else { 284 if (sx_try_xlock(&sb->sb_sx) == 0) 285 return (EWOULDBLOCK); 286 return (0); 287 } 288 } 289 290 void 291 sbunlock(struct sockbuf *sb) 292 { 293 294 sx_xunlock(&sb->sb_sx); 295 } 296 297 /* 298 * Wakeup processes waiting on a socket buffer. Do asynchronous notification 299 * via SIGIO if the socket has the SS_ASYNC flag set. 300 * 301 * Called with the socket buffer lock held; will release the lock by the end 302 * of the function. This allows the caller to acquire the socket buffer lock 303 * while testing for the need for various sorts of wakeup and hold it through 304 * to the point where it's no longer required. We currently hold the lock 305 * through calls out to other subsystems (with the exception of kqueue), and 306 * then release it to avoid lock order issues. It's not clear that's 307 * correct. 308 */ 309 void 310 sowakeup(struct socket *so, struct sockbuf *sb) 311 { 312 int ret; 313 314 SOCKBUF_LOCK_ASSERT(sb); 315 316 selwakeuppri(&sb->sb_sel, PSOCK); 317 if (!SEL_WAITING(&sb->sb_sel)) 318 sb->sb_flags &= ~SB_SEL; 319 if (sb->sb_flags & SB_WAIT) { 320 sb->sb_flags &= ~SB_WAIT; 321 wakeup(&sb->sb_acc); 322 } 323 KNOTE_LOCKED(&sb->sb_sel.si_note, 0); 324 if (sb->sb_upcall != NULL) { 325 ret = sb->sb_upcall(so, sb->sb_upcallarg, M_NOWAIT); 326 if (ret == SU_ISCONNECTED) { 327 KASSERT(sb == &so->so_rcv, 328 ("SO_SND upcall returned SU_ISCONNECTED")); 329 soupcall_clear(so, SO_RCV); 330 } 331 } else 332 ret = SU_OK; 333 if (sb->sb_flags & SB_AIO) 334 aio_swake(so, sb); 335 SOCKBUF_UNLOCK(sb); 336 if (ret == SU_ISCONNECTED) 337 soisconnected(so); 338 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL) 339 pgsigio(&so->so_sigio, SIGIO, 0); 340 mtx_assert(SOCKBUF_MTX(sb), MA_NOTOWNED); 341 } 342 343 /* 344 * Socket buffer (struct sockbuf) utility routines. 345 * 346 * Each socket contains two socket buffers: one for sending data and one for 347 * receiving data. Each buffer contains a queue of mbufs, information about 348 * the number of mbufs and amount of data in the queue, and other fields 349 * allowing select() statements and notification on data availability to be 350 * implemented. 351 * 352 * Data stored in a socket buffer is maintained as a list of records. Each 353 * record is a list of mbufs chained together with the m_next field. Records 354 * are chained together with the m_nextpkt field. The upper level routine 355 * soreceive() expects the following conventions to be observed when placing 356 * information in the receive buffer: 357 * 358 * 1. If the protocol requires each message be preceded by the sender's name, 359 * then a record containing that name must be present before any 360 * associated data (mbuf's must be of type MT_SONAME). 361 * 2. If the protocol supports the exchange of ``access rights'' (really just 362 * additional data associated with the message), and there are ``rights'' 363 * to be received, then a record containing this data should be present 364 * (mbuf's must be of type MT_RIGHTS). 365 * 3. If a name or rights record exists, then it must be followed by a data 366 * record, perhaps of zero length. 367 * 368 * Before using a new socket structure it is first necessary to reserve 369 * buffer space to the socket, by calling sbreserve(). This should commit 370 * some of the available buffer space in the system buffer pool for the 371 * socket (currently, it does nothing but enforce limits). The space should 372 * be released by calling sbrelease() when the socket is destroyed. 373 */ 374 int 375 soreserve(struct socket *so, u_long sndcc, u_long rcvcc) 376 { 377 struct thread *td = curthread; 378 379 SOCKBUF_LOCK(&so->so_snd); 380 SOCKBUF_LOCK(&so->so_rcv); 381 if (sbreserve_locked(&so->so_snd, sndcc, so, td) == 0) 382 goto bad; 383 if (sbreserve_locked(&so->so_rcv, rcvcc, so, td) == 0) 384 goto bad2; 385 if (so->so_rcv.sb_lowat == 0) 386 so->so_rcv.sb_lowat = 1; 387 if (so->so_snd.sb_lowat == 0) 388 so->so_snd.sb_lowat = MCLBYTES; 389 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) 390 so->so_snd.sb_lowat = so->so_snd.sb_hiwat; 391 SOCKBUF_UNLOCK(&so->so_rcv); 392 SOCKBUF_UNLOCK(&so->so_snd); 393 return (0); 394 bad2: 395 sbrelease_locked(&so->so_snd, so); 396 bad: 397 SOCKBUF_UNLOCK(&so->so_rcv); 398 SOCKBUF_UNLOCK(&so->so_snd); 399 return (ENOBUFS); 400 } 401 402 static int 403 sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS) 404 { 405 int error = 0; 406 u_long tmp_sb_max = sb_max; 407 408 error = sysctl_handle_long(oidp, &tmp_sb_max, arg2, req); 409 if (error || !req->newptr) 410 return (error); 411 if (tmp_sb_max < MSIZE + MCLBYTES) 412 return (EINVAL); 413 sb_max = tmp_sb_max; 414 sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES); 415 return (0); 416 } 417 418 /* 419 * Allot mbufs to a sockbuf. Attempt to scale mbmax so that mbcnt doesn't 420 * become limiting if buffering efficiency is near the normal case. 421 */ 422 int 423 sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so, 424 struct thread *td) 425 { 426 rlim_t sbsize_limit; 427 428 SOCKBUF_LOCK_ASSERT(sb); 429 430 /* 431 * When a thread is passed, we take into account the thread's socket 432 * buffer size limit. The caller will generally pass curthread, but 433 * in the TCP input path, NULL will be passed to indicate that no 434 * appropriate thread resource limits are available. In that case, 435 * we don't apply a process limit. 436 */ 437 if (cc > sb_max_adj) 438 return (0); 439 if (td != NULL) { 440 sbsize_limit = lim_cur(td, RLIMIT_SBSIZE); 441 } else 442 sbsize_limit = RLIM_INFINITY; 443 if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc, 444 sbsize_limit)) 445 return (0); 446 sb->sb_mbmax = min(cc * sb_efficiency, sb_max); 447 if (sb->sb_lowat > sb->sb_hiwat) 448 sb->sb_lowat = sb->sb_hiwat; 449 return (1); 450 } 451 452 int 453 sbreserve(struct sockbuf *sb, u_long cc, struct socket *so, 454 struct thread *td) 455 { 456 int error; 457 458 SOCKBUF_LOCK(sb); 459 error = sbreserve_locked(sb, cc, so, td); 460 SOCKBUF_UNLOCK(sb); 461 return (error); 462 } 463 464 /* 465 * Free mbufs held by a socket, and reserved mbuf space. 466 */ 467 void 468 sbrelease_internal(struct sockbuf *sb, struct socket *so) 469 { 470 471 sbflush_internal(sb); 472 (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0, 473 RLIM_INFINITY); 474 sb->sb_mbmax = 0; 475 } 476 477 void 478 sbrelease_locked(struct sockbuf *sb, struct socket *so) 479 { 480 481 SOCKBUF_LOCK_ASSERT(sb); 482 483 sbrelease_internal(sb, so); 484 } 485 486 void 487 sbrelease(struct sockbuf *sb, struct socket *so) 488 { 489 490 SOCKBUF_LOCK(sb); 491 sbrelease_locked(sb, so); 492 SOCKBUF_UNLOCK(sb); 493 } 494 495 void 496 sbdestroy(struct sockbuf *sb, struct socket *so) 497 { 498 499 sbrelease_internal(sb, so); 500 } 501 502 /* 503 * Routines to add and remove data from an mbuf queue. 504 * 505 * The routines sbappend() or sbappendrecord() are normally called to append 506 * new mbufs to a socket buffer, after checking that adequate space is 507 * available, comparing the function sbspace() with the amount of data to be 508 * added. sbappendrecord() differs from sbappend() in that data supplied is 509 * treated as the beginning of a new record. To place a sender's address, 510 * optional access rights, and data in a socket receive buffer, 511 * sbappendaddr() should be used. To place access rights and data in a 512 * socket receive buffer, sbappendrights() should be used. In either case, 513 * the new data begins a new record. Note that unlike sbappend() and 514 * sbappendrecord(), these routines check for the caller that there will be 515 * enough space to store the data. Each fails if there is not enough space, 516 * or if it cannot find mbufs to store additional information in. 517 * 518 * Reliable protocols may use the socket send buffer to hold data awaiting 519 * acknowledgement. Data is normally copied from a socket send buffer in a 520 * protocol with m_copy for output to a peer, and then removing the data from 521 * the socket buffer with sbdrop() or sbdroprecord() when the data is 522 * acknowledged by the peer. 523 */ 524 #ifdef SOCKBUF_DEBUG 525 void 526 sblastrecordchk(struct sockbuf *sb, const char *file, int line) 527 { 528 struct mbuf *m = sb->sb_mb; 529 530 SOCKBUF_LOCK_ASSERT(sb); 531 532 while (m && m->m_nextpkt) 533 m = m->m_nextpkt; 534 535 if (m != sb->sb_lastrecord) { 536 printf("%s: sb_mb %p sb_lastrecord %p last %p\n", 537 __func__, sb->sb_mb, sb->sb_lastrecord, m); 538 printf("packet chain:\n"); 539 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) 540 printf("\t%p\n", m); 541 panic("%s from %s:%u", __func__, file, line); 542 } 543 } 544 545 void 546 sblastmbufchk(struct sockbuf *sb, const char *file, int line) 547 { 548 struct mbuf *m = sb->sb_mb; 549 struct mbuf *n; 550 551 SOCKBUF_LOCK_ASSERT(sb); 552 553 while (m && m->m_nextpkt) 554 m = m->m_nextpkt; 555 556 while (m && m->m_next) 557 m = m->m_next; 558 559 if (m != sb->sb_mbtail) { 560 printf("%s: sb_mb %p sb_mbtail %p last %p\n", 561 __func__, sb->sb_mb, sb->sb_mbtail, m); 562 printf("packet tree:\n"); 563 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) { 564 printf("\t"); 565 for (n = m; n != NULL; n = n->m_next) 566 printf("%p ", n); 567 printf("\n"); 568 } 569 panic("%s from %s:%u", __func__, file, line); 570 } 571 } 572 #endif /* SOCKBUF_DEBUG */ 573 574 #define SBLINKRECORD(sb, m0) do { \ 575 SOCKBUF_LOCK_ASSERT(sb); \ 576 if ((sb)->sb_lastrecord != NULL) \ 577 (sb)->sb_lastrecord->m_nextpkt = (m0); \ 578 else \ 579 (sb)->sb_mb = (m0); \ 580 (sb)->sb_lastrecord = (m0); \ 581 } while (/*CONSTCOND*/0) 582 583 /* 584 * Append mbuf chain m to the last record in the socket buffer sb. The 585 * additional space associated the mbuf chain is recorded in sb. Empty mbufs 586 * are discarded and mbufs are compacted where possible. 587 */ 588 void 589 sbappend_locked(struct sockbuf *sb, struct mbuf *m, int flags) 590 { 591 struct mbuf *n; 592 593 SOCKBUF_LOCK_ASSERT(sb); 594 595 if (m == 0) 596 return; 597 sbm_clrprotoflags(m, flags); 598 SBLASTRECORDCHK(sb); 599 n = sb->sb_mb; 600 if (n) { 601 while (n->m_nextpkt) 602 n = n->m_nextpkt; 603 do { 604 if (n->m_flags & M_EOR) { 605 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */ 606 return; 607 } 608 } while (n->m_next && (n = n->m_next)); 609 } else { 610 /* 611 * XXX Would like to simply use sb_mbtail here, but 612 * XXX I need to verify that I won't miss an EOR that 613 * XXX way. 614 */ 615 if ((n = sb->sb_lastrecord) != NULL) { 616 do { 617 if (n->m_flags & M_EOR) { 618 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */ 619 return; 620 } 621 } while (n->m_next && (n = n->m_next)); 622 } else { 623 /* 624 * If this is the first record in the socket buffer, 625 * it's also the last record. 626 */ 627 sb->sb_lastrecord = m; 628 } 629 } 630 sbcompress(sb, m, n); 631 SBLASTRECORDCHK(sb); 632 } 633 634 /* 635 * Append mbuf chain m to the last record in the socket buffer sb. The 636 * additional space associated the mbuf chain is recorded in sb. Empty mbufs 637 * are discarded and mbufs are compacted where possible. 638 */ 639 void 640 sbappend(struct sockbuf *sb, struct mbuf *m, int flags) 641 { 642 643 SOCKBUF_LOCK(sb); 644 sbappend_locked(sb, m, flags); 645 SOCKBUF_UNLOCK(sb); 646 } 647 648 /* 649 * This version of sbappend() should only be used when the caller absolutely 650 * knows that there will never be more than one record in the socket buffer, 651 * that is, a stream protocol (such as TCP). 652 */ 653 void 654 sbappendstream_locked(struct sockbuf *sb, struct mbuf *m, int flags) 655 { 656 SOCKBUF_LOCK_ASSERT(sb); 657 658 KASSERT(m->m_nextpkt == NULL,("sbappendstream 0")); 659 KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1")); 660 661 SBLASTMBUFCHK(sb); 662 663 /* Remove all packet headers and mbuf tags to get a pure data chain. */ 664 m_demote(m, 1, flags & PRUS_NOTREADY ? M_NOTREADY : 0); 665 666 sbcompress(sb, m, sb->sb_mbtail); 667 668 sb->sb_lastrecord = sb->sb_mb; 669 SBLASTRECORDCHK(sb); 670 } 671 672 /* 673 * This version of sbappend() should only be used when the caller absolutely 674 * knows that there will never be more than one record in the socket buffer, 675 * that is, a stream protocol (such as TCP). 676 */ 677 void 678 sbappendstream(struct sockbuf *sb, struct mbuf *m, int flags) 679 { 680 681 SOCKBUF_LOCK(sb); 682 sbappendstream_locked(sb, m, flags); 683 SOCKBUF_UNLOCK(sb); 684 } 685 686 #ifdef SOCKBUF_DEBUG 687 void 688 sbcheck(struct sockbuf *sb, const char *file, int line) 689 { 690 struct mbuf *m, *n, *fnrdy; 691 u_long acc, ccc, mbcnt; 692 693 SOCKBUF_LOCK_ASSERT(sb); 694 695 acc = ccc = mbcnt = 0; 696 fnrdy = NULL; 697 698 for (m = sb->sb_mb; m; m = n) { 699 n = m->m_nextpkt; 700 for (; m; m = m->m_next) { 701 if (m->m_len == 0) { 702 printf("sb %p empty mbuf %p\n", sb, m); 703 goto fail; 704 } 705 if ((m->m_flags & M_NOTREADY) && fnrdy == NULL) { 706 if (m != sb->sb_fnrdy) { 707 printf("sb %p: fnrdy %p != m %p\n", 708 sb, sb->sb_fnrdy, m); 709 goto fail; 710 } 711 fnrdy = m; 712 } 713 if (fnrdy) { 714 if (!(m->m_flags & M_NOTAVAIL)) { 715 printf("sb %p: fnrdy %p, m %p is avail\n", 716 sb, sb->sb_fnrdy, m); 717 goto fail; 718 } 719 } else 720 acc += m->m_len; 721 ccc += m->m_len; 722 mbcnt += MSIZE; 723 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */ 724 mbcnt += m->m_ext.ext_size; 725 } 726 } 727 if (acc != sb->sb_acc || ccc != sb->sb_ccc || mbcnt != sb->sb_mbcnt) { 728 printf("acc %ld/%u ccc %ld/%u mbcnt %ld/%u\n", 729 acc, sb->sb_acc, ccc, sb->sb_ccc, mbcnt, sb->sb_mbcnt); 730 goto fail; 731 } 732 return; 733 fail: 734 panic("%s from %s:%u", __func__, file, line); 735 } 736 #endif 737 738 /* 739 * As above, except the mbuf chain begins a new record. 740 */ 741 void 742 sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0) 743 { 744 struct mbuf *m; 745 746 SOCKBUF_LOCK_ASSERT(sb); 747 748 if (m0 == 0) 749 return; 750 m_clrprotoflags(m0); 751 /* 752 * Put the first mbuf on the queue. Note this permits zero length 753 * records. 754 */ 755 sballoc(sb, m0); 756 SBLASTRECORDCHK(sb); 757 SBLINKRECORD(sb, m0); 758 sb->sb_mbtail = m0; 759 m = m0->m_next; 760 m0->m_next = 0; 761 if (m && (m0->m_flags & M_EOR)) { 762 m0->m_flags &= ~M_EOR; 763 m->m_flags |= M_EOR; 764 } 765 /* always call sbcompress() so it can do SBLASTMBUFCHK() */ 766 sbcompress(sb, m, m0); 767 } 768 769 /* 770 * As above, except the mbuf chain begins a new record. 771 */ 772 void 773 sbappendrecord(struct sockbuf *sb, struct mbuf *m0) 774 { 775 776 SOCKBUF_LOCK(sb); 777 sbappendrecord_locked(sb, m0); 778 SOCKBUF_UNLOCK(sb); 779 } 780 781 /* Helper routine that appends data, control, and address to a sockbuf. */ 782 static int 783 sbappendaddr_locked_internal(struct sockbuf *sb, const struct sockaddr *asa, 784 struct mbuf *m0, struct mbuf *control, struct mbuf *ctrl_last) 785 { 786 struct mbuf *m, *n, *nlast; 787 #if MSIZE <= 256 788 if (asa->sa_len > MLEN) 789 return (0); 790 #endif 791 m = m_get(M_NOWAIT, MT_SONAME); 792 if (m == NULL) 793 return (0); 794 m->m_len = asa->sa_len; 795 bcopy(asa, mtod(m, caddr_t), asa->sa_len); 796 if (m0) 797 m_clrprotoflags(m0); 798 if (ctrl_last) 799 ctrl_last->m_next = m0; /* concatenate data to control */ 800 else 801 control = m0; 802 m->m_next = control; 803 for (n = m; n->m_next != NULL; n = n->m_next) 804 sballoc(sb, n); 805 sballoc(sb, n); 806 nlast = n; 807 SBLINKRECORD(sb, m); 808 809 sb->sb_mbtail = nlast; 810 SBLASTMBUFCHK(sb); 811 812 SBLASTRECORDCHK(sb); 813 return (1); 814 } 815 816 /* 817 * Append address and data, and optionally, control (ancillary) data to the 818 * receive queue of a socket. If present, m0 must include a packet header 819 * with total length. Returns 0 if no space in sockbuf or insufficient 820 * mbufs. 821 */ 822 int 823 sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa, 824 struct mbuf *m0, struct mbuf *control) 825 { 826 struct mbuf *ctrl_last; 827 int space = asa->sa_len; 828 829 SOCKBUF_LOCK_ASSERT(sb); 830 831 if (m0 && (m0->m_flags & M_PKTHDR) == 0) 832 panic("sbappendaddr_locked"); 833 if (m0) 834 space += m0->m_pkthdr.len; 835 space += m_length(control, &ctrl_last); 836 837 if (space > sbspace(sb)) 838 return (0); 839 return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last)); 840 } 841 842 /* 843 * Append address and data, and optionally, control (ancillary) data to the 844 * receive queue of a socket. If present, m0 must include a packet header 845 * with total length. Returns 0 if insufficient mbufs. Does not validate space 846 * on the receiving sockbuf. 847 */ 848 int 849 sbappendaddr_nospacecheck_locked(struct sockbuf *sb, const struct sockaddr *asa, 850 struct mbuf *m0, struct mbuf *control) 851 { 852 struct mbuf *ctrl_last; 853 854 SOCKBUF_LOCK_ASSERT(sb); 855 856 ctrl_last = (control == NULL) ? NULL : m_last(control); 857 return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last)); 858 } 859 860 /* 861 * Append address and data, and optionally, control (ancillary) data to the 862 * receive queue of a socket. If present, m0 must include a packet header 863 * with total length. Returns 0 if no space in sockbuf or insufficient 864 * mbufs. 865 */ 866 int 867 sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa, 868 struct mbuf *m0, struct mbuf *control) 869 { 870 int retval; 871 872 SOCKBUF_LOCK(sb); 873 retval = sbappendaddr_locked(sb, asa, m0, control); 874 SOCKBUF_UNLOCK(sb); 875 return (retval); 876 } 877 878 int 879 sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0, 880 struct mbuf *control) 881 { 882 struct mbuf *m, *n, *mlast; 883 int space; 884 885 SOCKBUF_LOCK_ASSERT(sb); 886 887 if (control == 0) 888 panic("sbappendcontrol_locked"); 889 space = m_length(control, &n) + m_length(m0, NULL); 890 891 if (space > sbspace(sb)) 892 return (0); 893 m_clrprotoflags(m0); 894 n->m_next = m0; /* concatenate data to control */ 895 896 SBLASTRECORDCHK(sb); 897 898 for (m = control; m->m_next; m = m->m_next) 899 sballoc(sb, m); 900 sballoc(sb, m); 901 mlast = m; 902 SBLINKRECORD(sb, control); 903 904 sb->sb_mbtail = mlast; 905 SBLASTMBUFCHK(sb); 906 907 SBLASTRECORDCHK(sb); 908 return (1); 909 } 910 911 int 912 sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control) 913 { 914 int retval; 915 916 SOCKBUF_LOCK(sb); 917 retval = sbappendcontrol_locked(sb, m0, control); 918 SOCKBUF_UNLOCK(sb); 919 return (retval); 920 } 921 922 /* 923 * Append the data in mbuf chain (m) into the socket buffer sb following mbuf 924 * (n). If (n) is NULL, the buffer is presumed empty. 925 * 926 * When the data is compressed, mbufs in the chain may be handled in one of 927 * three ways: 928 * 929 * (1) The mbuf may simply be dropped, if it contributes nothing (no data, no 930 * record boundary, and no change in data type). 931 * 932 * (2) The mbuf may be coalesced -- i.e., data in the mbuf may be copied into 933 * an mbuf already in the socket buffer. This can occur if an 934 * appropriate mbuf exists, there is room, both mbufs are not marked as 935 * not ready, and no merging of data types will occur. 936 * 937 * (3) The mbuf may be appended to the end of the existing mbuf chain. 938 * 939 * If any of the new mbufs is marked as M_EOR, mark the last mbuf appended as 940 * end-of-record. 941 */ 942 void 943 sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n) 944 { 945 int eor = 0; 946 struct mbuf *o; 947 948 SOCKBUF_LOCK_ASSERT(sb); 949 950 while (m) { 951 eor |= m->m_flags & M_EOR; 952 if (m->m_len == 0 && 953 (eor == 0 || 954 (((o = m->m_next) || (o = n)) && 955 o->m_type == m->m_type))) { 956 if (sb->sb_lastrecord == m) 957 sb->sb_lastrecord = m->m_next; 958 m = m_free(m); 959 continue; 960 } 961 if (n && (n->m_flags & M_EOR) == 0 && 962 M_WRITABLE(n) && 963 ((sb->sb_flags & SB_NOCOALESCE) == 0) && 964 !(m->m_flags & M_NOTREADY) && 965 !(n->m_flags & M_NOTREADY) && 966 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */ 967 m->m_len <= M_TRAILINGSPACE(n) && 968 n->m_type == m->m_type) { 969 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, 970 (unsigned)m->m_len); 971 n->m_len += m->m_len; 972 sb->sb_ccc += m->m_len; 973 if (sb->sb_fnrdy == NULL) 974 sb->sb_acc += m->m_len; 975 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA) 976 /* XXX: Probably don't need.*/ 977 sb->sb_ctl += m->m_len; 978 m = m_free(m); 979 continue; 980 } 981 if (n) 982 n->m_next = m; 983 else 984 sb->sb_mb = m; 985 sb->sb_mbtail = m; 986 sballoc(sb, m); 987 n = m; 988 m->m_flags &= ~M_EOR; 989 m = m->m_next; 990 n->m_next = 0; 991 } 992 if (eor) { 993 KASSERT(n != NULL, ("sbcompress: eor && n == NULL")); 994 n->m_flags |= eor; 995 } 996 SBLASTMBUFCHK(sb); 997 } 998 999 /* 1000 * Free all mbufs in a sockbuf. Check that all resources are reclaimed. 1001 */ 1002 static void 1003 sbflush_internal(struct sockbuf *sb) 1004 { 1005 1006 while (sb->sb_mbcnt) { 1007 /* 1008 * Don't call sbcut(sb, 0) if the leading mbuf is non-empty: 1009 * we would loop forever. Panic instead. 1010 */ 1011 if (sb->sb_ccc == 0 && (sb->sb_mb == NULL || sb->sb_mb->m_len)) 1012 break; 1013 m_freem(sbcut_internal(sb, (int)sb->sb_ccc)); 1014 } 1015 KASSERT(sb->sb_ccc == 0 && sb->sb_mb == 0 && sb->sb_mbcnt == 0, 1016 ("%s: ccc %u mb %p mbcnt %u", __func__, 1017 sb->sb_ccc, (void *)sb->sb_mb, sb->sb_mbcnt)); 1018 } 1019 1020 void 1021 sbflush_locked(struct sockbuf *sb) 1022 { 1023 1024 SOCKBUF_LOCK_ASSERT(sb); 1025 sbflush_internal(sb); 1026 } 1027 1028 void 1029 sbflush(struct sockbuf *sb) 1030 { 1031 1032 SOCKBUF_LOCK(sb); 1033 sbflush_locked(sb); 1034 SOCKBUF_UNLOCK(sb); 1035 } 1036 1037 /* 1038 * Cut data from (the front of) a sockbuf. 1039 */ 1040 static struct mbuf * 1041 sbcut_internal(struct sockbuf *sb, int len) 1042 { 1043 struct mbuf *m, *next, *mfree; 1044 1045 next = (m = sb->sb_mb) ? m->m_nextpkt : 0; 1046 mfree = NULL; 1047 1048 while (len > 0) { 1049 if (m == NULL) { 1050 KASSERT(next, ("%s: no next, len %d", __func__, len)); 1051 m = next; 1052 next = m->m_nextpkt; 1053 } 1054 if (m->m_len > len) { 1055 KASSERT(!(m->m_flags & M_NOTAVAIL), 1056 ("%s: m %p M_NOTAVAIL", __func__, m)); 1057 m->m_len -= len; 1058 m->m_data += len; 1059 sb->sb_ccc -= len; 1060 sb->sb_acc -= len; 1061 if (sb->sb_sndptroff != 0) 1062 sb->sb_sndptroff -= len; 1063 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA) 1064 sb->sb_ctl -= len; 1065 break; 1066 } 1067 len -= m->m_len; 1068 sbfree(sb, m); 1069 /* 1070 * Do not put M_NOTREADY buffers to the free list, they 1071 * are referenced from outside. 1072 */ 1073 if (m->m_flags & M_NOTREADY) 1074 m = m->m_next; 1075 else { 1076 struct mbuf *n; 1077 1078 n = m->m_next; 1079 m->m_next = mfree; 1080 mfree = m; 1081 m = n; 1082 } 1083 } 1084 /* 1085 * Free any zero-length mbufs from the buffer. 1086 * For SOCK_DGRAM sockets such mbufs represent empty records. 1087 * XXX: For SOCK_STREAM sockets such mbufs can appear in the buffer, 1088 * when sosend_generic() needs to send only control data. 1089 */ 1090 while (m && m->m_len == 0) { 1091 struct mbuf *n; 1092 1093 sbfree(sb, m); 1094 n = m->m_next; 1095 m->m_next = mfree; 1096 mfree = m; 1097 m = n; 1098 } 1099 if (m) { 1100 sb->sb_mb = m; 1101 m->m_nextpkt = next; 1102 } else 1103 sb->sb_mb = next; 1104 /* 1105 * First part is an inline SB_EMPTY_FIXUP(). Second part makes sure 1106 * sb_lastrecord is up-to-date if we dropped part of the last record. 1107 */ 1108 m = sb->sb_mb; 1109 if (m == NULL) { 1110 sb->sb_mbtail = NULL; 1111 sb->sb_lastrecord = NULL; 1112 } else if (m->m_nextpkt == NULL) { 1113 sb->sb_lastrecord = m; 1114 } 1115 1116 return (mfree); 1117 } 1118 1119 /* 1120 * Drop data from (the front of) a sockbuf. 1121 */ 1122 void 1123 sbdrop_locked(struct sockbuf *sb, int len) 1124 { 1125 1126 SOCKBUF_LOCK_ASSERT(sb); 1127 m_freem(sbcut_internal(sb, len)); 1128 } 1129 1130 /* 1131 * Drop data from (the front of) a sockbuf, 1132 * and return it to caller. 1133 */ 1134 struct mbuf * 1135 sbcut_locked(struct sockbuf *sb, int len) 1136 { 1137 1138 SOCKBUF_LOCK_ASSERT(sb); 1139 return (sbcut_internal(sb, len)); 1140 } 1141 1142 void 1143 sbdrop(struct sockbuf *sb, int len) 1144 { 1145 struct mbuf *mfree; 1146 1147 SOCKBUF_LOCK(sb); 1148 mfree = sbcut_internal(sb, len); 1149 SOCKBUF_UNLOCK(sb); 1150 1151 m_freem(mfree); 1152 } 1153 1154 /* 1155 * Maintain a pointer and offset pair into the socket buffer mbuf chain to 1156 * avoid traversal of the entire socket buffer for larger offsets. 1157 */ 1158 struct mbuf * 1159 sbsndptr(struct sockbuf *sb, u_int off, u_int len, u_int *moff) 1160 { 1161 struct mbuf *m, *ret; 1162 1163 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__)); 1164 KASSERT(off + len <= sb->sb_acc, ("%s: beyond sb", __func__)); 1165 KASSERT(sb->sb_sndptroff <= sb->sb_acc, ("%s: sndptroff broken", __func__)); 1166 1167 /* 1168 * Is off below stored offset? Happens on retransmits. 1169 * Just return, we can't help here. 1170 */ 1171 if (sb->sb_sndptroff > off) { 1172 *moff = off; 1173 return (sb->sb_mb); 1174 } 1175 1176 /* Return closest mbuf in chain for current offset. */ 1177 *moff = off - sb->sb_sndptroff; 1178 m = ret = sb->sb_sndptr ? sb->sb_sndptr : sb->sb_mb; 1179 if (*moff == m->m_len) { 1180 *moff = 0; 1181 sb->sb_sndptroff += m->m_len; 1182 m = ret = m->m_next; 1183 KASSERT(ret->m_len > 0, 1184 ("mbuf %p in sockbuf %p chain has no valid data", ret, sb)); 1185 } 1186 1187 /* Advance by len to be as close as possible for the next transmit. */ 1188 for (off = off - sb->sb_sndptroff + len - 1; 1189 off > 0 && m != NULL && off >= m->m_len; 1190 m = m->m_next) { 1191 sb->sb_sndptroff += m->m_len; 1192 off -= m->m_len; 1193 } 1194 if (off > 0 && m == NULL) 1195 panic("%s: sockbuf %p and mbuf %p clashing", __func__, sb, ret); 1196 sb->sb_sndptr = m; 1197 1198 return (ret); 1199 } 1200 1201 /* 1202 * Return the first mbuf and the mbuf data offset for the provided 1203 * send offset without changing the "sb_sndptroff" field. 1204 */ 1205 struct mbuf * 1206 sbsndmbuf(struct sockbuf *sb, u_int off, u_int *moff) 1207 { 1208 struct mbuf *m; 1209 1210 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__)); 1211 1212 /* 1213 * If the "off" is below the stored offset, which happens on 1214 * retransmits, just use "sb_mb": 1215 */ 1216 if (sb->sb_sndptr == NULL || sb->sb_sndptroff > off) { 1217 m = sb->sb_mb; 1218 } else { 1219 m = sb->sb_sndptr; 1220 off -= sb->sb_sndptroff; 1221 } 1222 while (off > 0 && m != NULL) { 1223 if (off < m->m_len) 1224 break; 1225 off -= m->m_len; 1226 m = m->m_next; 1227 } 1228 *moff = off; 1229 return (m); 1230 } 1231 1232 /* 1233 * Drop a record off the front of a sockbuf and move the next record to the 1234 * front. 1235 */ 1236 void 1237 sbdroprecord_locked(struct sockbuf *sb) 1238 { 1239 struct mbuf *m; 1240 1241 SOCKBUF_LOCK_ASSERT(sb); 1242 1243 m = sb->sb_mb; 1244 if (m) { 1245 sb->sb_mb = m->m_nextpkt; 1246 do { 1247 sbfree(sb, m); 1248 m = m_free(m); 1249 } while (m); 1250 } 1251 SB_EMPTY_FIXUP(sb); 1252 } 1253 1254 /* 1255 * Drop a record off the front of a sockbuf and move the next record to the 1256 * front. 1257 */ 1258 void 1259 sbdroprecord(struct sockbuf *sb) 1260 { 1261 1262 SOCKBUF_LOCK(sb); 1263 sbdroprecord_locked(sb); 1264 SOCKBUF_UNLOCK(sb); 1265 } 1266 1267 /* 1268 * Create a "control" mbuf containing the specified data with the specified 1269 * type for presentation on a socket buffer. 1270 */ 1271 struct mbuf * 1272 sbcreatecontrol(caddr_t p, int size, int type, int level) 1273 { 1274 struct cmsghdr *cp; 1275 struct mbuf *m; 1276 1277 if (CMSG_SPACE((u_int)size) > MCLBYTES) 1278 return ((struct mbuf *) NULL); 1279 if (CMSG_SPACE((u_int)size) > MLEN) 1280 m = m_getcl(M_NOWAIT, MT_CONTROL, 0); 1281 else 1282 m = m_get(M_NOWAIT, MT_CONTROL); 1283 if (m == NULL) 1284 return ((struct mbuf *) NULL); 1285 cp = mtod(m, struct cmsghdr *); 1286 m->m_len = 0; 1287 KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m), 1288 ("sbcreatecontrol: short mbuf")); 1289 /* 1290 * Don't leave the padding between the msg header and the 1291 * cmsg data and the padding after the cmsg data un-initialized. 1292 */ 1293 bzero(cp, CMSG_SPACE((u_int)size)); 1294 if (p != NULL) 1295 (void)memcpy(CMSG_DATA(cp), p, size); 1296 m->m_len = CMSG_SPACE(size); 1297 cp->cmsg_len = CMSG_LEN(size); 1298 cp->cmsg_level = level; 1299 cp->cmsg_type = type; 1300 return (m); 1301 } 1302 1303 /* 1304 * This does the same for socket buffers that sotoxsocket does for sockets: 1305 * generate an user-format data structure describing the socket buffer. Note 1306 * that the xsockbuf structure, since it is always embedded in a socket, does 1307 * not include a self pointer nor a length. We make this entry point public 1308 * in case some other mechanism needs it. 1309 */ 1310 void 1311 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb) 1312 { 1313 1314 xsb->sb_cc = sb->sb_ccc; 1315 xsb->sb_hiwat = sb->sb_hiwat; 1316 xsb->sb_mbcnt = sb->sb_mbcnt; 1317 xsb->sb_mcnt = sb->sb_mcnt; 1318 xsb->sb_ccnt = sb->sb_ccnt; 1319 xsb->sb_mbmax = sb->sb_mbmax; 1320 xsb->sb_lowat = sb->sb_lowat; 1321 xsb->sb_flags = sb->sb_flags; 1322 xsb->sb_timeo = sb->sb_timeo; 1323 } 1324 1325 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */ 1326 static int dummy; 1327 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, ""); 1328 SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_ULONG|CTLFLAG_RW, 1329 &sb_max, 0, sysctl_handle_sb_max, "LU", "Maximum socket buffer size"); 1330 SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW, 1331 &sb_efficiency, 0, "Socket buffer size waste factor"); 1332