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