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