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