xref: /freebsd/usr.sbin/ngctl/dot.c (revision 49b49cda41feabe3439f7318e8bf40e3896c7bf4)
1 
2 /*
3  * dot.c
4  *
5  * Copyright (c) 2004 Brian Fundakowski Feldman
6  * Copyright (c) 1996-1999 Whistle Communications, Inc.
7  * All rights reserved.
8  *
9  * Subject to the following obligations and disclaimer of warranty, use and
10  * redistribution of this software, in source or object code forms, with or
11  * without modifications are expressly permitted by Whistle Communications;
12  * provided, however, that:
13  * 1. Any and all reproductions of the source or object code must include the
14  *    copyright notice above and the following disclaimer of warranties; and
15  * 2. No rights are granted, in any manner or form, to use Whistle
16  *    Communications, Inc. trademarks, including the mark "WHISTLE
17  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18  *    such appears in the above copyright notice or in the software.
19  *
20  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36  * OF SUCH DAMAGE.
37  *
38  * $FreeBSD$
39  */
40 
41 #include <err.h>
42 #include <inttypes.h>
43 #include <netgraph.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 
48 #include "ngctl.h"
49 
50 #define UNNAMED		"\\<unnamed\\>"
51 
52 static int DotCmd(int ac, char **av);
53 
54 const struct ngcmd dot_cmd = {
55 	DotCmd,
56 	"dot [outputfile]",
57 	"Produce a GraphViz (.dot) of the entire netgraph.",
58 	"If no outputfile is specified, stdout will be assumed.",
59 	{ "graphviz", "confdot" }
60 };
61 
62 static int
63 DotCmd(int ac, char **av)
64 {
65 	struct ng_mesg *nlresp;
66 	struct namelist *nlist;
67 	FILE *f = stdout;
68 	int ch;
69 	u_int i;
70 
71 	/* Get options */
72 	optind = 1;
73 	while ((ch = getopt(ac, av, "")) != -1) {
74 		switch (ch) {
75 		case '?':
76 		default:
77 			return (CMDRTN_USAGE);
78 			break;
79 		}
80 	}
81 	ac -= optind;
82 	av += optind;
83 
84 	/* Get arguments */
85 	switch (ac) {
86 	case 1:
87 		f = fopen(av[0], "w");
88 		if (f == NULL) {
89 			warn("Could not open %s for writing", av[0]);
90 			return (CMDRTN_ERROR);
91 		}
92 	case 0:
93 		break;
94 	default:
95 		if (f != stdout)
96 			(void)fclose(f);
97 		return (CMDRTN_USAGE);
98 	}
99 
100 	/* Get list of nodes */
101 	if (NgSendMsg(csock, ".", NGM_GENERIC_COOKIE, NGM_LISTNODES, NULL,
102 	    0) < 0) {
103 		warn("send listnodes msg");
104 		goto error;
105 	}
106 	if (NgAllocRecvMsg(csock, &nlresp, NULL) < 0) {
107 		warn("recv listnodes msg");
108 		goto error;
109 	}
110 
111 	nlist = (struct namelist *)nlresp->data;
112 	fprintf(f, "graph netgraph {\n");
113 	/* TODO: implement rank = same or subgraphs at some point */
114 	fprintf(f, "\tedge [ weight = 1.0 ];\n");
115 	fprintf(f, "\tnode [ shape = record, fontsize = 12 ] {\n");
116 	for (i = 0; i < nlist->numnames; i++)
117 		fprintf(f, "\t\t\"%jx\" [ label = \"{%s:|{%s|[%jx]:}}\" ];\n",
118 		    (uintmax_t)nlist->nodeinfo[i].id,
119 		    nlist->nodeinfo[i].name[0] != '\0' ?
120 		    nlist->nodeinfo[i].name : UNNAMED,
121 		    nlist->nodeinfo[i].type, (uintmax_t)nlist->nodeinfo[i].id);
122 	fprintf(f, "\t};\n");
123 
124 	fprintf(f, "\tsubgraph cluster_disconnected {\n");
125 	fprintf(f, "\t\tbgcolor = pink;\n");
126 	for (i = 0; i < nlist->numnames; i++)
127 		if (nlist->nodeinfo[i].hooks == 0)
128 			fprintf(f, "\t\t\"%jx\";\n",
129 			    (uintmax_t)nlist->nodeinfo[i].id);
130 	fprintf(f, "\t};\n");
131 
132 	for (i = 0; i < nlist->numnames; i++) {
133 		struct ng_mesg *hlresp;
134 		struct hooklist *hlist;
135 		struct nodeinfo *ninfo;
136 		char path[NG_PATHSIZ];
137 		u_int j;
138 
139 		(void)snprintf(path, sizeof(path), "[%jx]:",
140 		    (uintmax_t)nlist->nodeinfo[i].id);
141 
142 		/* Get node info and hook list */
143 		if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
144 		    NULL, 0) < 0) {
145 			free(nlresp);
146 			warn("send listhooks msg");
147 			goto error;
148 		}
149 		if (NgAllocRecvMsg(csock, &hlresp, NULL) < 0) {
150 			free(nlresp);
151 			warn("recv listhooks msg");
152 			goto error;
153 		}
154 
155 		hlist = (struct hooklist *)hlresp->data;
156 		ninfo = &hlist->nodeinfo;
157 		if (ninfo->hooks == 0) {
158 			free(hlresp);
159 			continue;
160 		}
161 
162 		fprintf(f, "\tnode [ shape = octagon, fontsize = 10 ] {\n");
163 		for (j = 0; j < ninfo->hooks; j++)
164 			fprintf(f, "\t\t\"%jx.%s\" [ label = \"%s\" ];\n",
165 			    (uintmax_t)nlist->nodeinfo[i].id,
166 			    hlist->link[j].ourhook, hlist->link[j].ourhook);
167 		fprintf(f, "\t};\n");
168 
169 		fprintf(f, "\t{\n\t\tedge [ weight = 2.0, style = bold ];\n");
170 		for (j = 0; j < ninfo->hooks; j++)
171 			fprintf(f, "\t\t\"%jx\" -- \"%jx.%s\";\n",
172 			    (uintmax_t)nlist->nodeinfo[i].id,
173 			    (uintmax_t)nlist->nodeinfo[i].id,
174 			    hlist->link[j].ourhook);
175 		fprintf(f, "\t};\n");
176 
177 		for (j = 0; j < ninfo->hooks; j++) {
178 			/* Only print the edges going in one direction. */
179 			if (hlist->link[j].nodeinfo.id > nlist->nodeinfo[i].id)
180 				continue;
181 			fprintf(f, "\t\"%jx.%s\" -- \"%jx.%s\";\n",
182 			    (uintmax_t)nlist->nodeinfo[i].id,
183 			    hlist->link[j].ourhook,
184 			    (uintmax_t)hlist->link[j].nodeinfo.id,
185 			    hlist->link[j].peerhook);
186 		}
187 		free(hlresp);
188 	}
189 
190 	fprintf(f, "}\n");
191 
192 	free(nlresp);
193 	if (f != stdout)
194 		(void)fclose(f);
195 	return (CMDRTN_OK);
196 error:
197 	if (f != stdout)
198 		(void)fclose(f);
199 	return (CMDRTN_ERROR);
200 }
201