version 1.182, 2009/01/15 15:25:37 |
version 1.220, 2013/11/02 20:09:33 |
|
|
/* $NetBSD$ */ |
/* $NetBSD$ */ |
|
|
/*- |
/*- |
* Copyright (c) 2002, 2007, 2008 The NetBSD Foundation, Inc. |
* Copyright (c) 2002, 2007, 2008, 2009 The NetBSD Foundation, Inc. |
* All rights reserved. |
* All rights reserved. |
* |
* |
* This code is derived from software contributed to The NetBSD Foundation |
* This code is derived from software contributed to The NetBSD Foundation |
* by Jason R. Thorpe of Wasabi Systems, Inc. |
* by Jason R. Thorpe of Wasabi Systems, Inc, and by Andrew Doran. |
* |
* |
* Redistribution and use in source and binary forms, with or without |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* modification, are permitted provided that the following conditions |
Line 92 __KERNEL_RCSID(0, "$NetBSD$"); |
|
Line 92 __KERNEL_RCSID(0, "$NetBSD$"); |
|
#include <sys/kauth.h> |
#include <sys/kauth.h> |
#include <sys/mutex.h> |
#include <sys/mutex.h> |
#include <sys/condvar.h> |
#include <sys/condvar.h> |
|
#include <sys/kthread.h> |
|
|
#ifdef COMPAT_50 |
#ifdef COMPAT_50 |
#include <compat/sys/time.h> |
#include <compat/sys/time.h> |
#define SO_OSNDTIMEO 0x1005 |
#include <compat/sys/socket.h> |
#define SO_ORCVTIMEO 0x1006 |
|
#endif |
#endif |
|
|
#include <uvm/uvm.h> |
#include <uvm/uvm_extern.h> |
|
#include <uvm/uvm_loan.h> |
|
#include <uvm/uvm_page.h> |
|
|
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 134 EVCNT_ATTACH_STATIC(sosend_kvalimit); |
|
Line 136 EVCNT_ATTACH_STATIC(sosend_kvalimit); |
|
|
|
#endif /* SOSEND_COUNTERS */ |
#endif /* SOSEND_COUNTERS */ |
|
|
static struct callback_entry sokva_reclaimerentry; |
|
|
|
#if defined(SOSEND_NO_LOAN) || defined(MULTIPROCESSOR) |
#if defined(SOSEND_NO_LOAN) || defined(MULTIPROCESSOR) |
int sock_loan_thresh = -1; |
int sock_loan_thresh = -1; |
#else |
#else |
Line 143 int sock_loan_thresh = 4096; |
|
Line 143 int sock_loan_thresh = 4096; |
|
#endif |
#endif |
|
|
static kmutex_t so_pendfree_lock; |
static kmutex_t so_pendfree_lock; |
static struct mbuf *so_pendfree; |
static struct mbuf *so_pendfree = NULL; |
|
|
#ifndef SOMAXKVA |
#ifndef SOMAXKVA |
#define SOMAXKVA (16 * 1024 * 1024) |
#define SOMAXKVA (16 * 1024 * 1024) |
Line 152 int somaxkva = SOMAXKVA; |
|
Line 152 int somaxkva = SOMAXKVA; |
|
static int socurkva; |
static int socurkva; |
static kcondvar_t socurkva_cv; |
static kcondvar_t socurkva_cv; |
|
|
|
static kauth_listener_t socket_listener; |
|
|
#define SOCK_LOAN_CHUNK 65536 |
#define SOCK_LOAN_CHUNK 65536 |
|
|
static size_t sodopendfree(void); |
static void sopendfree_thread(void *); |
static size_t sodopendfreel(void); |
static kcondvar_t pendfree_thread_cv; |
|
static lwp_t *sopendfree_lwp; |
|
|
static void sysctl_kern_somaxkva_setup(void); |
static void sysctl_kern_socket_setup(void); |
static struct sysctllog *socket_sysctllog; |
static struct sysctllog *socket_sysctllog; |
|
|
static vsize_t |
static vsize_t |
Line 167 sokvareserve(struct socket *so, vsize_t |
|
Line 170 sokvareserve(struct socket *so, vsize_t |
|
|
|
mutex_enter(&so_pendfree_lock); |
mutex_enter(&so_pendfree_lock); |
while (socurkva + len > somaxkva) { |
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); |
SOSEND_COUNTER_INCR(&sosend_kvalimit); |
error = cv_wait_sig(&socurkva_cv, &so_pendfree_lock); |
error = cv_wait_sig(&socurkva_cv, &so_pendfree_lock); |
if (error) { |
if (error) { |
Line 209 sokvaunreserve(vsize_t len) |
|
Line 197 sokvaunreserve(vsize_t len) |
|
*/ |
*/ |
|
|
vaddr_t |
vaddr_t |
sokvaalloc(vsize_t len, struct socket *so) |
sokvaalloc(vaddr_t sva, vsize_t len, struct socket *so) |
{ |
{ |
vaddr_t lva; |
vaddr_t lva; |
|
|
Line 224 sokvaalloc(vsize_t len, struct socket *s |
|
Line 212 sokvaalloc(vsize_t len, struct socket *s |
|
* allocate kva. |
* allocate kva. |
*/ |
*/ |
|
|
lva = uvm_km_alloc(kernel_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA); |
lva = uvm_km_alloc(kernel_map, len, atop(sva) & uvmexp.colormask, |
|
UVM_KMF_COLORMATCH | UVM_KMF_VAONLY | UVM_KMF_WAITVA); |
if (lva == 0) { |
if (lva == 0) { |
sokvaunreserve(len); |
sokvaunreserve(len); |
return (0); |
return (0); |
Line 274 sodoloanfree(struct vm_page **pgs, void |
|
Line 263 sodoloanfree(struct vm_page **pgs, void |
|
sokvafree(sva, len); |
sokvafree(sva, len); |
} |
} |
|
|
static size_t |
|
sodopendfree(void) |
|
{ |
|
size_t rv; |
|
|
|
if (__predict_true(so_pendfree == NULL)) |
|
return 0; |
|
|
|
mutex_enter(&so_pendfree_lock); |
|
rv = sodopendfreel(); |
|
mutex_exit(&so_pendfree_lock); |
|
|
|
return rv; |
|
} |
|
|
|
/* |
/* |
* sodopendfreel: free mbufs on "pendfree" list. |
* sopendfree_thread: free mbufs on "pendfree" list. |
* unlock and relock so_pendfree_lock when freeing mbufs. |
* unlock and relock so_pendfree_lock when freeing mbufs. |
* |
|
* => called with so_pendfree_lock held. |
|
*/ |
*/ |
|
|
static size_t |
static void |
sodopendfreel(void) |
sopendfree_thread(void *v) |
{ |
{ |
struct mbuf *m, *next; |
struct mbuf *m, *next; |
size_t rv = 0; |
size_t rv; |
|
|
KASSERT(mutex_owned(&so_pendfree_lock)); |
|
|
|
while (so_pendfree != NULL) { |
mutex_enter(&so_pendfree_lock); |
m = so_pendfree; |
|
so_pendfree = NULL; |
|
mutex_exit(&so_pendfree_lock); |
|
|
|
for (; m != NULL; m = next) { |
for (;;) { |
next = m->m_next; |
rv = 0; |
KASSERT((~m->m_flags & (M_EXT|M_EXT_PAGES)) == 0); |
while (so_pendfree != NULL) { |
KASSERT(m->m_ext.ext_refcnt == 0); |
m = so_pendfree; |
|
so_pendfree = NULL; |
|
mutex_exit(&so_pendfree_lock); |
|
|
|
for (; m != NULL; m = next) { |
|
next = m->m_next; |
|
KASSERT((~m->m_flags & (M_EXT|M_EXT_PAGES)) == 0); |
|
KASSERT(m->m_ext.ext_refcnt == 0); |
|
|
|
rv += m->m_ext.ext_size; |
|
sodoloanfree(m->m_ext.ext_pgs, m->m_ext.ext_buf, |
|
m->m_ext.ext_size); |
|
pool_cache_put(mb_cache, m); |
|
} |
|
|
rv += m->m_ext.ext_size; |
mutex_enter(&so_pendfree_lock); |
sodoloanfree(m->m_ext.ext_pgs, m->m_ext.ext_buf, |
|
m->m_ext.ext_size); |
|
pool_cache_put(mb_cache, m); |
|
} |
} |
|
if (rv) |
mutex_enter(&so_pendfree_lock); |
cv_broadcast(&socurkva_cv); |
|
cv_wait(&pendfree_thread_cv, &so_pendfree_lock); |
} |
} |
|
panic("sopendfree_thread"); |
return (rv); |
/* NOTREACHED */ |
} |
} |
|
|
void |
void |
Line 342 soloanfree(struct mbuf *m, void *buf, si |
|
Line 320 soloanfree(struct mbuf *m, void *buf, si |
|
mutex_enter(&so_pendfree_lock); |
mutex_enter(&so_pendfree_lock); |
m->m_next = so_pendfree; |
m->m_next = so_pendfree; |
so_pendfree = m; |
so_pendfree = m; |
cv_broadcast(&socurkva_cv); |
cv_signal(&pendfree_thread_cv); |
mutex_exit(&so_pendfree_lock); |
mutex_exit(&so_pendfree_lock); |
} |
} |
|
|
Line 372 sosend_loan(struct socket *so, struct ui |
|
Line 350 sosend_loan(struct socket *so, struct ui |
|
|
|
KASSERT(npgs <= M_EXT_MAXPAGES); |
KASSERT(npgs <= M_EXT_MAXPAGES); |
|
|
lva = sokvaalloc(len, so); |
lva = sokvaalloc(sva, len, so); |
if (lva == 0) |
if (lva == 0) |
return 0; |
return 0; |
|
|
Line 385 sosend_loan(struct socket *so, struct ui |
|
Line 363 sosend_loan(struct socket *so, struct ui |
|
|
|
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(m->m_ext.ext_pgs[i]), |
pmap_kenter_pa(va, VM_PAGE_TO_PHYS(m->m_ext.ext_pgs[i]), |
VM_PROT_READ); |
VM_PROT_READ, 0); |
pmap_update(pmap_kernel()); |
pmap_update(pmap_kernel()); |
|
|
lva += (vaddr_t) iov->iov_base & PAGE_MASK; |
lva += (vaddr_t) iov->iov_base & PAGE_MASK; |
Line 405 sosend_loan(struct socket *so, struct ui |
|
Line 383 sosend_loan(struct socket *so, struct ui |
|
return (space); |
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; |
|
} |
|
|
|
struct mbuf * |
struct mbuf * |
getsombuf(struct socket *so, int type) |
getsombuf(struct socket *so, int type) |
{ |
{ |
Line 429 getsombuf(struct socket *so, int type) |
|
Line 393 getsombuf(struct socket *so, int type) |
|
return m; |
return m; |
} |
} |
|
|
|
static int |
|
socket_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie, |
|
void *arg0, void *arg1, void *arg2, void *arg3) |
|
{ |
|
int result; |
|
enum kauth_network_req req; |
|
|
|
result = KAUTH_RESULT_DEFER; |
|
req = (enum kauth_network_req)arg0; |
|
|
|
if ((action != KAUTH_NETWORK_SOCKET) && |
|
(action != KAUTH_NETWORK_BIND)) |
|
return result; |
|
|
|
switch (req) { |
|
case KAUTH_REQ_NETWORK_BIND_PORT: |
|
result = KAUTH_RESULT_ALLOW; |
|
break; |
|
|
|
case KAUTH_REQ_NETWORK_SOCKET_DROP: { |
|
/* Normal users can only drop their own connections. */ |
|
struct socket *so = (struct socket *)arg1; |
|
|
|
if (so->so_cred && proc_uidmatch(cred, so->so_cred) == 0) |
|
result = KAUTH_RESULT_ALLOW; |
|
|
|
break; |
|
} |
|
|
|
case KAUTH_REQ_NETWORK_SOCKET_OPEN: |
|
/* We allow "raw" routing/bluetooth sockets to anyone. */ |
|
if ((u_long)arg1 == PF_ROUTE || (u_long)arg1 == PF_OROUTE |
|
|| (u_long)arg1 == PF_BLUETOOTH) { |
|
result = KAUTH_RESULT_ALLOW; |
|
} else { |
|
/* Privileged, let secmodel handle this. */ |
|
if ((u_long)arg2 == SOCK_RAW) |
|
break; |
|
} |
|
|
|
result = KAUTH_RESULT_ALLOW; |
|
|
|
break; |
|
|
|
case KAUTH_REQ_NETWORK_SOCKET_CANSEE: |
|
result = KAUTH_RESULT_ALLOW; |
|
|
|
break; |
|
|
|
default: |
|
break; |
|
} |
|
|
|
return result; |
|
} |
|
|
void |
void |
soinit(void) |
soinit(void) |
{ |
{ |
|
|
sysctl_kern_somaxkva_setup(); |
sysctl_kern_socket_setup(); |
|
|
mutex_init(&so_pendfree_lock, MUTEX_DEFAULT, IPL_VM); |
mutex_init(&so_pendfree_lock, MUTEX_DEFAULT, IPL_VM); |
softnet_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); |
softnet_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); |
cv_init(&socurkva_cv, "sokva"); |
cv_init(&socurkva_cv, "sokva"); |
|
cv_init(&pendfree_thread_cv, "sopendfr"); |
soinit2(); |
soinit2(); |
|
|
/* Set the initial adjusted socket buffer size. */ |
/* Set the initial adjusted socket buffer size. */ |
if (sb_max_set(sb_max)) |
if (sb_max_set(sb_max)) |
panic("bad initial sb_max value: %lu", sb_max); |
panic("bad initial sb_max value: %lu", sb_max); |
|
|
callback_register(&vm_map_to_kernel(kernel_map)->vmk_reclaim_callback, |
socket_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK, |
&sokva_reclaimerentry, NULL, sokva_reclaim_callback); |
socket_listener_cb, NULL); |
|
} |
|
|
|
void |
|
soinit1(void) |
|
{ |
|
int error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, |
|
sopendfree_thread, NULL, &sopendfree_lwp, "sopendfree"); |
|
if (error) |
|
panic("soinit1 %d", error); |
} |
} |
|
|
/* |
/* |
Line 502 socreate(int dom, struct socket **aso, i |
|
Line 532 socreate(int dom, struct socket **aso, i |
|
#endif |
#endif |
uid = kauth_cred_geteuid(l->l_cred); |
uid = kauth_cred_geteuid(l->l_cred); |
so->so_uidinfo = uid_find(uid); |
so->so_uidinfo = uid_find(uid); |
so->so_egid = kauth_cred_getegid(l->l_cred); |
|
so->so_cpid = l->l_proc->p_pid; |
so->so_cpid = l->l_proc->p_pid; |
if (lockso != NULL) { |
if (lockso != NULL) { |
/* Caller wants us to share a lock. */ |
/* Caller wants us to share a lock. */ |
lock = lockso->so_lock; |
lock = lockso->so_lock; |
so->so_lock = lock; |
so->so_lock = lock; |
mutex_obj_hold(lock); |
mutex_obj_hold(lock); |
|
/* XXX Why is this not solock, to match sounlock? */ |
mutex_enter(lock); |
mutex_enter(lock); |
} else { |
} else { |
/* Lock assigned and taken during PRU_ATTACH. */ |
/* Lock assigned and taken during PRU_ATTACH. */ |
Line 521 socreate(int dom, struct socket **aso, i |
|
Line 551 socreate(int dom, struct socket **aso, i |
|
sofree(so); |
sofree(so); |
return error; |
return error; |
} |
} |
|
so->so_cred = kauth_cred_dup(l->l_cred); |
sounlock(so); |
sounlock(so); |
*aso = so; |
*aso = so; |
return 0; |
return 0; |
Line 536 fsocreate(int domain, struct socket **so |
|
Line 567 fsocreate(int domain, struct socket **so |
|
struct socket *so; |
struct socket *so; |
struct file *fp; |
struct file *fp; |
int fd, error; |
int fd, error; |
|
int flags = type & SOCK_FLAGS_MASK; |
|
|
|
type &= ~SOCK_FLAGS_MASK; |
if ((error = fd_allocfile(&fp, &fd)) != 0) |
if ((error = fd_allocfile(&fp, &fd)) != 0) |
return (error); |
return error; |
fp->f_flag = FREAD|FWRITE; |
fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0); |
|
fp->f_flag = FREAD|FWRITE|((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)| |
|
((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0); |
fp->f_type = DTYPE_SOCKET; |
fp->f_type = DTYPE_SOCKET; |
fp->f_ops = &socketops; |
fp->f_ops = &socketops; |
error = socreate(domain, &so, type, protocol, l, NULL); |
error = socreate(domain, &so, type, protocol, l, NULL); |
Line 551 fsocreate(int domain, struct socket **so |
|
Line 586 fsocreate(int domain, struct socket **so |
|
fp->f_data = so; |
fp->f_data = so; |
fd_affix(curproc, fp, fd); |
fd_affix(curproc, fp, fd); |
*fdout = fd; |
*fdout = fd; |
|
if (flags & SOCK_NONBLOCK) |
|
so->so_state |= SS_NBIO; |
} |
} |
return error; |
return error; |
} |
} |
|
|
int |
int |
|
sofamily(const struct socket *so) |
|
{ |
|
const struct protosw *pr; |
|
const struct domain *dom; |
|
|
|
if ((pr = so->so_proto) == NULL) |
|
return AF_UNSPEC; |
|
if ((dom = pr->pr_domain) == NULL) |
|
return AF_UNSPEC; |
|
return dom->dom_family; |
|
} |
|
|
|
int |
sobind(struct socket *so, struct mbuf *nam, struct lwp *l) |
sobind(struct socket *so, struct mbuf *nam, struct lwp *l) |
{ |
{ |
int error; |
int error; |
Line 575 solisten(struct socket *so, int backlog, |
|
Line 625 solisten(struct socket *so, int backlog, |
|
if ((so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING | |
if ((so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING | |
SS_ISDISCONNECTING)) != 0) { |
SS_ISDISCONNECTING)) != 0) { |
sounlock(so); |
sounlock(so); |
return (EOPNOTSUPP); |
return (EINVAL); |
} |
} |
error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL, |
error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL, |
NULL, NULL, l); |
NULL, NULL, l); |
Line 678 soclose(struct socket *so) |
|
Line 728 soclose(struct socket *so) |
|
goto drop; |
goto drop; |
} |
} |
if (so->so_options & SO_LINGER) { |
if (so->so_options & SO_LINGER) { |
if ((so->so_state & SS_ISDISCONNECTING) && so->so_nbio) |
if ((so->so_state & (SS_ISDISCONNECTING|SS_NBIO)) == |
|
(SS_ISDISCONNECTING|SS_NBIO)) |
goto drop; |
goto drop; |
while (so->so_state & SS_ISCONNECTED) { |
while (so->so_state & SS_ISCONNECTED) { |
error = sowait(so, so->so_linger * hz); |
error = sowait(so, true, so->so_linger * hz); |
if (error) |
if (error) |
break; |
break; |
} |
} |
Line 697 soclose(struct socket *so) |
|
Line 748 soclose(struct socket *so) |
|
discard: |
discard: |
if (so->so_state & SS_NOFDREF) |
if (so->so_state & SS_NOFDREF) |
panic("soclose: NOFDREF"); |
panic("soclose: NOFDREF"); |
|
kauth_cred_free(so->so_cred); |
so->so_state |= SS_NOFDREF; |
so->so_state |= SS_NOFDREF; |
sofree(so); |
sofree(so); |
return (error); |
return (error); |
Line 799 sodisconnect(struct socket *so) |
|
Line 851 sodisconnect(struct socket *so) |
|
error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, |
error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, |
NULL, NULL, NULL, NULL); |
NULL, NULL, NULL, NULL); |
} |
} |
sodopendfree(); |
|
return (error); |
return (error); |
} |
} |
|
|
Line 826 sosend(struct socket *so, struct mbuf *a |
|
Line 877 sosend(struct socket *so, struct mbuf *a |
|
struct mbuf *control, int flags, struct lwp *l) |
struct mbuf *control, int flags, struct lwp *l) |
{ |
{ |
struct mbuf **mp, *m; |
struct mbuf **mp, *m; |
struct proc *p; |
|
long space, len, resid, clen, mlen; |
long space, len, resid, clen, mlen; |
int error, s, dontroute, atomic; |
int error, s, dontroute, atomic; |
|
short wakeup_state = 0; |
|
|
p = l->l_proc; |
|
sodopendfree(); |
|
clen = 0; |
clen = 0; |
|
|
/* |
/* |
Line 878 sosend(struct socket *so, struct mbuf *a |
|
Line 927 sosend(struct socket *so, struct mbuf *a |
|
} |
} |
if ((so->so_state & SS_ISCONNECTED) == 0) { |
if ((so->so_state & SS_ISCONNECTED) == 0) { |
if (so->so_proto->pr_flags & PR_CONNREQUIRED) { |
if (so->so_proto->pr_flags & PR_CONNREQUIRED) { |
if ((so->so_state & SS_ISCONFIRMING) == 0 && |
if (resid || clen == 0) { |
!(resid == 0 && clen != 0)) { |
|
error = ENOTCONN; |
error = ENOTCONN; |
goto release; |
goto release; |
} |
} |
Line 898 sosend(struct socket *so, struct mbuf *a |
|
Line 946 sosend(struct socket *so, struct mbuf *a |
|
} |
} |
if (space < resid + clen && |
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_nbio) { |
if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO)) { |
error = EWOULDBLOCK; |
error = EWOULDBLOCK; |
goto release; |
goto release; |
} |
} |
sbunlock(&so->so_snd); |
sbunlock(&so->so_snd); |
|
if (wakeup_state & SS_RESTARTSYS) { |
|
error = ERESTART; |
|
goto out; |
|
} |
error = sbwait(&so->so_snd); |
error = sbwait(&so->so_snd); |
if (error) |
if (error) |
goto out; |
goto out; |
|
wakeup_state = so->so_state; |
goto restart; |
goto restart; |
} |
} |
|
wakeup_state = 0; |
mp = ⊤ |
mp = ⊤ |
space -= clen; |
space -= clen; |
do { |
do { |
Line 942 sosend(struct socket *so, struct mbuf *a |
|
Line 996 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); |
m_clget(m, M_WAIT); |
m_clget(m, M_DONTWAIT); |
if ((m->m_flags & M_EXT) == 0) |
if ((m->m_flags & M_EXT) == 0) |
goto nopages; |
goto nopages; |
mlen = MCLBYTES; |
mlen = MCLBYTES; |
Line 1078 soreceive(struct socket *so, struct mbuf |
|
Line 1132 soreceive(struct socket *so, struct mbuf |
|
{ |
{ |
struct lwp *l = curlwp; |
struct lwp *l = curlwp; |
struct mbuf *m, **mp, *mt; |
struct mbuf *m, **mp, *mt; |
int atomic, flags, len, error, s, offset, moff, type, orig_resid; |
size_t len, offset, moff, orig_resid; |
|
int atomic, flags, error, s, type; |
const struct protosw *pr; |
const struct protosw *pr; |
struct mbuf *nextrecord; |
struct mbuf *nextrecord; |
int mbuf_removed = 0; |
int mbuf_removed = 0; |
const struct domain *dom; |
const struct domain *dom; |
|
short wakeup_state = 0; |
|
|
pr = so->so_proto; |
pr = so->so_proto; |
atomic = pr->pr_flags & PR_ATOMIC; |
atomic = pr->pr_flags & PR_ATOMIC; |
Line 1100 soreceive(struct socket *so, struct mbuf |
|
Line 1156 soreceive(struct socket *so, struct mbuf |
|
else |
else |
flags = 0; |
flags = 0; |
|
|
if ((flags & MSG_DONTWAIT) == 0) |
|
sodopendfree(); |
|
|
|
if (flags & MSG_OOB) { |
if (flags & MSG_OOB) { |
m = m_get(M_WAIT, MT_DATA); |
m = m_get(M_WAIT, MT_DATA); |
solock(so); |
solock(so); |
Line 1113 soreceive(struct socket *so, struct mbuf |
|
Line 1166 soreceive(struct socket *so, struct mbuf |
|
goto bad; |
goto bad; |
do { |
do { |
error = uiomove(mtod(m, void *), |
error = uiomove(mtod(m, void *), |
(int) min(uio->uio_resid, m->m_len), uio); |
MIN(uio->uio_resid, m->m_len), uio); |
m = m_free(m); |
m = m_free(m); |
} while (uio->uio_resid > 0 && error == 0 && m); |
} while (uio->uio_resid > 0 && error == 0 && m); |
bad: |
bad: |
Line 1131 soreceive(struct socket *so, struct mbuf |
|
Line 1184 soreceive(struct socket *so, struct mbuf |
|
*/ |
*/ |
s = splsoftnet(); |
s = splsoftnet(); |
solock(so); |
solock(so); |
if (so->so_state & SS_ISCONFIRMING && uio->uio_resid) |
|
(*pr->pr_usrreq)(so, PRU_RCVD, NULL, NULL, NULL, l); |
|
|
|
restart: |
restart: |
if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0) { |
if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0) { |
sounlock(so); |
sounlock(so); |
Line 1190 soreceive(struct socket *so, struct mbuf |
|
Line 1240 soreceive(struct socket *so, struct mbuf |
|
} |
} |
if (uio->uio_resid == 0) |
if (uio->uio_resid == 0) |
goto release; |
goto release; |
if (so->so_nbio || (flags & MSG_DONTWAIT)) { |
if ((so->so_state & SS_NBIO) || |
|
(flags & (MSG_DONTWAIT|MSG_NBIO))) { |
error = EWOULDBLOCK; |
error = EWOULDBLOCK; |
goto release; |
goto release; |
} |
} |
SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 1"); |
SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 1"); |
SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 1"); |
SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 1"); |
sbunlock(&so->so_rcv); |
sbunlock(&so->so_rcv); |
error = sbwait(&so->so_rcv); |
if (wakeup_state & SS_RESTARTSYS) |
|
error = ERESTART; |
|
else |
|
error = sbwait(&so->so_rcv); |
if (error != 0) { |
if (error != 0) { |
sounlock(so); |
sounlock(so); |
splx(s); |
splx(s); |
return error; |
return error; |
} |
} |
|
wakeup_state = so->so_state; |
goto restart; |
goto restart; |
} |
} |
dontblock: |
dontblock: |
Line 1291 soreceive(struct socket *so, struct mbuf |
|
Line 1346 soreceive(struct socket *so, struct mbuf |
|
type == SCM_RIGHTS) { |
type == SCM_RIGHTS) { |
sounlock(so); |
sounlock(so); |
splx(s); |
splx(s); |
error = (*dom->dom_externalize)(cm, l); |
error = (*dom->dom_externalize)(cm, l, |
|
(flags & MSG_CMSG_CLOEXEC) ? |
|
O_CLOEXEC : 0); |
s = splsoftnet(); |
s = splsoftnet(); |
solock(so); |
solock(so); |
} |
} |
Line 1341 soreceive(struct socket *so, struct mbuf |
|
Line 1398 soreceive(struct socket *so, struct mbuf |
|
panic("receive 3"); |
panic("receive 3"); |
#endif |
#endif |
so->so_state &= ~SS_RCVATMARK; |
so->so_state &= ~SS_RCVATMARK; |
|
wakeup_state = 0; |
len = uio->uio_resid; |
len = uio->uio_resid; |
if (so->so_oobmark && len > so->so_oobmark - offset) |
if (so->so_oobmark && len > so->so_oobmark - offset) |
len = so->so_oobmark - offset; |
len = so->so_oobmark - offset; |
Line 1359 soreceive(struct socket *so, struct mbuf |
|
Line 1417 soreceive(struct socket *so, struct mbuf |
|
SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove"); |
SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove"); |
sounlock(so); |
sounlock(so); |
splx(s); |
splx(s); |
error = uiomove(mtod(m, char *) + moff, (int)len, uio); |
error = uiomove(mtod(m, char *) + moff, len, uio); |
s = splsoftnet(); |
s = splsoftnet(); |
solock(so); |
solock(so); |
if (error != 0) { |
if (error != 0) { |
Line 1473 soreceive(struct socket *so, struct mbuf |
|
Line 1531 soreceive(struct socket *so, struct mbuf |
|
NULL, (struct mbuf *)(long)flags, NULL, l); |
NULL, (struct mbuf *)(long)flags, NULL, l); |
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); |
if (wakeup_state & SS_RESTARTSYS) |
|
error = ERESTART; |
|
else |
|
error = sbwait(&so->so_rcv); |
if (error != 0) { |
if (error != 0) { |
sbunlock(&so->so_rcv); |
sbunlock(&so->so_rcv); |
sounlock(so); |
sounlock(so); |
Line 1482 soreceive(struct socket *so, struct mbuf |
|
Line 1543 soreceive(struct socket *so, struct mbuf |
|
} |
} |
if ((m = so->so_rcv.sb_mb) != NULL) |
if ((m = so->so_rcv.sb_mb) != NULL) |
nextrecord = m->m_nextpkt; |
nextrecord = m->m_nextpkt; |
|
wakeup_state = so->so_state; |
} |
} |
} |
} |
|
|
Line 1549 soshutdown(struct socket *so, int how) |
|
Line 1611 soshutdown(struct socket *so, int how) |
|
} |
} |
|
|
void |
void |
|
sorestart(struct socket *so) |
|
{ |
|
/* |
|
* An application has called close() on an fd on which another |
|
* of its threads has called a socket system call. |
|
* Mark this and wake everyone up, and code that would block again |
|
* instead returns ERESTART. |
|
* On system call re-entry the fd is validated and EBADF returned. |
|
* Any other fd will block again on the 2nd syscall. |
|
*/ |
|
solock(so); |
|
so->so_state |= SS_RESTARTSYS; |
|
cv_broadcast(&so->so_cv); |
|
cv_broadcast(&so->so_snd.sb_cv); |
|
cv_broadcast(&so->so_rcv.sb_cv); |
|
sounlock(so); |
|
} |
|
|
|
void |
sorflush(struct socket *so) |
sorflush(struct socket *so) |
{ |
{ |
struct sockbuf *sb, asb; |
struct sockbuf *sb, asb; |
Line 1583 sorflush(struct socket *so) |
|
Line 1664 sorflush(struct socket *so) |
|
static int |
static int |
sosetopt1(struct socket *so, const struct sockopt *sopt) |
sosetopt1(struct socket *so, const struct sockopt *sopt) |
{ |
{ |
int error = EINVAL, optval, opt; |
int error = EINVAL, opt; |
|
int optval = 0; /* XXX: gcc */ |
struct linger l; |
struct linger l; |
struct timeval tv; |
struct timeval tv; |
|
|
Line 1620 sosetopt1(struct socket *so, const struc |
|
Line 1702 sosetopt1(struct socket *so, const struc |
|
case SO_REUSEPORT: |
case SO_REUSEPORT: |
case SO_OOBINLINE: |
case SO_OOBINLINE: |
case SO_TIMESTAMP: |
case SO_TIMESTAMP: |
|
case SO_NOSIGPIPE: |
|
#ifdef SO_OTIMESTAMP |
|
case SO_OTIMESTAMP: |
|
#endif |
error = sockopt_getint(sopt, &optval); |
error = sockopt_getint(sopt, &optval); |
solock(so); |
solock(so); |
if (error) |
if (error) |
Line 1690 sosetopt1(struct socket *so, const struc |
|
Line 1776 sosetopt1(struct socket *so, const struc |
|
case SO_ORCVTIMEO: { |
case SO_ORCVTIMEO: { |
struct timeval50 otv; |
struct timeval50 otv; |
error = sockopt_get(sopt, &otv, sizeof(otv)); |
error = sockopt_get(sopt, &otv, sizeof(otv)); |
|
if (error) { |
|
solock(so); |
|
break; |
|
} |
timeval50_to_timeval(&otv, &tv); |
timeval50_to_timeval(&otv, &tv); |
opt = opt == SO_OSNDTIMEO ? SO_SNDTIMEO : SO_RCVTIMEO; |
opt = opt == SO_OSNDTIMEO ? SO_SNDTIMEO : SO_RCVTIMEO; |
error = 0; |
error = 0; |
Line 1813 sogetopt1(struct socket *so, struct sock |
|
Line 1903 sogetopt1(struct socket *so, struct sock |
|
case SO_BROADCAST: |
case SO_BROADCAST: |
case SO_OOBINLINE: |
case SO_OOBINLINE: |
case SO_TIMESTAMP: |
case SO_TIMESTAMP: |
|
case SO_NOSIGPIPE: |
|
#ifdef SO_OTIMESTAMP |
|
case SO_OTIMESTAMP: |
|
#endif |
|
case SO_ACCEPTCONN: |
error = sockopt_setint(sopt, (so->so_options & opt) ? 1 : 0); |
error = sockopt_setint(sopt, (so->so_options & opt) ? 1 : 0); |
break; |
break; |
|
|
Line 2069 sohasoutofband(struct socket *so) |
|
Line 2164 sohasoutofband(struct socket *so) |
|
{ |
{ |
|
|
fownsignal(so->so_pgid, SIGURG, POLL_PRI, POLLPRI|POLLRDBAND, so); |
fownsignal(so->so_pgid, SIGURG, POLL_PRI, POLLPRI|POLLRDBAND, so); |
selnotify(&so->so_rcv.sb_sel, POLLPRI | POLLRDBAND, 0); |
selnotify(&so->so_rcv.sb_sel, POLLPRI | POLLRDBAND, NOTE_SUBMIT); |
} |
} |
|
|
static void |
static void |
Line 2270 sopoll(struct socket *so, int events) |
|
Line 2365 sopoll(struct socket *so, int events) |
|
#include <sys/sysctl.h> |
#include <sys/sysctl.h> |
|
|
static int sysctl_kern_somaxkva(SYSCTLFN_PROTO); |
static int sysctl_kern_somaxkva(SYSCTLFN_PROTO); |
|
static int sysctl_kern_sbmax(SYSCTLFN_PROTO); |
|
|
/* |
/* |
* sysctl helper routine for kern.somaxkva. ensures that the given |
* sysctl helper routine for kern.somaxkva. ensures that the given |
Line 2300 sysctl_kern_somaxkva(SYSCTLFN_ARGS) |
|
Line 2396 sysctl_kern_somaxkva(SYSCTLFN_ARGS) |
|
return (error); |
return (error); |
} |
} |
|
|
|
/* |
|
* sysctl helper routine for kern.sbmax. Basically just ensures that |
|
* any new value is not too small. |
|
*/ |
|
static int |
|
sysctl_kern_sbmax(SYSCTLFN_ARGS) |
|
{ |
|
int error, new_sbmax; |
|
struct sysctlnode node; |
|
|
|
new_sbmax = sb_max; |
|
node = *rnode; |
|
node.sysctl_data = &new_sbmax; |
|
error = sysctl_lookup(SYSCTLFN_CALL(&node)); |
|
if (error || newp == NULL) |
|
return (error); |
|
|
|
KERNEL_LOCK(1, NULL); |
|
error = sb_max_set(new_sbmax); |
|
KERNEL_UNLOCK_ONE(NULL); |
|
|
|
return (error); |
|
} |
|
|
static void |
static void |
sysctl_kern_somaxkva_setup() |
sysctl_kern_socket_setup(void) |
{ |
{ |
|
|
KASSERT(socket_sysctllog == NULL); |
KASSERT(socket_sysctllog == NULL); |
Line 2318 sysctl_kern_somaxkva_setup() |
|
Line 2438 sysctl_kern_somaxkva_setup() |
|
"used for socket buffers"), |
"used for socket buffers"), |
sysctl_kern_somaxkva, 0, NULL, 0, |
sysctl_kern_somaxkva, 0, NULL, 0, |
CTL_KERN, KERN_SOMAXKVA, CTL_EOL); |
CTL_KERN, KERN_SOMAXKVA, CTL_EOL); |
|
|
|
sysctl_createv(&socket_sysctllog, 0, NULL, NULL, |
|
CTLFLAG_PERMANENT|CTLFLAG_READWRITE, |
|
CTLTYPE_INT, "sbmax", |
|
SYSCTL_DESCR("Maximum socket buffer size"), |
|
sysctl_kern_sbmax, 0, NULL, 0, |
|
CTL_KERN, KERN_SBMAX, CTL_EOL); |
} |
} |