1/*- 2 */ 3 4#ifndef lint 5static 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 33static struct iflaggparam params = { 34 .lagg_type = LAGG_TYPE_DEFAULT, 35}; 36 37static char lacpbuf[120]; /* LACP peer '[(a,a,a),(p,p,p)]' */ 38 39static void 40setlaggport(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 61static void 62unsetlaggport(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 74static void 75setlaggproto(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 98static void 99setlaggflowidshift(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 114static void 115setlaggrr_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 130static void 131setlaggsetopt(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_LACP_STRICT: 141 case -LAGG_OPT_LACP_STRICT: 142 case LAGG_OPT_LACP_TXTEST: 143 case -LAGG_OPT_LACP_TXTEST: 144 case LAGG_OPT_LACP_RXTEST: 145 case -LAGG_OPT_LACP_RXTEST: 146 case LAGG_OPT_LACP_FAST_TIMO: 147 case -LAGG_OPT_LACP_FAST_TIMO: 148 break; 149 default: 150 err(1, "Invalid lagg option"); 151 } 152 strlcpy(ro.ro_ifname, name, sizeof(ro.ro_ifname)); 153 154 if (ioctl(s, SIOCSLAGGOPTS, &ro) != 0) 155 err(1, "SIOCSLAGGOPTS"); 156} 157 158static void 159setlagghash(const char *val, int d, int s, const struct afswtch *afp) 160{ 161 struct lagg_reqflags rf; 162 char *str, *tmp, *tok; 163 164 165 rf.rf_flags = 0; 166 str = tmp = strdup(val); 167 while ((tok = strsep(&tmp, ",")) != NULL) { 168 if (strcmp(tok, "l2") == 0) 169 rf.rf_flags |= LAGG_F_HASHL2; 170 else if (strcmp(tok, "l3") == 0) 171 rf.rf_flags |= LAGG_F_HASHL3; 172 else if (strcmp(tok, "l4") == 0) 173 rf.rf_flags |= LAGG_F_HASHL4; 174 else 175 errx(1, "Invalid lagghash option: %s", tok); 176 } 177 free(str); 178 if (rf.rf_flags == 0) 179 errx(1, "No lagghash options supplied"); 180 181 strlcpy(rf.rf_ifname, name, sizeof(rf.rf_ifname)); 182 if (ioctl(s, SIOCSLAGGHASH, &rf)) 183 err(1, "SIOCSLAGGHASH"); 184} 185 186static char * 187lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen) 188{ 189 snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X", 190 (int)mac[0], (int)mac[1], (int)mac[2], (int)mac[3], 191 (int)mac[4], (int)mac[5]); 192 193 return (buf); 194} 195 196static char * 197lacp_format_peer(struct lacp_opreq *req, const char *sep) 198{ 199 char macbuf1[20]; 200 char macbuf2[20]; 201 202 snprintf(lacpbuf, sizeof(lacpbuf), 203 "[(%04X,%s,%04X,%04X,%04X),%s(%04X,%s,%04X,%04X,%04X)]", 204 req->actor_prio, 205 lacp_format_mac(req->actor_mac, macbuf1, sizeof(macbuf1)), 206 req->actor_key, req->actor_portprio, req->actor_portno, sep, 207 req->partner_prio, 208 lacp_format_mac(req->partner_mac, macbuf2, sizeof(macbuf2)), 209 req->partner_key, req->partner_portprio, req->partner_portno); 210 211 return(lacpbuf); 212} 213 214static void 215lagg_status(int s) 216{ 217 struct lagg_protos lpr[] = LAGG_PROTOS; 218 struct lagg_reqport rpbuf[LAGG_MAX_PORTS]; 219 struct lagg_reqall ra; 220 struct lagg_reqopts ro; 221 struct lagg_reqflags rf; 222 struct lacp_opreq *lp; 223 const char *proto = "<unknown>"; 224 int i; 225 226 bzero(&ra, sizeof(ra)); 227 bzero(&ro, sizeof(ro)); 228 229 strlcpy(ra.ra_ifname, name, sizeof(ra.ra_ifname)); 230 ra.ra_size = sizeof(rpbuf); 231 ra.ra_port = rpbuf; 232 233 strlcpy(ro.ro_ifname, name, sizeof(ro.ro_ifname)); 234 ioctl(s, SIOCGLAGGOPTS, &ro); 235 236 strlcpy(rf.rf_ifname, name, sizeof(rf.rf_ifname)); 237 if (ioctl(s, SIOCGLAGGFLAGS, &rf) != 0) 238 rf.rf_flags = 0; 239 240 if (ioctl(s, SIOCGLAGG, &ra) == 0) { 241 lp = (struct lacp_opreq *)&ra.ra_lacpreq; 242 243 for (i = 0; i < nitems(lpr); i++) { 244 if (ra.ra_proto == lpr[i].lpr_proto) { 245 proto = lpr[i].lpr_name; 246 break; 247 } 248 } 249 250 printf("\tlaggproto %s", proto); 251 if (rf.rf_flags & LAGG_F_HASHMASK) { 252 const char *sep = ""; 253 254 printf(" lagghash "); 255 if (rf.rf_flags & LAGG_F_HASHL2) { 256 printf("%sl2", sep); 257 sep = ","; 258 } 259 if (rf.rf_flags & LAGG_F_HASHL3) { 260 printf("%sl3", sep); 261 sep = ","; 262 } 263 if (rf.rf_flags & LAGG_F_HASHL4) { 264 printf("%sl4", sep); 265 sep = ","; 266 } 267 } 268 putchar('\n'); 269 if (verbose) { 270 printf("\tlagg options:\n"); 271 printb("\t\tflags", ro.ro_opts, LAGG_OPT_BITS); 272 putchar('\n'); 273 printf("\t\tflowid_shift: %d\n", ro.ro_flowid_shift); 274 if (ra.ra_proto == LAGG_PROTO_ROUNDROBIN) 275 printf("\t\trr_limit: %d\n", ro.ro_bkt); 276 printf("\tlagg statistics:\n"); 277 printf("\t\tactive ports: %d\n", ro.ro_active); 278 printf("\t\tflapping: %u\n", ro.ro_flapping); 279 if (ra.ra_proto == LAGG_PROTO_LACP) { 280 printf("\tlag id: %s\n", 281 lacp_format_peer(lp, "\n\t\t ")); 282 } 283 } 284 285 for (i = 0; i < ra.ra_ports; i++) { 286 lp = (struct lacp_opreq *)&rpbuf[i].rp_lacpreq; 287 printf("\tlaggport: %s ", rpbuf[i].rp_portname); 288 printb("flags", rpbuf[i].rp_flags, LAGG_PORT_BITS); 289 if (verbose && ra.ra_proto == LAGG_PROTO_LACP) 290 printb(" state", lp->actor_state, 291 LACP_STATE_BITS); 292 putchar('\n'); 293 if (verbose && ra.ra_proto == LAGG_PROTO_LACP) 294 printf("\t\t%s\n", 295 lacp_format_peer(lp, "\n\t\t ")); 296 } 297 298 if (0 /* XXX */) { 299 printf("\tsupported aggregation protocols:\n"); 300 for (i = 0; i < nitems(lpr); i++) 301 printf("\t\tlaggproto %s\n", lpr[i].lpr_name); 302 } 303 } 304} 305 306static 307DECL_CMD_FUNC(setlaggtype, arg, d) 308{ 309 static const struct lagg_types lt[] = LAGG_TYPES; 310 int i; 311 312 for (i = 0; i < nitems(lt); i++) { 313 if (strcmp(arg, lt[i].lt_name) == 0) { 314 params.lagg_type = lt[i].lt_value; 315 return; 316 } 317 } 318 errx(1, "invalid lagg type: %s", arg); 319} 320 321static void 322lagg_create(int s, struct ifreq *ifr) 323{ 324 ifr->ifr_data = (caddr_t) ¶ms; 325 ioctl_ifcreate(s, ifr); 326} 327 328static struct cmd lagg_cmds[] = { 329 DEF_CLONE_CMD_ARG("laggtype", setlaggtype), 330 DEF_CMD_ARG("laggport", setlaggport), 331 DEF_CMD_ARG("-laggport", unsetlaggport), 332 DEF_CMD_ARG("laggproto", setlaggproto), 333 DEF_CMD_ARG("lagghash", setlagghash), 334 DEF_CMD("use_flowid", LAGG_OPT_USE_FLOWID, setlaggsetopt), 335 DEF_CMD("-use_flowid", -LAGG_OPT_USE_FLOWID, setlaggsetopt), 336 DEF_CMD("lacp_strict", LAGG_OPT_LACP_STRICT, setlaggsetopt), 337 DEF_CMD("-lacp_strict", -LAGG_OPT_LACP_STRICT, setlaggsetopt), 338 DEF_CMD("lacp_txtest", LAGG_OPT_LACP_TXTEST, setlaggsetopt), 339 DEF_CMD("-lacp_txtest", -LAGG_OPT_LACP_TXTEST, setlaggsetopt), 340 DEF_CMD("lacp_rxtest", LAGG_OPT_LACP_RXTEST, setlaggsetopt), 341 DEF_CMD("-lacp_rxtest", -LAGG_OPT_LACP_RXTEST, setlaggsetopt), 342 DEF_CMD("lacp_fast_timeout", LAGG_OPT_LACP_FAST_TIMO, setlaggsetopt), 343 DEF_CMD("-lacp_fast_timeout", -LAGG_OPT_LACP_FAST_TIMO, setlaggsetopt), 344 DEF_CMD_ARG("flowid_shift", setlaggflowidshift), 345 DEF_CMD_ARG("rr_limit", setlaggrr_limit), 346}; 347static struct afswtch af_lagg = { 348 .af_name = "af_lagg", 349 .af_af = AF_UNSPEC, 350 .af_other_status = lagg_status, 351}; 352 353static __constructor void 354lagg_ctor(void) 355{ 356 int i; 357 358 for (i = 0; i < nitems(lagg_cmds); i++) 359 cmd_register(&lagg_cmds[i]); 360 af_register(&af_lagg); 361 clone_setdefcallback("lagg", lagg_create); 362} 363