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