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 * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33df8bae1dSRodney W. Grimes * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93 34ff5c09daSGarrett Wollman * $Id: uipc_socket2.c,v 1.5 1995/05/30 08:06:22 rgrimes Exp $ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37df8bae1dSRodney W. Grimes #include <sys/param.h> 38df8bae1dSRodney W. Grimes #include <sys/systm.h> 39ff5c09daSGarrett Wollman #include <sys/kernel.h> 40df8bae1dSRodney W. Grimes #include <sys/proc.h> 41df8bae1dSRodney W. Grimes #include <sys/file.h> 42df8bae1dSRodney W. Grimes #include <sys/buf.h> 43df8bae1dSRodney W. Grimes #include <sys/malloc.h> 44df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 45df8bae1dSRodney W. Grimes #include <sys/protosw.h> 46797f2d22SPoul-Henning Kamp #include <sys/stat.h> 47df8bae1dSRodney W. Grimes #include <sys/socket.h> 48df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 49797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 50ff5c09daSGarrett Wollman #include <sys/sysctl.h> 5126f9a767SRodney W. Grimes 52df8bae1dSRodney W. Grimes /* 53df8bae1dSRodney W. Grimes * Primitive routines for operating on sockets and socket buffers 54df8bae1dSRodney W. Grimes */ 55df8bae1dSRodney W. Grimes 56df8bae1dSRodney W. Grimes /* strings for sleep message: */ 57df8bae1dSRodney W. Grimes char netio[] = "netio"; 58df8bae1dSRodney W. Grimes char netcon[] = "netcon"; 59df8bae1dSRodney W. Grimes char netcls[] = "netcls"; 60df8bae1dSRodney W. Grimes 61ff5c09daSGarrett Wollman u_long sb_max = SB_MAX; /* XXX should be static */ 62ff5c09daSGarrett Wollman SYSCTL_INT(_kern, KERN_MAXSOCKBUF, maxsockbuf, CTLFLAG_RW, &sb_max, 0, "") 63df8bae1dSRodney W. Grimes 64df8bae1dSRodney W. Grimes /* 65df8bae1dSRodney W. Grimes * Procedures to manipulate state flags of socket 66df8bae1dSRodney W. Grimes * and do appropriate wakeups. Normal sequence from the 67df8bae1dSRodney W. Grimes * active (originating) side is that soisconnecting() is 68df8bae1dSRodney W. Grimes * called during processing of connect() call, 69df8bae1dSRodney W. Grimes * resulting in an eventual call to soisconnected() if/when the 70df8bae1dSRodney W. Grimes * connection is established. When the connection is torn down 71df8bae1dSRodney W. Grimes * soisdisconnecting() is called during processing of disconnect() call, 72df8bae1dSRodney W. Grimes * and soisdisconnected() is called when the connection to the peer 73df8bae1dSRodney W. Grimes * is totally severed. The semantics of these routines are such that 74df8bae1dSRodney W. Grimes * connectionless protocols can call soisconnected() and soisdisconnected() 75df8bae1dSRodney W. Grimes * only, bypassing the in-progress calls when setting up a ``connection'' 76df8bae1dSRodney W. Grimes * takes no time. 77df8bae1dSRodney W. Grimes * 78df8bae1dSRodney W. Grimes * From the passive side, a socket is created with 79df8bae1dSRodney W. Grimes * two queues of sockets: so_q0 for connections in progress 80df8bae1dSRodney W. Grimes * and so_q for connections already made and awaiting user acceptance. 81df8bae1dSRodney W. Grimes * As a protocol is preparing incoming connections, it creates a socket 82df8bae1dSRodney W. Grimes * structure queued on so_q0 by calling sonewconn(). When the connection 83df8bae1dSRodney W. Grimes * is established, soisconnected() is called, and transfers the 84df8bae1dSRodney W. Grimes * socket structure to so_q, making it available to accept(). 85df8bae1dSRodney W. Grimes * 86df8bae1dSRodney W. Grimes * If a socket is closed with sockets on either 87df8bae1dSRodney W. Grimes * so_q0 or so_q, these sockets are dropped. 88df8bae1dSRodney W. Grimes * 89df8bae1dSRodney W. Grimes * If higher level protocols are implemented in 90df8bae1dSRodney W. Grimes * the kernel, the wakeups done here will sometimes 91df8bae1dSRodney W. Grimes * cause software-interrupt process scheduling. 92df8bae1dSRodney W. Grimes */ 93df8bae1dSRodney W. Grimes 9426f9a767SRodney W. Grimes void 95df8bae1dSRodney W. Grimes soisconnecting(so) 96df8bae1dSRodney W. Grimes register struct socket *so; 97df8bae1dSRodney W. Grimes { 98df8bae1dSRodney W. Grimes 99df8bae1dSRodney W. Grimes so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); 100df8bae1dSRodney W. Grimes so->so_state |= SS_ISCONNECTING; 101df8bae1dSRodney W. Grimes } 102df8bae1dSRodney W. Grimes 10326f9a767SRodney W. Grimes void 104df8bae1dSRodney W. Grimes soisconnected(so) 105df8bae1dSRodney W. Grimes register struct socket *so; 106df8bae1dSRodney W. Grimes { 107df8bae1dSRodney W. Grimes register struct socket *head = so->so_head; 108df8bae1dSRodney W. Grimes 109df8bae1dSRodney W. Grimes so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); 110df8bae1dSRodney W. Grimes so->so_state |= SS_ISCONNECTED; 111df8bae1dSRodney W. Grimes if (head && soqremque(so, 0)) { 112df8bae1dSRodney W. Grimes soqinsque(head, so, 1); 113df8bae1dSRodney W. Grimes sorwakeup(head); 114df8bae1dSRodney W. Grimes wakeup((caddr_t)&head->so_timeo); 115df8bae1dSRodney W. Grimes } else { 116df8bae1dSRodney W. Grimes wakeup((caddr_t)&so->so_timeo); 117df8bae1dSRodney W. Grimes sorwakeup(so); 118df8bae1dSRodney W. Grimes sowwakeup(so); 119df8bae1dSRodney W. Grimes } 120df8bae1dSRodney W. Grimes } 121df8bae1dSRodney W. Grimes 12226f9a767SRodney W. Grimes void 123df8bae1dSRodney W. Grimes soisdisconnecting(so) 124df8bae1dSRodney W. Grimes register struct socket *so; 125df8bae1dSRodney W. Grimes { 126df8bae1dSRodney W. Grimes 127df8bae1dSRodney W. Grimes so->so_state &= ~SS_ISCONNECTING; 128df8bae1dSRodney W. Grimes so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE); 129df8bae1dSRodney W. Grimes wakeup((caddr_t)&so->so_timeo); 130df8bae1dSRodney W. Grimes sowwakeup(so); 131df8bae1dSRodney W. Grimes sorwakeup(so); 132df8bae1dSRodney W. Grimes } 133df8bae1dSRodney W. Grimes 13426f9a767SRodney W. Grimes void 135df8bae1dSRodney W. Grimes soisdisconnected(so) 136df8bae1dSRodney W. Grimes register struct socket *so; 137df8bae1dSRodney W. Grimes { 138df8bae1dSRodney W. Grimes 139df8bae1dSRodney W. Grimes so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); 140df8bae1dSRodney W. Grimes so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE); 141df8bae1dSRodney W. Grimes wakeup((caddr_t)&so->so_timeo); 142df8bae1dSRodney W. Grimes sowwakeup(so); 143df8bae1dSRodney W. Grimes sorwakeup(so); 144df8bae1dSRodney W. Grimes } 145df8bae1dSRodney W. Grimes 146df8bae1dSRodney W. Grimes /* 147df8bae1dSRodney W. Grimes * When an attempt at a new connection is noted on a socket 148df8bae1dSRodney W. Grimes * which accepts connections, sonewconn is called. If the 149df8bae1dSRodney W. Grimes * connection is possible (subject to space constraints, etc.) 150df8bae1dSRodney W. Grimes * then we allocate a new structure, propoerly linked into the 151df8bae1dSRodney W. Grimes * data structure of the original socket, and return this. 152df8bae1dSRodney W. Grimes * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED. 153df8bae1dSRodney W. Grimes * 154df8bae1dSRodney W. Grimes * Currently, sonewconn() is defined as sonewconn1() in socketvar.h 155df8bae1dSRodney W. Grimes * to catch calls that are missing the (new) second parameter. 156df8bae1dSRodney W. Grimes */ 157df8bae1dSRodney W. Grimes struct socket * 158df8bae1dSRodney W. Grimes sonewconn1(head, connstatus) 159df8bae1dSRodney W. Grimes register struct socket *head; 160df8bae1dSRodney W. Grimes int connstatus; 161df8bae1dSRodney W. Grimes { 162df8bae1dSRodney W. Grimes register struct socket *so; 163df8bae1dSRodney W. Grimes int soqueue = connstatus ? 1 : 0; 164df8bae1dSRodney W. Grimes 165df8bae1dSRodney W. Grimes if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2) 166df8bae1dSRodney W. Grimes return ((struct socket *)0); 167df8bae1dSRodney W. Grimes MALLOC(so, struct socket *, sizeof(*so), M_SOCKET, M_DONTWAIT); 168df8bae1dSRodney W. Grimes if (so == NULL) 169df8bae1dSRodney W. Grimes return ((struct socket *)0); 170df8bae1dSRodney W. Grimes bzero((caddr_t)so, sizeof(*so)); 171df8bae1dSRodney W. Grimes so->so_type = head->so_type; 172df8bae1dSRodney W. Grimes so->so_options = head->so_options &~ SO_ACCEPTCONN; 173df8bae1dSRodney W. Grimes so->so_linger = head->so_linger; 174df8bae1dSRodney W. Grimes so->so_state = head->so_state | SS_NOFDREF; 175df8bae1dSRodney W. Grimes so->so_proto = head->so_proto; 176df8bae1dSRodney W. Grimes so->so_timeo = head->so_timeo; 177df8bae1dSRodney W. Grimes so->so_pgid = head->so_pgid; 178df8bae1dSRodney W. Grimes (void) soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat); 179df8bae1dSRodney W. Grimes soqinsque(head, so, soqueue); 180df8bae1dSRodney W. Grimes if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH, 181df8bae1dSRodney W. Grimes (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)) { 182df8bae1dSRodney W. Grimes (void) soqremque(so, soqueue); 183df8bae1dSRodney W. Grimes (void) free((caddr_t)so, M_SOCKET); 184df8bae1dSRodney W. Grimes return ((struct socket *)0); 185df8bae1dSRodney W. Grimes } 186df8bae1dSRodney W. Grimes if (connstatus) { 187df8bae1dSRodney W. Grimes sorwakeup(head); 188df8bae1dSRodney W. Grimes wakeup((caddr_t)&head->so_timeo); 189df8bae1dSRodney W. Grimes so->so_state |= connstatus; 190df8bae1dSRodney W. Grimes } 191df8bae1dSRodney W. Grimes return (so); 192df8bae1dSRodney W. Grimes } 193df8bae1dSRodney W. Grimes 19426f9a767SRodney W. Grimes void 195df8bae1dSRodney W. Grimes soqinsque(head, so, q) 196df8bae1dSRodney W. Grimes register struct socket *head, *so; 197df8bae1dSRodney W. Grimes int q; 198df8bae1dSRodney W. Grimes { 199df8bae1dSRodney W. Grimes 200df8bae1dSRodney W. Grimes register struct socket **prev; 201df8bae1dSRodney W. Grimes so->so_head = head; 202df8bae1dSRodney W. Grimes if (q == 0) { 203df8bae1dSRodney W. Grimes head->so_q0len++; 204df8bae1dSRodney W. Grimes so->so_q0 = 0; 205df8bae1dSRodney W. Grimes for (prev = &(head->so_q0); *prev; ) 206df8bae1dSRodney W. Grimes prev = &((*prev)->so_q0); 207df8bae1dSRodney W. Grimes } else { 208df8bae1dSRodney W. Grimes head->so_qlen++; 209df8bae1dSRodney W. Grimes so->so_q = 0; 210df8bae1dSRodney W. Grimes for (prev = &(head->so_q); *prev; ) 211df8bae1dSRodney W. Grimes prev = &((*prev)->so_q); 212df8bae1dSRodney W. Grimes } 213df8bae1dSRodney W. Grimes *prev = so; 214df8bae1dSRodney W. Grimes } 215df8bae1dSRodney W. Grimes 21626f9a767SRodney W. Grimes int 217df8bae1dSRodney W. Grimes soqremque(so, q) 218df8bae1dSRodney W. Grimes register struct socket *so; 219df8bae1dSRodney W. Grimes int q; 220df8bae1dSRodney W. Grimes { 221df8bae1dSRodney W. Grimes register struct socket *head, *prev, *next; 222df8bae1dSRodney W. Grimes 223df8bae1dSRodney W. Grimes head = so->so_head; 224df8bae1dSRodney W. Grimes prev = head; 225df8bae1dSRodney W. Grimes for (;;) { 226df8bae1dSRodney W. Grimes next = q ? prev->so_q : prev->so_q0; 227df8bae1dSRodney W. Grimes if (next == so) 228df8bae1dSRodney W. Grimes break; 229df8bae1dSRodney W. Grimes if (next == 0) 230df8bae1dSRodney W. Grimes return (0); 231df8bae1dSRodney W. Grimes prev = next; 232df8bae1dSRodney W. Grimes } 233df8bae1dSRodney W. Grimes if (q == 0) { 234df8bae1dSRodney W. Grimes prev->so_q0 = next->so_q0; 235df8bae1dSRodney W. Grimes head->so_q0len--; 236df8bae1dSRodney W. Grimes } else { 237df8bae1dSRodney W. Grimes prev->so_q = next->so_q; 238df8bae1dSRodney W. Grimes head->so_qlen--; 239df8bae1dSRodney W. Grimes } 240df8bae1dSRodney W. Grimes next->so_q0 = next->so_q = 0; 241df8bae1dSRodney W. Grimes next->so_head = 0; 242df8bae1dSRodney W. Grimes return (1); 243df8bae1dSRodney W. Grimes } 244df8bae1dSRodney W. Grimes 245df8bae1dSRodney W. Grimes /* 246df8bae1dSRodney W. Grimes * Socantsendmore indicates that no more data will be sent on the 247df8bae1dSRodney W. Grimes * socket; it would normally be applied to a socket when the user 248df8bae1dSRodney W. Grimes * informs the system that no more data is to be sent, by the protocol 249df8bae1dSRodney W. Grimes * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data 250df8bae1dSRodney W. Grimes * will be received, and will normally be applied to the socket by a 251df8bae1dSRodney W. Grimes * protocol when it detects that the peer will send no more data. 252df8bae1dSRodney W. Grimes * Data queued for reading in the socket may yet be read. 253df8bae1dSRodney W. Grimes */ 254df8bae1dSRodney W. Grimes 25526f9a767SRodney W. Grimes void 256df8bae1dSRodney W. Grimes socantsendmore(so) 257df8bae1dSRodney W. Grimes struct socket *so; 258df8bae1dSRodney W. Grimes { 259df8bae1dSRodney W. Grimes 260df8bae1dSRodney W. Grimes so->so_state |= SS_CANTSENDMORE; 261df8bae1dSRodney W. Grimes sowwakeup(so); 262df8bae1dSRodney W. Grimes } 263df8bae1dSRodney W. Grimes 26426f9a767SRodney W. Grimes void 265df8bae1dSRodney W. Grimes socantrcvmore(so) 266df8bae1dSRodney W. Grimes struct socket *so; 267df8bae1dSRodney W. Grimes { 268df8bae1dSRodney W. Grimes 269df8bae1dSRodney W. Grimes so->so_state |= SS_CANTRCVMORE; 270df8bae1dSRodney W. Grimes sorwakeup(so); 271df8bae1dSRodney W. Grimes } 272df8bae1dSRodney W. Grimes 273df8bae1dSRodney W. Grimes /* 274df8bae1dSRodney W. Grimes * Wait for data to arrive at/drain from a socket buffer. 275df8bae1dSRodney W. Grimes */ 27626f9a767SRodney W. Grimes int 277df8bae1dSRodney W. Grimes sbwait(sb) 278df8bae1dSRodney W. Grimes struct sockbuf *sb; 279df8bae1dSRodney W. Grimes { 280df8bae1dSRodney W. Grimes 281df8bae1dSRodney W. Grimes sb->sb_flags |= SB_WAIT; 282df8bae1dSRodney W. Grimes return (tsleep((caddr_t)&sb->sb_cc, 283df8bae1dSRodney W. Grimes (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, netio, 284df8bae1dSRodney W. Grimes sb->sb_timeo)); 285df8bae1dSRodney W. Grimes } 286df8bae1dSRodney W. Grimes 287df8bae1dSRodney W. Grimes /* 288df8bae1dSRodney W. Grimes * Lock a sockbuf already known to be locked; 289df8bae1dSRodney W. Grimes * return any error returned from sleep (EINTR). 290df8bae1dSRodney W. Grimes */ 29126f9a767SRodney W. Grimes int 292df8bae1dSRodney W. Grimes sb_lock(sb) 293df8bae1dSRodney W. Grimes register struct sockbuf *sb; 294df8bae1dSRodney W. Grimes { 295df8bae1dSRodney W. Grimes int error; 296df8bae1dSRodney W. Grimes 297df8bae1dSRodney W. Grimes while (sb->sb_flags & SB_LOCK) { 298df8bae1dSRodney W. Grimes sb->sb_flags |= SB_WANT; 299797f2d22SPoul-Henning Kamp error = tsleep((caddr_t)&sb->sb_flags, 300df8bae1dSRodney W. Grimes (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH, 301797f2d22SPoul-Henning Kamp netio, 0); 302797f2d22SPoul-Henning Kamp if (error) 303df8bae1dSRodney W. Grimes return (error); 304df8bae1dSRodney W. Grimes } 305df8bae1dSRodney W. Grimes sb->sb_flags |= SB_LOCK; 306df8bae1dSRodney W. Grimes return (0); 307df8bae1dSRodney W. Grimes } 308df8bae1dSRodney W. Grimes 309df8bae1dSRodney W. Grimes /* 310df8bae1dSRodney W. Grimes * Wakeup processes waiting on a socket buffer. 311df8bae1dSRodney W. Grimes * Do asynchronous notification via SIGIO 312df8bae1dSRodney W. Grimes * if the socket has the SS_ASYNC flag set. 313df8bae1dSRodney W. Grimes */ 31426f9a767SRodney W. Grimes void 315df8bae1dSRodney W. Grimes sowakeup(so, sb) 316df8bae1dSRodney W. Grimes register struct socket *so; 317df8bae1dSRodney W. Grimes register struct sockbuf *sb; 318df8bae1dSRodney W. Grimes { 319df8bae1dSRodney W. Grimes struct proc *p; 320df8bae1dSRodney W. Grimes 321df8bae1dSRodney W. Grimes selwakeup(&sb->sb_sel); 322df8bae1dSRodney W. Grimes sb->sb_flags &= ~SB_SEL; 323df8bae1dSRodney W. Grimes if (sb->sb_flags & SB_WAIT) { 324df8bae1dSRodney W. Grimes sb->sb_flags &= ~SB_WAIT; 325df8bae1dSRodney W. Grimes wakeup((caddr_t)&sb->sb_cc); 326df8bae1dSRodney W. Grimes } 327df8bae1dSRodney W. Grimes if (so->so_state & SS_ASYNC) { 328df8bae1dSRodney W. Grimes if (so->so_pgid < 0) 329df8bae1dSRodney W. Grimes gsignal(-so->so_pgid, SIGIO); 330df8bae1dSRodney W. Grimes else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0) 331df8bae1dSRodney W. Grimes psignal(p, SIGIO); 332df8bae1dSRodney W. Grimes } 333df8bae1dSRodney W. Grimes } 334df8bae1dSRodney W. Grimes 335df8bae1dSRodney W. Grimes /* 336df8bae1dSRodney W. Grimes * Socket buffer (struct sockbuf) utility routines. 337df8bae1dSRodney W. Grimes * 338df8bae1dSRodney W. Grimes * Each socket contains two socket buffers: one for sending data and 339df8bae1dSRodney W. Grimes * one for receiving data. Each buffer contains a queue of mbufs, 340df8bae1dSRodney W. Grimes * information about the number of mbufs and amount of data in the 341df8bae1dSRodney W. Grimes * queue, and other fields allowing select() statements and notification 342df8bae1dSRodney W. Grimes * on data availability to be implemented. 343df8bae1dSRodney W. Grimes * 344df8bae1dSRodney W. Grimes * Data stored in a socket buffer is maintained as a list of records. 345df8bae1dSRodney W. Grimes * Each record is a list of mbufs chained together with the m_next 346df8bae1dSRodney W. Grimes * field. Records are chained together with the m_nextpkt field. The upper 347df8bae1dSRodney W. Grimes * level routine soreceive() expects the following conventions to be 348df8bae1dSRodney W. Grimes * observed when placing information in the receive buffer: 349df8bae1dSRodney W. Grimes * 350df8bae1dSRodney W. Grimes * 1. If the protocol requires each message be preceded by the sender's 351df8bae1dSRodney W. Grimes * name, then a record containing that name must be present before 352df8bae1dSRodney W. Grimes * any associated data (mbuf's must be of type MT_SONAME). 353df8bae1dSRodney W. Grimes * 2. If the protocol supports the exchange of ``access rights'' (really 354df8bae1dSRodney W. Grimes * just additional data associated with the message), and there are 355df8bae1dSRodney W. Grimes * ``rights'' to be received, then a record containing this data 356df8bae1dSRodney W. Grimes * should be present (mbuf's must be of type MT_RIGHTS). 357df8bae1dSRodney W. Grimes * 3. If a name or rights record exists, then it must be followed by 358df8bae1dSRodney W. Grimes * a data record, perhaps of zero length. 359df8bae1dSRodney W. Grimes * 360df8bae1dSRodney W. Grimes * Before using a new socket structure it is first necessary to reserve 361df8bae1dSRodney W. Grimes * buffer space to the socket, by calling sbreserve(). This should commit 362df8bae1dSRodney W. Grimes * some of the available buffer space in the system buffer pool for the 363df8bae1dSRodney W. Grimes * socket (currently, it does nothing but enforce limits). The space 364df8bae1dSRodney W. Grimes * should be released by calling sbrelease() when the socket is destroyed. 365df8bae1dSRodney W. Grimes */ 366df8bae1dSRodney W. Grimes 36726f9a767SRodney W. Grimes int 368df8bae1dSRodney W. Grimes soreserve(so, sndcc, rcvcc) 369df8bae1dSRodney W. Grimes register struct socket *so; 370df8bae1dSRodney W. Grimes u_long sndcc, rcvcc; 371df8bae1dSRodney W. Grimes { 372df8bae1dSRodney W. Grimes 373df8bae1dSRodney W. Grimes if (sbreserve(&so->so_snd, sndcc) == 0) 374df8bae1dSRodney W. Grimes goto bad; 375df8bae1dSRodney W. Grimes if (sbreserve(&so->so_rcv, rcvcc) == 0) 376df8bae1dSRodney W. Grimes goto bad2; 377df8bae1dSRodney W. Grimes if (so->so_rcv.sb_lowat == 0) 378df8bae1dSRodney W. Grimes so->so_rcv.sb_lowat = 1; 379df8bae1dSRodney W. Grimes if (so->so_snd.sb_lowat == 0) 380df8bae1dSRodney W. Grimes so->so_snd.sb_lowat = MCLBYTES; 381df8bae1dSRodney W. Grimes if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) 382df8bae1dSRodney W. Grimes so->so_snd.sb_lowat = so->so_snd.sb_hiwat; 383df8bae1dSRodney W. Grimes return (0); 384df8bae1dSRodney W. Grimes bad2: 385df8bae1dSRodney W. Grimes sbrelease(&so->so_snd); 386df8bae1dSRodney W. Grimes bad: 387df8bae1dSRodney W. Grimes return (ENOBUFS); 388df8bae1dSRodney W. Grimes } 389df8bae1dSRodney W. Grimes 390df8bae1dSRodney W. Grimes /* 391df8bae1dSRodney W. Grimes * Allot mbufs to a sockbuf. 392df8bae1dSRodney W. Grimes * Attempt to scale mbmax so that mbcnt doesn't become limiting 393df8bae1dSRodney W. Grimes * if buffering efficiency is near the normal case. 394df8bae1dSRodney W. Grimes */ 39526f9a767SRodney W. Grimes int 396df8bae1dSRodney W. Grimes sbreserve(sb, cc) 397df8bae1dSRodney W. Grimes struct sockbuf *sb; 398df8bae1dSRodney W. Grimes u_long cc; 399df8bae1dSRodney W. Grimes { 400df8bae1dSRodney W. Grimes 401df8bae1dSRodney W. Grimes if (cc > sb_max * MCLBYTES / (MSIZE + MCLBYTES)) 402df8bae1dSRodney W. Grimes return (0); 403df8bae1dSRodney W. Grimes sb->sb_hiwat = cc; 404df8bae1dSRodney W. Grimes sb->sb_mbmax = min(cc * 2, sb_max); 405df8bae1dSRodney W. Grimes if (sb->sb_lowat > sb->sb_hiwat) 406df8bae1dSRodney W. Grimes sb->sb_lowat = sb->sb_hiwat; 407df8bae1dSRodney W. Grimes return (1); 408df8bae1dSRodney W. Grimes } 409df8bae1dSRodney W. Grimes 410df8bae1dSRodney W. Grimes /* 411df8bae1dSRodney W. Grimes * Free mbufs held by a socket, and reserved mbuf space. 412df8bae1dSRodney W. Grimes */ 41326f9a767SRodney W. Grimes void 414df8bae1dSRodney W. Grimes sbrelease(sb) 415df8bae1dSRodney W. Grimes struct sockbuf *sb; 416df8bae1dSRodney W. Grimes { 417df8bae1dSRodney W. Grimes 418df8bae1dSRodney W. Grimes sbflush(sb); 419df8bae1dSRodney W. Grimes sb->sb_hiwat = sb->sb_mbmax = 0; 420df8bae1dSRodney W. Grimes } 421df8bae1dSRodney W. Grimes 422df8bae1dSRodney W. Grimes /* 423df8bae1dSRodney W. Grimes * Routines to add and remove 424df8bae1dSRodney W. Grimes * data from an mbuf queue. 425df8bae1dSRodney W. Grimes * 426df8bae1dSRodney W. Grimes * The routines sbappend() or sbappendrecord() are normally called to 427df8bae1dSRodney W. Grimes * append new mbufs to a socket buffer, after checking that adequate 428df8bae1dSRodney W. Grimes * space is available, comparing the function sbspace() with the amount 429df8bae1dSRodney W. Grimes * of data to be added. sbappendrecord() differs from sbappend() in 430df8bae1dSRodney W. Grimes * that data supplied is treated as the beginning of a new record. 431df8bae1dSRodney W. Grimes * To place a sender's address, optional access rights, and data in a 432df8bae1dSRodney W. Grimes * socket receive buffer, sbappendaddr() should be used. To place 433df8bae1dSRodney W. Grimes * access rights and data in a socket receive buffer, sbappendrights() 434df8bae1dSRodney W. Grimes * should be used. In either case, the new data begins a new record. 435df8bae1dSRodney W. Grimes * Note that unlike sbappend() and sbappendrecord(), these routines check 436df8bae1dSRodney W. Grimes * for the caller that there will be enough space to store the data. 437df8bae1dSRodney W. Grimes * Each fails if there is not enough space, or if it cannot find mbufs 438df8bae1dSRodney W. Grimes * to store additional information in. 439df8bae1dSRodney W. Grimes * 440df8bae1dSRodney W. Grimes * Reliable protocols may use the socket send buffer to hold data 441df8bae1dSRodney W. Grimes * awaiting acknowledgement. Data is normally copied from a socket 442df8bae1dSRodney W. Grimes * send buffer in a protocol with m_copy for output to a peer, 443df8bae1dSRodney W. Grimes * and then removing the data from the socket buffer with sbdrop() 444df8bae1dSRodney W. Grimes * or sbdroprecord() when the data is acknowledged by the peer. 445df8bae1dSRodney W. Grimes */ 446df8bae1dSRodney W. Grimes 447df8bae1dSRodney W. Grimes /* 448df8bae1dSRodney W. Grimes * Append mbuf chain m to the last record in the 449df8bae1dSRodney W. Grimes * socket buffer sb. The additional space associated 450df8bae1dSRodney W. Grimes * the mbuf chain is recorded in sb. Empty mbufs are 451df8bae1dSRodney W. Grimes * discarded and mbufs are compacted where possible. 452df8bae1dSRodney W. Grimes */ 45326f9a767SRodney W. Grimes void 454df8bae1dSRodney W. Grimes sbappend(sb, m) 455df8bae1dSRodney W. Grimes struct sockbuf *sb; 456df8bae1dSRodney W. Grimes struct mbuf *m; 457df8bae1dSRodney W. Grimes { 458df8bae1dSRodney W. Grimes register struct mbuf *n; 459df8bae1dSRodney W. Grimes 460df8bae1dSRodney W. Grimes if (m == 0) 461df8bae1dSRodney W. Grimes return; 462797f2d22SPoul-Henning Kamp n = sb->sb_mb; 463797f2d22SPoul-Henning Kamp if (n) { 464df8bae1dSRodney W. Grimes while (n->m_nextpkt) 465df8bae1dSRodney W. Grimes n = n->m_nextpkt; 466df8bae1dSRodney W. Grimes do { 467df8bae1dSRodney W. Grimes if (n->m_flags & M_EOR) { 468df8bae1dSRodney W. Grimes sbappendrecord(sb, m); /* XXXXXX!!!! */ 469df8bae1dSRodney W. Grimes return; 470df8bae1dSRodney W. Grimes } 471df8bae1dSRodney W. Grimes } while (n->m_next && (n = n->m_next)); 472df8bae1dSRodney W. Grimes } 473df8bae1dSRodney W. Grimes sbcompress(sb, m, n); 474df8bae1dSRodney W. Grimes } 475df8bae1dSRodney W. Grimes 476df8bae1dSRodney W. Grimes #ifdef SOCKBUF_DEBUG 47726f9a767SRodney W. Grimes void 478df8bae1dSRodney W. Grimes sbcheck(sb) 479df8bae1dSRodney W. Grimes register struct sockbuf *sb; 480df8bae1dSRodney W. Grimes { 481df8bae1dSRodney W. Grimes register struct mbuf *m; 482df8bae1dSRodney W. Grimes register int len = 0, mbcnt = 0; 483df8bae1dSRodney W. Grimes 484df8bae1dSRodney W. Grimes for (m = sb->sb_mb; m; m = m->m_next) { 485df8bae1dSRodney W. Grimes len += m->m_len; 486df8bae1dSRodney W. Grimes mbcnt += MSIZE; 487df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT) 488df8bae1dSRodney W. Grimes mbcnt += m->m_ext.ext_size; 489df8bae1dSRodney W. Grimes if (m->m_nextpkt) 490df8bae1dSRodney W. Grimes panic("sbcheck nextpkt"); 491df8bae1dSRodney W. Grimes } 492df8bae1dSRodney W. Grimes if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) { 493df8bae1dSRodney W. Grimes printf("cc %d != %d || mbcnt %d != %d\n", len, sb->sb_cc, 494df8bae1dSRodney W. Grimes mbcnt, sb->sb_mbcnt); 495df8bae1dSRodney W. Grimes panic("sbcheck"); 496df8bae1dSRodney W. Grimes } 497df8bae1dSRodney W. Grimes } 498df8bae1dSRodney W. Grimes #endif 499df8bae1dSRodney W. Grimes 500df8bae1dSRodney W. Grimes /* 501df8bae1dSRodney W. Grimes * As above, except the mbuf chain 502df8bae1dSRodney W. Grimes * begins a new record. 503df8bae1dSRodney W. Grimes */ 50426f9a767SRodney W. Grimes void 505df8bae1dSRodney W. Grimes sbappendrecord(sb, m0) 506df8bae1dSRodney W. Grimes register struct sockbuf *sb; 507df8bae1dSRodney W. Grimes register struct mbuf *m0; 508df8bae1dSRodney W. Grimes { 509df8bae1dSRodney W. Grimes register struct mbuf *m; 510df8bae1dSRodney W. Grimes 511df8bae1dSRodney W. Grimes if (m0 == 0) 512df8bae1dSRodney W. Grimes return; 513797f2d22SPoul-Henning Kamp m = sb->sb_mb; 514797f2d22SPoul-Henning Kamp if (m) 515df8bae1dSRodney W. Grimes while (m->m_nextpkt) 516df8bae1dSRodney W. Grimes m = m->m_nextpkt; 517df8bae1dSRodney W. Grimes /* 518df8bae1dSRodney W. Grimes * Put the first mbuf on the queue. 519df8bae1dSRodney W. Grimes * Note this permits zero length records. 520df8bae1dSRodney W. Grimes */ 521df8bae1dSRodney W. Grimes sballoc(sb, m0); 522df8bae1dSRodney W. Grimes if (m) 523df8bae1dSRodney W. Grimes m->m_nextpkt = m0; 524df8bae1dSRodney W. Grimes else 525df8bae1dSRodney W. Grimes sb->sb_mb = m0; 526df8bae1dSRodney W. Grimes m = m0->m_next; 527df8bae1dSRodney W. Grimes m0->m_next = 0; 528df8bae1dSRodney W. Grimes if (m && (m0->m_flags & M_EOR)) { 529df8bae1dSRodney W. Grimes m0->m_flags &= ~M_EOR; 530df8bae1dSRodney W. Grimes m->m_flags |= M_EOR; 531df8bae1dSRodney W. Grimes } 532df8bae1dSRodney W. Grimes sbcompress(sb, m, m0); 533df8bae1dSRodney W. Grimes } 534df8bae1dSRodney W. Grimes 535df8bae1dSRodney W. Grimes /* 536df8bae1dSRodney W. Grimes * As above except that OOB data 537df8bae1dSRodney W. Grimes * is inserted at the beginning of the sockbuf, 538df8bae1dSRodney W. Grimes * but after any other OOB data. 539df8bae1dSRodney W. Grimes */ 54026f9a767SRodney W. Grimes void 541df8bae1dSRodney W. Grimes sbinsertoob(sb, m0) 542df8bae1dSRodney W. Grimes register struct sockbuf *sb; 543df8bae1dSRodney W. Grimes register struct mbuf *m0; 544df8bae1dSRodney W. Grimes { 545df8bae1dSRodney W. Grimes register struct mbuf *m; 546df8bae1dSRodney W. Grimes register struct mbuf **mp; 547df8bae1dSRodney W. Grimes 548df8bae1dSRodney W. Grimes if (m0 == 0) 549df8bae1dSRodney W. Grimes return; 550797f2d22SPoul-Henning Kamp for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) { 551797f2d22SPoul-Henning Kamp m = *mp; 552df8bae1dSRodney W. Grimes again: 553df8bae1dSRodney W. Grimes switch (m->m_type) { 554df8bae1dSRodney W. Grimes 555df8bae1dSRodney W. Grimes case MT_OOBDATA: 556df8bae1dSRodney W. Grimes continue; /* WANT next train */ 557df8bae1dSRodney W. Grimes 558df8bae1dSRodney W. Grimes case MT_CONTROL: 559797f2d22SPoul-Henning Kamp m = m->m_next; 560797f2d22SPoul-Henning Kamp if (m) 561df8bae1dSRodney W. Grimes goto again; /* inspect THIS train further */ 562df8bae1dSRodney W. Grimes } 563df8bae1dSRodney W. Grimes break; 564df8bae1dSRodney W. Grimes } 565df8bae1dSRodney W. Grimes /* 566df8bae1dSRodney W. Grimes * Put the first mbuf on the queue. 567df8bae1dSRodney W. Grimes * Note this permits zero length records. 568df8bae1dSRodney W. Grimes */ 569df8bae1dSRodney W. Grimes sballoc(sb, m0); 570df8bae1dSRodney W. Grimes m0->m_nextpkt = *mp; 571df8bae1dSRodney W. Grimes *mp = m0; 572df8bae1dSRodney W. Grimes m = m0->m_next; 573df8bae1dSRodney W. Grimes m0->m_next = 0; 574df8bae1dSRodney W. Grimes if (m && (m0->m_flags & M_EOR)) { 575df8bae1dSRodney W. Grimes m0->m_flags &= ~M_EOR; 576df8bae1dSRodney W. Grimes m->m_flags |= M_EOR; 577df8bae1dSRodney W. Grimes } 578df8bae1dSRodney W. Grimes sbcompress(sb, m, m0); 579df8bae1dSRodney W. Grimes } 580df8bae1dSRodney W. Grimes 581df8bae1dSRodney W. Grimes /* 582df8bae1dSRodney W. Grimes * Append address and data, and optionally, control (ancillary) data 583df8bae1dSRodney W. Grimes * to the receive queue of a socket. If present, 584df8bae1dSRodney W. Grimes * m0 must include a packet header with total length. 585df8bae1dSRodney W. Grimes * Returns 0 if no space in sockbuf or insufficient mbufs. 586df8bae1dSRodney W. Grimes */ 58726f9a767SRodney W. Grimes int 588df8bae1dSRodney W. Grimes sbappendaddr(sb, asa, m0, control) 589df8bae1dSRodney W. Grimes register struct sockbuf *sb; 590df8bae1dSRodney W. Grimes struct sockaddr *asa; 591df8bae1dSRodney W. Grimes struct mbuf *m0, *control; 592df8bae1dSRodney W. Grimes { 593df8bae1dSRodney W. Grimes register struct mbuf *m, *n; 594df8bae1dSRodney W. Grimes int space = asa->sa_len; 595df8bae1dSRodney W. Grimes 596df8bae1dSRodney W. Grimes if (m0 && (m0->m_flags & M_PKTHDR) == 0) 597df8bae1dSRodney W. Grimes panic("sbappendaddr"); 598df8bae1dSRodney W. Grimes if (m0) 599df8bae1dSRodney W. Grimes space += m0->m_pkthdr.len; 600df8bae1dSRodney W. Grimes for (n = control; n; n = n->m_next) { 601df8bae1dSRodney W. Grimes space += n->m_len; 602df8bae1dSRodney W. Grimes if (n->m_next == 0) /* keep pointer to last control buf */ 603df8bae1dSRodney W. Grimes break; 604df8bae1dSRodney W. Grimes } 605df8bae1dSRodney W. Grimes if (space > sbspace(sb)) 606df8bae1dSRodney W. Grimes return (0); 607df8bae1dSRodney W. Grimes if (asa->sa_len > MLEN) 608df8bae1dSRodney W. Grimes return (0); 609df8bae1dSRodney W. Grimes MGET(m, M_DONTWAIT, MT_SONAME); 610df8bae1dSRodney W. Grimes if (m == 0) 611df8bae1dSRodney W. Grimes return (0); 612df8bae1dSRodney W. Grimes m->m_len = asa->sa_len; 613df8bae1dSRodney W. Grimes bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len); 614df8bae1dSRodney W. Grimes if (n) 615df8bae1dSRodney W. Grimes n->m_next = m0; /* concatenate data to control */ 616df8bae1dSRodney W. Grimes else 617df8bae1dSRodney W. Grimes control = m0; 618df8bae1dSRodney W. Grimes m->m_next = control; 619df8bae1dSRodney W. Grimes for (n = m; n; n = n->m_next) 620df8bae1dSRodney W. Grimes sballoc(sb, n); 621797f2d22SPoul-Henning Kamp n = sb->sb_mb; 622797f2d22SPoul-Henning Kamp if (n) { 623df8bae1dSRodney W. Grimes while (n->m_nextpkt) 624df8bae1dSRodney W. Grimes n = n->m_nextpkt; 625df8bae1dSRodney W. Grimes n->m_nextpkt = m; 626df8bae1dSRodney W. Grimes } else 627df8bae1dSRodney W. Grimes sb->sb_mb = m; 628df8bae1dSRodney W. Grimes return (1); 629df8bae1dSRodney W. Grimes } 630df8bae1dSRodney W. Grimes 63126f9a767SRodney W. Grimes int 632df8bae1dSRodney W. Grimes sbappendcontrol(sb, m0, control) 633df8bae1dSRodney W. Grimes struct sockbuf *sb; 634df8bae1dSRodney W. Grimes struct mbuf *control, *m0; 635df8bae1dSRodney W. Grimes { 636df8bae1dSRodney W. Grimes register struct mbuf *m, *n; 637df8bae1dSRodney W. Grimes int space = 0; 638df8bae1dSRodney W. Grimes 639df8bae1dSRodney W. Grimes if (control == 0) 640df8bae1dSRodney W. Grimes panic("sbappendcontrol"); 641df8bae1dSRodney W. Grimes for (m = control; ; m = m->m_next) { 642df8bae1dSRodney W. Grimes space += m->m_len; 643df8bae1dSRodney W. Grimes if (m->m_next == 0) 644df8bae1dSRodney W. Grimes break; 645df8bae1dSRodney W. Grimes } 646df8bae1dSRodney W. Grimes n = m; /* save pointer to last control buffer */ 647df8bae1dSRodney W. Grimes for (m = m0; m; m = m->m_next) 648df8bae1dSRodney W. Grimes space += m->m_len; 649df8bae1dSRodney W. Grimes if (space > sbspace(sb)) 650df8bae1dSRodney W. Grimes return (0); 651df8bae1dSRodney W. Grimes n->m_next = m0; /* concatenate data to control */ 652df8bae1dSRodney W. Grimes for (m = control; m; m = m->m_next) 653df8bae1dSRodney W. Grimes sballoc(sb, m); 654797f2d22SPoul-Henning Kamp n = sb->sb_mb; 655797f2d22SPoul-Henning Kamp if (n) { 656df8bae1dSRodney W. Grimes while (n->m_nextpkt) 657df8bae1dSRodney W. Grimes n = n->m_nextpkt; 658df8bae1dSRodney W. Grimes n->m_nextpkt = control; 659df8bae1dSRodney W. Grimes } else 660df8bae1dSRodney W. Grimes sb->sb_mb = control; 661df8bae1dSRodney W. Grimes return (1); 662df8bae1dSRodney W. Grimes } 663df8bae1dSRodney W. Grimes 664df8bae1dSRodney W. Grimes /* 665df8bae1dSRodney W. Grimes * Compress mbuf chain m into the socket 666df8bae1dSRodney W. Grimes * buffer sb following mbuf n. If n 667df8bae1dSRodney W. Grimes * is null, the buffer is presumed empty. 668df8bae1dSRodney W. Grimes */ 66926f9a767SRodney W. Grimes void 670df8bae1dSRodney W. Grimes sbcompress(sb, m, n) 671df8bae1dSRodney W. Grimes register struct sockbuf *sb; 672df8bae1dSRodney W. Grimes register struct mbuf *m, *n; 673df8bae1dSRodney W. Grimes { 674df8bae1dSRodney W. Grimes register int eor = 0; 675df8bae1dSRodney W. Grimes register struct mbuf *o; 676df8bae1dSRodney W. Grimes 677df8bae1dSRodney W. Grimes while (m) { 678df8bae1dSRodney W. Grimes eor |= m->m_flags & M_EOR; 679df8bae1dSRodney W. Grimes if (m->m_len == 0 && 680df8bae1dSRodney W. Grimes (eor == 0 || 681df8bae1dSRodney W. Grimes (((o = m->m_next) || (o = n)) && 682df8bae1dSRodney W. Grimes o->m_type == m->m_type))) { 683df8bae1dSRodney W. Grimes m = m_free(m); 684df8bae1dSRodney W. Grimes continue; 685df8bae1dSRodney W. Grimes } 686df8bae1dSRodney W. Grimes if (n && (n->m_flags & (M_EXT | M_EOR)) == 0 && 687df8bae1dSRodney W. Grimes (n->m_data + n->m_len + m->m_len) < &n->m_dat[MLEN] && 688df8bae1dSRodney W. Grimes n->m_type == m->m_type) { 689df8bae1dSRodney W. Grimes bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, 690df8bae1dSRodney W. Grimes (unsigned)m->m_len); 691df8bae1dSRodney W. Grimes n->m_len += m->m_len; 692df8bae1dSRodney W. Grimes sb->sb_cc += m->m_len; 693df8bae1dSRodney W. Grimes m = m_free(m); 694df8bae1dSRodney W. Grimes continue; 695df8bae1dSRodney W. Grimes } 696df8bae1dSRodney W. Grimes if (n) 697df8bae1dSRodney W. Grimes n->m_next = m; 698df8bae1dSRodney W. Grimes else 699df8bae1dSRodney W. Grimes sb->sb_mb = m; 700df8bae1dSRodney W. Grimes sballoc(sb, m); 701df8bae1dSRodney W. Grimes n = m; 702df8bae1dSRodney W. Grimes m->m_flags &= ~M_EOR; 703df8bae1dSRodney W. Grimes m = m->m_next; 704df8bae1dSRodney W. Grimes n->m_next = 0; 705df8bae1dSRodney W. Grimes } 706df8bae1dSRodney W. Grimes if (eor) { 707df8bae1dSRodney W. Grimes if (n) 708df8bae1dSRodney W. Grimes n->m_flags |= eor; 709df8bae1dSRodney W. Grimes else 710df8bae1dSRodney W. Grimes printf("semi-panic: sbcompress\n"); 711df8bae1dSRodney W. Grimes } 712df8bae1dSRodney W. Grimes } 713df8bae1dSRodney W. Grimes 714df8bae1dSRodney W. Grimes /* 715df8bae1dSRodney W. Grimes * Free all mbufs in a sockbuf. 716df8bae1dSRodney W. Grimes * Check that all resources are reclaimed. 717df8bae1dSRodney W. Grimes */ 71826f9a767SRodney W. Grimes void 719df8bae1dSRodney W. Grimes sbflush(sb) 720df8bae1dSRodney W. Grimes register struct sockbuf *sb; 721df8bae1dSRodney W. Grimes { 722df8bae1dSRodney W. Grimes 723df8bae1dSRodney W. Grimes if (sb->sb_flags & SB_LOCK) 724df8bae1dSRodney W. Grimes panic("sbflush"); 725df8bae1dSRodney W. Grimes while (sb->sb_mbcnt) 726df8bae1dSRodney W. Grimes sbdrop(sb, (int)sb->sb_cc); 727df8bae1dSRodney W. Grimes if (sb->sb_cc || sb->sb_mb) 728df8bae1dSRodney W. Grimes panic("sbflush 2"); 729df8bae1dSRodney W. Grimes } 730df8bae1dSRodney W. Grimes 731df8bae1dSRodney W. Grimes /* 732df8bae1dSRodney W. Grimes * Drop data from (the front of) a sockbuf. 733df8bae1dSRodney W. Grimes */ 73426f9a767SRodney W. Grimes void 735df8bae1dSRodney W. Grimes sbdrop(sb, len) 736df8bae1dSRodney W. Grimes register struct sockbuf *sb; 737df8bae1dSRodney W. Grimes register int len; 738df8bae1dSRodney W. Grimes { 739df8bae1dSRodney W. Grimes register struct mbuf *m, *mn; 740df8bae1dSRodney W. Grimes struct mbuf *next; 741df8bae1dSRodney W. Grimes 742df8bae1dSRodney W. Grimes next = (m = sb->sb_mb) ? m->m_nextpkt : 0; 743df8bae1dSRodney W. Grimes while (len > 0) { 744df8bae1dSRodney W. Grimes if (m == 0) { 745df8bae1dSRodney W. Grimes if (next == 0) 746df8bae1dSRodney W. Grimes panic("sbdrop"); 747df8bae1dSRodney W. Grimes m = next; 748df8bae1dSRodney W. Grimes next = m->m_nextpkt; 749df8bae1dSRodney W. Grimes continue; 750df8bae1dSRodney W. Grimes } 751df8bae1dSRodney W. Grimes if (m->m_len > len) { 752df8bae1dSRodney W. Grimes m->m_len -= len; 753df8bae1dSRodney W. Grimes m->m_data += len; 754df8bae1dSRodney W. Grimes sb->sb_cc -= len; 755df8bae1dSRodney W. Grimes break; 756df8bae1dSRodney W. Grimes } 757df8bae1dSRodney W. Grimes len -= m->m_len; 758df8bae1dSRodney W. Grimes sbfree(sb, m); 759df8bae1dSRodney W. Grimes MFREE(m, mn); 760df8bae1dSRodney W. Grimes m = mn; 761df8bae1dSRodney W. Grimes } 762df8bae1dSRodney W. Grimes while (m && m->m_len == 0) { 763df8bae1dSRodney W. Grimes sbfree(sb, m); 764df8bae1dSRodney W. Grimes MFREE(m, mn); 765df8bae1dSRodney W. Grimes m = mn; 766df8bae1dSRodney W. Grimes } 767df8bae1dSRodney W. Grimes if (m) { 768df8bae1dSRodney W. Grimes sb->sb_mb = m; 769df8bae1dSRodney W. Grimes m->m_nextpkt = next; 770df8bae1dSRodney W. Grimes } else 771df8bae1dSRodney W. Grimes sb->sb_mb = next; 772df8bae1dSRodney W. Grimes } 773df8bae1dSRodney W. Grimes 774df8bae1dSRodney W. Grimes /* 775df8bae1dSRodney W. Grimes * Drop a record off the front of a sockbuf 776df8bae1dSRodney W. Grimes * and move the next record to the front. 777df8bae1dSRodney W. Grimes */ 77826f9a767SRodney W. Grimes void 779df8bae1dSRodney W. Grimes sbdroprecord(sb) 780df8bae1dSRodney W. Grimes register struct sockbuf *sb; 781df8bae1dSRodney W. Grimes { 782df8bae1dSRodney W. Grimes register struct mbuf *m, *mn; 783df8bae1dSRodney W. Grimes 784df8bae1dSRodney W. Grimes m = sb->sb_mb; 785df8bae1dSRodney W. Grimes if (m) { 786df8bae1dSRodney W. Grimes sb->sb_mb = m->m_nextpkt; 787df8bae1dSRodney W. Grimes do { 788df8bae1dSRodney W. Grimes sbfree(sb, m); 789df8bae1dSRodney W. Grimes MFREE(m, mn); 790797f2d22SPoul-Henning Kamp m = mn; 791797f2d22SPoul-Henning Kamp } while (m); 792df8bae1dSRodney W. Grimes } 793df8bae1dSRodney W. Grimes } 794