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/route.h> 21 22 #include <ctype.h> 23 #include <stdio.h> 24 #include <string.h> 25 #include <stdlib.h> 26 #include <unistd.h> 27 #include <err.h> 28 #include <errno.h> 29 30 #include "ifconfig.h" 31 32 char lacpbuf[120]; /* LACP peer '[(a,a,a),(p,p,p)]' */ 33 34 static void 35 setlaggport(const char *val, int d, int s, const struct afswtch *afp) 36 { 37 struct lagg_reqport rp; 38 39 bzero(&rp, sizeof(rp)); 40 strlcpy(rp.rp_ifname, name, sizeof(rp.rp_ifname)); 41 strlcpy(rp.rp_portname, val, sizeof(rp.rp_portname)); 42 43 if (ioctl(s, SIOCSLAGGPORT, &rp)) 44 err(1, "SIOCSLAGGPORT"); 45 } 46 47 static void 48 unsetlaggport(const char *val, int d, int s, const struct afswtch *afp) 49 { 50 struct lagg_reqport rp; 51 52 bzero(&rp, sizeof(rp)); 53 strlcpy(rp.rp_ifname, name, sizeof(rp.rp_ifname)); 54 strlcpy(rp.rp_portname, val, sizeof(rp.rp_portname)); 55 56 if (ioctl(s, SIOCSLAGGDELPORT, &rp)) 57 err(1, "SIOCSLAGGDELPORT"); 58 } 59 60 static void 61 setlaggproto(const char *val, int d, int s, const struct afswtch *afp) 62 { 63 struct lagg_protos lpr[] = LAGG_PROTOS; 64 struct lagg_reqall ra; 65 int i; 66 67 bzero(&ra, sizeof(ra)); 68 ra.ra_proto = LAGG_PROTO_MAX; 69 70 for (i = 0; i < (sizeof(lpr) / sizeof(lpr[0])); i++) { 71 if (strcmp(val, lpr[i].lpr_name) == 0) { 72 ra.ra_proto = lpr[i].lpr_proto; 73 break; 74 } 75 } 76 if (ra.ra_proto == LAGG_PROTO_MAX) 77 errx(1, "Invalid aggregation protocol: %s", val); 78 79 strlcpy(ra.ra_ifname, name, sizeof(ra.ra_ifname)); 80 if (ioctl(s, SIOCSLAGG, &ra) != 0) 81 err(1, "SIOCSLAGG"); 82 } 83 84 static void 85 setlagghash(const char *val, int d, int s, const struct afswtch *afp) 86 { 87 struct lagg_reqflags rf; 88 char *str, *tmp, *tok; 89 90 91 rf.rf_flags = 0; 92 str = tmp = strdup(val); 93 while ((tok = strsep(&tmp, ",")) != NULL) { 94 if (strcmp(tok, "l2") == 0) 95 rf.rf_flags |= LAGG_F_HASHL2; 96 else if (strcmp(tok, "l3") == 0) 97 rf.rf_flags |= LAGG_F_HASHL3; 98 else if (strcmp(tok, "l4") == 0) 99 rf.rf_flags |= LAGG_F_HASHL4; 100 else { 101 free(str); 102 errx(1, "Invalid lagghash option: %s", tok); 103 } 104 } 105 free(str); 106 if (rf.rf_flags == 0) 107 errx(1, "No lagghash options supplied"); 108 109 strlcpy(rf.rf_ifname, name, sizeof(rf.rf_ifname)); 110 if (ioctl(s, SIOCSLAGGHASH, &rf)) 111 err(1, "SIOCSLAGGHASH"); 112 } 113 114 static char * 115 lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen) 116 { 117 snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X", 118 (int)mac[0], (int)mac[1], (int)mac[2], (int)mac[3], 119 (int)mac[4], (int)mac[5]); 120 121 return (buf); 122 } 123 124 static char * 125 lacp_format_peer(struct lacp_opreq *req, const char *sep) 126 { 127 char macbuf1[20]; 128 char macbuf2[20]; 129 130 snprintf(lacpbuf, sizeof(lacpbuf), 131 "[(%04X,%s,%04X,%04X,%04X),%s(%04X,%s,%04X,%04X,%04X)]", 132 req->actor_prio, 133 lacp_format_mac(req->actor_mac, macbuf1, sizeof(macbuf1)), 134 req->actor_key, req->actor_portprio, req->actor_portno, sep, 135 req->partner_prio, 136 lacp_format_mac(req->partner_mac, macbuf2, sizeof(macbuf2)), 137 req->partner_key, req->partner_portprio, req->partner_portno); 138 139 return(lacpbuf); 140 } 141 142 static void 143 lagg_status(int s) 144 { 145 struct lagg_protos lpr[] = LAGG_PROTOS; 146 struct lagg_reqport rp, rpbuf[LAGG_MAX_PORTS]; 147 struct lagg_reqall ra; 148 struct lagg_reqflags rf; 149 struct lacp_opreq *lp; 150 const char *proto = "<unknown>"; 151 int i, isport = 0; 152 153 bzero(&rp, sizeof(rp)); 154 bzero(&ra, sizeof(ra)); 155 156 strlcpy(rp.rp_ifname, name, sizeof(rp.rp_ifname)); 157 strlcpy(rp.rp_portname, name, sizeof(rp.rp_portname)); 158 159 if (ioctl(s, SIOCGLAGGPORT, &rp) == 0) 160 isport = 1; 161 162 strlcpy(ra.ra_ifname, name, sizeof(ra.ra_ifname)); 163 ra.ra_size = sizeof(rpbuf); 164 ra.ra_port = rpbuf; 165 166 strlcpy(rf.rf_ifname, name, sizeof(rf.rf_ifname)); 167 if (ioctl(s, SIOCGLAGGFLAGS, &rf) != 0) 168 rf.rf_flags = 0; 169 170 if (ioctl(s, SIOCGLAGG, &ra) == 0) { 171 lp = (struct lacp_opreq *)&ra.ra_lacpreq; 172 173 for (i = 0; i < (sizeof(lpr) / sizeof(lpr[0])); i++) { 174 if (ra.ra_proto == lpr[i].lpr_proto) { 175 proto = lpr[i].lpr_name; 176 break; 177 } 178 } 179 180 printf("\tlaggproto %s", proto); 181 if (rf.rf_flags & LAGG_F_HASHMASK) { 182 const char *sep = ""; 183 184 printf(" lagghash "); 185 if (rf.rf_flags & LAGG_F_HASHL2) { 186 printf("%sl2", sep); 187 sep = ","; 188 } 189 if (rf.rf_flags & LAGG_F_HASHL3) { 190 printf("%sl3", sep); 191 sep = ","; 192 } 193 if (rf.rf_flags & LAGG_F_HASHL4) { 194 printf("%sl4", sep); 195 sep = ","; 196 } 197 } 198 if (isport) 199 printf(" laggdev %s", rp.rp_ifname); 200 putchar('\n'); 201 if (verbose && ra.ra_proto == LAGG_PROTO_LACP) 202 printf("\tlag id: %s\n", 203 lacp_format_peer(lp, "\n\t\t ")); 204 205 for (i = 0; i < ra.ra_ports; i++) { 206 lp = (struct lacp_opreq *)&rpbuf[i].rp_lacpreq; 207 printf("\tlaggport: %s ", rpbuf[i].rp_portname); 208 printb("flags", rpbuf[i].rp_flags, LAGG_PORT_BITS); 209 if (verbose && ra.ra_proto == LAGG_PROTO_LACP) 210 printf(" state=%X", lp->actor_state); 211 putchar('\n'); 212 if (verbose && ra.ra_proto == LAGG_PROTO_LACP) 213 printf("\t\t%s\n", 214 lacp_format_peer(lp, "\n\t\t ")); 215 } 216 217 if (0 /* XXX */) { 218 printf("\tsupported aggregation protocols:\n"); 219 for (i = 0; i < (sizeof(lpr) / sizeof(lpr[0])); i++) 220 printf("\t\tlaggproto %s\n", lpr[i].lpr_name); 221 } 222 } 223 } 224 225 static struct cmd lagg_cmds[] = { 226 DEF_CMD_ARG("laggport", setlaggport), 227 DEF_CMD_ARG("-laggport", unsetlaggport), 228 DEF_CMD_ARG("laggproto", setlaggproto), 229 DEF_CMD_ARG("lagghash", setlagghash), 230 }; 231 static struct afswtch af_lagg = { 232 .af_name = "af_lagg", 233 .af_af = AF_UNSPEC, 234 .af_other_status = lagg_status, 235 }; 236 237 static __constructor void 238 lagg_ctor(void) 239 { 240 #define N(a) (sizeof(a) / sizeof(a[0])) 241 int i; 242 243 for (i = 0; i < N(lagg_cmds); i++) 244 cmd_register(&lagg_cmds[i]); 245 af_register(&af_lagg); 246 #undef N 247 } 248