xref: /netbsd-src/sys/net/agr/ieee8023ad_lacp_debug.c (revision 39a10a70574fa76ae2a46ca6cfbced6f775d411b)
1 /*	$NetBSD: ieee8023ad_lacp_debug.c,v 1.7 2021/11/30 01:17:02 yamaguchi 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.7 2021/11/30 01:17:02 yamaguchi Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/callout.h>
35 
36 #include <net/if.h>
37 #include <net/if_ether.h>
38 #include <net/ether_slowprotocols.h>
39 
40 #include <net/agr/ieee8023_tlv.h>
41 #include <net/agr/ieee8023ad_lacp.h>
42 #include <net/agr/ieee8023ad_lacp_impl.h>
43 #include <net/agr/ieee8023ad_lacp_debug.h>
44 
45 #if defined(LACP_DEBUG)
46 int lacpdebug = 0;
47 #endif /* defined(LACP_DEBUG) */
48 
49 const char *
lacp_format_mac(const uint8_t * mac,char * buf,size_t buflen)50 lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen)
51 {
52 
53 	snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X",
54 	    (int)mac[0],
55 	    (int)mac[1],
56 	    (int)mac[2],
57 	    (int)mac[3],
58 	    (int)mac[4],
59 	    (int)mac[5]);
60 
61 	return buf;
62 }
63 
64 const char *
lacp_format_systemid(const struct lacp_systemid * sysid,char * buf,size_t buflen)65 lacp_format_systemid(const struct lacp_systemid *sysid,
66     char *buf, size_t buflen)
67 {
68 	char macbuf[LACP_MACSTR_MAX+1];
69 
70 	snprintf(buf, buflen, "%04X,%s",
71 	    be16toh(sysid->lsi_prio),
72 	    lacp_format_mac(sysid->lsi_mac, macbuf, sizeof(macbuf)));
73 
74 	return buf;
75 }
76 
77 const char *
lacp_format_portid(const struct lacp_portid * portid,char * buf,size_t buflen)78 lacp_format_portid(const struct lacp_portid *portid, char *buf, size_t buflen)
79 {
80 
81 	snprintf(buf, buflen, "%04X,%04X",
82 	    be16toh(portid->lpi_prio),
83 	    be16toh(portid->lpi_portno));
84 
85 	return buf;
86 }
87 
88 const char *
lacp_format_partner(const struct lacp_peerinfo * peer,char * buf,size_t buflen)89 lacp_format_partner(const struct lacp_peerinfo *peer, char *buf, size_t buflen)
90 {
91 	char sysid[LACP_SYSTEMIDSTR_MAX+1];
92 	char portid[LACP_PORTIDSTR_MAX+1];
93 
94 	snprintf(buf, buflen, "(%s,%04X,%s)",
95 	    lacp_format_systemid(&peer->lip_systemid, sysid, sizeof(sysid)),
96 	    be16toh(peer->lip_key),
97 	    lacp_format_portid(&peer->lip_portid, portid, sizeof(portid)));
98 
99 	return buf;
100 }
101 
102 const char *
lacp_format_lagid(const struct lacp_peerinfo * a,const struct lacp_peerinfo * b,char * buf,size_t buflen)103 lacp_format_lagid(const struct lacp_peerinfo *a,
104     const struct lacp_peerinfo *b, char *buf, size_t buflen)
105 {
106 	char astr[LACP_PARTNERSTR_MAX+1];
107 	char bstr[LACP_PARTNERSTR_MAX+1];
108 
109 #if 0
110 	/*
111 	 * there's a convention to display small numbered peer
112 	 * in the left.
113 	 */
114 
115 	if (lacp_compare_peerinfo(a, b) > 0) {
116 		const struct lacp_peerinfo *t;
117 
118 		t = a;
119 		a = b;
120 		b = t;
121 	}
122 #endif
123 
124 	snprintf(buf, buflen, "[%s,%s]",
125 	    lacp_format_partner(a, astr, sizeof(astr)),
126 	    lacp_format_partner(b, bstr, sizeof(bstr)));
127 
128 	return buf;
129 }
130 
131 const char *
lacp_format_lagid_aggregator(const struct lacp_aggregator * la,char * buf,size_t buflen)132 lacp_format_lagid_aggregator(const struct lacp_aggregator *la,
133     char *buf, size_t buflen)
134 {
135 
136 	if (la == NULL) {
137 		return "(none)";
138 	}
139 
140 	return lacp_format_lagid(&la->la_actor, &la->la_partner, buf, buflen);
141 }
142 
143 const char *
lacp_format_state(uint8_t state,char * buf,size_t buflen)144 lacp_format_state(uint8_t state, char *buf, size_t buflen)
145 {
146 	static const char lacp_state_bits[] = LACP_STATE_BITS;
147 
148 	snprintb(buf, buflen, lacp_state_bits, state);
149 
150 	return buf;
151 }
152 
153 void
lacp_dump_lacpdu(const struct lacpdu * du)154 lacp_dump_lacpdu(const struct lacpdu *du)
155 {
156 	char buf[LACP_PARTNERSTR_MAX+1];
157 	char buf2[LACP_STATESTR_MAX+1];
158 
159 	printf("actor=%s\n",
160 	    lacp_format_partner(&du->ldu_actor, buf, sizeof(buf)));
161 	printf("actor.state=%s\n",
162 	    lacp_format_state(du->ldu_actor.lip_state, buf2, sizeof(buf2)));
163 	printf("partner=%s\n",
164 	    lacp_format_partner(&du->ldu_partner, buf, sizeof(buf)));
165 	printf("partner.state=%s\n",
166 	    lacp_format_state(du->ldu_partner.lip_state, buf2, sizeof(buf2)));
167 
168 	printf("maxdelay=%d\n", be16toh(du->ldu_collector.lci_maxdelay));
169 }
170 
171 #if defined(LACP_DEBUG)
172 #include <net/agr/if_agrvar_impl.h>
173 
174 void
lacp_dprintf(const struct lacp_port * lp,const char * fmt,...)175 lacp_dprintf(const struct lacp_port *lp, const char *fmt, ...)
176 {
177 	va_list va;
178 
179 	if (lacpdebug == 0) {
180 		return;
181 	}
182 
183 	if (lp) {
184 		printf("%s: ", lp->lp_agrport->port_ifp->if_xname);
185 	}
186 
187 	va_start(va, fmt);
188 	vprintf(fmt, va);
189 	va_end(va);
190 }
191 #endif
192