Skip to content

Commit

Permalink
split PRU_CONNECT2 & PRU_PURGEIF function out of pr_generic() usrreq
Browse files Browse the repository at this point in the history
switches and put into separate functions

  - always KASSERT(solocked(so)) even if not implemented
    (for PRU_CONNECT2 only)

  - replace calls to pr_generic() with req = PRU_CONNECT2 with calls to
    pr_connect2()

  - replace calls to pr_generic() with req = PRU_PURGEIF with calls to
    pr_purgeif()

put common code from unp_connect2() (used by unp_connect() into
unp_connect1() and call out to it when needed

patch only briefly reviewed by rmind@
  • Loading branch information
rtr committed Aug 9, 2014
1 parent 8cb7902 commit c47a59c
Show file tree
Hide file tree
Showing 24 changed files with 628 additions and 359 deletions.
7 changes: 3 additions & 4 deletions sys/kern/uipc_socket.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: uipc_socket.c,v 1.233 2014/08/08 03:05:45 rtr Exp $ */
/* $NetBSD: uipc_socket.c,v 1.234 2014/08/09 05:33:00 rtr Exp $ */

/*-
* Copyright (c) 2002, 2007, 2008, 2009 The NetBSD Foundation, Inc.
Expand Down Expand Up @@ -71,7 +71,7 @@
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.233 2014/08/08 03:05:45 rtr Exp $");
__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.234 2014/08/09 05:33:00 rtr Exp $");

#include "opt_compat_netbsd.h"
#include "opt_sock_counters.h"
Expand Down Expand Up @@ -836,8 +836,7 @@ soconnect2(struct socket *so1, struct socket *so2)
{
KASSERT(solocked2(so1, so2));

return (*so1->so_proto->pr_usrreqs->pr_generic)(so1,
PRU_CONNECT2, NULL, (struct mbuf *)so2, NULL, NULL);
return (*so1->so_proto->pr_usrreqs->pr_connect2)(so1, so2);
}

int
Expand Down
6 changes: 3 additions & 3 deletions sys/kern/uipc_syscalls.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: uipc_syscalls.c,v 1.171 2014/07/09 04:54:03 rtr Exp $ */
/* $NetBSD: uipc_syscalls.c,v 1.172 2014/08/09 05:33:00 rtr Exp $ */

/*-
* Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
Expand Down Expand Up @@ -61,7 +61,7 @@
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.171 2014/07/09 04:54:03 rtr Exp $");
__KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.172 2014/08/09 05:33:00 rtr Exp $");

#include "opt_pipe.h"

Expand Down Expand Up @@ -1276,7 +1276,7 @@ pipe1(struct lwp *l, register_t *retval, int flags)
wf->f_data = wso;
retval[1] = fd;
solock(wso);
error = unp_connect2(wso, rso, PRU_CONNECT2);
error = unp_connect2(wso, rso);
sounlock(wso);
if (error != 0)
goto free4;
Expand Down
166 changes: 104 additions & 62 deletions sys/kern/uipc_usrreq.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: uipc_usrreq.c,v 1.168 2014/08/08 03:05:45 rtr Exp $ */
/* $NetBSD: uipc_usrreq.c,v 1.169 2014/08/09 05:33:00 rtr Exp $ */

/*-
* Copyright (c) 1998, 2000, 2004, 2008, 2009 The NetBSD Foundation, Inc.
Expand Down Expand Up @@ -96,7 +96,7 @@
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.168 2014/08/08 03:05:45 rtr Exp $");
__KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.169 2014/08/09 05:33:00 rtr Exp $");

#include <sys/param.h>
#include <sys/systm.h>
Expand Down Expand Up @@ -565,15 +565,14 @@ static int
unp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
struct mbuf *control, struct lwp *l)
{
struct unpcb *unp;
int error = 0;

KASSERT(req != PRU_ATTACH);
KASSERT(req != PRU_DETACH);
KASSERT(req != PRU_ACCEPT);
KASSERT(req != PRU_BIND);
KASSERT(req != PRU_LISTEN);
KASSERT(req != PRU_CONNECT);
KASSERT(req != PRU_CONNECT2);
KASSERT(req != PRU_DISCONNECT);
KASSERT(req != PRU_SHUTDOWN);
KASSERT(req != PRU_ABORT);
Expand All @@ -585,27 +584,16 @@ unp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
KASSERT(req != PRU_RCVOOB);
KASSERT(req != PRU_SEND);
KASSERT(req != PRU_SENDOOB);
KASSERT(req != PRU_PURGEIF);

KASSERT(solocked(so));
unp = sotounpcb(so);

KASSERT(!control);
if (unp == NULL) {
error = EINVAL;
goto release;
}
if (sotounpcb(so) == NULL)
return EINVAL;

switch (req) {
case PRU_CONNECT2:
error = unp_connect2(so, (struct socket *)nam, PRU_CONNECT2);
break;
panic("piusrreq");

default:
panic("piusrreq");
}

release:
return (error);
return 0;
}

/*
Expand Down Expand Up @@ -1088,6 +1076,56 @@ unp_abort(struct socket *so)
return 0;
}

static int
unp_connect1(struct socket *so, struct socket *so2)
{
struct unpcb *unp = sotounpcb(so);
struct unpcb *unp2;

if (so2->so_type != so->so_type)
return EPROTOTYPE;

/*
* All three sockets involved must be locked by same lock:
*
* local endpoint (so)
* remote endpoint (so2)
* queue head (so2->so_head, only if PR_CONNREQUIRED)
*/
KASSERT(solocked2(so, so2));
KASSERT(so->so_head == NULL);
if (so2->so_head != NULL) {
KASSERT(so2->so_lock == uipc_lock);
KASSERT(solocked2(so2, so2->so_head));
}

unp2 = sotounpcb(so2);
unp->unp_conn = unp2;
switch (so->so_type) {

case SOCK_DGRAM:
unp->unp_nextref = unp2->unp_refs;
unp2->unp_refs = unp;
soisconnected(so);
break;

case SOCK_SEQPACKET: /* FALLTHROUGH */
case SOCK_STREAM:

/*
* SOCK_SEQPACKET and SOCK_STREAM cases are handled by callers
* which are unp_connect() or unp_connect2().
*/

break;

default:
panic("unp_connect1");
}

return 0;
}

int
unp_connect(struct socket *so, struct mbuf *nam, struct lwp *l)
{
Expand Down Expand Up @@ -1182,7 +1220,38 @@ unp_connect(struct socket *so, struct mbuf *nam, struct lwp *l)
}
so2 = so3;
}
error = unp_connect2(so, so2, PRU_CONNECT);
error = unp_connect1(so, so2);
if (error) {
sounlock(so);
goto bad;
}
unp2 = sotounpcb(so2);
switch (so->so_type) {

/*
* SOCK_DGRAM and default cases are handled in prior call to
* unp_connect1(), do not add a default case without fixing
* unp_connect1().
*/

case SOCK_SEQPACKET: /* FALLTHROUGH */
case SOCK_STREAM:
unp2->unp_conn = unp;
if ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT)
soisconnecting(so);
else
soisconnected(so);
soisconnected(so2);
/*
* If the connection is fully established, break the
* association with uipc_lock and give the connected
* pair a seperate lock to share.
*/
KASSERT(so2->so_head != NULL);
unp_setpeerlocks(so, so2);
break;

}
sounlock(so);
bad:
vput(vp);
Expand All @@ -1194,64 +1263,36 @@ unp_connect(struct socket *so, struct mbuf *nam, struct lwp *l)
}

int
unp_connect2(struct socket *so, struct socket *so2, int req)
unp_connect2(struct socket *so, struct socket *so2)
{
struct unpcb *unp = sotounpcb(so);
struct unpcb *unp2;
int error = 0;

if (so2->so_type != so->so_type)
return (EPROTOTYPE);

/*
* All three sockets involved must be locked by same lock:
*
* local endpoint (so)
* remote endpoint (so2)
* queue head (so2->so_head, only if PR_CONNREQUIRED)
*/
KASSERT(solocked2(so, so2));
KASSERT(so->so_head == NULL);
if (so2->so_head != NULL) {
KASSERT(so2->so_lock == uipc_lock);
KASSERT(solocked2(so2, so2->so_head));
}

error = unp_connect1(so, so2);
if (error)
return error;

unp2 = sotounpcb(so2);
unp->unp_conn = unp2;
switch (so->so_type) {

case SOCK_DGRAM:
unp->unp_nextref = unp2->unp_refs;
unp2->unp_refs = unp;
soisconnected(so);
break;
/*
* SOCK_DGRAM and default cases are handled in prior call to
* unp_connect1(), do not add a default case without fixing
* unp_connect1().
*/

case SOCK_SEQPACKET: /* FALLTHROUGH */
case SOCK_STREAM:
unp2->unp_conn = unp;
if (req == PRU_CONNECT &&
((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT))
soisconnecting(so);
else
soisconnected(so);
soisconnected(so);
soisconnected(so2);
/*
* If the connection is fully established, break the
* association with uipc_lock and give the connected
* pair a seperate lock to share. For CONNECT2, we
* require that the locks already match (the sockets
* are created that way).
*/
if (req == PRU_CONNECT) {
KASSERT(so2->so_head != NULL);
unp_setpeerlocks(so, so2);
}
break;

default:
panic("unp_connect2");
}
return (0);
return error;
}

static void
Expand Down Expand Up @@ -1928,6 +1969,7 @@ const struct pr_usrreqs unp_usrreqs = {
.pr_bind = unp_bind,
.pr_listen = unp_listen,
.pr_connect = unp_connect,
.pr_connect2 = unp_connect2,
.pr_disconnect = unp_disconnect,
.pr_shutdown = unp_shutdown,
.pr_abort = unp_abort,
Expand Down
6 changes: 3 additions & 3 deletions sys/miscfs/fifofs/fifo_vnops.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: fifo_vnops.c,v 1.76 2014/07/25 08:20:52 dholland Exp $ */
/* $NetBSD: fifo_vnops.c,v 1.77 2014/08/09 05:33:01 rtr Exp $ */

/*-
* Copyright (c) 2008 The NetBSD Foundation, Inc.
Expand Down Expand Up @@ -58,7 +58,7 @@
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: fifo_vnops.c,v 1.76 2014/07/25 08:20:52 dholland Exp $");
__KERNEL_RCSID(0, "$NetBSD: fifo_vnops.c,v 1.77 2014/08/09 05:33:01 rtr Exp $");

#include <sys/param.h>
#include <sys/systm.h>
Expand Down Expand Up @@ -149,7 +149,7 @@ fifo_open(void *v)
}
fip->fi_writesock = wso;
solock(wso);
if ((error = unp_connect2(wso, rso, PRU_CONNECT2)) != 0) {
if ((error = unp_connect2(wso, rso)) != 0) {
sounlock(wso);
(void)soclose(wso);
(void)soclose(rso);
Expand Down
12 changes: 4 additions & 8 deletions sys/net/if.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: if.c,v 1.289 2014/07/31 06:35:47 ozaki-r Exp $ */
/* $NetBSD: if.c,v 1.290 2014/08/09 05:33:01 rtr Exp $ */

/*-
* Copyright (c) 1999, 2000, 2001, 2008 The NetBSD Foundation, Inc.
Expand Down Expand Up @@ -90,7 +90,7 @@
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if.c,v 1.289 2014/07/31 06:35:47 ozaki-r Exp $");
__KERNEL_RCSID(0, "$NetBSD: if.c,v 1.290 2014/08/09 05:33:01 rtr Exp $");

#include "opt_inet.h"

Expand Down Expand Up @@ -800,9 +800,7 @@ if_detach(struct ifnet *ifp)
pr < dp->dom_protoswNPROTOSW; pr++) {
so.so_proto = pr;
if (pr->pr_usrreqs) {
(void) (*pr->pr_usrreqs->pr_generic)(&so,
PRU_PURGEIF, NULL, NULL,
(struct mbuf *) ifp, curlwp);
(void) (*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
purged = 1;
}
}
Expand Down Expand Up @@ -852,9 +850,7 @@ if_detach(struct ifnet *ifp)
for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
so.so_proto = pr;
if (pr->pr_usrreqs && pr->pr_flags & PR_PURGEIF)
(void)(*pr->pr_usrreqs->pr_generic)(&so,
PRU_PURGEIF, NULL, NULL,
(struct mbuf *)ifp, curlwp);
(void)(*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
}
}

Expand Down
Loading

0 comments on commit c47a59c

Please sign in to comment.