xref: /netbsd/sys/net/agr/ieee8023ad_lacp_debug.c (revision 6550d01e)
1 /*	$NetBSD: ieee8023ad_lacp_debug.c,v 1.5 2008/12/16 22:35:38 christos Exp $	*/
2 
3 /*-
4  * Copyright (c)2005 YAMAMOTO Takashi,
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: ieee8023ad_lacp_debug.c,v 1.5 2008/12/16 22:35:38 christos Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/callout.h>
35 
36 #include <machine/stdarg.h>
37 
38 #include <net/if.h>
39 #include <net/if_ether.h>
40 
41 #include <net/agr/ieee8023_slowprotocols.h>
42 #include <net/agr/ieee8023_tlv.h>
43 #include <net/agr/ieee8023ad_lacp.h>
44 #include <net/agr/ieee8023ad_lacp_impl.h>
45 #include <net/agr/ieee8023ad_lacp_debug.h>
46 
47 #if defined(LACP_DEBUG)
48 int lacpdebug = 0;
49 #endif /* defined(LACP_DEBUG) */
50 
51 const char *
52 lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen)
53 {
54 
55 	snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X",
56 	    (int)mac[0],
57 	    (int)mac[1],
58 	    (int)mac[2],
59 	    (int)mac[3],
60 	    (int)mac[4],
61 	    (int)mac[5]);
62 
63 	return buf;
64 }
65 
66 const char *
67 lacp_format_systemid(const struct lacp_systemid *sysid,
68     char *buf, size_t buflen)
69 {
70 	char macbuf[LACP_MACSTR_MAX+1];
71 
72 	snprintf(buf, buflen, "%04X,%s",
73 	    be16toh(sysid->lsi_prio),
74 	    lacp_format_mac(sysid->lsi_mac, macbuf, sizeof(macbuf)));
75 
76 	return buf;
77 }
78 
79 const char *
80 lacp_format_portid(const struct lacp_portid *portid, char *buf, size_t buflen)
81 {
82 
83 	snprintf(buf, buflen, "%04X,%04X",
84 	    be16toh(portid->lpi_prio),
85 	    be16toh(portid->lpi_portno));
86 
87 	return buf;
88 }
89 
90 const char *
91 lacp_format_partner(const struct lacp_peerinfo *peer, char *buf, size_t buflen)
92 {
93 	char sysid[LACP_SYSTEMIDSTR_MAX+1];
94 	char portid[LACP_PORTIDSTR_MAX+1];
95 
96 	snprintf(buf, buflen, "(%s,%04X,%s)",
97 	    lacp_format_systemid(&peer->lip_systemid, sysid, sizeof(sysid)),
98 	    be16toh(peer->lip_key),
99 	    lacp_format_portid(&peer->lip_portid, portid, sizeof(portid)));
100 
101 	return buf;
102 }
103 
104 const char *
105 lacp_format_lagid(const struct lacp_peerinfo *a,
106     const struct lacp_peerinfo *b, char *buf, size_t buflen)
107 {
108 	char astr[LACP_PARTNERSTR_MAX+1];
109 	char bstr[LACP_PARTNERSTR_MAX+1];
110 
111 #if 0
112 	/*
113 	 * there's a convention to display small numbered peer
114 	 * in the left.
115 	 */
116 
117 	if (lacp_compare_peerinfo(a, b) > 0) {
118 		const struct lacp_peerinfo *t;
119 
120 		t = a;
121 		a = b;
122 		b = t;
123 	}
124 #endif
125 
126 	snprintf(buf, buflen, "[%s,%s]",
127 	    lacp_format_partner(a, astr, sizeof(astr)),
128 	    lacp_format_partner(b, bstr, sizeof(bstr)));
129 
130 	return buf;
131 }
132 
133 const char *
134 lacp_format_lagid_aggregator(const struct lacp_aggregator *la,
135     char *buf, size_t buflen)
136 {
137 
138 	if (la == NULL) {
139 		return "(none)";
140 	}
141 
142 	return lacp_format_lagid(&la->la_actor, &la->la_partner, buf, buflen);
143 }
144 
145 const char *
146 lacp_format_state(uint8_t state, char *buf, size_t buflen)
147 {
148 	static const char lacp_state_bits[] = LACP_STATE_BITS;
149 
150 	snprintb(buf, buflen, lacp_state_bits, state);
151 
152 	return buf;
153 }
154 
155 void
156 lacp_dump_lacpdu(const struct lacpdu *du)
157 {
158 	char buf[LACP_PARTNERSTR_MAX+1];
159 	char buf2[LACP_STATESTR_MAX+1];
160 
161 	printf("actor=%s\n",
162 	    lacp_format_partner(&du->ldu_actor, buf, sizeof(buf)));
163 	printf("actor.state=%s\n",
164 	    lacp_format_state(du->ldu_actor.lip_state, buf2, sizeof(buf2)));
165 	printf("partner=%s\n",
166 	    lacp_format_partner(&du->ldu_partner, buf, sizeof(buf)));
167 	printf("partner.state=%s\n",
168 	    lacp_format_state(du->ldu_partner.lip_state, buf2, sizeof(buf2)));
169 
170 	printf("maxdelay=%d\n", be16toh(du->ldu_collector.lci_maxdelay));
171 }
172 
173 #if defined(LACP_DEBUG)
174 #include <net/agr/if_agrvar_impl.h>
175 
176 void
177 lacp_dprintf(const struct lacp_port *lp, const char *fmt, ...)
178 {
179 	va_list va;
180 
181 	if (lacpdebug == 0) {
182 		return;
183 	}
184 
185 	if (lp) {
186 		printf("%s: ", lp->lp_agrport->port_ifp->if_xname);
187 	}
188 
189 	va_start(va, fmt);
190 	vprintf(fmt, va);
191 	va_end(va);
192 }
193 #endif
194