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