xref: /freebsd/usr.sbin/ngctl/dot.c (revision 52ec752989b2e6d4e9a59a8ff25d8ff596d85e62)
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 <inttypes.h>
42 
43 #include "ngctl.h"
44 
45 #define UNNAMED		"\\<unnamed\\>"
46 
47 static int DotCmd(int ac, char **av);
48 
49 const struct ngcmd dot_cmd = {
50 	DotCmd,
51 	"dot [outputfile]",
52 	"Produce a GraphViz (.dot) of the entire netgraph.",
53 	"If no outputfile is specified, stdout will be assumed.",
54 	{ "graphviz", "confdot" }
55 };
56 
57 static int
58 DotCmd(int ac, char **av)
59 {
60 	u_char nlrbuf[16 * 1024];
61 	struct ng_mesg *const nlresp = (struct ng_mesg *)nlrbuf;
62 	struct namelist *const nlist = (struct namelist *)nlresp->data;
63 	FILE *f = stdout;
64 	int ch, i;
65 
66 	/* Get options */
67 	optind = 1;
68 	while ((ch = getopt(ac, av, "")) != EOF) {
69 		switch (ch) {
70 		case '?':
71 		default:
72 			return (CMDRTN_USAGE);
73 			break;
74 		}
75 	}
76 	ac -= optind;
77 	av += optind;
78 
79 	/* Get arguments */
80 	switch (ac) {
81 	case 1:
82 		f = fopen(av[0], "w");
83 		if (f == NULL) {
84 			warn("Could not open %s for writing", av[0]);
85 			return (CMDRTN_ERROR);
86 		}
87 	case 0:
88 		break;
89 	default:
90 		if (f != stdout)
91 			(void)fclose(f);
92 		return (CMDRTN_USAGE);
93 	}
94 
95 	/* Get list of nodes */
96 	if (NgSendMsg(csock, ".", NGM_GENERIC_COOKIE, NGM_LISTNODES, NULL,
97 	    0) < 0) {
98 		warn("send listnodes msg");
99 		goto error;
100 	}
101 	if (NgRecvMsg(csock, nlresp, sizeof(nlrbuf), NULL) < 0) {
102 		warn("recv listnodes msg");
103 		goto error;
104 	}
105 
106 	fprintf(f, "graph netgraph {\n");
107 	/* TODO: implement rank = same or subgraphs at some point */
108 	fprintf(f, "\tedge [ weight = 1.0 ];\n");
109 	fprintf(f, "\tnode [ shape = record, fontsize = 12 ] {\n");
110 	for (i = 0; i < nlist->numnames; i++)
111 		fprintf(f, "\t\t\"%jx\" [ label = \"{%s:|{%s|[%jx]:}}\" ];\n",
112 		    (uintmax_t)nlist->nodeinfo[i].id,
113 		    nlist->nodeinfo[i].name[0] != '\0' ?
114 		    nlist->nodeinfo[i].name : UNNAMED,
115 		    nlist->nodeinfo[i].type, (uintmax_t)nlist->nodeinfo[i].id);
116 	fprintf(f, "\t};\n");
117 
118 	fprintf(f, "\tsubgraph cluster_disconnected {\n");
119 	fprintf(f, "\t\tbgcolor = pink;\n");
120 	for (i = 0; i < nlist->numnames; i++)
121 		if (nlist->nodeinfo[i].hooks == 0)
122 			fprintf(f, "\t\t\"%jx\";\n",
123 			    (uintmax_t)nlist->nodeinfo[i].id);
124 	fprintf(f, "\t};\n");
125 
126 	for (i = 0; i < nlist->numnames; i++) {
127 		u_char hlrbuf[16 * 1024];
128 		struct ng_mesg *const hlresp = (struct ng_mesg *)hlrbuf;
129 		struct hooklist *const hlist = (struct hooklist *)hlresp->data;
130 		struct nodeinfo *const ninfo = &hlist->nodeinfo;
131 		char path[NG_PATHSIZ];
132 		int j;
133 
134 		(void)snprintf(path, sizeof(path), "[%jx]:",
135 		    (uintmax_t)nlist->nodeinfo[i].id);
136 
137 		/* Get node info and hook list */
138 		if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
139 		    NULL, 0) < 0) {
140 			warn("send listhooks msg");
141 			goto error;
142 		}
143 		if (NgRecvMsg(csock, hlresp, sizeof(hlrbuf), NULL) < 0) {
144 			warn("recv listhooks msg");
145 			goto error;
146 		}
147 
148 		if (ninfo->hooks == 0)
149 			continue;
150 
151 		fprintf(f, "\tnode [ shape = octagon, fontsize = 10 ] {\n");
152 		for (j = 0; j < ninfo->hooks; j++)
153 			fprintf(f, "\t\t\"%jx.%s\" [ label = \"%s\" ];\n",
154 			    (uintmax_t)nlist->nodeinfo[i].id,
155 			    hlist->link[j].ourhook, hlist->link[j].ourhook);
156 		fprintf(f, "\t};\n");
157 
158 		fprintf(f, "\t{\n\t\tedge [ weight = 2.0, style = bold ];\n");
159 		for (j = 0; j < ninfo->hooks; j++)
160 			fprintf(f, "\t\t\"%jx\" -- \"%jx.%s\";\n",
161 			    (uintmax_t)nlist->nodeinfo[i].id,
162 			    (uintmax_t)nlist->nodeinfo[i].id,
163 			    hlist->link[j].ourhook);
164 		fprintf(f, "\t};\n");
165 
166 		for (j = 0; j < ninfo->hooks; j++) {
167 			/* Only print the edges going in one direction. */
168 			if (hlist->link[j].nodeinfo.id > nlist->nodeinfo[i].id)
169 				continue;
170 			fprintf(f, "\t\"%jx.%s\" -- \"%jx.%s\";\n",
171 			    (uintmax_t)nlist->nodeinfo[i].id,
172 			    hlist->link[j].ourhook,
173 			    (uintmax_t)hlist->link[j].nodeinfo.id,
174 			    hlist->link[j].peerhook);
175 		}
176 
177 	}
178 
179 	fprintf(f, "};\n");
180 
181 	if (f != stdout)
182 		(void)fclose(f);
183 	return (CMDRTN_OK);
184 error:
185 	if (f != stdout)
186 		(void)fclose(f);
187 	return (CMDRTN_ERROR);
188 }
189