kernel/nata: Extract ad_get_geometry().
[dragonfly.git] / sbin / ifconfig / iflagg.c
blobdf44f10d0e104a44e6899c56ddfc23df500197b7
1 /*-
2 */
4 #ifndef lint
5 static const char rcsid[] =
6 "$FreeBSD: head/sbin/ifconfig/iflagg.c 249897 2013-04-25 16:34:04Z glebius $";
7 #endif /* not lint */
9 #include <sys/param.h>
10 #include <sys/ioctl.h>
11 #include <sys/socket.h>
12 #include <sys/sockio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
17 #include <net/ethernet.h>
18 #include <net/if.h>
19 #include <net/lagg/if_lagg.h>
20 #include <net/route.h>
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>
30 #include "ifconfig.h"
32 char lacpbuf[120]; /* LACP peer '[(a,a,a),(p,p,p)]' */
34 static void
35 setlaggport(const char *val, int d, int s, const struct afswtch *afp)
37 struct lagg_reqport rp;
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));
43 /* Don't choke if the port is already in this lagg. */
44 if (ioctl(s, SIOCSLAGGPORT, &rp) && errno != EEXIST)
45 err(1, "SIOCSLAGGPORT");
48 static void
49 unsetlaggport(const char *val, int d, int s, const struct afswtch *afp)
51 struct lagg_reqport rp;
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));
57 if (ioctl(s, SIOCSLAGGDELPORT, &rp))
58 err(1, "SIOCSLAGGDELPORT");
61 static void
62 setlaggproto(const char *val, int d, int s, const struct afswtch *afp)
64 struct lagg_protos lpr[] = LAGG_PROTOS;
65 struct lagg_reqall ra;
66 int i;
68 bzero(&ra, sizeof(ra));
69 ra.ra_proto = LAGG_PROTO_MAX;
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;
77 if (ra.ra_proto == LAGG_PROTO_MAX)
78 errx(1, "Invalid aggregation protocol: %s", val);
80 strlcpy(ra.ra_ifname, name, sizeof(ra.ra_ifname));
81 if (ioctl(s, SIOCSLAGG, &ra) != 0)
82 err(1, "SIOCSLAGG");
85 static void
86 setlagghash(const char *val, int d, int s, const struct afswtch *afp)
88 struct lagg_reqflags rf;
89 char *str, *tmp, *tok;
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 errx(1, "Invalid lagghash option: %s", tok);
104 free(str);
105 if (rf.rf_flags == 0)
106 errx(1, "No lagghash options supplied");
108 strlcpy(rf.rf_ifname, name, sizeof(rf.rf_ifname));
109 if (ioctl(s, SIOCSLAGGHASH, &rf))
110 err(1, "SIOCSLAGGHASH");
113 static char *
114 lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen)
116 snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X",
117 (int)mac[0], (int)mac[1], (int)mac[2], (int)mac[3],
118 (int)mac[4], (int)mac[5]);
120 return (buf);
123 static char *
124 lacp_format_peer(struct lacp_opreq *req, const char *sep)
126 char macbuf1[20];
127 char macbuf2[20];
129 snprintf(lacpbuf, sizeof(lacpbuf),
130 "[(%04X,%s,%04X,%04X,%04X),%s(%04X,%s,%04X,%04X,%04X)]",
131 req->actor_prio,
132 lacp_format_mac(req->actor_mac, macbuf1, sizeof(macbuf1)),
133 req->actor_key, req->actor_portprio, req->actor_portno, sep,
134 req->partner_prio,
135 lacp_format_mac(req->partner_mac, macbuf2, sizeof(macbuf2)),
136 req->partner_key, req->partner_portprio, req->partner_portno);
138 return(lacpbuf);
141 static void
142 lagg_status(int s)
144 struct lagg_protos lpr[] = LAGG_PROTOS;
145 struct lagg_reqport rp, rpbuf[LAGG_MAX_PORTS];
146 struct lagg_reqall ra;
147 struct lagg_reqflags rf;
148 struct lacp_opreq *lp;
149 const char *proto = "<unknown>";
150 int i, isport = 0;
152 bzero(&rp, sizeof(rp));
153 bzero(&ra, sizeof(ra));
155 strlcpy(rp.rp_ifname, name, sizeof(rp.rp_ifname));
156 strlcpy(rp.rp_portname, name, sizeof(rp.rp_portname));
158 if (ioctl(s, SIOCGLAGGPORT, &rp) == 0)
159 isport = 1;
161 strlcpy(ra.ra_ifname, name, sizeof(ra.ra_ifname));
162 ra.ra_size = sizeof(rpbuf);
163 ra.ra_port = rpbuf;
165 strlcpy(rf.rf_ifname, name, sizeof(rf.rf_ifname));
166 if (ioctl(s, SIOCGLAGGFLAGS, &rf) != 0)
167 rf.rf_flags = 0;
169 if (ioctl(s, SIOCGLAGG, &ra) == 0) {
170 lp = (struct lacp_opreq *)&ra.ra_lacpreq;
172 for (i = 0; i < (sizeof(lpr) / sizeof(lpr[0])); i++) {
173 if (ra.ra_proto == lpr[i].lpr_proto) {
174 proto = lpr[i].lpr_name;
175 break;
179 printf("\tlaggproto %s", proto);
180 if (rf.rf_flags & LAGG_F_HASHMASK) {
181 const char *sep = "";
183 printf(" lagghash ");
184 if (rf.rf_flags & LAGG_F_HASHL2) {
185 printf("%sl2", sep);
186 sep = ",";
188 if (rf.rf_flags & LAGG_F_HASHL3) {
189 printf("%sl3", sep);
190 sep = ",";
192 if (rf.rf_flags & LAGG_F_HASHL4) {
193 printf("%sl4", sep);
194 sep = ",";
197 if (isport)
198 printf(" laggdev %s", rp.rp_ifname);
199 putchar('\n');
200 if (verbose && ra.ra_proto == LAGG_PROTO_LACP)
201 printf("\tlag id: %s\n",
202 lacp_format_peer(lp, "\n\t\t "));
204 for (i = 0; i < ra.ra_ports; i++) {
205 lp = (struct lacp_opreq *)&rpbuf[i].rp_lacpreq;
206 printf("\tlaggport: %s ", rpbuf[i].rp_portname);
207 printb("flags", rpbuf[i].rp_flags, LAGG_PORT_BITS);
208 if (verbose && ra.ra_proto == LAGG_PROTO_LACP)
209 printf(" state=%X", lp->actor_state);
210 putchar('\n');
211 if (verbose && ra.ra_proto == LAGG_PROTO_LACP)
212 printf("\t\t%s\n",
213 lacp_format_peer(lp, "\n\t\t "));
216 if (0 /* XXX */) {
217 printf("\tsupported aggregation protocols:\n");
218 for (i = 0; i < (sizeof(lpr) / sizeof(lpr[0])); i++)
219 printf("\t\tlaggproto %s\n", lpr[i].lpr_name);
224 static struct cmd lagg_cmds[] = {
225 DEF_CMD_ARG("laggport", setlaggport),
226 DEF_CMD_ARG("-laggport", unsetlaggport),
227 DEF_CMD_ARG("laggproto", setlaggproto),
228 DEF_CMD_ARG("lagghash", setlagghash),
230 static struct afswtch af_lagg = {
231 .af_name = "af_lagg",
232 .af_af = AF_UNSPEC,
233 .af_other_status = lagg_status,
236 static __constructor(101) void
237 lagg_ctor(void)
239 #define N(a) (sizeof(a) / sizeof(a[0]))
240 int i;
242 for (i = 0; i < N(lagg_cmds); i++)
243 cmd_register(&lagg_cmds[i]);
244 af_register(&af_lagg);
245 #undef N