version 1.54.2.16, 2002/08/27 23:47:35 |
version 1.97.2.1.2.2.2.1, 2005/10/31 13:36:41 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
* 2. Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* documentation and/or other materials provided with the distribution. |
* 3. All advertising materials mentioning features or use of this software |
* 3. Neither the name of the University nor the names of its contributors |
* 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 |
|
* may be used to endorse or promote products derived from this software |
* may be used to endorse or promote products derived from this software |
* without specific prior written permission. |
* without specific prior written permission. |
* |
* |
Line 76 __KERNEL_RCSID(0, "$NetBSD$"); |
|
Line 72 __KERNEL_RCSID(0, "$NetBSD$"); |
|
|
|
#include "opt_sock_counters.h" |
#include "opt_sock_counters.h" |
#include "opt_sosend_loan.h" |
#include "opt_sosend_loan.h" |
|
#include "opt_mbuftrace.h" |
|
#include "opt_somaxkva.h" |
|
|
#include <sys/param.h> |
#include <sys/param.h> |
#include <sys/systm.h> |
#include <sys/systm.h> |
Line 91 __KERNEL_RCSID(0, "$NetBSD$"); |
|
Line 89 __KERNEL_RCSID(0, "$NetBSD$"); |
|
#include <sys/signalvar.h> |
#include <sys/signalvar.h> |
#include <sys/resourcevar.h> |
#include <sys/resourcevar.h> |
#include <sys/pool.h> |
#include <sys/pool.h> |
|
#include <sys/event.h> |
|
#include <sys/poll.h> |
|
|
#include <uvm/uvm.h> |
#include <uvm/uvm.h> |
|
|
struct pool socket_pool; |
struct pool socket_pool; |
|
|
|
MALLOC_DEFINE(M_SOOPTS, "soopts", "socket options"); |
|
MALLOC_DEFINE(M_SONAME, "soname", "socket name"); |
|
|
extern int somaxconn; /* patchable (XXX sysctl) */ |
extern int somaxconn; /* patchable (XXX sysctl) */ |
int somaxconn = SOMAXCONN; |
int somaxconn = SOMAXCONN; |
|
|
|
|
soinit(void) |
soinit(void) |
{ |
{ |
|
|
|
/* Set the initial adjusted socket buffer size. */ |
|
if (sb_max_set(sb_max)) |
|
panic("bad initial sb_max value: %lu\n", sb_max); |
|
|
pool_init(&socket_pool, sizeof(struct socket), 0, 0, 0, |
pool_init(&socket_pool, sizeof(struct socket), 0, 0, 0, |
"sockpl", NULL); |
"sockpl", NULL); |
|
|
Line 140 int use_sosend_loan = 0; |
|
Line 147 int use_sosend_loan = 0; |
|
int use_sosend_loan = 1; |
int use_sosend_loan = 1; |
#endif |
#endif |
|
|
|
struct simplelock so_pendfree_slock = SIMPLELOCK_INITIALIZER; |
struct mbuf *so_pendfree; |
struct mbuf *so_pendfree; |
|
|
int somaxkva = 16 * 1024 * 1024; |
#ifndef SOMAXKVA |
|
#define SOMAXKVA (16 * 1024 * 1024) |
|
#endif |
|
int somaxkva = SOMAXKVA; |
int socurkva; |
int socurkva; |
int sokvawaiters; |
int sokvawaiters; |
|
|
#define SOCK_LOAN_THRESH 4096 |
#define SOCK_LOAN_THRESH 4096 |
#define SOCK_LOAN_CHUNK 65536 |
#define SOCK_LOAN_CHUNK 65536 |
|
|
|
static size_t sodopendfree(struct socket *); |
|
static size_t sodopendfreel(struct socket *); |
|
static __inline void sokvareserve(struct socket *, vsize_t); |
|
static __inline void sokvaunreserve(vsize_t); |
|
|
|
static __inline void |
|
sokvareserve(struct socket *so, vsize_t len) |
|
{ |
|
int s; |
|
|
|
s = splvm(); |
|
simple_lock(&so_pendfree_slock); |
|
while (socurkva + len > somaxkva) { |
|
size_t freed; |
|
|
|
/* |
|
* try to do pendfree. |
|
*/ |
|
|
|
freed = sodopendfreel(so); |
|
|
|
/* |
|
* if some kva was freed, try again. |
|
*/ |
|
|
|
if (freed) |
|
continue; |
|
|
|
SOSEND_COUNTER_INCR(&sosend_kvalimit); |
|
sokvawaiters++; |
|
(void) ltsleep(&socurkva, PVM, "sokva", 0, &so_pendfree_slock); |
|
sokvawaiters--; |
|
} |
|
socurkva += len; |
|
simple_unlock(&so_pendfree_slock); |
|
splx(s); |
|
} |
|
|
|
static __inline void |
|
sokvaunreserve(vsize_t len) |
|
{ |
|
int s; |
|
|
|
s = splvm(); |
|
simple_lock(&so_pendfree_slock); |
|
socurkva -= len; |
|
if (sokvawaiters) |
|
wakeup(&socurkva); |
|
simple_unlock(&so_pendfree_slock); |
|
splx(s); |
|
} |
|
|
|
/* |
|
* sokvaalloc: allocate kva for loan. |
|
*/ |
|
|
|
vaddr_t |
|
sokvaalloc(vsize_t len, struct socket *so) |
|
{ |
|
vaddr_t lva; |
|
|
|
/* |
|
* reserve kva. |
|
*/ |
|
|
|
sokvareserve(so, len); |
|
|
|
/* |
|
* allocate kva. |
|
*/ |
|
|
|
lva = uvm_km_valloc_wait(kernel_map, len); |
|
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); |
|
|
|
/* |
|
* unreserve kva. |
|
*/ |
|
|
|
sokvaunreserve(len); |
|
} |
|
|
static void |
static void |
sodoloanfree(caddr_t buf, u_int size) |
sodoloanfree(struct vm_page **pgs, caddr_t buf, size_t size) |
{ |
{ |
struct vm_page **pgs; |
|
vaddr_t va, sva, eva; |
vaddr_t va, sva, eva; |
vsize_t len; |
vsize_t len; |
paddr_t pa; |
paddr_t pa; |
Line 163 sodoloanfree(caddr_t buf, u_int size) |
|
Line 274 sodoloanfree(caddr_t buf, u_int size) |
|
len = eva - sva; |
len = eva - sva; |
npgs = len >> PAGE_SHIFT; |
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) { |
for (i = 0, va = sva; va < eva; i++, va += PAGE_SIZE) { |
if (pmap_extract(pmap_kernel(), va, &pa) == FALSE) |
if (pmap_extract(pmap_kernel(), va, &pa) == FALSE) |
panic("sodoloanfree: va 0x%lx not mapped", va); |
panic("sodoloanfree: va 0x%lx not mapped", va); |
pgs[i] = PHYS_TO_VM_PAGE(pa); |
pgs[i] = PHYS_TO_VM_PAGE(pa); |
|
} |
} |
} |
|
|
pmap_kremove(sva, len); |
pmap_kremove(sva, len); |
pmap_update(pmap_kernel()); |
pmap_update(pmap_kernel()); |
uvm_unloan(pgs, npgs, UVM_LOAN_TOPAGE); |
uvm_unloan(pgs, npgs, UVM_LOAN_TOPAGE); |
uvm_km_free(kernel_map, sva, len); |
sokvafree(sva, len); |
socurkva -= len; |
|
if (sokvawaiters) |
|
wakeup(&socurkva); |
|
} |
} |
|
|
static size_t |
static size_t |
sodopendfree(struct socket *so) |
sodopendfree(struct socket *so) |
{ |
{ |
struct mbuf *m; |
|
size_t rv = 0; |
|
int s; |
int s; |
|
size_t rv; |
|
|
s = splvm(); |
s = splvm(); |
|
simple_lock(&so_pendfree_slock); |
|
rv = sodopendfreel(so); |
|
simple_unlock(&so_pendfree_slock); |
|
splx(s); |
|
|
for (;;) { |
return rv; |
m = so_pendfree; |
} |
if (m == NULL) |
|
break; |
|
so_pendfree = m->m_next; |
|
splx(s); |
|
|
|
rv += m->m_ext.ext_size; |
/* |
sodoloanfree(m->m_ext.ext_buf, m->m_ext.ext_size); |
* sodopendfreel: free mbufs on "pendfree" list. |
s = splvm(); |
* unlock and relock so_pendfree_slock when freeing mbufs. |
pool_cache_put(&mbpool_cache, m); |
* |
} |
* => called with so_pendfree_slock held. |
|
* => called at splvm. |
|
*/ |
|
|
|
static size_t |
|
sodopendfreel(struct socket *so) |
|
{ |
|
size_t rv = 0; |
|
|
|
LOCK_ASSERT(simple_lock_held(&so_pendfree_slock)); |
|
|
for (;;) { |
for (;;) { |
m = so->so_pendfree; |
struct mbuf *m; |
|
struct mbuf *next; |
|
|
|
m = so_pendfree; |
if (m == NULL) |
if (m == NULL) |
break; |
break; |
so->so_pendfree = m->m_next; |
so_pendfree = NULL; |
splx(s); |
simple_unlock(&so_pendfree_slock); |
|
/* XXX splx */ |
|
|
|
for (; m != NULL; m = next) { |
|
next = m->m_next; |
|
|
|
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); |
|
pool_cache_put(&mbpool_cache, m); |
|
} |
|
|
rv += m->m_ext.ext_size; |
/* XXX splvm */ |
sodoloanfree(m->m_ext.ext_buf, m->m_ext.ext_size); |
simple_lock(&so_pendfree_slock); |
s = splvm(); |
|
pool_cache_put(&mbpool_cache, m); |
|
} |
} |
|
|
splx(s); |
|
return (rv); |
return (rv); |
} |
} |
|
|
static void |
void |
soloanfree(struct mbuf *m, caddr_t buf, u_int size, void *arg) |
soloanfree(struct mbuf *m, caddr_t buf, size_t size, void *arg) |
{ |
{ |
struct socket *so = arg; |
|
int s; |
int s; |
|
|
if (m == NULL) { |
if (m == NULL) { |
sodoloanfree(buf, size); |
|
|
/* |
|
* called from MEXTREMOVE. |
|
*/ |
|
|
|
sodoloanfree(NULL, buf, size); |
return; |
return; |
} |
} |
|
|
|
/* |
|
* postpone freeing mbuf. |
|
* |
|
* we can't do it in interrupt context |
|
* because we need to put kva back to kernel_map. |
|
*/ |
|
|
s = splvm(); |
s = splvm(); |
m->m_next = so->so_pendfree; |
simple_lock(&so_pendfree_slock); |
so->so_pendfree = m; |
m->m_next = so_pendfree; |
splx(s); |
so_pendfree = m; |
if (sokvawaiters) |
if (sokvawaiters) |
wakeup(&socurkva); |
wakeup(&socurkva); |
|
simple_unlock(&so_pendfree_slock); |
|
splx(s); |
} |
} |
|
|
static long |
static long |
Line 244 sosend_loan(struct socket *so, struct ui |
|
Line 386 sosend_loan(struct socket *so, struct ui |
|
struct iovec *iov = uio->uio_iov; |
struct iovec *iov = uio->uio_iov; |
vaddr_t sva, eva; |
vaddr_t sva, eva; |
vsize_t len; |
vsize_t len; |
struct vm_page **pgs; |
|
vaddr_t lva, va; |
vaddr_t lva, va; |
int npgs, s, i, error; |
int npgs, i, error; |
|
|
if (uio->uio_segflg != UIO_USERSPACE) |
if (uio->uio_segflg != UIO_USERSPACE) |
return (0); |
return (0); |
Line 261 sosend_loan(struct socket *so, struct ui |
|
Line 402 sosend_loan(struct socket *so, struct ui |
|
len = eva - sva; |
len = eva - sva; |
npgs = len >> PAGE_SHIFT; |
npgs = len >> PAGE_SHIFT; |
|
|
while (socurkva + len > somaxkva) { |
/* XXX KDASSERT */ |
if (sodopendfree(so)) |
KASSERT(npgs <= M_EXT_MAXPAGES); |
continue; |
|
SOSEND_COUNTER_INCR(&sosend_kvalimit); |
|
s = splvm(); |
|
sokvawaiters++; |
|
(void) tsleep(&socurkva, PVM, "sokva", 0); |
|
sokvawaiters--; |
|
splx(s); |
|
} |
|
|
|
lva = uvm_km_valloc_wait(kernel_map, len); |
lva = sokvaalloc(len, so); |
if (lva == 0) |
if (lva == 0) |
return (0); |
return 0; |
socurkva += len; |
|
|
|
pgs = alloca(npgs * sizeof(*pgs)); |
|
|
|
error = uvm_loan(&uio->uio_procp->p_vmspace->vm_map, sva, len, |
error = uvm_loan(&uio->uio_procp->p_vmspace->vm_map, sva, len, |
pgs, UVM_LOAN_TOPAGE); |
m->m_ext.ext_pgs, UVM_LOAN_TOPAGE); |
if (error) { |
if (error) { |
uvm_km_free(kernel_map, lva, len); |
sokvafree(lva, len); |
socurkva -= len; |
|
return (0); |
return (0); |
} |
} |
|
|
for (i = 0, va = lva; i < npgs; i++, va += PAGE_SIZE) |
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()); |
pmap_update(pmap_kernel()); |
|
|
lva += (vaddr_t) iov->iov_base & PAGE_MASK; |
lva += (vaddr_t) iov->iov_base & PAGE_MASK; |
|
|
MEXTADD(m, (caddr_t) lva, space, M_MBUF, soloanfree, so); |
MEXTADD(m, (caddr_t) lva, space, M_MBUF, soloanfree, so); |
|
m->m_flags |= M_EXT_PAGES | M_EXT_ROMAP; |
|
|
uio->uio_resid -= space; |
uio->uio_resid -= space; |
/* uio_offset not updated, not set/used for write(2) */ |
/* uio_offset not updated, not set/used for write(2) */ |
Line 341 socreate(int dom, struct socket **aso, i |
|
Line 472 socreate(int dom, struct socket **aso, i |
|
so->so_proto = prp; |
so->so_proto = prp; |
so->so_send = sosend; |
so->so_send = sosend; |
so->so_receive = soreceive; |
so->so_receive = soreceive; |
|
#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 |
if (p != 0) |
if (p != 0) |
so->so_uid = p->p_ucred->cr_uid; |
so->so_uid = p->p_ucred->cr_uid; |
error = (*prp->pr_usrreq)(so, PRU_ATTACH, (struct mbuf *)0, |
error = (*prp->pr_usrreq)(so, PRU_ATTACH, (struct mbuf *)0, |
Line 392 solisten(struct socket *so, int backlog) |
|
Line 528 solisten(struct socket *so, int backlog) |
|
void |
void |
sofree(struct socket *so) |
sofree(struct socket *so) |
{ |
{ |
struct mbuf *m; |
|
|
|
if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) |
if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) |
return; |
return; |
Line 407 sofree(struct socket *so) |
|
Line 542 sofree(struct socket *so) |
|
} |
} |
sbrelease(&so->so_snd); |
sbrelease(&so->so_snd); |
sorflush(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); |
pool_put(&socket_pool, so); |
} |
} |
|
|
Line 653 sosend(struct socket *so, struct mbuf *a |
|
Line 783 sosend(struct socket *so, struct mbuf *a |
|
if ((atomic && resid > so->so_snd.sb_hiwat) || |
if ((atomic && resid > so->so_snd.sb_hiwat) || |
clen > so->so_snd.sb_hiwat) |
clen > so->so_snd.sb_hiwat) |
snderr(EMSGSIZE); |
snderr(EMSGSIZE); |
if (space < resid + clen && uio && |
if (space < resid + clen && |
(atomic || space < so->so_snd.sb_lowat || space < clen)) { |
(atomic || space < so->so_snd.sb_lowat || space < clen)) { |
if (so->so_state & SS_NBIO) |
if (so->so_state & SS_NBIO) |
snderr(EWOULDBLOCK); |
snderr(EWOULDBLOCK); |
Line 677 sosend(struct socket *so, struct mbuf *a |
|
Line 807 sosend(struct socket *so, struct mbuf *a |
|
top->m_flags |= M_EOR; |
top->m_flags |= M_EOR; |
} else do { |
} else do { |
if (top == 0) { |
if (top == 0) { |
MGETHDR(m, M_WAIT, MT_DATA); |
m = m_gethdr(M_WAIT, MT_DATA); |
mlen = MHLEN; |
mlen = MHLEN; |
m->m_pkthdr.len = 0; |
m->m_pkthdr.len = 0; |
m->m_pkthdr.rcvif = (struct ifnet *)0; |
m->m_pkthdr.rcvif = (struct ifnet *)0; |
} else { |
} else { |
MGET(m, M_WAIT, MT_DATA); |
m = m_get(M_WAIT, MT_DATA); |
mlen = MLEN; |
mlen = MLEN; |
} |
} |
|
MCLAIM(m, so->so_snd.sb_mowner); |
if (use_sosend_loan && |
if (use_sosend_loan && |
uio->uio_iov->iov_len >= SOCK_LOAN_THRESH && |
uio->uio_iov->iov_len >= SOCK_LOAN_THRESH && |
space >= SOCK_LOAN_THRESH && |
space >= SOCK_LOAN_THRESH && |
Line 696 sosend(struct socket *so, struct mbuf *a |
|
Line 827 sosend(struct socket *so, struct mbuf *a |
|
} |
} |
if (resid >= MINCLSIZE && space >= MCLBYTES) { |
if (resid >= MINCLSIZE && space >= MCLBYTES) { |
SOSEND_COUNTER_INCR(&sosend_copy_big); |
SOSEND_COUNTER_INCR(&sosend_copy_big); |
MCLGET(m, M_WAIT); |
m_clget(m, M_WAIT); |
if ((m->m_flags & M_EXT) == 0) |
if ((m->m_flags & M_EXT) == 0) |
goto nopages; |
goto nopages; |
mlen = MCLBYTES; |
mlen = MCLBYTES; |
Line 1227 sorflush(struct socket *so) |
|
Line 1358 sorflush(struct socket *so) |
|
socantrcvmore(so); |
socantrcvmore(so); |
sbunlock(sb); |
sbunlock(sb); |
asb = *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); |
splx(s); |
if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose) |
if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose) |
(*pr->pr_domain->dom_dispose)(asb.sb_mb); |
(*pr->pr_domain->dom_dispose)(asb.sb_mb); |
Line 1255 sosetopt(struct socket *so, int level, i |
|
Line 1391 sosetopt(struct socket *so, int level, i |
|
error = EINVAL; |
error = EINVAL; |
goto bad; |
goto bad; |
} |
} |
|
if (mtod(m, struct linger *)->l_linger < 0 || |
|
mtod(m, struct linger *)->l_linger > (INT_MAX / hz)) { |
|
error = EDOM; |
|
goto bad; |
|
} |
so->so_linger = mtod(m, struct linger *)->l_linger; |
so->so_linger = mtod(m, struct linger *)->l_linger; |
/* fall thru... */ |
/* fall thru... */ |
|
|
Line 1340 sosetopt(struct socket *so, int level, i |
|
Line 1481 sosetopt(struct socket *so, int level, i |
|
goto bad; |
goto bad; |
} |
} |
tv = mtod(m, struct timeval *); |
tv = mtod(m, struct timeval *); |
if (tv->tv_sec * hz + tv->tv_usec / tick > SHRT_MAX) { |
if (tv->tv_sec > (SHRT_MAX - tv->tv_usec / tick) / hz) { |
error = EDOM; |
error = EDOM; |
goto bad; |
goto bad; |
} |
} |
val = tv->tv_sec * hz + tv->tv_usec / tick; |
val = tv->tv_sec * hz + tv->tv_usec / tick; |
|
if (val == 0 && tv->tv_usec != 0) |
|
val = 1; |
|
|
switch (optname) { |
switch (optname) { |
|
|
Line 1460 sogetopt(struct socket *so, int level, i |
|
Line 1603 sogetopt(struct socket *so, int level, i |
|
void |
void |
sohasoutofband(struct socket *so) |
sohasoutofband(struct socket *so) |
{ |
{ |
struct proc *p; |
fownsignal(so->so_pgid, SIGURG, POLL_PRI, POLLPRI|POLLRDBAND, so); |
|
|
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); |
|
selwakeup(&so->so_rcv.sb_sel); |
selwakeup(&so->so_rcv.sb_sel); |
} |
} |
|
|
|
static void |
|
filt_sordetach(struct knote *kn) |
|
{ |
|
struct socket *so; |
|
|
|
so = (struct socket *)kn->kn_fp->f_data; |
|
SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext); |
|
if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist)) |
|
so->so_rcv.sb_flags &= ~SB_KNOTE; |
|
} |
|
|
|
/*ARGSUSED*/ |
|
static int |
|
filt_soread(struct knote *kn, long hint) |
|
{ |
|
struct socket *so; |
|
|
|
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_fflags = so->so_error; |
|
return (1); |
|
} |
|
if (so->so_error) /* temporary udp error */ |
|
return (1); |
|
if (kn->kn_sfflags & NOTE_LOWAT) |
|
return (kn->kn_data >= kn->kn_sdata); |
|
return (kn->kn_data >= so->so_rcv.sb_lowat); |
|
} |
|
|
|
static void |
|
filt_sowdetach(struct knote *kn) |
|
{ |
|
struct socket *so; |
|
|
|
so = (struct socket *)kn->kn_fp->f_data; |
|
SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext); |
|
if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist)) |
|
so->so_snd.sb_flags &= ~SB_KNOTE; |
|
} |
|
|
|
/*ARGSUSED*/ |
|
static int |
|
filt_sowrite(struct knote *kn, long hint) |
|
{ |
|
struct socket *so; |
|
|
|
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_fflags = so->so_error; |
|
return (1); |
|
} |
|
if (so->so_error) /* temporary udp error */ |
|
return (1); |
|
if (((so->so_state & SS_ISCONNECTED) == 0) && |
|
(so->so_proto->pr_flags & PR_CONNREQUIRED)) |
|
return (0); |
|
if (kn->kn_sfflags & NOTE_LOWAT) |
|
return (kn->kn_data >= kn->kn_sdata); |
|
return (kn->kn_data >= so->so_snd.sb_lowat); |
|
} |
|
|
|
/*ARGSUSED*/ |
|
static int |
|
filt_solisten(struct knote *kn, long hint) |
|
{ |
|
struct socket *so; |
|
|
|
so = (struct socket *)kn->kn_fp->f_data; |
|
|
|
/* |
|
* Set kn_data to number of incoming connections, not |
|
* counting partial (incomplete) connections. |
|
*/ |
|
kn->kn_data = so->so_qlen; |
|
return (kn->kn_data > 0); |
|
} |
|
|
|
static const struct filterops solisten_filtops = |
|
{ 1, NULL, filt_sordetach, filt_solisten }; |
|
static const struct filterops soread_filtops = |
|
{ 1, NULL, filt_sordetach, filt_soread }; |
|
static const struct filterops sowrite_filtops = |
|
{ 1, NULL, filt_sowdetach, filt_sowrite }; |
|
|
|
int |
|
soo_kqfilter(struct file *fp, struct knote *kn) |
|
{ |
|
struct socket *so; |
|
struct sockbuf *sb; |
|
|
|
so = (struct socket *)kn->kn_fp->f_data; |
|
switch (kn->kn_filter) { |
|
case EVFILT_READ: |
|
if (so->so_options & SO_ACCEPTCONN) |
|
kn->kn_fop = &solisten_filtops; |
|
else |
|
kn->kn_fop = &soread_filtops; |
|
sb = &so->so_rcv; |
|
break; |
|
case EVFILT_WRITE: |
|
kn->kn_fop = &sowrite_filtops; |
|
sb = &so->so_snd; |
|
break; |
|
default: |
|
return (1); |
|
} |
|
SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, kn, kn_selnext); |
|
sb->sb_flags |= SB_KNOTE; |
|
return (0); |
|
} |
|
|
|
#include <sys/sysctl.h> |
|
|
|
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; |
|
int s; |
|
|
|
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); |
|
|
|
s = splvm(); |
|
simple_lock(&so_pendfree_slock); |
|
somaxkva = new_somaxkva; |
|
wakeup(&socurkva); |
|
simple_unlock(&so_pendfree_slock); |
|
splx(s); |
|
|
|
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); |
|
} |