1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1990, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 29df8bae1dSRodney W. Grimes * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 32677b542eSDavid E. O'Brien #include <sys/cdefs.h> 33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 34677b542eSDavid E. O'Brien 35335654d7SRobert Watson #include "opt_mac.h" 365b86eac4SJesper Skriver #include "opt_param.h" 37335654d7SRobert Watson 38df8bae1dSRodney W. Grimes #include <sys/param.h> 39960ed29cSSeigo Tanimura #include <sys/aio.h> /* for aio_swake proto */ 4098271db4SGarrett Wollman #include <sys/domain.h> 41960ed29cSSeigo Tanimura #include <sys/event.h> 42134c934cSMike Smith #include <sys/file.h> /* for maxfiles */ 43ff5c09daSGarrett Wollman #include <sys/kernel.h> 44fb919e4dSMark Murray #include <sys/lock.h> 45335654d7SRobert Watson #include <sys/mac.h> 46f9d0d524SRobert Watson #include <sys/malloc.h> 47df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 48960ed29cSSeigo Tanimura #include <sys/mutex.h> 49fb919e4dSMark Murray #include <sys/proc.h> 50df8bae1dSRodney W. Grimes #include <sys/protosw.h> 512f9a2132SBrian Feldman #include <sys/resourcevar.h> 52960ed29cSSeigo Tanimura #include <sys/signalvar.h> 53df8bae1dSRodney W. Grimes #include <sys/socket.h> 54df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 55960ed29cSSeigo Tanimura #include <sys/stat.h> 56ff5c09daSGarrett Wollman #include <sys/sysctl.h> 57960ed29cSSeigo Tanimura #include <sys/systm.h> 5826f9a767SRodney W. Grimes 5954d77689SJeff Roberson int maxsockets; 6054d77689SJeff Roberson 6121d56e9cSAlfred Perlstein void (*aio_swake)(struct socket *, struct sockbuf *); 6221d56e9cSAlfred Perlstein 63df8bae1dSRodney W. Grimes /* 64df8bae1dSRodney W. Grimes * Primitive routines for operating on sockets and socket buffers 65df8bae1dSRodney W. Grimes */ 66df8bae1dSRodney W. Grimes 6779cb7eb4SDavid Greenman u_long sb_max = SB_MAX; 6851da11a2SMark Murray static u_long sb_max_adj = 6979cb7eb4SDavid Greenman SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */ 70df8bae1dSRodney W. Grimes 714b29bc4fSGarrett Wollman static u_long sb_efficiency = 8; /* parameter for sbreserve() */ 724b29bc4fSGarrett Wollman 73df8bae1dSRodney W. Grimes /* 74df8bae1dSRodney W. Grimes * Procedures to manipulate state flags of socket 75df8bae1dSRodney W. Grimes * and do appropriate wakeups. Normal sequence from the 76df8bae1dSRodney W. Grimes * active (originating) side is that soisconnecting() is 77df8bae1dSRodney W. Grimes * called during processing of connect() call, 78df8bae1dSRodney W. Grimes * resulting in an eventual call to soisconnected() if/when the 79df8bae1dSRodney W. Grimes * connection is established. When the connection is torn down 80df8bae1dSRodney W. Grimes * soisdisconnecting() is called during processing of disconnect() call, 81df8bae1dSRodney W. Grimes * and soisdisconnected() is called when the connection to the peer 82df8bae1dSRodney W. Grimes * is totally severed. The semantics of these routines are such that 83df8bae1dSRodney W. Grimes * connectionless protocols can call soisconnected() and soisdisconnected() 84df8bae1dSRodney W. Grimes * only, bypassing the in-progress calls when setting up a ``connection'' 85df8bae1dSRodney W. Grimes * takes no time. 86df8bae1dSRodney W. Grimes * 87df8bae1dSRodney W. Grimes * From the passive side, a socket is created with 88dc97381eSPeter Wemm * two queues of sockets: so_incomp for connections in progress 89dc97381eSPeter Wemm * and so_comp for connections already made and awaiting user acceptance. 90df8bae1dSRodney W. Grimes * As a protocol is preparing incoming connections, it creates a socket 91dc97381eSPeter Wemm * structure queued on so_incomp by calling sonewconn(). When the connection 92df8bae1dSRodney W. Grimes * is established, soisconnected() is called, and transfers the 93dc97381eSPeter Wemm * socket structure to so_comp, making it available to accept(). 94df8bae1dSRodney W. Grimes * 95df8bae1dSRodney W. Grimes * If a socket is closed with sockets on either 96dc97381eSPeter Wemm * so_incomp or so_comp, these sockets are dropped. 97df8bae1dSRodney W. Grimes * 98df8bae1dSRodney W. Grimes * If higher level protocols are implemented in 99df8bae1dSRodney W. Grimes * the kernel, the wakeups done here will sometimes 100df8bae1dSRodney W. Grimes * cause software-interrupt process scheduling. 101df8bae1dSRodney W. Grimes */ 102df8bae1dSRodney W. Grimes 10326f9a767SRodney W. Grimes void 104df8bae1dSRodney W. Grimes soisconnecting(so) 105df8bae1dSRodney W. Grimes register struct socket *so; 106df8bae1dSRodney W. Grimes { 107df8bae1dSRodney W. Grimes 1089535efc0SRobert Watson SOCK_LOCK(so); 109df8bae1dSRodney W. Grimes so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); 110df8bae1dSRodney W. Grimes so->so_state |= SS_ISCONNECTING; 1119535efc0SRobert Watson SOCK_UNLOCK(so); 112df8bae1dSRodney W. Grimes } 113df8bae1dSRodney W. Grimes 11426f9a767SRodney W. Grimes void 115d48d4b25SSeigo Tanimura soisconnected(so) 116d48d4b25SSeigo Tanimura struct socket *so; 117d48d4b25SSeigo Tanimura { 1182658b3bbSRobert Watson struct socket *head; 119d48d4b25SSeigo Tanimura 1209535efc0SRobert Watson SOCK_LOCK(so); 121d48d4b25SSeigo Tanimura so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); 122d48d4b25SSeigo Tanimura so->so_state |= SS_ISCONNECTED; 1239535efc0SRobert Watson SOCK_UNLOCK(so); 1242658b3bbSRobert Watson ACCEPT_LOCK(); 1252658b3bbSRobert Watson head = so->so_head; 1262658b3bbSRobert Watson if (head != NULL && (so->so_qstate & SQ_INCOMP)) { 1272658b3bbSRobert Watson if ((so->so_options & SO_ACCEPTFILTER) == 0) { 128be24e9e8SDavid Greenman TAILQ_REMOVE(&head->so_incomp, so, so_list); 129a51764a8SPaul Traina head->so_incqlen--; 13036568179SRobert Watson so->so_qstate &= ~SQ_INCOMP; 131be24e9e8SDavid Greenman TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); 132e1f1827fSMike Silbersack head->so_qlen++; 13336568179SRobert Watson so->so_qstate |= SQ_COMP; 1342658b3bbSRobert Watson ACCEPT_UNLOCK(); 13503e49181SSeigo Tanimura sorwakeup(head); 136a91b8721SDavid Greenman wakeup_one(&head->so_timeo); 137df8bae1dSRodney W. Grimes } else { 1382658b3bbSRobert Watson ACCEPT_UNLOCK(); 1399535efc0SRobert Watson SOCK_LOCK(so); 1402658b3bbSRobert Watson so->so_upcall = 1412658b3bbSRobert Watson head->so_accf->so_accept_filter->accf_callback; 1422658b3bbSRobert Watson so->so_upcallarg = head->so_accf->so_accept_filter_arg; 1432658b3bbSRobert Watson so->so_rcv.sb_flags |= SB_UPCALL; 1442658b3bbSRobert Watson so->so_options &= ~SO_ACCEPTFILTER; 1459535efc0SRobert Watson SOCK_UNLOCK(so); 1462658b3bbSRobert Watson so->so_upcall(so, so->so_upcallarg, M_TRYWAIT); 1472658b3bbSRobert Watson } 1482658b3bbSRobert Watson return; 1492658b3bbSRobert Watson } 1502658b3bbSRobert Watson ACCEPT_UNLOCK(); 151a91b8721SDavid Greenman wakeup(&so->so_timeo); 15203e49181SSeigo Tanimura sorwakeup(so); 15303e49181SSeigo Tanimura sowwakeup(so); 154df8bae1dSRodney W. Grimes } 155df8bae1dSRodney W. Grimes 15626f9a767SRodney W. Grimes void 157df8bae1dSRodney W. Grimes soisdisconnecting(so) 158df8bae1dSRodney W. Grimes register struct socket *so; 159df8bae1dSRodney W. Grimes { 160df8bae1dSRodney W. Grimes 1619535efc0SRobert Watson /* 1629535efc0SRobert Watson * XXXRW: This code separately acquires SOCK_LOCK(so) and 1639535efc0SRobert Watson * SOCKBUF_LOCK(&so->so_rcv) even though they are the same mutex to 1649535efc0SRobert Watson * avoid introducing the assumption that they are the same. 1659535efc0SRobert Watson */ 1669535efc0SRobert Watson SOCK_LOCK(so); 167df8bae1dSRodney W. Grimes so->so_state &= ~SS_ISCONNECTING; 168c0b99ffaSRobert Watson so->so_state |= SS_ISDISCONNECTING; 1699535efc0SRobert Watson SOCK_UNLOCK(so); 1707721f5d7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 171c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_CANTRCVMORE; 1727721f5d7SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 1737721f5d7SRobert Watson SOCKBUF_LOCK(&so->so_snd); 174c0b99ffaSRobert Watson so->so_snd.sb_state |= SBS_CANTSENDMORE; 1757721f5d7SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 17680208239SAlfred Perlstein wakeup(&so->so_timeo); 17703e49181SSeigo Tanimura sowwakeup(so); 17803e49181SSeigo Tanimura sorwakeup(so); 179df8bae1dSRodney W. Grimes } 180df8bae1dSRodney W. Grimes 18126f9a767SRodney W. Grimes void 182df8bae1dSRodney W. Grimes soisdisconnected(so) 183df8bae1dSRodney W. Grimes register struct socket *so; 184df8bae1dSRodney W. Grimes { 185df8bae1dSRodney W. Grimes 1869535efc0SRobert Watson /* 1879535efc0SRobert Watson * XXXRW: This code separately acquires SOCK_LOCK(so) and 1889535efc0SRobert Watson * SOCKBUF_LOCK(&so->so_rcv) even though they are the same mutex to 1899535efc0SRobert Watson * avoid introducing the assumption that they are the same. 1909535efc0SRobert Watson */ 1919535efc0SRobert Watson SOCK_LOCK(so); 19203e49181SSeigo Tanimura so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); 193c0b99ffaSRobert Watson so->so_state |= SS_ISDISCONNECTED; 1949535efc0SRobert Watson SOCK_UNLOCK(so); 1957721f5d7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 196c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_CANTRCVMORE; 1977721f5d7SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 1987721f5d7SRobert Watson SOCKBUF_LOCK(&so->so_snd); 199c0b99ffaSRobert Watson so->so_snd.sb_state |= SBS_CANTSENDMORE; 200a34b7046SRobert Watson sbdrop_locked(&so->so_snd, so->so_snd.sb_cc); 2017721f5d7SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 20280208239SAlfred Perlstein wakeup(&so->so_timeo); 20303e49181SSeigo Tanimura sowwakeup(so); 20403e49181SSeigo Tanimura sorwakeup(so); 205df8bae1dSRodney W. Grimes } 206df8bae1dSRodney W. Grimes 207df8bae1dSRodney W. Grimes /* 208df8bae1dSRodney W. Grimes * When an attempt at a new connection is noted on a socket 209df8bae1dSRodney W. Grimes * which accepts connections, sonewconn is called. If the 210df8bae1dSRodney W. Grimes * connection is possible (subject to space constraints, etc.) 211df8bae1dSRodney W. Grimes * then we allocate a new structure, propoerly linked into the 212df8bae1dSRodney W. Grimes * data structure of the original socket, and return this. 213df8bae1dSRodney W. Grimes * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED. 214b1e4abd2SMatthew Dillon * 215b1e4abd2SMatthew Dillon * note: the ref count on the socket is 0 on return 216df8bae1dSRodney W. Grimes */ 217df8bae1dSRodney W. Grimes struct socket * 218548af278SBill Fenner sonewconn(head, connstatus) 219df8bae1dSRodney W. Grimes register struct socket *head; 220df8bae1dSRodney W. Grimes int connstatus; 221df8bae1dSRodney W. Grimes { 222df8bae1dSRodney W. Grimes register struct socket *so; 2232658b3bbSRobert Watson int over; 224df8bae1dSRodney W. Grimes 2252658b3bbSRobert Watson ACCEPT_LOCK(); 2262658b3bbSRobert Watson over = (head->so_qlen > 3 * head->so_qlimit / 2); 2272658b3bbSRobert Watson ACCEPT_UNLOCK(); 2282658b3bbSRobert Watson if (over) 229df8bae1dSRodney W. Grimes return ((struct socket *)0); 2302bc87dcfSRobert Watson so = soalloc(M_NOWAIT); 231df8bae1dSRodney W. Grimes if (so == NULL) 232df8bae1dSRodney W. Grimes return ((struct socket *)0); 233205b2b61SPeter Wemm if ((head->so_options & SO_ACCEPTFILTER) != 0) 234205b2b61SPeter Wemm connstatus = 0; 235be24e9e8SDavid Greenman so->so_head = head; 236df8bae1dSRodney W. Grimes so->so_type = head->so_type; 237df8bae1dSRodney W. Grimes so->so_options = head->so_options &~ SO_ACCEPTCONN; 238df8bae1dSRodney W. Grimes so->so_linger = head->so_linger; 239df8bae1dSRodney W. Grimes so->so_state = head->so_state | SS_NOFDREF; 240df8bae1dSRodney W. Grimes so->so_proto = head->so_proto; 241df8bae1dSRodney W. Grimes so->so_timeo = head->so_timeo; 242bd78ceceSJohn Baldwin so->so_cred = crhold(head->so_cred); 243335654d7SRobert Watson #ifdef MAC 244310e7cebSRobert Watson SOCK_LOCK(head); 245335654d7SRobert Watson mac_create_socket_from_socket(head, so); 246310e7cebSRobert Watson SOCK_UNLOCK(head); 247335654d7SRobert Watson #endif 2482f9a2132SBrian Feldman if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) || 2492f9a2132SBrian Feldman (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) { 2505ee0a409SAlan Cox sodealloc(so); 251b1396a35SGarrett Wollman return ((struct socket *)0); 252b1396a35SGarrett Wollman } 2532658b3bbSRobert Watson ACCEPT_LOCK(); 254be24e9e8SDavid Greenman if (connstatus) { 255be24e9e8SDavid Greenman TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); 25636568179SRobert Watson so->so_qstate |= SQ_COMP; 257e1f1827fSMike Silbersack head->so_qlen++; 258be24e9e8SDavid Greenman } else { 2592658b3bbSRobert Watson /* 2602658b3bbSRobert Watson * XXXRW: Keep removing sockets from the head until there's 2612658b3bbSRobert Watson * room for us to insert on the tail. In pre-locking 2622658b3bbSRobert Watson * revisions, this was a simple if(), but as we could be 2632658b3bbSRobert Watson * racing with other threads and soabort() requires dropping 2642658b3bbSRobert Watson * locks, we must loop waiting for the condition to be true. 2652658b3bbSRobert Watson */ 2662658b3bbSRobert Watson while (head->so_incqlen > head->so_qlimit) { 267e1f1827fSMike Silbersack struct socket *sp; 268e1f1827fSMike Silbersack sp = TAILQ_FIRST(&head->so_incomp); 2692658b3bbSRobert Watson TAILQ_REMOVE(&so->so_incomp, sp, so_list); 2702658b3bbSRobert Watson head->so_incqlen--; 2712658b3bbSRobert Watson sp->so_qstate &= ~SQ_INCOMP; 2722658b3bbSRobert Watson sp->so_head = NULL; 2732658b3bbSRobert Watson ACCEPT_UNLOCK(); 274e1f1827fSMike Silbersack (void) soabort(sp); 2752658b3bbSRobert Watson ACCEPT_LOCK(); 276e1f1827fSMike Silbersack } 277be24e9e8SDavid Greenman TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list); 27836568179SRobert Watson so->so_qstate |= SQ_INCOMP; 279ebb0cbeaSPaul Traina head->so_incqlen++; 280be24e9e8SDavid Greenman } 2812658b3bbSRobert Watson ACCEPT_UNLOCK(); 282df8bae1dSRodney W. Grimes if (connstatus) { 2832658b3bbSRobert Watson so->so_state |= connstatus; 28403e49181SSeigo Tanimura sorwakeup(head); 285c2696aafSPaul Saab wakeup_one(&head->so_timeo); 286df8bae1dSRodney W. Grimes } 287df8bae1dSRodney W. Grimes return (so); 288df8bae1dSRodney W. Grimes } 289df8bae1dSRodney W. Grimes 290df8bae1dSRodney W. Grimes /* 291df8bae1dSRodney W. Grimes * Socantsendmore indicates that no more data will be sent on the 292df8bae1dSRodney W. Grimes * socket; it would normally be applied to a socket when the user 293df8bae1dSRodney W. Grimes * informs the system that no more data is to be sent, by the protocol 294df8bae1dSRodney W. Grimes * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data 295df8bae1dSRodney W. Grimes * will be received, and will normally be applied to the socket by a 296df8bae1dSRodney W. Grimes * protocol when it detects that the peer will send no more data. 297df8bae1dSRodney W. Grimes * Data queued for reading in the socket may yet be read. 298df8bae1dSRodney W. Grimes */ 299a34b7046SRobert Watson void 300a34b7046SRobert Watson socantsendmore_locked(so) 301a34b7046SRobert Watson struct socket *so; 302a34b7046SRobert Watson { 303a34b7046SRobert Watson 304a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(&so->so_snd); 305a34b7046SRobert Watson 306a34b7046SRobert Watson so->so_snd.sb_state |= SBS_CANTSENDMORE; 307a34b7046SRobert Watson sowwakeup_locked(so); 308a34b7046SRobert Watson mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED); 309a34b7046SRobert Watson } 310df8bae1dSRodney W. Grimes 31126f9a767SRodney W. Grimes void 312df8bae1dSRodney W. Grimes socantsendmore(so) 313df8bae1dSRodney W. Grimes struct socket *so; 314df8bae1dSRodney W. Grimes { 315df8bae1dSRodney W. Grimes 316a34b7046SRobert Watson SOCKBUF_LOCK(&so->so_snd); 317a34b7046SRobert Watson socantsendmore_locked(so); 318a34b7046SRobert Watson mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED); 319a34b7046SRobert Watson } 320a34b7046SRobert Watson 321a34b7046SRobert Watson void 322a34b7046SRobert Watson socantrcvmore_locked(so) 323a34b7046SRobert Watson struct socket *so; 324a34b7046SRobert Watson { 325a34b7046SRobert Watson 326a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(&so->so_rcv); 327a34b7046SRobert Watson 328a34b7046SRobert Watson so->so_rcv.sb_state |= SBS_CANTRCVMORE; 329a34b7046SRobert Watson sorwakeup_locked(so); 330a34b7046SRobert Watson mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED); 331df8bae1dSRodney W. Grimes } 332df8bae1dSRodney W. Grimes 33326f9a767SRodney W. Grimes void 334df8bae1dSRodney W. Grimes socantrcvmore(so) 335df8bae1dSRodney W. Grimes struct socket *so; 336df8bae1dSRodney W. Grimes { 337df8bae1dSRodney W. Grimes 338a34b7046SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 339a34b7046SRobert Watson socantrcvmore_locked(so); 340a34b7046SRobert Watson mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED); 341df8bae1dSRodney W. Grimes } 342df8bae1dSRodney W. Grimes 343df8bae1dSRodney W. Grimes /* 344df8bae1dSRodney W. Grimes * Wait for data to arrive at/drain from a socket buffer. 345df8bae1dSRodney W. Grimes */ 34626f9a767SRodney W. Grimes int 347df8bae1dSRodney W. Grimes sbwait(sb) 348df8bae1dSRodney W. Grimes struct sockbuf *sb; 349df8bae1dSRodney W. Grimes { 350df8bae1dSRodney W. Grimes 35131f555a1SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 35231f555a1SRobert Watson 353df8bae1dSRodney W. Grimes sb->sb_flags |= SB_WAIT; 35431f555a1SRobert Watson return (msleep(&sb->sb_cc, &sb->sb_mtx, 35547daf5d5SBruce Evans (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait", 356df8bae1dSRodney W. Grimes sb->sb_timeo)); 357df8bae1dSRodney W. Grimes } 358df8bae1dSRodney W. Grimes 359df8bae1dSRodney W. Grimes /* 360df8bae1dSRodney W. Grimes * Lock a sockbuf already known to be locked; 361df8bae1dSRodney W. Grimes * return any error returned from sleep (EINTR). 362df8bae1dSRodney W. Grimes */ 36326f9a767SRodney W. Grimes int 364df8bae1dSRodney W. Grimes sb_lock(sb) 365df8bae1dSRodney W. Grimes register struct sockbuf *sb; 366df8bae1dSRodney W. Grimes { 367df8bae1dSRodney W. Grimes int error; 368df8bae1dSRodney W. Grimes 36931f555a1SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 37031f555a1SRobert Watson 371df8bae1dSRodney W. Grimes while (sb->sb_flags & SB_LOCK) { 372df8bae1dSRodney W. Grimes sb->sb_flags |= SB_WANT; 37331f555a1SRobert Watson error = msleep(&sb->sb_flags, &sb->sb_mtx, 374df8bae1dSRodney W. Grimes (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH, 37547daf5d5SBruce Evans "sblock", 0); 376797f2d22SPoul-Henning Kamp if (error) 377df8bae1dSRodney W. Grimes return (error); 378df8bae1dSRodney W. Grimes } 379df8bae1dSRodney W. Grimes sb->sb_flags |= SB_LOCK; 380df8bae1dSRodney W. Grimes return (0); 381df8bae1dSRodney W. Grimes } 382df8bae1dSRodney W. Grimes 383df8bae1dSRodney W. Grimes /* 384a34b7046SRobert Watson * Wakeup processes waiting on a socket buffer. Do asynchronous 385a34b7046SRobert Watson * notification via SIGIO if the socket has the SS_ASYNC flag set. 386a34b7046SRobert Watson * 387a34b7046SRobert Watson * Called with the socket buffer lock held; will release the lock by the end 388a34b7046SRobert Watson * of the function. This allows the caller to acquire the socket buffer lock 389a34b7046SRobert Watson * while testing for the need for various sorts of wakeup and hold it through 390a34b7046SRobert Watson * to the point where it's no longer required. We currently hold the lock 391a34b7046SRobert Watson * through calls out to other subsystems (with the exception of kqueue), and 392a34b7046SRobert Watson * then release it to avoid lock order issues. It's not clear that's 393a34b7046SRobert Watson * correct. 394df8bae1dSRodney W. Grimes */ 39526f9a767SRodney W. Grimes void 396df8bae1dSRodney W. Grimes sowakeup(so, sb) 397df8bae1dSRodney W. Grimes register struct socket *so; 398df8bae1dSRodney W. Grimes register struct sockbuf *sb; 399df8bae1dSRodney W. Grimes { 400d48d4b25SSeigo Tanimura 401a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 402a34b7046SRobert Watson 403512824f8SSeigo Tanimura selwakeuppri(&sb->sb_sel, PSOCK); 404df8bae1dSRodney W. Grimes sb->sb_flags &= ~SB_SEL; 405df8bae1dSRodney W. Grimes if (sb->sb_flags & SB_WAIT) { 406df8bae1dSRodney W. Grimes sb->sb_flags &= ~SB_WAIT; 40780208239SAlfred Perlstein wakeup(&sb->sb_cc); 408df8bae1dSRodney W. Grimes } 409a34b7046SRobert Watson KNOTE(&sb->sb_sel.si_note, 0); 410a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 4114cc20ab1SSeigo Tanimura if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL) 412f1320723SAlfred Perlstein pgsigio(&so->so_sigio, SIGIO, 0); 4134cc20ab1SSeigo Tanimura if (sb->sb_flags & SB_UPCALL) 414a163d034SWarner Losh (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT); 4154cc20ab1SSeigo Tanimura if (sb->sb_flags & SB_AIO) 416bfbbc4aaSJason Evans aio_swake(so, sb); 417a34b7046SRobert Watson mtx_assert(SOCKBUF_MTX(sb), MA_NOTOWNED); 418df8bae1dSRodney W. Grimes } 419df8bae1dSRodney W. Grimes 420df8bae1dSRodney W. Grimes /* 421df8bae1dSRodney W. Grimes * Socket buffer (struct sockbuf) utility routines. 422df8bae1dSRodney W. Grimes * 423df8bae1dSRodney W. Grimes * Each socket contains two socket buffers: one for sending data and 424df8bae1dSRodney W. Grimes * one for receiving data. Each buffer contains a queue of mbufs, 425df8bae1dSRodney W. Grimes * information about the number of mbufs and amount of data in the 426df8bae1dSRodney W. Grimes * queue, and other fields allowing select() statements and notification 427df8bae1dSRodney W. Grimes * on data availability to be implemented. 428df8bae1dSRodney W. Grimes * 429df8bae1dSRodney W. Grimes * Data stored in a socket buffer is maintained as a list of records. 430df8bae1dSRodney W. Grimes * Each record is a list of mbufs chained together with the m_next 431df8bae1dSRodney W. Grimes * field. Records are chained together with the m_nextpkt field. The upper 432df8bae1dSRodney W. Grimes * level routine soreceive() expects the following conventions to be 433df8bae1dSRodney W. Grimes * observed when placing information in the receive buffer: 434df8bae1dSRodney W. Grimes * 435df8bae1dSRodney W. Grimes * 1. If the protocol requires each message be preceded by the sender's 436df8bae1dSRodney W. Grimes * name, then a record containing that name must be present before 437df8bae1dSRodney W. Grimes * any associated data (mbuf's must be of type MT_SONAME). 438df8bae1dSRodney W. Grimes * 2. If the protocol supports the exchange of ``access rights'' (really 439df8bae1dSRodney W. Grimes * just additional data associated with the message), and there are 440df8bae1dSRodney W. Grimes * ``rights'' to be received, then a record containing this data 441df8bae1dSRodney W. Grimes * should be present (mbuf's must be of type MT_RIGHTS). 442df8bae1dSRodney W. Grimes * 3. If a name or rights record exists, then it must be followed by 443df8bae1dSRodney W. Grimes * a data record, perhaps of zero length. 444df8bae1dSRodney W. Grimes * 445df8bae1dSRodney W. Grimes * Before using a new socket structure it is first necessary to reserve 446df8bae1dSRodney W. Grimes * buffer space to the socket, by calling sbreserve(). This should commit 447df8bae1dSRodney W. Grimes * some of the available buffer space in the system buffer pool for the 448df8bae1dSRodney W. Grimes * socket (currently, it does nothing but enforce limits). The space 449df8bae1dSRodney W. Grimes * should be released by calling sbrelease() when the socket is destroyed. 450df8bae1dSRodney W. Grimes */ 451df8bae1dSRodney W. Grimes 45226f9a767SRodney W. Grimes int 453df8bae1dSRodney W. Grimes soreserve(so, sndcc, rcvcc) 454df8bae1dSRodney W. Grimes register struct socket *so; 455df8bae1dSRodney W. Grimes u_long sndcc, rcvcc; 456df8bae1dSRodney W. Grimes { 457b40ce416SJulian Elischer struct thread *td = curthread; 458df8bae1dSRodney W. Grimes 4593f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 4609535efc0SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 4613f11a2f3SRobert Watson if (sbreserve_locked(&so->so_snd, sndcc, so, td) == 0) 4623f11a2f3SRobert Watson goto bad; 4633f11a2f3SRobert Watson if (sbreserve_locked(&so->so_rcv, rcvcc, so, td) == 0) 4643f11a2f3SRobert Watson goto bad2; 465df8bae1dSRodney W. Grimes if (so->so_rcv.sb_lowat == 0) 466df8bae1dSRodney W. Grimes so->so_rcv.sb_lowat = 1; 467df8bae1dSRodney W. Grimes if (so->so_snd.sb_lowat == 0) 468df8bae1dSRodney W. Grimes so->so_snd.sb_lowat = MCLBYTES; 469df8bae1dSRodney W. Grimes if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) 470df8bae1dSRodney W. Grimes so->so_snd.sb_lowat = so->so_snd.sb_hiwat; 4713f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 4729535efc0SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 473df8bae1dSRodney W. Grimes return (0); 474df8bae1dSRodney W. Grimes bad2: 4753f11a2f3SRobert Watson sbrelease_locked(&so->so_snd, so); 476df8bae1dSRodney W. Grimes bad: 4773f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 4783f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 479df8bae1dSRodney W. Grimes return (ENOBUFS); 480df8bae1dSRodney W. Grimes } 481df8bae1dSRodney W. Grimes 48279cb7eb4SDavid Greenman static int 48379cb7eb4SDavid Greenman sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS) 48479cb7eb4SDavid Greenman { 48579cb7eb4SDavid Greenman int error = 0; 48679cb7eb4SDavid Greenman u_long old_sb_max = sb_max; 48779cb7eb4SDavid Greenman 4881b978d45SHartmut Brandt error = SYSCTL_OUT(req, arg1, sizeof(u_long)); 48979cb7eb4SDavid Greenman if (error || !req->newptr) 49079cb7eb4SDavid Greenman return (error); 4911b978d45SHartmut Brandt error = SYSCTL_IN(req, arg1, sizeof(u_long)); 49279cb7eb4SDavid Greenman if (error) 49379cb7eb4SDavid Greenman return (error); 49479cb7eb4SDavid Greenman if (sb_max < MSIZE + MCLBYTES) { 49579cb7eb4SDavid Greenman sb_max = old_sb_max; 49679cb7eb4SDavid Greenman return (EINVAL); 49779cb7eb4SDavid Greenman } 49879cb7eb4SDavid Greenman sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES); 49979cb7eb4SDavid Greenman return (0); 50079cb7eb4SDavid Greenman } 50179cb7eb4SDavid Greenman 502df8bae1dSRodney W. Grimes /* 503df8bae1dSRodney W. Grimes * Allot mbufs to a sockbuf. 504df8bae1dSRodney W. Grimes * Attempt to scale mbmax so that mbcnt doesn't become limiting 505df8bae1dSRodney W. Grimes * if buffering efficiency is near the normal case. 506df8bae1dSRodney W. Grimes */ 50726f9a767SRodney W. Grimes int 5083f11a2f3SRobert Watson sbreserve_locked(sb, cc, so, td) 509df8bae1dSRodney W. Grimes struct sockbuf *sb; 510df8bae1dSRodney W. Grimes u_long cc; 511ecf72308SBrian Feldman struct socket *so; 512b40ce416SJulian Elischer struct thread *td; 513df8bae1dSRodney W. Grimes { 51491d5354aSJohn Baldwin rlim_t sbsize_limit; 515ecf72308SBrian Feldman 5163f11a2f3SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 5173f11a2f3SRobert Watson 518ecf72308SBrian Feldman /* 519b40ce416SJulian Elischer * td will only be NULL when we're in an interrupt 520ecf72308SBrian Feldman * (e.g. in tcp_input()) 521ecf72308SBrian Feldman */ 52279cb7eb4SDavid Greenman if (cc > sb_max_adj) 523df8bae1dSRodney W. Grimes return (0); 52491d5354aSJohn Baldwin if (td != NULL) { 52591d5354aSJohn Baldwin PROC_LOCK(td->td_proc); 52691d5354aSJohn Baldwin sbsize_limit = lim_cur(td->td_proc, RLIMIT_SBSIZE); 52791d5354aSJohn Baldwin PROC_UNLOCK(td->td_proc); 52891d5354aSJohn Baldwin } else 52991d5354aSJohn Baldwin sbsize_limit = RLIM_INFINITY; 530f535380cSDon Lewis if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc, 53191d5354aSJohn Baldwin sbsize_limit)) 532ecf72308SBrian Feldman return (0); 5334b29bc4fSGarrett Wollman sb->sb_mbmax = min(cc * sb_efficiency, sb_max); 534df8bae1dSRodney W. Grimes if (sb->sb_lowat > sb->sb_hiwat) 535df8bae1dSRodney W. Grimes sb->sb_lowat = sb->sb_hiwat; 536df8bae1dSRodney W. Grimes return (1); 537df8bae1dSRodney W. Grimes } 538df8bae1dSRodney W. Grimes 5393f11a2f3SRobert Watson int 5403f11a2f3SRobert Watson sbreserve(sb, cc, so, td) 5413f11a2f3SRobert Watson struct sockbuf *sb; 5423f11a2f3SRobert Watson u_long cc; 5433f11a2f3SRobert Watson struct socket *so; 5443f11a2f3SRobert Watson struct thread *td; 5453f11a2f3SRobert Watson { 5463f11a2f3SRobert Watson int error; 5473f11a2f3SRobert Watson 5483f11a2f3SRobert Watson SOCKBUF_LOCK(sb); 5493f11a2f3SRobert Watson error = sbreserve_locked(sb, cc, so, td); 5503f11a2f3SRobert Watson SOCKBUF_UNLOCK(sb); 5513f11a2f3SRobert Watson return (error); 5523f11a2f3SRobert Watson } 5533f11a2f3SRobert Watson 554df8bae1dSRodney W. Grimes /* 555df8bae1dSRodney W. Grimes * Free mbufs held by a socket, and reserved mbuf space. 556df8bae1dSRodney W. Grimes */ 55726f9a767SRodney W. Grimes void 558a34b7046SRobert Watson sbrelease_locked(sb, so) 559df8bae1dSRodney W. Grimes struct sockbuf *sb; 560ecf72308SBrian Feldman struct socket *so; 561df8bae1dSRodney W. Grimes { 562df8bae1dSRodney W. Grimes 563a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 564a34b7046SRobert Watson 565a34b7046SRobert Watson sbflush_locked(sb); 566f535380cSDon Lewis (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0, 567f535380cSDon Lewis RLIM_INFINITY); 5686aef685fSBrian Feldman sb->sb_mbmax = 0; 569df8bae1dSRodney W. Grimes } 570df8bae1dSRodney W. Grimes 571a34b7046SRobert Watson void 572a34b7046SRobert Watson sbrelease(sb, so) 573a34b7046SRobert Watson struct sockbuf *sb; 574a34b7046SRobert Watson struct socket *so; 575a34b7046SRobert Watson { 576a34b7046SRobert Watson 577a34b7046SRobert Watson SOCKBUF_LOCK(sb); 578a34b7046SRobert Watson sbrelease_locked(sb, so); 579a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 580a34b7046SRobert Watson } 581df8bae1dSRodney W. Grimes /* 582df8bae1dSRodney W. Grimes * Routines to add and remove 583df8bae1dSRodney W. Grimes * data from an mbuf queue. 584df8bae1dSRodney W. Grimes * 585df8bae1dSRodney W. Grimes * The routines sbappend() or sbappendrecord() are normally called to 586df8bae1dSRodney W. Grimes * append new mbufs to a socket buffer, after checking that adequate 587df8bae1dSRodney W. Grimes * space is available, comparing the function sbspace() with the amount 588df8bae1dSRodney W. Grimes * of data to be added. sbappendrecord() differs from sbappend() in 589df8bae1dSRodney W. Grimes * that data supplied is treated as the beginning of a new record. 590df8bae1dSRodney W. Grimes * To place a sender's address, optional access rights, and data in a 591df8bae1dSRodney W. Grimes * socket receive buffer, sbappendaddr() should be used. To place 592df8bae1dSRodney W. Grimes * access rights and data in a socket receive buffer, sbappendrights() 593df8bae1dSRodney W. Grimes * should be used. In either case, the new data begins a new record. 594df8bae1dSRodney W. Grimes * Note that unlike sbappend() and sbappendrecord(), these routines check 595df8bae1dSRodney W. Grimes * for the caller that there will be enough space to store the data. 596df8bae1dSRodney W. Grimes * Each fails if there is not enough space, or if it cannot find mbufs 597df8bae1dSRodney W. Grimes * to store additional information in. 598df8bae1dSRodney W. Grimes * 599df8bae1dSRodney W. Grimes * Reliable protocols may use the socket send buffer to hold data 600df8bae1dSRodney W. Grimes * awaiting acknowledgement. Data is normally copied from a socket 601df8bae1dSRodney W. Grimes * send buffer in a protocol with m_copy for output to a peer, 602df8bae1dSRodney W. Grimes * and then removing the data from the socket buffer with sbdrop() 603df8bae1dSRodney W. Grimes * or sbdroprecord() when the data is acknowledged by the peer. 604df8bae1dSRodney W. Grimes */ 605df8bae1dSRodney W. Grimes 606395bb186SSam Leffler #ifdef SOCKBUF_DEBUG 607395bb186SSam Leffler void 608395bb186SSam Leffler sblastrecordchk(struct sockbuf *sb, const char *file, int line) 609395bb186SSam Leffler { 610395bb186SSam Leffler struct mbuf *m = sb->sb_mb; 611395bb186SSam Leffler 612a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 613a34b7046SRobert Watson 614395bb186SSam Leffler while (m && m->m_nextpkt) 615395bb186SSam Leffler m = m->m_nextpkt; 616395bb186SSam Leffler 617395bb186SSam Leffler if (m != sb->sb_lastrecord) { 618395bb186SSam Leffler printf("%s: sb_mb %p sb_lastrecord %p last %p\n", 619395bb186SSam Leffler __func__, sb->sb_mb, sb->sb_lastrecord, m); 620395bb186SSam Leffler printf("packet chain:\n"); 621395bb186SSam Leffler for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) 622395bb186SSam Leffler printf("\t%p\n", m); 623395bb186SSam Leffler panic("%s from %s:%u", __func__, file, line); 624395bb186SSam Leffler } 625395bb186SSam Leffler } 626395bb186SSam Leffler 627395bb186SSam Leffler void 628395bb186SSam Leffler sblastmbufchk(struct sockbuf *sb, const char *file, int line) 629395bb186SSam Leffler { 630395bb186SSam Leffler struct mbuf *m = sb->sb_mb; 631395bb186SSam Leffler struct mbuf *n; 632395bb186SSam Leffler 633a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 634a34b7046SRobert Watson 635395bb186SSam Leffler while (m && m->m_nextpkt) 636395bb186SSam Leffler m = m->m_nextpkt; 637395bb186SSam Leffler 638395bb186SSam Leffler while (m && m->m_next) 639395bb186SSam Leffler m = m->m_next; 640395bb186SSam Leffler 641395bb186SSam Leffler if (m != sb->sb_mbtail) { 642395bb186SSam Leffler printf("%s: sb_mb %p sb_mbtail %p last %p\n", 643395bb186SSam Leffler __func__, sb->sb_mb, sb->sb_mbtail, m); 644395bb186SSam Leffler printf("packet tree:\n"); 645395bb186SSam Leffler for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) { 646395bb186SSam Leffler printf("\t"); 647395bb186SSam Leffler for (n = m; n != NULL; n = n->m_next) 648395bb186SSam Leffler printf("%p ", n); 649395bb186SSam Leffler printf("\n"); 650395bb186SSam Leffler } 651395bb186SSam Leffler panic("%s from %s:%u", __func__, file, line); 652395bb186SSam Leffler } 653395bb186SSam Leffler } 654395bb186SSam Leffler #endif /* SOCKBUF_DEBUG */ 655395bb186SSam Leffler 656395bb186SSam Leffler #define SBLINKRECORD(sb, m0) do { \ 657a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); \ 658395bb186SSam Leffler if ((sb)->sb_lastrecord != NULL) \ 659395bb186SSam Leffler (sb)->sb_lastrecord->m_nextpkt = (m0); \ 660395bb186SSam Leffler else \ 661395bb186SSam Leffler (sb)->sb_mb = (m0); \ 662395bb186SSam Leffler (sb)->sb_lastrecord = (m0); \ 663395bb186SSam Leffler } while (/*CONSTCOND*/0) 664395bb186SSam Leffler 665df8bae1dSRodney W. Grimes /* 666df8bae1dSRodney W. Grimes * Append mbuf chain m to the last record in the 667df8bae1dSRodney W. Grimes * socket buffer sb. The additional space associated 668df8bae1dSRodney W. Grimes * the mbuf chain is recorded in sb. Empty mbufs are 669df8bae1dSRodney W. Grimes * discarded and mbufs are compacted where possible. 670df8bae1dSRodney W. Grimes */ 67126f9a767SRodney W. Grimes void 672a34b7046SRobert Watson sbappend_locked(sb, m) 673df8bae1dSRodney W. Grimes struct sockbuf *sb; 674df8bae1dSRodney W. Grimes struct mbuf *m; 675df8bae1dSRodney W. Grimes { 676df8bae1dSRodney W. Grimes register struct mbuf *n; 677df8bae1dSRodney W. Grimes 678a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 679a34b7046SRobert Watson 680df8bae1dSRodney W. Grimes if (m == 0) 681df8bae1dSRodney W. Grimes return; 682a34b7046SRobert Watson 683395bb186SSam Leffler SBLASTRECORDCHK(sb); 684797f2d22SPoul-Henning Kamp n = sb->sb_mb; 685797f2d22SPoul-Henning Kamp if (n) { 686df8bae1dSRodney W. Grimes while (n->m_nextpkt) 687df8bae1dSRodney W. Grimes n = n->m_nextpkt; 688df8bae1dSRodney W. Grimes do { 689df8bae1dSRodney W. Grimes if (n->m_flags & M_EOR) { 690a34b7046SRobert Watson sbappendrecord_locked(sb, m); /* XXXXXX!!!! */ 691df8bae1dSRodney W. Grimes return; 692df8bae1dSRodney W. Grimes } 693df8bae1dSRodney W. Grimes } while (n->m_next && (n = n->m_next)); 694395bb186SSam Leffler } else { 695395bb186SSam Leffler /* 696395bb186SSam Leffler * XXX Would like to simply use sb_mbtail here, but 697395bb186SSam Leffler * XXX I need to verify that I won't miss an EOR that 698395bb186SSam Leffler * XXX way. 699395bb186SSam Leffler */ 700395bb186SSam Leffler if ((n = sb->sb_lastrecord) != NULL) { 701395bb186SSam Leffler do { 702395bb186SSam Leffler if (n->m_flags & M_EOR) { 703a34b7046SRobert Watson sbappendrecord_locked(sb, m); /* XXXXXX!!!! */ 704395bb186SSam Leffler return; 705395bb186SSam Leffler } 706395bb186SSam Leffler } while (n->m_next && (n = n->m_next)); 707395bb186SSam Leffler } else { 708395bb186SSam Leffler /* 709395bb186SSam Leffler * If this is the first record in the socket buffer, 710395bb186SSam Leffler * it's also the last record. 711395bb186SSam Leffler */ 712395bb186SSam Leffler sb->sb_lastrecord = m; 713395bb186SSam Leffler } 714df8bae1dSRodney W. Grimes } 715df8bae1dSRodney W. Grimes sbcompress(sb, m, n); 716395bb186SSam Leffler SBLASTRECORDCHK(sb); 717395bb186SSam Leffler } 718395bb186SSam Leffler 719395bb186SSam Leffler /* 720a34b7046SRobert Watson * Append mbuf chain m to the last record in the 721a34b7046SRobert Watson * socket buffer sb. The additional space associated 722a34b7046SRobert Watson * the mbuf chain is recorded in sb. Empty mbufs are 723a34b7046SRobert Watson * discarded and mbufs are compacted where possible. 724a34b7046SRobert Watson */ 725a34b7046SRobert Watson void 726a34b7046SRobert Watson sbappend(sb, m) 727a34b7046SRobert Watson struct sockbuf *sb; 728a34b7046SRobert Watson struct mbuf *m; 729a34b7046SRobert Watson { 730a34b7046SRobert Watson 731a34b7046SRobert Watson SOCKBUF_LOCK(sb); 732a34b7046SRobert Watson sbappend_locked(sb, m); 733a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 734a34b7046SRobert Watson } 735a34b7046SRobert Watson 736a34b7046SRobert Watson /* 737395bb186SSam Leffler * This version of sbappend() should only be used when the caller 738395bb186SSam Leffler * absolutely knows that there will never be more than one record 739395bb186SSam Leffler * in the socket buffer, that is, a stream protocol (such as TCP). 740395bb186SSam Leffler */ 741395bb186SSam Leffler void 742a34b7046SRobert Watson sbappendstream_locked(struct sockbuf *sb, struct mbuf *m) 743395bb186SSam Leffler { 744a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 745395bb186SSam Leffler 746395bb186SSam Leffler KASSERT(m->m_nextpkt == NULL,("sbappendstream 0")); 747395bb186SSam Leffler KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1")); 748395bb186SSam Leffler 749395bb186SSam Leffler SBLASTMBUFCHK(sb); 750395bb186SSam Leffler 751395bb186SSam Leffler sbcompress(sb, m, sb->sb_mbtail); 752395bb186SSam Leffler 753395bb186SSam Leffler sb->sb_lastrecord = sb->sb_mb; 754395bb186SSam Leffler SBLASTRECORDCHK(sb); 755df8bae1dSRodney W. Grimes } 756df8bae1dSRodney W. Grimes 757a34b7046SRobert Watson /* 758a34b7046SRobert Watson * This version of sbappend() should only be used when the caller 759a34b7046SRobert Watson * absolutely knows that there will never be more than one record 760a34b7046SRobert Watson * in the socket buffer, that is, a stream protocol (such as TCP). 761a34b7046SRobert Watson */ 762a34b7046SRobert Watson void 763a34b7046SRobert Watson sbappendstream(struct sockbuf *sb, struct mbuf *m) 764a34b7046SRobert Watson { 765a34b7046SRobert Watson 766a34b7046SRobert Watson SOCKBUF_LOCK(sb); 767a34b7046SRobert Watson sbappendstream_locked(sb, m); 768a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 769a34b7046SRobert Watson } 770a34b7046SRobert Watson 771df8bae1dSRodney W. Grimes #ifdef SOCKBUF_DEBUG 77226f9a767SRodney W. Grimes void 773df8bae1dSRodney W. Grimes sbcheck(sb) 7747ed60de8SPoul-Henning Kamp struct sockbuf *sb; 775df8bae1dSRodney W. Grimes { 7767ed60de8SPoul-Henning Kamp struct mbuf *m; 7777ed60de8SPoul-Henning Kamp struct mbuf *n = 0; 7787ed60de8SPoul-Henning Kamp u_long len = 0, mbcnt = 0; 779df8bae1dSRodney W. Grimes 780a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 781a34b7046SRobert Watson 7820931333fSBill Fenner for (m = sb->sb_mb; m; m = n) { 7830931333fSBill Fenner n = m->m_nextpkt; 7840931333fSBill Fenner for (; m; m = m->m_next) { 785df8bae1dSRodney W. Grimes len += m->m_len; 786df8bae1dSRodney W. Grimes mbcnt += MSIZE; 787313861b8SJulian Elischer if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */ 788df8bae1dSRodney W. Grimes mbcnt += m->m_ext.ext_size; 7890931333fSBill Fenner } 790df8bae1dSRodney W. Grimes } 791df8bae1dSRodney W. Grimes if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) { 792395bb186SSam Leffler printf("cc %ld != %u || mbcnt %ld != %u\n", len, sb->sb_cc, 793df8bae1dSRodney W. Grimes mbcnt, sb->sb_mbcnt); 794df8bae1dSRodney W. Grimes panic("sbcheck"); 795df8bae1dSRodney W. Grimes } 796df8bae1dSRodney W. Grimes } 797df8bae1dSRodney W. Grimes #endif 798df8bae1dSRodney W. Grimes 799df8bae1dSRodney W. Grimes /* 800df8bae1dSRodney W. Grimes * As above, except the mbuf chain 801df8bae1dSRodney W. Grimes * begins a new record. 802df8bae1dSRodney W. Grimes */ 80326f9a767SRodney W. Grimes void 804a34b7046SRobert Watson sbappendrecord_locked(sb, m0) 805df8bae1dSRodney W. Grimes register struct sockbuf *sb; 806df8bae1dSRodney W. Grimes register struct mbuf *m0; 807df8bae1dSRodney W. Grimes { 808df8bae1dSRodney W. Grimes register struct mbuf *m; 809df8bae1dSRodney W. Grimes 810a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 811a34b7046SRobert Watson 812df8bae1dSRodney W. Grimes if (m0 == 0) 813df8bae1dSRodney W. Grimes return; 814797f2d22SPoul-Henning Kamp m = sb->sb_mb; 815797f2d22SPoul-Henning Kamp if (m) 816df8bae1dSRodney W. Grimes while (m->m_nextpkt) 817df8bae1dSRodney W. Grimes m = m->m_nextpkt; 818df8bae1dSRodney W. Grimes /* 819df8bae1dSRodney W. Grimes * Put the first mbuf on the queue. 820df8bae1dSRodney W. Grimes * Note this permits zero length records. 821df8bae1dSRodney W. Grimes */ 822df8bae1dSRodney W. Grimes sballoc(sb, m0); 823395bb186SSam Leffler SBLASTRECORDCHK(sb); 824395bb186SSam Leffler SBLINKRECORD(sb, m0); 825df8bae1dSRodney W. Grimes if (m) 826df8bae1dSRodney W. Grimes m->m_nextpkt = m0; 827df8bae1dSRodney W. Grimes else 828df8bae1dSRodney W. Grimes sb->sb_mb = m0; 829df8bae1dSRodney W. Grimes m = m0->m_next; 830df8bae1dSRodney W. Grimes m0->m_next = 0; 831df8bae1dSRodney W. Grimes if (m && (m0->m_flags & M_EOR)) { 832df8bae1dSRodney W. Grimes m0->m_flags &= ~M_EOR; 833df8bae1dSRodney W. Grimes m->m_flags |= M_EOR; 834df8bae1dSRodney W. Grimes } 835df8bae1dSRodney W. Grimes sbcompress(sb, m, m0); 836df8bae1dSRodney W. Grimes } 837df8bae1dSRodney W. Grimes 838df8bae1dSRodney W. Grimes /* 839a34b7046SRobert Watson * As above, except the mbuf chain 840a34b7046SRobert Watson * begins a new record. 841a34b7046SRobert Watson */ 842a34b7046SRobert Watson void 843a34b7046SRobert Watson sbappendrecord(sb, m0) 844a34b7046SRobert Watson register struct sockbuf *sb; 845a34b7046SRobert Watson register struct mbuf *m0; 846a34b7046SRobert Watson { 847a34b7046SRobert Watson 848a34b7046SRobert Watson SOCKBUF_LOCK(sb); 849a34b7046SRobert Watson sbappendrecord_locked(sb, m0); 850a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 851a34b7046SRobert Watson } 852a34b7046SRobert Watson 853a34b7046SRobert Watson /* 854df8bae1dSRodney W. Grimes * As above except that OOB data 855df8bae1dSRodney W. Grimes * is inserted at the beginning of the sockbuf, 856df8bae1dSRodney W. Grimes * but after any other OOB data. 857df8bae1dSRodney W. Grimes */ 85826f9a767SRodney W. Grimes void 859a34b7046SRobert Watson sbinsertoob_locked(sb, m0) 860df8bae1dSRodney W. Grimes register struct sockbuf *sb; 861df8bae1dSRodney W. Grimes register struct mbuf *m0; 862df8bae1dSRodney W. Grimes { 863df8bae1dSRodney W. Grimes register struct mbuf *m; 864df8bae1dSRodney W. Grimes register struct mbuf **mp; 865df8bae1dSRodney W. Grimes 866a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 867a34b7046SRobert Watson 868df8bae1dSRodney W. Grimes if (m0 == 0) 869df8bae1dSRodney W. Grimes return; 870797f2d22SPoul-Henning Kamp for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) { 871797f2d22SPoul-Henning Kamp m = *mp; 872df8bae1dSRodney W. Grimes again: 873df8bae1dSRodney W. Grimes switch (m->m_type) { 874df8bae1dSRodney W. Grimes 875df8bae1dSRodney W. Grimes case MT_OOBDATA: 876df8bae1dSRodney W. Grimes continue; /* WANT next train */ 877df8bae1dSRodney W. Grimes 878df8bae1dSRodney W. Grimes case MT_CONTROL: 879797f2d22SPoul-Henning Kamp m = m->m_next; 880797f2d22SPoul-Henning Kamp if (m) 881df8bae1dSRodney W. Grimes goto again; /* inspect THIS train further */ 882df8bae1dSRodney W. Grimes } 883df8bae1dSRodney W. Grimes break; 884df8bae1dSRodney W. Grimes } 885df8bae1dSRodney W. Grimes /* 886df8bae1dSRodney W. Grimes * Put the first mbuf on the queue. 887df8bae1dSRodney W. Grimes * Note this permits zero length records. 888df8bae1dSRodney W. Grimes */ 889df8bae1dSRodney W. Grimes sballoc(sb, m0); 890df8bae1dSRodney W. Grimes m0->m_nextpkt = *mp; 891df8bae1dSRodney W. Grimes *mp = m0; 892df8bae1dSRodney W. Grimes m = m0->m_next; 893df8bae1dSRodney W. Grimes m0->m_next = 0; 894df8bae1dSRodney W. Grimes if (m && (m0->m_flags & M_EOR)) { 895df8bae1dSRodney W. Grimes m0->m_flags &= ~M_EOR; 896df8bae1dSRodney W. Grimes m->m_flags |= M_EOR; 897df8bae1dSRodney W. Grimes } 898df8bae1dSRodney W. Grimes sbcompress(sb, m, m0); 899df8bae1dSRodney W. Grimes } 900df8bae1dSRodney W. Grimes 901df8bae1dSRodney W. Grimes /* 902a34b7046SRobert Watson * As above except that OOB data 903a34b7046SRobert Watson * is inserted at the beginning of the sockbuf, 904a34b7046SRobert Watson * but after any other OOB data. 905a34b7046SRobert Watson */ 906a34b7046SRobert Watson void 907a34b7046SRobert Watson sbinsertoob(sb, m0) 908a34b7046SRobert Watson register struct sockbuf *sb; 909a34b7046SRobert Watson register struct mbuf *m0; 910a34b7046SRobert Watson { 911a34b7046SRobert Watson 912a34b7046SRobert Watson SOCKBUF_LOCK(sb); 913a34b7046SRobert Watson sbinsertoob_locked(sb, m0); 914a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 915a34b7046SRobert Watson } 916a34b7046SRobert Watson 917a34b7046SRobert Watson /* 918df8bae1dSRodney W. Grimes * Append address and data, and optionally, control (ancillary) data 919df8bae1dSRodney W. Grimes * to the receive queue of a socket. If present, 920df8bae1dSRodney W. Grimes * m0 must include a packet header with total length. 921df8bae1dSRodney W. Grimes * Returns 0 if no space in sockbuf or insufficient mbufs. 922df8bae1dSRodney W. Grimes */ 92326f9a767SRodney W. Grimes int 924a34b7046SRobert Watson sbappendaddr_locked(sb, asa, m0, control) 9257ed60de8SPoul-Henning Kamp struct sockbuf *sb; 926e7dd9a10SRobert Watson const struct sockaddr *asa; 927df8bae1dSRodney W. Grimes struct mbuf *m0, *control; 928df8bae1dSRodney W. Grimes { 929395bb186SSam Leffler struct mbuf *m, *n, *nlast; 930df8bae1dSRodney W. Grimes int space = asa->sa_len; 931df8bae1dSRodney W. Grimes 932a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 933a34b7046SRobert Watson 934df8bae1dSRodney W. Grimes if (m0 && (m0->m_flags & M_PKTHDR) == 0) 935a34b7046SRobert Watson panic("sbappendaddr_locked"); 936df8bae1dSRodney W. Grimes if (m0) 937df8bae1dSRodney W. Grimes space += m0->m_pkthdr.len; 9387ed60de8SPoul-Henning Kamp space += m_length(control, &n); 939a34b7046SRobert Watson 940df8bae1dSRodney W. Grimes if (space > sbspace(sb)) 941df8bae1dSRodney W. Grimes return (0); 942c43cad1aSScott Long #if MSIZE <= 256 943df8bae1dSRodney W. Grimes if (asa->sa_len > MLEN) 944df8bae1dSRodney W. Grimes return (0); 945c43cad1aSScott Long #endif 946a163d034SWarner Losh MGET(m, M_DONTWAIT, MT_SONAME); 947df8bae1dSRodney W. Grimes if (m == 0) 948df8bae1dSRodney W. Grimes return (0); 949df8bae1dSRodney W. Grimes m->m_len = asa->sa_len; 95080208239SAlfred Perlstein bcopy(asa, mtod(m, caddr_t), asa->sa_len); 951df8bae1dSRodney W. Grimes if (n) 952df8bae1dSRodney W. Grimes n->m_next = m0; /* concatenate data to control */ 953df8bae1dSRodney W. Grimes else 954df8bae1dSRodney W. Grimes control = m0; 955df8bae1dSRodney W. Grimes m->m_next = control; 956395bb186SSam Leffler for (n = m; n->m_next != NULL; n = n->m_next) 957df8bae1dSRodney W. Grimes sballoc(sb, n); 958395bb186SSam Leffler sballoc(sb, n); 959395bb186SSam Leffler nlast = n; 960395bb186SSam Leffler SBLINKRECORD(sb, m); 961395bb186SSam Leffler 962395bb186SSam Leffler sb->sb_mbtail = nlast; 963395bb186SSam Leffler SBLASTMBUFCHK(sb); 964395bb186SSam Leffler 965395bb186SSam Leffler SBLASTRECORDCHK(sb); 966df8bae1dSRodney W. Grimes return (1); 967df8bae1dSRodney W. Grimes } 968df8bae1dSRodney W. Grimes 969a34b7046SRobert Watson /* 970a34b7046SRobert Watson * Append address and data, and optionally, control (ancillary) data 971a34b7046SRobert Watson * to the receive queue of a socket. If present, 972a34b7046SRobert Watson * m0 must include a packet header with total length. 973a34b7046SRobert Watson * Returns 0 if no space in sockbuf or insufficient mbufs. 974a34b7046SRobert Watson */ 97526f9a767SRodney W. Grimes int 976a34b7046SRobert Watson sbappendaddr(sb, asa, m0, control) 977a34b7046SRobert Watson struct sockbuf *sb; 978a34b7046SRobert Watson const struct sockaddr *asa; 979a34b7046SRobert Watson struct mbuf *m0, *control; 980a34b7046SRobert Watson { 981a34b7046SRobert Watson int retval; 982a34b7046SRobert Watson 983a34b7046SRobert Watson SOCKBUF_LOCK(sb); 984a34b7046SRobert Watson retval = sbappendaddr_locked(sb, asa, m0, control); 985a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 986a34b7046SRobert Watson return (retval); 987a34b7046SRobert Watson } 988a34b7046SRobert Watson 989a34b7046SRobert Watson int 990a34b7046SRobert Watson sbappendcontrol_locked(sb, m0, control) 991df8bae1dSRodney W. Grimes struct sockbuf *sb; 992df8bae1dSRodney W. Grimes struct mbuf *control, *m0; 993df8bae1dSRodney W. Grimes { 994395bb186SSam Leffler struct mbuf *m, *n, *mlast; 9957ed60de8SPoul-Henning Kamp int space; 996df8bae1dSRodney W. Grimes 997a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 998a34b7046SRobert Watson 999df8bae1dSRodney W. Grimes if (control == 0) 1000a34b7046SRobert Watson panic("sbappendcontrol_locked"); 10017ed60de8SPoul-Henning Kamp space = m_length(control, &n) + m_length(m0, NULL); 1002a34b7046SRobert Watson 1003df8bae1dSRodney W. Grimes if (space > sbspace(sb)) 1004df8bae1dSRodney W. Grimes return (0); 1005df8bae1dSRodney W. Grimes n->m_next = m0; /* concatenate data to control */ 1006395bb186SSam Leffler 1007395bb186SSam Leffler SBLASTRECORDCHK(sb); 1008395bb186SSam Leffler 1009395bb186SSam Leffler for (m = control; m->m_next; m = m->m_next) 1010df8bae1dSRodney W. Grimes sballoc(sb, m); 1011395bb186SSam Leffler sballoc(sb, m); 1012395bb186SSam Leffler mlast = m; 1013395bb186SSam Leffler SBLINKRECORD(sb, control); 1014395bb186SSam Leffler 1015395bb186SSam Leffler sb->sb_mbtail = mlast; 1016395bb186SSam Leffler SBLASTMBUFCHK(sb); 1017395bb186SSam Leffler 1018395bb186SSam Leffler SBLASTRECORDCHK(sb); 1019df8bae1dSRodney W. Grimes return (1); 1020df8bae1dSRodney W. Grimes } 1021df8bae1dSRodney W. Grimes 1022a34b7046SRobert Watson int 1023a34b7046SRobert Watson sbappendcontrol(sb, m0, control) 1024a34b7046SRobert Watson struct sockbuf *sb; 1025a34b7046SRobert Watson struct mbuf *control, *m0; 1026a34b7046SRobert Watson { 1027a34b7046SRobert Watson int retval; 1028a34b7046SRobert Watson 1029a34b7046SRobert Watson SOCKBUF_LOCK(sb); 1030a34b7046SRobert Watson retval = sbappendcontrol_locked(sb, m0, control); 1031a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 1032a34b7046SRobert Watson return (retval); 1033a34b7046SRobert Watson } 1034a34b7046SRobert Watson 1035df8bae1dSRodney W. Grimes /* 1036df8bae1dSRodney W. Grimes * Compress mbuf chain m into the socket 1037df8bae1dSRodney W. Grimes * buffer sb following mbuf n. If n 1038df8bae1dSRodney W. Grimes * is null, the buffer is presumed empty. 1039df8bae1dSRodney W. Grimes */ 104026f9a767SRodney W. Grimes void 1041df8bae1dSRodney W. Grimes sbcompress(sb, m, n) 1042df8bae1dSRodney W. Grimes register struct sockbuf *sb; 1043df8bae1dSRodney W. Grimes register struct mbuf *m, *n; 1044df8bae1dSRodney W. Grimes { 1045df8bae1dSRodney W. Grimes register int eor = 0; 1046df8bae1dSRodney W. Grimes register struct mbuf *o; 1047df8bae1dSRodney W. Grimes 1048a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 1049a34b7046SRobert Watson 1050df8bae1dSRodney W. Grimes while (m) { 1051df8bae1dSRodney W. Grimes eor |= m->m_flags & M_EOR; 1052df8bae1dSRodney W. Grimes if (m->m_len == 0 && 1053df8bae1dSRodney W. Grimes (eor == 0 || 1054df8bae1dSRodney W. Grimes (((o = m->m_next) || (o = n)) && 1055df8bae1dSRodney W. Grimes o->m_type == m->m_type))) { 1056395bb186SSam Leffler if (sb->sb_lastrecord == m) 1057395bb186SSam Leffler sb->sb_lastrecord = m->m_next; 1058df8bae1dSRodney W. Grimes m = m_free(m); 1059df8bae1dSRodney W. Grimes continue; 1060df8bae1dSRodney W. Grimes } 106132af0d74SDavid Malone if (n && (n->m_flags & M_EOR) == 0 && 106232af0d74SDavid Malone M_WRITABLE(n) && 106332af0d74SDavid Malone m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */ 106432af0d74SDavid Malone m->m_len <= M_TRAILINGSPACE(n) && 1065df8bae1dSRodney W. Grimes n->m_type == m->m_type) { 1066df8bae1dSRodney W. Grimes bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, 1067df8bae1dSRodney W. Grimes (unsigned)m->m_len); 1068df8bae1dSRodney W. Grimes n->m_len += m->m_len; 1069df8bae1dSRodney W. Grimes sb->sb_cc += m->m_len; 1070b3f1af6bSTim J. Robbins if (m->m_type != MT_DATA && m->m_type != MT_HEADER && 1071b3f1af6bSTim J. Robbins m->m_type != MT_OOBDATA) 1072b3f1af6bSTim J. Robbins /* XXX: Probably don't need.*/ 107304ac9b97SKelly Yancey sb->sb_ctl += m->m_len; 1074df8bae1dSRodney W. Grimes m = m_free(m); 1075df8bae1dSRodney W. Grimes continue; 1076df8bae1dSRodney W. Grimes } 1077df8bae1dSRodney W. Grimes if (n) 1078df8bae1dSRodney W. Grimes n->m_next = m; 1079df8bae1dSRodney W. Grimes else 1080df8bae1dSRodney W. Grimes sb->sb_mb = m; 1081395bb186SSam Leffler sb->sb_mbtail = m; 1082df8bae1dSRodney W. Grimes sballoc(sb, m); 1083df8bae1dSRodney W. Grimes n = m; 1084df8bae1dSRodney W. Grimes m->m_flags &= ~M_EOR; 1085df8bae1dSRodney W. Grimes m = m->m_next; 1086df8bae1dSRodney W. Grimes n->m_next = 0; 1087df8bae1dSRodney W. Grimes } 1088df8bae1dSRodney W. Grimes if (eor) { 1089df8bae1dSRodney W. Grimes if (n) 1090df8bae1dSRodney W. Grimes n->m_flags |= eor; 1091df8bae1dSRodney W. Grimes else 1092df8bae1dSRodney W. Grimes printf("semi-panic: sbcompress\n"); 1093df8bae1dSRodney W. Grimes } 1094395bb186SSam Leffler SBLASTMBUFCHK(sb); 1095df8bae1dSRodney W. Grimes } 1096df8bae1dSRodney W. Grimes 1097df8bae1dSRodney W. Grimes /* 1098df8bae1dSRodney W. Grimes * Free all mbufs in a sockbuf. 1099df8bae1dSRodney W. Grimes * Check that all resources are reclaimed. 1100df8bae1dSRodney W. Grimes */ 110126f9a767SRodney W. Grimes void 1102a34b7046SRobert Watson sbflush_locked(sb) 1103df8bae1dSRodney W. Grimes register struct sockbuf *sb; 1104df8bae1dSRodney W. Grimes { 1105df8bae1dSRodney W. Grimes 1106a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 1107a34b7046SRobert Watson 1108df8bae1dSRodney W. Grimes if (sb->sb_flags & SB_LOCK) 1109a34b7046SRobert Watson panic("sbflush_locked: locked"); 111023f84772SPierre Beyssac while (sb->sb_mbcnt) { 111123f84772SPierre Beyssac /* 111223f84772SPierre Beyssac * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty: 111323f84772SPierre Beyssac * we would loop forever. Panic instead. 111423f84772SPierre Beyssac */ 111523f84772SPierre Beyssac if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len)) 111623f84772SPierre Beyssac break; 1117a34b7046SRobert Watson sbdrop_locked(sb, (int)sb->sb_cc); 111823f84772SPierre Beyssac } 11190931333fSBill Fenner if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt) 1120a34b7046SRobert Watson panic("sbflush_locked: cc %u || mb %p || mbcnt %u", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt); 1121a34b7046SRobert Watson } 1122a34b7046SRobert Watson 1123a34b7046SRobert Watson void 1124a34b7046SRobert Watson sbflush(sb) 1125a34b7046SRobert Watson register struct sockbuf *sb; 1126a34b7046SRobert Watson { 1127a34b7046SRobert Watson 1128a34b7046SRobert Watson SOCKBUF_LOCK(sb); 1129a34b7046SRobert Watson sbflush_locked(sb); 1130a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 1131df8bae1dSRodney W. Grimes } 1132df8bae1dSRodney W. Grimes 1133df8bae1dSRodney W. Grimes /* 1134df8bae1dSRodney W. Grimes * Drop data from (the front of) a sockbuf. 1135df8bae1dSRodney W. Grimes */ 113626f9a767SRodney W. Grimes void 1137a34b7046SRobert Watson sbdrop_locked(sb, len) 1138df8bae1dSRodney W. Grimes register struct sockbuf *sb; 1139df8bae1dSRodney W. Grimes register int len; 1140df8bae1dSRodney W. Grimes { 1141ecde8f7cSMatthew Dillon register struct mbuf *m; 1142df8bae1dSRodney W. Grimes struct mbuf *next; 1143df8bae1dSRodney W. Grimes 1144a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 1145a34b7046SRobert Watson 1146df8bae1dSRodney W. Grimes next = (m = sb->sb_mb) ? m->m_nextpkt : 0; 1147df8bae1dSRodney W. Grimes while (len > 0) { 1148df8bae1dSRodney W. Grimes if (m == 0) { 1149df8bae1dSRodney W. Grimes if (next == 0) 1150df8bae1dSRodney W. Grimes panic("sbdrop"); 1151df8bae1dSRodney W. Grimes m = next; 1152df8bae1dSRodney W. Grimes next = m->m_nextpkt; 1153df8bae1dSRodney W. Grimes continue; 1154df8bae1dSRodney W. Grimes } 1155df8bae1dSRodney W. Grimes if (m->m_len > len) { 1156df8bae1dSRodney W. Grimes m->m_len -= len; 1157df8bae1dSRodney W. Grimes m->m_data += len; 1158df8bae1dSRodney W. Grimes sb->sb_cc -= len; 1159b3f1af6bSTim J. Robbins if (m->m_type != MT_DATA && m->m_type != MT_HEADER && 1160b3f1af6bSTim J. Robbins m->m_type != MT_OOBDATA) 116104ac9b97SKelly Yancey sb->sb_ctl -= len; 1162df8bae1dSRodney W. Grimes break; 1163df8bae1dSRodney W. Grimes } 1164df8bae1dSRodney W. Grimes len -= m->m_len; 1165df8bae1dSRodney W. Grimes sbfree(sb, m); 1166ecde8f7cSMatthew Dillon m = m_free(m); 1167df8bae1dSRodney W. Grimes } 1168df8bae1dSRodney W. Grimes while (m && m->m_len == 0) { 1169df8bae1dSRodney W. Grimes sbfree(sb, m); 1170ecde8f7cSMatthew Dillon m = m_free(m); 1171df8bae1dSRodney W. Grimes } 1172df8bae1dSRodney W. Grimes if (m) { 1173df8bae1dSRodney W. Grimes sb->sb_mb = m; 1174df8bae1dSRodney W. Grimes m->m_nextpkt = next; 1175df8bae1dSRodney W. Grimes } else 1176df8bae1dSRodney W. Grimes sb->sb_mb = next; 1177395bb186SSam Leffler /* 1178395bb186SSam Leffler * First part is an inline SB_EMPTY_FIXUP(). Second part 1179395bb186SSam Leffler * makes sure sb_lastrecord is up-to-date if we dropped 1180395bb186SSam Leffler * part of the last record. 1181395bb186SSam Leffler */ 1182395bb186SSam Leffler m = sb->sb_mb; 1183395bb186SSam Leffler if (m == NULL) { 1184395bb186SSam Leffler sb->sb_mbtail = NULL; 1185395bb186SSam Leffler sb->sb_lastrecord = NULL; 1186395bb186SSam Leffler } else if (m->m_nextpkt == NULL) { 1187395bb186SSam Leffler sb->sb_lastrecord = m; 1188395bb186SSam Leffler } 1189df8bae1dSRodney W. Grimes } 1190df8bae1dSRodney W. Grimes 1191df8bae1dSRodney W. Grimes /* 1192a34b7046SRobert Watson * Drop data from (the front of) a sockbuf. 1193a34b7046SRobert Watson */ 1194a34b7046SRobert Watson void 1195a34b7046SRobert Watson sbdrop(sb, len) 1196a34b7046SRobert Watson register struct sockbuf *sb; 1197a34b7046SRobert Watson register int len; 1198a34b7046SRobert Watson { 1199a34b7046SRobert Watson 1200a34b7046SRobert Watson SOCKBUF_LOCK(sb); 1201a34b7046SRobert Watson sbdrop_locked(sb, len); 1202a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 1203a34b7046SRobert Watson } 1204a34b7046SRobert Watson 1205a34b7046SRobert Watson /* 1206df8bae1dSRodney W. Grimes * Drop a record off the front of a sockbuf 1207df8bae1dSRodney W. Grimes * and move the next record to the front. 1208df8bae1dSRodney W. Grimes */ 120926f9a767SRodney W. Grimes void 1210a34b7046SRobert Watson sbdroprecord_locked(sb) 1211df8bae1dSRodney W. Grimes register struct sockbuf *sb; 1212df8bae1dSRodney W. Grimes { 1213ecde8f7cSMatthew Dillon register struct mbuf *m; 1214df8bae1dSRodney W. Grimes 1215a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 1216a34b7046SRobert Watson 1217df8bae1dSRodney W. Grimes m = sb->sb_mb; 1218df8bae1dSRodney W. Grimes if (m) { 1219df8bae1dSRodney W. Grimes sb->sb_mb = m->m_nextpkt; 1220df8bae1dSRodney W. Grimes do { 1221df8bae1dSRodney W. Grimes sbfree(sb, m); 1222ecde8f7cSMatthew Dillon m = m_free(m); 1223797f2d22SPoul-Henning Kamp } while (m); 1224df8bae1dSRodney W. Grimes } 1225395bb186SSam Leffler SB_EMPTY_FIXUP(sb); 1226df8bae1dSRodney W. Grimes } 12271e4ad9ceSGarrett Wollman 122882c23ebaSBill Fenner /* 1229a34b7046SRobert Watson * Drop a record off the front of a sockbuf 1230a34b7046SRobert Watson * and move the next record to the front. 1231a34b7046SRobert Watson */ 1232a34b7046SRobert Watson void 1233a34b7046SRobert Watson sbdroprecord(sb) 1234a34b7046SRobert Watson register struct sockbuf *sb; 1235a34b7046SRobert Watson { 1236a34b7046SRobert Watson 1237a34b7046SRobert Watson SOCKBUF_LOCK(sb); 1238a34b7046SRobert Watson sbdroprecord_locked(sb); 1239a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 1240a34b7046SRobert Watson } 1241a34b7046SRobert Watson 1242a34b7046SRobert Watson /* 124382c23ebaSBill Fenner * Create a "control" mbuf containing the specified data 124482c23ebaSBill Fenner * with the specified type for presentation on a socket buffer. 124582c23ebaSBill Fenner */ 124682c23ebaSBill Fenner struct mbuf * 124782c23ebaSBill Fenner sbcreatecontrol(p, size, type, level) 124882c23ebaSBill Fenner caddr_t p; 124982c23ebaSBill Fenner register int size; 125082c23ebaSBill Fenner int type, level; 125182c23ebaSBill Fenner { 125282c23ebaSBill Fenner register struct cmsghdr *cp; 125382c23ebaSBill Fenner struct mbuf *m; 125482c23ebaSBill Fenner 125559bdd405SDavid Malone if (CMSG_SPACE((u_int)size) > MCLBYTES) 12560b97e97cSYoshinobu Inoue return ((struct mbuf *) NULL); 1257099a0e58SBosko Milekic if (CMSG_SPACE((u_int)size > MLEN)) 1258099a0e58SBosko Milekic m = m_getcl(M_DONTWAIT, MT_CONTROL, 0); 1259099a0e58SBosko Milekic else 1260099a0e58SBosko Milekic m = m_get(M_DONTWAIT, MT_CONTROL); 1261099a0e58SBosko Milekic if (m == NULL) 126282c23ebaSBill Fenner return ((struct mbuf *) NULL); 126382c23ebaSBill Fenner cp = mtod(m, struct cmsghdr *); 126459bdd405SDavid Malone m->m_len = 0; 126559bdd405SDavid Malone KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m), 126659bdd405SDavid Malone ("sbcreatecontrol: short mbuf")); 126759bdd405SDavid Malone if (p != NULL) 126882c23ebaSBill Fenner (void)memcpy(CMSG_DATA(cp), p, size); 12697d0d8dc3SYoshinobu Inoue m->m_len = CMSG_SPACE(size); 12707d0d8dc3SYoshinobu Inoue cp->cmsg_len = CMSG_LEN(size); 127182c23ebaSBill Fenner cp->cmsg_level = level; 127282c23ebaSBill Fenner cp->cmsg_type = type; 127382c23ebaSBill Fenner return (m); 127482c23ebaSBill Fenner } 127582c23ebaSBill Fenner 12761e4ad9ceSGarrett Wollman /* 12772c37256eSGarrett Wollman * Some routines that return EOPNOTSUPP for entry points that are not 12782c37256eSGarrett Wollman * supported by a protocol. Fill in as needed. 12791e4ad9ceSGarrett Wollman */ 12801e4ad9ceSGarrett Wollman int 128157bf258eSGarrett Wollman pru_accept_notsupp(struct socket *so, struct sockaddr **nam) 1282d8392c6cSGarrett Wollman { 1283d8392c6cSGarrett Wollman return EOPNOTSUPP; 1284d8392c6cSGarrett Wollman } 1285d8392c6cSGarrett Wollman 1286d8392c6cSGarrett Wollman int 1287b40ce416SJulian Elischer pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td) 12889f907986SPeter Wemm { 12899f907986SPeter Wemm return EOPNOTSUPP; 12909f907986SPeter Wemm } 12919f907986SPeter Wemm 12929f907986SPeter Wemm int 12932c37256eSGarrett Wollman pru_connect2_notsupp(struct socket *so1, struct socket *so2) 12941e4ad9ceSGarrett Wollman { 12952c37256eSGarrett Wollman return EOPNOTSUPP; 12961e4ad9ceSGarrett Wollman } 1297d8392c6cSGarrett Wollman 1298d8392c6cSGarrett Wollman int 1299ecbb00a2SDoug Rabson pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data, 1300b40ce416SJulian Elischer struct ifnet *ifp, struct thread *td) 1301a29f300eSGarrett Wollman { 1302a29f300eSGarrett Wollman return EOPNOTSUPP; 1303a29f300eSGarrett Wollman } 1304a29f300eSGarrett Wollman 1305a29f300eSGarrett Wollman int 1306b40ce416SJulian Elischer pru_listen_notsupp(struct socket *so, struct thread *td) 1307d8392c6cSGarrett Wollman { 1308d8392c6cSGarrett Wollman return EOPNOTSUPP; 1309d8392c6cSGarrett Wollman } 1310d8392c6cSGarrett Wollman 1311d8392c6cSGarrett Wollman int 1312d8392c6cSGarrett Wollman pru_rcvd_notsupp(struct socket *so, int flags) 1313d8392c6cSGarrett Wollman { 1314d8392c6cSGarrett Wollman return EOPNOTSUPP; 1315d8392c6cSGarrett Wollman } 1316d8392c6cSGarrett Wollman 1317d8392c6cSGarrett Wollman int 1318d8392c6cSGarrett Wollman pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags) 1319d8392c6cSGarrett Wollman { 1320d8392c6cSGarrett Wollman return EOPNOTSUPP; 1321d8392c6cSGarrett Wollman } 1322d8392c6cSGarrett Wollman 1323d8392c6cSGarrett Wollman /* 1324d8392c6cSGarrett Wollman * This isn't really a ``null'' operation, but it's the default one 1325d8392c6cSGarrett Wollman * and doesn't do anything destructive. 1326d8392c6cSGarrett Wollman */ 1327d8392c6cSGarrett Wollman int 1328d8392c6cSGarrett Wollman pru_sense_null(struct socket *so, struct stat *sb) 1329d8392c6cSGarrett Wollman { 1330d8392c6cSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 1331d8392c6cSGarrett Wollman return 0; 1332d8392c6cSGarrett Wollman } 1333639acc13SGarrett Wollman 1334639acc13SGarrett Wollman /* 1335a557af22SRobert Watson * For protocol types that don't keep cached copies of labels in their 1336a557af22SRobert Watson * pcbs, provide a null sosetlabel that does a NOOP. 1337a557af22SRobert Watson */ 1338a557af22SRobert Watson void 1339a557af22SRobert Watson pru_sosetlabel_null(struct socket *so) 1340a557af22SRobert Watson { 1341a557af22SRobert Watson 1342a557af22SRobert Watson } 1343a557af22SRobert Watson 1344a557af22SRobert Watson /* 134557bf258eSGarrett Wollman * Make a copy of a sockaddr in a malloced buffer of type M_SONAME. 134657bf258eSGarrett Wollman */ 134757bf258eSGarrett Wollman struct sockaddr * 1348746e5bf0SRobert Watson sodupsockaddr(const struct sockaddr *sa, int mflags) 134957bf258eSGarrett Wollman { 135057bf258eSGarrett Wollman struct sockaddr *sa2; 135157bf258eSGarrett Wollman 1352746e5bf0SRobert Watson sa2 = malloc(sa->sa_len, M_SONAME, mflags); 135357bf258eSGarrett Wollman if (sa2) 135457bf258eSGarrett Wollman bcopy(sa, sa2, sa->sa_len); 135557bf258eSGarrett Wollman return sa2; 135657bf258eSGarrett Wollman } 135757bf258eSGarrett Wollman 135857bf258eSGarrett Wollman /* 135998271db4SGarrett Wollman * Create an external-format (``xsocket'') structure using the information 136098271db4SGarrett Wollman * in the kernel-format socket structure pointed to by so. This is done 136198271db4SGarrett Wollman * to reduce the spew of irrelevant information over this interface, 136298271db4SGarrett Wollman * to isolate user code from changes in the kernel structure, and 136398271db4SGarrett Wollman * potentially to provide information-hiding if we decide that 136498271db4SGarrett Wollman * some of this information should be hidden from users. 136598271db4SGarrett Wollman */ 136698271db4SGarrett Wollman void 136798271db4SGarrett Wollman sotoxsocket(struct socket *so, struct xsocket *xso) 136898271db4SGarrett Wollman { 136998271db4SGarrett Wollman xso->xso_len = sizeof *xso; 137098271db4SGarrett Wollman xso->xso_so = so; 137198271db4SGarrett Wollman xso->so_type = so->so_type; 137298271db4SGarrett Wollman xso->so_options = so->so_options; 137398271db4SGarrett Wollman xso->so_linger = so->so_linger; 137498271db4SGarrett Wollman xso->so_state = so->so_state; 137598271db4SGarrett Wollman xso->so_pcb = so->so_pcb; 137698271db4SGarrett Wollman xso->xso_protocol = so->so_proto->pr_protocol; 137798271db4SGarrett Wollman xso->xso_family = so->so_proto->pr_domain->dom_family; 137898271db4SGarrett Wollman xso->so_qlen = so->so_qlen; 137998271db4SGarrett Wollman xso->so_incqlen = so->so_incqlen; 138098271db4SGarrett Wollman xso->so_qlimit = so->so_qlimit; 138198271db4SGarrett Wollman xso->so_timeo = so->so_timeo; 138298271db4SGarrett Wollman xso->so_error = so->so_error; 1383831d27a9SDon Lewis xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0; 138498271db4SGarrett Wollman xso->so_oobmark = so->so_oobmark; 138598271db4SGarrett Wollman sbtoxsockbuf(&so->so_snd, &xso->so_snd); 138698271db4SGarrett Wollman sbtoxsockbuf(&so->so_rcv, &xso->so_rcv); 13872f9a2132SBrian Feldman xso->so_uid = so->so_cred->cr_uid; 138898271db4SGarrett Wollman } 138998271db4SGarrett Wollman 139098271db4SGarrett Wollman /* 139198271db4SGarrett Wollman * This does the same for sockbufs. Note that the xsockbuf structure, 139298271db4SGarrett Wollman * since it is always embedded in a socket, does not include a self 139398271db4SGarrett Wollman * pointer nor a length. We make this entry point public in case 139498271db4SGarrett Wollman * some other mechanism needs it. 139598271db4SGarrett Wollman */ 139698271db4SGarrett Wollman void 139798271db4SGarrett Wollman sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb) 139898271db4SGarrett Wollman { 139998271db4SGarrett Wollman xsb->sb_cc = sb->sb_cc; 140098271db4SGarrett Wollman xsb->sb_hiwat = sb->sb_hiwat; 140198271db4SGarrett Wollman xsb->sb_mbcnt = sb->sb_mbcnt; 140298271db4SGarrett Wollman xsb->sb_mbmax = sb->sb_mbmax; 140398271db4SGarrett Wollman xsb->sb_lowat = sb->sb_lowat; 140498271db4SGarrett Wollman xsb->sb_flags = sb->sb_flags; 140598271db4SGarrett Wollman xsb->sb_timeo = sb->sb_timeo; 140698271db4SGarrett Wollman } 140798271db4SGarrett Wollman 140898271db4SGarrett Wollman /* 1409639acc13SGarrett Wollman * Here is the definition of some of the basic objects in the kern.ipc 1410639acc13SGarrett Wollman * branch of the MIB. 1411639acc13SGarrett Wollman */ 1412639acc13SGarrett Wollman SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC"); 1413639acc13SGarrett Wollman 1414639acc13SGarrett Wollman /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */ 1415639acc13SGarrett Wollman static int dummy; 1416639acc13SGarrett Wollman SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, ""); 14171b978d45SHartmut Brandt SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_ULONG|CTLFLAG_RW, 14181b978d45SHartmut Brandt &sb_max, 0, sysctl_handle_sb_max, "LU", "Maximum socket buffer size"); 1419184dcdc7SMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RDTUN, 142054d77689SJeff Roberson &maxsockets, 0, "Maximum number of sockets avaliable"); 14211b978d45SHartmut Brandt SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW, 1422639acc13SGarrett Wollman &sb_efficiency, 0, ""); 142354d77689SJeff Roberson 142454d77689SJeff Roberson /* 142554d77689SJeff Roberson * Initialise maxsockets 142654d77689SJeff Roberson */ 142754d77689SJeff Roberson static void init_maxsockets(void *ignored) 142854d77689SJeff Roberson { 142954d77689SJeff Roberson TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets); 143054d77689SJeff Roberson maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters)); 143154d77689SJeff Roberson } 143254d77689SJeff Roberson SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL); 1433