Home | History | Annotate | Line # | Download | only in dev
kttcp.c revision 1.14
      1  1.14     lukem /*	$NetBSD: kttcp.c,v 1.14 2003/07/14 15:47:04 lukem Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*
      4   1.1   thorpej  * Copyright (c) 2002 Wasabi Systems, Inc.
      5   1.1   thorpej  * All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * Written by Frank van der Linden and Jason R. Thorpe for
      8   1.1   thorpej  * Wasabi Systems, Inc.
      9   1.1   thorpej  *
     10   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     11   1.1   thorpej  * modification, are permitted provided that the following conditions
     12   1.1   thorpej  * are met:
     13   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     14   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     15   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     18   1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     19   1.1   thorpej  *    must display the following acknowledgement:
     20   1.1   thorpej  *	This product includes software developed for the NetBSD Project by
     21   1.1   thorpej  *	Wasabi Systems, Inc.
     22   1.1   thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     23   1.1   thorpej  *    or promote products derived from this software without specific prior
     24   1.1   thorpej  *    written permission.
     25   1.1   thorpej  *
     26   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     27   1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     30   1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1   thorpej  */
     38   1.1   thorpej 
     39   1.1   thorpej /*
     40   1.1   thorpej  * kttcp.c --
     41   1.1   thorpej  *
     42   1.1   thorpej  *	This module provides kernel support for testing network
     43   1.1   thorpej  *	throughput from the perspective of the kernel.  It is
     44   1.1   thorpej  *	similar in spirit to the classic ttcp network benchmark
     45   1.1   thorpej  *	program, the main difference being that with kttcp, the
     46   1.1   thorpej  *	kernel is the source and sink of the data.
     47   1.1   thorpej  *
     48   1.1   thorpej  *	Testing like this is useful for a few reasons:
     49   1.1   thorpej  *
     50   1.1   thorpej  *	1. This allows us to know what kind of performance we can
     51   1.1   thorpej  *	   expect from network applications that run in the kernel
     52   1.1   thorpej  *	   space, such as the NFS server or the NFS client.  These
     53   1.1   thorpej  *	   applications don't have to move the data to/from userspace,
     54   1.1   thorpej  *	   and so benchmark programs which run in userspace don't
     55   1.1   thorpej  *	   give us an accurate model.
     56   1.1   thorpej  *
     57   1.1   thorpej  *	2. Since data received is just thrown away, the receiver
     58   1.1   thorpej  *	   is very fast.  This can provide better exercise for the
     59   1.1   thorpej  *	   sender at the other end.
     60   1.1   thorpej  *
     61   1.1   thorpej  *	3. Since the NetBSD kernel currently uses a run-to-completion
     62   1.1   thorpej  *	   scheduling model, kttcp provides a benchmark model where
     63   1.1   thorpej  *	   preemption of the benchmark program is not an issue.
     64   1.1   thorpej  */
     65  1.14     lukem 
     66  1.14     lukem #include <sys/cdefs.h>
     67  1.14     lukem __KERNEL_RCSID(0, "$NetBSD: kttcp.c,v 1.14 2003/07/14 15:47:04 lukem Exp $");
     68   1.1   thorpej 
     69   1.1   thorpej #include <sys/param.h>
     70   1.1   thorpej #include <sys/types.h>
     71   1.1   thorpej #include <sys/ioctl.h>
     72   1.1   thorpej #include <sys/file.h>
     73   1.1   thorpej #include <sys/filedesc.h>
     74   1.1   thorpej #include <sys/conf.h>
     75   1.1   thorpej #include <sys/systm.h>
     76   1.1   thorpej #include <sys/protosw.h>
     77   1.1   thorpej #include <sys/proc.h>
     78   1.1   thorpej #include <sys/resourcevar.h>
     79   1.1   thorpej #include <sys/signal.h>
     80   1.1   thorpej #include <sys/socketvar.h>
     81   1.1   thorpej #include <sys/socket.h>
     82   1.1   thorpej #include <sys/mbuf.h>
     83   1.7   thorpej #include <sys/sa.h>
     84   1.1   thorpej #include <sys/mount.h>
     85   1.1   thorpej #include <sys/syscallargs.h>
     86   1.1   thorpej 
     87   1.1   thorpej #include <dev/kttcpio.h>
     88   1.1   thorpej 
     89  1.13      fvdl static int kttcp_send(struct proc *p, struct kttcp_io_args *);
     90  1.13      fvdl static int kttcp_recv(struct proc *p, struct kttcp_io_args *);
     91   1.1   thorpej static int kttcp_sosend(struct socket *, unsigned long long,
     92  1.13      fvdl 			unsigned long long *, struct proc *, int);
     93   1.1   thorpej static int kttcp_soreceive(struct socket *, unsigned long long,
     94  1.13      fvdl 			   unsigned long long *, struct proc *, int *);
     95   1.1   thorpej 
     96   1.1   thorpej void	kttcpattach(int);
     97   1.1   thorpej 
     98   1.5   gehenna dev_type_ioctl(kttcpioctl);
     99   1.5   gehenna 
    100   1.5   gehenna const struct cdevsw kttcp_cdevsw = {
    101   1.5   gehenna 	nullopen, nullclose, noread, nowrite, kttcpioctl,
    102   1.6  jdolecek 	nostop, notty, nopoll, nommap, nokqfilter,
    103   1.5   gehenna };
    104   1.1   thorpej 
    105   1.1   thorpej void
    106   1.1   thorpej kttcpattach(int count)
    107   1.1   thorpej {
    108   1.1   thorpej 	/* Do nothing. */
    109   1.1   thorpej }
    110   1.1   thorpej 
    111   1.1   thorpej int
    112  1.13      fvdl kttcpioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    113   1.1   thorpej {
    114   1.1   thorpej 	int error;
    115   1.1   thorpej 
    116   1.1   thorpej 	if ((flag & FWRITE) == 0)
    117   1.1   thorpej 		return EPERM;
    118   1.1   thorpej 
    119   1.1   thorpej 	switch (cmd) {
    120   1.1   thorpej 	case KTTCP_IO_SEND:
    121  1.13      fvdl 		error = kttcp_send(p, (struct kttcp_io_args *) data);
    122   1.1   thorpej 		break;
    123   1.1   thorpej 
    124   1.1   thorpej 	case KTTCP_IO_RECV:
    125  1.13      fvdl 		error = kttcp_recv(p, (struct kttcp_io_args *) data);
    126   1.1   thorpej 		break;
    127   1.1   thorpej 
    128   1.1   thorpej 	default:
    129   1.1   thorpej 		return EINVAL;
    130   1.1   thorpej 	}
    131   1.1   thorpej 
    132   1.1   thorpej 	return error;
    133   1.1   thorpej }
    134   1.1   thorpej 
    135   1.1   thorpej static int
    136  1.13      fvdl kttcp_send(struct proc *p, struct kttcp_io_args *kio)
    137   1.1   thorpej {
    138   1.1   thorpej 	struct file *fp;
    139   1.1   thorpej 	int error;
    140   1.1   thorpej 	struct timeval t0, t1;
    141   1.1   thorpej 	unsigned long long len, done;
    142   1.1   thorpej 
    143   1.1   thorpej 	if (kio->kio_totalsize >= KTTCP_MAX_XMIT)
    144   1.1   thorpej 		return EINVAL;
    145   1.1   thorpej 
    146  1.13      fvdl 	fp = fd_getfile(p->p_fd, kio->kio_socket);
    147   1.1   thorpej 	if (fp == NULL)
    148   1.1   thorpej 		return EBADF;
    149   1.8    briggs 	FILE_USE(fp);
    150   1.8    briggs 	if (fp->f_type != DTYPE_SOCKET) {
    151  1.13      fvdl 		FILE_UNUSE(fp, p);
    152   1.1   thorpej 		return EFTYPE;
    153   1.8    briggs 	}
    154   1.1   thorpej 
    155   1.1   thorpej 	len = kio->kio_totalsize;
    156   1.1   thorpej 	microtime(&t0);
    157   1.1   thorpej 	do {
    158   1.1   thorpej 		error = kttcp_sosend((struct socket *)fp->f_data, len,
    159  1.13      fvdl 		    &done, p, 0);
    160   1.1   thorpej 		len -= done;
    161   1.1   thorpej 	} while (error == 0 && len > 0);
    162   1.8    briggs 
    163  1.13      fvdl 	FILE_UNUSE(fp, p);
    164   1.8    briggs 
    165   1.1   thorpej 	microtime(&t1);
    166   1.1   thorpej 	if (error != 0)
    167   1.1   thorpej 		return error;
    168   1.1   thorpej 	timersub(&t1, &t0, &kio->kio_elapsed);
    169   1.1   thorpej 
    170   1.1   thorpej 	kio->kio_bytesdone = kio->kio_totalsize - len;
    171   1.1   thorpej 
    172   1.1   thorpej 	return 0;
    173   1.1   thorpej }
    174   1.1   thorpej 
    175   1.1   thorpej static int
    176  1.13      fvdl kttcp_recv(struct proc *p, struct kttcp_io_args *kio)
    177   1.1   thorpej {
    178   1.1   thorpej 	struct file *fp;
    179   1.1   thorpej 	int error;
    180   1.1   thorpej 	struct timeval t0, t1;
    181   1.1   thorpej 	unsigned long long len, done;
    182   1.1   thorpej 
    183   1.1   thorpej 	if (kio->kio_totalsize > KTTCP_MAX_XMIT)
    184   1.1   thorpej 		return EINVAL;
    185   1.1   thorpej 
    186  1.13      fvdl 	fp = fd_getfile(p->p_fd, kio->kio_socket);
    187   1.8    briggs 	if (fp == NULL)
    188   1.1   thorpej 		return EBADF;
    189   1.8    briggs 	FILE_USE(fp);
    190   1.8    briggs 	if (fp->f_type != DTYPE_SOCKET) {
    191  1.13      fvdl 		FILE_UNUSE(fp, p);
    192   1.8    briggs 		return EBADF;
    193   1.8    briggs 	}
    194   1.1   thorpej 	len = kio->kio_totalsize;
    195   1.1   thorpej 	microtime(&t0);
    196   1.1   thorpej 	do {
    197   1.1   thorpej 		error = kttcp_soreceive((struct socket *)fp->f_data,
    198  1.13      fvdl 		    len, &done, p, NULL);
    199   1.1   thorpej 		len -= done;
    200   1.1   thorpej 	} while (error == 0 && len > 0 && done > 0);
    201   1.8    briggs 
    202  1.13      fvdl 	FILE_UNUSE(fp, p);
    203   1.8    briggs 
    204   1.1   thorpej 	microtime(&t1);
    205   1.1   thorpej 	if (error == EPIPE)
    206   1.1   thorpej 		error = 0;
    207   1.1   thorpej 	if (error != 0)
    208   1.1   thorpej 		return error;
    209   1.1   thorpej 	timersub(&t1, &t0, &kio->kio_elapsed);
    210   1.1   thorpej 
    211   1.1   thorpej 	kio->kio_bytesdone = kio->kio_totalsize - len;
    212   1.1   thorpej 
    213   1.1   thorpej 	return 0;
    214   1.1   thorpej }
    215   1.1   thorpej 
    216   1.1   thorpej #define SBLOCKWAIT(f)   (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
    217   1.1   thorpej 
    218   1.1   thorpej /*
    219   1.1   thorpej  * Slightly changed version of sosend()
    220   1.1   thorpej  */
    221   1.1   thorpej static int
    222   1.1   thorpej kttcp_sosend(struct socket *so, unsigned long long slen,
    223  1.13      fvdl 	     unsigned long long *done, struct proc *p, int flags)
    224   1.1   thorpej {
    225   1.1   thorpej 	struct mbuf **mp, *m, *top;
    226   1.1   thorpej 	long space, len, mlen;
    227   1.1   thorpej 	int error, s, dontroute, atomic;
    228   1.1   thorpej 	long long resid;
    229   1.1   thorpej 
    230   1.1   thorpej 	atomic = sosendallatonce(so);
    231   1.1   thorpej 	resid = slen;
    232   1.1   thorpej 	top = NULL;
    233   1.1   thorpej 	/*
    234   1.1   thorpej 	 * In theory resid should be unsigned.
    235   1.1   thorpej 	 * However, space must be signed, as it might be less than 0
    236   1.1   thorpej 	 * if we over-committed, and we must use a signed comparison
    237   1.1   thorpej 	 * of space and resid.  On the other hand, a negative resid
    238   1.1   thorpej 	 * causes us to loop sending 0-length segments to the protocol.
    239   1.1   thorpej 	 */
    240   1.1   thorpej 	if (resid < 0) {
    241   1.1   thorpej 		error = EINVAL;
    242   1.1   thorpej 		goto out;
    243   1.1   thorpej 	}
    244   1.1   thorpej 	dontroute =
    245   1.1   thorpej 	    (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
    246   1.1   thorpej 	    (so->so_proto->pr_flags & PR_ATOMIC);
    247  1.13      fvdl 	p->p_stats->p_ru.ru_msgsnd++;
    248   1.1   thorpej #define	snderr(errno)	{ error = errno; splx(s); goto release; }
    249   1.1   thorpej 
    250   1.1   thorpej  restart:
    251   1.1   thorpej 	if ((error = sblock(&so->so_snd, SBLOCKWAIT(flags))) != 0)
    252   1.1   thorpej 		goto out;
    253   1.1   thorpej 	do {
    254   1.1   thorpej 		s = splsoftnet();
    255   1.1   thorpej 		if (so->so_state & SS_CANTSENDMORE)
    256   1.1   thorpej 			snderr(EPIPE);
    257   1.1   thorpej 		if (so->so_error) {
    258   1.1   thorpej 			error = so->so_error;
    259   1.1   thorpej 			so->so_error = 0;
    260   1.1   thorpej 			splx(s);
    261   1.1   thorpej 			goto release;
    262   1.1   thorpej 		}
    263   1.1   thorpej 		if ((so->so_state & SS_ISCONNECTED) == 0) {
    264   1.1   thorpej 			if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
    265   1.1   thorpej 				if ((so->so_state & SS_ISCONFIRMING) == 0)
    266   1.1   thorpej 					snderr(ENOTCONN);
    267   1.1   thorpej 			} else
    268   1.1   thorpej 				snderr(EDESTADDRREQ);
    269   1.1   thorpej 		}
    270   1.1   thorpej 		space = sbspace(&so->so_snd);
    271   1.1   thorpej 		if (flags & MSG_OOB)
    272   1.1   thorpej 			space += 1024;
    273   1.1   thorpej 		if ((atomic && resid > so->so_snd.sb_hiwat))
    274   1.1   thorpej 			snderr(EMSGSIZE);
    275   1.1   thorpej 		if (space < resid && (atomic || space < so->so_snd.sb_lowat)) {
    276   1.1   thorpej 			if (so->so_state & SS_NBIO)
    277   1.1   thorpej 				snderr(EWOULDBLOCK);
    278   1.2   thorpej 			SBLASTRECORDCHK(&so->so_rcv,
    279   1.2   thorpej 			    "kttcp_soreceive sbwait 1");
    280   1.2   thorpej 			SBLASTMBUFCHK(&so->so_rcv,
    281   1.2   thorpej 			    "kttcp_soreceive sbwait 1");
    282   1.1   thorpej 			sbunlock(&so->so_snd);
    283   1.1   thorpej 			error = sbwait(&so->so_snd);
    284   1.1   thorpej 			splx(s);
    285   1.1   thorpej 			if (error)
    286   1.1   thorpej 				goto out;
    287   1.1   thorpej 			goto restart;
    288   1.1   thorpej 		}
    289   1.1   thorpej 		splx(s);
    290   1.1   thorpej 		mp = &top;
    291   1.1   thorpej 		do {
    292   1.1   thorpej 			do {
    293   1.1   thorpej 				if (top == 0) {
    294   1.9      matt 					m = m_gethdr(M_WAIT, MT_DATA);
    295   1.1   thorpej 					mlen = MHLEN;
    296   1.1   thorpej 					m->m_pkthdr.len = 0;
    297  1.12    simonb 					m->m_pkthdr.rcvif = NULL;
    298   1.1   thorpej 				} else {
    299   1.9      matt 					m = m_get(M_WAIT, MT_DATA);
    300   1.1   thorpej 					mlen = MLEN;
    301   1.1   thorpej 				}
    302   1.1   thorpej 				if (resid >= MINCLSIZE && space >= MCLBYTES) {
    303   1.9      matt 					m_clget(m, M_WAIT);
    304   1.1   thorpej 					if ((m->m_flags & M_EXT) == 0)
    305   1.1   thorpej 						goto nopages;
    306   1.1   thorpej 					mlen = MCLBYTES;
    307   1.1   thorpej #ifdef	MAPPED_MBUFS
    308   1.1   thorpej 					len = lmin(MCLBYTES, resid);
    309   1.1   thorpej #else
    310   1.1   thorpej 					if (atomic && top == 0) {
    311   1.1   thorpej 						len = lmin(MCLBYTES - max_hdr,
    312   1.1   thorpej 						    resid);
    313   1.1   thorpej 						m->m_data += max_hdr;
    314   1.1   thorpej 					} else
    315   1.1   thorpej 						len = lmin(MCLBYTES, resid);
    316   1.1   thorpej #endif
    317   1.1   thorpej 					space -= len;
    318   1.1   thorpej 				} else {
    319   1.1   thorpej nopages:
    320   1.1   thorpej 					len = lmin(lmin(mlen, resid), space);
    321   1.1   thorpej 					space -= len;
    322   1.1   thorpej 					/*
    323   1.1   thorpej 					 * For datagram protocols, leave room
    324   1.1   thorpej 					 * for protocol headers in first mbuf.
    325   1.1   thorpej 					 */
    326   1.1   thorpej 					if (atomic && top == 0 && len < mlen)
    327   1.1   thorpej 						MH_ALIGN(m, len);
    328   1.1   thorpej 				}
    329   1.1   thorpej 				resid -= len;
    330   1.1   thorpej 				m->m_len = len;
    331   1.1   thorpej 				*mp = m;
    332   1.1   thorpej 				top->m_pkthdr.len += len;
    333   1.1   thorpej 				if (error)
    334   1.1   thorpej 					goto release;
    335   1.1   thorpej 				mp = &m->m_next;
    336   1.1   thorpej 				if (resid <= 0) {
    337   1.1   thorpej 					if (flags & MSG_EOR)
    338   1.1   thorpej 						top->m_flags |= M_EOR;
    339   1.1   thorpej 					break;
    340   1.1   thorpej 				}
    341   1.1   thorpej 			} while (space > 0 && atomic);
    342   1.1   thorpej 
    343   1.1   thorpej 			s = splsoftnet();
    344   1.1   thorpej 
    345   1.1   thorpej 			if (so->so_state & SS_CANTSENDMORE)
    346   1.1   thorpej 				snderr(EPIPE);
    347   1.1   thorpej 
    348   1.1   thorpej 			if (dontroute)
    349   1.1   thorpej 				so->so_options |= SO_DONTROUTE;
    350   1.1   thorpej 			if (resid > 0)
    351   1.1   thorpej 				so->so_state |= SS_MORETOCOME;
    352   1.1   thorpej 			error = (*so->so_proto->pr_usrreq)(so,
    353   1.1   thorpej 			    (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,
    354  1.13      fvdl 			    top, NULL, NULL, p);
    355   1.1   thorpej 			if (dontroute)
    356   1.1   thorpej 				so->so_options &= ~SO_DONTROUTE;
    357   1.1   thorpej 			if (resid > 0)
    358   1.1   thorpej 				so->so_state &= ~SS_MORETOCOME;
    359   1.1   thorpej 			splx(s);
    360   1.1   thorpej 
    361   1.1   thorpej 			top = 0;
    362   1.1   thorpej 			mp = &top;
    363   1.1   thorpej 			if (error)
    364   1.1   thorpej 				goto release;
    365   1.1   thorpej 		} while (resid && space > 0);
    366   1.1   thorpej 	} while (resid);
    367   1.1   thorpej 
    368   1.1   thorpej  release:
    369   1.1   thorpej 	sbunlock(&so->so_snd);
    370   1.1   thorpej  out:
    371   1.1   thorpej 	if (top)
    372   1.1   thorpej 		m_freem(top);
    373   1.1   thorpej 	*done = slen - resid;
    374   1.1   thorpej #if 0
    375   1.1   thorpej 	printf("sosend: error %d slen %llu resid %lld\n", error, slen, resid);
    376   1.1   thorpej #endif
    377   1.1   thorpej 	return (error);
    378   1.1   thorpej }
    379   1.1   thorpej 
    380   1.1   thorpej static int
    381   1.1   thorpej kttcp_soreceive(struct socket *so, unsigned long long slen,
    382  1.13      fvdl 		unsigned long long *done, struct proc *p, int *flagsp)
    383   1.1   thorpej {
    384   1.1   thorpej 	struct mbuf *m, **mp;
    385   1.1   thorpej 	int flags, len, error, s, offset, moff, type;
    386   1.1   thorpej 	long long orig_resid, resid;
    387   1.1   thorpej 	struct protosw	*pr;
    388   1.1   thorpej 	struct mbuf *nextrecord;
    389   1.1   thorpej 
    390   1.1   thorpej 	pr = so->so_proto;
    391   1.1   thorpej 	mp = NULL;
    392   1.1   thorpej 	type = 0;
    393   1.1   thorpej 	resid = orig_resid = slen;
    394   1.1   thorpej 	if (flagsp)
    395   1.1   thorpej 		flags = *flagsp &~ MSG_EOR;
    396   1.1   thorpej 	else
    397   1.1   thorpej  		flags = 0;
    398   1.1   thorpej 	if (flags & MSG_OOB) {
    399   1.1   thorpej 		m = m_get(M_WAIT, MT_DATA);
    400   1.1   thorpej 		error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m,
    401  1.12    simonb 		    (struct mbuf *)(long)(flags & MSG_PEEK), NULL, NULL);
    402   1.1   thorpej 		if (error)
    403   1.1   thorpej 			goto bad;
    404   1.1   thorpej 		do {
    405   1.1   thorpej 			resid -= min(resid, m->m_len);
    406   1.1   thorpej 			m = m_free(m);
    407   1.1   thorpej 		} while (resid && error == 0 && m);
    408   1.1   thorpej  bad:
    409   1.1   thorpej 		if (m)
    410   1.1   thorpej 			m_freem(m);
    411   1.1   thorpej 		return (error);
    412   1.1   thorpej 	}
    413   1.1   thorpej 	if (mp)
    414  1.12    simonb 		*mp = NULL;
    415   1.1   thorpej 	if (so->so_state & SS_ISCONFIRMING && resid)
    416  1.12    simonb 		(*pr->pr_usrreq)(so, PRU_RCVD, NULL, NULL, NULL, NULL);
    417   1.1   thorpej 
    418   1.1   thorpej  restart:
    419   1.1   thorpej 	if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0)
    420   1.1   thorpej 		return (error);
    421   1.1   thorpej 	s = splsoftnet();
    422   1.1   thorpej 
    423   1.1   thorpej 	m = so->so_rcv.sb_mb;
    424   1.1   thorpej 	/*
    425   1.1   thorpej 	 * If we have less data than requested, block awaiting more
    426   1.1   thorpej 	 * (subject to any timeout) if:
    427   1.1   thorpej 	 *   1. the current count is less than the low water mark,
    428   1.1   thorpej 	 *   2. MSG_WAITALL is set, and it is possible to do the entire
    429   1.1   thorpej 	 *	receive operation at once if we block (resid <= hiwat), or
    430   1.1   thorpej 	 *   3. MSG_DONTWAIT is not set.
    431   1.1   thorpej 	 * If MSG_WAITALL is set but resid is larger than the receive buffer,
    432   1.1   thorpej 	 * we have to do the receive in sections, and thus risk returning
    433   1.1   thorpej 	 * a short count if a timeout or signal occurs after we start.
    434   1.1   thorpej 	 */
    435  1.13      fvdl 	if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
    436   1.1   thorpej 	    so->so_rcv.sb_cc < resid) &&
    437   1.1   thorpej 	    (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
    438   1.1   thorpej 	    ((flags & MSG_WAITALL) && resid <= so->so_rcv.sb_hiwat)) &&
    439  1.13      fvdl 	    m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) {
    440   1.1   thorpej #ifdef DIAGNOSTIC
    441  1.13      fvdl 		if (m == NULL && so->so_rcv.sb_cc)
    442   1.1   thorpej 			panic("receive 1");
    443   1.1   thorpej #endif
    444   1.1   thorpej 		if (so->so_error) {
    445   1.1   thorpej 			if (m)
    446   1.1   thorpej 				goto dontblock;
    447   1.1   thorpej 			error = so->so_error;
    448   1.1   thorpej 			if ((flags & MSG_PEEK) == 0)
    449   1.1   thorpej 				so->so_error = 0;
    450   1.1   thorpej 			goto release;
    451   1.1   thorpej 		}
    452   1.1   thorpej 		if (so->so_state & SS_CANTRCVMORE) {
    453   1.1   thorpej 			if (m)
    454   1.1   thorpej 				goto dontblock;
    455   1.1   thorpej 			else
    456   1.1   thorpej 				goto release;
    457   1.1   thorpej 		}
    458   1.1   thorpej 		for (; m; m = m->m_next)
    459   1.1   thorpej 			if (m->m_type == MT_OOBDATA  || (m->m_flags & M_EOR)) {
    460   1.1   thorpej 				m = so->so_rcv.sb_mb;
    461   1.1   thorpej 				goto dontblock;
    462   1.1   thorpej 			}
    463   1.1   thorpej 		if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
    464   1.1   thorpej 		    (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
    465   1.1   thorpej 			error = ENOTCONN;
    466   1.1   thorpej 			goto release;
    467   1.1   thorpej 		}
    468   1.1   thorpej 		if (resid == 0)
    469   1.1   thorpej 			goto release;
    470   1.1   thorpej 		if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
    471   1.1   thorpej 			error = EWOULDBLOCK;
    472   1.1   thorpej 			goto release;
    473   1.1   thorpej 		}
    474   1.1   thorpej 		sbunlock(&so->so_rcv);
    475   1.1   thorpej 		error = sbwait(&so->so_rcv);
    476   1.1   thorpej 		splx(s);
    477   1.1   thorpej 		if (error)
    478   1.1   thorpej 			return (error);
    479   1.1   thorpej 		goto restart;
    480   1.1   thorpej 	}
    481   1.1   thorpej  dontblock:
    482   1.2   thorpej 	/*
    483   1.2   thorpej 	 * On entry here, m points to the first record of the socket buffer.
    484   1.2   thorpej 	 * While we process the initial mbufs containing address and control
    485   1.2   thorpej 	 * info, we save a copy of m->m_nextpkt into nextrecord.
    486   1.2   thorpej 	 */
    487   1.1   thorpej #ifdef notyet /* XXXX */
    488  1.13      fvdl 	if (uio->uio_procp)
    489  1.13      fvdl 		uio->uio_procp->p_stats->p_ru.ru_msgrcv++;
    490   1.1   thorpej #endif
    491   1.2   thorpej 	KASSERT(m == so->so_rcv.sb_mb);
    492   1.2   thorpej 	SBLASTRECORDCHK(&so->so_rcv, "kttcp_soreceive 1");
    493   1.2   thorpej 	SBLASTMBUFCHK(&so->so_rcv, "kttcp_soreceive 1");
    494   1.1   thorpej 	nextrecord = m->m_nextpkt;
    495   1.1   thorpej 	if (pr->pr_flags & PR_ADDR) {
    496   1.1   thorpej #ifdef DIAGNOSTIC
    497   1.1   thorpej 		if (m->m_type != MT_SONAME)
    498   1.1   thorpej 			panic("receive 1a");
    499   1.1   thorpej #endif
    500   1.1   thorpej 		orig_resid = 0;
    501   1.1   thorpej 		if (flags & MSG_PEEK) {
    502   1.1   thorpej 			m = m->m_next;
    503   1.1   thorpej 		} else {
    504   1.1   thorpej 			sbfree(&so->so_rcv, m);
    505   1.1   thorpej 			MFREE(m, so->so_rcv.sb_mb);
    506   1.1   thorpej 			m = so->so_rcv.sb_mb;
    507   1.1   thorpej 		}
    508   1.1   thorpej 	}
    509   1.1   thorpej 	while (m && m->m_type == MT_CONTROL && error == 0) {
    510   1.1   thorpej 		if (flags & MSG_PEEK) {
    511   1.1   thorpej 			m = m->m_next;
    512   1.1   thorpej 		} else {
    513   1.1   thorpej 			sbfree(&so->so_rcv, m);
    514   1.1   thorpej 			MFREE(m, so->so_rcv.sb_mb);
    515   1.1   thorpej 			m = so->so_rcv.sb_mb;
    516   1.1   thorpej 		}
    517   1.1   thorpej 	}
    518   1.2   thorpej 
    519   1.2   thorpej 	/*
    520   1.2   thorpej 	 * If m is non-NULL, we have some data to read.  From now on,
    521   1.2   thorpej 	 * make sure to keep sb_lastrecord consistent when working on
    522   1.2   thorpej 	 * the last packet on the chain (nextrecord == NULL) and we
    523   1.2   thorpej 	 * change m->m_nextpkt.
    524   1.2   thorpej 	 */
    525   1.1   thorpej 	if (m) {
    526   1.2   thorpej 		if ((flags & MSG_PEEK) == 0) {
    527   1.1   thorpej 			m->m_nextpkt = nextrecord;
    528   1.2   thorpej 			/*
    529   1.2   thorpej 			 * If nextrecord == NULL (this is a single chain),
    530   1.2   thorpej 			 * then sb_lastrecord may not be valid here if m
    531   1.2   thorpej 			 * was changed earlier.
    532   1.2   thorpej 			 */
    533   1.2   thorpej 			if (nextrecord == NULL) {
    534   1.2   thorpej 				KASSERT(so->so_rcv.sb_mb == m);
    535   1.2   thorpej 				so->so_rcv.sb_lastrecord = m;
    536   1.2   thorpej 			}
    537   1.2   thorpej 		}
    538   1.1   thorpej 		type = m->m_type;
    539   1.1   thorpej 		if (type == MT_OOBDATA)
    540   1.1   thorpej 			flags |= MSG_OOB;
    541   1.2   thorpej 	} else {
    542   1.2   thorpej 		if ((flags & MSG_PEEK) == 0) {
    543   1.2   thorpej 			KASSERT(so->so_rcv.sb_mb == m);
    544   1.2   thorpej 			so->so_rcv.sb_mb = nextrecord;
    545   1.4   thorpej 			SB_EMPTY_FIXUP(&so->so_rcv);
    546   1.2   thorpej 		}
    547   1.1   thorpej 	}
    548   1.2   thorpej 	SBLASTRECORDCHK(&so->so_rcv, "kttcp_soreceive 2");
    549   1.2   thorpej 	SBLASTMBUFCHK(&so->so_rcv, "kttcp_soreceive 2");
    550   1.2   thorpej 
    551   1.1   thorpej 	moff = 0;
    552   1.1   thorpej 	offset = 0;
    553   1.1   thorpej 	while (m && resid > 0 && error == 0) {
    554   1.1   thorpej 		if (m->m_type == MT_OOBDATA) {
    555   1.1   thorpej 			if (type != MT_OOBDATA)
    556   1.1   thorpej 				break;
    557   1.1   thorpej 		} else if (type == MT_OOBDATA)
    558   1.1   thorpej 			break;
    559   1.1   thorpej #ifdef DIAGNOSTIC
    560   1.1   thorpej 		else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
    561   1.1   thorpej 			panic("receive 3");
    562   1.1   thorpej #endif
    563   1.1   thorpej 		so->so_state &= ~SS_RCVATMARK;
    564   1.1   thorpej 		len = resid;
    565   1.1   thorpej 		if (so->so_oobmark && len > so->so_oobmark - offset)
    566   1.1   thorpej 			len = so->so_oobmark - offset;
    567   1.1   thorpej 		if (len > m->m_len - moff)
    568   1.1   thorpej 			len = m->m_len - moff;
    569   1.1   thorpej 		/*
    570   1.1   thorpej 		 * If mp is set, just pass back the mbufs.
    571   1.1   thorpej 		 * Otherwise copy them out via the uio, then free.
    572   1.1   thorpej 		 * Sockbuf must be consistent here (points to current mbuf,
    573   1.1   thorpej 		 * it points to next record) when we drop priority;
    574   1.1   thorpej 		 * we must note any additions to the sockbuf when we
    575   1.1   thorpej 		 * block interrupts again.
    576   1.1   thorpej 		 */
    577   1.1   thorpej 		resid -= len;
    578   1.1   thorpej 		if (len == m->m_len - moff) {
    579   1.1   thorpej 			if (m->m_flags & M_EOR)
    580   1.1   thorpej 				flags |= MSG_EOR;
    581   1.1   thorpej 			if (flags & MSG_PEEK) {
    582   1.1   thorpej 				m = m->m_next;
    583   1.1   thorpej 				moff = 0;
    584   1.1   thorpej 			} else {
    585   1.1   thorpej 				nextrecord = m->m_nextpkt;
    586   1.1   thorpej 				sbfree(&so->so_rcv, m);
    587   1.1   thorpej 				if (mp) {
    588   1.1   thorpej 					*mp = m;
    589   1.1   thorpej 					mp = &m->m_next;
    590   1.1   thorpej 					so->so_rcv.sb_mb = m = m->m_next;
    591  1.12    simonb 					*mp = NULL;
    592   1.1   thorpej 				} else {
    593   1.1   thorpej 					MFREE(m, so->so_rcv.sb_mb);
    594   1.1   thorpej 					m = so->so_rcv.sb_mb;
    595   1.1   thorpej 				}
    596   1.2   thorpej 				/*
    597   1.2   thorpej 				 * If m != NULL, we also know that
    598   1.2   thorpej 				 * so->so_rcv.sb_mb != NULL.
    599   1.2   thorpej 				 */
    600   1.2   thorpej 				KASSERT(so->so_rcv.sb_mb == m);
    601   1.2   thorpej 				if (m) {
    602   1.1   thorpej 					m->m_nextpkt = nextrecord;
    603   1.2   thorpej 					if (nextrecord == NULL)
    604   1.2   thorpej 						so->so_rcv.sb_lastrecord = m;
    605   1.2   thorpej 				} else {
    606   1.2   thorpej 					so->so_rcv.sb_mb = nextrecord;
    607   1.4   thorpej 					SB_EMPTY_FIXUP(&so->so_rcv);
    608   1.2   thorpej 				}
    609   1.2   thorpej 				SBLASTRECORDCHK(&so->so_rcv,
    610   1.2   thorpej 				    "kttcp_soreceive 3");
    611   1.2   thorpej 				SBLASTMBUFCHK(&so->so_rcv,
    612   1.2   thorpej 				    "kttcp_soreceive 3");
    613   1.1   thorpej 			}
    614   1.1   thorpej 		} else {
    615   1.1   thorpej 			if (flags & MSG_PEEK)
    616   1.1   thorpej 				moff += len;
    617   1.1   thorpej 			else {
    618   1.1   thorpej 				if (mp)
    619   1.1   thorpej 					*mp = m_copym(m, 0, len, M_WAIT);
    620   1.1   thorpej 				m->m_data += len;
    621   1.1   thorpej 				m->m_len -= len;
    622   1.1   thorpej 				so->so_rcv.sb_cc -= len;
    623   1.1   thorpej 			}
    624   1.1   thorpej 		}
    625   1.1   thorpej 		if (so->so_oobmark) {
    626   1.1   thorpej 			if ((flags & MSG_PEEK) == 0) {
    627   1.1   thorpej 				so->so_oobmark -= len;
    628   1.1   thorpej 				if (so->so_oobmark == 0) {
    629   1.1   thorpej 					so->so_state |= SS_RCVATMARK;
    630   1.1   thorpej 					break;
    631   1.1   thorpej 				}
    632   1.1   thorpej 			} else {
    633   1.1   thorpej 				offset += len;
    634   1.1   thorpej 				if (offset == so->so_oobmark)
    635   1.1   thorpej 					break;
    636   1.1   thorpej 			}
    637   1.1   thorpej 		}
    638   1.1   thorpej 		if (flags & MSG_EOR)
    639   1.1   thorpej 			break;
    640   1.1   thorpej 		/*
    641   1.1   thorpej 		 * If the MSG_WAITALL flag is set (for non-atomic socket),
    642   1.1   thorpej 		 * we must not quit until "uio->uio_resid == 0" or an error
    643   1.1   thorpej 		 * termination.  If a signal/timeout occurs, return
    644   1.1   thorpej 		 * with a short count but without error.
    645   1.1   thorpej 		 * Keep sockbuf locked against other readers.
    646   1.1   thorpej 		 */
    647  1.13      fvdl 		while (flags & MSG_WAITALL && m == NULL && resid > 0 &&
    648   1.1   thorpej 		    !sosendallatonce(so) && !nextrecord) {
    649   1.1   thorpej 			if (so->so_error || so->so_state & SS_CANTRCVMORE)
    650   1.1   thorpej 				break;
    651   1.3   thorpej 			/*
    652   1.3   thorpej 			 * If we are peeking and the socket receive buffer is
    653   1.3   thorpej 			 * full, stop since we can't get more data to peek at.
    654   1.3   thorpej 			 */
    655   1.3   thorpej 			if ((flags & MSG_PEEK) && sbspace(&so->so_rcv) <= 0)
    656   1.3   thorpej 				break;
    657   1.3   thorpej 			/*
    658   1.3   thorpej 			 * If we've drained the socket buffer, tell the
    659   1.3   thorpej 			 * protocol in case it needs to do something to
    660   1.3   thorpej 			 * get it filled again.
    661   1.3   thorpej 			 */
    662   1.3   thorpej 			if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb)
    663  1.12    simonb 				(*pr->pr_usrreq)(so, PRU_RCVD, NULL,
    664  1.12    simonb 				    (struct mbuf *)(long)flags, NULL, NULL);
    665   1.2   thorpej 			SBLASTRECORDCHK(&so->so_rcv,
    666   1.2   thorpej 			    "kttcp_soreceive sbwait 2");
    667   1.2   thorpej 			SBLASTMBUFCHK(&so->so_rcv,
    668   1.2   thorpej 			    "kttcp_soreceive sbwait 2");
    669   1.1   thorpej 			error = sbwait(&so->so_rcv);
    670   1.1   thorpej 			if (error) {
    671   1.1   thorpej 				sbunlock(&so->so_rcv);
    672   1.1   thorpej 				splx(s);
    673   1.1   thorpej 				return (0);
    674   1.1   thorpej 			}
    675   1.1   thorpej 			if ((m = so->so_rcv.sb_mb) != NULL)
    676   1.1   thorpej 				nextrecord = m->m_nextpkt;
    677   1.1   thorpej 		}
    678   1.1   thorpej 	}
    679   1.1   thorpej 
    680   1.1   thorpej 	if (m && pr->pr_flags & PR_ATOMIC) {
    681   1.1   thorpej 		flags |= MSG_TRUNC;
    682   1.1   thorpej 		if ((flags & MSG_PEEK) == 0)
    683   1.1   thorpej 			(void) sbdroprecord(&so->so_rcv);
    684   1.1   thorpej 	}
    685   1.1   thorpej 	if ((flags & MSG_PEEK) == 0) {
    686  1.13      fvdl 		if (m == NULL) {
    687   1.2   thorpej 			/*
    688   1.4   thorpej 			 * First part is an SB_EMPTY_FIXUP().  Second part
    689   1.2   thorpej 			 * makes sure sb_lastrecord is up-to-date if
    690   1.2   thorpej 			 * there is still data in the socket buffer.
    691   1.2   thorpej 			 */
    692   1.1   thorpej 			so->so_rcv.sb_mb = nextrecord;
    693   1.2   thorpej 			if (so->so_rcv.sb_mb == NULL) {
    694   1.2   thorpej 				so->so_rcv.sb_mbtail = NULL;
    695   1.2   thorpej 				so->so_rcv.sb_lastrecord = NULL;
    696   1.2   thorpej 			} else if (nextrecord->m_nextpkt == NULL)
    697   1.2   thorpej 				so->so_rcv.sb_lastrecord = nextrecord;
    698   1.2   thorpej 		}
    699   1.2   thorpej 		SBLASTRECORDCHK(&so->so_rcv, "kttcp_soreceive 4");
    700   1.2   thorpej 		SBLASTMBUFCHK(&so->so_rcv, "kttcp_soreceive 4");
    701   1.1   thorpej 		if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
    702  1.12    simonb 			(*pr->pr_usrreq)(so, PRU_RCVD, NULL,
    703  1.12    simonb 			    (struct mbuf *)(long)flags, NULL, NULL);
    704   1.1   thorpej 	}
    705   1.1   thorpej 	if (orig_resid == resid && orig_resid &&
    706   1.1   thorpej 	    (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
    707   1.1   thorpej 		sbunlock(&so->so_rcv);
    708   1.1   thorpej 		splx(s);
    709   1.1   thorpej 		goto restart;
    710   1.1   thorpej 	}
    711   1.1   thorpej 
    712   1.1   thorpej 	if (flagsp)
    713   1.1   thorpej 		*flagsp |= flags;
    714   1.1   thorpej  release:
    715   1.1   thorpej 	sbunlock(&so->so_rcv);
    716   1.1   thorpej 	splx(s);
    717   1.1   thorpej 	*done = slen - resid;
    718   1.1   thorpej #if 0
    719   1.1   thorpej 	printf("soreceive: error %d slen %llu resid %lld\n", error, slen, resid);
    720   1.1   thorpej #endif
    721   1.1   thorpej 	return (error);
    722   1.1   thorpej }
    723