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