uipc_socket2.c revision 1.49
1193326Sed/* $NetBSD: uipc_socket2.c,v 1.49 2003/02/26 06:31:11 matt Exp $ */ 2193326Sed 3193326Sed/* 4193326Sed * Copyright (c) 1982, 1986, 1988, 1990, 1993 5193326Sed * The Regents of the University of California. All rights reserved. 6193326Sed * 7193326Sed * Redistribution and use in source and binary forms, with or without 8193326Sed * modification, are permitted provided that the following conditions 9193326Sed * are met: 10193326Sed * 1. Redistributions of source code must retain the above copyright 11193326Sed * notice, this list of conditions and the following disclaimer. 12193326Sed * 2. Redistributions in binary form must reproduce the above copyright 13193326Sed * notice, this list of conditions and the following disclaimer in the 14193326Sed * documentation and/or other materials provided with the distribution. 15193326Sed * 3. All advertising materials mentioning features or use of this software 16193326Sed * must display the following acknowledgement: 17218893Sdim * This product includes software developed by the University of 18193326Sed * California, Berkeley and its contributors. 19193326Sed * 4. Neither the name of the University nor the names of its contributors 20193326Sed * may be used to endorse or promote products derived from this software 21193326Sed * without specific prior written permission. 22193326Sed * 23193326Sed * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24198092Srdivacky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25193326Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26193326Sed * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27193326Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28193326Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29193326Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30193326Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31193326Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32218893Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33218893Sdim * SUCH DAMAGE. 34218893Sdim * 35193326Sed * @(#)uipc_socket2.c 8.2 (Berkeley) 2/14/95 36193326Sed */ 37193326Sed 38193326Sed#include <sys/cdefs.h> 39193326Sed__KERNEL_RCSID(0, "$NetBSD: uipc_socket2.c,v 1.49 2003/02/26 06:31:11 matt Exp $"); 40193326Sed 41193326Sed#include <sys/param.h> 42193326Sed#include <sys/systm.h> 43193326Sed#include <sys/proc.h> 44193326Sed#include <sys/file.h> 45218893Sdim#include <sys/buf.h> 46193326Sed#include <sys/malloc.h> 47193326Sed#include <sys/mbuf.h> 48198092Srdivacky#include <sys/protosw.h> 49198092Srdivacky#include <sys/socket.h> 50198092Srdivacky#include <sys/socketvar.h> 51193326Sed#include <sys/signalvar.h> 52198092Srdivacky 53198092Srdivacky/* 54193326Sed * Primitive routines for operating on sockets and socket buffers 55218893Sdim */ 56193326Sed 57198092Srdivacky/* strings for sleep message: */ 58193326Sedconst char netcon[] = "netcon"; 59193326Sedconst char netcls[] = "netcls"; 60193326Sedconst char netio[] = "netio"; 61193326Sedconst char netlck[] = "netlck"; 62207619Srdivacky 63193326Sed/* 64193326Sed * Procedures to manipulate state flags of socket 65207619Srdivacky * and do appropriate wakeups. Normal sequence from the 66193326Sed * active (originating) side is that soisconnecting() is 67218893Sdim * called during processing of connect() call, 68218893Sdim * resulting in an eventual call to soisconnected() if/when the 69218893Sdim * connection is established. When the connection is torn down 70218893Sdim * soisdisconnecting() is called during processing of disconnect() call, 71207619Srdivacky * and soisdisconnected() is called when the connection to the peer 72198092Srdivacky * is totally severed. The semantics of these routines are such that 73193326Sed * connectionless protocols can call soisconnected() and soisdisconnected() 74193326Sed * only, bypassing the in-progress calls when setting up a ``connection'' 75198092Srdivacky * takes no time. 76193326Sed * 77193326Sed * From the passive side, a socket is created with 78193326Sed * two queues of sockets: so_q0 for connections in progress 79193326Sed * and so_q for connections already made and awaiting user acceptance. 80198092Srdivacky * As a protocol is preparing incoming connections, it creates a socket 81207619Srdivacky * structure queued on so_q0 by calling sonewconn(). When the connection 82193326Sed * is established, soisconnected() is called, and transfers the 83207619Srdivacky * socket structure to so_q, making it available to accept(). 84207619Srdivacky * 85207619Srdivacky * If a socket is closed with sockets on either 86207619Srdivacky * so_q0 or so_q, these sockets are dropped. 87198092Srdivacky * 88218893Sdim * If higher level protocols are implemented in 89193326Sed * the kernel, the wakeups done here will sometimes 90193326Sed * cause software-interrupt process scheduling. 91198092Srdivacky */ 92193326Sed 93193326Sedvoid 94193326Sedsoisconnecting(struct socket *so) 95193326Sed{ 96198092Srdivacky 97193326Sed so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); 98218893Sdim so->so_state |= SS_ISCONNECTING; 99193326Sed} 100193326Sed 101193326Sedvoid 102193326Sedsoisconnected(struct socket *so) 103193326Sed{ 104193326Sed struct socket *head; 105193326Sed 106193326Sed head = so->so_head; 107193326Sed so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); 108218893Sdim so->so_state |= SS_ISCONNECTED; 109218893Sdim if (head && soqremque(so, 0)) { 110218893Sdim soqinsque(head, so, 1); 111193326Sed sorwakeup(head); 112193326Sed wakeup((caddr_t)&head->so_timeo); 113193326Sed } else { 114193326Sed wakeup((caddr_t)&so->so_timeo); 115193326Sed sorwakeup(so); 116198092Srdivacky sowwakeup(so); 117193326Sed } 118193326Sed} 119193326Sed 120193326Sedvoid 121193326Sedsoisdisconnecting(struct socket *so) 122218893Sdim{ 123193326Sed 124193326Sed so->so_state &= ~SS_ISCONNECTING; 125198092Srdivacky so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE); 126193326Sed wakeup((caddr_t)&so->so_timeo); 127193326Sed sowwakeup(so); 128198092Srdivacky sorwakeup(so); 129193326Sed} 130193326Sed 131193326Sedvoid 132193326Sedsoisdisconnected(struct socket *so) 133198092Srdivacky{ 134193326Sed 135218893Sdim so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); 136193326Sed so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED); 137198092Srdivacky wakeup((caddr_t)&so->so_timeo); 138193326Sed sowwakeup(so); 139193326Sed sorwakeup(so); 140193326Sed} 141193326Sed 142198092Srdivacky/* 143198092Srdivacky * When an attempt at a new connection is noted on a socket 144193326Sed * which accepts connections, sonewconn is called. If the 145193326Sed * connection is possible (subject to space constraints, etc.) 146193326Sed * then we allocate a new structure, propoerly linked into the 147193326Sed * data structure of the original socket, and return this. 148218893Sdim * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED. 149218893Sdim * 150218893Sdim * Currently, sonewconn() is defined as sonewconn1() in socketvar.h 151193326Sed * to catch calls that are missing the (new) second parameter. 152193326Sed */ 153193326Sedstruct socket * 154194613Sedsonewconn1(struct socket *head, int connstatus) 155194613Sed{ 156198092Srdivacky struct socket *so; 157193326Sed int soqueue; 158193326Sed 159193326Sed soqueue = connstatus ? 1 : 0; 160193326Sed if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2) 161193326Sed return ((struct socket *)0); 162218893Sdim so = pool_get(&socket_pool, PR_NOWAIT); 163193326Sed if (so == NULL) 164193326Sed return (NULL); 165198092Srdivacky memset((caddr_t)so, 0, sizeof(*so)); 166193326Sed so->so_type = head->so_type; 167193326Sed so->so_options = head->so_options &~ SO_ACCEPTCONN; 168193326Sed so->so_linger = head->so_linger; 169193326Sed so->so_state = head->so_state | SS_NOFDREF; 170198092Srdivacky so->so_proto = head->so_proto; 171193326Sed so->so_timeo = head->so_timeo; 172218893Sdim so->so_pgid = head->so_pgid; 173193326Sed so->so_send = head->so_send; 174193326Sed so->so_receive = head->so_receive; 175193326Sed so->so_uid = head->so_uid; 176193326Sed#ifdef MBUFTRACE 177193326Sed so->so_mowner = head->so_mowner; 178193326Sed so->so_rcv.sb_mowner = head->so_rcv.sb_mowner; 179193326Sed so->so_snd.sb_mowner = head->so_snd.sb_mowner; 180193326Sed#endif 181193326Sed (void) soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat); 182198092Srdivacky soqinsque(head, so, soqueue); 183193326Sed if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH, 184218893Sdim (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0, 185218893Sdim (struct proc *)0)) { 186198092Srdivacky (void) soqremque(so, soqueue); 187218893Sdim pool_put(&socket_pool, so); 188218893Sdim return (NULL); 189218893Sdim } 190218893Sdim if (connstatus) { 191198092Srdivacky sorwakeup(head); 192193326Sed wakeup((caddr_t)&head->so_timeo); 193193326Sed so->so_state |= connstatus; 194193326Sed } 195193326Sed return (so); 196193326Sed} 197193326Sed 198198092Srdivackyvoid 199193326Sedsoqinsque(struct socket *head, struct socket *so, int q) 200193326Sed{ 201193326Sed 202198092Srdivacky#ifdef DIAGNOSTIC 203193326Sed if (so->so_onq != NULL) 204193326Sed panic("soqinsque"); 205193326Sed#endif 206193326Sed 207198092Srdivacky so->so_head = head; 208193326Sed if (q == 0) { 209193326Sed head->so_q0len++; 210193326Sed so->so_onq = &head->so_q0; 211218893Sdim } else { 212193326Sed head->so_qlen++; 213198092Srdivacky so->so_onq = &head->so_q; 214193326Sed } 215198092Srdivacky TAILQ_INSERT_TAIL(so->so_onq, so, so_qe); 216198092Srdivacky} 217198092Srdivacky 218193326Sedint 219193326Sedsoqremque(struct socket *so, int q) 220198092Srdivacky{ 221193326Sed struct socket *head; 222218893Sdim 223193326Sed head = so->so_head; 224193326Sed if (q == 0) { 225193326Sed if (so->so_onq != &head->so_q0) 226193326Sed return (0); 227193326Sed head->so_q0len--; 228193326Sed } else { 229193326Sed if (so->so_onq != &head->so_q) 230218893Sdim return (0); 231218893Sdim head->so_qlen--; 232218893Sdim } 233218893Sdim TAILQ_REMOVE(so->so_onq, so, so_qe); 234218893Sdim so->so_onq = NULL; 235218893Sdim so->so_head = NULL; 236218893Sdim return (1); 237193326Sed} 238218893Sdim 239218893Sdim/* 240218893Sdim * Socantsendmore indicates that no more data will be sent on the 241218893Sdim * socket; it would normally be applied to a socket when the user 242218893Sdim * informs the system that no more data is to be sent, by the protocol 243218893Sdim * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data 244218893Sdim * will be received, and will normally be applied to the socket by a 245193326Sed * protocol when it detects that the peer will send no more data. 246198092Srdivacky * Data queued for reading in the socket may yet be read. 247218893Sdim */ 248193326Sed 249218893Sdimvoid 250218893Sdimsocantsendmore(struct socket *so) 251218893Sdim{ 252218893Sdim 253218893Sdim so->so_state |= SS_CANTSENDMORE; 254193326Sed sowwakeup(so); 255218893Sdim} 256218893Sdim 257218893Sdimvoid 258218893Sdimsocantrcvmore(struct socket *so) 259218893Sdim{ 260218893Sdim 261218893Sdim so->so_state |= SS_CANTRCVMORE; 262218893Sdim sorwakeup(so); 263218893Sdim} 264218893Sdim 265198092Srdivacky/* 266218893Sdim * Wait for data to arrive at/drain from a socket buffer. 267218893Sdim */ 268218893Sdimint 269218893Sdimsbwait(struct sockbuf *sb) 270218893Sdim{ 271218893Sdim 272218893Sdim sb->sb_flags |= SB_WAIT; 273218893Sdim return (tsleep((caddr_t)&sb->sb_cc, 274218893Sdim (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, netio, 275218893Sdim sb->sb_timeo)); 276218893Sdim} 277218893Sdim 278218893Sdim/* 279218893Sdim * Lock a sockbuf already known to be locked; 280218893Sdim * return any error returned from sleep (EINTR). 281218893Sdim */ 282218893Sdimint 283218893Sdimsb_lock(struct sockbuf *sb) 284218893Sdim{ 285218893Sdim int error; 286218893Sdim 287218893Sdim while (sb->sb_flags & SB_LOCK) { 288218893Sdim sb->sb_flags |= SB_WANT; 289218893Sdim error = tsleep((caddr_t)&sb->sb_flags, 290218893Sdim (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH, 291218893Sdim netlck, 0); 292218893Sdim if (error) 293218893Sdim return (error); 294193326Sed } 295193326Sed sb->sb_flags |= SB_LOCK; 296193326Sed return (0); 297218893Sdim} 298218893Sdim 299198092Srdivacky/* 300218893Sdim * Wakeup processes waiting on a socket buffer. 301218893Sdim * Do asynchronous notification via SIGIO 302218893Sdim * if the socket buffer has the SB_ASYNC flag set. 303218893Sdim */ 304198092Srdivackyvoid 305218893Sdimsowakeup(struct socket *so, struct sockbuf *sb) 306218893Sdim{ 307218893Sdim struct proc *p; 308218893Sdim 309218893Sdim selnotify(&sb->sb_sel, 0); 310218893Sdim sb->sb_flags &= ~SB_SEL; 311218893Sdim if (sb->sb_flags & SB_WAIT) { 312218893Sdim sb->sb_flags &= ~SB_WAIT; 313218893Sdim wakeup((caddr_t)&sb->sb_cc); 314218893Sdim } 315218893Sdim if (sb->sb_flags & SB_ASYNC) { 316218893Sdim if (so->so_pgid < 0) 317218893Sdim gsignal(-so->so_pgid, SIGIO); 318218893Sdim else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0) 319218893Sdim psignal(p, SIGIO); 320218893Sdim } 321218893Sdim if (sb->sb_flags & SB_UPCALL) 322218893Sdim (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT); 323218893Sdim} 324218893Sdim 325218893Sdim/* 326218893Sdim * Socket buffer (struct sockbuf) utility routines. 327218893Sdim * 328218893Sdim * Each socket contains two socket buffers: one for sending data and 329218893Sdim * one for receiving data. Each buffer contains a queue of mbufs, 330218893Sdim * information about the number of mbufs and amount of data in the 331218893Sdim * queue, and other fields allowing poll() statements and notification 332218893Sdim * on data availability to be implemented. 333218893Sdim * 334193326Sed * Data stored in a socket buffer is maintained as a list of records. 335218893Sdim * Each record is a list of mbufs chained together with the m_next 336218893Sdim * field. Records are chained together with the m_nextpkt field. The upper 337218893Sdim * level routine soreceive() expects the following conventions to be 338218893Sdim * observed when placing information in the receive buffer: 339218893Sdim * 340218893Sdim * 1. If the protocol requires each message be preceded by the sender's 341218893Sdim * name, then a record containing that name must be present before 342218893Sdim * any associated data (mbuf's must be of type MT_SONAME). 343218893Sdim * 2. If the protocol supports the exchange of ``access rights'' (really 344218893Sdim * just additional data associated with the message), and there are 345218893Sdim * ``rights'' to be received, then a record containing this data 346193326Sed * should be present (mbuf's must be of type MT_CONTROL). 347218893Sdim * 3. If a name or rights record exists, then it must be followed by 348218893Sdim * a data record, perhaps of zero length. 349218893Sdim * 350218893Sdim * Before using a new socket structure it is first necessary to reserve 351193326Sed * buffer space to the socket, by calling sbreserve(). This should commit 352198092Srdivacky * some of the available buffer space in the system buffer pool for the 353198092Srdivacky * socket (currently, it does nothing but enforce limits). The space 354198092Srdivacky * should be released by calling sbrelease() when the socket is destroyed. 355193326Sed */ 356193326Sed 357198092Srdivackyint 358193326Sedsoreserve(struct socket *so, u_long sndcc, u_long rcvcc) 359218893Sdim{ 360218893Sdim 361218893Sdim if (sbreserve(&so->so_snd, sndcc) == 0) 362218893Sdim goto bad; 363193326Sed if (sbreserve(&so->so_rcv, rcvcc) == 0) 364218893Sdim goto bad2; 365193326Sed if (so->so_rcv.sb_lowat == 0) 366198092Srdivacky so->so_rcv.sb_lowat = 1; 367218893Sdim if (so->so_snd.sb_lowat == 0) 368218893Sdim so->so_snd.sb_lowat = MCLBYTES; 369218893Sdim if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) 370218893Sdim so->so_snd.sb_lowat = so->so_snd.sb_hiwat; 371218893Sdim return (0); 372218893Sdim bad2: 373193326Sed sbrelease(&so->so_snd); 374218893Sdim bad: 375218893Sdim return (ENOBUFS); 376218893Sdim} 377218893Sdim 378218893Sdim/* 379218893Sdim * Allot mbufs to a sockbuf. 380218893Sdim * Attempt to scale mbmax so that mbcnt doesn't become limiting 381218893Sdim * if buffering efficiency is near the normal case. 382198092Srdivacky */ 383218893Sdimint 384218893Sdimsbreserve(struct sockbuf *sb, u_long cc) 385193326Sed{ 386198092Srdivacky 387207619Srdivacky if (cc == 0 || 388207619Srdivacky (u_quad_t) cc > (u_quad_t) sb_max * MCLBYTES / (MSIZE + MCLBYTES)) 389207619Srdivacky return (0); 390207619Srdivacky sb->sb_hiwat = cc; 391207619Srdivacky sb->sb_mbmax = min(cc * 2, sb_max); 392207619Srdivacky if (sb->sb_lowat > sb->sb_hiwat) 393207619Srdivacky sb->sb_lowat = sb->sb_hiwat; 394207619Srdivacky return (1); 395207619Srdivacky} 396207619Srdivacky 397207619Srdivacky/* 398207619Srdivacky * Free mbufs held by a socket, and reserved mbuf space. 399207619Srdivacky */ 400207619Srdivackyvoid 401207619Srdivackysbrelease(struct sockbuf *sb) 402207619Srdivacky{ 403207619Srdivacky 404207619Srdivacky sbflush(sb); 405207619Srdivacky sb->sb_hiwat = sb->sb_mbmax = 0; 406207619Srdivacky} 407207619Srdivacky 408207619Srdivacky/* 409207619Srdivacky * Routines to add and remove 410193326Sed * data from an mbuf queue. 411207619Srdivacky * 412207619Srdivacky * The routines sbappend() or sbappendrecord() are normally called to 413207619Srdivacky * append new mbufs to a socket buffer, after checking that adequate 414198092Srdivacky * space is available, comparing the function sbspace() with the amount 415207619Srdivacky * of data to be added. sbappendrecord() differs from sbappend() in 416207619Srdivacky * that data supplied is treated as the beginning of a new record. 417207619Srdivacky * To place a sender's address, optional access rights, and data in a 418207619Srdivacky * socket receive buffer, sbappendaddr() should be used. To place 419207619Srdivacky * access rights and data in a socket receive buffer, sbappendrights() 420198092Srdivacky * should be used. In either case, the new data begins a new record. 421207619Srdivacky * Note that unlike sbappend() and sbappendrecord(), these routines check 422207619Srdivacky * for the caller that there will be enough space to store the data. 423207619Srdivacky * Each fails if there is not enough space, or if it cannot find mbufs 424207619Srdivacky * to store additional information in. 425207619Srdivacky * 426207619Srdivacky * Reliable protocols may use the socket send buffer to hold data 427204962Srdivacky * awaiting acknowledgement. Data is normally copied from a socket 428207619Srdivacky * send buffer in a protocol with m_copy for output to a peer, 429207619Srdivacky * and then removing the data from the socket buffer with sbdrop() 430207619Srdivacky * or sbdroprecord() when the data is acknowledged by the peer. 431198092Srdivacky */ 432207619Srdivacky 433207619Srdivacky#ifdef SOCKBUF_DEBUG 434207619Srdivackyvoid 435207619Srdivackysblastrecordchk(struct sockbuf *sb, const char *where) 436193326Sed{ 437218893Sdim struct mbuf *m = sb->sb_mb; 438218893Sdim 439218893Sdim while (m && m->m_nextpkt) 440207619Srdivacky m = m->m_nextpkt; 441207619Srdivacky 442207619Srdivacky if (m != sb->sb_lastrecord) { 443193326Sed printf("sblastrecordchk: sb_mb %p sb_lastrecord %p last %p\n", 444207619Srdivacky sb->sb_mb, sb->sb_lastrecord, m); 445207619Srdivacky printf("packet chain:\n"); 446207619Srdivacky for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) 447193326Sed printf("\t%p\n", m); 448218893Sdim panic("sblastrecordchk from %s", where); 449207619Srdivacky } 450207619Srdivacky} 451207619Srdivacky 452207619Srdivackyvoid 453207619Srdivackysblastmbufchk(struct sockbuf *sb, const char *where) 454218893Sdim{ 455207619Srdivacky struct mbuf *m = sb->sb_mb; 456207619Srdivacky struct mbuf *n; 457207619Srdivacky 458218893Sdim while (m && m->m_nextpkt) 459207619Srdivacky m = m->m_nextpkt; 460207619Srdivacky 461207619Srdivacky while (m && m->m_next) 462218893Sdim m = m->m_next; 463207619Srdivacky 464207619Srdivacky if (m != sb->sb_mbtail) { 465207619Srdivacky printf("sblastmbufchk: sb_mb %p sb_mbtail %p last %p\n", 466218893Sdim sb->sb_mb, sb->sb_mbtail, m); 467207619Srdivacky printf("packet tree:\n"); 468207619Srdivacky for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) { 469207619Srdivacky printf("\t"); 470218893Sdim for (n = m; n != NULL; n = n->m_next) 471207619Srdivacky printf("%p ", n); 472207619Srdivacky printf("\n"); 473207619Srdivacky } 474198092Srdivacky panic("sblastmbufchk from %s", where); 475207619Srdivacky } 476207619Srdivacky} 477207619Srdivacky#endif /* SOCKBUF_DEBUG */ 478207619Srdivacky 479207619Srdivacky#define SBLINKRECORD(sb, m0) \ 480207619Srdivackydo { \ 481207619Srdivacky if ((sb)->sb_lastrecord != NULL) \ 482207619Srdivacky (sb)->sb_lastrecord->m_nextpkt = (m0); \ 483207619Srdivacky else \ 484207619Srdivacky (sb)->sb_mb = (m0); \ 485207619Srdivacky (sb)->sb_lastrecord = (m0); \ 486193326Sed} while (/*CONSTCOND*/0) 487207619Srdivacky 488207619Srdivacky/* 489207619Srdivacky * Append mbuf chain m to the last record in the 490207619Srdivacky * socket buffer sb. The additional space associated 491207619Srdivacky * the mbuf chain is recorded in sb. Empty mbufs are 492207619Srdivacky * discarded and mbufs are compacted where possible. 493207619Srdivacky */ 494207619Srdivackyvoid 495207619Srdivackysbappend(struct sockbuf *sb, struct mbuf *m) 496207619Srdivacky{ 497207619Srdivacky struct mbuf *n; 498193326Sed 499207619Srdivacky if (m == 0) 500207619Srdivacky return; 501207619Srdivacky 502207619Srdivacky#ifdef MBUFTRACE 503207619Srdivacky m_claim(m, sb->sb_mowner); 504207619Srdivacky#endif 505218893Sdim 506218893Sdim SBLASTRECORDCHK(sb, "sbappend 1"); 507218893Sdim 508218893Sdim if ((n = sb->sb_lastrecord) != NULL) { 509207619Srdivacky /* 510207619Srdivacky * XXX Would like to simply use sb_mbtail here, but 511207619Srdivacky * XXX I need to verify that I won't miss an EOR that 512207619Srdivacky * XXX way. 513207619Srdivacky */ 514207619Srdivacky do { 515207619Srdivacky if (n->m_flags & M_EOR) { 516207619Srdivacky sbappendrecord(sb, m); /* XXXXXX!!!! */ 517207619Srdivacky return; 518207619Srdivacky } 519207619Srdivacky } while (n->m_next && (n = n->m_next)); 520207619Srdivacky } else { 521207619Srdivacky /* 522207619Srdivacky * If this is the first record in the socket buffer, it's 523207619Srdivacky * also the last record. 524207619Srdivacky */ 525207619Srdivacky sb->sb_lastrecord = m; 526218893Sdim } 527218893Sdim sbcompress(sb, m, n); 528207619Srdivacky SBLASTRECORDCHK(sb, "sbappend 2"); 529207619Srdivacky} 530207619Srdivacky 531207619Srdivacky/* 532207619Srdivacky * This version of sbappend() should only be used when the caller 533218893Sdim * absolutely knows that there will never be more than one record 534207619Srdivacky * in the socket buffer, that is, a stream protocol (such as TCP). 535207619Srdivacky */ 536207619Srdivackyvoid 537198092Srdivackysbappendstream(struct sockbuf *sb, struct mbuf *m) 538207619Srdivacky{ 539207619Srdivacky 540207619Srdivacky KDASSERT(m->m_nextpkt == NULL); 541207619Srdivacky KASSERT(sb->sb_mb == sb->sb_lastrecord); 542207619Srdivacky 543207619Srdivacky SBLASTMBUFCHK(sb, __func__); 544218893Sdim 545218893Sdim#ifdef MBUFTRACE 546218893Sdim m_claim(m, sb->sb_mowner); 547218893Sdim#endif 548207619Srdivacky 549207619Srdivacky sbcompress(sb, m, sb->sb_mbtail); 550207619Srdivacky 551207619Srdivacky sb->sb_lastrecord = sb->sb_mb; 552207619Srdivacky SBLASTRECORDCHK(sb, __func__); 553207619Srdivacky} 554207619Srdivacky 555207619Srdivacky#ifdef SOCKBUF_DEBUG 556207619Srdivackyvoid 557207619Srdivackysbcheck(struct sockbuf *sb) 558207619Srdivacky{ 559207619Srdivacky struct mbuf *m; 560207619Srdivacky u_long len, mbcnt; 561207619Srdivacky 562207619Srdivacky len = 0; 563207619Srdivacky mbcnt = 0; 564218893Sdim for (m = sb->sb_mb; m; m = m->m_next) { 565207619Srdivacky len += m->m_len; 566207619Srdivacky mbcnt += MSIZE; 567207619Srdivacky if (m->m_flags & M_EXT) 568218893Sdim mbcnt += m->m_ext.ext_size; 569207619Srdivacky if (m->m_nextpkt) 570207619Srdivacky panic("sbcheck nextpkt"); 571207619Srdivacky } 572198092Srdivacky if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) { 573207619Srdivacky printf("cc %lu != %lu || mbcnt %lu != %lu\n", len, sb->sb_cc, 574207619Srdivacky mbcnt, sb->sb_mbcnt); 575207619Srdivacky panic("sbcheck"); 576207619Srdivacky } 577207619Srdivacky} 578207619Srdivacky#endif 579218893Sdim 580218893Sdim/* 581218893Sdim * As above, except the mbuf chain 582218893Sdim * begins a new record. 583207619Srdivacky */ 584207619Srdivackyvoid 585207619Srdivackysbappendrecord(struct sockbuf *sb, struct mbuf *m0) 586207619Srdivacky{ 587207619Srdivacky struct mbuf *m; 588207619Srdivacky 589207619Srdivacky if (m0 == 0) 590207619Srdivacky return; 591207619Srdivacky 592207619Srdivacky#ifdef MBUFTRACE 593207619Srdivacky m_claim(m0, sb->sb_mowner); 594207619Srdivacky#endif 595207619Srdivacky /* 596207619Srdivacky * Put the first mbuf on the queue. 597207619Srdivacky * Note this permits zero length records. 598207619Srdivacky */ 599218893Sdim sballoc(sb, m0); 600207619Srdivacky SBLASTRECORDCHK(sb, "sbappendrecord 1"); 601207619Srdivacky SBLINKRECORD(sb, m0); 602207619Srdivacky m = m0->m_next; 603218893Sdim m0->m_next = 0; 604207619Srdivacky if (m && (m0->m_flags & M_EOR)) { 605207619Srdivacky m0->m_flags &= ~M_EOR; 606207619Srdivacky m->m_flags |= M_EOR; 607198092Srdivacky } 608207619Srdivacky sbcompress(sb, m, m0); 609207619Srdivacky SBLASTRECORDCHK(sb, "sbappendrecord 2"); 610207619Srdivacky} 611207619Srdivacky 612207619Srdivacky/* 613207619Srdivacky * As above except that OOB data 614207619Srdivacky * is inserted at the beginning of the sockbuf, 615207619Srdivacky * but after any other OOB data. 616198092Srdivacky */ 617207619Srdivackyvoid 618207619Srdivackysbinsertoob(struct sockbuf *sb, struct mbuf *m0) 619207619Srdivacky{ 620207619Srdivacky struct mbuf *m, **mp; 621218893Sdim 622218893Sdim if (m0 == 0) 623218893Sdim return; 624207619Srdivacky 625207619Srdivacky SBLASTRECORDCHK(sb, "sbinsertoob 1"); 626207619Srdivacky 627207619Srdivacky for (mp = &sb->sb_mb; (m = *mp) != NULL; mp = &((*mp)->m_nextpkt)) { 628198092Srdivacky again: 629207619Srdivacky switch (m->m_type) { 630207619Srdivacky 631207619Srdivacky case MT_OOBDATA: 632207619Srdivacky continue; /* WANT next train */ 633207619Srdivacky 634193326Sed case MT_CONTROL: 635198092Srdivacky if ((m = m->m_next) != NULL) 636207619Srdivacky goto again; /* inspect THIS train further */ 637207619Srdivacky } 638207619Srdivacky break; 639207619Srdivacky } 640207619Srdivacky /* 641207619Srdivacky * Put the first mbuf on the queue. 642207619Srdivacky * Note this permits zero length records. 643198092Srdivacky */ 644207619Srdivacky sballoc(sb, m0); 645207619Srdivacky m0->m_nextpkt = *mp; 646207619Srdivacky if (*mp == NULL) { 647207619Srdivacky /* m0 is actually the new tail */ 648207619Srdivacky sb->sb_lastrecord = m0; 649198092Srdivacky } 650207619Srdivacky *mp = m0; 651207619Srdivacky m = m0->m_next; 652207619Srdivacky m0->m_next = 0; 653207619Srdivacky if (m && (m0->m_flags & M_EOR)) { 654207619Srdivacky m0->m_flags &= ~M_EOR; 655207619Srdivacky m->m_flags |= M_EOR; 656207619Srdivacky } 657207619Srdivacky sbcompress(sb, m, m0); 658207619Srdivacky SBLASTRECORDCHK(sb, "sbinsertoob 2"); 659207619Srdivacky} 660207619Srdivacky 661207619Srdivacky/* 662204962Srdivacky * Append address and data, and optionally, control (ancillary) data 663207619Srdivacky * to the receive queue of a socket. If present, 664207619Srdivacky * m0 must include a packet header with total length. 665204962Srdivacky * Returns 0 if no space in sockbuf or insufficient mbufs. 666207619Srdivacky */ 667207619Srdivackyint 668207619Srdivackysbappendaddr(struct sockbuf *sb, struct sockaddr *asa, struct mbuf *m0, 669207619Srdivacky struct mbuf *control) 670207619Srdivacky{ 671207619Srdivacky struct mbuf *m, *n, *nlast; 672207619Srdivacky int space; 673204962Srdivacky 674207619Srdivacky space = asa->sa_len; 675207619Srdivacky 676207619Srdivacky if (m0 != NULL) { 677207619Srdivacky if ((m0->m_flags & M_PKTHDR) == 0) 678204962Srdivacky panic("sbappendaddr"); 679207619Srdivacky space += m0->m_pkthdr.len; 680207619Srdivacky#ifdef MBUFTRACE 681207619Srdivacky m_claim(m0, sb->sb_mowner); 682207619Srdivacky#endif 683207619Srdivacky } 684198092Srdivacky for (n = control; n; n = n->m_next) { 685207619Srdivacky space += n->m_len; 686193326Sed MCLAIM(n, sb->sb_mowner); 687198092Srdivacky if (n->m_next == 0) /* keep pointer to last control buf */ 688207619Srdivacky break; 689207619Srdivacky } 690207619Srdivacky if (space > sbspace(sb)) 691207619Srdivacky return (0); 692207619Srdivacky MGET(m, M_DONTWAIT, MT_SONAME); 693207619Srdivacky if (m == 0) 694207619Srdivacky return (0); 695207619Srdivacky MCLAIM(m, sb->sb_mowner); 696207619Srdivacky if (asa->sa_len > MLEN) { 697207619Srdivacky MEXTMALLOC(m, asa->sa_len, M_NOWAIT); 698207619Srdivacky if ((m->m_flags & M_EXT) == 0) { 699207619Srdivacky m_free(m); 700207619Srdivacky return (0); 701207619Srdivacky } 702207619Srdivacky } 703207619Srdivacky m->m_len = asa->sa_len; 704207619Srdivacky memcpy(mtod(m, caddr_t), (caddr_t)asa, asa->sa_len); 705207619Srdivacky if (n) 706207619Srdivacky n->m_next = m0; /* concatenate data to control */ 707207619Srdivacky else 708207619Srdivacky control = m0; 709207619Srdivacky m->m_next = control; 710207619Srdivacky 711207619Srdivacky SBLASTRECORDCHK(sb, "sbappendaddr 1"); 712207619Srdivacky 713207619Srdivacky for (n = m; n->m_next != NULL; n = n->m_next) 714207619Srdivacky sballoc(sb, n); 715207619Srdivacky sballoc(sb, n); 716207619Srdivacky nlast = n; 717207619Srdivacky SBLINKRECORD(sb, m); 718207619Srdivacky 719207619Srdivacky sb->sb_mbtail = nlast; 720207619Srdivacky SBLASTMBUFCHK(sb, "sbappendaddr"); 721207619Srdivacky 722207619Srdivacky SBLASTRECORDCHK(sb, "sbappendaddr 2"); 723207619Srdivacky 724207619Srdivacky return (1); 725207619Srdivacky} 726207619Srdivacky 727207619Srdivackyint 728207619Srdivackysbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control) 729207619Srdivacky{ 730207619Srdivacky struct mbuf *m, *mlast, *n; 731207619Srdivacky int space; 732207619Srdivacky 733207619Srdivacky space = 0; 734207619Srdivacky if (control == 0) 735207619Srdivacky panic("sbappendcontrol"); 736207619Srdivacky for (m = control; ; m = m->m_next) { 737207619Srdivacky space += m->m_len; 738207619Srdivacky MCLAIM(m, sb->sb_mowner); 739207619Srdivacky if (m->m_next == 0) 740207619Srdivacky break; 741207619Srdivacky } 742207619Srdivacky n = m; /* save pointer to last control buffer */ 743207619Srdivacky for (m = m0; m; m = m->m_next) { 744207619Srdivacky MCLAIM(m, sb->sb_mowner); 745207619Srdivacky space += m->m_len; 746193326Sed } 747207619Srdivacky if (space > sbspace(sb)) 748207619Srdivacky return (0); 749207619Srdivacky n->m_next = m0; /* concatenate data to control */ 750218893Sdim 751218893Sdim SBLASTRECORDCHK(sb, "sbappendcontrol 1"); 752193326Sed 753218893Sdim for (m = control; m->m_next != NULL; m = m->m_next) 754218893Sdim sballoc(sb, m); 755207619Srdivacky sballoc(sb, m); 756198092Srdivacky mlast = m; 757193326Sed SBLINKRECORD(sb, control); 758193326Sed 759193326Sed sb->sb_mbtail = mlast; 760207619Srdivacky SBLASTMBUFCHK(sb, "sbappendcontrol"); 761193326Sed 762193326Sed SBLASTRECORDCHK(sb, "sbappendcontrol 2"); 763193326Sed 764207619Srdivacky return (1); 765193326Sed} 766193326Sed 767193326Sed/* 768193326Sed * Compress mbuf chain m into the socket 769207619Srdivacky * buffer sb following mbuf n. If n 770193326Sed * is null, the buffer is presumed empty. 771198092Srdivacky */ 772207619Srdivackyvoid 773207619Srdivackysbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n) 774218893Sdim{ 775193326Sed int eor; 776193326Sed struct mbuf *o; 777207619Srdivacky 778207619Srdivacky eor = 0; 779193326Sed while (m) { 780218893Sdim eor |= m->m_flags & M_EOR; 781207619Srdivacky if (m->m_len == 0 && 782193326Sed (eor == 0 || 783193326Sed (((o = m->m_next) || (o = n)) && 784193326Sed o->m_type == m->m_type))) { 785193326Sed if (sb->sb_lastrecord == m) 786193326Sed sb->sb_lastrecord = m->m_next; 787193326Sed m = m_free(m); 788198092Srdivacky continue; 789193326Sed } 790218893Sdim if (n && (n->m_flags & M_EOR) == 0 && 791198092Srdivacky /* M_TRAILINGSPACE() checks buffer writeability */ 792193326Sed m->m_len <= MCLBYTES / 4 && /* XXX Don't copy too much */ 793193326Sed m->m_len <= M_TRAILINGSPACE(n) && 794198092Srdivacky n->m_type == m->m_type) { 795218893Sdim memcpy(mtod(n, caddr_t) + n->m_len, mtod(m, caddr_t), 796218893Sdim (unsigned)m->m_len); 797218893Sdim n->m_len += m->m_len; 798193326Sed sb->sb_cc += m->m_len; 799218893Sdim m = m_free(m); 800218893Sdim continue; 801218893Sdim } 802218893Sdim if (n) 803218893Sdim n->m_next = m; 804218893Sdim else 805193326Sed sb->sb_mb = m; 806218893Sdim sb->sb_mbtail = m; 807218893Sdim sballoc(sb, m); 808193326Sed n = m; 809193326Sed m->m_flags &= ~M_EOR; 810198092Srdivacky m = m->m_next; 811198092Srdivacky n->m_next = 0; 812198092Srdivacky } 813198092Srdivacky if (eor) { 814198092Srdivacky if (n) 815198092Srdivacky n->m_flags |= eor; 816198092Srdivacky else 817198092Srdivacky printf("semi-panic: sbcompress\n"); 818198092Srdivacky } 819198092Srdivacky SBLASTMBUFCHK(sb, __func__); 820198092Srdivacky} 821198092Srdivacky 822198092Srdivacky/* 823218893Sdim * Free all mbufs in a sockbuf. 824218893Sdim * Check that all resources are reclaimed. 825218893Sdim */ 826198092Srdivackyvoid 827198092Srdivackysbflush(struct sockbuf *sb) 828198092Srdivacky{ 829198092Srdivacky 830198092Srdivacky KASSERT((sb->sb_flags & SB_LOCK) == 0); 831198092Srdivacky 832198092Srdivacky while (sb->sb_mbcnt) 833198092Srdivacky sbdrop(sb, (int)sb->sb_cc); 834198092Srdivacky 835198092Srdivacky KASSERT(sb->sb_cc == 0); 836198092Srdivacky KASSERT(sb->sb_mb == NULL); 837198092Srdivacky KASSERT(sb->sb_mbtail == NULL); 838198092Srdivacky KASSERT(sb->sb_lastrecord == NULL); 839198092Srdivacky} 840198092Srdivacky 841198092Srdivacky/* 842218893Sdim * Drop data from (the front of) a sockbuf. 843198092Srdivacky */ 844198092Srdivackyvoid 845198092Srdivackysbdrop(struct sockbuf *sb, int len) 846218893Sdim{ 847198092Srdivacky struct mbuf *m, *mn, *next; 848198092Srdivacky 849198092Srdivacky next = (m = sb->sb_mb) ? m->m_nextpkt : 0; 850198092Srdivacky while (len > 0) { 851198092Srdivacky if (m == 0) { 852198092Srdivacky if (next == 0) 853198092Srdivacky panic("sbdrop"); 854218893Sdim m = next; 855198092Srdivacky next = m->m_nextpkt; 856198092Srdivacky continue; 857193326Sed } 858193326Sed if (m->m_len > len) { 859193326Sed m->m_len -= len; 860 m->m_data += len; 861 sb->sb_cc -= len; 862 break; 863 } 864 len -= m->m_len; 865 sbfree(sb, m); 866 MFREE(m, mn); 867 m = mn; 868 } 869 while (m && m->m_len == 0) { 870 sbfree(sb, m); 871 MFREE(m, mn); 872 m = mn; 873 } 874 if (m) { 875 sb->sb_mb = m; 876 m->m_nextpkt = next; 877 } else 878 sb->sb_mb = next; 879 /* 880 * First part is an inline SB_EMPTY_FIXUP(). Second part 881 * makes sure sb_lastrecord is up-to-date if we dropped 882 * part of the last record. 883 */ 884 m = sb->sb_mb; 885 if (m == NULL) { 886 sb->sb_mbtail = NULL; 887 sb->sb_lastrecord = NULL; 888 } else if (m->m_nextpkt == NULL) 889 sb->sb_lastrecord = m; 890} 891 892/* 893 * Drop a record off the front of a sockbuf 894 * and move the next record to the front. 895 */ 896void 897sbdroprecord(struct sockbuf *sb) 898{ 899 struct mbuf *m, *mn; 900 901 m = sb->sb_mb; 902 if (m) { 903 sb->sb_mb = m->m_nextpkt; 904 do { 905 sbfree(sb, m); 906 MFREE(m, mn); 907 } while ((m = mn) != NULL); 908 } 909 SB_EMPTY_FIXUP(sb); 910} 911 912/* 913 * Create a "control" mbuf containing the specified data 914 * with the specified type for presentation on a socket buffer. 915 */ 916struct mbuf * 917sbcreatecontrol(caddr_t p, int size, int type, int level) 918{ 919 struct cmsghdr *cp; 920 struct mbuf *m; 921 922 if (CMSG_SPACE(size) > MCLBYTES) { 923 printf("sbcreatecontrol: message too large %d\n", size); 924 return NULL; 925 } 926 927 if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL) 928 return ((struct mbuf *) NULL); 929 if (CMSG_SPACE(size) > MLEN) { 930 MCLGET(m, M_DONTWAIT); 931 if ((m->m_flags & M_EXT) == 0) { 932 m_free(m); 933 return NULL; 934 } 935 } 936 cp = mtod(m, struct cmsghdr *); 937 memcpy(CMSG_DATA(cp), p, size); 938 m->m_len = CMSG_SPACE(size); 939 cp->cmsg_len = CMSG_LEN(size); 940 cp->cmsg_level = level; 941 cp->cmsg_type = type; 942 return (m); 943} 944