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