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