Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 30 Aug 2005 19:00:19 GMT
From:      soc-anders <soc-anders@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 82861 for review
Message-ID:  <200508301900.j7UJ0Jla003804@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=82861

Change 82861 by soc-anders@soc-anders_gimli on 2005/08/30 19:00:04

	Created external version of the netgraph pcb structure and made
	it accessible via sysctl (net.graph.pcblist) and modified netstat 
	to obtain socket info using the new sysctl mib rather than kvm.
	
	Minor modifictions to libnetgraph to account for new dependencies. 

Affected files ...

.. //depot/projects/soc2005/ifcleanup/src/src/lib/libnetgraph/debug.c#2 edit
.. //depot/projects/soc2005/ifcleanup/src/src/lib/libnetgraph/msg.c#2 edit
.. //depot/projects/soc2005/ifcleanup/src/src/lib/libnetgraph/sock.c#2 edit
.. //depot/projects/soc2005/ifcleanup/src/src/sys/netgraph/ng_socket.c#2 edit
.. //depot/projects/soc2005/ifcleanup/src/src/sys/netgraph/ng_socket.h#2 edit
.. //depot/projects/soc2005/ifcleanup/src/src/usr.bin/netstat/netgraph.c#3 edit

Differences ...

==== //depot/projects/soc2005/ifcleanup/src/src/lib/libnetgraph/debug.c#2 (text+ko) ====

@@ -52,6 +52,7 @@
 #include <net/bpf.h>
 
 #include <netgraph/ng_message.h>
+#include <sys/socketvar.h> /* for xsocket */
 #include <netgraph/ng_socket.h>
 
 #include "netgraph.h"

==== //depot/projects/soc2005/ifcleanup/src/src/lib/libnetgraph/msg.c#2 (text+ko) ====

@@ -44,6 +44,7 @@
 #include <sys/types.h>
 #include <stdarg.h>
 #include <netgraph/ng_message.h>
+#include <sys/socketvar.h> /* for xsocket */
 #include <netgraph/ng_socket.h>
 
 #include "netgraph.h"

==== //depot/projects/soc2005/ifcleanup/src/src/lib/libnetgraph/sock.c#2 (text+ko) ====

@@ -44,6 +44,7 @@
 #include <sys/types.h>
 #include <stdarg.h>
 #include <netgraph/ng_message.h>
+#include <sys/socketvar.h>
 #include <netgraph/ng_socket.h>
 
 #include "netgraph.h"

==== //depot/projects/soc2005/ifcleanup/src/src/sys/netgraph/ng_socket.c#2 (text+ko) ====

@@ -67,6 +67,7 @@
 #include <sys/sx.h>
 #include <sys/sysctl.h>
 #include <sys/systm.h>
+#include <sys/proc.h>
 #ifdef NOTYET
 #include <sys/vnode.h>
 #endif
@@ -157,6 +158,9 @@
 
 /* List of all sockets */
 static LIST_HEAD(, ngpcb) ngsocklist;
+static int get_ngpcblist(SYSCTL_HANDLER_ARGS);
+SYSCTL_PROC(_net_graph, OID_AUTO, pcblist, CTLFLAG_RD, 0, 0,
+	    get_ngpcblist, "S,xngpcb", "List of netgraph sockets");
 
 static struct mtx	ngsocketlist_mtx;
 
@@ -1029,6 +1033,38 @@
 {
 	return (0);
 }
+
+static int 
+get_ngpcblist(SYSCTL_HANDLER_ARGS)
+{
+	struct ngpcb *ngp;
+	size_t xngsz = 0;
+	int error = 0;
+
+	mtx_lock(&ngsocketlist_mtx);
+	LIST_FOREACH(ngp, &ngsocklist, socks) {
+		struct xngpcb xng;
+		xngsz += sizeof(xng);
+
+		if (req->oldptr == NULL ||
+		    cr_canseesocket(req->td->td_ucred, ngp->ng_socket) != 0)
+			continue;
+
+		bzero(&xng, sizeof(xng));
+		xng.xng_type = ngp->type;
+		sotoxsocket(ngp->ng_socket, &xng.xng_socket);
+		if (ngp->sockdata)
+			xng.xng_node_ID = ngp->sockdata->node->nd_ID;
+
+		error = SYSCTL_OUT(req, &xng, sizeof(xng));
+	} 
+	mtx_unlock(&ngsocketlist_mtx);
+	
+	if (req->oldptr == NULL)
+		req->oldlen = xngsz;
+	return error;
+}
+
 /*
  * Control and data socket type descriptors
  */

==== //depot/projects/soc2005/ifcleanup/src/src/sys/netgraph/ng_socket.h#2 (text+ko) ====

@@ -44,6 +44,9 @@
 #ifndef _NETGRAPH_NG_SOCKET_H_
 #define _NETGRAPH_NG_SOCKET_H_
 
+#include <sys/socketvar.h>  /* XXX: temp. cludge */
+#include <netgraph/ng_message.h>
+
 /* Netgraph node type name and cookie */
 #define	NG_SOCKET_NODE_TYPE	"socket"
 #define	NGM_SOCKET_COOKIE	851601233
@@ -67,5 +70,13 @@
 	char    sg_data[14];	/* actually longer; address value */
 };
 
+/* External version of ngpcb for sysctl */
+struct xngpcb {
+	struct xsocket 	xng_socket; /* the socket */
+	int 		xng_type; /* NG_CONTROL or NG_DATA */
+	ng_ID_t 	xng_node_ID; /* node associated with socket */
+};
+
+
 #endif /* _NETGRAPH_NG_SOCKET_H_ */
 

==== //depot/projects/soc2005/ifcleanup/src/src/usr.bin/netstat/netgraph.c#3 (text+ko) ====

@@ -41,17 +41,17 @@
 #include <sys/socketvar.h>
 #include <sys/protosw.h>
 #include <sys/linker.h>
+#include <sys/sysctl.h>
 
 #include <net/route.h>
 
 #include <netgraph.h>
 #include <netgraph/ng_message.h>
 #include <netgraph/ng_socket.h>
-#include <netgraph/ng_socketvar.h>
 
-#include <nlist.h>
 #include <errno.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <err.h>
@@ -63,83 +63,39 @@
 void
 netgraphprotopr(u_long off, const char *name, int af1 __unused)
 {
-#ifdef IFCLEANUP
-	struct ngpcb *this, *next;
-	struct ngpcb ngpcb;
-	struct ngsock info;
-	struct socket sockb;
-	int debug = 1;
+	struct xngpcb *xng;
+	struct xsocket *sockb;
+	size_t len = 0;
+	char *buf;
+	int i;
 
-	/* If symbol not found, try looking in the KLD module */
-	if (off == 0) {
-		const char *const modname = "ng_socket.ko";
-/* XXX We should get "mpath" from "sysctl kern.module_path" */
-		const char *mpath[] = { "/", "/boot/", "/modules/", NULL };
-		struct nlist sym[] = { { "_ngsocklist" }, { NULL } };
-		const char **pre;
-		struct kld_file_stat ks;
-		int fileid;
-
-		/* See if module is loaded */
-		if ((fileid = kldfind(modname)) < 0) {
-			if (debug)
-				warn("kldfind(%s)", modname);
-			return;
-		}
-
-		/* Get module info */
-		memset(&ks, 0, sizeof(ks));
-		ks.version = sizeof(struct kld_file_stat);
-		if (kldstat(fileid, &ks) < 0) {
-			if (debug)
-				warn("kldstat(%d)", fileid);
-			return;
-		}
-
-		/* Get symbol table from module file */
-		for (pre = mpath; *pre; pre++) {
-			char path[MAXPATHLEN];
-
-			snprintf(path, sizeof(path), "%s%s", *pre, modname);
-			if (nlist(path, sym) == 0)
-				break;
-		}
-
-		/* Did we find it? */
-		if (sym[0].n_value == 0) {
-			if (debug)
-				warnx("%s not found", modname);
-			return;
-		}
-
-		/* Symbol found at load address plus symbol offset */
-		off = (u_long) ks.address + sym[0].n_value;
+	/* Get pcb list */
+	if (sysctlbyname("net.graph.pcblist", NULL, &len, NULL, 0) < 0)
+		return;
+	if ((buf = malloc(len)) == NULL)
+		err(1, "malloc");
+	if (sysctlbyname("net.graph.pcblist", buf, &len, NULL, 0) < 0) {
+		free(buf);
+		return;
 	}
-
-	/* Get pointer to first socket */
-	kread(off, (char *)&this, sizeof(this));
-
 	/* Get my own socket node */
 	if (csock == -1)
 		NgMkSockNode(NULL, &csock, NULL);
 
-	for (; this != NULL; this = next) {
+	len = len/sizeof(*xng);
+	for (i = 0,xng = (struct xngpcb *)buf; i < len; ++i, ++xng) {
 		u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)];
 		struct ng_mesg *resp = (struct ng_mesg *) rbuf;
 		struct nodeinfo *ni = (struct nodeinfo *) resp->data;
 		char path[64];
 
-		/* Read in ngpcb structure */
-		kread((u_long)this, (char *)&ngpcb, sizeof(ngpcb));
-		next = LIST_NEXT(&ngpcb, socks);
-
 		/* Read in socket structure */
-		kread((u_long)ngpcb.ng_socket, (char *)&sockb, sizeof(sockb));
+		sockb = &xng->xng_socket;
 
 		/* Check type of socket */
-		if (strcmp(name, "ctrl") == 0 && ngpcb.type != NG_CONTROL)
+		if (strcmp(name, "ctrl") == 0 && xng->xng_type != NG_CONTROL)
 			continue;
-		if (strcmp(name, "data") == 0 && ngpcb.type != NG_DATA)
+		if (strcmp(name, "data") == 0 && xng->xng_type != NG_DATA)
 			continue;
 
 		/* Do headline */
@@ -155,19 +111,18 @@
 
 		/* Show socket */
 		if (Aflag)
-			printf("%8lx ", (u_long) this);
+			printf("%8lx ", (u_long) sockb->xso_so);
 		printf("%-5.5s %6u %6u ",
-		    name, sockb.so_rcv.sb_cc, sockb.so_snd.sb_cc);
+		    name, sockb->so_rcv.sb_cc, sockb->so_snd.sb_cc);
 
 		/* Get ngsock structure */
-		if (ngpcb.sockdata == 0)	/* unconnected data socket */
+		if (xng->xng_node_ID == 0)	/* unconnected data socket */
 			goto finish;
-		kread((u_long)ngpcb.sockdata, (char *)&info, sizeof(info));
 
 		/* Get info on associated node */
-		if (info.node == 0 || csock == -1)
+		if (csock == -1)
 			goto finish;
-		snprintf(path, sizeof(path), "[%lx]:", (u_long) info.node);
+		snprintf(path, sizeof(path), "[%lx]:", (u_long)xng->xng_node_ID);
 		if (NgSendMsg(csock, path,
 		    NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0)
 			goto finish;
@@ -181,6 +136,7 @@
 finish:
 		putchar('\n');
 	}
-#endif /* IFCLEANUP */
+	if (buf)
+		free(buf);
 }
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200508301900.j7UJ0Jla003804>