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