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_bkt = (int)strtol(val, NULL, 10); 118 119 if (ioctl(s, SIOCSLAGGOPTS, &ro) != 0) 120 err(1, "SIOCSLAGG"); 121 } 122 123 static void 124 setlaggsetopt(const char *val, int d, int s, const struct afswtch *afp) 125 { 126 struct lagg_reqopts ro; 127 128 bzero(&ro, sizeof(ro)); 129 ro.ro_opts = d; 130 switch (ro.ro_opts) { 131 case LAGG_OPT_USE_FLOWID: 132 case -LAGG_OPT_USE_FLOWID: 133 case LAGG_OPT_USE_NUMA: 134 case -LAGG_OPT_USE_NUMA: 135 case LAGG_OPT_LACP_STRICT: 136 case -LAGG_OPT_LACP_STRICT: 137 case LAGG_OPT_LACP_TXTEST: 138 case -LAGG_OPT_LACP_TXTEST: 139 case LAGG_OPT_LACP_RXTEST: 140 case -LAGG_OPT_LACP_RXTEST: 141 case LAGG_OPT_LACP_TIMEOUT: 142 case -LAGG_OPT_LACP_TIMEOUT: 143 break; 144 default: 145 err(1, "Invalid lagg option"); 146 } 147 strlcpy(ro.ro_ifname, name, sizeof(ro.ro_ifname)); 148 149 if (ioctl(s, SIOCSLAGGOPTS, &ro) != 0) 150 err(1, "SIOCSLAGGOPTS"); 151 } 152 153 static void 154 setlagghash(const char *val, int d, int s, const struct afswtch *afp) 155 { 156 struct lagg_reqflags rf; 157 char *str, *tmp, *tok; 158 159 160 rf.rf_flags = 0; 161 str = tmp = strdup(val); 162 while ((tok = strsep(&tmp, ",")) != NULL) { 163 if (strcmp(tok, "l2") == 0) 164 rf.rf_flags |= LAGG_F_HASHL2; 165 else if (strcmp(tok, "l3") == 0) 166 rf.rf_flags |= LAGG_F_HASHL3; 167 else if (strcmp(tok, "l4") == 0) 168 rf.rf_flags |= LAGG_F_HASHL4; 169 else 170 errx(1, "Invalid lagghash option: %s", tok); 171 } 172 free(str); 173 if (rf.rf_flags == 0) 174 errx(1, "No lagghash options supplied"); 175 176 strlcpy(rf.rf_ifname, name, sizeof(rf.rf_ifname)); 177 if (ioctl(s, SIOCSLAGGHASH, &rf)) 178 err(1, "SIOCSLAGGHASH"); 179 } 180 181 static char * 182 lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen) 183 { 184 snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X", 185 (int)mac[0], (int)mac[1], (int)mac[2], (int)mac[3], 186 (int)mac[4], (int)mac[5]); 187 188 return (buf); 189 } 190 191 static char * 192 lacp_format_peer(struct lacp_opreq *req, const char *sep) 193 { 194 char macbuf1[20]; 195 char macbuf2[20]; 196 197 snprintf(lacpbuf, sizeof(lacpbuf), 198 "[(%04X,%s,%04X,%04X,%04X),%s(%04X,%s,%04X,%04X,%04X)]", 199 req->actor_prio, 200 lacp_format_mac(req->actor_mac, macbuf1, sizeof(macbuf1)), 201 req->actor_key, req->actor_portprio, req->actor_portno, sep, 202 req->partner_prio, 203 lacp_format_mac(req->partner_mac, macbuf2, sizeof(macbuf2)), 204 req->partner_key, req->partner_portprio, req->partner_portno); 205 206 return(lacpbuf); 207 } 208 209 static void 210 lagg_status(int s) 211 { 212 struct lagg_protos lpr[] = LAGG_PROTOS; 213 struct lagg_reqport rpbuf[LAGG_MAX_PORTS]; 214 struct lagg_reqall ra; 215 struct lagg_reqopts ro; 216 struct lagg_reqflags rf; 217 struct lacp_opreq *lp; 218 const char *proto = "<unknown>"; 219 int i; 220 221 bzero(&ra, sizeof(ra)); 222 bzero(&ro, sizeof(ro)); 223 224 strlcpy(ra.ra_ifname, name, sizeof(ra.ra_ifname)); 225 ra.ra_size = sizeof(rpbuf); 226 ra.ra_port = rpbuf; 227 228 strlcpy(ro.ro_ifname, name, sizeof(ro.ro_ifname)); 229 ioctl(s, SIOCGLAGGOPTS, &ro); 230 231 strlcpy(rf.rf_ifname, name, sizeof(rf.rf_ifname)); 232 if (ioctl(s, SIOCGLAGGFLAGS, &rf) != 0) 233 rf.rf_flags = 0; 234 235 if (ioctl(s, SIOCGLAGG, &ra) == 0) { 236 lp = (struct lacp_opreq *)&ra.ra_lacpreq; 237 238 for (i = 0; i < nitems(lpr); i++) { 239 if (ra.ra_proto == lpr[i].lpr_proto) { 240 proto = lpr[i].lpr_name; 241 break; 242 } 243 } 244 245 printf("\tlaggproto %s", proto); 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 printf("\tlag id: %s\n", 276 lacp_format_peer(lp, "\n\t\t ")); 277 } 278 } 279 280 for (i = 0; i < ra.ra_ports; i++) { 281 lp = (struct lacp_opreq *)&rpbuf[i].rp_lacpreq; 282 printf("\tlaggport: %s ", rpbuf[i].rp_portname); 283 printb("flags", rpbuf[i].rp_flags, LAGG_PORT_BITS); 284 if (verbose && ra.ra_proto == LAGG_PROTO_LACP) 285 printb(" state", lp->actor_state, 286 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 if (0 /* XXX */) { 294 printf("\tsupported aggregation protocols:\n"); 295 for (i = 0; i < nitems(lpr); i++) 296 printf("\t\tlaggproto %s\n", lpr[i].lpr_name); 297 } 298 } 299 } 300 301 static struct cmd lagg_cmds[] = { 302 DEF_CMD_ARG("laggport", setlaggport), 303 DEF_CMD_ARG("-laggport", unsetlaggport), 304 DEF_CMD_ARG("laggproto", setlaggproto), 305 DEF_CMD_ARG("lagghash", setlagghash), 306 DEF_CMD("use_flowid", LAGG_OPT_USE_FLOWID, setlaggsetopt), 307 DEF_CMD("-use_flowid", -LAGG_OPT_USE_FLOWID, setlaggsetopt), 308 DEF_CMD("use_numa", LAGG_OPT_USE_NUMA, setlaggsetopt), 309 DEF_CMD("-use_numa", -LAGG_OPT_USE_NUMA, setlaggsetopt), 310 DEF_CMD("lacp_strict", LAGG_OPT_LACP_STRICT, setlaggsetopt), 311 DEF_CMD("-lacp_strict", -LAGG_OPT_LACP_STRICT, setlaggsetopt), 312 DEF_CMD("lacp_txtest", LAGG_OPT_LACP_TXTEST, setlaggsetopt), 313 DEF_CMD("-lacp_txtest", -LAGG_OPT_LACP_TXTEST, setlaggsetopt), 314 DEF_CMD("lacp_rxtest", LAGG_OPT_LACP_RXTEST, setlaggsetopt), 315 DEF_CMD("-lacp_rxtest", -LAGG_OPT_LACP_RXTEST, setlaggsetopt), 316 DEF_CMD("lacp_fast_timeout", LAGG_OPT_LACP_TIMEOUT, setlaggsetopt), 317 DEF_CMD("-lacp_fast_timeout", -LAGG_OPT_LACP_TIMEOUT, setlaggsetopt), 318 DEF_CMD_ARG("flowid_shift", setlaggflowidshift), 319 DEF_CMD_ARG("rr_limit", setlaggrr_limit), 320 }; 321 static struct afswtch af_lagg = { 322 .af_name = "af_lagg", 323 .af_af = AF_UNSPEC, 324 .af_other_status = lagg_status, 325 }; 326 327 static __constructor void 328 lagg_ctor(void) 329 { 330 int i; 331 332 for (i = 0; i < nitems(lagg_cmds); i++) 333 cmd_register(&lagg_cmds[i]); 334 af_register(&af_lagg); 335 } 336