[BACK]Return to uipc_socket.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / kern

Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.

Diff for /src/sys/kern/uipc_socket.c between version 1.199 and 1.207

version 1.199, 2009/12/30 06:58:50 version 1.207, 2012/01/25 00:28:36
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>
 #include <compat/sys/socket.h>  #include <compat/sys/socket.h>
 #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 142  int sock_loan_thresh = 4096;
Line 145  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 155  static kauth_listener_t socket_listener;
Line 158  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_somaxkva_setup(void);
 static struct sysctllog *socket_sysctllog;  static struct sysctllog *socket_sysctllog;
Line 168  sokvareserve(struct socket *so, vsize_t 
Line 172  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 275  sodoloanfree(struct vm_page **pgs, void 
Line 264  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 343  soloanfree(struct mbuf *m, void *buf, si
Line 321  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 413  sokva_reclaim_callback(struct callback_e
Line 391  sokva_reclaim_callback(struct callback_e
         KASSERT(ce == &sokva_reclaimerentry);          KASSERT(ce == &sokva_reclaimerentry);
         KASSERT(obj == NULL);          KASSERT(obj == NULL);
   
         sodopendfree();  
         if (!vm_map_starved_p(kernel_map)) {          if (!vm_map_starved_p(kernel_map)) {
                 return CALLBACK_CHAIN_ABORT;                  return CALLBACK_CHAIN_ABORT;
         }          }
Line 461  socket_listener_cb(kauth_cred_t cred, ka
Line 438  socket_listener_cb(kauth_cred_t cred, ka
   
         case KAUTH_REQ_NETWORK_SOCKET_OPEN:          case KAUTH_REQ_NETWORK_SOCKET_OPEN:
                 /* We allow "raw" routing/bluetooth sockets to anyone. */                  /* We allow "raw" routing/bluetooth sockets to anyone. */
                 if ((u_long)arg1 == PF_ROUTE || (u_long)arg1 == PF_BLUETOOTH)                  if ((u_long)arg1 == PF_ROUTE || (u_long)arg1 == PF_OROUTE
                       || (u_long)arg1 == PF_BLUETOOTH) {
                         result = KAUTH_RESULT_ALLOW;                          result = KAUTH_RESULT_ALLOW;
                 else {                  } else {
                         /* Privileged, let secmodel handle this. */                          /* Privileged, let secmodel handle this. */
                         if ((u_long)arg2 == SOCK_RAW)                          if ((u_long)arg2 == SOCK_RAW)
                                 break;                                  break;
Line 494  soinit(void)
Line 472  soinit(void)
         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. */
Line 507  soinit(void)
Line 486  soinit(void)
             socket_listener_cb, NULL);              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);
   }
   
 /*  /*
  * Socket operation routines.   * Socket operation routines.
  * These routines are called by the routines in   * These routines are called by the routines in
Line 561  socreate(int dom, struct socket **aso, i
Line 549  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. */
Line 596  fsocreate(int domain, struct socket **so
Line 583  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 751  soclose(struct socket *so)
Line 742  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, true, so->so_linger * hz);                                  error = sowait(so, true, so->so_linger * hz);
Line 873  sodisconnect(struct socket *so)
Line 865  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 906  sosend(struct socket *so, struct mbuf *a
Line 897  sosend(struct socket *so, struct mbuf *a
         short           wakeup_state = 0;          short           wakeup_state = 0;
   
         p = l->l_proc;          p = l->l_proc;
         sodopendfree();  
         clen = 0;          clen = 0;
   
         /*          /*
Line 973  sosend(struct socket *so, struct mbuf *a
Line 963  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;
                         }                          }
Line 1023  sosend(struct socket *so, struct mbuf *a
Line 1013  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 1182  soreceive(struct socket *so, struct mbuf
Line 1172  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 1272  soreceive(struct socket *so, struct mbuf
Line 1259  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;
                 }                  }
Line 1377  soreceive(struct socket *so, struct mbuf
Line 1365  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 1730  sosetopt1(struct socket *so, const struc
Line 1720  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  #ifdef SO_OTIMESTAMP
         case SO_OTIMESTAMP:          case SO_OTIMESTAMP:
 #endif  #endif
Line 1930  sogetopt1(struct socket *so, struct sock
Line 1921  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  #ifdef SO_OTIMESTAMP
         case SO_OTIMESTAMP:          case SO_OTIMESTAMP:
 #endif  #endif

Legend:
Removed from v.1.199  
changed lines
  Added in v.1.207

CVSweb <webmaster@jp.NetBSD.org>