xref: /freebsd/sbin/ifconfig/iflagg.c (revision c6db8143eda5c775467145ac73e8ebec47afdd8f)
1 /*-
2  */
3 
4 #ifndef lint
5 static const char rcsid[] =
6   "$FreeBSD$";
7 #endif /* not lint */
8 
9 #include <sys/param.h>
10 #include <sys/ioctl.h>
11 #include <sys/socket.h>
12 #include <sys/sockio.h>
13 
14 #include <stdlib.h>
15 #include <unistd.h>
16 
17 #include <net/ethernet.h>
18 #include <net/if.h>
19 #include <net/if_lagg.h>
20 #include <net/ieee8023ad_lacp.h>
21 #include <net/route.h>
22 
23 #include <ctype.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <err.h>
29 #include <errno.h>
30 
31 #include "ifconfig.h"
32 
33 char lacpbuf[120];	/* LACP peer '[(a,a,a),(p,p,p)]' */
34 
35 static void
36 setlaggport(const char *val, int d, int s, const struct afswtch *afp)
37 {
38 	struct lagg_reqport rp;
39 
40 	bzero(&rp, sizeof(rp));
41 	strlcpy(rp.rp_ifname, name, sizeof(rp.rp_ifname));
42 	strlcpy(rp.rp_portname, val, sizeof(rp.rp_portname));
43 
44 	/* Don't choke if the port is already in this lagg. */
45 	if (ioctl(s, SIOCSLAGGPORT, &rp) && errno != EEXIST)
46 		err(1, "SIOCSLAGGPORT");
47 }
48 
49 static void
50 unsetlaggport(const char *val, int d, int s, const struct afswtch *afp)
51 {
52 	struct lagg_reqport rp;
53 
54 	bzero(&rp, sizeof(rp));
55 	strlcpy(rp.rp_ifname, name, sizeof(rp.rp_ifname));
56 	strlcpy(rp.rp_portname, val, sizeof(rp.rp_portname));
57 
58 	if (ioctl(s, SIOCSLAGGDELPORT, &rp))
59 		err(1, "SIOCSLAGGDELPORT");
60 }
61 
62 static void
63 setlaggproto(const char *val, int d, int s, const struct afswtch *afp)
64 {
65 	struct lagg_protos lpr[] = LAGG_PROTOS;
66 	struct lagg_reqall ra;
67 	int i;
68 
69 	bzero(&ra, sizeof(ra));
70 	ra.ra_proto = LAGG_PROTO_MAX;
71 
72 	for (i = 0; i < nitems(lpr); i++) {
73 		if (strcmp(val, lpr[i].lpr_name) == 0) {
74 			ra.ra_proto = lpr[i].lpr_proto;
75 			break;
76 		}
77 	}
78 	if (ra.ra_proto == LAGG_PROTO_MAX)
79 		errx(1, "Invalid aggregation protocol: %s", val);
80 
81 	strlcpy(ra.ra_ifname, name, sizeof(ra.ra_ifname));
82 	if (ioctl(s, SIOCSLAGG, &ra) != 0)
83 		err(1, "SIOCSLAGG");
84 }
85 
86 static void
87 setlaggflowidshift(const char *val, int d, int s, const struct afswtch *afp)
88 {
89 	struct lagg_reqopts ro;
90 
91 	bzero(&ro, sizeof(ro));
92 	ro.ro_opts = LAGG_OPT_FLOWIDSHIFT;
93 	strlcpy(ro.ro_ifname, name, sizeof(ro.ro_ifname));
94 	ro.ro_flowid_shift = (int)strtol(val, NULL, 10);
95 	if (ro.ro_flowid_shift & ~LAGG_OPT_FLOWIDSHIFT_MASK)
96 		errx(1, "Invalid flowid_shift option: %s", val);
97 
98 	if (ioctl(s, SIOCSLAGGOPTS, &ro) != 0)
99 		err(1, "SIOCSLAGGOPTS");
100 }
101 
102 static void
103 setlaggsetopt(const char *val, int d, int s, const struct afswtch *afp)
104 {
105 	struct lagg_reqopts ro;
106 
107 	bzero(&ro, sizeof(ro));
108 	ro.ro_opts = d;
109 	switch (ro.ro_opts) {
110 	case LAGG_OPT_USE_FLOWID:
111 	case -LAGG_OPT_USE_FLOWID:
112 	case LAGG_OPT_LACP_STRICT:
113 	case -LAGG_OPT_LACP_STRICT:
114 	case LAGG_OPT_LACP_TXTEST:
115 	case -LAGG_OPT_LACP_TXTEST:
116 	case LAGG_OPT_LACP_RXTEST:
117 	case -LAGG_OPT_LACP_RXTEST:
118 		break;
119 	default:
120 		err(1, "Invalid lagg option");
121 	}
122 	strlcpy(ro.ro_ifname, name, sizeof(ro.ro_ifname));
123 
124 	if (ioctl(s, SIOCSLAGGOPTS, &ro) != 0)
125 		err(1, "SIOCSLAGGOPTS");
126 }
127 
128 static void
129 setlagghash(const char *val, int d, int s, const struct afswtch *afp)
130 {
131 	struct lagg_reqflags rf;
132 	char *str, *tmp, *tok;
133 
134 
135 	rf.rf_flags = 0;
136 	str = tmp = strdup(val);
137 	while ((tok = strsep(&tmp, ",")) != NULL) {
138 		if (strcmp(tok, "l2") == 0)
139 			rf.rf_flags |= LAGG_F_HASHL2;
140 		else if (strcmp(tok, "l3") == 0)
141 			rf.rf_flags |= LAGG_F_HASHL3;
142 		else if (strcmp(tok, "l4") == 0)
143 			rf.rf_flags |= LAGG_F_HASHL4;
144 		else
145 			errx(1, "Invalid lagghash option: %s", tok);
146 	}
147 	free(str);
148 	if (rf.rf_flags == 0)
149 		errx(1, "No lagghash options supplied");
150 
151 	strlcpy(rf.rf_ifname, name, sizeof(rf.rf_ifname));
152 	if (ioctl(s, SIOCSLAGGHASH, &rf))
153 		err(1, "SIOCSLAGGHASH");
154 }
155 
156 static char *
157 lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen)
158 {
159 	snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X",
160 	    (int)mac[0], (int)mac[1], (int)mac[2], (int)mac[3],
161 	    (int)mac[4], (int)mac[5]);
162 
163 	return (buf);
164 }
165 
166 static char *
167 lacp_format_peer(struct lacp_opreq *req, const char *sep)
168 {
169 	char macbuf1[20];
170 	char macbuf2[20];
171 
172 	snprintf(lacpbuf, sizeof(lacpbuf),
173 	    "[(%04X,%s,%04X,%04X,%04X),%s(%04X,%s,%04X,%04X,%04X)]",
174 	    req->actor_prio,
175 	    lacp_format_mac(req->actor_mac, macbuf1, sizeof(macbuf1)),
176 	    req->actor_key, req->actor_portprio, req->actor_portno, sep,
177 	    req->partner_prio,
178 	    lacp_format_mac(req->partner_mac, macbuf2, sizeof(macbuf2)),
179 	    req->partner_key, req->partner_portprio, req->partner_portno);
180 
181 	return(lacpbuf);
182 }
183 
184 static void
185 lagg_status(int s)
186 {
187 	struct lagg_protos lpr[] = LAGG_PROTOS;
188 	struct lagg_reqport rp, rpbuf[LAGG_MAX_PORTS];
189 	struct lagg_reqall ra;
190 	struct lagg_reqopts ro;
191 	struct lagg_reqflags rf;
192 	struct lacp_opreq *lp;
193 	const char *proto = "<unknown>";
194 	int i, isport = 0;
195 
196 	bzero(&rp, sizeof(rp));
197 	bzero(&ra, sizeof(ra));
198 	bzero(&ro, sizeof(ro));
199 
200 	strlcpy(rp.rp_ifname, name, sizeof(rp.rp_ifname));
201 	strlcpy(rp.rp_portname, name, sizeof(rp.rp_portname));
202 
203 	if (ioctl(s, SIOCGLAGGPORT, &rp) == 0)
204 		isport = 1;
205 
206 	strlcpy(ra.ra_ifname, name, sizeof(ra.ra_ifname));
207 	ra.ra_size = sizeof(rpbuf);
208 	ra.ra_port = rpbuf;
209 
210 	strlcpy(ro.ro_ifname, name, sizeof(ro.ro_ifname));
211 	ioctl(s, SIOCGLAGGOPTS, &ro);
212 
213 	strlcpy(rf.rf_ifname, name, sizeof(rf.rf_ifname));
214 	if (ioctl(s, SIOCGLAGGFLAGS, &rf) != 0)
215 		rf.rf_flags = 0;
216 
217 	if (ioctl(s, SIOCGLAGG, &ra) == 0) {
218 		lp = (struct lacp_opreq *)&ra.ra_lacpreq;
219 
220 		for (i = 0; i < nitems(lpr); i++) {
221 			if (ra.ra_proto == lpr[i].lpr_proto) {
222 				proto = lpr[i].lpr_name;
223 				break;
224 			}
225 		}
226 
227 		printf("\tlaggproto %s", proto);
228 		if (rf.rf_flags & LAGG_F_HASHMASK) {
229 			const char *sep = "";
230 
231 			printf(" lagghash ");
232 			if (rf.rf_flags & LAGG_F_HASHL2) {
233 				printf("%sl2", sep);
234 				sep = ",";
235 			}
236 			if (rf.rf_flags & LAGG_F_HASHL3) {
237 				printf("%sl3", sep);
238 				sep = ",";
239 			}
240 			if (rf.rf_flags & LAGG_F_HASHL4) {
241 				printf("%sl4", sep);
242 				sep = ",";
243 			}
244 		}
245 		if (isport)
246 			printf(" laggdev %s", rp.rp_ifname);
247 		putchar('\n');
248 		if (verbose) {
249 			printf("\tlagg options:\n");
250 			printb("\t\tflags", ro.ro_opts, LAGG_OPT_BITS);
251 			putchar('\n');
252 			printf("\t\tflowid_shift: %d\n", ro.ro_flowid_shift);
253 			printf("\tlagg statistics:\n");
254 			printf("\t\tactive ports: %d\n", ro.ro_active);
255 			printf("\t\tflapping: %u\n", ro.ro_flapping);
256 			if (ra.ra_proto == LAGG_PROTO_LACP) {
257 				printf("\tlag id: %s\n",
258 				    lacp_format_peer(lp, "\n\t\t "));
259 			}
260 		}
261 
262 		for (i = 0; i < ra.ra_ports; i++) {
263 			lp = (struct lacp_opreq *)&rpbuf[i].rp_lacpreq;
264 			printf("\tlaggport: %s ", rpbuf[i].rp_portname);
265 			printb("flags", rpbuf[i].rp_flags, LAGG_PORT_BITS);
266 			if (verbose && ra.ra_proto == LAGG_PROTO_LACP)
267 				printb(" state", lp->actor_state,
268 				    LACP_STATE_BITS);
269 			putchar('\n');
270 			if (verbose && ra.ra_proto == LAGG_PROTO_LACP)
271 				printf("\t\t%s\n",
272 				    lacp_format_peer(lp, "\n\t\t "));
273 		}
274 
275 		if (0 /* XXX */) {
276 			printf("\tsupported aggregation protocols:\n");
277 			for (i = 0; i < (sizeof(lpr) / sizeof(lpr[0])); i++)
278 				printf("\t\tlaggproto %s\n", lpr[i].lpr_name);
279 		}
280 	}
281 }
282 
283 static struct cmd lagg_cmds[] = {
284 	DEF_CMD_ARG("laggport",		setlaggport),
285 	DEF_CMD_ARG("-laggport",	unsetlaggport),
286 	DEF_CMD_ARG("laggproto",	setlaggproto),
287 	DEF_CMD_ARG("lagghash",		setlagghash),
288 	DEF_CMD("use_flowid",	LAGG_OPT_USE_FLOWID,	setlaggsetopt),
289 	DEF_CMD("-use_flowid",	-LAGG_OPT_USE_FLOWID,	setlaggsetopt),
290 	DEF_CMD("lacp_strict",	LAGG_OPT_LACP_STRICT,	setlaggsetopt),
291 	DEF_CMD("-lacp_strict",	-LAGG_OPT_LACP_STRICT,	setlaggsetopt),
292 	DEF_CMD("lacp_txtest",	LAGG_OPT_LACP_TXTEST,	setlaggsetopt),
293 	DEF_CMD("-lacp_txtest",	-LAGG_OPT_LACP_TXTEST,	setlaggsetopt),
294 	DEF_CMD("lacp_rxtest",	LAGG_OPT_LACP_RXTEST,	setlaggsetopt),
295 	DEF_CMD("-lacp_rxtest",	-LAGG_OPT_LACP_RXTEST,	setlaggsetopt),
296 	DEF_CMD_ARG("flowid_shift",	setlaggflowidshift),
297 };
298 static struct afswtch af_lagg = {
299 	.af_name	= "af_lagg",
300 	.af_af		= AF_UNSPEC,
301 	.af_other_status = lagg_status,
302 };
303 
304 static __constructor void
305 lagg_ctor(void)
306 {
307 #define	N(a)	(sizeof(a) / sizeof(a[0]))
308 	int i;
309 
310 	for (i = 0; i < N(lagg_cmds);  i++)
311 		cmd_register(&lagg_cmds[i]);
312 	af_register(&af_lagg);
313 #undef N
314 }
315