PERFORCE change 150404 for review

Andrew Thompson thompsa at FreeBSD.org
Wed Sep 24 21:41:46 UTC 2008


http://perforce.freebsd.org/chv.cgi?CH=150404

Change 150404 by thompsa at thompsa_burger on 2008/09/24 21:40:48

	Update for the latest hooks.

Affected files ...

.. //depot/projects/mpsafetty/sys/netgraph/ng_tty.c#5 edit

Differences ...

==== //depot/projects/mpsafetty/sys/netgraph/ng_tty.c#5 (text+ko) ====

@@ -92,6 +92,8 @@
 };
 typedef struct ngt_softc *sc_p;
 
+static int ngt_unit;
+
 /* Flags */
 #define FLG_DEBUG		0x0002
 
@@ -100,6 +102,7 @@
 static ng_rcvmsg_t		ngt_rcvmsg;
 static ng_shutdown_t		ngt_shutdown;
 static ng_newhook_t		ngt_newhook;
+static ng_connect_t		ngt_connect;
 static ng_rcvdata_t		ngt_rcvdata;
 static ng_disconnect_t		ngt_disconnect;
 
@@ -109,6 +112,7 @@
 static th_getc_poll_t		ngt_getc_poll;
 static th_rint_bypass_t		ngt_rint_bypass;
 static th_rint_poll_t		ngt_rint_poll;
+static th_rint_done_t		ngt_rint_done;
 static th_close_t		ngt_close;
 
 static struct ttyhook ngt_hook = {
@@ -116,6 +120,7 @@
 	.th_getc_poll = ngt_getc_poll,
 	.th_rint_bypass	= ngt_rint_bypass,
 	.th_rint_poll = ngt_rint_poll,
+	.th_rint_done = ngt_rint_done,
 	.th_close = ngt_close,
 };
 
@@ -127,11 +132,15 @@
 	.rcvmsg =	ngt_rcvmsg,
 	.shutdown =	ngt_shutdown,
 	.newhook =	ngt_newhook,
+	.connect =	ngt_connect,
 	.rcvdata =	ngt_rcvdata,
 	.disconnect =	ngt_disconnect,
 };
 NETGRAPH_INIT(tty, &typestruct);
 
+#define	NGTLOCK(sc)	IF_LOCK(&sc->outq)
+#define	NGTUNLOCK(sc)	IF_UNLOCK(&sc->outq)
+
 /******************************************************************
 		    NETGRAPH NODE METHODS
 ******************************************************************/
@@ -146,6 +155,7 @@
 ngt_constructor(node_p node)
 {
 	sc_p sc;
+	char name[sizeof(NG_TTY_NODE_TYPE) + 8];
 
 	/* Allocate private structure */
 	MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO);
@@ -153,10 +163,18 @@
 		return (ENOMEM);
 
 	NG_NODE_SET_PRIVATE(node, sc);
+	sc->node = node;
 
 	mtx_init(&sc->outq.ifq_mtx, "ng_tty node+queue", NULL, MTX_DEF);
 	IFQ_SET_MAXLEN(&sc->outq, MAX_MBUFQ);
 
+	atomic_add_int(&ngt_unit, 1);
+	snprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit);
+
+	/* Assign node its name */
+	if (ng_name_node(node, name))
+		log(LOG_WARNING, "%s: can't name node %s\n",
+		    __func__, name);
 	/* Done */
 	return (0);
 }
@@ -169,24 +187,32 @@
 {
 	const sc_p sc = NG_NODE_PRIVATE(node);
 
-	/* TTY must be attached */
-	if (sc->tp == NULL)
-		return (EINVAL);
-
 	if (strcmp(name, NG_TTY_HOOK))
 		return (EINVAL);
 
 	if (sc->hook)
 		return (EISCONN);
 
-	tty_lock(sc->tp);
+	NGTLOCK(sc);
 	sc->hook = hook;
-	tty_unlock(sc->tp);
+	NGTUNLOCK(sc);
 
 	return (0);
 }
 
 /*
+ * Set the hook into queueing mode (for outgoing packets),
+ * so that we wont deliver mbuf thru the whole graph holding
+ * tty locks.
+ */
+static int
+ngt_connect(hook_p hook)
+{
+	NG_HOOK_FORCE_QUEUE(hook);
+	return (0);
+}
+
+/*
  * Disconnect the hook
  */
 static int
@@ -197,9 +223,9 @@
 	if (hook != sc->hook)
 		panic(__func__);
 
-	tty_lock(sc->tp);
+	NGTLOCK(sc);
 	sc->hook = NULL;
-	tty_unlock(sc->tp);
+	NGTUNLOCK(sc);
 
 	return (0);
 }
@@ -219,6 +245,8 @@
 		ttyhook_unregister(tp);
 	}
 	/* Free resources */
+	IF_DRAIN(&sc->outq);
+	mtx_destroy(&(sc)->outq.ifq_mtx);
 	NG_NODE_UNREF(sc->node);
 	FREE(sc, M_NETGRAPH);
 
@@ -241,7 +269,6 @@
 	case NGM_TTY_COOKIE:
 		switch (msg->header.cmd) {
 		case NGM_TTY_SET_TTY:
-			/* XXX Locking? or will netgraph serialize us? */
 			if (sc->tp != NULL)
 				return (EBUSY);
 			error = ttyhook_register(&sc->tp, td, *(int *)msg->data,
@@ -298,6 +325,11 @@
 	NGI_GET_M(item, m);
 	NG_FREE_ITEM(item);
 
+	if (tp == NULL) {
+		NG_FREE_M(m);
+		return (ENXIO);
+	}
+
 	IF_LOCK(&sc->outq);
 	if (_IF_QFULL(&sc->outq)) {
 		_IF_DROP(&sc->outq);
@@ -307,11 +339,11 @@
 	}
 
 	_IF_ENQUEUE(&sc->outq, m);
+	sc->outqlen += m->m_pkthdr.len;
 	IF_UNLOCK(&sc->outq);
 
 	/* notify the TTY that data is ready */
 	tty_lock(tp);
-	sc->outqlen += m->m_pkthdr.len;
 	if (!tty_gone(tp))
 		ttydevsw_outwakeup(tp);
 	tty_unlock(tp);
@@ -355,8 +387,11 @@
 			break;
 		}
 	}
+	IF_LOCK(&sc->outq);
 	sc->outqlen -= total;
+	IF_UNLOCK(&sc->outq);
 	MPASS(sc->outqlen >= 0);
+
 	return (total);
 }
 
@@ -410,9 +445,17 @@
 }
 
 static void
+ngt_rint_done(struct tty *tp)
+{
+	/* Do nothing */
+}
+
+static void
 ngt_close(struct tty *tp)
 {
-	/* XXX ??? */
-	ttyhook_unregister(tp);
+	sc_p sc = ttyhook_softc(tp);
+
+	/* Must be queued to drop the tty lock */
+	ng_rmnode_flags(sc->node, NG_QUEUE);
 }
 


More information about the p4-projects mailing list