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