Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/sys/kern/uipc_socket.c,v rcsdiff: /ftp/cvs/cvsroot/src/sys/kern/uipc_socket.c,v: warning: Unknown phrases like `commitid ...;' are present. retrieving revision 1.76 retrieving revision 1.138 diff -u -p -r1.76 -r1.138 --- src/sys/kern/uipc_socket.c 2003/01/31 05:00:24 1.76 +++ src/sys/kern/uipc_socket.c 2007/04/03 23:44:53 1.138 @@ -1,7 +1,7 @@ -/* $NetBSD: uipc_socket.c,v 1.76 2003/01/31 05:00:24 thorpej Exp $ */ +/* $NetBSD: uipc_socket.c,v 1.138 2007/04/03 23:44:53 rmind Exp $ */ /*- - * Copyright (c) 2002 The NetBSD Foundation, Inc. + * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation @@ -48,11 +48,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -72,10 +68,12 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.76 2003/01/31 05:00:24 thorpej Exp $"); +__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.138 2007/04/03 23:44:53 rmind Exp $"); #include "opt_sock_counters.h" #include "opt_sosend_loan.h" +#include "opt_mbuftrace.h" +#include "opt_somaxkva.h" #include #include @@ -92,10 +90,18 @@ __KERNEL_RCSID(0, "$NetBSD: uipc_socket. #include #include #include +#include +#include +#include +#include #include -struct pool socket_pool; +POOL_INIT(socket_pool, sizeof(struct socket), 0, 0, 0, "sockpl", NULL, + IPL_SOFTNET); + +MALLOC_DEFINE(M_SOOPTS, "soopts", "socket options"); +MALLOC_DEFINE(M_SONAME, "soname", "socket name"); extern int somaxconn; /* patchable (XXX sysctl) */ int somaxconn = SOMAXCONN; @@ -103,57 +109,147 @@ int somaxconn = SOMAXCONN; #ifdef SOSEND_COUNTERS #include -struct evcnt sosend_loan_big = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, +static struct evcnt sosend_loan_big = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "sosend", "loan big"); -struct evcnt sosend_copy_big = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, +static struct evcnt sosend_copy_big = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "sosend", "copy big"); -struct evcnt sosend_copy_small = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, +static struct evcnt sosend_copy_small = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "sosend", "copy small"); -struct evcnt sosend_kvalimit = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, +static struct evcnt sosend_kvalimit = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "sosend", "kva limit"); #define SOSEND_COUNTER_INCR(ev) (ev)->ev_count++ +EVCNT_ATTACH_STATIC(sosend_loan_big); +EVCNT_ATTACH_STATIC(sosend_copy_big); +EVCNT_ATTACH_STATIC(sosend_copy_small); +EVCNT_ATTACH_STATIC(sosend_kvalimit); #else #define SOSEND_COUNTER_INCR(ev) /* nothing */ #endif /* SOSEND_COUNTERS */ -void -soinit(void) -{ - - pool_init(&socket_pool, sizeof(struct socket), 0, 0, 0, - "sockpl", NULL); - -#ifdef SOSEND_COUNTERS - evcnt_attach_static(&sosend_loan_big); - evcnt_attach_static(&sosend_copy_big); - evcnt_attach_static(&sosend_copy_small); - evcnt_attach_static(&sosend_kvalimit); -#endif /* SOSEND_COUNTERS */ -} +static struct callback_entry sokva_reclaimerentry; #ifdef SOSEND_NO_LOAN -int use_sosend_loan = 0; +int sock_loan_thresh = -1; #else -int use_sosend_loan = 1; +int sock_loan_thresh = 4096; #endif -struct mbuf *so_pendfree; +static kmutex_t so_pendfree_lock; +static struct mbuf *so_pendfree; -int somaxkva = 16 * 1024 * 1024; -int socurkva; -int sokvawaiters; +#ifndef SOMAXKVA +#define SOMAXKVA (16 * 1024 * 1024) +#endif +int somaxkva = SOMAXKVA; +static int socurkva; +static kcondvar_t socurkva_cv; -#define SOCK_LOAN_THRESH 4096 #define SOCK_LOAN_CHUNK 65536 +static size_t sodopendfree(void); +static size_t sodopendfreel(void); + +static vsize_t +sokvareserve(struct socket *so, vsize_t len) +{ + int error; + + mutex_enter(&so_pendfree_lock); + while (socurkva + len > somaxkva) { + size_t freed; + + /* + * try to do pendfree. + */ + + freed = sodopendfreel(); + + /* + * if some kva was freed, try again. + */ + + if (freed) + continue; + + SOSEND_COUNTER_INCR(&sosend_kvalimit); + error = cv_wait_sig(&socurkva_cv, &so_pendfree_lock); + if (error) { + len = 0; + break; + } + } + socurkva += len; + mutex_exit(&so_pendfree_lock); + return len; +} + +static void +sokvaunreserve(vsize_t len) +{ + + mutex_enter(&so_pendfree_lock); + socurkva -= len; + cv_broadcast(&socurkva_cv); + mutex_exit(&so_pendfree_lock); +} + +/* + * sokvaalloc: allocate kva for loan. + */ + +vaddr_t +sokvaalloc(vsize_t len, struct socket *so) +{ + vaddr_t lva; + + /* + * reserve kva. + */ + + if (sokvareserve(so, len) == 0) + return 0; + + /* + * allocate kva. + */ + + lva = uvm_km_alloc(kernel_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA); + if (lva == 0) { + sokvaunreserve(len); + return (0); + } + + return lva; +} + +/* + * sokvafree: free kva for loan. + */ + +void +sokvafree(vaddr_t sva, vsize_t len) +{ + + /* + * free kva. + */ + + uvm_km_free(kernel_map, sva, len, UVM_KMF_VAONLY); + + /* + * unreserve kva. + */ + + sokvaunreserve(len); +} + static void -sodoloanfree(caddr_t buf, size_t size) +sodoloanfree(struct vm_page **pgs, void *buf, size_t size) { - struct vm_page **pgs; vaddr_t va, sva, eva; vsize_t len; paddr_t pa; @@ -164,79 +260,99 @@ sodoloanfree(caddr_t buf, size_t size) len = eva - sva; npgs = len >> PAGE_SHIFT; - pgs = alloca(npgs * sizeof(*pgs)); + if (__predict_false(pgs == NULL)) { + pgs = alloca(npgs * sizeof(*pgs)); - for (i = 0, va = sva; va < eva; i++, va += PAGE_SIZE) { - if (pmap_extract(pmap_kernel(), va, &pa) == FALSE) - panic("sodoloanfree: va 0x%lx not mapped", va); - pgs[i] = PHYS_TO_VM_PAGE(pa); + for (i = 0, va = sva; va < eva; i++, va += PAGE_SIZE) { + if (pmap_extract(pmap_kernel(), va, &pa) == false) + panic("sodoloanfree: va 0x%lx not mapped", va); + pgs[i] = PHYS_TO_VM_PAGE(pa); + } } pmap_kremove(sva, len); pmap_update(pmap_kernel()); uvm_unloan(pgs, npgs, UVM_LOAN_TOPAGE); - uvm_km_free(kernel_map, sva, len); - socurkva -= len; - if (sokvawaiters) - wakeup(&socurkva); + sokvafree(sva, len); } static size_t -sodopendfree(struct socket *so) +sodopendfree() { - struct mbuf *m; + size_t rv; + + mutex_enter(&so_pendfree_lock); + rv = sodopendfreel(); + mutex_exit(&so_pendfree_lock); + + return rv; +} + +/* + * sodopendfreel: free mbufs on "pendfree" list. + * unlock and relock so_pendfree_lock when freeing mbufs. + * + * => called with so_pendfree_lock held. + */ + +static size_t +sodopendfreel() +{ + struct mbuf *m, *next; size_t rv = 0; int s; - s = splvm(); + KASSERT(mutex_owned(&so_pendfree_lock)); - for (;;) { + while (so_pendfree != NULL) { m = so_pendfree; - if (m == NULL) - break; - so_pendfree = m->m_next; - splx(s); + so_pendfree = NULL; + mutex_exit(&so_pendfree_lock); - rv += m->m_ext.ext_size; - sodoloanfree(m->m_ext.ext_buf, m->m_ext.ext_size); - s = splvm(); - pool_cache_put(&mbpool_cache, m); - } + for (; m != NULL; m = next) { + next = m->m_next; - for (;;) { - m = so->so_pendfree; - if (m == NULL) - break; - so->so_pendfree = m->m_next; - splx(s); + rv += m->m_ext.ext_size; + sodoloanfree((m->m_flags & M_EXT_PAGES) ? + m->m_ext.ext_pgs : NULL, m->m_ext.ext_buf, + m->m_ext.ext_size); + s = splvm(); + pool_cache_put(&mbpool_cache, m); + splx(s); + } - rv += m->m_ext.ext_size; - sodoloanfree(m->m_ext.ext_buf, m->m_ext.ext_size); - s = splvm(); - pool_cache_put(&mbpool_cache, m); + mutex_enter(&so_pendfree_lock); } - splx(s); return (rv); } -static void -soloanfree(struct mbuf *m, caddr_t buf, size_t size, void *arg) +void +soloanfree(struct mbuf *m, void *buf, size_t size, void *arg) { - struct socket *so = arg; - int s; if (m == NULL) { - sodoloanfree(buf, size); + + /* + * called from MEXTREMOVE. + */ + + sodoloanfree(NULL, buf, size); return; } - s = splvm(); - m->m_next = so->so_pendfree; - so->so_pendfree = m; - splx(s); - if (sokvawaiters) - wakeup(&socurkva); + /* + * postpone freeing mbuf. + * + * we can't do it in interrupt context + * because we need to put kva back to kernel_map. + */ + + mutex_enter(&so_pendfree_lock); + m->m_next = so_pendfree; + so_pendfree = m; + cv_broadcast(&socurkva_cv); + mutex_exit(&so_pendfree_lock); } static long @@ -245,11 +361,10 @@ sosend_loan(struct socket *so, struct ui struct iovec *iov = uio->uio_iov; vaddr_t sva, eva; vsize_t len; - struct vm_page **pgs; vaddr_t lva, va; - int npgs, s, i, error; + int npgs, i, error; - if (uio->uio_segflg != UIO_USERSPACE) + if (VMSPACE_IS_KERNEL_P(uio->uio_vmspace)) return (0); if (iov->iov_len < (size_t) space) @@ -262,43 +377,33 @@ sosend_loan(struct socket *so, struct ui len = eva - sva; npgs = len >> PAGE_SHIFT; - while (socurkva + len > somaxkva) { - if (sodopendfree(so)) - continue; - SOSEND_COUNTER_INCR(&sosend_kvalimit); - s = splvm(); - sokvawaiters++; - (void) tsleep(&socurkva, PVM, "sokva", 0); - sokvawaiters--; - splx(s); - } + /* XXX KDASSERT */ + KASSERT(npgs <= M_EXT_MAXPAGES); - lva = uvm_km_valloc_wait(kernel_map, len); + lva = sokvaalloc(len, so); if (lva == 0) - return (0); - socurkva += len; - - pgs = alloca(npgs * sizeof(*pgs)); + return 0; - error = uvm_loan(&uio->uio_procp->p_vmspace->vm_map, sva, len, - pgs, UVM_LOAN_TOPAGE); + error = uvm_loan(&uio->uio_vmspace->vm_map, sva, len, + m->m_ext.ext_pgs, UVM_LOAN_TOPAGE); if (error) { - uvm_km_free(kernel_map, lva, len); - socurkva -= len; + sokvafree(lva, len); return (0); } for (i = 0, va = lva; i < npgs; i++, va += PAGE_SIZE) - pmap_kenter_pa(va, VM_PAGE_TO_PHYS(pgs[i]), VM_PROT_READ); + pmap_kenter_pa(va, VM_PAGE_TO_PHYS(m->m_ext.ext_pgs[i]), + VM_PROT_READ); pmap_update(pmap_kernel()); lva += (vaddr_t) iov->iov_base & PAGE_MASK; - MEXTADD(m, (caddr_t) lva, space, M_MBUF, soloanfree, so); + MEXTADD(m, (void *) lva, space, M_MBUF, soloanfree, so); + m->m_flags |= M_EXT_PAGES | M_EXT_ROMAP; uio->uio_resid -= space; /* uio_offset not updated, not set/used for write(2) */ - uio->uio_iov->iov_base = (caddr_t) uio->uio_iov->iov_base + space; + uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + space; uio->uio_iov->iov_len -= space; if (uio->uio_iov->iov_len == 0) { uio->uio_iov++; @@ -308,6 +413,35 @@ sosend_loan(struct socket *so, struct ui return (space); } +static int +sokva_reclaim_callback(struct callback_entry *ce, void *obj, void *arg) +{ + + KASSERT(ce == &sokva_reclaimerentry); + KASSERT(obj == NULL); + + sodopendfree(); + if (!vm_map_starved_p(kernel_map)) { + return CALLBACK_CHAIN_ABORT; + } + return CALLBACK_CHAIN_CONTINUE; +} + +void +soinit(void) +{ + + mutex_init(&so_pendfree_lock, MUTEX_DRIVER, IPL_VM); + cv_init(&socurkva_cv, "sokva"); + + /* Set the initial adjusted socket buffer size. */ + if (sb_max_set(sb_max)) + panic("bad initial sb_max value: %lu", sb_max); + + callback_register(&vm_map_to_kernel(kernel_map)->vmk_reclaim_callback, + &sokva_reclaimerentry, NULL, sokva_reclaim_callback); +} + /* * Socket operation routines. * These routines are called by the routines in @@ -317,35 +451,54 @@ sosend_loan(struct socket *so, struct ui */ /*ARGSUSED*/ int -socreate(int dom, struct socket **aso, int type, int proto) +socreate(int dom, struct socket **aso, int type, int proto, struct lwp *l) { - struct proc *p; - struct protosw *prp; + const struct protosw *prp; struct socket *so; + uid_t uid; int error, s; - p = curproc; /* XXX */ + error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET, + KAUTH_REQ_NETWORK_SOCKET_OPEN, KAUTH_ARG(dom), KAUTH_ARG(type), + KAUTH_ARG(proto)); + if (error) + return (error); + if (proto) prp = pffindproto(dom, proto, type); else prp = pffindtype(dom, type); - if (prp == 0 || prp->pr_usrreq == 0) + if (prp == 0) { + /* no support for domain */ + if (pffinddomain(dom) == 0) + return (EAFNOSUPPORT); + /* no support for socket type */ + if (proto == 0 && type != 0) + return (EPROTOTYPE); + return (EPROTONOSUPPORT); + } + if (prp->pr_usrreq == 0) return (EPROTONOSUPPORT); if (prp->pr_type != type) return (EPROTOTYPE); s = splsoftnet(); so = pool_get(&socket_pool, PR_WAITOK); - memset((caddr_t)so, 0, sizeof(*so)); + memset((void *)so, 0, sizeof(*so)); TAILQ_INIT(&so->so_q0); TAILQ_INIT(&so->so_q); so->so_type = type; so->so_proto = prp; so->so_send = sosend; so->so_receive = soreceive; - if (p != 0) - so->so_uid = p->p_ucred->cr_uid; +#ifdef MBUFTRACE + so->so_rcv.sb_mowner = &prp->pr_domain->dom_mowner; + so->so_snd.sb_mowner = &prp->pr_domain->dom_mowner; + so->so_mowner = &prp->pr_domain->dom_mowner; +#endif + uid = kauth_cred_geteuid(l->l_cred); + so->so_uidinfo = uid_find(uid); error = (*prp->pr_usrreq)(so, PRU_ATTACH, (struct mbuf *)0, - (struct mbuf *)(long)proto, (struct mbuf *)0, p); + (struct mbuf *)(long)proto, (struct mbuf *)0, l); if (error) { so->so_state |= SS_NOFDREF; sofree(so); @@ -358,13 +511,13 @@ socreate(int dom, struct socket **aso, i } int -sobind(struct socket *so, struct mbuf *nam, struct proc *p) +sobind(struct socket *so, struct mbuf *nam, struct lwp *l) { int s, error; s = splsoftnet(); error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, (struct mbuf *)0, - nam, (struct mbuf *)0, p); + nam, (struct mbuf *)0, l); splx(s); return (error); } @@ -376,7 +529,7 @@ solisten(struct socket *so, int backlog) s = splsoftnet(); error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, (struct mbuf *)0, - (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0); + (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0); if (error) { splx(s); return (error); @@ -393,7 +546,6 @@ solisten(struct socket *so, int backlog) void sofree(struct socket *so) { - struct mbuf *m; if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) return; @@ -406,13 +558,14 @@ sofree(struct socket *so) if (!soqremque(so, 0)) return; } - sbrelease(&so->so_snd); + if (so->so_rcv.sb_hiwat) + (void)chgsbsize(so->so_uidinfo, &so->so_rcv.sb_hiwat, 0, + RLIM_INFINITY); + if (so->so_snd.sb_hiwat) + (void)chgsbsize(so->so_uidinfo, &so->so_snd.sb_hiwat, 0, + RLIM_INFINITY); + sbrelease(&so->so_snd, so); sorflush(so); - while ((m = so->so_pendfree) != NULL) { - so->so_pendfree = m->m_next; - m->m_next = so_pendfree; - so_pendfree = m; - } pool_put(&socket_pool, so); } @@ -452,7 +605,7 @@ soclose(struct socket *so) (so->so_state & SS_NBIO)) goto drop; while (so->so_state & SS_ISCONNECTED) { - error = tsleep((caddr_t)&so->so_timeo, + error = tsleep((void *)&so->so_timeo, PSOCK | PCATCH, netcls, so->so_linger * hz); if (error) @@ -464,7 +617,7 @@ soclose(struct socket *so) if (so->so_pcb) { int error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0, - (struct proc *)0); + (struct lwp *)0); if (error == 0) error = error2; } @@ -485,7 +638,7 @@ soabort(struct socket *so) { return (*so->so_proto->pr_usrreq)(so, PRU_ABORT, (struct mbuf *)0, - (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0); + (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0); } int @@ -501,7 +654,7 @@ soaccept(struct socket *so, struct mbuf if ((so->so_state & SS_ISDISCONNECTED) == 0 || (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0) error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, - (struct mbuf *)0, nam, (struct mbuf *)0, (struct proc *)0); + (struct mbuf *)0, nam, (struct mbuf *)0, (struct lwp *)0); else error = ECONNABORTED; @@ -510,12 +663,10 @@ soaccept(struct socket *so, struct mbuf } int -soconnect(struct socket *so, struct mbuf *nam) +soconnect(struct socket *so, struct mbuf *nam, struct lwp *l) { - struct proc *p; int s, error; - p = curproc; /* XXX */ if (so->so_options & SO_ACCEPTCONN) return (EOPNOTSUPP); s = splsoftnet(); @@ -531,7 +682,7 @@ soconnect(struct socket *so, struct mbuf error = EISCONN; else error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, - (struct mbuf *)0, nam, (struct mbuf *)0, p); + (struct mbuf *)0, nam, (struct mbuf *)0, l); splx(s); return (error); } @@ -544,7 +695,7 @@ soconnect2(struct socket *so1, struct so s = splsoftnet(); error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2, (struct mbuf *)0, (struct mbuf *)so2, (struct mbuf *)0, - (struct proc *)0); + (struct lwp *)0); splx(s); return (error); } @@ -565,10 +716,10 @@ sodisconnect(struct socket *so) } error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0, - (struct proc *)0); + (struct lwp *)0); bad: splx(s); - sodopendfree(so); + sodopendfree(); return (error); } @@ -592,16 +743,16 @@ sodisconnect(struct socket *so) */ int sosend(struct socket *so, struct mbuf *addr, struct uio *uio, struct mbuf *top, - struct mbuf *control, int flags) + struct mbuf *control, int flags, struct lwp *l) { - struct proc *p; struct mbuf **mp, *m; + struct proc *p; long space, len, resid, clen, mlen; int error, s, dontroute, atomic; - sodopendfree(so); + p = l->l_proc; + sodopendfree(); - p = curproc; /* XXX */ clen = 0; atomic = sosendallatonce(so) || top; if (uio) @@ -622,7 +773,8 @@ sosend(struct socket *so, struct mbuf *a dontroute = (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && (so->so_proto->pr_flags & PR_ATOMIC); - p->p_stats->p_ru.ru_msgsnd++; + if (p) + p->p_stats->p_ru.ru_msgsnd++; if (control) clen = control->m_len; #define snderr(errno) { error = errno; splx(s); goto release; } @@ -654,7 +806,7 @@ sosend(struct socket *so, struct mbuf *a if ((atomic && resid > so->so_snd.sb_hiwat) || clen > so->so_snd.sb_hiwat) snderr(EMSGSIZE); - if (space < resid + clen && uio && + if (space < resid + clen && (atomic || space < so->so_snd.sb_lowat || space < clen)) { if (so->so_state & SS_NBIO) snderr(EWOULDBLOCK); @@ -678,17 +830,18 @@ sosend(struct socket *so, struct mbuf *a top->m_flags |= M_EOR; } else do { if (top == 0) { - MGETHDR(m, M_WAIT, MT_DATA); + m = m_gethdr(M_WAIT, MT_DATA); mlen = MHLEN; m->m_pkthdr.len = 0; m->m_pkthdr.rcvif = (struct ifnet *)0; } else { - MGET(m, M_WAIT, MT_DATA); + m = m_get(M_WAIT, MT_DATA); mlen = MLEN; } - if (use_sosend_loan && - uio->uio_iov->iov_len >= SOCK_LOAN_THRESH && - space >= SOCK_LOAN_THRESH && + MCLAIM(m, so->so_snd.sb_mowner); + if (sock_loan_thresh >= 0 && + uio->uio_iov->iov_len >= sock_loan_thresh && + space >= sock_loan_thresh && (len = sosend_loan(so, uio, m, space)) != 0) { SOSEND_COUNTER_INCR(&sosend_loan_big); @@ -697,7 +850,7 @@ sosend(struct socket *so, struct mbuf *a } if (resid >= MINCLSIZE && space >= MCLBYTES) { SOSEND_COUNTER_INCR(&sosend_copy_big); - MCLGET(m, M_WAIT); + m_clget(m, M_WAIT); if ((m->m_flags & M_EXT) == 0) goto nopages; mlen = MCLBYTES; @@ -720,7 +873,7 @@ sosend(struct socket *so, struct mbuf *a if (atomic && top == 0 && len < mlen) MH_ALIGN(m, len); } - error = uiomove(mtod(m, caddr_t), (int)len, + error = uiomove(mtod(m, void *), (int)len, uio); have_data: resid = uio->uio_resid; @@ -736,7 +889,7 @@ sosend(struct socket *so, struct mbuf *a break; } } while (space > 0 && atomic); - + s = splsoftnet(); if (so->so_state & SS_CANTSENDMORE) @@ -748,7 +901,7 @@ sosend(struct socket *so, struct mbuf *a so->so_state |= SS_MORETOCOME; error = (*so->so_proto->pr_usrreq)(so, (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND, - top, addr, control, p); + top, addr, control, curlwp); /* XXX */ if (dontroute) so->so_options &= ~SO_DONTROUTE; if (resid > 0) @@ -794,9 +947,10 @@ int soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp) { + struct lwp *l = curlwp; struct mbuf *m, **mp; int flags, len, error, s, offset, moff, type, orig_resid; - struct protosw *pr; + const struct protosw *pr; struct mbuf *nextrecord; int mbuf_removed = 0; @@ -804,6 +958,7 @@ soreceive(struct socket *so, struct mbuf mp = mp0; type = 0; orig_resid = uio->uio_resid; + if (paddr) *paddr = 0; if (controlp) @@ -814,17 +969,17 @@ soreceive(struct socket *so, struct mbuf flags = 0; if ((flags & MSG_DONTWAIT) == 0) - sodopendfree(so); + sodopendfree(); if (flags & MSG_OOB) { m = m_get(M_WAIT, MT_DATA); error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m, - (struct mbuf *)(long)(flags & MSG_PEEK), (struct mbuf *)0, - (struct proc *)0); + (struct mbuf *)(long)(flags & MSG_PEEK), + (struct mbuf *)0, l); if (error) goto bad; do { - error = uiomove(mtod(m, caddr_t), + error = uiomove(mtod(m, void *), (int) min(uio->uio_resid, m->m_len), uio); m = m_free(m); } while (uio->uio_resid && error == 0 && m); @@ -837,7 +992,7 @@ soreceive(struct socket *so, struct mbuf *mp = (struct mbuf *)0; if (so->so_state & SS_ISCONFIRMING && uio->uio_resid) (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0, - (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0); + (struct mbuf *)0, (struct mbuf *)0, l); restart: if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0) @@ -910,10 +1065,8 @@ soreceive(struct socket *so, struct mbuf * While we process the initial mbufs containing address and control * info, we save a copy of m->m_nextpkt into nextrecord. */ -#ifdef notyet /* XXXX */ - if (uio->uio_procp) - uio->uio_procp->p_stats->p_ru.ru_msgrcv++; -#endif + if (l) + l->l_proc->p_stats->p_ru.ru_msgrcv++; KASSERT(m == so->so_rcv.sb_mb); SBLASTRECORDCHK(&so->so_rcv, "soreceive 1"); SBLASTMBUFCHK(&so->so_rcv, "soreceive 1"); @@ -951,15 +1104,23 @@ soreceive(struct socket *so, struct mbuf sbfree(&so->so_rcv, m); mbuf_removed = 1; if (controlp) { - if (pr->pr_domain->dom_externalize && + struct domain *dom = pr->pr_domain; + if (dom->dom_externalize && l && mtod(m, struct cmsghdr *)->cmsg_type == SCM_RIGHTS) - error = (*pr->pr_domain->dom_externalize)(m); + error = (*dom->dom_externalize)(m, l); *controlp = m; so->so_rcv.sb_mb = m->m_next; m->m_next = 0; m = so->so_rcv.sb_mb; } else { + /* + * Dispose of any SCM_RIGHTS message that went + * through the read path rather than recv. + */ + if (pr->pr_domain->dom_dispose && + mtod(m, struct cmsghdr *)->cmsg_type == SCM_RIGHTS) + (*pr->pr_domain->dom_dispose)(m); MFREE(m, so->so_rcv.sb_mb); m = so->so_rcv.sb_mb; } @@ -1032,7 +1193,7 @@ soreceive(struct socket *so, struct mbuf SBLASTRECORDCHK(&so->so_rcv, "soreceive uiomove"); SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove"); splx(s); - error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio); + error = uiomove(mtod(m, char *) + moff, (int)len, uio); s = splsoftnet(); if (error) { /* @@ -1140,8 +1301,7 @@ soreceive(struct socket *so, struct mbuf (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0, (struct mbuf *)(long)flags, - (struct mbuf *)0, - (struct proc *)0); + (struct mbuf *)0, l); SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2"); SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2"); error = sbwait(&so->so_rcv); @@ -1178,8 +1338,7 @@ soreceive(struct socket *so, struct mbuf SBLASTMBUFCHK(&so->so_rcv, "soreceive 4"); if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0, - (struct mbuf *)(long)flags, (struct mbuf *)0, - (struct proc *)0); + (struct mbuf *)(long)flags, (struct mbuf *)0, l); } if (orig_resid == uio->uio_resid && orig_resid && (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) { @@ -1187,7 +1346,7 @@ soreceive(struct socket *so, struct mbuf splx(s); goto restart; } - + if (flagsp) *flagsp |= flags; release: @@ -1199,7 +1358,7 @@ soreceive(struct socket *so, struct mbuf int soshutdown(struct socket *so, int how) { - struct protosw *pr; + const struct protosw *pr; pr = so->so_proto; if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR)) @@ -1209,7 +1368,7 @@ soshutdown(struct socket *so, int how) sorflush(so); if (how == SHUT_WR || how == SHUT_RDWR) return (*pr->pr_usrreq)(so, PRU_SHUTDOWN, (struct mbuf *)0, - (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0); + (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0); return (0); } @@ -1217,7 +1376,7 @@ void sorflush(struct socket *so) { struct sockbuf *sb, asb; - struct protosw *pr; + const struct protosw *pr; int s; sb = &so->so_rcv; @@ -1228,11 +1387,16 @@ sorflush(struct socket *so) socantrcvmore(so); sbunlock(sb); asb = *sb; - memset((caddr_t)sb, 0, sizeof(*sb)); + /* + * Clear most of the sockbuf structure, but leave some of the + * fields valid. + */ + memset(&sb->sb_startzero, 0, + sizeof(*sb) - offsetof(struct sockbuf, sb_startzero)); splx(s); if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose) (*pr->pr_domain->dom_dispose)(asb.sb_mb); - sbrelease(&asb); + sbrelease(&asb, so); } int @@ -1240,6 +1404,7 @@ sosetopt(struct socket *so, int level, i { int error; struct mbuf *m; + struct linger *l; error = 0; m = m0; @@ -1256,8 +1421,18 @@ sosetopt(struct socket *so, int level, i error = EINVAL; goto bad; } - so->so_linger = mtod(m, struct linger *)->l_linger; - /* fall thru... */ + l = mtod(m, struct linger *); + if (l->l_linger < 0 || l->l_linger > USHRT_MAX || + l->l_linger > (INT_MAX / hz)) { + error = EDOM; + goto bad; + } + so->so_linger = l->l_linger; + if (l->l_onoff) + so->so_options |= SO_LINGER; + else + so->so_options &= ~SO_LINGER; + break; case SO_DEBUG: case SO_KEEPALIVE: @@ -1306,7 +1481,7 @@ sosetopt(struct socket *so, int level, i case SO_RCVBUF: if (sbreserve(optname == SO_SNDBUF ? &so->so_snd : &so->so_rcv, - (u_long) optval) == 0) { + (u_long) optval, so) == 0) { error = ENOBUFS; goto bad; } @@ -1334,14 +1509,14 @@ sosetopt(struct socket *so, int level, i case SO_RCVTIMEO: { struct timeval *tv; - short val; + int val; if (m == NULL || m->m_len < sizeof(*tv)) { error = EINVAL; goto bad; } tv = mtod(m, struct timeval *); - if (tv->tv_sec > (SHRT_MAX - tv->tv_usec / tick) / hz) { + if (tv->tv_sec > (INT_MAX - tv->tv_usec / tick) / hz) { error = EDOM; goto bad; } @@ -1397,7 +1572,7 @@ sogetopt(struct socket *so, int level, i case SO_LINGER: m->m_len = sizeof(struct linger); mtod(m, struct linger *)->l_onoff = - so->so_options & SO_LINGER; + (so->so_options & SO_LINGER) ? 1 : 0; mtod(m, struct linger *)->l_linger = so->so_linger; break; @@ -1410,7 +1585,7 @@ sogetopt(struct socket *so, int level, i case SO_BROADCAST: case SO_OOBINLINE: case SO_TIMESTAMP: - *mtod(m, int *) = so->so_options & optname; + *mtod(m, int *) = (so->so_options & optname) ? 1 : 0; break; case SO_TYPE: @@ -1451,6 +1626,10 @@ sogetopt(struct socket *so, int level, i break; } + case SO_OVERFLOWED: + *mtod(m, int *) = so->so_rcv.sb_overflowed; + break; + default: (void)m_free(m); return (ENOPROTOOPT); @@ -1463,12 +1642,7 @@ sogetopt(struct socket *so, int level, i void sohasoutofband(struct socket *so) { - struct proc *p; - - if (so->so_pgid < 0) - gsignal(-so->so_pgid, SIGURG); - else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0) - psignal(p, SIGURG); + fownsignal(so->so_pgid, SIGURG, POLL_PRI, POLLPRI|POLLRDBAND, so); selwakeup(&so->so_rcv.sb_sel); } @@ -1492,7 +1666,7 @@ filt_soread(struct knote *kn, long hint) so = (struct socket *)kn->kn_fp->f_data; kn->kn_data = so->so_rcv.sb_cc; if (so->so_state & SS_CANTRCVMORE) { - kn->kn_flags |= EV_EOF; + kn->kn_flags |= EV_EOF; kn->kn_fflags = so->so_error; return (1); } @@ -1523,7 +1697,7 @@ filt_sowrite(struct knote *kn, long hint so = (struct socket *)kn->kn_fp->f_data; kn->kn_data = sbspace(&so->so_snd); if (so->so_state & SS_CANTSENDMORE) { - kn->kn_flags |= EV_EOF; + kn->kn_flags |= EV_EOF; kn->kn_fflags = so->so_error; return (1); } @@ -1548,7 +1722,7 @@ filt_solisten(struct knote *kn, long hin /* * Set kn_data to number of incoming connections, not * counting partial (incomplete) connections. - */ + */ kn->kn_data = so->so_qlen; return (kn->kn_data > 0); } @@ -1587,3 +1761,53 @@ soo_kqfilter(struct file *fp, struct kno return (0); } +#include + +static int sysctl_kern_somaxkva(SYSCTLFN_PROTO); + +/* + * sysctl helper routine for kern.somaxkva. ensures that the given + * value is not too small. + * (XXX should we maybe make sure it's not too large as well?) + */ +static int +sysctl_kern_somaxkva(SYSCTLFN_ARGS) +{ + int error, new_somaxkva; + struct sysctlnode node; + + new_somaxkva = somaxkva; + node = *rnode; + node.sysctl_data = &new_somaxkva; + error = sysctl_lookup(SYSCTLFN_CALL(&node)); + if (error || newp == NULL) + return (error); + + if (new_somaxkva < (16 * 1024 * 1024)) /* sanity */ + return (EINVAL); + + mutex_enter(&so_pendfree_lock); + somaxkva = new_somaxkva; + cv_broadcast(&socurkva_cv); + mutex_exit(&so_pendfree_lock); + + return (error); +} + +SYSCTL_SETUP(sysctl_kern_somaxkva_setup, "sysctl kern.somaxkva setup") +{ + + sysctl_createv(clog, 0, NULL, NULL, + CTLFLAG_PERMANENT, + CTLTYPE_NODE, "kern", NULL, + NULL, 0, NULL, 0, + CTL_KERN, CTL_EOL); + + sysctl_createv(clog, 0, NULL, NULL, + CTLFLAG_PERMANENT|CTLFLAG_READWRITE, + CTLTYPE_INT, "somaxkva", + SYSCTL_DESCR("Maximum amount of kernel memory to be " + "used for socket buffers"), + sysctl_kern_somaxkva, 0, NULL, 0, + CTL_KERN, KERN_SOMAXKVA, CTL_EOL); +}