1 /*-
2 */
3
4 #include <sys/param.h>
5 #include <sys/ioctl.h>
6 #include <sys/socket.h>
7 #include <sys/sockio.h>
8
9 #include <stdlib.h>
10 #include <unistd.h>
11
12 #include <net/ethernet.h>
13 #include <net/if.h>
14 #include <net/if_lagg.h>
15 #include <net/ieee8023ad_lacp.h>
16 #include <net/route.h>
17
18 #include <ctype.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <err.h>
24 #include <errno.h>
25
26 #include <libifconfig.h>
27
28 #include "ifconfig.h"
29
30 static struct iflaggparam params = {
31 .lagg_type = LAGG_TYPE_DEFAULT,
32 };
33
34 static char lacpbuf[120]; /* LACP peer '[(a,a,a),(p,p,p)]' */
35
36 static void
setlaggport(const char * val,int d,int s,const struct afswtch * afp)37 setlaggport(const char *val, int d, int s, const struct afswtch *afp)
38 {
39 struct lagg_reqport rp;
40
41 bzero(&rp, sizeof(rp));
42 strlcpy(rp.rp_ifname, name, sizeof(rp.rp_ifname));
43 strlcpy(rp.rp_portname, val, sizeof(rp.rp_portname));
44
45 /*
46 * Do not exit with an error here. Doing so permits a
47 * failed NIC to take down an entire lagg.
48 *
49 * Don't error at all if the port is already in the lagg.
50 */
51 if (ioctl(s, SIOCSLAGGPORT, &rp) && errno != EEXIST) {
52 warnx("%s %s: SIOCSLAGGPORT: %s",
53 name, val, strerror(errno));
54 exit_code = 1;
55 }
56 }
57
58 static void
unsetlaggport(const char * val,int d,int s,const struct afswtch * afp)59 unsetlaggport(const char *val, int d, int s, const struct afswtch *afp)
60 {
61 struct lagg_reqport rp;
62
63 bzero(&rp, sizeof(rp));
64 strlcpy(rp.rp_ifname, name, sizeof(rp.rp_ifname));
65 strlcpy(rp.rp_portname, val, sizeof(rp.rp_portname));
66
67 if (ioctl(s, SIOCSLAGGDELPORT, &rp))
68 err(1, "SIOCSLAGGDELPORT");
69 }
70
71 static void
setlaggproto(const char * val,int d,int s,const struct afswtch * afp)72 setlaggproto(const char *val, int d, int s, const struct afswtch *afp)
73 {
74 struct lagg_protos lpr[] = LAGG_PROTOS;
75 struct lagg_reqall ra;
76 int i;
77
78 bzero(&ra, sizeof(ra));
79 ra.ra_proto = LAGG_PROTO_MAX;
80
81 for (i = 0; i < nitems(lpr); i++) {
82 if (strcmp(val, lpr[i].lpr_name) == 0) {
83 ra.ra_proto = lpr[i].lpr_proto;
84 break;
85 }
86 }
87 if (ra.ra_proto == LAGG_PROTO_MAX)
88 errx(1, "Invalid aggregation protocol: %s", val);
89
90 strlcpy(ra.ra_ifname, name, sizeof(ra.ra_ifname));
91 if (ioctl(s, SIOCSLAGG, &ra) != 0)
92 err(1, "SIOCSLAGG");
93 }
94
95 static void
setlaggflowidshift(const char * val,int d,int s,const struct afswtch * afp)96 setlaggflowidshift(const char *val, int d, int s, const struct afswtch *afp)
97 {
98 struct lagg_reqopts ro;
99
100 bzero(&ro, sizeof(ro));
101 ro.ro_opts = LAGG_OPT_FLOWIDSHIFT;
102 strlcpy(ro.ro_ifname, name, sizeof(ro.ro_ifname));
103 ro.ro_flowid_shift = (int)strtol(val, NULL, 10);
104 if (ro.ro_flowid_shift & ~LAGG_OPT_FLOWIDSHIFT_MASK)
105 errx(1, "Invalid flowid_shift option: %s", val);
106
107 if (ioctl(s, SIOCSLAGGOPTS, &ro) != 0)
108 err(1, "SIOCSLAGGOPTS");
109 }
110
111 static void
setlaggrr_limit(const char * val,int d,int s,const struct afswtch * afp)112 setlaggrr_limit(const char *val, int d, int s, const struct afswtch *afp)
113 {
114 struct lagg_reqopts ro;
115
116 bzero(&ro, sizeof(ro));
117 strlcpy(ro.ro_ifname, name, 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(s, SIOCSLAGGOPTS, &ro) != 0)
124 err(1, "SIOCSLAGGOPTS");
125 }
126
127 static void
setlaggsetopt(const char * val,int d,int s,const struct afswtch * afp)128 setlaggsetopt(const char *val, int d, int s, const struct afswtch *afp)
129 {
130 struct lagg_reqopts ro;
131
132 bzero(&ro, sizeof(ro));
133 ro.ro_opts = d;
134 switch (ro.ro_opts) {
135 case LAGG_OPT_USE_FLOWID:
136 case -LAGG_OPT_USE_FLOWID:
137 case LAGG_OPT_USE_NUMA:
138 case -LAGG_OPT_USE_NUMA:
139 case LAGG_OPT_LACP_STRICT:
140 case -LAGG_OPT_LACP_STRICT:
141 case LAGG_OPT_LACP_TXTEST:
142 case -LAGG_OPT_LACP_TXTEST:
143 case LAGG_OPT_LACP_RXTEST:
144 case -LAGG_OPT_LACP_RXTEST:
145 case LAGG_OPT_LACP_FAST_TIMO:
146 case -LAGG_OPT_LACP_FAST_TIMO:
147 break;
148 default:
149 err(1, "Invalid lagg option");
150 }
151 strlcpy(ro.ro_ifname, name, sizeof(ro.ro_ifname));
152
153 if (ioctl(s, SIOCSLAGGOPTS, &ro) != 0)
154 err(1, "SIOCSLAGGOPTS");
155 }
156
157 static void
setlagghash(const char * val,int d,int s,const struct afswtch * afp)158 setlagghash(const char *val, int d, int s, const struct afswtch *afp)
159 {
160 struct lagg_reqflags rf;
161 char *str, *tmp, *tok;
162
163
164 rf.rf_flags = 0;
165 str = tmp = strdup(val);
166 while ((tok = strsep(&tmp, ",")) != NULL) {
167 if (strcmp(tok, "l2") == 0)
168 rf.rf_flags |= LAGG_F_HASHL2;
169 else if (strcmp(tok, "l3") == 0)
170 rf.rf_flags |= LAGG_F_HASHL3;
171 else if (strcmp(tok, "l4") == 0)
172 rf.rf_flags |= LAGG_F_HASHL4;
173 else
174 errx(1, "Invalid lagghash option: %s", tok);
175 }
176 free(str);
177 if (rf.rf_flags == 0)
178 errx(1, "No lagghash options supplied");
179
180 strlcpy(rf.rf_ifname, name, sizeof(rf.rf_ifname));
181 if (ioctl(s, SIOCSLAGGHASH, &rf))
182 err(1, "SIOCSLAGGHASH");
183 }
184
185 static char *
lacp_format_mac(const uint8_t * mac,char * buf,size_t buflen)186 lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen)
187 {
188 snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X",
189 (int)mac[0], (int)mac[1], (int)mac[2], (int)mac[3],
190 (int)mac[4], (int)mac[5]);
191
192 return (buf);
193 }
194
195 static char *
lacp_format_peer(struct lacp_opreq * req,const char * sep)196 lacp_format_peer(struct lacp_opreq *req, const char *sep)
197 {
198 char macbuf1[20];
199 char macbuf2[20];
200
201 snprintf(lacpbuf, sizeof(lacpbuf),
202 "[(%04X,%s,%04X,%04X,%04X),%s(%04X,%s,%04X,%04X,%04X)]",
203 req->actor_prio,
204 lacp_format_mac(req->actor_mac, macbuf1, sizeof(macbuf1)),
205 req->actor_key, req->actor_portprio, req->actor_portno, sep,
206 req->partner_prio,
207 lacp_format_mac(req->partner_mac, macbuf2, sizeof(macbuf2)),
208 req->partner_key, req->partner_portprio, req->partner_portno);
209
210 return(lacpbuf);
211 }
212
213 static void
lagg_status(int s)214 lagg_status(int s)
215 {
216 struct lagg_protos protos[] = LAGG_PROTOS;
217 struct ifconfig_lagg_status *lagg;
218 struct lagg_reqall *ra;
219 struct lagg_reqflags *rf;
220 struct lagg_reqopts *ro;
221 struct lagg_reqport *ports;
222 struct lacp_opreq *lp;
223 const char *proto;
224
225 if (ifconfig_lagg_get_lagg_status(lifh, name, &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 < 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
DECL_CMD_FUNC(setlaggtype,arg,d)293 DECL_CMD_FUNC(setlaggtype, arg, d)
294 {
295 static const struct lagg_types lt[] = LAGG_TYPES;
296 int i;
297
298 for (i = 0; i < nitems(lt); i++) {
299 if (strcmp(arg, lt[i].lt_name) == 0) {
300 params.lagg_type = lt[i].lt_value;
301 return;
302 }
303 }
304 errx(1, "invalid lagg type: %s", arg);
305 }
306
307 static void
lagg_create(int s,struct ifreq * ifr)308 lagg_create(int s, struct ifreq *ifr)
309 {
310 ifr->ifr_data = (caddr_t) ¶ms;
311 ioctl_ifcreate(s, ifr);
312 }
313
314 static struct cmd lagg_cmds[] = {
315 DEF_CLONE_CMD_ARG("laggtype", setlaggtype),
316 DEF_CMD_ARG("laggport", setlaggport),
317 DEF_CMD_ARG("-laggport", unsetlaggport),
318 DEF_CMD_ARG("laggproto", setlaggproto),
319 DEF_CMD_ARG("lagghash", setlagghash),
320 DEF_CMD("use_flowid", LAGG_OPT_USE_FLOWID, setlaggsetopt),
321 DEF_CMD("-use_flowid", -LAGG_OPT_USE_FLOWID, setlaggsetopt),
322 DEF_CMD("use_numa", LAGG_OPT_USE_NUMA, setlaggsetopt),
323 DEF_CMD("-use_numa", -LAGG_OPT_USE_NUMA, setlaggsetopt),
324 DEF_CMD("lacp_strict", LAGG_OPT_LACP_STRICT, setlaggsetopt),
325 DEF_CMD("-lacp_strict", -LAGG_OPT_LACP_STRICT, setlaggsetopt),
326 DEF_CMD("lacp_txtest", LAGG_OPT_LACP_TXTEST, setlaggsetopt),
327 DEF_CMD("-lacp_txtest", -LAGG_OPT_LACP_TXTEST, setlaggsetopt),
328 DEF_CMD("lacp_rxtest", LAGG_OPT_LACP_RXTEST, setlaggsetopt),
329 DEF_CMD("-lacp_rxtest", -LAGG_OPT_LACP_RXTEST, setlaggsetopt),
330 DEF_CMD("lacp_fast_timeout", LAGG_OPT_LACP_FAST_TIMO, setlaggsetopt),
331 DEF_CMD("-lacp_fast_timeout", -LAGG_OPT_LACP_FAST_TIMO, setlaggsetopt),
332 DEF_CMD_ARG("flowid_shift", setlaggflowidshift),
333 DEF_CMD_ARG("rr_limit", setlaggrr_limit),
334 };
335 static struct afswtch af_lagg = {
336 .af_name = "af_lagg",
337 .af_af = AF_UNSPEC,
338 .af_other_status = lagg_status,
339 };
340
341 static __constructor void
lagg_ctor(void)342 lagg_ctor(void)
343 {
344 int i;
345
346 for (i = 0; i < nitems(lagg_cmds); i++)
347 cmd_register(&lagg_cmds[i]);
348 af_register(&af_lagg);
349 clone_setdefcallback_prefix("lagg", lagg_create);
350 }
351