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