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