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 37 setlaggport(if_ctx *ctx, const char *val, int dummy __unused) 38 { 39 struct lagg_reqport rp = {}; 40 41 strlcpy(rp.rp_ifname, ctx->ifname, 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_ctx(ctx, SIOCSLAGGPORT, &rp) && errno != EEXIST) { 51 warnx("%s %s: SIOCSLAGGPORT: %s", 52 ctx->ifname, val, strerror(errno)); 53 exit_code = 1; 54 } 55 } 56 57 static void 58 unsetlaggport(if_ctx *ctx, const char *val, int dummy __unused) 59 { 60 struct lagg_reqport rp = {}; 61 62 strlcpy(rp.rp_ifname, ctx->ifname, sizeof(rp.rp_ifname)); 63 strlcpy(rp.rp_portname, val, sizeof(rp.rp_portname)); 64 65 if (ioctl_ctx(ctx, SIOCSLAGGDELPORT, &rp)) 66 err(1, "SIOCSLAGGDELPORT"); 67 } 68 69 static void 70 setlaggproto(if_ctx *ctx, const char *val, int dummy __unused) 71 { 72 struct lagg_protos lpr[] = LAGG_PROTOS; 73 struct lagg_reqall ra; 74 75 bzero(&ra, sizeof(ra)); 76 ra.ra_proto = LAGG_PROTO_MAX; 77 78 for (size_t i = 0; i < nitems(lpr); i++) { 79 if (strcmp(val, lpr[i].lpr_name) == 0) { 80 ra.ra_proto = lpr[i].lpr_proto; 81 break; 82 } 83 } 84 if (ra.ra_proto == LAGG_PROTO_MAX) 85 errx(1, "Invalid aggregation protocol: %s", val); 86 87 strlcpy(ra.ra_ifname, ctx->ifname, sizeof(ra.ra_ifname)); 88 if (ioctl_ctx(ctx, SIOCSLAGG, &ra) != 0) 89 err(1, "SIOCSLAGG"); 90 } 91 92 static void 93 setlaggflowidshift(if_ctx *ctx, const char *val, int dummy __unused) 94 { 95 struct lagg_reqopts ro = {}; 96 97 ro.ro_opts = LAGG_OPT_FLOWIDSHIFT; 98 strlcpy(ro.ro_ifname, ctx->ifname, sizeof(ro.ro_ifname)); 99 ro.ro_flowid_shift = (int)strtol(val, NULL, 10); 100 if (ro.ro_flowid_shift & ~LAGG_OPT_FLOWIDSHIFT_MASK) 101 errx(1, "Invalid flowid_shift option: %s", val); 102 103 if (ioctl_ctx(ctx, SIOCSLAGGOPTS, &ro) != 0) 104 err(1, "SIOCSLAGGOPTS"); 105 } 106 107 static void 108 setlaggrr_limit(if_ctx *ctx, const char *val, int dummy __unused) 109 { 110 struct lagg_reqopts ro = {}; 111 112 strlcpy(ro.ro_ifname, ctx->ifname, sizeof(ro.ro_ifname)); 113 ro.ro_opts = LAGG_OPT_RR_LIMIT; 114 ro.ro_bkt = (uint32_t)strtoul(val, NULL, 10); 115 if (ro.ro_bkt == 0) 116 errx(1, "Invalid round-robin stride: %s", val); 117 118 if (ioctl_ctx(ctx, SIOCSLAGGOPTS, &ro) != 0) 119 err(1, "SIOCSLAGGOPTS"); 120 } 121 122 static void 123 setlaggsetopt(if_ctx *ctx, const char *val __unused, int d) 124 { 125 struct lagg_reqopts ro = {}; 126 127 ro.ro_opts = d; 128 switch (ro.ro_opts) { 129 case LAGG_OPT_USE_FLOWID: 130 case -LAGG_OPT_USE_FLOWID: 131 case LAGG_OPT_USE_NUMA: 132 case -LAGG_OPT_USE_NUMA: 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_FAST_TIMO: 140 case -LAGG_OPT_LACP_FAST_TIMO: 141 break; 142 default: 143 err(1, "Invalid lagg option"); 144 } 145 strlcpy(ro.ro_ifname, ctx->ifname, sizeof(ro.ro_ifname)); 146 147 if (ioctl_ctx(ctx, SIOCSLAGGOPTS, &ro) != 0) 148 err(1, "SIOCSLAGGOPTS"); 149 } 150 151 static void 152 setlagghash(if_ctx *ctx, const char *val, int dummy __unused) 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, ctx->ifname, sizeof(rf.rf_ifname)); 175 if (ioctl_ctx(ctx, 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(if_ctx *ctx) 209 { 210 struct lagg_protos protos[] = LAGG_PROTOS; 211 struct ifconfig_lagg_status *lagg; 212 struct lagg_reqall *ra; 213 struct lagg_reqflags *rf; 214 struct lagg_reqopts *ro; 215 struct lagg_reqport *ports; 216 struct lacp_opreq *lp; 217 const char *proto; 218 const int verbose = ctx->args->verbose; 219 220 if (ifconfig_lagg_get_lagg_status(lifh, ctx->ifname, &lagg) == -1) 221 return; 222 223 ra = lagg->ra; 224 rf = lagg->rf; 225 ro = lagg->ro; 226 ports = ra->ra_port; 227 228 proto = "<unknown>"; 229 for (size_t i = 0; i < nitems(protos); ++i) { 230 if (ra->ra_proto == protos[i].lpr_proto) { 231 proto = protos[i].lpr_name; 232 break; 233 } 234 } 235 printf("\tlaggproto %s", proto); 236 237 if (rf->rf_flags & LAGG_F_HASHMASK) { 238 const char *sep = ""; 239 240 printf(" lagghash "); 241 if (rf->rf_flags & LAGG_F_HASHL2) { 242 printf("%sl2", sep); 243 sep = ","; 244 } 245 if (rf->rf_flags & LAGG_F_HASHL3) { 246 printf("%sl3", sep); 247 sep = ","; 248 } 249 if (rf->rf_flags & LAGG_F_HASHL4) { 250 printf("%sl4", sep); 251 sep = ","; 252 } 253 } 254 putchar('\n'); 255 if (verbose) { 256 printf("\tlagg options:\n"); 257 printb("\t\tflags", ro->ro_opts, LAGG_OPT_BITS); 258 putchar('\n'); 259 printf("\t\tflowid_shift: %d\n", ro->ro_flowid_shift); 260 if (ra->ra_proto == LAGG_PROTO_ROUNDROBIN) 261 printf("\t\trr_limit: %d\n", ro->ro_bkt); 262 printf("\tlagg statistics:\n"); 263 printf("\t\tactive ports: %d\n", ro->ro_active); 264 printf("\t\tflapping: %u\n", ro->ro_flapping); 265 if (ra->ra_proto == LAGG_PROTO_LACP) { 266 lp = &ra->ra_lacpreq; 267 printf("\tlag id: %s\n", 268 lacp_format_peer(lp, "\n\t\t ")); 269 } 270 } 271 272 for (size_t i = 0; i < (size_t)ra->ra_ports; ++i) { 273 lp = &ports[i].rp_lacpreq; 274 printf("\tlaggport: %s ", ports[i].rp_portname); 275 printb("flags", ports[i].rp_flags, LAGG_PORT_BITS); 276 if (verbose && ra->ra_proto == LAGG_PROTO_LACP) 277 printb(" state", lp->actor_state, LACP_STATE_BITS); 278 putchar('\n'); 279 if (verbose && ra->ra_proto == LAGG_PROTO_LACP) 280 printf("\t\t%s\n", 281 lacp_format_peer(lp, "\n\t\t ")); 282 } 283 284 ifconfig_lagg_free_lagg_status(lagg); 285 } 286 287 static void 288 setlaggtype(if_ctx *ctx __unused, const char *arg, int dummy __unused) 289 { 290 static const struct lagg_types lt[] = LAGG_TYPES; 291 292 for (size_t i = 0; i < nitems(lt); i++) { 293 if (strcmp(arg, lt[i].lt_name) == 0) { 294 params.lagg_type = lt[i].lt_value; 295 return; 296 } 297 } 298 errx(1, "invalid lagg type: %s", arg); 299 } 300 301 static void 302 lagg_create(if_ctx *ctx, struct ifreq *ifr) 303 { 304 ifr->ifr_data = (caddr_t) ¶ms; 305 ifcreate_ioctl(ctx, ifr); 306 } 307 308 static struct cmd lagg_cmds[] = { 309 DEF_CLONE_CMD_ARG("laggtype", setlaggtype), 310 DEF_CMD_ARG("laggport", setlaggport), 311 DEF_CMD_ARG("-laggport", unsetlaggport), 312 DEF_CMD_ARG("laggproto", setlaggproto), 313 DEF_CMD_ARG("lagghash", setlagghash), 314 DEF_CMD("use_flowid", LAGG_OPT_USE_FLOWID, setlaggsetopt), 315 DEF_CMD("-use_flowid", -LAGG_OPT_USE_FLOWID, setlaggsetopt), 316 DEF_CMD("use_numa", LAGG_OPT_USE_NUMA, setlaggsetopt), 317 DEF_CMD("-use_numa", -LAGG_OPT_USE_NUMA, setlaggsetopt), 318 DEF_CMD("lacp_strict", LAGG_OPT_LACP_STRICT, setlaggsetopt), 319 DEF_CMD("-lacp_strict", -LAGG_OPT_LACP_STRICT, setlaggsetopt), 320 DEF_CMD("lacp_txtest", LAGG_OPT_LACP_TXTEST, setlaggsetopt), 321 DEF_CMD("-lacp_txtest", -LAGG_OPT_LACP_TXTEST, setlaggsetopt), 322 DEF_CMD("lacp_rxtest", LAGG_OPT_LACP_RXTEST, setlaggsetopt), 323 DEF_CMD("-lacp_rxtest", -LAGG_OPT_LACP_RXTEST, setlaggsetopt), 324 DEF_CMD("lacp_fast_timeout", LAGG_OPT_LACP_FAST_TIMO, setlaggsetopt), 325 DEF_CMD("-lacp_fast_timeout", -LAGG_OPT_LACP_FAST_TIMO, setlaggsetopt), 326 DEF_CMD_ARG("flowid_shift", setlaggflowidshift), 327 DEF_CMD_ARG("rr_limit", setlaggrr_limit), 328 }; 329 static struct afswtch af_lagg = { 330 .af_name = "af_lagg", 331 .af_af = AF_UNSPEC, 332 .af_other_status = lagg_status, 333 }; 334 335 static __constructor void 336 lagg_ctor(void) 337 { 338 for (size_t i = 0; i < nitems(lagg_cmds); i++) 339 cmd_register(&lagg_cmds[i]); 340 af_register(&af_lagg); 341 clone_setdefcallback_prefix("lagg", lagg_create); 342 } 343