Changed socket code to use 4.4BSD queue macros. This includes removing
the obsolete soqinsque and soqremque functions as well as collapsing so_q0len and so_qlen into a single queue length of unaccepted connections. Now the queue of unaccepted & complete connections is checked directly for queued sockets. The new code should be functionally equivilent to the old while being substantially faster - especially in cases where large numbers of connections are often queued for accept (e.g. http).
This commit is contained in:
parent
2ee45d7d28
commit
be24e9e8fa
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=14547
@ -31,7 +31,7 @@
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93
|
||||
* $Id: uipc_socket2.c,v 1.7 1995/12/14 22:51:02 bde Exp $
|
||||
* $Id: uipc_socket2.c,v 1.8 1996/01/05 21:41:54 wollman Exp $
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
@ -107,8 +107,11 @@ soisconnected(so)
|
||||
|
||||
so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
|
||||
so->so_state |= SS_ISCONNECTED;
|
||||
if (head && soqremque(so, 0)) {
|
||||
soqinsque(head, so, 1);
|
||||
if (head && (so->so_state & SS_INCOMP)) {
|
||||
TAILQ_REMOVE(&head->so_incomp, so, so_list);
|
||||
so->so_state &= ~SS_INCOMP;
|
||||
TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
|
||||
so->so_state |= SS_COMP;
|
||||
sorwakeup(head);
|
||||
wakeup((caddr_t)&head->so_timeo);
|
||||
} else {
|
||||
@ -161,12 +164,13 @@ sonewconn1(head, connstatus)
|
||||
register struct socket *so;
|
||||
int soqueue = connstatus ? 1 : 0;
|
||||
|
||||
if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2)
|
||||
if (head->so_qlen > 3 * head->so_qlimit / 2)
|
||||
return ((struct socket *)0);
|
||||
MALLOC(so, struct socket *, sizeof(*so), M_SOCKET, M_DONTWAIT);
|
||||
if (so == NULL)
|
||||
return ((struct socket *)0);
|
||||
bzero((caddr_t)so, sizeof(*so));
|
||||
so->so_head = head;
|
||||
so->so_type = head->so_type;
|
||||
so->so_options = head->so_options &~ SO_ACCEPTCONN;
|
||||
so->so_linger = head->so_linger;
|
||||
@ -175,10 +179,22 @@ sonewconn1(head, connstatus)
|
||||
so->so_timeo = head->so_timeo;
|
||||
so->so_pgid = head->so_pgid;
|
||||
(void) soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat);
|
||||
soqinsque(head, so, soqueue);
|
||||
if (connstatus) {
|
||||
TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
|
||||
so->so_state |= SS_COMP;
|
||||
} else {
|
||||
TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
|
||||
so->so_state |= SS_INCOMP;
|
||||
}
|
||||
head->so_qlen++;
|
||||
if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH,
|
||||
(struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)) {
|
||||
(void) soqremque(so, soqueue);
|
||||
if (so->so_state & SS_COMP) {
|
||||
TAILQ_REMOVE(&head->so_comp, so, so_list);
|
||||
} else {
|
||||
TAILQ_REMOVE(&head->so_incomp, so, so_list);
|
||||
}
|
||||
head->so_qlen--;
|
||||
(void) free((caddr_t)so, M_SOCKET);
|
||||
return ((struct socket *)0);
|
||||
}
|
||||
@ -190,57 +206,6 @@ sonewconn1(head, connstatus)
|
||||
return (so);
|
||||
}
|
||||
|
||||
void
|
||||
soqinsque(head, so, q)
|
||||
register struct socket *head, *so;
|
||||
int q;
|
||||
{
|
||||
|
||||
register struct socket **prev;
|
||||
so->so_head = head;
|
||||
if (q == 0) {
|
||||
head->so_q0len++;
|
||||
so->so_q0 = 0;
|
||||
for (prev = &(head->so_q0); *prev; )
|
||||
prev = &((*prev)->so_q0);
|
||||
} else {
|
||||
head->so_qlen++;
|
||||
so->so_q = 0;
|
||||
for (prev = &(head->so_q); *prev; )
|
||||
prev = &((*prev)->so_q);
|
||||
}
|
||||
*prev = so;
|
||||
}
|
||||
|
||||
int
|
||||
soqremque(so, q)
|
||||
register struct socket *so;
|
||||
int q;
|
||||
{
|
||||
register struct socket *head, *prev, *next;
|
||||
|
||||
head = so->so_head;
|
||||
prev = head;
|
||||
for (;;) {
|
||||
next = q ? prev->so_q : prev->so_q0;
|
||||
if (next == so)
|
||||
break;
|
||||
if (next == 0)
|
||||
return (0);
|
||||
prev = next;
|
||||
}
|
||||
if (q == 0) {
|
||||
prev->so_q0 = next->so_q0;
|
||||
head->so_q0len--;
|
||||
} else {
|
||||
prev->so_q = next->so_q;
|
||||
head->so_qlen--;
|
||||
}
|
||||
next->so_q0 = next->so_q = 0;
|
||||
next->so_head = 0;
|
||||
return (1);
|
||||
}
|
||||
|
||||
/*
|
||||
* Socantsendmore indicates that no more data will be sent on the
|
||||
* socket; it would normally be applied to a socket when the user
|
||||
|
@ -31,10 +31,11 @@
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
|
||||
* $Id: uipc_socket.c,v 1.13 1995/12/14 22:51:01 bde Exp $
|
||||
* $Id: uipc_socket.c,v 1.15 1996/02/13 18:16:20 wollman Exp $
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/queue.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/proc.h>
|
||||
#include <sys/file.h>
|
||||
@ -82,6 +83,8 @@ socreate(dom, aso, type, proto, p)
|
||||
return (EPROTOTYPE);
|
||||
MALLOC(so, struct socket *, sizeof(*so), M_SOCKET, M_WAIT);
|
||||
bzero((caddr_t)so, sizeof(*so));
|
||||
TAILQ_INIT(&so->so_incomp);
|
||||
TAILQ_INIT(&so->so_comp);
|
||||
so->so_type = type;
|
||||
if (p->p_ucred->cr_uid == 0)
|
||||
so->so_state = SS_PRIV;
|
||||
@ -127,7 +130,7 @@ solisten(so, backlog)
|
||||
splx(s);
|
||||
return (error);
|
||||
}
|
||||
if (so->so_q == 0)
|
||||
if (so->so_comp.tqh_first == NULL)
|
||||
so->so_options |= SO_ACCEPTCONN;
|
||||
if (backlog < 0 || backlog > somaxconn)
|
||||
backlog = somaxconn;
|
||||
@ -140,13 +143,21 @@ void
|
||||
sofree(so)
|
||||
register struct socket *so;
|
||||
{
|
||||
struct socket *head = so->so_head;
|
||||
|
||||
if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
|
||||
return;
|
||||
if (so->so_head) {
|
||||
if (!soqremque(so, 0) && !soqremque(so, 1))
|
||||
panic("sofree dq");
|
||||
so->so_head = 0;
|
||||
if (head != NULL) {
|
||||
if (so->so_state & SS_INCOMP) {
|
||||
TAILQ_REMOVE(&head->so_incomp, so, so_list);
|
||||
} else if (so->so_state & SS_COMP) {
|
||||
TAILQ_REMOVE(&head->so_comp, so, so_list);
|
||||
} else {
|
||||
panic("sofree: not queued");
|
||||
}
|
||||
head->so_qlen--;
|
||||
so->so_state &= ~(SS_INCOMP|SS_COMP);
|
||||
so->so_head = NULL;
|
||||
}
|
||||
sbrelease(&so->so_snd);
|
||||
sorflush(so);
|
||||
@ -166,10 +177,12 @@ soclose(so)
|
||||
int error = 0;
|
||||
|
||||
if (so->so_options & SO_ACCEPTCONN) {
|
||||
while (so->so_q0)
|
||||
(void) soabort(so->so_q0);
|
||||
while (so->so_q)
|
||||
(void) soabort(so->so_q);
|
||||
struct socket *sp;
|
||||
|
||||
for (sp = so->so_incomp.tqh_first; sp != NULL; sp = sp->so_list.tqe_next)
|
||||
(void) soabort(sp);
|
||||
for (sp = so->so_comp.tqh_first; sp != NULL; sp = sp->so_list.tqe_next)
|
||||
(void) soabort(sp);
|
||||
}
|
||||
if (so->so_pcb == 0)
|
||||
goto discard;
|
||||
|
@ -31,7 +31,7 @@
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93
|
||||
* $Id: uipc_socket2.c,v 1.7 1995/12/14 22:51:02 bde Exp $
|
||||
* $Id: uipc_socket2.c,v 1.8 1996/01/05 21:41:54 wollman Exp $
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
@ -107,8 +107,11 @@ soisconnected(so)
|
||||
|
||||
so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
|
||||
so->so_state |= SS_ISCONNECTED;
|
||||
if (head && soqremque(so, 0)) {
|
||||
soqinsque(head, so, 1);
|
||||
if (head && (so->so_state & SS_INCOMP)) {
|
||||
TAILQ_REMOVE(&head->so_incomp, so, so_list);
|
||||
so->so_state &= ~SS_INCOMP;
|
||||
TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
|
||||
so->so_state |= SS_COMP;
|
||||
sorwakeup(head);
|
||||
wakeup((caddr_t)&head->so_timeo);
|
||||
} else {
|
||||
@ -161,12 +164,13 @@ sonewconn1(head, connstatus)
|
||||
register struct socket *so;
|
||||
int soqueue = connstatus ? 1 : 0;
|
||||
|
||||
if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2)
|
||||
if (head->so_qlen > 3 * head->so_qlimit / 2)
|
||||
return ((struct socket *)0);
|
||||
MALLOC(so, struct socket *, sizeof(*so), M_SOCKET, M_DONTWAIT);
|
||||
if (so == NULL)
|
||||
return ((struct socket *)0);
|
||||
bzero((caddr_t)so, sizeof(*so));
|
||||
so->so_head = head;
|
||||
so->so_type = head->so_type;
|
||||
so->so_options = head->so_options &~ SO_ACCEPTCONN;
|
||||
so->so_linger = head->so_linger;
|
||||
@ -175,10 +179,22 @@ sonewconn1(head, connstatus)
|
||||
so->so_timeo = head->so_timeo;
|
||||
so->so_pgid = head->so_pgid;
|
||||
(void) soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat);
|
||||
soqinsque(head, so, soqueue);
|
||||
if (connstatus) {
|
||||
TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
|
||||
so->so_state |= SS_COMP;
|
||||
} else {
|
||||
TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
|
||||
so->so_state |= SS_INCOMP;
|
||||
}
|
||||
head->so_qlen++;
|
||||
if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH,
|
||||
(struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)) {
|
||||
(void) soqremque(so, soqueue);
|
||||
if (so->so_state & SS_COMP) {
|
||||
TAILQ_REMOVE(&head->so_comp, so, so_list);
|
||||
} else {
|
||||
TAILQ_REMOVE(&head->so_incomp, so, so_list);
|
||||
}
|
||||
head->so_qlen--;
|
||||
(void) free((caddr_t)so, M_SOCKET);
|
||||
return ((struct socket *)0);
|
||||
}
|
||||
@ -190,57 +206,6 @@ sonewconn1(head, connstatus)
|
||||
return (so);
|
||||
}
|
||||
|
||||
void
|
||||
soqinsque(head, so, q)
|
||||
register struct socket *head, *so;
|
||||
int q;
|
||||
{
|
||||
|
||||
register struct socket **prev;
|
||||
so->so_head = head;
|
||||
if (q == 0) {
|
||||
head->so_q0len++;
|
||||
so->so_q0 = 0;
|
||||
for (prev = &(head->so_q0); *prev; )
|
||||
prev = &((*prev)->so_q0);
|
||||
} else {
|
||||
head->so_qlen++;
|
||||
so->so_q = 0;
|
||||
for (prev = &(head->so_q); *prev; )
|
||||
prev = &((*prev)->so_q);
|
||||
}
|
||||
*prev = so;
|
||||
}
|
||||
|
||||
int
|
||||
soqremque(so, q)
|
||||
register struct socket *so;
|
||||
int q;
|
||||
{
|
||||
register struct socket *head, *prev, *next;
|
||||
|
||||
head = so->so_head;
|
||||
prev = head;
|
||||
for (;;) {
|
||||
next = q ? prev->so_q : prev->so_q0;
|
||||
if (next == so)
|
||||
break;
|
||||
if (next == 0)
|
||||
return (0);
|
||||
prev = next;
|
||||
}
|
||||
if (q == 0) {
|
||||
prev->so_q0 = next->so_q0;
|
||||
head->so_q0len--;
|
||||
} else {
|
||||
prev->so_q = next->so_q;
|
||||
head->so_qlen--;
|
||||
}
|
||||
next->so_q0 = next->so_q = 0;
|
||||
next->so_head = 0;
|
||||
return (1);
|
||||
}
|
||||
|
||||
/*
|
||||
* Socantsendmore indicates that no more data will be sent on the
|
||||
* socket; it would normally be applied to a socket when the user
|
||||
|
@ -31,7 +31,7 @@
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
|
||||
* $Id: uipc_syscalls.c,v 1.14 1996/02/13 18:16:21 wollman Exp $
|
||||
* $Id: uipc_syscalls.c,v 1.15 1996/02/24 13:38:07 phk Exp $
|
||||
*/
|
||||
|
||||
#include "opt_ktrace.h"
|
||||
@ -167,7 +167,7 @@ accept1(p, uap, retval, compat)
|
||||
struct file *fp;
|
||||
struct mbuf *nam;
|
||||
int namelen, error, s;
|
||||
register struct socket *so;
|
||||
struct socket *head, *so;
|
||||
|
||||
if (uap->name) {
|
||||
error = copyin((caddr_t)uap->anamelen, (caddr_t)&namelen,
|
||||
@ -179,30 +179,30 @@ accept1(p, uap, retval, compat)
|
||||
if (error)
|
||||
return (error);
|
||||
s = splnet();
|
||||
so = (struct socket *)fp->f_data;
|
||||
if ((so->so_options & SO_ACCEPTCONN) == 0) {
|
||||
head = (struct socket *)fp->f_data;
|
||||
if ((head->so_options & SO_ACCEPTCONN) == 0) {
|
||||
splx(s);
|
||||
return (EINVAL);
|
||||
}
|
||||
if ((so->so_state & SS_NBIO) && so->so_qlen == 0) {
|
||||
if ((head->so_state & SS_NBIO) && head->so_comp.tqh_first == NULL) {
|
||||
splx(s);
|
||||
return (EWOULDBLOCK);
|
||||
}
|
||||
while (so->so_qlen == 0 && so->so_error == 0) {
|
||||
if (so->so_state & SS_CANTRCVMORE) {
|
||||
so->so_error = ECONNABORTED;
|
||||
while (head->so_comp.tqh_first == NULL && head->so_error == 0) {
|
||||
if (head->so_state & SS_CANTRCVMORE) {
|
||||
head->so_error = ECONNABORTED;
|
||||
break;
|
||||
}
|
||||
error = tsleep((caddr_t)&so->so_timeo, PSOCK | PCATCH,
|
||||
error = tsleep((caddr_t)&head->so_timeo, PSOCK | PCATCH,
|
||||
"accept", 0);
|
||||
if (error) {
|
||||
splx(s);
|
||||
return (error);
|
||||
}
|
||||
}
|
||||
if (so->so_error) {
|
||||
error = so->so_error;
|
||||
so->so_error = 0;
|
||||
if (head->so_error) {
|
||||
error = head->so_error;
|
||||
head->so_error = 0;
|
||||
splx(s);
|
||||
return (error);
|
||||
}
|
||||
@ -211,11 +211,15 @@ accept1(p, uap, retval, compat)
|
||||
splx(s);
|
||||
return (error);
|
||||
}
|
||||
{ struct socket *aso = so->so_q;
|
||||
if (soqremque(aso, 1) == 0)
|
||||
panic("accept");
|
||||
so = aso;
|
||||
}
|
||||
|
||||
so = head->so_comp.tqh_first;
|
||||
if (so == NULL)
|
||||
panic("accept: nothing queued");
|
||||
TAILQ_REMOVE(&head->so_comp, so, so_list);
|
||||
so->so_state &= ~SS_COMP;
|
||||
so->so_head = NULL;
|
||||
head->so_qlen--;
|
||||
|
||||
fp->f_type = DTYPE_SOCKET;
|
||||
fp->f_flag = FREAD|FWRITE;
|
||||
fp->f_ops = &socketops;
|
||||
|
@ -31,7 +31,7 @@
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* @(#)socketvar.h 8.3 (Berkeley) 2/19/95
|
||||
* $Id: socketvar.h,v 1.16 1996/02/29 00:07:13 hsu Exp $
|
||||
* $Id: socketvar.h,v 1.12 1996/03/11 02:22:23 hsu Exp $
|
||||
*/
|
||||
|
||||
#ifndef _SYS_SOCKETVAR_H_
|
||||
@ -66,10 +66,10 @@ struct socket {
|
||||
* and limit on number of queued connections for this socket.
|
||||
*/
|
||||
struct socket *so_head; /* back pointer to accept socket */
|
||||
struct socket *so_q0; /* queue of partial connections */
|
||||
struct socket *so_q; /* queue of incoming connections */
|
||||
short so_q0len; /* partials on so_q0 */
|
||||
short so_qlen; /* number of connections on so_q */
|
||||
TAILQ_HEAD(, socket) so_incomp; /* queue of partial unaccepted connections */
|
||||
TAILQ_HEAD(, socket) so_comp; /* queue of complete unaccepted connections */
|
||||
TAILQ_ENTRY(socket) so_list; /* list of unaccepted connections */
|
||||
short so_qlen; /* number of unaccepted connections */
|
||||
short so_qlimit; /* max number queued connections */
|
||||
short so_timeo; /* connection timeout */
|
||||
u_short so_error; /* error affecting connection */
|
||||
@ -106,18 +106,21 @@ struct socket {
|
||||
/*
|
||||
* Socket state bits.
|
||||
*/
|
||||
#define SS_NOFDREF 0x001 /* no file table ref any more */
|
||||
#define SS_ISCONNECTED 0x002 /* socket connected to a peer */
|
||||
#define SS_ISCONNECTING 0x004 /* in process of connecting to peer */
|
||||
#define SS_ISDISCONNECTING 0x008 /* in process of disconnecting */
|
||||
#define SS_CANTSENDMORE 0x010 /* can't send more data to peer */
|
||||
#define SS_CANTRCVMORE 0x020 /* can't receive more data from peer */
|
||||
#define SS_RCVATMARK 0x040 /* at mark on input */
|
||||
#define SS_NOFDREF 0x0001 /* no file table ref any more */
|
||||
#define SS_ISCONNECTED 0x0002 /* socket connected to a peer */
|
||||
#define SS_ISCONNECTING 0x0004 /* in process of connecting to peer */
|
||||
#define SS_ISDISCONNECTING 0x0008 /* in process of disconnecting */
|
||||
#define SS_CANTSENDMORE 0x0010 /* can't send more data to peer */
|
||||
#define SS_CANTRCVMORE 0x0020 /* can't receive more data from peer */
|
||||
#define SS_RCVATMARK 0x0040 /* at mark on input */
|
||||
|
||||
#define SS_PRIV 0x080 /* privileged for broadcast, raw... */
|
||||
#define SS_NBIO 0x100 /* non-blocking ops */
|
||||
#define SS_ASYNC 0x200 /* async i/o notify */
|
||||
#define SS_ISCONFIRMING 0x400 /* deciding to accept connection req */
|
||||
#define SS_PRIV 0x0080 /* privileged for broadcast, raw... */
|
||||
#define SS_NBIO 0x0100 /* non-blocking ops */
|
||||
#define SS_ASYNC 0x0200 /* async i/o notify */
|
||||
#define SS_ISCONFIRMING 0x0400 /* deciding to accept connection req */
|
||||
|
||||
#define SS_INCOMP 0x0800 /* unaccepted, incomplete connection */
|
||||
#define SS_COMP 0x1000 /* unaccepted, complete connection */
|
||||
|
||||
|
||||
/*
|
||||
@ -142,7 +145,7 @@ struct socket {
|
||||
#define soreadable(so) \
|
||||
((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
|
||||
((so)->so_state & SS_CANTRCVMORE) || \
|
||||
(so)->so_qlen || (so)->so_error)
|
||||
(so)->so_comp.tqh_first || (so)->so_error)
|
||||
|
||||
/* can we write something to so? */
|
||||
#define sowriteable(so) \
|
||||
@ -253,8 +256,6 @@ void soisdisconnecting __P((struct socket *so));
|
||||
int solisten __P((struct socket *so, int backlog));
|
||||
struct socket *
|
||||
sonewconn1 __P((struct socket *head, int connstatus));
|
||||
void soqinsque __P((struct socket *head, struct socket *so, int q));
|
||||
int soqremque __P((struct socket *so, int q));
|
||||
int soreceive __P((struct socket *so, struct mbuf **paddr, struct uio *uio,
|
||||
struct mbuf **mp0, struct mbuf **controlp, int *flagsp));
|
||||
int soreserve __P((struct socket *so, u_long sndcc, u_long rcvcc));
|
||||
|
Loading…
Reference in New Issue
Block a user