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