Add a GraphViz-exporting ngctl(8) "dot" command. You can now create

very useful .dot files of your netgraph(4) to quickly visualize the
nodes, hooks and edges.  An example of this can be found here:
	http://people.freebsd.org/~green/sample-netgraph-dot.ps
If anyone would like to refine the output further, please do so.
This commit is contained in:
Brian Feldman 2004-01-09 03:19:40 +00:00
parent b712e9ec66
commit bbd6d60a85
5 changed files with 203 additions and 11 deletions

View File

@ -3,7 +3,7 @@
PROG= ngctl
MAN= ngctl.8
SRCS= main.c mkpeer.c config.c connect.c name.c show.c list.c \
SRCS= main.c mkpeer.c config.c connect.c dot.c name.c show.c list.c \
msg.c debug.c shutdown.c rmhook.c status.c types.c write.c
DPADD= ${LIBNETGRAPH}

188
usr.sbin/ngctl/dot.c Normal file
View File

@ -0,0 +1,188 @@
/*
* dot.c
*
* Copyright (c) 2004 Brian Fundakowski Feldman
* Copyright (c) 1996-1999 Whistle Communications, Inc.
* All rights reserved.
*
* Subject to the following obligations and disclaimer of warranty, use and
* redistribution of this software, in source or object code forms, with or
* without modifications are expressly permitted by Whistle Communications;
* provided, however, that:
* 1. Any and all reproductions of the source or object code must include the
* copyright notice above and the following disclaimer of warranties; and
* 2. No rights are granted, in any manner or form, to use Whistle
* Communications, Inc. trademarks, including the mark "WHISTLE
* COMMUNICATIONS" on advertising, endorsements, or otherwise except as
* such appears in the above copyright notice or in the software.
*
* THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
* TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
* REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
* INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
* WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
* REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
* SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
* IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
* RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
* WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <inttypes.h>
#include "ngctl.h"
#define UNNAMED "\\<unnamed\\>"
static int DotCmd(int ac, char **av);
const struct ngcmd dot_cmd = {
DotCmd,
"dot [outputfile]",
"Produce a GraphViz (.dot) of the entire netgraph.",
"If no outputfile is specified, stdout will be assumed.",
{ "graphviz", "confdot" }
};
static int
DotCmd(int ac, char **av)
{
u_char nlrbuf[16 * 1024];
struct ng_mesg *const nlresp = (struct ng_mesg *)nlrbuf;
struct namelist *const nlist = (struct namelist *)nlresp->data;
FILE *f = stdout;
int ch, i;
/* Get options */
optind = 1;
while ((ch = getopt(ac, av, "")) != EOF) {
switch (ch) {
case '?':
default:
return (CMDRTN_USAGE);
break;
}
}
ac -= optind;
av += optind;
/* Get arguments */
switch (ac) {
case 1:
f = fopen(av[0], "w");
if (f == NULL) {
warn("Could not open %s for writing", av[0]);
return (CMDRTN_ERROR);
}
case 0:
break;
default:
if (f != stdout)
(void)fclose(f);
return (CMDRTN_USAGE);
}
/* Get list of nodes */
if (NgSendMsg(csock, ".", NGM_GENERIC_COOKIE, NGM_LISTNODES, NULL,
0) < 0) {
warn("send listnodes msg");
goto error;
}
if (NgRecvMsg(csock, nlresp, sizeof(nlrbuf), NULL) < 0) {
warn("recv listnodes msg");
goto error;
}
fprintf(f, "graph netgraph {\n");
/* TODO: implement rank = same or subgraphs at some point */
fprintf(f, "\tedge [ weight = 1.0 ];\n");
fprintf(f, "\tnode [ shape = record, fontsize = 12 ] {\n");
for (i = 0; i < nlist->numnames; i++)
fprintf(f, "\t\t\"%jx\" [ label = \"{%s:|{%s|[%jx]:}}\" ];\n",
(uintmax_t)nlist->nodeinfo[i].id,
nlist->nodeinfo[i].name[0] != '\0' ?
nlist->nodeinfo[i].name : UNNAMED,
nlist->nodeinfo[i].type, (uintmax_t)nlist->nodeinfo[i].id);
fprintf(f, "\t};\n");
fprintf(f, "\tsubgraph cluster_disconnected {\n");
fprintf(f, "\t\tbgcolor = pink;\n");
for (i = 0; i < nlist->numnames; i++)
if (nlist->nodeinfo[i].hooks == 0)
fprintf(f, "\t\t\"%jx\";\n",
(uintmax_t)nlist->nodeinfo[i].id);
fprintf(f, "\t};\n");
for (i = 0; i < nlist->numnames; i++) {
u_char hlrbuf[16 * 1024];
struct ng_mesg *const hlresp = (struct ng_mesg *)hlrbuf;
struct hooklist *const hlist = (struct hooklist *)hlresp->data;
struct nodeinfo *const ninfo = &hlist->nodeinfo;
char path[NG_PATHSIZ];
int j;
(void)snprintf(path, sizeof(path), "[%jx]:",
(uintmax_t)nlist->nodeinfo[i].id);
/* Get node info and hook list */
if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
NULL, 0) < 0) {
warn("send listhooks msg");
goto error;
}
if (NgRecvMsg(csock, hlresp, sizeof(hlrbuf), NULL) < 0) {
warn("recv listhooks msg");
goto error;
}
if (ninfo->hooks == 0)
continue;
fprintf(f, "\tnode [ shape = octagon, fontsize = 10 ] {\n");
for (j = 0; j < ninfo->hooks; j++)
fprintf(f, "\t\t\"%jx.%s\" [ label = \"%s\" ];\n",
(uintmax_t)nlist->nodeinfo[i].id,
hlist->link[j].ourhook, hlist->link[j].ourhook);
fprintf(f, "\t};\n");
fprintf(f, "\t{\n\t\tedge [ weight = 2.0, style = bold ];\n");
for (j = 0; j < ninfo->hooks; j++)
fprintf(f, "\t\t\"%jx\" -- \"%jx.%s\";\n",
(uintmax_t)nlist->nodeinfo[i].id,
(uintmax_t)nlist->nodeinfo[i].id,
hlist->link[j].ourhook);
fprintf(f, "\t};\n");
for (j = 0; j < ninfo->hooks; j++) {
/* Only print the edges going in one direction. */
if (hlist->link[j].nodeinfo.id > nlist->nodeinfo[i].id)
continue;
fprintf(f, "\t\"%jx.%s\" -- \"%jx.%s\";\n",
(uintmax_t)nlist->nodeinfo[i].id,
hlist->link[j].ourhook,
(uintmax_t)hlist->link[j].nodeinfo.id,
hlist->link[j].peerhook);
}
}
fprintf(f, "};\n");
if (f != stdout)
(void)fclose(f);
return (CMDRTN_OK);
error:
if (f != stdout)
(void)fclose(f);
return (CMDRTN_ERROR);
}

View File

@ -62,6 +62,7 @@ static const struct ngcmd *const cmds[] = {
&config_cmd,
&connect_cmd,
&debug_cmd,
&dot_cmd,
&help_cmd,
&list_cmd,
&mkpeer_cmd,

View File

@ -97,19 +97,21 @@ The currently supported commands in
are:
.Pp
.Bd -literal -offset indent -compact
connect Connects two nodes
config get or set configuration of node at <path>
connect Connects hook <peerhook> of the node at <relpath> to <hook>
debug Get/set debugging verbosity level
help Show command summary or get help on a command
dot Produce a GraphViz (.dot) of the entire netgraph.
help Show command summary or get more help on a specific command
list Show information about all nodes
mkpeer Create and connect a new node to an existing node
msg Send an ASCII formatted message to a node
name Assign a name to a node
mkpeer Create and connect a new node to the node at "path"
msg Send a netgraph control message to the node at "path"
name Assign name <name> to the node at <path>
read Read and execute commands from a file
rmhook Disconnect a node's hook
show Show information about a node
shutdown Shutdown a node
status Get human readable status from a node
types Show all installed node types
rmhook Disconnect hook "hook" of the node at "path"
show Show information about the node at <path>
shutdown Shutdown the node at <path>
status Get human readable status information from the node at <path>
types Show information about all installed node types
write Send a data packet down the hook named by "hook".
quit Exit program
.Ed

View File

@ -78,6 +78,7 @@ struct ngcmd {
extern const struct ngcmd config_cmd;
extern const struct ngcmd connect_cmd;
extern const struct ngcmd debug_cmd;
extern const struct ngcmd dot_cmd;
extern const struct ngcmd help_cmd;
extern const struct ngcmd list_cmd;
extern const struct ngcmd mkpeer_cmd;