| version 1.83.2.11, 2005/12/11 10:29:12 |
version 1.84, 2003/07/02 20:07:45 |
|
|
| * 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. Neither the name of the University nor the names of its contributors |
* 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 |
| * 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 90 __KERNEL_RCSID(0, "$NetBSD$"); |
|
| Line 94 __KERNEL_RCSID(0, "$NetBSD$"); |
|
| #include <sys/resourcevar.h> |
#include <sys/resourcevar.h> |
| #include <sys/pool.h> |
#include <sys/pool.h> |
| #include <sys/event.h> |
#include <sys/event.h> |
| #include <sys/poll.h> |
|
| |
|
| #include <uvm/uvm.h> |
#include <uvm/uvm.h> |
| |
|
| POOL_INIT(socket_pool, sizeof(struct socket), 0, 0, 0, "sockpl", NULL); |
struct pool socket_pool; |
| |
|
| MALLOC_DEFINE(M_SOOPTS, "soopts", "socket options"); |
MALLOC_DEFINE(M_SOOPTS, "soopts", "socket options"); |
| MALLOC_DEFINE(M_SONAME, "soname", "socket name"); |
MALLOC_DEFINE(M_SONAME, "soname", "socket name"); |
| Line 105 int somaxconn = SOMAXCONN; |
|
| Line 108 int somaxconn = SOMAXCONN; |
|
| #ifdef SOSEND_COUNTERS |
#ifdef SOSEND_COUNTERS |
| #include <sys/device.h> |
#include <sys/device.h> |
| |
|
| static struct evcnt sosend_loan_big = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, |
struct evcnt sosend_loan_big = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, |
| NULL, "sosend", "loan big"); |
NULL, "sosend", "loan big"); |
| static struct evcnt sosend_copy_big = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, |
struct evcnt sosend_copy_big = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, |
| NULL, "sosend", "copy big"); |
NULL, "sosend", "copy big"); |
| static struct evcnt sosend_copy_small = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, |
struct evcnt sosend_copy_small = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, |
| NULL, "sosend", "copy small"); |
NULL, "sosend", "copy small"); |
| static struct evcnt sosend_kvalimit = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, |
struct evcnt sosend_kvalimit = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, |
| NULL, "sosend", "kva limit"); |
NULL, "sosend", "kva limit"); |
| |
|
| #define SOSEND_COUNTER_INCR(ev) (ev)->ev_count++ |
#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 |
#else |
| |
|
| #define SOSEND_COUNTER_INCR(ev) /* nothing */ |
#define SOSEND_COUNTER_INCR(ev) /* nothing */ |
|
|
| soinit(void) |
soinit(void) |
| { |
{ |
| |
|
| /* Set the initial adjusted socket buffer size. */ |
pool_init(&socket_pool, sizeof(struct socket), 0, 0, 0, |
| if (sb_max_set(sb_max)) |
"sockpl", NULL); |
| panic("bad initial sb_max value: %lu", sb_max); |
|
| |
|
| |
#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 */ |
| } |
} |
| |
|
| #ifdef SOSEND_NO_LOAN |
#ifdef SOSEND_NO_LOAN |
| Line 142 int use_sosend_loan = 0; |
|
| Line 146 int use_sosend_loan = 0; |
|
| int use_sosend_loan = 1; |
int use_sosend_loan = 1; |
| #endif |
#endif |
| |
|
| static struct simplelock so_pendfree_slock = SIMPLELOCK_INITIALIZER; |
struct mbuf *so_pendfree; |
| static struct mbuf *so_pendfree; |
|
| |
|
| #ifndef SOMAXKVA |
#ifndef SOMAXKVA |
| #define SOMAXKVA (16 * 1024 * 1024) |
#define SOMAXKVA (16 * 1024 * 1024) |
| #endif |
#endif |
| int somaxkva = SOMAXKVA; |
int somaxkva = SOMAXKVA; |
| static int socurkva; |
int socurkva; |
| static 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 sodopendfree(struct socket *); |
| static size_t sodopendfreel(struct socket *); |
|
| |
|
| static vsize_t |
vaddr_t |
| sokvareserve(struct socket *so, vsize_t len) |
sokvaalloc(vsize_t len, struct socket *so) |
| { |
{ |
| |
vaddr_t lva; |
| int s; |
int s; |
| int error; |
|
| |
|
| s = splvm(); |
|
| simple_lock(&so_pendfree_slock); |
|
| while (socurkva + len > somaxkva) { |
while (socurkva + len > somaxkva) { |
| size_t freed; |
if (sodopendfree(so)) |
| |
|
| /* |
|
| * try to do pendfree. |
|
| */ |
|
| |
|
| freed = sodopendfreel(so); |
|
| |
|
| /* |
|
| * if some kva was freed, try again. |
|
| */ |
|
| |
|
| if (freed) |
|
| continue; |
continue; |
| |
|
| SOSEND_COUNTER_INCR(&sosend_kvalimit); |
SOSEND_COUNTER_INCR(&sosend_kvalimit); |
| |
s = splvm(); |
| sokvawaiters++; |
sokvawaiters++; |
| error = ltsleep(&socurkva, PVM | PCATCH, "sokva", 0, |
(void) tsleep(&socurkva, PVM, "sokva", 0); |
| &so_pendfree_slock); |
|
| sokvawaiters--; |
sokvawaiters--; |
| if (error) { |
splx(s); |
| len = 0; |
|
| break; |
|
| } |
|
| } |
} |
| socurkva += len; |
|
| simple_unlock(&so_pendfree_slock); |
|
| splx(s); |
|
| return len; |
|
| } |
|
| |
|
| static 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 |
lva = uvm_km_valloc_wait(kernel_map, len); |
| sokvaalloc(vsize_t len, struct socket *so) |
if (lva == 0) |
| { |
|
| 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 (0); |
| } |
socurkva += len; |
| |
|
| return lva; |
return lva; |
| } |
} |
| |
|
| /* |
|
| * sokvafree: free kva for loan. |
|
| */ |
|
| |
|
| void |
void |
| sokvafree(vaddr_t sva, vsize_t len) |
sokvafree(vaddr_t sva, vsize_t len) |
| { |
{ |
| |
|
| /* |
uvm_km_free(kernel_map, sva, len); |
| * free kva. |
socurkva -= len; |
| */ |
if (sokvawaiters) |
| |
wakeup(&socurkva); |
| uvm_km_free(kernel_map, sva, len, UVM_KMF_VAONLY); |
|
| |
|
| /* |
|
| * unreserve kva. |
|
| */ |
|
| |
|
| sokvaunreserve(len); |
|
| } |
} |
| |
|
| static void |
static void |
| Line 294 sodoloanfree(struct vm_page **pgs, caddr |
|
| Line 227 sodoloanfree(struct vm_page **pgs, caddr |
|
| 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); |
|
| |
|
| return rv; |
for (;;) { |
| } |
m = so_pendfree; |
| |
if (m == NULL) |
| /* |
break; |
| * sodopendfreel: free mbufs on "pendfree" list. |
so_pendfree = m->m_next; |
| * unlock and relock so_pendfree_slock when freeing mbufs. |
splx(s); |
| * |
|
| * => 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)); |
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); |
| |
} |
| |
|
| for (;;) { |
for (;;) { |
| struct mbuf *m; |
m = so->so_pendfree; |
| struct mbuf *next; |
|
| |
|
| m = so_pendfree; |
|
| if (m == NULL) |
if (m == NULL) |
| break; |
break; |
| so_pendfree = NULL; |
so->so_pendfree = m->m_next; |
| simple_unlock(&so_pendfree_slock); |
splx(s); |
| /* 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); |
|
| } |
|
| |
|
| /* XXX splvm */ |
rv += m->m_ext.ext_size; |
| simple_lock(&so_pendfree_slock); |
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); |
| return (rv); |
return (rv); |
| } |
} |
| |
|
| void |
void |
| soloanfree(struct mbuf *m, caddr_t buf, size_t 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) { |
| |
|
| /* |
|
| * called from MEXTREMOVE. |
|
| */ |
|
| |
|
| sodoloanfree(NULL, buf, size); |
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(); |
| simple_lock(&so_pendfree_slock); |
m->m_next = so->so_pendfree; |
| m->m_next = so_pendfree; |
so->so_pendfree = m; |
| so_pendfree = m; |
splx(s); |
| if (sokvawaiters) |
if (sokvawaiters) |
| wakeup(&socurkva); |
wakeup(&socurkva); |
| simple_unlock(&so_pendfree_slock); |
|
| splx(s); |
|
| } |
} |
| |
|
| static long |
static long |
| Line 405 sosend_loan(struct socket *so, struct ui |
|
| Line 310 sosend_loan(struct socket *so, struct ui |
|
| |
|
| /* XXX KDASSERT */ |
/* XXX KDASSERT */ |
| KASSERT(npgs <= M_EXT_MAXPAGES); |
KASSERT(npgs <= M_EXT_MAXPAGES); |
| KASSERT(uio->uio_lwp != NULL); |
|
| |
|
| lva = sokvaalloc(len, so); |
lva = sokvaalloc(len, so); |
| if (lva == 0) |
if (lva == 0) |
| return 0; |
return 0; |
| |
|
| error = uvm_loan(&uio->uio_lwp->l_proc->p_vmspace->vm_map, sva, len, |
error = uvm_loan(&uio->uio_procp->p_vmspace->vm_map, sva, len, |
| m->m_ext.ext_pgs, UVM_LOAN_TOPAGE); |
m->m_ext.ext_pgs, UVM_LOAN_TOPAGE); |
| if (error) { |
if (error) { |
| sokvafree(lva, len); |
sokvafree(lva, len); |
| Line 449 sosend_loan(struct socket *so, struct ui |
|
| Line 353 sosend_loan(struct socket *so, struct ui |
|
| */ |
*/ |
| /*ARGSUSED*/ |
/*ARGSUSED*/ |
| int |
int |
| socreate(int dom, struct socket **aso, int type, int proto, struct lwp *l) |
socreate(int dom, struct socket **aso, int type, int proto) |
| { |
{ |
| const struct protosw *prp; |
|
| struct socket *so; |
|
| struct proc *p; |
struct proc *p; |
| |
struct protosw *prp; |
| |
struct socket *so; |
| int error, s; |
int error, s; |
| |
|
| p = l->l_proc; |
p = curproc; /* XXX */ |
| if (proto) |
if (proto) |
| prp = pffindproto(dom, proto, type); |
prp = pffindproto(dom, proto, type); |
| else |
else |
| Line 480 socreate(int dom, struct socket **aso, i |
|
| Line 384 socreate(int dom, struct socket **aso, i |
|
| so->so_mowner = &prp->pr_domain->dom_mowner; |
so->so_mowner = &prp->pr_domain->dom_mowner; |
| #endif |
#endif |
| if (p != 0) |
if (p != 0) |
| so->so_uidinfo = uid_find(p->p_ucred->cr_uid); |
so->so_uid = p->p_ucred->cr_uid; |
| else |
|
| so->so_uidinfo = uid_find(0); |
|
| error = (*prp->pr_usrreq)(so, PRU_ATTACH, (struct mbuf *)0, |
error = (*prp->pr_usrreq)(so, PRU_ATTACH, (struct mbuf *)0, |
| (struct mbuf *)(long)proto, (struct mbuf *)0, l); |
(struct mbuf *)(long)proto, (struct mbuf *)0, p); |
| if (error) { |
if (error) { |
| so->so_state |= SS_NOFDREF; |
so->so_state |= SS_NOFDREF; |
| sofree(so); |
sofree(so); |
| Line 497 socreate(int dom, struct socket **aso, i |
|
| Line 399 socreate(int dom, struct socket **aso, i |
|
| } |
} |
| |
|
| int |
int |
| sobind(struct socket *so, struct mbuf *nam, struct lwp *l) |
sobind(struct socket *so, struct mbuf *nam, struct proc *p) |
| { |
{ |
| int s, error; |
int s, error; |
| |
|
| s = splsoftnet(); |
s = splsoftnet(); |
| error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, (struct mbuf *)0, |
error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, (struct mbuf *)0, |
| nam, (struct mbuf *)0, l); |
nam, (struct mbuf *)0, p); |
| splx(s); |
splx(s); |
| return (error); |
return (error); |
| } |
} |
| Line 515 solisten(struct socket *so, int backlog) |
|
| Line 417 solisten(struct socket *so, int backlog) |
|
| |
|
| s = splsoftnet(); |
s = splsoftnet(); |
| error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, (struct mbuf *)0, |
error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, (struct mbuf *)0, |
| (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0); |
(struct mbuf *)0, (struct mbuf *)0, (struct proc *)0); |
| if (error) { |
if (error) { |
| splx(s); |
splx(s); |
| return (error); |
return (error); |
| Line 532 solisten(struct socket *so, int backlog) |
|
| Line 434 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 544 sofree(struct socket *so) |
|
| Line 447 sofree(struct socket *so) |
|
| if (!soqremque(so, 0)) |
if (!soqremque(so, 0)) |
| return; |
return; |
| } |
} |
| if (so->so_rcv.sb_hiwat) |
sbrelease(&so->so_snd); |
| (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); |
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 603 soclose(struct socket *so) |
|
| Line 505 soclose(struct socket *so) |
|
| if (so->so_pcb) { |
if (so->so_pcb) { |
| int error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, |
int error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, |
| (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0, |
(struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0, |
| (struct lwp *)0); |
(struct proc *)0); |
| if (error == 0) |
if (error == 0) |
| error = error2; |
error = error2; |
| } |
} |
| Line 624 soabort(struct socket *so) |
|
| Line 526 soabort(struct socket *so) |
|
| { |
{ |
| |
|
| return (*so->so_proto->pr_usrreq)(so, PRU_ABORT, (struct mbuf *)0, |
return (*so->so_proto->pr_usrreq)(so, PRU_ABORT, (struct mbuf *)0, |
| (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0); |
(struct mbuf *)0, (struct mbuf *)0, (struct proc *)0); |
| } |
} |
| |
|
| int |
int |
| Line 640 soaccept(struct socket *so, struct mbuf |
|
| Line 542 soaccept(struct socket *so, struct mbuf |
|
| if ((so->so_state & SS_ISDISCONNECTED) == 0 || |
if ((so->so_state & SS_ISDISCONNECTED) == 0 || |
| (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0) |
(so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0) |
| error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, |
error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, |
| (struct mbuf *)0, nam, (struct mbuf *)0, (struct lwp *)0); |
(struct mbuf *)0, nam, (struct mbuf *)0, (struct proc *)0); |
| else |
else |
| error = ECONNABORTED; |
error = ECONNABORTED; |
| |
|
| Line 649 soaccept(struct socket *so, struct mbuf |
|
| Line 551 soaccept(struct socket *so, struct mbuf |
|
| } |
} |
| |
|
| int |
int |
| soconnect(struct socket *so, struct mbuf *nam, struct lwp *l) |
soconnect(struct socket *so, struct mbuf *nam) |
| { |
{ |
| |
struct proc *p; |
| int s, error; |
int s, error; |
| |
|
| |
p = curproc; /* XXX */ |
| if (so->so_options & SO_ACCEPTCONN) |
if (so->so_options & SO_ACCEPTCONN) |
| return (EOPNOTSUPP); |
return (EOPNOTSUPP); |
| s = splsoftnet(); |
s = splsoftnet(); |
| Line 668 soconnect(struct socket *so, struct mbuf |
|
| Line 572 soconnect(struct socket *so, struct mbuf |
|
| error = EISCONN; |
error = EISCONN; |
| else |
else |
| error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, |
error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, |
| (struct mbuf *)0, nam, (struct mbuf *)0, l); |
(struct mbuf *)0, nam, (struct mbuf *)0, p); |
| splx(s); |
splx(s); |
| return (error); |
return (error); |
| } |
} |
| Line 681 soconnect2(struct socket *so1, struct so |
|
| Line 585 soconnect2(struct socket *so1, struct so |
|
| s = splsoftnet(); |
s = splsoftnet(); |
| error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2, |
error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2, |
| (struct mbuf *)0, (struct mbuf *)so2, (struct mbuf *)0, |
(struct mbuf *)0, (struct mbuf *)so2, (struct mbuf *)0, |
| (struct lwp *)0); |
(struct proc *)0); |
| splx(s); |
splx(s); |
| return (error); |
return (error); |
| } |
} |
| Line 702 sodisconnect(struct socket *so) |
|
| Line 606 sodisconnect(struct socket *so) |
|
| } |
} |
| error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, |
error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, |
| (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0, |
(struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0, |
| (struct lwp *)0); |
(struct proc *)0); |
| bad: |
bad: |
| splx(s); |
splx(s); |
| sodopendfree(so); |
sodopendfree(so); |
| Line 729 sodisconnect(struct socket *so) |
|
| Line 633 sodisconnect(struct socket *so) |
|
| */ |
*/ |
| int |
int |
| sosend(struct socket *so, struct mbuf *addr, struct uio *uio, struct mbuf *top, |
sosend(struct socket *so, struct mbuf *addr, struct uio *uio, struct mbuf *top, |
| struct mbuf *control, int flags, struct lwp *l) |
struct mbuf *control, int flags) |
| { |
{ |
| struct mbuf **mp, *m; |
|
| struct proc *p; |
struct proc *p; |
| |
struct mbuf **mp, *m; |
| long space, len, resid, clen, mlen; |
long space, len, resid, clen, mlen; |
| int error, s, dontroute, atomic; |
int error, s, dontroute, atomic; |
| |
|
| p = l->l_proc; |
|
| sodopendfree(so); |
sodopendfree(so); |
| |
|
| |
p = curproc; /* XXX */ |
| clen = 0; |
clen = 0; |
| atomic = sosendallatonce(so) || top; |
atomic = sosendallatonce(so) || top; |
| if (uio) |
if (uio) |
| Line 759 sosend(struct socket *so, struct mbuf *a |
|
| Line 663 sosend(struct socket *so, struct mbuf *a |
|
| dontroute = |
dontroute = |
| (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && |
(flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && |
| (so->so_proto->pr_flags & PR_ATOMIC); |
(so->so_proto->pr_flags & PR_ATOMIC); |
| if (p) |
p->p_stats->p_ru.ru_msgsnd++; |
| p->p_stats->p_ru.ru_msgsnd++; |
|
| if (control) |
if (control) |
| clen = control->m_len; |
clen = control->m_len; |
| #define snderr(errno) { error = errno; splx(s); goto release; } |
#define snderr(errno) { error = errno; splx(s); goto release; } |
| Line 792 sosend(struct socket *so, struct mbuf *a |
|
| Line 695 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 && |
if (space < resid + clen && uio && |
| (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 875 sosend(struct socket *so, struct mbuf *a |
|
| Line 778 sosend(struct socket *so, struct mbuf *a |
|
| break; |
break; |
| } |
} |
| } while (space > 0 && atomic); |
} while (space > 0 && atomic); |
| |
|
| s = splsoftnet(); |
s = splsoftnet(); |
| |
|
| if (so->so_state & SS_CANTSENDMORE) |
if (so->so_state & SS_CANTSENDMORE) |
| Line 887 sosend(struct socket *so, struct mbuf *a |
|
| Line 790 sosend(struct socket *so, struct mbuf *a |
|
| so->so_state |= SS_MORETOCOME; |
so->so_state |= SS_MORETOCOME; |
| error = (*so->so_proto->pr_usrreq)(so, |
error = (*so->so_proto->pr_usrreq)(so, |
| (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND, |
(flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND, |
| top, addr, control, curlwp); /* XXX */ |
top, addr, control, p); |
| if (dontroute) |
if (dontroute) |
| so->so_options &= ~SO_DONTROUTE; |
so->so_options &= ~SO_DONTROUTE; |
| if (resid > 0) |
if (resid > 0) |
|
|
| soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio, |
soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio, |
| struct mbuf **mp0, struct mbuf **controlp, int *flagsp) |
struct mbuf **mp0, struct mbuf **controlp, int *flagsp) |
| { |
{ |
| struct lwp *l; |
|
| struct mbuf *m, **mp; |
struct mbuf *m, **mp; |
| int flags, len, error, s, offset, moff, type, orig_resid; |
int flags, len, error, s, offset, moff, type, orig_resid; |
| const struct protosw *pr; |
struct protosw *pr; |
| struct mbuf *nextrecord; |
struct mbuf *nextrecord; |
| int mbuf_removed = 0; |
int mbuf_removed = 0; |
| |
|
| Line 944 soreceive(struct socket *so, struct mbuf |
|
| Line 846 soreceive(struct socket *so, struct mbuf |
|
| mp = mp0; |
mp = mp0; |
| type = 0; |
type = 0; |
| orig_resid = uio->uio_resid; |
orig_resid = uio->uio_resid; |
| l = uio->uio_lwp; |
|
| |
|
| if (paddr) |
if (paddr) |
| *paddr = 0; |
*paddr = 0; |
| if (controlp) |
if (controlp) |
| Line 961 soreceive(struct socket *so, struct mbuf |
|
| Line 861 soreceive(struct socket *so, struct mbuf |
|
| if (flags & MSG_OOB) { |
if (flags & MSG_OOB) { |
| m = m_get(M_WAIT, MT_DATA); |
m = m_get(M_WAIT, MT_DATA); |
| error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m, |
error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m, |
| (struct mbuf *)(long)(flags & MSG_PEEK), |
(struct mbuf *)(long)(flags & MSG_PEEK), (struct mbuf *)0, |
| (struct mbuf *)0, l); |
(struct proc *)0); |
| if (error) |
if (error) |
| goto bad; |
goto bad; |
| do { |
do { |
| Line 979 soreceive(struct socket *so, struct mbuf |
|
| Line 879 soreceive(struct socket *so, struct mbuf |
|
| *mp = (struct mbuf *)0; |
*mp = (struct mbuf *)0; |
| if (so->so_state & SS_ISCONFIRMING && uio->uio_resid) |
if (so->so_state & SS_ISCONFIRMING && uio->uio_resid) |
| (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0, |
(*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0, |
| (struct mbuf *)0, (struct mbuf *)0, l); |
(struct mbuf *)0, (struct mbuf *)0, (struct proc *)0); |
| |
|
| restart: |
restart: |
| if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0) |
if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0) |
| Line 1052 soreceive(struct socket *so, struct mbuf |
|
| Line 952 soreceive(struct socket *so, struct mbuf |
|
| * While we process the initial mbufs containing address and control |
* While we process the initial mbufs containing address and control |
| * info, we save a copy of m->m_nextpkt into nextrecord. |
* info, we save a copy of m->m_nextpkt into nextrecord. |
| */ |
*/ |
| if (l) |
#ifdef notyet /* XXXX */ |
| l->l_proc->p_stats->p_ru.ru_msgrcv++; |
if (uio->uio_procp) |
| |
uio->uio_procp->p_stats->p_ru.ru_msgrcv++; |
| |
#endif |
| KASSERT(m == so->so_rcv.sb_mb); |
KASSERT(m == so->so_rcv.sb_mb); |
| SBLASTRECORDCHK(&so->so_rcv, "soreceive 1"); |
SBLASTRECORDCHK(&so->so_rcv, "soreceive 1"); |
| SBLASTMBUFCHK(&so->so_rcv, "soreceive 1"); |
SBLASTMBUFCHK(&so->so_rcv, "soreceive 1"); |
| Line 1091 soreceive(struct socket *so, struct mbuf |
|
| Line 993 soreceive(struct socket *so, struct mbuf |
|
| sbfree(&so->so_rcv, m); |
sbfree(&so->so_rcv, m); |
| mbuf_removed = 1; |
mbuf_removed = 1; |
| if (controlp) { |
if (controlp) { |
| struct domain *dom = pr->pr_domain; |
if (pr->pr_domain->dom_externalize && |
| if (dom->dom_externalize && l && |
|
| mtod(m, struct cmsghdr *)->cmsg_type == |
mtod(m, struct cmsghdr *)->cmsg_type == |
| SCM_RIGHTS) |
SCM_RIGHTS) |
| error = (*dom->dom_externalize)(m, l); |
error = (*pr->pr_domain->dom_externalize)(m); |
| *controlp = m; |
*controlp = m; |
| so->so_rcv.sb_mb = m->m_next; |
so->so_rcv.sb_mb = m->m_next; |
| m->m_next = 0; |
m->m_next = 0; |
| m = so->so_rcv.sb_mb; |
m = so->so_rcv.sb_mb; |
| } else { |
} 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); |
MFREE(m, so->so_rcv.sb_mb); |
| m = so->so_rcv.sb_mb; |
m = so->so_rcv.sb_mb; |
| } |
} |
| Line 1288 soreceive(struct socket *so, struct mbuf |
|
| Line 1182 soreceive(struct socket *so, struct mbuf |
|
| (*pr->pr_usrreq)(so, PRU_RCVD, |
(*pr->pr_usrreq)(so, PRU_RCVD, |
| (struct mbuf *)0, |
(struct mbuf *)0, |
| (struct mbuf *)(long)flags, |
(struct mbuf *)(long)flags, |
| (struct mbuf *)0, l); |
(struct mbuf *)0, |
| |
(struct proc *)0); |
| SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2"); |
SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2"); |
| SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2"); |
SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2"); |
| error = sbwait(&so->so_rcv); |
error = sbwait(&so->so_rcv); |
| Line 1325 soreceive(struct socket *so, struct mbuf |
|
| Line 1220 soreceive(struct socket *so, struct mbuf |
|
| SBLASTMBUFCHK(&so->so_rcv, "soreceive 4"); |
SBLASTMBUFCHK(&so->so_rcv, "soreceive 4"); |
| if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) |
if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) |
| (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0, |
(*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0, |
| (struct mbuf *)(long)flags, (struct mbuf *)0, l); |
(struct mbuf *)(long)flags, (struct mbuf *)0, |
| |
(struct proc *)0); |
| } |
} |
| if (orig_resid == uio->uio_resid && orig_resid && |
if (orig_resid == uio->uio_resid && orig_resid && |
| (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) { |
(flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) { |
| Line 1333 soreceive(struct socket *so, struct mbuf |
|
| Line 1229 soreceive(struct socket *so, struct mbuf |
|
| splx(s); |
splx(s); |
| goto restart; |
goto restart; |
| } |
} |
| |
|
| if (flagsp) |
if (flagsp) |
| *flagsp |= flags; |
*flagsp |= flags; |
| release: |
release: |
| Line 1345 soreceive(struct socket *so, struct mbuf |
|
| Line 1241 soreceive(struct socket *so, struct mbuf |
|
| int |
int |
| soshutdown(struct socket *so, int how) |
soshutdown(struct socket *so, int how) |
| { |
{ |
| const struct protosw *pr; |
struct protosw *pr; |
| |
|
| pr = so->so_proto; |
pr = so->so_proto; |
| if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR)) |
if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR)) |
| Line 1355 soshutdown(struct socket *so, int how) |
|
| Line 1251 soshutdown(struct socket *so, int how) |
|
| sorflush(so); |
sorflush(so); |
| if (how == SHUT_WR || how == SHUT_RDWR) |
if (how == SHUT_WR || how == SHUT_RDWR) |
| return (*pr->pr_usrreq)(so, PRU_SHUTDOWN, (struct mbuf *)0, |
return (*pr->pr_usrreq)(so, PRU_SHUTDOWN, (struct mbuf *)0, |
| (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0); |
(struct mbuf *)0, (struct mbuf *)0, (struct proc *)0); |
| return (0); |
return (0); |
| } |
} |
| |
|
|
|
| sorflush(struct socket *so) |
sorflush(struct socket *so) |
| { |
{ |
| struct sockbuf *sb, asb; |
struct sockbuf *sb, asb; |
| const struct protosw *pr; |
struct protosw *pr; |
| int s; |
int s; |
| |
|
| sb = &so->so_rcv; |
sb = &so->so_rcv; |
| Line 1374 sorflush(struct socket *so) |
|
| Line 1270 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); |
| sbrelease(&asb, so); |
sbrelease(&asb); |
| } |
} |
| |
|
| int |
int |
| Line 1407 sosetopt(struct socket *so, int level, i |
|
| Line 1298 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 1462 sosetopt(struct socket *so, int level, i |
|
| Line 1348 sosetopt(struct socket *so, int level, i |
|
| case SO_RCVBUF: |
case SO_RCVBUF: |
| if (sbreserve(optname == SO_SNDBUF ? |
if (sbreserve(optname == SO_SNDBUF ? |
| &so->so_snd : &so->so_rcv, |
&so->so_snd : &so->so_rcv, |
| (u_long) optval, so) == 0) { |
(u_long) optval) == 0) { |
| error = ENOBUFS; |
error = ENOBUFS; |
| goto bad; |
goto bad; |
| } |
} |
| Line 1490 sosetopt(struct socket *so, int level, i |
|
| Line 1376 sosetopt(struct socket *so, int level, i |
|
| case SO_RCVTIMEO: |
case SO_RCVTIMEO: |
| { |
{ |
| struct timeval *tv; |
struct timeval *tv; |
| int val; |
short val; |
| |
|
| if (m == NULL || m->m_len < sizeof(*tv)) { |
if (m == NULL || m->m_len < sizeof(*tv)) { |
| error = EINVAL; |
error = EINVAL; |
| goto bad; |
goto bad; |
| } |
} |
| tv = mtod(m, struct timeval *); |
tv = mtod(m, struct timeval *); |
| if (tv->tv_sec > (INT_MAX - tv->tv_usec / tick) / hz) { |
if (tv->tv_sec > (SHRT_MAX - tv->tv_usec / tick) / hz) { |
| error = EDOM; |
error = EDOM; |
| goto bad; |
goto bad; |
| } |
} |
| Line 1607 sogetopt(struct socket *so, int level, i |
|
| Line 1493 sogetopt(struct socket *so, int level, i |
|
| break; |
break; |
| } |
} |
| |
|
| case SO_OVERFLOWED: |
|
| *mtod(m, int *) = so->so_rcv.sb_overflowed; |
|
| break; |
|
| |
|
| default: |
default: |
| (void)m_free(m); |
(void)m_free(m); |
| return (ENOPROTOOPT); |
return (ENOPROTOOPT); |
| Line 1623 sogetopt(struct socket *so, int level, i |
|
| Line 1505 sogetopt(struct socket *so, int level, i |
|
| void |
void |
| sohasoutofband(struct socket *so) |
sohasoutofband(struct socket *so) |
| { |
{ |
| fownsignal(so->so_pgid, SIGURG, POLL_PRI, POLLPRI|POLLRDBAND, 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); |
| selwakeup(&so->so_rcv.sb_sel); |
selwakeup(&so->so_rcv.sb_sel); |
| } |
} |
| |
|
| Line 1647 filt_soread(struct knote *kn, long hint) |
|
| Line 1534 filt_soread(struct knote *kn, long hint) |
|
| so = (struct socket *)kn->kn_fp->f_data; |
so = (struct socket *)kn->kn_fp->f_data; |
| kn->kn_data = so->so_rcv.sb_cc; |
kn->kn_data = so->so_rcv.sb_cc; |
| if (so->so_state & SS_CANTRCVMORE) { |
if (so->so_state & SS_CANTRCVMORE) { |
| kn->kn_flags |= EV_EOF; |
kn->kn_flags |= EV_EOF; |
| kn->kn_fflags = so->so_error; |
kn->kn_fflags = so->so_error; |
| return (1); |
return (1); |
| } |
} |
| Line 1678 filt_sowrite(struct knote *kn, long hint |
|
| Line 1565 filt_sowrite(struct knote *kn, long hint |
|
| so = (struct socket *)kn->kn_fp->f_data; |
so = (struct socket *)kn->kn_fp->f_data; |
| kn->kn_data = sbspace(&so->so_snd); |
kn->kn_data = sbspace(&so->so_snd); |
| if (so->so_state & SS_CANTSENDMORE) { |
if (so->so_state & SS_CANTSENDMORE) { |
| kn->kn_flags |= EV_EOF; |
kn->kn_flags |= EV_EOF; |
| kn->kn_fflags = so->so_error; |
kn->kn_fflags = so->so_error; |
| return (1); |
return (1); |
| } |
} |
| Line 1703 filt_solisten(struct knote *kn, long hin |
|
| Line 1590 filt_solisten(struct knote *kn, long hin |
|
| /* |
/* |
| * Set kn_data to number of incoming connections, not |
* Set kn_data to number of incoming connections, not |
| * counting partial (incomplete) connections. |
* counting partial (incomplete) connections. |
| */ |
*/ |
| kn->kn_data = so->so_qlen; |
kn->kn_data = so->so_qlen; |
| return (kn->kn_data > 0); |
return (kn->kn_data > 0); |
| } |
} |
| Line 1742 soo_kqfilter(struct file *fp, struct kno |
|
| Line 1629 soo_kqfilter(struct file *fp, struct kno |
|
| return (0); |
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); |
|
| } |
|