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